@li0ard/dstu 0.1.8 → 0.2.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.
Files changed (53) hide show
  1. package/README.md +4 -2
  2. package/dstu4145/ec/index.d.ts +4 -0
  3. package/dstu4145/ec/index.js +19 -0
  4. package/dstu4145/ec/wnaf.d.ts +2 -1
  5. package/dstu4145/ecies.d.ts +82 -0
  6. package/dstu4145/ecies.js +72 -0
  7. package/dstu4145/gf2m/index.js +2 -11
  8. package/dstu4145/gf2m/onb.js +30 -16
  9. package/dstu4145/gf2m/utils.d.ts +1 -1
  10. package/dstu4145/gf2m/utils.js +12 -8
  11. package/dstu4145/index.d.ts +16 -225
  12. package/dstu4145/index.js +20 -26
  13. package/dstu9041/curve.d.ts +4 -12
  14. package/dstu9041/curve.js +5 -3
  15. package/dstu9041/index.d.ts +2 -8
  16. package/dstu9041/index.js +10 -6
  17. package/dstu9311/hash.d.ts +46 -12
  18. package/dstu9311/hash.js +89 -28
  19. package/dstu9311/index.js +4 -5
  20. package/iso15946.d.ts +3 -0
  21. package/iso15946.js +56 -0
  22. package/kalyna/index.d.ts +1 -1
  23. package/kalyna/index.js +6 -6
  24. package/keywrap.js +6 -3
  25. package/kupyna/index.d.ts +116 -67
  26. package/kupyna/index.js +122 -61
  27. package/kupyna/kmac.d.ts +6 -6
  28. package/kupyna/kmac.js +20 -15
  29. package/modes/cbc.d.ts +1 -1
  30. package/modes/cbc.js +5 -7
  31. package/modes/ccm.js +12 -3
  32. package/modes/cfb.js +42 -51
  33. package/modes/cmac.js +4 -1
  34. package/modes/ctr.js +4 -3
  35. package/modes/ecb.js +2 -2
  36. package/modes/gcm.d.ts +4 -1
  37. package/modes/gcm.js +32 -5
  38. package/modes/gmac.js +4 -1
  39. package/modes/index.d.ts +1 -1
  40. package/modes/index.js +1 -1
  41. package/modes/kw.d.ts +2 -6
  42. package/modes/kw.js +9 -16
  43. package/modes/ofb.js +3 -3
  44. package/modes/xts.d.ts +2 -6
  45. package/modes/xts.js +7 -3
  46. package/package.json +4 -4
  47. package/padding.d.ts +1 -1
  48. package/padding.js +5 -0
  49. package/strumok/index.d.ts +3 -2
  50. package/strumok/index.js +4 -2
  51. package/types.d.ts +77 -0
  52. package/utils.d.ts +1 -0
  53. package/utils.js +14 -1
