Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private static async cipher(algorithm: Algorithm, key: AesCryptoKey, data: ArrayBuffer, encrypt: boolean) {
this.checkLib();
const action = encrypt ? "encrypt" : "decrypt";
let res: Uint8Array;
if (isAlgorithm(algorithm, AesCrypto.AesCBC)) {
// AES-CBC
const iv = core.BufferSourceConverter.toArrayBuffer(algorithm.iv);
res = asmCrypto.AES_CBC[action](data, key.raw, undefined, iv);
} else if (isAlgorithm(algorithm, AesCrypto.AesGCM)) {
// AES-GCM
const iv = core.BufferSourceConverter.toArrayBuffer(algorithm.iv);
let additionalData;
if (algorithm.additionalData) {
additionalData = core.BufferSourceConverter.toArrayBuffer(algorithm.additionalData);
}
const tagLength = (algorithm.tagLength || 128) / 8;
res = asmCrypto.AES_GCM[action](data, key.raw, iv, additionalData, tagLength);
} else if (isAlgorithm(algorithm, AesCrypto.AesECB)) {
// // AES-ECB
res = asmCrypto.AES_ECB[action](data, key.raw, true);
} else {
throw new core.OperationError(`algorithm: Is not recognized`);
}
return res.buffer;
}
private fixFirefoxEcImportPkcs8(args: any[]) {
const preparedAlgorithm = this.prepareAlgorithm(args[2]) as EcKeyImportParams;
const algName = preparedAlgorithm.name.toUpperCase();
if (this.browserInfo.name === Browser.Firefox
&& args[0] === "pkcs8"
&& ~["ECDSA", "ECDH"].indexOf(algName)
&& ~["P-256", "P-384", "P-521"].indexOf(preparedAlgorithm.namedCurve)) {
if (!core.BufferSourceConverter.isBufferSource(args[1])) {
throw new TypeError("data: Is not ArrayBuffer or ArrayBufferView");
}
const preparedData = core.BufferSourceConverter.toArrayBuffer(args[1]);
// Convert PKCS8 to JWK
const keyInfo = AsnParser.parse(preparedData, asn.PrivateKeyInfo);
const privateKey = AsnParser.parse(keyInfo.privateKey, asn.EcPrivateKey);
const jwk: JsonWebKey = JsonSerializer.toJSON(privateKey);
jwk.ext = true;
jwk.key_ops = args[4];
jwk.crv = preparedAlgorithm.namedCurve;
jwk.kty = "EC";
args[0] = "jwk";
args[1] = jwk;
}
public async onDecrypt(algorithm: RsaPkcs1Params, key: RsaCryptoKey, data: ArrayBuffer): Promise {
// EM = 0x00 || 0x02 || PS || 0x00 || M
const EM = new asmCrypto.RSA(key.data).decrypt(new asmCrypto.BigNumber(core.BufferSourceConverter.toUint8Array(data))).result;
const k = key.algorithm.modulusLength >> 3;
if (data.byteLength !== k) {
throw new core.CryptoError("Decryption error. Encrypted message size doesn't match to key length");
}
// If the first octet of EM does not have hexadecimal value 0x00, if
// the second octet of EM does not have hexadecimal value 0x02, if
// there is no octet with hexadecimal value 0x00 to separate PS from
// M, or if the length of PS is less than 8 octets, output
// "decryption error" and stop.
let offset = 0;
if (EM[offset++] || EM[offset++] !== 2) {
throw new core.CryptoError("Decryption error");
}
do {
if (EM[offset++] === 0) {
break;
private static async cipher(algorithm: Algorithm, key: AesCryptoKey, data: ArrayBuffer, encrypt: boolean) {
this.checkLib();
const action = encrypt ? "encrypt" : "decrypt";
let res: Uint8Array;
if (isAlgorithm(algorithm, AesCrypto.AesCBC)) {
// AES-CBC
const iv = core.BufferSourceConverter.toArrayBuffer(algorithm.iv);
res = asmCrypto.AES_CBC[action](data, key.raw, undefined, iv);
} else if (isAlgorithm(algorithm, AesCrypto.AesGCM)) {
// AES-GCM
const iv = core.BufferSourceConverter.toArrayBuffer(algorithm.iv);
let additionalData;
if (algorithm.additionalData) {
additionalData = core.BufferSourceConverter.toArrayBuffer(algorithm.additionalData);
}
const tagLength = (algorithm.tagLength || 128) / 8;
res = asmCrypto.AES_GCM[action](data, key.raw, iv, additionalData, tagLength);
} else if (isAlgorithm(algorithm, AesCrypto.AesECB)) {
// // AES-ECB
res = asmCrypto.AES_ECB[action](data, key.raw, true);
} else {
throw new core.OperationError(`algorithm: Is not recognized`);
}
return res.buffer;
}
public static async importKey(format: string, keyData: JsonWebKey | ArrayBuffer, algorithm: core.DesImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise {
this.checkLib();
let raw: ArrayBuffer;
if (core.isJWK(keyData)) {
raw = Convert.FromBase64Url(keyData.k);
} else {
raw = core.BufferSourceConverter.toArrayBuffer(keyData);
}
// check key length
if ((algorithm.name === "DES-CBC" && raw.byteLength !== 8)
|| (algorithm.name === "DES-EDE3-CBC" && raw.byteLength !== 24)) {
throw new core.OperationError("keyData: Is wrong key length");
}
const key = new DesCryptoKey({ name: algorithm.name, length: raw.byteLength << 3 }, extractable, keyUsages, new Uint8Array(raw));
return key;
}
private fixFirefoxEcImportPkcs8(args: any[]) {
const preparedAlgorithm = this.prepareAlgorithm(args[2]) as EcKeyImportParams;
const algName = preparedAlgorithm.name.toUpperCase();
if (this.browserInfo.name === Browser.Firefox
&& args[0] === "pkcs8"
&& ~["ECDSA", "ECDH"].indexOf(algName)
&& ~["P-256", "P-384", "P-521"].indexOf(preparedAlgorithm.namedCurve)) {
if (!core.BufferSourceConverter.isBufferSource(args[1])) {
throw new TypeError("data: Is not ArrayBuffer or ArrayBufferView");
}
const preparedData = core.BufferSourceConverter.toArrayBuffer(args[1]);
// Convert PKCS8 to JWK
const keyInfo = AsnParser.parse(preparedData, asn.PrivateKeyInfo);
const privateKey = AsnParser.parse(keyInfo.privateKey, asn.EcPrivateKey);
const jwk: JsonWebKey = JsonSerializer.toJSON(privateKey);
jwk.ext = true;
jwk.key_ops = args[4];
jwk.crv = preparedAlgorithm.namedCurve;
jwk.kty = "EC";
args[0] = "jwk";
args[1] = jwk;
}
}
switch (key.algorithm.hash.name.toUpperCase()) {
case "SHA-1":
fn = asmCrypto.HmacSha1;
break;
case "SHA-256":
fn = asmCrypto.HmacSha256;
break;
case "SHA-512":
fn = asmCrypto.HmacSha512;
break;
default:
throw new core.OperationError("key.algorithm.hash: Is not recognized");
}
const result = new fn(key.data)
.process(core.BufferSourceConverter.toUint8Array(data))
.finish().result;
return core.BufferSourceConverter.toArrayBuffer(result);
}
break;
case "SHA-256":
fn = asmCrypto.HmacSha256;
break;
case "SHA-512":
fn = asmCrypto.HmacSha512;
break;
default:
throw new core.OperationError("key.algorithm.hash: Is not recognized");
}
const result = new fn(key.data)
.process(core.BufferSourceConverter.toUint8Array(data))
.finish().result;
return core.BufferSourceConverter.toArrayBuffer(result);
}
public async onDeriveBits(algorithm: Pbkdf2Params, baseKey: PbkdfCryptoKey, length: number): Promise {
let result: Uint8Array;
const salt = core.BufferSourceConverter.toUint8Array(algorithm.salt);
const password = baseKey.raw;
switch ((algorithm.hash as Algorithm).name.toUpperCase()) {
case "SHA-1":
result = asmCrypto.Pbkdf2HmacSha1(password, salt, algorithm.iterations, length >> 3);
break;
case "SHA-256":
result = asmCrypto.Pbkdf2HmacSha256(password, salt, algorithm.iterations, length >> 3);
break;
case "SHA-512":
result = asmCrypto.Pbkdf2HmacSha512(password, salt, algorithm.iterations, length >> 3);
break;
default:
throw new core.OperationError(`algorithm.hash: '${(algorithm.hash as Algorithm).name}' hash algorithm is not supported`);
}
return core.BufferSourceConverter.toArrayBuffer(result);
}
const salt = core.BufferSourceConverter.toUint8Array(algorithm.salt);
const password = baseKey.raw;
switch ((algorithm.hash as Algorithm).name.toUpperCase()) {
case "SHA-1":
result = asmCrypto.Pbkdf2HmacSha1(password, salt, algorithm.iterations, length >> 3);
break;
case "SHA-256":
result = asmCrypto.Pbkdf2HmacSha256(password, salt, algorithm.iterations, length >> 3);
break;
case "SHA-512":
result = asmCrypto.Pbkdf2HmacSha512(password, salt, algorithm.iterations, length >> 3);
break;
default:
throw new core.OperationError(`algorithm.hash: '${(algorithm.hash as Algorithm).name}' hash algorithm is not supported`);
}
return core.BufferSourceConverter.toArrayBuffer(result);
}