@peculiar/webcrypto
Version:
A WebCrypto Polyfill for NodeJS
148 lines (147 loc) • 6.44 kB
JavaScript
/**
* Copyright (c) 2019-2026, Peculiar Ventures
* SPDX-License-Identifier: MIT
*/
import { AesCryptoKey } from "./key.js";
import * as core from "webcrypto-core";
import { Buffer } from "node:buffer";
import crypto from "node:crypto";
import { JsonParser, JsonSerializer } from "@peculiar/json-schema";
import { assertBufferSource, toArrayBuffer, toUint8Array } from "@peculiar/utils";
// src/mechs/aes/crypto.ts
var AesCrypto = class {
static AES_KW_IV = Buffer.from("A6A6A6A6A6A6A6A6", "hex");
static async generateKey(algorithm, extractable, keyUsages) {
const key = new AesCryptoKey();
key.algorithm = algorithm;
key.extractable = extractable;
key.usages = keyUsages;
key.data = crypto.randomBytes(algorithm.length >> 3);
return key;
}
static async exportKey(format, key) {
if (!(key instanceof AesCryptoKey)) throw new Error("key: Is not AesCryptoKey");
switch (format.toLowerCase()) {
case "jwk": return JsonSerializer.toJSON(key);
case "raw": return toArrayBuffer(key.data);
default: throw new core.OperationError("format: Must be 'jwk' or 'raw'");
}
}
static async importKey(format, keyData, algorithm, extractable, keyUsages) {
let key;
switch (format.toLowerCase()) {
case "jwk":
key = JsonParser.fromJSON(keyData, { targetSchema: AesCryptoKey });
break;
case "raw":
assertBufferSource(keyData);
key = new AesCryptoKey();
key.data = Buffer.from(toUint8Array(keyData));
break;
default: throw new core.OperationError("format: Must be 'jwk' or 'raw'");
}
key.algorithm = algorithm;
key.algorithm.length = key.data.length << 3;
key.extractable = extractable;
key.usages = keyUsages;
switch (key.algorithm.length) {
case 128:
case 192:
case 256: break;
default: throw new core.OperationError("keyData: Is wrong key length");
}
return key;
}
static async encrypt(algorithm, key, data) {
switch (algorithm.name.toUpperCase()) {
case "AES-CBC": return this.encryptAesCBC(algorithm, key, Buffer.from(data));
case "AES-CTR": return this.encryptAesCTR(algorithm, key, Buffer.from(data));
case "AES-GCM": return this.encryptAesGCM(algorithm, key, Buffer.from(data));
case "AES-KW": return this.encryptAesKW(algorithm, key, Buffer.from(data));
case "AES-ECB": return this.encryptAesECB(algorithm, key, Buffer.from(data));
default: throw new core.OperationError("algorithm: Is not recognized");
}
}
static async decrypt(algorithm, key, data) {
if (!(key instanceof AesCryptoKey)) throw new Error("key: Is not AesCryptoKey");
switch (algorithm.name.toUpperCase()) {
case "AES-CBC": return this.decryptAesCBC(algorithm, key, Buffer.from(data));
case "AES-CTR": return this.decryptAesCTR(algorithm, key, Buffer.from(data));
case "AES-GCM": return this.decryptAesGCM(algorithm, key, Buffer.from(data));
case "AES-KW": return this.decryptAesKW(algorithm, key, Buffer.from(data));
case "AES-ECB": return this.decryptAesECB(algorithm, key, Buffer.from(data));
default: throw new core.OperationError("algorithm: Is not recognized");
}
}
static async encryptAesCBC(algorithm, key, data) {
const cipher = crypto.createCipheriv(`aes-${key.algorithm.length}-cbc`, key.data, toUint8Array(algorithm.iv));
let enc = cipher.update(data);
enc = Buffer.concat([enc, cipher.final()]);
return toArrayBuffer(enc);
}
static async decryptAesCBC(algorithm, key, data) {
const decipher = crypto.createDecipheriv(`aes-${key.algorithm.length}-cbc`, key.data, toUint8Array(algorithm.iv));
let dec = decipher.update(data);
dec = Buffer.concat([dec, decipher.final()]);
return toArrayBuffer(dec);
}
static async encryptAesCTR(algorithm, key, data) {
const cipher = crypto.createCipheriv(`aes-${key.algorithm.length}-ctr`, key.data, Buffer.from(toUint8Array(algorithm.counter)));
let enc = cipher.update(data);
enc = Buffer.concat([enc, cipher.final()]);
return toArrayBuffer(enc);
}
static async decryptAesCTR(algorithm, key, data) {
const decipher = crypto.createDecipheriv(`aes-${key.algorithm.length}-ctr`, key.data, toUint8Array(algorithm.counter));
let dec = decipher.update(data);
dec = Buffer.concat([dec, decipher.final()]);
return toArrayBuffer(dec);
}
static async encryptAesGCM(algorithm, key, data) {
const cipher = crypto.createCipheriv(`aes-${key.algorithm.length}-gcm`, key.data, Buffer.from(toUint8Array(algorithm.iv)), { authTagLength: (algorithm.tagLength || 128) >> 3 });
if (algorithm.additionalData) cipher.setAAD(Buffer.from(toUint8Array(algorithm.additionalData)));
let enc = cipher.update(data);
enc = Buffer.concat([
enc,
cipher.final(),
cipher.getAuthTag()
]);
return toArrayBuffer(enc);
}
static async decryptAesGCM(algorithm, key, data) {
const tagLength = (algorithm.tagLength || 128) >> 3;
const decipher = crypto.createDecipheriv(`aes-${key.algorithm.length}-gcm`, key.data, toUint8Array(algorithm.iv), { authTagLength: tagLength });
const enc = data.slice(0, data.length - tagLength);
const tag = data.slice(data.length - tagLength);
if (algorithm.additionalData) decipher.setAAD(Buffer.from(toUint8Array(algorithm.additionalData)));
decipher.setAuthTag(tag);
let dec = decipher.update(enc);
dec = Buffer.concat([dec, decipher.final()]);
return toArrayBuffer(dec);
}
static async encryptAesKW(algorithm, key, data) {
const cipher = crypto.createCipheriv(`id-aes${key.algorithm.length}-wrap`, key.data, this.AES_KW_IV);
let enc = cipher.update(data);
enc = Buffer.concat([enc, cipher.final()]);
return toArrayBuffer(enc);
}
static async decryptAesKW(algorithm, key, data) {
const decipher = crypto.createDecipheriv(`id-aes${key.algorithm.length}-wrap`, key.data, this.AES_KW_IV);
let dec = decipher.update(data);
dec = Buffer.concat([dec, decipher.final()]);
return toArrayBuffer(dec);
}
static async encryptAesECB(algorithm, key, data) {
const cipher = crypto.createCipheriv(`aes-${key.algorithm.length}-ecb`, key.data, /* @__PURE__ */ new Uint8Array(0));
let enc = cipher.update(data);
enc = Buffer.concat([enc, cipher.final()]);
return toArrayBuffer(enc);
}
static async decryptAesECB(algorithm, key, data) {
const decipher = crypto.createDecipheriv(`aes-${key.algorithm.length}-ecb`, key.data, /* @__PURE__ */ new Uint8Array(0));
let dec = decipher.update(data);
dec = Buffer.concat([dec, decipher.final()]);
return toArrayBuffer(dec);
}
};
export { AesCrypto };