mirror of
https://github.com/bitwarden/browser
synced 2025-12-17 16:53:34 +00:00
rename some crypto terms
This commit is contained in:
@@ -6,15 +6,15 @@ export class CipherString {
|
|||||||
encryptedString?: string;
|
encryptedString?: string;
|
||||||
encryptionType?: EncryptionType;
|
encryptionType?: EncryptionType;
|
||||||
decryptedValue?: string;
|
decryptedValue?: string;
|
||||||
cipherText?: string;
|
data?: string;
|
||||||
initializationVector?: string;
|
iv?: string;
|
||||||
mac?: string;
|
mac?: string;
|
||||||
|
|
||||||
constructor(encryptedStringOrType: string | EncryptionType, ct?: string, iv?: string, mac?: string) {
|
constructor(encryptedStringOrType: string | EncryptionType, data?: string, iv?: string, mac?: string) {
|
||||||
if (ct != null) {
|
if (data != null) {
|
||||||
// ct and header
|
// data and header
|
||||||
const encType = encryptedStringOrType as EncryptionType;
|
const encType = encryptedStringOrType as EncryptionType;
|
||||||
this.encryptedString = encType + '.' + ct;
|
this.encryptedString = encType + '.' + data;
|
||||||
|
|
||||||
// iv
|
// iv
|
||||||
if (iv != null) {
|
if (iv != null) {
|
||||||
@@ -27,8 +27,8 @@ export class CipherString {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.encryptionType = encType;
|
this.encryptionType = encType;
|
||||||
this.cipherText = ct;
|
this.data = data;
|
||||||
this.initializationVector = iv;
|
this.iv = iv;
|
||||||
this.mac = mac;
|
this.mac = mac;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@@ -62,8 +62,8 @@ export class CipherString {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.initializationVector = encPieces[0];
|
this.iv = encPieces[0];
|
||||||
this.cipherText = encPieces[1];
|
this.data = encPieces[1];
|
||||||
this.mac = encPieces[2];
|
this.mac = encPieces[2];
|
||||||
break;
|
break;
|
||||||
case EncryptionType.AesCbc256_B64:
|
case EncryptionType.AesCbc256_B64:
|
||||||
@@ -71,8 +71,8 @@ export class CipherString {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.initializationVector = encPieces[0];
|
this.iv = encPieces[0];
|
||||||
this.cipherText = encPieces[1];
|
this.data = encPieces[1];
|
||||||
break;
|
break;
|
||||||
case EncryptionType.Rsa2048_OaepSha256_B64:
|
case EncryptionType.Rsa2048_OaepSha256_B64:
|
||||||
case EncryptionType.Rsa2048_OaepSha1_B64:
|
case EncryptionType.Rsa2048_OaepSha1_B64:
|
||||||
@@ -80,7 +80,7 @@ export class CipherString {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.cipherText = encPieces[0];
|
this.data = encPieces[0];
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { SymmetricCryptoKey } from './symmetricCryptoKey';
|
|||||||
|
|
||||||
export class EncryptedObject {
|
export class EncryptedObject {
|
||||||
iv: ArrayBuffer;
|
iv: ArrayBuffer;
|
||||||
ct: ArrayBuffer;
|
data: ArrayBuffer;
|
||||||
mac: ArrayBuffer;
|
mac: ArrayBuffer;
|
||||||
key: SymmetricCryptoKey;
|
key: SymmetricCryptoKey;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -277,9 +277,9 @@ export class CryptoService implements CryptoServiceAbstraction {
|
|||||||
|
|
||||||
const encObj = await this.aesEncrypt(plainBuf, key);
|
const encObj = await this.aesEncrypt(plainBuf, key);
|
||||||
const iv = Utils.fromBufferToB64(encObj.iv);
|
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;
|
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<ArrayBuffer> {
|
async encryptToBytes(plainValue: ArrayBuffer, key?: SymmetricCryptoKey): Promise<ArrayBuffer> {
|
||||||
@@ -289,22 +289,22 @@ export class CryptoService implements CryptoServiceAbstraction {
|
|||||||
macLen = encValue.mac.byteLength;
|
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([encValue.key.encType]);
|
||||||
encBytes.set(new Uint8Array(encValue.iv), 1);
|
encBytes.set(new Uint8Array(encValue.iv), 1);
|
||||||
if (encValue.mac != null) {
|
if (encValue.mac != null) {
|
||||||
encBytes.set(new Uint8Array(encValue.mac), 1 + encValue.iv.byteLength);
|
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;
|
return encBytes.buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
async decrypt(cipherString: CipherString, key?: SymmetricCryptoKey): Promise<ArrayBuffer> {
|
async decrypt(cipherString: CipherString, key?: SymmetricCryptoKey): Promise<ArrayBuffer> {
|
||||||
const iv = Utils.fromB64ToArray(cipherString.initializationVector).buffer;
|
const iv = Utils.fromB64ToArray(cipherString.iv).buffer;
|
||||||
const ct = Utils.fromB64ToArray(cipherString.cipherText).buffer;
|
const data = Utils.fromB64ToArray(cipherString.data).buffer;
|
||||||
const mac = cipherString.mac ? Utils.fromB64ToArray(cipherString.mac).buffer : null;
|
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) {
|
if (decipher == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -313,8 +313,8 @@ export class CryptoService implements CryptoServiceAbstraction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async decryptToUtf8(cipherString: CipherString, key?: SymmetricCryptoKey): Promise<string> {
|
async decryptToUtf8(cipherString: CipherString, key?: SymmetricCryptoKey): Promise<string> {
|
||||||
return await this.aesDecryptToUtf8(cipherString.encryptionType, cipherString.cipherText,
|
return await this.aesDecryptToUtf8(cipherString.encryptionType, cipherString.data,
|
||||||
cipherString.initializationVector, cipherString.mac, key);
|
cipherString.iv, cipherString.mac, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
async decryptFromBytes(encBuf: ArrayBuffer, key: SymmetricCryptoKey): Promise<ArrayBuffer> {
|
async decryptFromBytes(encBuf: ArrayBuffer, key: SymmetricCryptoKey): Promise<ArrayBuffer> {
|
||||||
@@ -393,23 +393,23 @@ export class CryptoService implements CryptoServiceAbstraction {
|
|||||||
|
|
||||||
// Helpers
|
// Helpers
|
||||||
|
|
||||||
private async aesEncrypt(plainValue: ArrayBuffer, key: SymmetricCryptoKey): Promise<EncryptedObject> {
|
private async aesEncrypt(data: ArrayBuffer, key: SymmetricCryptoKey): Promise<EncryptedObject> {
|
||||||
const obj = new EncryptedObject();
|
const obj = new EncryptedObject();
|
||||||
obj.key = await this.getKeyForEncryption(key);
|
obj.key = await this.getKeyForEncryption(key);
|
||||||
obj.iv = await this.cryptoFunctionService.randomBytes(16);
|
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) {
|
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.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');
|
obj.mac = await this.cryptoFunctionService.hmac(macData.buffer, obj.key.macKey, 'sha256');
|
||||||
}
|
}
|
||||||
|
|
||||||
return obj;
|
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<string> {
|
key: SymmetricCryptoKey): Promise<string> {
|
||||||
const keyForEnc = await this.getKeyForEncryption(key);
|
const keyForEnc = await this.getKeyForEncryption(key);
|
||||||
const theKey = this.resolveLegacyKey(encType, keyForEnc);
|
const theKey = this.resolveLegacyKey(encType, keyForEnc);
|
||||||
@@ -426,7 +426,7 @@ export class CryptoService implements CryptoServiceAbstraction {
|
|||||||
return null;
|
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) {
|
if (fastParams.macKey != null && fastParams.mac != null) {
|
||||||
const computedMac = await this.cryptoFunctionService.hmacFast(fastParams.macData,
|
const computedMac = await this.cryptoFunctionService.hmacFast(fastParams.macData,
|
||||||
fastParams.macKey, 'sha256');
|
fastParams.macKey, 'sha256');
|
||||||
@@ -441,7 +441,7 @@ export class CryptoService implements CryptoServiceAbstraction {
|
|||||||
return this.cryptoFunctionService.aesDecryptFast(fastParams);
|
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<ArrayBuffer> {
|
mac: ArrayBuffer, key: SymmetricCryptoKey): Promise<ArrayBuffer> {
|
||||||
const keyForEnc = await this.getKeyForEncryption(key);
|
const keyForEnc = await this.getKeyForEncryption(key);
|
||||||
const theKey = this.resolveLegacyKey(encType, keyForEnc);
|
const theKey = this.resolveLegacyKey(encType, keyForEnc);
|
||||||
@@ -455,9 +455,9 @@ export class CryptoService implements CryptoServiceAbstraction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (theKey.macKey != null && mac != null) {
|
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(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');
|
const computedMac = await this.cryptoFunctionService.hmac(macData.buffer, theKey.macKey, 'sha256');
|
||||||
if (computedMac === null) {
|
if (computedMac === null) {
|
||||||
return 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<ArrayBuffer> {
|
private async rsaDecrypt(encValue: string): Promise<ArrayBuffer> {
|
||||||
@@ -510,11 +510,11 @@ export class CryptoService implements CryptoServiceAbstraction {
|
|||||||
throw new Error('encPieces unavailable.');
|
throw new Error('encPieces unavailable.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const ct = Utils.fromB64ToArray(encPieces[0]).buffer;
|
const data = Utils.fromB64ToArray(encPieces[0]).buffer;
|
||||||
const key = await this.getEncKey();
|
const key = await this.getEncKey();
|
||||||
if (key != null && key.macKey != null && encPieces.length > 1) {
|
if (key != null && key.macKey != null && encPieces.length > 1) {
|
||||||
const mac = Utils.fromB64ToArray(encPieces[1]).buffer;
|
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);
|
const macsEqual = await this.cryptoFunctionService.compare(mac, computedMac);
|
||||||
if (!macsEqual) {
|
if (!macsEqual) {
|
||||||
throw new Error('MAC failed.');
|
throw new Error('MAC failed.');
|
||||||
@@ -539,7 +539,7 @@ export class CryptoService implements CryptoServiceAbstraction {
|
|||||||
throw new Error('encType unavailable.');
|
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<SymmetricCryptoKey> {
|
private async getKeyForEncryption(key?: SymmetricCryptoKey): Promise<SymmetricCryptoKey> {
|
||||||
|
|||||||
Reference in New Issue
Block a user