Skip to content

Commit

Permalink
Add support for other two NIST curves
Browse files Browse the repository at this point in the history
  • Loading branch information
divergentdave committed Jul 10, 2024
1 parent 399cb0e commit c6498c1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ kdf-all = ["kdf-sha256", "kdf-sha384", "kdf-sha512"]
kdf-sha256 = []
kdf-sha384 = []
kdf-sha512 = []
kem-all = ["kem-dh-p256-hkdf-sha256", "kem-x25519-hkdf-sha256"]
kem-all = ["kem-dh-p256-hkdf-sha256", "kem-dh-p384-hkdf-sha384", "kem-dh-p521-hkdf-sha512", "kem-x25519-hkdf-sha256"]
kem-dh-p256-hkdf-sha256 = ["hpke/p256"]
kem-dh-p384-hkdf-sha384 = ["hpke/p384"]
kem-dh-p521-hkdf-sha512 = ["hpke/p521"]
kem-x25519-hkdf-sha256 = ["hpke/x25519"]

[dependencies]
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ from rust, even when opting out of `algo-all`.
* *kdf-all*: Enables `kdf-sha256`, `kdf-sha384`, `kdf-sha512`
algorithm features. Enabled by default.

* *kem-all*: Enables both `kem-dh-p256-hkdf-sha256` and
* *kem-all*: Enables `kem-dh-p256-hkdf-sha256`,
`kem-dh-p384-hkdf-sha384`, `kem-dh-p521-hkdf-sha512`, and
`kem-x25519-hkdf-sha256` algorithm features. Enabled by default.

* *serde*: enables derived serde serialization and deserialization for
Expand Down
22 changes: 21 additions & 1 deletion src/kem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use wasm_bindgen::prelude::*;

/**
Kem represents an asymmetric key encapsulation mechanism, as per
[RFC9180§7.1][section-7.1]. Currently only two of options listed in
[RFC9180§7.1][section-7.1]. Currently only four of the options listed in
the hpke draft are available.
[section-7.1]: https://www.rfc-editor.org/rfc/rfc9180.html#section-7.1
Expand All @@ -27,6 +27,14 @@ pub enum Kem {
#[cfg(feature = "kem-dh-p256-hkdf-sha256")]
DhP256HkdfSha256 = 16,

/// DHKEM(P-384, HKDF-SHA384) [NISTCurves](https://doi.org/10.6028/nist.fips.186-4)
#[cfg(feature = "kem-dh-p384-hkdf-sha384")]
DhP384HkdfSha384 = 17,

/// DHKEM(P-521, HKDF-SHA512) [NISTCurves](https://doi.org/10.6028/nist.fips.186-4)
#[cfg(feature = "kem-dh-p521-hkdf-sha512")]
DhP521HkdfSha512 = 18,

/// DHKEM(X25519, HKDF-SHA256) [RFC7748](https://www.rfc-editor.org/info/rfc7748)
#[cfg(feature = "kem-x25519-hkdf-sha256")]
X25519HkdfSha256 = 32,
Expand All @@ -41,6 +49,14 @@ impl FromStr for Kem {
"p256sha256" | "dhkemp256hkdfsha256" | "p256hkdfsha256" | "dhkem(p256, hkdfsha256)" => {
Ok(Self::DhP256HkdfSha256)
}
#[cfg(feature = "kem-dh-p384-hkdf-sha384")]
"p384sha384" | "dhkemp384hkdfsha384" | "p384hkdfsha384" | "dhkem(p384, hkdfsha384)" => {
Ok(Self::DhP384HkdfSha384)
}
#[cfg(feature = "kem-dh-p521-hkdf-sha512")]
"p521sha512" | "dhkemp521hkdfsha512" | "p521hkdfsha512" | "dhkem(p521, hkdfsha512)" => {
Ok(Self::DhP521HkdfSha512)
}
#[cfg(feature = "kem-x25519-hkdf-sha256")]
"x25519sha256"
| "dhkemx25519hkdfsha256"
Expand All @@ -63,6 +79,10 @@ impl Kem {
pub const KEM_ALL: &[Kem] = &[
#[cfg(feature = "kem-dh-p256-hkdf-sha256")]
Kem::DhP256HkdfSha256,
#[cfg(feature = "kem-dh-p384-hkdf-sha384")]
Kem::DhP384HkdfSha384,
#[cfg(feature = "kem-dh-p521-hkdf-sha512")]
Kem::DhP521HkdfSha512,
#[cfg(feature = "kem-x25519-hkdf-sha256")]
Kem::X25519HkdfSha256,
];
6 changes: 6 additions & 0 deletions src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ pub fn gen_keypair(kem: Kem) -> Keypair {
#[cfg(feature = "kem-dh-p256-hkdf-sha256")]
Kem::DhP256HkdfSha256 => gen_kp::<hpke::kem::DhP256HkdfSha256>(),

#[cfg(feature = "kem-dh-p384-hkdf-sha384")]
Kem::DhP384HkdfSha384 => gen_kp::<hpke::kem::DhP384HkdfSha384>(),

#[cfg(feature = "kem-dh-p521-hkdf-sha512")]
Kem::DhP521HkdfSha512 => gen_kp::<hpke::kem::DhP521HkdfSha512>(),

#[cfg(feature = "kem-x25519-hkdf-sha256")]
Kem::X25519HkdfSha256 => gen_kp::<hpke::kem::X25519HkdfSha256>(),
}
Expand Down
4 changes: 4 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ macro_rules! match_algo {
match $kem {
#[cfg(feature = "kem-dh-p256-hkdf-sha256")]
$crate::Kem::DhP256HkdfSha256 => $fn::<$aead, $kdf, hpke::kem::DhP256HkdfSha256>,
#[cfg(feature = "kem-dh-p384-hkdf-sha384")]
$crate::Kem::DhP384HkdfSha384 => $fn::<$aead, $kdf, hpke::kem::DhP384HkdfSha384>,
#[cfg(feature = "kem-dh-p521-hkdf-sha512")]
$crate::Kem::DhP521HkdfSha512 => $fn::<$aead, $kdf, hpke::kem::DhP521HkdfSha512>,
#[cfg(feature = "kem-x25519-hkdf-sha256")]
$crate::Kem::X25519HkdfSha256 => $fn::<$aead, $kdf, hpke::kem::X25519HkdfSha256>,
}
Expand Down

0 comments on commit c6498c1

Please sign in to comment.