keccak unit tests

This commit is contained in:
Sam Hadow 2025-02-18 15:01:30 +01:00
parent a761bdb209
commit 7732504136
5 changed files with 124 additions and 82 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
# Swap Files # # Swap Files #
.*.kate-swp .*.kate-swp
.swp.* .swp.*
# node.js
node_modules/ node_modules/
package-lock.json package-lock.json

View File

@ -146,27 +146,26 @@ function Keccak(rate, capacity, inputBytes, delimitedSuffix, outputByteLen) {
return new Uint8Array(output); return new Uint8Array(output);
} }
// SHAKE and SHA3 functions const keccak = {
function SHAKE128(inputBytes, outputByteLen) { // SHAKE and SHA3 functions
return Keccak(1344, 256, inputBytes, 0x1f, outputByteLen); SHAKE128: (inputBytes, outputByteLen) => {
return Keccak(1344, 256, inputBytes, 0x1f, outputByteLen);
},
SHAKE256: (inputBytes, outputByteLen) => {
return Keccak(1088, 512, inputBytes, 0x1f, outputByteLen);
},
SHA3_224: (inputBytes) => {
return Keccak(1152, 448, inputBytes, 0x06, 224 / 8);
},
SHA3_256: (inputBytes) => {
return Keccak(1088, 512, inputBytes, 0x06, 256 / 8);
},
SHA3_384: (inputBytes) => {
return Keccak(832, 768, inputBytes, 0x06, 384 / 8);
},
SHA3_512: (inputBytes) => {
return Keccak(576, 1024, inputBytes, 0x06, 512 / 8);
}
} }
function SHAKE256(inputBytes, outputByteLen) { module.exports = keccak;
return Keccak(1088, 512, inputBytes, 0x1f, outputByteLen);
}
function SHA3_224(inputBytes) {
return Keccak(1152, 448, inputBytes, 0x06, 224 / 8);
}
function SHA3_256(inputBytes) {
return Keccak(1088, 512, inputBytes, 0x06, 256 / 8);
}
function SHA3_384(inputBytes) {
return Keccak(832, 768, inputBytes, 0x06, 384 / 8);
}
function SHA3_512(inputBytes) {
return Keccak(576, 1024, inputBytes, 0x06, 512 / 8);
}

9
src/stringutils.js Normal file
View File

@ -0,0 +1,9 @@
const stringutils = {
hexToArray: (hexString) => {
return Uint8Array.from(Buffer.from(hexString, 'hex'));
},
arrayToHex: (hex) => {
return Buffer.from(hex).toString('hex');
}
}
module.exports = stringutils;

View File

