From eda99e4f12c28922d01aa51a6329194e8b839e27 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 13 Jun 2018 17:10:52 -0400 Subject: [PATCH] hasKey helper --- src/abstractions/crypto.service.ts | 1 + src/services/crypto.service.ts | 18 +++++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/abstractions/crypto.service.ts b/src/abstractions/crypto.service.ts index 4220a36b90..fc55466ee7 100644 --- a/src/abstractions/crypto.service.ts +++ b/src/abstractions/crypto.service.ts @@ -15,6 +15,7 @@ export abstract class CryptoService { getPrivateKey: () => Promise; getOrgKeys: () => Promise>; getOrgKey: (orgId: string) => Promise; + hasKey: () => Promise; clearKey: () => Promise; clearKeyHash: () => Promise; clearEncKey: (memoryOnly?: boolean) => Promise; diff --git a/src/services/crypto.service.ts b/src/services/crypto.service.ts index 9b400687dc..697bf76cc4 100644 --- a/src/services/crypto.service.ts +++ b/src/services/crypto.service.ts @@ -192,6 +192,10 @@ export class CryptoService implements CryptoServiceAbstraction { return orgKeys.get(orgId); } + async hasKey(): Promise { + return (await this.getKey()) != null; + } + clearKey(): Promise { this.key = this.legacyEtmKey = null; return this.secureStorageService.remove(Keys.key); @@ -282,13 +286,6 @@ export class CryptoService implements CryptoServiceAbstraction { */ } - async stretchKey(key: SymmetricCryptoKey): Promise { - const newKey = new Uint8Array(64); - newKey.set(await this.hkdfExpand(key.key, Utils.fromUtf8ToArray('enc'), 32)); - newKey.set(await this.hkdfExpand(key.key, Utils.fromUtf8ToArray('mac'), 32), 32); - return new SymmetricCryptoKey(newKey.buffer); - } - async encrypt(plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey): Promise { if (plainValue == null) { return Promise.resolve(null); @@ -594,6 +591,13 @@ export class CryptoService implements CryptoServiceAbstraction { return key; } + private async stretchKey(key: SymmetricCryptoKey): Promise { + const newKey = new Uint8Array(64); + newKey.set(await this.hkdfExpand(key.key, Utils.fromUtf8ToArray('enc'), 32)); + newKey.set(await this.hkdfExpand(key.key, Utils.fromUtf8ToArray('mac'), 32), 32); + return new SymmetricCryptoKey(newKey.buffer); + } + // ref: https://tools.ietf.org/html/rfc5869 private async hkdfExpand(prk: ArrayBuffer, info: Uint8Array, size: number) { const hashLen = 32; // sha256