@sebastienrousseau/crypto-lib 0.0.2 → 0.0.7

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 (243) hide show
  1. package/README.md +334 -185
  2. package/dist/accel/index.d.ts +11 -0
  3. package/dist/accel/index.d.ts.map +1 -0
  4. package/dist/accel/index.js +31 -0
  5. package/dist/accel/native-pqc-bridge.d.ts +18 -0
  6. package/dist/accel/native-pqc-bridge.d.ts.map +1 -0
  7. package/dist/accel/native-pqc-bridge.js +100 -0
  8. package/dist/accel/wasm-bridge.d.ts +35 -0
  9. package/dist/accel/wasm-bridge.d.ts.map +1 -0
  10. package/dist/accel/wasm-bridge.js +161 -0
  11. package/dist/accel/webcrypto-bridge.d.ts +32 -0
  12. package/dist/accel/webcrypto-bridge.d.ts.map +1 -0
  13. package/dist/accel/webcrypto-bridge.js +179 -0
  14. package/dist/accel/webcrypto-modern.d.ts +40 -0
  15. package/dist/accel/webcrypto-modern.d.ts.map +1 -0
  16. package/dist/accel/webcrypto-modern.js +213 -0
  17. package/dist/accel/worker-pool.d.ts +23 -0
  18. package/dist/accel/worker-pool.d.ts.map +1 -0
  19. package/dist/accel/worker-pool.js +119 -0
  20. package/dist/bin/cryptolib.d.ts +25 -0
  21. package/dist/bin/cryptolib.d.ts.map +1 -0
  22. package/dist/bin/cryptolib.js +87 -0
  23. package/dist/config/config.d.ts +8 -0
  24. package/dist/config/config.d.ts.map +1 -0
  25. package/dist/config/config.js +13 -0
  26. package/dist/crypto.d.ts +43 -0
  27. package/dist/crypto.d.ts.map +1 -0
  28. package/dist/crypto.js +107 -0
  29. package/dist/enums.d.ts +81 -0
  30. package/dist/enums.d.ts.map +1 -0
  31. package/dist/enums.js +82 -0
  32. package/dist/errors.d.ts +14 -0
  33. package/dist/errors.d.ts.map +1 -0
  34. package/dist/errors.js +22 -0
  35. package/dist/high-level/index.d.ts +11 -0
  36. package/dist/high-level/index.d.ts.map +1 -0
  37. package/dist/high-level/index.js +53 -0
  38. package/dist/high-level/key-wrap.d.ts +16 -0
  39. package/dist/high-level/key-wrap.d.ts.map +1 -0
  40. package/dist/high-level/key-wrap.js +93 -0
  41. package/dist/high-level/multi-recipient.d.ts +25 -0
  42. package/dist/high-level/multi-recipient.d.ts.map +1 -0
  43. package/dist/high-level/multi-recipient.js +122 -0
  44. package/dist/high-level/password-encrypt.d.ts +14 -0
  45. package/dist/high-level/password-encrypt.d.ts.map +1 -0
  46. package/dist/high-level/password-encrypt.js +93 -0
  47. package/dist/high-level/sealedbox.d.ts +13 -0
  48. package/dist/high-level/sealedbox.d.ts.map +1 -0
  49. package/dist/high-level/sealedbox.js +119 -0
  50. package/dist/high-level/secretbox.d.ts +7 -0
  51. package/dist/high-level/secretbox.d.ts.map +1 -0
  52. package/dist/high-level/secretbox.js +54 -0
  53. package/dist/index.d.ts +16 -2
  54. package/dist/index.d.ts.map +1 -1
  55. package/dist/index.js +61 -2
  56. package/dist/key/key.d.ts +10 -0
  57. package/dist/key/key.d.ts.map +1 -0
  58. package/dist/key/key.js +25 -0
  59. package/dist/key/keystore.d.ts +11 -0
  60. package/dist/key/keystore.d.ts.map +1 -0
  61. package/dist/key/keystore.js +87 -0
  62. package/dist/keys/index.d.ts +7 -0
  63. package/dist/keys/index.d.ts.map +1 -0
  64. package/dist/keys/index.js +22 -0
  65. package/dist/keys/keygen.d.ts +16 -0
  66. package/dist/keys/keygen.d.ts.map +1 -0
  67. package/dist/keys/keygen.js +128 -0
  68. package/dist/keys/keyring.d.ts +35 -0
  69. package/dist/keys/keyring.d.ts.map +1 -0
  70. package/dist/keys/keyring.js +136 -0
  71. package/dist/keys/serialize.d.ts +30 -0
  72. package/dist/keys/serialize.d.ts.map +1 -0
  73. package/dist/keys/serialize.js +120 -0
  74. package/dist/lib/decrypt.d.ts +5 -5
  75. package/dist/lib/decrypt.d.ts.map +1 -1
  76. package/dist/lib/decrypt.js +49 -39
  77. package/dist/lib/encrypt.d.ts +2 -5
  78. package/dist/lib/encrypt.d.ts.map +1 -1
  79. package/dist/lib/encrypt.js +34 -39
  80. package/dist/lib/generate.d.ts +2 -11
  81. package/dist/lib/generate.d.ts.map +1 -1
  82. package/dist/lib/generate.js +57 -29
  83. package/dist/lib/index.d.ts +10 -0
  84. package/dist/lib/index.d.ts.map +1 -0
  85. package/dist/lib/index.js +53 -0
  86. package/dist/lib/key-output.d.ts +7 -0
  87. package/dist/lib/key-output.d.ts.map +1 -0
  88. package/dist/lib/key-output.js +50 -0
  89. package/dist/lib/reformat.d.ts +9 -0
  90. package/dist/lib/reformat.d.ts.map +1 -0
  91. package/dist/lib/reformat.js +67 -0
  92. package/dist/lib/revoke.d.ts +3 -3
  93. package/dist/lib/revoke.d.ts.map +1 -1
  94. package/dist/lib/revoke.js +36 -28
  95. package/dist/lib/session.d.ts +5 -0
  96. package/dist/lib/session.d.ts.map +1 -0
  97. package/dist/lib/session.js +55 -0
  98. package/dist/lib/sign.d.ts +4 -0
  99. package/dist/lib/sign.d.ts.map +1 -0
  100. package/dist/lib/sign.js +63 -0
  101. package/dist/lib/verify.d.ts +5 -0
  102. package/dist/lib/verify.d.ts.map +1 -0
  103. package/dist/lib/verify.js +52 -0
  104. package/dist/modern/aead.d.ts +17 -0
  105. package/dist/modern/aead.d.ts.map +1 -0
  106. package/dist/modern/aead.js +51 -0
  107. package/dist/modern/aes.d.ts +35 -0
  108. package/dist/modern/aes.d.ts.map +1 -0
  109. package/dist/modern/aes.js +82 -0
  110. package/dist/modern/cost-limits.d.ts +11 -0
  111. package/dist/modern/cost-limits.d.ts.map +1 -0
  112. package/dist/modern/cost-limits.js +35 -0
  113. package/dist/modern/curves.d.ts +86 -0
  114. package/dist/modern/curves.d.ts.map +1 -0
  115. package/dist/modern/curves.js +171 -0
  116. package/dist/modern/ecdh.d.ts +11 -0
  117. package/dist/modern/ecdh.d.ts.map +1 -0
  118. package/dist/modern/ecdh.js +28 -0
  119. package/dist/modern/fn-dsa.d.ts +19 -0
  120. package/dist/modern/fn-dsa.d.ts.map +1 -0
  121. package/dist/modern/fn-dsa.js +57 -0
  122. package/dist/modern/hash.d.ts +13 -0
  123. package/dist/modern/hash.d.ts.map +1 -0
  124. package/dist/modern/hash.js +45 -0
  125. package/dist/modern/hpke.d.ts +47 -0
  126. package/dist/modern/hpke.d.ts.map +1 -0
  127. package/dist/modern/hpke.js +207 -0
  128. package/dist/modern/index.d.ts +40 -0
  129. package/dist/modern/index.d.ts.map +1 -0
  130. package/dist/modern/index.js +194 -0
  131. package/dist/modern/kdf.d.ts +23 -0
  132. package/dist/modern/kdf.d.ts.map +1 -0
  133. package/dist/modern/kdf.js +67 -0
  134. package/dist/modern/mac.d.ts +51 -0
  135. package/dist/modern/mac.d.ts.map +1 -0
  136. package/dist/modern/mac.js +119 -0
  137. package/dist/modern/password.d.ts +40 -0
  138. package/dist/modern/password.d.ts.map +1 -0
  139. package/dist/modern/password.js +123 -0
  140. package/dist/modern/pq-hash-sign.d.ts +18 -0
  141. package/dist/modern/pq-hash-sign.d.ts.map +1 -0
  142. package/dist/modern/pq-hash-sign.js +73 -0
  143. package/dist/modern/pq-kem.d.ts +88 -0
  144. package/dist/modern/pq-kem.d.ts.map +1 -0
  145. package/dist/modern/pq-kem.js +270 -0
  146. package/dist/modern/pq-sign.d.ts +30 -0
  147. package/dist/modern/pq-sign.d.ts.map +1 -0
  148. package/dist/modern/pq-sign.js +94 -0
  149. package/dist/modern/pq.d.ts +38 -0
  150. package/dist/modern/pq.d.ts.map +1 -0
  151. package/dist/modern/pq.js +80 -0
  152. package/dist/modern/signing.d.ts +16 -0
  153. package/dist/modern/signing.d.ts.map +1 -0
  154. package/dist/modern/signing.js +34 -0
  155. package/dist/protocols/index.d.ts +7 -0
  156. package/dist/protocols/index.d.ts.map +1 -0
  157. package/dist/protocols/index.js +46 -0
  158. package/dist/protocols/iso20022.d.ts +51 -0
  159. package/dist/protocols/iso20022.d.ts.map +1 -0
  160. package/dist/protocols/iso20022.js +145 -0
  161. package/dist/protocols/opaque/ake.d.ts +51 -0
  162. package/dist/protocols/opaque/ake.d.ts.map +1 -0
  163. package/dist/protocols/opaque/ake.js +102 -0
  164. package/dist/protocols/opaque/client.d.ts +45 -0
  165. package/dist/protocols/opaque/client.d.ts.map +1 -0
  166. package/dist/protocols/opaque/client.js +63 -0
  167. package/dist/protocols/opaque/config.d.ts +14 -0
  168. package/dist/protocols/opaque/config.d.ts.map +1 -0
  169. package/dist/protocols/opaque/config.js +18 -0
  170. package/dist/protocols/opaque/credentials.d.ts +29 -0
  171. package/dist/protocols/opaque/credentials.d.ts.map +1 -0
  172. package/dist/protocols/opaque/credentials.js +61 -0
  173. package/dist/protocols/opaque/envelope.d.ts +36 -0
  174. package/dist/protocols/opaque/envelope.d.ts.map +1 -0
  175. package/dist/protocols/opaque/envelope.js +77 -0
  176. package/dist/protocols/opaque/ksf.d.ts +15 -0
  177. package/dist/protocols/opaque/ksf.d.ts.map +1 -0
  178. package/dist/protocols/opaque/ksf.js +27 -0
  179. package/dist/protocols/opaque/messages.d.ts +61 -0
  180. package/dist/protocols/opaque/messages.d.ts.map +1 -0
  181. package/dist/protocols/opaque/messages.js +94 -0
  182. package/dist/protocols/opaque/server.d.ts +33 -0
  183. package/dist/protocols/opaque/server.d.ts.map +1 -0
  184. package/dist/protocols/opaque/server.js +74 -0
  185. package/dist/protocols/opaque/suite.d.ts +58 -0
  186. package/dist/protocols/opaque/suite.d.ts.map +1 -0
  187. package/dist/protocols/opaque/suite.js +115 -0
  188. package/dist/protocols/pake.d.ts +14 -0
  189. package/dist/protocols/pake.d.ts.map +1 -0
  190. package/dist/protocols/pake.js +33 -0
  191. package/dist/protocols/pqxdh.d.ts +53 -0
  192. package/dist/protocols/pqxdh.d.ts.map +1 -0
  193. package/dist/protocols/pqxdh.js +143 -0
  194. package/dist/protocols/ratchet.d.ts +52 -0
  195. package/dist/protocols/ratchet.d.ts.map +1 -0
  196. package/dist/protocols/ratchet.js +187 -0
  197. package/dist/protocols/threshold.d.ts +23 -0
  198. package/dist/protocols/threshold.d.ts.map +1 -0
  199. package/dist/protocols/threshold.js +191 -0
  200. package/dist/registry.d.ts +20 -0
  201. package/dist/registry.d.ts.map +1 -0
  202. package/dist/registry.js +399 -0
  203. package/dist/streaming/index.d.ts +7 -0
  204. package/dist/streaming/index.d.ts.map +1 -0
  205. package/dist/streaming/index.js +15 -0
  206. package/dist/streaming/stream-aead.d.ts +17 -0
  207. package/dist/streaming/stream-aead.d.ts.map +1 -0
  208. package/dist/streaming/stream-aead.js +125 -0
  209. package/dist/streaming/stream-hash.d.ts +9 -0
  210. package/dist/streaming/stream-hash.d.ts.map +1 -0
  211. package/dist/streaming/stream-hash.js +52 -0
  212. package/dist/streaming/web-streams.d.ts +17 -0
  213. package/dist/streaming/web-streams.d.ts.map +1 -0
  214. package/dist/streaming/web-streams.js +114 -0
  215. package/dist/tokens/index.d.ts +5 -0
  216. package/dist/tokens/index.d.ts.map +1 -0
  217. package/dist/tokens/index.js +15 -0
  218. package/dist/tokens/key-rotation.d.ts +20 -0
  219. package/dist/tokens/key-rotation.d.ts.map +1 -0
  220. package/dist/tokens/key-rotation.js +76 -0
  221. package/dist/tokens/paseto.d.ts +46 -0
  222. package/dist/tokens/paseto.d.ts.map +1 -0
  223. package/dist/tokens/paseto.js +200 -0
  224. package/dist/types/types.d.ts +57 -0
  225. package/dist/types/types.d.ts.map +1 -1
  226. package/dist/types/types.js +1 -0
  227. package/dist/utils.d.ts +13 -0
  228. package/dist/utils.d.ts.map +1 -0
  229. package/dist/utils.js +56 -0
  230. package/package.json +77 -130
  231. package/dist/COPYRIGHT +0 -15
  232. package/dist/Report.txt +0 -72
  233. package/dist/bin/crypto-lib.d.ts +0 -13
  234. package/dist/bin/crypto-lib.d.ts.map +0 -1
  235. package/dist/bin/crypto-lib.js +0 -52
  236. package/dist/bin/crypto-lib.js.map +0 -1
  237. package/dist/index.js.map +0 -1
  238. package/dist/lib/decrypt.js.map +0 -1
  239. package/dist/lib/encrypt.js.map +0 -1
  240. package/dist/lib/generate.js.map +0 -1
  241. package/dist/lib/revoke.js.map +0 -1
  242. package/dist/package.json +0 -131
  243. package/dist/types/types.js.map +0 -1
