-
Notifications
You must be signed in to change notification settings - Fork 742
feat: add pure mlkem_1024 definition #5468
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
#include "api/s2n.h" | ||
#include "crypto/s2n_pq.h" | ||
#include "tests/s2n_test.h" | ||
#include "tests/testlib/s2n_testlib.h" | ||
#include "tls/s2n_connection.h" | ||
#include "tls/s2n_tls13_handshake.c" | ||
#include "utils/s2n_blob.h" | ||
#include "utils/s2n_mem.h" | ||
#include "utils/s2n_safety.h" | ||
|
||
#define MLKEM1024_SECRET "B408D5D115713F0A93047DBBEA832E4340787686D59A9A2D106BD662BA0AA035" | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
BEGIN_TEST(); | ||
|
||
/* Test: TLS 1.3 pure ML-KEM-1024 shared secret computation */ | ||
{ | ||
S2N_BLOB_FROM_HEX(expected_secret, MLKEM1024_SECRET); | ||
|
||
DEFER_CLEANUP(struct s2n_connection *client_conn = NULL, s2n_connection_ptr_free); | ||
DEFER_CLEANUP(struct s2n_connection *server_conn = NULL, s2n_connection_ptr_free); | ||
EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT)); | ||
EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER)); | ||
|
||
client_conn->kex_params.server_kem_group_params.kem_group = &s2n_pure_mlkem_1024; | ||
client_conn->kex_params.client_kem_group_params.kem_group = &s2n_pure_mlkem_1024; | ||
server_conn->kex_params.server_kem_group_params.kem_group = &s2n_pure_mlkem_1024; | ||
server_conn->kex_params.client_kem_group_params.kem_group = &s2n_pure_mlkem_1024; | ||
|
||
client_conn->kex_params.server_kem_group_params.kem_params.kem = s2n_pure_mlkem_1024.kem; | ||
client_conn->kex_params.client_kem_group_params.kem_params.kem = s2n_pure_mlkem_1024.kem; | ||
server_conn->kex_params.server_kem_group_params.kem_params.kem = s2n_pure_mlkem_1024.kem; | ||
server_conn->kex_params.client_kem_group_params.kem_params.kem = s2n_pure_mlkem_1024.kem; | ||
|
||
POSIX_GUARD(s2n_dup(&expected_secret, | ||
&client_conn->kex_params.client_kem_group_params.kem_params.shared_secret)); | ||
POSIX_GUARD(s2n_dup(&expected_secret, | ||
&server_conn->kex_params.client_kem_group_params.kem_params.shared_secret)); | ||
|
||
DEFER_CLEANUP(struct s2n_blob client_secret = { 0 }, s2n_free); | ||
DEFER_CLEANUP(struct s2n_blob server_secret = { 0 }, s2n_free); | ||
|
||
EXPECT_SUCCESS(s2n_tls13_compute_shared_secret(client_conn, &client_secret)); | ||
EXPECT_SUCCESS(s2n_tls13_compute_shared_secret(server_conn, &server_secret)); | ||
|
||
S2N_BLOB_EXPECT_EQUAL(client_secret, server_secret); | ||
|
||
EXPECT_EQUAL(client_secret.size, expected_secret.size); | ||
EXPECT_BYTEARRAY_EQUAL(client_secret.data, expected_secret.data, expected_secret.size); | ||
}; | ||
|
||
END_TEST(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,8 +73,10 @@ int s2n_tls13_compute_ecc_shared_secret(struct s2n_connection *conn, struct s2n_ | |
} | ||
|
||
/* Computes the ECDHE+PQKEM hybrid shared secret as defined in | ||
* https://tools.ietf.org/html/draft-stebila-tls-hybrid-design */ | ||
int s2n_tls13_compute_pq_hybrid_shared_secret(struct s2n_connection *conn, struct s2n_blob *shared_secret) | ||
* https://tools.ietf.org/html/draft-stebila-tls-hybrid-design | ||
* Also supports "pure PQ" mode when kem_group->curve == &s2n_ecc_curve_none. | ||
*/ | ||
int s2n_tls13_compute_pq_shared_secret(struct s2n_connection *conn, struct s2n_blob *shared_secret) | ||
{ | ||
Comment on lines
+79
to
80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to be this complicated? Why wouldn't 411d852 work? I checked, and that passes s2n_tls13_pure_pq_shared_secret_test. It's just my suggestions from #5468 (comment), except we don't even need the branch on send_kem_first. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah okay switching to your proposal, I was confused on your suggestion. |
||
POSIX_ENSURE_REF(conn); | ||
POSIX_ENSURE_REF(shared_secret); | ||
|
@@ -93,15 +95,6 @@ int s2n_tls13_compute_pq_hybrid_shared_secret(struct s2n_connection *conn, struc | |
struct s2n_ecc_evp_params *client_ecc_params = &client_kem_group_params->ecc_params; | ||
POSIX_ENSURE_REF(client_ecc_params); | ||
|
||
DEFER_CLEANUP(struct s2n_blob ecdhe_shared_secret = { 0 }, s2n_free_or_wipe); | ||
|
||
/* Compute the ECDHE shared secret, and retrieve the PQ shared secret. */ | ||
if (conn->mode == S2N_CLIENT) { | ||
POSIX_GUARD(s2n_ecc_evp_compute_shared_secret_from_params(client_ecc_params, server_ecc_params, &ecdhe_shared_secret)); | ||
} else { | ||
POSIX_GUARD(s2n_ecc_evp_compute_shared_secret_from_params(server_ecc_params, client_ecc_params, &ecdhe_shared_secret)); | ||
} | ||
|
||
struct s2n_blob *pq_shared_secret = &client_kem_group_params->kem_params.shared_secret; | ||
POSIX_ENSURE_REF(pq_shared_secret); | ||
POSIX_ENSURE_REF(pq_shared_secret->data); | ||
|
@@ -110,6 +103,16 @@ int s2n_tls13_compute_pq_hybrid_shared_secret(struct s2n_connection *conn, struc | |
POSIX_ENSURE_REF(negotiated_kem_group); | ||
POSIX_ENSURE_REF(negotiated_kem_group->kem); | ||
|
||
DEFER_CLEANUP(struct s2n_blob ecdhe_shared_secret = { 0 }, s2n_free_or_wipe); | ||
|
||
if (negotiated_kem_group->curve == &s2n_ecc_curve_none) { | ||
POSIX_ENSURE_EQ(ecdhe_shared_secret.size, 0); | ||
} else if (conn->mode == S2N_CLIENT) { | ||
POSIX_GUARD(s2n_ecc_evp_compute_shared_secret_from_params(client_ecc_params, server_ecc_params, &ecdhe_shared_secret)); | ||
} else { | ||
POSIX_GUARD(s2n_ecc_evp_compute_shared_secret_from_params(server_ecc_params, client_ecc_params, &ecdhe_shared_secret)); | ||
} | ||
|
||
POSIX_ENSURE_EQ(pq_shared_secret->size, negotiated_kem_group->kem->shared_secret_key_length); | ||
|
||
/* Construct the concatenated/hybrid shared secret */ | ||
|
@@ -139,7 +142,7 @@ int s2n_tls13_compute_shared_secret(struct s2n_connection *conn, struct s2n_blob | |
POSIX_ENSURE_REF(conn); | ||
|
||
if (s2n_tls13_pq_hybrid_supported(conn)) { | ||
POSIX_GUARD(s2n_tls13_compute_pq_hybrid_shared_secret(conn, shared_secret)); | ||
POSIX_GUARD(s2n_tls13_compute_pq_shared_secret(conn, shared_secret)); | ||
} else { | ||
POSIX_GUARD(s2n_tls13_compute_ecc_shared_secret(conn, shared_secret)); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,7 @@ | |
#define TLS_PQ_KEM_GROUP_ID_SECP256R1_MLKEM_768 0x11EB | ||
#define TLS_PQ_KEM_GROUP_ID_X25519_MLKEM_768 0x11EC | ||
#define TLS_PQ_KEM_GROUP_ID_SECP384R1_MLKEM_1024 0x11ED | ||
#define TLS_PQ_KEM_GROUP_ID_MLKEM_1024 0x0202 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Value found from https://datatracker.ietf.org/doc/html/draft-ietf-tls-mlkem-04 section 4.1 |
||
#define TLS_PQ_KEM_GROUP_ID_X25519_KYBER_512_R3 0x2F39 | ||
#define TLS_PQ_KEM_GROUP_ID_SECP256R1_KYBER_512_R3 0x2F3A | ||
#define TLS_PQ_KEM_GROUP_ID_SECP384R1_KYBER_768_R3 0x2F3C | ||
|
Uh oh!
There was an error while loading. Please reload this page.