Skip to content

Commit b66b0ce

Browse files
committed
Feat: Switch using nanorand crate with rand_chacha and rand_core crates for CSPRNG
Fix: Fix errors resulting from this change Feat: Make all features optional to allow only usage of the `random` feature without importing other crates Fix: Fix the issues with the example due to changes to the new API
1 parent d0681b9 commit b66b0ce

File tree

9 files changed

+350
-109
lines changed

9 files changed

+350
-109
lines changed

Cargo.toml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,22 @@ maintenance = { status = "passively-maintained" }
1818

1919
[dependencies]
2020
aead = { version = "0.5.2", features = ["bytes"], optional = true }
21-
arrayvec = { version = "0.7.2", default-features = false }
22-
blake3 = { version = "1.3.3", default-features = false }
23-
bytes = { version = "1.4.0" }
21+
arrayvec = { version = "0.7.2", default-features = true, optional = true }
22+
blake3 = { version = "1.3.3", default-features = true, optional = true }
23+
bytes = { version = "1.4.0", optional = true }
2424
chacha20poly1305 = { version = "0.10.1", features = [
2525
"reduced-round",
2626
], default-features = true, optional = true }
2727
lazy_static = { version = "1.4.0", optional = true }
28-
nanorand = { version = "0.7.0", features = [
29-
"chacha",
30-
"zeroize",
31-
"getrandom",
32-
], optional = true }
28+
rand_chacha = { version = "0.3.1", default-features = false, optional = true }
29+
rand_core = { version = "0.6.4", features = ["getrandom"], optional = true }
3330
zeroize = { version = "1.5.7", default-features = false }
3431

