Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add webcrypto aes-gcm opitonal aad #6

Merged
merged 2 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 38 additions & 12 deletions src/webcrypto/aes.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,59 @@
import { ensureBytes } from '../utils.js';
import { getWebcryptoSubtle } from './utils.js';

function generate(algo: string, length: number) {
/**
* AAD is only effective on AES-256-GCM or AES-128-GCM. Otherwise it'll be ignored
*/
export type Cipher = (
key: Uint8Array,
nonce: Uint8Array,
AAD?: Uint8Array
kigawas marked this conversation as resolved.
Show resolved Hide resolved
) => {
keyLength: number;
encrypt(plaintext: Uint8Array): Promise<Uint8Array>;
decrypt(ciphertext: Uint8Array): Promise<Uint8Array>;
};

type Algo = 'AES-CTR' | 'AES-GCM' | 'AES-CBC';
type BitLength = 128 | 256;

function getCryptParams(
algo: Algo,
nonce: Uint8Array,
AAD?: Uint8Array
): AesCbcParams | AesCtrParams | AesGcmParams {
const params = { name: algo };
if (algo === 'AES-CTR') {
return { ...params, counter: nonce, length: 64 } as AesCtrParams;
} else if (algo === 'AES-GCM') {
return { ...params, iv: nonce, additionalData: AAD } as AesGcmParams;
} else if (algo === 'AES-CBC') {
return { ...params, iv: nonce } as AesCbcParams;
} else {
throw new Error('unknown aes cipher');
}
}

function generate(algo: Algo, length: BitLength): Cipher {
const keyLength = length / 8;
const keyParams = { name: algo, length };
const cryptParams: Record<string, any> = { name: algo };
// const params: Record<string, any> = ({ e: algo, i: { name: algo, length } });

return (key: Uint8Array, nonce: Uint8Array) => {
return (key: Uint8Array, nonce: Uint8Array, AAD?: Uint8Array) => {
ensureBytes(key, keyLength);
if (algo === 'AES-CTR') {
cryptParams.counter = nonce;
cryptParams.length = 64;
} else {
cryptParams.iv = nonce;
}
const cryptParams = getCryptParams(algo, nonce, AAD);

return {
keyLength,

async encrypt(plaintext: Uint8Array): Promise<Uint8Array> {
async encrypt(plaintext: Uint8Array) {
ensureBytes(plaintext);
const cr = getWebcryptoSubtle();
const iKey = await cr.importKey('raw', key, keyParams, true, ['encrypt']);
const cipher = await cr.encrypt(cryptParams, iKey, plaintext);
return new Uint8Array(cipher);
},

async decrypt(ciphertext: Uint8Array): Promise<Uint8Array> {
async decrypt(ciphertext: Uint8Array) {
ensureBytes(ciphertext);
const cr = getWebcryptoSubtle();
const iKey = await cr.importKey('raw', key, keyParams, true, ['decrypt']);
Expand Down
2 changes: 1 addition & 1 deletion src/webcrypto/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
// See utils.ts for details.
declare const globalThis: Record<string, any> | undefined;
export const crypto =
export const crypto: Crypto =
kigawas marked this conversation as resolved.
Show resolved Hide resolved
typeof globalThis === 'object' && 'crypto' in globalThis ? globalThis.crypto : undefined;
2 changes: 1 addition & 1 deletion src/webcrypto/cryptoNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
// The file will throw on node.js 14 and earlier.
// @ts-ignore
import * as nc from 'node:crypto';
export const crypto =
export const crypto: Crypto =
nc && typeof nc === 'object' && 'webcrypto' in nc ? (nc.webcrypto as any) : undefined;
Loading