quarkdash 1.0.7 → 1.1.0
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/README.md +4 -2
- package/dist/cjs/cipher/chacha.js +154 -0
- package/dist/cjs/cipher/chacha.js.map +1 -0
- package/dist/cjs/cipher/cipher.js +34 -0
- package/dist/cjs/cipher/cipher.js.map +1 -0
- package/dist/cjs/cipher/gimli.js +143 -0
- package/dist/cjs/cipher/gimli.js.map +1 -0
- package/dist/cjs/core/kdf.js.map +1 -0
- package/dist/cjs/{mac.js → core/mac.js} +37 -1
- package/dist/cjs/core/mac.js.map +1 -0
- package/dist/cjs/{types.js.map → core/types.js.map} +1 -1
- package/dist/cjs/{utils.js → core/utils.js} +25 -8
- package/dist/cjs/core/utils.js.map +1 -0
- package/dist/cjs/core/wasm_loader.js +100 -0
- package/dist/cjs/core/wasm_loader.js.map +1 -0
- package/dist/cjs/crypto.js +30 -13
- package/dist/cjs/crypto.js.map +1 -1
- package/dist/cjs/{sha.js → hash/sha.js} +3 -2
- package/dist/cjs/hash/sha.js.map +1 -0
- package/dist/cjs/hash/shake.js +318 -0
- package/dist/cjs/hash/shake.js.map +1 -0
- package/dist/cjs/index.js +16 -11
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/{ringlwe.js → session/baselwe.js} +246 -203
- package/dist/cjs/session/baselwe.js.map +1 -0
- package/dist/cjs/session/ringlwe.js +19 -0
- package/dist/cjs/session/ringlwe.js.map +1 -0
- package/dist/cjs/session/rringlwe.js +42 -0
- package/dist/cjs/session/rringlwe.js.map +1 -0
- package/dist/esm/cipher/chacha.js +150 -0
- package/dist/esm/cipher/chacha.js.map +1 -0
- package/dist/esm/cipher/cipher.js +30 -0
- package/dist/esm/cipher/cipher.js.map +1 -0
- package/dist/esm/cipher/gimli.js +139 -0
- package/dist/esm/cipher/gimli.js.map +1 -0
- package/dist/esm/core/kdf.js.map +1 -0
- package/dist/esm/core/mac.js +87 -0
- package/dist/esm/core/mac.js.map +1 -0
- package/dist/esm/{types.js.map → core/types.js.map} +1 -1
- package/dist/esm/{utils.js → core/utils.js} +24 -4
- package/dist/esm/core/utils.js.map +1 -0
- package/dist/esm/core/wasm_loader.js +64 -0
- package/dist/esm/core/wasm_loader.js.map +1 -0
- package/dist/esm/crypto.js +30 -13
- package/dist/esm/crypto.js.map +1 -1
- package/dist/esm/{sha.js → hash/sha.js} +3 -2
- package/dist/esm/hash/sha.js.map +1 -0
- package/dist/esm/hash/shake.js +311 -0
- package/dist/esm/hash/shake.js.map +1 -0
- package/dist/esm/index.js +16 -11
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/{ringlwe.js → session/baselwe.js} +244 -201
- package/dist/esm/session/baselwe.js.map +1 -0
- package/dist/esm/session/ringlwe.js +15 -0
- package/dist/esm/session/ringlwe.js.map +1 -0
- package/dist/esm/session/rringlwe.js +38 -0
- package/dist/esm/session/rringlwe.js.map +1 -0
- package/dist/shake.wasm +0 -0
- package/dist/types/cipher/chacha.d.ts +67 -0
- package/dist/types/cipher/cipher.d.ts +31 -0
- package/dist/types/cipher/gimli.d.ts +64 -0
- package/dist/types/{kdf.d.ts → core/kdf.d.ts} +3 -2
- package/dist/types/{mac.d.ts → core/mac.d.ts} +21 -2
- package/dist/types/{types.d.ts → core/types.d.ts} +5 -2
- package/dist/types/{utils.d.ts → core/utils.d.ts} +13 -0
- package/dist/types/core/wasm_loader.d.ts +5 -0
- package/dist/types/crypto.d.ts +8 -4
- package/dist/types/{sha.d.ts → hash/sha.d.ts} +3 -2
- package/dist/types/hash/shake.d.ts +85 -0
- package/dist/types/index.d.ts +15 -10
- package/dist/types/{ringlwe.d.ts → session/baselwe.d.ts} +87 -91
- package/dist/types/session/ringlwe.d.ts +21 -0
- package/dist/types/session/rringlwe.d.ts +27 -0
- package/package.json +13 -3
- package/wasm/shake.wasm +0 -0
- package/dist/cjs/cipher.js +0 -265
- package/dist/cjs/cipher.js.map +0 -1
- package/dist/cjs/kdf.js.map +0 -1
- package/dist/cjs/mac.js.map +0 -1
- package/dist/cjs/ringlwe.js.map +0 -1
- package/dist/cjs/sha.js.map +0 -1
- package/dist/cjs/shake.js +0 -159
- package/dist/cjs/shake.js.map +0 -1
- package/dist/cjs/utils.js.map +0 -1
- package/dist/esm/cipher.js +0 -259
- package/dist/esm/cipher.js.map +0 -1
- package/dist/esm/kdf.js.map +0 -1
- package/dist/esm/mac.js +0 -51
- package/dist/esm/mac.js.map +0 -1
- package/dist/esm/ringlwe.js.map +0 -1
- package/dist/esm/sha.js.map +0 -1
- package/dist/esm/shake.js +0 -155
- package/dist/esm/shake.js.map +0 -1
- package/dist/esm/utils.js.map +0 -1
- package/dist/types/cipher.d.ts +0 -153
- package/dist/types/shake.d.ts +0 -53
- package/img/cover.png +0 -0
- package/src/cipher.ts +0 -260
- package/src/crypto.ts +0 -306
- package/src/index.ts +0 -27
- package/src/kdf.ts +0 -64
- package/src/mac.ts +0 -65
- package/src/ringlwe.ts +0 -289
- package/src/sha.ts +0 -265
- package/src/shake.ts +0 -163
- package/src/types.ts +0 -75
- package/src/utils.ts +0 -290
- package/tsconfig.cjs.json +0 -7
- package/tsconfig.json +0 -18
- /package/dist/cjs/{kdf.js → core/kdf.js} +0 -0
- /package/dist/cjs/{types.js → core/types.js} +0 -0
- /package/dist/esm/{kdf.js → core/kdf.js} +0 -0
- /package/dist/esm/{types.js → core/types.js} +0 -0
package/dist/types/cipher.d.ts
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* QuarkDash Ciphers Implementation
|
|
3
|
-
*
|
|
4
|
-
* @git https://github.com/devsdaddy/quarkdash
|
|
5
|
-
* @version 1.0.0
|
|
6
|
-
* @author Elijah Rastorguev
|
|
7
|
-
* @build 1000
|
|
8
|
-
* @website https://dev.to/devsdaddy
|
|
9
|
-
*/
|
|
10
|
-
import { ICipher } from "./types";
|
|
11
|
-
/**
|
|
12
|
-
* Cipher Type
|
|
13
|
-
*/
|
|
14
|
-
export declare enum CipherType {
|
|
15
|
-
ChaCha20 = 0,
|
|
16
|
-
Gimli = 1
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Cipher Factory
|
|
20
|
-
*/
|
|
21
|
-
export declare class CipherFactory {
|
|
22
|
-
/**
|
|
23
|
-
* Create Cipher
|
|
24
|
-
* @param algorithm {CipherType} Current cipher type
|
|
25
|
-
* @param key {Uint8Array} Key buffer
|
|
26
|
-
* @param nonce {Uint8Array} Nonce buffer
|
|
27
|
-
* @returns {ICipher} Cipher class instance
|
|
28
|
-
*/
|
|
29
|
-
static create(algorithm: CipherType, key: Uint8Array, nonce: Uint8Array): ICipher;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* ChaCha20 Based Cipher Implementation
|
|
33
|
-
*/
|
|
34
|
-
export declare class QuarkDashChaCha implements ICipher {
|
|
35
|
-
private readonly key;
|
|
36
|
-
private readonly nonce;
|
|
37
|
-
/**
|
|
38
|
-
* Create ChaCha20 Cipher
|
|
39
|
-
* @param key {Uint8Array} Key buffer
|
|
40
|
-
* @param nonce {Uint8Array} Nonce buffer
|
|
41
|
-
*/
|
|
42
|
-
constructor(key: Uint8Array, nonce: Uint8Array);
|
|
43
|
-
/**
|
|
44
|
-
* Encrypt data async using ChaCha20
|
|
45
|
-
* @param data {Uint8Array} Raw data buffer
|
|
46
|
-
* @returns {Promise<Uint8Array>} Result buffer
|
|
47
|
-
* TODO: GPU Calculations
|
|
48
|
-
*/
|
|
49
|
-
encrypt(data: Uint8Array): Promise<Uint8Array>;
|
|
50
|
-
/**
|
|
51
|
-
* Decrypt data async using ChaCha20
|
|
52
|
-
* @param data {Uint8Array} Encrypted raw data buffer
|
|
53
|
-
* @returns {Promise<Uint8Array>} Result buffer
|
|
54
|
-
* TODO: GPU Calculations
|
|
55
|
-
*/
|
|
56
|
-
decrypt(data: Uint8Array): Promise<Uint8Array>;
|
|
57
|
-
/**
|
|
58
|
-
* Encrypt data sync using ChaCha20
|
|
59
|
-
* @param data {Uint8Array} Raw data buffer
|
|
60
|
-
* @returns {Uint8Array} Result buffer
|
|
61
|
-
*/
|
|
62
|
-
encryptSync(data: Uint8Array): Uint8Array;
|
|
63
|
-
/**
|
|
64
|
-
* Decrypt data sync using ChaCha20
|
|
65
|
-
* @param data {Uint8Array} Encrypted raw data buffer
|
|
66
|
-
* @returns {Uint8Array} Result buffer
|
|
67
|
-
*/
|
|
68
|
-
decryptSync(data: Uint8Array): Uint8Array;
|
|
69
|
-
/**
|
|
70
|
-
* Process ChaCha20 Cipher
|
|
71
|
-
* @param data {Uint8Array} Data for processing
|
|
72
|
-
* @returns {Uint8Array} Processing result
|
|
73
|
-
* @private
|
|
74
|
-
*/
|
|
75
|
-
private process;
|
|
76
|
-
/**
|
|
77
|
-
* Get keystream block
|
|
78
|
-
* @param counter {number} counter
|
|
79
|
-
* @returns {Uint8Array} result buffer
|
|
80
|
-
* @private
|
|
81
|
-
*/
|
|
82
|
-
private keystreamBlock;
|
|
83
|
-
/**
|
|
84
|
-
* Quarter Round
|
|
85
|
-
* @param s {Uint8Array} Buffer
|
|
86
|
-
* @param a {number}
|
|
87
|
-
* @param b {number}
|
|
88
|
-
* @param c {number}
|
|
89
|
-
* @param d {number}
|
|
90
|
-
* @private
|
|
91
|
-
*/
|
|
92
|
-
private quarterRound;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Gimli Cipher
|
|
96
|
-
*/
|
|
97
|
-
export declare class QuarkDashGimli implements ICipher {
|
|
98
|
-
private readonly key;
|
|
99
|
-
private readonly nonce;
|
|
100
|
-
/**
|
|
101
|
-
* Create Gimli Cipher
|
|
102
|
-
* @param key {Uint8Array} Key buffer
|
|
103
|
-
* @param nonce {Uint8Array} Nonce buffer
|
|
104
|
-
*/
|
|
105
|
-
constructor(key: Uint8Array, nonce: Uint8Array);
|
|
106
|
-
/**
|
|
107
|
-
* Encrypt data async using Gimli
|
|
108
|
-
* @param data {Uint8Array} Raw data buffer
|
|
109
|
-
* @returns {Promise<Uint8Array>} Result buffer
|
|
110
|
-
* TODO: GPU Calculations
|
|
111
|
-
*/
|
|
112
|
-
encrypt(data: Uint8Array): Promise<Uint8Array>;
|
|
113
|
-
/**
|
|
114
|
-
* Decrypt data async using Gimli
|
|
115
|
-
* @param data {Uint8Array} Encrypted raw data buffer
|
|
116
|
-
* @returns {Promise<Uint8Array>} Result buffer
|
|
117
|
-
* TODO: GPU Calculations
|
|
118
|
-
*/
|
|
119
|
-
decrypt(data: Uint8Array): Promise<Uint8Array>;
|
|
120
|
-
/**
|
|
121
|
-
* Encrypt data sync using Gimli
|
|
122
|
-
* @param data {Uint8Array} Raw data buffer
|
|
123
|
-
* @returns {Uint8Array} Result buffer
|
|
124
|
-
*/
|
|
125
|
-
encryptSync(data: Uint8Array): Uint8Array;
|
|
126
|
-
/**
|
|
127
|
-
* Decrypt data sync using Gimli
|
|
128
|
-
* @param data {Uint8Array} Encrypted raw data buffer
|
|
129
|
-
* @returns {Uint8Array} Result buffer
|
|
130
|
-
*/
|
|
131
|
-
decryptSync(data: Uint8Array): Uint8Array;
|
|
132
|
-
/**
|
|
133
|
-
* Process Gimli Cipher
|
|
134
|
-
* @param data {Uint8Array} Input buffer
|
|
135
|
-
* @returns {Uint8Array} Output buffer
|
|
136
|
-
* @private
|
|
137
|
-
*/
|
|
138
|
-
private process;
|
|
139
|
-
/**
|
|
140
|
-
* Get keystream block
|
|
141
|
-
* @param counter {number} Counter
|
|
142
|
-
* @returns {Uint8Array} Result buffer
|
|
143
|
-
* @private
|
|
144
|
-
*/
|
|
145
|
-
private keystreamBlock;
|
|
146
|
-
/**
|
|
147
|
-
* Gimli Round
|
|
148
|
-
* @param state {Uint32Array} State buffer
|
|
149
|
-
* @param round {number} Round number
|
|
150
|
-
* @private
|
|
151
|
-
*/
|
|
152
|
-
private gimliRound;
|
|
153
|
-
}
|
package/dist/types/shake.d.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* QuarkDash Crypto SHAKE-256 Implementation
|
|
3
|
-
*
|
|
4
|
-
* @git https://github.com/devsdaddy/quarkdash
|
|
5
|
-
* @version 1.0.0
|
|
6
|
-
* @author Elijah Rastorguev
|
|
7
|
-
* @build 1000
|
|
8
|
-
* @website https://dev.to/devsdaddy
|
|
9
|
-
*/
|
|
10
|
-
/**
|
|
11
|
-
* Shake-256 Hash
|
|
12
|
-
*/
|
|
13
|
-
export declare class Shake256 {
|
|
14
|
-
private static KECCAK_ROUNDS;
|
|
15
|
-
private static RATE_BYTES;
|
|
16
|
-
private static ROTATIONS;
|
|
17
|
-
private static RC;
|
|
18
|
-
/**
|
|
19
|
-
* Shake-256 async
|
|
20
|
-
* @param input {Uint8Array} Input buffer
|
|
21
|
-
* @param outputLength {number} Output buffer length
|
|
22
|
-
* @returns {Uint8Array} Output buffer
|
|
23
|
-
*/
|
|
24
|
-
static hash(input: Uint8Array, outputLength: number): Promise<Uint8Array>;
|
|
25
|
-
/**
|
|
26
|
-
* Shake-256 sync
|
|
27
|
-
* @param input {Uint8Array} Input buffer
|
|
28
|
-
* @param outputLength {number} Output buffer length
|
|
29
|
-
* @returns {Uint8Array} Output buffer
|
|
30
|
-
*/
|
|
31
|
-
static hashSync(input: Uint8Array, outputLength: number): Uint8Array;
|
|
32
|
-
/**
|
|
33
|
-
* Process SHAKE-256
|
|
34
|
-
* @param input {Uint8Array} Input buffer
|
|
35
|
-
* @param outputLength {number} Output length
|
|
36
|
-
* @returns {Uint8Array} Output buffer
|
|
37
|
-
* @private
|
|
38
|
-
*/
|
|
39
|
-
private static process;
|
|
40
|
-
/**
|
|
41
|
-
* Keccak Function
|
|
42
|
-
* @param state {bigint[]} State array
|
|
43
|
-
* @private
|
|
44
|
-
*/
|
|
45
|
-
private static keccakF;
|
|
46
|
-
/**
|
|
47
|
-
* Rotate function
|
|
48
|
-
* @param x {number}
|
|
49
|
-
* @param n {number}
|
|
50
|
-
* @private
|
|
51
|
-
*/
|
|
52
|
-
private static rot;
|
|
53
|
-
}
|
package/img/cover.png
DELETED
|
Binary file
|
package/src/cipher.ts
DELETED
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* QuarkDash Ciphers Implementation
|
|
3
|
-
*
|
|
4
|
-
* @git https://github.com/devsdaddy/quarkdash
|
|
5
|
-
* @version 1.0.0
|
|
6
|
-
* @author Elijah Rastorguev
|
|
7
|
-
* @build 1000
|
|
8
|
-
* @website https://dev.to/devsdaddy
|
|
9
|
-
*/
|
|
10
|
-
/* Import Required Modules */
|
|
11
|
-
import {ICipher} from "./types";
|
|
12
|
-
import {QuarkDashUtils} from "./utils";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Cipher Type
|
|
16
|
-
*/
|
|
17
|
-
export enum CipherType {
|
|
18
|
-
ChaCha20 = 0,
|
|
19
|
-
Gimli = 1
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Cipher Factory
|
|
24
|
-
*/
|
|
25
|
-
export class CipherFactory {
|
|
26
|
-
/**
|
|
27
|
-
* Create Cipher
|
|
28
|
-
* @param algorithm {CipherType} Current cipher type
|
|
29
|
-
* @param key {Uint8Array} Key buffer
|
|
30
|
-
* @param nonce {Uint8Array} Nonce buffer
|
|
31
|
-
* @returns {ICipher} Cipher class instance
|
|
32
|
-
*/
|
|
33
|
-
static create(algorithm: CipherType, key: Uint8Array, nonce: Uint8Array): ICipher {
|
|
34
|
-
switch(algorithm) {
|
|
35
|
-
case CipherType.ChaCha20: return new QuarkDashChaCha(key, nonce);
|
|
36
|
-
case CipherType.Gimli: return new QuarkDashGimli(key, nonce);
|
|
37
|
-
default: throw new Error('Unsupported cipher type');
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* ChaCha20 Based Cipher Implementation
|
|
44
|
-
*/
|
|
45
|
-
export class QuarkDashChaCha implements ICipher {
|
|
46
|
-
// Key and Nonce
|
|
47
|
-
private readonly key: Uint8Array;
|
|
48
|
-
private readonly nonce: Uint8Array;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Create ChaCha20 Cipher
|
|
52
|
-
* @param key {Uint8Array} Key buffer
|
|
53
|
-
* @param nonce {Uint8Array} Nonce buffer
|
|
54
|
-
*/
|
|
55
|
-
constructor(key: Uint8Array, nonce: Uint8Array) {
|
|
56
|
-
if (key.length !== 32) throw new Error('Key must be 32 bytes');
|
|
57
|
-
if (nonce.length !== 12) throw new Error('Nonce must be 12 bytes');
|
|
58
|
-
this.key = key;
|
|
59
|
-
this.nonce = nonce;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Encrypt data async using ChaCha20
|
|
64
|
-
* @param data {Uint8Array} Raw data buffer
|
|
65
|
-
* @returns {Promise<Uint8Array>} Result buffer
|
|
66
|
-
* TODO: GPU Calculations
|
|
67
|
-
*/
|
|
68
|
-
public async encrypt(data: Uint8Array): Promise<Uint8Array> { return this.process(data); }
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Decrypt data async using ChaCha20
|
|
72
|
-
* @param data {Uint8Array} Encrypted raw data buffer
|
|
73
|
-
* @returns {Promise<Uint8Array>} Result buffer
|
|
74
|
-
* TODO: GPU Calculations
|
|
75
|
-
*/
|
|
76
|
-
public async decrypt(data: Uint8Array): Promise<Uint8Array> { return this.process(data); }
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Encrypt data sync using ChaCha20
|
|
80
|
-
* @param data {Uint8Array} Raw data buffer
|
|
81
|
-
* @returns {Uint8Array} Result buffer
|
|
82
|
-
*/
|
|
83
|
-
public encryptSync(data: Uint8Array): Uint8Array { return this.process(data); }
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Decrypt data sync using ChaCha20
|
|
87
|
-
* @param data {Uint8Array} Encrypted raw data buffer
|
|
88
|
-
* @returns {Uint8Array} Result buffer
|
|
89
|
-
*/
|
|
90
|
-
public decryptSync(data: Uint8Array): Uint8Array { return this.process(data); }
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Process ChaCha20 Cipher
|
|
94
|
-
* @param data {Uint8Array} Data for processing
|
|
95
|
-
* @returns {Uint8Array} Processing result
|
|
96
|
-
* @private
|
|
97
|
-
*/
|
|
98
|
-
private process(data: Uint8Array): Uint8Array {
|
|
99
|
-
const out = new Uint8Array(data.length);
|
|
100
|
-
let block = 0;
|
|
101
|
-
let pos = 0;
|
|
102
|
-
while (pos < data.length) {
|
|
103
|
-
const ks = this.keystreamBlock(block);
|
|
104
|
-
const len = Math.min(64, data.length - pos);
|
|
105
|
-
for (let i = 0; i < len; i++) out[pos+i] = data[pos+i] ^ ks[i];
|
|
106
|
-
pos += len;
|
|
107
|
-
block++;
|
|
108
|
-
}
|
|
109
|
-
return out;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Get keystream block
|
|
114
|
-
* @param counter {number} counter
|
|
115
|
-
* @returns {Uint8Array} result buffer
|
|
116
|
-
* @private
|
|
117
|
-
*/
|
|
118
|
-
private keystreamBlock(counter: number): Uint8Array {
|
|
119
|
-
const state = new Uint32Array(16);
|
|
120
|
-
state[0]=0x61707865; state[1]=0x3320646e; state[2]=0x79622d32; state[3]=0x6b206574;
|
|
121
|
-
for (let i=0;i<8;i++) state[4+i] = QuarkDashUtils.readU32(this.key, i*4);
|
|
122
|
-
state[12] = counter;
|
|
123
|
-
for (let i=0;i<3;i++) state[13+i] = QuarkDashUtils.readU32(this.nonce, i*4);
|
|
124
|
-
const working = new Uint32Array(state);
|
|
125
|
-
for (let r=0;r<10;r++) {
|
|
126
|
-
this.quarterRound(working,0,4,8,12); this.quarterRound(working,1,5,9,13);
|
|
127
|
-
this.quarterRound(working,2,6,10,14); this.quarterRound(working,3,7,11,15);
|
|
128
|
-
this.quarterRound(working,0,5,10,15); this.quarterRound(working,1,6,11,12);
|
|
129
|
-
this.quarterRound(working,2,7,8,13); this.quarterRound(working,3,4,9,14);
|
|
130
|
-
}
|
|
131
|
-
for(let i=0;i<16;i++) working[i] += state[i];
|
|
132
|
-
const out = new Uint8Array(64);
|
|
133
|
-
for(let i=0;i<16;i++) QuarkDashUtils.writeU32(working[i], out, i*4);
|
|
134
|
-
return out;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Quarter Round
|
|
139
|
-
* @param s {Uint8Array} Buffer
|
|
140
|
-
* @param a {number}
|
|
141
|
-
* @param b {number}
|
|
142
|
-
* @param c {number}
|
|
143
|
-
* @param d {number}
|
|
144
|
-
* @private
|
|
145
|
-
*/
|
|
146
|
-
private quarterRound(s:Uint32Array, a:number,b:number,c:number,d:number){
|
|
147
|
-
s[a] += s[b]; s[d] ^= s[a]; s[d] = (s[d]<<16)|(s[d]>>>16);
|
|
148
|
-
s[c] += s[d]; s[b] ^= s[c]; s[b] = (s[b]<<12)|(s[b]>>>20);
|
|
149
|
-
s[a] += s[b]; s[d] ^= s[a]; s[d] = (s[d]<<8)|(s[d]>>>24);
|
|
150
|
-
s[c] += s[d]; s[b] ^= s[c]; s[b] = (s[b]<<7)|(s[b]>>>25);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Gimli Cipher
|
|
156
|
-
*/
|
|
157
|
-
export class QuarkDashGimli implements ICipher {
|
|
158
|
-
// Key and Nonce
|
|
159
|
-
private readonly key: Uint8Array;
|
|
160
|
-
private readonly nonce: Uint8Array;
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Create Gimli Cipher
|
|
164
|
-
* @param key {Uint8Array} Key buffer
|
|
165
|
-
* @param nonce {Uint8Array} Nonce buffer
|
|
166
|
-
*/
|
|
167
|
-
constructor(key: Uint8Array, nonce: Uint8Array) {
|
|
168
|
-
if (key.length !== 32) throw new Error('Key must be 32 bytes');
|
|
169
|
-
if (nonce.length !== 12) throw new Error('Nonce must be 12 bytes');
|
|
170
|
-
this.key = key;
|
|
171
|
-
this.nonce = nonce;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Encrypt data async using Gimli
|
|
176
|
-
* @param data {Uint8Array} Raw data buffer
|
|
177
|
-
* @returns {Promise<Uint8Array>} Result buffer
|
|
178
|
-
* TODO: GPU Calculations
|
|
179
|
-
*/
|
|
180
|
-
public async encrypt(data: Uint8Array): Promise<Uint8Array> { return this.process(data); }
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Decrypt data async using Gimli
|
|
184
|
-
* @param data {Uint8Array} Encrypted raw data buffer
|
|
185
|
-
* @returns {Promise<Uint8Array>} Result buffer
|
|
186
|
-
* TODO: GPU Calculations
|
|
187
|
-
*/
|
|
188
|
-
public async decrypt(data: Uint8Array): Promise<Uint8Array> { return this.process(data); }
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Encrypt data sync using Gimli
|
|
192
|
-
* @param data {Uint8Array} Raw data buffer
|
|
193
|
-
* @returns {Uint8Array} Result buffer
|
|
194
|
-
*/
|
|
195
|
-
public encryptSync(data: Uint8Array): Uint8Array { return this.process(data); }
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Decrypt data sync using Gimli
|
|
199
|
-
* @param data {Uint8Array} Encrypted raw data buffer
|
|
200
|
-
* @returns {Uint8Array} Result buffer
|
|
201
|
-
*/
|
|
202
|
-
public decryptSync(data: Uint8Array): Uint8Array { return this.process(data); }
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Process Gimli Cipher
|
|
206
|
-
* @param data {Uint8Array} Input buffer
|
|
207
|
-
* @returns {Uint8Array} Output buffer
|
|
208
|
-
* @private
|
|
209
|
-
*/
|
|
210
|
-
private process(data: Uint8Array): Uint8Array {
|
|
211
|
-
const out = new Uint8Array(data.length);
|
|
212
|
-
let block = 0;
|
|
213
|
-
let pos = 0;
|
|
214
|
-
while (pos < data.length) {
|
|
215
|
-
const ks = this.keystreamBlock(block);
|
|
216
|
-
const len = Math.min(48, data.length - pos);
|
|
217
|
-
for (let i = 0; i < len; i++) out[pos+i] = data[pos+i] ^ ks[i];
|
|
218
|
-
pos += len;
|
|
219
|
-
block++;
|
|
220
|
-
}
|
|
221
|
-
return out;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Get keystream block
|
|
226
|
-
* @param counter {number} Counter
|
|
227
|
-
* @returns {Uint8Array} Result buffer
|
|
228
|
-
* @private
|
|
229
|
-
*/
|
|
230
|
-
private keystreamBlock(counter: number): Uint8Array {
|
|
231
|
-
const state = new Uint32Array(12);
|
|
232
|
-
for (let i=0;i<8;i++) state[i] = QuarkDashUtils.readU32(this.key, i*4);
|
|
233
|
-
state[8] = QuarkDashUtils.readU32(this.nonce,0);
|
|
234
|
-
state[9] = QuarkDashUtils.readU32(this.nonce,4);
|
|
235
|
-
state[10] = QuarkDashUtils.readU32(this.nonce,8);
|
|
236
|
-
state[11] = counter;
|
|
237
|
-
for (let r=0;r<24;r++) this.gimliRound(state, r);
|
|
238
|
-
const out = new Uint8Array(48);
|
|
239
|
-
for(let i=0;i<12;i++) QuarkDashUtils.writeU32(state[i], out, i*4);
|
|
240
|
-
return out;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Gimli Round
|
|
245
|
-
* @param state {Uint32Array} State buffer
|
|
246
|
-
* @param round {number} Round number
|
|
247
|
-
* @private
|
|
248
|
-
*/
|
|
249
|
-
private gimliRound(state:Uint32Array, round:number){
|
|
250
|
-
for(let i=0;i<4;i++){
|
|
251
|
-
const x=state[i], y=state[i+4], z=state[i+8];
|
|
252
|
-
const newX = x ^ (z<<1) ^ ((y&z)<<2);
|
|
253
|
-
const newY = y ^ x ^ ((x|z)<<1);
|
|
254
|
-
const newZ = z ^ y ^ ((x&y)<<3);
|
|
255
|
-
state[i]=newX; state[i+4]=newY; state[i+8]=newZ;
|
|
256
|
-
}
|
|
257
|
-
const t=state[1]; state[1]=state[2]; state[2]=state[3]; state[3]=t;
|
|
258
|
-
if((round&3)===0) state[0] ^= (0x9e377900 | round);
|
|
259
|
-
}
|
|
260
|
-
}
|