@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.
- package/README.md +304 -314
- package/dist/accel/index.d.ts +11 -0
- package/dist/accel/index.d.ts.map +1 -0
- package/dist/accel/index.js +31 -0
- package/dist/accel/native-pqc-bridge.d.ts +18 -0
- package/dist/accel/native-pqc-bridge.d.ts.map +1 -0
- package/dist/accel/native-pqc-bridge.js +100 -0
- package/dist/accel/wasm-bridge.d.ts +35 -0
- package/dist/accel/wasm-bridge.d.ts.map +1 -0
- package/dist/accel/wasm-bridge.js +161 -0
- package/dist/accel/webcrypto-bridge.d.ts +32 -0
- package/dist/accel/webcrypto-bridge.d.ts.map +1 -0
- package/dist/accel/webcrypto-bridge.js +179 -0
- package/dist/accel/webcrypto-modern.d.ts +40 -0
- package/dist/accel/webcrypto-modern.d.ts.map +1 -0
- package/dist/accel/webcrypto-modern.js +213 -0
- package/dist/accel/worker-pool.d.ts +23 -0
- package/dist/accel/worker-pool.d.ts.map +1 -0
- package/dist/accel/worker-pool.js +119 -0
- package/dist/bin/cryptolib.d.ts +9 -7
- package/dist/bin/cryptolib.d.ts.map +1 -1
- package/dist/bin/cryptolib.js +25 -16
- package/dist/config/config.d.ts.map +1 -1
- package/dist/crypto.d.ts +43 -0
- package/dist/crypto.d.ts.map +1 -0
- package/dist/crypto.js +107 -0
- package/dist/enums.d.ts +29 -29
- package/dist/enums.d.ts.map +1 -1
- package/dist/enums.js +54 -54
- package/dist/errors.d.ts +14 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +22 -0
- package/dist/high-level/index.d.ts +11 -0
- package/dist/high-level/index.d.ts.map +1 -0
- package/dist/high-level/index.js +53 -0
- package/dist/high-level/key-wrap.d.ts +16 -0
- package/dist/high-level/key-wrap.d.ts.map +1 -0
- package/dist/high-level/key-wrap.js +93 -0
- package/dist/high-level/multi-recipient.d.ts +25 -0
- package/dist/high-level/multi-recipient.d.ts.map +1 -0
- package/dist/high-level/multi-recipient.js +122 -0
- package/dist/high-level/password-encrypt.d.ts +14 -0
- package/dist/high-level/password-encrypt.d.ts.map +1 -0
- package/dist/high-level/password-encrypt.js +93 -0
- package/dist/high-level/sealedbox.d.ts +13 -0
- package/dist/high-level/sealedbox.d.ts.map +1 -0
- package/dist/high-level/sealedbox.js +119 -0
- package/dist/high-level/secretbox.d.ts +7 -0
- package/dist/high-level/secretbox.d.ts.map +1 -0
- package/dist/high-level/secretbox.js +54 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +59 -0
- package/dist/key/key.d.ts +6 -13
- package/dist/key/key.d.ts.map +1 -1
- package/dist/key/key.js +20 -14
- package/dist/key/keystore.d.ts +11 -0
- package/dist/key/keystore.d.ts.map +1 -0
- package/dist/key/keystore.js +87 -0
- package/dist/keys/index.d.ts +7 -0
- package/dist/keys/index.d.ts.map +1 -0
- package/dist/keys/index.js +22 -0
- package/dist/keys/keygen.d.ts +16 -0
- package/dist/keys/keygen.d.ts.map +1 -0
- package/dist/keys/keygen.js +128 -0
- package/dist/keys/keyring.d.ts +35 -0
- package/dist/keys/keyring.d.ts.map +1 -0
- package/dist/keys/keyring.js +136 -0
- package/dist/keys/serialize.d.ts +30 -0
- package/dist/keys/serialize.d.ts.map +1 -0
- package/dist/keys/serialize.js +120 -0
- package/dist/lib/decrypt.d.ts +4 -1
- package/dist/lib/decrypt.d.ts.map +1 -1
- package/dist/lib/decrypt.js +43 -51
- package/dist/lib/encrypt.d.ts +1 -1
- package/dist/lib/encrypt.d.ts.map +1 -1
- package/dist/lib/encrypt.js +29 -45
- package/dist/lib/generate.d.ts.map +1 -1
- package/dist/lib/generate.js +56 -63
- package/dist/lib/index.d.ts.map +1 -1
- package/dist/lib/index.js +17 -7
- package/dist/lib/key-output.d.ts +7 -0
- package/dist/lib/key-output.d.ts.map +1 -0
- package/dist/lib/key-output.js +50 -0
- package/dist/lib/reformat.d.ts +6 -1
- package/dist/lib/reformat.d.ts.map +1 -1
- package/dist/lib/reformat.js +37 -61
- package/dist/lib/revoke.d.ts +2 -2
- package/dist/lib/revoke.d.ts.map +1 -1
- package/dist/lib/revoke.js +33 -50
- package/dist/lib/session.d.ts.map +1 -1
- package/dist/lib/session.js +21 -36
- package/dist/lib/sign.d.ts.map +1 -1
- package/dist/lib/sign.js +36 -43
- package/dist/lib/verify.d.ts +1 -3
- package/dist/lib/verify.d.ts.map +1 -1
- package/dist/lib/verify.js +25 -30
- package/dist/modern/aead.d.ts +17 -0
- package/dist/modern/aead.d.ts.map +1 -0
- package/dist/modern/aead.js +51 -0
- package/dist/modern/aes.d.ts +35 -0
- package/dist/modern/aes.d.ts.map +1 -0
- package/dist/modern/aes.js +82 -0
- package/dist/modern/cost-limits.d.ts +11 -0
- package/dist/modern/cost-limits.d.ts.map +1 -0
- package/dist/modern/cost-limits.js +35 -0
- package/dist/modern/curves.d.ts +86 -0
- package/dist/modern/curves.d.ts.map +1 -0
- package/dist/modern/curves.js +171 -0
- package/dist/modern/ecdh.d.ts +11 -0
- package/dist/modern/ecdh.d.ts.map +1 -0
- package/dist/modern/ecdh.js +28 -0
- package/dist/modern/fn-dsa.d.ts +19 -0
- package/dist/modern/fn-dsa.d.ts.map +1 -0
- package/dist/modern/fn-dsa.js +57 -0
- package/dist/modern/hash.d.ts +13 -0
- package/dist/modern/hash.d.ts.map +1 -0
- package/dist/modern/hash.js +45 -0
- package/dist/modern/hpke.d.ts +47 -0
- package/dist/modern/hpke.d.ts.map +1 -0
- package/dist/modern/hpke.js +207 -0
- package/dist/modern/index.d.ts +40 -0
- package/dist/modern/index.d.ts.map +1 -0
- package/dist/modern/index.js +194 -0
- package/dist/modern/kdf.d.ts +23 -0
- package/dist/modern/kdf.d.ts.map +1 -0
- package/dist/modern/kdf.js +67 -0
- package/dist/modern/mac.d.ts +51 -0
- package/dist/modern/mac.d.ts.map +1 -0
- package/dist/modern/mac.js +119 -0
- package/dist/modern/password.d.ts +40 -0
- package/dist/modern/password.d.ts.map +1 -0
- package/dist/modern/password.js +123 -0
- package/dist/modern/pq-hash-sign.d.ts +18 -0
- package/dist/modern/pq-hash-sign.d.ts.map +1 -0
- package/dist/modern/pq-hash-sign.js +73 -0
- package/dist/modern/pq-kem.d.ts +88 -0
- package/dist/modern/pq-kem.d.ts.map +1 -0
- package/dist/modern/pq-kem.js +270 -0
- package/dist/modern/pq-sign.d.ts +30 -0
- package/dist/modern/pq-sign.d.ts.map +1 -0
- package/dist/modern/pq-sign.js +94 -0
- package/dist/modern/pq.d.ts +38 -0
- package/dist/modern/pq.d.ts.map +1 -0
- package/dist/modern/pq.js +80 -0
- package/dist/modern/signing.d.ts +16 -0
- package/dist/modern/signing.d.ts.map +1 -0
- package/dist/modern/signing.js +34 -0
- package/dist/protocols/index.d.ts +7 -0
- package/dist/protocols/index.d.ts.map +1 -0
- package/dist/protocols/index.js +46 -0
- package/dist/protocols/iso20022.d.ts +51 -0
- package/dist/protocols/iso20022.d.ts.map +1 -0
- package/dist/protocols/iso20022.js +145 -0
- package/dist/protocols/opaque/ake.d.ts +51 -0
- package/dist/protocols/opaque/ake.d.ts.map +1 -0
- package/dist/protocols/opaque/ake.js +102 -0
- package/dist/protocols/opaque/client.d.ts +45 -0
- package/dist/protocols/opaque/client.d.ts.map +1 -0
- package/dist/protocols/opaque/client.js +63 -0
- package/dist/protocols/opaque/config.d.ts +14 -0
- package/dist/protocols/opaque/config.d.ts.map +1 -0
- package/dist/protocols/opaque/config.js +18 -0
- package/dist/protocols/opaque/credentials.d.ts +29 -0
- package/dist/protocols/opaque/credentials.d.ts.map +1 -0
- package/dist/protocols/opaque/credentials.js +61 -0
- package/dist/protocols/opaque/envelope.d.ts +36 -0
- package/dist/protocols/opaque/envelope.d.ts.map +1 -0
- package/dist/protocols/opaque/envelope.js +77 -0
- package/dist/protocols/opaque/ksf.d.ts +15 -0
- package/dist/protocols/opaque/ksf.d.ts.map +1 -0
- package/dist/protocols/opaque/ksf.js +27 -0
- package/dist/protocols/opaque/messages.d.ts +61 -0
- package/dist/protocols/opaque/messages.d.ts.map +1 -0
- package/dist/protocols/opaque/messages.js +94 -0
- package/dist/protocols/opaque/server.d.ts +33 -0
- package/dist/protocols/opaque/server.d.ts.map +1 -0
- package/dist/protocols/opaque/server.js +74 -0
- package/dist/protocols/opaque/suite.d.ts +58 -0
- package/dist/protocols/opaque/suite.d.ts.map +1 -0
- package/dist/protocols/opaque/suite.js +115 -0
- package/dist/protocols/pake.d.ts +14 -0
- package/dist/protocols/pake.d.ts.map +1 -0
- package/dist/protocols/pake.js +33 -0
- package/dist/protocols/pqxdh.d.ts +53 -0
- package/dist/protocols/pqxdh.d.ts.map +1 -0
- package/dist/protocols/pqxdh.js +143 -0
- package/dist/protocols/ratchet.d.ts +52 -0
- package/dist/protocols/ratchet.d.ts.map +1 -0
- package/dist/protocols/ratchet.js +187 -0
- package/dist/protocols/threshold.d.ts +23 -0
- package/dist/protocols/threshold.d.ts.map +1 -0
- package/dist/protocols/threshold.js +191 -0
- package/dist/registry.d.ts +20 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +399 -0
- package/dist/streaming/index.d.ts +7 -0
- package/dist/streaming/index.d.ts.map +1 -0
- package/dist/streaming/index.js +15 -0
- package/dist/streaming/stream-aead.d.ts +17 -0
- package/dist/streaming/stream-aead.d.ts.map +1 -0
- package/dist/streaming/stream-aead.js +125 -0
- package/dist/streaming/stream-hash.d.ts +9 -0
- package/dist/streaming/stream-hash.d.ts.map +1 -0
- package/dist/streaming/stream-hash.js +52 -0
- package/dist/streaming/web-streams.d.ts +17 -0
- package/dist/streaming/web-streams.d.ts.map +1 -0
- package/dist/streaming/web-streams.js +114 -0
- package/dist/tokens/index.d.ts +5 -0
- package/dist/tokens/index.d.ts.map +1 -0
- package/dist/tokens/index.js +15 -0
- package/dist/tokens/key-rotation.d.ts +20 -0
- package/dist/tokens/key-rotation.d.ts.map +1 -0
- package/dist/tokens/key-rotation.js +76 -0
- package/dist/tokens/paseto.d.ts +46 -0
- package/dist/tokens/paseto.d.ts.map +1 -0
- package/dist/tokens/paseto.js +200 -0
- package/dist/types/types.d.ts +24 -16
- package/dist/types/types.d.ts.map +1 -1
- package/dist/utils.d.ts +13 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +56 -0
- package/package.json +77 -142
- package/dist/COPYRIGHT +0 -15
- package/dist/Report.txt +0 -160
- package/dist/bin/cryptolib.js.map +0 -1
- package/dist/config/config.js.map +0 -1
- package/dist/data/decrypted.txt +0 -1
- package/dist/data/detached.sig +0 -1
- package/dist/data/encrypted.txt +0 -1
- package/dist/data/signed.sig +0 -1
- package/dist/enums.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/key/key.js.map +0 -1
- package/dist/key/rsa-reformat.cert +0 -1
- package/dist/key/rsa-reformat.key +0 -1
- package/dist/key/rsa-reformat.pub +0 -1
- package/dist/key/rsa-revoke.key +0 -1
- package/dist/key/rsa-revoke.pub +0 -1
- package/dist/key/rsa.cert +0 -1
- package/dist/key/rsa.key +0 -1
- package/dist/key/rsa.pub +0 -1
- package/dist/lib/decrypt.js.map +0 -1
- package/dist/lib/encrypt.js.map +0 -1
- package/dist/lib/generate.js.map +0 -1
- package/dist/lib/index.js.map +0 -1
- package/dist/lib/reformat.js.map +0 -1
- package/dist/lib/revoke.js.map +0 -1
- package/dist/lib/session.js.map +0 -1
- package/dist/lib/sign.js.map +0 -1
- package/dist/lib/verify.js.map +0 -1
- package/dist/package.json +0 -143
- package/dist/types/types.js.map +0 -1
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateP256KeyPair = generateP256KeyPair;
|
|
4
|
+
exports.p256Sign = p256Sign;
|
|
5
|
+
exports.p256Verify = p256Verify;
|
|
6
|
+
exports.generateP384KeyPair = generateP384KeyPair;
|
|
7
|
+
exports.p384Sign = p384Sign;
|
|
8
|
+
exports.p384Verify = p384Verify;
|
|
9
|
+
exports.generateEd448KeyPair = generateEd448KeyPair;
|
|
10
|
+
exports.ed448Sign = ed448Sign;
|
|
11
|
+
exports.ed448Verify = ed448Verify;
|
|
12
|
+
exports.generateX448KeyPair = generateX448KeyPair;
|
|
13
|
+
exports.x448Exchange = x448Exchange;
|
|
14
|
+
exports.ecdhP256 = ecdhP256;
|
|
15
|
+
exports.ecdhP384 = ecdhP384;
|
|
16
|
+
exports.generateSchnorrKeyPair = generateSchnorrKeyPair;
|
|
17
|
+
exports.schnorrSign = schnorrSign;
|
|
18
|
+
exports.schnorrVerify = schnorrVerify;
|
|
19
|
+
const nist_js_1 = require("@noble/curves/nist.js");
|
|
20
|
+
const ed448_js_1 = require("@noble/curves/ed448.js");
|
|
21
|
+
const secp256k1_js_1 = require("@noble/curves/secp256k1.js");
|
|
22
|
+
const utils_js_1 = require("@noble/ciphers/utils.js");
|
|
23
|
+
const HEX_RE = /^[0-9a-fA-F]*$/;
|
|
24
|
+
function assertHex(input, label) {
|
|
25
|
+
if (!HEX_RE.test(input)) {
|
|
26
|
+
throw new Error(`Invalid hex string for ${label}`);
|
|
27
|
+
}
|
|
28
|
+
return Buffer.from(input, "hex");
|
|
29
|
+
}
|
|
30
|
+
function toBytes(input) {
|
|
31
|
+
if (input instanceof Uint8Array)
|
|
32
|
+
return input;
|
|
33
|
+
return Buffer.from(input, "utf8");
|
|
34
|
+
}
|
|
35
|
+
function generateP256KeyPair() {
|
|
36
|
+
const privateKey = nist_js_1.p256.utils.randomSecretKey();
|
|
37
|
+
const publicKey = nist_js_1.p256.getPublicKey(privateKey, false);
|
|
38
|
+
return {
|
|
39
|
+
privateKey: Buffer.from(privateKey).toString("hex"),
|
|
40
|
+
publicKey: Buffer.from(publicKey).toString("hex"),
|
|
41
|
+
algorithm: "ecdsa-p256",
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function p256Sign(privateKeyHex, message) {
|
|
45
|
+
const key = assertHex(privateKeyHex, "privateKey");
|
|
46
|
+
const msg = toBytes(message);
|
|
47
|
+
const signature = nist_js_1.p256.sign(msg, key);
|
|
48
|
+
return {
|
|
49
|
+
signature: Buffer.from(signature).toString("hex"),
|
|
50
|
+
algorithm: "ecdsa-p256",
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function p256Verify(publicKeyHex, message, signatureHex) {
|
|
54
|
+
const key = assertHex(publicKeyHex, "publicKey");
|
|
55
|
+
const msg = toBytes(message);
|
|
56
|
+
const sig = assertHex(signatureHex, "signature");
|
|
57
|
+
const valid = nist_js_1.p256.verify(sig, msg, key);
|
|
58
|
+
return { valid, algorithm: "ecdsa-p256" };
|
|
59
|
+
}
|
|
60
|
+
function generateP384KeyPair() {
|
|
61
|
+
const privateKey = nist_js_1.p384.utils.randomSecretKey();
|
|
62
|
+
const publicKey = nist_js_1.p384.getPublicKey(privateKey, false);
|
|
63
|
+
return {
|
|
64
|
+
privateKey: Buffer.from(privateKey).toString("hex"),
|
|
65
|
+
publicKey: Buffer.from(publicKey).toString("hex"),
|
|
66
|
+
algorithm: "ecdsa-p384",
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function p384Sign(privateKeyHex, message) {
|
|
70
|
+
const key = assertHex(privateKeyHex, "privateKey");
|
|
71
|
+
const msg = toBytes(message);
|
|
72
|
+
const signature = nist_js_1.p384.sign(msg, key);
|
|
73
|
+
return {
|
|
74
|
+
signature: Buffer.from(signature).toString("hex"),
|
|
75
|
+
algorithm: "ecdsa-p384",
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function p384Verify(publicKeyHex, message, signatureHex) {
|
|
79
|
+
const key = assertHex(publicKeyHex, "publicKey");
|
|
80
|
+
const msg = toBytes(message);
|
|
81
|
+
const sig = assertHex(signatureHex, "signature");
|
|
82
|
+
const valid = nist_js_1.p384.verify(sig, msg, key);
|
|
83
|
+
return { valid, algorithm: "ecdsa-p384" };
|
|
84
|
+
}
|
|
85
|
+
function generateEd448KeyPair() {
|
|
86
|
+
const privateKey = (0, utils_js_1.randomBytes)(57);
|
|
87
|
+
const publicKey = ed448_js_1.ed448.getPublicKey(privateKey);
|
|
88
|
+
return {
|
|
89
|
+
privateKey: Buffer.from(privateKey).toString("hex"),
|
|
90
|
+
publicKey: Buffer.from(publicKey).toString("hex"),
|
|
91
|
+
algorithm: "ed448",
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function ed448Sign(privateKeyHex, message) {
|
|
95
|
+
const key = assertHex(privateKeyHex, "privateKey");
|
|
96
|
+
const msg = toBytes(message);
|
|
97
|
+
const signature = ed448_js_1.ed448.sign(msg, key);
|
|
98
|
+
return {
|
|
99
|
+
signature: Buffer.from(signature).toString("hex"),
|
|
100
|
+
algorithm: "ed448",
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function ed448Verify(publicKeyHex, message, signatureHex) {
|
|
104
|
+
const key = assertHex(publicKeyHex, "publicKey");
|
|
105
|
+
const msg = toBytes(message);
|
|
106
|
+
const sig = assertHex(signatureHex, "signature");
|
|
107
|
+
const valid = ed448_js_1.ed448.verify(sig, msg, key);
|
|
108
|
+
return { valid, algorithm: "ed448" };
|
|
109
|
+
}
|
|
110
|
+
function generateX448KeyPair() {
|
|
111
|
+
const privateKey = ed448_js_1.x448.utils.randomSecretKey();
|
|
112
|
+
const publicKey = ed448_js_1.x448.getPublicKey(privateKey);
|
|
113
|
+
return {
|
|
114
|
+
privateKey: Buffer.from(privateKey).toString("hex"),
|
|
115
|
+
publicKey: Buffer.from(publicKey).toString("hex"),
|
|
116
|
+
algorithm: "x448",
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function x448Exchange(privateKeyHex, theirPublicKeyHex) {
|
|
120
|
+
const priv = assertHex(privateKeyHex, "privateKey");
|
|
121
|
+
const pub = assertHex(theirPublicKeyHex, "theirPublicKey");
|
|
122
|
+
const shared = ed448_js_1.x448.getSharedSecret(priv, pub);
|
|
123
|
+
return {
|
|
124
|
+
sharedSecret: Buffer.from(shared).toString("hex"),
|
|
125
|
+
algorithm: "x448",
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function ecdhP256(privateKeyHex, theirPublicKeyHex) {
|
|
129
|
+
const priv = assertHex(privateKeyHex, "privateKey");
|
|
130
|
+
const pub = assertHex(theirPublicKeyHex, "theirPublicKey");
|
|
131
|
+
const shared = nist_js_1.p256.getSharedSecret(priv, pub);
|
|
132
|
+
return {
|
|
133
|
+
sharedSecret: Buffer.from(shared).toString("hex"),
|
|
134
|
+
algorithm: "ecdh-p256",
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function ecdhP384(privateKeyHex, theirPublicKeyHex) {
|
|
138
|
+
const priv = assertHex(privateKeyHex, "privateKey");
|
|
139
|
+
const pub = assertHex(theirPublicKeyHex, "theirPublicKey");
|
|
140
|
+
const shared = nist_js_1.p384.getSharedSecret(priv, pub);
|
|
141
|
+
return {
|
|
142
|
+
sharedSecret: Buffer.from(shared).toString("hex"),
|
|
143
|
+
algorithm: "ecdh-p384",
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function generateSchnorrKeyPair() {
|
|
147
|
+
const privateKey = (0, utils_js_1.randomBytes)(32);
|
|
148
|
+
const publicKey = secp256k1_js_1.schnorr.getPublicKey(privateKey);
|
|
149
|
+
return {
|
|
150
|
+
privateKey: Buffer.from(privateKey).toString("hex"),
|
|
151
|
+
publicKey: Buffer.from(publicKey).toString("hex"),
|
|
152
|
+
algorithm: "schnorr",
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function schnorrSign(privateKeyHex, message) {
|
|
156
|
+
const key = assertHex(privateKeyHex, "privateKey");
|
|
157
|
+
const msg = toBytes(message);
|
|
158
|
+
const signature = secp256k1_js_1.schnorr.sign(msg, key);
|
|
159
|
+
return {
|
|
160
|
+
signature: Buffer.from(signature).toString("hex"),
|
|
161
|
+
algorithm: "schnorr",
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function schnorrVerify(publicKeyHex, message, signatureHex) {
|
|
165
|
+
const key = assertHex(publicKeyHex, "publicKey");
|
|
166
|
+
const msg = toBytes(message);
|
|
167
|
+
const sig = assertHex(signatureHex, "signature");
|
|
168
|
+
const valid = secp256k1_js_1.schnorr.verify(sig, msg, key);
|
|
169
|
+
return { valid, algorithm: "schnorr" };
|
|
170
|
+
}
|
|
171
|
+
//# sourceMappingURL=curves.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface X25519KeyPair {
|
|
2
|
+
privateKey: string;
|
|
3
|
+
publicKey: string;
|
|
4
|
+
}
|
|
5
|
+
export interface KeyExchangeResult {
|
|
6
|
+
sharedSecret: string;
|
|
7
|
+
algorithm: "x25519";
|
|
8
|
+
}
|
|
9
|
+
export declare function generateX25519KeyPair(): X25519KeyPair;
|
|
10
|
+
export declare function x25519Exchange(privateKey: string | Uint8Array, theirPublicKey: string | Uint8Array): KeyExchangeResult;
|
|
11
|
+
//# sourceMappingURL=ecdh.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ecdh.d.ts","sourceRoot":"","sources":["../../src/modern/ecdh.ts"],"names":[],"mappings":"AAiBA,MAAM,WAAW,aAAa;IAE5B,UAAU,EAAE,MAAM,CAAC;IAEnB,SAAS,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,iBAAiB;IAEhC,YAAY,EAAE,MAAM,CAAC;IAErB,SAAS,EAAE,QAAQ,CAAC;CACrB;AAKD,wBAAgB,qBAAqB,IAAI,aAAa,CAQrD;AASD,wBAAgB,cAAc,CAC5B,UAAU,EAAE,MAAM,GAAG,UAAU,EAC/B,cAAc,EAAE,MAAM,GAAG,UAAU,GAClC,iBAAiB,CAgBnB"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateX25519KeyPair = generateX25519KeyPair;
|
|
4
|
+
exports.x25519Exchange = x25519Exchange;
|
|
5
|
+
const ed25519_js_1 = require("@noble/curves/ed25519.js");
|
|
6
|
+
const utils_js_1 = require("@noble/ciphers/utils.js");
|
|
7
|
+
function generateX25519KeyPair() {
|
|
8
|
+
const privateKey = (0, utils_js_1.randomBytes)(32);
|
|
9
|
+
const publicKey = ed25519_js_1.x25519.getPublicKey(privateKey);
|
|
10
|
+
return {
|
|
11
|
+
privateKey: Buffer.from(privateKey).toString("hex"),
|
|
12
|
+
publicKey: Buffer.from(publicKey).toString("hex"),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function x25519Exchange(privateKey, theirPublicKey) {
|
|
16
|
+
const priv = typeof privateKey === "string"
|
|
17
|
+
? Buffer.from(privateKey, "hex")
|
|
18
|
+
: privateKey;
|
|
19
|
+
const pub = typeof theirPublicKey === "string"
|
|
20
|
+
? Buffer.from(theirPublicKey, "hex")
|
|
21
|
+
: theirPublicKey;
|
|
22
|
+
const shared = ed25519_js_1.x25519.getSharedSecret(priv, pub);
|
|
23
|
+
return {
|
|
24
|
+
sharedSecret: Buffer.from(shared).toString("hex"),
|
|
25
|
+
algorithm: "x25519",
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=ecdh.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type FnDsaLevel = 512 | 1024;
|
|
2
|
+
export type FnDsaAlgorithm = "fn-dsa-512" | "fn-dsa-1024";
|
|
3
|
+
export interface FnDsaKeyPairResult {
|
|
4
|
+
publicKey: string;
|
|
5
|
+
secretKey: string;
|
|
6
|
+
algorithm: FnDsaAlgorithm;
|
|
7
|
+
}
|
|
8
|
+
export interface FnDsaSignResult {
|
|
9
|
+
signature: string;
|
|
10
|
+
algorithm: FnDsaAlgorithm;
|
|
11
|
+
}
|
|
12
|
+
export interface FnDsaVerifyResult {
|
|
13
|
+
valid: boolean;
|
|
14
|
+
algorithm: FnDsaAlgorithm;
|
|
15
|
+
}
|
|
16
|
+
export declare function fnDsaKeygen(level: FnDsaLevel): FnDsaKeyPairResult;
|
|
17
|
+
export declare function fnDsaSign(level: FnDsaLevel, secretKeyHex: string, messageHex: string): FnDsaSignResult;
|
|
18
|
+
export declare function fnDsaVerify(level: FnDsaLevel, publicKeyHex: string, messageHex: string, signatureHex: string): FnDsaVerifyResult;
|
|
19
|
+
//# sourceMappingURL=fn-dsa.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fn-dsa.d.ts","sourceRoot":"","sources":["../../src/modern/fn-dsa.ts"],"names":[],"mappings":"AAyBA,MAAM,MAAM,UAAU,GAAG,GAAG,GAAG,IAAI,CAAC;AAUpC,MAAM,MAAM,cAAc,GAAG,YAAY,GAAG,aAAa,CAAC;AAW1D,MAAM,WAAW,kBAAkB;IAEjC,SAAS,EAAE,MAAM,CAAC;IAElB,SAAS,EAAE,MAAM,CAAC;IAElB,SAAS,EAAE,cAAc,CAAC;CAC3B;AAWD,MAAM,WAAW,eAAe;IAE9B,SAAS,EAAE,MAAM,CAAC;IAElB,SAAS,EAAE,cAAc,CAAC;CAC3B;AAWD,MAAM,WAAW,iBAAiB;IAEhC,KAAK,EAAE,OAAO,CAAC;IAEf,SAAS,EAAE,cAAc,CAAC;CAC3B;AAkDD,wBAAgB,WAAW,CAAC,KAAK,EAAE,UAAU,GAAG,kBAAkB,CAQjE;AAkBD,wBAAgB,SAAS,CACvB,KAAK,EAAE,UAAU,EACjB,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,MAAM,GACjB,eAAe,CASjB;AAoBD,wBAAgB,WAAW,CACzB,KAAK,EAAE,UAAU,EACjB,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,MAAM,GACnB,iBAAiB,CAUnB"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fnDsaKeygen = fnDsaKeygen;
|
|
4
|
+
exports.fnDsaSign = fnDsaSign;
|
|
5
|
+
exports.fnDsaVerify = fnDsaVerify;
|
|
6
|
+
const falcon_js_1 = require("@noble/post-quantum/falcon.js");
|
|
7
|
+
const HEX_RE = /^[0-9a-fA-F]*$/;
|
|
8
|
+
function assertHex(input, label) {
|
|
9
|
+
if (!HEX_RE.test(input)) {
|
|
10
|
+
throw new Error(`Invalid hex string for ${label}`);
|
|
11
|
+
}
|
|
12
|
+
return Buffer.from(input, "hex");
|
|
13
|
+
}
|
|
14
|
+
function getFalcon(level) {
|
|
15
|
+
switch (level) {
|
|
16
|
+
case 512:
|
|
17
|
+
return falcon_js_1.falcon512;
|
|
18
|
+
case 1024:
|
|
19
|
+
return falcon_js_1.falcon1024;
|
|
20
|
+
default:
|
|
21
|
+
throw new Error(`Unsupported FN-DSA level: ${level}. Supported: 512, 1024`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function algorithmName(level) {
|
|
25
|
+
return `fn-dsa-${level}`;
|
|
26
|
+
}
|
|
27
|
+
function fnDsaKeygen(level) {
|
|
28
|
+
const falcon = getFalcon(level);
|
|
29
|
+
const { publicKey, secretKey } = falcon.keygen();
|
|
30
|
+
return {
|
|
31
|
+
publicKey: Buffer.from(publicKey).toString("hex"),
|
|
32
|
+
secretKey: Buffer.from(secretKey).toString("hex"),
|
|
33
|
+
algorithm: algorithmName(level),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function fnDsaSign(level, secretKeyHex, messageHex) {
|
|
37
|
+
const falcon = getFalcon(level);
|
|
38
|
+
const secretKey = assertHex(secretKeyHex, "secretKey");
|
|
39
|
+
const msg = assertHex(messageHex, "message");
|
|
40
|
+
const signature = falcon.sign(msg, secretKey);
|
|
41
|
+
return {
|
|
42
|
+
signature: Buffer.from(signature).toString("hex"),
|
|
43
|
+
algorithm: algorithmName(level),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function fnDsaVerify(level, publicKeyHex, messageHex, signatureHex) {
|
|
47
|
+
const falcon = getFalcon(level);
|
|
48
|
+
const publicKey = assertHex(publicKeyHex, "publicKey");
|
|
49
|
+
const msg = assertHex(messageHex, "message");
|
|
50
|
+
const signature = assertHex(signatureHex, "signature");
|
|
51
|
+
const valid = falcon.verify(signature, msg, publicKey);
|
|
52
|
+
return {
|
|
53
|
+
valid,
|
|
54
|
+
algorithm: algorithmName(level),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=fn-dsa.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const HASH_ALGORITHMS: readonly ["sha256", "sha384", "sha512", "sha3-256", "sha3-512", "blake2b", "blake3"];
|
|
2
|
+
export type HashAlgorithm = (typeof HASH_ALGORITHMS)[number];
|
|
3
|
+
export interface HashOptions {
|
|
4
|
+
algorithm: HashAlgorithm;
|
|
5
|
+
data: string | Uint8Array;
|
|
6
|
+
}
|
|
7
|
+
export interface HashResult {
|
|
8
|
+
digest: string;
|
|
9
|
+
algorithm: HashAlgorithm;
|
|
10
|
+
length: number;
|
|
11
|
+
}
|
|
12
|
+
export declare function hash(options: HashOptions): HashResult;
|
|
13
|
+
//# sourceMappingURL=hash.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/modern/hash.ts"],"names":[],"mappings":"AAiBA,eAAO,MAAM,eAAe,sFAQlB,CAAC;AAGX,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,eAAe,CAAC,CAAC,MAAM,CAAC,CAAC;AAG7D,MAAM,WAAW,WAAW;IAE1B,SAAS,EAAE,aAAa,CAAC;IAEzB,IAAI,EAAE,MAAM,GAAG,UAAU,CAAC;CAC3B;AAGD,MAAM,WAAW,UAAU;IAEzB,MAAM,EAAE,MAAM,CAAC;IAEf,SAAS,EAAE,aAAa,CAAC;IAEzB,MAAM,EAAE,MAAM,CAAC;CAChB;AAuBD,wBAAgB,IAAI,CAAC,OAAO,EAAE,WAAW,GAAG,UAAU,CAiBrD"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HASH_ALGORITHMS = void 0;
|
|
4
|
+
exports.hash = hash;
|
|
5
|
+
const sha2_js_1 = require("@noble/hashes/sha2.js");
|
|
6
|
+
const sha3_js_1 = require("@noble/hashes/sha3.js");
|
|
7
|
+
const blake2_js_1 = require("@noble/hashes/blake2.js");
|
|
8
|
+
const blake3_js_1 = require("@noble/hashes/blake3.js");
|
|
9
|
+
exports.HASH_ALGORITHMS = [
|
|
10
|
+
"sha256",
|
|
11
|
+
"sha384",
|
|
12
|
+
"sha512",
|
|
13
|
+
"sha3-256",
|
|
14
|
+
"sha3-512",
|
|
15
|
+
"blake2b",
|
|
16
|
+
"blake3",
|
|
17
|
+
];
|
|
18
|
+
function toBytes(input) {
|
|
19
|
+
if (input instanceof Uint8Array)
|
|
20
|
+
return input;
|
|
21
|
+
return Buffer.from(input, "utf8");
|
|
22
|
+
}
|
|
23
|
+
const hashFunctions = {
|
|
24
|
+
sha256: (d) => (0, sha2_js_1.sha256)(d),
|
|
25
|
+
sha384: (d) => (0, sha2_js_1.sha384)(d),
|
|
26
|
+
sha512: (d) => (0, sha2_js_1.sha512)(d),
|
|
27
|
+
"sha3-256": (d) => (0, sha3_js_1.sha3_256)(d),
|
|
28
|
+
"sha3-512": (d) => (0, sha3_js_1.sha3_512)(d),
|
|
29
|
+
blake2b: (d) => (0, blake2_js_1.blake2b)(d),
|
|
30
|
+
blake3: (d) => (0, blake3_js_1.blake3)(d),
|
|
31
|
+
};
|
|
32
|
+
function hash(options) {
|
|
33
|
+
const fn = hashFunctions[options.algorithm];
|
|
34
|
+
if (!fn) {
|
|
35
|
+
throw new Error(`Unsupported algorithm: ${options.algorithm}. Supported: ${exports.HASH_ALGORITHMS.join(", ")}`);
|
|
36
|
+
}
|
|
37
|
+
const data = toBytes(options.data);
|
|
38
|
+
const digest = fn(data);
|
|
39
|
+
return {
|
|
40
|
+
digest: Buffer.from(digest).toString("hex"),
|
|
41
|
+
algorithm: options.algorithm,
|
|
42
|
+
length: digest.length,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=hash.js.map
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export type HpkeKem = "x25519" | "p256";
|
|
2
|
+
export type HpkeAead = "chacha20-poly1305" | "aes-128-gcm";
|
|
3
|
+
export type HpkeMode = "base" | "psk";
|
|
4
|
+
export interface HpkeKeyPair {
|
|
5
|
+
publicKey: string;
|
|
6
|
+
privateKey: string;
|
|
7
|
+
}
|
|
8
|
+
export interface HpkeSealResult {
|
|
9
|
+
ciphertext: string;
|
|
10
|
+
encapsulatedKey: string;
|
|
11
|
+
}
|
|
12
|
+
export interface HpkeOpenResult {
|
|
13
|
+
plaintext: string;
|
|
14
|
+
}
|
|
15
|
+
export interface HpkeSuiteOptions {
|
|
16
|
+
kem?: HpkeKem;
|
|
17
|
+
aead?: HpkeAead;
|
|
18
|
+
}
|
|
19
|
+
export interface HpkePskOptions {
|
|
20
|
+
psk: string;
|
|
21
|
+
pskId: string;
|
|
22
|
+
}
|
|
23
|
+
export declare function hpkeGenerateKeyPair(kem?: HpkeKem): HpkeKeyPair;
|
|
24
|
+
export declare function hpkeSeal(options: {
|
|
25
|
+
recipientPublicKey: string;
|
|
26
|
+
plaintext: string;
|
|
27
|
+
info?: string;
|
|
28
|
+
aad?: string;
|
|
29
|
+
suite?: HpkeSuiteOptions;
|
|
30
|
+
psk?: HpkePskOptions;
|
|
31
|
+
}): HpkeSealResult;
|
|
32
|
+
export declare function hpkeSealWithEphemeral(options: Parameters<typeof hpkeSeal>[0], ephemeralPrivateKey: Uint8Array): HpkeSealResult & {
|
|
33
|
+
sharedSecret: string;
|
|
34
|
+
key: string;
|
|
35
|
+
baseNonce: string;
|
|
36
|
+
exporterSecret: string;
|
|
37
|
+
};
|
|
38
|
+
export declare function hpkeOpen(options: {
|
|
39
|
+
recipientPrivateKey: string;
|
|
40
|
+
encapsulatedKey: string;
|
|
41
|
+
ciphertext: string;
|
|
42
|
+
info?: string;
|
|
43
|
+
aad?: string;
|
|
44
|
+
suite?: HpkeSuiteOptions;
|
|
45
|
+
psk?: HpkePskOptions;
|
|
46
|
+
}): HpkeOpenResult;
|
|
47
|
+
//# sourceMappingURL=hpke.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hpke.d.ts","sourceRoot":"","sources":["../../src/modern/hpke.ts"],"names":[],"mappings":"AA4DA,MAAM,MAAM,OAAO,GAAG,QAAQ,GAAG,MAAM,CAAC;AAUxC,MAAM,MAAM,QAAQ,GAAG,mBAAmB,GAAG,aAAa,CAAC;AAU3D,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,KAAK,CAAC;AAUtC,MAAM,WAAW,WAAW;IAE1B,SAAS,EAAE,MAAM,CAAC;IAElB,UAAU,EAAE,MAAM,CAAC;CACpB;AAUD,MAAM,WAAW,cAAc;IAE7B,UAAU,EAAE,MAAM,CAAC;IAEnB,eAAe,EAAE,MAAM,CAAC;CACzB;AAUD,MAAM,WAAW,cAAc;IAE7B,SAAS,EAAE,MAAM,CAAC;CACnB;AAUD,MAAM,WAAW,gBAAgB;IAE/B,GAAG,CAAC,EAAE,OAAO,CAAC;IAEd,IAAI,CAAC,EAAE,QAAQ,CAAC;CACjB;AAUD,MAAM,WAAW,cAAc;IAE7B,GAAG,EAAE,MAAM,CAAC;IAEZ,KAAK,EAAE,MAAM,CAAC;CACf;AAiWD,wBAAgB,mBAAmB,CAAC,GAAG,GAAE,OAAkB,GAAG,WAAW,CAMxE;AAuBD,wBAAgB,QAAQ,CAAC,OAAO,EAAE;IAEhC,kBAAkB,EAAE,MAAM,CAAC;IAE3B,SAAS,EAAE,MAAM,CAAC;IAElB,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd,GAAG,CAAC,EAAE,MAAM,CAAC;IAEb,KAAK,CAAC,EAAE,gBAAgB,CAAC;IAEzB,GAAG,CAAC,EAAE,cAAc,CAAC;CACtB,GAAG,cAAc,CAOjB;AAUD,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,UAAU,CAAC,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAC,EACvC,mBAAmB,EAAE,UAAU,GAC9B,cAAc,GAAG;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,cAAc,EAAE,MAAM,CAAC;CACxB,CA2BA;AA0BD,wBAAgB,QAAQ,CAAC,OAAO,EAAE;IAEhC,mBAAmB,EAAE,MAAM,CAAC;IAE5B,eAAe,EAAE,MAAM,CAAC;IAExB,UAAU,EAAE,MAAM,CAAC;IAEnB,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd,GAAG,CAAC,EAAE,MAAM,CAAC;IAEb,KAAK,CAAC,EAAE,gBAAgB,CAAC;IAEzB,GAAG,CAAC,EAAE,cAAc,CAAC;CACtB,GAAG,cAAc,CAgBjB"}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hpkeGenerateKeyPair = hpkeGenerateKeyPair;
|
|
4
|
+
exports.hpkeSeal = hpkeSeal;
|
|
5
|
+
exports.hpkeSealWithEphemeral = hpkeSealWithEphemeral;
|
|
6
|
+
exports.hpkeOpen = hpkeOpen;
|
|
7
|
+
const ed25519_js_1 = require("@noble/curves/ed25519.js");
|
|
8
|
+
const nist_js_1 = require("@noble/curves/nist.js");
|
|
9
|
+
const hkdf_js_1 = require("@noble/hashes/hkdf.js");
|
|
10
|
+
const sha2_js_1 = require("@noble/hashes/sha2.js");
|
|
11
|
+
const chacha_js_1 = require("@noble/ciphers/chacha.js");
|
|
12
|
+
const aes_js_1 = require("@noble/ciphers/aes.js");
|
|
13
|
+
const KEM_X25519 = 0x0020;
|
|
14
|
+
const KEM_P256 = 0x0010;
|
|
15
|
+
const KDF_HKDF_SHA256 = 0x0001;
|
|
16
|
+
const AEAD_AES_128_GCM = 0x0001;
|
|
17
|
+
const AEAD_CHACHA20_POLY1305 = 0x0003;
|
|
18
|
+
const MODE_BASE = 0x00;
|
|
19
|
+
const MODE_PSK = 0x01;
|
|
20
|
+
const HEX_RE = /^[0-9a-fA-F]*$/;
|
|
21
|
+
function hexToBytes(hex) {
|
|
22
|
+
if (!HEX_RE.test(hex) || hex.length % 2 !== 0) {
|
|
23
|
+
throw new Error("Invalid hex string");
|
|
24
|
+
}
|
|
25
|
+
return Buffer.from(hex, "hex");
|
|
26
|
+
}
|
|
27
|
+
function bytesToHex(bytes) {
|
|
28
|
+
return Buffer.from(bytes).toString("hex");
|
|
29
|
+
}
|
|
30
|
+
function i2osp2(n) {
|
|
31
|
+
return new Uint8Array([(n >> 8) & 0xff, n & 0xff]);
|
|
32
|
+
}
|
|
33
|
+
function concat(...arrays) {
|
|
34
|
+
const len = arrays.reduce((s, a) => s + a.length, 0);
|
|
35
|
+
const result = new Uint8Array(len);
|
|
36
|
+
let offset = 0;
|
|
37
|
+
for (const a of arrays) {
|
|
38
|
+
result.set(a, offset);
|
|
39
|
+
offset += a.length;
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
function suiteParams(kem, aead) {
|
|
44
|
+
const kemId = kem === "x25519" ? KEM_X25519 : KEM_P256;
|
|
45
|
+
const aeadId = aead === "chacha20-poly1305" ? AEAD_CHACHA20_POLY1305 : AEAD_AES_128_GCM;
|
|
46
|
+
const nk = aead === "chacha20-poly1305" ? 32 : 16;
|
|
47
|
+
const nn = 12;
|
|
48
|
+
const nSecret = 32;
|
|
49
|
+
let nEnc;
|
|
50
|
+
let nSk;
|
|
51
|
+
if (kem === "x25519") {
|
|
52
|
+
nEnc = 32;
|
|
53
|
+
nSk = 32;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
nEnc = 65;
|
|
57
|
+
nSk = 32;
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
kemId,
|
|
61
|
+
kdfId: KDF_HKDF_SHA256,
|
|
62
|
+
aeadId,
|
|
63
|
+
nk,
|
|
64
|
+
nn,
|
|
65
|
+
nSecret,
|
|
66
|
+
nEnc,
|
|
67
|
+
nSk,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function hpkeSuiteId(params) {
|
|
71
|
+
return concat(Buffer.from("HPKE"), i2osp2(params.kemId), i2osp2(params.kdfId), i2osp2(params.aeadId));
|
|
72
|
+
}
|
|
73
|
+
function kemSuiteId(kemId) {
|
|
74
|
+
return concat(Buffer.from("KEM"), i2osp2(kemId));
|
|
75
|
+
}
|
|
76
|
+
function labeledExtract(suiteId, salt, label, ikm) {
|
|
77
|
+
const labeledIkm = concat(Buffer.from("HPKE-v1"), suiteId, Buffer.from(label), ikm);
|
|
78
|
+
return (0, hkdf_js_1.extract)(sha2_js_1.sha256, labeledIkm, salt.length > 0 ? salt : undefined);
|
|
79
|
+
}
|
|
80
|
+
function labeledExpand(suiteId, prk, label, info, length) {
|
|
81
|
+
const labeledInfo = concat(i2osp2(length), Buffer.from("HPKE-v1"), suiteId, Buffer.from(label), info);
|
|
82
|
+
return (0, hkdf_js_1.expand)(sha2_js_1.sha256, prk, labeledInfo, length);
|
|
83
|
+
}
|
|
84
|
+
function kemPublicKey(kem, sk) {
|
|
85
|
+
return kem === "x25519"
|
|
86
|
+
? ed25519_js_1.x25519.getPublicKey(sk)
|
|
87
|
+
: nist_js_1.p256.getPublicKey(sk, false);
|
|
88
|
+
}
|
|
89
|
+
function kemRandomPrivateKey(kem) {
|
|
90
|
+
return kem === "x25519"
|
|
91
|
+
? ed25519_js_1.x25519.utils.randomSecretKey()
|
|
92
|
+
: nist_js_1.p256.utils.randomSecretKey();
|
|
93
|
+
}
|
|
94
|
+
function kemDh(kem, sk, pk) {
|
|
95
|
+
if (kem === "x25519") {
|
|
96
|
+
return ed25519_js_1.x25519.getSharedSecret(sk, pk);
|
|
97
|
+
}
|
|
98
|
+
return nist_js_1.p256.getSharedSecret(sk, pk, false).subarray(1, 33);
|
|
99
|
+
}
|
|
100
|
+
function extractAndExpand(params, dh, kemContext) {
|
|
101
|
+
const kemSid = kemSuiteId(params.kemId);
|
|
102
|
+
const eaePrk = labeledExtract(kemSid, new Uint8Array(0), "eae_prk", dh);
|
|
103
|
+
return labeledExpand(kemSid, eaePrk, "shared_secret", kemContext, params.nSecret);
|
|
104
|
+
}
|
|
105
|
+
function dhkemEncap(kem, recipientPub, ephPriv, params) {
|
|
106
|
+
const enc = kemPublicKey(kem, ephPriv);
|
|
107
|
+
const dh = kemDh(kem, ephPriv, recipientPub);
|
|
108
|
+
const sharedSecret = extractAndExpand(params, dh, concat(enc, recipientPub));
|
|
109
|
+
return { sharedSecret, enc };
|
|
110
|
+
}
|
|
111
|
+
function dhkemDecap(kem, enc, recipientPriv, params) {
|
|
112
|
+
const dh = kemDh(kem, recipientPriv, enc);
|
|
113
|
+
const recipientPub = kemPublicKey(kem, recipientPriv);
|
|
114
|
+
return extractAndExpand(params, dh, concat(enc, recipientPub));
|
|
115
|
+
}
|
|
116
|
+
function keySchedule(params, sharedSecret, inputs) {
|
|
117
|
+
const suiteId = hpkeSuiteId(params);
|
|
118
|
+
const empty = new Uint8Array(0);
|
|
119
|
+
const pskIdHash = labeledExtract(suiteId, empty, "psk_id_hash", inputs.pskId);
|
|
120
|
+
const infoHash = labeledExtract(suiteId, empty, "info_hash", inputs.info);
|
|
121
|
+
const ksContext = concat(new Uint8Array([inputs.mode]), pskIdHash, infoHash);
|
|
122
|
+
const secret = labeledExtract(suiteId, sharedSecret, "secret", inputs.psk);
|
|
123
|
+
return {
|
|
124
|
+
key: labeledExpand(suiteId, secret, "key", ksContext, params.nk),
|
|
125
|
+
baseNonce: labeledExpand(suiteId, secret, "base_nonce", ksContext, params.nn),
|
|
126
|
+
exporterSecret: labeledExpand(suiteId, secret, "exp", ksContext, params.nSecret),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
function scheduleInputs(options) {
|
|
130
|
+
const info = options.info ? hexToBytes(options.info) : new Uint8Array(0);
|
|
131
|
+
if (!options.psk) {
|
|
132
|
+
return {
|
|
133
|
+
mode: MODE_BASE,
|
|
134
|
+
info,
|
|
135
|
+
psk: new Uint8Array(0),
|
|
136
|
+
pskId: new Uint8Array(0),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
const psk = hexToBytes(options.psk.psk);
|
|
140
|
+
const pskId = hexToBytes(options.psk.pskId);
|
|
141
|
+
if (psk.length === 0 || pskId.length === 0) {
|
|
142
|
+
throw new Error("PSK mode requires a non-empty psk and pskId");
|
|
143
|
+
}
|
|
144
|
+
return { mode: MODE_PSK, info, psk, pskId };
|
|
145
|
+
}
|
|
146
|
+
function aeadSeal(aead, key, nonce, aad, plaintext) {
|
|
147
|
+
if (aead === "chacha20-poly1305") {
|
|
148
|
+
return (0, chacha_js_1.chacha20poly1305)(key, nonce, aad).encrypt(plaintext);
|
|
149
|
+
}
|
|
150
|
+
return (0, aes_js_1.gcm)(key, nonce, aad).encrypt(plaintext);
|
|
151
|
+
}
|
|
152
|
+
function aeadOpen(aead, key, nonce, aad, ciphertext) {
|
|
153
|
+
if (aead === "chacha20-poly1305") {
|
|
154
|
+
return (0, chacha_js_1.chacha20poly1305)(key, nonce, aad).decrypt(ciphertext);
|
|
155
|
+
}
|
|
156
|
+
return (0, aes_js_1.gcm)(key, nonce, aad).decrypt(ciphertext);
|
|
157
|
+
}
|
|
158
|
+
function hpkeGenerateKeyPair(kem = "x25519") {
|
|
159
|
+
const privateKey = kemRandomPrivateKey(kem);
|
|
160
|
+
return {
|
|
161
|
+
publicKey: bytesToHex(kemPublicKey(kem, privateKey)),
|
|
162
|
+
privateKey: bytesToHex(privateKey),
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
function hpkeSeal(options) {
|
|
166
|
+
const kem = options.suite?.kem ?? "x25519";
|
|
167
|
+
const sealed = hpkeSealWithEphemeral(options, kemRandomPrivateKey(kem));
|
|
168
|
+
return {
|
|
169
|
+
ciphertext: sealed.ciphertext,
|
|
170
|
+
encapsulatedKey: sealed.encapsulatedKey,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
function hpkeSealWithEphemeral(options, ephemeralPrivateKey) {
|
|
174
|
+
const kem = options.suite?.kem ?? "x25519";
|
|
175
|
+
const aead = options.suite?.aead ?? "chacha20-poly1305";
|
|
176
|
+
const params = suiteParams(kem, aead);
|
|
177
|
+
const recipientPub = hexToBytes(options.recipientPublicKey);
|
|
178
|
+
const plaintext = hexToBytes(options.plaintext);
|
|
179
|
+
const aad = options.aad ? hexToBytes(options.aad) : new Uint8Array(0);
|
|
180
|
+
const inputs = scheduleInputs(options);
|
|
181
|
+
const { sharedSecret, enc } = dhkemEncap(kem, recipientPub, ephemeralPrivateKey, params);
|
|
182
|
+
const ks = keySchedule(params, sharedSecret, inputs);
|
|
183
|
+
const ct = aeadSeal(aead, ks.key, ks.baseNonce, aad, plaintext);
|
|
184
|
+
return {
|
|
185
|
+
ciphertext: bytesToHex(ct),
|
|
186
|
+
encapsulatedKey: bytesToHex(enc),
|
|
187
|
+
sharedSecret: bytesToHex(sharedSecret),
|
|
188
|
+
key: bytesToHex(ks.key),
|
|
189
|
+
baseNonce: bytesToHex(ks.baseNonce),
|
|
190
|
+
exporterSecret: bytesToHex(ks.exporterSecret),
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
function hpkeOpen(options) {
|
|
194
|
+
const kem = options.suite?.kem ?? "x25519";
|
|
195
|
+
const aead = options.suite?.aead ?? "chacha20-poly1305";
|
|
196
|
+
const params = suiteParams(kem, aead);
|
|
197
|
+
const recipientPriv = hexToBytes(options.recipientPrivateKey);
|
|
198
|
+
const enc = hexToBytes(options.encapsulatedKey);
|
|
199
|
+
const ciphertext = hexToBytes(options.ciphertext);
|
|
200
|
+
const aad = options.aad ? hexToBytes(options.aad) : new Uint8Array(0);
|
|
201
|
+
const inputs = scheduleInputs(options);
|
|
202
|
+
const sharedSecret = dhkemDecap(kem, enc, recipientPriv, params);
|
|
203
|
+
const { key, baseNonce } = keySchedule(params, sharedSecret, inputs);
|
|
204
|
+
const pt = aeadOpen(aead, key, baseNonce, aad, ciphertext);
|
|
205
|
+
return { plaintext: bytesToHex(pt) };
|
|
206
|
+
}
|
|
207
|
+
//# sourceMappingURL=hpke.js.map
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export { aeadEncrypt, aeadDecrypt } from "./aead";
|
|
2
|
+
export type { AeadEncryptOptions, AeadDecryptOptions, AeadEncryptResult, } from "./aead";
|
|
3
|
+
export { aesGcmEncrypt, aesGcmDecrypt, aesGcmSivEncrypt, aesGcmSivDecrypt, } from "./aes";
|
|
4
|
+
export type { AesGcmEncryptOptions, AesGcmEncryptResult, AesGcmDecryptOptions, AesGcmAlgorithm, AesGcmSivEncryptOptions, AesGcmSivEncryptResult, AesGcmSivDecryptOptions, AesGcmSivAlgorithm, } from "./aes";
|
|
5
|
+
export { hash, HASH_ALGORITHMS } from "./hash";
|
|
6
|
+
export type { HashOptions, HashResult, HashAlgorithm } from "./hash";
|
|
7
|
+
export { kdfDerive, KDF_ALGORITHMS } from "./kdf";
|
|
8
|
+
export type { KdfDeriveOptions, KdfResult, KdfAlgorithm } from "./kdf";
|
|
9
|
+
export { generateEd25519KeyPair, ed25519Sign, ed25519Verify } from "./signing";
|
|
10
|
+
export type { Ed25519KeyPair, SignResult, VerifyResult } from "./signing";
|
|
11
|
+
export { generateX25519KeyPair, x25519Exchange } from "./ecdh";
|
|
12
|
+
export type { X25519KeyPair, KeyExchangeResult } from "./ecdh";
|
|
13
|
+
export { mlKemGenerateKeyPair, mlKemEncapsulate, mlKemDecapsulate, hybridGenerateKeyPair, hybridEncapsulate, hybridDecapsulate, } from "./pq";
|
|
14
|
+
export type { MlKemKeyPair, MlKemEncapsulateResult, MlKemDecapsulateResult, HybridKeyPair, HybridEncapsulateResult, HybridDecapsulateResult, } from "./pq";
|
|
15
|
+
export { mlKemKeygen, mlKemEncapsulate as mlKemEncap, mlKemDecapsulate as mlKemDecap, hybridKemKeygen, hybridKemEncapsulate, hybridKemDecapsulate, p256MlKemKeygen, p256MlKemEncapsulate, p256MlKemDecapsulate, x448MlKemKeygen, x448MlKemEncapsulate, x448MlKemDecapsulate, RFC10024_X25519_MLKEM768, RFC10024_SECP256R1_MLKEM768, RFC10024_CODEPOINTS, normalizeHybridKemAlgorithm, } from "./pq-kem";
|
|
16
|
+
export type { MlKemLevel, MlKemAlgorithm, MlKemKeyPairResult, MlKemEncapsulateResult as MlKemEncapResult, MlKemDecapsulateResult as MlKemDecapResult, HybridKemAlgorithm, HybridKemKeyPair, HybridKemEncapsulateResult, HybridKemDecapsulateResult, P256MlKemKeyPair, P256MlKemEncapsulateResult, P256MlKemDecapsulateResult, X448MlKemKeyPair, X448MlKemEncapsulateResult, X448MlKemDecapsulateResult, } from "./pq-kem";
|
|
17
|
+
export { mlDsaKeygen, mlDsaSign, mlDsaVerify, hybridSign, hybridVerify, } from "./pq-sign";
|
|
18
|
+
export type { MlDsaLevel, MlDsaAlgorithm, MlDsaKeyPairResult, MlDsaSignResult, MlDsaVerifyResult, HybridSignResult, HybridVerifyResult, } from "./pq-sign";
|
|
19
|
+
export { fnDsaKeygen, fnDsaSign, fnDsaVerify } from "./fn-dsa";
|
|
20
|
+
export type { FnDsaLevel, FnDsaAlgorithm, FnDsaKeyPairResult, FnDsaSignResult, FnDsaVerifyResult, } from "./fn-dsa";
|
|
21
|
+
export { slhDsaKeygen, slhDsaSign, slhDsaVerify } from "./pq-hash-sign";
|
|
22
|
+
export type { SlhDsaVariant, SlhDsaKeyPairResult, SlhDsaSignResult, SlhDsaVerifyResult, } from "./pq-hash-sign";
|
|
23
|
+
export { computeHmac, verifyHmac, HMAC_ALGORITHMS, computeKmac, verifyKmac, KMAC_ALGORITHMS, } from "./mac";
|
|
24
|
+
export type { HmacAlgorithm, HmacComputeOptions, HmacComputeResult, HmacVerifyOptions, HmacVerifyResult, KmacAlgorithm, KmacComputeOptions, KmacComputeResult, KmacVerifyOptions, KmacVerifyResult, } from "./mac";
|
|
25
|
+
export { hashPassword, verifyPassword, verifyPasswordPhc } from "./password";
|
|
26
|
+
export type { Argon2Variant, Argon2Params, HashPasswordOptions, HashPasswordResult, VerifyPasswordOptions, VerifyPasswordResult, VerifyPhcOptions, } from "./password";
|
|
27
|
+
export { generateP256KeyPair, p256Sign, p256Verify, generateP384KeyPair, p384Sign, p384Verify, generateEd448KeyPair, ed448Sign, ed448Verify, generateX448KeyPair, x448Exchange, ecdhP256, ecdhP384, generateSchnorrKeyPair, schnorrSign, schnorrVerify, } from "./curves";
|
|
28
|
+
export type { P256KeyPair, P256SignResult, P256VerifyResult, P384KeyPair, P384SignResult, P384VerifyResult, Ed448KeyPair, Ed448SignResult, Ed448VerifyResult, X448KeyPair, X448ExchangeResult, EcdhP256Result, EcdhP384Result, SchnorrKeyPair, SchnorrSignResult, SchnorrVerifyResult, } from "./curves";
|
|
29
|
+
export { hpkeGenerateKeyPair, hpkeSeal, hpkeOpen } from "./hpke";
|
|
30
|
+
export type { HpkeKem, HpkeAead, HpkeMode, HpkeKeyPair, HpkeSealResult, HpkeOpenResult, HpkeSuiteOptions, HpkePskOptions, } from "./hpke";
|
|
31
|
+
export declare const SUPPORTED_ALGORITHMS: {
|
|
32
|
+
readonly encryption: readonly ["xchacha20-poly1305", "aes-128-gcm", "aes-256-gcm", "aes-128-gcm-siv", "aes-256-gcm-siv"];
|
|
33
|
+
readonly hashing: readonly ["sha256", "sha384", "sha512", "sha3-256", "sha3-512", "blake2b", "blake3"];
|
|
34
|
+
readonly kdf: readonly ["scrypt", "hkdf-sha256", "pbkdf2-sha256", "argon2id", "argon2i", "argon2d"];
|
|
35
|
+
readonly mac: readonly ["hmac-sha256", "hmac-sha384", "hmac-sha512", "hmac-sha3-256", "hmac-sha3-512", "kmac-128", "kmac-256"];
|
|
36
|
+
readonly signing: readonly ["ed25519", "ed448", "ecdsa-p256", "ecdsa-p384", "schnorr", "ml-dsa-44", "ml-dsa-65", "ml-dsa-87", "slh-dsa-sha2-128f", "slh-dsa-sha2-128s", "slh-dsa-sha2-192f", "slh-dsa-sha2-192s", "slh-dsa-sha2-256f", "slh-dsa-sha2-256s", "slh-dsa-shake-128f", "slh-dsa-shake-128s", "slh-dsa-shake-192f", "slh-dsa-shake-192s", "slh-dsa-shake-256f", "slh-dsa-shake-256s", "ed25519-ml-dsa-44", "ed25519-ml-dsa-65", "ed25519-ml-dsa-87", "fn-dsa-512", "fn-dsa-1024"];
|
|
37
|
+
readonly keyExchange: readonly ["x25519", "x448", "ecdh-p256", "ecdh-p384", "x25519-ml-kem-512", "x25519-ml-kem-768", "x25519-ml-kem-1024", "p256-ml-kem-768", "x448-ml-kem-1024", "hpke-x25519-chacha20", "hpke-x25519-aes128gcm", "hpke-p256-aes128gcm"];
|
|
38
|
+
readonly postQuantum: readonly ["ml-kem-512", "ml-kem-768", "ml-kem-1024", "ml-dsa-44", "ml-dsa-65", "ml-dsa-87", "slh-dsa-sha2-128f", "slh-dsa-sha2-128s", "slh-dsa-sha2-192f", "slh-dsa-sha2-192s", "slh-dsa-sha2-256f", "slh-dsa-sha2-256s", "slh-dsa-shake-128f", "slh-dsa-shake-128s", "slh-dsa-shake-192f", "slh-dsa-shake-192s", "slh-dsa-shake-256f", "slh-dsa-shake-256s", "fn-dsa-512", "fn-dsa-1024"];
|
|
39
|
+
};
|
|
40
|
+
//# sourceMappingURL=index.d.ts.map
|