diff --git a/src/private-wrap.ts b/src/private-wrap.ts index a310acd..662c09e 100644 --- a/src/private-wrap.ts +++ b/src/private-wrap.ts @@ -17,17 +17,17 @@ export default class PrivateWrap { return crypto.subtle.generateKey(consts.ECDH, extractable, ['deriveKey']) as Promise } - public static async encrypt(data: CryptoKey, pubkey: CryptoKey): Promise { - log.trace('encrypt') + public static async wrap(data: CryptoKey, pubkey: CryptoKey): Promise { + log.trace('wrap') const tmp_keypair = await PrivateWrap.gen() const kd = await ecdh(tmp_keypair.privateKey, pubkey, DHusage.wrap) - const box = await SecretWrap.encrypt(data, kd) + const box = await SecretWrap.wrap(data, kd) return new this(box, tmp_keypair.publicKey) } - public async decrypt(privkey: CryptoKey): Promise { - log.trace('decrypt') + public async unwrap(privkey: CryptoKey): Promise { + log.trace('unwrap') const kd = await ecdh(privkey, this.pubkey, DHusage.wrap) - return this.box.decrypt(kd) + return this.box.unwrap(kd) } public async toString(): Promise { diff --git a/src/pwd-wrap.ts b/src/pwd-wrap.ts index e059c1f..5b6e94b 100644 --- a/src/pwd-wrap.ts +++ b/src/pwd-wrap.ts @@ -16,17 +16,17 @@ export default class PwdWrap { return (await hkdf(k, Usage.wrap)) as CryptoKey } - public static async encrypt(data: CryptoKey, pwd: string, salt?: Uint8Array): Promise { - log.trace('encrypt') + public static async wrap(data: CryptoKey, pwd: string, salt?: Uint8Array): Promise { + log.trace('wrap') salt = salt ?? crypto.getRandomValues(new Uint8Array(16)) const k = await PwdWrap.derive(pwd, salt) - const box = await SecretWrap.encrypt(data, k) + const box = await SecretWrap.wrap(data, k) return new PwdWrap(box, salt) } - public async decrypt(pwd: string): Promise { - log.trace('decrypt') + public async unwrap(pwd: string): Promise { + log.trace('unwrap') const k = await PwdWrap.derive(pwd, this.salt) - return this.box.decrypt(k) + return this.box.unwrap(k) } public toString(): string { diff --git a/src/secret-wrap.ts b/src/secret-wrap.ts index 9656675..7e5e2da 100644 --- a/src/secret-wrap.ts +++ b/src/secret-wrap.ts @@ -36,15 +36,15 @@ export default class SecretWrap { } } - public static async encrypt(data: CryptoKey, key: CryptoKey): Promise { - log.trace('encrypt') + public static async wrap(data: CryptoKey, key: CryptoKey): Promise { + log.trace('wrap') const format = SecretWrap.format(data.type) const iv = crypto.getRandomValues(new Uint8Array(12)) const box = await crypto.subtle.wrapKey(format, data, key, {name: consts.ENCRYPTION, iv}) return new SecretWrap(new Uint8Array(box), data.algorithm, data.usages, format, iv) } - public async decrypt(key: CryptoKey): Promise { - log.trace('decrypt') + public async unwrap(key: CryptoKey): Promise { + log.trace('unwrap') try { return await crypto.subtle.unwrapKey( this.type, diff --git a/test/misc.test.ts b/test/misc.test.ts index 790be14..005b910 100644 --- a/test/misc.test.ts +++ b/test/misc.test.ts @@ -24,8 +24,8 @@ test('Private wrap', async () => { const message = crypto.getRandomValues(new Uint8Array(8)) const k_wrapped = await SecretBox.gen(true) const boxed_message = await SecretBox.encrypt(message, k_wrapped) - const box = await PrivateWrap.encrypt(k_wrapped, de!) - const unbox = await box.decrypt(k.privateKey) + const box = await PrivateWrap.wrap(k_wrapped, de!) + const unbox = await box.unwrap(k.privateKey) expect(unbox).not.toBeNull() const unboxed_message = await boxed_message.decrypt(unbox!) diff --git a/test/private-wrap.test.ts b/test/private-wrap.test.ts index 0c363af..67738b7 100644 --- a/test/private-wrap.test.ts +++ b/test/private-wrap.test.ts @@ -13,13 +13,13 @@ beforeAll(async () => { }) test('base case', async () => { - const box = await PrivateWrap.encrypt(message, k1.publicKey) - const unboxed = await box.decrypt(k1.privateKey) + const box = await PrivateWrap.wrap(message, k1.publicKey) + const unboxed = await box.unwrap(k1.privateKey) expect(unboxed).toEqual(message) }) test("Different key can't decrypt", async () => { - const box = await PrivateWrap.encrypt(message, k1.publicKey) - const unboxed = await box.decrypt(k2.privateKey) + const box = await PrivateWrap.wrap(message, k1.publicKey) + const unboxed = await box.unwrap(k2.privateKey) expect(unboxed).toBeNull() }) @@ -35,7 +35,7 @@ test('Key generation', async () => { }) test('serialization', async () => { - const box = await PrivateWrap.encrypt(message, k1.publicKey) + const box = await PrivateWrap.wrap(message, k1.publicKey) const ser = await box.toString() const de = await PrivateWrap.fromString(ser) diff --git a/test/pwd-wrap.test.ts b/test/pwd-wrap.test.ts index cb851a8..9f2429b 100644 --- a/test/pwd-wrap.test.ts +++ b/test/pwd-wrap.test.ts @@ -12,18 +12,18 @@ beforeAll(async () => { }) test('base case', async () => { - const box = await PwdWrap.encrypt(message, k1) - const unboxed = await box.decrypt(k1) + const box = await PwdWrap.wrap(message, k1) + const unboxed = await box.unwrap(k1) expect(unboxed).toEqual(message) }) test("Different key can't decrypt", async () => { - const box = await PwdWrap.encrypt(message, k1) - const unboxed = await box.decrypt(k2) + const box = await PwdWrap.wrap(message, k1) + const unboxed = await box.unwrap(k2) expect(unboxed).toBeNull() }) test('serialization', async () => { - const box = await PwdWrap.encrypt(message, k1) + const box = await PwdWrap.wrap(message, k1) const ser = box.toString() const de = PwdWrap.fromString(ser) diff --git a/test/secret-wrap.test.ts b/test/secret-wrap.test.ts index 1ce8283..4e746a2 100644 --- a/test/secret-wrap.test.ts +++ b/test/secret-wrap.test.ts @@ -12,13 +12,13 @@ beforeAll(async () => { }) test('base case', async () => { - const box = await SecretWrap.encrypt(message, k1) - const unboxed = await box.decrypt(k1) + const box = await SecretWrap.wrap(message, k1) + const unboxed = await box.unwrap(k1) expect(unboxed).toEqual(message) }) test("Different key can't decrypt", async () => { - const box = await SecretWrap.encrypt(message, k1) - const unboxed = await box.decrypt(k2) + const box = await SecretWrap.wrap(message, k1) + const unboxed = await box.unwrap(k2) expect(unboxed).toBeNull() }) @@ -28,7 +28,7 @@ test('Key generation', async () => { }) test('serialization', async () => { - const box = await SecretWrap.encrypt(message, k1) + const box = await SecretWrap.wrap(message, k1) const ser = box.toString() const de = SecretWrap.fromString(ser) diff --git a/test/wrap.test.ts b/test/wrap.test.ts index 9142c52..7ff6027 100644 --- a/test/wrap.test.ts +++ b/test/wrap.test.ts @@ -8,14 +8,14 @@ beforeAll(async () => { }) async function seal(key: CryptoKey): Promise { - const box = await SecretWrap.encrypt(key, wrap_key) - const unboxed = await box.decrypt(wrap_key) + const box = await SecretWrap.wrap(key, wrap_key) + const unboxed = await box.unwrap(wrap_key) expect(unboxed).not.toBeNull() return unboxed! } async function cant_seal(key: CryptoKey): Promise { expect(key.extractable).toBeFalse() - expect(SecretWrap.encrypt(key, wrap_key)).rejects.toThrow() + expect(SecretWrap.wrap(key, wrap_key)).rejects.toThrow() } test('Signature', async () => { @@ -29,10 +29,10 @@ test('Signature', async () => { }) test('SecretWrap', async () => { let k = await SecretWrap.gen(true) - const box = await SecretWrap.encrypt(k, k) // Please kids, never do this for real ! + const box = await SecretWrap.wrap(k, k) // Please kids, never do this for real ! k = await seal(k) - const unbox = await box.decrypt(k) + const unbox = await box.unwrap(k) expect(unbox).not.toBeNull() expect(unbox).toEqual(k) }) @@ -48,10 +48,10 @@ test('SecretBox', async () => { }) test('PrivateWrap', async () => { const k = await PrivateWrap.gen(true) - const box = await PrivateWrap.encrypt(wrap_key, k.publicKey) + const box = await PrivateWrap.wrap(wrap_key, k.publicKey) const privk = await seal(k.privateKey) - const unbox = await box.decrypt(privk) + const unbox = await box.unwrap(privk) expect(unbox).not.toBeNull() expect(unbox).toEqual(wrap_key) }) @@ -67,8 +67,8 @@ test('PrivateBox', async () => { }) test("Can't unwrap with a different key", async () => { const wrap_k2 = await SecretWrap.gen(false) - const box = await SecretWrap.encrypt(wrap_key, wrap_key) // Never do this kids ! - const unbox = await box.decrypt(wrap_k2) + const box = await SecretWrap.wrap(wrap_key, wrap_key) // Never do this kids ! + const unbox = await box.unwrap(wrap_k2) expect(unbox).toBeNull() }) test('Cant wrap non-extractable', async () => {