@cloudpss/crypto 0.5.25 → 0.5.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/benchmark.js +2 -2
  2. package/dist/encryption/browser.js +1 -2
  3. package/dist/encryption/browser.js.map +1 -1
  4. package/dist/encryption/common.d.ts +2 -2
  5. package/dist/encryption/common.js +6 -4
  6. package/dist/encryption/common.js.map +1 -1
  7. package/dist/encryption/index.js +4 -1
  8. package/dist/encryption/index.js.map +1 -1
  9. package/dist/encryption/module.d.ts +1 -1
  10. package/dist/encryption/module.js +2 -2
  11. package/dist/encryption/module.js.map +1 -1
  12. package/dist/encryption/node.js +4 -3
  13. package/dist/encryption/node.js.map +1 -1
  14. package/dist/encryption/{pure-js.d.ts → wasm.d.ts} +2 -2
  15. package/dist/encryption/wasm.js +22 -0
  16. package/dist/encryption/wasm.js.map +1 -0
  17. package/dist/encryption/web.js +5 -1
  18. package/dist/encryption/web.js.map +1 -1
  19. package/dist/utils.d.ts +2 -0
  20. package/dist/utils.js +2 -0
  21. package/dist/utils.js.map +1 -1
  22. package/lib/wasm.d.ts +26 -0
  23. package/lib/wasm.js +149 -0
  24. package/package.json +10 -12
  25. package/src/encryption/browser.ts +1 -2
  26. package/src/encryption/common.ts +10 -6
  27. package/src/encryption/index.ts +3 -1
  28. package/src/encryption/module.ts +4 -4
  29. package/src/encryption/node.ts +4 -3
  30. package/src/encryption/wasm.ts +47 -0
  31. package/src/encryption/web.ts +5 -1
  32. package/src/utils.ts +3 -0
  33. package/tests/encryption.js +53 -32
  34. package/tsconfig.json +2 -1
  35. package/wasm-build.js +30 -0
  36. package/dist/encryption/js/aes.d.ts +0 -20
  37. package/dist/encryption/js/aes.js +0 -151
  38. package/dist/encryption/js/aes.js.map +0 -1
  39. package/dist/encryption/js/gcm.d.ts +0 -26
  40. package/dist/encryption/js/gcm.js +0 -226
  41. package/dist/encryption/js/gcm.js.map +0 -1
  42. package/dist/encryption/pure-js.js +0 -82
  43. package/dist/encryption/pure-js.js.map +0 -1
  44. package/src/encryption/js/aes.ts +0 -191
  45. package/src/encryption/js/gcm.ts +0 -258
  46. package/src/encryption/pure-js.ts +0 -105
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudpss/crypto",
3
- "version": "0.5.25",
3
+ "version": "0.5.28",
4
4
  "author": "CloudPSS",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -13,22 +13,20 @@
13
13
  "browser": "./dist/encryption/browser.js",
14
14
  "node": "./dist/encryption/node.js",
15
15
  "default": "./dist/encryption/browser.js"
16
- }
17
- },
18
- "dependencies": {
19
- "sjcl": "^1.0.8"
16
+ },
17
+ "#lib-wasm": "./lib/wasm.js"
20
18
  },
19
+ "dependencies": {},
21
20
  "devDependencies": {
22
- "crypto-js": "^4.2.0",
23
- "hash-wasm": "^4.11.0",
24
- "@types/crypto-js": "^4.2.2",
25
- "@types/sjcl": "^1.0.34"
21
+ "esbuild": "^0.20.2",
22
+ "esbuild-plugin-wasm": "^1.1.0"
26
23
  },
27
24
  "scripts": {
28
- "start": "pnpm clean && tsc --watch",
29
- "build": "pnpm clean && tsc",
25
+ "start": "pnpm clean && pnpm build:wasm && tsc --watch",
26
+ "build": "pnpm clean && pnpm build:wasm && tsc",
27
+ "build:wasm": "node ./wasm-build.js",
30
28
  "test": "NODE_OPTIONS=\"${NODE_OPTIONS:-} --experimental-vm-modules\" jest",
31
29
  "benchmark": "node ./benchmark",
32
- "clean": "rimraf dist"
30
+ "clean": "rimraf dist lib"
33
31
  }
34
32
  }
