@peculiar/webcrypto
Version:
A WebCrypto Polyfill for NodeJS
148 lines (147 loc) • 3.87 kB
JavaScript
/**
* Copyright (c) 2019-2026, Peculiar Ventures
* SPDX-License-Identifier: MIT
*/
import { AesCryptoKey } from "./key.js";
import { getCryptoKey, setCryptoKey } from "../storage.js";
import { AesCrypto } from "./crypto.js";
import * as core from "webcrypto-core";
import { Buffer } from "node:buffer";
import crypto from "node:crypto";
// src/mechs/aes/aes_cmac.ts
const zero = Buffer.from([
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
]);
const rb = Buffer.from([
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
135
]);
const blockSize = 16;
function bitShiftLeft(buffer) {
const shifted = Buffer.alloc(buffer.length);
const last = buffer.length - 1;
for (let index = 0; index < last; index++) {
shifted[index] = buffer[index] << 1;
if (buffer[index + 1] & 128) shifted[index] += 1;
}
shifted[last] = buffer[last] << 1;
return shifted;
}
function xor(a, b) {
const length = Math.min(a.length, b.length);
const output = Buffer.alloc(length);
for (let index = 0; index < length; index++) output[index] = a[index] ^ b[index];
return output;
}
function aes(key, message) {
const cipher = crypto.createCipheriv(`aes${key.length << 3}`, key, zero);
const result = cipher.update(message);
cipher.final();
return result;
}
function getMessageBlock(message, blockIndex) {
const block = Buffer.alloc(blockSize);
const start = blockIndex * blockSize;
const end = start + blockSize;
message.copy(block, 0, start, end);
return block;
}
function getPaddedMessageBlock(message, blockIndex) {
const block = Buffer.alloc(blockSize);
const start = blockIndex * blockSize;
const end = message.length;
block.fill(0);
message.copy(block, 0, start, end);
block[end - start] = 128;
return block;
}
function generateSubkeys(key) {
const l = aes(key, zero);
let subkey1 = bitShiftLeft(l);
if (l[0] & 128) subkey1 = xor(subkey1, rb);
let subkey2 = bitShiftLeft(subkey1);
if (subkey1[0] & 128) subkey2 = xor(subkey2, rb);
return {
subkey1,
subkey2
};
}
function aesCmac(key, message) {
const subkeys = generateSubkeys(key);
let blockCount = Math.ceil(message.length / blockSize);
let lastBlockCompleteFlag;
let lastBlock;
if (blockCount === 0) {
blockCount = 1;
lastBlockCompleteFlag = false;
} else lastBlockCompleteFlag = message.length % blockSize === 0;
const lastBlockIndex = blockCount - 1;
if (lastBlockCompleteFlag) lastBlock = xor(getMessageBlock(message, lastBlockIndex), subkeys.subkey1);
else lastBlock = xor(getPaddedMessageBlock(message, lastBlockIndex), subkeys.subkey2);
let x = zero;
let y;
for (let index = 0; index < lastBlockIndex; index++) {
y = xor(x, getMessageBlock(message, index));
x = aes(key, y);
}
y = xor(lastBlock, x);
return aes(key, y);
}
var AesCmacProvider = class extends core.AesCmacProvider {
async onGenerateKey(algorithm, extractable, keyUsages) {
const key = await AesCrypto.generateKey({
name: this.name,
length: algorithm.length
}, extractable, keyUsages);
return setCryptoKey(key);
}
async onSign(algorithm, key, data) {
const result = aesCmac(getCryptoKey(key).data, Buffer.from(data));
return new Uint8Array(result).buffer;
}
async onVerify(algorithm, key, signature, data) {
const signature2 = await this.sign(algorithm, key, data);
return Buffer.from(signature).compare(Buffer.from(signature2)) === 0;
}
async onExportKey(format, key) {
return AesCrypto.exportKey(format, getCryptoKey(key));
}
async onImportKey(format, keyData, algorithm, extractable, keyUsages) {
const res = await AesCrypto.importKey(format, keyData, { name: algorithm.name }, extractable, keyUsages);
return setCryptoKey(res);
}
checkCryptoKey(key, keyUsage) {
super.checkCryptoKey(key, keyUsage);
if (!(getCryptoKey(key) instanceof AesCryptoKey)) throw new TypeError("key: Is not a AesCryptoKey");
}
};
export { AesCmacProvider };