@sebastienrousseau/crypto-lib 0.0.34 → 0.0.36
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 +40 -0
- package/dist/streaming/chunks.d.ts.map +1 -0
- package/dist/streaming/chunks.js +173 -0
- 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 +33 -82
- 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 +90 -147
- package/dist/streaming/stream-pq-aead.d.ts +7 -3
- package/dist/streaming/stream-pq-aead.d.ts.map +1 -1
- package/dist/streaming/stream-pq-aead.js +34 -114
- 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/pq-kem.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RFC10024_CODEPOINTS = exports.RFC10024_SECP256R1_MLKEM768 = exports.RFC10024_X25519_MLKEM768 = void 0;
|
|
3
|
+
exports.mlKemDecap = exports.mlKemEncap = exports.mlKemGenerateKeyPair = exports.RFC10024_CODEPOINTS = exports.RFC10024_SECP256R1_MLKEM768 = exports.RFC10024_X25519_MLKEM768 = void 0;
|
|
4
4
|
exports.normalizeHybridKemAlgorithm = normalizeHybridKemAlgorithm;
|
|
5
5
|
exports.mlKemKeygen = mlKemKeygen;
|
|
6
6
|
exports.mlKemEncapsulate = mlKemEncapsulate;
|
|
@@ -14,13 +14,14 @@ exports.p256MlKemDecapsulate = p256MlKemDecapsulate;
|
|
|
14
14
|
exports.x448MlKemKeygen = x448MlKemKeygen;
|
|
15
15
|
exports.x448MlKemEncapsulate = x448MlKemEncapsulate;
|
|
16
16
|
exports.x448MlKemDecapsulate = x448MlKemDecapsulate;
|
|
17
|
-
const
|
|
17
|
+
const pq_backend_1 = require("../accel/pq-backend");
|
|
18
|
+
const hybrid_combiner_1 = require("./hybrid-combiner");
|
|
18
19
|
const ed25519_js_1 = require("@noble/curves/ed25519.js");
|
|
19
20
|
const ed448_js_1 = require("@noble/curves/ed448.js");
|
|
20
21
|
const nist_js_1 = require("@noble/curves/nist.js");
|
|
21
|
-
const hkdf_js_1 = require("@noble/hashes/hkdf.js");
|
|
22
|
-
const sha2_js_1 = require("@noble/hashes/sha2.js");
|
|
23
22
|
const utils_js_1 = require("@noble/ciphers/utils.js");
|
|
23
|
+
const errors_1 = require("../errors");
|
|
24
|
+
const encoding_1 = require("../encoding");
|
|
24
25
|
exports.RFC10024_X25519_MLKEM768 = "X25519MLKEM768";
|
|
25
26
|
exports.RFC10024_SECP256R1_MLKEM768 = "SecP256r1MLKEM768";
|
|
26
27
|
exports.RFC10024_CODEPOINTS = {
|
|
@@ -30,53 +31,32 @@ exports.RFC10024_CODEPOINTS = {
|
|
|
30
31
|
function normalizeHybridKemAlgorithm(name) {
|
|
31
32
|
if (name === exports.RFC10024_X25519_MLKEM768 ||
|
|
32
33
|
name === exports.RFC10024_SECP256R1_MLKEM768) {
|
|
33
|
-
throw new
|
|
34
|
-
"its hybrid KEMs use a different combiner and do not interoperate");
|
|
34
|
+
throw new errors_1.CryptoError(`${name} (RFC 10024 TLS group) is not implemented by this library; ` +
|
|
35
|
+
"its hybrid KEMs use a different combiner and do not interoperate", errors_1.CryptoErrorCode.UNSUPPORTED_ALGORITHM);
|
|
35
36
|
}
|
|
36
37
|
return name;
|
|
37
38
|
}
|
|
38
|
-
const HEX_RE = /^[0-9a-fA-F]*$/;
|
|
39
39
|
function assertHex(input, label) {
|
|
40
|
-
if (!
|
|
41
|
-
throw new
|
|
40
|
+
if (!(0, encoding_1.isHex)(input)) {
|
|
41
|
+
throw new errors_1.CryptoError(`Invalid hex string for ${label}`, errors_1.CryptoErrorCode.INVALID_HEX);
|
|
42
42
|
}
|
|
43
|
-
return
|
|
43
|
+
return (0, encoding_1.decodeHex)(input);
|
|
44
44
|
}
|
|
45
45
|
function getKem(level) {
|
|
46
46
|
switch (level) {
|
|
47
47
|
case 512:
|
|
48
|
-
return
|
|
48
|
+
return pq_backend_1.ml_kem512;
|
|
49
49
|
case 768:
|
|
50
|
-
return
|
|
50
|
+
return pq_backend_1.ml_kem768;
|
|
51
51
|
case 1024:
|
|
52
|
-
return
|
|
52
|
+
return pq_backend_1.ml_kem1024;
|
|
53
53
|
default:
|
|
54
|
-
throw new
|
|
54
|
+
throw new errors_1.CryptoError(`Unsupported ML-KEM level: ${level}. Supported: 512, 768, 1024`, errors_1.CryptoErrorCode.UNSUPPORTED_ALGORITHM);
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
function algorithmName(level) {
|
|
58
58
|
return `ml-kem-${level}`;
|
|
59
59
|
}
|
|
60
|
-
function lengthPrefixed(bytes) {
|
|
61
|
-
const out = new Uint8Array(4 + bytes.length);
|
|
62
|
-
new DataView(out.buffer).setUint32(0, bytes.length, false);
|
|
63
|
-
out.set(bytes, 4);
|
|
64
|
-
return out;
|
|
65
|
-
}
|
|
66
|
-
function combineHybrid(t) {
|
|
67
|
-
const ikm = Buffer.concat([t.ssClassical, t.ssMlKem]);
|
|
68
|
-
const info = Buffer.concat([
|
|
69
|
-
lengthPrefixed(Buffer.from(`crypto-service/hybrid-kem/v2/${t.algorithm}`)),
|
|
70
|
-
lengthPrefixed(t.ctClassical),
|
|
71
|
-
lengthPrefixed(t.pkClassical),
|
|
72
|
-
lengthPrefixed(t.ctMlKem),
|
|
73
|
-
lengthPrefixed(t.pkMlKem),
|
|
74
|
-
]);
|
|
75
|
-
return (0, hkdf_js_1.hkdf)(sha2_js_1.sha256, ikm, undefined, info, 32);
|
|
76
|
-
}
|
|
77
|
-
function toHex(bytes) {
|
|
78
|
-
return Buffer.from(bytes).toString("hex");
|
|
79
|
-
}
|
|
80
60
|
function hybridAlgorithmName(level) {
|
|
81
61
|
return `x25519-ml-kem-${level}`;
|
|
82
62
|
}
|
|
@@ -84,41 +64,53 @@ function mlKemKeygen(level) {
|
|
|
84
64
|
const kem = getKem(level);
|
|
85
65
|
const { publicKey, secretKey } = kem.keygen();
|
|
86
66
|
return {
|
|
87
|
-
publicKey:
|
|
88
|
-
secretKey:
|
|
67
|
+
publicKey: (0, encoding_1.toHex)(publicKey),
|
|
68
|
+
secretKey: (0, encoding_1.toHex)(secretKey),
|
|
89
69
|
algorithm: algorithmName(level),
|
|
90
70
|
};
|
|
91
71
|
}
|
|
92
|
-
function mlKemEncapsulate(
|
|
72
|
+
function mlKemEncapsulate(levelOrKey, key) {
|
|
73
|
+
const [level, publicKeyHex] = typeof levelOrKey === "string"
|
|
74
|
+
? [768, levelOrKey]
|
|
75
|
+
: [levelOrKey, key];
|
|
93
76
|
const kem = getKem(level);
|
|
94
77
|
const publicKey = assertHex(publicKeyHex, "publicKey");
|
|
95
78
|
const { cipherText, sharedSecret } = kem.encapsulate(publicKey);
|
|
96
79
|
return {
|
|
97
|
-
ciphertext:
|
|
98
|
-
sharedSecret:
|
|
80
|
+
ciphertext: (0, encoding_1.toHex)(cipherText),
|
|
81
|
+
sharedSecret: (0, encoding_1.toHex)(sharedSecret),
|
|
99
82
|
algorithm: algorithmName(level),
|
|
100
83
|
};
|
|
101
84
|
}
|
|
102
|
-
function mlKemDecapsulate(
|
|
85
|
+
function mlKemDecapsulate(levelOrKey, keyOrCiphertext, ciphertext) {
|
|
86
|
+
const [level, secretKeyHex, ciphertextHex] = typeof levelOrKey === "string"
|
|
87
|
+
? [768, levelOrKey, keyOrCiphertext]
|
|
88
|
+
: [levelOrKey, keyOrCiphertext, ciphertext];
|
|
103
89
|
const kem = getKem(level);
|
|
104
90
|
const secretKey = assertHex(secretKeyHex, "secretKey");
|
|
105
91
|
const cipherText = assertHex(ciphertextHex, "ciphertext");
|
|
106
92
|
const sharedSecret = kem.decapsulate(cipherText, secretKey);
|
|
107
93
|
return {
|
|
108
|
-
sharedSecret:
|
|
94
|
+
sharedSecret: (0, encoding_1.toHex)(sharedSecret),
|
|
109
95
|
algorithm: algorithmName(level),
|
|
110
96
|
};
|
|
111
97
|
}
|
|
98
|
+
const mlKemGenerateKeyPair = () => mlKemKeygen(768);
|
|
99
|
+
exports.mlKemGenerateKeyPair = mlKemGenerateKeyPair;
|
|
100
|
+
const mlKemEncap = (level, publicKeyHex) => mlKemEncapsulate(level, publicKeyHex);
|
|
101
|
+
exports.mlKemEncap = mlKemEncap;
|
|
102
|
+
const mlKemDecap = (level, secretKeyHex, ciphertextHex) => mlKemDecapsulate(level, secretKeyHex, ciphertextHex);
|
|
103
|
+
exports.mlKemDecap = mlKemDecap;
|
|
112
104
|
function hybridKemKeygen(kemLevel = 768) {
|
|
113
105
|
const kem = getKem(kemLevel);
|
|
114
106
|
const x25519Priv = (0, utils_js_1.randomBytes)(32);
|
|
115
107
|
const x25519Pub = ed25519_js_1.x25519.getPublicKey(x25519Priv);
|
|
116
108
|
const { publicKey: mlKemPub, secretKey: mlKemSec } = kem.keygen();
|
|
117
109
|
return {
|
|
118
|
-
x25519PrivateKey:
|
|
119
|
-
x25519PublicKey:
|
|
120
|
-
mlKemPublicKey:
|
|
121
|
-
mlKemSecretKey:
|
|
110
|
+
x25519PrivateKey: (0, encoding_1.toHex)(x25519Priv),
|
|
111
|
+
x25519PublicKey: (0, encoding_1.toHex)(x25519Pub),
|
|
112
|
+
mlKemPublicKey: (0, encoding_1.toHex)(mlKemPub),
|
|
113
|
+
mlKemSecretKey: (0, encoding_1.toHex)(mlKemSec),
|
|
122
114
|
algorithm: hybridAlgorithmName(kemLevel),
|
|
123
115
|
};
|
|
124
116
|
}
|
|
@@ -131,7 +123,7 @@ function hybridKemEncapsulate(kemLevel, theirX25519Public, theirMlKemPublic) {
|
|
|
131
123
|
const ssClassical = ed25519_js_1.x25519.getSharedSecret(ephemeralPriv, pkClassical);
|
|
132
124
|
const { cipherText, sharedSecret: ssMlKem } = kem.encapsulate(pkMlKem);
|
|
133
125
|
const algorithm = hybridAlgorithmName(kemLevel);
|
|
134
|
-
const derived = combineHybrid({
|
|
126
|
+
const derived = (0, hybrid_combiner_1.combineHybrid)({
|
|
135
127
|
algorithm,
|
|
136
128
|
ssClassical,
|
|
137
129
|
ssMlKem,
|
|
@@ -141,9 +133,9 @@ function hybridKemEncapsulate(kemLevel, theirX25519Public, theirMlKemPublic) {
|
|
|
141
133
|
pkMlKem,
|
|
142
134
|
});
|
|
143
135
|
return {
|
|
144
|
-
x25519EphemeralPublic: toHex(ephemeralPub),
|
|
145
|
-
mlKemCiphertext: toHex(cipherText),
|
|
146
|
-
sharedSecret: toHex(derived),
|
|
136
|
+
x25519EphemeralPublic: (0, encoding_1.toHex)(ephemeralPub),
|
|
137
|
+
mlKemCiphertext: (0, encoding_1.toHex)(cipherText),
|
|
138
|
+
sharedSecret: (0, encoding_1.toHex)(derived),
|
|
147
139
|
algorithm,
|
|
148
140
|
};
|
|
149
141
|
}
|
|
@@ -154,7 +146,7 @@ function hybridKemDecapsulate(kemLevel, ourX25519Private, ourMlKemSecret, theirX
|
|
|
154
146
|
const ctMlKem = assertHex(mlKemCiphertext, "mlKemCiphertext");
|
|
155
147
|
const mlKemSecret = assertHex(ourMlKemSecret, "ourMlKemSecret");
|
|
156
148
|
const algorithm = hybridAlgorithmName(kemLevel);
|
|
157
|
-
const derived = combineHybrid({
|
|
149
|
+
const derived = (0, hybrid_combiner_1.combineHybrid)({
|
|
158
150
|
algorithm,
|
|
159
151
|
ssClassical: ed25519_js_1.x25519.getSharedSecret(priv, ctClassical),
|
|
160
152
|
ssMlKem: kem.decapsulate(ctMlKem, mlKemSecret),
|
|
@@ -163,17 +155,17 @@ function hybridKemDecapsulate(kemLevel, ourX25519Private, ourMlKemSecret, theirX
|
|
|
163
155
|
ctMlKem,
|
|
164
156
|
pkMlKem: kem.getPublicKey(mlKemSecret),
|
|
165
157
|
});
|
|
166
|
-
return { sharedSecret: toHex(derived), algorithm };
|
|
158
|
+
return { sharedSecret: (0, encoding_1.toHex)(derived), algorithm };
|
|
167
159
|
}
|
|
168
160
|
function p256MlKemKeygen() {
|
|
169
161
|
const p256Priv = nist_js_1.p256.utils.randomSecretKey();
|
|
170
162
|
const p256Pub = nist_js_1.p256.getPublicKey(p256Priv, false);
|
|
171
|
-
const { publicKey: mlKemPub, secretKey: mlKemSec } =
|
|
163
|
+
const { publicKey: mlKemPub, secretKey: mlKemSec } = pq_backend_1.ml_kem768.keygen();
|
|
172
164
|
return {
|
|
173
|
-
p256PrivateKey:
|
|
174
|
-
p256PublicKey:
|
|
175
|
-
mlKemPublicKey:
|
|
176
|
-
mlKemSecretKey:
|
|
165
|
+
p256PrivateKey: (0, encoding_1.toHex)(p256Priv),
|
|
166
|
+
p256PublicKey: (0, encoding_1.toHex)(p256Pub),
|
|
167
|
+
mlKemPublicKey: (0, encoding_1.toHex)(mlKemPub),
|
|
168
|
+
mlKemSecretKey: (0, encoding_1.toHex)(mlKemSec),
|
|
177
169
|
algorithm: "p256-ml-kem-768",
|
|
178
170
|
};
|
|
179
171
|
}
|
|
@@ -183,8 +175,8 @@ function p256MlKemEncapsulate(theirP256Public, theirMlKemPublic) {
|
|
|
183
175
|
const ephemeralPriv = nist_js_1.p256.utils.randomSecretKey();
|
|
184
176
|
const ephemeralPub = nist_js_1.p256.getPublicKey(ephemeralPriv, false);
|
|
185
177
|
const ssClassical = nist_js_1.p256.getSharedSecret(ephemeralPriv, pkClassical);
|
|
186
|
-
const { cipherText, sharedSecret: ssMlKem } =
|
|
187
|
-
const derived = combineHybrid({
|
|
178
|
+
const { cipherText, sharedSecret: ssMlKem } = pq_backend_1.ml_kem768.encapsulate(pkMlKem);
|
|
179
|
+
const derived = (0, hybrid_combiner_1.combineHybrid)({
|
|
188
180
|
algorithm: "p256-ml-kem-768",
|
|
189
181
|
ssClassical,
|
|
190
182
|
ssMlKem,
|
|
@@ -194,9 +186,9 @@ function p256MlKemEncapsulate(theirP256Public, theirMlKemPublic) {
|
|
|
194
186
|
pkMlKem,
|
|
195
187
|
});
|
|
196
188
|
return {
|
|
197
|
-
p256EphemeralPublic: toHex(ephemeralPub),
|
|
198
|
-
mlKemCiphertext: toHex(cipherText),
|
|
199
|
-
sharedSecret: toHex(derived),
|
|
189
|
+
p256EphemeralPublic: (0, encoding_1.toHex)(ephemeralPub),
|
|
190
|
+
mlKemCiphertext: (0, encoding_1.toHex)(cipherText),
|
|
191
|
+
sharedSecret: (0, encoding_1.toHex)(derived),
|
|
200
192
|
algorithm: "p256-ml-kem-768",
|
|
201
193
|
};
|
|
202
194
|
}
|
|
@@ -205,26 +197,26 @@ function p256MlKemDecapsulate(ourP256Private, ourMlKemSecret, theirP256Ephemeral
|
|
|
205
197
|
const ctClassical = assertHex(theirP256Ephemeral, "theirP256Ephemeral");
|
|
206
198
|
const ctMlKem = assertHex(mlKemCiphertext, "mlKemCiphertext");
|
|
207
199
|
const mlKemSecret = assertHex(ourMlKemSecret, "ourMlKemSecret");
|
|
208
|
-
const derived = combineHybrid({
|
|
200
|
+
const derived = (0, hybrid_combiner_1.combineHybrid)({
|
|
209
201
|
algorithm: "p256-ml-kem-768",
|
|
210
202
|
ssClassical: nist_js_1.p256.getSharedSecret(priv, ctClassical),
|
|
211
|
-
ssMlKem:
|
|
203
|
+
ssMlKem: pq_backend_1.ml_kem768.decapsulate(ctMlKem, mlKemSecret),
|
|
212
204
|
ctClassical,
|
|
213
205
|
pkClassical: nist_js_1.p256.getPublicKey(priv, false),
|
|
214
206
|
ctMlKem,
|
|
215
|
-
pkMlKem:
|
|
207
|
+
pkMlKem: pq_backend_1.ml_kem768.getPublicKey(mlKemSecret),
|
|
216
208
|
});
|
|
217
|
-
return { sharedSecret: toHex(derived), algorithm: "p256-ml-kem-768" };
|
|
209
|
+
return { sharedSecret: (0, encoding_1.toHex)(derived), algorithm: "p256-ml-kem-768" };
|
|
218
210
|
}
|
|
219
211
|
function x448MlKemKeygen() {
|
|
220
212
|
const x448Priv = ed448_js_1.x448.utils.randomSecretKey();
|
|
221
213
|
const x448Pub = ed448_js_1.x448.getPublicKey(x448Priv);
|
|
222
|
-
const { publicKey: mlKemPub, secretKey: mlKemSec } =
|
|
214
|
+
const { publicKey: mlKemPub, secretKey: mlKemSec } = pq_backend_1.ml_kem1024.keygen();
|
|
223
215
|
return {
|
|
224
|
-
x448PrivateKey:
|
|
225
|
-
x448PublicKey:
|
|
226
|
-
mlKemPublicKey:
|
|
227
|
-
mlKemSecretKey:
|
|
216
|
+
x448PrivateKey: (0, encoding_1.toHex)(x448Priv),
|
|
217
|
+
x448PublicKey: (0, encoding_1.toHex)(x448Pub),
|
|
218
|
+
mlKemPublicKey: (0, encoding_1.toHex)(mlKemPub),
|
|
219
|
+
mlKemSecretKey: (0, encoding_1.toHex)(mlKemSec),
|
|
228
220
|
algorithm: "x448-ml-kem-1024",
|
|
229
221
|
};
|
|
230
222
|
}
|
|
@@ -234,8 +226,8 @@ function x448MlKemEncapsulate(theirX448Public, theirMlKemPublic) {
|
|
|
234
226
|
const ephemeralPriv = ed448_js_1.x448.utils.randomSecretKey();
|
|
235
227
|
const ephemeralPub = ed448_js_1.x448.getPublicKey(ephemeralPriv);
|
|
236
228
|
const ssClassical = ed448_js_1.x448.getSharedSecret(ephemeralPriv, pkClassical);
|
|
237
|
-
const { cipherText, sharedSecret: ssMlKem } =
|
|
238
|
-
const derived = combineHybrid({
|
|
229
|
+
const { cipherText, sharedSecret: ssMlKem } = pq_backend_1.ml_kem1024.encapsulate(pkMlKem);
|
|
230
|
+
const derived = (0, hybrid_combiner_1.combineHybrid)({
|
|
239
231
|
algorithm: "x448-ml-kem-1024",
|
|
240
232
|
ssClassical,
|
|
241
233
|
ssMlKem,
|
|
@@ -245,9 +237,9 @@ function x448MlKemEncapsulate(theirX448Public, theirMlKemPublic) {
|
|
|
245
237
|
pkMlKem,
|
|
246
238
|
});
|
|
247
239
|
return {
|
|
248
|
-
x448EphemeralPublic: toHex(ephemeralPub),
|
|
249
|
-
mlKemCiphertext: toHex(cipherText),
|
|
250
|
-
sharedSecret: toHex(derived),
|
|
240
|
+
x448EphemeralPublic: (0, encoding_1.toHex)(ephemeralPub),
|
|
241
|
+
mlKemCiphertext: (0, encoding_1.toHex)(cipherText),
|
|
242
|
+
sharedSecret: (0, encoding_1.toHex)(derived),
|
|
251
243
|
algorithm: "x448-ml-kem-1024",
|
|
252
244
|
};
|
|
253
245
|
}
|
|
@@ -256,15 +248,15 @@ function x448MlKemDecapsulate(ourX448Private, ourMlKemSecret, theirX448Ephemeral
|
|
|
256
248
|
const ctClassical = assertHex(theirX448Ephemeral, "theirX448Ephemeral");
|
|
257
249
|
const ctMlKem = assertHex(mlKemCiphertext, "mlKemCiphertext");
|
|
258
250
|
const mlKemSecret = assertHex(ourMlKemSecret, "ourMlKemSecret");
|
|
259
|
-
const derived = combineHybrid({
|
|
251
|
+
const derived = (0, hybrid_combiner_1.combineHybrid)({
|
|
260
252
|
algorithm: "x448-ml-kem-1024",
|
|
261
253
|
ssClassical: ed448_js_1.x448.getSharedSecret(priv, ctClassical),
|
|
262
|
-
ssMlKem:
|
|
254
|
+
ssMlKem: pq_backend_1.ml_kem1024.decapsulate(ctMlKem, mlKemSecret),
|
|
263
255
|
ctClassical,
|
|
264
256
|
pkClassical: ed448_js_1.x448.getPublicKey(priv),
|
|
265
257
|
ctMlKem,
|
|
266
|
-
pkMlKem:
|
|
258
|
+
pkMlKem: pq_backend_1.ml_kem1024.getPublicKey(mlKemSecret),
|
|
267
259
|
});
|
|
268
|
-
return { sharedSecret: toHex(derived), algorithm: "x448-ml-kem-1024" };
|
|
260
|
+
return { sharedSecret: (0, encoding_1.toHex)(derived), algorithm: "x448-ml-kem-1024" };
|
|
269
261
|
}
|
|
270
262
|
//# sourceMappingURL=pq-kem.js.map
|
package/dist/modern/pq-sign.d.ts
CHANGED
|
@@ -16,8 +16,15 @@ export interface MlDsaVerifyResult {
|
|
|
16
16
|
export interface HybridSignResult {
|
|
17
17
|
ed25519Signature: string;
|
|
18
18
|
mlDsaSignature: string;
|
|
19
|
+
signature: string;
|
|
19
20
|
algorithm: string;
|
|
20
21
|
}
|
|
22
|
+
export interface HybridSignOptions {
|
|
23
|
+
context?: string | Uint8Array;
|
|
24
|
+
}
|
|
25
|
+
export interface HybridVerifyOptions extends HybridSignOptions {
|
|
26
|
+
acceptLegacy?: boolean;
|
|
27
|
+
}
|
|
21
28
|
export interface HybridVerifyResult {
|
|
22
29
|
valid: boolean;
|
|
23
30
|
algorithm: string;
|
|
@@ -25,6 +32,6 @@ export interface HybridVerifyResult {
|
|
|
25
32
|
export declare function mlDsaKeygen(level: MlDsaLevel): MlDsaKeyPairResult;
|
|
26
33
|
export declare function mlDsaSign(level: MlDsaLevel, secretKeyHex: string, message: string | Uint8Array): MlDsaSignResult;
|
|
27
34
|
export declare function mlDsaVerify(level: MlDsaLevel, publicKeyHex: string, message: string | Uint8Array, signatureHex: string): MlDsaVerifyResult;
|
|
28
|
-
export declare function hybridSign(secretKeyEd25519Hex: string, secretKeyMlDsaHex: string, message: string | Uint8Array, mlDsaLevel?: MlDsaLevel): HybridSignResult;
|
|
29
|
-
export declare function hybridVerify(pubKeyEd25519Hex: string, pubKeyMlDsaHex: string, message: string | Uint8Array, sigEd25519Hex: string, sigMlDsaHex: string, mlDsaLevel?: MlDsaLevel): HybridVerifyResult;
|
|
35
|
+
export declare function hybridSign(secretKeyEd25519Hex: string, secretKeyMlDsaHex: string, message: string | Uint8Array, mlDsaLevel?: MlDsaLevel, options?: HybridSignOptions): HybridSignResult;
|
|
36
|
+
export declare function hybridVerify(pubKeyEd25519Hex: string, pubKeyMlDsaHex: string, message: string | Uint8Array, sigEd25519Hex: string, sigMlDsaHex: string, mlDsaLevel?: MlDsaLevel, options?: HybridVerifyOptions): HybridVerifyResult;
|
|
30
37
|
//# sourceMappingURL=pq-sign.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pq-sign.d.ts","sourceRoot":"","sources":["../../src/modern/pq-sign.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"pq-sign.d.ts","sourceRoot":"","sources":["../../src/modern/pq-sign.ts"],"names":[],"mappings":"AA+BA,MAAM,MAAM,UAAU,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC;AAGtC,MAAM,MAAM,cAAc,GAAG,WAAW,GAAG,WAAW,GAAG,WAAW,CAAC;AAGrE,MAAM,WAAW,kBAAkB;IAEjC,SAAS,EAAE,MAAM,CAAC;IAElB,SAAS,EAAE,MAAM,CAAC;IAElB,SAAS,EAAE,cAAc,CAAC;CAC3B;AAGD,MAAM,WAAW,eAAe;IAE9B,SAAS,EAAE,MAAM,CAAC;IAElB,SAAS,EAAE,cAAc,CAAC;CAC3B;AAGD,MAAM,WAAW,iBAAiB;IAEhC,KAAK,EAAE,OAAO,CAAC;IAEf,SAAS,EAAE,cAAc,CAAC;CAC3B;AAGD,MAAM,WAAW,gBAAgB;IAE/B,gBAAgB,EAAE,MAAM,CAAC;IAEzB,cAAc,EAAE,MAAM,CAAC;IAKvB,SAAS,EAAE,MAAM,CAAC;IAElB,SAAS,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,iBAAiB;IAEhC,OAAO,CAAC,EAAE,MAAM,GAAG,UAAU,CAAC;CAC/B;AAGD,MAAM,WAAW,mBAAoB,SAAQ,iBAAiB;IAM5D,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB;AAGD,MAAM,WAAW,kBAAkB;IAEjC,KAAK,EAAE,OAAO,CAAC;IAEf,SAAS,EAAE,MAAM,CAAC;CACnB;AAgDD,wBAAgB,WAAW,CAAC,KAAK,EAAE,UAAU,GAAG,kBAAkB,CAQjE;AAKD,wBAAgB,SAAS,CACvB,KAAK,EAAE,UAAU,EACjB,YAAY,EAAE,MAAM,EACpB,OAAO,EAAE,MAAM,GAAG,UAAU,GAC3B,eAAe,CASjB;AAKD,wBAAgB,WAAW,CACzB,KAAK,EAAE,UAAU,EACjB,YAAY,EAAE,MAAM,EACpB,OAAO,EAAE,MAAM,GAAG,UAAU,EAC5B,YAAY,EAAE,MAAM,GACnB,iBAAiB,CAUnB;AA6DD,wBAAgB,UAAU,CACxB,mBAAmB,EAAE,MAAM,EAC3B,iBAAiB,EAAE,MAAM,EACzB,OAAO,EAAE,MAAM,GAAG,UAAU,EAC5B,UAAU,GAAE,UAAe,EAC3B,OAAO,GAAE,iBAAsB,GAC9B,gBAAgB,CAsBlB;AAeD,wBAAgB,YAAY,CAC1B,gBAAgB,EAAE,MAAM,EACxB,cAAc,EAAE,MAAM,EACtB,OAAO,EAAE,MAAM,GAAG,UAAU,EAC5B,aAAa,EAAE,MAAM,EACrB,WAAW,EAAE,MAAM,EACnB,UAAU,GAAE,UAAe,EAC3B,OAAO,GAAE,mBAAwB,GAChC,kBAAkB,CAqBpB"}
|
package/dist/modern/pq-sign.js
CHANGED
|
@@ -5,30 +5,32 @@ exports.mlDsaSign = mlDsaSign;
|
|
|
5
5
|
exports.mlDsaVerify = mlDsaVerify;
|
|
6
6
|
exports.hybridSign = hybridSign;
|
|
7
7
|
exports.hybridVerify = hybridVerify;
|
|
8
|
-
const
|
|
8
|
+
const pq_backend_1 = require("../accel/pq-backend");
|
|
9
9
|
const ed25519_js_1 = require("@noble/curves/ed25519.js");
|
|
10
|
-
const
|
|
10
|
+
const sha2_js_1 = require("@noble/hashes/sha2.js");
|
|
11
|
+
const errors_1 = require("../errors");
|
|
12
|
+
const encoding_1 = require("../encoding");
|
|
11
13
|
function assertHex(input, label) {
|
|
12
|
-
if (!
|
|
13
|
-
throw new
|
|
14
|
+
if (!(0, encoding_1.isHex)(input)) {
|
|
15
|
+
throw new errors_1.CryptoError(`Invalid hex string for ${label}`, errors_1.CryptoErrorCode.INVALID_HEX);
|
|
14
16
|
}
|
|
15
|
-
return
|
|
17
|
+
return (0, encoding_1.decodeHex)(input);
|
|
16
18
|
}
|
|
17
19
|
function toBytes(input) {
|
|
18
20
|
if (input instanceof Uint8Array)
|
|
19
21
|
return input;
|
|
20
|
-
return
|
|
22
|
+
return (0, encoding_1.utf8)(input);
|
|
21
23
|
}
|
|
22
24
|
function getDsa(level) {
|
|
23
25
|
switch (level) {
|
|
24
26
|
case 44:
|
|
25
|
-
return
|
|
27
|
+
return pq_backend_1.ml_dsa44;
|
|
26
28
|
case 65:
|
|
27
|
-
return
|
|
29
|
+
return pq_backend_1.ml_dsa65;
|
|
28
30
|
case 87:
|
|
29
|
-
return
|
|
31
|
+
return pq_backend_1.ml_dsa87;
|
|
30
32
|
default:
|
|
31
|
-
throw new
|
|
33
|
+
throw new errors_1.CryptoError(`Unsupported ML-DSA level: ${level}. Supported: 44, 65, 87`, errors_1.CryptoErrorCode.UNSUPPORTED_ALGORITHM);
|
|
32
34
|
}
|
|
33
35
|
}
|
|
34
36
|
function algorithmName(level) {
|
|
@@ -38,8 +40,8 @@ function mlDsaKeygen(level) {
|
|
|
38
40
|
const dsa = getDsa(level);
|
|
39
41
|
const { publicKey, secretKey } = dsa.keygen();
|
|
40
42
|
return {
|
|
41
|
-
publicKey:
|
|
42
|
-
secretKey:
|
|
43
|
+
publicKey: (0, encoding_1.toHex)(publicKey),
|
|
44
|
+
secretKey: (0, encoding_1.toHex)(secretKey),
|
|
43
45
|
algorithm: algorithmName(level),
|
|
44
46
|
};
|
|
45
47
|
}
|
|
@@ -49,7 +51,7 @@ function mlDsaSign(level, secretKeyHex, message) {
|
|
|
49
51
|
const msg = toBytes(message);
|
|
50
52
|
const signature = dsa.sign(msg, secretKey);
|
|
51
53
|
return {
|
|
52
|
-
signature:
|
|
54
|
+
signature: (0, encoding_1.toHex)(signature),
|
|
53
55
|
algorithm: algorithmName(level),
|
|
54
56
|
};
|
|
55
57
|
}
|
|
@@ -64,30 +66,65 @@ function mlDsaVerify(level, publicKeyHex, message, signatureHex) {
|
|
|
64
66
|
algorithm: algorithmName(level),
|
|
65
67
|
};
|
|
66
68
|
}
|
|
67
|
-
|
|
69
|
+
const COMPOSITE_PREFIX = new TextEncoder().encode("CompositeAlgorithmSignatures2025");
|
|
70
|
+
const COMPOSITE_LABELS = {
|
|
71
|
+
44: new TextEncoder().encode("COMPSIG-MLDSA44-Ed25519-SHA512"),
|
|
72
|
+
65: new TextEncoder().encode("COMPSIG-MLDSA65-Ed25519-SHA512"),
|
|
73
|
+
};
|
|
74
|
+
function compositeLabel(level) {
|
|
75
|
+
const label = COMPOSITE_LABELS[level];
|
|
76
|
+
if (!label) {
|
|
77
|
+
throw new errors_1.CryptoError("Composite ML-DSA defines no ML-DSA-87 + Ed25519 algorithm; use ML-DSA-44 or ML-DSA-65", errors_1.CryptoErrorCode.UNSUPPORTED_ALGORITHM);
|
|
78
|
+
}
|
|
79
|
+
return label;
|
|
80
|
+
}
|
|
81
|
+
function messageRepresentative(label, message, context) {
|
|
82
|
+
const ctx = toBytes(context ?? new Uint8Array(0));
|
|
83
|
+
if (ctx.length > 255) {
|
|
84
|
+
throw new errors_1.CryptoError("Hybrid signature context must be at most 255 bytes", errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
85
|
+
}
|
|
86
|
+
return Uint8Array.from((0, encoding_1.concat)([
|
|
87
|
+
COMPOSITE_PREFIX,
|
|
88
|
+
label,
|
|
89
|
+
new Uint8Array([ctx.length]),
|
|
90
|
+
ctx,
|
|
91
|
+
(0, sha2_js_1.sha512)(message),
|
|
92
|
+
]));
|
|
93
|
+
}
|
|
94
|
+
function hybridSign(secretKeyEd25519Hex, secretKeyMlDsaHex, message, mlDsaLevel = 65, options = {}) {
|
|
95
|
+
const label = compositeLabel(mlDsaLevel);
|
|
68
96
|
const dsa = getDsa(mlDsaLevel);
|
|
69
97
|
const ed25519Key = assertHex(secretKeyEd25519Hex, "secretKeyEd25519");
|
|
70
98
|
const mlDsaKey = assertHex(secretKeyMlDsaHex, "secretKeyMlDsa");
|
|
71
|
-
const
|
|
72
|
-
const
|
|
73
|
-
const
|
|
99
|
+
const mPrime = messageRepresentative(label, toBytes(message), options.context);
|
|
100
|
+
const mlDsaSig = dsa.sign(mPrime, mlDsaKey, { context: label });
|
|
101
|
+
const ed25519Sig = ed25519_js_1.ed25519.sign(mPrime, ed25519Key);
|
|
102
|
+
const mlDsaHex = (0, encoding_1.toHex)(mlDsaSig);
|
|
103
|
+
const ed25519Hex = (0, encoding_1.toHex)(ed25519Sig);
|
|
74
104
|
return {
|
|
75
|
-
ed25519Signature:
|
|
76
|
-
mlDsaSignature:
|
|
105
|
+
ed25519Signature: ed25519Hex,
|
|
106
|
+
mlDsaSignature: mlDsaHex,
|
|
107
|
+
signature: mlDsaHex + ed25519Hex,
|
|
77
108
|
algorithm: `ed25519-ml-dsa-${mlDsaLevel}`,
|
|
78
109
|
};
|
|
79
110
|
}
|
|
80
|
-
function hybridVerify(pubKeyEd25519Hex, pubKeyMlDsaHex, message, sigEd25519Hex, sigMlDsaHex, mlDsaLevel = 65) {
|
|
111
|
+
function hybridVerify(pubKeyEd25519Hex, pubKeyMlDsaHex, message, sigEd25519Hex, sigMlDsaHex, mlDsaLevel = 65, options = {}) {
|
|
81
112
|
const dsa = getDsa(mlDsaLevel);
|
|
82
113
|
const ed25519Pub = assertHex(pubKeyEd25519Hex, "pubKeyEd25519");
|
|
83
114
|
const mlDsaPub = assertHex(pubKeyMlDsaHex, "pubKeyMlDsa");
|
|
84
115
|
const msg = toBytes(message);
|
|
85
116
|
const ed25519Sig = assertHex(sigEd25519Hex, "sigEd25519");
|
|
86
117
|
const mlDsaSig = assertHex(sigMlDsaHex, "sigMlDsa");
|
|
87
|
-
const
|
|
88
|
-
|
|
118
|
+
const both = (signed, mlDsaOpts) => ed25519_js_1.ed25519.verify(ed25519Sig, signed, ed25519Pub, { zip215: false }) &&
|
|
119
|
+
dsa.verify(mlDsaSig, signed, mlDsaPub, mlDsaOpts);
|
|
120
|
+
const legacy = options.acceptLegacy === true && both(msg);
|
|
121
|
+
if (legacy || (options.acceptLegacy && !COMPOSITE_LABELS[mlDsaLevel])) {
|
|
122
|
+
return { valid: legacy, algorithm: `ed25519-ml-dsa-${mlDsaLevel}` };
|
|
123
|
+
}
|
|
124
|
+
const label = compositeLabel(mlDsaLevel);
|
|
125
|
+
const mPrime = messageRepresentative(label, msg, options.context);
|
|
89
126
|
return {
|
|
90
|
-
valid:
|
|
127
|
+
valid: both(mPrime, { context: label }),
|
|
91
128
|
algorithm: `ed25519-ml-dsa-${mlDsaLevel}`,
|
|
92
129
|
};
|
|
93
130
|
}
|
package/dist/modern/pq.d.ts
CHANGED
|
@@ -1,20 +1,3 @@
|
|
|
1
|
-
export interface MlKemKeyPair {
|
|
2
|
-
publicKey: string;
|
|
3
|
-
secretKey: string;
|
|
4
|
-
algorithm: "ml-kem-768";
|
|
5
|
-
}
|
|
6
|
-
export interface MlKemEncapsulateResult {
|
|
7
|
-
ciphertext: string;
|
|
8
|
-
sharedSecret: string;
|
|
9
|
-
algorithm: "ml-kem-768";
|
|
10
|
-
}
|
|
11
|
-
export interface MlKemDecapsulateResult {
|
|
12
|
-
sharedSecret: string;
|
|
13
|
-
algorithm: "ml-kem-768";
|
|
14
|
-
}
|
|
15
|
-
export declare function mlKemGenerateKeyPair(): MlKemKeyPair;
|
|
16
|
-
export declare function mlKemEncapsulate(publicKeyHex: string): MlKemEncapsulateResult;
|
|
17
|
-
export declare function mlKemDecapsulate(secretKeyHex: string, ciphertextHex: string): MlKemDecapsulateResult;
|
|
18
1
|
export interface HybridKeyPair {
|
|
19
2
|
x25519PrivateKey: string;
|
|
20
3
|
x25519PublicKey: string;
|
package/dist/modern/pq.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pq.d.ts","sourceRoot":"","sources":["../../src/modern/pq.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"pq.d.ts","sourceRoot":"","sources":["../../src/modern/pq.ts"],"names":[],"mappings":"AA4BA,MAAM,WAAW,aAAa;IAE5B,gBAAgB,EAAE,MAAM,CAAC;IAEzB,eAAe,EAAE,MAAM,CAAC;IAExB,cAAc,EAAE,MAAM,CAAC;IAEvB,cAAc,EAAE,MAAM,CAAC;IAEvB,SAAS,EAAE,mBAAmB,CAAC;CAChC;AAGD,MAAM,WAAW,uBAAuB;IAEtC,qBAAqB,EAAE,MAAM,CAAC;IAE9B,eAAe,EAAE,MAAM,CAAC;IAExB,YAAY,EAAE,MAAM,CAAC;IAErB,SAAS,EAAE,mBAAmB,CAAC;CAChC;AAGD,MAAM,WAAW,uBAAuB;IAEtC,YAAY,EAAE,MAAM,CAAC;IAErB,SAAS,EAAE,mBAAmB,CAAC;CAChC;AAOD,wBAAgB,qBAAqB,IAAI,aAAa,CAYrD;AAYD,wBAAgB,iBAAiB,CAC/B,iBAAiB,EAAE,MAAM,EACzB,gBAAgB,EAAE,MAAM,GACvB,uBAAuB,CAiCzB;AAaD,wBAAgB,iBAAiB,CAC/B,gBAAgB,EAAE,MAAM,EACxB,cAAc,EAAE,MAAM,EACtB,oBAAoB,EAAE,MAAM,EAC5B,eAAe,EAAE,MAAM,GACtB,uBAAuB,CA8BzB"}
|
package/dist/modern/pq.js
CHANGED
|
@@ -1,79 +1,51 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.mlKemGenerateKeyPair = mlKemGenerateKeyPair;
|
|
4
|
-
exports.mlKemEncapsulate = mlKemEncapsulate;
|
|
5
|
-
exports.mlKemDecapsulate = mlKemDecapsulate;
|
|
6
3
|
exports.hybridGenerateKeyPair = hybridGenerateKeyPair;
|
|
7
4
|
exports.hybridEncapsulate = hybridEncapsulate;
|
|
8
5
|
exports.hybridDecapsulate = hybridDecapsulate;
|
|
9
|
-
const
|
|
6
|
+
const pq_backend_1 = require("../accel/pq-backend");
|
|
10
7
|
const ed25519_js_1 = require("@noble/curves/ed25519.js");
|
|
11
8
|
const hkdf_js_1 = require("@noble/hashes/hkdf.js");
|
|
12
9
|
const sha2_js_1 = require("@noble/hashes/sha2.js");
|
|
13
10
|
const utils_js_1 = require("@noble/ciphers/utils.js");
|
|
14
|
-
|
|
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
|
-
}
|
|
11
|
+
const encoding_1 = require("../encoding");
|
|
40
12
|
function hybridGenerateKeyPair() {
|
|
41
13
|
const x25519Priv = (0, utils_js_1.randomBytes)(32);
|
|
42
14
|
const x25519Pub = ed25519_js_1.x25519.getPublicKey(x25519Priv);
|
|
43
|
-
const { publicKey: mlKemPub, secretKey: mlKemSec } =
|
|
15
|
+
const { publicKey: mlKemPub, secretKey: mlKemSec } = pq_backend_1.ml_kem768.keygen();
|
|
44
16
|
return {
|
|
45
|
-
x25519PrivateKey:
|
|
46
|
-
x25519PublicKey:
|
|
47
|
-
mlKemPublicKey:
|
|
48
|
-
mlKemSecretKey:
|
|
17
|
+
x25519PrivateKey: (0, encoding_1.toHex)(x25519Priv),
|
|
18
|
+
x25519PublicKey: (0, encoding_1.toHex)(x25519Pub),
|
|
19
|
+
mlKemPublicKey: (0, encoding_1.toHex)(mlKemPub),
|
|
20
|
+
mlKemSecretKey: (0, encoding_1.toHex)(mlKemSec),
|
|
49
21
|
algorithm: "x25519-ml-kem-768",
|
|
50
22
|
};
|
|
51
23
|
}
|
|
52
24
|
function hybridEncapsulate(theirX25519Public, theirMlKemPublic) {
|
|
53
25
|
const ephemeralPriv = (0, utils_js_1.randomBytes)(32);
|
|
54
26
|
const ephemeralPub = ed25519_js_1.x25519.getPublicKey(ephemeralPriv);
|
|
55
|
-
const x25519Shared = ed25519_js_1.x25519.getSharedSecret(ephemeralPriv,
|
|
56
|
-
const { cipherText, sharedSecret: mlKemShared } =
|
|
27
|
+
const x25519Shared = ed25519_js_1.x25519.getSharedSecret(ephemeralPriv, (0, encoding_1.decodeHex)(theirX25519Public));
|
|
28
|
+
const { cipherText, sharedSecret: mlKemShared } = pq_backend_1.ml_kem768.encapsulate((0, encoding_1.decodeHex)(theirMlKemPublic));
|
|
57
29
|
const combined = new Uint8Array(x25519Shared.length + mlKemShared.length);
|
|
58
30
|
combined.set(x25519Shared);
|
|
59
31
|
combined.set(mlKemShared, x25519Shared.length);
|
|
60
32
|
const derivedSecret = (0, hkdf_js_1.hkdf)(sha2_js_1.sha256, combined, undefined, new TextEncoder().encode("x25519-ml-kem-768-hybrid"), 32);
|
|
61
33
|
return {
|
|
62
|
-
x25519EphemeralPublic:
|
|
63
|
-
mlKemCiphertext:
|
|
64
|
-
sharedSecret:
|
|
34
|
+
x25519EphemeralPublic: (0, encoding_1.toHex)(ephemeralPub),
|
|
35
|
+
mlKemCiphertext: (0, encoding_1.toHex)(cipherText),
|
|
36
|
+
sharedSecret: (0, encoding_1.toHex)(derivedSecret),
|
|
65
37
|
algorithm: "x25519-ml-kem-768",
|
|
66
38
|
};
|
|
67
39
|
}
|
|
68
40
|
function hybridDecapsulate(ourX25519Private, ourMlKemSecret, theirX25519Ephemeral, mlKemCiphertext) {
|
|
69
|
-
const x25519Shared = ed25519_js_1.x25519.getSharedSecret(
|
|
70
|
-
const mlKemShared =
|
|
41
|
+
const x25519Shared = ed25519_js_1.x25519.getSharedSecret((0, encoding_1.decodeHex)(ourX25519Private), (0, encoding_1.decodeHex)(theirX25519Ephemeral));
|
|
42
|
+
const mlKemShared = pq_backend_1.ml_kem768.decapsulate((0, encoding_1.decodeHex)(mlKemCiphertext), (0, encoding_1.decodeHex)(ourMlKemSecret));
|
|
71
43
|
const combined = new Uint8Array(x25519Shared.length + mlKemShared.length);
|
|
72
44
|
combined.set(x25519Shared);
|
|
73
45
|
combined.set(mlKemShared, x25519Shared.length);
|
|
74
46
|
const derivedSecret = (0, hkdf_js_1.hkdf)(sha2_js_1.sha256, combined, undefined, new TextEncoder().encode("x25519-ml-kem-768-hybrid"), 32);
|
|
75
47
|
return {
|
|
76
|
-
sharedSecret:
|
|
48
|
+
sharedSecret: (0, encoding_1.toHex)(derivedSecret),
|
|
77
49
|
algorithm: "x25519-ml-kem-768",
|
|
78
50
|
};
|
|
79
51
|
}
|
package/dist/modern/signing.d.ts
CHANGED
|
@@ -10,7 +10,8 @@ export interface VerifyResult {
|
|
|
10
10
|
valid: boolean;
|
|
11
11
|
algorithm: "ed25519";
|
|
12
12
|
}
|
|
13
|
-
export declare function
|
|
13
|
+
export declare function ed25519Keygen(): Ed25519KeyPair;
|
|
14
|
+
export declare const generateEd25519KeyPair: typeof ed25519Keygen;
|
|
14
15
|
export declare function ed25519Sign(privateKey: string | Uint8Array, message: string | Uint8Array): SignResult;
|
|
15
16
|
export declare function ed25519Verify(publicKey: string | Uint8Array, message: string | Uint8Array, signature: string | Uint8Array): VerifyResult;
|
|
16
17
|
//# sourceMappingURL=signing.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"signing.d.ts","sourceRoot":"","sources":["../../src/modern/signing.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"signing.d.ts","sourceRoot":"","sources":["../../src/modern/signing.ts"],"names":[],"mappings":"AAoBA,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,aAAa,IAAI,cAAc,CAQ9C;AAGD,eAAO,MAAM,sBAAsB,sBAAgB,CAAC;AAKpD,wBAAgB,WAAW,CACzB,UAAU,EAAE,MAAM,GAAG,UAAU,EAC/B,OAAO,EAAE,MAAM,GAAG,UAAU,GAC3B,UAAU,CAWZ;AAKD,wBAAgB,aAAa,CAC3B,SAAS,EAAE,MAAM,GAAG,UAAU,EAC9B,OAAO,EAAE,MAAM,GAAG,UAAU,EAC5B,SAAS,EAAE,MAAM,GAAG,UAAU,GAC7B,YAAY,CAUd"}
|