Skip to content

Commit fb54c0a

Browse files
authored
Merge pull request #10734 from philljj/bsdkm_fixes
bsdkm: fenrir and misc cleanup.
2 parents 5a9a49d + cfdbeac commit fb54c0a

4 files changed

Lines changed: 51 additions & 19 deletions

File tree

bsdkm/bsdkm_wc_port.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ extern struct malloc_type M_WOLFSSL[1];
8181
#if defined(WOLFSSL_BSDKM_MEMORY_DEBUG)
8282
#define XMALLOC(s, h, t) ({ \
8383
(void)(h); (void)(t); \
84+
size_t _sz = (size_t)(s); \
8485
int _wait_flag = curthread->td_critnest == 0 ? M_WAITOK : M_NOWAIT; \
85-
void * _ptr = malloc((s), M_WOLFSSL, _wait_flag | M_ZERO); \
86-
printf("info: malloc: %p, M_WOLFSSL, %zu\n", _ptr, (size_t) s); \
86+
void * _ptr = malloc(_sz, M_WOLFSSL, _wait_flag | M_ZERO); \
87+
printf("info: malloc: %p, M_WOLFSSL, %zu\n", _ptr, _sz); \
8788
(void *)_ptr; \
8889
})
8990

bsdkm/wolfkmod.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ static int wolfkdriv_cbc_work(device_t dev, wolfkdriv_session_t * session,
830830
else {
831831
error = wc_AesCbcDecrypt(&aes, out_block, in_block, seg_len);
832832
if (error) {
833-
device_printf(dev, "error: wc_AesCbcEncrypt: %d\n", error);
833+
device_printf(dev, "error: wc_AesCbcDecrypt: %d\n", error);
834834
goto cbc_work_out;
835835
}
836836
}
@@ -930,7 +930,7 @@ static int wolfkdriv_gcm_work(device_t dev, wolfkdriv_session_t * session,
930930

931931
/* process aad first */
932932
if (crp->crp_aad != NULL) {
933-
/* they passed aad in separate buffer. */
933+
/* they passed aad in separate buffer. process it in one go. */
934934
if (is_encrypt) {
935935
error = wc_AesGcmEncryptUpdate(&aes, NULL, NULL, 0,
936936
crp->crp_aad, crp->crp_aad_length);
@@ -956,6 +956,13 @@ static int wolfkdriv_gcm_work(device_t dev, wolfkdriv_session_t * session,
956956
in_seg = crypto_cursor_segment(&cc_in, &in_len);
957957
seg_len = MIN(aad_len, in_len);
958958

959+
if (seg_len == 0) {
960+
/* the crypto_cursor logic should prevent this from happening,
961+
* but just in case. */
962+
error = EINVAL;
963+
goto gcm_work_out;
964+
}
965+
959966
if (is_encrypt) {
960967
error = wc_AesGcmEncryptUpdate(&aes, NULL, NULL, 0,
961968
in_seg, seg_len);
@@ -982,8 +989,6 @@ static int wolfkdriv_gcm_work(device_t dev, wolfkdriv_session_t * session,
982989
crypto_cursor_init(&cc_in, &crp->crp_buf);
983990
crypto_cursor_advance(&cc_in, crp->crp_payload_start);
984991

985-
in_seg = crypto_cursor_segment(&cc_in, &in_len);
986-
987992
/* handle if the user supplied a separate out buffer. */
988993
if (CRYPTO_HAS_OUTPUT_BUFFER(crp)) {
989994
crypto_cursor_init(&cc_out, &crp->crp_obuf);
@@ -993,27 +998,34 @@ static int wolfkdriv_gcm_work(device_t dev, wolfkdriv_session_t * session,
993998
cc_out = cc_in;
994999
}
9951000

996-
out_seg = crypto_cursor_segment(&cc_out, &out_len);
997-
9981001
while (data_len) {
9991002
/* process through the available segments. */
10001003
in_seg = crypto_cursor_segment(&cc_in, &in_len);
10011004
out_seg = crypto_cursor_segment(&cc_out, &out_len);
10021005
seg_len = MIN(data_len, MIN(in_len, out_len));
10031006

1007+
if (seg_len == 0) {
1008+
/* the crypto_cursor logic should prevent this from happening,
1009+
* but just in case. */
1010+
error = EINVAL;
1011+
goto gcm_work_out;
1012+
}
1013+
10041014
if (is_encrypt) {
10051015
error = wc_AesGcmEncryptUpdate(&aes, out_seg, in_seg, seg_len,
10061016
NULL, 0);
10071017
if (error) {
1008-
device_printf(dev, "error: wc_AesGcmEncrypt: %d\n", error);
1018+
device_printf(dev, "error: wc_AesGcmEncryptUpdate: %d\n",
1019+
error);
10091020
goto gcm_work_out;
10101021
}
10111022
}
10121023
else {
10131024
error = wc_AesGcmDecryptUpdate(&aes, out_seg, in_seg, seg_len,
10141025
NULL, 0);
10151026
if (error) {
1016-
device_printf(dev, "error: wc_AesGcmDecrypt: %d\n", error);
1027+
device_printf(dev, "error: wc_AesGcmDecryptUpdate: %d\n",
1028+
error);
10171029
goto gcm_work_out;
10181030
}
10191031
}

bsdkm/wolfkmod_aes.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static int wolfkdriv_test_aes_cbc_big(device_t dev, int crid)
2020
struct crypto_session_params csp;
2121
struct cryptop * crp = NULL;
2222
Aes * aes_encrypt = NULL;
23-
int error = 0;
23+
int error = -1;
2424
byte msg[] = {
2525
0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
2626
0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
@@ -50,7 +50,7 @@ static int wolfkdriv_test_aes_cbc_big(device_t dev, int crid)
5050

5151
error = wc_AesInit(aes_encrypt, NULL, INVALID_DEVID);
5252
if (error) {
53-
device_printf(dev, "error: newsession_cipher: aes init: %d\n", error);
53+
device_printf(dev, "error: wc_AesInit: %d\n", error);
5454
goto test_aes_cbc_big_out;
5555
}
5656

@@ -72,14 +72,20 @@ static int wolfkdriv_test_aes_cbc_big(device_t dev, int crid)
7272
csp.csp_ivlen = WC_AES_BLOCK_SIZE;
7373
csp.csp_cipher_key = key;
7474
csp.csp_cipher_klen = WC_AES_BLOCK_SIZE;
75+
76+
/* get crypto session handle */
7577
error = crypto_newsession(&session, &csp, crid);
7678
if (error || session == NULL) {
79+
device_printf(dev, "error: test_aes: crypto_newsession: %d, %p\n",
80+
error, (void *)session);
81+
error = ENOMEM;
7782
goto test_aes_cbc_big_out;
7883
}
7984

8085
crp = crypto_getreq(session, M_WAITOK);
8186
if (crp == NULL) {
8287
device_printf(dev, "error: test_aes: crypto_getreq failed\n");
88+
error = ENOMEM;
8389
goto test_aes_cbc_big_out;
8490
}
8591

@@ -121,6 +127,7 @@ static int wolfkdriv_test_aes_cbc_big(device_t dev, int crid)
121127
goto test_aes_cbc_big_out;
122128
}
123129

130+
device_printf(dev, "info: test_aes_cbc_big: passed\n");
124131
test_aes_cbc_big_out:
125132
#if defined(WOLFSSL_BSDKM_VERBOSE_DEBUG)
126133
device_printf(dev, "info: test_aes_cbc_big: error=%d, session=%p, crp=%p\n",
@@ -155,7 +162,7 @@ static int wolfkdriv_test_aes_gcm(device_t dev, int crid)
155162
struct crypto_session_params csp;
156163
struct cryptop * crp = NULL;
157164
Aes * enc = NULL;
158-
int error = 0;
165+
int error = -1;
159166

160167
WOLFSSL_SMALL_STACK_STATIC const byte p[] =
161168
{
@@ -226,6 +233,12 @@ static int wolfkdriv_test_aes_gcm(device_t dev, int crid)
226233
goto test_aes_gcm_out;
227234
}
228235

236+
error = wc_AesInit(enc, NULL, INVALID_DEVID);
237+
if (error) {
238+
device_printf(dev, "error: wc_AesInit: %d\n", error);
239+
goto test_aes_gcm_out;
240+
}
241+
229242
error = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
230243
if (error) { goto test_aes_gcm_out; }
231244

@@ -259,13 +272,15 @@ static int wolfkdriv_test_aes_gcm(device_t dev, int crid)
259272
if (error || session == NULL) {
260273
device_printf(dev, "error: test_aes: crypto_newsession: %d, %p\n",
261274
error, (void *)session);
275+
error = ENOMEM;
262276
goto test_aes_gcm_out;
263277
}
264278

265279
/* get a crypto op handle */
266280
crp = crypto_getreq(session, M_WAITOK);
267281
if (crp == NULL) {
268282
device_printf(dev, "error: test_aes: crypto_getreq failed\n");
283+
error = ENOMEM;
269284
goto test_aes_gcm_out;
270285
}
271286

@@ -309,6 +324,7 @@ static int wolfkdriv_test_aes_gcm(device_t dev, int crid)
309324
error = XMEMCMP(resultC2, p, sizeof(p));
310325
if (error) { goto test_aes_gcm_out; }
311326

327+
device_printf(dev, "info: test_aes_gcm: passed\n");
312328
test_aes_gcm_out:
313329
#if defined(WOLFSSL_BSDKM_VERBOSE_DEBUG)
314330
device_printf(dev, "info: test_aes_gcm: error=%d, session=%p, crp=%p\n",

bsdkm/x86_vecreg.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,25 @@ void wolfkmod_vecreg_exit(void)
107107
* Build with WOLFSSL_BSDKM_FPU_DEBUG to see verbose FPU logging.
108108
*/
109109
#if defined(WOLFSSL_BSDKM_FPU_DEBUG)
110-
#define wolfkmod_print_curthread(what) \
110+
#define wolfkmod_print_curthread(what) do { \
111111
printf("%s: cpuid = %d, curthread: td_tid = %d, pid = %d (%s), " \
112112
"td_critnest = %d, kernfpu = %02x\n", \
113113
(what), PCPU_GET(cpuid), curthread->td_tid, \
114114
curthread->td_proc ? curthread->td_proc->p_pid : -1, \
115115
curthread->td_proc ? curthread->td_proc->p_comm : "noproc", \
116116
curthread->td_critnest, \
117-
curthread->td_pcb->pcb_flags & PCB_KERNFPU);
117+
curthread->td_pcb->pcb_flags & PCB_KERNFPU); \
118+
} while (0)
118119

119-
#define wolfkmod_fpu_kern_enter() \
120+
#define wolfkmod_fpu_kern_enter() do { \
120121
wolfkmod_print_curthread("fpu_kern_enter"); \
121-
fpu_kern_enter(curthread, NULL, FPU_KERN_NOCTX);
122+
fpu_kern_enter(curthread, NULL, FPU_KERN_NOCTX); \
123+
} while (0)
122124

123-
#define wolfkmod_fpu_kern_leave() \
125+
#define wolfkmod_fpu_kern_leave() do { \
124126
wolfkmod_print_curthread("fpu_kern_leave"); \
125-
fpu_kern_leave(curthread, NULL);
127+
fpu_kern_leave(curthread, NULL); \
128+
} while (0)
126129
#else
127130
#define wolfkmod_fpu_kern_enter() \
128131
fpu_kern_enter(curthread, NULL, FPU_KERN_NOCTX);

0 commit comments

Comments
 (0)