@sebastienrousseau/crypto-lib 0.0.3 → 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 (253) hide show
  1. package/README.md +304 -314
  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 +9 -7
  21. package/dist/bin/cryptolib.d.ts.map +1 -1
  22. package/dist/bin/cryptolib.js +25 -16
  23. package/dist/config/config.d.ts.map +1 -1
  24. package/dist/crypto.d.ts +43 -0
  25. package/dist/crypto.d.ts.map +1 -0
  26. package/dist/crypto.js +107 -0
  27. package/dist/enums.d.ts +29 -29
  28. package/dist/enums.d.ts.map +1 -1
  29. package/dist/enums.js +54 -54
  30. package/dist/errors.d.ts +14 -0
  31. package/dist/errors.d.ts.map +1 -0
  32. package/dist/errors.js +22 -0
  33. package/dist/high-level/index.d.ts +11 -0
  34. package/dist/high-level/index.d.ts.map +1 -0
  35. package/dist/high-level/index.js +53 -0
  36. package/dist/high-level/key-wrap.d.ts +16 -0
  37. package/dist/high-level/key-wrap.d.ts.map +1 -0
  38. package/dist/high-level/key-wrap.js +93 -0
  39. package/dist/high-level/multi-recipient.d.ts +25 -0
  40. package/dist/high-level/multi-recipient.d.ts.map +1 -0
  41. package/dist/high-level/multi-recipient.js +122 -0
  42. package/dist/high-level/password-encrypt.d.ts +14 -0
  43. package/dist/high-level/password-encrypt.d.ts.map +1 -0
  44. package/dist/high-level/password-encrypt.js +93 -0
  45. package/dist/high-level/sealedbox.d.ts +13 -0
  46. package/dist/high-level/sealedbox.d.ts.map +1 -0
  47. package/dist/high-level/sealedbox.js +119 -0
  48. package/dist/high-level/secretbox.d.ts +7 -0
  49. package/dist/high-level/secretbox.d.ts.map +1 -0
  50. package/dist/high-level/secretbox.js +54 -0
  51. package/dist/index.d.ts +14 -0
  52. package/dist/index.d.ts.map +1 -1
  53. package/dist/index.js +59 -0
  54. package/dist/key/key.d.ts +6 -13
  55. package/dist/key/key.d.ts.map +1 -1
  56. package/dist/key/key.js +20 -14
  57. package/dist/key/keystore.d.ts +11 -0
  58. package/dist/key/keystore.d.ts.map +1 -0
  59. package/dist/key/keystore.js +87 -0
  60. package/dist/keys/index.d.ts +7 -0
  61. package/dist/keys/index.d.ts.map +1 -0
  62. package/dist/keys/index.js +22 -0
  63. package/dist/keys/keygen.d.ts +16 -0
  64. package/dist/keys/keygen.d.ts.map +1 -0
  65. package/dist/keys/keygen.js +128 -0
  66. package/dist/keys/keyring.d.ts +35 -0
  67. package/dist/keys/keyring.d.ts.map +1 -0
  68. package/dist/keys/keyring.js +136 -0
  69. package/dist/keys/serialize.d.ts +30 -0
  70. package/dist/keys/serialize.d.ts.map +1 -0
  71. package/dist/keys/serialize.js +120 -0
  72. package/dist/lib/decrypt.d.ts +4 -1
  73. package/dist/lib/decrypt.d.ts.map +1 -1
  74. package/dist/lib/decrypt.js +43 -51
  75. package/dist/lib/encrypt.d.ts +1 -1
  76. package/dist/lib/encrypt.d.ts.map +1 -1
  77. package/dist/lib/encrypt.js +29 -45
  78. package/dist/lib/generate.d.ts.map +1 -1
  79. package/dist/lib/generate.js +56 -63
  80. package/dist/lib/index.d.ts.map +1 -1
  81. package/dist/lib/index.js +17 -7
  82. package/dist/lib/key-output.d.ts +7 -0
  83. package/dist/lib/key-output.d.ts.map +1 -0
  84. package/dist/lib/key-output.js +50 -0
  85. package/dist/lib/reformat.d.ts +6 -1
  86. package/dist/lib/reformat.d.ts.map +1 -1
  87. package/dist/lib/reformat.js +37 -61
  88. package/dist/lib/revoke.d.ts +2 -2
  89. package/dist/lib/revoke.d.ts.map +1 -1
  90. package/dist/lib/revoke.js +33 -50
  91. package/dist/lib/session.d.ts.map +1 -1
  92. package/dist/lib/session.js +21 -36
  93. package/dist/lib/sign.d.ts.map +1 -1
  94. package/dist/lib/sign.js +36 -43
  95. package/dist/lib/verify.d.ts +1 -3
  96. package/dist/lib/verify.d.ts.map +1 -1
  97. package/dist/lib/verify.js +25 -30
  98. package/dist/modern/aead.d.ts +17 -0
  99. package/dist/modern/aead.d.ts.map +1 -0
  100. package/dist/modern/aead.js +51 -0
  101. package/dist/modern/aes.d.ts +35 -0
  102. package/dist/modern/aes.d.ts.map +1 -0
  103. package/dist/modern/aes.js +82 -0
  104. package/dist/modern/cost-limits.d.ts +11 -0
  105. package/dist/modern/cost-limits.d.ts.map +1 -0
  106. package/dist/modern/cost-limits.js +35 -0
  107. package/dist/modern/curves.d.ts +86 -0
  108. package/dist/modern/curves.d.ts.map +1 -0
  109. package/dist/modern/curves.js +171 -0
  110. package/dist/modern/ecdh.d.ts +11 -0
  111. package/dist/modern/ecdh.d.ts.map +1 -0
  112. package/dist/modern/ecdh.js +28 -0
  113. package/dist/modern/fn-dsa.d.ts +19 -0
  114. package/dist/modern/fn-dsa.d.ts.map +1 -0
  115. package/dist/modern/fn-dsa.js +57 -0
  116. package/dist/modern/hash.d.ts +13 -0
  117. package/dist/modern/hash.d.ts.map +1 -0
  118. package/dist/modern/hash.js +45 -0
  119. package/dist/modern/hpke.d.ts +47 -0
  120. package/dist/modern/hpke.d.ts.map +1 -0
  121. package/dist/modern/hpke.js +207 -0
  122. package/dist/modern/index.d.ts +40 -0
  123. package/dist/modern/index.d.ts.map +1 -0
  124. package/dist/modern/index.js +194 -0
  125. package/dist/modern/kdf.d.ts +23 -0
  126. package/dist/modern/kdf.d.ts.map +1 -0
  127. package/dist/modern/kdf.js +67 -0
  128. package/dist/modern/mac.d.ts +51 -0
  129. package/dist/modern/mac.d.ts.map +1 -0
  130. package/dist/modern/mac.js +119 -0
  131. package/dist/modern/password.d.ts +40 -0
  132. package/dist/modern/password.d.ts.map +1 -0
  133. package/dist/modern/password.js +123 -0
  134. package/dist/modern/pq-hash-sign.d.ts +18 -0
  135. package/dist/modern/pq-hash-sign.d.ts.map +1 -0
  136. package/dist/modern/pq-hash-sign.js +73 -0
  137. package/dist/modern/pq-kem.d.ts +88 -0
  138. package/dist/modern/pq-kem.d.ts.map +1 -0
  139. package/dist/modern/pq-kem.js +270 -0
  140. package/dist/modern/pq-sign.d.ts +30 -0
  141. package/dist/modern/pq-sign.d.ts.map +1 -0
  142. package/dist/modern/pq-sign.js +94 -0
  143. package/dist/modern/pq.d.ts +38 -0
  144. package/dist/modern/pq.d.ts.map +1 -0
  145. package/dist/modern/pq.js +80 -0
  146. package/dist/modern/signing.d.ts +16 -0
  147. package/dist/modern/signing.d.ts.map +1 -0
  148. package/dist/modern/signing.js +34 -0
  149. package/dist/protocols/index.d.ts +7 -0
  150. package/dist/protocols/index.d.ts.map +1 -0
  151. package/dist/protocols/index.js +46 -0
  152. package/dist/protocols/iso20022.d.ts +51 -0
  153. package/dist/protocols/iso20022.d.ts.map +1 -0
  154. package/dist/protocols/iso20022.js +145 -0
  155. package/dist/protocols/opaque/ake.d.ts +51 -0
  156. package/dist/protocols/opaque/ake.d.ts.map +1 -0
  157. package/dist/protocols/opaque/ake.js +102 -0
  158. package/dist/protocols/opaque/client.d.ts +45 -0
  159. package/dist/protocols/opaque/client.d.ts.map +1 -0
  160. package/dist/protocols/opaque/client.js +63 -0
  161. package/dist/protocols/opaque/config.d.ts +14 -0
  162. package/dist/protocols/opaque/config.d.ts.map +1 -0
  163. package/dist/protocols/opaque/config.js +18 -0
  164. package/dist/protocols/opaque/credentials.d.ts +29 -0
  165. package/dist/protocols/opaque/credentials.d.ts.map +1 -0
  166. package/dist/protocols/opaque/credentials.js +61 -0
  167. package/dist/protocols/opaque/envelope.d.ts +36 -0
  168. package/dist/protocols/opaque/envelope.d.ts.map +1 -0
  169. package/dist/protocols/opaque/envelope.js +77 -0
  170. package/dist/protocols/opaque/ksf.d.ts +15 -0
  171. package/dist/protocols/opaque/ksf.d.ts.map +1 -0
  172. package/dist/protocols/opaque/ksf.js +27 -0
  173. package/dist/protocols/opaque/messages.d.ts +61 -0
  174. package/dist/protocols/opaque/messages.d.ts.map +1 -0
  175. package/dist/protocols/opaque/messages.js +94 -0
  176. package/dist/protocols/opaque/server.d.ts +33 -0
  177. package/dist/protocols/opaque/server.d.ts.map +1 -0
  178. package/dist/protocols/opaque/server.js +74 -0
  179. package/dist/protocols/opaque/suite.d.ts +58 -0
  180. package/dist/protocols/opaque/suite.d.ts.map +1 -0
  181. package/dist/protocols/opaque/suite.js +115 -0
  182. package/dist/protocols/pake.d.ts +14 -0
  183. package/dist/protocols/pake.d.ts.map +1 -0
  184. package/dist/protocols/pake.js +33 -0
  185. package/dist/protocols/pqxdh.d.ts +53 -0
  186. package/dist/protocols/pqxdh.d.ts.map +1 -0
  187. package/dist/protocols/pqxdh.js +143 -0
  188. package/dist/protocols/ratchet.d.ts +52 -0
  189. package/dist/protocols/ratchet.d.ts.map +1 -0
  190. package/dist/protocols/ratchet.js +187 -0
  191. package/dist/protocols/threshold.d.ts +23 -0
  192. package/dist/protocols/threshold.d.ts.map +1 -0
  193. package/dist/protocols/threshold.js +191 -0
  194. package/dist/registry.d.ts +20 -0
  195. package/dist/registry.d.ts.map +1 -0
  196. package/dist/registry.js +399 -0
  197. package/dist/streaming/index.d.ts +7 -0
  198. package/dist/streaming/index.d.ts.map +1 -0
  199. package/dist/streaming/index.js +15 -0
  200. package/dist/streaming/stream-aead.d.ts +17 -0
  201. package/dist/streaming/stream-aead.d.ts.map +1 -0
  202. package/dist/streaming/stream-aead.js +125 -0
  203. package/dist/streaming/stream-hash.d.ts +9 -0
  204. package/dist/streaming/stream-hash.d.ts.map +1 -0
  205. package/dist/streaming/stream-hash.js +52 -0
  206. package/dist/streaming/web-streams.d.ts +17 -0
  207. package/dist/streaming/web-streams.d.ts.map +1 -0
  208. package/dist/streaming/web-streams.js +114 -0
  209. package/dist/tokens/index.d.ts +5 -0
  210. package/dist/tokens/index.d.ts.map +1 -0
  211. package/dist/tokens/index.js +15 -0
  212. package/dist/tokens/key-rotation.d.ts +20 -0
  213. package/dist/tokens/key-rotation.d.ts.map +1 -0
  214. package/dist/tokens/key-rotation.js +76 -0
  215. package/dist/tokens/paseto.d.ts +46 -0
  216. package/dist/tokens/paseto.d.ts.map +1 -0
  217. package/dist/tokens/paseto.js +200 -0
  218. package/dist/types/types.d.ts +24 -16
  219. package/dist/types/types.d.ts.map +1 -1
  220. package/dist/utils.d.ts +13 -0
  221. package/dist/utils.d.ts.map +1 -0
  222. package/dist/utils.js +56 -0
  223. package/package.json +77 -142
  224. package/dist/COPYRIGHT +0 -15
  225. package/dist/Report.txt +0 -160
  226. package/dist/bin/cryptolib.js.map +0 -1
  227. package/dist/config/config.js.map +0 -1
  228. package/dist/data/decrypted.txt +0 -1
  229. package/dist/data/detached.sig +0 -1
  230. package/dist/data/encrypted.txt +0 -1
  231. package/dist/data/signed.sig +0 -1
  232. package/dist/enums.js.map +0 -1
  233. package/dist/index.js.map +0 -1
  234. package/dist/key/key.js.map +0 -1
  235. package/dist/key/rsa-reformat.cert +0 -1
  236. package/dist/key/rsa-reformat.key +0 -1
  237. package/dist/key/rsa-reformat.pub +0 -1
  238. package/dist/key/rsa-revoke.key +0 -1
  239. package/dist/key/rsa-revoke.pub +0 -1
  240. package/dist/key/rsa.cert +0 -1
  241. package/dist/key/rsa.key +0 -1
  242. package/dist/key/rsa.pub +0 -1
  243. package/dist/lib/decrypt.js.map +0 -1
  244. package/dist/lib/encrypt.js.map +0 -1
  245. package/dist/lib/generate.js.map +0 -1
  246. package/dist/lib/index.js.map +0 -1
  247. package/dist/lib/reformat.js.map +0 -1
  248. package/dist/lib/revoke.js.map +0 -1
  249. package/dist/lib/session.js.map +0 -1
  250. package/dist/lib/sign.js.map +0 -1
  251. package/dist/lib/verify.js.map +0 -1
  252. package/dist/package.json +0 -143
  253. package/dist/types/types.js.map +0 -1
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mlKemGenerateKeyPair = mlKemGenerateKeyPair;
4
+ exports.mlKemEncapsulate = mlKemEncapsulate;
5
+ exports.mlKemDecapsulate = mlKemDecapsulate;
6
+ exports.hybridGenerateKeyPair = hybridGenerateKeyPair;
7
+ exports.hybridEncapsulate = hybridEncapsulate;
8
+ exports.hybridDecapsulate = hybridDecapsulate;
9
+ const ml_kem_js_1 = require("@noble/post-quantum/ml-kem.js");
10
+ const ed25519_js_1 = require("@noble/curves/ed25519.js");
11
+ const hkdf_js_1 = require("@noble/hashes/hkdf.js");
12
+ const sha2_js_1 = require("@noble/hashes/sha2.js");
13
+ const utils_js_1 = require("@noble/ciphers/utils.js");
14
+ function mlKemGenerateKeyPair() {
15
+ const { publicKey, secretKey } = ml_kem_js_1.ml_kem768.keygen();
16
+ return {
17
+ publicKey: Buffer.from(publicKey).toString("hex"),
18
+ secretKey: Buffer.from(secretKey).toString("hex"),
19
+ algorithm: "ml-kem-768",
20
+ };
21
+ }
22
+ function mlKemEncapsulate(publicKeyHex) {
23
+ const publicKey = Buffer.from(publicKeyHex, "hex");
24
+ const { cipherText, sharedSecret } = ml_kem_js_1.ml_kem768.encapsulate(publicKey);
25
+ return {
26
+ ciphertext: Buffer.from(cipherText).toString("hex"),
27
+ sharedSecret: Buffer.from(sharedSecret).toString("hex"),
28
+ algorithm: "ml-kem-768",
29
+ };
30
+ }
31
+ function mlKemDecapsulate(secretKeyHex, ciphertextHex) {
32
+ const secretKey = Buffer.from(secretKeyHex, "hex");
33
+ const cipherText = Buffer.from(ciphertextHex, "hex");
34
+ const sharedSecret = ml_kem_js_1.ml_kem768.decapsulate(cipherText, secretKey);
35
+ return {
36
+ sharedSecret: Buffer.from(sharedSecret).toString("hex"),
37
+ algorithm: "ml-kem-768",
38
+ };
39
+ }
40
+ function hybridGenerateKeyPair() {
41
+ const x25519Priv = (0, utils_js_1.randomBytes)(32);
42
+ const x25519Pub = ed25519_js_1.x25519.getPublicKey(x25519Priv);
43
+ const { publicKey: mlKemPub, secretKey: mlKemSec } = ml_kem_js_1.ml_kem768.keygen();
44
+ return {
45
+ x25519PrivateKey: Buffer.from(x25519Priv).toString("hex"),
46
+ x25519PublicKey: Buffer.from(x25519Pub).toString("hex"),
47
+ mlKemPublicKey: Buffer.from(mlKemPub).toString("hex"),
48
+ mlKemSecretKey: Buffer.from(mlKemSec).toString("hex"),
49
+ algorithm: "x25519-ml-kem-768",
50
+ };
51
+ }
52
+ function hybridEncapsulate(theirX25519Public, theirMlKemPublic) {
53
+ const ephemeralPriv = (0, utils_js_1.randomBytes)(32);
54
+ const ephemeralPub = ed25519_js_1.x25519.getPublicKey(ephemeralPriv);
55
+ const x25519Shared = ed25519_js_1.x25519.getSharedSecret(ephemeralPriv, Buffer.from(theirX25519Public, "hex"));
56
+ const { cipherText, sharedSecret: mlKemShared } = ml_kem_js_1.ml_kem768.encapsulate(Buffer.from(theirMlKemPublic, "hex"));
57
+ const combined = new Uint8Array(x25519Shared.length + mlKemShared.length);
58
+ combined.set(x25519Shared);
59
+ combined.set(mlKemShared, x25519Shared.length);
60
+ const derivedSecret = (0, hkdf_js_1.hkdf)(sha2_js_1.sha256, combined, undefined, new TextEncoder().encode("x25519-ml-kem-768-hybrid"), 32);
61
+ return {
62
+ x25519EphemeralPublic: Buffer.from(ephemeralPub).toString("hex"),
63
+ mlKemCiphertext: Buffer.from(cipherText).toString("hex"),
64
+ sharedSecret: Buffer.from(derivedSecret).toString("hex"),
65
+ algorithm: "x25519-ml-kem-768",
66
+ };
67
+ }
68
+ function hybridDecapsulate(ourX25519Private, ourMlKemSecret, theirX25519Ephemeral, mlKemCiphertext) {
69
+ const x25519Shared = ed25519_js_1.x25519.getSharedSecret(Buffer.from(ourX25519Private, "hex"), Buffer.from(theirX25519Ephemeral, "hex"));
70
+ const mlKemShared = ml_kem_js_1.ml_kem768.decapsulate(Buffer.from(mlKemCiphertext, "hex"), Buffer.from(ourMlKemSecret, "hex"));
71
+ const combined = new Uint8Array(x25519Shared.length + mlKemShared.length);
72
+ combined.set(x25519Shared);
73
+ combined.set(mlKemShared, x25519Shared.length);
74
+ const derivedSecret = (0, hkdf_js_1.hkdf)(sha2_js_1.sha256, combined, undefined, new TextEncoder().encode("x25519-ml-kem-768-hybrid"), 32);
75
+ return {
76
+ sharedSecret: Buffer.from(derivedSecret).toString("hex"),
77
+ algorithm: "x25519-ml-kem-768",
78
+ };
79
+ }
80
+ //# sourceMappingURL=pq.js.map
@@ -0,0 +1,16 @@
1
+ export interface Ed25519KeyPair {
2
+ privateKey: string;
3
+ publicKey: string;
4
+ }
5
+ export interface SignResult {
6
+ signature: string;
7
+ algorithm: "ed25519";
8
+ }
9
+ export interface VerifyResult {
10
+ valid: boolean;
11
+ algorithm: "ed25519";
12
+ }
13
+ export declare function generateEd25519KeyPair(): Ed25519KeyPair;
14
+ export declare function ed25519Sign(privateKey: string | Uint8Array, message: string | Uint8Array): SignResult;
15
+ export declare function ed25519Verify(publicKey: string | Uint8Array, message: string | Uint8Array, signature: string | Uint8Array): VerifyResult;
16
+ //# sourceMappingURL=signing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signing.d.ts","sourceRoot":"","sources":["../../src/modern/signing.ts"],"names":[],"mappings":"AAmBA,MAAM,WAAW,cAAc;IAE7B,UAAU,EAAE,MAAM,CAAC;IAEnB,SAAS,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,UAAU;IAEzB,SAAS,EAAE,MAAM,CAAC;IAElB,SAAS,EAAE,SAAS,CAAC;CACtB;AAGD,MAAM,WAAW,YAAY;IAE3B,KAAK,EAAE,OAAO,CAAC;IAEf,SAAS,EAAE,SAAS,CAAC;CACtB;AAKD,wBAAgB,sBAAsB,IAAI,cAAc,CAQvD;AAKD,wBAAgB,WAAW,CACzB,UAAU,EAAE,MAAM,GAAG,UAAU,EAC/B,OAAO,EAAE,MAAM,GAAG,UAAU,GAC3B,UAAU,CAcZ;AAKD,wBAAgB,aAAa,CAC3B,SAAS,EAAE,MAAM,GAAG,UAAU,EAC9B,OAAO,EAAE,MAAM,GAAG,UAAU,EAC5B,SAAS,EAAE,MAAM,GAAG,UAAU,GAC7B,YAAY,CAWd"}
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateEd25519KeyPair = generateEd25519KeyPair;
4
+ exports.ed25519Sign = ed25519Sign;
5
+ exports.ed25519Verify = ed25519Verify;
6
+ const ed25519_js_1 = require("@noble/curves/ed25519.js");
7
+ const utils_js_1 = require("@noble/ciphers/utils.js");
8
+ function generateEd25519KeyPair() {
9
+ const privateKey = (0, utils_js_1.randomBytes)(32);
10
+ const publicKey = ed25519_js_1.ed25519.getPublicKey(privateKey);
11
+ return {
12
+ privateKey: Buffer.from(privateKey).toString("hex"),
13
+ publicKey: Buffer.from(publicKey).toString("hex"),
14
+ };
15
+ }
16
+ function ed25519Sign(privateKey, message) {
17
+ const key = typeof privateKey === "string"
18
+ ? Buffer.from(privateKey, "hex")
19
+ : privateKey;
20
+ const msg = typeof message === "string" ? Buffer.from(message, "utf8") : message;
21
+ const signature = ed25519_js_1.ed25519.sign(msg, key);
22
+ return {
23
+ signature: Buffer.from(signature).toString("hex"),
24
+ algorithm: "ed25519",
25
+ };
26
+ }
27
+ function ed25519Verify(publicKey, message, signature) {
28
+ const key = typeof publicKey === "string" ? Buffer.from(publicKey, "hex") : publicKey;
29
+ const msg = typeof message === "string" ? Buffer.from(message, "utf8") : message;
30
+ const sig = typeof signature === "string" ? Buffer.from(signature, "hex") : signature;
31
+ const valid = ed25519_js_1.ed25519.verify(sig, msg, key);
32
+ return { valid, algorithm: "ed25519" };
33
+ }
34
+ //# sourceMappingURL=signing.js.map
@@ -0,0 +1,7 @@
1
+ export * as pqxdh from "./pqxdh";
2
+ export * as ratchet from "./ratchet";
3
+ export * as pake from "./pake";
4
+ export * as threshold from "./threshold";
5
+ export * as iso20022 from "./iso20022";
6
+ export * from "./iso20022";
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/protocols/index.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,OAAO,MAAM,WAAW,CAAC;AACrC,OAAO,KAAK,IAAI,MAAM,QAAQ,CAAC;AAC/B,OAAO,KAAK,SAAS,MAAM,aAAa,CAAC;AACzC,OAAO,KAAK,QAAQ,MAAM,YAAY,CAAC;AACvC,cAAc,YAAY,CAAC"}
@@ -0,0 +1,46 @@
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
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
36
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.iso20022 = exports.threshold = exports.pake = exports.ratchet = exports.pqxdh = void 0;
40
+ exports.pqxdh = __importStar(require("./pqxdh"));
41
+ exports.ratchet = __importStar(require("./ratchet"));
42
+ exports.pake = __importStar(require("./pake"));
43
+ exports.threshold = __importStar(require("./threshold"));
44
+ exports.iso20022 = __importStar(require("./iso20022"));
45
+ __exportStar(require("./iso20022"), exports);
46
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,51 @@
1
+ import { type MlDsaLevel } from "../modern/pq-sign";
2
+ export type Iso20022MessageType = "pacs.008" | "pain.001" | "camt.053" | "generic";
3
+ export type Iso20022DigestAlgorithm = "sha256" | "sha384" | "sha512";
4
+ export interface Iso20022DualSignatureEnvelope {
5
+ messageId: string;
6
+ messageType: Iso20022MessageType;
7
+ payloadDigest: string;
8
+ digestAlgorithm: Iso20022DigestAlgorithm;
9
+ timestamp: string;
10
+ classical: {
11
+ algorithm: "ed25519";
12
+ signature: string;
13
+ publicKey: string;
14
+ };
15
+ postQuantum: {
16
+ algorithm: "ml-dsa-44" | "ml-dsa-65" | "ml-dsa-87";
17
+ signature: string;
18
+ publicKey: string;
19
+ };
20
+ }
21
+ export interface Iso20022SignOptions {
22
+ messageId: string;
23
+ messageType?: Iso20022MessageType;
24
+ payload: string | Record<string, unknown>;
25
+ classicalKey: {
26
+ privateKeyHex: string;
27
+ publicKeyHex: string;
28
+ };
29
+ postQuantumKey: {
30
+ secretKeyHex: string;
31
+ publicKeyHex: string;
32
+ level?: MlDsaLevel;
33
+ };
34
+ digestAlgorithm?: Iso20022DigestAlgorithm;
35
+ }
36
+ export interface Iso20022VerifyResult {
37
+ valid: boolean;
38
+ digestMatches: boolean;
39
+ classicalValid: boolean;
40
+ postQuantumValid: boolean;
41
+ messageId: string;
42
+ }
43
+ export interface Iso20022TrustedKeys {
44
+ classicalPublicKeyHex: string;
45
+ postQuantumPublicKeyHex: string;
46
+ }
47
+ export declare function canonicalizePayload(payload: string | Record<string, unknown>): string;
48
+ export declare function computeIso20022Digest(payload: string | Record<string, unknown>, algorithm?: Iso20022DigestAlgorithm): string;
49
+ export declare function signIso20022Payment(options: Iso20022SignOptions): Iso20022DualSignatureEnvelope;
50
+ export declare function verifyIso20022Payment(envelope: Iso20022DualSignatureEnvelope, payload: string | Record<string, unknown>, trustedKeys: Iso20022TrustedKeys): Iso20022VerifyResult;
51
+ //# sourceMappingURL=iso20022.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"iso20022.d.ts","sourceRoot":"","sources":["../../src/protocols/iso20022.ts"],"names":[],"mappings":"AAuBA,OAAO,EAA0B,KAAK,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAI5E,MAAM,MAAM,mBAAmB,GAC7B,UAAU,GAAG,UAAU,GAAG,UAAU,GAAG,SAAS,CAAC;AAGnD,MAAM,MAAM,uBAAuB,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAGrE,MAAM,WAAW,6BAA6B;IAE5C,SAAS,EAAE,MAAM,CAAC;IAElB,WAAW,EAAE,mBAAmB,CAAC;IAEjC,aAAa,EAAE,MAAM,CAAC;IAEtB,eAAe,EAAE,uBAAuB,CAAC;IAEzC,SAAS,EAAE,MAAM,CAAC;IAElB,SAAS,EAAE;QACT,SAAS,EAAE,SAAS,CAAC;QACrB,SAAS,EAAE,MAAM,CAAC;QAClB,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC;IAEF,WAAW,EAAE;QACX,SAAS,EAAE,WAAW,GAAG,WAAW,GAAG,WAAW,CAAC;QACnD,SAAS,EAAE,MAAM,CAAC;QAClB,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC;CACH;AAGD,MAAM,WAAW,mBAAmB;IAElC,SAAS,EAAE,MAAM,CAAC;IAElB,WAAW,CAAC,EAAE,mBAAmB,CAAC;IAElC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAE1C,YAAY,EAAE;QACZ,aAAa,EAAE,MAAM,CAAC;QACtB,YAAY,EAAE,MAAM,CAAC;KACtB,CAAC;IAEF,cAAc,EAAE;QACd,YAAY,EAAE,MAAM,CAAC;QACrB,YAAY,EAAE,MAAM,CAAC;QACrB,KAAK,CAAC,EAAE,UAAU,CAAC;KACpB,CAAC;IAEF,eAAe,CAAC,EAAE,uBAAuB,CAAC;CAC3C;AAGD,MAAM,WAAW,oBAAoB;IAEnC,KAAK,EAAE,OAAO,CAAC;IAEf,aAAa,EAAE,OAAO,CAAC;IAEvB,cAAc,EAAE,OAAO,CAAC;IAExB,gBAAgB,EAAE,OAAO,CAAC;IAE1B,SAAS,EAAE,MAAM,CAAC;CACnB;AASD,MAAM,WAAW,mBAAmB;IAElC,qBAAqB,EAAE,MAAM,CAAC;IAE9B,uBAAuB,EAAE,MAAM,CAAC;CACjC;AAyDD,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACxC,MAAM,CAKR;AAiBD,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACzC,SAAS,GAAE,uBAAkC,GAC5C,MAAM,CAGR;AAqBD,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,mBAAmB,GAC3B,6BAA6B,CA+C/B;AAqDD,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,6BAA6B,EACvC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACzC,WAAW,EAAE,mBAAmB,GAC/B,oBAAoB,CAsCtB"}
@@ -0,0 +1,145 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.canonicalizePayload = canonicalizePayload;
4
+ exports.computeIso20022Digest = computeIso20022Digest;
5
+ exports.signIso20022Payment = signIso20022Payment;
6
+ exports.verifyIso20022Payment = verifyIso20022Payment;
7
+ const node_crypto_1 = require("node:crypto");
8
+ const signing_1 = require("../modern/signing");
9
+ const pq_sign_1 = require("../modern/pq-sign");
10
+ const serialize_1 = require("../keys/serialize");
11
+ const STATEMENT_DOMAIN = "crypto-service/iso20022-dual-signature/v2";
12
+ const ML_DSA_LEVELS = {
13
+ "ml-dsa-44": 44,
14
+ "ml-dsa-65": 65,
15
+ "ml-dsa-87": 87,
16
+ };
17
+ function encodeStatement(fields) {
18
+ const parts = [
19
+ STATEMENT_DOMAIN,
20
+ fields.messageType,
21
+ fields.messageId,
22
+ fields.digestAlgorithm,
23
+ fields.payloadDigest,
24
+ fields.timestamp,
25
+ fields.classicalAlgorithm,
26
+ fields.classicalPublicKeyHex.toLowerCase(),
27
+ fields.postQuantumAlgorithm,
28
+ fields.postQuantumPublicKeyHex.toLowerCase(),
29
+ ];
30
+ const chunks = [];
31
+ for (const part of parts) {
32
+ const bytes = Buffer.from(part, "utf8");
33
+ const len = Buffer.alloc(4);
34
+ len.writeUInt32BE(bytes.length, 0);
35
+ chunks.push(len, bytes);
36
+ }
37
+ return Buffer.concat(chunks);
38
+ }
39
+ function canonicalizePayload(payload) {
40
+ if (typeof payload === "string") {
41
+ return payload.replace(/\r\n/g, "\n").trim();
42
+ }
43
+ return JSON.stringify(sortObjectKeys(payload));
44
+ }
45
+ function sortObjectKeys(obj) {
46
+ if (obj === null || typeof obj !== "object") {
47
+ return obj;
48
+ }
49
+ if (Array.isArray(obj)) {
50
+ return obj.map(sortObjectKeys);
51
+ }
52
+ const sorted = {};
53
+ for (const key of Object.keys(obj).sort()) {
54
+ sorted[key] = sortObjectKeys(obj[key]);
55
+ }
56
+ return sorted;
57
+ }
58
+ function computeIso20022Digest(payload, algorithm = "sha256") {
59
+ const canonical = canonicalizePayload(payload);
60
+ return (0, node_crypto_1.createHash)(algorithm).update(canonical, "utf8").digest("hex");
61
+ }
62
+ function signIso20022Payment(options) {
63
+ const digestAlgorithm = options.digestAlgorithm ?? "sha256";
64
+ const messageType = options.messageType ?? "generic";
65
+ const level = options.postQuantumKey.level ?? 65;
66
+ const payloadDigest = computeIso20022Digest(options.payload, digestAlgorithm);
67
+ const timestamp = new Date().toISOString();
68
+ const postQuantumAlgorithm = `ml-dsa-${level}`;
69
+ const statementBytes = encodeStatement({
70
+ messageType,
71
+ messageId: options.messageId,
72
+ digestAlgorithm,
73
+ payloadDigest,
74
+ timestamp,
75
+ classicalAlgorithm: "ed25519",
76
+ classicalPublicKeyHex: options.classicalKey.publicKeyHex,
77
+ postQuantumAlgorithm,
78
+ postQuantumPublicKeyHex: options.postQuantumKey.publicKeyHex,
79
+ });
80
+ const classicalSig = (0, signing_1.ed25519Sign)(options.classicalKey.privateKeyHex, (0, serialize_1.bytesToHex)(statementBytes));
81
+ const pqSig = (0, pq_sign_1.mlDsaSign)(level, options.postQuantumKey.secretKeyHex, statementBytes);
82
+ return {
83
+ messageId: options.messageId,
84
+ messageType,
85
+ payloadDigest,
86
+ digestAlgorithm,
87
+ timestamp,
88
+ classical: {
89
+ algorithm: "ed25519",
90
+ signature: classicalSig.signature,
91
+ publicKey: options.classicalKey.publicKeyHex,
92
+ },
93
+ postQuantum: {
94
+ algorithm: pqSig.algorithm,
95
+ signature: pqSig.signature,
96
+ publicKey: options.postQuantumKey.publicKeyHex,
97
+ },
98
+ };
99
+ }
100
+ function verifyClassical(publicKeyHex, statement, signatureHex) {
101
+ try {
102
+ return (0, signing_1.ed25519Verify)(publicKeyHex, (0, serialize_1.bytesToHex)(statement), signatureHex)
103
+ .valid;
104
+ }
105
+ catch {
106
+ return false;
107
+ }
108
+ }
109
+ function verifyPostQuantum(algorithm, publicKeyHex, statement, signatureHex) {
110
+ const level = ML_DSA_LEVELS[algorithm];
111
+ if (level === undefined) {
112
+ return false;
113
+ }
114
+ try {
115
+ return (0, pq_sign_1.mlDsaVerify)(level, publicKeyHex, statement, signatureHex).valid;
116
+ }
117
+ catch {
118
+ return false;
119
+ }
120
+ }
121
+ function verifyIso20022Payment(envelope, payload, trustedKeys) {
122
+ const computedDigest = computeIso20022Digest(payload, envelope.digestAlgorithm);
123
+ const digestMatches = computedDigest === envelope.payloadDigest;
124
+ const statement = encodeStatement({
125
+ messageType: envelope.messageType,
126
+ messageId: envelope.messageId,
127
+ digestAlgorithm: envelope.digestAlgorithm,
128
+ payloadDigest: envelope.payloadDigest,
129
+ timestamp: envelope.timestamp,
130
+ classicalAlgorithm: envelope.classical.algorithm,
131
+ classicalPublicKeyHex: trustedKeys.classicalPublicKeyHex,
132
+ postQuantumAlgorithm: envelope.postQuantum.algorithm,
133
+ postQuantumPublicKeyHex: trustedKeys.postQuantumPublicKeyHex,
134
+ });
135
+ const classicalValid = verifyClassical(trustedKeys.classicalPublicKeyHex, statement, envelope.classical.signature);
136
+ const postQuantumValid = verifyPostQuantum(envelope.postQuantum.algorithm, trustedKeys.postQuantumPublicKeyHex, statement, envelope.postQuantum.signature);
137
+ return {
138
+ valid: digestMatches && classicalValid && postQuantumValid,
139
+ digestMatches,
140
+ classicalValid,
141
+ postQuantumValid,
142
+ messageId: envelope.messageId,
143
+ };
144
+ }
145
+ //# sourceMappingURL=iso20022.js.map
@@ -0,0 +1,51 @@
1
+ import type { CleartextCredentials } from "./envelope";
2
+ import type { AuthResponse, CredentialRequest, CredentialResponse, KE1, KE2, KE3 } from "./messages";
3
+ import type { Suite } from "./suite";
4
+ export interface ClientAkeState {
5
+ clientSecret: Uint8Array;
6
+ ke1: KE1;
7
+ }
8
+ export interface ServerLoginState {
9
+ expectedClientMac: Uint8Array;
10
+ sessionKey: Uint8Array;
11
+ }
12
+ export interface DerivedKeys {
13
+ handshakeSecret: Uint8Array;
14
+ km2: Uint8Array;
15
+ km3: Uint8Array;
16
+ sessionKey: Uint8Array;
17
+ }
18
+ export declare function expandLabel(s: Suite, secret: Uint8Array, label: string, context: Uint8Array, length: number): Uint8Array;
19
+ export interface PreambleInput {
20
+ context: Uint8Array;
21
+ clientIdentity: Uint8Array;
22
+ ke1: KE1;
23
+ serverIdentity: Uint8Array;
24
+ credentialResponse: CredentialResponse;
25
+ serverNonce: Uint8Array;
26
+ serverPublicKeyshare: Uint8Array;
27
+ }
28
+ export declare function preamble(p: PreambleInput): Uint8Array;
29
+ export declare function deriveKeys(s: Suite, ikm: Uint8Array, transcript: Uint8Array): DerivedKeys;
30
+ export declare function authClientStart(s: Suite, credentialRequest: CredentialRequest, clientNonce: Uint8Array, clientKeyshareSeed: Uint8Array): ClientAkeState;
31
+ export interface AuthServerRespondInput {
32
+ context: Uint8Array;
33
+ cleartextCredentials: CleartextCredentials;
34
+ serverPrivateKey: Uint8Array;
35
+ clientPublicKey: Uint8Array;
36
+ ke1: KE1;
37
+ credentialResponse: CredentialResponse;
38
+ serverNonce: Uint8Array;
39
+ serverKeyshareSeed: Uint8Array;
40
+ }
41
+ export declare function authServerRespond(s: Suite, input: AuthServerRespondInput): {
42
+ authResponse: AuthResponse;
43
+ state: ServerLoginState;
44
+ keys: DerivedKeys;
45
+ };
46
+ export declare function authClientFinalize(s: Suite, context: Uint8Array, cc: CleartextCredentials, clientPrivateKey: Uint8Array, state: ClientAkeState, ke2: KE2): {
47
+ ke3: KE3;
48
+ sessionKey: Uint8Array;
49
+ };
50
+ export declare function authServerFinalize(state: ServerLoginState, ke3: KE3): Uint8Array;
51
+ //# sourceMappingURL=ake.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ake.d.ts","sourceRoot":"","sources":["../../../src/protocols/opaque/ake.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAEvD,OAAO,KAAK,EACV,YAAY,EACZ,iBAAiB,EACjB,kBAAkB,EAClB,GAAG,EACH,GAAG,EACH,GAAG,EACJ,MAAM,YAAY,CAAC;AAYpB,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAGrC,MAAM,WAAW,cAAc;IAE7B,YAAY,EAAE,UAAU,CAAC;IAEzB,GAAG,EAAE,GAAG,CAAC;CACV;AAGD,MAAM,WAAW,gBAAgB;IAE/B,iBAAiB,EAAE,UAAU,CAAC;IAE9B,UAAU,EAAE,UAAU,CAAC;CACxB;AAGD,MAAM,WAAW,WAAW;IAC1B,eAAe,EAAE,UAAU,CAAC;IAC5B,GAAG,EAAE,UAAU,CAAC;IAChB,GAAG,EAAE,UAAU,CAAC;IAChB,UAAU,EAAE,UAAU,CAAC;CACxB;AAGD,wBAAgB,WAAW,CACzB,CAAC,EAAE,KAAK,EACR,MAAM,EAAE,UAAU,EAClB,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,UAAU,EACnB,MAAM,EAAE,MAAM,GACb,UAAU,CAUZ;AAGD,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,UAAU,CAAC;IACpB,cAAc,EAAE,UAAU,CAAC;IAC3B,GAAG,EAAE,GAAG,CAAC;IACT,cAAc,EAAE,UAAU,CAAC;IAC3B,kBAAkB,EAAE,kBAAkB,CAAC;IACvC,WAAW,EAAE,UAAU,CAAC;IACxB,oBAAoB,EAAE,UAAU,CAAC;CAClC;AAGD,wBAAgB,QAAQ,CAAC,CAAC,EAAE,aAAa,GAAG,UAAU,CAcrD;AAGD,wBAAgB,UAAU,CACxB,CAAC,EAAE,KAAK,EACR,GAAG,EAAE,UAAU,EACf,UAAU,EAAE,UAAU,GACrB,WAAW,CAWb;AAUD,wBAAgB,eAAe,CAC7B,CAAC,EAAE,KAAK,EACR,iBAAiB,EAAE,iBAAiB,EACpC,WAAW,EAAE,UAAU,EACvB,kBAAkB,EAAE,UAAU,GAC7B,cAAc,CAUhB;AAGD,MAAM,WAAW,sBAAsB;IACrC,OAAO,EAAE,UAAU,CAAC;IACpB,oBAAoB,EAAE,oBAAoB,CAAC;IAC3C,gBAAgB,EAAE,UAAU,CAAC;IAC7B,eAAe,EAAE,UAAU,CAAC;IAC5B,GAAG,EAAE,GAAG,CAAC;IACT,kBAAkB,EAAE,kBAAkB,CAAC;IACvC,WAAW,EAAE,UAAU,CAAC;IACxB,kBAAkB,EAAE,UAAU,CAAC;CAChC;AAGD,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,sBAAsB;;;;EA8BxE;AAGD,wBAAgB,kBAAkB,CAChC,CAAC,EAAE,KAAK,EACR,OAAO,EAAE,UAAU,EACnB,EAAE,EAAE,oBAAoB,EACxB,gBAAgB,EAAE,UAAU,EAC5B,KAAK,EAAE,cAAc,EACrB,GAAG,EAAE,GAAG,GACP;IAAE,GAAG,EAAE,GAAG,CAAC;IAAC,UAAU,EAAE,UAAU,CAAA;CAAE,CAyBtC;AAGD,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,gBAAgB,EACvB,GAAG,EAAE,GAAG,GACP,UAAU,CAQZ"}
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.expandLabel = expandLabel;
4
+ exports.preamble = preamble;
5
+ exports.deriveKeys = deriveKeys;
6
+ exports.authClientStart = authClientStart;
7
+ exports.authServerRespond = authServerRespond;
8
+ exports.authClientFinalize = authClientFinalize;
9
+ exports.authServerFinalize = authServerFinalize;
10
+ const errors_1 = require("../../errors");
11
+ const utils_1 = require("../../utils");
12
+ const messages_1 = require("./messages");
13
+ const suite_1 = require("./suite");
14
+ function expandLabel(s, secret, label, context, length) {
15
+ const fullLabel = (0, suite_1.utf8)("OPAQUE-" + label);
16
+ const customLabel = (0, suite_1.concat)((0, suite_1.i2osp)(length, 2), (0, suite_1.i2osp)(fullLabel.length, 1), fullLabel, (0, suite_1.i2osp)(context.length, 1), context);
17
+ return (0, suite_1.kdfExpand)(s, secret, customLabel, length);
18
+ }
19
+ function preamble(p) {
20
+ return (0, suite_1.concat)((0, suite_1.utf8)("OPAQUEv1-"), (0, suite_1.i2osp)(p.context.length, 2), p.context, (0, suite_1.i2osp)(p.clientIdentity.length, 2), p.clientIdentity, (0, messages_1.serializeKE1)(p.ke1), (0, suite_1.i2osp)(p.serverIdentity.length, 2), p.serverIdentity, (0, messages_1.serializeCredentialResponse)(p.credentialResponse), p.serverNonce, p.serverPublicKeyshare);
21
+ }
22
+ function deriveKeys(s, ikm, transcript) {
23
+ const prk = (0, suite_1.kdfExtract)(s, new Uint8Array(0), ikm);
24
+ const th = s.hash(transcript);
25
+ const handshakeSecret = expandLabel(s, prk, "HandshakeSecret", th, s.Nh);
26
+ const none = new Uint8Array(0);
27
+ return {
28
+ handshakeSecret,
29
+ sessionKey: expandLabel(s, prk, "SessionKey", th, s.Nh),
30
+ km2: expandLabel(s, handshakeSecret, "ServerMAC", none, s.Nh),
31
+ km3: expandLabel(s, handshakeSecret, "ClientMAC", none, s.Nh),
32
+ };
33
+ }
34
+ function transcriptMacs(s, keys, transcript) {
35
+ const serverMac = (0, suite_1.mac)(s, keys.km2, s.hash(transcript));
36
+ const clientMac = (0, suite_1.mac)(s, keys.km3, s.hash((0, suite_1.concat)(transcript, serverMac)));
37
+ return { serverMac, clientMac };
38
+ }
39
+ function authClientStart(s, credentialRequest, clientNonce, clientKeyshareSeed) {
40
+ const kp = (0, suite_1.deriveDiffieHellmanKeyPair)(s, clientKeyshareSeed);
41
+ const ke1 = {
42
+ credentialRequest,
43
+ authRequest: {
44
+ clientNonce: clientNonce.slice(),
45
+ clientPublicKeyshare: kp.publicKey,
46
+ },
47
+ };
48
+ return { clientSecret: kp.privateKey, ke1 };
49
+ }
50
+ function authServerRespond(s, input) {
51
+ const kp = (0, suite_1.deriveDiffieHellmanKeyPair)(s, input.serverKeyshareSeed);
52
+ const cc = input.cleartextCredentials;
53
+ const transcript = preamble({
54
+ context: input.context,
55
+ clientIdentity: cc.clientIdentity,
56
+ ke1: input.ke1,
57
+ serverIdentity: cc.serverIdentity,
58
+ credentialResponse: input.credentialResponse,
59
+ serverNonce: input.serverNonce,
60
+ serverPublicKeyshare: kp.publicKey,
61
+ });
62
+ const clientKeyshare = input.ke1.authRequest.clientPublicKeyshare;
63
+ const ikm = (0, suite_1.concat)((0, suite_1.diffieHellman)(s, kp.privateKey, clientKeyshare), (0, suite_1.diffieHellman)(s, input.serverPrivateKey, clientKeyshare), (0, suite_1.diffieHellman)(s, kp.privateKey, input.clientPublicKey));
64
+ const keys = deriveKeys(s, ikm, transcript);
65
+ const { serverMac, clientMac } = transcriptMacs(s, keys, transcript);
66
+ const authResponse = {
67
+ serverNonce: input.serverNonce.slice(),
68
+ serverPublicKeyshare: kp.publicKey,
69
+ serverMac,
70
+ };
71
+ const state = {
72
+ expectedClientMac: clientMac,
73
+ sessionKey: keys.sessionKey,
74
+ };
75
+ return { authResponse, state, keys };
76
+ }
77
+ function authClientFinalize(s, context, cc, clientPrivateKey, state, ke2) {
78
+ const serverKeyshare = ke2.authResponse.serverPublicKeyshare;
79
+ const ikm = (0, suite_1.concat)((0, suite_1.diffieHellman)(s, state.clientSecret, serverKeyshare), (0, suite_1.diffieHellman)(s, state.clientSecret, cc.serverPublicKey), (0, suite_1.diffieHellman)(s, clientPrivateKey, serverKeyshare));
80
+ const transcript = preamble({
81
+ context,
82
+ clientIdentity: cc.clientIdentity,
83
+ ke1: state.ke1,
84
+ serverIdentity: cc.serverIdentity,
85
+ credentialResponse: ke2.credentialResponse,
86
+ serverNonce: ke2.authResponse.serverNonce,
87
+ serverPublicKeyshare: serverKeyshare,
88
+ });
89
+ const keys = deriveKeys(s, ikm, transcript);
90
+ const { serverMac, clientMac } = transcriptMacs(s, keys, transcript);
91
+ if (!(0, utils_1.timingSafeEqual)(ke2.authResponse.serverMac, serverMac)) {
92
+ throw new errors_1.CryptoError("OPAQUE server authentication failed: invalid server MAC", suite_1.OpaqueErrorCode.SERVER_AUTHENTICATION);
93
+ }
94
+ return { ke3: { clientMac }, sessionKey: keys.sessionKey };
95
+ }
96
+ function authServerFinalize(state, ke3) {
97
+ if (!(0, utils_1.timingSafeEqual)(ke3.clientMac, state.expectedClientMac)) {
98
+ throw new errors_1.CryptoError("OPAQUE client authentication failed: invalid client MAC", suite_1.OpaqueErrorCode.CLIENT_AUTHENTICATION);
99
+ }
100
+ return state.sessionKey.slice();
101
+ }
102
+ //# sourceMappingURL=ake.js.map
@@ -0,0 +1,45 @@
1
+ import type { OpaqueConfig } from "./config";
2
+ import type { Identities } from "./envelope";
3
+ import type { KE1, KE2, KE3, RegistrationRecord, RegistrationRequest, RegistrationResponse } from "./messages";
4
+ export interface ClientLoginState {
5
+ config: OpaqueConfig;
6
+ password: Uint8Array;
7
+ blind: Uint8Array;
8
+ clientSecret: Uint8Array;
9
+ ke1: KE1;
10
+ }
11
+ export interface KE1Randomness {
12
+ blind?: Uint8Array;
13
+ clientNonce?: Uint8Array;
14
+ clientKeyshareSeed?: Uint8Array;
15
+ }
16
+ export declare function createRegistrationRequestWith(password: Uint8Array | string, config: OpaqueConfig, blind?: Uint8Array): {
17
+ request: RegistrationRequest;
18
+ blind: Uint8Array;
19
+ };
20
+ export declare function createRegistrationRequest(password: Uint8Array | string, config?: OpaqueConfig): {
21
+ request: RegistrationRequest;
22
+ blind: Uint8Array;
23
+ };
24
+ export declare function finalizeRegistrationRequestWith(password: Uint8Array | string, blind: Uint8Array, response: RegistrationResponse, identities: Identities, config: OpaqueConfig, envelopeNonce?: Uint8Array): {
25
+ record: RegistrationRecord;
26
+ exportKey: Uint8Array;
27
+ };
28
+ export declare function finalizeRegistrationRequest(password: Uint8Array | string, blind: Uint8Array, response: RegistrationResponse, identities?: Identities, config?: OpaqueConfig): {
29
+ record: RegistrationRecord;
30
+ exportKey: Uint8Array;
31
+ };
32
+ export declare function generateKE1With(password: Uint8Array | string, config: OpaqueConfig, r: KE1Randomness): {
33
+ ke1: KE1;
34
+ state: ClientLoginState;
35
+ };
36
+ export declare function generateKE1(password: Uint8Array | string, config?: OpaqueConfig): {
37
+ ke1: KE1;
38
+ state: ClientLoginState;
39
+ };
40
+ export declare function generateKE3(state: ClientLoginState, ke2: KE2, identities?: Identities): {
41
+ ke3: KE3;
42
+ sessionKey: Uint8Array;
43
+ exportKey: Uint8Array;
44
+ };
45
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../src/protocols/opaque/client.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAG7C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAO7C,OAAO,KAAK,EACV,GAAG,EACH,GAAG,EACH,GAAG,EACH,kBAAkB,EAClB,mBAAmB,EACnB,oBAAoB,EACrB,MAAM,YAAY,CAAC;AAIpB,MAAM,WAAW,gBAAgB;IAE/B,MAAM,EAAE,YAAY,CAAC;IAErB,QAAQ,EAAE,UAAU,CAAC;IAErB,KAAK,EAAE,UAAU,CAAC;IAElB,YAAY,EAAE,UAAU,CAAC;IAEzB,GAAG,EAAE,GAAG,CAAC;CACV;AAGD,MAAM,WAAW,aAAa;IAC5B,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB,WAAW,CAAC,EAAE,UAAU,CAAC;IACzB,kBAAkB,CAAC,EAAE,UAAU,CAAC;CACjC;AAGD,wBAAgB,6BAA6B,CAC3C,QAAQ,EAAE,UAAU,GAAG,MAAM,EAC7B,MAAM,EAAE,YAAY,EACpB,KAAK,CAAC,EAAE,UAAU,GACjB;IAAE,OAAO,EAAE,mBAAmB,CAAC;IAAC,KAAK,EAAE,UAAU,CAAA;CAAE,CAIrD;AAUD,wBAAgB,yBAAyB,CACvC,QAAQ,EAAE,UAAU,GAAG,MAAM,EAC7B,MAAM,GAAE,YAAiB,GACxB;IAAE,OAAO,EAAE,mBAAmB,CAAC;IAAC,KAAK,EAAE,UAAU,CAAA;CAAE,CAErD;AAGD,wBAAgB,+BAA+B,CAC7C,QAAQ,EAAE,UAAU,GAAG,MAAM,EAC7B,KAAK,EAAE,UAAU,EACjB,QAAQ,EAAE,oBAAoB,EAC9B,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,YAAY,EACpB,aAAa,GAAE,UAA4B,GAC1C;IAAE,MAAM,EAAE,kBAAkB,CAAC;IAAC,SAAS,EAAE,UAAU,CAAA;CAAE,CAyBvD;AAeD,wBAAgB,2BAA2B,CACzC,QAAQ,EAAE,UAAU,GAAG,MAAM,EAC7B,KAAK,EAAE,UAAU,EACjB,QAAQ,EAAE,oBAAoB,EAC9B,UAAU,GAAE,UAAe,EAC3B,MAAM,GAAE,YAAiB,GACxB;IAAE,MAAM,EAAE,kBAAkB,CAAC;IAAC,SAAS,EAAE,UAAU,CAAA;CAAE,CAQvD;AAGD,wBAAgB,eAAe,CAC7B,QAAQ,EAAE,UAAU,GAAG,MAAM,EAC7B,MAAM,EAAE,YAAY,EACpB,CAAC,EAAE,aAAa,GACf;IAAE,GAAG,EAAE,GAAG,CAAC;IAAC,KAAK,EAAE,gBAAgB,CAAA;CAAE,CAkBvC;AASD,wBAAgB,WAAW,CACzB,QAAQ,EAAE,UAAU,GAAG,MAAM,EAC7B,MAAM,GAAE,YAAiB,GACxB;IAAE,GAAG,EAAE,GAAG,CAAC;IAAC,KAAK,EAAE,gBAAgB,CAAA;CAAE,CAEvC;AAgBD,wBAAgB,WAAW,CACzB,KAAK,EAAE,gBAAgB,EACvB,GAAG,EAAE,GAAG,EACR,UAAU,GAAE,UAAe,GAC1B;IAAE,GAAG,EAAE,GAAG,CAAC;IAAC,UAAU,EAAE,UAAU,CAAC;IAAC,SAAS,EAAE,UAAU,CAAA;CAAE,CAoB7D"}