Skip to content

Commit 0fbad03

Browse files
committed
moving sponge logic into cas-lib
1 parent 5416ef7 commit 0fbad03

File tree

4 files changed

+9
-45
lines changed

4 files changed

+9
-45
lines changed

index.node

1 KB
Binary file not shown.

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,5 @@ mod digital_signature {
3131
}
3232

3333
mod sponges {
34-
pub mod cas_ascon_aead;
3534
pub mod ascon_aead;
3635
}

src/sponges/ascon_aead.rs

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,11 @@
11

22
use aes_gcm::AeadCore;
3-
use ascon_aead::{aead::{generic_array::GenericArray, Aead, KeyInit, OsRng}, Ascon128};
3+
use cas_lib::sponges::{ascon_aead::AsconAead, cas_ascon_aead::CASAsconAead};
44
use napi_derive::napi;
55

6-
use super::cas_ascon_aead::{CASAsconAead};
7-
pub struct AsconAead;
8-
9-
impl CASAsconAead for AsconAead {
10-
fn encrypt(key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
11-
let key_generic_array = GenericArray::from_slice(&key);
12-
let nonce_generic_array = GenericArray::from_slice(&nonce);
13-
let cipher = Ascon128::new(key_generic_array);
14-
let ciphertext = cipher.encrypt(&nonce_generic_array, plaintext.as_ref()).unwrap();
15-
ciphertext
16-
}
17-
18-
fn decrypt(key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
19-
let key_generic_array = GenericArray::from_slice(&key);
20-
let nonce_generic_array = GenericArray::from_slice(&nonce);
21-
let cipher = Ascon128::new(key_generic_array);
22-
let plaintext = cipher.decrypt(&nonce_generic_array, ciphertext.as_ref()).unwrap();
23-
plaintext
24-
}
25-
26-
fn generate_key() -> Vec<u8> {
27-
return Ascon128::generate_key(&mut OsRng).to_vec();
28-
}
29-
30-
fn generate_nonce() -> Vec<u8> {
31-
return Ascon128::generate_nonce(&mut OsRng).to_vec();
32-
}
33-
}
34-
356
#[napi]
367
pub fn ascon128_key_generate() -> Vec<u8> {
37-
return AsconAead::generate_key();
8+
return <AsconAead as CASAsconAead>::generate_key();
389
}
3910

4011
#[test]
@@ -45,7 +16,7 @@ fn test_ascon128_key_generate() {
4516

4617
#[napi]
4718
pub fn ascon128_nonce_generate() -> Vec<u8> {
48-
return AsconAead::generate_nonce();
19+
return <AsconAead as CASAsconAead>::generate_nonce();
4920
}
5021

5122
#[test]
@@ -56,27 +27,27 @@ pub fn test_ascon128_nonce_generate() {
5627

5728
#[napi]
5829
pub fn ascon128_encrypt(key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
59-
return AsconAead::encrypt(key, nonce, plaintext);
30+
return <AsconAead as CASAsconAead>::encrypt(key, nonce, plaintext);
6031
}
6132

6233
#[test]
6334
pub fn test_ascon128_encrypt() {
64-
let key = AsconAead::generate_key();
65-
let nonce = AsconAead::generate_nonce();
35+
let key = <AsconAead as CASAsconAead>::generate_key();
36+
let nonce = <AsconAead as CASAsconAead>::generate_nonce();
6637
let plaintext = b"Hello, World!".to_vec();
6738
let ciphertext = ascon128_encrypt(key.clone(), nonce.clone(), plaintext.clone());
6839
assert_ne!(ciphertext, plaintext);
6940
}
7041

7142
#[napi]
7243
pub fn ascon128_decrypt(key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
73-
return AsconAead::decrypt(key, nonce, ciphertext);
44+
return <AsconAead as CASAsconAead>::decrypt(key, nonce, ciphertext);
7445
}
7546

7647
#[test]
7748
pub fn test_ascon128_decrypt() {
78-
let key = AsconAead::generate_key();
79-
let nonce = AsconAead::generate_nonce();
49+
let key = <AsconAead as CASAsconAead>::generate_key();
50+
let nonce = <AsconAead as CASAsconAead>::generate_nonce();
8051
let plaintext = b"Hello, World!".to_vec();
8152
let ciphertext = ascon128_encrypt(key.clone(), nonce.clone(), plaintext.clone());
8253
let decrypted = ascon128_decrypt(key.clone(), nonce.clone(), ciphertext.clone());

src/sponges/cas_ascon_aead.rs

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)