Skip to content

Commit 14b30fd

Browse files
ravulapxvenkatesh6911
authored andcommitted
Fixed offload issue for qat hw aes-ccm and sm3 when modules removed
Signed-off-by: Tirupatigopi Ravulapalli <ravulapallix.tirupatigopi@intel.com>
1 parent f1cc5a0 commit 14b30fd

File tree

7 files changed

+142
-38
lines changed

7 files changed

+142
-38
lines changed

e_qat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ int fallback_to_openssl = 0;
182182
int qat_openssl3_prf_fallback = 0;
183183
int qat_openssl3_hkdf_fallback = 0;
184184
int qat_openssl3_sm2_fallback = 0;
185-
int qat_openssl3_sm3_fallback = 0;
186185
int qat_openssl3_sha_fallback = 0;
187186
#endif
187+
int qat_openssl3_sm3_fallback = 0;
188188
int fallback_to_qat_sw = 0; /* QAT HW initialize fail, offload to QAT SW. */
189189
int qat_hw_offload = 0;
190190
int qat_sw_offload = 0;
@@ -1379,7 +1379,7 @@ int bind_qat(ENGINE *e, const char *id)
13791379
INFO("QAT_SW SM3 for Provider Enabled\n");
13801380
# endif
13811381
# ifdef ENABLE_QAT_HW_CCM
1382-
qat_hw_aes_ccm_offload = 1;
1382+
if (qat_hw_aes_ccm_offload)
13831383
INFO("QAT_HW AES-CCM for Provider Enabled\n");
13841384
# endif
13851385
#endif

e_qat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,9 @@ extern int fallback_to_openssl;
405405
extern int qat_openssl3_prf_fallback;
406406
extern int qat_openssl3_hkdf_fallback;
407407
extern int qat_openssl3_sm2_fallback;
408-
extern int qat_openssl3_sm3_fallback;
409408
extern int qat_openssl3_sha_fallback;
410409
#endif
410+
extern int qat_openssl3_sm3_fallback;
411411
extern int fallback_to_qat_sw; /* QAT HW initialization fail, offload to QAT SW. */
412412
extern int qat_hw_offload;
413413
extern int qat_sw_offload;

qat_hw_ccm.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,12 @@ int qat_aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
15991599
return RET_FAIL;
16001600
}
16011601

1602+
if (qat_get_qat_offload_disabled()) {
1603+
DEBUG("- Switched to software mode\n");
1604+
fallback = 1;
1605+
goto end;
1606+
}
1607+
16021608
DEBUG("enc = %d - ctx = %p, out = %p, in = %p, len = %zu\n",
16031609
enc, (void *)ctx, (void *)out, (void *)in, len);
16041610

qat_hw_sm3.c

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,25 @@ int qat_hw_sm3_init(EVP_MD_CTX *ctx)
468468
return sts;
469469
}
470470
# endif
471+
#else
472+
if ((!qat_hw_sm3_offload) || (qat_get_qat_offload_disabled())) {
473+
QAT_SM3_CTX *qat_sm3_ctx = (QAT_SM3_CTX *) ctx;
474+
qat_sm3_ctx->sw_md_ctx = EVP_MD_CTX_new();
475+
if (qat_sm3_ctx->sw_md_ctx == NULL) {
476+
WARN("EVP_MD_CTX_new failed.\n");
477+
}
478+
qat_sm3_ctx->sw_md = EVP_MD_fetch(NULL, "sm3", "provider=default");
479+
if (qat_sm3_ctx->sw_md == NULL) {
480+
WARN("EVP_MD_fetch failed.\n");
481+
}
482+
if (!EVP_DigestInit_ex(qat_sm3_ctx->sw_md_ctx,
483+
qat_sm3_ctx->sw_md, NULL)) {
484+
WARN("Software calculate failed \n");
485+
return 0;
486+
}
487+
DEBUG("SW Init Finished %p\n", qat_sm3_ctx);
488+
return 1;
489+
}
471490
#endif
472491
return 1;
473492
}
@@ -514,13 +533,6 @@ int qat_hw_sm3_update(EVP_MD_CTX *ctx, const void *in, size_t len)
514533
QATerr(QAT_F_QAT_HW_SM3_UPDATE, QAT_R_INVALID_INPUT);
515534
return 0;
516535
}
517-
518-
# if defined(QAT_OPENSSL_3) && !defined(QAT_OPENSSL_PROVIDER)
519-
if (qat_openssl3_sm3_fallback == 1) {
520-
DEBUG("- Switched to software mode\n");
521-
goto fallback;
522-
}
523-
# endif
524536
#ifdef QAT_OPENSSL_PROVIDER
525537
qat_sm3_ctx = (QAT_SM3_CTX *)ctx;
526538
#else
@@ -532,6 +544,10 @@ int qat_hw_sm3_update(EVP_MD_CTX *ctx, const void *in, size_t len)
532544
return 0;
533545
}
534546

