summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/internal/crypto/cipher.ts
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-02-14 17:38:45 +0100
committerGitHub <noreply@github.com>2023-02-14 17:38:45 +0100
commitd47147fb6ad229b1c039aff9d0959b6e281f4df5 (patch)
tree6e9e790f2b9bc71b5f0c9c7e64b95cae31579d58 /ext/node/polyfills/internal/crypto/cipher.ts
parent1d00bbe47e2ca14e2d2151518e02b2324461a065 (diff)
feat(ext/node): embed std/node into the snapshot (#17724)
This commit moves "deno_std/node" in "ext/node" crate. The code is transpiled and snapshotted during the build process. During the first pass a minimal amount of work was done to create the snapshot, a lot of code in "ext/node" depends on presence of "Deno" global. This code will be gradually fixed in the follow up PRs to migrate it to import relevant APIs from "internal:" modules. Currently the code from snapshot is not used in any way, and all Node/npm compatibility still uses code from "https://deno.land/std/node" (or from the location specified by "DENO_NODE_COMPAT_URL"). This will also be handled in a follow up PRs. --------- Co-authored-by: crowlkats <crowlkats@toaxl.com> Co-authored-by: Divy Srivastava <dj.srivastava23@gmail.com> Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
Diffstat (limited to 'ext/node/polyfills/internal/crypto/cipher.ts')
-rw-r--r--ext/node/polyfills/internal/crypto/cipher.ts292
1 files changed, 292 insertions, 0 deletions
diff --git a/ext/node/polyfills/internal/crypto/cipher.ts b/ext/node/polyfills/internal/crypto/cipher.ts
new file mode 100644
index 000000000..2778b40fa
--- /dev/null
+++ b/ext/node/polyfills/internal/crypto/cipher.ts
@@ -0,0 +1,292 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+// Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license.
+
+import { ERR_INVALID_ARG_TYPE } from "internal:deno_node/polyfills/internal/errors.ts";
+import {
+ validateInt32,
+ validateObject,
+} from "internal:deno_node/polyfills/internal/validators.mjs";
+import { Buffer } from "internal:deno_node/polyfills/buffer.ts";
+import { notImplemented } from "internal:deno_node/polyfills/_utils.ts";
+import type { TransformOptions } from "internal:deno_node/polyfills/_stream.d.ts";
+import { Transform } from "internal:deno_node/polyfills/_stream.mjs";
+import { KeyObject } from "internal:deno_node/polyfills/internal/crypto/keys.ts";
+import type { BufferEncoding } from "internal:deno_node/polyfills/_global.d.ts";
+import type {
+ BinaryLike,
+ Encoding,
+} from "internal:deno_node/polyfills/internal/crypto/types.ts";
+import {
+ privateDecrypt,
+ privateEncrypt,
+ publicDecrypt,
+ publicEncrypt,
+} from "internal:deno_node/polyfills/_crypto/crypto_browserify/public_encrypt/mod.js";
+
+export {
+ privateDecrypt,
+ privateEncrypt,
+ publicDecrypt,
+ publicEncrypt,
+} from "internal:deno_node/polyfills/_crypto/crypto_browserify/public_encrypt/mod.js";
+
+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: BinaryLike): Buffer;
+ update(data: string, inputEncoding: Encoding): Buffer;
+ update(
+ data: ArrayBufferView,
+ inputEncoding: undefined,
+ outputEncoding: Encoding,
+ ): string;
+ update(
+ data: string,
+ inputEncoding: Encoding | undefined,
+ outputEncoding: Encoding,
+ ): string;
+
+ final(): Buffer;
+ final(outputEncoding: BufferEncoding): string;
+
+ 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;
+}
+
+export class Cipheriv extends Transform implements Cipher {
+ constructor(
+ _cipher: string,
+ _key: CipherKey,
+ _iv: BinaryLike | null,
+ _options?: TransformOptions,
+ ) {
+ super();
+
+ notImplemented("crypto.Cipheriv");
+ }
+
+ final(): Buffer;
+ final(outputEncoding: BufferEncoding): string;
+ final(_outputEncoding?: string): Buffer | string {
+ notImplemented("crypto.Cipheriv.prototype.final");
+ }
+
+ getAuthTag(): Buffer {
+ notImplemented("crypto.Cipheriv.prototype.getAuthTag");
+ }
+
+ setAAD(
+ _buffer: ArrayBufferView,
+ _options?: {
+ plaintextLength: number;
+ },
+ ): this {
+ notImplemented("crypto.Cipheriv.prototype.setAAD");
+ }
+
+ setAutoPadding(_autoPadding?: boolean): this {
+ notImplemented("crypto.Cipheriv.prototype.setAutoPadding");
+ }
+
+ update(data: BinaryLike): Buffer;
+ update(data: string, inputEncoding: Encoding): Buffer;
+ update(
+ data: ArrayBufferView,
+ inputEncoding: undefined,
+ outputEncoding: Encoding,
+ ): string;
+ update(
+ data: string,
+ inputEncoding: Encoding | undefined,
+ outputEncoding: Encoding,
+ ): string;
+ update(
+ _data: string | BinaryLike | ArrayBufferView,
+ _inputEncoding?: Encoding,
+ _outputEncoding?: Encoding,
+ ): Buffer | string {
+ notImplemented("crypto.Cipheriv.prototype.update");
+ }
+}
+
+export class Decipheriv extends Transform implements Cipher {
+ constructor(
+ _cipher: string,
+ _key: CipherKey,
+ _iv: BinaryLike | null,
+ _options?: TransformOptions,
+ ) {
+ super();
+
+ notImplemented("crypto.Decipheriv");
+ }
+
+ final(): Buffer;
+ final(outputEncoding: BufferEncoding): string;
+ final(_outputEncoding?: string): Buffer | string {
+ notImplemented("crypto.Decipheriv.prototype.final");
+ }
+
+ 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(data: BinaryLike): Buffer;
+ update(data: string, inputEncoding: Encoding): Buffer;
+ update(
+ data: ArrayBufferView,
+ inputEncoding: undefined,
+ outputEncoding: Encoding,
+ ): string;
+ update(
+ data: string,
+ inputEncoding: Encoding | undefined,
+ outputEncoding: Encoding,
+ ): string;
+ update(
+ _data: string | BinaryLike | ArrayBufferView,
+ _inputEncoding?: Encoding,
+ _outputEncoding?: Encoding,
+ ): Buffer | string {
+ notImplemented("crypto.Decipheriv.prototype.update");
+ }
+}
+
+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");
+}
+
+export default {
+ privateDecrypt,
+ privateEncrypt,
+ publicDecrypt,
+ publicEncrypt,
+ Cipheriv,
+ Decipheriv,
+ getCipherInfo,
+};