From 018fe224b5309baa931210695bd212b8de060340 Mon Sep 17 00:00:00 2001 From: Jacob Rothstein Date: Wed, 9 Aug 2023 13:15:27 -0700 Subject: [PATCH 1/3] add FromStr representations of kdf, kem, and aead --- src/aead.rs | 25 +++++++++++++++++++++++-- src/kdf.rs | 22 ++++++++++++++++++++-- src/kem.rs | 25 ++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/aead.rs b/src/aead.rs index 4c22888f16e..36f26c227d2 100644 --- a/src/aead.rs +++ b/src/aead.rs @@ -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) @@ -29,6 +31,25 @@ pub enum Aead { ChaCha20Poly1305 = 3, } +impl FromStr for Aead { + type Err = IdLookupError; + + fn from_str(s: &str) -> Result { + 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")] diff --git a/src/kdf.rs b/src/kdf.rs index 890c3d99bbe..c2bbf1a9620 100644 --- a/src/kdf.rs +++ b/src/kdf.rs @@ -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) @@ -29,6 +31,22 @@ pub enum Kdf { Sha512 = 3, } +impl FromStr for Kdf { + type Err = IdLookupError; + + fn from_str(s: &str) -> Result { + match &*s.to_lowercase() { + #[cfg(feature = "kdf-sha256")] + "sha256" | "sha-256" => Ok(Self::Sha256), + #[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")] diff --git a/src/kem.rs b/src/kem.rs index 98247e42e2c..e8253d7b8e5 100644 --- a/src/kem.rs +++ b/src/kem.rs @@ -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 @@ -31,6 +32,24 @@ pub enum Kem { X25519HkdfSha256 = 32, } +impl FromStr for Kem { + type Err = IdLookupError; + + fn from_str(s: &str) -> Result { + 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)" => { + Ok(Self::X25519HkdfSha256) + } + _ => Err(IdLookupError("kem not recognized")), + } + } +} + impl Kem { /// generate a [`Keypair`] for this [`Config`] or [`Kem`]. #[must_use] From 869c6d87a7bb7b3f135b54c02a63bd3f1fb3590b Mon Sep 17 00:00:00 2001 From: Jacob Rothstein Date: Wed, 9 Aug 2023 16:33:22 -0700 Subject: [PATCH 2/3] remove dashes before comparing, add hkdf-sha-nnn options --- src/aead.rs | 11 ++++------- src/kdf.rs | 8 ++++---- src/kem.rs | 10 +++------- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/aead.rs b/src/aead.rs index 36f26c227d2..c50b564eedb 100644 --- a/src/aead.rs +++ b/src/aead.rs @@ -35,16 +35,13 @@ impl FromStr for Aead { type Err = IdLookupError; fn from_str(s: &str) -> Result { - match &*s.to_lowercase() { + match &*s.to_lowercase().replace('-', "") { #[cfg(feature = "aead-aes-gcm-128")] - "aesgcm128" | "aes-gcm-128" | "aes-128-gcm" => Ok(Self::AesGcm128), + "aesgcm128" | "aes128gcm" => Ok(Self::AesGcm128), #[cfg(feature = "aead-aes-gcm-256")] - "aesgcm256" | "aes-gcm-256" | "aes-256-gcm" => Ok(Self::AesGcm256), + "aesgcm256" | "aes256gcm" => 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), + "chacha20poly1305" => Ok(Self::ChaCha20Poly1305), _ => Err(IdLookupError("aead not recognized")), } } diff --git a/src/kdf.rs b/src/kdf.rs index c2bbf1a9620..32102683f31 100644 --- a/src/kdf.rs +++ b/src/kdf.rs @@ -35,13 +35,13 @@ impl FromStr for Kdf { type Err = IdLookupError; fn from_str(s: &str) -> Result { - match &*s.to_lowercase() { + match &*s.to_lowercase().replace('-', "") { #[cfg(feature = "kdf-sha256")] - "sha256" | "sha-256" => Ok(Self::Sha256), + "hkdfsha256" | "sha256" => Ok(Self::Sha256), #[cfg(feature = "kdf-sha384")] - "sha384" | "sha-384" => Ok(Self::Sha384), + "hkdfsha384" | "sha384" => Ok(Self::Sha384), #[cfg(feature = "kdf-sha512")] - "sha512" | "sha-512" => Ok(Self::Sha512), + "hkdfsha512" | "sha512" => Ok(Self::Sha512), _ => Err(IdLookupError("kdf not recognized")), } } diff --git a/src/kem.rs b/src/kem.rs index e8253d7b8e5..621f8158cda 100644 --- a/src/kem.rs +++ b/src/kem.rs @@ -36,15 +36,11 @@ impl FromStr for Kem { type Err = IdLookupError; fn from_str(s: &str) -> Result { - match &*s.to_lowercase() { + match &*s.to_lowercase().replace('-', "") { #[cfg(feature = "kem-dh-p256-hkdf-sha256")] - "dhp256hkdfsha256" | "dh-p256-hkdf-sha256" | "dhkem(p-256, hkdf-sha256)" => { - Ok(Self::DhP256HkdfSha256) - } + "dhp256hkdfsha256" | "dhkem(p256, hkdfsha256)" => Ok(Self::DhP256HkdfSha256), #[cfg(feature = "kem-x25519-hkdf-sha256")] - "x25519hkdfsha256" | "x25519-hkdf-sha256" | "dhkem(x25519, hkdf-sha256)" => { - Ok(Self::X25519HkdfSha256) - } + "x25519hkdfsha256" | "dhkem(x25519, hkdfsha256)" => Ok(Self::X25519HkdfSha256), _ => Err(IdLookupError("kem not recognized")), } } From eab505301211f4552db18a4c241933909aaafd4f Mon Sep 17 00:00:00 2001 From: Jacob Rothstein Date: Thu, 10 Aug 2023 10:59:06 -0700 Subject: [PATCH 3/3] add p256-sha256, x25519-sha256, dhkem-x25519-hkdf-sha256, and dhkem-p256-hkdf-sha256 --- src/kem.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/kem.rs b/src/kem.rs index 621f8158cda..a63ff564136 100644 --- a/src/kem.rs +++ b/src/kem.rs @@ -38,9 +38,14 @@ impl FromStr for Kem { fn from_str(s: &str) -> Result { match &*s.to_lowercase().replace('-', "") { #[cfg(feature = "kem-dh-p256-hkdf-sha256")] - "dhp256hkdfsha256" | "dhkem(p256, hkdfsha256)" => Ok(Self::DhP256HkdfSha256), + "p256sha256" | "dhkemp256hkdfsha256" | "p256hkdfsha256" | "dhkem(p256, hkdfsha256)" => { + Ok(Self::DhP256HkdfSha256) + } #[cfg(feature = "kem-x25519-hkdf-sha256")] - "x25519hkdfsha256" | "dhkem(x25519, hkdfsha256)" => Ok(Self::X25519HkdfSha256), + "x25519sha256" + | "dhkemx25519hkdfsha256" + | "x25519hkdfsha256" + | "dhkem(x25519, hkdfsha256)" => Ok(Self::X25519HkdfSha256), _ => Err(IdLookupError("kem not recognized")), } }