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

WIP: secp256k1 DHKEM #52

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Filled in k256 KEM and most automated tests
  • Loading branch information
rozbb committed Nov 16, 2023
commit 469dd349f444c6eba84761be8aa9fd1f94aa51fc
29 changes: 29 additions & 0 deletions src/aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,35 @@ mod test {
);
}

#[cfg(all(feature = "p256", any(feature = "alloc", feature = "std")))]
mod k256_tests {
use super::*;

test_export_idempotence!(test_export_idempotence_k256, crate::kem::DhP256HkdfSha256);
test_exportonly_panics!(
test_exportonly_panics_k256_seal,
test_exportonly_panics_k256_open,
crate::kem::DhK256HkdfSha256
);
test_overflow!(test_overflow_k256, crate::kem::DhK256HkdfSha256);

test_ctx_correctness!(
test_ctx_correctness_aes128_k256,
AesGcm128,
crate::kem::DhK256HkdfSha256
);
test_ctx_correctness!(
test_ctx_correctness_aes256_k256,
AesGcm256,
crate::kem::DhK256HkdfSha256
);
test_ctx_correctness!(
test_ctx_correctness_chacha_k256,
ChaCha20Poly1305,
crate::kem::DhK256HkdfSha256
);
}

/// Tests that Serialize::write_exact() panics when given a buffer of incorrect length
#[should_panic]
#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/dhkex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub trait DhKeyExchange {
}

#[cfg(any(feature = "p256", feature = "p384", feature = "k256"))]
pub(crate) mod ecdh_nistp;
pub(crate) mod ecdh_nist;

#[cfg(feature = "x25519")]
pub(crate) mod x25519;
29 changes: 26 additions & 3 deletions src/dhkex/ecdh_nistp.rs → src/dhkex/ecdh_nist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ nistp_dhkex!(
mod tests {
use crate::{dhkex::DhKeyExchange, test_util::dhkex_gen_keypair, Deserializable, Serializable};

#[cfg(feature = "k256")]
use super::k256::DhK256;
#[cfg(feature = "p256")]
use super::p256::DhP256;
#[cfg(feature = "p384")]
Expand Down Expand Up @@ -437,6 +439,12 @@ mod tests {
fn test_vector_ecdh_p384() {
test_vector_ecdh::<DhP384>(&P384_PRIVKEYS[0], &P384_PUBKEYS[1], &P384_DH_RES_XCOORD);
}
#[cfg(feature = "k256")]
#[test]
fn test_vector_ecdh_k256() {
// TODO: Get some test vectors
test_vector_ecdh::<DhK256>(todo!(), todo!(), todo!());
}

#[cfg(feature = "p256")]
#[test]
Expand All @@ -448,6 +456,12 @@ mod tests {
fn test_vector_corresponding_pubkey_p384() {
test_vector_corresponding_pubkey::<DhP384>(P384_PRIVKEYS, P384_PUBKEYS);
}
#[cfg(feature = "k256")]
#[test]
fn test_vector_corresponding_pubkey_k256() {
// TODO: Get some test vectors
test_vector_corresponding_pubkey::<DhK256>(todo!(), todo!());
}

#[cfg(feature = "p256")]
#[test]
Expand All @@ -459,16 +473,25 @@ mod tests {
fn test_pubkey_serialize_correctness_p384() {
test_pubkey_serialize_correctness::<DhP384>();
}
#[cfg(feature = "k256")]
#[test]
fn test_pubkey_serialize_correctness_k256() {
test_pubkey_serialize_correctness::<DhK256>();
}

#[cfg(feature = "256")]
#[cfg(feature = "p256")]
#[test]
fn test_dh_serialize_correctness_p256() {
test_dh_serialize_correctness::<DhP256>();
}

#[cfg(feature = "384")]
#[cfg(feature = "p384")]
#[test]
fn test_dh_serialize_correctness_p384() {
test_dh_serialize_correctness::<DhP384>();
}
#[cfg(feature = "k256")]
#[test]
fn test_dh_serialize_correctness_k256() {
test_dh_serialize_correctness::<DhK256>();
}
}
8 changes: 8 additions & 0 deletions src/kem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,12 @@ mod tests {
test_encap_correctness!(test_encap_correctness_p384, crate::kem::DhP384HkdfSha384);
test_encapped_serialize!(test_encapped_serialize_p384, crate::kem::DhP384HkdfSha384);
}

#[cfg(feature = "k256")]
mod k256_tests {
use super::*;

test_encap_correctness!(test_encap_correctness_k256, crate::kem::DhK256HkdfSha256);
test_encapped_serialize!(test_encapped_serialize_k256, crate::kem::DhK256HkdfSha256);
}
}
15 changes: 13 additions & 2 deletions src/kem/dhkem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ impl_dhkem!(
impl_dhkem!(
dhp256_hkdfsha256,
DhP256HkdfSha256,
crate::dhkex::ecdh_nistp::p256::DhP256,
crate::dhkex::ecdh_nist::p256::DhP256,
crate::kdf::HkdfSha256,
0x0010,
"Represents DHKEM(P-256, HKDF-SHA256)"
Expand All @@ -375,8 +375,19 @@ impl_dhkem!(
impl_dhkem!(
dhp384_hkdfsha384,
DhP384HkdfSha384,
crate::dhkex::ecdh_nistp::p384::DhP384,
crate::dhkex::ecdh_nist::p384::DhP384,
crate::kdf::HkdfSha384,
0x0011,
"Represents DHKEM(P-384, HKDF-SHA384)"
);

// Implement DHKEM(K-256, HKDF-SHA256)
#[cfg(feature = "k256")]
impl_dhkem!(
dhk256_hkdfsha256,
DhK256HkdfSha256,
crate::dhkex::ecdh_nist::k256::DhK256,
crate::kdf::HkdfSha256,
0x0016,
"Represents DHKEM(K-256, HKDF-SHA256)"
);
18 changes: 18 additions & 0 deletions src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,22 @@ mod test {
crate::kem::dhp384_hkdfsha384::DhP384HkdfSha384
);
}

#[cfg(feature = "k256")]
mod k256_tests {
use super::*;

test_setup_correctness!(
test_setup_correctness_k256,
ChaCha20Poly1305,
HkdfSha256,
crate::kem::dhk256_hkdfsha256::DhK256HkdfSha256
);
test_setup_soundness!(
test_setup_soundness_k256,
ChaCha20Poly1305,
HkdfSha256,
crate::kem::dhk256_hkdfsha256::DhK256HkdfSha256
);
}
}
8 changes: 8 additions & 0 deletions src/single_shot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,12 @@ mod test {
crate::kdf::HkdfSha384,
crate::kem::dhp384_hkdfsha384::DhP384HkdfSha384
);

#[cfg(feature = "k256")]
test_single_shot_correctness!(
test_single_shot_correctness_k256,
ChaCha20Poly1305,
crate::kdf::HkdfSha256,
crate::kem::dhk256_hkdfsha256::DhK256HkdfSha256
);
}