diff options
| author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-02-14 17:38:45 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-14 17:38:45 +0100 |
| commit | d47147fb6ad229b1c039aff9d0959b6e281f4df5 (patch) | |
| tree | 6e9e790f2b9bc71b5f0c9c7e64b95cae31579d58 /ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes | |
| parent | 1d00bbe47e2ca14e2d2151518e02b2324461a065 (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/_crypto/crypto_browserify/browserify_aes/modes')
8 files changed, 430 insertions, 0 deletions
diff --git a/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/cbc.js b/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/cbc.js new file mode 100644 index 000000000..837adf32f --- /dev/null +++ b/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/cbc.js @@ -0,0 +1,22 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +// Copyright 2014-2017 browserify-aes contributors. All rights reserved. MIT license. +// Copyright 2013 Maxwell Krohn. All rights reserved. MIT license. +// Copyright 2009-2013 Jeff Mott. All rights reserved. MIT license. + +import { xor } from "internal:deno_node/polyfills/_crypto/crypto_browserify/browserify_aes/xor.ts"; + +export const encrypt = function (self, block) { + const data = xor(block, self._prev); + + self._prev = self._cipher.encryptBlock(data); + return self._prev; +}; + +export const decrypt = function (self, block) { + const pad = self._prev; + + self._prev = block; + const out = self._cipher.decryptBlock(block); + + return xor(out, pad); +}; diff --git a/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/cfb.js b/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/cfb.js new file mode 100644 index 000000000..8bfd81463 --- /dev/null +++ b/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/cfb.js @@ -0,0 +1,41 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +// Copyright 2014-2017 browserify-aes contributors. All rights reserved. MIT license. +// Copyright 2013 Maxwell Krohn. All rights reserved. MIT license. +// Copyright 2009-2013 Jeff Mott. All rights reserved. MIT license. + +import { xor } from "internal:deno_node/polyfills/_crypto/crypto_browserify/browserify_aes/xor.ts"; +import { Buffer } from "internal:deno_node/polyfills/buffer.ts"; + +function encryptStart(self, data, decrypt) { + const len = data.length; + const out = xor(data, self._cache); + self._cache = self._cache.slice(len); + self._prev = Buffer.concat([self._prev, decrypt ? data : out]); + return out; +} + +export const encrypt = function (self, data, decrypt) { + let out = Buffer.allocUnsafe(0); + let len; + + while (data.length) { + if (self._cache.length === 0) { + self._cache = self._cipher.encryptBlock(self._prev); + self._prev = Buffer.allocUnsafe(0); + } + + if (self._cache.length <= data.length) { + len = self._cache.length; + out = Buffer.concat([ + out, + encryptStart(self, data.slice(0, len), decrypt), + ]); + data = data.slice(len); + } else { + out = Buffer.concat([out, encryptStart(self, data, decrypt)]); + break; + } + } + + return out; +}; diff --git a/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/cfb1.js b/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/cfb1.js new file mode 100644 index 000000000..2af8824f0 --- /dev/null +++ b/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/cfb1.js @@ -0,0 +1,47 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +// Copyright 2014-2017 browserify-aes contributors. All rights reserved. MIT license. +// Copyright 2013 Maxwell Krohn. All rights reserved. MIT license. +// Copyright 2009-2013 Jeff Mott. All rights reserved. MIT license. + +import { Buffer } from "internal:deno_node/polyfills/buffer.ts"; + +function encryptByte(self, byteParam, decrypt) { + let pad; + let i = -1; + const len = 8; + let out = 0; + let bit, value; + while (++i < len) { + pad = self._cipher.encryptBlock(self._prev); + bit = (byteParam & (1 << (7 - i))) ? 0x80 : 0; + value = pad[0] ^ bit; + out += (value & 0x80) >> (i % 8); + self._prev = shiftIn(self._prev, decrypt ? bit : value); + } + return out; +} + +function shiftIn(buffer, value) { + const len = buffer.length; + let i = -1; + const out = Buffer.allocUnsafe(buffer.length); + buffer = Buffer.concat([buffer, Buffer.from([value])]); + + while (++i < len) { + out[i] = buffer[i] << 1 | buffer[i + 1] >> (7); + } + + return out; +} + +export const encrypt = function (self, chunk, decrypt) { + const len = chunk.length; + const out = Buffer.allocUnsafe(len); + let i = -1; + + while (++i < len) { + out[i] = encryptByte(self, chunk[i], decrypt); + } + + return out; +}; diff --git a/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/cfb8.js b/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/cfb8.js new file mode 100644 index 000000000..262494a81 --- /dev/null +++ b/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/cfb8.js @@ -0,0 +1,30 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +// Copyright 2014-2017 browserify-aes contributors. All rights reserved. MIT license. +// Copyright 2013 Maxwell Krohn. All rights reserved. MIT license. +// Copyright 2009-2013 Jeff Mott. All rights reserved. MIT license. + +import { Buffer } from "internal:deno_node/polyfills/buffer.ts"; + +function encryptByte(self, byteParam, decrypt) { + const pad = self._cipher.encryptBlock(self._prev); + const out = pad[0] ^ byteParam; + + self._prev = Buffer.concat([ + self._prev.slice(1), + Buffer.from([decrypt ? byteParam : out]), + ]); + + return out; +} + +export const encrypt = function (self, chunk, decrypt) { + const len = chunk.length; + const out = Buffer.allocUnsafe(len); + let i = -1; + + while (++i < len) { + out[i] = encryptByte(self, chunk[i], decrypt); + } + + return out; +}; diff --git a/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/ctr.js b/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/ctr.js new file mode 100644 index 000000000..fd1781ab2 --- /dev/null +++ b/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/ctr.js @@ -0,0 +1,35 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +// Copyright 2014-2017 browserify-aes contributors. All rights reserved. MIT license. +// Copyright 2013 Maxwell Krohn. All rights reserved. MIT license. +// Copyright 2009-2013 Jeff Mott. All rights reserved. MIT license. + +import { xor } from "internal:deno_node/polyfills/_crypto/crypto_browserify/browserify_aes/xor.ts"; +import { Buffer } from "internal:deno_node/polyfills/buffer.ts"; +import { incr32 } from "internal:deno_node/polyfills/_crypto/crypto_browserify/browserify_aes/incr32.js"; + +function getBlock(self) { + const out = self._cipher.encryptBlockRaw(self._prev); + incr32(self._prev); + return out; +} + +const blockSize = 16; +export const encrypt = function (self, chunk) { + const chunkNum = Math.ceil(chunk.length / blockSize); + const start = self._cache.length; + self._cache = Buffer.concat([ + self._cache, + Buffer.allocUnsafe(chunkNum * blockSize), + ]); + for (let i = 0; i < chunkNum; i++) { + const out = getBlock(self); + const offset = start + i * blockSize; + self._cache.writeUInt32BE(out[0], offset + 0); + self._cache.writeUInt32BE(out[1], offset + 4); + self._cache.writeUInt32BE(out[2], offset + 8); + self._cache.writeUInt32BE(out[3], offset + 12); + } + const pad = self._cache.slice(0, chunk.length); + self._cache = self._cache.slice(chunk.length); + return xor(chunk, pad); +}; diff --git a/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/ecb.js b/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/ecb.js new file mode 100644 index 000000000..b4f99cffb --- /dev/null +++ b/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/ecb.js @@ -0,0 +1,12 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +// Copyright 2014-2017 browserify-aes contributors. All rights reserved. MIT license. +// Copyright 2013 Maxwell Krohn. All rights reserved. MIT license. +// Copyright 2009-2013 Jeff Mott. All rights reserved. MIT license. + +export const encrypt = function (self, block) { + return self._cipher.encryptBlock(block); +}; + +export const decrypt = function (self, block) { + return self._cipher.decryptBlock(block); +}; diff --git a/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/mod.js b/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/mod.js new file mode 100644 index 000000000..b30ed0b25 --- /dev/null +++ b/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/mod.js @@ -0,0 +1,221 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +// Copyright 2014-2017 browserify-aes contributors. All rights reserved. MIT license. +// Copyright 2013 Maxwell Krohn. All rights reserved. MIT license. +// Copyright 2009-2013 Jeff Mott. All rights reserved. MIT license. + +import * as ECB from "internal:deno_node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/ecb.js"; +import * as CBC from "internal:deno_node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/cbc.js"; +import * as CFB from "internal:deno_node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/cfb.js"; +import * as CFB8 from "internal:deno_node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/cfb8.js"; +import * as CFB1 from "internal:deno_node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/cfb1.js"; +import * as OFB from "internal:deno_node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/ofb.js"; +import * as CTR from "internal:deno_node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/ctr.js"; + +const GCM = CTR; + +const modeModules = { + ECB, + CBC, + CFB, + CFB8, + CFB1, + OFB, + CTR, + GCM, +}; + +export const MODES = { + "aes-128-ecb": { + "cipher": "AES", + "key": 128, + "iv": 0, + "mode": "ECB", + "type": "block", + }, + "aes-192-ecb": { + "cipher": "AES", + "key": 192, + "iv": 0, + "mode": "ECB", + "type": "block", + }, + "aes-256-ecb": { + "cipher": "AES", + "key": 256, + "iv": 0, + "mode": "ECB", + "type": "block", + }, + "aes-128-cbc": { + "cipher": "AES", + "key": 128, + "iv": 16, + "mode": "CBC", + "type": "block", + }, + "aes-192-cbc": { + "cipher": "AES", + "key": 192, + "iv": 16, + "mode": "CBC", + "type": "block", + }, + "aes-256-cbc": { + "cipher": "AES", + "key": 256, + "iv": 16, + "mode": "CBC", + "type": "block", + }, + "aes128": { + "cipher": "AES", + "key": 128, + "iv": 16, + "mode": "CBC", + "type": "block", + }, + "aes192": { + "cipher": "AES", + "key": 192, + "iv": 16, + "mode": "CBC", + "type": "block", + }, + "aes256": { + "cipher": "AES", + "key": 256, + "iv": 16, + "mode": "CBC", + "type": "block", + }, + "aes-128-cfb": { + "cipher": "AES", + "key": 128, + "iv": 16, + "mode": "CFB", + "type": "stream", + }, + "aes-192-cfb": { + "cipher": "AES", + "key": 192, + "iv": 16, + "mode": "CFB", + "type": "stream", + }, + "aes-256-cfb": { + "cipher": "AES", + "key": 256, + "iv": 16, + "mode": "CFB", + "type": "stream", + }, + "aes-128-cfb8": { + "cipher": "AES", + "key": 128, + "iv": 16, + "mode": "CFB8", + "type": "stream", + }, + "aes-192-cfb8": { + "cipher": "AES", + "key": 192, + "iv": 16, + "mode": "CFB8", + "type": "stream", + }, + "aes-256-cfb8": { + "cipher": "AES", + "key": 256, + "iv": 16, + "mode": "CFB8", + "type": "stream", + }, + "aes-128-cfb1": { + "cipher": "AES", + "key": 128, + "iv": 16, + "mode": "CFB1", + "type": "stream", + }, + "aes-192-cfb1": { + "cipher": "AES", + "key": 192, + "iv": 16, + "mode": "CFB1", + "type": "stream", + }, + "aes-256-cfb1": { + "cipher": "AES", + "key": 256, + "iv": 16, + "mode": "CFB1", + "type": "stream", + }, + "aes-128-ofb": { + "cipher": "AES", + "key": 128, + "iv": 16, + "mode": "OFB", + "type": "stream", + }, + "aes-192-ofb": { + "cipher": "AES", + "key": 192, + "iv": 16, + "mode": "OFB", + "type": "stream", + }, + "aes-256-ofb": { + "cipher": "AES", + "key": 256, + "iv": 16, + "mode": "OFB", + "type": "stream", + }, + "aes-128-ctr": { + "cipher": "AES", + "key": 128, + "iv": 16, + "mode": "CTR", + "type": "stream", + }, + "aes-192-ctr": { + "cipher": "AES", + "key": 192, + "iv": 16, + "mode": "CTR", + "type": "stream", + }, + "aes-256-ctr": { + "cipher": "AES", + "key": 256, + "iv": 16, + "mode": "CTR", + "type": "stream", + }, + "aes-128-gcm": { + "cipher": "AES", + "key": 128, + "iv": 12, + "mode": "GCM", + "type": "auth", + }, + "aes-192-gcm": { + "cipher": "AES", + "key": 192, + "iv": 12, + "mode": "GCM", + "type": "auth", + }, + "aes-256-gcm": { + "cipher": "AES", + "key": 256, + "iv": 12, + "mode": "GCM", + "type": "auth", + }, +}; + +for (const mode of Object.values(MODES)) { + mode.module = modeModules[mode.mode]; +} diff --git a/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/ofb.js b/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/ofb.js new file mode 100644 index 000000000..20ccdf015 --- /dev/null +++ b/ext/node/polyfills/_crypto/crypto_browserify/browserify_aes/modes/ofb.js @@ -0,0 +1,22 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +// Copyright 2014-2017 browserify-aes contributors. All rights reserved. MIT license. +// Copyright 2013 Maxwell Krohn. All rights reserved. MIT license. +// Copyright 2009-2013 Jeff Mott. All rights reserved. MIT license. + +import { xor } from "internal:deno_node/polyfills/_crypto/crypto_browserify/browserify_aes/xor.ts"; +import { Buffer } from "internal:deno_node/polyfills/buffer.ts"; + +function getBlock(self) { + self._prev = self._cipher.encryptBlock(self._prev); + return self._prev; +} + +export const encrypt = function (self, chunk) { + while (self._cache.length < chunk.length) { + self._cache = Buffer.concat([self._cache, getBlock(self)]); + } + + const pad = self._cache.slice(0, chunk.length); + self._cache = self._cache.slice(chunk.length); + return xor(chunk, pad); +}; |
