@ray-js/wechat-tycrypto 0.0.1-beta-1

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.
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@ray-js/wechat-tycrypto",
3
+ "version": "0.0.1-beta-1",
4
+ "publishConfig": {
5
+ "access": "public",
6
+ "registry": "https://registry.npmjs.org"
7
+ },
8
+ "module": "./index.esm.js",
9
+ "main": "./index.js",
10
+ "type": "module",
11
+ "types": "./src/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./src/index.d.ts",
15
+ "import": "./index.esm.js",
16
+ "require": "./index.js"
17
+ }
18
+ },
19
+ "dependencies": {
20
+ "js-md5": "0.7.3"
21
+ },
22
+ "peerDependencies": {}
23
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from './lib/other/errors';
2
+ export * from './lib/aes/gcm';
3
+ export * from './lib/aes/ecb';
4
+ export * from './lib/aes/aes';
5
+ export * from './lib/aes/cbc';
6
+ export * from './lib/aes/ccm';
@@ -0,0 +1,113 @@
1
+ /**
2
+ * @file {@link http://asmjs.org Asm.js} implementation of the {@link https://en.wikipedia.org/wiki/Advanced_Encryption_Standard Advanced Encryption Standard}.
3
+ * @author Artem S Vybornov <vybornov@gmail.com>
4
+ * @license MIT
5
+ */
6
+ export declare class AES_asm {
7
+ constructor(foreign: any, heap: ArrayBuffer);
8
+ /**
9
+ * AES enciphering mode constants
10
+ * @enum {number}
11
+ * @const
12
+ */
13
+ static ENC: {
14
+ ECB: 0;
15
+ CBC: 2;
16
+ CFB: 4;
17
+ OFB: 6;
18
+ CTR: 7;
19
+ [key: string]: number;
20
+ };
21
+ /**
22
+ * AES deciphering mode constants
23
+ * @enum {number}
24
+ * @const
25
+ */
26
+ static DEC: {
27
+ ECB: 1;
28
+ CBC: 3;
29
+ CFB: 5;
30
+ OFB: 6;
31
+ CTR: 7;
32
+ [key: string]: number;
33
+ };
34
+ /**
35
+ * AES MAC mode constants
36
+ * @enum {number}
37
+ * @const
38
+ */
39
+ static MAC: {
40
+ CBC: number;
41
+ GCM: number;
42
+ };
43
+ /**
44
+ * Heap data offset
45
+ * @type {number}
46
+ * @const
47
+ */
48
+ static HEAP_DATA: number;
49
+ /**
50
+ * @param ks - key size, 4/6/8 (for 128/192/256-bit key correspondingly)
51
+ * @param k0 - key vector components
52
+ * @param k1 - key vector components
53
+ * @param k2 - key vector components
54
+ * @param k3 - key vector components
55
+ * @param k4 - key vector components
56
+ * @param k5 - key vector components
57
+ * @param k6 - key vector components
58
+ * @param k7 - key vector components
59
+ */
60
+ set_key(ks: number, k0: number, k1: number, k2: number, k3: number, k4: number, k5: number, k6: number, k7: number): void;
61
+ /**
62
+ * Populate the internal iv of the module
63
+ */
64
+ set_iv(i0: number, i1: number, i2: number, i3: number): void;
65
+ /**
66
+ * Set counter mask for CTR-family modes
67
+ */
68
+ set_mask(m0: number, m1: number, m2: number, m3: number): void;
69
+ /**
70
+ * Set nonce for CTR-family modes
71
+ */
72
+ set_nonce(n0: number, n1: number, n2: number, n3: number): void;
73
+ /**
74
+ * Set counter for CTR-family modes
75
+ */
76
+ set_counter(c0: number, c1: number, c2: number, c3: number): void;
77
+ /**
78
+ * Perform ciphering operation on the supplied data
79
+ *
80
+ * @param mode - block cipher mode (see {@link AES_asm} mode constants)
81
+ * @param pos - offset of the data being processed
82
+ * @param len - length of the data being processed
83
+ * @return Actual amount of data have been processed
84
+ */
85
+ cipher(mode: number, pos: number, len: number): number;
86
+ /**
87
+ * GCM initialization
88
+ */
89
+ gcm_init(): void;
90
+ /**
91
+ * Store the internal iv vector into the heap
92
+ *
93
+ * @returns The number of bytes have been written into the heap, always 16
94
+ */
95
+ get_iv(pos: number): 16 | -1;
96
+ /**
97
+ * Calculates MAC of the supplied data
98
+ *
99
+ * @param mode - block cipher mode (see {@link AES_asm} mode constants)
100
+ * @param pos - offset of the data being processed
101
+ * @param len - length of the data being processed
102
+ * @return Actual amount of data have been processed
103
+ */
104
+ mac(mode: number, pos: number, len: number): number;
105
+ /**
106
+ * Store the internal state vector into the heap.
107
+ *
108
+ * @param pos - offset where to put the data
109
+ * @return The number of bytes have been written into the heap, always 16.
110
+ */
111
+ get_state(pos: number): 16 | -1;
112
+ }
113
+ export declare type AES_mode = 'ECB' | 'CBC' | 'CFB' | 'OFB' | 'CTR' | 'CCM';
@@ -0,0 +1,15 @@
1
+ import { AES_asm, AES_mode } from './aes.asm';
2
+ export declare class AES {
3
+ readonly heap: Uint8Array;
4
+ readonly asm: AES_asm;
5
+ private readonly mode;
6
+ padding: boolean;
7
+ pos: number;
8
+ len: number;
9
+ constructor(key: Uint8Array, iv: Uint8Array | undefined, padding: boolean | undefined, mode: AES_mode, heap?: Uint8Array, asm?: AES_asm);
10
+ AES_Encrypt_process(data: Uint8Array): Uint8Array;
11
+ AES_Encrypt_finish(): Uint8Array;
12
+ AES_Decrypt_process(data: Uint8Array): Uint8Array;
13
+ AES_Decrypt_finish(): Uint8Array;
14
+ }
15
+ export default AES;
@@ -0,0 +1,9 @@
1
+ import { AES } from './aes';
2
+ export declare class AES_CBC {
3
+ private aes;
4
+ static encrypt(data: Uint8Array, key: Uint8Array, padding?: boolean, iv?: Uint8Array): Uint8Array;
5
+ static decrypt(data: Uint8Array, key: Uint8Array, padding?: boolean, iv?: Uint8Array): Uint8Array;
6
+ constructor(key: Uint8Array, iv?: Uint8Array, padding?: boolean, aes?: AES);
7
+ encrypt(data: Uint8Array): Uint8Array;
8
+ decrypt(data: Uint8Array): Uint8Array;
9
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Counter with CBC-MAC (CCM)
3
+ *
4
+ * Due to JS limitations (52 bits of Number precision) maximum encrypted message length
5
+ * is limited to ~4 PiB ( 2^52 - 16 ) per `nonce`-`key` pair.
6
+ * That also limits `lengthSize` parameter maximum value to 7 (not 8 as described in RFC3610).
7
+ *
8
+ * Additional authenticated data `adata` maximum length is chosen to be no more than 65279 bytes ( 2^16 - 2^8 ),
9
+ * which is considered enough for the most of use-cases.
10
+ *
11
+ * And one more important thing: in case of progressive ciphering of a data stream (in other
12
+ * words when data can't be held in-memory at a whole and are ciphered chunk-by-chunk)
13
+ * you have to know the `dataLength` in advance and pass that value to the cipher options.
14
+ */
15
+ import { AES } from './aes';
16
+ export declare class AES_CCM {
17
+ private readonly tagSize;
18
+ private readonly lengthSize;
19
+ private nonce;
20
+ private readonly adata;
21
+ private counter;
22
+ private dataLength;
23
+ private aes;
24
+ static encrypt(clear: Uint8Array, key: Uint8Array, nonce: Uint8Array, adata: Uint8Array | undefined, tagsize?: number): Uint8Array;
25
+ static decrypt(cipher: Uint8Array, key: Uint8Array, nonce: Uint8Array, adata: Uint8Array | undefined, tagsize?: number): Uint8Array;
26
+ constructor(key: Uint8Array, nonce: Uint8Array, adata: Uint8Array | undefined, tagSize: number | undefined, dataLength: number, aes?: AES);
27
+ encrypt(data: Uint8Array): Uint8Array;
28
+ decrypt(data: Uint8Array): Uint8Array;
29
+ AES_CCM_calculate_iv(): void;
30
+ _cbc_mac_process(data: Uint8Array): void;
31
+ AES_CCM_Encrypt_process(data: Uint8Array): Uint8Array;
32
+ AES_CCM_Encrypt_finish(): Uint8Array;
33
+ AES_CCM_Decrypt_process(data: Uint8Array): Uint8Array;
34
+ AES_CCM_Decrypt_finish(): Uint8Array;
35
+ private AES_CTR_set_options;
36
+ }
@@ -0,0 +1,10 @@
1
+ import { AES } from './aes';
2
+ export declare class AES_ECB {
3
+ private aes;
4
+ static encrypt(data: Uint8Array, key: Uint8Array, padding?: boolean): Uint8Array;
5
+ static decrypt(data: Uint8Array, key: Uint8Array, padding?: boolean): Uint8Array;
6
+ constructor(key: Uint8Array, padding?: boolean, aes?: AES);
7
+ encrypt(data: Uint8Array): Uint8Array;
8
+ decrypt(data: Uint8Array): Uint8Array;
9
+ }
10
+ export default AES_ECB;
@@ -0,0 +1,3 @@
1
+ import { AES_asm } from './aes.asm';
2
+ export declare const _AES_heap_instance: Uint8Array;
3
+ export declare const _AES_asm_instance: AES_asm;
@@ -0,0 +1,21 @@
1
+ import { AES } from './aes';
2
+ export declare class AES_GCM {
3
+ private readonly tagSize;
4
+ private readonly adata;
5
+ private readonly gamma0;
6
+ private aes;
7
+ private counter;
8
+ static encrypt(cleartext: Uint8Array, key: Uint8Array, nonce: Uint8Array, adata?: Uint8Array, tagsize?: number): Uint8Array;
9
+ static decrypt(ciphertext: Uint8Array, key: Uint8Array, nonce: Uint8Array, adata?: Uint8Array, tagsize?: number): Uint8Array;
10
+ constructor(key: Uint8Array, nonce: Uint8Array, adata?: Uint8Array, tagSize?: number, aes?: AES);
11
+ encrypt(data: Uint8Array): Uint8Array;
12
+ decrypt(data: Uint8Array): Uint8Array;
13
+ AES_GCM_Encrypt_process(data: Uint8Array): Uint8Array;
14
+ AES_GCM_Encrypt_finish(): Uint8Array;
15
+ AES_GCM_Decrypt_process(data: Uint8Array): Uint8Array;
16
+ AES_GCM_Decrypt_finish(): Uint8Array;
17
+ private AES_GCM_decrypt;
18
+ private AES_GCM_encrypt;
19
+ _gcm_mac_process(data: Uint8Array): void;
20
+ }
21
+ export default AES_GCM;
@@ -0,0 +1,9 @@
1
+ export declare class IllegalStateError extends Error {
2
+ constructor(...args: any[]);
3
+ }
4
+ export declare class IllegalArgumentError extends Error {
5
+ constructor(...args: any[]);
6
+ }
7
+ export declare class SecurityError extends Error {
8
+ constructor(...args: any[]);
9
+ }
@@ -0,0 +1,9 @@
1
+ export declare function pow2_ceil(a: number): number;
2
+ export declare function is_number(a: number): boolean;
3
+ export declare function is_string(a: string): boolean;
4
+ export declare function is_buffer(a: ArrayBuffer): boolean;
5
+ export declare function is_bytes(a: Uint8Array): boolean;
6
+ export declare function is_typed_array(a: any): boolean;
7
+ export declare function _heap_init(heap?: Uint8Array, heapSize?: number): Uint8Array;
8
+ export declare function _heap_write(heap: Uint8Array, hpos: number, data: Uint8Array, dpos: number, dlen: number): number;
9
+ export declare function joinBytes(...arg: Uint8Array[]): Uint8Array;