-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include hkdf implementation for elliptic curve (#39)
* Include hkdf implementation for elliptic curve * clear commented unneeded code * use connectPlatform instead * connectPlatform some more
- Loading branch information
1 parent
7039f51
commit b57b432
Showing
8 changed files
with
7,742 additions
and
4,300 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
// @ts-ignore | ||
import crypto from 'crypto' | ||
import {nodePlatform} from "../node/platform"; | ||
import {browserPlatform} from "../browser/platform" | ||
import {publicKey, privateKey} from "./ecies-test-vectors"; | ||
import {TextEncoder, TextDecoder} from 'util'; | ||
import type {Platform} from "../platform"; | ||
import {connectPlatform, platform} from "../platform"; | ||
|
||
Object.assign(global, {TextDecoder, TextEncoder}) | ||
|
||
Object.defineProperty(global.self, 'crypto', { | ||
value: { | ||
subtle: crypto.webcrypto.subtle, | ||
getRandomValues: (array: any) => crypto.randomBytes(array.length) | ||
} | ||
}) | ||
|
||
describe('crypto test', () => { | ||
it('node API encrypts a message under EC and then decrypts it (test key pair)', async () => { | ||
connectPlatform(nodePlatform) | ||
await ecEncryptionTest(publicKey, privateKey) | ||
}) | ||
it('node API encrypts a message under EC and then decrypts it (generated key pair)', async () => { | ||
connectPlatform(nodePlatform) | ||
const kp = await platform.generateECKeyPair() | ||
await ecEncryptionTest(kp.publicKey, kp.privateKey) | ||
}) | ||
it('browser API encrypts a message under EC and then decrypts it (test key pair)', async () => { | ||
connectPlatform(browserPlatform) | ||
await ecEncryptionTest(publicKey, privateKey) | ||
}) | ||
it('browser API encrypts a message under EC and then decrypts it (generated key pair)', async () => { | ||
connectPlatform(browserPlatform) | ||
const kp = await platform.generateECKeyPair() | ||
await ecEncryptionTest(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(publicKey, privateKey) | ||
}) | ||
it('node API encrypts a message with HKDF under EC and then decrypts it (generated key pair)', async () => { | ||
connectPlatform(nodePlatform) | ||
const kp = await platform.generateECKeyPair() | ||
await ecWithHkdfEncryptionTest(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(publicKey, privateKey) | ||
}) | ||
it('browser API encrypts a message with HKDF under EC and then decrypts it (generated key pair)', async () => { | ||
connectPlatform(browserPlatform) | ||
const kp = await platform.generateECKeyPair() | ||
await ecWithHkdfEncryptionTest(kp.publicKey, kp.privateKey) | ||
}) | ||
}) | ||
|
||
async function ecEncryptionTest(publicKey: Uint8Array, privateKey: Uint8Array) { | ||
const message = 'test' | ||
const cipher = await platform.publicEncryptEC(platform.stringToBytes(message), publicKey, Buffer.from([])) | ||
expect(cipher).toBeTruthy() | ||
const decryptedBuffer = await platform.privateDecryptEC(cipher, privateKey, publicKey, undefined) | ||
const decryptedMsg = platform.bytesToString(decryptedBuffer) | ||
expect(decryptedMsg).toEqual(message) | ||
} | ||
|
||
async function ecWithHkdfEncryptionTest(publicKey: Uint8Array, privateKey: Uint8Array) { | ||
const message = 'test' | ||
const cipher = await platform.publicEncryptECWithHKDF(message, publicKey, Buffer.from([])) | ||
expect(cipher).toBeTruthy() | ||
const decryptedBuffer = await platform.privateDecryptEC(cipher, privateKey, publicKey, undefined, true) | ||
const decryptedMsg = platform.bytesToString(decryptedBuffer) | ||
expect(decryptedMsg).toEqual(message) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const publicKey = new Uint8Array(Buffer.from('BHkCO8yY00I3_4W1iwWGnLSE3DTaQnoLBrTYD1nSjyKF1QFIZgGKZhgofxWE9Ss4OrZV24Oyl080377FGd_Iv_I', 'base64url')) | ||
|
||
export const privateKey = new Uint8Array(Buffer.from('Rew7EMRzZcnMM3vvhN8tjdk6x2V4tHuzHKTM5z2QYpo', 'base64url')) | ||
|
||
export const id = 'keeper-unit-test' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters