use crate::algorithm;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(u8)]
pub enum Algorithm {
Rsa2048 = 0x09,
Rsa3072 = 0x0a,
Rsa4096 = 0x0b,
Ed25519 = 0x2e,
EcP224 = 0x2f,
EcP256 = 0x0c,
EcP384 = 0x0d,
EcP521 = 0x0e,
EcK256 = 0x0f,
EcBp256 = 0x10,
EcBp384 = 0x11,
EcBp512 = 0x12,
}
impl Algorithm {
pub fn from_u8(tag: u8) -> Result<Self, algorithm::Error> {
Ok(match tag {
0x09 => Algorithm::Rsa2048,
0x0a => Algorithm::Rsa3072,
0x0b => Algorithm::Rsa4096,
0x0c => Algorithm::EcP256,
0x0d => Algorithm::EcP384,
0x0e => Algorithm::EcP521,
0x0f => Algorithm::EcK256,
0x10 => Algorithm::EcBp256,
0x11 => Algorithm::EcBp384,
0x12 => Algorithm::EcBp512,
0x2e => Algorithm::Ed25519,
0x2f => Algorithm::EcP224,
_ => fail!(
algorithm::ErrorKind::TagInvalid,
"unknown asymmetric algorithm ID: 0x{:02x}",
tag
),
})
}
pub fn to_u8(self) -> u8 {
self as u8
}
pub fn key_len(self) -> usize {
match self {
Algorithm::Rsa2048 => 256,
Algorithm::Rsa3072 => 384,
Algorithm::Rsa4096 => 512,
Algorithm::Ed25519 => 32,
Algorithm::EcP224 => 28,
Algorithm::EcP256 => 32,
Algorithm::EcK256 => 32,
Algorithm::EcP384 => 48,
Algorithm::EcP521 => 66,
Algorithm::EcBp256 => 32,
Algorithm::EcBp384 => 48,
Algorithm::EcBp512 => 64,
}
}
}
impl_algorithm_serializers!(Algorithm);