Skip to content

Commit 426c719

Browse files
authored
changing ascon back to Vec<u8> (#33)
1 parent 87a5be4 commit 426c719

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

src/sponges/ascon_aead.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ pub struct AsconAead;
99

1010
impl CASAsconAead for AsconAead {
1111
/// Encrypts with AscondAead
12-
fn encrypt(key: [u8; 16], nonce: [u8; 16], plaintext: Vec<u8>) -> Vec<u8> {
12+
fn encrypt(key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
13+
if key.len() != 16 || nonce.len() != 16 {
14+
panic!("Key and nonce must be 16 bytes long");
15+
}
1316
let key_generic_array = GenericArray::from_slice(&key);
1417
let nonce_generic_array = GenericArray::from_slice(&nonce);
1518
let cipher = Ascon128::new(key_generic_array);
@@ -18,7 +21,10 @@ impl CASAsconAead for AsconAead {
1821
}
1922

2023
/// Encrypts with AscondAead on the threadpool
21-
fn encrypt_threadpool(key: [u8; 16], nonce: [u8; 16], plaintext: Vec<u8>) -> Vec<u8> {
24+
fn encrypt_threadpool(key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
25+
if key.len() != 16 || nonce.len() != 16 {
26+
panic!("Key and nonce must be 16 bytes long");
27+
}
2228
let (sender, receiver) = mpsc::channel();
2329
rayon::spawn(move || {
2430
let ciphertext = Self::encrypt(key, nonce, plaintext);
@@ -29,7 +35,10 @@ impl CASAsconAead for AsconAead {
2935
}
3036

3137
/// Decrypts with AscondAead
32-
fn decrypt(key: [u8; 16], nonce: [u8; 16], ciphertext: Vec<u8>) -> Vec<u8> {
38+
fn decrypt(key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
39+
if key.len() != 16 || nonce.len() != 16 {
40+
panic!("Key and nonce must be 16 bytes long");
41+
}
3342
let key_generic_array = GenericArray::from_slice(&key);
3443
let nonce_generic_array = GenericArray::from_slice(&nonce);
3544
let cipher = Ascon128::new(key_generic_array);
@@ -38,7 +47,10 @@ impl CASAsconAead for AsconAead {
3847
}
3948

4049
/// Decrypts with AscondAead on the threadpool
41-
fn decrypt_threadpool(key: [u8; 16], nonce: [u8; 16], ciphertext: Vec<u8>) -> Vec<u8> {
50+
fn decrypt_threadpool(key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
51+
if key.len() != 16 || nonce.len() != 16 {
52+
panic!("Key and nonce must be 16 bytes long");
53+
}
4254
let (sender, receiver) = mpsc::channel();
4355
rayon::spawn(move || {
4456
let plaintext = Self::decrypt(key, nonce, ciphertext);
@@ -49,12 +61,12 @@ impl CASAsconAead for AsconAead {
4961
}
5062

5163
/// Generates a 16-byte key for Ascon Aead
52-
fn generate_key() -> [u8; 16] {
53-
return Ascon128::generate_key(&mut OsRng).into();
64+
fn generate_key() -> Vec<u8> {
65+
return Ascon128::generate_key(&mut OsRng).to_vec();
5466
}
5567

5668
/// Generates a 16-byte key for Ascon Aead on the threadpool
57-
fn generate_key_threadpool() -> [u8; 16] {
69+
fn generate_key_threadpool() -> Vec<u8> {
5870
let (sender, receiver) = mpsc::channel();
5971
rayon::spawn(move || {
6072
let key = Self::generate_key();
@@ -65,12 +77,12 @@ impl CASAsconAead for AsconAead {
6577
}
6678

6779
/// Generates a Ascon Aead nonce
68-
fn generate_nonce() -> [u8; 16] {
69-
return Ascon128::generate_nonce(&mut OsRng).into();
80+
fn generate_nonce() -> Vec<u8> {
81+
return Ascon128::generate_nonce(&mut OsRng).to_vec();
7082
}
7183

7284
/// Generates a Ascon Aead nonce on the threadpool
73-
fn generate_nonce_threadpool() -> [u8; 16] {
85+
fn generate_nonce_threadpool() -> Vec<u8> {
7486
let (sender, receiver) = mpsc::channel();
7587
rayon::spawn(move || {
7688
let key = <AsconAead as CASAsconAead>::generate_nonce();

src/sponges/cas_ascon_aead.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
pub trait CASAsconAead {
2-
fn generate_key() -> [u8; 16];
3-
fn generate_key_threadpool() -> [u8; 16];
4-
fn generate_nonce() -> [u8; 16];
5-
fn generate_nonce_threadpool() -> [u8; 16];
6-
fn encrypt(key: [u8; 16], nonce: [u8; 16], plaintext: Vec<u8>) -> Vec<u8>;
7-
fn encrypt_threadpool(key: [u8; 16], nonce: [u8; 16], plaintext: Vec<u8>) -> Vec<u8>;
8-
fn decrypt(key: [u8; 16], nonce: [u8; 16], ciphertext: Vec<u8>) -> Vec<u8>;
9-
fn decrypt_threadpool(key: [u8; 16], nonce: [u8; 16], ciphertext: Vec<u8>) -> Vec<u8>;
2+
fn generate_key() -> Vec<u8>;
3+
fn generate_key_threadpool() -> Vec<u8>;
4+
fn generate_nonce() -> Vec<u8>;
5+
fn generate_nonce_threadpool() -> Vec<u8>;
6+
fn encrypt(key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8>;
7+
fn encrypt_threadpool(key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8>;
8+
fn decrypt(key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8>;
9+
fn decrypt_threadpool(key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8>;
1010
}

tests/sponges.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod tests {
99
let file_bytes: Vec<u8> = std::fs::read(path).unwrap();
1010
let ascon_nonce = <AsconAead as CASAsconAead>::generate_nonce();
1111
let ascon_key = <AsconAead as CASAsconAead>::generate_key();
12-
let encrypted_bytes = <AsconAead as CASAsconAead>::encrypt(ascon_key, ascon_nonce, file_bytes.clone());
12+
let encrypted_bytes = <AsconAead as CASAsconAead>::encrypt(ascon_key.clone(), ascon_nonce.clone(), file_bytes.clone());
1313
let mut file = File::create("encrypted.docx").unwrap();
1414
file.write_all(&encrypted_bytes).unwrap();
1515

@@ -25,7 +25,7 @@ mod tests {
2525
let file_bytes: Vec<u8> = std::fs::read(path).unwrap();
2626
let ascon_nonce = <AsconAead as CASAsconAead>::generate_nonce_threadpool();
2727
let ascon_key = <AsconAead as CASAsconAead>::generate_key_threadpool();
28-
let encrypted_bytes = <AsconAead as CASAsconAead>::encrypt_threadpool(ascon_key, ascon_nonce, file_bytes.clone());
28+
let encrypted_bytes = <AsconAead as CASAsconAead>::encrypt_threadpool(ascon_key.clone(), ascon_nonce.clone(), file_bytes.clone());
2929
let mut file = File::create("encrypted.docx").unwrap();
3030
file.write_all(&encrypted_bytes).unwrap();
3131

0 commit comments

Comments
 (0)