Skip to content

Commit

Permalink
Refactor internal storage to remove direct generic_array dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastinas committed Sep 11, 2024
1 parent 295353c commit 817e473
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ zeroize = { version = "1.4", optional = true, default-features = false, features
serde = { version = "1", optional = true, default-features = false, features = [
"derive",
] }
# needs to be compatible with aes and ctr; required to enable serde and zeroize features
generic-array = { version = "0.14", default-features = false }

[features]
std = ["ctr/std"]
zeroize = ["dep:zeroize", "ctr/zeroize", "aes/zeroize", "generic-array/zeroize"]
zeroize = ["dep:zeroize", "ctr/zeroize", "aes/zeroize"]
default = ["std", "serde", "zeroize"]
serde = ["dep:serde", "generic-array/serde"]
serde = ["dep:serde"]

[package.metadata.docs.rs]
all-features = true
Expand Down
27 changes: 13 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ pub type Seed = GenericArray<u8, SeedLength>;
#[cfg_attr(feature = "zeroize", derive(zeroize::ZeroizeOnDrop))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NistPqcAes256CtrRng {
key: GenericArray<u8, KeyLength>,
v: GenericArray<u8, VLength>,
key: [u8; KeyLength::USIZE],
v: [u8; VLength::USIZE],
}

impl SeedableRng for NistPqcAes256CtrRng {
Expand All @@ -74,11 +74,11 @@ impl SeedableRng for NistPqcAes256CtrRng {
cipher.seek(16);
cipher.apply_keystream(&mut seed);

let key_v = seed.as_slice();
Self {
key: *GenericArray::from_slice(&key_v[..KeyLength::USIZE]),
v: *GenericArray::from_slice(&key_v[KeyLength::USIZE..]),
}
let mut key = [0; KeyLength::USIZE];
let mut v = [0; VLength::USIZE];
key.copy_from_slice(&seed[..KeyLength::USIZE]);
v.copy_from_slice(&seed[KeyLength::USIZE..]);
Self { key, v }
}
}

Expand Down Expand Up @@ -114,16 +114,19 @@ impl RngCore for NistPqcAes256CtrRng {
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
let mut cipher = Aes256Ctr::new(&self.key, &self.v);
let mut cipher = Aes256Ctr::new(
GenericArray::from_slice(&self.key),
GenericArray::from_slice(&self.v),
);
cipher.seek(16);
cipher.apply_keystream(dest);
cipher.seek(
(cipher.current_pos::<usize>() + (VLength::USIZE - 1)) / VLength::USIZE
* VLength::USIZE,
);

let mut key = GenericArray::default();
let mut v = GenericArray::default();
let mut key = [0; KeyLength::USIZE];
let mut v = [0; VLength::USIZE];
cipher.apply_keystream(&mut key);
cipher.apply_keystream(&mut v);
self.key = key;
Expand Down Expand Up @@ -155,7 +158,6 @@ mod test {
0x73, 0x8b, 0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e, 0x07, 0x4e, 0xc5, 0xd3,
0xba, 0xf3, 0x9d, 0x18,
]
.into()
);
let mut buf = [0; 8];
rng.fill_bytes(&mut buf);
Expand All @@ -167,7 +169,6 @@ mod test {
0x68, 0x0b, 0xaf, 0x44, 0x43, 0x92, 0x2a, 0x11, 0x91, 0x78, 0xfb, 0x81, 0x91, 0xd4,
0xc9, 0xd0, 0xa5, 0x8f,
]
.into()
);
let mut buf = [0; 4];
rng.fill_bytes(&mut buf);
Expand All @@ -184,7 +185,6 @@ mod test {
0x73, 0x8b, 0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e, 0x07, 0x4e, 0xc5, 0xd3,
0xba, 0xf3, 0x9d, 0x18,
]
.into()
);
let mut buf = [0; 16];
rng.fill_bytes(&mut buf);
Expand All @@ -202,7 +202,6 @@ mod test {
0x68, 0x0b, 0xaf, 0x44, 0x43, 0x92, 0x2a, 0x11, 0x91, 0x78, 0xfb, 0x81, 0x91, 0xd4,
0xc9, 0xd0, 0xa5, 0x8f,
]
.into()
);
let mut buf = [0; 4];
rng.fill_bytes(&mut buf);
Expand Down

0 comments on commit 817e473

Please sign in to comment.