yubihsm/opaque/
algorithm.rs

1//! Pseudo-algorithms for opaque data
2
3use crate::algorithm;
4
5/// Valid algorithms for opaque data
6#[derive(Copy, Clone, Debug, Eq, PartialEq)]
7#[repr(u8)]
8pub enum Algorithm {
9    /// Arbitrary opaque data
10    Data = 0x1e,
11
12    /// X.509 certificates
13    X509Certificate = 0x1f,
14}
15
16impl Algorithm {
17    /// Convert an unsigned byte tag into an `Algorithm` (if valid)
18    pub fn from_u8(tag: u8) -> Result<Self, algorithm::Error> {
19        Ok(match tag {
20            0x1e => Algorithm::Data,
21            0x1f => Algorithm::X509Certificate,
22            _ => fail!(
23                algorithm::ErrorKind::TagInvalid,
24                "unknown opaque data ID: 0x{:02x}",
25                tag
26            ),
27        })
28    }
29
30    /// Serialize algorithm ID as a byte
31    pub fn to_u8(self) -> u8 {
32        self as u8
33    }
34}
35
36impl_algorithm_serializers!(Algorithm);