@@ -0,0 +1,13 @@
1
+ export interface SealedBoxResult {
2
+ sealed: string;
3
+ algorithm: "x25519-xchacha20-poly1305";
4
+ }
5
+ export interface PqSealedBoxResult {
6
+ sealed: string;
7
+ algorithm: "x25519-ml-kem-768-xchacha20-poly1305";
8
+ }
9
+ export declare function seal(recipientPublicKey: string | Uint8Array, plaintext: string | Uint8Array): SealedBoxResult;
10
+ export declare function open(recipientSecretKey: string | Uint8Array, sealed: string | Uint8Array): Uint8Array;
11
+ export declare function sealPQ(recipientX25519Public: string | Uint8Array, recipientMlKemPublic: string | Uint8Array, plaintext: string | Uint8Array): PqSealedBoxResult;
12
+ export declare function openPQ(recipientX25519Secret: string | Uint8Array, recipientMlKemSecret: string | Uint8Array, sealed: string | Uint8Array): Uint8Array;
13
+ //# sourceMappingURL=sealedbox.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sealedbox.d.ts","sourceRoot":"","sources":["../../src/high-level/sealedbox.ts"],"names":[],"mappings":"AA8CA,MAAM,WAAW,eAAe;IAE9B,MAAM,EAAE,MAAM,CAAC;IAEf,SAAS,EAAE,2BAA2B,CAAC;CACxC;AAGD,MAAM,WAAW,iBAAiB;IAEhC,MAAM,EAAE,MAAM,CAAC;IAEf,SAAS,EAAE,sCAAsC,CAAC;CACnD;AAQD,wBAAgB,IAAI,CAClB,kBAAkB,EAAE,MAAM,GAAG,UAAU,EACvC,SAAS,EAAE,MAAM,GAAG,UAAU,GAC7B,eAAe,CAsCjB;AAQD,wBAAgB,IAAI,CAClB,kBAAkB,EAAE,MAAM,GAAG,UAAU,EACvC,MAAM,EAAE,MAAM,GAAG,UAAU,GAC1B,UAAU,CA+BZ;AAmBD,wBAAgB,MAAM,CACpB,qBAAqB,EAAE,MAAM,GAAG,UAAU,EAC1C,oBAAoB,EAAE,MAAM,GAAG,UAAU,EACzC,SAAS,EAAE,MAAM,GAAG,UAAU,GAC7B,iBAAiB,CAoDnB;AAYD,wBAAgB,MAAM,CACpB,qBAAqB,EAAE,MAAM,GAAG,UAAU,EAC1C,oBAAoB,EAAE,MAAM,GAAG,UAAU,EACzC,MAAM,EAAE,MAAM,GAAG,UAAU,GAC1B,UAAU,CAiDZ"}
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.seal = seal;
4
+ exports.open = open;
5
+ exports.sealPQ = sealPQ;
6
+ exports.openPQ = openPQ;
7
+ const ed25519_js_1 = require("@noble/curves/ed25519.js");
8
+ const ml_kem_js_1 = require("@noble/post-quantum/ml-kem.js");
9
+ const hkdf_js_1 = require("@noble/hashes/hkdf.js");
10
+ const sha2_js_1 = require("@noble/hashes/sha2.js");
11
+ const chacha_js_1 = require("@noble/ciphers/chacha.js");
12
+ const utils_js_1 = require("@noble/ciphers/utils.js");
13
+ const EPHEMERAL_LEN = 32;
14
+ const NONCE_LEN = 24;
15
+ const TAG_LEN = 16;
16
+ const HEX_RE = /^[0-9a-fA-F]*$/;
17
+ function hexToBytes(hex) {
18
+ if (!HEX_RE.test(hex))
19
+ throw new Error("Invalid hex string");
20
+ return Buffer.from(hex, "hex");
21
+ }
22
+ function toData(input) {
23
+ return input instanceof Uint8Array ? input : Buffer.from(input, "utf8");
24
+ }
25
+ function seal(recipientPublicKey, plaintext) {
26
+ const recipPub = typeof recipientPublicKey === "string"
27
+ ? hexToBytes(recipientPublicKey)
28
+ : recipientPublicKey;
29
+ const ephPriv = (0, utils_js_1.randomBytes)(32);
30
+ const ephPub = ed25519_js_1.x25519.getPublicKey(ephPriv);
31
+ const raw = ed25519_js_1.x25519.getSharedSecret(ephPriv, recipPub);
32
+ const encKey = (0, hkdf_js_1.hkdf)(sha2_js_1.sha256, raw, ephPub, new TextEncoder().encode("sealedbox-v1"), 32);
33
+ const nonce = (0, utils_js_1.randomBytes)(NONCE_LEN);
34
+ const pt = toData(plaintext);
35
+ const cipher = (0, chacha_js_1.xchacha20poly1305)(encKey, nonce);
36
+ const ct = cipher.encrypt(pt);
37
+ const out = new Uint8Array(EPHEMERAL_LEN + NONCE_LEN + ct.length);
38
+ out.set(ephPub);
39
+ out.set(nonce, EPHEMERAL_LEN);
40
+ out.set(ct, EPHEMERAL_LEN + NONCE_LEN);
41
+ return {
42
+ sealed: Buffer.from(out).toString("base64"),
43
+ algorithm: "x25519-xchacha20-poly1305",
44
+ };
45
+ }
46
+ function open(recipientSecretKey, sealed) {
47
+ const secKey = typeof recipientSecretKey === "string"
48
+ ? hexToBytes(recipientSecretKey)
49
+ : recipientSecretKey;
50
+ const raw = sealed instanceof Uint8Array ? sealed : Buffer.from(sealed, "base64");
51
+ const minLen = EPHEMERAL_LEN + NONCE_LEN + TAG_LEN;
52
+ if (raw.length < minLen) {
53
+ throw new Error(`Sealed box too short: expected >= ${minLen} bytes, got ${raw.length}`);
54
+ }
55
+ const ephPub = raw.subarray(0, EPHEMERAL_LEN);
56
+ const nonce = raw.subarray(EPHEMERAL_LEN, EPHEMERAL_LEN + NONCE_LEN);
57
+ const ct = raw.subarray(EPHEMERAL_LEN + NONCE_LEN);
58
+ const shared = ed25519_js_1.x25519.getSharedSecret(secKey, ephPub);
59
+ const encKey = (0, hkdf_js_1.hkdf)(sha2_js_1.sha256, shared, ephPub, new TextEncoder().encode("sealedbox-v1"), 32);
60
+ const cipher = (0, chacha_js_1.xchacha20poly1305)(encKey, nonce);
61
+ return cipher.decrypt(ct);
62
+ }
63
+ function sealPQ(recipientX25519Public, recipientMlKemPublic, plaintext) {
64
+ const x25519Pub = typeof recipientX25519Public === "string"
65
+ ? hexToBytes(recipientX25519Public)
66
+ : recipientX25519Public;
67
+ const mlKemPub = typeof recipientMlKemPublic === "string"
68
+ ? hexToBytes(recipientMlKemPublic)
69
+ : recipientMlKemPublic;
70
+ const ephPriv = (0, utils_js_1.randomBytes)(32);
71
+ const ephPub = ed25519_js_1.x25519.getPublicKey(ephPriv);
72
+ const x25519Shared = ed25519_js_1.x25519.getSharedSecret(ephPriv, x25519Pub);
73
+ const { cipherText: mlKemCt, sharedSecret: mlKemShared } = ml_kem_js_1.ml_kem768.encapsulate(mlKemPub);
74
+ const combined = new Uint8Array(x25519Shared.length + mlKemShared.length);
75
+ combined.set(x25519Shared);
76
+ combined.set(mlKemShared, x25519Shared.length);
77
+ const encKey = (0, hkdf_js_1.hkdf)(sha2_js_1.sha256, combined, ephPub, new TextEncoder().encode("pq-sealedbox-v1"), 32);
78
+ const nonce = (0, utils_js_1.randomBytes)(NONCE_LEN);
79
+ const pt = toData(plaintext);
80
+ const cipher = (0, chacha_js_1.xchacha20poly1305)(encKey, nonce);
81
+ const ct = cipher.encrypt(pt);
82
+ const mlKemCtLen = mlKemCt.length;
83
+ const out = new Uint8Array(EPHEMERAL_LEN + mlKemCtLen + NONCE_LEN + ct.length);
84
+ out.set(ephPub);
85
+ out.set(mlKemCt, EPHEMERAL_LEN);
86
+ out.set(nonce, EPHEMERAL_LEN + mlKemCtLen);
87
+ out.set(ct, EPHEMERAL_LEN + mlKemCtLen + NONCE_LEN);
88
+ return {
89
+ sealed: Buffer.from(out).toString("base64"),
90
+ algorithm: "x25519-ml-kem-768-xchacha20-poly1305",
91
+ };
92
+ }
93
+ const ML_KEM_768_CT_LEN = 1088;
94
+ function openPQ(recipientX25519Secret, recipientMlKemSecret, sealed) {
95
+ const x25519Sec = typeof recipientX25519Secret === "string"
96
+ ? hexToBytes(recipientX25519Secret)
97
+ : recipientX25519Secret;
98
+ const mlKemSec = typeof recipientMlKemSecret === "string"
99
+ ? hexToBytes(recipientMlKemSecret)
100
+ : recipientMlKemSecret;
101
+ const raw = sealed instanceof Uint8Array ? sealed : Buffer.from(sealed, "base64");
102
+ const minLen = EPHEMERAL_LEN + ML_KEM_768_CT_LEN + NONCE_LEN + TAG_LEN;
103
+ if (raw.length < minLen) {
104
+ throw new Error(`PQ sealed box too short: expected >= ${minLen} bytes, got ${raw.length}`);
105
+ }
106
+ const ephPub = raw.subarray(0, EPHEMERAL_LEN);
107
+ const mlKemCt = raw.subarray(EPHEMERAL_LEN, EPHEMERAL_LEN + ML_KEM_768_CT_LEN);
108
+ const nonce = raw.subarray(EPHEMERAL_LEN + ML_KEM_768_CT_LEN, EPHEMERAL_LEN + ML_KEM_768_CT_LEN + NONCE_LEN);
109
+ const ct = raw.subarray(EPHEMERAL_LEN + ML_KEM_768_CT_LEN + NONCE_LEN);
110
+ const x25519Shared = ed25519_js_1.x25519.getSharedSecret(x25519Sec, ephPub);
111
+ const mlKemShared = ml_kem_js_1.ml_kem768.decapsulate(mlKemCt, mlKemSec);
112
+ const combined = new Uint8Array(x25519Shared.length + mlKemShared.length);
113
+ combined.set(x25519Shared);
114
+ combined.set(mlKemShared, x25519Shared.length);
115
+ const encKey = (0, hkdf_js_1.hkdf)(sha2_js_1.sha256, combined, ephPub, new TextEncoder().encode("pq-sealedbox-v1"), 32);
116
+ const cipher = (0, chacha_js_1.xchacha20poly1305)(encKey, nonce);
117
+ return cipher.decrypt(ct);
118
+ }
119
+ //# sourceMappingURL=sealedbox.js.map
@@ -0,0 +1,7 @@
1
+ export interface SecretboxResult {
2
+ sealed: string;
3
+ algorithm: "xchacha20-poly1305";
4
+ }
5
+ export declare function seal(key: string | Uint8Array, plaintext: string | Uint8Array, aad?: Uint8Array): SecretboxResult;
6
+ export declare function open(key: string | Uint8Array, sealed: string | Uint8Array, aad?: Uint8Array): Uint8Array;
7
+ //# sourceMappingURL=secretbox.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"secretbox.d.ts","sourceRoot":"","sources":["../../src/high-level/secretbox.ts"],"names":[],"mappings":"AAoDA,MAAM,WAAW,eAAe;IAE9B,MAAM,EAAE,MAAM,CAAC;IAEf,SAAS,EAAE,oBAAoB,CAAC;CACjC;AAQD,wBAAgB,IAAI,CAClB,GAAG,EAAE,MAAM,GAAG,UAAU,EACxB,SAAS,EAAE,MAAM,GAAG,UAAU,EAC9B,GAAG,CAAC,EAAE,UAAU,GACf,eAAe,CAajB;AAOD,wBAAgB,IAAI,CAClB,GAAG,EAAE,MAAM,GAAG,UAAU,EACxB,MAAM,EAAE,MAAM,GAAG,UAAU,EAC3B,GAAG,CAAC,EAAE,UAAU,GACf,UAAU,CAWZ"}
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.seal = seal;
4
+ exports.open = open;
5
+ const chacha_js_1 = require("@noble/ciphers/chacha.js");
6
+ const utils_js_1 = require("@noble/ciphers/utils.js");
7
+ const NONCE_LEN = 24;
8
+ const KEY_LEN = 32;
9
+ const TAG_LEN = 16;
10
+ const HEX_RE = /^[0-9a-fA-F]*$/;
11
+ function toKey(key) {
12
+ if (key instanceof Uint8Array) {
13
+ if (key.length !== KEY_LEN) {
14
+ throw new Error(`Key must be ${KEY_LEN} bytes, got ${key.length}`);
15
+ }
16
+ return key;
17
+ }
18
+ if (!HEX_RE.test(key))
19
+ throw new Error("Invalid hex key");
20
+ const buf = Buffer.from(key, "hex");
21
+ if (buf.length !== KEY_LEN) {
22
+ throw new Error(`Key must be ${KEY_LEN} bytes (${KEY_LEN * 8} bits), got ${buf.length}`);
23
+ }
24
+ return buf;
25
+ }
26
+ function toData(input) {
27
+ return input instanceof Uint8Array ? input : Buffer.from(input, "utf8");
28
+ }
29
+ function seal(key, plaintext, aad) {
30
+ const k = toKey(key);
31
+ const pt = toData(plaintext);
32
+ const nonce = (0, utils_js_1.randomBytes)(NONCE_LEN);
33
+ const cipher = (0, chacha_js_1.xchacha20poly1305)(k, nonce, aad);
34
+ const ct = cipher.encrypt(pt);
35
+ const out = new Uint8Array(NONCE_LEN + ct.length);
36
+ out.set(nonce);
37
+ out.set(ct, NONCE_LEN);
38
+ return {
39
+ sealed: Buffer.from(out).toString("base64"),
40
+ algorithm: "xchacha20-poly1305",
41
+ };
42
+ }
43
+ function open(key, sealed, aad) {
44
+ const k = toKey(key);
45
+ const raw = sealed instanceof Uint8Array ? sealed : Buffer.from(sealed, "base64");
46
+ if (raw.length < NONCE_LEN + TAG_LEN) {
47
+ throw new Error("Sealed box too short — missing nonce or auth tag");
48
+ }
49
+ const nonce = raw.subarray(0, NONCE_LEN);
50
+ const ct = raw.subarray(NONCE_LEN);
51
+ const cipher = (0, chacha_js_1.xchacha20poly1305)(k, nonce, aad);
52
+ return cipher.decrypt(ct);
53
+ }
54
+ //# sourceMappingURL=secretbox.js.map
package/dist/index.d.ts CHANGED
@@ -1,3 +1,17 @@
1
- import cryptoLib from "./bin/crypto-lib";
2
- export default cryptoLib;
1
+ import CryptoLib from "./bin/cryptolib";
2
+ export default CryptoLib;
3
+ export * from "./modern";
4
+ export * from "./high-level";
5
+ export * from "./keys";
6
+ export * from "./streaming";
7
+ export * as protocols from "./protocols";
8
+ export * from "./protocols/iso20022";
9
+ export * as tokens from "./tokens";
10
+ export * from "./accel";
11
+ export { crypto } from "./crypto";
12
+ export type { SignAlgorithm, VerifyPasswordOptions, HmacVerifyOptions, } from "./crypto";
13
+ export { getAlgorithm, listAlgorithms, recommended, isDeprecated, } from "./registry";
14
+ export type { AlgorithmInfo, AlgorithmCategory, AlgorithmStatus, SecurityLevel, } from "./registry";
15
+ export { CryptoError, CryptoErrorCode } from "./errors";
16
+ export { timingSafeEqual, SecureBuffer, wipeMemory } from "./utils";
3
17
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,kBAAkB,CAAC;AAEzC,eAAe,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,OAAO,SAAS,MAAM,iBAAiB,CAAC;AAGxC,eAAe,SAAS,CAAC;AAGzB,cAAc,UAAU,CAAC;AAGzB,cAAc,cAAc,CAAC;AAG7B,cAAc,QAAQ,CAAC;AAGvB,cAAc,aAAa,CAAC;AAG5B,OAAO,KAAK,SAAS,MAAM,aAAa,CAAC;AACzC,cAAc,sBAAsB,CAAC;AAGrC,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AAGnC,cAAc,SAAS,CAAC;AAGxB,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,YAAY,EACV,aAAa,EACb,qBAAqB,EACrB,iBAAiB,GAClB,MAAM,UAAU,CAAC;AAGlB,OAAO,EACL,YAAY,EACZ,cAAc,EACd,WAAW,EACX,YAAY,GACb,MAAM,YAAY,CAAC;AAEpB,YAAY,EACV,aAAa,EACb,iBAAiB,EACjB,eAAe,EACf,aAAa,GACd,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAGxD,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC"}
package/dist/index.js CHANGED
@@ -1,8 +1,67 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
+ };
21
+ var __importStar = (this && this.__importStar) || (function () {
22
+ var ownKeys = function(o) {
23
+ ownKeys = Object.getOwnPropertyNames || function (o) {
24
+ var ar = [];
25
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
26
+ return ar;
27
+ };
28
+ return ownKeys(o);
29
+ };
30
+ return function (mod) {
31
+ if (mod && mod.__esModule) return mod;
32
+ var result = {};
33
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
34
+ __setModuleDefault(result, mod);
35
+ return result;
36
+ };
37
+ })();
2
38
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
39
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
40
  };
