Skip to content

Commit

Permalink
fixup! core: Add helpers for checking mechanisms at compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-nitrokey committed Jan 6, 2025
1 parent 0b44ea7 commit db58da3
Showing 1 changed file with 73 additions and 39 deletions.
112 changes: 73 additions & 39 deletions core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,16 +457,17 @@ macro_rules! generate_mechanism {
$(#[$outer:meta])*
$vis:vis enum $name:ident {
$(
#[cfg($($cfg_cond:tt)*)]
$(#[$inner:meta])*
$feature:literal: $variant:ident,
$variant:ident,
)*
}
) => {
$(#[$outer])*
$vis enum $name {
$(
#[cfg($($cfg_cond)*)]
$(#[$inner])*
#[cfg(feature = $feature)]
$variant,
)*
}
Expand All @@ -477,7 +478,7 @@ macro_rules! generate_mechanism {
/// The contents of this constant depends on the enabled features.
pub const ENABLED: &[Self] = &[
$(
#[cfg(feature = $feature)]
#[cfg($($cfg_cond)*)]
Self::$variant,
)*
];
Expand All @@ -487,7 +488,7 @@ macro_rules! generate_mechanism {
let _ = other;
match *self {
$(
#[cfg(feature = $feature)]
#[cfg($($cfg_cond)*)]
Self::$variant => matches!(other, Self::$variant),
)*
}
Expand All @@ -500,7 +501,7 @@ macro_rules! generate_mechanism {
pub const fn panic(&self) -> ! {
match *self {
$(
#[cfg(feature = $feature)]
#[cfg($($cfg_cond)*)]
Self::$variant => panic!(concat!("panic for mechanism: ", stringify!($variant))),
)*
}
Expand All @@ -513,53 +514,86 @@ generate_mechanism! {
#[derive(Copy, Clone, Eq, PartialEq, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Mechanism {
"aes256-cbc": Aes256Cbc,
"chacha8-poly1305": Chacha8Poly1305,
"ed255": Ed255,
"hmac-blake2s": HmacBlake2s,
"hmac-sha1": HmacSha1,
"hmac-sha256": HmacSha256,
"hmac-sha512": HmacSha512,
#[cfg(feature = "aes256-cbc")]
Aes256Cbc,
#[cfg(feature = "chacha8-poly1305")]
Chacha8Poly1305,
#[cfg(feature = "ed255")]
Ed255,
#[cfg(feature = "hmac-blake2s")]
HmacBlake2s,
#[cfg(feature = "hmac-sha1")]
HmacSha1,
#[cfg(feature = "hmac-sha256")]
HmacSha256,
#[cfg(feature = "hmac-sha512")]
HmacSha512,
// P256XSha256,
"p256": P256,
"p256": P256Prehashed,
"p384": P384,
"p384": P384Prehashed,
"p521": P521,
"p521": P521Prehashed,
"brainpoolp256r1": BrainpoolP256R1,
"brainpoolp256r1": BrainpoolP256R1Prehashed,
"brainpoolp384r1": BrainpoolP384R1,
"brainpoolp384r1": BrainpoolP384R1Prehashed,
"brainpoolp512r1": BrainpoolP512R1,
"brainpoolp512r1": BrainpoolP512R1Prehashed,
"secp256k1": Secp256k1,
"secp256k1": Secp256k1Prehashed,
#[cfg(feature = "p256")]
P256,
#[cfg(feature = "p256")]
P256Prehashed,
#[cfg(feature = "p384")]
P384,
#[cfg(feature = "p384")]
P384Prehashed,
#[cfg(feature = "p521")]
P521,
#[cfg(feature = "p521")]
P521Prehashed,
#[cfg(feature = "brainpoolp256r1")]
BrainpoolP256R1,
#[cfg(feature = "brainpoolp256r1")]
BrainpoolP256R1Prehashed,
#[cfg(feature = "brainpoolp384r1")]
BrainpoolP384R1,
#[cfg(feature = "brainpoolp384r1")]
BrainpoolP384R1Prehashed,
#[cfg(feature = "brainpoolp512r1")]
BrainpoolP512R1,
#[cfg(feature = "brainpoolp512r1")]
BrainpoolP512R1Prehashed,
#[cfg(feature = "secp256k1")]
Secp256k1,
#[cfg(feature = "secp256k1")]
Secp256k1Prehashed,
// clients can also do hashing by themselves
"sha256": Sha256,
"tdes": Tdes,
"totp": Totp,
"trng": Trng,
"x255": X255,
#[cfg(feature = "sha256")]
Sha256,
#[cfg(feature = "tdes")]
Tdes,
#[cfg(feature = "totp")]
Totp,
#[cfg(feature = "trng")]
Trng,
#[cfg(feature = "x255")]
X255,
#[cfg(feature = "shared-secret")]
/// Used to serialize the output of a diffie-hellman
"shared-secret": SharedSecret,
SharedSecret,

#[cfg(feature = "rsa2048")]
/// Exposes the Raw RSA encryption/decryption primitive. Be aware this is dangerous.
/// Not having any padding can allow an attacker to obtain plaintexts and forge signatures.
/// It should only be used if absolutely necessary.
"rsa2048": Rsa2048Raw,
Rsa2048Raw,
#[cfg(feature = "rsa3072")]
/// Exposes the Raw RSA encryption/decryption primitive. Be aware this is dangerous.
/// Not having any padding can allow an attacker to obtain plaintexts and forge signatures.
/// It should only be used if absolutely necessary.
"rsa3072": Rsa3072Raw,
Rsa3072Raw,
#[cfg(feature = "rsa4096")]
/// Exposes the Raw RSA encryption/decryption primitive. Be aware this is dangerous.
/// Not having any padding can allow an attacker to obtain plaintexts and forge signatures.
/// It should only be used if absolutely necessary.
"rsa4096": Rsa4096Raw,

"rsa2048": Rsa2048Pkcs1v15,
"rsa3072": Rsa3072Pkcs1v15,
"rsa4096": Rsa4096Pkcs1v15,
Rsa4096Raw,

#[cfg(feature = "rsa2048")]
Rsa2048Pkcs1v15,
#[cfg(feature = "rsa3072")]
Rsa3072Pkcs1v15,
#[cfg(feature = "rsa4096")]
Rsa4096Pkcs1v15,
}
}

Expand Down

0 comments on commit db58da3

Please sign in to comment.