From cc27f98aaee3f2fc59dec0c596c7c5b9ad53cf5e Mon Sep 17 00:00:00 2001
From: Kyle Spearrin <kyle.spearrin@gmail.com>
Date: Tue, 5 Mar 2019 16:36:11 -0500
Subject: [PATCH] makeKeyFromPin in background context

---
 src/abstractions/crypto.service.ts       |  1 +
 src/angular/components/lock.component.ts |  7 ++-----
 src/services/crypto.service.ts           | 12 ++++++++++++
 3 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/src/abstractions/crypto.service.ts b/src/abstractions/crypto.service.ts
index 889500c40f..3ee45f1bc1 100644
--- a/src/abstractions/crypto.service.ts
+++ b/src/abstractions/crypto.service.ts
@@ -30,6 +30,7 @@ export abstract class CryptoService {
     clearKeys: () => Promise<any>;
     toggleKey: () => Promise<any>;
     makeKey: (password: string, salt: string, kdf: KdfType, kdfIterations: number) => Promise<SymmetricCryptoKey>;
+    makeKeyFromPin: (pin: string, salt: string, kdf: KdfType, kdfIterations: number) => Promise<SymmetricCryptoKey>;
     makeShareKey: () => Promise<[CipherString, SymmetricCryptoKey]>;
     makeKeyPair: (key?: SymmetricCryptoKey) => Promise<[string, CipherString]>;
     makePinKey: (pin: string, salt: string, kdf: KdfType, kdfIterations: number) => Promise<SymmetricCryptoKey>;
diff --git a/src/angular/components/lock.component.ts b/src/angular/components/lock.component.ts
index 419124f038..31493ac872 100644
--- a/src/angular/components/lock.component.ts
+++ b/src/angular/components/lock.component.ts
@@ -66,12 +66,9 @@ export class LockComponent implements OnInit {
                         this.doContinue();
                     }
                 } else {
-                    const pinProtectedKey = await this.storageService.get<string>(ConstantsService.pinProtectedKey);
-                    const protectedKeyCs = new CipherString(pinProtectedKey);
-                    const pinKey = await this.cryptoService.makePinKey(this.pin, this.email, kdf, kdfIterations);
-                    const decKey = await this.cryptoService.decryptToBytes(protectedKeyCs, pinKey);
+                    const key = await this.cryptoService.makeKeyFromPin(this.pin, this.email, kdf, kdfIterations);
                     failed = false;
-                    await this.setKeyAndContinue(new SymmetricCryptoKey(decKey));
+                    await this.setKeyAndContinue(key);
                 }
             } catch {
                 failed = true;
diff --git a/src/services/crypto.service.ts b/src/services/crypto.service.ts
index 865ec3bfc1..b2da1ce532 100644
--- a/src/services/crypto.service.ts
+++ b/src/services/crypto.service.ts
@@ -310,6 +310,18 @@ export class CryptoService implements CryptoServiceAbstraction {
         return new SymmetricCryptoKey(key);
     }
 
+    async makeKeyFromPin(pin: string, salt: string, kdf: KdfType, kdfIterations: number):
+        Promise<SymmetricCryptoKey> {
+        const pinProtectedKey = await this.storageService.get<string>(ConstantsService.pinProtectedKey);
+        if (pinProtectedKey == null) {
+            throw new Error('No PIN protected key found.');
+        }
+        const protectedKeyCs = new CipherString(pinProtectedKey);
+        const pinKey = await this.makePinKey(pin, salt, kdf, kdfIterations);
+        const decKey = await this.decryptToBytes(protectedKeyCs, pinKey);
+        return new SymmetricCryptoKey(decKey);
+    }
+
     async makeShareKey(): Promise<[CipherString, SymmetricCryptoKey]> {
         const shareKey = await this.cryptoFunctionService.randomBytes(64);
         const publicKey = await this.getPublicKey();