@@ -4,7 +4,6 @@ import type { EncryptedData, PlainData } from './common.js';
4
4
  const module = () => {
5
5
  if (
6
6
  typeof crypto == 'object' &&
7
- typeof crypto.getRandomValues == 'function' &&
8
7
  typeof crypto.subtle == 'object' &&
9
8
  typeof crypto.subtle.importKey == 'function' &&
10
9
  typeof crypto.subtle.deriveKey == 'function' &&
@@ -13,7 +12,7 @@ const module = () => {
13
12
  ) {
14
13
  return import('./web.js');
15
14
  } else {
16
- return import('./pure-js.js');
15
+ return import('./wasm.js');
17
16
  }
18
17
  };
19
18
 
@@ -1,4 +1,4 @@
1
- import { toUint8Array } from '../utils.js';
1
+ import { EMPTY_BUFFER, toUint8Array } from '../utils.js';
2
2
 
3
3
  /** PBKDF2 迭代次数 */
4
4
  export const PBKDF2_ITERATIONS = 100_000;
@@ -20,7 +20,7 @@ export const AES_TAG_SIZE = 128 / 8;
20
20
  /** 加密输入/解密结果 */
21
21
  export interface PlainData {
22
22
  /** 附加数据 */
23
- aad?: Uint8Array;
23
+ aad: Uint8Array;
24
24
  /** 明文数据 */
25
25
  data: Uint8Array;
26
26
  }
@@ -30,7 +30,7 @@ export interface EncryptedData {
30
30
  /** NONCE */
31
31
  nonce: Uint8Array;
32
32
  /** 附加数据 */
33
- aad?: Uint8Array;
33
+ aad: Uint8Array;
34
34
  /** 加密后的数据和 tag */
35
35
  data: Uint8Array;
36
36
  }
@@ -71,15 +71,19 @@ export function parseEncrypted(data: BinaryData): EncryptedData | undefined {
71
71
  (buffer[MAGIC_NUMBER.length + NONCE_SIZE + 1] << 16) |
72
72
  (buffer[MAGIC_NUMBER.length + NONCE_SIZE + 2] << 8) |
73
73
  buffer[MAGIC_NUMBER.length + NONCE_SIZE + 3];
74
- if (aadSize > AAD_MAX_SIZE || aadSize < 0) return undefined;
74
+ if (aadSize > AAD_MAX_SIZE || aadSize < 0) {
75
+ return undefined;
76
+ }
75
77
  const paddingAadSize = padding(aadSize, AAD_PADDING);
76
- if (buffer.byteLength < paddingAadSize + MIN_ENCRYPTED_SIZE) return undefined;
78
+ if (buffer.byteLength < paddingAadSize + MIN_ENCRYPTED_SIZE) {
79
+ return undefined;
80
+ }
77
81
  const aad = aadSize
78
82
  ? buffer.subarray(
79
83
  MAGIC_NUMBER.length + NONCE_SIZE + AAD_LEN_SIZE,
80
84
  MAGIC_NUMBER.length + NONCE_SIZE + AAD_LEN_SIZE + aadSize,
81
85
  )
82
- : undefined;
86
+ : EMPTY_BUFFER;
83
87
  const encrypted = buffer.subarray(MAGIC_NUMBER.length + NONCE_SIZE + AAD_LEN_SIZE + paddingAadSize);
84
88
  return { nonce, aad, data: encrypted };
85
89
  }
@@ -18,7 +18,9 @@ export function extractAad(data: BinaryData): Uint8Array | undefined {
18
18
  if (encrypted == null) {
19
19
  throw new TypeError('Invalid encrypted data');
20
20
  }
21
- return encrypted.aad;
21
+ const { aad } = encrypted;
22
+ if (!aad.byteLength) return undefined;
23
+ return aad;
22
24
  }
23
25
 
24
26
  // eslint-disable-next-line @typescript-eslint/unbound-method
@@ -1,4 +1,4 @@
1
- import { toUint8Array } from '../utils.js';
1
+ import { EMPTY_BUFFER, toUint8Array } from '../utils.js';
2
2
  import {
3
3
  AAD_LEN_SIZE,
4
4
  AAD_MAX_SIZE,
@@ -41,7 +41,7 @@ interface Module {
41
41
  }
42
42
 
43
43
  /** 创建模块 */
44
- export function createModule(impl: typeof import('#encryption')): Module {
44
+ export function createModule(impl: typeof import('#encryption') | typeof import('./wasm.js')): Module {
45
45
  const encryptAad: Module['encryptAad'] = async (data, aad, passphrase) => {
46
46
  assertPassphrase(passphrase);
47
47
  const aadSize = aad?.byteLength ?? 0;
@@ -50,7 +50,7 @@ export function createModule(impl: typeof import('#encryption')): Module {
50
50
  }
51
51
  const paddedAddSize = padding(aadSize, AAD_PADDING);
52
52
  const plain: PlainData = {
53
- aad: aadSize ? toUint8Array(aad!) : undefined,
53
+ aad: aadSize ? toUint8Array(aad!) : EMPTY_BUFFER,
54
54
  data: toUint8Array(data),
55
55
  };
56
56
  const encrypted = await impl.encrypt(plain, passphrase);
@@ -64,7 +64,7 @@ export function createModule(impl: typeof import('#encryption')): Module {
64
64
  result[MAGIC_NUMBER.length + NONCE_SIZE + 1] = aadSize >>> 16;
65
65
  result[MAGIC_NUMBER.length + NONCE_SIZE + 2] = aadSize >>> 8;
66
66
  result[MAGIC_NUMBER.length + NONCE_SIZE + 3] = aadSize;
67
- result.set(plain.aad!, MAGIC_NUMBER.length + NONCE_SIZE + AAD_LEN_SIZE);
67
+ result.set(plain.aad, MAGIC_NUMBER.length + NONCE_SIZE + AAD_LEN_SIZE);
68
68
  }
69
69
  result.set(encrypted.data, MAGIC_NUMBER.length + NONCE_SIZE + AAD_LEN_SIZE + paddedAddSize);
70
70
  return result;
@@ -20,10 +20,11 @@ export async function encrypt({ data, aad }: PlainData, passphrase: string): Pro
20
20
  const nonce = randomBytes(NONCE_SIZE);
21
21
  const key = await aesKdf(passphrase, nonce);
22
22
  const cipher = createCipheriv('aes-256-gcm', key, nonce, { authTagLength: AES_TAG_SIZE });
23
- if (aad) cipher.setAAD(aad);
23
+ if (aad.byteLength) cipher.setAAD(aad);
24
24
  const encrypted = Buffer.concat([cipher.update(data), cipher.final(), cipher.getAuthTag()]);
25
25
  return {
26
26
  nonce: toUint8Array(nonce),
27
+ aad,
27
28
  data: toUint8Array(encrypted),
28
29
  };
29
30
  }
@@ -33,7 +34,7 @@ export async function decrypt({ nonce, aad, data }: EncryptedData, passphrase: s
33
34
  const key = await aesKdf(passphrase, nonce);
34
35
  const decipher = createDecipheriv('aes-256-gcm', key, nonce, { authTagLength: AES_TAG_SIZE });
35
36
  decipher.setAuthTag(data.subarray(data.length - AES_TAG_SIZE));
36
- if (aad) decipher.setAAD(aad);
37
+ if (aad.byteLength) decipher.setAAD(aad);
37
38
  const decrypted = Buffer.concat([decipher.update(data.subarray(0, data.length - AES_TAG_SIZE)), decipher.final()]);
38
- return { data: toUint8Array(decrypted) };
39
+ return { data: toUint8Array(decrypted), aad };
39
40
  }
@@ -0,0 +1,47 @@
1
+ import {
2
+ NONCE_SIZE,
3
+ AES_KEY_SIZE,
4
+ AES_TAG_SIZE,
5
+ type EncryptedData,
6
+ PBKDF2_ITERATIONS,
7
+ type PlainData,
8
+ } from './common.js';
9
+ import * as mod from '#lib-wasm';
10
+
11
+ const encoder = new TextEncoder();
12
+
13
+ /** crypto-js encrypt */
14
+ export function encrypt({ data, aad }: PlainData, passphrase: string): EncryptedData {
15
+ const nonce = crypto.getRandomValues(new Uint8Array(NONCE_SIZE));
16
+ const result = mod.encrypt(
17
+ encoder.encode(passphrase),
18
+ data,
19
+ aad,
20
+ nonce,
21
+ PBKDF2_ITERATIONS,
22
+ AES_KEY_SIZE,
23
+ AES_TAG_SIZE,
24
+ );
25
+ return {
26
+ nonce,
27
+ aad,
28
+ data: result,
29
+ };
30
+ }
31
+
32
+ /** crypto-js decrypt */
33
+ export function decrypt({ data, aad, nonce }: EncryptedData, passphrase: string): PlainData {
34
+ const decrypted = mod.decrypt(
35
+ encoder.encode(passphrase),
36
+ data,
37
+ aad,
38
+ nonce,
39
+ PBKDF2_ITERATIONS,
40
+ AES_KEY_SIZE,
41
+ AES_TAG_SIZE,
42
+ );
43
+ return {
44
+ aad,
45
+ data: decrypted,
46
+ };
47
+ }
@@ -40,6 +40,7 @@ export async function encrypt({ data, aad }: PlainData, passphrase: string): Pro
40
40
  );
41
41
  return {
42
42
  nonce,
43
+ aad,
43
44
  data: new Uint8Array(encrypted),
44
45
  };
45
46
  }
@@ -57,5 +58,8 @@ export async function decrypt({ data, nonce, aad }: EncryptedData, passphrase: s
57
58
  key,
58
59
  data,
59
60
  );
60
- return { data: new Uint8Array(decrypted) };
61
+ return {
62
+ aad,
63
+ data: new Uint8Array(decrypted),
64
+ };
61
65
  }
package/src/utils.ts CHANGED
@@ -8,3 +8,6 @@ export function toUint8Array(data: BinaryData): Uint8Array {
8
8
  }
9
9
  return new Uint8Array(data, 0, data.byteLength);
10
10
  }
