Skip to content

Commit 4232f8f

Browse files
committed
Add unit tests to exercise new code paths
1 parent 4ae7cc0 commit 4232f8f

14 files changed

Lines changed: 1102 additions & 70 deletions

src/wp_cmac.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,13 @@ static wp_CmacCtx* wp_cmac_dup(wp_CmacCtx* src)
162162
dst = wp_cmac_new(NULL);
163163
}
164164
if (dst != NULL) {
165+
/* Copy the entire context to preserve in-progress CMAC state. */
166+
XMEMCPY(&dst->cmac, &src->cmac, sizeof(Cmac));
165167
dst->type = src->type;
166168
dst->size = src->size;
167169
dst->expKeySize = src->expKeySize;
168-
169-
if ((src->keyLen != 0) &&
170-
(!wp_cmac_set_key(dst, src->key, src->keyLen, 1))) {
171-
wp_cmac_free(dst);
172-
dst = NULL;
173-
}
170+
XMEMCPY(dst->key, src->key, src->keyLen);
171+
dst->keyLen = src->keyLen;
174172
}
175173

176174
return dst;

src/wp_drbg.c

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,17 +334,41 @@ static int wp_drbg_reseed(wp_DrbgCtx* ctx, int predResist,
334334
const unsigned char* addIn, size_t addInLen)
335335
{
336336
int ok = 1;
337-
338337
int rc;
338+
unsigned char *seed = NULL;
339+
size_t seedLen = 0;
339340

340341
WOLFPROV_ENTER(WP_LOG_COMP_RNG, "wp_drbg_reseed");
341342

342-
rc = wc_RNG_DRBG_Reseed(ctx->rng, entropy, (word32)entropyLen);
343-
if (rc != 0) {
344-
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_COMP_RNG, "wc_RNG_DRBG_Reseed", rc);
345-
ok = 0;
343+
/* If no entropy provided, get fresh entropy from the OS source. */
344+
if (entropy == NULL || entropyLen == 0) {
345+
seedLen = 48;
346+
seed = OPENSSL_malloc(seedLen);
347+
if (seed == NULL) {
348+
ok = 0;
349+
}
350+
if (ok) {
351+
OS_Seed osSeed;
352+
rc = wc_GenerateSeed(&osSeed, seed, (word32)seedLen);
353+
if (rc != 0) {
354+
ok = 0;
355+
}
356+
else {
357+
entropy = seed;
358+
entropyLen = seedLen;
359+
}
360+
}
346361
}
347-
if (ok && (addInLen > 0)) {
362+
363+
if (ok && entropy != NULL && entropyLen > 0) {
364+
rc = wc_RNG_DRBG_Reseed(ctx->rng, entropy, (word32)entropyLen);
365+
if (rc != 0) {
366+
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_COMP_RNG,
367+
"wc_RNG_DRBG_Reseed", rc);
368+
ok = 0;
369+
}
370+
}
371+
if (ok && (addInLen > 0) && (addIn != NULL)) {
348372
rc = wc_RNG_DRBG_Reseed(ctx->rng, addIn, (word32)addInLen);
349373
if (rc != 0) {
350374
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_COMP_RNG,
@@ -353,6 +377,11 @@ static int wp_drbg_reseed(wp_DrbgCtx* ctx, int predResist,
353377
}
354378
}
355379

380+
/* Clear seed from parent if we obtained one. */
381+
if (seed != NULL && ctx->parentClearSeed != NULL) {
382+
ctx->parentClearSeed(ctx->parent, seed, seedLen);
383+
}
384+
356385
(void)predResist;
357386

358387
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
@@ -998,12 +998,34 @@ static int wp_rsa_get_params_pss(wp_RsaPssParams* pss, OSSL_PARAM params[])
998998
ok = 0;
999999
}
10001000
}
1001-
/* MGF is default so don't set. */
1002-
if (ok && (pss->mgf != WP_RSA_PSS_MGF_DEF)) {
1001+
/* Always export MGF1 digest when requested. Translate wolfSSL-style
1002+
* digest names to OpenSSL-style names for interoperability. */
1003+
if (ok) {
10031004
p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_RSA_MGF1_DIGEST);
1004-
if ((p != NULL) &&
1005-
!OSSL_PARAM_set_utf8_string(p, pss->mgfMdName)) {
1006-
ok = 0;
1005+
if (p != NULL) {
1006+
const char* mgfName = NULL;
1007+
/* Convert mgf type to OpenSSL name via wp_digest_to_ossl_digest. */
1008+
if (pss->mgf != WP_RSA_PSS_MGF_DEF) {
1009+
enum wc_HashType mgfHash = WC_HASH_TYPE_NONE;
1010+
switch (pss->mgf) {
1011+
case WC_MGF1SHA256: mgfHash = WC_HASH_TYPE_SHA256; break;
1012+
case WC_MGF1SHA384: mgfHash = WC_HASH_TYPE_SHA384; break;
1013+
case WC_MGF1SHA512: mgfHash = WC_HASH_TYPE_SHA512; break;
1014+
default: break;
1015+
}
1016+
if (mgfHash != WC_HASH_TYPE_NONE) {
1017+
wp_digest_to_ossl_digest(mgfHash, &mgfName);
1018+
}
1019+
}
1020+
/* Fall back to signing digest if MGF1 not explicitly set. */
1021+
if (mgfName == NULL) {
1022+
if (!wp_digest_to_ossl_digest(pss->hashType, &mgfName)) {
1023+
mgfName = OSSL_DIGEST_NAME_SHA1;
1024+
}
1025+
}
1026+
if (!OSSL_PARAM_set_utf8_string(p, mgfName)) {
1027+
ok = 0;
1028+
}
10071029
}
10081030
}
10091031
if (ok) {
@@ -1607,6 +1629,7 @@ static wp_Rsa* wp_rsa_gen(wp_RsaGenCtx* ctx, OSSL_CALLBACK* cb, void* cbArg)
16071629
rsa->hasPub = 1;
16081630
rsa->hasPriv = 1;
16091631
rsa->pssParams = ctx->pssParams;
1632+
rsa->pssDefSet = ctx->pssDefSet;
16101633
break;
16111634
}
16121635
}

