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 post-quantum key agreement X25519MLKEM768 #21445

Merged
merged 1 commit into from
Sep 24, 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
4 changes: 2 additions & 2 deletions lib/std/crypto/tls.zig
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ pub const NamedGroup = enum(u16) {
ffdhe8192 = 0x0104,

// Hybrid post-quantum key agreements
x25519_kyber512d00 = 0xFE30,
x25519_kyber768d00 = 0x6399,
secp256r1_ml_kem256 = 0x11EB,
x25519_ml_kem768 = 0x11EC,

_,
};
Expand Down
14 changes: 7 additions & 7 deletions lib/std/crypto/tls/Client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) In
// Only possible to happen if the private key is all zeroes.
error.IdentityElement => return error.InsufficientEntropy,
};
const kyber768_kp = crypto.kem.kyber_d00.Kyber768.KeyPair.create(null) catch {};
const ml_kem768_kp = crypto.kem.ml_kem.MLKem768.KeyPair.create(null) catch {};

const extensions_payload =
tls.extension(.supported_versions, [_]u8{
Expand All @@ -172,7 +172,7 @@ pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) In
.rsa_pss_rsae_sha512,
.ed25519,
})) ++ tls.extension(.supported_groups, enum_array(tls.NamedGroup, &.{
.x25519_kyber768d00,
.x25519_ml_kem768,
.secp256r1,
.x25519,
})) ++ tls.extension(
Expand All @@ -181,8 +181,8 @@ pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) In
array(1, x25519_kp.public_key) ++
int2(@intFromEnum(tls.NamedGroup.secp256r1)) ++
array(1, secp256r1_kp.public_key.toUncompressedSec1()) ++
int2(@intFromEnum(tls.NamedGroup.x25519_kyber768d00)) ++
array(1, x25519_kp.public_key ++ kyber768_kp.public_key.toBytes())),
int2(@intFromEnum(tls.NamedGroup.x25519_ml_kem768)) ++
array(1, x25519_kp.public_key ++ ml_kem768_kp.public_key.toBytes())),
) ++
int2(@intFromEnum(tls.ExtensionType.server_name)) ++
int2(host_len + 5) ++ // byte length of this extension payload
Expand Down Expand Up @@ -298,17 +298,17 @@ pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) In
const key_size = extd.decode(u16);
try extd.ensure(key_size);
switch (named_group) {
.x25519_kyber768d00 => {
.x25519_ml_kem768 => {
const xksl = crypto.dh.X25519.public_length;
const hksl = xksl + crypto.kem.kyber_d00.Kyber768.ciphertext_length;
const hksl = xksl + crypto.kem.ml_kem.MLKem768.ciphertext_length;
if (key_size != hksl)
return error.TlsIllegalParameter;
const server_ks = extd.array(hksl);

shared_key = &((crypto.dh.X25519.scalarmult(
x25519_kp.secret_key,
server_ks[0..xksl].*,
) catch return error.TlsDecryptFailure) ++ (kyber768_kp.secret_key.decaps(
) catch return error.TlsDecryptFailure) ++ (ml_kem768_kp.secret_key.decaps(
server_ks[xksl..hksl],
) catch return error.TlsDecryptFailure));
},
Expand Down