Skip to content

Commit

Permalink
refactor: add consts crate
Browse files Browse the repository at this point in the history
  • Loading branch information
AshGw committed May 25, 2024
1 parent e061ae1 commit 1b1901b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub const BLOCK_SIZE: usize = 16;
pub const NONCE_SIZE: usize = 12;
pub const TAG_SIZE: usize = 16;
16 changes: 6 additions & 10 deletions src/ctr.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
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;

use crate::types::CTRInitializer;
pub struct Aes256Ctr32(ctr::Ctr32BE<Aes256>);

const _BLOCK_SIZE: usize =
<Aes256 as aes::cipher::BlockSizeUser>::BlockSize::USIZE;
const _NONCE_SIZE: usize = _BLOCK_SIZE - 4;

impl Aes256Ctr32 {
pub fn new(
algo: Aes256,
nonce: &Nonce,
initializer: CTRInitializer,
) -> Result<Self> {
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))
}

Expand Down
4 changes: 1 addition & 3 deletions src/gcm.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down

0 comments on commit 1b1901b

Please sign in to comment.