use crate::error::{JoseError, Result};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum JwsAlgorithm {
HS256,
HS384,
HS512,
RS256,
RS384,
RS512,
PS256,
PS384,
PS512,
ES256,
ES384,
ES512,
EdDSA,
ES256K,
#[cfg(feature = "post-quantum")]
#[serde(rename = "ML-DSA-44")]
MlDsa44,
#[cfg(feature = "post-quantum")]
#[serde(rename = "ML-DSA-65")]
MlDsa65,
#[cfg(feature = "post-quantum")]
#[serde(rename = "ML-DSA-87")]
MlDsa87,
#[cfg(feature = "deprecated")]
None,
}
#[allow(clippy::should_implement_trait)] impl JwsAlgorithm {
pub fn to_crypto(self) -> Result<kryptering::SignatureAlgorithm> {
use kryptering::{EcCurve, HashAlgorithm, SignatureAlgorithm};
match self {
Self::HS256 => Ok(SignatureAlgorithm::Hmac(HashAlgorithm::Sha256)),
Self::HS384 => Ok(SignatureAlgorithm::Hmac(HashAlgorithm::Sha384)),
Self::HS512 => Ok(SignatureAlgorithm::Hmac(HashAlgorithm::Sha512)),
Self::RS256 => Ok(SignatureAlgorithm::RsaPkcs1v15(HashAlgorithm::Sha256)),
Self::RS384 => Ok(SignatureAlgorithm::RsaPkcs1v15(HashAlgorithm::Sha384)),
Self::RS512 => Ok(SignatureAlgorithm::RsaPkcs1v15(HashAlgorithm::Sha512)),
Self::PS256 => Ok(SignatureAlgorithm::RsaPss(HashAlgorithm::Sha256)),
Self::PS384 => Ok(SignatureAlgorithm::RsaPss(HashAlgorithm::Sha384)),
Self::PS512 => Ok(SignatureAlgorithm::RsaPss(HashAlgorithm::Sha512)),
Self::ES256 => Ok(SignatureAlgorithm::Ecdsa(
EcCurve::P256,
HashAlgorithm::Sha256,
)),
Self::ES384 => Ok(SignatureAlgorithm::Ecdsa(
EcCurve::P384,
HashAlgorithm::Sha384,
)),
Self::ES512 => Ok(SignatureAlgorithm::Ecdsa(
EcCurve::P521,
HashAlgorithm::Sha512,
)),
Self::EdDSA => Ok(SignatureAlgorithm::Ed25519),
Self::ES256K => Err(JoseError::UnsupportedAlgorithm(
"ES256K (secp256k1) is not yet supported by kryptering".into(),
)),
#[cfg(feature = "post-quantum")]
Self::MlDsa44 => Ok(SignatureAlgorithm::MlDsa(kryptering::MlDsaVariant::MlDsa44)),
#[cfg(feature = "post-quantum")]
Self::MlDsa65 => Ok(SignatureAlgorithm::MlDsa(kryptering::MlDsaVariant::MlDsa65)),
#[cfg(feature = "post-quantum")]
Self::MlDsa87 => Ok(SignatureAlgorithm::MlDsa(kryptering::MlDsaVariant::MlDsa87)),
#[cfg(feature = "deprecated")]
Self::None => Err(JoseError::UnsupportedAlgorithm(
"\"none\" algorithm has no cryptographic operation".into(),
)),
}
}
pub fn from_str(s: &str) -> Result<Self> {
match s {
"HS256" => Ok(Self::HS256),
"HS384" => Ok(Self::HS384),
"HS512" => Ok(Self::HS512),
"RS256" => Ok(Self::RS256),
"RS384" => Ok(Self::RS384),
"RS512" => Ok(Self::RS512),
"PS256" => Ok(Self::PS256),
"PS384" => Ok(Self::PS384),
"PS512" => Ok(Self::PS512),
"ES256" => Ok(Self::ES256),
"ES384" => Ok(Self::ES384),
"ES512" => Ok(Self::ES512),
"EdDSA" => Ok(Self::EdDSA),
"ES256K" => Ok(Self::ES256K),
#[cfg(feature = "post-quantum")]
"ML-DSA-44" => Ok(Self::MlDsa44),
#[cfg(feature = "post-quantum")]
"ML-DSA-65" => Ok(Self::MlDsa65),
#[cfg(feature = "post-quantum")]
"ML-DSA-87" => Ok(Self::MlDsa87),
#[cfg(feature = "deprecated")]
"none" => Ok(Self::None),
other => Err(JoseError::UnsupportedAlgorithm(other.to_string())),
}
}
pub fn as_str(self) -> &'static str {
match self {
Self::HS256 => "HS256",
Self::HS384 => "HS384",
Self::HS512 => "HS512",
Self::RS256 => "RS256",
Self::RS384 => "RS384",
Self::RS512 => "RS512",
Self::PS256 => "PS256",
Self::PS384 => "PS384",
Self::PS512 => "PS512",
Self::ES256 => "ES256",
Self::ES384 => "ES384",
Self::ES512 => "ES512",
Self::EdDSA => "EdDSA",
Self::ES256K => "ES256K",
#[cfg(feature = "post-quantum")]
Self::MlDsa44 => "ML-DSA-44",
#[cfg(feature = "post-quantum")]
Self::MlDsa65 => "ML-DSA-65",
#[cfg(feature = "post-quantum")]
Self::MlDsa87 => "ML-DSA-87",
#[cfg(feature = "deprecated")]
Self::None => "none",
}
}
}
impl std::fmt::Display for JwsAlgorithm {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum JweAlgorithm {
#[serde(rename = "dir")]
Dir,
A128KW,
A192KW,
A256KW,
#[cfg(feature = "deprecated")]
#[serde(rename = "RSA-OAEP")]
RsaOaep,
#[serde(rename = "RSA-OAEP-256")]
RsaOaep256,
#[serde(rename = "ECDH-ES")]
EcdhEs,
#[serde(rename = "ECDH-ES+A128KW")]
EcdhEsA128kw,
#[serde(rename = "ECDH-ES+A192KW")]
EcdhEsA192kw,
#[serde(rename = "ECDH-ES+A256KW")]
EcdhEsA256kw,
#[serde(rename = "PBES2-HS256+A128KW")]
Pbes2Hs256A128kw,
#[serde(rename = "PBES2-HS384+A192KW")]
Pbes2Hs384A192kw,
#[serde(rename = "PBES2-HS512+A256KW")]
Pbes2Hs512A256kw,
}
#[allow(clippy::should_implement_trait)]
impl JweAlgorithm {
pub fn from_str(s: &str) -> Result<Self> {
match s {
"dir" => Ok(Self::Dir),
"A128KW" => Ok(Self::A128KW),
"A192KW" => Ok(Self::A192KW),
"A256KW" => Ok(Self::A256KW),
#[cfg(feature = "deprecated")]
"RSA-OAEP" => Ok(Self::RsaOaep),
"RSA-OAEP-256" => Ok(Self::RsaOaep256),
"ECDH-ES" => Ok(Self::EcdhEs),
"ECDH-ES+A128KW" => Ok(Self::EcdhEsA128kw),
"ECDH-ES+A192KW" => Ok(Self::EcdhEsA192kw),
"ECDH-ES+A256KW" => Ok(Self::EcdhEsA256kw),
"PBES2-HS256+A128KW" => Ok(Self::Pbes2Hs256A128kw),
"PBES2-HS384+A192KW" => Ok(Self::Pbes2Hs384A192kw),
"PBES2-HS512+A256KW" => Ok(Self::Pbes2Hs512A256kw),
other => Err(JoseError::UnsupportedAlgorithm(other.to_string())),
}
}
pub fn as_str(self) -> &'static str {
match self {
Self::Dir => "dir",
Self::A128KW => "A128KW",
Self::A192KW => "A192KW",
Self::A256KW => "A256KW",
#[cfg(feature = "deprecated")]
Self::RsaOaep => "RSA-OAEP",
Self::RsaOaep256 => "RSA-OAEP-256",
Self::EcdhEs => "ECDH-ES",
Self::EcdhEsA128kw => "ECDH-ES+A128KW",
Self::EcdhEsA192kw => "ECDH-ES+A192KW",
Self::EcdhEsA256kw => "ECDH-ES+A256KW",
Self::Pbes2Hs256A128kw => "PBES2-HS256+A128KW",
Self::Pbes2Hs384A192kw => "PBES2-HS384+A192KW",
Self::Pbes2Hs512A256kw => "PBES2-HS512+A256KW",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum JweEncryption {
#[serde(rename = "A128CBC-HS256")]
A128CbcHs256,
#[serde(rename = "A192CBC-HS384")]
A192CbcHs384,
#[serde(rename = "A256CBC-HS512")]
A256CbcHs512,
A128GCM,
A192GCM,
A256GCM,
}
#[allow(clippy::should_implement_trait)]
impl JweEncryption {
pub fn cek_size(self) -> usize {
match self {
Self::A128CbcHs256 => 32, Self::A192CbcHs384 => 48, Self::A256CbcHs512 => 64, Self::A128GCM => 16,
Self::A192GCM => 24,
Self::A256GCM => 32,
}
}
pub fn from_str(s: &str) -> Result<Self> {
match s {
"A128CBC-HS256" => Ok(Self::A128CbcHs256),
"A192CBC-HS384" => Ok(Self::A192CbcHs384),
"A256CBC-HS512" => Ok(Self::A256CbcHs512),
"A128GCM" => Ok(Self::A128GCM),
"A192GCM" => Ok(Self::A192GCM),
"A256GCM" => Ok(Self::A256GCM),
other => Err(JoseError::UnsupportedAlgorithm(other.to_string())),
}
}
pub fn as_str(self) -> &'static str {
match self {
Self::A128CbcHs256 => "A128CBC-HS256",
Self::A192CbcHs384 => "A192CBC-HS384",
Self::A256CbcHs512 => "A256CBC-HS512",
Self::A128GCM => "A128GCM",
Self::A192GCM => "A192GCM",
Self::A256GCM => "A256GCM",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn es256k_from_str() {
let alg = JwsAlgorithm::from_str("ES256K").unwrap();
assert_eq!(alg, JwsAlgorithm::ES256K);
}
#[test]
fn es256k_as_str() {
assert_eq!(JwsAlgorithm::ES256K.as_str(), "ES256K");
}
#[test]
fn es256k_to_crypto_returns_error() {
let result = JwsAlgorithm::ES256K.to_crypto();
assert!(result.is_err());
}
#[test]
fn es256k_roundtrip() {
let alg = JwsAlgorithm::from_str(JwsAlgorithm::ES256K.as_str()).unwrap();
assert_eq!(alg, JwsAlgorithm::ES256K);
}
#[test]
fn existing_algorithms_to_crypto_still_work() {
assert!(JwsAlgorithm::HS256.to_crypto().is_ok());
assert!(JwsAlgorithm::RS256.to_crypto().is_ok());
assert!(JwsAlgorithm::ES256.to_crypto().is_ok());
assert!(JwsAlgorithm::EdDSA.to_crypto().is_ok());
}
}