From 29556c5d3beb28eb0258e70773c5ffc316db4139 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 9 May 2018 16:00:15 -0400 Subject: [PATCH] rename some crypto terms --- src/models/domain/cipherString.ts | 26 ++++++++-------- src/models/domain/encryptedObject.ts | 2 +- src/services/crypto.service.ts | 44 ++++++++++++++-------------- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/models/domain/cipherString.ts b/src/models/domain/cipherString.ts index b6068edd16c..93ff9e78da7 100644 --- a/src/models/domain/cipherString.ts +++ b/src/models/domain/cipherString.ts @@ -6,15 +6,15 @@ export class CipherString { encryptedString?: string; encryptionType?: EncryptionType; decryptedValue?: string; - cipherText?: string; - initializationVector?: string; + data?: string; + iv?: string; mac?: string; - constructor(encryptedStringOrType: string | EncryptionType, ct?: string, iv?: string, mac?: string) { - if (ct != null) { - // ct and header + constructor(encryptedStringOrType: string | EncryptionType, data?: string, iv?: string, mac?: string) { + if (data != null) { + // data and header const encType = encryptedStringOrType as EncryptionType; - this.encryptedString = encType + '.' + ct; + this.encryptedString = encType + '.' + data; // iv if (iv != null) { @@ -27,8 +27,8 @@ export class CipherString { } this.encryptionType = encType; - this.cipherText = ct; - this.initializationVector = iv; + this.data = data; + this.iv = iv; this.mac = mac; return; @@ -62,8 +62,8 @@ export class CipherString { return; } - this.initializationVector = encPieces[0]; - this.cipherText = encPieces[1]; + this.iv = encPieces[0]; + this.data = encPieces[1]; this.mac = encPieces[2]; break; case EncryptionType.AesCbc256_B64: @@ -71,8 +71,8 @@ export class CipherString { return; } - this.initializationVector = encPieces[0]; - this.cipherText = encPieces[1]; + this.iv = encPieces[0]; + this.data = encPieces[1]; break; case EncryptionType.Rsa2048_OaepSha256_B64: case EncryptionType.Rsa2048_OaepSha1_B64: @@ -80,7 +80,7 @@ export class CipherString { return; } - this.cipherText = encPieces[0]; + this.data = encPieces[0]; break; default: return; diff --git a/src/models/domain/encryptedObject.ts b/src/models/domain/encryptedObject.ts index 10e32797a69..f21fe300f40 100644 --- a/src/models/domain/encryptedObject.ts +++ b/src/models/domain/encryptedObject.ts @@ -2,7 +2,7 @@ import { SymmetricCryptoKey } from './symmetricCryptoKey'; export class EncryptedObject { iv: ArrayBuffer; - ct: ArrayBuffer; + data: ArrayBuffer; mac: ArrayBuffer; key: SymmetricCryptoKey; } diff --git a/src/services/crypto.service.ts b/src/services/crypto.service.ts index 7886ab4c0d3..8ff31b0c6df 100644 --- a/src/services/crypto.service.ts +++ b/src/services/crypto.service.ts @@ -277,9 +277,9 @@ export class CryptoService implements CryptoServiceAbstraction { const encObj = await this.aesEncrypt(plainBuf, key); const iv = Utils.fromBufferToB64(encObj.iv); - const ct = Utils.fromBufferToB64(encObj.ct); + const data = Utils.fromBufferToB64(encObj.data); const mac = encObj.mac != null ? Utils.fromBufferToB64(encObj.mac) : null; - return new CipherString(encObj.key.encType, iv, ct, mac); + return new CipherString(encObj.key.encType, iv, data, mac); } async encryptToBytes(plainValue: ArrayBuffer, key?: SymmetricCryptoKey): Promise { @@ -289,22 +289,22 @@ export class CryptoService implements CryptoServiceAbstraction { macLen = encValue.mac.byteLength; } - const encBytes = new Uint8Array(1 + encValue.iv.byteLength + macLen + encValue.ct.byteLength); + const encBytes = new Uint8Array(1 + encValue.iv.byteLength + macLen + encValue.data.byteLength); encBytes.set([encValue.key.encType]); encBytes.set(new Uint8Array(encValue.iv), 1); if (encValue.mac != null) { encBytes.set(new Uint8Array(encValue.mac), 1 + encValue.iv.byteLength); } - encBytes.set(new Uint8Array(encValue.ct), 1 + encValue.iv.byteLength + macLen); + encBytes.set(new Uint8Array(encValue.data), 1 + encValue.iv.byteLength + macLen); return encBytes.buffer; } async decrypt(cipherString: CipherString, key?: SymmetricCryptoKey): Promise { - const iv = Utils.fromB64ToArray(cipherString.initializationVector).buffer; - const ct = Utils.fromB64ToArray(cipherString.cipherText).buffer; + const iv = Utils.fromB64ToArray(cipherString.iv).buffer; + const data = Utils.fromB64ToArray(cipherString.data).buffer; const mac = cipherString.mac ? Utils.fromB64ToArray(cipherString.mac).buffer : null; - const decipher = await this.aesDecryptToBytes(cipherString.encryptionType, ct, iv, mac, key); + const decipher = await this.aesDecryptToBytes(cipherString.encryptionType, data, iv, mac, key); if (decipher == null) { return null; } @@ -313,8 +313,8 @@ export class CryptoService implements CryptoServiceAbstraction { } async decryptToUtf8(cipherString: CipherString, key?: SymmetricCryptoKey): Promise { - return await this.aesDecryptToUtf8(cipherString.encryptionType, cipherString.cipherText, - cipherString.initializationVector, cipherString.mac, key); + return await this.aesDecryptToUtf8(cipherString.encryptionType, cipherString.data, + cipherString.iv, cipherString.mac, key); } async decryptFromBytes(encBuf: ArrayBuffer, key: SymmetricCryptoKey): Promise { @@ -393,23 +393,23 @@ export class CryptoService implements CryptoServiceAbstraction { // Helpers - private async aesEncrypt(plainValue: ArrayBuffer, key: SymmetricCryptoKey): Promise { + private async aesEncrypt(data: ArrayBuffer, key: SymmetricCryptoKey): Promise { const obj = new EncryptedObject(); obj.key = await this.getKeyForEncryption(key); obj.iv = await this.cryptoFunctionService.randomBytes(16); - obj.ct = await this.cryptoFunctionService.aesEncrypt(plainValue, obj.iv, obj.key.encKey); + obj.data = await this.cryptoFunctionService.aesEncrypt(data, obj.iv, obj.key.encKey); if (obj.key.macKey != null) { - const macData = new Uint8Array(obj.iv.byteLength + obj.ct.byteLength); + const macData = new Uint8Array(obj.iv.byteLength + obj.data.byteLength); macData.set(new Uint8Array(obj.iv), 0); - macData.set(new Uint8Array(obj.ct), obj.iv.byteLength); + macData.set(new Uint8Array(obj.data), obj.iv.byteLength); obj.mac = await this.cryptoFunctionService.hmac(macData.buffer, obj.key.macKey, 'sha256'); } return obj; } - private async aesDecryptToUtf8(encType: EncryptionType, ct: string, iv: string, mac: string, + private async aesDecryptToUtf8(encType: EncryptionType, data: string, iv: string, mac: string, key: SymmetricCryptoKey): Promise { const keyForEnc = await this.getKeyForEncryption(key); const theKey = this.resolveLegacyKey(encType, keyForEnc); @@ -426,7 +426,7 @@ export class CryptoService implements CryptoServiceAbstraction { return null; } - const fastParams = this.cryptoFunctionService.aesDecryptFastParameters(ct, iv, mac, theKey); + const fastParams = this.cryptoFunctionService.aesDecryptFastParameters(data, iv, mac, theKey); if (fastParams.macKey != null && fastParams.mac != null) { const computedMac = await this.cryptoFunctionService.hmacFast(fastParams.macData, fastParams.macKey, 'sha256'); @@ -441,7 +441,7 @@ export class CryptoService implements CryptoServiceAbstraction { return this.cryptoFunctionService.aesDecryptFast(fastParams); } - private async aesDecryptToBytes(encType: EncryptionType, ct: ArrayBuffer, iv: ArrayBuffer, + private async aesDecryptToBytes(encType: EncryptionType, data: ArrayBuffer, iv: ArrayBuffer, mac: ArrayBuffer, key: SymmetricCryptoKey): Promise { const keyForEnc = await this.getKeyForEncryption(key); const theKey = this.resolveLegacyKey(encType, keyForEnc); @@ -455,9 +455,9 @@ export class CryptoService implements CryptoServiceAbstraction { } if (theKey.macKey != null && mac != null) { - const macData = new Uint8Array(iv.byteLength + ct.byteLength); + const macData = new Uint8Array(iv.byteLength + data.byteLength); macData.set(new Uint8Array(iv), 0); - macData.set(new Uint8Array(ct), iv.byteLength); + macData.set(new Uint8Array(data), iv.byteLength); const computedMac = await this.cryptoFunctionService.hmac(macData.buffer, theKey.macKey, 'sha256'); if (computedMac === null) { return null; @@ -471,7 +471,7 @@ export class CryptoService implements CryptoServiceAbstraction { } } - return await this.cryptoFunctionService.aesDecrypt(ct, iv, theKey.encKey); + return await this.cryptoFunctionService.aesDecrypt(data, iv, theKey.encKey); } private async rsaDecrypt(encValue: string): Promise { @@ -510,11 +510,11 @@ export class CryptoService implements CryptoServiceAbstraction { throw new Error('encPieces unavailable.'); } - const ct = Utils.fromB64ToArray(encPieces[0]).buffer; + const data = Utils.fromB64ToArray(encPieces[0]).buffer; const key = await this.getEncKey(); if (key != null && key.macKey != null && encPieces.length > 1) { const mac = Utils.fromB64ToArray(encPieces[1]).buffer; - const computedMac = await this.cryptoFunctionService.hmac(ct, key.macKey, 'sha256'); + const computedMac = await this.cryptoFunctionService.hmac(data, key.macKey, 'sha256'); const macsEqual = await this.cryptoFunctionService.compare(mac, computedMac); if (!macsEqual) { throw new Error('MAC failed.'); @@ -539,7 +539,7 @@ export class CryptoService implements CryptoServiceAbstraction { throw new Error('encType unavailable.'); } - return this.cryptoFunctionService.rsaDecrypt(ct, privateKey, alg); + return this.cryptoFunctionService.rsaDecrypt(data, privateKey, alg); } private async getKeyForEncryption(key?: SymmetricCryptoKey): Promise {