3532
[features]
36-
default = ["symm_asymm"]
33+
default = ["symm_asymm", "random"]
3734
encryption = ["dep:aead", "dep:chacha20poly1305", "dep:lazy_static", "random"]
38-
random = ["dep:nanorand"]
39-
symm_asymm = []
35+
random = ["dep:rand_core", "rand_chacha"]
36+
symm_asymm = ["dep:bytes", "dep:arrayvec", "dep:blake3"]
4037
clonable_mem = []
4138
full = ["symm_asymm", "clonable_mem", "random", "encryption"]
4239

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,22 @@ use memsecurity::*;
3636
fn main() {
3737
let mut foo = EncryptedMem::<32>::new();
3838

39-
let plaintext_bytes = ZeroizeBytesArray::csprng();
39+
let plaintext_bytes = CsprngArray::<32>::gen();
4040

41-
println!(" PLAINTEXT: {:?}", plaintext_bytes); //WARNING: THIS IS AN EXAMPLE, DO NOT PRINT SECRETS IN CODE
41+
println!(" PLAINTEXT: {:?}", plaintext_bytes.expose()); //SECURELY PRINTED TO CONSOLE USING DEBUG TRAIT
42+
println!(" PLAINTEXT: {:?}", plaintext_bytes.expose()); //SECURELY PRINTED TO CONSOLE USING DISPLAY TRAIT
43+
println!(" PLAINTEXT: {:?}", plaintext_bytes.expose()); //WARNING: THIS IS AN EXAMPLE, DO NOT PRINT SECRETS IN CODE
4244

43-
foo.encrypt(&plaintext_bytes).unwrap();
45+
let data = ZeroizeBytesArray::new_with_data(plaintext_bytes.expose());
46+
47+
foo.encrypt(&data).unwrap();
4448

4549
println!("CIPHERTEXT: {:?}", foo.ciphertext());
4650
println!(" XNONCE: {:?}", foo.xnonce());
4751

4852
let decrypted = foo.decrypt().unwrap();
4953

5054
println!(" DECRYPTED:{:?}", decrypted);
51-
assert_eq!(plaintext_bytes, decrypted);
55+
assert_eq!(data, decrypted);
5256
}
5357
```

examples/simple.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@ use memsecurity::*;
33
fn main() {
44
let mut foo = EncryptedMem::<32>::new();
55

6-
let plaintext_bytes = ZeroizeBytesArray::csprng();
6+
let plaintext_bytes = CsprngArray::<32>::gen();
77

8-
println!(" PLAINTEXT: {:?}", plaintext_bytes); //WARNING: THIS IS AN EXAMPLE, DO NOT PRINT SECRETS IN CODE
8+
println!(" PLAINTEXT: {:?}", plaintext_bytes.expose()); //SECURELY PRINTED TO CONSOLE USING DEBUG TRAIT
9+
println!(" PLAINTEXT: {:?}", plaintext_bytes.expose()); //SECURELY PRINTED TO CONSOLE USING DISPLAY TRAIT
10+
println!(" PLAINTEXT: {:?}", plaintext_bytes.expose()); //WARNING: THIS IS AN EXAMPLE, DO NOT PRINT SECRETS IN CODE
911

10-
foo.encrypt(&plaintext_bytes).unwrap();
12+
let data = ZeroizeBytesArray::new_with_data(plaintext_bytes.expose());
13+
14+
foo.encrypt(&data).unwrap();
1115

1216
println!("CIPHERTEXT: {:?}", foo.ciphertext());
1317
println!(" XNONCE: {:?}", foo.xnonce());
1418

1519
let decrypted = foo.decrypt().unwrap();
1620

1721
println!(" DECRYPTED:{:?}", decrypted);
18-
assert_eq!(plaintext_bytes, decrypted);
22+
assert_eq!(data, decrypted);
1923
}

src/errors.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@ pub type MemSecurityResult<T> = Result<T, MemSecurityErr>;
55
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
66
pub enum MemSecurityErr {
77
/// An error was encountered while encrypting the data
8+
#[cfg(feature = "encryption")]
89
EncryptionErr,
9-
/// An error was encountered when decrypting data using XChaCha12Poly1305
10+
/// An error was encountered when decrypting data using XChaCha12Poly1305
11+
#[cfg(feature = "encryption")]
1012
DecryptionError,
13+
/// The length of the arrays should be the same
14+
InvalidArrayLength {
15+
/// The length defined in generic value `N` in `const N: usize`
16+
const_n_len: usize,
17+
/// The length of the mutable array `&mut [u8; N]`
18+
buffer_len: usize,
19+
},
1120
}

src/keygen.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,25 @@ mod keymaker {
2323

2424
use super::{SealingKeyPages, DEFAULT_VAULT_PAGES, DEFAULT_VAULT_PAGE_SIZE};
2525
use crate::{
26-
EncryptedMem, MemSecurityErr, MemSecurityResult, ZeroizeBytes, ZeroizeBytesArray,
27-
TAG_LENGTH,
26+
CsprngArray, EncryptedMem, MemSecurityErr, MemSecurityResult, ZeroizeBytes,
27+
ZeroizeBytesArray, TAG_LENGTH,
2828
};
2929
use bytes::BytesMut;
3030
use chacha20poly1305::{
3131
aead::{AeadInPlace, KeyInit},
3232
Key, XChaCha12Poly1305, XNonce,
3333
};
34-
use nanorand::{ChaCha8, Rng};
3534

3635
lazy_static::lazy_static! {
3736
static ref PREKEY: SealingKeyPages = {
3837

3938
let mut pages = [[0u8; DEFAULT_VAULT_PAGE_SIZE]; DEFAULT_VAULT_PAGES];
4039

4140
(0..DEFAULT_VAULT_PAGES).for_each(|vault_page_index| {
42-
let mut chacha_rng = ChaCha8::new();
43-
let mut random_bytes = [0; DEFAULT_VAULT_PAGE_SIZE];
44-
(0..DEFAULT_VAULT_PAGE_SIZE).for_each(|index| {
45-
random_bytes[index] = chacha_rng.generate::<u8>();
46-
});
4741

48-
pages[vault_page_index] = random_bytes;
42+
let random_bytes = CsprngArray::<DEFAULT_VAULT_PAGE_SIZE>::gen();
43+
44+
random_bytes.take(&mut pages[vault_page_index]).unwrap(); //Never fails since array lengths are always equal
4945
});
5046

5147
SealingKeyPages(pages)

src/lib.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,56 +20,56 @@ pub use keygen::*;
2020
mod zeroizable_arrays;
2121
pub use zeroizable_arrays::*;
2222

23-
#[cfg(feature = "encryption")]
2423
mod errors;
25-
#[cfg(feature = "encryption")]
2624
pub use errors::*;
2725

26+
#[cfg(feature = "random")]
27+
mod random;
28+
#[cfg(feature = "random")]
29+
pub use random::*;
30+
2831
mod traits;
2932
pub use traits::*;
3033

3134
/// Re-export crates
3235
#[cfg(feature = "encryption")]
3336
pub use aead;
37+
#[cfg(feature = "encryption")]
3438
pub use arrayvec;
39+
#[cfg(feature = "encryption")]
3540
pub use blake3;
41+
#[cfg(feature = "encryption")]
3642
pub use bytes;
3743
#[cfg(feature = "encryption")]
3844
pub use chacha20poly1305;
3945
#[cfg(feature = "encryption")]
4046
pub use lazy_static;
4147
#[cfg(feature = "random")]
42-
pub use nanorand;
48+
pub use rand_chacha;
49+
#[cfg(feature = "random")]
50+
pub use rand_core;
4351
pub use zeroize;
4452

4553
// TODO Test different nonces
4654
// TODO Test different cipher and plaintext
4755

4856
#[cfg(tests)]
4957
mod sanity_tests {
50-
use memsecurity::*;
58+
use memsecurity::{prelude::*, zeroize::Zeroize};
5159

5260
#[test]
5361
fn csprng() {
5462
// Create a new array of 32 bytes that is randomly generated and cryptographically secure
55-
let plaintext_bytes1 = ZeroizeBytesArray::<32>::csprng();
56-
let plaintext_bytes2 = ZeroizeBytesArray::<32>::csprng();
57-
assert_eq!(
58-
plaintext_bytes1.expose().len(),
59-
plaintext_bytes2.expose().len()
60-
);
61-
assert_ne!(plaintext_bytes1, plaintext_bytes2);
62-
63-
let plaintext_bytes1 = ZeroizeArray::<32>::csprng();
64-
let plaintext_bytes2 = ZeroizeArray::<32>::csprng();
63+
let plaintext_bytes1 = CsprngArray::<32>::csprng();
64+
let plaintext_bytes2 = CsprngArray::<64>::csprng();
6565
assert_eq!(
6666
plaintext_bytes1.expose().len(),
6767
plaintext_bytes2.expose().len()
6868
);
6969
assert_ne!(plaintext_bytes1, plaintext_bytes2);
7070

71-
let plaintext_bytes1 = ZeroizeBytes::csprng::<32>();
72-
let plaintext_bytes2 = ZeroizeBytes::csprng::<32>();
71+
let plaintext_bytes1 = CsprngArray::<32>::csprng();
72+
let plaintext_bytes2 = CsprngArray::<32>::csprng();
7373
assert_eq!(
7474
plaintext_bytes1.expose().len(),
7575
plaintext_bytes2.expose().len()
@@ -81,10 +81,13 @@ mod sanity_tests {
8181
fn cipher() {
8282
let mut foo = EncryptedMem::<32>::new();
8383

84-
let plaintext_bytes = ZeroizeBytesArray::csprng();
85-
foo.encrypt(&plaintext_bytes).unwrap();
84+
let mut plaintext_bytes = CsprngArray::<32>::csprng();
85+
let data = ZeroizeBytesArray::new_with_data(plaintext_bytes.expose());
86+
foo.encrypt(&data).unwrap();
8687
let decrypted = foo.decrypt().unwrap();
8788

88-
assert_eq!(plaintext_bytes, decrypted);
89+
assert_eq!(data, decrypted);
90+
plaintext_bytes.zeroize();
91+
assert_eq!(plaintext_bytes.expose(), [0u8; 32]);
8992
}
9093
}

0 commit comments

Comments
 (0)