1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
pub mod aes;
mod checksum;
mod cipher;
pub(crate) mod common;
pub mod des;
pub mod diffie_hellman;
pub(crate) mod nfold;
pub(crate) mod utils;

use ::aes::cipher::block_padding::UnpadError;
use ::aes::cipher::inout::PadError;
use thiserror::Error;

/// https://www.rfc-editor.org/rfc/rfc3962.html#section-4
/// the 8-octet ASCII string "kerberos"
pub const KERBEROS: &[u8; 8] = b"kerberos";

#[derive(Error, Debug)]
pub enum KerberosCryptoError {
    #[error("Invalid key length: {0}. Expected: {1}")]
    KeyLength(usize, usize),
    #[error("Invalid cipher length: {0}. Expected at least: {1}")]
    CipherLength(usize, usize),
    #[error("Invalid algorithm identifier: {0}")]
    AlgorithmIdentifier(usize),
    #[error("Invalid algorithm identifier: {0:?}")]
    AlgorithmIdentifierData(Vec<u8>),
    #[error("Bad integrity: calculated hmac is different than provided")]
    IntegrityCheck,
    #[error("Cipher error: {0}")]
    CipherError(String),
    #[error("Padding error: {0:?}")]
    CipherUnpad(UnpadError),
    #[error("Padding error: {0:?}")]
    CipherPad(PadError),
    #[error("Invalid seed bit len: {0}")]
    SeedBitLen(String),
}

impl From<UnpadError> for KerberosCryptoError {
    fn from(err: UnpadError) -> Self {
        Self::CipherUnpad(err)
    }
}

impl From<PadError> for KerberosCryptoError {
    fn from(err: PadError) -> Self {
        Self::CipherPad(err)
    }
}

pub type KerberosCryptoResult<T> = Result<T, KerberosCryptoError>;

pub use checksum::{Checksum, ChecksumSuite};
pub use cipher::{Cipher, CipherSuite};