Merge pull request #9470 from lealem47/MLKEM_PUB_HASH_E

ML-KEM: Add check for Pubkey hash mismatch on decoding the dk
This commit is contained in:
David Garske
2025-12-01 12:49:48 -08:00
committed by GitHub
3 changed files with 15 additions and 2 deletions

View File

@@ -656,6 +656,9 @@ const char* wc_GetErrorString(int error)
case INTERRUPTED_E:
return "Process interrupted";
case MLKEM_PUB_HASH_E:
return "ML-KEM priv key's stored hash doesn't match encoded pub key";
case MAX_CODE_E:
case WC_SPAN1_MIN_CODE_E:
case MIN_CODE_E:

View File

@@ -1689,7 +1689,15 @@ int wc_MlKemKey_DecodePrivateKey(MlKemKey* key, const unsigned char* in,
/* Decode the public key that is after the private key. */
mlkemkey_decode_public(key->pub, key->pubSeed, p, k);
/* Compute the hash of the public key. */
ret = MLKEM_HASH_H(&key->hash, p, pubLen, key->h);
p += pubLen;
}
if (ret == 0) {
/* Compare computed public key hash with stored hash */
if (XMEMCMP(key->h, p, WC_ML_KEM_SYM_SZ) != 0)
ret = MLKEM_PUB_HASH_E;
/* Copy the hash of the encoded public key that is after public key. */
XMEMCPY(key->h, p, sizeof(key->h));