547+
if ((qat_openssl3_sm3_fallback == 1) || (!qat_hw_sm3_offload) || (qat_get_qat_offload_disabled())) {
548+
DEBUG("- Switched to software mode\n");
549+
goto fallback;
550+
}
535551
qat_sm3_ctx->rcv_count += len;
536552
DUMPL("sm3 hw update receive", in, len);
537553

@@ -599,12 +615,19 @@ int qat_hw_sm3_update(EVP_MD_CTX *ctx, const void *in, size_t len)
599615
#endif
600616
return 1;
601617

602-
# if defined(QAT_OPENSSL_3) && !defined(QAT_OPENSSL_PROVIDER)
603618
fallback:
619+
# if defined(QAT_OPENSSL_3) && !defined(QAT_OPENSSL_PROVIDER)
604620
sw_fn_ptr = EVP_MD_meth_get_update((EVP_MD *)EVP_sm3());
605621
sts = (*sw_fn_ptr)(ctx, in, len);
606622
DEBUG("SW Finished %p\n", ctx);
607623
return sts;
624+
# else
625+
if (!EVP_DigestUpdate(qat_sm3_ctx->sw_md_ctx, in, len)) {
626+
WARN("Software calculate failed \n");
627+
return 0;
628+
}
629+
DEBUG("SW Update Finished %p\n", qat_sm3_ctx);
630+
return 1;
608631
# endif
609632
}
610633

@@ -676,14 +699,6 @@ int qat_hw_sm3_final(EVP_MD_CTX *ctx, unsigned char *md)
676699
QATerr(QAT_F_QAT_HW_SM3_FINAL, QAT_R_INPUT_PARAM_INVALID);
677700
return 0;
678701
}
679-
680-
# if defined(QAT_OPENSSL_3) && !defined(QAT_OPENSSL_PROVIDER)
681-
if (qat_openssl3_sm3_fallback == 1) {
682-
DEBUG("- Switched to software mode\n");
683-
goto fallback;
684-
}
685-
# endif
686-
687702
#ifdef QAT_OPENSSL_PROVIDER
688703
qat_sm3_ctx = (QAT_SM3_CTX *)ctx;
689704
#else
@@ -694,6 +709,12 @@ int qat_hw_sm3_final(EVP_MD_CTX *ctx, unsigned char *md)
694709
QATerr(QAT_F_QAT_HW_SM3_FINAL, QAT_R_CTX_NULL);
695710
return 0;
696711
}
712+
713+
if ((qat_openssl3_sm3_fallback == 1) || (!qat_hw_sm3_offload) || (qat_get_qat_offload_disabled())) {
714+
DEBUG("- Switched to software mode\n");
715+
goto fallback;
716+
}
717+
697718
# ifndef ENABLE_QAT_SMALL_PKT_OFFLOAD
698719
if (qat_sm3_ctx->rcv_count <= CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD_HW_SM3) {
699720
/* Software calculation can start from init, because SPO threshold will
@@ -748,12 +769,19 @@ int qat_hw_sm3_final(EVP_MD_CTX *ctx, unsigned char *md)
748769

749770
return 1;
750771

751-
# if defined(QAT_OPENSSL_3) && !defined(QAT_OPENSSL_PROVIDER)
752772
fallback:
773+
# if defined(QAT_OPENSSL_3) && !defined(QAT_OPENSSL_PROVIDER)
753774
sw_fn_ptr = EVP_MD_meth_get_final((EVP_MD *)EVP_sm3());
754775
sts = (*sw_fn_ptr)(ctx, md);
755776
DEBUG("SW Finished %p\n", ctx);
756777
return sts;
778+
# else
779+
if (!EVP_DigestFinal_ex(qat_sm3_ctx->sw_md_ctx, md, NULL)) {
780+
WARN("Software calculate failed \n");
781+
return 0;
782+
}
783+
DEBUG("SW Final Finished %p\n", qat_sm3_ctx);
784+
return 1;
757785
# endif
758786
}
759787

qat_prov_aes_ccm.c

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,37 @@ QAT_EVP_CIPHER get_default_cipher_aes_ccm(int nid)
112112
return ccm_cipher;
113113
}
114114

115+
static int qat_aes_ccm_tls_init(QAT_PROV_CCM_CTX *ctx, unsigned char *aad, size_t alen)
116+
{
117+
size_t len;
118+
119+
if (!qat_prov_is_running() || alen != EVP_AEAD_TLS1_AAD_LEN)
120+
return 0;
121+
122+
/* Save the aad for later use. */
123+
memcpy(ctx->buf, aad, alen);
124+
ctx->tls_aad_len = alen;
125+
126+
len = ctx->buf[alen - 2] << 8 | ctx->buf[alen - 1];
127+
if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN)
128+
return 0;
129+
130+
/* Correct length for explicit iv. */
131+
len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
132+
133+
if (!ctx->enc) {
134+
if (len < ctx->M)
135+
return 0;
136+
/* Correct length for tag. */
137+
len -= ctx->M;
138+
}
139+
ctx->buf[alen - 2] = (unsigned char)(len >> 8);
140+
ctx->buf[alen - 1] = (unsigned char)(len & 0xff);
141+
142+
/* Extra padding: tag appended to record. */
143+
return ctx->M;
144+
}
145+
115146
static void *qat_aes_ccm_newctx(void *provctx, size_t keybits, int nid)
116147
{
117148
QAT_PROV_AES_CCM_CTX *ctx = NULL;
@@ -139,10 +170,12 @@ size_t qat_aes_ccm_get_ivlen(QAT_PROV_CCM_CTX * ctx)
139170
return QAT_AES_CCM_OP_VALUE - ctx->L;
140171
}
141172

