Skip to content

Commit bd5eead

Browse files
authored
Merge pull request #11359 from jackctj117/PQ-FIPS-Fenrir
Pq fips fenrir
2 parents 5070676 + 095e018 commit bd5eead

6 files changed

Lines changed: 165 additions & 11 deletions

File tree

‎tests/api/test_ecc.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,15 @@ int test_wc_ecc_make_key(void)
299299
ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL),
300300
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
301301

302+
#if FIPS_VERSION3_GE(7,0,0)
303+
/* v7 refuses key generation on a named curve below 224 bits even when
304+
* the requested key size is large enough. */
305+
wc_ecc_free(&key);
306+
ExpectIntEQ(wc_ecc_init(&key), 0);
307+
ExpectIntEQ(wc_ecc_make_key_ex(&rng, 32, &key, ECC_SECP192R1),
308+
WC_NO_ERR_TRACE(ECC_CURVE_OID_E));
309+
#endif
310+
302311
DoExpectIntEQ(wc_FreeRng(&rng), 0);
303312
wc_ecc_free(&key);
304313

@@ -600,6 +609,23 @@ int test_wc_ecc_signVerify_hash(void)
600609
#endif
601610
#endif /* HAVE_ECC_VERIFY */
602611

612+
#if FIPS_VERSION3_GE(7,0,0) && \
613+
(defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && \
614+
(ECC_MIN_KEY_SZ <= 192)
615+
/* v7 refuses to sign on a curve below 224 bits. Key generation there is
616+
* refused too, so import a P-192 key (d = 1, Q = G). */
617+
wc_ecc_free(&key);
618+
ExpectIntEQ(wc_ecc_init(&key), 0);
619+
ExpectIntEQ(wc_ecc_import_raw_ex(&key,
620+
"188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012",
621+
"07192B95FFC8DA78631011ED6B24CDD573F977A11E794811",
622+
"000000000000000000000000000000000000000000000001",
623+
ECC_SECP192R1), 0);
624+
siglen = ECC_BUFSIZE;
625+
ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
626+
WC_NO_ERR_TRACE(SIG_TYPE_E));
627+
#endif
628+
603629
DoExpectIntEQ(wc_FreeRng(&rng), 0);
604630
wc_ecc_free(&key);
605631

‎wolfcrypt/src/ecc.c‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6535,6 +6535,27 @@ int wc_ecc_make_key_ex2(WC_RNG* rng, int keysize, ecc_key* key, int curve_id,
65356535
{
65366536
int err;
65376537

6538+
#if FIPS_VERSION3_GE(7,0,0)
6539+
/* SP 800-131A Rev. 2 Table 2 disallows ECDSA key pair generation
6540+
* providing fewer than 112 bits of security strength (len(n) < 224), and
6541+
* SP 800-186 Section 3.2.1.1 retains P-192 for legacy use only, so
6542+
* refuse to generate on any curve smaller than 224 bits. The bound
6543+
* mirrors wc_ecc_set_curve(): an explicit curve_id selects the curve
6544+
* directly, otherwise keysize selects the smallest compiled curve that
6545+
* fits, so keysize 0 would land on the smallest compiled curve and is
6546+
* rejected too. Signature verification with the small curves stays
6547+
* available (legacy use per SP 800-131A Rev. 2 Table 2). */
6548+
if (curve_id > ECC_CURVE_DEF) {
6549+
if (wc_ecc_get_curve_size_from_id(curve_id) < WC_ECC_FIPS_GEN_MIN) {
6550+
return ECC_CURVE_OID_E;
6551+
}
6552+
}
6553+
else if (keysize < WC_ECC_FIPS_GEN_MIN) {
6554+
/* Same code the fips.c keygen wrappers return for this case. */
6555+
return BAD_FUNC_ARG;
6556+
}
6557+
#endif
6558+
65386559
err = _ecc_make_key_ex(rng, keysize, key, curve_id, flags);
65396560

65406561
#if (FIPS_VERSION_GE(5,0) || defined(WOLFSSL_VALIDATE_ECC_KEYGEN)) && \
@@ -7232,6 +7253,25 @@ static int wc_ecc_sign_hash_async(const byte* in, word32 inlen, byte* out,
72327253
}
72337254
#endif /* WOLFSSL_ASYNC_CRYPT && WC_ASYNC_ENABLE_ECC */
72347255

7256+
#if FIPS_VERSION3_GE(7,0,0)
7257+
/* SP 800-131A Rev. 2 Table 2: ECDSA digital signature generation with
7258+
* len(n) < 224 is disallowed, while signature verification with those
7259+
* curves remains legacy use, so only the signing paths are gated. Every
7260+
* signing entry point calls this ahead of any crypto-callback or hardware
7261+
* dispatch, so no backend signs on a too-small curve. The curve is resolved
7262+
* from key->dp, never from ecc_sets[key->idx], so a custom-curve key
7263+
* (idx == ECC_CUSTOM_IDX) cannot index out of range. A key with no curve
7264+
* set is left to the caller's own argument checks; wc_ecc_sign_hash()
7265+
* checks again after a crypto callback has had the chance to resolve it. */
7266+
static int ecc_fips_sign_curve_allowed(const ecc_key* key)
7267+
{
7268+
if ((key->dp != NULL) && (key->dp->size < WC_ECC_FIPS_GEN_MIN)) {
7269+
return SIG_TYPE_E;
7270+
}
7271+
return 0;
7272+
}
7273+
#endif /* FIPS_VERSION3_GE(7,0,0) */
7274+
72357275
/**
72367276
Sign a message digest
72377277
in The message digest to sign
@@ -7267,14 +7307,33 @@ int wc_ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen,
72677307
return BAD_LENGTH_E;
72687308
}
72697309

7310+
#if FIPS_VERSION3_GE(7,0,0)
7311+
err = ecc_fips_sign_curve_allowed(key);
7312+
if (err != 0) {
7313+
return err;
7314+
}
7315+
#endif
7316+
72707317
#ifdef WOLF_CRYPTO_CB
72717318
#ifndef WOLF_CRYPTO_CB_FIND
72727319
if (key->devId != INVALID_DEVID)
72737320
#endif
72747321
{
72757322
err = wc_CryptoCb_EccSign(in, inlen, out, outlen, rng, key);
7276-
if (err != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
7323+
if (err != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
7324+
#if FIPS_VERSION3_GE(7,0,0)
7325+
/* A key referenced by id or label has no curve until the
7326+
* callback resolves it, so it passed the check above. Check
7327+
* again and do not release a signature made on a too-small
7328+
* curve. */
7329+
if ((err == 0) && (ecc_fips_sign_curve_allowed(key) != 0)) {
7330+
ForceZero(out, *outlen);
7331+
*outlen = 0;
7332+
err = SIG_TYPE_E;
7333+
}
7334+
#endif
72777335
return err;
7336+
}
72787337
/* fall-through when unavailable */
72797338
}
72807339
#endif
@@ -7498,6 +7557,12 @@ int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
74987557
return ECC_BAD_ARG_E;
74997558
#endif
75007559

7560+
#if FIPS_VERSION3_GE(7,0,0)
7561+
if (ecc_fips_sign_curve_allowed(key) != 0) {
7562+
return SIG_TYPE_E;
7563+
}
7564+
#endif
7565+
75017566
return stm32_ecc_sign_hash_ex(in, inlen, rng, key, r, s);
75027567
}
75037568

@@ -7896,6 +7961,14 @@ int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
78967961
return ECC_BAD_ARG_E;
78977962
}
78987963

7964+
#if FIPS_VERSION3_GE(7,0,0)
7965+
/* wc_ecc_sign_hash_ex() is public, so it is gated as well as
7966+
* wc_ecc_sign_hash(). */
7967+
if (ecc_fips_sign_curve_allowed(key) != 0) {
7968+
return SIG_TYPE_E;
7969+
}
7970+
#endif
7971+
78997972
#if defined(WOLFSSL_SP_MATH)
79007973
if (key->idx == ECC_CUSTOM_IDX || (1
79017974
#ifndef WOLFSSL_SP_NO_256

‎wolfcrypt/src/hmac.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575

7676
/* map the _Software calls used by kcapi_hmac.c */
7777
#define wc_HmacSetKey wc_HmacSetKey_Software
78+
#define wc_HmacSetKey_ex wc_HmacSetKey_ex_Software
7879
#define wc_HmacUpdate wc_HmacUpdate_Software
7980
#define wc_HmacFinal wc_HmacFinal_Software
8081
#endif
@@ -1727,6 +1728,7 @@ int wc_HmacFinal(Hmac* hmac, byte* hash)
17271728

17281729
/* unmap the _Software calls used by kcapi_hmac.c */
17291730
#undef wc_HmacSetKey
1731+
#undef wc_HmacSetKey_ex
17301732
#undef wc_HmacUpdate
17311733
#undef wc_HmacFinal
17321734

‎wolfcrypt/src/port/kcapi/kcapi_hmac.c‎

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,12 @@ void wc_HmacFree(Hmac* hmac)
9292
}
9393
}
9494

