@sebastienrousseau/crypto-lib 0.0.35 → 0.0.37

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 (174) hide show
  1. package/README.md +17 -2
  2. package/dist/accel/index.d.ts +5 -4
  3. package/dist/accel/index.d.ts.map +1 -1
  4. package/dist/accel/index.js +5 -5
  5. package/dist/accel/kdf-backend.d.ts +13 -0
  6. package/dist/accel/kdf-backend.d.ts.map +1 -0
  7. package/dist/accel/kdf-backend.js +81 -0
  8. package/dist/accel/native-pqc-bridge.d.ts.map +1 -1
  9. package/dist/accel/native-pqc-bridge.js +12 -52
  10. package/dist/accel/pq-backend.d.ts +3652 -0
  11. package/dist/accel/pq-backend.d.ts.map +1 -0
  12. package/dist/accel/pq-backend.js +215 -0
  13. package/dist/accel/runtime.d.ts +3 -0
  14. package/dist/accel/runtime.d.ts.map +1 -0
  15. package/dist/accel/runtime.js +9 -0
  16. package/dist/accel/wasm-bridge.d.ts.map +1 -1
  17. package/dist/accel/wasm-bridge.js +14 -13
  18. package/dist/accel/webcrypto-bridge.d.ts.map +1 -1
  19. package/dist/accel/webcrypto-bridge.js +14 -13
  20. package/dist/accel/webcrypto-modern.d.ts.map +1 -1
  21. package/dist/accel/webcrypto-modern.js +15 -14
  22. package/dist/accel/worker-pool.d.ts +18 -0
  23. package/dist/accel/worker-pool.d.ts.map +1 -1
  24. package/dist/accel/worker-pool.js +113 -37
  25. package/dist/bin/cryptolib.d.ts +7 -4
  26. package/dist/bin/cryptolib.d.ts.map +1 -1
  27. package/dist/bin/cryptolib.js +6 -6
  28. package/dist/crypto.d.ts.map +1 -1
  29. package/dist/crypto.js +2 -1
  30. package/dist/encoding.d.ts +10 -0
  31. package/dist/encoding.d.ts.map +1 -0
  32. package/dist/encoding.js +84 -0
  33. package/dist/errors.d.ts +11 -0
  34. package/dist/errors.d.ts.map +1 -1
  35. package/dist/errors.js +20 -1
  36. package/dist/high-level/key-wrap.d.ts.map +1 -1
  37. package/dist/high-level/key-wrap.js +12 -17
  38. package/dist/high-level/multi-recipient.d.ts.map +1 -1
  39. package/dist/high-level/multi-recipient.js +15 -28
  40. package/dist/high-level/password-encrypt.d.ts.map +1 -1
  41. package/dist/high-level/password-encrypt.js +11 -11
  42. package/dist/high-level/sealedbox.d.ts.map +1 -1
  43. package/dist/high-level/sealedbox.js +16 -28
  44. package/dist/high-level/secretbox.d.ts.map +1 -1
  45. package/dist/high-level/secretbox.js +11 -10
  46. package/dist/index.d.ts +1 -1
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +3 -1
  49. package/dist/key/keystore.d.ts.map +1 -1
  50. package/dist/key/keystore.js +2 -1
  51. package/dist/keys/keygen.d.ts.map +1 -1
  52. package/dist/keys/keygen.js +13 -14
  53. package/dist/keys/keyring.d.ts.map +1 -1
  54. package/dist/keys/keyring.js +4 -2
  55. package/dist/keys/serialize.d.ts.map +1 -1
  56. package/dist/keys/serialize.js +18 -16
  57. package/dist/lib/decrypt.d.ts.map +1 -1
  58. package/dist/lib/decrypt.js +2 -1
  59. package/dist/lib/generate.d.ts +2 -1
  60. package/dist/lib/generate.d.ts.map +1 -1
  61. package/dist/lib/generate.js +7 -6
  62. package/dist/lib/key-output.d.ts +4 -1
  63. package/dist/lib/key-output.d.ts.map +1 -1
  64. package/dist/lib/key-output.js +2 -2
  65. package/dist/lib/reformat.d.ts +2 -1
  66. package/dist/lib/reformat.d.ts.map +1 -1
  67. package/dist/lib/reformat.js +2 -2
  68. package/dist/lib/revoke.d.ts +2 -1
  69. package/dist/lib/revoke.d.ts.map +1 -1
  70. package/dist/lib/revoke.js +2 -2
  71. package/dist/lib/verify.d.ts +6 -2
  72. package/dist/lib/verify.d.ts.map +1 -1
  73. package/dist/lib/verify.js +28 -9
  74. package/dist/modern/aead.d.ts.map +1 -1
  75. package/dist/modern/aead.js +11 -10
  76. package/dist/modern/aes.d.ts.map +1 -1
  77. package/dist/modern/aes.js +10 -9
  78. package/dist/modern/cost-limits.d.ts.map +1 -1
  79. package/dist/modern/cost-limits.js +3 -2
  80. package/dist/modern/curves.d.ts +10 -5
  81. package/dist/modern/curves.d.ts.map +1 -1
  82. package/dist/modern/curves.js +43 -36
  83. package/dist/modern/ecdh.d.ts +2 -1
  84. package/dist/modern/ecdh.d.ts.map +1 -1
  85. package/dist/modern/ecdh.js +10 -9
  86. package/dist/modern/fn-dsa.d.ts.map +1 -1
  87. package/dist/modern/fn-dsa.js +9 -8
  88. package/dist/modern/hash.d.ts.map +1 -1
  89. package/dist/modern/hash.js +5 -3
  90. package/dist/modern/hpke-xwing.d.ts +10 -0
  91. package/dist/modern/hpke-xwing.d.ts.map +1 -0
  92. package/dist/modern/hpke-xwing.js +26 -0
  93. package/dist/modern/hpke.d.ts +1 -1
  94. package/dist/modern/hpke.d.ts.map +1 -1
  95. package/dist/modern/hpke.js +60 -28
  96. package/dist/modern/hybrid-combiner.d.ts +20 -0
  97. package/dist/modern/hybrid-combiner.d.ts.map +1 -0
  98. package/dist/modern/hybrid-combiner.js +79 -0
  99. package/dist/modern/index.d.ts +10 -10
  100. package/dist/modern/index.d.ts.map +1 -1
  101. package/dist/modern/index.js +17 -7
  102. package/dist/modern/kdf.d.ts.map +1 -1
  103. package/dist/modern/kdf.js +12 -12
  104. package/dist/modern/mac.d.ts +1 -0
  105. package/dist/modern/mac.d.ts.map +1 -1
  106. package/dist/modern/mac.js +24 -15
  107. package/dist/modern/password.d.ts.map +1 -1
  108. package/dist/modern/password.js +19 -18
  109. package/dist/modern/pq-hash-sign.d.ts.map +1 -1
  110. package/dist/modern/pq-hash-sign.js +23 -22
  111. package/dist/modern/pq-kem.d.ts +8 -0
  112. package/dist/modern/pq-kem.d.ts.map +1 -1
  113. package/dist/modern/pq-kem.js +71 -79
  114. package/dist/modern/pq-sign.d.ts +9 -2
  115. package/dist/modern/pq-sign.d.ts.map +1 -1
  116. package/dist/modern/pq-sign.js +60 -23
  117. package/dist/modern/pq.d.ts +0 -17
  118. package/dist/modern/pq.d.ts.map +1 -1
  119. package/dist/modern/pq.js +15 -43
  120. package/dist/modern/signing.d.ts +2 -1
  121. package/dist/modern/signing.d.ts.map +1 -1
  122. package/dist/modern/signing.js +14 -13
  123. package/dist/protocols/iso20022.d.ts.map +1 -1
  124. package/dist/protocols/iso20022.js +12 -6
  125. package/dist/protocols/opaque/ksf.js +2 -2
  126. package/dist/protocols/opaque/suite.d.ts +1 -8
  127. package/dist/protocols/opaque/suite.d.ts.map +1 -1
  128. package/dist/protocols/opaque/suite.js +5 -10
  129. package/dist/protocols/pqxdh.d.ts +9 -2
  130. package/dist/protocols/pqxdh.d.ts.map +1 -1
  131. package/dist/protocols/pqxdh.js +99 -94
  132. package/dist/protocols/ratchet.d.ts.map +1 -1
  133. package/dist/protocols/ratchet.js +14 -15
  134. package/dist/protocols/threshold.d.ts.map +1 -1
  135. package/dist/protocols/threshold.js +16 -15
  136. package/dist/registry.d.ts.map +1 -1
  137. package/dist/registry.js +26 -0
  138. package/dist/streaming/chunks.d.ts +31 -0
  139. package/dist/streaming/chunks.d.ts.map +1 -1
  140. package/dist/streaming/chunks.js +149 -13
  141. package/dist/streaming/stream-aead.d.ts +9 -0
  142. package/dist/streaming/stream-aead.d.ts.map +1 -1
  143. package/dist/streaming/stream-aead.js +27 -53
  144. package/dist/streaming/stream-hash.d.ts.map +1 -1
  145. package/dist/streaming/stream-hash.js +6 -4
  146. package/dist/streaming/stream-multi-pq-aead.d.ts +12 -0
  147. package/dist/streaming/stream-multi-pq-aead.d.ts.map +1 -1
  148. package/dist/streaming/stream-multi-pq-aead.js +82 -106
  149. package/dist/streaming/stream-pq-aead.d.ts +7 -2
  150. package/dist/streaming/stream-pq-aead.d.ts.map +1 -1
  151. package/dist/streaming/stream-pq-aead.js +29 -79
  152. package/dist/streaming/web-streams.d.ts +5 -2
  153. package/dist/streaming/web-streams.d.ts.map +1 -1
  154. package/dist/streaming/web-streams.js +90 -151
  155. package/dist/testing/index.d.ts +5 -0
  156. package/dist/testing/index.d.ts.map +1 -0
  157. package/dist/testing/index.js +13 -0
  158. package/dist/tokens/index.d.ts.map +1 -1
  159. package/dist/tokens/key-rotation.d.ts.map +1 -1
  160. package/dist/tokens/key-rotation.js +4 -2
  161. package/dist/tokens/paseto.d.ts +2 -0
  162. package/dist/tokens/paseto.d.ts.map +1 -1
  163. package/dist/tokens/paseto.js +56 -29
  164. package/dist/types/types.d.ts +1 -0
  165. package/dist/types/types.d.ts.map +1 -1
  166. package/dist/utils.d.ts.map +1 -1
  167. package/dist/utils.js +3 -2
  168. package/package.json +12 -2
  169. package/dist/config/config.d.ts +0 -8
  170. package/dist/config/config.d.ts.map +0 -1
  171. package/dist/config/config.js +0 -13
  172. package/dist/key/key.d.ts +0 -10
  173. package/dist/key/key.d.ts.map +0 -1
  174. package/dist/key/key.js +0 -25
