diff --git a/keeperapi/src/__tests__/crypto.test.ts b/keeperapi/src/__tests__/crypto.test.ts index b3746b1..959009b 100644 --- a/keeperapi/src/__tests__/crypto.test.ts +++ b/keeperapi/src/__tests__/crypto.test.ts @@ -9,6 +9,7 @@ import {browserPlatform} from "../browser/platform" import {publicKey, privateKey} from "./ecies-test-vectors"; import {TextEncoder, TextDecoder} from 'util'; import type {Platform} from "../platform"; +import {connectPlatform} from "../platform"; Object.assign(global, {TextDecoder, TextEncoder}) @@ -21,31 +22,39 @@ Object.defineProperty(global.self, 'crypto', { describe('crypto test', () => { it('node API encrypts a message under EC and then decrypts it (test key pair)', async () => { + connectPlatform(nodePlatform) await ecEncryptionTest(nodePlatform, publicKey, privateKey) }) it('node API encrypts a message under EC and then decrypts it (generated key pair)', async () => { const kp = await nodePlatform.generateECKeyPair() + connectPlatform(nodePlatform) await ecEncryptionTest(nodePlatform, kp.publicKey, kp.privateKey) }) it('browser API encrypts a message under EC and then decrypts it (test key pair)', async () => { + connectPlatform(browserPlatform) await ecEncryptionTest(browserPlatform, publicKey, privateKey) }) it('browser API encrypts a message under EC and then decrypts it (generated key pair)', async () => { const kp = await browserPlatform.generateECKeyPair() + connectPlatform(browserPlatform) await ecEncryptionTest(browserPlatform, kp.publicKey, kp.privateKey) }) it('node API encrypts a message with HKDF under EC and then decrypts it (test key pair)', async () => { + connectPlatform(nodePlatform) await ecWithHkdfEncryptionTest(nodePlatform, publicKey, privateKey) }) it('node API encrypts a message with HKDF under EC and then decrypts it (generated key pair)', async () => { const kp = await nodePlatform.generateECKeyPair() + connectPlatform(nodePlatform) await ecWithHkdfEncryptionTest(nodePlatform, kp.publicKey, kp.privateKey) }) it('browser API encrypts a message with HKDF under EC and then decrypts it (test key pair)', async () => { + connectPlatform(browserPlatform) await ecWithHkdfEncryptionTest(browserPlatform, publicKey, privateKey) }) it('browser API encrypts a message with HKDF under EC and then decrypts it (generated key pair)', async () => { const kp = await browserPlatform.generateECKeyPair() + connectPlatform(browserPlatform) await ecWithHkdfEncryptionTest(browserPlatform, kp.publicKey, kp.privateKey) }) }) diff --git a/keeperapi/src/browser/platform.ts b/keeperapi/src/browser/platform.ts index 7ff1717..64773c7 100644 --- a/keeperapi/src/browser/platform.ts +++ b/keeperapi/src/browser/platform.ts @@ -387,7 +387,7 @@ export const browserPlatform: Platform = class { const ecdh = await crypto.subtle.generateKey({ name: 'ECDH', namedCurve: 'P-256' }, true, ['deriveBits']) const privateKey = await crypto.subtle.exportKey('jwk', ecdh.privateKey) const publicKey = await crypto.subtle.exportKey('raw', ecdh.publicKey) - return { publicKey: new Uint8Array(publicKey), privateKey: normal64Bytes(privateKey.d!, this) } + return { publicKey: new Uint8Array(publicKey), privateKey: normal64Bytes(privateKey.d!) } } static async publicEncryptECWithHKDF(message: string | Uint8Array, pubKey: Uint8Array, id: Uint8Array): Promise { @@ -469,9 +469,9 @@ export const browserPlatform: Platform = class { } static async importPrivateKeyEC(privateKey: Uint8Array, publicKey: Uint8Array) { - const x = webSafe64FromBytes(publicKey.subarray(1, 33), this) - const y = webSafe64FromBytes(publicKey.subarray(33, 65), this) - const d = webSafe64FromBytes(privateKey, this) + const x = webSafe64FromBytes(publicKey.subarray(1, 33)) + const y = webSafe64FromBytes(publicKey.subarray(33, 65)) + const d = webSafe64FromBytes(privateKey) const jwk = { 'crv': 'P-256', diff --git a/keeperapi/src/utils.ts b/keeperapi/src/utils.ts index 8efd291..89abccf 100644 --- a/keeperapi/src/utils.ts +++ b/keeperapi/src/utils.ts @@ -56,16 +56,16 @@ export function webSafe64(source: string): string { return source.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); } -export function webSafe64FromBytes(source: Uint8Array, thisPlatform?: Platform): string { - return webSafe64((thisPlatform ?? platform).bytesToBase64(source)); +export function webSafe64FromBytes(source: Uint8Array): string { + return webSafe64(platform.bytesToBase64(source)); } export function normal64(source: string): string { return source.replace(/-/g, '+').replace(/_/g, '/') + '=='.substring(0, (3 * source.length) % 4); } -export function normal64Bytes(source: string, thisPlatform?: Platform): Uint8Array { - return (thisPlatform ?? platform).base64ToBytes(normal64(source)); +export function normal64Bytes(source: string): Uint8Array { + return platform.base64ToBytes(normal64(source)); } export function isTwoFactorResultCode(resultCode: string): boolean {