Skip to content

Commit 2a08fbe

Browse files
committed
MLKEM: check public key when decoding
Check that the public key values are less than Q when decoding.
1 parent 2354ea1 commit 2a08fbe

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

wolfcrypt/src/wc_mlkem.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,9 @@ int wc_MlKemKey_DecodePublicKey(MlKemKey* key, const unsigned char* in,
17931793

17941794
if (ret == 0) {
17951795
mlkemkey_decode_public(key->pub, key->pubSeed, p, k);
1796-
1796+
ret = mlkem_check_public(key->pub, k);
1797+
}
1798+
if (ret == 0) {
17971799
/* Calculate public hash. */
17981800
ret = MLKEM_HASH_H(&key->hash, in, len, key->h);
17991801
}

wolfcrypt/src/wc_mlkem_poly.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6074,4 +6074,27 @@ void mlkem_to_bytes(byte* b, sword16* p, int k)
60746074
}
60756075
}
60766076

6077+
/**
6078+
* Check the public key values are smaller than the modulus.
6079+
*
6080+
* @param [in] pub Public key - vector.
6081+
* @param [in] k Number of polynomials in vector.
6082+
* @return 0 when all values are in range.
6083+
* @return PUBLIC_KEY_E when at least one value is out of range.
6084+
*/
6085+
int mlkem_check_public(sword16* pub, int k)
6086+
{
6087+
int ret = 0;
6088+
int i;
6089+
6090+
for (i = 0; i < k * MLKEM_N; i++) {
6091+
if (pub[i] >= MLKEM_Q) {
6092+
ret = PUBLIC_KEY_E;
6093+
break;
6094+
}
6095+
}
6096+
6097+
return ret;
6098+
}
6099+
60776100
#endif /* WOLFSSL_WC_MLKEM */

wolfssl/wolfcrypt/wc_mlkem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ WOLFSSL_LOCAL
238238
void mlkem_from_bytes(sword16* p, const byte* b, int k);
239239
WOLFSSL_LOCAL
240240
void mlkem_to_bytes(byte* b, sword16* p, int k);
241+
WOLFSSL_LOCAL
242+
int mlkem_check_public(sword16* p, int k);
241243

242244
#ifdef USE_INTEL_SPEEDUP
243245
WOLFSSL_LOCAL

0 commit comments

Comments
 (0)