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 FromStr representations of kdf, kem, and aead #40

Merged
merged 3 commits into from
Aug 10, 2023
Merged
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
25 changes: 23 additions & 2 deletions src/aead.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::IdLookupError;
use num_enum::TryFromPrimitive;
use std::str::FromStr;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

use num_enum::TryFromPrimitive;

/**
Aead represents an authenticated encryption with additional data
encryption function, as per [RFC9180§7.3](https://www.rfc-editor.org/rfc/rfc9180.html#section-7.3)
Expand All @@ -29,6 +31,25 @@ pub enum Aead {
ChaCha20Poly1305 = 3,
}

impl FromStr for Aead {
type Err = IdLookupError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match &*s.to_lowercase() {
#[cfg(feature = "aead-aes-gcm-128")]
"aesgcm128" | "aes-gcm-128" | "aes-128-gcm" => Ok(Self::AesGcm128),
#[cfg(feature = "aead-aes-gcm-256")]
"aesgcm256" | "aes-gcm-256" | "aes-256-gcm" => Ok(Self::AesGcm256),
#[cfg(feature = "aead-chacha-20-poly-1305")]
"chacha20poly1305"
| "chacha-20-poly-1305"
| "cha-cha-20-poly-1305"
| "chacha20-poly1305" => Ok(Self::ChaCha20Poly1305),
_ => Err(IdLookupError("aead not recognized")),
}
}
}

/// An iterable slice of [`Aead`] variants
pub const AEAD_ALL: &[Aead] = &[
#[cfg(feature = "aead-aes-gcm-128")]
Expand Down
22 changes: 20 additions & 2 deletions src/kdf.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::IdLookupError;
use num_enum::TryFromPrimitive;
use std::str::FromStr;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

use num_enum::TryFromPrimitive;

/**
Kdf represents an key derivation function, as per
[RFC9180§7.2](https://www.rfc-editor.org/rfc/rfc9180.html#section-7.2)
Expand All @@ -29,6 +31,22 @@ pub enum Kdf {
Sha512 = 3,
}

impl FromStr for Kdf {
type Err = IdLookupError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match &*s.to_lowercase() {
#[cfg(feature = "kdf-sha256")]
"sha256" | "sha-256" => Ok(Self::Sha256),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to include the "HKDF-" prefix in these identifiers for completeness and future-proofing. See RFC9180, Table 3.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a second commit (869c6d8) that handles hkdf-sha256 and HkdfSha256

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we require the prefix?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think accepting either is probably fine.

#[cfg(feature = "kdf-sha384")]
"sha384" | "sha-384" => Ok(Self::Sha384),
#[cfg(feature = "kdf-sha512")]
"sha512" | "sha-512" => Ok(Self::Sha512),
_ => Err(IdLookupError("kdf not recognized")),
}
}
}

/// An iterable slice of [`Kdf`] variants
pub const KDF_ALL: &[Kdf] = &[
#[cfg(feature = "kdf-sha256")]
Expand Down
25 changes: 22 additions & 3 deletions src/kem.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::{IdLookupError, Keypair};
use num_enum::TryFromPrimitive;
use std::str::FromStr;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

use crate::Keypair;
use num_enum::TryFromPrimitive;

/**
Kem represents an asymmetric key encapsulation mechanism, as per
[RFC9180§7.1][section-7.1]. Currently only two of options listed in
Expand Down Expand Up @@ -31,6 +32,24 @@ pub enum Kem {
X25519HkdfSha256 = 32,
}

impl FromStr for Kem {
type Err = IdLookupError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match &*s.to_lowercase() {
#[cfg(feature = "kem-dh-p256-hkdf-sha256")]
"dhp256hkdfsha256" | "dh-p256-hkdf-sha256" | "dhkem(p-256, hkdf-sha256)" => {
Ok(Self::DhP256HkdfSha256)
}
#[cfg(feature = "kem-x25519-hkdf-sha256")]
"x25519hkdfsha256" | "x25519-hkdf-sha256" | "dhkem(x25519, hkdf-sha256)" => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the shorter nicknames for this and the P-256-based KEM are inconsistent on whether they include "dh-"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's your suggested resolution to this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend dropping "dh" from "dhp256hkdfsha256". (I think it would be unusual to abbreviate DHKEM to DH, and more natural to omit the DHKEM part altogether)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it's worth raising this with the backing crate as well. I just cargo-culted the naming from hpke::kem::DhP256HkdfSha256

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in eab5053 — i also added versions that don't label the kdf, so p256-sha256 and x25519-sha256. let me know if that's somehow inaccurate or if there are other uses of sha256 as part of these kems other than hkdf

Ok(Self::X25519HkdfSha256)
}
_ => Err(IdLookupError("kem not recognized")),
}
}
}

impl Kem {
/// generate a [`Keypair`] for this [`Config`] or [`Kem`].
#[must_use]
Expand Down
Loading