@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.
- package/README.md +17 -2
- package/dist/accel/index.d.ts +5 -4
- package/dist/accel/index.d.ts.map +1 -1
- package/dist/accel/index.js +5 -5
- package/dist/accel/kdf-backend.d.ts +13 -0
- package/dist/accel/kdf-backend.d.ts.map +1 -0
- package/dist/accel/kdf-backend.js +81 -0
- package/dist/accel/native-pqc-bridge.d.ts.map +1 -1
- package/dist/accel/native-pqc-bridge.js +12 -52
- package/dist/accel/pq-backend.d.ts +3652 -0
- package/dist/accel/pq-backend.d.ts.map +1 -0
- package/dist/accel/pq-backend.js +215 -0
- package/dist/accel/runtime.d.ts +3 -0
- package/dist/accel/runtime.d.ts.map +1 -0
- package/dist/accel/runtime.js +9 -0
- package/dist/accel/wasm-bridge.d.ts.map +1 -1
- package/dist/accel/wasm-bridge.js +14 -13
- package/dist/accel/webcrypto-bridge.d.ts.map +1 -1
- package/dist/accel/webcrypto-bridge.js +14 -13
- package/dist/accel/webcrypto-modern.d.ts.map +1 -1
- package/dist/accel/webcrypto-modern.js +15 -14
- package/dist/accel/worker-pool.d.ts +18 -0
- package/dist/accel/worker-pool.d.ts.map +1 -1
- package/dist/accel/worker-pool.js +113 -37
- package/dist/bin/cryptolib.d.ts +7 -4
- package/dist/bin/cryptolib.d.ts.map +1 -1
- package/dist/bin/cryptolib.js +6 -6
- package/dist/crypto.d.ts.map +1 -1
- package/dist/crypto.js +2 -1
- package/dist/encoding.d.ts +10 -0
- package/dist/encoding.d.ts.map +1 -0
- package/dist/encoding.js +84 -0
- package/dist/errors.d.ts +11 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +20 -1
- package/dist/high-level/key-wrap.d.ts.map +1 -1
- package/dist/high-level/key-wrap.js +12 -17
- package/dist/high-level/multi-recipient.d.ts.map +1 -1
- package/dist/high-level/multi-recipient.js +15 -28
- package/dist/high-level/password-encrypt.d.ts.map +1 -1
- package/dist/high-level/password-encrypt.js +11 -11
- package/dist/high-level/sealedbox.d.ts.map +1 -1
- package/dist/high-level/sealedbox.js +16 -28
- package/dist/high-level/secretbox.d.ts.map +1 -1
- package/dist/high-level/secretbox.js +11 -10
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/key/keystore.d.ts.map +1 -1
- package/dist/key/keystore.js +2 -1
- package/dist/keys/keygen.d.ts.map +1 -1
- package/dist/keys/keygen.js +13 -14
- package/dist/keys/keyring.d.ts.map +1 -1
- package/dist/keys/keyring.js +4 -2
- package/dist/keys/serialize.d.ts.map +1 -1
- package/dist/keys/serialize.js +18 -16
- package/dist/lib/decrypt.d.ts.map +1 -1
- package/dist/lib/decrypt.js +2 -1
- package/dist/lib/generate.d.ts +2 -1
- package/dist/lib/generate.d.ts.map +1 -1
- package/dist/lib/generate.js +7 -6
- package/dist/lib/key-output.d.ts +4 -1
- package/dist/lib/key-output.d.ts.map +1 -1
- package/dist/lib/key-output.js +2 -2
- package/dist/lib/reformat.d.ts +2 -1
- package/dist/lib/reformat.d.ts.map +1 -1
- package/dist/lib/reformat.js +2 -2
- package/dist/lib/revoke.d.ts +2 -1
- package/dist/lib/revoke.d.ts.map +1 -1
- package/dist/lib/revoke.js +2 -2
- package/dist/lib/verify.d.ts +6 -2
- package/dist/lib/verify.d.ts.map +1 -1
- package/dist/lib/verify.js +28 -9
- package/dist/modern/aead.d.ts.map +1 -1
- package/dist/modern/aead.js +11 -10
- package/dist/modern/aes.d.ts.map +1 -1
- package/dist/modern/aes.js +10 -9
- package/dist/modern/cost-limits.d.ts.map +1 -1
- package/dist/modern/cost-limits.js +3 -2
- package/dist/modern/curves.d.ts +10 -5
- package/dist/modern/curves.d.ts.map +1 -1
- package/dist/modern/curves.js +43 -36
- package/dist/modern/ecdh.d.ts +2 -1
- package/dist/modern/ecdh.d.ts.map +1 -1
- package/dist/modern/ecdh.js +10 -9
- package/dist/modern/fn-dsa.d.ts.map +1 -1
- package/dist/modern/fn-dsa.js +9 -8
- package/dist/modern/hash.d.ts.map +1 -1
- package/dist/modern/hash.js +5 -3
- package/dist/modern/hpke-xwing.d.ts +10 -0
- package/dist/modern/hpke-xwing.d.ts.map +1 -0
- package/dist/modern/hpke-xwing.js +26 -0
- package/dist/modern/hpke.d.ts +1 -1
- package/dist/modern/hpke.d.ts.map +1 -1
- package/dist/modern/hpke.js +60 -28
- package/dist/modern/hybrid-combiner.d.ts +20 -0
- package/dist/modern/hybrid-combiner.d.ts.map +1 -0
- package/dist/modern/hybrid-combiner.js +79 -0
- package/dist/modern/index.d.ts +10 -10
- package/dist/modern/index.d.ts.map +1 -1
- package/dist/modern/index.js +17 -7
- package/dist/modern/kdf.d.ts.map +1 -1
- package/dist/modern/kdf.js +12 -12
- package/dist/modern/mac.d.ts +1 -0
- package/dist/modern/mac.d.ts.map +1 -1
- package/dist/modern/mac.js +24 -15
- package/dist/modern/password.d.ts.map +1 -1
- package/dist/modern/password.js +19 -18
- package/dist/modern/pq-hash-sign.d.ts.map +1 -1
- package/dist/modern/pq-hash-sign.js +23 -22
- package/dist/modern/pq-kem.d.ts +8 -0
- package/dist/modern/pq-kem.d.ts.map +1 -1
- package/dist/modern/pq-kem.js +71 -79
- package/dist/modern/pq-sign.d.ts +9 -2
- package/dist/modern/pq-sign.d.ts.map +1 -1
- package/dist/modern/pq-sign.js +60 -23
- package/dist/modern/pq.d.ts +0 -17
- package/dist/modern/pq.d.ts.map +1 -1
- package/dist/modern/pq.js +15 -43
- package/dist/modern/signing.d.ts +2 -1
- package/dist/modern/signing.d.ts.map +1 -1
- package/dist/modern/signing.js +14 -13
- package/dist/protocols/iso20022.d.ts.map +1 -1
- package/dist/protocols/iso20022.js +12 -6
- package/dist/protocols/opaque/ksf.js +2 -2
- package/dist/protocols/opaque/suite.d.ts +1 -8
- package/dist/protocols/opaque/suite.d.ts.map +1 -1
- package/dist/protocols/opaque/suite.js +5 -10
- package/dist/protocols/pqxdh.d.ts +9 -2
- package/dist/protocols/pqxdh.d.ts.map +1 -1
- package/dist/protocols/pqxdh.js +99 -94
- package/dist/protocols/ratchet.d.ts.map +1 -1
- package/dist/protocols/ratchet.js +14 -15
- package/dist/protocols/threshold.d.ts.map +1 -1
- package/dist/protocols/threshold.js +16 -15
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +26 -0
- package/dist/streaming/chunks.d.ts +31 -0
- package/dist/streaming/chunks.d.ts.map +1 -1
- package/dist/streaming/chunks.js +149 -13
- package/dist/streaming/stream-aead.d.ts +9 -0
- package/dist/streaming/stream-aead.d.ts.map +1 -1
- package/dist/streaming/stream-aead.js +27 -53
- package/dist/streaming/stream-hash.d.ts.map +1 -1
- package/dist/streaming/stream-hash.js +6 -4
- package/dist/streaming/stream-multi-pq-aead.d.ts +12 -0
- package/dist/streaming/stream-multi-pq-aead.d.ts.map +1 -1
- package/dist/streaming/stream-multi-pq-aead.js +82 -106
- package/dist/streaming/stream-pq-aead.d.ts +7 -2
- package/dist/streaming/stream-pq-aead.d.ts.map +1 -1
- package/dist/streaming/stream-pq-aead.js +29 -79
- package/dist/streaming/web-streams.d.ts +5 -2
- package/dist/streaming/web-streams.d.ts.map +1 -1
- package/dist/streaming/web-streams.js +90 -151
- package/dist/testing/index.d.ts +5 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +13 -0
- package/dist/tokens/index.d.ts.map +1 -1
- package/dist/tokens/key-rotation.d.ts.map +1 -1
- package/dist/tokens/key-rotation.js +4 -2
- package/dist/tokens/paseto.d.ts +2 -0
- package/dist/tokens/paseto.d.ts.map +1 -1
- package/dist/tokens/paseto.js +56 -29
- package/dist/types/types.d.ts +1 -0
- package/dist/types/types.d.ts.map +1 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +3 -2
- package/package.json +12 -2
- package/dist/config/config.d.ts +0 -8
- package/dist/config/config.d.ts.map +0 -1
- package/dist/config/config.js +0 -13
- package/dist/key/key.d.ts +0 -10
- package/dist/key/key.d.ts.map +0 -1
- package/dist/key/key.js +0 -25
package/dist/modern/signing.js
CHANGED
|
@@ -1,34 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generateEd25519KeyPair =
|
|
3
|
+
exports.generateEd25519KeyPair = void 0;
|
|
4
|
+
exports.ed25519Keygen = ed25519Keygen;
|
|
4
5
|
exports.ed25519Sign = ed25519Sign;
|
|
5
6
|
exports.ed25519Verify = ed25519Verify;
|
|
6
7
|
const ed25519_js_1 = require("@noble/curves/ed25519.js");
|
|
7
8
|
const utils_js_1 = require("@noble/ciphers/utils.js");
|
|
8
|
-
|
|
9
|
+
const encoding_1 = require("../encoding");
|
|
10
|
+
function ed25519Keygen() {
|
|
9
11
|
const privateKey = (0, utils_js_1.randomBytes)(32);
|
|
10
12
|
const publicKey = ed25519_js_1.ed25519.getPublicKey(privateKey);
|
|
11
13
|
return {
|
|
12
|
-
privateKey:
|
|
13
|
-
publicKey:
|
|
14
|
+
privateKey: (0, encoding_1.toHex)(privateKey),
|
|
15
|
+
publicKey: (0, encoding_1.toHex)(publicKey),
|
|
14
16
|
};
|
|
15
17
|
}
|
|
18
|
+
exports.generateEd25519KeyPair = ed25519Keygen;
|
|
16
19
|
function ed25519Sign(privateKey, message) {
|
|
17
|
-
const key = typeof privateKey === "string"
|
|
18
|
-
|
|
19
|
-
: privateKey;
|
|
20
|
-
const msg = typeof message === "string" ? Buffer.from(message, "utf8") : message;
|
|
20
|
+
const key = typeof privateKey === "string" ? (0, encoding_1.decodeHex)(privateKey) : privateKey;
|
|
21
|
+
const msg = typeof message === "string" ? (0, encoding_1.utf8)(message) : message;
|
|
21
22
|
const signature = ed25519_js_1.ed25519.sign(msg, key);
|
|
22
23
|
return {
|
|
23
|
-
signature:
|
|
24
|
+
signature: (0, encoding_1.toHex)(signature),
|
|
24
25
|
algorithm: "ed25519",
|
|
25
26
|
};
|
|
26
27
|
}
|
|
27
28
|
function ed25519Verify(publicKey, message, signature) {
|
|
28
|
-
const key = typeof publicKey === "string" ?
|
|
29
|
-
const msg = typeof message === "string" ?
|
|
30
|
-
const sig = typeof signature === "string" ?
|
|
31
|
-
const valid = ed25519_js_1.ed25519.verify(sig, msg, key);
|
|
29
|
+
const key = typeof publicKey === "string" ? (0, encoding_1.decodeHex)(publicKey) : publicKey;
|
|
30
|
+
const msg = typeof message === "string" ? (0, encoding_1.utf8)(message) : message;
|
|
31
|
+
const sig = typeof signature === "string" ? (0, encoding_1.decodeHex)(signature) : signature;
|
|
32
|
+
const valid = ed25519_js_1.ed25519.verify(sig, msg, key, { zip215: false });
|
|
32
33
|
return { valid, algorithm: "ed25519" };
|
|
33
34
|
}
|
|
34
35
|
//# sourceMappingURL=signing.js.map
|
|
@@ -1 +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;
|
|
1
|
+
{"version":3,"file":"iso20022.d.ts","sourceRoot":"","sources":["../../src/protocols/iso20022.ts"],"names":[],"mappings":"AAuBA,OAAO,EAA0B,KAAK,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAM5E,MAAM,MAAM,mBAAmB,GAC7B,UAAU,GAAG,UAAU,GAAG,UAAU,GAAG,SAAS,CAAC;AAGnD,MAAM,MAAM,uBAAuB,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAC;AASrE,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;QACr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,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"}
|
|
@@ -4,10 +4,13 @@ exports.canonicalizePayload = canonicalizePayload;
|
|
|
4
4
|
exports.computeIso20022Digest = computeIso20022Digest;
|
|
5
5
|
exports.signIso20022Payment = signIso20022Payment;
|
|
6
6
|
exports.verifyIso20022Payment = verifyIso20022Payment;
|
|
7
|
-
const
|
|
7
|
+
const sha2_js_1 = require("@noble/hashes/sha2.js");
|
|
8
8
|
const signing_1 = require("../modern/signing");
|
|
9
9
|
const pq_sign_1 = require("../modern/pq-sign");
|
|
10
10
|
const serialize_1 = require("../keys/serialize");
|
|
11
|
+
const encoding_1 = require("../encoding");
|
|
12
|
+
const errors_1 = require("../errors");
|
|
13
|
+
const DIGESTS = { sha256: sha2_js_1.sha256, sha384: sha2_js_1.sha384, sha512: sha2_js_1.sha512 };
|
|
11
14
|
const STATEMENT_DOMAIN = "crypto-service/iso20022-dual-signature/v2";
|
|
12
15
|
const ML_DSA_LEVELS = {
|
|
13
16
|
"ml-dsa-44": 44,
|
|
@@ -29,12 +32,12 @@ function encodeStatement(fields) {
|
|
|
29
32
|
];
|
|
30
33
|
const chunks = [];
|
|
31
34
|
for (const part of parts) {
|
|
32
|
-
const bytes =
|
|
33
|
-
const len =
|
|
34
|
-
len.
|
|
35
|
+
const bytes = (0, encoding_1.utf8)(part);
|
|
36
|
+
const len = new Uint8Array(4);
|
|
37
|
+
new DataView(len.buffer).setUint32(0, bytes.length);
|
|
35
38
|
chunks.push(len, bytes);
|
|
36
39
|
}
|
|
37
|
-
return
|
|
40
|
+
return (0, encoding_1.concat)(chunks);
|
|
38
41
|
}
|
|
39
42
|
function canonicalizePayload(payload) {
|
|
40
43
|
if (typeof payload === "string") {
|
|
@@ -56,8 +59,11 @@ function sortObjectKeys(obj) {
|
|
|
56
59
|
return sorted;
|
|
57
60
|
}
|
|
58
61
|
function computeIso20022Digest(payload, algorithm = "sha256") {
|
|
62
|
+
if (!Object.hasOwn(DIGESTS, algorithm)) {
|
|
63
|
+
throw new errors_1.CryptoError(`Unsupported digest algorithm: ${algorithm}`, errors_1.CryptoErrorCode.UNSUPPORTED_ALGORITHM);
|
|
64
|
+
}
|
|
59
65
|
const canonical = canonicalizePayload(payload);
|
|
60
|
-
return (0,
|
|
66
|
+
return (0, encoding_1.toHex)(DIGESTS[algorithm]((0, encoding_1.utf8)(canonical)));
|
|
61
67
|
}
|
|
62
68
|
function signIso20022Payment(options) {
|
|
63
69
|
const digestAlgorithm = options.digestAlgorithm ?? "sha256";
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DEFAULT_KSF = exports.identityKsf = exports.RFC9807_SCRYPT_PARAMS = void 0;
|
|
4
4
|
exports.scryptKsf = scryptKsf;
|
|
5
|
-
const
|
|
5
|
+
const kdf_backend_1 = require("../../accel/kdf-backend");
|
|
6
6
|
const cost_limits_1 = require("../../modern/cost-limits");
|
|
7
7
|
exports.RFC9807_SCRYPT_PARAMS = Object.freeze({
|
|
8
8
|
N: 32768,
|
|
@@ -16,7 +16,7 @@ function scryptKsf(params = {}) {
|
|
|
16
16
|
const salt = new Uint8Array(16);
|
|
17
17
|
return Object.freeze({
|
|
18
18
|
name: `scrypt(N=${N},r=${r},p=${p},dkLen=${dkLen})`,
|
|
19
|
-
stretch: (msg) => (0,
|
|
19
|
+
stretch: (msg) => (0, kdf_backend_1.scrypt)(msg, salt, { N, r, p, dkLen }),
|
|
20
20
|
});
|
|
21
21
|
}
|
|
22
22
|
exports.identityKsf = Object.freeze({
|
|
@@ -2,13 +2,7 @@ import { p256_oprf } from "@noble/curves/nist.js";
|
|
|
2
2
|
import { concatBytes } from "@noble/hashes/utils.js";
|
|
3
3
|
import type { CHash } from "@noble/hashes/utils.js";
|
|
4
4
|
export type SuiteId = "P256-SHA256" | "ristretto255-SHA512";
|
|
5
|
-
export
|
|
6
|
-
readonly ENVELOPE_RECOVERY: "OPAQUE_ENVELOPE_RECOVERY";
|
|
7
|
-
readonly SERVER_AUTHENTICATION: "OPAQUE_SERVER_AUTHENTICATION";
|
|
8
|
-
readonly CLIENT_AUTHENTICATION: "OPAQUE_CLIENT_AUTHENTICATION";
|
|
9
|
-
readonly DESERIALIZE: "OPAQUE_DESERIALIZE";
|
|
10
|
-
readonly INVALID_INPUT: "OPAQUE_INVALID_INPUT";
|
|
11
|
-
};
|
|
5
|
+
export { OpaqueErrorCode } from "../../errors";
|
|
12
6
|
export declare const Nn = 32;
|
|
13
7
|
export declare const Nseed = 32;
|
|
14
8
|
interface Element {
|
|
@@ -54,5 +48,4 @@ export declare function deriveDiffieHellmanKeyPair(s: Suite, seed: Uint8Array):
|
|
|
54
48
|
};
|
|
55
49
|
export declare function diffieHellman(s: Suite, privateKey: Uint8Array, publicKey: Uint8Array): Uint8Array;
|
|
56
50
|
export declare function blindWith(s: Suite, input: Uint8Array, blind: Uint8Array): Uint8Array;
|
|
57
|
-
export {};
|
|
58
51
|
//# sourceMappingURL=suite.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"suite.d.ts","sourceRoot":"","sources":["../../../src/protocols/opaque/suite.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAqB,SAAS,EAAE,MAAM,uBAAuB,CAAC;AASrE,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAC;AAIpD,MAAM,MAAM,OAAO,GAAG,aAAa,GAAG,qBAAqB,CAAC;AAG5D,
|
|
1
|
+
{"version":3,"file":"suite.d.ts","sourceRoot":"","sources":["../../../src/protocols/opaque/suite.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAqB,SAAS,EAA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,UAAU,CAKZ"}
|
|
@@ -20,13 +20,8 @@ const hkdf_js_1 = require("@noble/hashes/hkdf.js");
|
|
|
20
20
|
const hmac_js_1 = require("@noble/hashes/hmac.js");
|
|
21
21
|
const utils_js_1 = require("@noble/hashes/utils.js");
|
|
22
22
|
const errors_1 = require("../../errors");
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
SERVER_AUTHENTICATION: "OPAQUE_SERVER_AUTHENTICATION",
|
|
26
|
-
CLIENT_AUTHENTICATION: "OPAQUE_CLIENT_AUTHENTICATION",
|
|
27
|
-
DESERIALIZE: "OPAQUE_DESERIALIZE",
|
|
28
|
-
INVALID_INPUT: "OPAQUE_INVALID_INPUT",
|
|
29
|
-
};
|
|
23
|
+
var errors_2 = require("../../errors");
|
|
24
|
+
Object.defineProperty(exports, "OpaqueErrorCode", { enumerable: true, get: function () { return errors_2.OpaqueErrorCode; } });
|
|
30
25
|
exports.Nn = 32;
|
|
31
26
|
exports.Nseed = 32;
|
|
32
27
|
function utf8(s) {
|
|
@@ -71,7 +66,7 @@ const SUITES = {
|
|
|
71
66
|
function getSuite(id) {
|
|
72
67
|
const suite = Object.hasOwn(SUITES, id) ? SUITES[id] : undefined;
|
|
73
68
|
if (suite === undefined) {
|
|
74
|
-
throw new errors_1.CryptoError(`Unsupported OPAQUE suite: ${String(id)}`,
|
|
69
|
+
throw new errors_1.CryptoError(`Unsupported OPAQUE suite: ${String(id)}`, errors_1.OpaqueErrorCode.INVALID_INPUT);
|
|
75
70
|
}
|
|
76
71
|
return suite;
|
|
77
72
|
}
|
|
@@ -90,10 +85,10 @@ function decodeElement(s, bytes, label) {
|
|
|
90
85
|
element = s.group.fromBytes(bytes);
|
|
91
86
|
}
|
|
92
87
|
catch {
|
|
93
|
-
throw new errors_1.CryptoError(`Invalid ${label}`,
|
|
88
|
+
throw new errors_1.CryptoError(`Invalid ${label}`, errors_1.OpaqueErrorCode.DESERIALIZE);
|
|
94
89
|
}
|
|
95
90
|
if (element.equals(s.group.ZERO)) {
|
|
96
|
-
throw new errors_1.CryptoError(`Invalid ${label}: identity element`,
|
|
91
|
+
throw new errors_1.CryptoError(`Invalid ${label}: identity element`, errors_1.OpaqueErrorCode.DESERIALIZE);
|
|
97
92
|
}
|
|
98
93
|
return element;
|
|
99
94
|
}
|
|
@@ -21,12 +21,17 @@ export interface PqPreKey {
|
|
|
21
21
|
export interface InitiateSessionParams {
|
|
22
22
|
identityKeyPair: IdentityKeyPair;
|
|
23
23
|
remoteIdentityPublic: string;
|
|
24
|
+
remoteIdentitySigningPublic: string;
|
|
24
25
|
remoteSignedPreKeyPublic: string;
|
|
26
|
+
remoteSignedPreKeySignature: string;
|
|
25
27
|
remoteOneTimePreKeyPublic?: string;
|
|
26
28
|
remotePqPreKeyPublic: string;
|
|
29
|
+
remotePqPreKeySignature: string;
|
|
30
|
+
info?: string;
|
|
27
31
|
}
|
|
28
32
|
export interface InitiateSessionResult {
|
|
29
33
|
sharedSecret: string;
|
|
34
|
+
associatedData: string;
|
|
30
35
|
ephemeralPublicKey: string;
|
|
31
36
|
pqCiphertext: string;
|
|
32
37
|
algorithm: "pqxdh";
|
|
@@ -39,15 +44,17 @@ export interface RespondToSessionParams {
|
|
|
39
44
|
remoteIdentityPublic: string;
|
|
40
45
|
remoteEphemeralPublic: string;
|
|
41
46
|
pqCiphertext: string;
|
|
47
|
+
info?: string;
|
|
42
48
|
}
|
|
43
49
|
export interface RespondToSessionResult {
|
|
44
50
|
sharedSecret: string;
|
|
51
|
+
associatedData: string;
|
|
45
52
|
algorithm: "pqxdh";
|
|
46
53
|
}
|
|
47
54
|
export declare function generateIdentityKeyPair(): IdentityKeyPair;
|
|
48
|
-
export declare function generateSignedPreKey(
|
|
55
|
+
export declare function generateSignedPreKey(identity: IdentityKeyPair): SignedPreKey;
|
|
49
56
|
export declare function generateOneTimePreKey(): OneTimePreKey;
|
|
50
|
-
export declare function generatePqPreKey(
|
|
57
|
+
export declare function generatePqPreKey(identity: IdentityKeyPair): PqPreKey;
|
|
51
58
|
export declare function initiateSession(params: InitiateSessionParams): InitiateSessionResult;
|
|
52
59
|
export declare function respondToSession(params: RespondToSessionParams): RespondToSessionResult;
|
|
53
60
|
//# sourceMappingURL=pqxdh.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pqxdh.d.ts","sourceRoot":"","sources":["../../src/protocols/pqxdh.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"pqxdh.d.ts","sourceRoot":"","sources":["../../src/protocols/pqxdh.ts"],"names":[],"mappings":"AAyCA,MAAM,WAAW,eAAe;IAE9B,UAAU,EAAE,MAAM,CAAC;IAEnB,SAAS,EAAE,MAAM,CAAC;IAElB,iBAAiB,EAAE,MAAM,CAAC;IAE1B,gBAAgB,EAAE,MAAM,CAAC;CAC1B;AAGD,MAAM,WAAW,YAAY;IAE3B,UAAU,EAAE,MAAM,CAAC;IAEnB,SAAS,EAAE,MAAM,CAAC;IAElB,SAAS,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,aAAa;IAE5B,UAAU,EAAE,MAAM,CAAC;IAEnB,SAAS,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,QAAQ;IAEvB,SAAS,EAAE,MAAM,CAAC;IAElB,SAAS,EAAE,MAAM,CAAC;IAElB,SAAS,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,qBAAqB;IAEpC,eAAe,EAAE,eAAe,CAAC;IAEjC,oBAAoB,EAAE,MAAM,CAAC;IAE7B,2BAA2B,EAAE,MAAM,CAAC;IAEpC,wBAAwB,EAAE,MAAM,CAAC;IAEjC,2BAA2B,EAAE,MAAM,CAAC;IAEpC,yBAAyB,CAAC,EAAE,MAAM,CAAC;IAEnC,oBAAoB,EAAE,MAAM,CAAC;IAE7B,uBAAuB,EAAE,MAAM,CAAC;IAEhC,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAGD,MAAM,WAAW,qBAAqB;IAEpC,YAAY,EAAE,MAAM,CAAC;IAErB,cAAc,EAAE,MAAM,CAAC;IAEvB,kBAAkB,EAAE,MAAM,CAAC;IAE3B,YAAY,EAAE,MAAM,CAAC;IAErB,SAAS,EAAE,OAAO,CAAC;CACpB;AAGD,MAAM,WAAW,sBAAsB;IAErC,eAAe,EAAE,eAAe,CAAC;IAEjC,mBAAmB,EAAE,MAAM,CAAC;IAE5B,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAE9B,cAAc,EAAE,MAAM,CAAC;IAEvB,oBAAoB,EAAE,MAAM,CAAC;IAE7B,qBAAqB,EAAE,MAAM,CAAC;IAE9B,YAAY,EAAE,MAAM,CAAC;IAErB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAGD,MAAM,WAAW,sBAAsB;IAErC,YAAY,EAAE,MAAM,CAAC;IAErB,cAAc,EAAE,MAAM,CAAC;IAEvB,SAAS,EAAE,OAAO,CAAC;CACpB;AA+FD,wBAAgB,uBAAuB,IAAI,eAAe,CAUzD;AAMD,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,eAAe,GAAG,YAAY,CAW5E;AAKD,wBAAgB,qBAAqB,IAAI,aAAa,CAOrD;AAMD,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,eAAe,GAAG,QAAQ,CAYpE;AAmDD,wBAAgB,eAAe,CAC7B,MAAM,EAAE,qBAAqB,GAC5B,qBAAqB,CAsCvB;AASD,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,sBAAsB,GAC7B,sBAAsB,CAyCxB"}
|
package/dist/protocols/pqxdh.js
CHANGED
|
@@ -7,136 +7,141 @@ exports.generatePqPreKey = generatePqPreKey;
|
|
|
7
7
|
exports.initiateSession = initiateSession;
|
|
8
8
|
exports.respondToSession = respondToSession;
|
|
9
9
|
const ed25519_js_1 = require("@noble/curves/ed25519.js");
|
|
10
|
-
const
|
|
10
|
+
const pq_backend_1 = require("../accel/pq-backend");
|
|
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
|
|
14
|
+
const errors_1 = require("../errors");
|
|
15
|
+
const encoding_1 = require("../encoding");
|
|
15
16
|
function hexToBytes(hex, label) {
|
|
16
|
-
if (!
|
|
17
|
-
throw new
|
|
17
|
+
if (!(0, encoding_1.isHex)(hex)) {
|
|
18
|
+
throw new errors_1.CryptoError(`Invalid hex string for ${label}`, errors_1.CryptoErrorCode.INVALID_HEX);
|
|
18
19
|
}
|
|
19
|
-
return
|
|
20
|
+
return (0, encoding_1.decodeHex)(hex);
|
|
21
|
+
}
|
|
22
|
+
const cat = (...parts) => (0, encoding_1.concat)(parts);
|
|
23
|
+
const CURVE25519_TYPE = 0x01;
|
|
24
|
+
const ML_KEM_768_TYPE = 0x02;
|
|
25
|
+
const encodeEC = (publicKey) => cat(new Uint8Array([CURVE25519_TYPE]), publicKey);
|
|
26
|
+
const encodeKEM = (publicKey) => cat(new Uint8Array([ML_KEM_768_TYPE]), publicKey);
|
|
27
|
+
const DEFAULT_INFO = "crypto-service";
|
|
28
|
+
function kdfInfo(info) {
|
|
29
|
+
if (!/^[\x20-\x7e]{8,}$/.test(info)) {
|
|
30
|
+
throw new errors_1.CryptoError("PQXDH info must be at least 8 printable ASCII characters", errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
31
|
+
}
|
|
32
|
+
return new TextEncoder().encode(`${info}_CURVE25519_SHA-256_ML-KEM-768`);
|
|
33
|
+
}
|
|
34
|
+
function deriveSessionKey(secrets, info) {
|
|
35
|
+
const ikm = cat(new Uint8Array(32).fill(0xff), ...secrets);
|
|
36
|
+
const sk = (0, hkdf_js_1.hkdf)(sha2_js_1.sha256, ikm, new Uint8Array(32), kdfInfo(info ?? DEFAULT_INFO), 32);
|
|
37
|
+
ikm.fill(0);
|
|
38
|
+
for (const secret of secrets)
|
|
39
|
+
secret.fill(0);
|
|
40
|
+
return sk;
|
|
41
|
+
}
|
|
42
|
+
const preKeyMessage = (identity, encodedPreKey) => cat(encodeEC(identity), encodedPreKey);
|
|
43
|
+
function assertSigned(signingPublic, message, signature, label) {
|
|
44
|
+
const valid = signature.length === 64 &&
|
|
45
|
+
ed25519_js_1.ed25519.verify(signature, message, signingPublic, { zip215: false });
|
|
46
|
+
if (!valid)
|
|
47
|
+
throw new errors_1.CryptoError(`Invalid ${label} signature`, errors_1.CryptoErrorCode.AUTH_FAILED);
|
|
20
48
|
}
|
|
21
|
-
const PQXDH_INFO = new TextEncoder().encode("pqxdh-v1");
|
|
22
49
|
function generateIdentityKeyPair() {
|
|
23
50
|
const x25519Priv = (0, utils_js_1.randomBytes)(32);
|
|
24
|
-
const x25519Pub = ed25519_js_1.x25519.getPublicKey(x25519Priv);
|
|
25
51
|
const signingPriv = (0, utils_js_1.randomBytes)(32);
|
|
26
|
-
const signingPub = ed25519_js_1.ed25519.getPublicKey(signingPriv);
|
|
27
52
|
return {
|
|
28
|
-
privateKey:
|
|
29
|
-
publicKey:
|
|
30
|
-
signingPrivateKey:
|
|
31
|
-
signingPublicKey:
|
|
53
|
+
privateKey: (0, encoding_1.toHex)(x25519Priv),
|
|
54
|
+
publicKey: (0, encoding_1.toHex)(ed25519_js_1.x25519.getPublicKey(x25519Priv)),
|
|
55
|
+
signingPrivateKey: (0, encoding_1.toHex)(signingPriv),
|
|
56
|
+
signingPublicKey: (0, encoding_1.toHex)(ed25519_js_1.ed25519.getPublicKey(signingPriv)),
|
|
32
57
|
};
|
|
33
58
|
}
|
|
34
|
-
function generateSignedPreKey(
|
|
35
|
-
const sigKey = hexToBytes(
|
|
59
|
+
function generateSignedPreKey(identity) {
|
|
60
|
+
const sigKey = hexToBytes(identity.signingPrivateKey, "signingPrivateKey");
|
|
61
|
+
const ik = hexToBytes(identity.publicKey, "identityPublicKey");
|
|
36
62
|
const prePriv = (0, utils_js_1.randomBytes)(32);
|
|
37
63
|
const prePub = ed25519_js_1.x25519.getPublicKey(prePriv);
|
|
38
|
-
const signature = ed25519_js_1.ed25519.sign(prePub, sigKey);
|
|
39
64
|
return {
|
|
40
|
-
privateKey:
|
|
41
|
-
publicKey:
|
|
42
|
-
signature:
|
|
65
|
+
privateKey: (0, encoding_1.toHex)(prePriv),
|
|
66
|
+
publicKey: (0, encoding_1.toHex)(prePub),
|
|
67
|
+
signature: (0, encoding_1.toHex)(ed25519_js_1.ed25519.sign(preKeyMessage(ik, encodeEC(prePub)), sigKey)),
|
|
43
68
|
};
|
|
44
69
|
}
|
|
45
70
|
function generateOneTimePreKey() {
|
|
46
71
|
const priv = (0, utils_js_1.randomBytes)(32);
|
|
47
|
-
const pub = ed25519_js_1.x25519.getPublicKey(priv);
|
|
48
72
|
return {
|
|
49
|
-
privateKey:
|
|
50
|
-
publicKey:
|
|
73
|
+
privateKey: (0, encoding_1.toHex)(priv),
|
|
74
|
+
publicKey: (0, encoding_1.toHex)(ed25519_js_1.x25519.getPublicKey(priv)),
|
|
51
75
|
};
|
|
52
76
|
}
|
|
53
|
-
function generatePqPreKey(
|
|
54
|
-
const sigKey = hexToBytes(
|
|
55
|
-
const
|
|
56
|
-
const
|
|
77
|
+
function generatePqPreKey(identity) {
|
|
78
|
+
const sigKey = hexToBytes(identity.signingPrivateKey, "signingPrivateKey");
|
|
79
|
+
const ik = hexToBytes(identity.publicKey, "identityPublicKey");
|
|
80
|
+
const { publicKey, secretKey } = pq_backend_1.ml_kem768.keygen();
|
|
57
81
|
return {
|
|
58
|
-
publicKey:
|
|
59
|
-
secretKey:
|
|
60
|
-
signature:
|
|
82
|
+
publicKey: (0, encoding_1.toHex)(publicKey),
|
|
83
|
+
secretKey: (0, encoding_1.toHex)(secretKey),
|
|
84
|
+
signature: (0, encoding_1.toHex)(ed25519_js_1.ed25519.sign(preKeyMessage(ik, encodeKEM(publicKey)), sigKey)),
|
|
61
85
|
};
|
|
62
86
|
}
|
|
87
|
+
function verifiedBundle(params) {
|
|
88
|
+
const remoteIk = hexToBytes(params.remoteIdentityPublic, "remoteIdentityPublic");
|
|
89
|
+
const remoteSigning = hexToBytes(params.remoteIdentitySigningPublic, "remoteIdentitySigningPublic");
|
|
90
|
+
const spk = hexToBytes(params.remoteSignedPreKeyPublic, "remoteSignedPreKeyPublic");
|
|
91
|
+
const pqpk = hexToBytes(params.remotePqPreKeyPublic, "remotePqPreKeyPublic");
|
|
92
|
+
assertSigned(remoteSigning, preKeyMessage(remoteIk, encodeEC(spk)), hexToBytes(params.remoteSignedPreKeySignature, "remoteSignedPreKeySignature"), "signed pre-key");
|
|
93
|
+
assertSigned(remoteSigning, preKeyMessage(remoteIk, encodeKEM(pqpk)), hexToBytes(params.remotePqPreKeySignature, "remotePqPreKeySignature"), "PQ pre-key");
|
|
94
|
+
return { remoteIk, spk, pqpk };
|
|
95
|
+
}
|
|
63
96
|
function initiateSession(params) {
|
|
64
|
-
const {
|
|
65
|
-
const ikPriv = hexToBytes(identityKeyPair.privateKey, "identityPrivateKey");
|
|
66
|
-
const
|
|
67
|
-
const remoteSPKPub = hexToBytes(remoteSignedPreKeyPublic, "remoteSignedPreKeyPublic");
|
|
68
|
-
const remotePqPub = hexToBytes(remotePqPreKeyPublic, "remotePqPreKeyPublic");
|
|
97
|
+
const { remoteIk, spk, pqpk } = verifiedBundle(params);
|
|
98
|
+
const ikPriv = hexToBytes(params.identityKeyPair.privateKey, "identityPrivateKey");
|
|
99
|
+
const ikPub = hexToBytes(params.identityKeyPair.publicKey, "identityPublicKey");
|
|
69
100
|
const ephPriv = (0, utils_js_1.randomBytes)(32);
|
|
70
101
|
const ephPub = ed25519_js_1.x25519.getPublicKey(ephPriv);
|
|
71
|
-
const
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const dhLen = dh1.length + dh2.length + dh3.length;
|
|
81
|
-
const dh4Len = dh4 ? dh4.length : 0;
|
|
82
|
-
const totalLen = dhLen + dh4Len + pqShared.length;
|
|
83
|
-
const combined = new Uint8Array(totalLen);
|
|
84
|
-
let offset = 0;
|
|
85
|
-
combined.set(dh1, offset);
|
|
86
|
-
offset += dh1.length;
|
|
87
|
-
combined.set(dh2, offset);
|
|
88
|
-
offset += dh2.length;
|
|
89
|
-
combined.set(dh3, offset);
|
|
90
|
-
offset += dh3.length;
|
|
91
|
-
if (dh4) {
|
|
92
|
-
combined.set(dh4, offset);
|
|
93
|
-
offset += dh4.length;
|
|
102
|
+
const { cipherText, sharedSecret: ss } = pq_backend_1.ml_kem768.encapsulate(pqpk);
|
|
103
|
+
const secrets = [
|
|
104
|
+
ed25519_js_1.x25519.getSharedSecret(ikPriv, spk),
|
|
105
|
+
ed25519_js_1.x25519.getSharedSecret(ephPriv, remoteIk),
|
|
106
|
+
ed25519_js_1.x25519.getSharedSecret(ephPriv, spk),
|
|
107
|
+
];
|
|
108
|
+
if (params.remoteOneTimePreKeyPublic) {
|
|
109
|
+
const opk = hexToBytes(params.remoteOneTimePreKeyPublic, "remoteOneTimePreKeyPublic");
|
|
110
|
+
secrets.push(ed25519_js_1.x25519.getSharedSecret(ephPriv, opk));
|
|
94
111
|
}
|
|
95
|
-
|
|
96
|
-
const sharedSecret = (
|
|
112
|
+
secrets.push(ss);
|
|
113
|
+
const sharedSecret = deriveSessionKey(secrets, params.info);
|
|
114
|
+
ephPriv.fill(0);
|
|
97
115
|
return {
|
|
98
|
-
sharedSecret:
|
|
99
|
-
|
|
100
|
-
|
|
116
|
+
sharedSecret: (0, encoding_1.toHex)(sharedSecret),
|
|
117
|
+
associatedData: (0, encoding_1.toHex)(cat(encodeEC(ikPub), encodeEC(remoteIk))),
|
|
118
|
+
ephemeralPublicKey: (0, encoding_1.toHex)(ephPub),
|
|
119
|
+
pqCiphertext: (0, encoding_1.toHex)(cipherText),
|
|
101
120
|
algorithm: "pqxdh",
|
|
102
121
|
};
|
|
103
122
|
}
|
|
104
123
|
function respondToSession(params) {
|
|
105
|
-
const
|
|
106
|
-
const
|
|
107
|
-
const spkPriv = hexToBytes(signedPreKeyPrivate, "signedPreKeyPrivate");
|
|
108
|
-
const pqSecret = hexToBytes(pqPreKeySecret, "pqPreKeySecret");
|
|
109
|
-
const
|
|
110
|
-
const
|
|
111
|
-
const
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
const pqShared = ml_kem_js_1.ml_kem768.decapsulate(pqCt, pqSecret);
|
|
121
|
-
const dhLen = dh1.length + dh2.length + dh3.length;
|
|
122
|
-
const dh4Len = dh4 ? dh4.length : 0;
|
|
123
|
-
const totalLen = dhLen + dh4Len + pqShared.length;
|
|
124
|
-
const combined = new Uint8Array(totalLen);
|
|
125
|
-
let offset = 0;
|
|
126
|
-
combined.set(dh1, offset);
|
|
127
|
-
offset += dh1.length;
|
|
128
|
-
combined.set(dh2, offset);
|
|
129
|
-
offset += dh2.length;
|
|
130
|
-
combined.set(dh3, offset);
|
|
131
|
-
offset += dh3.length;
|
|
132
|
-
if (dh4) {
|
|
133
|
-
combined.set(dh4, offset);
|
|
134
|
-
offset += dh4.length;
|
|
124
|
+
const ikPriv = hexToBytes(params.identityKeyPair.privateKey, "identityPrivateKey");
|
|
125
|
+
const ikPub = hexToBytes(params.identityKeyPair.publicKey, "identityPublicKey");
|
|
126
|
+
const spkPriv = hexToBytes(params.signedPreKeyPrivate, "signedPreKeyPrivate");
|
|
127
|
+
const pqSecret = hexToBytes(params.pqPreKeySecret, "pqPreKeySecret");
|
|
128
|
+
const remoteIk = hexToBytes(params.remoteIdentityPublic, "remoteIdentityPublic");
|
|
129
|
+
const remoteEph = hexToBytes(params.remoteEphemeralPublic, "remoteEphemeralPublic");
|
|
130
|
+
const ct = hexToBytes(params.pqCiphertext, "pqCiphertext");
|
|
131
|
+
const secrets = [
|
|
132
|
+
ed25519_js_1.x25519.getSharedSecret(spkPriv, remoteIk),
|
|
133
|
+
ed25519_js_1.x25519.getSharedSecret(ikPriv, remoteEph),
|
|
134
|
+
ed25519_js_1.x25519.getSharedSecret(spkPriv, remoteEph),
|
|
135
|
+
];
|
|
136
|
+
if (params.oneTimePreKeyPrivate) {
|
|
137
|
+
const opkPriv = hexToBytes(params.oneTimePreKeyPrivate, "oneTimePreKeyPrivate");
|
|
138
|
+
secrets.push(ed25519_js_1.x25519.getSharedSecret(opkPriv, remoteEph));
|
|
135
139
|
}
|
|
136
|
-
|
|
137
|
-
const sharedSecret = (
|
|
140
|
+
secrets.push(pq_backend_1.ml_kem768.decapsulate(ct, pqSecret));
|
|
141
|
+
const sharedSecret = deriveSessionKey(secrets, params.info);
|
|
138
142
|
return {
|
|
139
|
-
sharedSecret:
|
|
143
|
+
sharedSecret: (0, encoding_1.toHex)(sharedSecret),
|
|
144
|
+
associatedData: (0, encoding_1.toHex)(cat(encodeEC(remoteIk), encodeEC(ikPub))),
|
|
140
145
|
algorithm: "pqxdh",
|
|
141
146
|
};
|
|
142
147
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ratchet.d.ts","sourceRoot":"","sources":["../../src/protocols/ratchet.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ratchet.d.ts","sourceRoot":"","sources":["../../src/protocols/ratchet.ts"],"names":[],"mappings":"AAoCA,MAAM,WAAW,aAAa;IAE5B,SAAS,EAAE,MAAM,CAAC;IAElB,mBAAmB,EAAE,MAAM,CAAC;IAE5B,aAAa,EAAE,MAAM,CAAC;CACvB;AAGD,MAAM,WAAW,gBAAgB;IAE/B,MAAM,EAAE,aAAa,CAAC;IAEtB,UAAU,EAAE,MAAM,CAAC;CACpB;AAGD,MAAM,WAAW,qBAAqB;IAEpC,QAAQ,EAAE,MAAM,CAAC;IAEjB,KAAK,EAAE,MAAM,CAAC;CACf;AAGD,MAAM,WAAW,kBAAkB;IAEjC,qBAAqB,EAAE,MAAM,CAAC;IAE9B,oBAAoB,EAAE,MAAM,C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;CAkBtB"}
|
|
@@ -7,17 +7,18 @@ const sha2_js_1 = require("@noble/hashes/sha2.js");
|
|
|
7
7
|
const hmac_js_1 = require("@noble/hashes/hmac.js");
|
|
8
8
|
const chacha_js_1 = require("@noble/ciphers/chacha.js");
|
|
9
9
|
const utils_js_1 = require("@noble/ciphers/utils.js");
|
|
10
|
+
const errors_1 = require("../errors");
|
|
11
|
+
const encoding_1 = require("../encoding");
|
|
10
12
|
const NONCE_LEN = 24;
|
|
11
|
-
const HEX_RE = /^[0-9a-fA-F]*$/;
|
|
12
13
|
const MAX_SKIP = 256;
|
|
13
14
|
function hexToBytes(hex) {
|
|
14
|
-
if (!
|
|
15
|
-
throw new
|
|
15
|
+
if (!(0, encoding_1.isHex)(hex)) {
|
|
16
|
+
throw new errors_1.CryptoError("Invalid hex string", errors_1.CryptoErrorCode.INVALID_HEX);
|
|
16
17
|
}
|
|
17
|
-
return
|
|
18
|
+
return (0, encoding_1.decodeHex)(hex);
|
|
18
19
|
}
|
|
19
20
|
function bytesToHex(bytes) {
|
|
20
|
-
return
|
|
21
|
+
return (0, encoding_1.toHex)(bytes);
|
|
21
22
|
}
|
|
22
23
|
class SymmetricRatchet {
|
|
23
24
|
_chainKey;
|
|
@@ -91,7 +92,7 @@ class DoubleRatchet {
|
|
|
91
92
|
}
|
|
92
93
|
encrypt(plaintext) {
|
|
93
94
|
if (!this._state.sendingChainKey) {
|
|
94
|
-
throw new
|
|
95
|
+
throw new errors_1.CryptoError("Sending chain not initialized — cannot encrypt", errors_1.CryptoErrorCode.INVALID_STATE);
|
|
95
96
|
}
|
|
96
97
|
const chain = new SymmetricRatchet(this._state.sendingChainKey);
|
|
97
98
|
const { messageKey } = chain.next();
|
|
@@ -103,10 +104,8 @@ class DoubleRatchet {
|
|
|
103
104
|
};
|
|
104
105
|
this._state.sendingMessageNumber++;
|
|
105
106
|
const nonce = (0, utils_js_1.randomBytes)(NONCE_LEN);
|
|
106
|
-
const pt = plaintext instanceof Uint8Array
|
|
107
|
-
|
|
108
|
-
: Buffer.from(plaintext, "utf8");
|
|
109
|
-
const aad = Buffer.from(JSON.stringify(header), "utf8");
|
|
107
|
+
const pt = plaintext instanceof Uint8Array ? plaintext : (0, encoding_1.utf8)(plaintext);
|
|
108
|
+
const aad = (0, encoding_1.utf8)(JSON.stringify(header));
|
|
110
109
|
const cipher = (0, chacha_js_1.xchacha20poly1305)(messageKey, nonce, aad);
|
|
111
110
|
const ct = cipher.encrypt(pt);
|
|
112
111
|
const packed = new Uint8Array(NONCE_LEN + ct.length);
|
|
@@ -114,7 +113,7 @@ class DoubleRatchet {
|
|
|
114
113
|
packed.set(ct, NONCE_LEN);
|
|
115
114
|
return {
|
|
116
115
|
header,
|
|
117
|
-
ciphertext:
|
|
116
|
+
ciphertext: (0, encoding_1.toBase64)(packed),
|
|
118
117
|
};
|
|
119
118
|
}
|
|
120
119
|
decrypt(header, ciphertext) {
|
|
@@ -138,13 +137,13 @@ class DoubleRatchet {
|
|
|
138
137
|
return this._decryptWithKey(messageKey, header, ciphertext);
|
|
139
138
|
}
|
|
140
139
|
_decryptWithKey(messageKey, header, ciphertext) {
|
|
141
|
-
const raw =
|
|
140
|
+
const raw = (0, encoding_1.fromBase64)(ciphertext);
|
|
142
141
|
if (raw.length < NONCE_LEN + 16) {
|
|
143
|
-
throw new
|
|
142
|
+
throw new errors_1.CryptoError("Ciphertext too short", errors_1.CryptoErrorCode.INVALID_CIPHERTEXT);
|
|
144
143
|
}
|
|
145
144
|
const nonce = raw.subarray(0, NONCE_LEN);
|
|
146
145
|
const ct = raw.subarray(NONCE_LEN);
|
|
147
|
-
const aad =
|
|
146
|
+
const aad = (0, encoding_1.utf8)(JSON.stringify(header));
|
|
148
147
|
const cipher = (0, chacha_js_1.xchacha20poly1305)(messageKey, nonce, aad);
|
|
149
148
|
return cipher.decrypt(ct);
|
|
150
149
|
}
|
|
@@ -170,7 +169,7 @@ class DoubleRatchet {
|
|
|
170
169
|
}
|
|
171
170
|
_skipMessages(publicKey, start, until) {
|
|
172
171
|
if (until - start > MAX_SKIP) {
|
|
173
|
-
throw new
|
|
172
|
+
throw new errors_1.CryptoError(`Too many skipped messages: ${until - start} exceeds max ${MAX_SKIP}`, errors_1.CryptoErrorCode.INVALID_STATE);
|
|
174
173
|
}
|
|
175
174
|
if (!this._state.receivingChainKey)
|
|
176
175
|
return;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"threshold.d.ts","sourceRoot":"","sources":["../../src/protocols/threshold.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"threshold.d.ts","sourceRoot":"","sources":["../../src/protocols/threshold.ts"],"names":[],"mappings":"AA2BA,MAAM,WAAW,KAAK;IAEpB,KAAK,EAAE,MAAM,CAAC;IAEd,KAAK,EAAE,MAAM,CAAC;CACf;AAGD,MAAM,WAAW,WAAW;IAE1B,MAAM,EAAE,KAAK,EAAE,CAAC;IAEhB,SAAS,EAAE,MAAM,CAAC;IAElB,SAAS,EAAE,gBAAgB,CAAC;CAC7B;AAGD,MAAM,WAAW,kBAAkB;IAEjC,WAAW,EAAE,MAAM,EAAE,CAAC;IAEtB,SAAS,EAAE,qBAAqB,CAAC;CAClC;AA6HD,wBAAgB,WAAW,CACzB,MAAM,EAAE,MAAM,EACd,CAAC,EAAE,MAAM,EACT,SAAS,EAAE,MAAM,GAChB,WAAW,CAoDb;AAQD,wBAAgB,aAAa,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,CAuCrD;AAaD,wBAAgB,0BAA0B,CACxC,YAAY,EAAE,MAAM,EAAE,GACrB,kBAAkB,CAoBpB;AAYD,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,KAAK,EACZ,WAAW,EAAE,kBAAkB,GAC9B,OAAO,CAsBT;AAUD,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,MAAM,EACd,CAAC,EAAE,MAAM,EACT,SAAS,EAAE,MAAM,GAChB;IAED,MAAM,EAAE,KAAK,EAAE,CAAC;IAEhB,WAAW,EAAE,kBAAkB,CAAC;IAEhC,SAAS,EAAE,MAAM,CAAC;CACnB,CAoDA"}
|