yubihsm/authentication/
algorithm.rs

1//! Authentication algorithms
2
3use crate::algorithm;
4
5/// Valid algorithms for auth keys
6#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
7#[repr(u8)]
8pub enum Algorithm {
9    /// YubiHSM AES PSK authentication
10    #[default]
11    YubicoAes = 0x26,
12}
13
14impl Algorithm {
15    /// Convert an unsigned byte tag into an `authentication::Algorithm` (if valid)
16    pub fn from_u8(tag: u8) -> Result<Self, algorithm::Error> {
17        Ok(match tag {
18            0x26 => Algorithm::YubicoAes,
19            _ => fail!(
20                algorithm::ErrorKind::TagInvalid,
21                "unknown auth algorithm ID: 0x{:02x}",
22                tag
23            ),
24        })
25    }
26
27    /// Serialize algorithm ID as a byte
28    pub fn to_u8(self) -> u8 {
29        self as u8
30    }
31
32    /// Return the size of the given key (as expected by the `YubiHSM 2`) in bytes
33    pub fn key_len(self) -> usize {
34        match self {
35            Algorithm::YubicoAes => 32,
36        }
37    }
38}
39
40impl_algorithm_serializers!(Algorithm);