Skip to content

Commit a2f46cd

Browse files
authored
Merge pull request #9470 from lealem47/MLKEM_PUB_HASH_E
ML-KEM: Add check for Pubkey hash mismatch on decoding the dk
2 parents 93944d2 + eace021 commit a2f46cd

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

wolfcrypt/src/error.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,9 @@ const char* wc_GetErrorString(int error)
656656
case INTERRUPTED_E:
657657
return "Process interrupted";
658658

659+
case MLKEM_PUB_HASH_E:
660+
return "ML-KEM priv key's stored hash doesn't match encoded pub key";
661+
659662
case MAX_CODE_E:
660663
case WC_SPAN1_MIN_CODE_E:
661664
case MIN_CODE_E:

wolfcrypt/src/wc_mlkem.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,15 @@ int wc_MlKemKey_DecodePrivateKey(MlKemKey* key, const unsigned char* in,
16891689

16901690
/* Decode the public key that is after the private key. */
16911691
mlkemkey_decode_public(key->pub, key->pubSeed, p, k);
1692+
/* Compute the hash of the public key. */
1693+
ret = MLKEM_HASH_H(&key->hash, p, pubLen, key->h);
16921694
p += pubLen;
1695+
}
1696+
1697+
if (ret == 0) {
1698+
/* Compare computed public key hash with stored hash */
1699+
if (XMEMCMP(key->h, p, WC_ML_KEM_SYM_SZ) != 0)
1700+
ret = MLKEM_PUB_HASH_E;
16931701

16941702
/* Copy the hash of the encoded public key that is after public key. */
16951703
XMEMCPY(key->h, p, sizeof(key->h));

wolfssl/wolfcrypt/error-crypt.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,11 @@ enum wolfCrypt_ErrorCodes {
307307
WC_ACCEL_INHIBIT_E = -1002, /* Crypto acceleration is currently inhibited */
308308
BAD_INDEX_E = -1003, /* Bad index */
309309
INTERRUPTED_E = -1004, /* Process interrupted */
310+
MLKEM_PUB_HASH_E = -1005, /* Encoded public key in decapsulation key does
311+
* not match stored hash*/
310312

311-
WC_SPAN2_LAST_E = -1004, /* Update to indicate last used error code */
312-
WC_LAST_E = -1004, /* the last code used either here or in
313+
WC_SPAN2_LAST_E = -1005, /* Update to indicate last used error code */
314+
WC_LAST_E = -1005, /* the last code used either here or in
313315
* error-ssl.h */
314316

315317
WC_SPAN2_MIN_CODE_E = -1999, /* Last usable code in span 2 */

0 commit comments

Comments
 (0)