5
41
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const crypto_lib_1 = __importDefault(require("./bin/crypto-lib"));
7
- exports.default = crypto_lib_1.default;
42
+ exports.wipeMemory = exports.SecureBuffer = exports.timingSafeEqual = exports.CryptoErrorCode = exports.CryptoError = exports.isDeprecated = exports.recommended = exports.listAlgorithms = exports.getAlgorithm = exports.crypto = exports.tokens = exports.protocols = void 0;
43
+ const cryptolib_1 = __importDefault(require("./bin/cryptolib"));
44
+ exports.default = cryptolib_1.default;
45
+ __exportStar(require("./modern"), exports);
46
+ __exportStar(require("./high-level"), exports);
47
+ __exportStar(require("./keys"), exports);
48
+ __exportStar(require("./streaming"), exports);
49
+ exports.protocols = __importStar(require("./protocols"));
50
+ __exportStar(require("./protocols/iso20022"), exports);
51
+ exports.tokens = __importStar(require("./tokens"));
52
+ __exportStar(require("./accel"), exports);
53
+ var crypto_1 = require("./crypto");
54
+ Object.defineProperty(exports, "crypto", { enumerable: true, get: function () { return crypto_1.crypto; } });
55
+ var registry_1 = require("./registry");
56
+ Object.defineProperty(exports, "getAlgorithm", { enumerable: true, get: function () { return registry_1.getAlgorithm; } });
57
+ Object.defineProperty(exports, "listAlgorithms", { enumerable: true, get: function () { return registry_1.listAlgorithms; } });
58
+ Object.defineProperty(exports, "recommended", { enumerable: true, get: function () { return registry_1.recommended; } });
59
+ Object.defineProperty(exports, "isDeprecated", { enumerable: true, get: function () { return registry_1.isDeprecated; } });
60
+ var errors_1 = require("./errors");
61
+ Object.defineProperty(exports, "CryptoError", { enumerable: true, get: function () { return errors_1.CryptoError; } });
62
+ Object.defineProperty(exports, "CryptoErrorCode", { enumerable: true, get: function () { return errors_1.CryptoErrorCode; } });
63
+ var utils_1 = require("./utils");
64
+ Object.defineProperty(exports, "timingSafeEqual", { enumerable: true, get: function () { return utils_1.timingSafeEqual; } });
65
+ Object.defineProperty(exports, "SecureBuffer", { enumerable: true, get: function () { return utils_1.SecureBuffer; } });
66
+ Object.defineProperty(exports, "wipeMemory", { enumerable: true, get: function () { return utils_1.wipeMemory; } });
8
67
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,10 @@
1
+ export declare const getPrivateKey: () => Promise<string>;
2
+ export declare const getPublicKey: () => Promise<string>;
3
+ export declare const getRevocationCertificate: () => Promise<string>;
4
+ declare const _default: {
5
+ getPrivateKey: () => Promise<string>;
6
+ getPublicKey: () => Promise<string>;
7
+ getRevocationCertificate: () => Promise<string>;
8
+ };
9
+ export default _default;
10
+ //# sourceMappingURL=key.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"key.d.ts","sourceRoot":"","sources":["../../src/key/key.ts"],"names":[],"mappings":"AAoBA,eAAO,MAAM,aAAa,QAAa,OAAO,CAAC,MAAM,CAGpD,CAAC;AAKF,eAAO,MAAM,YAAY,QAAa,OAAO,CAAC,MAAM,CAGnD,CAAC;AAMF,eAAO,MAAM,wBAAwB,QAAa,OAAO,CAAC,MAAM,CAG/D,CAAC;;yBApBqC,OAAO,CAAC,MAAM,CAAC;wBAQhB,OAAO,CAAC,MAAM,CAAC;oCASH,OAAO,CAAC,MAAM,CAAC;;AAMjE,wBAOE"}
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getRevocationCertificate = exports.getPublicKey = exports.getPrivateKey = void 0;
4
+ const keystore_1 = require("./keystore");
5
+ const getPrivateKey = async () => {
6
+ const ks = await (0, keystore_1.loadKeystore)();
7
+ return ks.privateKeyArmored;
8
+ };
9
+ exports.getPrivateKey = getPrivateKey;
10
+ const getPublicKey = async () => {
11
+ const ks = await (0, keystore_1.loadKeystore)();
12
+ return ks.publicKeyArmored;
13
+ };
14
+ exports.getPublicKey = getPublicKey;
15
+ const getRevocationCertificate = async () => {
16
+ const ks = await (0, keystore_1.loadKeystore)();
17
+ return ks.revocationArmored;
18
+ };
19
+ exports.getRevocationCertificate = getRevocationCertificate;
20
+ exports.default = {
21
+ getPrivateKey: exports.getPrivateKey,
22
+ getPublicKey: exports.getPublicKey,
23
+ getRevocationCertificate: exports.getRevocationCertificate,
24
+ };
25
+ //# sourceMappingURL=key.js.map
@@ -0,0 +1,11 @@
1
+ import * as openpgp from "openpgp";
2
+ export interface Keystore {
3
+ readonly privateKeyArmored: string;
4
+ readonly publicKeyArmored: string;
5
+ readonly revocationArmored: string;
6
+ }
7
+ export declare function decodeArmor(raw: Buffer): string;
8
+ export declare function loadKeystore(dir?: string): Promise<Keystore>;
9
+ export declare function _resetKeystoreForTests(): void;
10
+ export declare function unlockPrivateKey(armoredKey: string, passphrase: string): Promise<openpgp.PrivateKey>;
11
+ //# sourceMappingURL=keystore.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keystore.d.ts","sourceRoot":"","sources":["../../src/key/keystore.ts"],"names":[],"mappings":"AAwBA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAGnC,MAAM,WAAW,QAAQ;IAEvB,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAC;IAEnC,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;IAElC,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAC;CACpC;AAOD,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAU/C;AAyBD,wBAAgB,YAAY,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAgB5D;AAKD,wBAAgB,sBAAsB,IAAI,IAAI,CAE7C;AAaD,wBAAsB,gBAAgB,CACpC,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,CAG7B"}
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.decodeArmor = decodeArmor;
37
+ exports.loadKeystore = loadKeystore;
38
+ exports._resetKeystoreForTests = _resetKeystoreForTests;
39
+ exports.unlockPrivateKey = unlockPrivateKey;
40
+ const promises_1 = require("fs/promises");
41
+ const path = __importStar(require("path"));
42
+ const openpgp = __importStar(require("openpgp"));
43
+ const ARMOR_BEGIN = Buffer.from("-----", "ascii");
44
+ function decodeArmor(raw) {
45
+ if (raw.length >= ARMOR_BEGIN.length &&
46
+ raw.subarray(0, ARMOR_BEGIN.length).equals(ARMOR_BEGIN)) {
47
+ return raw.toString("latin1");
48
+ }
49
+ return Buffer.from(raw.toString("latin1"), "base64").toString("latin1");
50
+ }
51
+ let cache;
52
+ function resolveKeyDir(dir) {
53
+ if (dir)
54
+ return dir;
55
+ if (process.env["CRYPTO_KEY_DIR"])
56
+ return process.env["CRYPTO_KEY_DIR"];
57
+ if (process.env["NODE_ENV"] === "production") {
58
+ throw new Error("CRYPTO_KEY_DIR is not set; refusing to use the bundled test keys in production");
59
+ }
60
+ return path.resolve(__dirname, "..", "key");
61
+ }
62
+ function loadKeystore(dir) {
63
+ if (cache)
64
+ return cache;
65
+ const keyDir = resolveKeyDir(dir);
66
+ cache = (async () => {
67
+ const [priv, pub, cert] = await Promise.all([
68
+ (0, promises_1.readFile)(path.join(keyDir, "rsa.key")),
69
+ (0, promises_1.readFile)(path.join(keyDir, "rsa.pub")),
70
+ (0, promises_1.readFile)(path.join(keyDir, "rsa.cert")),
71
+ ]);
72
+ return Object.freeze({
73
+ privateKeyArmored: decodeArmor(priv),
74
+ publicKeyArmored: decodeArmor(pub),
75
+ revocationArmored: decodeArmor(cert),
76
+ });
77
+ })();
78
+ return cache;
79
+ }
80
+ function _resetKeystoreForTests() {
81
+ cache = undefined;
82
+ }
83
+ async function unlockPrivateKey(armoredKey, passphrase) {
84
+ const read = await openpgp.readPrivateKey({ armoredKey });
85
+ return openpgp.decryptKey({ privateKey: read, passphrase });
86
+ }
87
+ //# sourceMappingURL=keystore.js.map
@@ -0,0 +1,7 @@
1
+ export { hexToBytes, bytesToHex, bytesToBase64, base64ToBytes, bytesToBase64url, base64urlToBytes, encodePem, decodePem, ed25519ToJwk, x25519ToJwk, jwkToHex, jwkThumbprint, } from "./serialize";
2
+ export type { PemLabel, Jwk } from "./serialize";
3
+ export { generateKeyPair, KEY_ALGORITHMS } from "./keygen";
4
+ export type { KeyAlgorithm, KeyMetadata, GeneratedKeyPair } from "./keygen";
5
+ export { Keyring } from "./keyring";
6
+ export type { KeyEntry, Jwks } from "./keyring";
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/keys/index.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,UAAU,EACV,UAAU,EACV,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EAChB,SAAS,EACT,SAAS,EACT,YAAY,EACZ,WAAW,EACX,QAAQ,EACR,aAAa,GACd,MAAM,aAAa,CAAC;AAGrB,YAAY,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAEjD,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE3D,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE5E,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,YAAY,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC"}
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Keyring = exports.KEY_ALGORITHMS = exports.generateKeyPair = exports.jwkThumbprint = exports.jwkToHex = exports.x25519ToJwk = exports.ed25519ToJwk = exports.decodePem = exports.encodePem = exports.base64urlToBytes = exports.bytesToBase64url = exports.base64ToBytes = exports.bytesToBase64 = exports.bytesToHex = exports.hexToBytes = void 0;
4
+ var serialize_1 = require("./serialize");
5
+ Object.defineProperty(exports, "hexToBytes", { enumerable: true, get: function () { return serialize_1.hexToBytes; } });
6
+ Object.defineProperty(exports, "bytesToHex", { enumerable: true, get: function () { return serialize_1.bytesToHex; } });
7
+ Object.defineProperty(exports, "bytesToBase64", { enumerable: true, get: function () { return serialize_1.bytesToBase64; } });
8
+ Object.defineProperty(exports, "base64ToBytes", { enumerable: true, get: function () { return serialize_1.base64ToBytes; } });
9
+ Object.defineProperty(exports, "bytesToBase64url", { enumerable: true, get: function () { return serialize_1.bytesToBase64url; } });
10
+ Object.defineProperty(exports, "base64urlToBytes", { enumerable: true, get: function () { return serialize_1.base64urlToBytes; } });
11
+ Object.defineProperty(exports, "encodePem", { enumerable: true, get: function () { return serialize_1.encodePem; } });
12
+ Object.defineProperty(exports, "decodePem", { enumerable: true, get: function () { return serialize_1.decodePem; } });
13
+ Object.defineProperty(exports, "ed25519ToJwk", { enumerable: true, get: function () { return serialize_1.ed25519ToJwk; } });
14
+ Object.defineProperty(exports, "x25519ToJwk", { enumerable: true, get: function () { return serialize_1.x25519ToJwk; } });
15
+ Object.defineProperty(exports, "jwkToHex", { enumerable: true, get: function () { return serialize_1.jwkToHex; } });
16
+ Object.defineProperty(exports, "jwkThumbprint", { enumerable: true, get: function () { return serialize_1.jwkThumbprint; } });
17
+ var keygen_1 = require("./keygen");
18
+ Object.defineProperty(exports, "generateKeyPair", { enumerable: true, get: function () { return keygen_1.generateKeyPair; } });
19
+ Object.defineProperty(exports, "KEY_ALGORITHMS", { enumerable: true, get: function () { return keygen_1.KEY_ALGORITHMS; } });
20
+ var keyring_1 = require("./keyring");
21
+ Object.defineProperty(exports, "Keyring", { enumerable: true, get: function () { return keyring_1.Keyring; } });
22
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,16 @@
1
+ export declare const KEY_ALGORITHMS: readonly ["ed25519", "x25519", "ed448", "x448", "p256", "p384", "ml-kem-512", "ml-kem-768", "ml-kem-1024", "ml-dsa-44", "ml-dsa-65", "ml-dsa-87"];
2
+ export type KeyAlgorithm = (typeof KEY_ALGORITHMS)[number];
3
+ export interface KeyMetadata {
4
+ kid?: string | undefined;
5
+ use?: "sig" | "enc" | undefined;
6
+ exp?: string | undefined;
7
+ }
8
+ export interface GeneratedKeyPair {
9
+ publicKey: string;
10
+ privateKey: string;
11
+ algorithm: KeyAlgorithm;
12
+ kid: string;
13
+ metadata: KeyMetadata;
14
+ }
15
+ export declare function generateKeyPair(algorithm: KeyAlgorithm, metadata?: KeyMetadata): GeneratedKeyPair;
16
+ //# sourceMappingURL=keygen.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keygen.d.ts","sourceRoot":"","sources":["../../src/keys/keygen.ts"],"names":[],"mappings":"AA4BA,eAAO,MAAM,cAAc,mJAajB,CAAC;AAGX,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC;AAG3D,MAAM,WAAW,WAAW;IAE1B,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAEzB,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,GAAG,SAAS,CAAC;IAEhC,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC1B;AAGD,MAAM,WAAW,gBAAgB;IAE/B,SAAS,EAAE,MAAM,CAAC;IAElB,UAAU,EAAE,MAAM,CAAC;IAEnB,SAAS,EAAE,YAAY,CAAC;IAExB,GAAG,EAAE,MAAM,CAAC;IAEZ,QAAQ,EAAE,WAAW,CAAC;CACvB;AAuBD,wBAAgB,eAAe,CAC7B,SAAS,EAAE,YAAY,EACvB,QAAQ,GAAE,WAAgB,GACzB,gBAAgB,CAkGlB"}
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.KEY_ALGORITHMS = void 0;
4
+ exports.generateKeyPair = generateKeyPair;
5
+ const ed25519_js_1 = require("@noble/curves/ed25519.js");
6
+ const ed448_js_1 = require("@noble/curves/ed448.js");
7
+ const nist_js_1 = require("@noble/curves/nist.js");
8
+ const ml_kem_js_1 = require("@noble/post-quantum/ml-kem.js");
9
+ const ml_dsa_js_1 = require("@noble/post-quantum/ml-dsa.js");
10
+ const utils_js_1 = require("@noble/ciphers/utils.js");
11
+ const sha2_js_1 = require("@noble/hashes/sha2.js");
12
+ const serialize_1 = require("./serialize");
13
+ exports.KEY_ALGORITHMS = [
14
+ "ed25519",
15
+ "x25519",
16
+ "ed448",
17
+ "x448",
18
+ "p256",
19
+ "p384",
20
+ "ml-kem-512",
21
+ "ml-kem-768",
22
+ "ml-kem-1024",
23
+ "ml-dsa-44",
24
+ "ml-dsa-65",
25
+ "ml-dsa-87",
26
+ ];
27
+ function toHex(bytes) {
28
+ return Buffer.from(bytes).toString("hex");
29
+ }
30
+ function generateKid(publicKey) {
31
+ const digest = (0, sha2_js_1.sha256)(publicKey);
32
+ return (0, serialize_1.bytesToBase64url)(digest.subarray(0, 16));
33
+ }
34
+ function generateKeyPair(algorithm, metadata = {}) {
35
+ let publicKey;
36
+ let privateKey;
37
+ switch (algorithm) {
38
+ case "ed25519": {
39
+ const priv = ed25519_js_1.ed25519.utils.randomSecretKey();
40
+ const pub = ed25519_js_1.ed25519.getPublicKey(priv);
41
+ privateKey = priv;
42
+ publicKey = pub;
43
+ break;
44
+ }
45
+ case "x25519": {
46
+ const priv = (0, utils_js_1.randomBytes)(32);
47
+ const pub = ed25519_js_1.x25519.getPublicKey(priv);
48
+ privateKey = priv;
49
+ publicKey = pub;
50
+ break;
51
+ }
52
+ case "ed448": {
53
+ const priv = ed448_js_1.ed448.utils.randomSecretKey();
54
+ const pub = ed448_js_1.ed448.getPublicKey(priv);
55
+ privateKey = priv;
56
+ publicKey = pub;
57
+ break;
58
+ }
59
+ case "x448": {
60
+ const priv = (0, utils_js_1.randomBytes)(56);
61
+ const pub = ed448_js_1.x448.getPublicKey(priv);
62
+ privateKey = priv;
63
+ publicKey = pub;
64
+ break;
65
+ }
66
+ case "p256": {
67
+ const priv = nist_js_1.p256.utils.randomSecretKey();
68
+ const pub = nist_js_1.p256.getPublicKey(priv);
69
+ privateKey = priv;
70
+ publicKey = pub;
71
+ break;
72
+ }
73
+ case "p384": {
74
+ const priv = nist_js_1.p384.utils.randomSecretKey();
75
+ const pub = nist_js_1.p384.getPublicKey(priv);
76
+ privateKey = priv;
77
+ publicKey = pub;
78
+ break;
79
+ }
80
+ case "ml-kem-512": {
81
+ const kp = ml_kem_js_1.ml_kem512.keygen();
82
+ publicKey = kp.publicKey;
83
+ privateKey = kp.secretKey;
84
+ break;
85
+ }
86
+ case "ml-kem-768": {
87
+ const kp = ml_kem_js_1.ml_kem768.keygen();
88
+ publicKey = kp.publicKey;
89
+ privateKey = kp.secretKey;
90
+ break;
91
+ }
92
+ case "ml-kem-1024": {
93
+ const kp = ml_kem_js_1.ml_kem1024.keygen();
94
+ publicKey = kp.publicKey;
95
+ privateKey = kp.secretKey;
96
+ break;
97
+ }
98
+ case "ml-dsa-44": {
99
+ const kp = ml_dsa_js_1.ml_dsa44.keygen();
100
+ publicKey = kp.publicKey;
101
+ privateKey = kp.secretKey;
102
+ break;
103
+ }
104
+ case "ml-dsa-65": {
105
+ const kp = ml_dsa_js_1.ml_dsa65.keygen();
106
+ publicKey = kp.publicKey;
107
+ privateKey = kp.secretKey;
108
+ break;
109
+ }
110
+ case "ml-dsa-87": {
111
+ const kp = ml_dsa_js_1.ml_dsa87.keygen();
112
+ publicKey = kp.publicKey;
113
+ privateKey = kp.secretKey;
114
+ break;
115
+ }
116
+ default:
117
+ throw new Error(`Unsupported algorithm: ${algorithm}. Supported: ${exports.KEY_ALGORITHMS.join(", ")}`);
118
+ }
119
+ const kid = metadata.kid ?? generateKid(publicKey);
120
+ return {
121
+ publicKey: toHex(publicKey),
122
+ privateKey: toHex(privateKey),
123
+ algorithm,
124
+ kid,
125
+ metadata: { ...metadata, kid },
126
+ };
127
+ }
128
+ //# sourceMappingURL=keygen.js.map