@li0ard/dstu 0.1.9 → 0.2.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/README.md +1 -1
- package/const.d.ts +5 -14
- package/const.js +47 -1010
- package/dstu4145/const.js +15 -15
- package/dstu4145/ec/index.d.ts +4 -0
- package/dstu4145/ec/index.js +28 -27
- package/dstu4145/ecies.d.ts +2 -2
- package/dstu4145/ecies.js +13 -2
- package/dstu4145/gf2m/utils.js +1 -1
- package/dstu4145/index.js +26 -17
- package/dstu9041/curve.d.ts +1 -1
- package/dstu9041/curve.js +16 -29
- package/dstu9041/index.js +21 -25
- package/dstu9311/const.js +12 -12
- package/dstu9311/index.d.ts +1 -2
- package/dstu9311/index.js +37 -34
- package/gf/index.d.ts +5 -0
- package/gf/index.js +62 -0
- package/gost34311/index.d.ts +55 -0
- package/gost34311/index.js +167 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/iso15946.d.ts +1 -1
- package/iso15946.js +11 -6
- package/kalyna/index.d.ts +3 -5
- package/kalyna/index.js +119 -98
- package/keywrap.js +11 -13
- package/kupyna/index.d.ts +100 -133
- package/kupyna/index.js +192 -180
- package/modes/_guard.d.ts +4 -0
- package/modes/_guard.js +6 -0
- package/modes/cbc.d.ts +1 -1
- package/modes/cbc.js +6 -7
- package/modes/ccm.js +13 -4
- package/modes/cfb.js +43 -51
- package/modes/cmac.js +6 -2
- package/modes/ctr.js +6 -4
- package/modes/ecb.js +2 -2
- package/modes/gcm.d.ts +4 -1
- package/modes/gcm.js +33 -5
- package/modes/gmac.js +8 -9
- package/modes/index.d.ts +1 -1
- package/modes/index.js +1 -1
- package/modes/kw.js +10 -17
- package/modes/ofb.js +5 -4
- package/modes/xts.js +15 -25
- package/package.json +12 -9
- package/strumok/index.d.ts +4 -5
- package/strumok/index.js +132 -87
- package/utils.d.ts +7 -13
- package/utils.js +31 -61
- package/dstu9311/hash.d.ts +0 -41
- package/dstu9311/hash.js +0 -98
- package/kupyna/kmac.d.ts +0 -46
- package/kupyna/kmac.js +0 -125
package/kupyna/index.js
CHANGED
|
@@ -1,144 +1,147 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { abytes, aexists, ahash, aoutput, clean, createHasher, createView } from "@noble/hashes/utils.js";
|
|
2
|
+
import { HashMD } from "@noble/hashes/_md.js";
|
|
3
3
|
import { column } from "../kalyna/index.js";
|
|
4
|
-
import {
|
|
5
|
-
const
|
|
6
|
-
class Kupyna {
|
|
7
|
-
blockLen;
|
|
8
|
-
outputLen;
|
|
9
|
-
canXOF = false;
|
|
4
|
+
import { _U32 } from "../utils.js";
|
|
5
|
+
const R_LO = 0xF0F0F0F3, R_HI = 0x00F0F0F0;
|
|
6
|
+
class Kupyna extends HashMD {
|
|
10
7
|
rounds;
|
|
11
8
|
stSize;
|
|
12
|
-
|
|
9
|
+
offs;
|
|
13
10
|
s;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
m;
|
|
12
|
+
ap;
|
|
13
|
+
aq;
|
|
14
|
+
tmp;
|
|
17
15
|
constructor(blockLen) {
|
|
18
|
-
|
|
19
|
-
this.stSize =
|
|
20
|
-
this.threshold = blockLen - 12;
|
|
16
|
+
super(blockLen, blockLen / 2, 12, true);
|
|
17
|
+
this.stSize = blockLen >>> 3;
|
|
21
18
|
this.rounds = 4 * Math.log2(blockLen) - 14;
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
this.
|
|
27
|
-
this.
|
|
28
|
-
this.
|
|
29
|
-
this.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
this.
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
this.nx = 0;
|
|
41
|
-
}
|
|
42
|
-
data = data.slice(n);
|
|
43
|
-
}
|
|
44
|
-
while (data.length >= this.blockLen) {
|
|
45
|
-
this.F(bytesToUint64sLE(data.subarray(0, this.blockLen)));
|
|
46
|
-
this.nx = 0;
|
|
47
|
-
data = data.slice(this.blockLen);
|
|
19
|
+
this.offs = [0, 1, 2, 3, 4, 5, 6, blockLen === 64 ? 7 : 11];
|
|
20
|
+
const w = this.stSize * 2;
|
|
21
|
+
this.s = new Uint32Array(w);
|
|
22
|
+
this.m = new Uint32Array(w);
|
|
23
|
+
this.ap = new Uint32Array(w);
|
|
24
|
+
this.aq = new Uint32Array(w);
|
|
25
|
+
this.tmp = new Uint32Array(w);
|
|
26
|
+
this.s[0] = blockLen;
|
|
27
|
+
}
|
|
28
|
+
// Unused
|
|
29
|
+
get() { return []; }
|
|
30
|
+
set(..._args) { }
|
|
31
|
+
process(buf, offset = 0) {
|
|
32
|
+
for (let i = 0; i < this.m.length; i++)
|
|
33
|
+
this.m[i] = buf.getUint32(offset + (i << 2), true);
|
|
34
|
+
for (let i = 0; i < this.s.length; i++) {
|
|
35
|
+
this.ap[i] = this.s[i] ^ this.m[i];
|
|
36
|
+
this.aq[i] = this.m[i];
|
|
48
37
|
}
|
|
49
|
-
|
|
50
|
-
this.
|
|
51
|
-
this.
|
|
38
|
+
for (let r = 0; r < this.rounds; r += 2) {
|
|
39
|
+
this.P(this.ap, this.tmp, r);
|
|
40
|
+
this.Q(this.aq, this.tmp, r);
|
|
52
41
|
}
|
|
53
|
-
|
|
42
|
+
for (let i = 0; i < this.s.length; i++)
|
|
43
|
+
this.s[i] ^= this.ap[i] ^ this.aq[i];
|
|
54
44
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
this.digestInto(buffer);
|
|
58
|
-
return buffer;
|
|
45
|
+
roundClean() {
|
|
46
|
+
clean(this.m, this.ap, this.aq, this.tmp);
|
|
59
47
|
}
|
|
60
|
-
|
|
61
|
-
this.
|
|
62
|
-
this.
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
48
|
+
destroy() {
|
|
49
|
+
this.destroyed = true;
|
|
50
|
+
clean(this.s, this.buffer);
|
|
51
|
+
this.roundClean();
|
|
52
|
+
}
|
|
53
|
+
digestInto(out) {
|
|
54
|
+
aexists(this);
|
|
55
|
+
aoutput(out, this);
|
|
56
|
+
this.finished = true;
|
|
57
|
+
const threshold = this.blockLen - 12;
|
|
58
|
+
let pos = this.pos;
|
|
59
|
+
this.buffer[pos++] = 0x80;
|
|
60
|
+
if (pos > threshold) {
|
|
61
|
+
this.buffer.fill(0, pos);
|
|
62
|
+
this.process(this.view);
|
|
63
|
+
pos = 0;
|
|
72
64
|
}
|
|
73
|
-
|
|
74
|
-
this.
|
|
75
|
-
this.
|
|
65
|
+
this.buffer.fill(0, pos);
|
|
66
|
+
const length = this.length * 8;
|
|
67
|
+
this.view.setUint32(threshold, length >>> 0, true);
|
|
68
|
+
this.view.setUint32(threshold + 4, (length / 2 ** 32) | 0, true);
|
|
69
|
+
this.process(this.view);
|
|
76
70
|
this.outputTransform();
|
|
77
|
-
|
|
78
|
-
this.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
return column(x, i, this.stSize, 1, 2, 3, 4, 5, 6, this.blockLen == 64 ? 7 : 11);
|
|
71
|
+
this.roundClean();
|
|
72
|
+
const oview = out === this.buffer ? this.view : createView(out);
|
|
73
|
+
for (let i = this.outputLen >>> 2, j = 0; i < this.s.length; i++, j += 4)
|
|
74
|
+
oview.setUint32(j, this.s[i], true);
|
|
82
75
|
}
|
|
83
76
|
G(x, y) {
|
|
84
77
|
for (let i = 0; i < this.stSize; i++)
|
|
85
|
-
|
|
78
|
+
column(x, i, this.stSize, this.offs, y, i << 1);
|
|
86
79
|
}
|
|
87
80
|
P(x, y, round) {
|
|
88
|
-
for (let i = 0n; i < BigInt(this.stSize); i++)
|
|
89
|
-
x[Number(i)] ^= (i << 4n) ^ round;
|
|
90
|
-
const r1 = round + 1n;
|
|
91
81
|
for (let i = 0; i < this.stSize; i++)
|
|
92
|
-
|
|
82
|
+
x[i << 1] ^= (i << 4) ^ round;
|
|
83
|
+
const r1 = round + 1;
|
|
84
|
+
for (let i = 0; i < this.stSize; i++) {
|
|
85
|
+
const j = i << 1;
|
|
86
|
+
column(x, i, this.stSize, this.offs, y, j);
|
|
87
|
+
y[j] ^= (i << 4) ^ r1;
|
|
88
|
+
}
|
|
93
89
|
this.G(y, x);
|
|
94
90
|
}
|
|
95
91
|
Q(x, y, round) {
|
|
96
|
-
for (let
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
92
|
+
for (let i = 0; i < this.stSize; i++) {
|
|
93
|
+
const j = i << 1, tb = (((this.stSize - 1 - i) << 4) ^ round) & 0xFF, ch = (R_HI ^ (tb << 24)) >>> 0, lo = x[j] + R_LO;
|
|
94
|
+
x[j + 1] += ch + (lo > _U32 ? 1 : 0);
|
|
95
|
+
x[j] = lo;
|
|
96
|
+
}
|
|
97
|
+
const r1 = round + 1;
|
|
98
|
+
for (let i = 0; i < this.stSize; i++) {
|
|
99
|
+
const j = i << 1;
|
|
100
|
+
column(x, i, this.stSize, this.offs, y, j);
|
|
101
|
+
const tb = (((this.stSize - 1 - i) << 4) ^ r1) & 0xFF;
|
|
102
|
+
const ch = (R_HI ^ (tb << 24)) >>> 0;
|
|
103
|
+
const lo = y[j] + R_LO;
|
|
104
|
+
y[j + 1] += ch + (lo > _U32 ? 1 : 0);
|
|
105
|
+
y[j] = lo;
|
|
106
|
+
}
|
|
101
107
|
this.G(y, x);
|
|
102
108
|
}
|
|
103
109
|
outputTransform() {
|
|
104
|
-
|
|
105
|
-
for (let r =
|
|
106
|
-
this.P(
|
|
107
|
-
for (let
|
|
108
|
-
this.s[
|
|
110
|
+
this.ap.set(this.s);
|
|
111
|
+
for (let r = 0; r < this.rounds; r += 2)
|
|
112
|
+
this.P(this.ap, this.tmp, r);
|
|
113
|
+
for (let i = 0; i < this.s.length; i++)
|
|
114
|
+
this.s[i] ^= this.ap[i];
|
|
109
115
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
for (let column = 0; column < this.stSize; column++) {
|
|
115
|
-
AP1[column] = this.s[column] ^ b[column];
|
|
116
|
-
AQ1[column] = b[column];
|
|
117
|
-
}
|
|
118
|
-
for (let r = 0n; r < BigInt(this.rounds); r += 2n) {
|
|
119
|
-
this.P(AP1, tmp, r);
|
|
120
|
-
this.Q(AQ1, tmp, r);
|
|
121
|
-
}
|
|
122
|
-
for (let column = 0; column < this.stSize; column++)
|
|
123
|
-
this.s[column] ^= AP1[column] ^ AQ1[column];
|
|
116
|
+
_cloneInto(to) {
|
|
117
|
+
to ||= new this.constructor();
|
|
118
|
+
to.s.set(this.s);
|
|
119
|
+
return this._cloneIntoMeta(to);
|
|
124
120
|
}
|
|
125
121
|
}
|
|
126
122
|
class KupynaDerived {
|
|
127
|
-
hash;
|
|
128
123
|
slice;
|
|
129
124
|
outputLen;
|
|
130
125
|
blockLen;
|
|
131
126
|
canXOF = false;
|
|
132
|
-
|
|
127
|
+
h;
|
|
133
128
|
constructor(hash, slice) {
|
|
134
|
-
this.hash = hash;
|
|
135
129
|
this.slice = slice;
|
|
130
|
+
ahash(hash);
|
|
136
131
|
this.outputLen = Math.abs(slice);
|
|
137
|
-
this.blockLen = hash
|
|
132
|
+
this.blockLen = hash.blockLen;
|
|
133
|
+
this.h = hash.create();
|
|
138
134
|
}
|
|
139
|
-
destroy() { }
|
|
135
|
+
destroy() { this.h.destroy(); }
|
|
136
|
+
_cloneInto(to) {
|
|
137
|
+
to ||= new this.constructor();
|
|
138
|
+
to.h = this.h.clone();
|
|
139
|
+
return to;
|
|
140
|
+
}
|
|
141
|
+
clone() { return this._cloneInto(); }
|
|
140
142
|
update(data) {
|
|
141
|
-
|
|
143
|
+
abytes(data, undefined, "data");
|
|
144
|
+
this.h.update(data);
|
|
142
145
|
return this;
|
|
143
146
|
}
|
|
144
147
|
digest() {
|
|
@@ -147,88 +150,97 @@ class KupynaDerived {
|
|
|
147
150
|
return buffer;
|
|
148
151
|
}
|
|
149
152
|
digestInto(buffer) {
|
|
150
|
-
|
|
153
|
+
aoutput(buffer, this);
|
|
154
|
+
buffer.set(this.h.digest().subarray(this.slice));
|
|
151
155
|
}
|
|
152
156
|
}
|
|
153
|
-
/** Kupyna
|
|
154
|
-
export class
|
|
155
|
-
/** Kupyna 256 bit version */
|
|
157
|
+
/** Internal Kupyna-256 hash class */
|
|
158
|
+
export class _Kupyna256 extends Kupyna {
|
|
156
159
|
constructor() { super(64); }
|
|
157
|
-
_cloneInto(to) {
|
|
158
|
-
to ||= new Kupyna256();
|
|
159
|
-
to.s = new BigUint64Array(this.s);
|
|
160
|
-
to.x = new Uint8Array(this.x);
|
|
161
|
-
to.nx = this.nx;
|
|
162
|
-
to.len = this.len;
|
|
163
|
-
return to;
|
|
164
|
-
}
|
|
165
|
-
clone() { return this._cloneInto(); }
|
|
166
|
-
/** Create hash instance */
|
|
167
|
-
static create() { return new Kupyna256(); }
|
|
168
160
|
}
|
|
169
|
-
/** Kupyna
|
|
170
|
-
export class
|
|
171
|
-
/** Kupyna 512 bit version */
|
|
161
|
+
/** Internal Kupyna-512 hash class */
|
|
162
|
+
export class _Kupyna512 extends Kupyna {
|
|
172
163
|
constructor() { super(128); }
|
|
173
|
-
_cloneInto(to) {
|
|
174
|
-
to ||= new Kupyna512();
|
|
175
|
-
to.s = new BigUint64Array(this.s);
|
|
176
|
-
to.x = new Uint8Array(this.x);
|
|
177
|
-
to.nx = this.nx;
|
|
178
|
-
to.len = this.len;
|
|
179
|
-
return to;
|
|
180
|
-
}
|
|
181
|
-
clone() { return this._cloneInto(); }
|
|
182
|
-
/** Create hash instance */
|
|
183
|
-
static create() { return new Kupyna512(); }
|
|
184
164
|
}
|
|
185
|
-
/**
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
165
|
+
/**
|
|
166
|
+
* Kupyna-256 hash function
|
|
167
|
+
*
|
|
168
|
+
* @param msg - message bytes to hash.
|
|
169
|
+
* @returns Digest bytes.
|
|
170
|
+
* @example
|
|
171
|
+
* ```ts
|
|
172
|
+
* import { kupyna256 } from "@li0ard/dstu/kupyna.js";
|
|
173
|
+
*
|
|
174
|
+
* kupyna256(new Uint8Array([97, 98, 99]));
|
|
175
|
+
* kupyna256.create().update(new Uint8Array([97, 98, 99])).digest();
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
export const kupyna256 = /* @__PURE__ */ createHasher(() => new _Kupyna256());
|
|
179
|
+
/**
|
|
180
|
+
* Kupyna-512 hash function
|
|
181
|
+
*
|
|
182
|
+
* @param msg - message bytes to hash.
|
|
183
|
+
* @returns Digest bytes.
|
|
184
|
+
* @example
|
|
185
|
+
* ```ts
|
|
186
|
+
* import { kupyna512 } from "@li0ard/dstu/kupyna.js";
|
|
187
|
+
*
|
|
188
|
+
* kupyna512(new Uint8Array([97, 98, 99]));
|
|
189
|
+
* kupyna512.create().update(new Uint8Array([97, 98, 99])).digest();
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
export const kupyna512 = /* @__PURE__ */ createHasher(() => new _Kupyna512());
|
|
193
|
+
/** Internal Kupyna-48 hash class */
|
|
194
|
+
export class _Kupyna48 extends KupynaDerived {
|
|
195
|
+
constructor() { super(kupyna256, -6); }
|
|
197
196
|
}
|
|
198
|
-
/** Kupyna
|
|
199
|
-
export class
|
|
200
|
-
|
|
201
|
-
constructor() { super(Kupyna512.create, -38); }
|
|
202
|
-
_cloneInto(to) {
|
|
203
|
-
to ||= new Kupyna304();
|
|
204
|
-
to.buffer = new Uint8Array(this.buffer);
|
|
205
|
-
return to;
|
|
206
|
-
}
|
|
207
|
-
clone() { return this._cloneInto(); }
|
|
208
|
-
/** Create hash instance */
|
|
209
|
-
static create() { return new Kupyna304(); }
|
|
197
|
+
/** Internal Kupyna-304 hash class */
|
|
198
|
+
export class _Kupyna304 extends KupynaDerived {
|
|
199
|
+
constructor() { super(kupyna512, -38); }
|
|
210
200
|
}
|
|
211
|
-
/** Kupyna
|
|
212
|
-
export class
|
|
213
|
-
|
|
214
|
-
constructor() { super(Kupyna512.create, -48); }
|
|
215
|
-
_cloneInto(to) {
|
|
216
|
-
to ||= new Kupyna384();
|
|
217
|
-
to.buffer = new Uint8Array(this.buffer);
|
|
218
|
-
return to;
|
|
219
|
-
}
|
|
220
|
-
clone() { return this._cloneInto(); }
|
|
221
|
-
/** Create hash instance */
|
|
222
|
-
static create() { return new Kupyna384(); }
|
|
201
|
+
/** Internal Kupyna-384 hash class */
|
|
202
|
+
export class _Kupyna384 extends KupynaDerived {
|
|
203
|
+
constructor() { super(kupyna512, -48); }
|
|
223
204
|
}
|
|
224
|
-
/**
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
205
|
+
/**
|
|
206
|
+
* Kupyna-48 hash function
|
|
207
|
+
*
|
|
208
|
+
* @param msg - message bytes to hash.
|
|
209
|
+
* @returns Digest bytes.
|
|
210
|
+
* @example
|
|
211
|
+
* ```ts
|
|
212
|
+
* import { kupyna48 } from "@li0ard/dstu/kupyna.js";
|
|
213
|
+
*
|
|
214
|
+
* kupyna48(new Uint8Array([97, 98, 99]));
|
|
215
|
+
* kupyna48.create().update(new Uint8Array([97, 98, 99])).digest();
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
export const kupyna48 = /* @__PURE__ */ createHasher(() => new _Kupyna48());
|
|
219
|
+
/**
|
|
220
|
+
* Kupyna-304 hash function
|
|
221
|
+
*
|
|
222
|
+
* @param msg - message bytes to hash.
|
|
223
|
+
* @returns Digest bytes.
|
|
224
|
+
* @example
|
|
225
|
+
* ```ts
|
|
226
|
+
* import { kupyna304 } from "@li0ard/dstu/kupyna.js";
|
|
227
|
+
*
|
|
228
|
+
* kupyna304(new Uint8Array([97, 98, 99]));
|
|
229
|
+
* kupyna304.create().update(new Uint8Array([97, 98, 99])).digest();
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
export const kupyna304 = /* @__PURE__ */ createHasher(() => new _Kupyna304());
|
|
233
|
+
/**
|
|
234
|
+
* Kupyna-384 hash function
|
|
235
|
+
*
|
|
236
|
+
* @param msg - message bytes to hash.
|
|
237
|
+
* @returns Digest bytes.
|
|
238
|
+
* @example
|
|
239
|
+
* ```ts
|
|
240
|
+
* import { kupyna384 } from "@li0ard/dstu/kupyna.js";
|
|
241
|
+
*
|
|
242
|
+
* kupyna384(new Uint8Array([97, 98, 99]));
|
|
243
|
+
* kupyna384.create().update(new Uint8Array([97, 98, 99])).digest();
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
246
|
+
export const kupyna384 = /* @__PURE__ */ createHasher(() => new _Kupyna384());
|
package/modes/_guard.js
ADDED
package/modes/cbc.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type TArg } from "@noble/hashes/utils.js";
|
|
2
2
|
import type { BlockMode, Cipher } from "../types.js";
|
|
3
3
|
/** Cipher Block Chaining (CBC) mode */
|
|
4
4
|
export declare const cbc: (cipher: Cipher, iv: TArg<Uint8Array>) => BlockMode;
|
package/modes/cbc.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { abytes } from "@noble/hashes/utils.js";
|
|
2
|
+
import { abytesAligned, xorBytes } from "../utils.js";
|
|
3
|
+
import { assertKalyna } from "./_guard.js";
|
|
2
4
|
/** Cipher Block Chaining (CBC) mode */
|
|
3
5
|
export const cbc = (cipher, iv) => {
|
|
4
6
|
assertKalyna(cipher);
|
|
5
|
-
|
|
6
|
-
throw new Error("Invalid IV size");
|
|
7
|
+
abytes(iv, cipher.blockSize, "iv");
|
|
7
8
|
return Object.freeze({
|
|
8
9
|
encrypt: (plaintext) => {
|
|
9
|
-
|
|
10
|
-
throw new Error("Plaintext not aligned");
|
|
10
|
+
abytesAligned(plaintext, cipher.blockSize, "plaintext");
|
|
11
11
|
let buf = iv;
|
|
12
12
|
const output = new Uint8Array(plaintext.length);
|
|
13
13
|
for (let i = 0; i < plaintext.length; i += cipher.blockSize) {
|
|
@@ -18,8 +18,7 @@ export const cbc = (cipher, iv) => {
|
|
|
18
18
|
return output;
|
|
19
19
|
},
|
|
20
20
|
decrypt: (ciphertext) => {
|
|
21
|
-
|
|
22
|
-
throw new Error("Ciphertext not aligned");
|
|
21
|
+
abytesAligned(ciphertext, cipher.blockSize, "ciphertext");
|
|
23
22
|
let buf = iv;
|
|
24
23
|
const output = new Uint8Array(ciphertext.length);
|
|
25
24
|
for (let i = 0; i < ciphertext.length; i += cipher.blockSize) {
|
package/modes/ccm.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { equalBytes, concatBytes } from "@noble/curves/utils.js";
|
|
2
|
-
import { assertKalyna } from "../utils.js";
|
|
1
|
+
import { abytes, anumber, equalBytes, concatBytes } from "@noble/curves/utils.js";
|
|
3
2
|
import { pad } from "../padding.js";
|
|
4
|
-
import {
|
|
3
|
+
import { half_ctr } from "./gcm.js";
|
|
4
|
+
import { assertKalyna } from "./_guard.js";
|
|
5
5
|
/** Counter with CBC-MAC (CCM) mode (AEAD) */
|
|
6
6
|
export const ccm = (cipher, iv, q = 16, Nb = 4) => {
|
|
7
7
|
assertKalyna(cipher);
|
|
@@ -9,7 +9,10 @@ export const ccm = (cipher, iv, q = 16, Nb = 4) => {
|
|
|
9
9
|
throw new Error('q must be 8, 16, 32, 48, or 64');
|
|
10
10
|
if (cipher.blockSize < Nb + 1)
|
|
11
11
|
throw new Error('Cipher block size must be >= Nb + 1');
|
|
12
|
+
abytes(iv, cipher.blockSize, "iv");
|
|
12
13
|
const tmp = cipher.blockSize - 1, tmp2 = tmp - Nb;
|
|
14
|
+
anumber(q, "q");
|
|
15
|
+
anumber(Nb, "Nb");
|
|
13
16
|
const calculateMac = (msg, aad) => {
|
|
14
17
|
aad ??= new Uint8Array();
|
|
15
18
|
const G1 = new Uint8Array(cipher.blockSize), G2 = new Uint8Array(cipher.blockSize), B = new Uint8Array(cipher.blockSize);
|
|
@@ -29,13 +32,19 @@ export const ccm = (cipher, iv, q = 16, Nb = 4) => {
|
|
|
29
32
|
updateMac(pad(msg, cipher.blockSize));
|
|
30
33
|
return B.slice(0, q);
|
|
31
34
|
};
|
|
32
|
-
const mode =
|
|
35
|
+
const mode = half_ctr(cipher, iv);
|
|
33
36
|
return Object.freeze({
|
|
34
37
|
seal: (plaintext, aad) => {
|
|
38
|
+
abytes(plaintext, undefined, "plaintext");
|
|
39
|
+
if (aad)
|
|
40
|
+
abytes(aad, undefined, "aad");
|
|
35
41
|
const mac = calculateMac(plaintext, aad);
|
|
36
42
|
return mode.crypt(concatBytes(plaintext, mac));
|
|
37
43
|
},
|
|
38
44
|
open: (ciphertext, aad) => {
|
|
45
|
+
abytes(ciphertext, undefined, "ciphertext");
|
|
46
|
+
if (aad)
|
|
47
|
+
abytes(aad, undefined, "aad");
|
|
39
48
|
const decrypted = mode.crypt(ciphertext);
|
|
40
49
|
const plaintext = decrypted.slice(0, -q);
|
|
41
50
|
const hC = calculateMac(plaintext, aad);
|