142-
int qat_aes_ccm_einit(void *ctx, const unsigned char *inkey, size_t keylen,
143-
const unsigned char *iv, size_t ivlen, int enc)
173+
int qat_aes_ccm_einit(void *vctx, const unsigned char *inkey, size_t keylen,
174+
const unsigned char *iv, size_t ivlen, const OSSL_PARAM param[])
144175
{
145176
int sts = 0;
177+
QAT_PROV_CCM_CTX *ctx = (QAT_PROV_CCM_CTX *) vctx;
178+
ctx->enc = 1;
146179
# if !defined(QAT20_OOT) && !defined(QAT_HW_INTREE) \
147180
&& !defined(QAT_HW_FBSD_OOT) && !defined(QAT_HW_FBSD_INTREE)
148181
QAT_PROV_CCM_CTX *qctx = (QAT_PROV_CCM_CTX *) ctx;
@@ -162,16 +195,23 @@ int qat_aes_ccm_einit(void *ctx, const unsigned char *inkey, size_t keylen,
162195
return sts;
163196
}
164197
# endif
165-
if (qat_hw_aes_ccm_offload)
198+
if (qat_hw_aes_ccm_offload) {
166199
sts = qat_aes_ccm_init(ctx, inkey, keylen, iv, ivlen, 1);
200+
if (sts != 1) {
201+
QATerr(ERR_LIB_PROV, QAT_R_EINIT_OPERATION_FAILED);
202+
return sts;
203+
}
204+
}
167205

168-
return sts;
206+
return qat_aes_ccm_set_ctx_params(ctx, param);
169207
}
170208

171-
int qat_aes_ccm_dinit(void *ctx, const unsigned char *inkey, size_t keylen,
172-
const unsigned char *iv, size_t ivlen, int enc)
209+
int qat_aes_ccm_dinit(void *vctx, const unsigned char *inkey, size_t keylen,
210+
const unsigned char *iv, size_t ivlen, const OSSL_PARAM param[])
173211
{
174212
int sts = 0;
213+
QAT_PROV_CCM_CTX *ctx = (QAT_PROV_CCM_CTX *) vctx;
214+
ctx->enc = 0;
175215
# if !defined(QAT20_OOT) && !defined(QAT_HW_INTREE) \
176216
&& !defined(QAT_HW_FBSD_OOT) && !defined(QAT_HW_FBSD_INTREE)
177217
QAT_PROV_CCM_CTX *qctx = (QAT_PROV_CCM_CTX *) ctx;
@@ -192,10 +232,15 @@ int qat_aes_ccm_dinit(void *ctx, const unsigned char *inkey, size_t keylen,
192232
return sts;
193233
}
194234
# endif
195-
if (qat_hw_aes_ccm_offload)
235+
if (qat_hw_aes_ccm_offload) {
196236
sts = qat_aes_ccm_init(ctx, inkey, keylen, iv, ivlen, 0);
237+
if (sts != 1) {
238+
QATerr(ERR_LIB_PROV, QAT_R_DINIT_OPERATION_FAILED);
239+
return sts;
240+
}
241+
}
197242

198-
return sts;
243+
return qat_aes_ccm_set_ctx_params(ctx, param);
199244
}
200245