11
+
12
+ /** 长度为 0 的 Uint8Array */
13
+ export const EMPTY_BUFFER = new Uint8Array(0);
@@ -5,7 +5,7 @@ import { createModule } from '../dist/encryption/module.js';
5
5
  import * as nodeImpl from '../dist/encryption/node.js';
6
6
  import * as browserImpl from '../dist/encryption/browser.js';
7
7
  import * as webImpl from '../dist/encryption/web.js';
8
- import * as jsImpl from '../dist/encryption/pure-js.js';
8
+ import * as wasmImpl from '../dist/encryption/wasm.js';
9
9
 
10
10
  const data = [
11
11
  Buffer.from(''),
@@ -23,13 +23,26 @@ describe('Encryption root export', () => {
23
23
  });
24
24
 
25
25
  it('check is encrypted', () => {
26
+ const nonce = Buffer.alloc(12);
27
+ const tag = Buffer.alloc(16);
28
+ const aadLength = Buffer.alloc(4);
29
+
26
30
  // @ts-expect-error bad type
27
31
  expect(() => isEncrypted({})).toThrow('Invalid data');
28
- expect(isEncrypted(Buffer.from(MAGIC_NUMBER))).toBe(false);
29
- expect(isEncrypted(Buffer.concat([MAGIC_NUMBER, Buffer.alloc(31)]))).toBe(false);
30
- expect(isEncrypted(Buffer.concat([MAGIC_NUMBER, Buffer.alloc(32)]))).toBe(true);
31
32
  expect(isEncrypted(Buffer.alloc(40))).toBe(false);
32
33
  expect(isEncrypted(Buffer.alloc(41))).toBe(false);
34
+
35
+ expect(isEncrypted(Buffer.from(MAGIC_NUMBER))).toBe(false);
36
+
37
+ expect(isEncrypted(Buffer.concat([MAGIC_NUMBER, nonce, aadLength, tag]))).toBe(true);
38
+ expect(isEncrypted(Buffer.concat([MAGIC_NUMBER, nonce, aadLength, tag.subarray(1)]))).toBe(false);
39
+
40
+ aadLength.writeUInt32BE(100);
41
+ expect(isEncrypted(Buffer.concat([MAGIC_NUMBER, nonce, aadLength, Buffer.alloc(111), tag]))).toBe(false);
42
+ expect(isEncrypted(Buffer.concat([MAGIC_NUMBER, nonce, aadLength, Buffer.alloc(112), tag]))).toBe(true);
43
+
44
+ aadLength.writeUInt32BE(0xffff_fffe);
45
+ expect(isEncrypted(Buffer.concat([MAGIC_NUMBER, nonce, aadLength, Buffer.alloc(112), tag]))).toBe(false);
33
46
  });
34
47
 
35
48
  it('encrypt check', async () => {
@@ -50,25 +63,32 @@ describe('Encryption root export', () => {
50
63
  await expect(() => decrypt(Buffer.alloc(100), 'xx')).rejects.toThrow('Invalid encrypted data');
51
64
  });
52
65
 
53
- it('accepts empty aad', async () => {
54
- const encrypted = await encryptAad(Buffer.alloc(0), Buffer.alloc(0), passphrase);
55
- expect(encrypted).toBeInstanceOf(Uint8Array);
56
- const extractedAad = extractAad(encrypted);
57
- expect(extractedAad).toBeUndefined();
58
- });
66
+ describe('aad', () => {
67
+ it('accepts empty aad', async () => {
68
+ const encrypted = await encryptAad(Buffer.alloc(0), Buffer.alloc(0), passphrase);
69
+ expect(encrypted).toBeInstanceOf(Uint8Array);
70
+ const extractedAad = extractAad(encrypted);
71
+ expect(extractedAad).toBeUndefined();
72
+ });
59
73
 
60
- it('accepts undefined aad', async () => {
61
- const encrypted = await encryptAad(Buffer.alloc(0), undefined, passphrase);
62
- expect(encrypted).toBeInstanceOf(Uint8Array);
63
- const extractedAad = extractAad(encrypted);
64
- expect(extractedAad).toBeUndefined();
65
- });
74
+ it('accepts undefined aad', async () => {
75
+ const encrypted = await encryptAad(Buffer.alloc(0), undefined, passphrase);
76
+ expect(encrypted).toBeInstanceOf(Uint8Array);
77
+ const extractedAad = extractAad(encrypted);
78
+ expect(extractedAad).toBeUndefined();
79
+ });
66
80
 
67
- it('rejects invalid aad size', async () => {
68
- const aad2 = Buffer.alloc(1024 * 1024 * 1024 + 1);
69
- await expect(async () => {
70
- await encryptAad(Buffer.alloc(0), aad2, passphrase);
71
- }).rejects.toThrow('Invalid AAD size');
81
+ it('rejects invalid aad size', async () => {
82
+ const aad2 = Buffer.alloc(1024 * 1024 * 1024 + 1);
83
+ await expect(async () => {
84
+ await encryptAad(Buffer.alloc(0), aad2, passphrase);
85
+ }).rejects.toThrow('Invalid AAD size');
86
+ });
87
+
88
+ it('rejects invalid data', () => {
89
+ const data = Buffer.alloc(10);
90
+ expect(() => extractAad(data)).toThrow('Invalid encrypted data');
91
+ });
72
92
  });
73
93
 
74
94
  checkModule({ encrypt, decrypt, encryptAad });
@@ -128,17 +148,18 @@ function checkModule(module) {
128
148
 
129
149
  /**
130
150
  * 检查实现
131
- * @param {(data: import('../dist/encryption/common.js').PlainData, passphrase: string) => Promise<import('../dist/encryption/common.js').EncryptedData>} encrypt encrypt
132
- * @param {(data: import('../dist/encryption/common.js').EncryptedData, passphrase: string) => Promise<import('../dist/encryption/common.js').PlainData>} decrypt decrypt
151
+ * @param {Function} encrypt encrypt
152
+ * @param {Function} decrypt decrypt
133
153
  */
134
154
  function checkImplEncryption(encrypt, decrypt) {
135
155
  it.each(data)(
136
156
  `$type[$length]`,
137
157
  async ({ raw }) => {
138
- const encrypted = await encrypt({ data: toUint8Array(raw) }, passphrase);
158
+ const encrypted = await encrypt({ data: toUint8Array(raw), aad: new Uint8Array(0) }, passphrase);
139
159
  expect(encrypted.nonce).toBeInstanceOf(Uint8Array);
140
160
  expect(encrypted.nonce.byteLength).toBe(12);
141
- expect(encrypted.aad).toBeUndefined();
161
+ expect(encrypted.aad).toBeInstanceOf(Uint8Array);
162
+ expect(encrypted.aad.byteLength).toBe(0);
142
163
  expect(encrypted.data).toBeInstanceOf(Uint8Array);
143
164
 
144
165
  await expect(async () => {
@@ -148,7 +169,8 @@ function checkImplEncryption(encrypt, decrypt) {
148
169
  const decrypted = await decrypt(encrypted, passphrase);
149
170
  expect(decrypted.data).toBeInstanceOf(Uint8Array);
150
171
  expect(decrypted.data).toEqual(toUint8Array(raw));
151
- expect(decrypted.aad).toBeUndefined();
172
+ expect(decrypted.aad).toBeInstanceOf(Uint8Array);
173
+ expect(decrypted.aad.byteLength).toBe(0);
152
174
  },
153
175
  100_000,
154
176
  );
@@ -191,7 +213,7 @@ describe('Encryption impl', () => {
191
213
  node: nodeImpl,
192
214
  browser: browserImpl,
193
215
  web: webImpl,
194
- js: jsImpl,
216
+ wasm: wasmImpl,
195
217
  });
196
218
  describe.each(impls)('impl %s', (name, impl) => {
197
219
  const module = createModule(impl);
@@ -208,14 +230,13 @@ describe('Encryption impl', () => {
208
230
  });
209
231
 
210
232
  describe('Encryption impl browser', () => {
211
- describe('should work without crypto', () => {
212
- const { crypto } = globalThis;
233
+ describe('should work without crypto subtle', () => {
234
+ const { subtle } = crypto;
213
235
  beforeAll(() => {
214
- // @ts-expect-error remove crypto
215
- globalThis.crypto = undefined;
236
+ Object.defineProperty(crypto, 'subtle', { value: undefined, configurable: true });
216
237
  });
217
238
  afterAll(() => {
218
- globalThis.crypto = crypto;
239
+ Object.defineProperty(crypto, 'subtle', { value: subtle, configurable: true });
219
240
  });
220
241
  checkModule(createModule(browserImpl));
221
242
  });
package/tsconfig.json CHANGED
@@ -4,7 +4,8 @@
4
4
  "compilerOptions": {
5
5
  "outDir": "./dist",
6
6
  "paths": {
7
- "#encryption": ["./src/encryption/node.ts", "./src/encryption/browser.ts"]
7
+ "#encryption": ["./src/encryption/node.ts", "./src/encryption/browser.ts"],
8
+ "#lib-wasm": ["./lib/wasm.d.ts"]
8
9
  }
9
10
  }
10
11
  }
package/wasm-build.js ADDED
@@ -0,0 +1,30 @@
1
+ import path from 'node:path';
2
+ import fs from 'node:fs/promises';
3
+ import { spawn } from 'node:child_process';
4
+ import { fileURLToPath } from 'node:url';
5
+ import esbuild from 'esbuild';
6
+ import { wasmLoader } from 'esbuild-plugin-wasm';
7
+ import { once } from 'node:events';
8
+
9
+ const __filename = fileURLToPath(import.meta.url);
10
+ const __dirname = path.dirname(__filename);
11
+
12
+ const wasmPack = spawn('wasm-pack', ['build', '--target', 'bundler', '--release'], {
13
+ stdio: 'inherit',
14
+ cwd: path.resolve(__dirname, './wasm'),
15
+ });
16
+
17
+ await once(wasmPack, 'exit');
18
+
19
+ await esbuild.build({
20
+ entryPoints: [path.resolve(__dirname, './wasm/pkg/wasm.js')],
21
+ outdir: path.resolve(__dirname, './lib'),
22
+ charset: 'utf8',
23
+ target: 'es2022',
24
+ format: 'esm',
25
+ bundle: true,
26
+ minify: false,
27
+ plugins: [wasmLoader({ mode: 'embedded' })],
28
+ });
29
+
30
+ await fs.copyFile(path.resolve(__dirname, './wasm/pkg/wasm.d.ts'), path.resolve(__dirname, './lib/wasm.d.ts'));
@@ -1,20 +0,0 @@
1
- /** AES 算法 */
2
- export declare class AES {
3
- /** 加密密钥 */
4
- private readonly encKey;
5
- /** 解密密钥 */
6
- private readonly decKey;
7
- constructor(key: Uint32Array);
8
- /**
9
- * Encryption and decryption core.
10
- */
11
- private crypt;
12
- /**
13
- * Encrypt a block of plain text.
14
- */
15
- encrypt(input: Uint32Array, inputOffset: number, output: Uint32Array, outputOffset: number): void;
16
- /**
17
- * Decrypt a block of cipher text.
18
- */
19
- decrypt(input: Uint32Array, inputOffset: number, output: Uint32Array, outputOffset: number): void;
20
- }
@@ -1,151 +0,0 @@
1
- /** Compute AES S-box Tables */
2
- function createSBox() {
3
- const encTable = [
4
- new Uint32Array(256),
5
- new Uint32Array(256),
6
- new Uint32Array(256),
7
- new Uint32Array(256),
8
- new Uint8Array(256),
9
- ];
10
- const decTable = [
11
- new Uint32Array(256),
12
- new Uint32Array(256),
13
- new Uint32Array(256),
14
- new Uint32Array(256),
15
- new Uint8Array(256),
16
- ];
17
- const sbox = encTable[4];
18
- const sboxInv = decTable[4];
19
- const d = new Uint8Array(256);
20
- const th = new Uint8Array(256);
21
- // Compute double and third tables
22
- for (let i = 0; i < 256; i++) {
23
- d[i] = (i << 1) ^ ((i >> 7) * 283);
24
- th[d[i] ^ i] = i;
25
- }
26
- let x = 0, xInv = 0, x2 = 0, x4 = 0, x8 = 0;
27
- for (; !sbox[x]; x ^= x2 || 1, xInv = th[xInv] || 1) {
28
- // Compute sbox
29
- let s = xInv ^ (xInv << 1) ^ (xInv << 2) ^ (xInv << 3) ^ (xInv << 4);
30
- s = (s >> 8) ^ (s & 255) ^ 99;
31
- sbox[x] = s;
32
- sboxInv[s] = x;
33
- // Compute MixColumns
34
- x8 = d[(x4 = d[(x2 = d[x])])];
35
- let tDec = (x8 * 0x101_0101) ^ (x4 * 0x1_0001) ^ (x2 * 0x101) ^ (x * 0x101_0100);
36
- let tEnc = (d[s] * 0x101) ^ (s * 0x101_0100);
37
- for (let i = 0; i < 4; i++) {
38
- encTable[i][x] = tEnc = (tEnc << 24) ^ (tEnc >>> 8);
39
- decTable[i][s] = tDec = (tDec << 24) ^ (tDec >>> 8);
40
- }
41
- }
42
- return [encTable, decTable];
43
- }
44
- let encryptTable;
45
- let decryptTable;
46
- /** 初始化 */
47
- function init() {
48
- if (!encryptTable) {
49
- [encryptTable, decryptTable] = createSBox();
50
- }
51
- }
52
- /** AES 算法 */
53
- export class AES {
54
- /** 加密密钥 */
55
- encKey;
56
- /** 解密密钥 */
57
- decKey;
58
- constructor(key) {
59
- if (key.length !== 4 && key.length !== 6 && key.length !== 8) {
60
- throw new TypeError('Invalid aes key length');
61
- }
62
- init();
63
- const sbox = encryptTable[4], decTable = decryptTable, keyLen = key.length, rKeyLen = 4 * key.length + 28;
64
- this.encKey = new Uint32Array(rKeyLen);
65
- this.decKey = new Uint32Array(rKeyLen);
66
- const { encKey, decKey } = this;
67
- encKey.set(key);
68
- // schedule encryption keys
69
- let rcon = 1;
70
- for (let i = keyLen; i < rKeyLen; i++) {
71
- let tmp = this.encKey[i - 1];
72
- // apply sbox
73
- if (i % keyLen === 0 || (keyLen === 8 && i % keyLen === 4)) {
74
- tmp =
75
- (sbox[tmp >>> 24] << 24) ^
76
- (sbox[(tmp >> 16) & 255] << 16) ^
77
- (sbox[(tmp >> 8) & 255] << 8) ^
78
- sbox[tmp & 255];
79
- // shift rows and add rcon
80
- if (i % keyLen === 0) {
81
- tmp = (tmp << 8) ^ (tmp >>> 24) ^ (rcon << 24);
82
- rcon = (rcon << 1) ^ ((rcon >> 7) * 283);
83
- }
84
- }
85
- encKey[i] = encKey[i - keyLen] ^ tmp;
86
- }
87
- // schedule decryption keys
88
- for (let i = rKeyLen, j = 0; i; j++, i--) {
89
- const tmp = encKey[j & 3 ? i : i - 4];
90
- if (i <= 4 || j < 4) {
91
- decKey[j] = tmp;
92
- }
93
- else {
94
- decKey[j] =
95
- decTable[0][sbox[tmp >>> 24]] ^
96
- decTable[1][sbox[(tmp >> 16) & 255]] ^
97
- decTable[2][sbox[(tmp >> 8) & 255]] ^
98
- decTable[3][sbox[tmp & 255]];
99
- }
100
- }
101
- }
102
- /**
103
- * Encryption and decryption core.
104
- */
105
- crypt(input, inputOffset, output, outputOffset, decrypt) {
106
- const key = decrypt ? this.decKey : this.encKey;
107
- const [t0, t1, t2, t3, sbox] = decrypt ? decryptTable : encryptTable;
108
- // state variables a,b,c,d are loaded with pre-whitened data
109
- let a = input[inputOffset] ^ key[0], b = input[inputOffset + (decrypt ? 3 : 1)] ^ key[1], c = input[inputOffset + 2] ^ key[2], d = input[inputOffset + (decrypt ? 1 : 3)] ^ key[3];
110
- let kIndex = 4;
111
- // Inner rounds. Cribbed from OpenSSL.
112
- const nInnerRounds = key.length / 4 - 2;
113
- for (let i = 0; i < nInnerRounds; i++) {
114
- const a2 = t0[a >>> 24] ^ t1[(b >> 16) & 255] ^ t2[(c >> 8) & 255] ^ t3[d & 255] ^ key[kIndex];
115
- const b2 = t0[b >>> 24] ^ t1[(c >> 16) & 255] ^ t2[(d >> 8) & 255] ^ t3[a & 255] ^ key[kIndex + 1];
116
- const c2 = t0[c >>> 24] ^ t1[(d >> 16) & 255] ^ t2[(a >> 8) & 255] ^ t3[b & 255] ^ key[kIndex + 2];
117
- d = t0[d >>> 24] ^ t1[(a >> 16) & 255] ^ t2[(b >> 8) & 255] ^ t3[c & 255] ^ key[kIndex + 3];
118
- a = a2;
119
- b = b2;
120
- c = c2;
121
- kIndex += 4;
122
- }
123
- // Last round.
124
- for (let i = 0; i < 4; i++) {
125
- output[outputOffset + (decrypt ? 3 & -i : i)] =
126
- (sbox[a >>> 24] << 24) ^
127
- (sbox[(b >> 16) & 255] << 16) ^
128
- (sbox[(c >> 8) & 255] << 8) ^
129
- sbox[d & 255] ^
130
- key[kIndex++];
131
- const a2 = a;
132
- a = b;
133
- b = c;
134
- c = d;
135
- d = a2;
136
- }
137
- }
138
- /**
139
- * Encrypt a block of plain text.
140
- */
141
- encrypt(input, inputOffset, output, outputOffset) {
142
- return this.crypt(input, inputOffset, output, outputOffset, false);
143
- }
144
- /**
145
- * Decrypt a block of cipher text.
146
- */
147
- decrypt(input, inputOffset, output, outputOffset) {
148
- return this.crypt(input, inputOffset, output, outputOffset, true);
149
- }
150
- }
151
- //# sourceMappingURL=aes.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"aes.js","sourceRoot":"","sources":["../../../src/encryption/js/aes.ts"],"names":[],"mappings":"AAGA,+BAA+B;AAC/B,SAAS,UAAU;IACf,MAAM,QAAQ,GAAc;QACxB,IAAI,WAAW,CAAC,GAAG,CAAC;QACpB,IAAI,WAAW,CAAC,GAAG,CAAC;QACpB,IAAI,WAAW,CAAC,GAAG,CAAC;QACpB,IAAI,WAAW,CAAC,GAAG,CAAC;QACpB,IAAI,UAAU,CAAC,GAAG,CAAC;KACtB,CAAC;IACF,MAAM,QAAQ,GAAc;QACxB,IAAI,WAAW,CAAC,GAAG,CAAC;QACpB,IAAI,WAAW,CAAC,GAAG,CAAC;QACpB,IAAI,WAAW,CAAC,GAAG,CAAC;QACpB,IAAI,WAAW,CAAC,GAAG,CAAC;QACpB,IAAI,UAAU,CAAC,GAAG,CAAC;KACtB,CAAC;IAEF,MAAM,IAAI,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;IACzB,MAAM,OAAO,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;IAE5B,MAAM,CAAC,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC;IAC9B,MAAM,EAAE,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC;IAE/B,kCAAkC;IAClC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;QAC3B,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC;QACnC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,C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G,CAAC,CAAC;YACN,CAAC,GAAG,CAAC,CAAC;YACN,CAAC,GAAG,EAAE,CAAC;QACX,CAAC;IACL,CAAC;IAED;;OAEG;IACH,OAAO,CAAC,KAAkB,EAAE,WAAmB,EAAE,MAAmB,EAAE,YAAoB;QACtF,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,YAAY,EAAE,KAAK,CAAC,CAAC;IACvE,CAAC;IAED;;OAEG;IACH,OAAO,CAAC,KAAkB,EAAE,WAAmB,EAAE,MAAmB,EAAE,YAAoB;QACtF,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,YAAY,EAAE,IAAI,CAAC,CAAC;IACtE,CAAC;CACJ"}
@@ -1,26 +0,0 @@
1
- import { AES } from './aes.js';
2
- /** GCM (Galois/Counter Mode) */
3
- export declare class GCM {
4
- readonly cipher: AES;
5
- readonly iv: Uint8Array;
6
- readonly tagLength: number;
7
- readonly aad: Uint8Array;
8
- constructor(cipher: AES, iv: Uint8Array, tagLength?: number, aad?: Uint8Array);
9
- /** Convert a Uint8Array to a Uint32Array */
10
- private toUint32Array;
11
- /** Convert a Uint32Array to a Uint8Array */
12
- private toUint8Array;
13
- /** Set out of range bytes to 0 */
14
- private clamp;
15
- private readonly H;
16
- /** Compute the galois multiplication of X and Y */
17
- private galoisMultiply;
18
- /** Ghash */
19
- private ghash;
20
- /** GCM CTR mode. */
21
- private ctr;
22
- /** 加密 */
23
- encrypt(data: Uint8Array): Uint8Array;
24
- /** 解密 */
25
- decrypt(data: Uint8Array): Uint8Array;
26
- }