2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-02-14 11:38:45 -05:00
|
|
|
// 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";
|
|
|
|
|
2023-11-09 12:56:59 -05:00
|
|
|
export function isStringOrBuffer(val) {
|
2023-08-11 03:34:23 -04:00
|
|
|
return typeof val === "string" ||
|
|
|
|
isArrayBufferView(val) ||
|
2023-12-02 23:28:13 -05:00
|
|
|
isAnyArrayBuffer(val) ||
|
|
|
|
Buffer.isBuffer(val);
|
2023-08-11 03:34:23 -04:00
|
|
|
}
|
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
|
|
|
|
2023-09-06 01:31:50 -04:00
|
|
|
const NO_TAG = new Uint8Array();
|
|
|
|
|
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-09-06 01:31:50 -04:00
|
|
|
#needsBlockCache: boolean;
|
|
|
|
|
|
|
|
#authTag?: Buffer;
|
|
|
|
|
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-09-06 01:31:50 -04:00
|
|
|
this.#needsBlockCache =
|
|
|
|
!(cipher == "aes-128-gcm" || cipher == "aes-256-gcm");
|
2023-08-26 01:15:37 -04:00
|
|
|
if (this.#context == 0) {
|
|
|
|
throw new TypeError("Unknown cipher");
|
|
|
|
}
|
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);
|
2023-09-06 01:31:50 -04:00
|
|
|
const maybeTag = ops.op_node_cipheriv_final(
|
|
|
|
this.#context,
|
|
|
|
this.#cache.cache,
|
|
|
|
buf,
|
|
|
|
);
|
|
|
|
if (maybeTag) {
|
|
|
|
this.#authTag = Buffer.from(maybeTag);
|
|
|
|
return encoding === "buffer" ? Buffer.from([]) : "";
|
|
|
|
}
|
2023-03-14 02:59:23 -04:00
|
|
|
return encoding === "buffer" ? buf : buf.toString(encoding);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getAuthTag(): Buffer {
|
2023-09-06 01:31:50 -04:00
|
|
|
return this.#authTag!;
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
setAAD(
|
2023-09-06 01:31:50 -04:00
|
|
|
buffer: ArrayBufferView,
|
2023-02-14 11:38:45 -05:00
|
|
|
_options?: {
|
|
|
|
plaintextLength: number;
|
|
|
|
},
|
|
|
|
): this {
|
2023-09-06 01:31:50 -04:00
|
|
|
ops.op_node_cipheriv_set_aad(this.#context, buffer);
|
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
|
2023-09-06 01:31:50 -04:00
|
|
|
let buf = data;
|
2023-03-18 08:51:28 -04:00
|
|
|
if (typeof data === "string" && typeof inputEncoding === "string") {
|
2023-09-06 01:31:50 -04:00
|
|
|
buf = Buffer.from(data, inputEncoding);
|
2023-03-18 08:51:28 -04:00
|
|
|
}
|
2023-09-06 01:31:50 -04:00
|
|
|
|
2023-03-18 08:51:28 -04:00
|
|
|
let output;
|
2023-09-06 01:31:50 -04:00
|
|
|
if (!this.#needsBlockCache) {
|
|
|
|
output = Buffer.allocUnsafe(buf.length);
|
|
|
|
ops.op_node_cipheriv_encrypt(this.#context, buf, output);
|
|
|
|
return outputEncoding === "buffer"
|
|
|
|
? output
|
|
|
|
: output.toString(outputEncoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#cache.add(buf);
|
|
|
|
const input = this.#cache.get();
|
|
|
|
|
2023-03-18 08:51:28 -04:00
|
|
|
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-09-06 01:31:50 -04:00
|
|
|
#needsBlockCache: boolean;
|
|
|
|
|
|
|
|
#authTag?: BinaryLike;
|
|
|
|
|
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-09-06 01:31:50 -04:00
|
|
|
this.#needsBlockCache =
|
|
|
|
!(cipher == "aes-128-gcm" || cipher == "aes-256-gcm");
|
2023-08-26 01:15:37 -04:00
|
|
|
if (this.#context == 0) {
|
|
|
|
throw new TypeError("Unknown cipher");
|
|
|
|
}
|
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);
|
2023-09-06 01:31:50 -04:00
|
|
|
ops.op_node_decipheriv_final(
|
|
|
|
this.#context,
|
|
|
|
this.#cache.cache,
|
|
|
|
buf,
|
|
|
|
this.#authTag || NO_TAG,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!this.#needsBlockCache) {
|
|
|
|
return encoding === "buffer" ? Buffer.from([]) : "";
|
|
|
|
}
|
|
|
|
|
2023-03-18 08:51:28 -04:00
|
|
|
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(
|
2023-09-06 01:31:50 -04:00
|
|
|
buffer: ArrayBufferView,
|
2023-02-14 11:38:45 -05:00
|
|
|
_options?: {
|
|
|
|
plaintextLength: number;
|
|
|
|
},
|
|
|
|
): this {
|
2023-09-06 01:31:50 -04:00
|
|
|
ops.op_node_decipheriv_set_aad(this.#context, buffer);
|
|
|
|
return this;
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2023-09-06 01:31:50 -04:00
|
|
|
setAuthTag(buffer: BinaryLike, _encoding?: string): this {
|
|
|
|
this.#authTag = buffer;
|
|
|
|
return this;
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2023-09-06 01:31:50 -04:00
|
|
|
let buf = data;
|
2023-03-18 08:51:28 -04:00
|
|
|
if (typeof data === "string" && typeof inputEncoding === "string") {
|
2023-09-06 01:31:50 -04:00
|
|
|
buf = Buffer.from(data, inputEncoding);
|
2023-03-18 08:51:28 -04:00
|
|
|
}
|
2023-09-06 01:31:50 -04:00
|
|
|
|
2023-03-18 08:51:28 -04:00
|
|
|
let output;
|
2023-09-06 01:31:50 -04:00
|
|
|
if (!this.#needsBlockCache) {
|
|
|
|
output = Buffer.allocUnsafe(buf.length);
|
|
|
|
ops.op_node_decipheriv_decrypt(this.#context, buf, output);
|
|
|
|
return outputEncoding === "buffer"
|
|
|
|
? output
|
|
|
|
: output.toString(outputEncoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#cache.add(buf);
|
|
|
|
const input = this.#cache.get();
|
2023-03-18 08:51:28 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-11-09 12:56:59 -05:00
|
|
|
export function prepareKey(key) {
|
2023-08-11 03:34:23 -04:00
|
|
|
// 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,
|
2023-11-09 12:56:59 -05:00
|
|
|
prepareKey,
|
2023-02-14 11:38:45 -05:00
|
|
|
getCipherInfo,
|
|
|
|
};
|