picky_krb/crypto/
checksum.rs

1use crate::constants::cksum_types::{HMAC_SHA1_96_AES128, HMAC_SHA1_96_AES256, HMAC_SHA1_DES3_KD};
2
3use super::aes::{HmacSha196Aes128, HmacSha196Aes256};
4use super::des::HmacSha1Des3Kd;
5use super::{KerberosCryptoError, KerberosCryptoResult};
6
7pub trait Checksum {
8    fn checksum_type(&self) -> ChecksumSuite;
9    fn checksum(&self, key: &[u8], key_usage: i32, payload: &[u8]) -> KerberosCryptoResult<Vec<u8>>;
10}
11
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub enum ChecksumSuite {
14    HmacSha196Aes256,
15    HmacSha196Aes128,
16    HmacSha1Des3Kd,
17}
18
19impl ChecksumSuite {
20    pub fn hasher(&self) -> Box<dyn Checksum> {
21        match self {
22            ChecksumSuite::HmacSha196Aes256 => Box::<HmacSha196Aes256>::default(),
23            ChecksumSuite::HmacSha196Aes128 => Box::<HmacSha196Aes128>::default(),
24            ChecksumSuite::HmacSha1Des3Kd => Box::<HmacSha1Des3Kd>::default(),
25        }
26    }
27}
28
29impl TryFrom<usize> for ChecksumSuite {
30    type Error = KerberosCryptoError;
31
32    fn try_from(identifier: usize) -> Result<Self, Self::Error> {
33        match identifier {
34            HMAC_SHA1_96_AES256 => Ok(Self::HmacSha196Aes256),
35            HMAC_SHA1_96_AES128 => Ok(Self::HmacSha196Aes128),
36            HMAC_SHA1_DES3_KD => Ok(Self::HmacSha1Des3Kd),
37            _ => Err(KerberosCryptoError::AlgorithmIdentifier(identifier)),
38        }
39    }
40}
41
42impl From<&ChecksumSuite> for u32 {
43    fn from(checksum_suite: &ChecksumSuite) -> Self {
44        match checksum_suite {
45            ChecksumSuite::HmacSha196Aes256 => HMAC_SHA1_96_AES256 as u32,
46            ChecksumSuite::HmacSha196Aes128 => HMAC_SHA1_96_AES128 as u32,
47            ChecksumSuite::HmacSha1Des3Kd => HMAC_SHA1_DES3_KD as u32,
48        }
49    }
50}
51
52impl From<ChecksumSuite> for u32 {
53    fn from(checksum_suite: ChecksumSuite) -> Self {
54        match checksum_suite {
55            ChecksumSuite::HmacSha196Aes256 => HMAC_SHA1_96_AES256 as u32,
56            ChecksumSuite::HmacSha196Aes128 => HMAC_SHA1_96_AES128 as u32,
57            ChecksumSuite::HmacSha1Des3Kd => HMAC_SHA1_DES3_KD as u32,
58        }
59    }
60}
61
62impl From<ChecksumSuite> for usize {
63    fn from(checksum_suite: ChecksumSuite) -> Self {
64        match checksum_suite {
65            ChecksumSuite::HmacSha196Aes256 => HMAC_SHA1_96_AES256,
66            ChecksumSuite::HmacSha196Aes128 => HMAC_SHA1_96_AES128,
67            ChecksumSuite::HmacSha1Des3Kd => HMAC_SHA1_DES3_KD,
68        }
69    }
70}
71
72impl From<ChecksumSuite> for u8 {
73    fn from(checksum_suite: ChecksumSuite) -> Self {
74        match checksum_suite {
75            ChecksumSuite::HmacSha196Aes256 => HMAC_SHA1_96_AES256 as u8,
76            ChecksumSuite::HmacSha196Aes128 => HMAC_SHA1_96_AES128 as u8,
77            ChecksumSuite::HmacSha1Des3Kd => HMAC_SHA1_DES3_KD as u8,
78        }
79    }
80}
81
82impl From<&ChecksumSuite> for u8 {
83    fn from(checksum_suite: &ChecksumSuite) -> Self {
84        match checksum_suite {
85            ChecksumSuite::HmacSha196Aes256 => HMAC_SHA1_96_AES256 as u8,
86            ChecksumSuite::HmacSha196Aes128 => HMAC_SHA1_96_AES128 as u8,
87            ChecksumSuite::HmacSha1Des3Kd => HMAC_SHA1_DES3_KD as u8,
88        }
89    }
90}