-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from Cryptographic-API-Services/pre-release
Ascon 128
- Loading branch information
Showing
13 changed files
with
189 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
ascon128Decrypt, | ||
ascon128Encrypt, | ||
ascon128KeyGenerate, | ||
ascon128NonceGenerate, | ||
} from "../../index"; | ||
|
||
export class AsconWrapper { | ||
ascon128Key(): Array<number> { | ||
return ascon128KeyGenerate(); | ||
} | ||
|
||
ascon128Nonce(): Array<number> { | ||
return ascon128NonceGenerate(); | ||
} | ||
|
||
ascon128Encrypt( | ||
key: Array<number>, | ||
nonce: Array<number>, | ||
plaintext: Array<number>, | ||
): Array<number> { | ||
if (!key || key.length === 0) { | ||
throw new Error("Key is required"); | ||
} | ||
if (!nonce || nonce.length === 0) { | ||
throw new Error("Nonce is required"); | ||
} | ||
if (!plaintext || plaintext.length === 0) { | ||
throw new Error("Plaintext is required"); | ||
} | ||
return ascon128Encrypt(key, nonce, plaintext); | ||
} | ||
|
||
ascon128Decrypt( | ||
key: Array<number>, | ||
nonce: Array<number>, | ||
ciphertext: Array<number>, | ||
): Array<number> { | ||
if (!key || key.length === 0) { | ||
throw new Error("Key is required"); | ||
} | ||
if (!nonce || nonce.length === 0) { | ||
throw new Error("Nonce is required"); | ||
} | ||
if (!ciphertext || ciphertext.length === 0) { | ||
throw new Error("Ciphertext is required"); | ||
} | ||
return ascon128Decrypt(key, nonce, ciphertext); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { AsconWrapper } from "./ascon-wrapper"; | ||
|
||
export {AsconWrapper} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
|
||
use aes_gcm::AeadCore; | ||
use ascon_aead::{aead::{generic_array::GenericArray, Aead, KeyInit, OsRng}, Ascon128}; | ||
use napi_derive::napi; | ||
|
||
use super::cas_ascon_aead::{CASAsconAead}; | ||
pub struct AsconAead; | ||
|
||
impl CASAsconAead for AsconAead { | ||
fn encrypt(key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> { | ||
let key_generic_array = GenericArray::from_slice(&key); | ||
let nonce_generic_array = GenericArray::from_slice(&nonce); | ||
let cipher = Ascon128::new(key_generic_array); | ||
let ciphertext = cipher.encrypt(&nonce_generic_array, plaintext.as_ref()).unwrap(); | ||
ciphertext | ||
} | ||
|
||
fn decrypt(key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> { | ||
let key_generic_array = GenericArray::from_slice(&key); | ||
let nonce_generic_array = GenericArray::from_slice(&nonce); | ||
let cipher = Ascon128::new(key_generic_array); | ||
let plaintext = cipher.decrypt(&nonce_generic_array, ciphertext.as_ref()).unwrap(); | ||
plaintext | ||
} | ||
|
||
fn generate_key() -> Vec<u8> { | ||
return Ascon128::generate_key(&mut OsRng).to_vec(); | ||
} | ||
|
||
fn generate_nonce() -> Vec<u8> { | ||
return Ascon128::generate_nonce(&mut OsRng).to_vec(); | ||
} | ||
} | ||
|
||
#[napi] | ||
pub fn ascon128_key_generate() -> Vec<u8> { | ||
return AsconAead::generate_key(); | ||
} | ||
|
||
#[test] | ||
fn test_ascon128_key_generate() { | ||
let key = ascon128_key_generate(); | ||
assert_eq!(key.len(), 16); | ||
} | ||
|
||
#[napi] | ||
pub fn ascon128_nonce_generate() -> Vec<u8> { | ||
return AsconAead::generate_nonce(); | ||
} | ||
|
||
#[test] | ||
pub fn test_ascon128_nonce_generate() { | ||
let nonce = ascon128_nonce_generate(); | ||
assert_eq!(nonce.len(), 16); | ||
} | ||
|
||
#[napi] | ||
pub fn ascon128_encrypt(key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> { | ||
return AsconAead::encrypt(key, nonce, plaintext); | ||
} | ||
|
||
#[test] | ||
pub fn test_ascon128_encrypt() { | ||
let key = AsconAead::generate_key(); | ||
let nonce = AsconAead::generate_nonce(); | ||
let plaintext = b"Hello, World!".to_vec(); | ||
let ciphertext = ascon128_encrypt(key.clone(), nonce.clone(), plaintext.clone()); | ||
assert_ne!(ciphertext, plaintext); | ||
} | ||
|
||
#[napi] | ||
pub fn ascon128_decrypt(key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> { | ||
return AsconAead::decrypt(key, nonce, ciphertext); | ||
} | ||
|
||
#[test] | ||
pub fn test_ascon128_decrypt() { | ||
let key = AsconAead::generate_key(); | ||
let nonce = AsconAead::generate_nonce(); | ||
let plaintext = b"Hello, World!".to_vec(); | ||
let ciphertext = ascon128_encrypt(key.clone(), nonce.clone(), plaintext.clone()); | ||
let decrypted = ascon128_decrypt(key.clone(), nonce.clone(), ciphertext.clone()); | ||
assert_eq!(decrypted, plaintext); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
pub trait CASAsconAead { | ||
fn generate_key() -> Vec<u8>; | ||
fn generate_nonce() -> Vec<u8>; | ||
fn encrypt(key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8>; | ||
fn decrypt(key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8>; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { AsconWrapper } from "../src-ts/sponges/ascon-wrapper"; | ||
import { assert } from "chai"; | ||
import { areEqual } from "./helpers/array"; | ||
|
||
describe("Sponges Tests", () => { | ||
it("Ascon 128 Encrypt", () => { | ||
const wrapper: AsconWrapper = new AsconWrapper(); | ||
const key: Array<number> = wrapper.ascon128Key(); | ||
const nonce: Array<number> = wrapper.ascon128Nonce(); | ||
const tohashed: string = "This is my array to encrypt"; | ||
const encoder = new TextEncoder(); | ||
const tohashBytes: Array<number> = Array.from(encoder.encode(tohashed)); | ||
const ciphertext = wrapper.ascon128Encrypt(key, nonce, tohashBytes); | ||
assert.isNotTrue(areEqual(tohashBytes, ciphertext)); | ||
}); | ||
|
||
it ("Ascon 128 Decrypt", () => { | ||
const wrapper: AsconWrapper = new AsconWrapper(); | ||
const key: Array<number> = wrapper.ascon128Key(); | ||
const nonce: Array<number> = wrapper.ascon128Nonce(); | ||
const tohashed: string = "This is my array to encrypt"; | ||
const encoder = new TextEncoder(); | ||
const tohashBytes: Array<number> = Array.from(encoder.encode(tohashed)); | ||
const ciphertext = wrapper.ascon128Encrypt(key, nonce, tohashBytes); | ||
const plaintext = wrapper.ascon128Decrypt(key, nonce, ciphertext); | ||
assert.equal(areEqual(plaintext, tohashBytes), true); | ||
}); | ||
}); |