95-
int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
95+
/* KCAPI counterpart of the software wc_HmacSetKey_ex(): callers that pass
96+
* allowFlag (the TLS PRF, HKDF, PBKDF2) then use wc_HmacUpdate() and
97+
* wc_HmacFinal() from this file, so the key has to be set on the same
98+
* backend. */
99+
int wc_HmacSetKey_ex(Hmac* hmac, int type, const byte* key, word32 length,
100+
int allowFlag)
96101
{
97102
int ret = 0;
98103
const char* ciphername = NULL;
@@ -107,23 +112,29 @@ int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
107112
#endif
108113

109114
#ifdef HAVE_FIPS
110-
if (length < HMAC_FIPS_MIN_KEY)
111-
return HMAC_MIN_KEYLEN_E;
115+
if (!allowFlag) {
116+
if (length < HMAC_FIPS_MIN_KEY)
117+
return HMAC_MIN_KEYLEN_E;
118+
}
119+
#else
120+
(void)allowFlag;
112121
#endif
113122

114123
if (ret == 0) {
115124
switch (type) {
116125
#ifndef NO_MD5
117126
case WC_MD5:
118-
return wc_HmacSetKey_Software(hmac, type, key, length);
127+
return wc_HmacSetKey_ex_Software(hmac, type, key, length,
128+
allowFlag);
119129
#endif
120130
#ifndef NO_SHA
121131
case WC_SHA:
122132
#if !defined(WOLFSSL_NO_KCAPI_HMAC_SHA1)
123133
ciphername = WC_NAME_HMAC_SHA1;
124134
break;
125135
#else
126-
return wc_HmacSetKey_Software(hmac, type, key, length);
136+
return wc_HmacSetKey_ex_Software(hmac, type, key, length,
137+
allowFlag);
127138
#endif
128139
#endif
129140
#ifdef WOLFSSL_SHA224
@@ -132,7 +143,8 @@ int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
132143
ciphername = WC_NAME_HMAC_SHA224;
133144
break;
134145
#else
135-
return wc_HmacSetKey_Software(hmac, type, key, length);
146+
return wc_HmacSetKey_ex_Software(hmac, type, key, length,
147+
allowFlag);
136148
#endif
137149
#endif
138150
#ifndef NO_SHA256
@@ -141,7 +153,8 @@ int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
141153
ciphername = WC_NAME_HMAC_SHA256;
142154
break;
143155
#else
144-
return wc_HmacSetKey_Software(hmac, type, key, length);
156+
return wc_HmacSetKey_ex_Software(hmac, type, key, length,
157+
allowFlag);
145158
#endif
146159
#endif
147160
#ifdef WOLFSSL_SHA384
@@ -150,7 +163,8 @@ int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
150163
ciphername = WC_NAME_HMAC_SHA384;
151164
break;
152165
#else
153-
return wc_HmacSetKey_Software(hmac, type, key, length);
166+
return wc_HmacSetKey_ex_Software(hmac, type, key, length,
167+
allowFlag);
154168
#endif
155169
#endif
156170
#ifdef WOLFSSL_SHA512
@@ -159,7 +173,8 @@ int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
159173
ciphername = WC_NAME_HMAC_SHA512;
160174
break;
161175
#else
162-
return wc_HmacSetKey_Software(hmac, type, key, length);
176+
return wc_HmacSetKey_ex_Software(hmac, type, key, length,
177+
allowFlag);
163178
#endif
164179
#endif
165180
#ifdef WOLFSSL_SHA3
@@ -175,7 +190,8 @@ int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
175190
#ifndef WOLFSSL_NOSHA3_512
176191
case WC_SHA3_512:
177192
#endif
178-
return wc_HmacSetKey_Software(hmac, type, key, length);
193+
return wc_HmacSetKey_ex_Software(hmac, type, key, length,
194+
allowFlag);
179195
#endif
180196
default:
181197
ret = BAD_FUNC_ARG;
@@ -204,6 +220,16 @@ int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
204220
return ret;
205221
}
206222

223+
int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
224+
{
225+
/* Same default as the software wc_HmacSetKey(). */
226+
#if defined(HAVE_FIPS)
227+
return wc_HmacSetKey_ex(hmac, type, key, length, 0);
228+
#else
229+
return wc_HmacSetKey_ex(hmac, type, key, length, 1);
230+
#endif
231+
}
232+
207233
int wc_HmacUpdate(Hmac* hmac, const byte* msg, word32 length)
208234
{
209235
int ret = 0;

‎wolfcrypt/test/test.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12390,6 +12390,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha_test(void)
1239012390
#endif
1239112391
}
1239212392

12393+
1239312394
out:
1239412395

1239512396
wc_HmacFree(&hmac);
@@ -50737,6 +50738,26 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hkdf_test(void)
5073750738
#endif /* !NO_SHA256 */
5073850739

5073950740
#ifndef NO_SHA
50741+
#if FIPS_VERSION3_GE(7,0,0) && defined(WC_HAVE_FIPS_INDICATOR) && \
50742+
!defined(FIPS_NO_WRAPPERS)
50743+
/* SP 800-56C Rev. 2 Section 4 approves the HKDF auxiliary function only
50744+
* with an approved hash and the module's key-derivation coverage is
50745+
* SHA-256/384/512, so the generic wrappers refuse SHA-1 (see
50746+
* HkdfDigestAllowed in fips.c). */
50747+
PRIVATE_KEY_UNLOCK();
50748+
ret = wc_HKDF_Extract_ex(WC_SHA, NULL, 0, ikm1, (word32)sizeof(ikm1),
50749+
prk, HEAP_HINT, devId);
50750+
PRIVATE_KEY_LOCK();
50751+
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
50752+
return WC_TEST_RET_ENC_EC(ret);
50753+
PRIVATE_KEY_UNLOCK();
50754+
ret = wc_HKDF_Expand_ex(WC_SHA, prk, WC_SHA_DIGEST_SIZE, NULL, 0,
50755+
okm1, (word32)L, HEAP_HINT, devId);
50756+
PRIVATE_KEY_LOCK();
50757+
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
50758+
return WC_TEST_RET_ENC_EC(ret);
50759+
ret = 0;
50760+
#else
5074050761
PRIVATE_KEY_UNLOCK();
5074150762
#if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || FIPS_VERSION_GE(7,0))
5074250763
ret = wc_HKDF_Extract_ex(WC_SHA, NULL, 0, ikm1, (word32)sizeof(ikm1),
@@ -50762,6 +50783,8 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hkdf_test(void)
5076250783

5076350784
if (XMEMCMP(okm1, res1, (unsigned long)L) != 0)
5076450785
return WC_TEST_RET_ENC_NC;
50786+
#endif /* FIPS_VERSION3_GE(7,0,0) && WC_HAVE_FIPS_INDICATOR &&
50787+
* !FIPS_NO_WRAPPERS */
5076550788

5076650789
#ifndef HAVE_FIPS
5076750790
/* fips can't have key size under 14 bytes, salt is key too */
@@ -51102,6 +51125,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prf_test(void)
5110251125
return WC_TEST_RET_ENC_NC;
5110351126
}
5110451127

51128+
5110551129
return 0;
5110651130
}
5110751131
#endif /* WOLFSSL_HAVE_PRF && !NO_HMAC && WOLFSSL_SHA384 && !WOLFSSL_NO_TLS12 */

‎wolfssl/wolfcrypt/hmac.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ WOLFSSL_API int wc_HmacFinal(Hmac* hmac, byte* hash);
196196
#ifdef WOLFSSL_KCAPI_HMAC
197197
WOLFSSL_API int wc_HmacSetKey_Software(Hmac* hmac, int type, const byte* key,
198198
word32 keySz);
199+
WOLFSSL_API int wc_HmacSetKey_ex_Software(Hmac* hmac, int type,
200+
const byte* key, word32 keySz,
201+
int allowFlag);
199202
WOLFSSL_API int wc_HmacUpdate_Software(Hmac* hmac, const byte* in, word32 sz);
200203
WOLFSSL_API int wc_HmacFinal_Software(Hmac* hmac, byte* out);
201204
#endif

0 commit comments

Comments
 (0)