201246
int qat_aes_ccm_stream_update(void *vctx, unsigned char *out,
@@ -239,7 +284,10 @@ int qat_aes_ccm_stream_update(void *vctx, unsigned char *out,
239284
return 0;
240285
}
241286
}
242-
287+
else {
288+
/* Set *outl to NULL when offload is disabled to avoid garbage values and prevent errors. */
289+
*outl = 0;
290+
}
243291
return 1;
244292

245293
}
@@ -269,11 +317,12 @@ int qat_aes_ccm_stream_final(void *vctx, unsigned char *out,
269317
}
270318
# endif
271319

272-
if (qat_hw_aes_ccm_offload)
320+
if (qat_hw_aes_ccm_offload) {
273321
i = qat_aes_ccm_cipher(ctx, out, outl, outsize, NULL, 0);
274322

275-
if (i <= 0)
276-
return 0;
323+
if (i <= 0)
324+
return 0;
325+
}
277326

278327
*outl = 0;
279328
return 1;
@@ -486,6 +535,8 @@ int qat_aes_ccm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
486535
if (qat_hw_aes_ccm_offload)
487536
sz = qat_aes_ccm_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, p->data_size,
488537
p->data);
538+
else
539+
sz = qat_aes_ccm_tls_init(ctx, p->data, p->data_size);
489540

490541
if (sz == 0) {
491542
QATerr(ERR_LIB_PROV, QAT_R_INVALID_DATA);

qat_prov_aes_ccm.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ void qat_aes_ccm_init_ctx(void *provctx, QAT_PROV_CCM_CTX * ctx, size_t keybits,
234234
int qat_aes_ccm_get_ctx_params(void *vctx, OSSL_PARAM params[]);
235235
int qat_aes_ccm_set_ctx_params(void *vctx, const OSSL_PARAM params[]);
236236
int qat_aes_ccm_einit(void *ctx, const unsigned char *inkey, size_t keylen,
237-
const unsigned char *iv, size_t ivlen, int enc);
237+
const unsigned char *iv, size_t ivlen, const OSSL_PARAM params[]);
238238
int qat_aes_ccm_dinit(void *ctx, const unsigned char *inkey, size_t keylen,
239-
const unsigned char *iv, size_t ivlen, int enc);
239+
const unsigned char *iv, size_t ivlen, const OSSL_PARAM params[]);
240240
int qat_aes_ccm_stream_update(void *ctx, unsigned char *out,
241241
size_t *outl, size_t outsize,
242242
const unsigned char *in, size_t inl);

qat_prov_sm3.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,33 @@ static void *qat_sm3_newctx(void *prov_ctc)
122122
return ctx;
123123
}
124124

125+
# ifdef ENABLE_QAT_HW_SM3
126+
static void qat_sm3_free_sw_md_ctx(QAT_SM3_CTX *ctx)
127+
{
128+
EVP_MD_CTX_free(ctx->sw_md_ctx);
129+
EVP_MD_free(ctx->sw_md);
130+
ctx->sw_md_ctx = NULL;
131+
ctx->sw_md = NULL;
132+
}
133+
# endif
134+
125135
static void qat_sm3_freectx(void *vctx)
126136
{
127137
# ifdef ENABLE_QAT_HW_SM3
128138
QAT_SM3_CTX *ctx = (QAT_SM3_CTX *)vctx;
129139
if (!qat_hw_sm3_cleanup(ctx)){
130140
WARN("qat sm3 ctx cleanup failed.\n");
131141
}
142+
143+
if ((qat_hw_sm3_offload) && (!qat_get_qat_offload_disabled())) {
132144
# ifndef ENABLE_QAT_SMALL_PKT_OFFLOAD
133-
EVP_MD_CTX_free(ctx->sw_md_ctx);
134-
EVP_MD_free(ctx->sw_md);
135-
ctx->sw_md_ctx = NULL;
136-
ctx->sw_md = NULL;
145+
qat_sm3_free_sw_md_ctx(ctx);
137146
# endif
147+
} else {
148+
if (ctx->rcv_count == 0) {
149+
qat_sm3_free_sw_md_ctx(ctx);
150+
}
151+
}
138152
# endif
139153
# ifdef ENABLE_QAT_SW_SM3
140154
QAT_SM3_CTX_mb *ctx = (QAT_SM3_CTX_mb *)vctx;
@@ -155,6 +169,11 @@ static void *qat_sm3_dupctx(void *ctx)
155169
# endif
156170
if (ret != NULL)
157171
*ret = *in;
172+
# ifdef ENABLE_QAT_HW_SM3
173+
if ((!qat_hw_sm3_offload) || (qat_get_qat_offload_disabled())) {
174+
ret->rcv_count = 1;
175+
}
176+
# endif
158177
return ret;
159178
}
160179

0 commit comments

Comments
 (0)