Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency Injection #34

Merged
merged 2 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"typescript": "^3.9.5"
},
"dependencies": {
"@privacyresearch/curve25519-typescript": "^0.0.6",
"@privacyresearch/curve25519-typescript": "^0.0.7",
"@privacyresearch/libsignal-protocol-protobuf-ts": "^0.0.5",
"@types/bytebuffer": "^5.0.41",
"@types/long": "^4.0.1",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export * from './curve'

import * as Internal from './internal'

export { setWebCrypto, setCurve } from './internal'

// returns a promise of something with the shape of the old libsignal
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export default async () => {
Expand Down
36 changes: 27 additions & 9 deletions src/internal/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,45 @@ import * as Internal from '.'
import * as util from '../helpers'
import { KeyPairType } from '../types'
import msrcrypto from 'msrcrypto'
import { AsyncCurve as AsyncCurveType } from '@privacyresearch/curve25519-typescript'

const webcrypto = window?.crypto || msrcrypto

export class Crypto {
private _curve: Internal.AsyncCurve
private _webcrypto: globalThis.Crypto

constructor() {
constructor(crypto?: globalThis.Crypto) {
this._curve = new Internal.AsyncCurve()
this._webcrypto = crypto || webcrypto
}

set webcrypto(wc: globalThis.Crypto) {
this._webcrypto = wc
}
set curve(c: AsyncCurveType) {
this._curve.curve = c
}

getRandomBytes(n: number): ArrayBuffer {
const array = new Uint8Array(n)
webcrypto.getRandomValues(array)
this._webcrypto.getRandomValues(array)
return util.uint8ArrayToArrayBuffer(array)
}

async encrypt(key: ArrayBuffer, data: ArrayBuffer, iv: ArrayBuffer): Promise<ArrayBuffer> {
const impkey = await webcrypto.subtle.importKey('raw', key, { name: 'AES-CBC' }, false, ['encrypt'])
const impkey = await this._webcrypto.subtle.importKey('raw', key, { name: 'AES-CBC' }, false, ['encrypt'])

return webcrypto.subtle.encrypt({ name: 'AES-CBC', iv: new Uint8Array(iv) }, impkey, data)
return this._webcrypto.subtle.encrypt({ name: 'AES-CBC', iv: new Uint8Array(iv) }, impkey, data)
}

async decrypt(key: ArrayBuffer, data: ArrayBuffer, iv: ArrayBuffer): Promise<ArrayBuffer> {
const impkey = await webcrypto.subtle.importKey('raw', key, { name: 'AES-CBC' }, false, ['decrypt'])
const impkey = await this._webcrypto.subtle.importKey('raw', key, { name: 'AES-CBC' }, false, ['decrypt'])

return webcrypto.subtle.decrypt({ name: 'AES-CBC', iv: new Uint8Array(iv) }, impkey, data)
return this._webcrypto.subtle.decrypt({ name: 'AES-CBC', iv: new Uint8Array(iv) }, impkey, data)
}
async sign(key: ArrayBuffer, data: ArrayBuffer): Promise<ArrayBuffer> {
const impkey = await webcrypto.subtle.importKey(
const impkey = await this._webcrypto.subtle.importKey(
'raw',
key,
{ name: 'HMAC', hash: { name: 'SHA-256' } },
Expand All @@ -39,14 +49,14 @@ export class Crypto {
)

try {
return webcrypto.subtle.sign({ name: 'HMAC', hash: 'SHA-256' }, impkey, data)
return this._webcrypto.subtle.sign({ name: 'HMAC', hash: 'SHA-256' }, impkey, data)
} catch (e) {
console.log({ e, data, impkey })
throw e
}
}
async hash(data: ArrayBuffer): Promise<ArrayBuffer> {
return webcrypto.subtle.digest({ name: 'SHA-512' }, data)
return this._webcrypto.subtle.digest({ name: 'SHA-512' }, data)
}

async HKDF(input: ArrayBuffer, salt: ArrayBuffer, info: ArrayBuffer): Promise<ArrayBuffer[]> {
Expand Down Expand Up @@ -94,6 +104,14 @@ export class Crypto {

export const crypto = new Crypto()

export function setWebCrypto(webcrypto: globalThis.Crypto): void {
crypto.webcrypto = webcrypto
}

export function setCurve(curve: AsyncCurveType): void {
crypto.curve = curve
}

// HKDF for TextSecure has a bit of additional handling - salts always end up being 32 bytes
export function HKDF(input: ArrayBuffer, salt: ArrayBuffer, info: unknown): Promise<ArrayBuffer[]> {
if (salt.byteLength != 32) {
Expand Down
19 changes: 16 additions & 3 deletions src/internal/curve.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { KeyPairType } from '../types'
import { Curve25519Wrapper, AsyncCurve25519Wrapper } from '@privacyresearch/curve25519-typescript'
import {
Curve25519Wrapper,
AsyncCurve25519Wrapper,
AsyncCurve as AsyncCurveType,
Curve as CurveType,
} from '@privacyresearch/curve25519-typescript'
import { uint8ArrayToArrayBuffer } from '../helpers'

export class Curve {
// Curve 25519 crypto
private _curve25519: Curve25519Wrapper
private _curve25519: CurveType
async: AsyncCurve
constructor(curve25519: Curve25519Wrapper) {
this._curve25519 = curve25519
this.async = new AsyncCurve()
}

set curve(c: CurveType) {
this._curve25519 = c
}

createKeyPair(privKey: ArrayBuffer): KeyPairType {
validatePrivKey(privKey)
const raw_keys = this._curve25519.keyPair(privKey)
Expand Down Expand Up @@ -58,11 +67,15 @@ export class Curve {
}

export class AsyncCurve {
private _curve25519: AsyncCurve25519Wrapper
private _curve25519: AsyncCurveType
constructor() {
this._curve25519 = new AsyncCurve25519Wrapper()
}

set curve(c: AsyncCurveType) {
this._curve25519 = c
}

async createKeyPair(privKey: ArrayBuffer): Promise<KeyPairType> {
validatePrivKey(privKey)
const raw_keys = await this._curve25519.keyPair(privKey)
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -463,10 +463,10 @@
"@types/yargs" "^15.0.0"
chalk "^4.0.0"

"@privacyresearch/curve25519-typescript@^0.0.6":
version "0.0.6"
resolved "https://registry.yarnpkg.com/@privacyresearch/curve25519-typescript/-/curve25519-typescript-0.0.6.tgz#e6f0e80eb54a7e6bb65e1e7ff512769b1a58536b"
integrity sha512-9ff+xUCkAKzqM7Mev4On8MLqB33wHzStD483C7fCtmktwhh3Taks2er7cuxpmXzlMbUa1BafgDRC4t52wrne+A==
"@privacyresearch/curve25519-typescript@^0.0.7":
version "0.0.7"
resolved "https://registry.yarnpkg.com/@privacyresearch/curve25519-typescript/-/curve25519-typescript-0.0.7.tgz#a028dea902af531997cfe331c58cccdefda977b3"
integrity sha512-9MyIq/pLsKYESHsY6yNE3g0o6AAaetzh+XOAumdU/CU0QNpDfG+0ciM5A7pEawWyas30/iUxKv0gLBnYVy2DWw==

"@privacyresearch/libsignal-protocol-protobuf-ts@^0.0.5":
version "0.0.5"
Expand Down