@@ -1,8 +1,27 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WorkerPool = void 0;
3
+ exports.WorkerPool = exports.WorkerTaskTimeoutError = exports.WorkerPoolBusyError = void 0;
4
4
  const node_worker_threads_1 = require("node:worker_threads");
5
5
  const node_os_1 = require("node:os");
6
+ const errors_1 = require("../errors");
7
+ class WorkerPoolBusyError extends Error {
8
+ name = "WorkerPoolBusyError";
9
+ }
10
+ exports.WorkerPoolBusyError = WorkerPoolBusyError;
11
+ function rebuildError(msg) {
12
+ const message = msg.error ?? "Worker task failed";
13
+ if (msg.name === "CryptoError" && msg.code !== undefined) {
14
+ return new errors_1.CryptoError(message, msg.code);
15
+ }
16
+ if (msg.name === "RangeError")
17
+ return new RangeError(message);
18
+ return new Error(message);
19
+ }
20
+ const MAIN_THREAD_ONLY = "WorkerPool must be created from the main thread";
21
+ class WorkerTaskTimeoutError extends Error {
22
+ name = "WorkerTaskTimeoutError";
23
+ }
24
+ exports.WorkerTaskTimeoutError = WorkerTaskTimeoutError;
6
25
  const WORKER_SCRIPT = `
7
26
  const { parentPort } = require("node:worker_threads");
8
27
  const { isAbsolute } = require("node:path");
@@ -23,9 +42,13 @@ parentPort.on("message", async (msg) => {
23
42
  const result = await fn(...msg.args);
24
43
  parentPort.postMessage({ success: true, result });
25
44
  } catch (err) {
45
+ // An error crosses the thread as data: send its name and, for a
46
+ // CryptoError, its code, so the main thread can rebuild it.
26
47
  parentPort.postMessage({
27
48
  success: false,
28
49
  error: err instanceof Error ? err.message : String(err),
50
+ name: err instanceof Error ? err.name : undefined,
51
+ code: err && typeof err.code === "string" ? err.code : undefined,
29
52
  });
30
53
  }
31
54
  });
@@ -34,31 +57,38 @@ class WorkerPool {
34
57
  workers = [];
35
58
  queue = [];
36
59
  terminated = false;
60
+ script;
61
+ maxQueue;
62
+ taskTimeoutMs;
63
+ resourceLimits;
37
64
  size;
38
65
  constructor(options = {}) {
39
66
  if (!node_worker_threads_1.isMainThread) {
40
- throw new Error("WorkerPool must be created from the main thread");
67
+ throw new errors_1.CryptoError(MAIN_THREAD_ONLY, errors_1.CryptoErrorCode.INVALID_STATE);
41
68
  }
42
69
  this.size = Math.max(1, options.size ?? Math.floor((0, node_os_1.cpus)().length / 2));
43
- const script = options.workerScript ?? undefined;
70
+ this.script = options.workerScript;
71
+ this.maxQueue = options.maxQueue ?? Number.POSITIVE_INFINITY;
72
+ this.taskTimeoutMs = options.taskTimeoutMs;
73
+ this.resourceLimits = options.resourceLimits;
44
74
  for (let i = 0; i < this.size; i++) {
45
- const worker = script
46
- ? new node_worker_threads_1.Worker(script)
47
- : new node_worker_threads_1.Worker(WORKER_SCRIPT, { eval: true });
48
- this.workers.push({ worker, busy: false });
75
+ this.workers.push(this.spawn());
49
76
  }
50
77
  }
51
78
  execute(task) {
52
79
  if (this.terminated) {
53
80
  return Promise.reject(new Error("WorkerPool has been shut down"));
54
81
  }
82
+ const available = this.workers.find((w) => !w.current);
83
+ if (!available && this.queue.length >= this.maxQueue) {
84
+ return Promise.reject(new WorkerPoolBusyError("WorkerPool queue is full"));
85
+ }
55
86
  return new Promise((resolve, reject) => {
56
87
  const pending = {
57
88
  task,
58
89
  resolve: resolve,
59
90
  reject,
60
91
  };
61
- const available = this.workers.find((w) => !w.busy);
62
92
  if (available) {
63
93
  this.dispatch(available, pending);
64
94
  }
@@ -69,10 +99,15 @@ class WorkerPool {
69
99
  }
70
100
  async shutdown() {
71
101
  this.terminated = true;
72
- for (const pending of this.queue) {
73
- pending.reject(new Error("WorkerPool is shutting down"));
74
- }
102
+ const stopping = new Error("WorkerPool is shutting down");
103
+ for (const pending of this.queue)
104
+ pending.reject(stopping);
75
105
  this.queue = [];
106
+ for (const entry of this.workers) {
107
+ this.clearTimer(entry);
108
+ entry.current?.reject(stopping);
109
+ entry.current = undefined;
110
+ }
76
111
  const terminations = this.workers.map((entry) => entry.worker.terminate());
77
112
  await Promise.all(terminations);
78
113
  this.workers = [];
@@ -81,38 +116,79 @@ class WorkerPool {
81
116
  return this.queue.length;
82
117
  }
83
118
  get active() {
84
- return this.workers.filter((w) => w.busy).length;
119
+ return this.workers.filter((w) => w.current).length;
85
120
  }
86
- dispatch(entry, pending) {
87
- entry.busy = true;
88
- const handler = (msg) => {
89
- entry.worker.off("message", handler);
90
- entry.worker.off("error", errorHandler);
91
- entry.busy = false;
92
- if (msg.success) {
93
- pending.resolve(msg.result);
94
- }
95
- else {
96
- pending.reject(new Error(msg.error ?? "Worker task failed"));
97
- }
98
- this.processQueue(entry);
121
+ spawn() {
122
+ const options = this.resourceLimits
123
+ ? { resourceLimits: this.resourceLimits }
124
+ : {};
125
+ const worker = this.script
126
+ ? new node_worker_threads_1.Worker(this.script, options)
127
+ : new node_worker_threads_1.Worker(WORKER_SCRIPT, { ...options, eval: true });
128
+ const entry = {
129
+ worker,
130
+ current: undefined,
131
+ timer: undefined,
132
+ retired: false,
133
+ online: false,
99
134
  };
100
- const errorHandler = (err) => {
101
- entry.worker.off("message", handler);
102
- entry.worker.off("error", errorHandler);
103
- entry.busy = false;
104
- pending.reject(err);
105
- this.processQueue(entry);
106
- };
107
- entry.worker.on("message", handler);
108
- entry.worker.on("error", errorHandler);
135
+ worker.once("online", () => {
136
+ entry.online = true;
137
+ if (entry.current)
138
+ this.startTimer(entry);
139
+ });
140
+ worker.on("message", (msg) => this.settle(entry, msg));
141
+ worker.on("error", (err) => this.fail(entry, err));
142
+ worker.on("exit", (code) => this.fail(entry, new Error(`Worker exited with code ${code}`)));
143
+ return entry;
144
+ }
145
+ dispatch(entry, pending) {
146
+ entry.current = pending;
147
+ if (entry.online)
148
+ this.startTimer(entry);
109
149
  entry.worker.postMessage(pending.task);
110
150
  }
151
+ startTimer(entry) {
152
+ if (this.taskTimeoutMs === undefined)
153
+ return;
154
+ const ms = this.taskTimeoutMs;
155
+ entry.timer = setTimeout(() => this.fail(entry, new WorkerTaskTimeoutError(`Worker task timed out after ${ms} ms`)), ms);
156
+ }
157
+ settle(entry, msg) {
158
+ const pending = entry.current;
159
+ if (!pending)
160
+ return;
161
+ this.clearTimer(entry);
162
+ entry.current = undefined;
163
+ if (msg.success) {
164
+ pending.resolve(msg.result);
165
+ }
166
+ else {
167
+ pending.reject(rebuildError(msg));
168
+ }
169
+ this.processQueue(entry);
170
+ }
171
+ fail(entry, err) {
172
+ if (entry.retired || this.terminated)
173
+ return;
174
+ entry.retired = true;
175
+ this.clearTimer(entry);
176
+ entry.current?.reject(err);
177
+ entry.current = undefined;
178
+ void entry.worker.terminate();
179
+ const replacement = this.spawn();
180
+ this.workers[this.workers.indexOf(entry)] = replacement;
181
+ this.processQueue(replacement);
182
+ }
183
+ clearTimer(entry) {
184
+ if (entry.timer !== undefined)
185
+ clearTimeout(entry.timer);
186
+ entry.timer = undefined;
187
+ }
111
188
  processQueue(entry) {
112
- if (this.queue.length > 0) {
113
- const next = this.queue.shift();
189
+ const next = this.queue.shift();
190
+ if (next)
114
191
  this.dispatch(entry, next);
115
- }
116
192
  }
117
193
  }
118
194
  exports.WorkerPool = WorkerPool;
@@ -1,16 +1,19 @@
1
+ import * as key from "../lib";
1
2
  import { dataDecrypt, dataEncrypt, dataGenerate, dataReformat, dataRevoke, dataSessionKey, dataSign, dataVerify } from "../types/types";
2
3
  import type { ArmoredKeyResult } from "../lib/reformat.js";
4
+ import type { KeyOutputOptions } from "../lib/key-output.js";
5
+ export type { KeyOutputOptions };
3
6
  export declare function decrypt(data: dataDecrypt): Promise<{
4
7
  data: string;
5
8
  signatureValid: boolean;
6
9
  }>;
7
10
  export declare function encrypt(data: dataEncrypt): Promise<string>;
8
- export declare function generate(data: dataGenerate): Promise<object>;
9
- export declare function reformat(data: dataReformat): Promise<ArmoredKeyResult>;
11
+ export declare function generate(data: dataGenerate, output?: KeyOutputOptions): Promise<object>;
12
+ export declare function reformat(data: dataReformat, output?: KeyOutputOptions): Promise<ArmoredKeyResult>;
10
13
  export declare function session(data: dataSessionKey): Promise<import("openpgp").SessionKey>;
11
- export declare function revoke(data: dataRevoke): Promise<ArmoredKeyResult>;
14
+ export declare function revoke(data: dataRevoke, output?: KeyOutputOptions): Promise<ArmoredKeyResult>;
12
15
  export declare function sign(data: dataSign): Promise<string>;
13
- export declare function verify(data: dataVerify): Promise<import("openpgp").VerifyMessageResult<GenericWebStream<T>>>;
16
+ export declare function verify(data: dataVerify): Promise<key.verify.PgpVerifyResult>;
14
17
  export { loadKeystore } from "../key/keystore";
15
18
  export type { Keystore } from "../key/keystore";
16
19
  //# sourceMappingURL=cryptolib.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cryptolib.d.ts","sourceRoot":"","sources":["../../src/bin/cryptolib.ts"],"names":[],"mappings":"AACA,OAAO,EACL,WAAW,EACX,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,cAAc,EACd,QAAQ,EACR,UAAU,EACX,MAAM,gBAAgB,CAAC;AAExB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAkB3D,wBAAsB,OAAO,CAAC,IAAI,EAAE,WAAW;;;GAG9C;AAkBD,wBAAsB,OAAO,CAAC,IAAI,EAAE,WAAW,mBAG9C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AAO,CAAC,IAAI,EAAE,cAAc,yCAGjD;AAiBD,wBAAsB,MAAM,CAC1B,IAAI,EAAE,UAAU,EAChB,MAAM,GAAE,gBAAqB,GAC5B,OAAO,CAAC,gBAAgB,CAAC,CAG3B;AAiBD,wBAAsB,IAAI,CAAC,IAAI,EAAE,QAAQ,mBAGxC;AAiBD,wBAAsB,MAAM,CAAC,IAAI,EAAE,UAAU,uCAG5C;AAOD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,YAAY,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC"}
@@ -51,20 +51,20 @@ async function encrypt(data) {
51
51
  const encrypt = await key.encrypt.default(data);
52
52
  return encrypt;
53
53
  }
54
- async function generate(data) {
55
- const generate = await key.generate.default(data);
54
+ async function generate(data, output = {}) {
55
+ const generate = await key.generate.default(data, output);
56
56
  return generate;
57
57
  }
58
- async function reformat(data) {
59
- const reformat = await key.reformat.default(data);
58
+ async function reformat(data, output = {}) {
59
+ const reformat = await key.reformat.default(data, output);
60
60
  return reformat;
61
61
  }
62
62
  async function session(data) {
63
63
  const verify = await key.session.default(data);
64
64
  return verify;
65
65
  }
66
- async function revoke(data) {
67
- const revoke = await key.revoke.default(data);
66
+ async function revoke(data, output = {}) {
67
+ const revoke = await key.revoke.default(data, output);
68
68
  return revoke;
69
69
  }
70
70
  async function sign(data) {
@@ -1 +1 @@
1
- {"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AA0BA,OAAO,EAAsB,KAAK,aAAa,EAAE,MAAM,eAAe,CAAC;AACvE,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACtB,MAAM,eAAe,CAAC;AAavB,OAAO,EAIL,KAAK,kBAAkB,EACvB,KAAK,oBAAoB,EAC1B,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,cAAc,EACf,M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AU,KAAG,MAAM;0CAkBpD,YAAY,aACZ,WAAW,KACrB,gBAAgB;+BAmBN,aAAa,iBACT,MAAM,WACZ,MAAM,GAAG,UAAU,KAC3B,MAAM;iCA0CI,aAAa,gBACV,MAAM,WACX,MAAM,GAAG,UAAU,gBACd,MAAM,KACnB,OAAO;sCAsCa,MAAM,GAAG,UAAU,KAAG,kBAAkB;uCAoBvC,qBAAqB,KAAG,oBAAoB;2CAsBxD,MAAM,GAAG,UAAU,OACxB,MAAM,KACV,oBAAoB;+BAkBV,aAAa,OACnB,MAAM,GAAG,UAAU,QAClB,MAAM,GAAG,UAAU,KACxB,MAAM;mCAoBW,iBAAiB,KAAG,OAAO;;;;;;;CAoBvC,CAAC"}
package/dist/crypto.js CHANGED
@@ -12,9 +12,10 @@ const utils_js_1 = require("@noble/ciphers/utils.js");
12
12
  const registry_1 = require("./registry");
13
13
  const mac_1 = require("./modern/mac");
14
14
  const errors_1 = require("./errors");
15
+ const encoding_1 = require("./encoding");
15
16
  exports.crypto = {
16
17
  randomKey() {
17
- return Buffer.from((0, utils_js_1.randomBytes)(32)).toString("hex");
18
+ return (0, encoding_1.toHex)((0, utils_js_1.randomBytes)(32));
18
19
  },
19
20
  encrypt(key, plaintext) {
20
21
  return (0, secretbox_1.seal)(key, plaintext).sealed;
@@ -0,0 +1,10 @@
1
+ export declare function isHex(value: unknown): value is string;
2
+ export declare function decodeHex(value: string): Uint8Array;
3
+ export declare const toHex: (bytes: Uint8Array) => string;
4
+ export declare const utf8: (text: string) => Uint8Array;
5
+ export declare const fromUtf8: (bytes: Uint8Array) => string;
6
+ export declare const concat: (parts: readonly Uint8Array[]) => Uint8Array;
7
+ export declare function toBase64(bytes: Uint8Array): string;
8
+ export declare const toBase64url: (bytes: Uint8Array) => string;
9
+ export declare function fromBase64(text: string): Uint8Array;
10
+ //# sourceMappingURL=encoding.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encoding.d.ts","sourceRoot":"","sources":["../src/encoding.ts"],"names":[],"mappings":"AA+BA,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAMrD;AAMD,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAKnD;AAGD,eAAO,MAAM,KAAK,GAAI,OAAO,UAAU,KAAG,MAA2B,CAAC;AAGtE,eAAO,MAAM,IAAI,GAAI,MAAM,MAAM,KAAG,UACJ,CAAC;AAGjC,eAAO,MAAM,QAAQ,GAAI,OAAO,UAAU,KAAG,MACZ,CAAC;AAGlC,eAAO,MAAM,MAAM,GAAI,OAAO,SAAS,UAAU,EAAE,KAAG,UAC/B,CAAC;AAYxB,wBAAgB,QAAQ,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAIlD;AAeD,eAAO,MAAM,WAAW,GAAI,OAAO,UAAU,KAAG,MACsB,CAAC;AAOvE,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,CAgBnD"}
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toBase64url = exports.concat = exports.fromUtf8 = exports.utf8 = exports.toHex = void 0;
4
+ exports.isHex = isHex;
5
+ exports.decodeHex = decodeHex;
6
+ exports.toBase64 = toBase64;
7
+ exports.fromBase64 = fromBase64;
8
+ const utils_js_1 = require("@noble/hashes/utils.js");
9
+ const errors_1 = require("./errors");
10
+ const HEX_DIGITS = /^[0-9a-fA-F]*$/;
11
+ function isHex(value) {
12
+ return (typeof value === "string" &&
13
+ value.length % 2 === 0 &&
14
+ HEX_DIGITS.test(value));
15
+ }
16
+ function decodeHex(value) {
17
+ if (!isHex(value)) {
18
+ throw new errors_1.CryptoError("Invalid hex string", errors_1.CryptoErrorCode.INVALID_HEX);
19
+ }
20
+ return (0, utils_js_1.hexToBytes)(value);
21
+ }
22
+ const toHex = (bytes) => (0, utils_js_1.bytesToHex)(bytes);
23
+ exports.toHex = toHex;
24
+ const utf8 = (text) => new TextEncoder().encode(text);
25
+ exports.utf8 = utf8;
26
+ const fromUtf8 = (bytes) => new TextDecoder().decode(bytes);
27
+ exports.fromUtf8 = fromUtf8;
28
+ const concat = (parts) => (0, utils_js_1.concatBytes)(...parts);
29
+ exports.concat = concat;
30
+ const B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
31
+ const B64_VALUE = new Int16Array(128).fill(-1);
32
+ for (let i = 0; i < 64; i++)
33
+ B64_VALUE[B64.charCodeAt(i)] = i;
34
+ B64_VALUE["-".charCodeAt(0)] = 62;
35
+ B64_VALUE["_".charCodeAt(0)] = 63;
36
+ function toBase64(bytes) {
37
+ let out = "";
38
+ for (let i = 0; i < bytes.length; i += 3)
39
+ out += encodeGroup(bytes, i);
40
+ return out;
41
+ }
42
+ function encodeGroup(bytes, i) {
43
+ const n = (bytes[i] << 16) | ((bytes[i + 1] ?? 0) << 8) | (bytes[i + 2] ?? 0);
44
+ const chars = [18, 12, 6, 0].map((shift) => B64[(n >> shift) & 63]);
45
+ return chars
46
+ .slice(0, bytes.length - i + 1)
47
+ .join("")
48
+ .padEnd(4, "=");
49
+ }
50
+ const toBase64url = (bytes) => trimPadding(toBase64(bytes).replace(/\+/g, "-").replace(/\//g, "_"));
51
+ exports.toBase64url = toBase64url;
52
+ function fromBase64(text) {
53
+ const clean = trimPadding(text.replace(/\s+/g, ""));
54
+ if (clean.length % 4 === 1)
55
+ throw invalidBase64();
56
+ const out = new Uint8Array(Math.floor((clean.length * 3) / 4));
57
+ let pending = 0;
58
+ let value = 0;
59
+ let o = 0;
60
+ for (const char of clean) {
61
+ value = ((value << 6) | base64Value(char)) & 0x3fff;
62
+ pending += 6;
63
+ if (pending >= 8) {
64
+ pending -= 8;
65
+ out[o++] = (value >> pending) & 0xff;
66
+ }
67
+ }
68
+ return out;
69
+ }
70
+ function base64Value(char) {
71
+ const code = char.charCodeAt(0);
72
+ const v = code < 128 ? B64_VALUE[code] : -1;
73
+ if (v < 0)
74
+ throw invalidBase64();
75
+ return v;
76
+ }
77
+ function trimPadding(text) {
78
+ let end = text.length;
79
+ while (end > 0 && text[end - 1] === "=")
80
+ end--;
81
+ return text.slice(0, end);
82
+ }
83
+ const invalidBase64 = () => new errors_1.CryptoError("Invalid base64 encoding", errors_1.CryptoErrorCode.INVALID_INPUT);
84
+ //# sourceMappingURL=encoding.js.map
package/dist/errors.d.ts CHANGED
@@ -10,5 +10,16 @@ export declare const CryptoErrorCode: {
10
10
  readonly AUTH_FAILED: "AUTH_FAILED";
11
11
  readonly BUFFER_DESTROYED: "BUFFER_DESTROYED";
12
12
  readonly INVALID_INPUT: "INVALID_INPUT";
13
+ readonly INVALID_TOKEN: "INVALID_TOKEN";
14
+ readonly INVALID_STATE: "INVALID_STATE";
15
+ readonly CONFIGURATION: "CONFIGURATION";
13
16
  };
17
+ export declare const OpaqueErrorCode: {
18
+ readonly ENVELOPE_RECOVERY: "OPAQUE_ENVELOPE_RECOVERY";
19
+ readonly SERVER_AUTHENTICATION: "OPAQUE_SERVER_AUTHENTICATION";
20
+ readonly CLIENT_AUTHENTICATION: "OPAQUE_CLIENT_AUTHENTICATION";
21
+ readonly DESERIALIZE: "OPAQUE_DESERIALIZE";
22
+ readonly INVALID_INPUT: "OPAQUE_INVALID_INPUT";
23
+ };
24
+ export declare function isCallerErrorCode(code: string): boolean;
14
25
  //# sourceMappingURL=errors.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAwBA,qBAAa,WAAY,SAAQ,KAAK;IAEpC,SAAgB,IAAI,EAAE,MAAM,CAAC;gBAajB,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;CAK1C;AAGD,eAAO,MAAM,eAAe;;;;;;;;CAelB,CAAC"}
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAwBA,qBAAa,WAAY,SAAQ,KAAK;IAEpC,SAAgB,IAAI,EAAE,MAAM,CAAC;gBAajB,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;CAK1C;AAGD,eAAO,MAAM,eAAe;;;;;;;;;;;CAqBlB,CAAC;AAGX,eAAO,MAAM,eAAe;;;;;;CAWlB,CAAC;AA2BX,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEvD"}
package/dist/errors.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CryptoErrorCode = exports.CryptoError = void 0;
3
+ exports.OpaqueErrorCode = exports.CryptoErrorCode = exports.CryptoError = void 0;
4
+ exports.isCallerErrorCode = isCallerErrorCode;
4
5
  class CryptoError extends Error {
5
6
  code;
6
7
  constructor(message, code) {
@@ -18,5 +19,23 @@ exports.CryptoErrorCode = {
18
19
  AUTH_FAILED: "AUTH_FAILED",
19
20
  BUFFER_DESTROYED: "BUFFER_DESTROYED",
20
21
  INVALID_INPUT: "INVALID_INPUT",
22
+ INVALID_TOKEN: "INVALID_TOKEN",
23
+ INVALID_STATE: "INVALID_STATE",
24
+ CONFIGURATION: "CONFIGURATION",
21
25
  };
26
+ exports.OpaqueErrorCode = {
27
+ ENVELOPE_RECOVERY: "OPAQUE_ENVELOPE_RECOVERY",
28
+ SERVER_AUTHENTICATION: "OPAQUE_SERVER_AUTHENTICATION",
29
+ CLIENT_AUTHENTICATION: "OPAQUE_CLIENT_AUTHENTICATION",
30
+ DESERIALIZE: "OPAQUE_DESERIALIZE",
31
+ INVALID_INPUT: "OPAQUE_INVALID_INPUT",
32
+ };
33
+ const LIBRARY_FAULT_CODES = new Set([
34
+ exports.CryptoErrorCode.BUFFER_DESTROYED,
35
+ exports.CryptoErrorCode.CONFIGURATION,
36
+ ]);
37
+ const CALLER_ERROR_CODES = new Set([...Object.values(exports.CryptoErrorCode), ...Object.values(exports.OpaqueErrorCode)].filter((code) => !LIBRARY_FAULT_CODES.has(code)));
38
+ function isCallerErrorCode(code) {
39
+ return CALLER_ERROR_CODES.has(code);
40
+ }
22
41
  //# sourceMappingURL=errors.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"key-wrap.d.ts","sourceRoot":"","sources":["../../src/high-level/key-wrap.ts"],"names":[],"mappings":"AAuCA,MAAM,WAAW,eAAe;IAE9B,OAAO,EAAE,MAAM,CAAC;IAEhB,SAAS,EAAE,QAAQ,GAAG,SAAS,CAAC;CACjC;AAGD,MAAM,WAAW,qBAAqB;IAEpC,OAAO,EAAE,MAAM,CAAC;IAEhB,kBAAkB,EAAE,MAAM,CAAC;IAE3B,SAAS,EAAE,eAAe,CAAC;CAC5B;AAUD,wBAAgB,SAAS,CACvB,GAAG,EAAE,MAAM,GAAG,UAAU,EACxB,SAAS,EAAE,MAAM,GAAG,UAAU,GAC7B,eAAe,CASjB;AAQD,wBAAgB,WAAW,CACzB,GAAG,EAAE,MAAM,GAAG,UAAU,EACxB,UAAU,EAAE,MAAM,GAAG,UAAU,GAC9B,UAAU,CAQZ;AAUD,wBAAgB,UAAU,CACxB,GAAG,EAAE,MAAM,GAAG,UAAU,EACxB,IAAI,EAAE,MAAM,GAAG,UAAU,GACxB,eAAe,CASjB;AAQD,wBAAgB,YAAY,CAC1B,GAAG,EAAE,MAAM,GAAG,UAAU,EACxB,WAAW,EAAE,MAAM,GAAG,UAAU,GAC/B,UAAU,CAQZ;AAcD,wBAAgB,eAAe,CAC7B,kBAAkB,EAAE,MAAM,GAAG,UAAU,EACvC,SAAS,EAAE,MAAM,GAAG,UAAU,GAC7B,qBAAqB,CA0BvB;AASD,wBAAgB,iBAAiB,CAC/B,kBAAkB,EAAE,MAAM,GAAG,UAAU,EACvC,kBAAkB,EAAE,MAAM,GAAG,UAAU,EACvC,UAAU,EAAE,MAAM,GAAG,UAAU,GAC9B,UAAU,CA0BZ"}
1
+ {"version":3,"file":"key-wrap.d.ts","sourceRoot":"","sources":["../../src/high-level/key-wrap.ts"],"names":[],"mappings":"AAuCA,MAAM,WAAW,eAAe;IAE9B,OAAO,EAAE,MAAM,CAAC;IAEhB,SAAS,EAAE,QAAQ,GAAG,SAAS,CAAC;CACjC;AAGD,MAAM,WAAW,qBAAqB;IAEpC,OAAO,EAAE,MAAM,CAAC;IAEhB,kBAAkB,EAAE,MAAM,CAAC;IAE3B,SAAS,EAAE,eAAe,CAAC;CAC5B;AAUD,wBAAgB,SAAS,CACvB,GAAG,EAAE,MAAM,GAAG,UAAU,EACxB,SAAS,EAAE,MAAM,GAAG,UAAU,GAC7B,eAAe,CASjB;AAQD,wBAAgB,WAAW,CACzB,GAAG,EAAE,MAAM,GAAG,UAAU,EACxB,UAAU,EAAE,MAAM,GAAG,UAAU,GAC9B,UAAU,CAMZ;AAUD,wBAAgB,UAAU,CACxB,GAAG,EAAE,MAAM,GAAG,UAAU,EACxB,IAAI,EAAE,MAAM,GAAG,UAAU,GACxB,eAAe,CASjB;AAQD,wBAAgB,YAAY,CAC1B,GAAG,EAAE,MAAM,GAAG,UAAU,EACxB,WAAW,EAAE,MAAM,GAAG,UAAU,GAC/B,UAAU,CAMZ;AAcD,wBAAgB,eAAe,CAC7B,kBAAkB,EAAE,MAAM,GAAG,UAAU,EACvC,SAAS,EAAE,MAAM,GAAG,UAAU,GAC7B,qBAAqB,CA0BvB;AASD,wBAAgB,iBAAiB,CAC/B,kBAAkB,EAAE,MAAM,GAAG,UAAU,EACvC,kBAAkB,EAAE,MAAM,GAAG,UAAU,EACvC,UAAU,EAAE,MAAM,GAAG,UAAU,GAC9B,UAAU,CAwBZ"}
@@ -11,11 +11,12 @@ const ed25519_js_1 = require("@noble/curves/ed25519.js");
11
11
  const hkdf_js_1 = require("@noble/hashes/hkdf.js");
12
12
  const sha2_js_1 = require("@noble/hashes/sha2.js");
13
13
  const utils_js_1 = require("@noble/ciphers/utils.js");
14
- const HEX_RE = /^[0-9a-fA-F]*$/;
14
+ const errors_1 = require("../errors");
15
+ const encoding_1 = require("../encoding");
15
16
  function hexToBytes(hex) {
16
- if (!HEX_RE.test(hex))
17
- throw new Error("Invalid hex string");
18
- return Buffer.from(hex, "hex");
17
+ if (!(0, encoding_1.isHex)(hex))
18
+ throw new errors_1.CryptoError("Invalid hex string", errors_1.CryptoErrorCode.INVALID_HEX);
19
+ return (0, encoding_1.decodeHex)(hex);
19
20
  }
20
21
  function toKey(key) {
21
22
  if (key instanceof Uint8Array)
@@ -28,15 +29,13 @@ function aesKwWrap(kek, keyToWrap) {
28
29
  const cipher = (0, aes_js_1.aeskw)(kekBytes);
29
30
  const wrapped = cipher.encrypt(keyBytes);
30
31
  return {
31
- wrapped: Buffer.from(wrapped).toString("base64"),
32
+ wrapped: (0, encoding_1.toBase64)(wrapped),
32
33
  algorithm: "aes-kw",
33
34
  };
34
35
  }
35
36
  function aesKwUnwrap(kek, wrappedKey) {
36
37
  const kekBytes = toKey(kek);
37
- const wrapped = wrappedKey instanceof Uint8Array
38
- ? wrappedKey
39
- : Buffer.from(wrappedKey, "base64");
38
+ const wrapped = wrappedKey instanceof Uint8Array ? wrappedKey : (0, encoding_1.fromBase64)(wrappedKey);
40
39
  const cipher = (0, aes_js_1.aeskw)(kekBytes);
41
40
  return cipher.decrypt(wrapped);
42
41
  }
@@ -46,15 +45,13 @@ function aesKwpWrap(kek, data) {
46
45
  const cipher = (0, aes_js_1.aeskwp)(kekBytes);
47
46
  const wrapped = cipher.encrypt(dataBytes);
48
47
  return {
49
- wrapped: Buffer.from(wrapped).toString("base64"),
48
+ wrapped: (0, encoding_1.toBase64)(wrapped),
50
49
  algorithm: "aes-kwp",
51
50
  };
52
51
  }
53
52
  function aesKwpUnwrap(kek, wrappedData) {
54
53
  const kekBytes = toKey(kek);
55
- const wrapped = wrappedData instanceof Uint8Array
56
- ? wrappedData
57
- : Buffer.from(wrappedData, "base64");
54
+ const wrapped = wrappedData instanceof Uint8Array ? wrappedData : (0, encoding_1.fromBase64)(wrappedData);
58
55
  const cipher = (0, aes_js_1.aeskwp)(kekBytes);
59
56
  return cipher.decrypt(wrapped);
60
57
  }
@@ -70,8 +67,8 @@ function x25519AesKwWrap(recipientPublicKey, keyToWrap) {
70
67
  const cipher = (0, aes_js_1.aeskw)(kek);
71
68
  const wrapped = cipher.encrypt(keyBytes);
72
69
  return {
73
- wrapped: Buffer.from(wrapped).toString("base64"),
74
- ephemeralPublicKey: Buffer.from(ephPub).toString("hex"),
70
+ wrapped: (0, encoding_1.toBase64)(wrapped),
71
+ ephemeralPublicKey: (0, encoding_1.toHex)(ephPub),
75
72
  algorithm: "x25519-aes-kw",
76
73
  };
77
74
  }
@@ -82,9 +79,7 @@ function x25519AesKwUnwrap(recipientSecretKey, ephemeralPublicKey, wrappedKey) {
82
79
  const ephPub = typeof ephemeralPublicKey === "string"
83
80
  ? hexToBytes(ephemeralPublicKey)
84
81
  : ephemeralPublicKey;
85
- const wrapped = wrappedKey instanceof Uint8Array
86
- ? wrappedKey
87
- : Buffer.from(wrappedKey, "base64");
82
+ const wrapped = wrappedKey instanceof Uint8Array ? wrappedKey : (0, encoding_1.fromBase64)(wrappedKey);
88
83
  const raw = ed25519_js_1.x25519.getSharedSecret(secKey, ephPub);
89
84
  const kek = (0, hkdf_js_1.hkdf)(sha2_js_1.sha256, raw, ephPub, new TextEncoder().encode("x25519-aes-kw-v1"), 32);
90
85
  const cipher = (0, aes_js_1.aeskw)(kek);
@@ -1 +1 @@
1
- {"version":3,"file":"multi-recipient.d.ts","sourceRoot":"","sources":["../../src/high-level/multi-recipient.ts"],"names":[],"mappings":"AAwCA,MAAM,WAAW,kBAAkB;IAEjC,IAAI,EAAE,QAAQ,CAAC;IAEf,SAAS,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,WAAW;IAE1B,IAAI,EAAE,mBAAmB,CAAC;IAE1B,eAAe,EAAE,MAAM,CAAC;IAExB,cAAc,EAAE,MAAM,CAAC;CACxB;AAGD,MAAM,MAAM,SAAS,GAAG,kBAAkB,GAAG,WAAW,CAAC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,GAAG,UAAU,EACpC,cAAc,EAAE,MAAM,GAAG,UAAU,EACnC,UAAU,EAAE,UAAU,EACtB,UAAU,EAAE,MAAM,GACjB,UAAU,CAiBZ"}
@@ -41,20 +41,20 @@ exports.multiDecryptPQ = multiDecryptPQ;
41
41
  const utils_js_1 = require("@noble/ciphers/utils.js");
42
42
  const secretbox = __importStar(require("./secretbox"));
43
43
  const key_wrap_1 = require("./key-wrap");
44
- const ed25519_js_1 = require("@noble/curves/ed25519.js");
45
- const ml_kem_js_1 = require("@noble/post-quantum/ml-kem.js");
44
+ const hybrid_combiner_1 = require("../modern/hybrid-combiner");
46
45
  const aes_js_1 = require("@noble/ciphers/aes.js");
47
46
  const hkdf_js_1 = require("@noble/hashes/hkdf.js");
48
47
  const sha2_js_1 = require("@noble/hashes/sha2.js");
49
- const HEX_RE = /^[0-9a-fA-F]*$/;
48
+ const errors_1 = require("../errors");
49
+ const encoding_1 = require("../encoding");
50
50
  function hexToBytes(hex) {
51
- if (!HEX_RE.test(hex))
52
- throw new Error("Invalid hex string");
53
- return Buffer.from(hex, "hex");
51
+ if (!(0, encoding_1.isHex)(hex))
52
+ throw new errors_1.CryptoError("Invalid hex string", errors_1.CryptoErrorCode.INVALID_HEX);
53
+ return (0, encoding_1.decodeHex)(hex);
54
54
  }
55
55
  function multiEncrypt(recipients, plaintext) {
56
56
  if (recipients.length === 0) {
57
- throw new Error("At least one recipient is required");
57
+ throw new errors_1.CryptoError("At least one recipient is required", errors_1.CryptoErrorCode.INVALID_INPUT);
58
58
  }
59
59
  const dek = (0, utils_js_1.randomBytes)(32);
60
60
  const sealed = secretbox.seal(dek, plaintext);
@@ -88,20 +88,13 @@ function multiDecryptClassical(secretKey, wrappedKey, ciphertext) {
88
88
  function wrapDekHybrid(dek, x25519PublicKey, mlKemPublicKey) {
89
89
  const x25519Pub = hexToBytes(x25519PublicKey);
90
90
  const mlKemPub = hexToBytes(mlKemPublicKey);
91
- const ephPriv = (0, utils_js_1.randomBytes)(32);
92
- const ephPub = ed25519_js_1.x25519.getPublicKey(ephPriv);
93
- const x25519Shared = ed25519_js_1.x25519.getSharedSecret(ephPriv, x25519Pub);
94
- const { cipherText: mlKemCt, sharedSecret: mlKemShared } = ml_kem_js_1.ml_kem768.encapsulate(mlKemPub);
95
- const combined = new Uint8Array(x25519Shared.length + mlKemShared.length);
96
- combined.set(x25519Shared);
97
- combined.set(mlKemShared, x25519Shared.length);
98
- const kek = (0, hkdf_js_1.hkdf)(sha2_js_1.sha256, combined, ephPub, new TextEncoder().encode("multi-recipient-pq-v1"), 32);
91
+ const { ephPub, mlKemCt, key: kek, } = (0, hybrid_combiner_1.hybridSend)(x25519Pub, mlKemPub, "multi-recipient");
99
92
  const cipher = (0, aes_js_1.aeskw)(kek);
100
93
  const wrapped = cipher.encrypt(dek);
101
94
  return {
102
- wrappedKey: Buffer.from(wrapped).toString("base64"),
103
- ephemeralPublicKey: Buffer.from(ephPub).toString("hex"),
104
- mlKemCiphertext: Buffer.from(mlKemCt).toString("hex"),
95
+ wrappedKey: (0, encoding_1.toBase64)(wrapped),
96
+ ephemeralPublicKey: (0, encoding_1.toHex)(ephPub),
97
+ mlKemCiphertext: (0, encoding_1.toHex)(mlKemCt),
105
98
  };
106
99
  }
107
100
  function unwrapDekHybrid(x25519SecretKey, mlKemSecretKey, ephemeralPublicKey, mlKemCiphertext, wrappedKey) {
@@ -113,19 +106,13 @@ function unwrapDekHybrid(x25519SecretKey, mlKemSecretKey, ephemeralPublicKey, ml
113
106
  : mlKemSecretKey;
114
107
  const ephPub = hexToBytes(ephemeralPublicKey);
115
108
  const mlKemCt = hexToBytes(mlKemCiphertext);
116
- const x25519Shared = ed25519_js_1.x25519.getSharedSecret(x25519Sec, ephPub);
117
- const mlKemShared = ml_kem_js_1.ml_kem768.decapsulate(mlKemCt, mlKemSec);
118
- const combined = new Uint8Array(x25519Shared.length + mlKemShared.length);
119
- combined.set(x25519Shared);
120
- combined.set(mlKemShared, x25519Shared.length);
121
- const kek = (0, hkdf_js_1.hkdf)(sha2_js_1.sha256, combined, ephPub, new TextEncoder().encode("multi-recipient-pq-v1"), 32);
122
- const wrapped = Buffer.from(wrappedKey, "base64");
123
- const cipher = (0, aes_js_1.aeskw)(kek);
124
- return cipher.decrypt(wrapped);
109
+ const keks = (0, hybrid_combiner_1.hybridReceiveKeys)(x25519Sec, mlKemSec, ephPub, mlKemCt, "multi-recipient", (x25519Shared, mlKemShared) => (0, hkdf_js_1.hkdf)(sha2_js_1.sha256, (0, encoding_1.concat)([x25519Shared, mlKemShared]), ephPub, new TextEncoder().encode("multi-recipient-pq-v1"), 32));
110
+ const wrapped = (0, encoding_1.fromBase64)(wrappedKey);
111
+ return (0, hybrid_combiner_1.openWithFirstKey)(keks, (kek) => (0, aes_js_1.aeskw)(kek).decrypt(wrapped));
125
112
  }
126
113
  function multiDecryptPQ(x25519SecretKey, mlKemSecretKey, wrappedKey, ciphertext) {
127
114
  if (!wrappedKey.mlKemCiphertext) {
128
- throw new Error("Missing ML-KEM ciphertext in wrapped key");
115
+ throw new errors_1.CryptoError("Missing ML-KEM ciphertext in wrapped key", errors_1.CryptoErrorCode.INVALID_CIPHERTEXT);
129
116
  }
130
117
  const dek = unwrapDekHybrid(x25519SecretKey, mlKemSecretKey, wrappedKey.ephemeralPublicKey, wrappedKey.mlKemCiphertext, wrappedKey.wrappedKey);
131
118
  return secretbox.open(dek, ciphertext);
@@ -1 +1 @@
1
- {"version":3,"file":"password-encrypt.d.ts","sourceRoot":"","sources":["../../src/high-level/password-encrypt.ts"],"names":[],"mappings":"AAoDA,MAAM,WAAW,sBAAsB;IAErC,QAAQ,EAAE,MAAM,GAAG,UAAU,CAAC;IAE9B,SAAS,EAAE,MAAM,GAAG,UAAU,CAAC;IAE/B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAGD,MAAM,WAAW,qBAAqB;IAEpC,SAAS,EAAE,MAAM,CAAC;IAElB,SAAS,EAAE,6BAA6B,CAAC;CAC1C;AAsDD,wBAAgB,eAAe,CAC7B,OAAO,EAAE,sBAAsB,GAC9B,qBAAqB,CAwBvB;AAQD,wBAAgB,eAAe,CAC7B,QAAQ,EAAE,MAAM,GAAG,UAAU,EAC7B,SAAS,EAAE,MAAM,GAAG,UAAU,GAC7B,UAAU,CA+BZ"}
1
+ {"version":3,"file":"password-encrypt.d.ts","sourceRoot":"","sources":["../../src/high-level/password-encrypt.ts"],"names":[],"mappings":"AAsDA,MAAM,WAAW,sBAAsB;IAErC,QAAQ,EAAE,MAAM,GAAG,UAAU,CAAC;IAE9B,SAAS,EAAE,MAAM,GAAG,UAAU,CAAC;IAE/B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAGD,MAAM,WAAW,qBAAqB;IAEpC,SAAS,EAAE,MAAM,CAAC;IAElB,SAAS,EAAE,6BAA6B,CAAC;CAC1C;AAsDD,wBAAgB,eAAe,CAC7B,OAAO,EAAE,sBAAsB,GAC9B,qBAAqB,CAwBvB;AAQD,wBAAgB,eAAe,CAC7B,QAAQ,EAAE,MAAM,GAAG,UAAU,EAC7B,SAAS,EAAE,MAAM,GAAG,UAAU,GAC7B,UAAU,CAsCZ"}