#[cfg(not(feature = "std"))]
use alloc::{format, string::String, string::ToString};
#[cfg(feature = "std")]
use std::string::{String, ToString};
macro_rules! define_identifier_trait {
($(#[$meta:meta])* $name:ident) => {
$(#[$meta])*
pub trait $name {
fn botan_name(&self) -> String;
}
impl $name for str {
fn botan_name(&self) -> String {
self.to_string()
}
}
impl $name for String {
fn botan_name(&self) -> String {
self.clone()
}
}
impl<T: $name + ?Sized> $name for &T {
fn botan_name(&self) -> String {
(*self).botan_name()
}
}
};
}
define_identifier_trait!(
HashAlgorithmIdentifier
);
define_identifier_trait!(
BlockCipherAlgorithmIdentifier
);
define_identifier_trait!(
CipherAlgorithmIdentifier
);
define_identifier_trait!(
MacAlgorithmIdentifier
);
define_identifier_trait!(
KdfAlgorithmIdentifier
);
define_identifier_trait!(
PasswordHashAlgorithmIdentifier
);
define_identifier_trait!(
Pkcs8KdfIdentifier
);
define_identifier_trait!(
PublicKeyAlgorithmIdentifier
);
define_identifier_trait!(
KeyGenParamsIdentifier
);
define_identifier_trait!(
EncryptionParamsIdentifier
);
define_identifier_trait!(
SignatureParamsIdentifier
);
define_identifier_trait!(
RngTypeIdentifier
);
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum HashAlgorithm {
Arbitrary(String),
AsconHash256,
Blake2b(u32),
Blake2s(u32),
Crc24,
Md5,
Ripemd160,
Sha1,
Sha224,
Sha256,
Sha384,
Sha512,
Sha512_256,
Sha3(u32),
Shake128(u32),
Shake256(u32),
Skein512(u32),
Sm3,
Whirlpool,
}
impl HashAlgorithm {
#[must_use]
pub fn botan_name(&self) -> String {
match self {
Self::Arbitrary(name) => name.clone(),
Self::AsconHash256 => "Ascon-Hash256".to_string(),
Self::Blake2b(output_bits) => format!("BLAKE2b({output_bits})"),
Self::Blake2s(output_bits) => format!("BLAKE2s({output_bits})"),
Self::Crc24 => "CRC24".to_string(),
Self::Md5 => "MD5".to_string(),
Self::Ripemd160 => "RIPEMD-160".to_string(),
Self::Sha1 => "SHA-1".to_string(),
Self::Sha224 => "SHA-224".to_string(),
Self::Sha256 => "SHA-256".to_string(),
Self::Sha384 => "SHA-384".to_string(),
Self::Sha512 => "SHA-512".to_string(),
Self::Sha512_256 => "SHA-512-256".to_string(),
Self::Sha3(output_bits) => format!("SHA-3({output_bits})"),
Self::Shake128(output_bits) => format!("SHAKE-128({output_bits})"),
Self::Shake256(output_bits) => format!("SHAKE-256({output_bits})"),
Self::Skein512(output_bits) => format!("Skein-512({output_bits})"),
Self::Sm3 => "SM3".to_string(),
Self::Whirlpool => "Whirlpool".to_string(),
}
}
}
impl HashAlgorithmIdentifier for HashAlgorithm {
fn botan_name(&self) -> String {
HashAlgorithm::botan_name(self)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum BlockCipherAlgorithm {
Arbitrary(String),
Aes128,
Aes192,
Aes256,
Aria128,
Aria192,
Aria256,
Blowfish,
Camellia128,
Camellia192,
Camellia256,
Cast128,
Des,
Idea,
Kuznyechik,
Seed,
Serpent,
Shacal2,
Sm4,
Threefish512,
TripleDes,
Twofish,
}
impl BlockCipherAlgorithm {
#[must_use]
pub fn botan_name(&self) -> String {
match self {
Self::Arbitrary(name) => name.clone(),
Self::Aes128 => "AES-128".to_string(),
Self::Aes192 => "AES-192".to_string(),
Self::Aes256 => "AES-256".to_string(),
Self::Aria128 => "ARIA-128".to_string(),
Self::Aria192 => "ARIA-192".to_string(),
Self::Aria256 => "ARIA-256".to_string(),
Self::Blowfish => "Blowfish".to_string(),
Self::Camellia128 => "Camellia-128".to_string(),
Self::Camellia192 => "Camellia-192".to_string(),
Self::Camellia256 => "Camellia-256".to_string(),
Self::Cast128 => "CAST-128".to_string(),
Self::Des => "DES".to_string(),
Self::Idea => "IDEA".to_string(),
Self::Kuznyechik => "Kuznyechik".to_string(),
Self::Seed => "SEED".to_string(),
Self::Serpent => "Serpent".to_string(),
Self::Shacal2 => "SHACAL2".to_string(),
Self::Sm4 => "SM4".to_string(),
Self::Threefish512 => "Threefish-512".to_string(),
Self::TripleDes => "TripleDES".to_string(),
Self::Twofish => "Twofish".to_string(),
}
}
}
impl BlockCipherAlgorithmIdentifier for BlockCipherAlgorithm {
fn botan_name(&self) -> String {
BlockCipherAlgorithm::botan_name(self)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CipherPadding {
Arbitrary(String),
AnsiX923,
Cts,
Esp,
NoPadding,
OneAndZeros,
Pkcs7,
}
impl CipherPadding {
#[must_use]
pub fn botan_name(&self) -> String {
match self {
Self::Arbitrary(name) => name.clone(),
Self::AnsiX923 => "X9.23".to_string(),
Self::Cts => "CTS".to_string(),
Self::Esp => "ESP".to_string(),
Self::NoPadding => "NoPadding".to_string(),
Self::OneAndZeros => "OneAndZeros".to_string(),
Self::Pkcs7 => "PKCS7".to_string(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum StreamCipherAlgorithm {
Arbitrary(String),
ChaCha8,
ChaCha12,
ChaCha20,
CtrBe(BlockCipherAlgorithm, Option<u32>),
Ofb(BlockCipherAlgorithm),
Rc4(Option<u32>),
Salsa20,
}
impl StreamCipherAlgorithm {
#[must_use]
pub fn botan_name(&self) -> String {
match self {
Self::Arbitrary(name) => name.clone(),
Self::ChaCha8 => "ChaCha(8)".to_string(),
Self::ChaCha12 => "ChaCha(12)".to_string(),
Self::ChaCha20 => "ChaCha20".to_string(),
Self::CtrBe(cipher, width) => ctr_be_name(cipher, *width),
Self::Ofb(cipher) => format!("OFB({})", cipher.botan_name()),
Self::Rc4(skip) => match skip {
Some(skip) => format!("RC4({skip})"),
None => "RC4".to_string(),
},
Self::Salsa20 => "Salsa20".to_string(),
}
}
}
impl CipherAlgorithmIdentifier for StreamCipherAlgorithm {
fn botan_name(&self) -> String {
StreamCipherAlgorithm::botan_name(self)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CipherAlgorithm {
Arbitrary(String),
AsconAead128,
Cbc(BlockCipherAlgorithm, Option<CipherPadding>),
Ccm(BlockCipherAlgorithm, Option<u32>, Option<u32>),
Cfb(BlockCipherAlgorithm, Option<u32>),
ChaCha20Poly1305,
CtrBe(BlockCipherAlgorithm, Option<u32>),
Eax(BlockCipherAlgorithm, Option<u32>),
Gcm(BlockCipherAlgorithm, Option<u32>),
GcmSiv(BlockCipherAlgorithm),
Ocb(BlockCipherAlgorithm, Option<u32>),
Siv(BlockCipherAlgorithm),
Stream(StreamCipherAlgorithm),
Xts(BlockCipherAlgorithm),
}
impl CipherAlgorithm {
#[must_use]
pub fn botan_name(&self) -> String {
match self {
Self::Arbitrary(name) => name.clone(),
Self::AsconAead128 => "Ascon-AEAD128".to_string(),
Self::Cbc(cipher, padding) => match padding {
Some(padding) => format!("{}/CBC/{}", cipher.botan_name(), padding.botan_name()),
None => format!("{}/CBC", cipher.botan_name()),
},
Self::Ccm(cipher, tag_len, length_field) => match (tag_len, length_field) {
(tag_len, Some(length_field)) => format!(
"{}/CCM({},{length_field})",
cipher.botan_name(),
tag_len.unwrap_or(16)
),
(Some(tag_len), None) => format!("{}/CCM({tag_len})", cipher.botan_name()),
(None, None) => format!("{}/CCM", cipher.botan_name()),
},
Self::Cfb(cipher, feedback_bits) => {
mode_with_optional_u32(cipher, "CFB", *feedback_bits)
}
Self::ChaCha20Poly1305 => "ChaCha20Poly1305".to_string(),
Self::CtrBe(cipher, width) => ctr_be_name(cipher, *width),
Self::Eax(cipher, tag_len) => mode_with_optional_u32(cipher, "EAX", *tag_len),
Self::Gcm(cipher, tag_len) => mode_with_optional_u32(cipher, "GCM", *tag_len),
Self::GcmSiv(cipher) => format!("{}/GCM-SIV", cipher.botan_name()),
Self::Ocb(cipher, tag_len) => mode_with_optional_u32(cipher, "OCB", *tag_len),
Self::Siv(cipher) => format!("{}/SIV", cipher.botan_name()),
Self::Stream(cipher) => cipher.botan_name(),
Self::Xts(cipher) => format!("{}/XTS", cipher.botan_name()),
}
}
}
impl CipherAlgorithmIdentifier for CipherAlgorithm {
fn botan_name(&self) -> String {
CipherAlgorithm::botan_name(self)
}
}
fn mode_with_optional_u32(
cipher: &BlockCipherAlgorithm,
mode: &str,
parameter: Option<u32>,
) -> String {
match parameter {
Some(parameter) => format!("{}/{mode}({parameter})", cipher.botan_name()),
None => format!("{}/{mode}", cipher.botan_name()),
}
}
fn ctr_be_name(cipher: &BlockCipherAlgorithm, width: Option<u32>) -> String {
match width {
Some(width) => format!("CTR-BE({},{width})", cipher.botan_name()),
None => format!("CTR-BE({})", cipher.botan_name()),
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MacAlgorithm {
Arbitrary(String),
Blake2b(u32),
Cmac(BlockCipherAlgorithm),
Gmac(BlockCipherAlgorithm),
Hmac(HashAlgorithm),
Kmac128(u32),
Kmac256(u32),
Poly1305,
}
impl MacAlgorithm {
#[must_use]
pub fn botan_name(&self) -> String {
match self {
Self::Arbitrary(name) => name.clone(),
Self::Blake2b(output_bits) => format!("BLAKE2b({output_bits})"),
Self::Cmac(cipher) => format!("CMAC({})", cipher.botan_name()),
Self::Gmac(cipher) => format!("GMAC({})", cipher.botan_name()),
Self::Hmac(hash) => format!("HMAC({})", hash.botan_name()),
Self::Kmac128(output_bits) => format!("KMAC-128({output_bits})"),
Self::Kmac256(output_bits) => format!("KMAC-256({output_bits})"),
Self::Poly1305 => "Poly1305".to_string(),
}
}
}
impl MacAlgorithmIdentifier for MacAlgorithm {
fn botan_name(&self) -> String {
MacAlgorithm::botan_name(self)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum KdfAlgorithm {
Arbitrary(String),
Hkdf(HashAlgorithm),
HkdfExpand(HashAlgorithm),
HkdfExpandMac(MacAlgorithm),
HkdfExtract(HashAlgorithm),
HkdfExtractMac(MacAlgorithm),
HkdfMac(MacAlgorithm),
Kdf1(HashAlgorithm),
Kdf1Iso18033(HashAlgorithm),
Kdf2(HashAlgorithm),
Raw,
Sp80056aHash(HashAlgorithm),
Sp80056aMac(MacAlgorithm),
Sp80056cHash(HashAlgorithm),
Sp80056cMac(MacAlgorithm),
Sp800108Counter(MacAlgorithm, Option<u32>, Option<u32>),
Sp800108Feedback(MacAlgorithm, Option<u32>, Option<u32>),
Sp800108Pipeline(MacAlgorithm, Option<u32>, Option<u32>),
Tls12Prf(HashAlgorithm),
Tls12PrfMac(MacAlgorithm),
X942Prf(String),
}
impl KdfAlgorithm {
#[must_use]
pub fn botan_name(&self) -> String {
match self {
Self::Arbitrary(name) => name.clone(),
Self::Hkdf(hash) => format!("HKDF({})", hash.botan_name()),
Self::HkdfExpand(hash) => format!("HKDF-Expand({})", hash.botan_name()),
Self::HkdfExpandMac(mac) => format!("HKDF-Expand({})", mac.botan_name()),
Self::HkdfExtract(hash) => format!("HKDF-Extract({})", hash.botan_name()),
Self::HkdfExtractMac(mac) => format!("HKDF-Extract({})", mac.botan_name()),
Self::HkdfMac(mac) => format!("HKDF({})", mac.botan_name()),
Self::Kdf1(hash) => format!("KDF1({})", hash.botan_name()),
Self::Kdf1Iso18033(hash) => format!("KDF1-18033({})", hash.botan_name()),
Self::Kdf2(hash) => format!("KDF2({})", hash.botan_name()),
Self::Raw => "Raw".to_string(),
Self::Sp80056aHash(hash) => format!("SP800-56A({})", hash.botan_name()),
Self::Sp80056aMac(mac) => format!("SP800-56A({})", mac.botan_name()),
Self::Sp80056cHash(hash) => format!("SP800-56C({})", hash.botan_name()),
Self::Sp80056cMac(mac) => format!("SP800-56C({})", mac.botan_name()),
Self::Sp800108Counter(mac, counter_bits, length_bits) => {
sp800_108_name("SP800-108-Counter", mac, *counter_bits, *length_bits)
}
Self::Sp800108Feedback(mac, counter_bits, length_bits) => {
sp800_108_name("SP800-108-Feedback", mac, *counter_bits, *length_bits)
}
Self::Sp800108Pipeline(mac, counter_bits, length_bits) => {
sp800_108_name("SP800-108-Pipeline", mac, *counter_bits, *length_bits)
}
Self::Tls12Prf(hash) => format!("TLS-12-PRF({})", hash.botan_name()),
Self::Tls12PrfMac(mac) => format!("TLS-12-PRF({})", mac.botan_name()),
Self::X942Prf(key_wrap_oid) => format!("X9.42-PRF({key_wrap_oid})"),
}
}
}
impl KdfAlgorithmIdentifier for KdfAlgorithm {
fn botan_name(&self) -> String {
KdfAlgorithm::botan_name(self)
}
}
fn sp800_108_name(
name: &str,
mac: &MacAlgorithm,
counter_bits: Option<u32>,
length_bits: Option<u32>,
) -> String {
match (counter_bits, length_bits) {
(counter_bits, Some(length_bits)) => {
format!(
"{}({},{},{length_bits})",
name,
mac.botan_name(),
counter_bits.unwrap_or(32)
)
}
(Some(counter_bits), None) => format!("{}({},{counter_bits})", name, mac.botan_name()),
(None, None) => format!("{}({})", name, mac.botan_name()),
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PasswordHashAlgorithm {
Arbitrary(String),
Argon2d,
Argon2i,
Argon2id,
BcryptPbkdf,
OpenPgpS2k(HashAlgorithm),
Pbkdf2(HashAlgorithm),
Pbkdf2Mac(MacAlgorithm),
Pkcs12Kdf(HashAlgorithm, u32),
Scrypt,
}
impl PasswordHashAlgorithm {
#[must_use]
pub fn botan_name(&self) -> String {
match self {
Self::Arbitrary(name) => name.clone(),
Self::Argon2d => "Argon2d".to_string(),
Self::Argon2i => "Argon2i".to_string(),
Self::Argon2id => "Argon2id".to_string(),
Self::BcryptPbkdf => "Bcrypt-PBKDF".to_string(),
Self::OpenPgpS2k(hash) => format!("OpenPGP-S2K({})", hash.botan_name()),
Self::Pbkdf2(hash) => format!("PBKDF2({})", hash.botan_name()),
Self::Pbkdf2Mac(mac) => format!("PBKDF2({})", mac.botan_name()),
Self::Pkcs12Kdf(hash, id) => format!("PKCS12-KDF({},{id})", hash.botan_name()),
Self::Scrypt => "Scrypt".to_string(),
}
}
}
impl PasswordHashAlgorithmIdentifier for PasswordHashAlgorithm {
fn botan_name(&self) -> String {
PasswordHashAlgorithm::botan_name(self)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Pkcs8Kdf {
Arbitrary(String),
Pbkdf2(HashAlgorithm),
Scrypt,
}
impl Pkcs8Kdf {
#[must_use]
pub fn botan_name(&self) -> String {
match self {
Self::Arbitrary(name) => name.clone(),
Self::Pbkdf2(hash) => hash.botan_name(),
Self::Scrypt => "Scrypt".to_string(),
}
}
}
impl Pkcs8KdfIdentifier for Pkcs8Kdf {
fn botan_name(&self) -> String {
Pkcs8Kdf::botan_name(self)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PublicKeyAlgorithm {
Arbitrary(String),
ClassicMcEliece,
Dh,
Ecdh,
Ecdsa,
Ecgdsa,
Eckcdsa,
Ed25519,
Ed448,
FrodoKem,
HssLms,
MlDsa,
MlKem,
Rsa,
SlhDsa,
Sm2,
X25519,
X448,
Xmss,
}
impl PublicKeyAlgorithm {
#[must_use]
pub fn botan_name(&self) -> String {
match self {
Self::Arbitrary(name) => name.clone(),
Self::ClassicMcEliece => "ClassicMcEliece".to_string(),
Self::Dh => "DH".to_string(),
Self::Ecdh => "ECDH".to_string(),
Self::Ecdsa => "ECDSA".to_string(),
Self::Ecgdsa => "ECGDSA".to_string(),
Self::Eckcdsa => "ECKCDSA".to_string(),
Self::Ed25519 => "Ed25519".to_string(),
Self::Ed448 => "Ed448".to_string(),
Self::FrodoKem => "FrodoKEM".to_string(),
Self::HssLms => "HSS-LMS".to_string(),
Self::MlDsa => "ML-DSA".to_string(),
Self::MlKem => "ML-KEM".to_string(),
Self::Rsa => "RSA".to_string(),
Self::SlhDsa => "SLH-DSA".to_string(),
Self::Sm2 => "SM2".to_string(),
Self::X25519 => "X25519".to_string(),
Self::X448 => "X448".to_string(),
Self::Xmss => "XMSS".to_string(),
}
}
}
impl PublicKeyAlgorithmIdentifier for PublicKeyAlgorithm {
fn botan_name(&self) -> String {
PublicKeyAlgorithm::botan_name(self)
}
}
impl KeyGenParamsIdentifier for u32 {
fn botan_name(&self) -> String {
self.to_string()
}
}
macro_rules! define_keygen_params_enum {
($(#[$meta:meta])* $name:ident, $($(#[$vmeta:meta])* $variant:ident => $botan_name:literal),+ $(,)?) => {
$(#[$meta])*
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum $name {
$($(#[$vmeta])* $variant),+
}
impl $name {
#[must_use]
pub fn botan_name(&self) -> String {
match self {
$(Self::$variant => $botan_name.to_string()),+
}
}
}
impl KeyGenParamsIdentifier for $name {
fn botan_name(&self) -> String {
$name::botan_name(self)
}
}
};
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EcGroupId {
Arbitrary(String),
Brainpool256r1,
Brainpool384r1,
Brainpool512r1,
Frp256v1,
Numsp512d1,
Secp192r1,
Secp224r1,
Secp256k1,
Secp256r1,
Secp384r1,
Secp521r1,
Sm2p256v1,
}
impl EcGroupId {
#[must_use]
pub fn botan_name(&self) -> String {
match self {
Self::Arbitrary(name) => name.clone(),
Self::Brainpool256r1 => "brainpool256r1".to_string(),
Self::Brainpool384r1 => "brainpool384r1".to_string(),
Self::Brainpool512r1 => "brainpool512r1".to_string(),
Self::Frp256v1 => "frp256v1".to_string(),
Self::Numsp512d1 => "numsp512d1".to_string(),
Self::Secp192r1 => "secp192r1".to_string(),
Self::Secp224r1 => "secp224r1".to_string(),
Self::Secp256k1 => "secp256k1".to_string(),
Self::Secp256r1 => "secp256r1".to_string(),
Self::Secp384r1 => "secp384r1".to_string(),
Self::Secp521r1 => "secp521r1".to_string(),
Self::Sm2p256v1 => "sm2p256v1".to_string(),
}
}
}
impl KeyGenParamsIdentifier for EcGroupId {
fn botan_name(&self) -> String {
EcGroupId::botan_name(self)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DlGroup {
Arbitrary(String),
FfdheIetf2048,
FfdheIetf3072,
FfdheIetf4096,
FfdheIetf6144,
FfdheIetf8192,
ModpIetf2048,
ModpIetf3072,
ModpIetf4096,
ModpIetf6144,
ModpIetf8192,
}
impl DlGroup {
#[must_use]
pub fn botan_name(&self) -> String {
match self {
Self::Arbitrary(name) => name.clone(),
Self::FfdheIetf2048 => "ffdhe/ietf/2048".to_string(),
Self::FfdheIetf3072 => "ffdhe/ietf/3072".to_string(),
Self::FfdheIetf4096 => "ffdhe/ietf/4096".to_string(),
Self::FfdheIetf6144 => "ffdhe/ietf/6144".to_string(),
Self::FfdheIetf8192 => "ffdhe/ietf/8192".to_string(),
Self::ModpIetf2048 => "modp/ietf/2048".to_string(),
Self::ModpIetf3072 => "modp/ietf/3072".to_string(),
Self::ModpIetf4096 => "modp/ietf/4096".to_string(),
Self::ModpIetf6144 => "modp/ietf/6144".to_string(),
Self::ModpIetf8192 => "modp/ietf/8192".to_string(),
}
}
}
impl KeyGenParamsIdentifier for DlGroup {
fn botan_name(&self) -> String {
DlGroup::botan_name(self)
}
}
define_keygen_params_enum!(
MlKemParams,
MlKem512 => "ML-KEM-512",
MlKem768 => "ML-KEM-768",
MlKem1024 => "ML-KEM-1024",
);
define_keygen_params_enum!(
MlDsaParams,
MlDsa4x4 => "ML-DSA-4x4",
MlDsa6x5 => "ML-DSA-6x5",
MlDsa8x7 => "ML-DSA-8x7",
);
define_keygen_params_enum!(
SlhDsaParams,
Sha2_128s => "SLH-DSA-SHA2-128s",
Sha2_128f => "SLH-DSA-SHA2-128f",
Sha2_192s => "SLH-DSA-SHA2-192s",
Sha2_192f => "SLH-DSA-SHA2-192f",
Sha2_256s => "SLH-DSA-SHA2-256s",
Sha2_256f => "SLH-DSA-SHA2-256f",
Shake128s => "SLH-DSA-SHAKE-128s",
Shake128f => "SLH-DSA-SHAKE-128f",
Shake192s => "SLH-DSA-SHAKE-192s",
Shake192f => "SLH-DSA-SHAKE-192f",
Shake256s => "SLH-DSA-SHAKE-256s",
Shake256f => "SLH-DSA-SHAKE-256f",
);
define_keygen_params_enum!(
FrodoKemParams,
Frodo640Shake => "FrodoKEM-640-SHAKE",
Frodo976Shake => "FrodoKEM-976-SHAKE",
Frodo1344Shake => "FrodoKEM-1344-SHAKE",
Frodo640Aes => "FrodoKEM-640-AES",
Frodo976Aes => "FrodoKEM-976-AES",
Frodo1344Aes => "FrodoKEM-1344-AES",
EFrodo640Shake => "eFrodoKEM-640-SHAKE",
EFrodo976Shake => "eFrodoKEM-976-SHAKE",
EFrodo1344Shake => "eFrodoKEM-1344-SHAKE",
EFrodo640Aes => "eFrodoKEM-640-AES",
EFrodo976Aes => "eFrodoKEM-976-AES",
EFrodo1344Aes => "eFrodoKEM-1344-AES",
);
define_keygen_params_enum!(
ClassicMcElieceParams,
P348864 => "ClassicMcEliece_348864",
P348864f => "ClassicMcEliece_348864f",
P460896 => "ClassicMcEliece_460896",
P460896f => "ClassicMcEliece_460896f",
P6688128 => "ClassicMcEliece_6688128",
P6688128f => "ClassicMcEliece_6688128f",
P6688128pc => "ClassicMcEliece_6688128pc",
P6688128pcf => "ClassicMcEliece_6688128pcf",
P6960119 => "ClassicMcEliece_6960119",
P6960119f => "ClassicMcEliece_6960119f",
P6960119pc => "ClassicMcEliece_6960119pc",
P6960119pcf => "ClassicMcEliece_6960119pcf",
P8192128 => "ClassicMcEliece_8192128",
P8192128f => "ClassicMcEliece_8192128f",
P8192128pc => "ClassicMcEliece_8192128pc",
P8192128pcf => "ClassicMcEliece_8192128pcf",
);
define_keygen_params_enum!(
XmssParams,
Sha2_10_256 => "XMSS-SHA2_10_256",
Sha2_16_256 => "XMSS-SHA2_16_256",
Sha2_20_256 => "XMSS-SHA2_20_256",
Sha2_10_512 => "XMSS-SHA2_10_512",
Sha2_16_512 => "XMSS-SHA2_16_512",
Sha2_20_512 => "XMSS-SHA2_20_512",
Sha2_10_192 => "XMSS-SHA2_10_192",
Sha2_16_192 => "XMSS-SHA2_16_192",
Sha2_20_192 => "XMSS-SHA2_20_192",
Shake10_256 => "XMSS-SHAKE_10_256",
Shake16_256 => "XMSS-SHAKE_16_256",
Shake20_256 => "XMSS-SHAKE_20_256",
Shake10_512 => "XMSS-SHAKE_10_512",
Shake16_512 => "XMSS-SHAKE_16_512",
Shake20_512 => "XMSS-SHAKE_20_512",
Shake256_10_256 => "XMSS-SHAKE256_10_256",
Shake256_16_256 => "XMSS-SHAKE256_16_256",
Shake256_20_256 => "XMSS-SHAKE256_20_256",
Shake256_10_192 => "XMSS-SHAKE256_10_192",
Shake256_16_192 => "XMSS-SHAKE256_16_192",
Shake256_20_192 => "XMSS-SHAKE256_20_192",
);
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EncryptionParams {
Arbitrary(String),
Raw,
RsaOaep {
hash: HashAlgorithm,
mgf1_hash: Option<HashAlgorithm>,
label: Option<String>,
},
RsaPkcs1v15,
Sm2(HashAlgorithm),
}
impl EncryptionParams {
#[must_use]
pub fn botan_name(&self) -> String {
match self {
Self::Arbitrary(name) => name.clone(),
Self::Raw => "Raw".to_string(),
Self::RsaOaep {
hash,
mgf1_hash,
label,
} => {
let mgf1 = match mgf1_hash {
Some(mgf1_hash) => format!("MGF1({})", mgf1_hash.botan_name()),
None => "MGF1".to_string(),
};
match label {
Some(label) => format!("OAEP({},{mgf1},{label})", hash.botan_name()),
None => format!("OAEP({},{mgf1})", hash.botan_name()),
}
}
Self::RsaPkcs1v15 => "PKCS1v15".to_string(),
Self::Sm2(hash) => hash.botan_name(),
}
}
}
impl EncryptionParamsIdentifier for EncryptionParams {
fn botan_name(&self) -> String {
EncryptionParams::botan_name(self)
}
}
impl EncryptionParamsIdentifier for Option<EncryptionParams> {
fn botan_name(&self) -> String {
match self {
Some(params) => EncryptionParams::botan_name(params),
None => String::new(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SignatureParams {
Arbitrary(String),
Deterministic,
Ed25519ph,
Ed448ph,
Hash(HashAlgorithm),
Randomized,
Raw(Option<HashAlgorithm>),
RsaPkcs1v15(HashAlgorithm),
RsaPkcs1v15Raw(Option<HashAlgorithm>),
RsaPss {
hash: HashAlgorithm,
salt_len: Option<u32>,
},
RsaPssRaw {
hash: HashAlgorithm,
salt_len: Option<u32>,
},
RsaX931(HashAlgorithm),
Sm2 {
user_id: String,
hash: Option<HashAlgorithm>,
},
}
impl SignatureParams {
#[must_use]
pub fn botan_name(&self) -> String {
match self {
Self::Arbitrary(name) => name.clone(),
Self::Deterministic => "Deterministic".to_string(),
Self::Ed25519ph => "Ed25519ph".to_string(),
Self::Ed448ph => "Ed448ph".to_string(),
Self::Hash(hash) => hash_sig_padding_name(hash),
Self::Randomized => "Randomized".to_string(),
Self::Raw(None) => "Raw".to_string(),
Self::Raw(Some(hash)) => format!("Raw({})", hash.botan_name()),
Self::RsaPkcs1v15(hash) => format!("PKCS1v15({})", hash.botan_name()),
Self::RsaPkcs1v15Raw(None) => "PKCS1v15(Raw)".to_string(),
Self::RsaPkcs1v15Raw(Some(hash)) => format!("PKCS1v15(Raw,{})", hash.botan_name()),
Self::RsaPss { hash, salt_len } => pss_name("PSS", hash, *salt_len),
Self::RsaPssRaw { hash, salt_len } => pss_name("PSS_Raw", hash, *salt_len),
Self::RsaX931(hash) => format!("X9.31({})", hash.botan_name()),
Self::Sm2 { user_id, hash } => match hash {
Some(hash) => format!("{user_id},{}", hash.botan_name()),
None => user_id.clone(),
},
}
}
}
fn hash_sig_padding_name(hash: &HashAlgorithm) -> String {
if crate::Version::major_version() == 2 {
format!("EMSA1({})", hash.botan_name())
} else {
hash.botan_name()
}
}
fn pss_name(scheme: &str, hash: &HashAlgorithm, salt_len: Option<u32>) -> String {
match salt_len {
Some(salt_len) => format!("{scheme}({},MGF1,{salt_len})", hash.botan_name()),
None => format!("{scheme}({})", hash.botan_name()),
}
}
impl SignatureParamsIdentifier for SignatureParams {
fn botan_name(&self) -> String {
SignatureParams::botan_name(self)
}
}
impl SignatureParamsIdentifier for HashAlgorithm {
fn botan_name(&self) -> String {
hash_sig_padding_name(self)
}
}
impl SignatureParamsIdentifier for Option<SignatureParams> {
fn botan_name(&self) -> String {
match self {
Some(params) => SignatureParams::botan_name(params),
None => String::new(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RngType {
Arbitrary(String),
EsdmFull,
EsdmPr,
Hwrng,
Jitter,
System,
User,
}
impl RngType {
#[must_use]
pub fn botan_name(&self) -> String {
match self {
Self::Arbitrary(name) => name.clone(),
Self::EsdmFull => "esdm-full".to_string(),
Self::EsdmPr => "esdm-pr".to_string(),
Self::Hwrng => "hwrng".to_string(),
Self::Jitter => "jitter".to_string(),
Self::System => "system".to_string(),
Self::User => "user".to_string(),
}
}
}
impl RngTypeIdentifier for RngType {
fn botan_name(&self) -> String {
RngType::botan_name(self)
}
}