@ -2,72 +2,70 @@ const { subtle } = require('node:crypto').webcrypto;
const authentication = require('../src/authentication'); const authentication = require('../src/authentication');
describe('authentication module', () => { describe('authentication module', () => {
describe('verifySignature', () => { it('should return the public key if the signature is verified successfully', async () => {
it('should return the public key if the signature is verified successfully', async () => { const { publicKey, privateKey } = await crypto.subtle.generateKey(
const { publicKey, privateKey } = await crypto.subtle.generateKey( {
{ name: "Ed25519",
name: "Ed25519", },
}, true,
true, ["sign", "verify"],
["sign", "verify"], );
); const exportedPubkey = await crypto.subtle.exportKey("spki", publicKey);
const exportedPubkey = await crypto.subtle.exportKey("spki", publicKey); let exportedAsString = String.fromCharCode.apply(null, new Uint8Array(exportedPubkey));
let exportedAsString = String.fromCharCode.apply(null, new Uint8Array(exportedPubkey)); let exportedAsBase64 = Buffer.from(exportedAsString, 'binary').toString('base64');
let exportedAsBase64 = Buffer.from(exportedAsString, 'binary').toString('base64'); let pemKey = `-----BEGIN PUBLIC KEY-----\n${exportedAsBase64}\n-----END PUBLIC KEY-----`;
let pemKey = `-----BEGIN PUBLIC KEY-----\n${exportedAsBase64}\n-----END PUBLIC KEY-----`; const publicKeys = [pemKey,];
const publicKeys = [pemKey,]; const msg = 'test message';
const msg = 'test message'; const encoder = new TextEncoder();
const encoder = new TextEncoder(); const encodedData = encoder.encode(msg);
const encodedData = encoder.encode(msg); const signature = await crypto.subtle.sign(
const signature = await crypto.subtle.sign( {
{ name: "Ed25519",
name: "Ed25519", },
}, privateKey,
privateKey, encodedData,
encodedData, );
); const result = await authentication.verifySignature(encodedData, signature, publicKeys);
const result = await authentication.verifySignature(encodedData, signature, publicKeys); expect(result).toBe(pemKey);
expect(result).toBe(pemKey);
let fakeKey = `-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAmrBLT6lyiFh/eUticsIFNY6AkjXuQPqj0Qvb99pCJJk=\n-----END PUBLIC KEY-----` let fakeKey = `-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAmrBLT6lyiFh/eUticsIFNY6AkjXuQPqj0Qvb99pCJJk=\n-----END PUBLIC KEY-----`
const result2 = await authentication.verifySignature(encodedData, signature, [fakeKey,]); const result2 = await authentication.verifySignature(encodedData, signature, [fakeKey,]);
expect(result2).toBe(null); expect(result2).toBe(null);
}); });
it('encoding and decoding test', async () => { it('encoding and decoding test', async () => {
const testkey = '-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEILXfcMirlUiI/o/r+8rK7ZIP0fyAMynxYzJXCI6BJHPk\n-----END PRIVATE KEY-----'; const testkey = '-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEILXfcMirlUiI/o/r+8rK7ZIP0fyAMynxYzJXCI6BJHPk\n-----END PRIVATE KEY-----';
const base64 = testkey.replace(`-----BEGIN PRIVATE KEY-----`, '').replace(`-----END PRIVATE KEY-----`, '').trim(); const base64 = testkey.replace(`-----BEGIN PRIVATE KEY-----`, '').replace(`-----END PRIVATE KEY-----`, '').trim();
const derBuffer = Buffer.from(base64, 'base64'); const derBuffer = Buffer.from(base64, 'base64');
let privkey = await crypto.subtle.importKey( let privkey = await crypto.subtle.importKey(
"pkcs8", "pkcs8",
derBuffer, derBuffer,
{ {
name: "Ed25519", name: "Ed25519",
}, },
true, true,
["sign"], ["sign"],
); );
const msg2 = '12f4b99e3784ac2e8b95427533a3dbc4'; const msg2 = '12f4b99e3784ac2e8b95427533a3dbc4';
const encoder2 = new TextEncoder(); const encoder2 = new TextEncoder();
const encodedData2 = encoder2.encode(msg2); const encodedData2 = encoder2.encode(msg2);
const signature2 = await crypto.subtle.sign( const signature2 = await crypto.subtle.sign(
{ {
name: "Ed25519", name: "Ed25519",
}, },
privkey, privkey,
encodedData2, encodedData2,
); );
const signatureBase64 = Buffer.from(signature2).toString('base64'); const signatureBase64 = Buffer.from(signature2).toString('base64');
const sig = Buffer.from(signatureBase64, 'base64'); const sig = Buffer.from(signatureBase64, 'base64');
let pemKey2 = '-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEALrj4A3Vftz5TgWXEHi5KG+HD+uQLGB3bGc4TprDi9kE=\n-----END PUBLIC KEY-----' let pemKey2 = '-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEALrj4A3Vftz5TgWXEHi5KG+HD+uQLGB3bGc4TprDi9kE=\n-----END PUBLIC KEY-----'
const publicKeys2 = [pemKey2,]; const publicKeys2 = [pemKey2,];
const result3 = await authentication.verifySignature(encodedData2, sig, publicKeys2); const result3 = await authentication.verifySignature(encodedData2, sig, publicKeys2);
expect(result3).toBe(pemKey2); expect(result3).toBe(pemKey2);
});
}); });
}); });

35
tests/keccak.test.js Normal file
View File

