Skip to content

Commit

Permalink
use connectPlatform instead
Browse files Browse the repository at this point in the history
  • Loading branch information
brianwphamSF committed Dec 6, 2023
1 parent cb8eea2 commit e16c436
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
9 changes: 9 additions & 0 deletions keeperapi/src/__tests__/crypto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand All @@ -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)
})
})
Expand Down
8 changes: 4 additions & 4 deletions keeperapi/src/browser/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8Array> {
Expand Down Expand Up @@ -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',
Expand Down
8 changes: 4 additions & 4 deletions keeperapi/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e16c436

Please sign in to comment.