Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Signed-off-by: Hanno Becker <[email protected]>
  • Loading branch information
hanno-becker committed Oct 31, 2024
1 parent 4d5bf01 commit 2f72643
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions mlkem/kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ static int check_pk(const uint8_t pk[MLKEM_PUBLICKEYBYTES]) {
polyvec_frombytes(&p, pk);
polyvec_reduce(&p);
polyvec_tobytes(p_reencoded, &p);
// Data is public, so a variable-time memcmp() is OK
if (memcmp(pk, p_reencoded, MLKEM_POLYVECBYTES)) {
return -1;
}
Expand All @@ -48,6 +49,9 @@ static int check_pk(const uint8_t pk[MLKEM_PUBLICKEYBYTES]) {
**************************************************/
static int check_sk(const uint8_t sk[MLKEM_SECRETKEYBYTES]) {
uint8_t test[MLKEM_SYMBYTES];
// The parts of `sk` being hashed and compared here are public, so
// no public information is leaked through the runtime or the return value
// of this function.
hash_h(test, sk + MLKEM_INDCPA_SECRETKEYBYTES, MLKEM_PUBLICKEYBYTES);
if (memcmp(sk + MLKEM_SECRETKEYBYTES - 2 * MLKEM_SYMBYTES, test,
MLKEM_SYMBYTES)) {
Expand Down Expand Up @@ -120,7 +124,7 @@ int crypto_kem_keypair(uint8_t *pk, uint8_t *sk) {
*bytes)
**
* Returns 0 on success, and -1 if the public key modulus check (see Section 7.2
*of FIPS203) fails.
* of FIPS203) fails.
**************************************************/
int crypto_kem_enc_derand(uint8_t *ct, uint8_t *ss, const uint8_t *pk,
const uint8_t *coins) {
Expand Down Expand Up @@ -159,7 +163,7 @@ int crypto_kem_enc_derand(uint8_t *ct, uint8_t *ss, const uint8_t *pk,
* (an already allocated array of MLKEM_PUBLICKEYBYTES bytes)
*
* Returns 0 on success, and -1 if the public key modulus check (see Section 7.2
*of FIPS203) fails
* of FIPS203) fails.
**************************************************/
int crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk) {
uint8_t coins[MLKEM_SYMBYTES] ALIGN;
Expand All @@ -181,7 +185,7 @@ int crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk) {
* (an already allocated array of MLKEM_SECRETKEYBYTES bytes)
*
* Returns 0 on success, and -1 if the secret key hash check (see Section 7.3 of
*FIPS203) fails
* FIPS203) fails.
*
* On failure, ss will contain a pseudo-random value.
**************************************************/
Expand Down

0 comments on commit 2f72643

Please sign in to comment.