Skip to content

Commit 9d60981

Browse files
Merge pull request #10695 from philljj/bsdkm_fixes
Bsdkm driver cleanup
2 parents 7aed5ac + c761755 commit 9d60981

2 files changed

Lines changed: 98 additions & 33 deletions

File tree

bsdkm/wolfkmod.c

Lines changed: 90 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -525,10 +525,14 @@ static int wolfkdriv_probesession(device_t dev,
525525

526526
softc = device_get_softc(dev);
527527

528+
/* sanitize csp values */
528529
switch (csp->csp_mode) {
529530
case CSP_MODE_CIPHER:
530531
switch (csp->csp_cipher_alg) {
531532
case CRYPTO_AES_CBC:
533+
if (csp->csp_ivlen != AES_BLOCK_LEN) {
534+
error = EINVAL;
535+
}
532536
break;
533537
default:
534538
error = EINVAL;
@@ -539,6 +543,16 @@ static int wolfkdriv_probesession(device_t dev,
539543
case CSP_MODE_AEAD:
540544
switch (csp->csp_cipher_alg) {
541545
case CRYPTO_AES_NIST_GCM_16:
546+
if (csp->csp_ivlen != GCM_NONCE_MIN_SZ &&
547+
csp->csp_ivlen != GCM_NONCE_MID_SZ &&
548+
csp->csp_ivlen != GCM_NONCE_MAX_SZ) {
549+
error = EINVAL;
550+
}
551+
552+
if (csp->csp_auth_mlen != 0 &&
553+
csp->csp_auth_mlen != WC_AES_BLOCK_SIZE) {
554+
error = EINVAL;
555+
}
542556
break;
543557
default:
544558
error = EINVAL;
@@ -553,7 +567,6 @@ static int wolfkdriv_probesession(device_t dev,
553567
}
554568

555569
(void)softc;
556-
(void)csp;
557570

558571
#if defined(WOLFSSL_BSDKM_VERBOSE_DEBUG)
559572
device_printf(dev, "info: probesession: mode=%d, cipher_alg=%d, error=%d\n",
@@ -569,44 +582,94 @@ static int wolfkdriv_newsession_aes(device_t dev,
569582
int error = 0;
570583
int klen = csp->csp_cipher_klen; /* key len in bytes */
571584

572-
switch (csp->csp_cipher_alg) {
573-
case CRYPTO_AES_NIST_GCM_16:
574-
session->type = CRYPTO_AES_NIST_GCM_16;
575-
break;
576-
case CRYPTO_AES_CBC:
577-
session->type = CRYPTO_AES_CBC;
578-
break;
579-
default:
580-
return (EOPNOTSUPP);
581-
}
582-
585+
/* sanitize csp values */
583586
if (klen != 16 && klen != 24 && klen != 32) {
584587
device_printf(dev, "info: newsession_cipher: invalid klen: %d\n", klen);
585588
return (EINVAL);
586589
}
587590

591+
if (csp->csp_cipher_alg == CRYPTO_AES_CBC) {
592+
if (csp->csp_ivlen != AES_BLOCK_LEN) {
593+
return (EINVAL);
594+
}
595+
}
596+
else if (csp->csp_cipher_alg == CRYPTO_AES_NIST_GCM_16) {
597+
if (csp->csp_ivlen != GCM_NONCE_MIN_SZ &&
598+
csp->csp_ivlen != GCM_NONCE_MID_SZ &&
599+
csp->csp_ivlen != GCM_NONCE_MAX_SZ) {
600+
return (EINVAL);
601+
}
602+
603+
if (csp->csp_auth_mlen != 0 &&
604+
csp->csp_auth_mlen != WC_AES_BLOCK_SIZE) {
605+
return (EINVAL);
606+
}
607+
}
608+
else {
609+
/* shouldn't happen, but just in case. */
610+
device_printf(dev, "error: newsession_cipher: unsupported alg: %d\n",
611+
csp->csp_cipher_alg);
612+
return (EINVAL);
613+
}
614+
588615
session->klen = klen;
589616
session->ivlen = csp->csp_ivlen;
590617

618+
memset(&session->aes_ctx.aes_encrypt, 0, sizeof(Aes));
619+
memset(&session->aes_ctx.aes_decrypt, 0, sizeof(Aes));
620+
591621
/* encrypt */
592622
error = wc_AesInit(&session->aes_ctx.aes_encrypt, NULL, INVALID_DEVID);
593623
if (error) {
594624
device_printf(dev, "error: newsession_cipher: aes init: %d\n", error);
595625
goto newsession_cipher_out;
596626
}
597627

598-
if (session->type == CRYPTO_AES_CBC) {
628+
switch (csp->csp_cipher_alg) {
629+
case CRYPTO_AES_NIST_GCM_16:
630+
session->type = CRYPTO_AES_NIST_GCM_16;
631+
error = wc_AesGcmSetKey(&session->aes_ctx.aes_encrypt,
632+
csp->csp_cipher_key,
633+
csp->csp_cipher_klen);
634+
if (error) {
635+
device_printf(dev, "error: wc_AesGcmSetKey: %d\n", error);
636+
goto newsession_cipher_out;
637+
}
638+
639+
break;
640+
case CRYPTO_AES_CBC:
641+
session->type = CRYPTO_AES_CBC;
599642
/* Need a separate decrypt structure for aes-cbc. */
600643
error = wc_AesInit(&session->aes_ctx.aes_decrypt, NULL, INVALID_DEVID);
601644
if (error) {
602645
device_printf(dev, "error: newsession_cipher: aes init: %d\n",
603646
error);
604647
goto newsession_cipher_out;
605648
}
649+
650+
error = wc_AesSetKey(&session->aes_ctx.aes_encrypt,
651+
csp->csp_cipher_key,
652+
csp->csp_cipher_klen, NULL, AES_ENCRYPTION);
653+
if (error) {
654+
device_printf(dev, "error: wc_AesSetKey: %d\n", error);
655+
goto newsession_cipher_out;
656+
}
657+
658+
error = wc_AesSetKey(&session->aes_ctx.aes_decrypt,
659+
csp->csp_cipher_key,
660+
csp->csp_cipher_klen, NULL, AES_DECRYPTION);
661+
if (error) {
662+
device_printf(dev, "error: wc_AesSetKey: %d\n", error);
663+
goto newsession_cipher_out;
664+
}
665+
666+
break;
667+
default:
668+
error = (EINVAL);
669+
break;
606670
}
607671

608672
newsession_cipher_out:
609-
610673
if (error != 0) {
611674
wolfkdriv_aes_ctx_clear(&session->aes_ctx);
612675
return (EINVAL);
@@ -683,7 +746,8 @@ static int wolfkdriv_cbc_work(device_t dev, wolfkdriv_session_t * session,
683746
size_t out_len = 0;
684747
int error = 0;
685748
int is_encrypt = 0;
686-
int type = AES_ENCRYPTION;
749+
750+
memset(&aes, 0, sizeof(aes));
687751

688752
if (csp->csp_cipher_alg != CRYPTO_AES_CBC) {
689753
error = EINVAL;
@@ -693,12 +757,10 @@ static int wolfkdriv_cbc_work(device_t dev, wolfkdriv_session_t * session,
693757
data_len = crp->crp_payload_length;
694758
if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
695759
is_encrypt = 1;
696-
type = AES_ENCRYPTION;
697760
memcpy(&aes, &session->aes_ctx.aes_encrypt, sizeof(aes));
698761
}
699762
else {
700763
is_encrypt = 0;
701-
type = AES_DECRYPTION;
702764
memcpy(&aes, &session->aes_ctx.aes_decrypt, sizeof(aes));
703765
}
704766

@@ -709,10 +771,9 @@ static int wolfkdriv_cbc_work(device_t dev, wolfkdriv_session_t * session,
709771
}
710772

711773
crypto_read_iv(crp, iv);
712-
error = wc_AesSetKey(&aes, csp->csp_cipher_key,
713-
csp->csp_cipher_klen, iv, type);
774+
error = wc_AesSetIV(&aes, iv);
714775
if (error) {
715-
device_printf(dev, "error: wc_AesSetKey: %d\n", error);
776+
device_printf(dev, "error: wc_AesSetIV: %d\n", error);
716777
goto cbc_work_out;
717778
}
718779

@@ -801,7 +862,7 @@ static int wolfkdriv_cbc_work(device_t dev, wolfkdriv_session_t * session,
801862

802863
cbc_work_out:
803864
/* cleanup. */
804-
wc_ForceZero(&aes, sizeof(aes));
865+
km_AesFree(&aes);
805866
wc_ForceZero(iv, sizeof(iv));
806867
wc_ForceZero(block, sizeof(block));
807868

@@ -838,13 +899,19 @@ static int wolfkdriv_gcm_work(device_t dev, wolfkdriv_session_t * session,
838899
int error = 0;
839900
int is_encrypt = 0;
840901

841-
memcpy(&aes, &session->aes_ctx.aes_encrypt, sizeof(aes));
902+
memset(&aes, 0, sizeof(aes));
842903

843904
if (csp->csp_cipher_alg != CRYPTO_AES_NIST_GCM_16) {
844905
error = EINVAL;
845906
goto gcm_work_out;
846907
}
847908

909+
memcpy(&aes, &session->aes_ctx.aes_encrypt, sizeof(aes));
910+
#if defined(WOLFSSL_AESGCM_STREAM) && defined(WOLFSSL_SMALL_STACK) && \
911+
!defined(WOLFSSL_AESNI)
912+
aes.streamData = NULL;
913+
#endif
914+
848915
data_len = crp->crp_payload_length;
849916
if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
850917
is_encrypt = 1;
@@ -853,13 +920,6 @@ static int wolfkdriv_gcm_work(device_t dev, wolfkdriv_session_t * session,
853920
is_encrypt = 0;
854921
}
855922

856-
error = wc_AesGcmSetKey(&aes, csp->csp_cipher_key,
857-
csp->csp_cipher_klen);
858-
if (error) {
859-
device_printf(dev, "error: wc_AesGcmSetKey: %d\n", error);
860-
goto gcm_work_out;
861-
}
862-
863923
crypto_read_iv(crp, iv);
864924
error = wc_AesGcmInit(&aes, NULL /* key */, 0 /* keylen */,
865925
iv, csp->csp_ivlen);
@@ -984,7 +1044,7 @@ static int wolfkdriv_gcm_work(device_t dev, wolfkdriv_session_t * session,
9841044

9851045
gcm_work_out:
9861046
/* cleanup. */
987-
wc_ForceZero(&aes, sizeof(aes));
1047+
km_AesFree(&aes);
9881048
wc_ForceZero(iv, sizeof(iv));
9891049
wc_ForceZero(auth_tag, sizeof(auth_tag));
9901050

bsdkm/wolfkmod_aes.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,12 @@ static int wolfkdriv_test_aes_cbc_big(device_t dev, int crid)
8383
goto test_aes_cbc_big_out;
8484
}
8585

86+
/* configure it.
87+
* note: CRYPTO_F_CBIFSYNC is required, or the callback may be deferred
88+
* to later, even if the session was sync. */
8689
crp->crp_callback = wolfkdriv_test_crp_callback;
8790
crp->crp_op = CRYPTO_OP_ENCRYPT;
88-
crp->crp_flags = CRYPTO_F_IV_SEPARATE;
91+
crp->crp_flags = CRYPTO_F_IV_SEPARATE | CRYPTO_F_CBIFSYNC;
8992

9093
memcpy(crp->crp_iv, iv, WC_AES_BLOCK_SIZE);
9194

@@ -266,10 +269,12 @@ static int wolfkdriv_test_aes_gcm(device_t dev, int crid)
266269
goto test_aes_gcm_out;
267270
}
268271

269-
/* configure it */
272+
/* configure it.
273+
* note: CRYPTO_F_CBIFSYNC is required, or the callback may be deferred
274+
* to later, even if the session was sync. */
270275
crp->crp_callback = wolfkdriv_test_crp_callback;
271276
crp->crp_op = (CRYPTO_OP_ENCRYPT | CRYPTO_OP_COMPUTE_DIGEST);
272-
crp->crp_flags = CRYPTO_F_IV_SEPARATE;
277+
crp->crp_flags = CRYPTO_F_IV_SEPARATE | CRYPTO_F_CBIFSYNC;
273278

274279
memcpy(crp->crp_iv, iv1, sizeof(iv1));
275280

0 commit comments

Comments
 (0)