Skip to content

Commit 7d7df40

Browse files
authored
Merge pull request #36 from Cryptographic-API-Services/#29-implement-hmac
#29 implement hmac
2 parents 8bcd5ce + 2404765 commit 7d7df40

File tree

10 files changed

+136
-3
lines changed

10 files changed

+136
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The official NPM page can be found [here](https://www.npmjs.com/package/cas-type
1212

1313

1414
## Consuming Library Documentation
15-
This Node.js NPM module is dependent on our Rust layer [here](./src) that contains methods to run industry-standard cryptographic operations sequentially, on threads, and the thread pool.
15+
This Node.js NPM module is dependent on our Rust layer [cas-lib](https://github.com/Cryptographic-API-Services/cas-lib). that contains methods to run industry-standard cryptographic operations sequentially, on threads, and the thread pool.
1616

1717
We utilize some smart people's existing work and we believe their documentation should be reviewed when possible.
1818
- [Spin Research](https://github.com/SpinResearch)

index.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ export function ascon128KeyGenerate(): Array<number>
4747
export function ascon128NonceGenerate(): Array<number>
4848
export function ascon128Encrypt(key: Array<number>, nonce: Array<number>, plaintext: Array<number>): Array<number>
4949
export function ascon128Decrypt(key: Array<number>, nonce: Array<number>, ciphertext: Array<number>): Array<number>
50+
export function hmacSign(key: Array<number>, message: Array<number>): Array<number>
51+
export function hmacSignThreadpool(key: Array<number>, message: Array<number>): Array<number>
52+
export function hmacVerify(key: Array<number>, message: Array<number>, signature: Array<number>): boolean
53+
export function hmacVerifyThreadpool(key: Array<number>, message: Array<number>, signature: Array<number>): boolean
5054
export type CASx25519SecretPublicKeyResult = CaSx25519SecretPublicKeyResult
5155
export class CaSx25519SecretPublicKeyResult {
5256
publicKey: Array<number>

index.node

20 KB
Binary file not shown.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
{
33
"name": "cas-typescript-sdk",
4-
"version": "1.0.25",
4+
"version": "1.0.26",
55
"description": "",
66
"main": "lib/index.js",
77
"types": "lib/index.d.ts",

src-ts/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export * from "./symmetric/index";
55
export * from "./asymmetric/index";
66
export * from "./hybrid/index";
77
export * from "./digital-signature";
8-
export * from "./sponges/index";
8+
export * from "./sponges/index";
9+
export * from "./message/index";

src-ts/message/hmac.ts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { hmacSign, hmacSignThreadpool, hmacVerify, hmacVerifyThreadpool } from "../../index";
2+
3+
export class HmacWrapper {
4+
public hmacSignBytes(key: Array<number>, message: Array<number>): Array<number> {
5+
if (key?.length === 0) {
6+
throw new Error("Must provide an allocated key");
7+
}
8+
if (message?.length === 0) {
9+
throw new Error("Must provide an allocated message");
10+
}
11+
return hmacSign(key, message);
12+
}
13+
14+
public hmacVerifyBytes(key: Array<number>, message: Array<number>, signature: Array<number>): boolean {
15+
if (key?.length === 0) {
16+
throw new Error("Must provide an allocated key");
17+
}
18+
if (message?.length === 0) {
19+
throw new Error("Must provide an allocated message");
20+
}
21+
if(signature?.length===0) {
22+
throw new Error("Must provide an allocated signature");
23+
}
24+
return hmacVerify(key, message, signature);
25+
}
26+
27+
public hmacSignBytesThreadpool(key: Array<number>, message: Array<number>): Array<number> {
28+
if (key?.length === 0) {
29+
throw new Error("Must provide an allocated key");
30+
}
31+
if (message?.length === 0) {
32+
throw new Error("Must provide an allocated message");
33+
}
34+
return hmacSignThreadpool(key, message);
35+
}
36+
37+
public hmacVerifyBytesThreadpool(key: Array<number>, message: Array<number>, signature: Array<number>): boolean {
38+
if (key?.length === 0) {
39+
throw new Error("Must provide an allocated key");
40+
}
41+
if (message?.length === 0) {
42+
throw new Error("Must provide an allocated message");
43+
}
44+
if(signature?.length===0) {
45+
throw new Error("Must provide an allocated signature");
46+
}
47+
return hmacVerifyThreadpool(key, message, signature);
48+
}
49+
}

src-ts/message/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { HmacWrapper } from "./hmac";
2+
3+
export { HmacWrapper };

src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ mod digital_signature {
3232

3333
mod sponges {
3434
pub mod ascon_aead;
35+
}
36+
37+
mod message {
38+
pub mod hmac;
3539
}

src/message/hmac.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use cas_lib::message::{cas_hmac::CASHMAC, hmac::HMAC};
2+
use napi_derive::napi;
3+
4+
#[napi]
5+
pub fn hmac_sign(key: Vec<u8>, message: Vec<u8>) -> Vec<u8> {
6+
return <HMAC as CASHMAC>::sign(key, message);
7+
}
8+
9+
#[napi]
10+
pub fn hmac_sign_threadpool(key: Vec<u8>, message: Vec<u8>) -> Vec<u8> {
11+
return <HMAC as CASHMAC>::sign_threadpool(key, message);
12+
}
13+
14+
#[napi]
15+
pub fn hmac_verify(key: Vec<u8>, message: Vec<u8>, signature: Vec<u8>) -> bool {
16+
return <HMAC as CASHMAC>::verify(key, message, signature);
17+
}
18+
19+
#[napi]
20+
pub fn hmac_verify_threadpool(key: Vec<u8>, message: Vec<u8>, signature: Vec<u8>) -> bool {
21+
return <HMAC as CASHMAC>::verify_threadpool(key, message, signature);
22+
}
23+
24+
#[test]
25+
fn hmac_sign_and_verify_test() {
26+
let mut key = b"ThisIsMyKeyForHmac".to_vec();
27+
let mut message = b"ThisIsMyMessageToSign".to_vec();
28+
let signature = hmac_sign(key.clone(), message.clone());
29+
let result = hmac_verify(key, message, signature);
30+
assert_eq!(true, result);
31+
}
32+
33+
#[test]
34+
fn hmac_sign_and_verify_threadpool_test() {
35+
let mut key = b"ThisIsMyKeyForHmac7789".to_vec();
36+
let mut message = b"ThisIsMyMessageToSign1230".to_vec();
37+
let signature = hmac_sign_threadpool(key.clone(), message.clone());
38+
let result = hmac_verify_threadpool(key, message, signature);
39+
assert_eq!(true, result);
40+
}

test-ts/hmac.test.spec.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { assert } from "chai";
2+
import { HmacWrapper } from "../src-ts/message/index";
3+
4+
describe("HMAC Tests", () => {
5+
it("Sign and Verify", () => {
6+
const wrapper = new HmacWrapper();
7+
const key: string = "This is my array to hash";
8+
const encoder = new TextEncoder();
9+
const keyBytes: Array<number> = Array.from(encoder.encode(key));
10+
const message: string = "This is my message";
11+
const messageBytes = Array.from(encoder.encode(message));
12+
const signature = wrapper.hmacSignBytes(keyBytes, messageBytes);
13+
const result = wrapper.hmacVerifyBytes(keyBytes, messageBytes, signature);
14+
assert.equal(true, result);
15+
});
16+
17+
it("Sign and Verify Threadpool", () => {
18+
const wrapper = new HmacWrapper();
19+
const key: string = "This is my array to hash";
20+
const encoder = new TextEncoder();
21+
const keyBytes: Array<number> = Array.from(encoder.encode(key));
22+
const message: string = "This is my message";
23+
const messageBytes = Array.from(encoder.encode(message));
24+
const signature = wrapper.hmacSignBytesThreadpool(keyBytes, messageBytes);
25+
const result = wrapper.hmacVerifyBytesThreadpool(
26+
keyBytes,
27+
messageBytes,
28+
signature,
29+
);
30+
assert.equal(true, result);
31+
});
32+
});

0 commit comments

Comments
 (0)