aes-universal-native
Version:
Native implementation of aes-universal
109 lines (108 loc) • 3.67 kB
JavaScript
import { AbstractCbcCipher as f, AbstractGcmCipher as w, AesCipher as B } from "aes-universal";
import e from "node-forge";
class b extends f {
/**
* Constructs a NativeCbcCipher instance.
* @param randomBytes - The random bytes function to be used for cryptographic operations.
*/
constructor(y) {
super(y), this.encryptInternal = async ({
encRawKey: n,
iv: i,
plaintext: a
}) => {
const c = e.util.binary.raw.encode(n), r = e.util.binary.raw.encode(i), t = e.cipher.createCipher("AES-CBC", c);
t.start({
iv: r
});
const o = e.util.binary.raw.encode(a), s = e.util.createBuffer(o);
if (t.update(s), !t.finish())
throw new Error("Encryption failed");
return e.util.binary.raw.decode(t.output.getBytes());
}, this.decryptInternal = async ({
encRawKey: n,
iv: i,
ciphertext: a
}) => {
const c = e.util.binary.raw.encode(n), r = e.util.binary.raw.encode(i), t = e.cipher.createDecipher("AES-CBC", c);
t.start({
iv: r
});
const o = e.util.binary.raw.encode(a), s = e.util.createBuffer(o);
if (t.update(s), !t.finish())
throw new Error("Decryption failed");
return e.util.binary.raw.decode(t.output.getBytes());
}, this.generateTag = async ({
macRawKey: n,
macData: i,
keyBitLength: a
}) => {
const c = `sha${a << 1}`, r = e.hmac.create();
return r.start(c, e.util.binary.raw.encode(n)), r.update(e.util.binary.raw.encode(i)), e.util.binary.raw.decode(r.digest().getBytes()).slice(0, a >> 3);
};
}
}
class g extends w {
/**
* Constructs a NativeGcmCipher instance.
* @param randomBytes - The random bytes function to be used for cryptographic operations.
*/
constructor(y) {
super(y), this.encryptInternal = async ({
encRawKey: n,
iv: i,
plaintext: a,
aad: c
}) => {
if (i.length !== 12)
throw new Error("IV must be 12 bytes for AES-GCM");
const r = e.util.binary.raw.encode(n), t = e.util.binary.raw.encode(i), o = e.util.binary.raw.encode(c), s = e.util.binary.raw.encode(a), l = e.util.createBuffer(s), u = e.cipher.createCipher("AES-GCM", r);
if (u.start({
iv: t,
additionalData: o,
tagLength: 128
}), u.update(l), !u.finish())
throw new Error("Encryption failed");
return {
ciphertext: e.util.binary.raw.decode(u.output.getBytes()),
tag: e.util.binary.raw.decode(u.mode.tag.getBytes())
};
}, this.decryptInternal = async ({
encRawKey: n,
iv: i,
ciphertext: a,
tag: c,
aad: r
}) => {
const t = e.util.binary.raw.encode(n), o = e.util.binary.raw.encode(i), s = e.util.binary.raw.encode(a), l = e.util.binary.raw.encode(c), u = e.util.createBuffer(l), h = e.util.binary.raw.encode(r), d = e.cipher.createDecipher("AES-GCM", t);
if (d.start({
iv: o,
additionalData: h,
tagLength: 128,
tag: u
}), d.update(e.util.createBuffer(s)), !d.finish())
throw new Error("Authentication failed: Invalid tag or corrupted data");
return e.util.binary.raw.decode(d.output.getBytes());
};
}
}
class x extends B {
/**
* Creates a new instance of NativeAesCipher.
*
* @param randomBytes - Function that generates cryptographically secure random bytes
* Must implement the RandomBytes interface from aes-universal
*/
constructor(y) {
super({
cbc: b,
gcm: g,
randomBytes: y
});
}
}
export {
x as NativeAesCipher,
b as NativeCbcCipher,
g as NativeGcmCipher
};