@@ -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 ;
0 commit comments