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

Add ML-KEM CAST for KeyGen, Encaps, and Decaps #1846

Merged
merged 1 commit into from
Sep 11, 2024
Merged
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
39 changes: 38 additions & 1 deletion crypto/fipsmodule/ml_kem/ml_kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "./ml_kem_ref/reduce.c"
#include "./ml_kem_ref/symmetric-shake.c"
#include "./ml_kem_ref/verify.c"
#include "../../internal.h"

// Note: These methods currently default to using the reference code for ML_KEM.
// In a future where AWS-LC has optimized options available, those can be
Expand All @@ -25,13 +26,21 @@
int ml_kem_512_keypair_deterministic(uint8_t *public_key /* OUT */,
uint8_t *secret_key /* OUT */,
const uint8_t *seed /* IN */) {
boringssl_ensure_ml_kem_self_test();
return ml_kem_512_keypair_deterministic_no_self_test(public_key, secret_key, seed);
}

int ml_kem_512_keypair_deterministic_no_self_test(uint8_t *public_key /* OUT */,
uint8_t *secret_key /* OUT */,
const uint8_t *seed /* IN */) {
ml_kem_params params;
ml_kem_512_params_init(&params);
return ml_kem_keypair_derand_ref(&params, public_key, secret_key, seed);
}

int ml_kem_512_keypair(uint8_t *public_key /* OUT */,
uint8_t *secret_key /* OUT */) {
boringssl_ensure_ml_kem_self_test();
ml_kem_params params;
ml_kem_512_params_init(&params);
return ml_kem_keypair_ref(&params, public_key, secret_key);
Expand All @@ -41,14 +50,24 @@ int ml_kem_512_encapsulate_deterministic(uint8_t *ciphertext /* OUT */,
uint8_t *shared_secret /* OUT */,
const uint8_t *public_key /* IN */,
const uint8_t *seed /* IN */) {
boringssl_ensure_ml_kem_self_test();
return ml_kem_512_encapsulate_deterministic_no_self_test(ciphertext, shared_secret, public_key, seed);
}

int ml_kem_512_encapsulate_deterministic_no_self_test(uint8_t *ciphertext /* OUT */,
uint8_t *shared_secret /* OUT */,
const uint8_t *public_key /* IN */,
const uint8_t *seed /* IN */) {
ml_kem_params params;
ml_kem_512_params_init(&params);
return ml_kem_enc_derand_ref(&params, ciphertext, shared_secret, public_key, seed);
return ml_kem_enc_derand_ref(&params, ciphertext, shared_secret, public_key,
seed);
}

int ml_kem_512_encapsulate(uint8_t *ciphertext /* OUT */,
uint8_t *shared_secret /* OUT */,
const uint8_t *public_key /* IN */) {
boringssl_ensure_ml_kem_self_test();
ml_kem_params params;
ml_kem_512_params_init(&params);
return ml_kem_enc_ref(&params, ciphertext, shared_secret, public_key);
Expand All @@ -57,21 +76,31 @@ int ml_kem_512_encapsulate(uint8_t *ciphertext /* OUT */,
int ml_kem_512_decapsulate(uint8_t *shared_secret /* OUT */,
const uint8_t *ciphertext /* IN */,
const uint8_t *secret_key /* IN */) {
boringssl_ensure_ml_kem_self_test();
return ml_kem_512_decapsulate_no_self_test(shared_secret, ciphertext, secret_key);
}

int ml_kem_512_decapsulate_no_self_test(uint8_t *shared_secret /* OUT */,
const uint8_t *ciphertext /* IN */,
const uint8_t *secret_key /* IN */) {
ml_kem_params params;
ml_kem_512_params_init(&params);
return ml_kem_dec_ref(&params, shared_secret, ciphertext, secret_key);
}


int ml_kem_768_keypair_deterministic(uint8_t *public_key /* OUT */,
uint8_t *secret_key /* OUT */,
const uint8_t *seed /* IN */) {
boringssl_ensure_ml_kem_self_test();
ml_kem_params params;
ml_kem_768_params_init(&params);
return ml_kem_keypair_derand_ref(&params, public_key, secret_key, seed);
}

int ml_kem_768_keypair(uint8_t *public_key /* OUT */,
uint8_t *secret_key /* OUT */) {
boringssl_ensure_ml_kem_self_test();
ml_kem_params params;
ml_kem_768_params_init(&params);
return ml_kem_keypair_ref(&params, public_key, secret_key);
Expand All @@ -81,6 +110,7 @@ int ml_kem_768_encapsulate_deterministic(uint8_t *ciphertext /* OUT */,
uint8_t *shared_secret /* OUT */,
const uint8_t *public_key /* IN */,
const uint8_t *seed /* IN */) {
boringssl_ensure_ml_kem_self_test();
ml_kem_params params;
ml_kem_768_params_init(&params);
return ml_kem_enc_derand_ref(&params, ciphertext, shared_secret, public_key, seed);
Expand All @@ -89,6 +119,7 @@ int ml_kem_768_encapsulate_deterministic(uint8_t *ciphertext /* OUT */,
int ml_kem_768_encapsulate(uint8_t *ciphertext /* OUT */,
uint8_t *shared_secret /* OUT */,
const uint8_t *public_key /* IN */) {
boringssl_ensure_ml_kem_self_test();
ml_kem_params params;
ml_kem_768_params_init(&params);
return ml_kem_enc_ref(&params, ciphertext, shared_secret, public_key);
Expand All @@ -97,6 +128,7 @@ int ml_kem_768_encapsulate(uint8_t *ciphertext /* OUT */,
int ml_kem_768_decapsulate(uint8_t *shared_secret /* OUT */,
const uint8_t *ciphertext /* IN */,
const uint8_t *secret_key /* IN */) {
boringssl_ensure_ml_kem_self_test();
ml_kem_params params;
ml_kem_768_params_init(&params);
return ml_kem_dec_ref(&params, shared_secret, ciphertext, secret_key);
Expand All @@ -105,13 +137,15 @@ int ml_kem_768_decapsulate(uint8_t *shared_secret /* OUT */,
int ml_kem_1024_keypair_deterministic(uint8_t *public_key /* OUT */,
uint8_t *secret_key /* OUT */,
const uint8_t *seed /* IN */) {
boringssl_ensure_ml_kem_self_test();
ml_kem_params params;
ml_kem_1024_params_init(&params);
return ml_kem_keypair_derand_ref(&params, public_key, secret_key, seed);
}

int ml_kem_1024_keypair(uint8_t *public_key /* OUT */,
uint8_t *secret_key /* OUT */) {
boringssl_ensure_ml_kem_self_test();
ml_kem_params params;
ml_kem_1024_params_init(&params);
return ml_kem_keypair_ref(&params, public_key, secret_key);
Expand All @@ -121,6 +155,7 @@ int ml_kem_1024_encapsulate_deterministic(uint8_t *ciphertext /* OUT */,
uint8_t *shared_secret /* OUT */,
const uint8_t *public_key /* IN */,
const uint8_t *seed /* IN */) {
boringssl_ensure_ml_kem_self_test();
ml_kem_params params;
ml_kem_1024_params_init(&params);
return ml_kem_enc_derand_ref(&params, ciphertext, shared_secret, public_key, seed);
Expand All @@ -129,6 +164,7 @@ int ml_kem_1024_encapsulate_deterministic(uint8_t *ciphertext /* OUT */,
int ml_kem_1024_encapsulate(uint8_t *ciphertext /* OUT */,
uint8_t *shared_secret /* OUT */,
const uint8_t *public_key /* IN */) {
boringssl_ensure_ml_kem_self_test();
ml_kem_params params;
ml_kem_1024_params_init(&params);
return ml_kem_enc_ref(&params, ciphertext, shared_secret, public_key);
Expand All @@ -137,6 +173,7 @@ int ml_kem_1024_encapsulate(uint8_t *ciphertext /* OUT */,
int ml_kem_1024_decapsulate(uint8_t *shared_secret /* OUT */,
const uint8_t *ciphertext /* IN */,
const uint8_t *secret_key /* IN */) {
boringssl_ensure_ml_kem_self_test();
ml_kem_params params;
ml_kem_1024_params_init(&params);
return ml_kem_dec_ref(&params, shared_secret, ciphertext, secret_key);
Expand Down
13 changes: 13 additions & 0 deletions crypto/fipsmodule/ml_kem/ml_kem.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ int ml_kem_512_keypair_deterministic(uint8_t *public_key /* OUT */,
uint8_t *secret_key /* OUT */,
const uint8_t *seed /* IN */);

int ml_kem_512_keypair_deterministic_no_self_test(uint8_t *public_key /* OUT */,
uint8_t *secret_key /* OUT */,
const uint8_t *seed /* IN */);

int ml_kem_512_keypair(uint8_t *public_key /* OUT */,
uint8_t *secret_key /* OUT */);

Expand All @@ -40,6 +44,11 @@ int ml_kem_512_encapsulate_deterministic(uint8_t *ciphertext /* OUT */,
const uint8_t *public_key /* IN */,
const uint8_t *seed /* IN */);

int ml_kem_512_encapsulate_deterministic_no_self_test(uint8_t *ciphertext /* OUT */,
uint8_t *shared_secret /* OUT */,
const uint8_t *public_key /* IN */,
const uint8_t *seed /* IN */);

int ml_kem_512_encapsulate(uint8_t *ciphertext /* OUT */,
uint8_t *shared_secret /* OUT */,
const uint8_t *public_key /* IN */);
Expand All @@ -48,6 +57,10 @@ int ml_kem_512_decapsulate(uint8_t *shared_secret /* OUT */,
const uint8_t *ciphertext /* IN */,
const uint8_t *secret_key /* IN */);

int ml_kem_512_decapsulate_no_self_test(uint8_t *shared_secret /* OUT */,
const uint8_t *ciphertext /* IN */,
const uint8_t *secret_key /* IN */);

int ml_kem_768_keypair_deterministic(uint8_t *public_key /* OUT */,
uint8_t *secret_key /* OUT */,
const uint8_t *seed /* IN */);
Expand Down
Loading
Loading