Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
init(iv, aad) {
let h = this.block.encrypt(new ArrayBuffer(this.block.blockSize));
this.ghash = new GHASH(h, aad);
if (iv.byteLength == 12) {
let one = new DataView(new ArrayBuffer(4));
one.setUint32(0, 1); // big endian
iv = iv.concat(one.buffer);
}
else {
let ghash = new GHASH(h);
iv = ghash.process(iv);
}
this.y0 = iv;
// start with y1
let y1 = BigInt.fromArrayBuffer(iv);
y1++;
this.ctr.setIV(ArrayBuffer.fromBigInt(y1));
}
encrypt(data, buf) {
init(iv, aad) {
let h = this.block.encrypt(new ArrayBuffer(this.block.blockSize));
this.ghash = new GHASH(h, aad);
if (iv.byteLength == 12) {
let one = new DataView(new ArrayBuffer(4));
one.setUint32(0, 1); // big endian
iv = iv.concat(one.buffer);
}
else {
let ghash = new GHASH(h);
iv = ghash.process(iv);
}
this.y0 = iv;
// start with y1
let y1 = BigInt.fromArrayBuffer(iv);
y1++;
this.ctr.setIV(ArrayBuffer.fromBigInt(y1));
}
encrypt(data, buf) {
static parse(buf) {
// RSA only
var key = {};
var ber = new Crypt.BER(buf);
if (ber.getTag() != 0x30) // SEQUENCE
throw new Error("PKCS8: not a sequence");
ber.getLength() // skip the sequence length
ber.getInteger(); // skip the Version
ber.next(); // skip the AlgorithmIdentifier
ber.getTag(); // OCTET STRING that includes the private key in the SEQUENCE
ber.getLength();
{
ber.getTag(); // SEQUENCE
ber.getLength();
ber.getInteger(); // skip the first INTEGER
key.modulus = ber.getInteger();
key.exponent = ber.getInteger();
key.privExponent = ber.getInteger();
key.prim1 = ber.getInteger();
key.prim2 = ber.getInteger();
static decrypt(buf, pass) {
var ber = new Crypt.BER(buf);
if (ber.getTag() == 0x30) {
ber.getLength();
if (ber.getTag() == 0x30) {
ber.getLength();
var oid = ber.getObjectIdentifier();
if (ber.getTag() == 0x30) {
ber.getLength();
var salt = ber.getOctetString();
var iter = ber.getInteger();
var data = ber.getOctetString();
var cipher, hash, mode, v;
switch (oid.toString()) {
case [1,2,840,113549,1,12,1,1].toString(): // pbeWithSHA1And128BitRC4
cipher = Crypt.RC4;
hash = Crypt.SHA1;
v = 512 / 8;
static parse(buf) {
// RSA only
var key = {};
var ber = new Crypt.BER(buf);
if (ber.getTag() != 0x30) // SEQUENCE
throw new Error("PKCS8: not a sequence");
ber.getLength() // skip the sequence length
ber.getInteger(); // skip the Version
ber.next(); // skip the AlgorithmIdentifier
ber.getTag(); // OCTET STRING that includes the private key in the SEQUENCE
ber.getLength();
{
ber.getTag(); // SEQUENCE
ber.getLength();
ber.getInteger(); // skip the first INTEGER
key.modulus = ber.getInteger();
key.exponent = ber.getInteger();
key.privExponent = ber.getInteger();
key.prim1 = ber.getInteger();
key.prim2 = ber.getInteger();
case [1, 2, 840, 113549, 1, 1, 13].toString(): // PKCS-1 SHA512 with RSA encryption
hash = Crypt.SHA512;
pk = Crypt.PKCS1_5;
sig = x509.sig;
break;
case [1, 2, 840, 113549, 1, 1, 14].toString(): // PKCS-1 SHA224 with RSA encryption
hash = Crypt.SHA224;
pk = Crypt.PKCS1_5;
sig = x509.sig;
break;
case [1, 2, 840, 10040, 4, 3].toString():
case [1, 3, 14, 3, 2, 27].toString():
hash = Crypt.SHA1;
pk = Crypt.DSA;
// needs to decode the sig value into
var ber = new Crypt.BER(x509.sig);
if (ber.getTag() == 0x30) {
ber.getLength();
var r = ber.getInteger();
var s = ber.getInteger();
sig = r.concat(s);
}
break;
default:
throw new Error("Cert: unsupported algorithm: " + x509.algo.toString());
break;
}
var H = (new hash()).process(x509.tbs);
return (new pk(spki, false, [] /* any oid */)).verify(H, sig);
};
register(cert) {
static parse(buf, privFlag) {
// currently RSA only
var key = {};
var ber = new Crypt.BER(buf);
if (ber.getTag() != 0x30) // SEQUENCE
throw new Error("PKCS1: not a sequence");
ber.getLength(); // skip the sequence length
ber.getInteger(); // ignore the first INTEGER
key.modulus = ber.getInteger();
key.exponent = ber.getInteger();
if (privFlag) {
key.privExponent = ber.getInteger();
key.prim1 = ber.getInteger();
key.prim2 = ber.getInteger();
key.exponent1 = ber.getInteger();
key.exponent2 = ber.getInteger();
key.coefficient = ber.getInteger();
}
return key;
};
static decrypt(buf, pass) {
var ber = new Crypt.BER(buf);
if (ber.getTag() == 0x30) {
ber.getLength();
if (ber.getTag() == 0x30) {
ber.getLength();
var oid = ber.getObjectIdentifier();
if (ber.getTag() == 0x30) {
ber.getLength();
var salt = ber.getOctetString();
var iter = ber.getInteger();
var data = ber.getOctetString();
var cipher, hash, mode, v;
switch (oid.toString()) {
case [1,2,840,113549,1,12,1,1].toString(): // pbeWithSHA1And128BitRC4
cipher = Crypt.RC4;
hash = Crypt.SHA1;
v = 512 / 8;
emeDecode(iEM, P, emLen) {
var EM = new Uint8Array(iEM.toChunk(emLen));
var H = this.H;
var hLen = H.outputSize;
var maskedSeed = EM.slice(0, hLen);
var maskedDB = EM.slice(hLen);
var seedMask = Crypt.PKCS1.MGF(maskedDB, hLen);
this.xor(maskedSeed, seedMask);
var dbMask = this.MGF(maskedSeed, emLen - hLen);
this.xor(maskedDB, dbMask);
var DB = maskedDB;
var pHash = H.process(P);
// check to see if the pHash equals the first hLen of DB
if (this.ncomp(pHash, DB, hLen) != 0) {
throw new Error("malformed input");
}
var c, i;
for (i = hLen; i < DB.length && (c = DB[i]) == 0x00; i++)
;
if (c != 0x01) {
throw new Error("malformed input");
}
return DB.slice(i+1);
verify(H, sig) {
// "20" is specified in the xmldsig-core spec.
var r = Crypt.PKCS1.OS2IP(sig.slice(0, 20));
var s = Crypt.PKCS1.OS2IP(sig.slice(20, 40));
return(this._verify(H, r, s));
};
};