Skip to content

Commit ab8cd6f

Browse files
authored
Merge pull request #9937 from douzzer/20260306-wc_Hash-refactor
20260306-wc_Hash-refactor
2 parents 051b83b + 2c26615 commit ab8cd6f

9 files changed

Lines changed: 739 additions & 386 deletions

File tree

configure.ac

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6082,8 +6082,10 @@ AS_CASE([$FIPS_VERSION],
60826082
[v6|ready|dev],[ # FIPS 140-3 SRTP-KDF
60836083
60846084
AS_IF([test "$FIPS_VERSION" = "dev"],
6085+
ENABLED_FIPS_DEV=yes
60856086
[AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_FIPS_DEV"])
60866087
AS_IF([test "$FIPS_VERSION" = "ready"],
6088+
ENABLED_FIPS_READY=yes
60876089
[AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_FIPS_READY"])
60886090
60896091
AM_CFLAGS="$AM_CFLAGS \
@@ -11310,6 +11312,19 @@ fi
1131011312
LIB_SOCKET_NSL
1131111313
AX_HARDEN_CC_COMPILER_FLAGS
1131211314
11315+
if test "$ENABLED_SELFTEST" = yes || test "$ENABLED_FIPS" = yes
11316+
then
11317+
if ! test "$ENABLED_FIPS_DEV" = yes && ! test "$ENABLED_FIPS_READY" = yes
11318+
then
11319+
# rsa.c wc_hash2mgf() switches on enum wc_HashType, which is defined
11320+
# outside the FIPS boundary. Unsupported hashes are correctly handled
11321+
# by the default clause.
11322+
AC_LANG_PUSH([C])
11323+
AX_APPEND_COMPILE_FLAGS([-Wno-switch-enum],,[$ax_append_compile_cflags_extra])
11324+
AC_LANG_POP
11325+
fi
11326+
fi
11327+
1131311328
# -Wdeprecated-enum-enum-conversion is on by default in C++20, but conflicts with
1131411329
# our use of enum constructs to define fungible constants.
1131511330
if test "$KERNEL_MODE_DEFAULTS" != "yes"

tests/api/test_hash.c

Lines changed: 65 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,35 @@ static const enum wc_HashType notCompiledHash[] = {
102102
WC_HASH_TYPE_SHA3_384,
103103
WC_HASH_TYPE_SHA3_512,
104104
#endif
105-
WC_HASH_TYPE_NONE /* Dummy value to ensure list is non-zero. */
106-
};
107-
static const int notCompiledHashLen = (sizeof(notCompiledHash) /
108-
sizeof(enum wc_HashType)) - 1;
109-
110-
static const enum wc_HashType notSupportedHash[] = {
111-
#if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
105+
#if !defined(WOLFSSL_SHA3) || !defined(WOLFSSL_SHAKE128)
112106
WC_HASH_TYPE_SHAKE128,
113107
#endif
114-
#if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
108+
#if !defined(WOLFSSL_SHA3) || !defined(WOLFSSL_SHAKE256)
115109
WC_HASH_TYPE_SHAKE256,
116110
#endif
111+
#if defined(NO_MD5) || defined(NO_SHA)
117112
WC_HASH_TYPE_MD5_SHA,
113+
#endif
114+
#ifndef WOLFSSL_MD2
118115
WC_HASH_TYPE_MD2,
116+
#endif
117+
#ifdef NO_MD4
119118
WC_HASH_TYPE_MD4,
119+
#endif
120+
#if !defined(HAVE_BLAKE2) && !defined(HAVE_BLAKE2S)
120121
WC_HASH_TYPE_BLAKE2B,
122+
#endif
123+
#if !defined(HAVE_BLAKE2) && !defined(HAVE_BLAKE2B)
121124
WC_HASH_TYPE_BLAKE2S,
122-
WC_HASH_TYPE_NONE
125+
#endif
126+
WC_HASH_TYPE_NONE /* Dummy value to ensure list is non-zero. */
127+
};
128+
static const int notCompiledHashLen = (sizeof(notCompiledHash) /
129+
sizeof(enum wc_HashType)) - 1;
130+
131+
static const int notSupportedHash[] = {
132+
WC_HASH_TYPE_NONE,
133+
WC_HASH_TYPE_MAX + 1
123134
};
124135
static const int notSupportedHashLen = (sizeof(notSupportedHash) /
125136
sizeof(enum wc_HashType));
@@ -156,18 +167,15 @@ static const enum wc_HashType sizeNotCompiledHash[] = {
156167
WC_HASH_TYPE_BLAKE2B,
157168
WC_HASH_TYPE_BLAKE2S,
158169
#endif
170+
WC_HASH_TYPE_SHAKE128,
171+
WC_HASH_TYPE_SHAKE256,
159172
WC_HASH_TYPE_NONE /* Dummy value to ensure list is non-zero. */
160173
};
161174
static const int sizeNotCompiledHashLen = (sizeof(sizeNotCompiledHash) /
162175
sizeof(enum wc_HashType)) - 1;
163-
static const enum wc_HashType sizeNotSupportedHash[] = {
164-
#if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
165-
WC_HASH_TYPE_SHAKE128,
166-
#endif
167-
#if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
168-
WC_HASH_TYPE_SHAKE256,
169-
#endif
170-
WC_HASH_TYPE_NONE
176+
static const int sizeNotSupportedHash[] = {
177+
WC_HASH_TYPE_NONE,
178+
WC_HASH_TYPE_MAX + 1
171179
};
172180
static const int sizeNotSupportedHashLen = (sizeof(sizeNotSupportedHash) /
173181
sizeof(enum wc_HashType));
@@ -214,19 +222,19 @@ int test_wc_HashInit(void)
214222

215223
for (i = 0; i < notSupportedHashLen; i++) {
216224
/* check for null ptr */
217-
ExpectIntEQ(wc_HashInit(NULL, notSupportedHash[i]),
225+
ExpectIntEQ(wc_HashInit(NULL, (enum wc_HashType)notSupportedHash[i]),
218226
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
219-
ExpectIntEQ(wc_HashInit_ex(NULL, notSupportedHash[i], HEAP_HINT,
227+
ExpectIntEQ(wc_HashInit_ex(NULL, (enum wc_HashType)notSupportedHash[i], HEAP_HINT,
220228
INVALID_DEVID), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
221229

222-
ExpectIntEQ(wc_HashInit(&hash, notSupportedHash[i]),
230+
ExpectIntEQ(wc_HashInit(&hash, (enum wc_HashType)notSupportedHash[i]),
223231
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
224-
wc_HashFree(&hash, notSupportedHash[i]);
225-
ExpectIntEQ(wc_HashInit_ex(&hash, notSupportedHash[i], HEAP_HINT,
232+
wc_HashFree(&hash, (enum wc_HashType)notSupportedHash[i]);
233+
ExpectIntEQ(wc_HashInit_ex(&hash, (enum wc_HashType)notSupportedHash[i], HEAP_HINT,
226234
INVALID_DEVID), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
227-
wc_HashFree(&hash, notSupportedHash[i]);
235+
wc_HashFree(&hash, (enum wc_HashType)notSupportedHash[i]);
228236

229-
wc_HashFree(NULL, notSupportedHash[i]);
237+
wc_HashFree(NULL, (enum wc_HashType)notSupportedHash[i]);
230238
} /* end of for loop */
231239

232240
#endif
@@ -282,25 +290,25 @@ int test_wc_HashUpdate(void)
282290
}
283291

284292
for (i = 0; i < notSupportedHashLen; i++) {
285-
ExpectIntEQ(wc_HashInit(&hash, notSupportedHash[i]),
293+
ExpectIntEQ(wc_HashInit(&hash, (enum wc_HashType)notSupportedHash[i]),
286294
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
287295

288296
/* Invalid parameters */
289-
ExpectIntEQ(wc_HashUpdate(NULL, notSupportedHash[i], NULL, 1),
297+
ExpectIntEQ(wc_HashUpdate(NULL, (enum wc_HashType)notSupportedHash[i], NULL, 1),
290298
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
291-
ExpectIntEQ(wc_HashUpdate(&hash, notSupportedHash[i], NULL, 1),
299+
ExpectIntEQ(wc_HashUpdate(&hash, (enum wc_HashType)notSupportedHash[i], NULL, 1),
292300
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
293-
ExpectIntEQ(wc_HashUpdate(NULL, notSupportedHash[i], NULL, 0),
301+
ExpectIntEQ(wc_HashUpdate(NULL, (enum wc_HashType)notSupportedHash[i], NULL, 0),
294302
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
295-
ExpectIntEQ(wc_HashUpdate(NULL, notSupportedHash[i], (byte*)"a", 1),
303+
ExpectIntEQ(wc_HashUpdate(NULL, (enum wc_HashType)notSupportedHash[i], (byte*)"a", 1),
296304
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
297305

298-
ExpectIntEQ(wc_HashUpdate(&hash, notSupportedHash[i], NULL, 0),
306+
ExpectIntEQ(wc_HashUpdate(&hash, (enum wc_HashType)notSupportedHash[i], NULL, 0),
299307
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
300-
ExpectIntEQ(wc_HashUpdate(&hash, notSupportedHash[i], (byte*)"a", 1),
308+
ExpectIntEQ(wc_HashUpdate(&hash, (enum wc_HashType)notSupportedHash[i], (byte*)"a", 1),
301309
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
302310

303-
wc_HashFree(&hash, notSupportedHash[i]);
311+
wc_HashFree(&hash, (enum wc_HashType)notSupportedHash[i]);
304312
}
305313

306314
#if defined(DEBUG_WOLFSSL) && !defined(NO_SHA256) && defined(WOLFSSL_SHA512)
@@ -357,21 +365,21 @@ int test_wc_HashFinal(void)
357365
}
358366

359367
for (i = 0; i < notSupportedHashLen; i++) {
360-
ExpectIntEQ(wc_HashInit(&hash, notSupportedHash[i]),
368+
ExpectIntEQ(wc_HashInit(&hash, (enum wc_HashType)notSupportedHash[i]),
361369
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
362370

363371
/* Invalid parameters */
364-
ExpectIntEQ(wc_HashFinal(NULL, notSupportedHash[i], NULL),
372+
ExpectIntEQ(wc_HashFinal(NULL, (enum wc_HashType)notSupportedHash[i], NULL),
365373
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
366-
ExpectIntEQ(wc_HashFinal(&hash, notSupportedHash[i], NULL),
374+
ExpectIntEQ(wc_HashFinal(&hash, (enum wc_HashType)notSupportedHash[i], NULL),
367375
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
368-
ExpectIntEQ(wc_HashFinal(NULL, notSupportedHash[i], digest),
376+
ExpectIntEQ(wc_HashFinal(NULL, (enum wc_HashType)notSupportedHash[i], digest),
369377
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
370378

371-
ExpectIntEQ(wc_HashFinal(&hash, notSupportedHash[i], digest),
379+
ExpectIntEQ(wc_HashFinal(&hash, (enum wc_HashType)notSupportedHash[i], digest),
372380
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
373381

374-
wc_HashFree(&hash, notSupportedHash[i]);
382+
wc_HashFree(&hash, (enum wc_HashType)notSupportedHash[i]);
375383
}
376384
#if defined(DEBUG_WOLFSSL) && !defined(NO_SHA256) && defined(WOLFSSL_SHA512)
377385
ExpectIntEQ(wc_HashInit(&hash, WC_HASH_TYPE_SHA256), 0);
@@ -417,7 +425,7 @@ int test_wc_HashNewDelete(void)
417425
}
418426

419427
for (i = 0; i < notSupportedHashLen; i++) {
420-
ExpectNull(wc_HashNew(notSupportedHash[i], HEAP_HINT, INVALID_DEVID,
428+
ExpectNull(wc_HashNew((enum wc_HashType)notSupportedHash[i], HEAP_HINT, INVALID_DEVID,
421429
&ret));
422430
ExpectIntEQ(ret, WC_NO_ERR_TRACE(BAD_FUNC_ARG));
423431
}
@@ -438,17 +446,13 @@ int test_wc_HashGetDigestSize(void)
438446
ExpectIntGT(wc_HashGetDigestSize(sizeSupportedHash[i]), 0);
439447
}
440448

441-
for (i = 0; i < notCompiledHashLen; i++) {
442-
ExpectIntEQ(wc_HashGetDigestSize(notCompiledHash[i]),
443-
WC_NO_ERR_TRACE(HASH_TYPE_E));
444-
}
445449
for (i = 0; i < sizeNotCompiledHashLen; i++) {
446450
ExpectIntEQ(wc_HashGetDigestSize(sizeNotCompiledHash[i]),
447451
WC_NO_ERR_TRACE(HASH_TYPE_E));
448452
}
449453

450454
for (i = 0; i < sizeNotSupportedHashLen; i++) {
451-
ExpectIntEQ(wc_HashGetDigestSize(sizeNotSupportedHash[i]),
455+
ExpectIntEQ(wc_HashGetDigestSize((enum wc_HashType)sizeNotSupportedHash[i]),
452456
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
453457
}
454458
#endif
@@ -468,17 +472,13 @@ int test_wc_HashGetBlockSize(void)
468472
ExpectIntGT(wc_HashGetBlockSize(sizeSupportedHash[i]), 0);
469473
}
470474

471-
for (i = 0; i < notCompiledHashLen; i++) {
472-
ExpectIntEQ(wc_HashGetBlockSize(notCompiledHash[i]),
473-
WC_NO_ERR_TRACE(HASH_TYPE_E));
474-
}
475475
for (i = 0; i < sizeNotCompiledHashLen; i++) {
476476
ExpectIntEQ(wc_HashGetBlockSize(sizeNotCompiledHash[i]),
477477
WC_NO_ERR_TRACE(HASH_TYPE_E));
478478
}
479479

480480
for (i = 0; i < sizeNotSupportedHashLen; i++) {
481-
ExpectIntEQ(wc_HashGetBlockSize(sizeNotSupportedHash[i]),
481+
ExpectIntEQ(wc_HashGetBlockSize((enum wc_HashType)sizeNotSupportedHash[i]),
482482
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
483483
}
484484
#endif
@@ -525,9 +525,9 @@ int test_wc_Hash(void)
525525
/* Algorithm only supported with wc_Hash() and wc_Hash_ex(). */
526526
continue;
527527
}
528-
ExpectIntEQ(wc_Hash(sizeNotSupportedHash[i], (byte*)"a", 1,
528+
ExpectIntEQ(wc_Hash((enum wc_HashType)sizeNotSupportedHash[i], (byte*)"a", 1,
529529
digest, sizeof(digest)), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
530-
ExpectIntEQ(wc_Hash_ex(sizeNotSupportedHash[i], (byte*)"a", 1,
530+
ExpectIntEQ(wc_Hash_ex((enum wc_HashType)sizeNotSupportedHash[i], (byte*)"a", 1,
531531
digest, sizeof(digest), HEAP_HINT, INVALID_DEVID),
532532
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
533533
}
@@ -570,11 +570,11 @@ int test_wc_HashSetFlags(void)
570570

571571
/* For loop to test not supported cases */
572572
for (i = 0; i < notSupportedHashLen; i++) {
573-
ExpectIntEQ(wc_HashInit(&hash, notSupportedHash[i]),
573+
ExpectIntEQ(wc_HashInit(&hash, (enum wc_HashType)notSupportedHash[i]),
574574
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
575-
ExpectIntEQ(wc_HashSetFlags(&hash, notSupportedHash[i], flags),
575+
ExpectIntEQ(wc_HashSetFlags(&hash, (enum wc_HashType)notSupportedHash[i], flags),
576576
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
577-
ExpectIntEQ(wc_HashFree(&hash, notSupportedHash[i]),
577+
ExpectIntEQ(wc_HashFree(&hash, (enum wc_HashType)notSupportedHash[i]),
578578
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
579579
}
580580
#endif
@@ -613,11 +613,11 @@ int test_wc_HashGetFlags(void)
613613

614614
/* For loop to test not supported cases */
615615
for (i = 0; i < notSupportedHashLen; i++) {
616-
ExpectIntEQ(wc_HashInit(&hash, notSupportedHash[i]),
616+
ExpectIntEQ(wc_HashInit(&hash, (enum wc_HashType)notSupportedHash[i]),
617617
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
618-
ExpectIntEQ(wc_HashGetFlags(&hash, notSupportedHash[i], &flags),
618+
ExpectIntEQ(wc_HashGetFlags(&hash, (enum wc_HashType)notSupportedHash[i], &flags),
619619
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
620-
ExpectIntEQ(wc_HashFree(&hash, notSupportedHash[i]),
620+
ExpectIntEQ(wc_HashFree(&hash, (enum wc_HashType)notSupportedHash[i]),
621621
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
622622
}
623623
#endif
@@ -682,7 +682,7 @@ int test_wc_HashGetOID(void)
682682
#ifdef WOLFSSL_MD2
683683
WC_HASH_TYPE_MD2,
684684
#endif
685-
#ifndef NO_MD5
685+
#if !defined(NO_MD5) && !defined(NO_SHA)
686686
WC_HASH_TYPE_MD5_SHA,
687687
#endif
688688
WC_HASH_TYPE_NONE /* Dummy value to ensure list is non-zero. */
@@ -696,15 +696,16 @@ int test_wc_HashGetOID(void)
696696
#ifdef NO_MD5
697697
WC_HASH_TYPE_MD5_SHA,
698698
#endif
699+
WC_HASH_TYPE_MD4,
700+
WC_HASH_TYPE_BLAKE2B,
701+
WC_HASH_TYPE_BLAKE2S,
699702
WC_HASH_TYPE_NONE /* Dummy value to ensure list is non-zero. */
700703
};
701704
static const int oidOnlyNotCompiledHashLen =
702705
(sizeof(oidOnlyNotCompiledHash) / sizeof(enum wc_HashType)) - 1;
703-
static const enum wc_HashType oidNotSupportedHash[] = {
704-
WC_HASH_TYPE_MD4,
705-
WC_HASH_TYPE_BLAKE2B,
706-
WC_HASH_TYPE_BLAKE2S,
707-
WC_HASH_TYPE_NONE
706+
static const int oidNotSupportedHash[] = {
707+
WC_HASH_TYPE_NONE,
708+
WC_HASH_TYPE_MAX + 1
708709
};
709710
static const int oidNotSupportedHashLen = (sizeof(oidNotSupportedHash) /
710711
sizeof(enum wc_HashType));
@@ -727,7 +728,7 @@ int test_wc_HashGetOID(void)
727728
}
728729

729730
for (i = 0; i < oidNotSupportedHashLen; i++) {
730-
ExpectIntEQ(wc_HashGetOID(oidNotSupportedHash[i]),
731+
ExpectIntEQ(wc_HashGetOID((enum wc_HashType)oidNotSupportedHash[i]),
731732
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
732733
}
733734
#endif

0 commit comments

Comments
 (0)