diff --git a/src/constants.rs b/src/constants.rs new file mode 100644 index 0000000..91d65f0 --- /dev/null +++ b/src/constants.rs @@ -0,0 +1,3 @@ +pub const BLOCK_SIZE: usize = 16; +pub const NONCE_SIZE: usize = 12; +pub const TAG_SIZE: usize = 16; diff --git a/src/ctr.rs b/src/ctr.rs index 84b0268..1369d77 100644 --- a/src/ctr.rs +++ b/src/ctr.rs @@ -1,6 +1,6 @@ +use crate::constants::{BLOCK_SIZE, NONCE_SIZE}; use crate::error::Error; use crate::types::{Bytes, Key, Nonce, Result}; -use aes::cipher::typenum::Unsigned; use aes::cipher::KeyInit; use aes::cipher::{InnerIvInit, StreamCipher, StreamCipherSeek}; use aes::Aes256; @@ -8,28 +8,24 @@ use aes::Aes256; use crate::types::CTRInitializer; pub struct Aes256Ctr32(ctr::Ctr32BE); -const _BLOCK_SIZE: usize = - ::BlockSize::USIZE; -const _NONCE_SIZE: usize = _BLOCK_SIZE - 4; - impl Aes256Ctr32 { pub fn new( algo: Aes256, nonce: &Nonce, initializer: CTRInitializer, ) -> Result { - if !is_valid_nonce_size(nonce, _NONCE_SIZE) { + if !is_valid_nonce_size(nonce, NONCE_SIZE) { return Err(Error::InvalidNonceSize { - expected_size: _NONCE_SIZE, + expected_size: NONCE_SIZE, }); } - let mut _nonce_block = [0u8; _BLOCK_SIZE]; - _nonce_block[0.._NONCE_SIZE].copy_from_slice(nonce); + let mut _nonce_block = [0u8; BLOCK_SIZE]; + _nonce_block[0..NONCE_SIZE].copy_from_slice(nonce); let mut ctr = ctr::Ctr32BE::from_core( ctr::CtrCore::inner_iv_init(algo, &_nonce_block.into()), ); - ctr.seek(_BLOCK_SIZE * (initializer as usize)); + ctr.seek(BLOCK_SIZE * (initializer as usize)); Ok(Self(ctr)) } diff --git a/src/gcm.rs b/src/gcm.rs index 293b148..c7b1b32 100644 --- a/src/gcm.rs +++ b/src/gcm.rs @@ -1,3 +1,4 @@ +use crate::constants::{NONCE_SIZE, TAG_SIZE}; use crate::ctr::Aes256Ctr32; use crate::error::Error; use crate::types::{Bytes, Key, Nonce, Result}; @@ -7,9 +8,6 @@ use aes::Aes256; use ghash::universal_hash::UniversalHash; use ghash::GHash; -pub const TAG_SIZE: usize = 16; -pub const NONCE_SIZE: usize = 12; - #[derive(Clone)] pub struct GcmGhash { ghash: GHash, diff --git a/src/lib.rs b/src/lib.rs index 20a5e63..0ccf9d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,13 @@ +pub mod constants; pub mod ctr; pub mod error; pub mod gcm; pub mod types; +use crate::constants::TAG_SIZE; use ctr::Aes256Ctr32; use error::Error; -use gcm::{setup as setup_gcm, GcmGhash, TAG_SIZE}; +use gcm::{setup as setup_gcm, GcmGhash}; use subtle::ConstantTimeEq; use types::{Bytes, Key, Nonce, Result};