@ -0,0 +1,35 @@
const keccak = require('../src/keccak');
const stringutils = require('../src/stringutils');
describe('keccak.js functions', () => {
// NIST SHA3 test vectors: https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/sha3/sha-3bytetestvectors.zip
it('SHA3_224', () => {
let msg = '31c82d71785b7ca6b651cb6c8c9ad5e2aceb0b0633c088d33aa247ada7a594ff4936c023251319820a9b19fc6c48de8a6f7ada214176ccdaadaeef51ed43714ac0c8269bbd497e46e78bb5e58196494b2471b1680e2d4c6dbd249831bd83a4d3be06c8a2e903933974aa05ee748bfe6ef359f7a143edf0d4918da916bd6f15e26a790cff514b40a5da7f72e1ed2fe63a05b8149587bea05653718cc8980eadbfeca85b7c9c286dd040936585938be7f98219700c83a9443c2856a80ff46852b26d1b1edf72a30203cf6c44a10fa6eaf1920173cedfb5c4cf3ac665b37a86ed02155bbbf17dc2e786af9478fe0889d86c5bfa85a242eb0854b1482b7bd16f67f80bef9c7a628f05a107936a64273a97b0088b0e515451f916b5656230a12ba6dc78';
let hash = 'aab23c9e7fb9d7dacefdfd0b1ae85ab1374abff7c4e3f7556ecae412';
let hash_test = stringutils.arrayToHex(keccak.SHA3_224(stringutils.hexToArray(msg)));
expect(hash_test).toBe(hash);
});
it('SHA3_256', () => {
let msg = 'b1caa396771a09a1db9bc20543e988e359d47c2a616417bbca1b62cb02796a888fc6eeff5c0b5c3d5062fcb4256f6ae1782f492c1cf03610b4a1fb7b814c057878e1190b9835425c7a4a0e182ad1f91535ed2a35033a5d8c670e21c575ff43c194a58a82d4a1a44881dd61f9f8161fc6b998860cbe4975780be93b6f87980bad0a99aa2cb7556b478ca35d1f3746c33e2bb7c47af426641cc7bbb3425e2144820345e1d0ea5b7da2c3236a52906acdc3b4d34e474dd714c0c40bf006a3a1d889a632983814bbc4a14fe5f159aa89249e7c738b3b73666bac2a615a83fd21ae0a1ce7352ade7b278b587158fd2fabb217aa1fe31d0bda53272045598015a8ae4d8cec226fefa58daa05500906c4d85e7567';
let hash = 'cb5648a1d61c6c5bdacd96f81c9591debc3950dcf658145b8d996570ba881a05';
let hash_test = stringutils.arrayToHex(keccak.SHA3_256(stringutils.hexToArray(msg)));
expect(hash_test).toBe(hash);
});
it('SHA3_384', () => {
let msg = '5fe35923b4e0af7dd24971812a58425519850a506dfa9b0d254795be785786c319a2567cbaa5e35bcf8fe83d943e23fa5169b73adc1fcf8b607084b15e6a013df147e46256e4e803ab75c110f77848136be7d806e8b2f868c16c3a90c14463407038cb7d9285079ef162c6a45cedf9c9f066375c969b5fcbcda37f02aacff4f31cded3767570885426bebd9eca877e44674e9ae2f0c24cdd0e7e1aaf1ff2fe7f80a1c4f5078eb34cd4f06fa94a2d1eab5806ca43fd0f06c60b63d5402b95c70c21ea65a151c5cfaf8262a46be3c722264b';
let hash = '3054d249f916a6039b2a9c3ebec1418791a0608a170e6d36486035e5f92635eaba98072a85373cb54e2ae3f982ce132b'
let hash_test = stringutils.arrayToHex(keccak.SHA3_384(stringutils.hexToArray(msg)));
expect(hash_test).toBe(hash);
});
it('SHA3_512', () => {
let msg = '664ef2e3a7059daf1c58caf52008c5227e85cdcb83b4c59457f02c508d4f4f69f826bd82c0cffc5cb6a97af6e561c6f96970005285e58f21ef6511d26e709889a7e513c434c90a3cf7448f0caeec7114c747b2a0758a3b4503a7cf0c69873ed31d94dbef2b7b2f168830ef7da3322c3d3e10cafb7c2c33c83bbf4c46a31da90cff3bfd4ccc6ed4b310758491eeba603a76';
let hash = 'e5825ff1a3c070d5a52fbbe711854a440554295ffb7a7969a17908d10163bfbe8f1d52a676e8a0137b56a11cdf0ffbb456bc899fc727d14bd8882232549d914e';
let hash_test = stringutils.arrayToHex(keccak.SHA3_512(stringutils.hexToArray(msg)));
expect(hash_test).toBe(hash);
});
});