Skip to content

Commit f5cb791

Browse files
committed
ML-KEM: Add check for Pubkey hash mismatch on decoding the dk
1 parent 59f4fa5 commit f5cb791

3 files changed

Lines changed: 18 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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,9 @@ int wc_MlKemKey_DecodePrivateKey(MlKemKey* key, const unsigned char* in,
16811681
}
16821682

16831683
if (ret == 0) {
1684+
byte computedHash[WC_ML_KEM_SYM_SZ];
1685+
XMEMSET(computedHash, 0, WC_ML_KEM_SYM_SZ);
1686+
16841687
/* Decode private key that is vector of polynomials.
16851688
* Alg 18 Step 1: dk_PKE <- dk[0 : 384k]
16861689
* Alg 15 Step 5: s_hat <- ByteDecode_12(dk_PKE) */
@@ -1689,16 +1692,24 @@ int wc_MlKemKey_DecodePrivateKey(MlKemKey* key, const unsigned char* in,
16891692

16901693
/* Decode the public key that is after the private key. */
16911694
mlkemkey_decode_public(key->pub, key->pubSeed, p, k);
1695+
/* Compute the hash of the public key. */
1696+
MLKEM_HASH_H(&key->hash, p, pubLen, computedHash);
16921697
p += pubLen;
16931698

16941699
/* Copy the hash of the encoded public key that is after public key. */
16951700
XMEMCPY(key->h, p, sizeof(key->h));
16961701
p += WC_ML_KEM_SYM_SZ;
1702+
16971703
/* Copy the z (randomizer) that is after hash. */
16981704
XMEMCPY(key->z, p, sizeof(key->z));
16991705

17001706
/* Set flags */
17011707
key->flags |= MLKEM_FLAG_H_SET | MLKEM_FLAG_BOTH_SET;
1708+
1709+
/* Compare computed public key hash with stored hash */
1710+
if (XMEMCMP(key->h, computedHash, WC_ML_KEM_SYM_SZ) != 0)
1711+
ret = MLKEM_PUB_HASH_E;
1712+
17021713
}
17031714

17041715
return ret;

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)