Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ML-KEM FIPS 203 destruction of intermediate values #1883

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions crypto/fipsmodule/ml_kem/ml_kem_ref/indcpa.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ void gen_matrix(ml_kem_params *params, polyvec *a, const uint8_t seed[KYBER_SYMB
}
}
}

// FIPS 203. Section 3.3 Destruction of intermediate values.
OPENSSL_cleanse(buf, sizeof(buf));
}

/*************************************************
Expand Down Expand Up @@ -244,6 +247,14 @@ void indcpa_keypair_derand(ml_kem_params *params,

pack_sk(params, sk, &skpv);
pack_pk(params, pk, &pkpv, publicseed);

// FIPS 203. Section 3.3 Destruction of intermediate values.
OPENSSL_cleanse(buf, sizeof(buf));
OPENSSL_cleanse(coins_with_domain_separator, sizeof(coins_with_domain_separator));
OPENSSL_cleanse(a, sizeof(a));
OPENSSL_cleanse(&e, sizeof(e));
OPENSSL_cleanse(&pkpv, sizeof(pkpv));
OPENSSL_cleanse(&skpv, sizeof(skpv));
}


Expand Down Expand Up @@ -303,6 +314,17 @@ void indcpa_enc(ml_kem_params *params,
poly_reduce(&v);

pack_ciphertext(params, c, &b, &v);

// FIPS 203. Section 3.3 Destruction of intermediate values.
OPENSSL_cleanse(seed, sizeof(seed));
OPENSSL_cleanse(&sp, sizeof(sp));
OPENSSL_cleanse(&pkpv, sizeof(pkpv));
OPENSSL_cleanse(&ep, sizeof(ep));
OPENSSL_cleanse(at, sizeof(at));
OPENSSL_cleanse(&b, sizeof(b));
OPENSSL_cleanse(&v, sizeof(v));
OPENSSL_cleanse(&k, sizeof(k));
OPENSSL_cleanse(&epp, sizeof(epp));
}

/*************************************************
Expand Down Expand Up @@ -340,4 +362,11 @@ void indcpa_dec(ml_kem_params *params,
poly_reduce(&mp);

poly_tomsg(m, &mp);


// FIPS 203. Section 3.3 Destruction of intermediate values.
OPENSSL_cleanse(&b, sizeof(b));
OPENSSL_cleanse(&skpv, sizeof(skpv));
OPENSSL_cleanse(&v, sizeof(v));
OPENSSL_cleanse(&mp, sizeof(mp));
}
14 changes: 14 additions & 0 deletions crypto/fipsmodule/ml_kem/ml_kem_ref/kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ int crypto_kem_keypair(ml_kem_params *params,
uint8_t coins[2*KYBER_SYMBYTES];
RAND_bytes(coins, 2*KYBER_SYMBYTES);
crypto_kem_keypair_derand(params, pk, sk, coins);

// FIPS 203. Section 3.3 Destruction of intermediate values.
OPENSSL_cleanse(coins, sizeof(coins));
return 0;
}

Expand Down Expand Up @@ -218,6 +221,10 @@ int crypto_kem_enc_derand(ml_kem_params *params,
indcpa_enc(params, ct, buf, pk, kr+KYBER_SYMBYTES);

memcpy(ss,kr,KYBER_SYMBYTES);

// FIPS 203. Section 3.3 Destruction of intermediate values.
OPENSSL_cleanse(buf, sizeof(buf));
OPENSSL_cleanse(kr, sizeof(kr));
return 0;
}

Expand Down Expand Up @@ -248,6 +255,9 @@ int crypto_kem_enc(ml_kem_params *params,
uint8_t coins[KYBER_SYMBYTES];
RAND_bytes(coins, KYBER_SYMBYTES);
crypto_kem_enc_derand(params, ct, ss, pk, coins);

// FIPS 203. Section 3.3 Destruction of intermediate values.
OPENSSL_cleanse(coins, sizeof(coins));
return 0;
}

Expand Down Expand Up @@ -301,5 +311,9 @@ int crypto_kem_dec(ml_kem_params *params,
/* Copy true key to return buffer if fail is false */
cmov(ss,kr,KYBER_SYMBYTES,!fail);

// FIPS 203. Section 3.3 Destruction of intermediate values.
OPENSSL_cleanse(buf, sizeof(buf));
OPENSSL_cleanse(kr, sizeof(kr));
OPENSSL_cleanse(cmp, sizeof(cmp));
return 0;
}
Loading