From 1749459be785a6328fcc6602ae8db86ad8244303 Mon Sep 17 00:00:00 2001 From: Pascal Perrenoud Date: Thu, 6 Jun 2024 13:31:44 +0200 Subject: [PATCH] kdf : Add tests --- test/kdf.test.ts | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 test/kdf.test.ts diff --git a/test/kdf.test.ts b/test/kdf.test.ts new file mode 100644 index 0000000..0ab4b23 --- /dev/null +++ b/test/kdf.test.ts @@ -0,0 +1,91 @@ +import {describe, expect, test} from 'bun:test' +import {kdf, PrivateWrap} from '../index' +import {ecdh} from "../src/kdf"; + +describe('Passwords', () => { + test('Base case', async () => { + const p = "yeet" + const s = new Uint8Array(2) + + const k1 = await kdf.pbkdf(s, p) + const k2 = await kdf.pbkdf(s, p) + + expect(k1).toEqual(k2) + }) + test('Different salt, different key', async () => { + const password = "yeet" + const s1 = new Uint8Array(1) + const s2 = new Uint8Array(2) + expect(s1).not.toEqual(s2) + + const k1 = await kdf.pbkdf(s1, password) + const k2 = await kdf.pbkdf(s2, password) + + expect(k2).not.toEqual(k1) + }) + test('Different password, different key', async () => { + const p1 = "yeet" + const p2 = "yaat" + expect(p1).not.toBe(p2) + const s = new Uint8Array(2) + + const k1 = await kdf.pbkdf(s, p1) + const k2 = await kdf.pbkdf(s, p2) + + expect(k1).not.toBe(k2) + }) +}) +describe('HKDF', () => { + test ('Base case', async () => { + const k = new Uint8Array(12) + const usage = kdf.Usage.sign + + const k1 = await kdf.hkdf(k, usage) + const k2 = await kdf.hkdf(k, usage) + + expect(k2).toEqual(k1) + }) + test('Different usage, different key', async () => { + const k = new Uint8Array(12) + const u1 = kdf.Usage.sign + const u2 = kdf.Usage.wrap + + const k1 = await kdf.hkdf(k, u1) + const k2 = await kdf.hkdf(k, u2) + + expect(k1).not.toEqual(k2) + }) + test('Different input, different output', async () => { + const k1 = new Uint8Array(1) + const k2 = new Uint8Array(2) + const u = kdf.Usage.sign + + const kd1 = await kdf.hkdf(k1, u) + const kd2 = await kdf.hkdf(k2, u) + + expect(kd1).not.toEqual(kd2) + }) + test.todo('Usages are set') + test.todo('Not extractable') +}) +describe('ECDH', () => { + test('Base case', async () => { + const k1 = await PrivateWrap.gen_keypair() + const k2 = await PrivateWrap.gen_keypair() + + const kd1 = await ecdh(k1.privateKey, k2.publicKey) + const kd2 = await ecdh(k2.privateKey, k1.publicKey) + + expect(kd1).toEqual(kd2) + }) + test('Different pubkey, different key', async () => { + const k1 = await PrivateWrap.gen_keypair() + const k2 = await PrivateWrap.gen_keypair() + const k3 = await PrivateWrap.gen_keypair() + + const kd1 = await ecdh(k1.privateKey, k2.publicKey) + const kd2 = await ecdh(k3.privateKey, k1.publicKey) + + expect(kd1).not.toBe(kd2) + }) +})