package/kupyna/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
1
+ import { type CHash, type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
2
2
  declare abstract class Kupyna<T extends Kupyna<T>> implements Hash<Kupyna<T>> {
3
3
  readonly blockLen: number;
4
4
  readonly outputLen: number;
@@ -21,142 +21,191 @@ declare abstract class Kupyna<T extends Kupyna<T>> implements Hash<Kupyna<T>> {
21
21
  private Q;
22
22
  private outputTransform;
23
23
  private F;
24
- abstract clone(): Kupyna<T>;
25
- abstract _cloneInto(): Kupyna<T>;
24
+ clone(): Kupyna<T>;
25
+ abstract _cloneInto(to?: Kupyna<T>): Kupyna<T>;
26
26
  }
27
27
  declare abstract class KupynaDerived<T extends Kupyna<T>> implements Hash<KupynaDerived<T>> {
28
- hash: () => Kupyna<T>;
29
28
  private readonly slice;
30
29
  readonly outputLen: number;
31
30
  readonly blockLen: number;
32
31
  readonly canXOF = false;
33
- buffer: Uint8Array;
34
- constructor(hash: () => Kupyna<T>, slice: number);
32
+ protected h: Hash<T>;
33
+ constructor(hash: CHash, slice: number);
35
34
  destroy(): void;
36
- abstract clone(): KupynaDerived<T>;
37
- abstract _cloneInto(): KupynaDerived<T>;
35
+ abstract _cloneInto(to?: KupynaDerived<T>): KupynaDerived<T>;
36
+ clone(): KupynaDerived<T>;
38
37
  update(data: TArg<Uint8Array>): this;
39
38
  digest(): TRet<Uint8Array>;
40
39
  digestInto(buffer: TArg<Uint8Array>): void;
41
40
  }
42
- /** Kupyna 256 bit version */
43
- export declare class Kupyna256 extends Kupyna<Kupyna256> {
44
- /** Kupyna 256 bit version */
41
+ /** Internal Kupyna-256 hash class */
42
+ export declare class _Kupyna256 extends Kupyna<_Kupyna256> {
45
43
  constructor();
46
- _cloneInto(to?: Kupyna256): Kupyna256;
47
- clone(): Kupyna256;
44
+ _cloneInto(to?: _Kupyna256): _Kupyna256;
48
45
  /** Create hash instance */
49
- static create(): Kupyna256;
46
+ static create(): _Kupyna256;
50
47
  }
51
- /** Kupyna 512 bit version */
52
- export declare class Kupyna512 extends Kupyna<Kupyna512> {
53
- /** Kupyna 512 bit version */
48
+ /** Internal Kupyna-512 hash class */
49
+ export declare class _Kupyna512 extends Kupyna<_Kupyna512> {
54
50
  constructor();
55
- _cloneInto(to?: Kupyna512): Kupyna512;
56
- clone(): Kupyna512;
51
+ _cloneInto(to?: _Kupyna512): _Kupyna512;
57
52
  /** Create hash instance */
58
- static create(): Kupyna512;
53
+ static create(): _Kupyna512;
59
54
  }
60
- /** Kupyna 48 bit version */
61
- export declare class Kupyna48 extends KupynaDerived<Kupyna256> {
62
- /** Kupyna 48 bit version */
63
- constructor();
64
- _cloneInto(to?: Kupyna48): Kupyna48;
65
- clone(): Kupyna48;
66
- /** Create hash instance */
67
- static create(): Kupyna48;
68
- }
69
- /** Kupyna 304 bit version */
70
- export declare class Kupyna304 extends KupynaDerived<Kupyna512> {
71
- /** Kupyna 304 bit version */
72
- constructor();
73
- _cloneInto(to?: Kupyna304): Kupyna304;
74
- clone(): Kupyna304;
75
- /** Create hash instance */
76
- static create(): Kupyna304;
77
- }
78
- /** Kupyna 384 bit version */
79
- export declare class Kupyna384 extends KupynaDerived<Kupyna512> {
80
- /** Kupyna 384 bit version */
81
- constructor();
82
- _cloneInto(to?: Kupyna384): Kupyna384;
83
- clone(): Kupyna384;
84
- /** Create hash instance */
85
- static create(): Kupyna384;
86
- }
87
- /** Kupyna 48 bit version */
88
- export declare const kupyna48: {
55
+ /**
56
+ * Kupyna-256 hash function
57
+ *
58
+ * @param msg - message bytes to hash.
59
+ * @returns Digest bytes.
60
+ * @example
61
+ * ```ts
62
+ * import { kupyna256 } from "@li0ard/dstu/kupyna.js";
63
+ *
64
+ * kupyna256(new Uint8Array([97, 98, 99]));
65
+ * kupyna256.create().update(new Uint8Array([97, 98, 99])).digest();
66
+ * ```
67
+ */
68
+ export declare const kupyna256: {
89
69
  outputLen: number;
90
70
  blockLen: number;
91
71
  canXOF: boolean;
92
72
  } & import("@noble/hashes/utils.js").HashInfo & {
93
73
  (msg: TArg<Uint8Array>): TRet<Uint8Array>;
94
- create(): Kupyna48;
74
+ create(): _Kupyna256;
95
75
  } & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
96
76
  outputLen: number;
97
77
  blockLen: number;
98
78
  canXOF: boolean;
99
79
  oid?: TRet<Uint8Array> | undefined;
100
- create: () => Kupyna48;
80
+ create: () => _Kupyna256;
101
81
  };
102
- /** Kupyna 256 bit version */
103
- export declare const kupyna256: {
82
+ /**
83
+ * Kupyna-512 hash function
84
+ *
85
+ * @param msg - message bytes to hash.
86
+ * @returns Digest bytes.
87
+ * @example
88
+ * ```ts
89
+ * import { kupyna512 } from "@li0ard/dstu/kupyna.js";
90
+ *
91
+ * kupyna512(new Uint8Array([97, 98, 99]));
92
+ * kupyna512.create().update(new Uint8Array([97, 98, 99])).digest();
93
+ * ```
94
+ */
95
+ export declare const kupyna512: {
104
96
  outputLen: number;
105
97
  blockLen: number;
106
98
  canXOF: boolean;
107
99
  } & import("@noble/hashes/utils.js").HashInfo & {
108
100
  (msg: TArg<Uint8Array>): TRet<Uint8Array>;
109
- create(): Kupyna256;
101
+ create(): _Kupyna512;
110
102
  } & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
111
103
  outputLen: number;
112
104
  blockLen: number;
113
105
  canXOF: boolean;
114
106
  oid?: TRet<Uint8Array> | undefined;
115
- create: () => Kupyna256;
107
+ create: () => _Kupyna512;
116
108
  };
117
- /** Kupyna 304 bit version */
118
- export declare const kupyna304: {
109
+ /** Internal Kupyna-48 hash class */
110
+ export declare class _Kupyna48 extends KupynaDerived<_Kupyna256> {
111
+ constructor();
112
+ _cloneInto(to?: _Kupyna48): _Kupyna48;
113
+ /** Create hash instance */
114
+ static create(): _Kupyna48;
115
+ }
116
+ /** Internal Kupyna-304 hash class */
117
+ export declare class _Kupyna304 extends KupynaDerived<_Kupyna512> {
118
+ constructor();
119
+ _cloneInto(to?: _Kupyna304): _Kupyna304;
120
+ /** Create hash instance */
121
+ static create(): _Kupyna304;
122
+ }
123
+ /** Internal Kupyna-384 hash class */
124
+ export declare class _Kupyna384 extends KupynaDerived<_Kupyna512> {
125
+ constructor();
126
+ _cloneInto(to?: _Kupyna384): _Kupyna384;
127
+ /** Create hash instance */
128
+ static create(): _Kupyna384;
129
+ }
130
+ /**
131
+ * Kupyna-48 hash function
132
+ *
133
+ * @param msg - message bytes to hash.
134
+ * @returns Digest bytes.
135
+ * @example
136
+ * ```ts
137
+ * import { kupyna48 } from "@li0ard/dstu/kupyna.js";
138
+ *
139
+ * kupyna48(new Uint8Array([97, 98, 99]));
140
+ * kupyna48.create().update(new Uint8Array([97, 98, 99])).digest();
141
+ * ```
142
+ */
143
+ export declare const kupyna48: {
119
144
  outputLen: number;
120
145
  blockLen: number;
121
146
  canXOF: boolean;
122
147
  } & import("@noble/hashes/utils.js").HashInfo & {
123
148
  (msg: TArg<Uint8Array>): TRet<Uint8Array>;
124
- create(): Kupyna304;
149
+ create(): _Kupyna48;
125
150
  } & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
126
151
  outputLen: number;
127
152
  blockLen: number;
128
153
  canXOF: boolean;
129
154
  oid?: TRet<Uint8Array> | undefined;
130
- create: () => Kupyna304;
155
+ create: () => _Kupyna48;
131
156
  };
132
- /** Kupyna 384 bit version */
133
- export declare const kupyna384: {
157
+ /**
158
+ * Kupyna-304 hash function
159
+ *
160
+ * @param msg - message bytes to hash.
161
+ * @returns Digest bytes.
162
+ * @example
163
+ * ```ts
164
+ * import { kupyna304 } from "@li0ard/dstu/kupyna.js";
165
+ *
166
+ * kupyna304(new Uint8Array([97, 98, 99]));
167
+ * kupyna304.create().update(new Uint8Array([97, 98, 99])).digest();
168
+ * ```
169
+ */
170
+ export declare const kupyna304: {
134
171
  outputLen: number;
135
172
  blockLen: number;
136
173
  canXOF: boolean;
137
174
  } & import("@noble/hashes/utils.js").HashInfo & {
138
175
  (msg: TArg<Uint8Array>): TRet<Uint8Array>;
139
- create(): Kupyna384;
176
+ create(): _Kupyna304;
140
177
  } & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
141
178
  outputLen: number;
142
179
  blockLen: number;
143
180
  canXOF: boolean;
144
181
  oid?: TRet<Uint8Array> | undefined;
145
- create: () => Kupyna384;
182
+ create: () => _Kupyna304;
146
183
  };
147
- /** Kupyna 512 bit version */
148
- export declare const kupyna512: {
184
+ /**
185
+ * Kupyna-384 hash function
186
+ *
187
+ * @param msg - message bytes to hash.
188
+ * @returns Digest bytes.
189
+ * @example
190
+ * ```ts
191
+ * import { kupyna384 } from "@li0ard/dstu/kupyna.js";
192
+ *
193
+ * kupyna384(new Uint8Array([97, 98, 99]));
194
+ * kupyna384.create().update(new Uint8Array([97, 98, 99])).digest();
195
+ * ```
196
+ */
197
+ export declare const kupyna384: {
149
198
  outputLen: number;
150
199
  blockLen: number;
151
200
  canXOF: boolean;
152
201
  } & import("@noble/hashes/utils.js").HashInfo & {
153
202
  (msg: TArg<Uint8Array>): TRet<Uint8Array>;
154
- create(): Kupyna512;
203
+ create(): _Kupyna384;
155
204
  } & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
156
205
  outputLen: number;
157
206
  blockLen: number;
158
207
  canXOF: boolean;
159
208
  oid?: TRet<Uint8Array> | undefined;
160
- create: () => Kupyna512;
209
+ create: () => _Kupyna384;
161
210
  };
162
211
  export * from "./kmac.js";
package/kupyna/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { concatBytes, createHasher } from "@noble/hashes/utils.js";
1
+ import { abytes, ahash, anumber, aoutput, createHasher } from "@noble/hashes/utils.js";
2
2
  import { bytesToUint64sLE, uint64sToBytesLE } from "../utils.js";
3
3
  import { column } from "../kalyna/index.js";
4
4
  import { numberToBytesLE } from "@noble/curves/utils.js";
@@ -16,20 +16,26 @@ class Kupyna {
16
16
  len;
17
17
  constructor(blockLen) {
18
18
  this.blockLen = blockLen;
19
+ anumber(blockLen, "blockLen");
19
20
  this.stSize = (blockLen / 2) / 4;
20
21
  this.threshold = blockLen - 12;
21
22
  this.rounds = 4 * Math.log2(blockLen) - 14;
22
23
  this.outputLen = blockLen / 2;
23
- this.destroy();
24
- }
25
- destroy() {
26
24
  this.s = new BigUint64Array(this.stSize);
27
25
  this.x = new Uint8Array(this.blockLen);
28
26
  this.nx = 0;
29
27
  this.len = 0n;
30
28
  this.s[0] = BigInt(this.blockLen);
31
29
  }
30
+ destroy() {
31
+ this.s[0] = BigInt(this.blockLen);
32
+ this.s.fill(0n, 1);
33
+ this.x.fill(0);
34
+ this.nx = 0;
35
+ this.len = 0n;
36
+ }
32
37
  update(data) {
38
+ abytes(data, undefined, "data");
33
39
  this.len += BigInt(data.length);
34
40
  if (this.nx > 0) {
35
41
  const n = Math.min(this.blockLen - this.nx, data.length);
@@ -58,6 +64,7 @@ class Kupyna {
58
64
  return buffer;
59
65
  }
60
66
  digestInto(buffer) {
67
+ aoutput(buffer, this);
61
68
  this.x[this.nx] = 0x80;
62
69
  this.nx++;
63
70
  const fillBytes = (start) => {
@@ -122,23 +129,26 @@ class Kupyna {
122
129
  for (let column = 0; column < this.stSize; column++)
123
130
  this.s[column] ^= AP1[column] ^ AQ1[column];
124
131
  }
132
+ clone() { return this._cloneInto(); }
125
133
  }
126
134
  class KupynaDerived {
127
- hash;
128
135
  slice;
129
136
  outputLen;
130
137
  blockLen;
131
138
  canXOF = false;
132
- buffer = new Uint8Array();
139
+ h;
133
140
  constructor(hash, slice) {
134
- this.hash = hash;
135
141
  this.slice = slice;
142
+ ahash(hash);
136
143
  this.outputLen = Math.abs(slice);
137
- this.blockLen = hash().blockLen;
144
+ this.blockLen = hash.blockLen;
145
+ this.h = hash.create();
138
146
  }
139
- destroy() { }
147
+ destroy() { this.h.destroy(); }
148
+ clone() { return this._cloneInto(); }
140
149
  update(data) {
141
- this.buffer = concatBytes(this.buffer, data);
150
+ abytes(data, undefined, "data");
151
+ this.h.update(data);
142
152
  return this;
143
153
  }
144
154
  digest() {
@@ -147,88 +157,139 @@ class KupynaDerived {
147
157
  return buffer;
148
158
  }
149
159
  digestInto(buffer) {
150
- buffer.set(this.hash().update(this.buffer).digest().subarray(this.slice));
160
+ aoutput(buffer, this);
161
+ buffer.set(this.h.digest().subarray(this.slice));
151
162
  }
152
163
  }
153
- /** Kupyna 256 bit version */
154
- export class Kupyna256 extends Kupyna {
155
- /** Kupyna 256 bit version */
164
+ /** Internal Kupyna-256 hash class */
165
+ export class _Kupyna256 extends Kupyna {
156
166
  constructor() { super(64); }
157
167
  _cloneInto(to) {
158
- to ||= new Kupyna256();
159
- to.s = new BigUint64Array(this.s);
160
- to.x = new Uint8Array(this.x);
168
+ to ||= new _Kupyna256();
169
+ to.s.set(this.s);
170
+ to.x.set(this.x);
161
171
  to.nx = this.nx;
162
172
  to.len = this.len;
163
173
  return to;
164
174
  }
165
- clone() { return this._cloneInto(); }
166
175
  /** Create hash instance */
167
- static create() { return new Kupyna256(); }
176
+ static create() { return new _Kupyna256(); }
168
177
  }
169
- /** Kupyna 512 bit version */
170
- export class Kupyna512 extends Kupyna {
171
- /** Kupyna 512 bit version */
178
+ /** Internal Kupyna-512 hash class */
179
+ export class _Kupyna512 extends Kupyna {
172
180
  constructor() { super(128); }
173
181
  _cloneInto(to) {
174
- to ||= new Kupyna512();
175
- to.s = new BigUint64Array(this.s);
176
- to.x = new Uint8Array(this.x);
182
+ to ||= new _Kupyna512();
183
+ to.s.set(this.s);
184
+ to.x.set(this.x);
177
185
  to.nx = this.nx;
178
186
  to.len = this.len;
179
187
  return to;
180
188
  }
181
- clone() { return this._cloneInto(); }
182
189
  /** Create hash instance */
183
- static create() { return new Kupyna512(); }
190
+ static create() { return new _Kupyna512(); }
184
191
  }
185
- /** Kupyna 48 bit version */
186
- export class Kupyna48 extends KupynaDerived {
187
- /** Kupyna 48 bit version */
188
- constructor() { super(Kupyna256.create, -6); }
192
+ /**
193
+ * Kupyna-256 hash function
194
+ *
195
+ * @param msg - message bytes to hash.
196
+ * @returns Digest bytes.
197
+ * @example
198
+ * ```ts
199
+ * import { kupyna256 } from "@li0ard/dstu/kupyna.js";
200
+ *
201
+ * kupyna256(new Uint8Array([97, 98, 99]));
202
+ * kupyna256.create().update(new Uint8Array([97, 98, 99])).digest();
203
+ * ```
204
+ */
205
+ export const kupyna256 = createHasher(_Kupyna256.create);
206
+ /**
207
+ * Kupyna-512 hash function
208
+ *
209
+ * @param msg - message bytes to hash.
210
+ * @returns Digest bytes.
211
+ * @example
212
+ * ```ts
213
+ * import { kupyna512 } from "@li0ard/dstu/kupyna.js";
214
+ *
215
+ * kupyna512(new Uint8Array([97, 98, 99]));
216
+ * kupyna512.create().update(new Uint8Array([97, 98, 99])).digest();
217
+ * ```
218
+ */
219
+ export const kupyna512 = createHasher(_Kupyna512.create);
220
+ /** Internal Kupyna-48 hash class */
221
+ export class _Kupyna48 extends KupynaDerived {
222
+ constructor() { super(kupyna256, -6); }
189
223
  _cloneInto(to) {
190
- to ||= new Kupyna48();
191
- to.buffer = new Uint8Array(this.buffer);
224
+ to ||= new _Kupyna48();
225
+ to.h = this.h.clone();
192
226
  return to;
193
227
  }
194
- clone() { return this._cloneInto(); }
195
228
  /** Create hash instance */
196
- static create() { return new Kupyna48(); }
229
+ static create() { return new _Kupyna48(); }
197
230
  }
198
- /** Kupyna 304 bit version */
199
- export class Kupyna304 extends KupynaDerived {
200
- /** Kupyna 304 bit version */
201
- constructor() { super(Kupyna512.create, -38); }
231
+ /** Internal Kupyna-304 hash class */
232
+ export class _Kupyna304 extends KupynaDerived {
233
+ constructor() { super(kupyna512, -38); }
202
234
  _cloneInto(to) {
203
- to ||= new Kupyna304();
204
- to.buffer = new Uint8Array(this.buffer);
235
+ to ||= new _Kupyna304();
236
+ to.h = this.h.clone();
205
237
  return to;
206
238
  }
207
- clone() { return this._cloneInto(); }
208
239
  /** Create hash instance */
209
- static create() { return new Kupyna304(); }
240
+ static create() { return new _Kupyna304(); }
210
241
  }
211
- /** Kupyna 384 bit version */
212
- export class Kupyna384 extends KupynaDerived {
213
- /** Kupyna 384 bit version */
214
- constructor() { super(Kupyna512.create, -48); }
242
+ /** Internal Kupyna-384 hash class */
243
+ export class _Kupyna384 extends KupynaDerived {
244
+ constructor() { super(kupyna512, -48); }
215
245
  _cloneInto(to) {
216
- to ||= new Kupyna384();
217
- to.buffer = new Uint8Array(this.buffer);
246
+ to ||= new _Kupyna384();
247
+ to.h = this.h.clone();
218
248
  return to;
219
249
  }
220
- clone() { return this._cloneInto(); }
221
250
  /** Create hash instance */
222
- static create() { return new Kupyna384(); }
251
+ static create() { return new _Kupyna384(); }
223
252
  }
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);
253
+ /**
254
+ * Kupyna-48 hash function
255
+ *
256
+ * @param msg - message bytes to hash.
257
+ * @returns Digest bytes.
258
+ * @example
259
+ * ```ts
260
+ * import { kupyna48 } from "@li0ard/dstu/kupyna.js";
261
+ *
262
+ * kupyna48(new Uint8Array([97, 98, 99]));
263
+ * kupyna48.create().update(new Uint8Array([97, 98, 99])).digest();
264
+ * ```
265
+ */
266
+ export const kupyna48 = createHasher(_Kupyna48.create);
267
+ /**
268
+ * Kupyna-304 hash function
269
+ *
270
+ * @param msg - message bytes to hash.
271
+ * @returns Digest bytes.
272
+ * @example
273
+ * ```ts
274
+ * import { kupyna304 } from "@li0ard/dstu/kupyna.js";
275
+ *
276
+ * kupyna304(new Uint8Array([97, 98, 99]));
277
+ * kupyna304.create().update(new Uint8Array([97, 98, 99])).digest();
278
+ * ```
279
+ */
280
+ export const kupyna304 = createHasher(_Kupyna304.create);
281
+ /**
282
+ * Kupyna-384 hash function
283
+ *
284
+ * @param msg - message bytes to hash.
285
+ * @returns Digest bytes.
286
+ * @example
287
+ * ```ts
288
+ * import { kupyna384 } from "@li0ard/dstu/kupyna.js";
289
+ *
290
+ * kupyna384(new Uint8Array([97, 98, 99]));
291
+ * kupyna384.create().update(new Uint8Array([97, 98, 99])).digest();
292
+ * ```
293
+ */
294
+ export const kupyna384 = createHasher(_Kupyna384.create);
234
295
  export * from "./kmac.js";
package/kupyna/kmac.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { Hash, TArg, TRet } from "@noble/hashes/utils.js";
2
- import { Kupyna256, Kupyna384, Kupyna512 } from "./index.js";
1
+ import { type CHash, type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
2
+ import { type _Kupyna256, type _Kupyna384, type _Kupyna512 } from "./index.js";
3
3
  declare abstract class KupynaKMAC<T, H extends Hash<H>> implements Hash<KupynaKMAC<T, H>> {
4
4
  readonly outputLen: number;
5
5
  readonly blockLen: number;
@@ -8,7 +8,7 @@ declare abstract class KupynaKMAC<T, H extends Hash<H>> implements Hash<KupynaKM
8
8
  protected h: H;
9
9
  protected ik: Uint8Array;
10
10
  protected len: bigint;
11
- constructor(hash: () => H, kpad: TArg<Uint8Array>, key: TArg<Uint8Array>);
11
+ constructor(hash: CHash<H>, kpad: TArg<Uint8Array>, key: TArg<Uint8Array>);
12
12
  abstract clone(): KupynaKMAC<T, H>;
13
13
  abstract _cloneInto(to?: KupynaKMAC<T, H>): KupynaKMAC<T, H>;
14
14
  update(data: TArg<Uint8Array>): this;
@@ -17,21 +17,21 @@ declare abstract class KupynaKMAC<T, H extends Hash<H>> implements Hash<KupynaKM
17
17
  destroy(): void;
18
18
  }
19
19
  /** Kupyna KMAC (256 version) */
20
- export declare class KupynaKMAC256 extends KupynaKMAC<KupynaKMAC256, Kupyna256> {
20
+ export declare class KupynaKMAC256 extends KupynaKMAC<KupynaKMAC256, _Kupyna256> {
21
21
  private key;
22
22
  constructor(key: TArg<Uint8Array>);
23
23
  _cloneInto(to?: KupynaKMAC256): KupynaKMAC256;
24
24
  clone(): KupynaKMAC256;
25
25
  }
26
26
  /** Kupyna KMAC (384 bit version) */
27
- export declare class KupynaKMAC384 extends KupynaKMAC<KupynaKMAC512, Kupyna384> {
27
+ export declare class KupynaKMAC384 extends KupynaKMAC<KupynaKMAC512, _Kupyna384> {
28
28
  private key;
29
29
  constructor(key: TArg<Uint8Array>);
30
30
  _cloneInto(to?: KupynaKMAC384): KupynaKMAC384;
31
31
  clone(): KupynaKMAC384;
32
32
  }
33
33
  /** Kupyna KMAC (512 bit version) */
34
- export declare class KupynaKMAC512 extends KupynaKMAC<KupynaKMAC512, Kupyna512> {
34
+ export declare class KupynaKMAC512 extends KupynaKMAC<KupynaKMAC512, _Kupyna512> {
35
35
  private key;
36
36
  constructor(key: TArg<Uint8Array>);
37
37
  _cloneInto(to?: KupynaKMAC512): KupynaKMAC512;
package/kupyna/kmac.js CHANGED
@@ -1,5 +1,6 @@
1
+ import { abytes, ahash } from "@noble/hashes/utils.js";
1
2
  import { uint64sToBytesLE } from "../utils.js";
2
- import { Kupyna256, Kupyna384, Kupyna512 } from "./index.js";
3
+ import { kupyna256, kupyna384, kupyna512 } from "./index.js";
3
4
  import { numberToBytesLE } from "@noble/curves/utils.js";
4
5
  const dpad = numberToBytesLE(128, 128);
5
6
  const kpad32 = new Uint8Array([
@@ -28,8 +29,11 @@ class KupynaKMAC {
28
29
  ik;
29
30
  len;
30
31
  constructor(hash, kpad, key) {
32
+ ahash(hash);
33
+ abytes(kpad, undefined, "kpad");
34
+ abytes(key, undefined, "key");
31
35
  this.len = 0n;
32
- this.h = hash();
36
+ this.h = hash.create();
33
37
  this.outputLen = this.h.outputLen;
34
38
  this.blockLen = this.h.blockLen;
35
39
  if (key.length != this.h.outputLen)
@@ -42,6 +46,7 @@ class KupynaKMAC {
42
46
  this.threshold = BigInt(this.blockLen - 12);
43
47
  }
44
48
  update(data) {
49
+ abytes(data, undefined, "data");
45
50
  this.len += BigInt(data.length);
46
51
  this.h.update(data);
47
52
  return this;
@@ -52,35 +57,35 @@ class KupynaKMAC {
52
57
  return buffer;
53
58
  }
54
59
  digestInto(buffer) {
55
- let n = this.len;
56
- let pad_size;
60
+ if (buffer.length != this.outputLen)
61
+ throw new Error("digestInto: Invalid buffer length");
62
+ let n = this.len, pad_size;
57
63
  if (n < this.threshold)
58
64
  pad_size = this.threshold - 1n - n;
59
65
  else
60
66
  pad_size = (BigInt(this.blockLen) - 1n) - ((n - this.threshold) % BigInt(this.blockLen));
61
67
  n *= 8n;
62
- this.h.update(dpad.slice(0, Number(pad_size + 1n)));
68
+ this.h.update(dpad.subarray(0, Number(pad_size + 1n)));
63
69
  this.h.update(uint64sToBytesLE(new BigUint64Array([n])));
64
- this.h.update(dpad.slice(16, 20));
70
+ this.h.update(dpad.subarray(16, 20));
65
71
  this.h.update(this.ik);
66
72
  this.h.digestInto(buffer);
73
+ this.destroy();
67
74
  }
68
- destroy() {
69
- throw new Error("Not implemented");
70
- }
75
+ destroy() { this.h.destroy(); }
71
76
  }
72
77
  /** Kupyna KMAC (256 version) */
73
78
  export class KupynaKMAC256 extends KupynaKMAC {
74
79
  key;
75
80
  constructor(key) {
76
- super(Kupyna256.create, kpad32, key);
81
+ super(kupyna256, kpad32, key);
77
82
  this.key = key;
78
83
  }
79
84
  _cloneInto(to) {
80
85
  to ||= new KupynaKMAC256(this.key);
81
86
  to.h = this.h.clone();
82
87
  to.len = this.len;
83
- to.ik = new Uint8Array(this.ik);
88
+ to.ik.set(this.ik);
84
89
  return to;
85
90
  }
86
91
  clone() { return this._cloneInto(); }
@@ -89,14 +94,14 @@ export class KupynaKMAC256 extends KupynaKMAC {
89
94
  export class KupynaKMAC384 extends KupynaKMAC {
90
95
  key;
91
96
  constructor(key) {
92
- super(Kupyna384.create, kpad48, key);
97
+ super(kupyna384, kpad48, key);
93
98
  this.key = key;
94
99
  }
95
100
  _cloneInto(to) {
96
101
  to ||= new KupynaKMAC384(this.key);
97
102
  to.h = this.h.clone();
98
103
  to.len = this.len;
99
- to.ik = new Uint8Array(this.ik);
104
+ to.ik.set(this.ik);
100
105
  return to;
101
106
  }
102
107
  clone() { return this._cloneInto(); }
@@ -105,14 +110,14 @@ export class KupynaKMAC384 extends KupynaKMAC {
105
110
  export class KupynaKMAC512 extends KupynaKMAC {
106
111
  key;
107
112
  constructor(key) {
108
- super(Kupyna512.create, kpad64, key);
113
+ super(kupyna512, kpad64, key);
109
114
  this.key = key;
110
115
  }
111
116
  _cloneInto(to) {
112
117
  to ||= new KupynaKMAC512(this.key);
113
118
  to.h = this.h.clone();
114
119
  to.len = this.len;
115
- to.ik = new Uint8Array(this.ik);
120
+ to.ik.set(this.ik);
116
121
  return to;
117
122
  }
118
123
  clone() { return this._cloneInto(); }