test/test_cmac.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,5 +257,114 @@ int test_cmac_create(void *data)
257257
return ret;
258258
}
259259

260+
int test_cmac_dup(void *data)
261+
{
262+
int ret = 0;
263+
EVP_MAC* emac = NULL;
264+
EVP_MAC_CTX* src = NULL;
265+
EVP_MAC_CTX* dup = NULL;
266+
OSSL_PARAM params[3];
267+
char cipher[] = "AES-256-CBC";
268+
unsigned char key[] = {
269+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
270+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
271+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
272+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
273+
};
274+
unsigned char prefix[] = "dup-prefix";
275+
unsigned char tailA[] = "-tail-a";
276+
unsigned char tailB[] = "-tail-b";
277+
unsigned char msgA[sizeof(prefix) + sizeof(tailA)];
278+
unsigned char msgB[sizeof(prefix) + sizeof(tailB)];
279+
unsigned char macA[16];
280+
unsigned char macB[16];
281+
unsigned char expA[16];
282+
unsigned char expB[16];
283+
size_t macASz = sizeof(macA);
284+
size_t macBSz = sizeof(macB);
285+
int expASz = sizeof(expA);
286+
int expBSz = sizeof(expB);
287+
288+
(void)data;
289+
290+
PRINT_MSG("Testing CMAC context dup");
291+
292+
/* Build full messages for one-shot expected MAC calculations. */
293+
memcpy(msgA, prefix, sizeof(prefix));
294+
memcpy(msgA + sizeof(prefix), tailA, sizeof(tailA));
295+
memcpy(msgB, prefix, sizeof(prefix));
296+
memcpy(msgB + sizeof(prefix), tailB, sizeof(tailB));
297+
298+
/* Compute expected MACs. */
299+
ret = test_cmac_gen_mac(wpLibCtx, cipher, key, (int)sizeof(key),
300+
msgA, (int)sizeof(msgA), expA, &expASz);
301+
if (ret != 0) {
302+
PRINT_MSG("Generate expected MAC A failed");
303+
}
304+
if (ret == 0) {
305+
ret = test_cmac_gen_mac(wpLibCtx, cipher, key, (int)sizeof(key),
306+
msgB, (int)sizeof(msgB), expB, &expBSz);
307+
if (ret != 0) {
308+
PRINT_MSG("Generate expected MAC B failed");
309+
}
310+
}
311+
312+
params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
313+
cipher, 0);
314+
params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
315+
(void*)key, sizeof(key));
316+
params[2] = OSSL_PARAM_construct_end();
317+
318+
if (ret == 0) {
319+
ret = (emac = EVP_MAC_fetch(wpLibCtx, "CMAC", NULL)) == NULL;
320+
}
321+
if (ret == 0) {
322+
ret = (src = EVP_MAC_CTX_new(emac)) == NULL;
323+
}
324+
if (ret == 0) {
325+
ret = EVP_MAC_CTX_set_params(src, params) != 1;
326+
}
327+
if (ret == 0) {
328+
ret = EVP_MAC_init(src, NULL, 0, NULL) != 1;
329+
}
330+
if (ret == 0) {
331+
ret = EVP_MAC_update(src, prefix, sizeof(prefix)) != 1;
332+
}
333+
/* Duplicate after partial update. */
334+
if (ret == 0) {
335+
ret = (dup = EVP_MAC_CTX_dup(src)) == NULL;
336+
}
337+
if (ret == 0) {
338+
ret = EVP_MAC_update(src, tailA, sizeof(tailA)) != 1;
339+
}
340+
if (ret == 0) {
341+
ret = EVP_MAC_update(dup, tailB, sizeof(tailB)) != 1;
342+
}
343+
if (ret == 0) {
344+
ret = EVP_MAC_final(src, macA, &macASz, sizeof(macA)) != 1;
345+
}
346+
if (ret == 0) {
347+
ret = EVP_MAC_final(dup, macB, &macBSz, sizeof(macB)) != 1;
348+
}
349+
if (ret == 0) {
350+
if ((macASz != (size_t)expASz) || (memcmp(macA, expA, macASz) != 0)) {
351+
PRINT_MSG("Duplicated source context MAC mismatch");
352+
ret = -1;
353+
}
354+
}
355+
if (ret == 0) {
356+
if ((macBSz != (size_t)expBSz) || (memcmp(macB, expB, macBSz) != 0)) {
357+
PRINT_MSG("Duplicated destination context MAC mismatch");
358+
ret = -1;
359+
}
360+
}
361+
362+
EVP_MAC_CTX_free(dup);
363+
EVP_MAC_CTX_free(src);
364+
EVP_MAC_free(emac);
365+
366+
return ret;
367+
}
368+
260369
#endif /* WP_HAVE_CMAC */
261370

0 commit comments

Comments
 (0)