2023-02-14 11:38:45 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
// Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license.
|
|
|
|
|
2023-06-27 02:18:22 -04:00
|
|
|
// TODO(petamoriken): enable prefer-primordials for node polyfills
|
|
|
|
// deno-lint-ignore-file prefer-primordials
|
|
|
|
|
2023-03-08 06:44:54 -05:00
|
|
|
import { ERR_INVALID_ARG_TYPE } from "ext:deno_node/internal/errors.ts";
|
2023-02-14 11:38:45 -05:00
|
|
|
import {
|
|
|
|
validateInt32,
|
|
|
|
validateObject,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_node/internal/validators.mjs";
|
2023-07-02 14:19:30 -04:00
|
|
|
import { Buffer } from "node:buffer";
|
2023-03-08 06:44:54 -05:00
|
|
|
import { notImplemented } from "ext:deno_node/_utils.ts";
|
|
|
|
import type { TransformOptions } from "ext:deno_node/_stream.d.ts";
|
|
|
|
import { Transform } from "ext:deno_node/_stream.mjs";
|
2023-08-11 03:34:23 -04:00
|
|
|
import {
|
|
|
|
getArrayBufferOrView,
|
|
|
|
KeyObject,
|
|
|
|
} from "ext:deno_node/internal/crypto/keys.ts";
|
2023-03-08 06:44:54 -05:00
|
|
|
import type { BufferEncoding } from "ext:deno_node/_global.d.ts";
|
2023-02-14 11:38:45 -05:00
|
|
|
import type {
|
|
|
|
BinaryLike,
|
|
|
|
Encoding,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_node/internal/crypto/types.ts";
|
2023-03-14 02:59:23 -04:00
|
|
|
import { getDefaultEncoding } from "ext:deno_node/internal/crypto/util.ts";
|
2023-08-11 03:34:23 -04:00
|
|
|
import {
|
|
|
|
isAnyArrayBuffer,
|
|
|
|
isArrayBufferView,
|
|
|
|
} from "ext:deno_node/internal/util/types.ts";
|
|
|
|
|
|
|
|
function isStringOrBuffer(val) {
|
|
|
|
return typeof val === "string" ||
|
|
|
|
isArrayBufferView(val) ||
|
|
|
|
isAnyArrayBuffer(val);
|
|
|
|
}
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2023-03-25 02:42:07 -04:00
|
|
|
const { ops, encode } = globalThis.__bootstrap.core;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
export type CipherCCMTypes =
|
|
|
|
| "aes-128-ccm"
|
|
|
|
| "aes-192-ccm"
|
|
|
|
| "aes-256-ccm"
|
|
|
|
| "chacha20-poly1305";
|
|
|
|
export type CipherGCMTypes = "aes-128-gcm" | "aes-192-gcm" | "aes-256-gcm";
|
|
|
|
export type CipherOCBTypes = "aes-128-ocb" | "aes-192-ocb" | "aes-256-ocb";
|
|
|
|
|
|
|
|
export type CipherKey = BinaryLike | KeyObject;
|
|
|
|
|
|
|
|
export interface CipherCCMOptions extends TransformOptions {
|
|
|
|
authTagLength: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface CipherGCMOptions extends TransformOptions {
|
|
|
|
authTagLength?: number | undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface CipherOCBOptions extends TransformOptions {
|
|
|
|
authTagLength: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Cipher extends ReturnType<typeof Transform> {
|
|
|
|
update(
|
|
|
|
data: string,
|
2023-03-14 02:59:23 -04:00
|
|
|
inputEncoding?: Encoding,
|
|
|
|
outputEncoding?: Encoding,
|
2023-02-14 11:38:45 -05:00
|
|
|
): string;
|
|
|
|
|
2023-03-14 02:59:23 -04:00
|
|
|
final(outputEncoding?: BufferEncoding): string;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
setAutoPadding(autoPadding?: boolean): this;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Decipher = Cipher;
|
|
|
|
|
|
|
|
export interface CipherCCM extends Cipher {
|
|
|
|
setAAD(
|
|
|
|
buffer: ArrayBufferView,
|
|
|
|
options: {
|
|
|
|
plaintextLength: number;
|
|
|
|
},
|
|
|
|
): this;
|
|
|
|
getAuthTag(): Buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface CipherGCM extends Cipher {
|
|
|
|
setAAD(
|
|
|
|
buffer: ArrayBufferView,
|
|
|
|
options?: {
|
|
|
|
plaintextLength: number;
|
|
|
|
},
|
|
|
|
): this;
|
|
|
|
getAuthTag(): Buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface CipherOCB extends Cipher {
|
|
|
|
setAAD(
|
|
|
|
buffer: ArrayBufferView,
|
|
|
|
options?: {
|
|
|
|
plaintextLength: number;
|
|
|
|
},
|
|
|
|
): this;
|
|
|
|
getAuthTag(): Buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface DecipherCCM extends Decipher {
|
|
|
|
setAuthTag(buffer: ArrayBufferView): this;
|
|
|
|
setAAD(
|
|
|
|
buffer: ArrayBufferView,
|
|
|
|
options: {
|
|
|
|
plaintextLength: number;
|
|
|
|
},
|
|
|
|
): this;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface DecipherGCM extends Decipher {
|
|
|
|
setAuthTag(buffer: ArrayBufferView): this;
|
|
|
|
setAAD(
|
|
|
|
buffer: ArrayBufferView,
|
|
|
|
options?: {
|
|
|
|
plaintextLength: number;
|
|
|
|
},
|
|
|
|
): this;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface DecipherOCB extends Decipher {
|
|
|
|
setAuthTag(buffer: ArrayBufferView): this;
|
|
|
|
setAAD(
|
|
|
|
buffer: ArrayBufferView,
|
|
|
|
options?: {
|
|
|
|
plaintextLength: number;
|
|
|
|
},
|
|
|
|
): this;
|
|
|
|
}
|
|
|
|
|
2023-03-25 02:42:07 -04:00
|
|
|
function toU8(input: string | Uint8Array): Uint8Array {
|
|
|
|
return typeof input === "string" ? encode(input) : input;
|
|
|
|
}
|
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
export class Cipheriv extends Transform implements Cipher {
|
2023-03-14 02:59:23 -04:00
|
|
|
/** CipherContext resource id */
|
|
|
|
#context: number;
|
|
|
|
|
|
|
|
/** plaintext data cache */
|
|
|
|
#cache: BlockModeCache;
|
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
constructor(
|
2023-03-14 02:59:23 -04:00
|
|
|
cipher: string,
|
|
|
|
key: CipherKey,
|
|
|
|
iv: BinaryLike | null,
|
|
|
|
options?: TransformOptions,
|
2023-02-14 11:38:45 -05:00
|
|
|
) {
|
2023-03-24 09:29:14 -04:00
|
|
|
super({
|
|
|
|
transform(chunk, encoding, cb) {
|
|
|
|
this.push(this.update(chunk, encoding));
|
|
|
|
cb();
|
|
|
|
},
|
|
|
|
final(cb) {
|
|
|
|
this.push(this.final());
|
|
|
|
cb();
|
|
|
|
},
|
|
|
|
...options,
|
|
|
|
});
|
2023-03-18 08:51:28 -04:00
|
|
|
this.#cache = new BlockModeCache(false);
|
2023-03-25 02:42:07 -04:00
|
|
|
this.#context = ops.op_node_create_cipheriv(cipher, toU8(key), toU8(iv));
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2023-03-14 02:59:23 -04:00
|
|
|
final(encoding: string = getDefaultEncoding()): Buffer | string {
|
|
|
|
const buf = new Buffer(16);
|
|
|
|
ops.op_node_cipheriv_final(this.#context, this.#cache.cache, buf);
|
|
|
|
return encoding === "buffer" ? buf : buf.toString(encoding);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getAuthTag(): Buffer {
|
|
|
|
notImplemented("crypto.Cipheriv.prototype.getAuthTag");
|
|
|
|
}
|
|
|
|
|
|
|
|
setAAD(
|
|
|
|
_buffer: ArrayBufferView,
|
|
|
|
_options?: {
|
|
|
|
plaintextLength: number;
|
|
|
|
},
|
|
|
|
): this {
|
|
|
|
notImplemented("crypto.Cipheriv.prototype.setAAD");
|
2023-03-14 02:59:23 -04:00
|
|
|
return this;
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
setAutoPadding(_autoPadding?: boolean): this {
|
|
|
|
notImplemented("crypto.Cipheriv.prototype.setAutoPadding");
|
2023-03-14 02:59:23 -04:00
|
|
|
return this;
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
update(
|
2023-03-14 02:59:23 -04:00
|
|
|
data: string | Buffer | ArrayBufferView,
|
2023-03-18 08:51:28 -04:00
|
|
|
inputEncoding?: Encoding,
|
2023-03-14 02:59:23 -04:00
|
|
|
outputEncoding: Encoding = getDefaultEncoding(),
|
2023-02-14 11:38:45 -05:00
|
|
|
): Buffer | string {
|
2023-03-18 08:51:28 -04:00
|
|
|
// TODO(kt3k): throw ERR_INVALID_ARG_TYPE if data is not string, Buffer, or ArrayBufferView
|
|
|
|
if (typeof data === "string" && typeof inputEncoding === "string") {
|
|
|
|
this.#cache.add(Buffer.from(data, inputEncoding));
|
|
|
|
} else {
|
|
|
|
this.#cache.add(data);
|
|
|
|
}
|
2023-03-14 02:59:23 -04:00
|
|
|
const input = this.#cache.get();
|
2023-03-18 08:51:28 -04:00
|
|
|
let output;
|
|
|
|
if (input === null) {
|
|
|
|
output = Buffer.alloc(0);
|
|
|
|
} else {
|
|
|
|
output = Buffer.allocUnsafe(input.length);
|
|
|
|
ops.op_node_cipheriv_encrypt(this.#context, input, output);
|
|
|
|
}
|
2023-03-14 02:59:23 -04:00
|
|
|
return outputEncoding === "buffer"
|
|
|
|
? output
|
|
|
|
: output.toString(outputEncoding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Caches data and output the chunk of multiple of 16.
|
|
|
|
* Used by CBC, ECB modes of block ciphers */
|
|
|
|
class BlockModeCache {
|
2023-03-18 08:51:28 -04:00
|
|
|
cache: Uint8Array;
|
|
|
|
// The last chunk can be padded when decrypting.
|
|
|
|
#lastChunkIsNonZero: boolean;
|
|
|
|
|
|
|
|
constructor(lastChunkIsNotZero = false) {
|
2023-03-14 02:59:23 -04:00
|
|
|
this.cache = new Uint8Array(0);
|
2023-03-18 08:51:28 -04:00
|
|
|
this.#lastChunkIsNonZero = lastChunkIsNotZero;
|
2023-03-14 02:59:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
add(data: Uint8Array) {
|
|
|
|
const cache = this.cache;
|
|
|
|
this.cache = new Uint8Array(cache.length + data.length);
|
|
|
|
this.cache.set(cache);
|
|
|
|
this.cache.set(data, cache.length);
|
|
|
|
}
|
|
|
|
|
2023-03-18 08:51:28 -04:00
|
|
|
/** Gets the chunk of the length of largest multiple of 16.
|
|
|
|
* Used for preparing data for encryption/decryption */
|
|
|
|
get(): Uint8Array | null {
|
|
|
|
let len = this.cache.length;
|
|
|
|
if (this.#lastChunkIsNonZero) {
|
|
|
|
// Reduces the available chunk length by 1 to keep the last chunk
|
|
|
|
len -= 1;
|
|
|
|
}
|
|
|
|
if (len < 16) {
|
2023-03-14 02:59:23 -04:00
|
|
|
return null;
|
|
|
|
}
|
2023-03-18 08:51:28 -04:00
|
|
|
|
|
|
|
len = Math.floor(len / 16) * 16;
|
2023-03-14 02:59:23 -04:00
|
|
|
const out = this.cache.subarray(0, len);
|
|
|
|
this.cache = this.cache.subarray(len);
|
|
|
|
return out;
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Decipheriv extends Transform implements Cipher {
|
2023-03-18 08:51:28 -04:00
|
|
|
/** DecipherContext resource id */
|
|
|
|
#context: number;
|
|
|
|
|
|
|
|
/** ciphertext data cache */
|
|
|
|
#cache: BlockModeCache;
|
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
constructor(
|
2023-03-18 08:51:28 -04:00
|
|
|
cipher: string,
|
|
|
|
key: CipherKey,
|
|
|
|
iv: BinaryLike | null,
|
|
|
|
options?: TransformOptions,
|
2023-02-14 11:38:45 -05:00
|
|
|
) {
|
2023-03-24 09:29:14 -04:00
|
|
|
super({
|
|
|
|
transform(chunk, encoding, cb) {
|
|
|
|
this.push(this.update(chunk, encoding));
|
|
|
|
cb();
|
|
|
|
},
|
|
|
|
final(cb) {
|
|
|
|
this.push(this.final());
|
|
|
|
cb();
|
|
|
|
},
|
|
|
|
...options,
|
|
|
|
});
|
2023-03-18 08:51:28 -04:00
|
|
|
this.#cache = new BlockModeCache(true);
|
2023-03-25 02:42:07 -04:00
|
|
|
this.#context = ops.op_node_create_decipheriv(cipher, toU8(key), toU8(iv));
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2023-03-18 08:51:28 -04:00
|
|
|
final(encoding: string = getDefaultEncoding()): Buffer | string {
|
|
|
|
let buf = new Buffer(16);
|
|
|
|
ops.op_node_decipheriv_final(this.#context, this.#cache.cache, buf);
|
|
|
|
buf = buf.subarray(0, 16 - buf.at(-1)); // Padded in Pkcs7 mode
|
|
|
|
return encoding === "buffer" ? buf : buf.toString(encoding);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
setAAD(
|
|
|
|
_buffer: ArrayBufferView,
|
|
|
|
_options?: {
|
|
|
|
plaintextLength: number;
|
|
|
|
},
|
|
|
|
): this {
|
|
|
|
notImplemented("crypto.Decipheriv.prototype.setAAD");
|
|
|
|
}
|
|
|
|
|
|
|
|
setAuthTag(_buffer: BinaryLike, _encoding?: string): this {
|
|
|
|
notImplemented("crypto.Decipheriv.prototype.setAuthTag");
|
|
|
|
}
|
|
|
|
|
|
|
|
setAutoPadding(_autoPadding?: boolean): this {
|
|
|
|
notImplemented("crypto.Decipheriv.prototype.setAutoPadding");
|
|
|
|
}
|
|
|
|
|
|
|
|
update(
|
2023-03-18 08:51:28 -04:00
|
|
|
data: string | Buffer | ArrayBufferView,
|
|
|
|
inputEncoding?: Encoding,
|
|
|
|
outputEncoding: Encoding = getDefaultEncoding(),
|
2023-02-14 11:38:45 -05:00
|
|
|
): Buffer | string {
|
2023-03-18 08:51:28 -04:00
|
|
|
// TODO(kt3k): throw ERR_INVALID_ARG_TYPE if data is not string, Buffer, or ArrayBufferView
|
|
|
|
if (typeof data === "string" && typeof inputEncoding === "string") {
|
|
|
|
this.#cache.add(Buffer.from(data, inputEncoding));
|
|
|
|
} else {
|
|
|
|
this.#cache.add(data);
|
|
|
|
}
|
|
|
|
const input = this.#cache.get();
|
|
|
|
let output;
|
|
|
|
if (input === null) {
|
|
|
|
output = Buffer.alloc(0);
|
|
|
|
} else {
|
|
|
|
output = new Buffer(input.length);
|
|
|
|
ops.op_node_decipheriv_decrypt(this.#context, input, output);
|
|
|
|
}
|
|
|
|
return outputEncoding === "buffer"
|
|
|
|
? output
|
|
|
|
: output.toString(outputEncoding);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCipherInfo(
|
|
|
|
nameOrNid: string | number,
|
|
|
|
options?: { keyLength?: number; ivLength?: number },
|
|
|
|
) {
|
|
|
|
if (typeof nameOrNid !== "string" && typeof nameOrNid !== "number") {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
"nameOrNid",
|
|
|
|
["string", "number"],
|
|
|
|
nameOrNid,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof nameOrNid === "number") {
|
|
|
|
validateInt32(nameOrNid, "nameOrNid");
|
|
|
|
}
|
|
|
|
|
|
|
|
let keyLength, ivLength;
|
|
|
|
|
|
|
|
if (options !== undefined) {
|
|
|
|
validateObject(options, "options");
|
|
|
|
|
|
|
|
({ keyLength, ivLength } = options);
|
|
|
|
|
|
|
|
if (keyLength !== undefined) {
|
|
|
|
validateInt32(keyLength, "options.keyLength");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ivLength !== undefined) {
|
|
|
|
validateInt32(ivLength, "options.ivLength");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notImplemented("crypto.getCipherInfo");
|
|
|
|
}
|
|
|
|
|
2023-02-20 11:52:28 -05:00
|
|
|
export function privateEncrypt(
|
|
|
|
privateKey: ArrayBufferView | string | KeyObject,
|
|
|
|
buffer: ArrayBufferView | string | KeyObject,
|
|
|
|
): Buffer {
|
2023-08-11 03:34:23 -04:00
|
|
|
const { data } = prepareKey(privateKey);
|
2023-02-20 11:52:28 -05:00
|
|
|
const padding = privateKey.padding || 1;
|
2023-08-11 03:34:23 -04:00
|
|
|
|
|
|
|
buffer = getArrayBufferOrView(buffer, "buffer");
|
|
|
|
return ops.op_node_private_encrypt(data, buffer, padding);
|
2023-02-20 11:52:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function privateDecrypt(
|
|
|
|
privateKey: ArrayBufferView | string | KeyObject,
|
|
|
|
buffer: ArrayBufferView | string | KeyObject,
|
|
|
|
): Buffer {
|
2023-08-11 03:34:23 -04:00
|
|
|
const { data } = prepareKey(privateKey);
|
2023-02-20 11:52:28 -05:00
|
|
|
const padding = privateKey.padding || 1;
|
2023-08-11 03:34:23 -04:00
|
|
|
|
|
|
|
buffer = getArrayBufferOrView(buffer, "buffer");
|
|
|
|
return ops.op_node_private_decrypt(data, buffer, padding);
|
2023-02-20 11:52:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function publicEncrypt(
|
|
|
|
publicKey: ArrayBufferView | string | KeyObject,
|
|
|
|
buffer: ArrayBufferView | string | KeyObject,
|
|
|
|
): Buffer {
|
2023-08-11 03:34:23 -04:00
|
|
|
const { data } = prepareKey(publicKey);
|
2023-02-20 11:52:28 -05:00
|
|
|
const padding = publicKey.padding || 1;
|
2023-08-11 03:34:23 -04:00
|
|
|
|
|
|
|
buffer = getArrayBufferOrView(buffer, "buffer");
|
|
|
|
return ops.op_node_public_encrypt(data, buffer, padding);
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareKey(key) {
|
|
|
|
// TODO(@littledivy): handle these cases
|
|
|
|
// - node KeyObject
|
|
|
|
// - web CryptoKey
|
|
|
|
if (isStringOrBuffer(key)) {
|
|
|
|
return { data: getArrayBufferOrView(key, "key") };
|
|
|
|
} else if (typeof key == "object") {
|
|
|
|
const { key: data, encoding } = key;
|
|
|
|
if (!isStringOrBuffer(data)) {
|
|
|
|
throw new TypeError("Invalid key type");
|
|
|
|
}
|
|
|
|
|
|
|
|
return { data: getArrayBufferOrView(data, "key", encoding) };
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new TypeError("Invalid key type");
|
2023-02-20 11:52:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function publicDecrypt() {
|
|
|
|
notImplemented("crypto.publicDecrypt");
|
|
|
|
}
|
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
export default {
|
|
|
|
privateDecrypt,
|
|
|
|
privateEncrypt,
|
|
|
|
publicDecrypt,
|
|
|
|
publicEncrypt,
|
|
|
|
Cipheriv,
|
|
|
|
Decipheriv,
|
|
|
|
getCipherInfo,
|
|
|
|
};
|