Skip to content

Commit

Permalink
refactor: remove unused circular dependency and also rewrite IV handl…
Browse files Browse the repository at this point in the history
…ing to not require lazy_static

* Remove unused circular dependency

* Change IV to a const array and remove lazy_static dependency

The old code was taking a copy of IV. Instead, we can just use
`GenericArray::from` to convert the `u8` array into a `GenericArray`.
This approach has the same cost without the need for `lazy_static`.
  • Loading branch information
sebastinas committed Sep 13, 2022
1 parent 721b287 commit 30aea2e
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 9 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ byteorder = "^1.4"
cast5 = "^0.10.0"
cfb-mode = "^0.7.0"
chrono = { version = "^0.4", default-features = false, features = ["clock", "std"] }
circular = "^0.3"
cipher = "^0.3"
clear_on_drop = { version = "0.2.3", features = ["no_cc"] }
crc24 = "^0.1"
Expand All @@ -36,7 +35,6 @@ des = "^0.7"
digest = "^0.9"
generic-array = "^0.14"
hex = "^0.4"
lazy_static = "1.2.0"
log = "0.4.6"
md-5 = "^0.9"
nom = "^4.2"
Expand Down
8 changes: 3 additions & 5 deletions src/crypto/aes_kw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use generic_array::GenericArray;

use crate::errors::Result;

lazy_static! {
static ref IV: GenericArray<u8, U8> = arr![u8; 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6];
}
const IV: [u8; 8] = [0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6];

/// AES Key Wrap
/// As defined in RFC 3394.
Expand Down Expand Up @@ -56,7 +54,7 @@ macro_rules! impl_aes_kw {
// 1) Initialize variables

// Set A to the IV
let mut a = *IV;
let mut a = GenericArray::from(IV);

// for i = 1 to n: R[i] = P[i]
let mut r = p.clone();
Expand Down Expand Up @@ -147,7 +145,7 @@ macro_rules! impl_aes_kw {

// 3) output the results

if a == *IV {
if &a == GenericArray::<u8, U8>::from_slice(&IV) {
Ok(r.iter().fold(Vec::with_capacity(r.len() * 8), |mut acc, v| {
acc.extend(v);
acc
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ extern crate num_derive;
#[macro_use]
extern crate generic_array;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
#[macro_use]
extern crate derive_builder;
Expand Down

0 comments on commit 30aea2e

Please sign in to comment.