Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
}
} while (offset < EM.length);
// 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;
}
} while (offset < EM.length);
if (offset < 11) {
throw new core.CryptoError("Decryption error. PS is less than 8 octets.");
}
if (offset === EM.length) {
throw new core.CryptoError("Decryption error. There is no octet with hexadecimal value 0x00 to separate PS from M");
}
return EM.buffer.slice(offset);
}
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;
}
} while (offset < EM.length);
if (offset < 11) {
throw new core.CryptoError("Decryption error. PS is less than 8 octets.");
}
if (offset === EM.length) {
throw new core.CryptoError("Decryption error. There is no octet with hexadecimal value 0x00 to separate PS from M");
}
return EM.buffer.slice(offset);
public async onEncrypt(algorithm: RsaPkcs1Params, key: RsaCryptoKey, data: ArrayBuffer): Promise {
const k = key.algorithm.modulusLength >> 3;
if (data.byteLength > k - 11) {
throw new core.CryptoError("Message too long");
}
// EM = 0x00 || 0x02 || PS || 0x00 || M
const psLen = k - data.byteLength - 3;
const PS = RsaCrypto.randomNonZeroValues(new Uint8Array(psLen));
const EM = new Uint8Array(k);
EM[0] = 0;
EM[1] = 2;
EM.set(PS, 2); // PS
EM[2 + psLen] = 0;
EM.set(new Uint8Array(data), 3 + psLen);
const result = new asmCrypto.RSA(key.data).encrypt(new asmCrypto.BigNumber(EM)).result;
return core.BufferSourceConverter.toArrayBuffer(result);
}
constructor(key: Key, alg: T) {
super();
this.p11Object = key;
switch (key.class) {
case ObjectClass.PUBLIC_KEY:
this.initPublicKey(key.toType());
break;
case ObjectClass.PRIVATE_KEY:
this.initPrivateKey(key.toType());
break;
case ObjectClass.SECRET_KEY:
this.initSecretKey(key.toType());
break;
default:
throw new core.CryptoError(`Wrong incoming session object '${ObjectClass[key.class]}'`);
}
this.algorithm = alg;
this.id = CryptoKey.getID(key);
this.onAssign();
}
let offset = 0;
if (EM[offset++] || EM[offset++] !== 2) {
throw new core.CryptoError("Decryption error");
}
do {
if (EM[offset++] === 0) {
break;
}
} while (offset < EM.length);
if (offset < 11) {
throw new core.CryptoError("Decryption error. PS is less than 8 octets.");
}
if (offset === EM.length) {
throw new core.CryptoError("Decryption error. There is no octet with hexadecimal value 0x00 to separate PS from M");
}
return EM.buffer.slice(offset);
}
session.generateKey(KeyGenMechanism.AES, template, (err, aesKey) => {
try {
if (err) {
reject(new core.CryptoError(`Aes: Can not generate new key\n${err.message}`));
} else {
resolve(new CryptoKey(aesKey, algorithm));
}
} catch (e) {
reject(e);
}
});
});
session.generateKeyPair(KeyGenMechanism.RSA, template.publicKey, template.privateKey, (err, keys) => {
try {
if (err) {
reject(new core.CryptoError(`Rsa: Can not generate new key\n${err.message}`));
} else {
const wcKeyPair = {
privateKey: new RsaCryptoKey(keys.privateKey, algorithm as RsaHashedKeyAlgorithm),
publicKey: new RsaCryptoKey(keys.publicKey, algorithm as RsaHashedKeyAlgorithm),
};
resolve(wcKeyPair as any);
}
} catch (e) {
reject(e);
}
});
});
this.crypto.session.generateKey(graphene.KeyGenMechanism.GENERIC_SECRET, template, (err, aesKey) => {
try {
if (err) {
reject(new core.CryptoError(`HMAC: Cannot generate new key\n${err.message}`));
} else {
resolve(new HmacCryptoKey(aesKey, { ...algorithm, name: this.name } as HmacKeyAlgorithm));
}
} catch (e) {
reject(e);
}
});
});
public getRandomValues(array: T): T {
if (array.byteLength > 65536) {
throw new core.CryptoError(`Failed to execute 'getRandomValues' on 'Crypto': The ArrayBufferView's byte length (${array.byteLength}) exceeds the number of bytes of entropy available via this API (65536).`);
}
const bytes = new Uint8Array(this.session.generateRandom(array.byteLength));
(array as unknown as Uint8Array).set(bytes);
return array;
}