use super::{oaep, pkcs1, pss};
use crate::algorithm;
use anomaly::fail;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(u8)]
pub enum Algorithm {
Oaep(oaep::Algorithm),
Pkcs1(pkcs1::Algorithm),
Pss(pss::Algorithm),
}
impl Algorithm {
pub fn from_u8(tag: u8) -> Result<Self, algorithm::Error> {
Ok(match tag {
0x01..=0x04 => Algorithm::Pkcs1(pkcs1::Algorithm::from_u8(tag)?),
0x05..=0x08 => Algorithm::Pss(pss::Algorithm::from_u8(tag)?),
0x19..=0x1c => Algorithm::Oaep(oaep::Algorithm::from_u8(tag)?),
_ => fail!(
algorithm::ErrorKind::TagInvalid,
"unknown RSA algorithm ID: 0x{:02x}",
tag
),
})
}
pub fn to_u8(self) -> u8 {
match self {
Algorithm::Oaep(alg) => alg.to_u8(),
Algorithm::Pkcs1(alg) => alg.to_u8(),
Algorithm::Pss(alg) => alg.to_u8(),
}
}
}
impl_algorithm_serializers!(Algorithm);
impl From<oaep::Algorithm> for Algorithm {
fn from(alg: oaep::Algorithm) -> Algorithm {
Algorithm::Oaep(alg)
}
}
impl From<pkcs1::Algorithm> for Algorithm {
fn from(alg: pkcs1::Algorithm) -> Algorithm {
Algorithm::Pkcs1(alg)
}
}
impl From<pss::Algorithm> for Algorithm {
fn from(alg: pss::Algorithm) -> Algorithm {
Algorithm::Pss(alg)
}
}