@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.
Files changed (55) hide show
  1. package/README.md +1 -1
  2. package/const.d.ts +5 -14
  3. package/const.js +47 -1010
  4. package/dstu4145/const.js +15 -15
  5. package/dstu4145/ec/index.d.ts +4 -0
  6. package/dstu4145/ec/index.js +28 -27
  7. package/dstu4145/ecies.d.ts +2 -2
  8. package/dstu4145/ecies.js +13 -2
  9. package/dstu4145/gf2m/utils.js +1 -1
  10. package/dstu4145/index.js +26 -17
  11. package/dstu9041/curve.d.ts +1 -1
  12. package/dstu9041/curve.js +16 -29
  13. package/dstu9041/index.js +21 -25
  14. package/dstu9311/const.js +12 -12
  15. package/dstu9311/index.d.ts +1 -2
  16. package/dstu9311/index.js +37 -34
  17. package/gf/index.d.ts +5 -0
  18. package/gf/index.js +62 -0
  19. package/gost34311/index.d.ts +55 -0
  20. package/gost34311/index.js +167 -0
  21. package/index.d.ts +1 -0
  22. package/index.js +1 -0
  23. package/iso15946.d.ts +1 -1
  24. package/iso15946.js +11 -6
  25. package/kalyna/index.d.ts +3 -5
  26. package/kalyna/index.js +119 -98
  27. package/keywrap.js +11 -13
  28. package/kupyna/index.d.ts +100 -133
  29. package/kupyna/index.js +192 -180
  30. package/modes/_guard.d.ts +4 -0
  31. package/modes/_guard.js +6 -0
  32. package/modes/cbc.d.ts +1 -1
  33. package/modes/cbc.js +6 -7
  34. package/modes/ccm.js +13 -4
  35. package/modes/cfb.js +43 -51
  36. package/modes/cmac.js +6 -2
  37. package/modes/ctr.js +6 -4
  38. package/modes/ecb.js +2 -2
  39. package/modes/gcm.d.ts +4 -1
  40. package/modes/gcm.js +33 -5
  41. package/modes/gmac.js +8 -9
  42. package/modes/index.d.ts +1 -1
  43. package/modes/index.js +1 -1
  44. package/modes/kw.js +10 -17
  45. package/modes/ofb.js +5 -4
  46. package/modes/xts.js +15 -25
  47. package/package.json +12 -9
  48. package/strumok/index.d.ts +4 -5
  49. package/strumok/index.js +132 -87
  50. package/utils.d.ts +7 -13
  51. package/utils.js +31 -61
  52. package/dstu9311/hash.d.ts +0 -41
  53. package/dstu9311/hash.js +0 -98
  54. package/kupyna/kmac.d.ts +0 -46
  55. package/kupyna/kmac.js +0 -125
package/kupyna/index.js CHANGED
@@ -1,144 +1,147 @@
1
- import { concatBytes, createHasher } from "@noble/hashes/utils.js";
2
- import { bytesToUint64sLE, uint64sToBytesLE } from "../utils.js";
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 { numberToBytesLE } from "@noble/curves/utils.js";
5
- const r = 0x00f0f0f0f0f0f0f3n;
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
- threshold;
9
+ offs;
13
10
  s;
