import {beforeAll, expect, test} from 'bun:test' import {SecretWrap, type Key} from 'boxes/secret-wrap' import * as sym from 'boxes/symmetric' import * as asym from 'boxes/asymmetric' import * as pwrap from 'boxes/private-wrap' let k1!: Key; let k2!: Key; let kw_wrap!: sym.Key; let kw_sym!: sym.Key; let kw_asym!: asym.KeyPair; let kw_priv!: pwrap.KeyPair; let kw_wrap_non!: sym.Key; let kw_sym_non!: sym.Key; let kw_asym_non!: asym.KeyPair; let kw_priv_non!: pwrap.KeyPair; beforeAll(async () => { k1 = await SecretWrap.gen_key(false) k2 = await SecretWrap.gen_key(true) expect(k1.extractable).toBe(false) expect(k2.extractable).toBe(true) kw_wrap = await sym.SecretBox.gen_key(true) expect(kw_wrap.extractable).toBe(true) kw_asym = await asym.PrivateBox.gen_keypair(true) expect(kw_asym[0].extractable).toBe(true) kw_priv = await pwrap.PrivateWrap.gen_key(true) expect(kw_priv[0].extractable).toBe(true) kw_sym = await sym.SecretBox.gen_key(true) expect(kw_sym.extractable).toBe(true) kw_wrap_non = await sym.SecretBox.gen_key(false) expect(kw_wrap_non.extractable).toBe(false) kw_asym_non = await asym.PrivateBox.gen_keypair(false) expect(kw_asym_non[0].extractable).toBe(false) kw_priv_non = await pwrap.PrivateWrap.gen_key(false) expect(kw_priv_non[0].extractable).toBe(false) kw_sym_non = await sym.SecretBox.gen_key(false) expect(kw_sym_non.extractable).toBe(false) }) test('base case', async () => { const testit = async (key: CryptoKey) => { console.log(`Testing ${key.type} key with usage ${key.usages}`) const wrapped = (await SecretWrap.wrap_key(k1, key)).expect("Should wrap the key") const unwrapped = (await wrapped.unwrap(k1)).expect("Should unwrap the key") expect(unwrapped).toEqual(key) } await testit(kw_wrap) await testit(kw_asym[0]) await testit(kw_priv[0]) await testit(kw_sym) }) test("toString and fromString and inverses", async () => { const testit = async (key: CryptoKey) => { console.log(`Testing ${key.type} key with usage ${key.usages}`) const wrapped = (await SecretWrap.wrap_key(k1, key)).expect("Should wrap the key") const wrapped_str = wrapped.toString() const unwrapped = (SecretWrap.fromString(wrapped_str)).expect("Should parse the key") expect(unwrapped).toEqual(wrapped) } await testit(kw_wrap) await testit(kw_asym[0]) await testit(kw_priv[0]) await testit(kw_sym) }) test("Can't unwrap with wrong key", async () => { const wrapped = (await SecretWrap.wrap_key(k1, kw_wrap)).expect("Should wrap the key") ;(await wrapped.unwrap(k2)).expect_err("Shouldn't unwrap with wrong key") }) test("Can't wrap if key is not extractable", async () => { ;(await SecretWrap.wrap_key(k1, kw_wrap_non)).expect_err("Shouldn't wrap if key is not extractable") ;(await SecretWrap.wrap_key(k1, kw_asym_non[0])).expect_err("Shouldn't wrap if key is not extractable") ;(await SecretWrap.wrap_key(k1, kw_priv_non[0])).expect_err("Shouldn't wrap if key is not extractable") ;(await SecretWrap.wrap_key(k1, kw_sym_non)).expect_err("Shouldn't wrap if key is not extractable") }) test("tampered IV", async () => { const wrapped = (await SecretWrap.wrap_key(k1, kw_wrap)).expect("Should wrap the key") // @ts-expect-error wrapped.iv[0] += 1 ;(await wrapped.unwrap(k1)).expect_err("Shouldn't unwrap with tampered IV") })