Skip to content

Commit 8e2d7c2

Browse files
committed
Add unit tests to exercise new code paths
1 parent 0ac5704 commit 8e2d7c2

14 files changed

Lines changed: 1095 additions & 69 deletions

src/wp_cmac.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,13 @@ static wp_CmacCtx* wp_cmac_dup(wp_CmacCtx* src)
165165
dst = wp_cmac_new(NULL);
166166
}
167167
if (dst != NULL) {
168+
/* Copy the entire context to preserve in-progress CMAC state. */
169+
XMEMCPY(&dst->cmac, &src->cmac, sizeof(Cmac));
168170
dst->type = src->type;
169171
dst->size = src->size;
170172
dst->expKeySize = src->expKeySize;
171-
172-
if ((src->keyLen != 0) &&
173-
(!wp_cmac_set_key(dst, src->key, src->keyLen, 1))) {
174-
wp_cmac_free(dst);
175-
dst = NULL;
176-
}
173+
XMEMCPY(dst->key, src->key, src->keyLen);
174+
dst->keyLen = src->keyLen;
177175
}
178176

179177
return dst;

src/wp_drbg.c

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,17 +337,41 @@ static int wp_drbg_reseed(wp_DrbgCtx* ctx, int predResist,
337337
const unsigned char* addIn, size_t addInLen)
338338
{
339339
int ok = 1;
340-
341340
int rc;
341+
unsigned char *seed = NULL;
342+
size_t seedLen = 0;
342343

343344
WOLFPROV_ENTER(WP_LOG_COMP_RNG, "wp_drbg_reseed");
344345

345-
rc = wc_RNG_DRBG_Reseed(ctx->rng, entropy, (word32)entropyLen);
346-
if (rc != 0) {
347-
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_COMP_RNG, "wc_RNG_DRBG_Reseed", rc);
348-
ok = 0;
346+
/* If no entropy provided, get fresh entropy from the OS source. */
347+
if (entropy == NULL || entropyLen == 0) {
348+
seedLen = 48;
349+
seed = OPENSSL_malloc(seedLen);
350+
if (seed == NULL) {
351+
ok = 0;
352+
}
353+
if (ok) {
354+
OS_Seed osSeed;
355+
rc = wc_GenerateSeed(&osSeed, seed, (word32)seedLen);
356+
if (rc != 0) {
357+
ok = 0;
358+
}
359+
else {
360+
entropy = seed;
361+
entropyLen = seedLen;
362+
}
363+
}
349364
}
350-
if (ok && (addInLen > 0)) {
365+
366+
if (ok && entropy != NULL && entropyLen > 0) {
367+
rc = wc_RNG_DRBG_Reseed(ctx->rng, entropy, (word32)entropyLen);
368+
if (rc != 0) {
369+
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_COMP_RNG,
370+
"wc_RNG_DRBG_Reseed", rc);
371+
ok = 0;
372+
}
373+
}
374+
if (ok && (addInLen > 0) && (addIn != NULL)) {
351375
rc = wc_RNG_DRBG_Reseed(ctx->rng, addIn, (word32)addInLen);
352376
if (rc != 0) {
353377
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_COMP_RNG,
@@ -356,6 +380,11 @@ static int wp_drbg_reseed(wp_DrbgCtx* ctx, int predResist,
356380
}
357381
}
358382

383+
/* Clear seed from parent if we obtained one. */
384+
if (seed != NULL && ctx->parentClearSeed != NULL) {
385+
ctx->parentClearSeed(ctx->parent, seed, seedLen);
386+
}
387+
359388
(void)predResist;
360389

361390
WOLFPROV_LEAVE(WP_LOG_COMP_RNG, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);

src/wp_ecx_kmgmt.c

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -365,50 +365,20 @@ static wp_Ecx* wp_ecx_dup(const wp_Ecx* src, int selection)
365365

366366
dst->includePublic = src->includePublic;
367367

368-
/* Copy public key if available and requested. */
369-
if (ok && src->hasPub &&
368+
/* Copy the key union directly to preserve all internal state. */
369+
XMEMCPY(&dst->key, &src->key, sizeof(src->key));
370+
371+
/* Copy public key flags if available and requested. */
372+
if (src->hasPub &&
370373
((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)) {
371-
byte buf[64];
372-
word32 len = (word32)sizeof(buf);
373-
int rc = (*src->data->exportPub)((void*)&src->key, buf, &len,
374-
ECX_LITTLE_ENDIAN);
375-
if (rc != 0) {
376-
ok = 0;
377-
}
378-
if (ok) {
379-
rc = (*dst->data->importPub)(buf, len, (void*)&dst->key,
380-
ECX_LITTLE_ENDIAN);
381-
if (rc != 0) {
382-
ok = 0;
383-
}
384-
}
385-
if (ok) {
386-
dst->hasPub = 1;
387-
}
374+
dst->hasPub = 1;
388375
}
389-
/* Copy private key if available and requested. */
390-
if (ok && src->hasPriv &&
376+
/* Copy private key flags if available and requested. */
377+
if (src->hasPriv &&
391378
((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)) {
392-
byte buf[64];
393-
word32 len = (word32)sizeof(buf);
394-
int rc = (*src->data->exportPriv)((void*)&src->key, buf, &len);
395-
if (rc != 0) {
396-
ok = 0;
397-
}
398-
if (ok) {
399-
rc = (*dst->data->importPriv)(buf, len, (void*)&dst->key,
400-
ECX_LITTLE_ENDIAN);
401-
if (rc != 0) {
402-
ok = 0;
403-
}
404-
}
405-
if (ok) {
406-
dst->hasPriv = 1;
407-
dst->clamped = src->clamped;
408-
XMEMCPY(dst->unclamped, src->unclamped,
409-
sizeof(src->unclamped));
410-
}
411-
wc_ForceZero(buf, len);
379+
dst->hasPriv = 1;
380+
dst->clamped = src->clamped;
381+
XMEMCPY(dst->unclamped, src->unclamped, sizeof(src->unclamped));
412382
}
413383

414384
if (!ok) {

src/wp_hmac.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ static wp_HmacCtx* wp_hmac_dup(wp_HmacCtx* src)
194194
dst->size = src->size;
195195
dst->provCtx = src->provCtx;
196196

197-
rc = wc_HmacCopy(&src->hmac, &dst->hmac);
198-
if (rc != 0) {
199-
ok = 0;
200-
}
197+
/* Copy the Hmac struct directly to preserve in-progress state.
198+
* wc_HmacCopy is not available in all wolfSSL versions. */
199+
XMEMCPY(&dst->hmac, &src->hmac, sizeof(Hmac));
200+
(void)rc;
201201

202202
if (ok && (src->key != NULL) &&
203203
(!wp_hmac_set_key(dst, src->key, src->keyLen, 0))) {

src/wp_mac_kmgmt.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,10 @@ static int wp_mac_has(const wp_Mac* mac, int selection)
319319
if (mac == NULL) {
320320
ok = 0;
321321
}
322+
if (ok && ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)) {
323+
/* MAC keys do not have a public key component. */
324+
ok = 0;
325+
}
322326
if (ok && ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)) {
323327
ok &= mac->key != NULL;
324328
}
@@ -345,11 +349,13 @@ static int wp_mac_match(const wp_Mac* mac1, const wp_Mac* mac2, int selection)
345349
if (!wolfssl_prov_is_running()) {
346350
ok = 0;
347351
}
348-
if (ok && ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) &&
349-
(mac1->keyLen != MAX_SIZE_T) && ((mac1->keyLen != mac2->keyLen) ||
350-
(CRYPTO_memcmp(mac1->key, mac2->key, mac1->keyLen) != 0) ||
351-
(XMEMCMP(mac1->cipher, mac2->cipher, WP_MAX_CIPH_NAME_SIZE) != 0))) {
352-
ok = 0;
352+
if (ok && ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)) {
353+
if ((mac1->keyLen == MAX_SIZE_T) || (mac2->keyLen == MAX_SIZE_T) ||
354+
(mac1->keyLen != mac2->keyLen) ||
355+
(CRYPTO_memcmp(mac1->key, mac2->key, mac1->keyLen) != 0) ||
356+
(XMEMCMP(mac1->cipher, mac2->cipher, WP_MAX_CIPH_NAME_SIZE) != 0)) {
357+
ok = 0;
358+
}
353359
}
354360

355361
WOLFPROV_LEAVE(WP_LOG_COMP_MAC, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);

src/wp_rsa_kmgmt.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -999,12 +999,34 @@ static int wp_rsa_get_params_pss(wp_RsaPssParams* pss, OSSL_PARAM params[])
999999
ok = 0;
10001000
}
10011001
}
1002-
/* MGF is default so don't set. */
1003-
if (ok && (pss->mgf != WP_RSA_PSS_MGF_DEF)) {
1002+
/* Always export MGF1 digest when requested. Translate wolfSSL-style
1003+
* digest names to OpenSSL-style names for interoperability. */
1004+
if (ok) {
10041005
p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_RSA_MGF1_DIGEST);
1005-
if ((p != NULL) &&
1006-
!OSSL_PARAM_set_utf8_string(p, pss->mgfMdName)) {
1007-
ok = 0;
1006+
if (p != NULL) {
1007+
const char* mgfName = NULL;
1008+
/* Convert mgf type to OpenSSL name via wp_digest_to_ossl_digest. */
1009+
if (pss->mgf != WP_RSA_PSS_MGF_DEF) {
1010+
enum wc_HashType mgfHash = WC_HASH_TYPE_NONE;
1011+
switch (pss->mgf) {
1012+
case WC_MGF1SHA256: mgfHash = WC_HASH_TYPE_SHA256; break;
1013+
case WC_MGF1SHA384: mgfHash = WC_HASH_TYPE_SHA384; break;
1014+
case WC_MGF1SHA512: mgfHash = WC_HASH_TYPE_SHA512; break;
1015+
default: break;
1016+
}
1017+
if (mgfHash != WC_HASH_TYPE_NONE) {
1018+
wp_digest_to_ossl_digest(mgfHash, &mgfName);
1019+
}
1020+
}
1021+
/* Fall back to signing digest if MGF1 not explicitly set. */
1022+
if (mgfName == NULL) {
1023+
if (!wp_digest_to_ossl_digest(pss->hashType, &mgfName)) {
1024+
mgfName = OSSL_DIGEST_NAME_SHA1;
1025+
}
1026+
}
1027+
if (!OSSL_PARAM_set_utf8_string(p, mgfName)) {
1028+
ok = 0;
1029+
}
10081030
}
10091031
}
10101032
if (ok) {
@@ -1608,6 +1630,7 @@ static wp_Rsa* wp_rsa_gen(wp_RsaGenCtx* ctx, OSSL_CALLBACK* cb, void* cbArg)
16081630
rsa->hasPub = 1;
16091631
rsa->hasPriv = 1;
16101632
rsa->pssParams = ctx->pssParams;
1633+
rsa->pssDefSet = ctx->pssDefSet;
16111634
break;
16121635
}
16131636
}

test/test_cmac.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,5 +358,114 @@ int test_cmac_multi_update(void *data)
358358
return err;
359359
}
360360

361+
int test_cmac_dup(void *data)
362+
{
363+
int ret = 0;
364+
EVP_MAC* emac = NULL;
365+
EVP_MAC_CTX* src = NULL;
366+
EVP_MAC_CTX* dup = NULL;
367+
OSSL_PARAM params[3];
368+
char cipher[] = "AES-256-CBC";
369+
unsigned char key[] = {
370+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
371+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
372+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
373+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
374+
};
375+
unsigned char prefix[] = "dup-prefix";
376+
unsigned char tailA[] = "-tail-a";
377+
unsigned char tailB[] = "-tail-b";
378+
unsigned char msgA[sizeof(prefix) + sizeof(tailA)];
379+
unsigned char msgB[sizeof(prefix) + sizeof(tailB)];
380+
unsigned char macA[16];
381+
unsigned char macB[16];
382+
unsigned char expA[16];
383+
unsigned char expB[16];
384+
size_t macASz = sizeof(macA);
385+
size_t macBSz = sizeof(macB);
386+
int expASz = sizeof(expA);
387+
int expBSz = sizeof(expB);
388+
389+
(void)data;
390+
391+
PRINT_MSG("Testing CMAC context dup");
392+
393+
/* Build full messages for one-shot expected MAC calculations. */
394+
memcpy(msgA, prefix, sizeof(prefix));
395+
memcpy(msgA + sizeof(prefix), tailA, sizeof(tailA));
396+
memcpy(msgB, prefix, sizeof(prefix));
397+
memcpy(msgB + sizeof(prefix), tailB, sizeof(tailB));
398+
399+
/* Compute expected MACs. */
400+
ret = test_cmac_gen_mac(wpLibCtx, cipher, key, (int)sizeof(key),
401+
msgA, (int)sizeof(msgA), expA, &expASz);
402+
if (ret != 0) {
403+
PRINT_MSG("Generate expected MAC A failed");
404+
}
405+
if (ret == 0) {
406+
ret = test_cmac_gen_mac(wpLibCtx, cipher, key, (int)sizeof(key),
407+
msgB, (int)sizeof(msgB), expB, &expBSz);
408+
if (ret != 0) {
409+
PRINT_MSG("Generate expected MAC B failed");
410+
}
411+
}
412+
413+
params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
414+
cipher, 0);
415+
params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
416+
(void*)key, sizeof(key));
417+
params[2] = OSSL_PARAM_construct_end();
418+
419+
if (ret == 0) {
420+
ret = (emac = EVP_MAC_fetch(wpLibCtx, "CMAC", NULL)) == NULL;
421+
}
422+
if (ret == 0) {
423+
ret = (src = EVP_MAC_CTX_new(emac)) == NULL;
424+
}
425+
if (ret == 0) {
426+
ret = EVP_MAC_CTX_set_params(src, params) != 1;
427+
}
428+
if (ret == 0) {
429+
ret = EVP_MAC_init(src, NULL, 0, NULL) != 1;
430+
}
431+
if (ret == 0) {
432+
ret = EVP_MAC_update(src, prefix, sizeof(prefix)) != 1;
433+
}
434+
/* Duplicate after partial update. */
435+
if (ret == 0) {
436+
ret = (dup = EVP_MAC_CTX_dup(src)) == NULL;
437+
}
438+
if (ret == 0) {
439+
ret = EVP_MAC_update(src, tailA, sizeof(tailA)) != 1;
440+
}
441+
if (ret == 0) {
442+
ret = EVP_MAC_update(dup, tailB, sizeof(tailB)) != 1;
443+
}
444+
if (ret == 0) {
445+
ret = EVP_MAC_final(src, macA, &macASz, sizeof(macA)) != 1;
446+
}
447+
if (ret == 0) {
448+
ret = EVP_MAC_final(dup, macB, &macBSz, sizeof(macB)) != 1;
449+
}
450+
if (ret == 0) {
451+
if ((macASz != (size_t)expASz) || (memcmp(macA, expA, macASz) != 0)) {
452+
PRINT_MSG("Duplicated source context MAC mismatch");
453+
ret = -1;
454+
}
455+
}
456+
if (ret == 0) {
457+
if ((macBSz != (size_t)expBSz) || (memcmp(macB, expB, macBSz) != 0)) {
458+
PRINT_MSG("Duplicated destination context MAC mismatch");
459+
ret = -1;
460+
}
461+
}
462+
463+
EVP_MAC_CTX_free(dup);
464+
EVP_MAC_CTX_free(src);
465+
EVP_MAC_free(emac);
466+
467+
return ret;
468+
}
469+
361470
#endif /* WP_HAVE_CMAC */
362471

0 commit comments

Comments
 (0)