use crypto_common::array::{Array, ArraySize};
use smb_msg::*;
use crate::crypto::{CryptoError, EncryptingAlgo};
pub(crate) fn trim_nonce<U: ArraySize>(
algo: &dyn EncryptingAlgo,
nonce: &EncryptionNonce,
) -> Array<u8, U> {
debug_assert!(nonce[algo.nonce_size()..].iter().all(|&x| x == 0));
Array::try_from(&nonce[..algo.nonce_size()]).unwrap()
}
pub const ENCRYPTING_ALGOS: &[EncryptionCipher] = &[
#[cfg(feature = "encrypt_aes128ccm")]
EncryptionCipher::Aes128Ccm,
#[cfg(feature = "encrypt_aes256ccm")]
EncryptionCipher::Aes256Ccm,
#[cfg(feature = "encrypt_aes128gcm")]
EncryptionCipher::Aes128Gcm,
#[cfg(feature = "encrypt_aes256gcm")]
EncryptionCipher::Aes256Gcm,
];
pub fn make_encrypting_algo(
encrypting_algorithm: EncryptionCipher,
encrypting_key: &[u8],
) -> Result<Box<dyn EncryptingAlgo>, CryptoError> {
if !ENCRYPTING_ALGOS.contains(&encrypting_algorithm) {
return Err(CryptoError::UnsupportedEncryptionAlgorithm(
encrypting_algorithm,
));
}
if cfg!(feature = "__debug-dump-keys") {
log::debug!(
"Using encryption algorithm {:?} with key {:02x?}",
encrypting_algorithm,
encrypting_key
);
}
match encrypting_algorithm {
#[cfg(feature = "encrypt_aes128ccm")]
EncryptionCipher::Aes128Ccm => Ok(super::encrypt_ccm::Aes128CcmEncryptor::build(
encrypting_key,
)?),
#[cfg(feature = "encrypt_aes256ccm")]
EncryptionCipher::Aes256Ccm => Ok(super::encrypt_ccm::Aes256CcmEncryptor::build(
encrypting_key,
)?),
#[cfg(feature = "encrypt_aes128gcm")]
EncryptionCipher::Aes128Gcm => Ok(super::encrypt_gcm::Aes128GcmEncryptor::build(
encrypting_key,
)?),
#[cfg(feature = "encrypt_aes256gcm")]
EncryptionCipher::Aes256Gcm => Ok(super::encrypt_gcm::Aes256GcmEncryptor::build(
encrypting_key,
)?),
#[allow(unreachable_patterns)]
_ => unreachable!("No algorithms should reach the disabled module instead."),
}
}