14
- x;
15
- nx;
16
- len;
11
+ m;
12
+ ap;
13
+ aq;
14
+ tmp;
17
15
  constructor(blockLen) {
18
- this.blockLen = blockLen;
19
- this.stSize = (blockLen / 2) / 4;
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.outputLen = blockLen / 2;
23
- this.destroy();
24
- }
25
- destroy() {
26
- this.s = new BigUint64Array(this.stSize);
27
- this.x = new Uint8Array(this.blockLen);
28
- this.nx = 0;
29
- this.len = 0n;
30
- this.s[0] = BigInt(this.blockLen);
31
- }
32
- update(data) {
33
- this.len += BigInt(data.length);
34
- if (this.nx > 0) {
35
- const n = Math.min(this.blockLen - this.nx, data.length);
36
- this.x.set(data.subarray(0, n), this.nx);
37
- this.nx += n;
38
- if (this.nx === this.blockLen) {
39
- this.F(bytesToUint64sLE(this.x));
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
- if (data.length > 0) {
50
- this.x.set(data, 0);
51
- this.nx = data.length;
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
- return this;
42
+ for (let i = 0; i < this.s.length; i++)
43
+ this.s[i] ^= this.ap[i] ^ this.aq[i];
54
44
  }
55
- digest() {
56
- const buffer = new Uint8Array(this.outputLen);
57
- this.digestInto(buffer);
58
- return buffer;
45
+ roundClean() {
46
+ clean(this.m, this.ap, this.aq, this.tmp);
59
47
  }
60
- digestInto(buffer) {
61
- this.x[this.nx] = 0x80;
62
- this.nx++;
63
- const fillBytes = (start) => {
64
- const available = this.x.length - start;
65
- if (available > 0)
66
- this.x.fill(0, start, start + available);
67
- };
68
- if (this.nx > this.threshold) {
69
- fillBytes(this.nx);
70
- this.F(bytesToUint64sLE(this.x));
71
- this.nx = 0;
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
- fillBytes(this.nx);
74
- this.x.set(numberToBytesLE(this.len * 8n, 12), this.threshold);
75
- this.F(bytesToUint64sLE(this.x));
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
- buffer.set(uint64sToBytesLE(this.s).subarray(this.outputLen));
78
- this.destroy();
79
- }
80
- column(x, i) {
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
- y[i] = this.column(x, i);
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
- y[i] = this.column(x, i) ^ BigInt(i << 4) ^ r1;
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 j = 0n; j < BigInt(this.stSize); j++)
97
- x[Number(j)] += r ^ ((((BigInt(this.stSize - 1) - j) * 0x10n) ^ round) << 56n);
98
- const r1 = round + 1n;
99
- for (let i = 0; i < this.stSize; i++)
100
- y[i] = this.column(x, i) + (r ^ ((BigInt((this.stSize - 1 - i) * 16) ^ r1) << 56n));
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
- const t1 = new BigUint64Array(this.s), t2 = new BigUint64Array(this.stSize);
105
- for (let r = 0n; r < BigInt(this.rounds); r += 2n)
106
- this.P(t1, t2, r);
107
- for (let column = 0; column < this.stSize; column++)
108
- this.s[column] ^= t1[column];
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
- F(b) {
111
- const AQ1 = new BigUint64Array(this.stSize);
112
- const AP1 = new BigUint64Array(this.stSize);
113
- const tmp = new BigUint64Array(this.stSize);
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
- buffer = new Uint8Array();
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().blockLen;
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
- this.buffer = concatBytes(this.buffer, data);
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
- buffer.set(this.hash().update(this.buffer).digest().subarray(this.slice));
153
+ aoutput(buffer, this);
154
+ buffer.set(this.h.digest().subarray(this.slice));
151
155
  }
152
156
  }
153
- /** Kupyna 256 bit version */
154
- export class Kupyna256 extends Kupyna {
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 512 bit version */
170
- export class Kupyna512 extends Kupyna {
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
- /** Kupyna 48 bit version */
186
- export class Kupyna48 extends KupynaDerived {
187
- /** Kupyna 48 bit version */
188
- constructor() { super(Kupyna256.create, -6); }
189
- _cloneInto(to) {
190
- to ||= new Kupyna48();
191
- to.buffer = new Uint8Array(this.buffer);
192
- return to;
193
- }
194
- clone() { return this._cloneInto(); }
195
- /** Create hash instance */
196
- static create() { return new Kupyna48(); }
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 304 bit version */
199
- export class Kupyna304 extends KupynaDerived {
200
- /** Kupyna 304 bit version */
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 384 bit version */
212
- export class Kupyna384 extends KupynaDerived {
213
- /** Kupyna 384 bit version */
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
- /** Kupyna 48 bit version */
225
- export const kupyna48 = createHasher(Kupyna48.create);
226
- /** Kupyna 256 bit version */
227
- export const kupyna256 = createHasher(Kupyna256.create);
228
- /** Kupyna 304 bit version */
229
- export const kupyna304 = createHasher(Kupyna304.create);
230
- /** Kupyna 384 bit version */
231
- export const kupyna384 = createHasher(Kupyna384.create);
232
- /** Kupyna 512 bit version */
233
- export const kupyna512 = createHasher(Kupyna512.create);
234
- export * from "./kmac.js";
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());
@@ -0,0 +1,4 @@
1
+ import { Kalyna } from "../kalyna/index.js";
2
+ import type { Cipher } from "../types.js";
3
+ export declare const isKalyna: (cipher: Cipher) => cipher is Kalyna;
4
+ export declare const assertKalyna: (cipher: Cipher) => void;
@@ -0,0 +1,6 @@
1
+ import { Kalyna } from "../kalyna/index.js";
2
+ export const isKalyna = (cipher) => cipher instanceof Kalyna;
3
+ export const assertKalyna = (cipher) => {
4
+ if (!isKalyna(cipher))
5
+ throw new Error("Cipher mode only for Kalyna");
6
+ };
package/modes/cbc.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { TArg } from "@noble/hashes/utils.js";
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 { assertKalyna, xorBytes } from "../utils.js";
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
- if (iv.length !== cipher.blockSize)
6
- throw new Error("Invalid IV size");
7
+ abytes(iv, cipher.blockSize, "iv");
7
8
  return Object.freeze({
8
9
  encrypt: (plaintext) => {
9
- if (plaintext.length % cipher.blockSize !== 0)
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
- if (ciphertext.length % cipher.blockSize !== 0)
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 { ctr } from "./ctr.js";
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 = ctr(cipher, iv);
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);