shogun-core 5.2.0 → 5.2.1
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/dist/browser/defaultVendors-node_modules_hpke_chacha20poly1305_esm_mod_js.shogun-core.js +1220 -0
- package/dist/browser/defaultVendors-node_modules_hpke_chacha20poly1305_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_hpke_hybridkem-x-wing_esm_mod_js.shogun-core.js +844 -0
- package/dist/browser/defaultVendors-node_modules_hpke_hybridkem-x-wing_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_mlkem_esm_mod_js.shogun-core.js +2335 -0
- package/dist/browser/defaultVendors-node_modules_mlkem_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_ciphers_chacha_js.shogun-core.js +999 -0
- package/dist/browser/defaultVendors-node_modules_noble_ciphers_chacha_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js +1651 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_edwards_js-node_modules_noble_curves_es-a82056.shogun-core.js +825 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_edwards_js-node_modules_noble_curves_es-a82056.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed25519_js.shogun-core.js +508 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed25519_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js +747 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js +1608 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_post-quantum_ml-dsa_js.shogun-core.js +2117 -0
- package/dist/browser/defaultVendors-node_modules_noble_post-quantum_ml-dsa_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_openpgp_dist_openpgp_min_mjs.shogun-core.js +86 -0
- package/dist/browser/defaultVendors-node_modules_openpgp_dist_openpgp_min_mjs.shogun-core.js.map +1 -0
- package/dist/browser/node_modules_hpke_ml-kem_esm_mod_js.shogun-core.js +539 -0
- package/dist/browser/node_modules_hpke_ml-kem_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/shogun-core.js +160386 -0
- package/dist/browser/shogun-core.js.map +1 -0
- package/dist/config/simplified-config.js +236 -0
- package/dist/core.js +329 -0
- package/dist/crypto/asymmetric.js +99 -0
- package/dist/crypto/double-ratchet.js +370 -0
- package/dist/crypto/file-encryption.js +213 -0
- package/dist/crypto/hashing.js +87 -0
- package/dist/crypto/index.js +34 -0
- package/dist/crypto/mls-codec.js +202 -0
- package/dist/crypto/mls.js +550 -0
- package/dist/crypto/pgp.js +390 -0
- package/dist/crypto/random-generation.js +341 -0
- package/dist/crypto/sframe.js +350 -0
- package/dist/crypto/signal-protocol.js +376 -0
- package/dist/crypto/symmetric.js +91 -0
- package/dist/crypto/types.js +2 -0
- package/dist/crypto/utils.js +140 -0
- package/dist/examples/auth-test.js +253 -0
- package/dist/examples/crypto-identity-example.js +151 -0
- package/dist/examples/crypto-working-test.js +83 -0
- package/dist/examples/double-ratchet-test.js +155 -0
- package/dist/examples/mls-advanced-example.js +294 -0
- package/dist/examples/mls-sframe-test.js +304 -0
- package/dist/examples/pgp-example.js +200 -0
- package/dist/examples/quick-auth-test.js +61 -0
- package/dist/examples/random-generation-test.js +151 -0
- package/dist/examples/signal-protocol-test.js +38 -0
- package/dist/examples/simple-api-test.js +114 -0
- package/dist/examples/simple-crypto-identity-example.js +84 -0
- package/dist/examples/timeout-test.js +227 -0
- package/dist/examples/zkproof-credentials-example.js +212 -0
- package/dist/examples/zkproof-example.js +201 -0
- package/dist/gundb/api.js +435 -0
- package/dist/gundb/crypto.js +283 -0
- package/dist/gundb/db.js +1946 -0
- package/dist/gundb/derive.js +232 -0
- package/dist/gundb/errors.js +76 -0
- package/dist/gundb/index.js +22 -0
- package/dist/gundb/rxjs.js +447 -0
- package/dist/gundb/types.js +5 -0
- package/dist/index.js +58 -0
- package/dist/interfaces/common.js +2 -0
- package/dist/interfaces/events.js +40 -0
- package/dist/interfaces/plugin.js +2 -0
- package/dist/interfaces/shogun.js +37 -0
- package/dist/managers/AuthManager.js +226 -0
- package/dist/managers/CoreInitializer.js +228 -0
- package/dist/managers/CryptoIdentityManager.js +366 -0
- package/dist/managers/EventManager.js +70 -0
- package/dist/managers/PluginManager.js +299 -0
- package/dist/plugins/base.js +50 -0
- package/dist/plugins/index.js +32 -0
- package/dist/plugins/nostr/index.js +20 -0
- package/dist/plugins/nostr/nostrConnector.js +419 -0
- package/dist/plugins/nostr/nostrConnectorPlugin.js +453 -0
- package/dist/plugins/nostr/nostrSigner.js +319 -0
- package/dist/plugins/nostr/types.js +2 -0
- package/dist/plugins/smartwallet/index.js +18 -0
- package/dist/plugins/smartwallet/smartWalletPlugin.js +511 -0
- package/dist/plugins/smartwallet/types.js +2 -0
- package/dist/plugins/web3/index.js +20 -0
- package/dist/plugins/web3/types.js +2 -0
- package/dist/plugins/web3/web3Connector.js +533 -0
- package/dist/plugins/web3/web3ConnectorPlugin.js +455 -0
- package/dist/plugins/web3/web3Signer.js +314 -0
- package/dist/plugins/webauthn/index.js +19 -0
- package/dist/plugins/webauthn/types.js +14 -0
- package/dist/plugins/webauthn/webauthn.js +496 -0
- package/dist/plugins/webauthn/webauthnPlugin.js +489 -0
- package/dist/plugins/webauthn/webauthnSigner.js +310 -0
- package/dist/plugins/zkproof/index.js +53 -0
- package/dist/plugins/zkproof/types.js +2 -0
- package/dist/plugins/zkproof/zkCredentials.js +213 -0
- package/dist/plugins/zkproof/zkProofConnector.js +198 -0
- package/dist/plugins/zkproof/zkProofPlugin.js +272 -0
- package/dist/storage/storage.js +145 -0
- package/dist/types/config/simplified-config.d.ts +114 -0
- package/dist/types/core.d.ts +305 -0
- package/dist/types/crypto/asymmetric.d.ts +6 -0
- package/dist/types/crypto/double-ratchet.d.ts +22 -0
- package/dist/types/crypto/file-encryption.d.ts +19 -0
- package/dist/types/crypto/hashing.d.ts +9 -0
- package/dist/types/crypto/index.d.ts +13 -0
- package/dist/types/crypto/mls-codec.d.ts +39 -0
- package/dist/types/crypto/mls.d.ts +130 -0
- package/dist/types/crypto/pgp.d.ts +95 -0
- package/dist/types/crypto/random-generation.d.ts +35 -0
- package/dist/types/crypto/sframe.d.ts +102 -0
- package/dist/types/crypto/signal-protocol.d.ts +26 -0
- package/dist/types/crypto/symmetric.d.ts +9 -0
- package/dist/types/crypto/types.d.ts +144 -0
- package/dist/types/crypto/utils.d.ts +22 -0
- package/dist/types/examples/auth-test.d.ts +8 -0
- package/dist/types/examples/crypto-identity-example.d.ts +5 -0
- package/dist/types/examples/crypto-working-test.d.ts +1 -0
- package/dist/types/examples/double-ratchet-test.d.ts +1 -0
- package/dist/types/examples/mls-advanced-example.d.ts +53 -0
- package/dist/types/examples/mls-sframe-test.d.ts +1 -0
- package/dist/types/examples/pgp-example.d.ts +75 -0
- package/dist/types/examples/quick-auth-test.d.ts +8 -0
- package/dist/types/examples/random-generation-test.d.ts +1 -0
- package/dist/types/examples/signal-protocol-test.d.ts +1 -0
- package/dist/types/examples/simple-api-test.d.ts +10 -0
- package/dist/types/examples/simple-crypto-identity-example.d.ts +6 -0
- package/dist/types/examples/timeout-test.d.ts +8 -0
- package/dist/types/examples/zkproof-credentials-example.d.ts +12 -0
- package/dist/types/examples/zkproof-example.d.ts +11 -0
- package/dist/types/gundb/api.d.ts +185 -0
- package/dist/types/gundb/crypto.d.ts +95 -0
- package/dist/types/gundb/db.d.ts +397 -0
- package/dist/types/gundb/derive.d.ts +21 -0
- package/dist/types/gundb/errors.d.ts +42 -0
- package/dist/types/gundb/index.d.ts +3 -0
- package/dist/types/gundb/rxjs.d.ts +110 -0
- package/dist/types/gundb/types.d.ts +255 -0
- package/dist/types/index.d.ts +16 -0
- package/dist/types/interfaces/common.d.ts +85 -0
- package/dist/types/interfaces/events.d.ts +131 -0
- package/dist/types/interfaces/plugin.d.ts +162 -0
- package/dist/types/interfaces/shogun.d.ts +208 -0
- package/dist/types/managers/AuthManager.d.ts +72 -0
- package/dist/types/managers/CoreInitializer.d.ts +40 -0
- package/dist/types/managers/CryptoIdentityManager.d.ts +102 -0
- package/dist/types/managers/EventManager.d.ts +49 -0
- package/dist/types/managers/PluginManager.d.ts +145 -0
- package/dist/types/plugins/base.d.ts +35 -0
- package/dist/types/plugins/index.d.ts +18 -0
- package/dist/types/plugins/nostr/index.d.ts +4 -0
- package/dist/types/plugins/nostr/nostrConnector.d.ts +119 -0
- package/dist/types/plugins/nostr/nostrConnectorPlugin.d.ts +163 -0
- package/dist/types/plugins/nostr/nostrSigner.d.ts +105 -0
- package/dist/types/plugins/nostr/types.d.ts +122 -0
- package/dist/types/plugins/smartwallet/index.d.ts +2 -0
- package/dist/types/plugins/smartwallet/smartWalletPlugin.d.ts +67 -0
- package/dist/types/plugins/smartwallet/types.d.ts +80 -0
- package/dist/types/plugins/web3/index.d.ts +4 -0
- package/dist/types/plugins/web3/types.d.ts +107 -0
- package/dist/types/plugins/web3/web3Connector.d.ts +129 -0
- package/dist/types/plugins/web3/web3ConnectorPlugin.d.ts +160 -0
- package/dist/types/plugins/web3/web3Signer.d.ts +114 -0
- package/dist/types/plugins/webauthn/index.d.ts +3 -0
- package/dist/types/plugins/webauthn/types.d.ts +183 -0
- package/dist/types/plugins/webauthn/webauthn.d.ts +129 -0
- package/dist/types/plugins/webauthn/webauthnPlugin.d.ts +179 -0
- package/dist/types/plugins/webauthn/webauthnSigner.d.ts +91 -0
- package/dist/types/plugins/zkproof/index.d.ts +48 -0
- package/dist/types/plugins/zkproof/types.d.ts +123 -0
- package/dist/types/plugins/zkproof/zkCredentials.d.ts +112 -0
- package/dist/types/plugins/zkproof/zkProofConnector.d.ts +46 -0
- package/dist/types/plugins/zkproof/zkProofPlugin.d.ts +76 -0
- package/dist/types/storage/storage.d.ts +51 -0
- package/dist/types/utils/errorHandler.d.ts +119 -0
- package/dist/types/utils/eventEmitter.d.ts +39 -0
- package/dist/types/utils/seedPhrase.d.ts +50 -0
- package/dist/types/utils/validation.d.ts +27 -0
- package/dist/utils/errorHandler.js +246 -0
- package/dist/utils/eventEmitter.js +79 -0
- package/dist/utils/seedPhrase.js +97 -0
- package/dist/utils/validation.js +81 -0
- package/package.json +10 -1
package/dist/browser/defaultVendors-node_modules_hpke_hybridkem-x-wing_esm_mod_js.shogun-core.js
ADDED
|
@@ -0,0 +1,844 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(this["webpackChunkShogunCore"] = this["webpackChunkShogunCore"] || []).push([["defaultVendors-node_modules_hpke_hybridkem-x-wing_esm_mod_js"],{
|
|
3
|
+
|
|
4
|
+
/***/ "./node_modules/@hpke/hybridkem-x-wing/esm/mod.js":
|
|
5
|
+
/*!********************************************************************!*\
|
|
6
|
+
!*** ./node_modules/@hpke/hybridkem-x-wing/esm/mod.js + 5 modules ***!
|
|
7
|
+
\********************************************************************/
|
|
8
|
+
/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => {
|
|
9
|
+
|
|
10
|
+
// ESM COMPAT FLAG
|
|
11
|
+
__webpack_require__.r(__webpack_exports__);
|
|
12
|
+
|
|
13
|
+
// EXPORTS
|
|
14
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
15
|
+
XWing: () => (/* reexport */ XWing)
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
// EXTERNAL MODULE: ./node_modules/mlkem/esm/mod.js + 12 modules
|
|
19
|
+
var mod = __webpack_require__("./node_modules/mlkem/esm/mod.js");
|
|
20
|
+
// EXTERNAL MODULE: ./node_modules/@hpke/common/esm/mod.js + 23 modules
|
|
21
|
+
var esm_mod = __webpack_require__("./node_modules/@hpke/common/esm/mod.js");
|
|
22
|
+
;// ./node_modules/@hpke/dhkem-x25519/esm/src/primitives/x25519.js
|
|
23
|
+
/**
|
|
24
|
+
* This file is based on noble-curves (https://github.com/paulmillr/noble-curves).
|
|
25
|
+
*
|
|
26
|
+
* noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com)
|
|
27
|
+
*
|
|
28
|
+
* The original file is located at:
|
|
29
|
+
* https://github.com/paulmillr/noble-curves/blob/b9d49d2b41d550571a0c5be443ecb62109fa3373/src/ed25519.ts
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* ed25519 Twisted Edwards curve with following addons:
|
|
33
|
+
* - X25519 ECDH
|
|
34
|
+
* - Ristretto cofactor elimination
|
|
35
|
+
* - Elligator hash-to-group / point indistinguishability
|
|
36
|
+
* @module
|
|
37
|
+
*/
|
|
38
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
39
|
+
|
|
40
|
+
const _1n = BigInt(1);
|
|
41
|
+
const _2n = BigInt(2);
|
|
42
|
+
const _3n = BigInt(3);
|
|
43
|
+
const _5n = BigInt(5);
|
|
44
|
+
// P = 2n**255n - 19n
|
|
45
|
+
const ed25519_CURVE_p = BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed");
|
|
46
|
+
function ed25519_pow_2_252_3(x) {
|
|
47
|
+
const _10n = BigInt(10);
|
|
48
|
+
const _20n = BigInt(20);
|
|
49
|
+
const _40n = BigInt(40);
|
|
50
|
+
const _80n = BigInt(80);
|
|
51
|
+
const P = ed25519_CURVE_p;
|
|
52
|
+
const x2 = (x * x) % P;
|
|
53
|
+
const b2 = (x2 * x) % P; // x^3, 11
|
|
54
|
+
const b4 = ((0,esm_mod.pow2)(b2, _2n, P) * b2) % P; // x^15, 1111
|
|
55
|
+
const b5 = ((0,esm_mod.pow2)(b4, _1n, P) * x) % P; // x^31
|
|
56
|
+
const b10 = ((0,esm_mod.pow2)(b5, _5n, P) * b5) % P;
|
|
57
|
+
const b20 = ((0,esm_mod.pow2)(b10, _10n, P) * b10) % P;
|
|
58
|
+
const b40 = ((0,esm_mod.pow2)(b20, _20n, P) * b20) % P;
|
|
59
|
+
const b80 = ((0,esm_mod.pow2)(b40, _40n, P) * b40) % P;
|
|
60
|
+
const b160 = ((0,esm_mod.pow2)(b80, _80n, P) * b80) % P;
|
|
61
|
+
const b240 = ((0,esm_mod.pow2)(b160, _80n, P) * b80) % P;
|
|
62
|
+
const b250 = ((0,esm_mod.pow2)(b240, _10n, P) * b10) % P;
|
|
63
|
+
const pow_p_5_8 = ((0,esm_mod.pow2)(b250, _2n, P) * x) % P;
|
|
64
|
+
// ^ To pow to (p+3)/8, multiply it by x.
|
|
65
|
+
return { pow_p_5_8, b2 };
|
|
66
|
+
}
|
|
67
|
+
function adjustScalarBytes(bytes) {
|
|
68
|
+
// Section 5: For X25519, in order to decode 32 random bytes as an integer scalar,
|
|
69
|
+
// set the three least significant bits of the first byte
|
|
70
|
+
bytes[0] &= 248; // 0b1111_1000
|
|
71
|
+
// and the most significant bit of the last to zero,
|
|
72
|
+
bytes[31] &= 127; // 0b0111_1111
|
|
73
|
+
// set the second most significant bit of the last byte to 1
|
|
74
|
+
bytes[31] |= 64; // 0b0100_0000
|
|
75
|
+
return bytes;
|
|
76
|
+
}
|
|
77
|
+
const x25519 = /* @__PURE__ */ (() => {
|
|
78
|
+
const P = ed25519_CURVE_p;
|
|
79
|
+
return (0,esm_mod.montgomery)({
|
|
80
|
+
P,
|
|
81
|
+
type: "x25519",
|
|
82
|
+
powPminus2: (x) => {
|
|
83
|
+
// x^(p-2) aka x^(2^255-21)
|
|
84
|
+
const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x);
|
|
85
|
+
return (0,esm_mod.mod)((0,esm_mod.pow2)(pow_p_5_8, _3n, P) * b2, P);
|
|
86
|
+
},
|
|
87
|
+
adjustScalarBytes,
|
|
88
|
+
});
|
|
89
|
+
})();
|
|
90
|
+
|
|
91
|
+
;// ./node_modules/@hpke/dhkem-x25519/esm/src/hkdfSha256.js
|
|
92
|
+
|
|
93
|
+
class HkdfSha256 extends esm_mod.HkdfSha256Native {
|
|
94
|
+
async extract(salt, ikm) {
|
|
95
|
+
await this._setup();
|
|
96
|
+
if (salt.byteLength === 0) {
|
|
97
|
+
salt = new ArrayBuffer(this.hashSize);
|
|
98
|
+
}
|
|
99
|
+
if (salt.byteLength !== this.hashSize) {
|
|
100
|
+
return (0,esm_mod.hmac)(esm_mod.sha256, new Uint8Array(salt), new Uint8Array(ikm))
|
|
101
|
+
.buffer;
|
|
102
|
+
}
|
|
103
|
+
const key = await this._api.importKey("raw", salt, this.algHash, false, [
|
|
104
|
+
"sign",
|
|
105
|
+
]);
|
|
106
|
+
return await this._api.sign("HMAC", key, ikm);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
;// ./node_modules/@hpke/dhkem-x25519/esm/src/dhkemX25519.js
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
const ALG_NAME = "X25519";
|
|
115
|
+
class X25519 {
|
|
116
|
+
constructor(hkdf) {
|
|
117
|
+
Object.defineProperty(this, "_hkdf", {
|
|
118
|
+
enumerable: true,
|
|
119
|
+
configurable: true,
|
|
120
|
+
writable: true,
|
|
121
|
+
value: void 0
|
|
122
|
+
});
|
|
123
|
+
Object.defineProperty(this, "_nPk", {
|
|
124
|
+
enumerable: true,
|
|
125
|
+
configurable: true,
|
|
126
|
+
writable: true,
|
|
127
|
+
value: void 0
|
|
128
|
+
});
|
|
129
|
+
Object.defineProperty(this, "_nSk", {
|
|
130
|
+
enumerable: true,
|
|
131
|
+
configurable: true,
|
|
132
|
+
writable: true,
|
|
133
|
+
value: void 0
|
|
134
|
+
});
|
|
135
|
+
this._hkdf = hkdf;
|
|
136
|
+
this._nPk = 32;
|
|
137
|
+
this._nSk = 32;
|
|
138
|
+
}
|
|
139
|
+
async serializePublicKey(key) {
|
|
140
|
+
try {
|
|
141
|
+
return await this._serializePublicKey(key);
|
|
142
|
+
}
|
|
143
|
+
catch (e) {
|
|
144
|
+
throw new esm_mod.SerializeError(e);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
async deserializePublicKey(key) {
|
|
148
|
+
try {
|
|
149
|
+
return await this._importRawKey(key, true);
|
|
150
|
+
}
|
|
151
|
+
catch (e) {
|
|
152
|
+
throw new esm_mod.DeserializeError(e);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
async serializePrivateKey(key) {
|
|
156
|
+
try {
|
|
157
|
+
return await this._serializePrivateKey(key);
|
|
158
|
+
}
|
|
159
|
+
catch (e) {
|
|
160
|
+
throw new esm_mod.SerializeError(e);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
async deserializePrivateKey(key) {
|
|
164
|
+
try {
|
|
165
|
+
return await this._importRawKey(key, false);
|
|
166
|
+
}
|
|
167
|
+
catch (e) {
|
|
168
|
+
throw new esm_mod.DeserializeError(e);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
async importKey(format, key, isPublic) {
|
|
172
|
+
try {
|
|
173
|
+
if (format === "raw") {
|
|
174
|
+
return await this._importRawKey(key, isPublic);
|
|
175
|
+
}
|
|
176
|
+
// jwk
|
|
177
|
+
if (key instanceof ArrayBuffer) {
|
|
178
|
+
throw new Error("Invalid jwk key format");
|
|
179
|
+
}
|
|
180
|
+
return await this._importJWK(key, isPublic);
|
|
181
|
+
}
|
|
182
|
+
catch (e) {
|
|
183
|
+
throw new esm_mod.DeserializeError(e);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async generateKeyPair() {
|
|
187
|
+
try {
|
|
188
|
+
const rawSk = await x25519.utils.randomSecretKey();
|
|
189
|
+
const sk = new esm_mod.XCryptoKey(ALG_NAME, rawSk, "private", esm_mod.KEM_USAGES);
|
|
190
|
+
const pk = await this.derivePublicKey(sk);
|
|
191
|
+
return { publicKey: pk, privateKey: sk };
|
|
192
|
+
}
|
|
193
|
+
catch (e) {
|
|
194
|
+
throw new esm_mod.NotSupportedError(e);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
async deriveKeyPair(ikm) {
|
|
198
|
+
try {
|
|
199
|
+
const dkpPrk = await this._hkdf.labeledExtract(esm_mod.EMPTY.buffer, esm_mod.LABEL_DKP_PRK, new Uint8Array(ikm));
|
|
200
|
+
const rawSk = await this._hkdf.labeledExpand(dkpPrk, esm_mod.LABEL_SK, esm_mod.EMPTY, this._nSk);
|
|
201
|
+
const sk = new esm_mod.XCryptoKey(ALG_NAME, new Uint8Array(rawSk), "private", esm_mod.KEM_USAGES);
|
|
202
|
+
return {
|
|
203
|
+
privateKey: sk,
|
|
204
|
+
publicKey: await this.derivePublicKey(sk),
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
catch (e) {
|
|
208
|
+
throw new esm_mod.DeriveKeyPairError(e);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
async derivePublicKey(key) {
|
|
212
|
+
try {
|
|
213
|
+
return await this._derivePublicKey(key);
|
|
214
|
+
}
|
|
215
|
+
catch (e) {
|
|
216
|
+
throw new esm_mod.DeserializeError(e);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
async dh(sk, pk) {
|
|
220
|
+
try {
|
|
221
|
+
return await this._dh(sk, pk);
|
|
222
|
+
}
|
|
223
|
+
catch (e) {
|
|
224
|
+
throw new esm_mod.SerializeError(e);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
async derive(sk, pk) {
|
|
228
|
+
try {
|
|
229
|
+
return await this._derive(sk, pk);
|
|
230
|
+
}
|
|
231
|
+
catch (e) {
|
|
232
|
+
throw new esm_mod.SerializeError(e);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
_serializePublicKey(k) {
|
|
236
|
+
return new Promise((resolve) => {
|
|
237
|
+
resolve(k.key.buffer);
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
_serializePrivateKey(k) {
|
|
241
|
+
return new Promise((resolve) => {
|
|
242
|
+
resolve(k.key.buffer);
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
_importRawKey(key, isPublic) {
|
|
246
|
+
return new Promise((resolve, reject) => {
|
|
247
|
+
if (isPublic && key.byteLength !== this._nPk) {
|
|
248
|
+
reject(new Error("Invalid length of the key"));
|
|
249
|
+
}
|
|
250
|
+
if (!isPublic && key.byteLength !== this._nSk) {
|
|
251
|
+
reject(new Error("Invalid length of the key"));
|
|
252
|
+
}
|
|
253
|
+
resolve(new esm_mod.XCryptoKey(ALG_NAME, new Uint8Array(key), isPublic ? "public" : "private", isPublic ? [] : esm_mod.KEM_USAGES));
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
_importJWK(key, isPublic) {
|
|
257
|
+
return new Promise((resolve, reject) => {
|
|
258
|
+
if (typeof key.kty === "undefined" || key.kty !== "OKP") {
|
|
259
|
+
reject(new Error(`Invalid kty: ${key.kty}`));
|
|
260
|
+
}
|
|
261
|
+
if (typeof key.crv === "undefined" || key.crv !== "X25519") {
|
|
262
|
+
reject(new Error(`Invalid crv: ${key.crv}`));
|
|
263
|
+
}
|
|
264
|
+
if (isPublic) {
|
|
265
|
+
if (typeof key.d !== "undefined") {
|
|
266
|
+
reject(new Error("Invalid key: `d` should not be set"));
|
|
267
|
+
}
|
|
268
|
+
if (typeof key.x === "undefined") {
|
|
269
|
+
reject(new Error("Invalid key: `x` not found"));
|
|
270
|
+
}
|
|
271
|
+
resolve(new esm_mod.XCryptoKey(ALG_NAME, (0,esm_mod.base64UrlToBytes)(key.x), "public"));
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
if (typeof key.d !== "string") {
|
|
275
|
+
reject(new Error("Invalid key: `d` not found"));
|
|
276
|
+
}
|
|
277
|
+
resolve(new esm_mod.XCryptoKey(ALG_NAME, (0,esm_mod.base64UrlToBytes)(key.d), "private", esm_mod.KEM_USAGES));
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
_derivePublicKey(k) {
|
|
282
|
+
return new Promise((resolve, reject) => {
|
|
283
|
+
try {
|
|
284
|
+
const pk = x25519.getPublicKey(k.key);
|
|
285
|
+
resolve(new esm_mod.XCryptoKey(ALG_NAME, pk, "public"));
|
|
286
|
+
}
|
|
287
|
+
catch (e) {
|
|
288
|
+
reject(e);
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
_dh(sk, pk) {
|
|
293
|
+
return new Promise((resolve, reject) => {
|
|
294
|
+
try {
|
|
295
|
+
resolve(x25519.getSharedSecret(sk.key, pk.key).buffer);
|
|
296
|
+
}
|
|
297
|
+
catch (e) {
|
|
298
|
+
reject(e);
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
_derive(sk, pk) {
|
|
303
|
+
return new Promise((resolve, reject) => {
|
|
304
|
+
try {
|
|
305
|
+
resolve(x25519.getSharedSecret(sk, pk));
|
|
306
|
+
}
|
|
307
|
+
catch (e) {
|
|
308
|
+
reject(e);
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* The DHKEM(X25519, HKDF-SHA256) for HPKE KEM implementing {@link KemInterface}.
|
|
315
|
+
*
|
|
316
|
+
* This class is implemented using
|
|
317
|
+
* {@link https://github.com/paulmillr/noble-curves | @noble/curves}.
|
|
318
|
+
*
|
|
319
|
+
* The instance of this class can be specified to the
|
|
320
|
+
* {@link https://jsr.io/@hpke/core/doc/~/CipherSuiteParams | CipherSuiteParams} as follows:
|
|
321
|
+
*
|
|
322
|
+
* @example Use with `@hpke/core`:
|
|
323
|
+
*
|
|
324
|
+
* ```ts
|
|
325
|
+
* import {
|
|
326
|
+
* Aes128Gcm,
|
|
327
|
+
* CipherSuite,
|
|
328
|
+
* HkdfSha256,
|
|
329
|
+
* } from "@hpke/core";
|
|
330
|
+
* import { DhkemX25519HkdfSha256 } from "@hpke/dhkem-x25519";
|
|
331
|
+
*
|
|
332
|
+
* const suite = new CipherSuite({
|
|
333
|
+
* kem: new DhkemX25519HkdfSha256(),
|
|
334
|
+
* kdf: new HkdfSha256(),
|
|
335
|
+
* aead: new Aes128Gcm(),
|
|
336
|
+
* });
|
|
337
|
+
* ```
|
|
338
|
+
*/
|
|
339
|
+
class DhkemX25519HkdfSha256 extends esm_mod.Dhkem {
|
|
340
|
+
constructor() {
|
|
341
|
+
const kdf = new HkdfSha256();
|
|
342
|
+
super(esm_mod.KemId.DhkemX25519HkdfSha256, new X25519(kdf), kdf);
|
|
343
|
+
/** KemId.DhkemX25519HkdfSha256 (0x0020) */
|
|
344
|
+
Object.defineProperty(this, "id", {
|
|
345
|
+
enumerable: true,
|
|
346
|
+
configurable: true,
|
|
347
|
+
writable: true,
|
|
348
|
+
value: esm_mod.KemId.DhkemX25519HkdfSha256
|
|
349
|
+
});
|
|
350
|
+
/** 32 */
|
|
351
|
+
Object.defineProperty(this, "secretSize", {
|
|
352
|
+
enumerable: true,
|
|
353
|
+
configurable: true,
|
|
354
|
+
writable: true,
|
|
355
|
+
value: 32
|
|
356
|
+
});
|
|
357
|
+
/** 32 */
|
|
358
|
+
Object.defineProperty(this, "encSize", {
|
|
359
|
+
enumerable: true,
|
|
360
|
+
configurable: true,
|
|
361
|
+
writable: true,
|
|
362
|
+
value: 32
|
|
363
|
+
});
|
|
364
|
+
/** 32 */
|
|
365
|
+
Object.defineProperty(this, "publicKeySize", {
|
|
366
|
+
enumerable: true,
|
|
367
|
+
configurable: true,
|
|
368
|
+
writable: true,
|
|
369
|
+
value: 32
|
|
370
|
+
});
|
|
371
|
+
/** 32 */
|
|
372
|
+
Object.defineProperty(this, "privateKeySize", {
|
|
373
|
+
enumerable: true,
|
|
374
|
+
configurable: true,
|
|
375
|
+
writable: true,
|
|
376
|
+
value: 32
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
;// ./node_modules/@hpke/dhkem-x25519/esm/mod.js
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
;// ./node_modules/@hpke/hybridkem-x-wing/esm/src/xWing.js
|
|
386
|
+
// @ts-ignore: Unreachable code error
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
const xWing_ALG_NAME = "X-Wing";
|
|
390
|
+
|
|
391
|
+
// deno-fmt-ignore
|
|
392
|
+
const X25519_BASE = new Uint8Array([
|
|
393
|
+
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
394
|
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
395
|
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
396
|
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
397
|
+
]);
|
|
398
|
+
// XWingLabel = concat(
|
|
399
|
+
// "\./",
|
|
400
|
+
// "/^\",
|
|
401
|
+
// );
|
|
402
|
+
// deno-fmt-ignore
|
|
403
|
+
const XWING_LABEL = new Uint8Array([92, 46, 47, 47, 94, 92]);
|
|
404
|
+
function combiner(ssM, ssX, ctX, pkX) {
|
|
405
|
+
const ret = new Uint8Array(ssM.length + ssX.length + ctX.length + pkX.length + XWING_LABEL.length);
|
|
406
|
+
ret.set(ssM, 0);
|
|
407
|
+
ret.set(ssX, ssM.length);
|
|
408
|
+
ret.set(ctX, ssM.length + ssX.length);
|
|
409
|
+
ret.set(pkX, ssM.length + ssX.length + ctX.length);
|
|
410
|
+
ret.set(XWING_LABEL, ssM.length + ssX.length + ctX.length + pkX.length);
|
|
411
|
+
return mod.sha3_256.create().update(ret).digest();
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* The Hybrid Post-Quantum KEM (X25519, Kyber768).
|
|
415
|
+
*
|
|
416
|
+
* This class is implemented using
|
|
417
|
+
* {@link https://github.com/Argyle-Software/kyber | pqc-kyber }.
|
|
418
|
+
*
|
|
419
|
+
* The instance of this class can be specified to the
|
|
420
|
+
* {@link https://jsr.io/@hpke/core/doc/~/CipherSuiteParams | CipherSuiteParams} as follows:
|
|
421
|
+
*
|
|
422
|
+
* @example Use with `@hpke/core`:
|
|
423
|
+
*
|
|
424
|
+
* ```ts
|
|
425
|
+
* import { Aes128Gcm, CipherSuite, HkdfSha256 } from "@hpke/core";
|
|
426
|
+
* import { XWing } from "@hpke/hybridkem-x-wing";
|
|
427
|
+
* const suite = new CipherSuite({
|
|
428
|
+
* kem: new XWing(),
|
|
429
|
+
* kdf: new HkdfSha256(),
|
|
430
|
+
* aead: new Aes128Gcm(),
|
|
431
|
+
* });
|
|
432
|
+
* ```
|
|
433
|
+
*/
|
|
434
|
+
class XWing {
|
|
435
|
+
constructor() {
|
|
436
|
+
Object.defineProperty(this, "id", {
|
|
437
|
+
enumerable: true,
|
|
438
|
+
configurable: true,
|
|
439
|
+
writable: true,
|
|
440
|
+
value: esm_mod.KemId.XWing
|
|
441
|
+
});
|
|
442
|
+
Object.defineProperty(this, "name", {
|
|
443
|
+
enumerable: true,
|
|
444
|
+
configurable: true,
|
|
445
|
+
writable: true,
|
|
446
|
+
value: xWing_ALG_NAME
|
|
447
|
+
});
|
|
448
|
+
Object.defineProperty(this, "secretSize", {
|
|
449
|
+
enumerable: true,
|
|
450
|
+
configurable: true,
|
|
451
|
+
writable: true,
|
|
452
|
+
value: 32
|
|
453
|
+
});
|
|
454
|
+
Object.defineProperty(this, "encSize", {
|
|
455
|
+
enumerable: true,
|
|
456
|
+
configurable: true,
|
|
457
|
+
writable: true,
|
|
458
|
+
value: 1120
|
|
459
|
+
});
|
|
460
|
+
Object.defineProperty(this, "publicKeySize", {
|
|
461
|
+
enumerable: true,
|
|
462
|
+
configurable: true,
|
|
463
|
+
writable: true,
|
|
464
|
+
value: 1216
|
|
465
|
+
});
|
|
466
|
+
Object.defineProperty(this, "privateKeySize", {
|
|
467
|
+
enumerable: true,
|
|
468
|
+
configurable: true,
|
|
469
|
+
writable: true,
|
|
470
|
+
value: 32
|
|
471
|
+
});
|
|
472
|
+
Object.defineProperty(this, "auth", {
|
|
473
|
+
enumerable: true,
|
|
474
|
+
configurable: true,
|
|
475
|
+
writable: true,
|
|
476
|
+
value: false
|
|
477
|
+
});
|
|
478
|
+
Object.defineProperty(this, "_m", {
|
|
479
|
+
enumerable: true,
|
|
480
|
+
configurable: true,
|
|
481
|
+
writable: true,
|
|
482
|
+
value: void 0
|
|
483
|
+
});
|
|
484
|
+
Object.defineProperty(this, "_x25519", {
|
|
485
|
+
enumerable: true,
|
|
486
|
+
configurable: true,
|
|
487
|
+
writable: true,
|
|
488
|
+
value: void 0
|
|
489
|
+
});
|
|
490
|
+
Object.defineProperty(this, "_api", {
|
|
491
|
+
enumerable: true,
|
|
492
|
+
configurable: true,
|
|
493
|
+
writable: true,
|
|
494
|
+
value: undefined
|
|
495
|
+
});
|
|
496
|
+
this._m = new mod.MlKem768();
|
|
497
|
+
this._x25519 = new X25519(new HkdfSha256());
|
|
498
|
+
}
|
|
499
|
+
async serializePublicKey(key) {
|
|
500
|
+
await this._setup();
|
|
501
|
+
try {
|
|
502
|
+
return await this._serializePublicKey(key);
|
|
503
|
+
}
|
|
504
|
+
catch (e) {
|
|
505
|
+
throw new esm_mod.SerializeError(e);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
async deserializePublicKey(key) {
|
|
509
|
+
await this._setup();
|
|
510
|
+
try {
|
|
511
|
+
return await this._deserializePublicKey(key);
|
|
512
|
+
}
|
|
513
|
+
catch (e) {
|
|
514
|
+
throw new esm_mod.DeserializeError(e);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
async serializePrivateKey(key) {
|
|
518
|
+
await this._setup();
|
|
519
|
+
try {
|
|
520
|
+
return await this._serializePrivateKey(key);
|
|
521
|
+
}
|
|
522
|
+
catch (e) {
|
|
523
|
+
throw new esm_mod.SerializeError(e);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
async deserializePrivateKey(key) {
|
|
527
|
+
await this._setup();
|
|
528
|
+
try {
|
|
529
|
+
return await this._deserializePrivateKey(key);
|
|
530
|
+
}
|
|
531
|
+
catch (e) {
|
|
532
|
+
throw new esm_mod.DeserializeError(e);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Generates a new key pair.
|
|
537
|
+
*
|
|
538
|
+
* @returns {Promise<CryptoKeyPair>} A promise that resolves with a new key pair.
|
|
539
|
+
*/
|
|
540
|
+
async generateKeyPair() {
|
|
541
|
+
await this._setup();
|
|
542
|
+
const sk = new Uint8Array(32);
|
|
543
|
+
try {
|
|
544
|
+
this._api.getRandomValues(sk);
|
|
545
|
+
}
|
|
546
|
+
catch (e) {
|
|
547
|
+
throw new esm_mod.NotSupportedError(e);
|
|
548
|
+
}
|
|
549
|
+
try {
|
|
550
|
+
const [_sk, pk] = await this._generateKeyPairDerand(sk);
|
|
551
|
+
const dSk = await this.deserializePrivateKey(sk.buffer);
|
|
552
|
+
const dPk = await this.deserializePublicKey(pk.buffer);
|
|
553
|
+
return { privateKey: dSk, publicKey: dPk };
|
|
554
|
+
}
|
|
555
|
+
catch (e) {
|
|
556
|
+
throw new esm_mod.DeriveKeyPairError(e);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Generates a key pair from the secret key.
|
|
561
|
+
* @param sk The secret key.
|
|
562
|
+
* @returns {Promise<CryptoKeyPair>} A promise that resolves with a new key pair.
|
|
563
|
+
* @throws {InvalidParamError} Thrown if the length of the secret key is not 32 bytes.
|
|
564
|
+
* @throws {DeriveKeyPairError} Thrown if the key pair cannot be derived.
|
|
565
|
+
*/
|
|
566
|
+
async generateKeyPairDerand(sk) {
|
|
567
|
+
if (sk.byteLength !== 32) {
|
|
568
|
+
throw new esm_mod.InvalidParamError("Invalid length of sk");
|
|
569
|
+
}
|
|
570
|
+
try {
|
|
571
|
+
const [_sk, pk] = await this._generateKeyPairDerand(sk);
|
|
572
|
+
const dSk = await this.deserializePrivateKey(sk.buffer);
|
|
573
|
+
const dPk = await this.deserializePublicKey(pk.buffer);
|
|
574
|
+
return { privateKey: dSk, publicKey: dPk };
|
|
575
|
+
}
|
|
576
|
+
catch (e) {
|
|
577
|
+
throw new esm_mod.DeriveKeyPairError(e);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Derives a key pair from the input keying material.
|
|
582
|
+
*
|
|
583
|
+
* @param {ArrayBuffer} ikm The input keying material.
|
|
584
|
+
* @returns {Promise<CryptoKeyPair>} A promise that resolves with a new key pair.
|
|
585
|
+
* @throws {DeriveKeyPairError} Thrown if the key pair cannot be derived.
|
|
586
|
+
* @throws {InvalidParamError} Thrown if the length of the IKM is not 32 bytes.
|
|
587
|
+
*/
|
|
588
|
+
async deriveKeyPair(ikm) {
|
|
589
|
+
await this._setup();
|
|
590
|
+
try {
|
|
591
|
+
const sk = mod.shake256.create({ dkLen: 32 })
|
|
592
|
+
.update(new Uint8Array(ikm))
|
|
593
|
+
.digest();
|
|
594
|
+
const [_sk, pk] = await this._generateKeyPairDerand(sk);
|
|
595
|
+
const dSk = await this.deserializePrivateKey(sk.buffer);
|
|
596
|
+
const dPk = await this.deserializePublicKey(pk.buffer);
|
|
597
|
+
return { privateKey: dSk, publicKey: dPk };
|
|
598
|
+
}
|
|
599
|
+
catch (e) {
|
|
600
|
+
throw new esm_mod.DeriveKeyPairError(e);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Imports a key from the input.
|
|
605
|
+
* @param format The format of the key. "raw" or "jwk" can be specified.
|
|
606
|
+
* @param key The key to import. If the format is "raw", the key must be an ArrayBuffer. If the format is "jwk", the key must be a JsonWebKey.
|
|
607
|
+
* @param isPublic A boolean indicating whether the key is public or not. The default is true.
|
|
608
|
+
* @returns {Promise<CryptoKey>} A promise that resolves with the imported key.
|
|
609
|
+
* @throws {DeserializeError} Thrown if the key cannot be imported.
|
|
610
|
+
*/
|
|
611
|
+
async importKey(format, key, isPublic = true) {
|
|
612
|
+
await this._setup();
|
|
613
|
+
try {
|
|
614
|
+
let ret;
|
|
615
|
+
if (format === "jwk") {
|
|
616
|
+
if (key instanceof ArrayBuffer || key instanceof Uint8Array) {
|
|
617
|
+
throw new Error("Invalid jwk key format");
|
|
618
|
+
}
|
|
619
|
+
ret = await this._importJWK(key, isPublic);
|
|
620
|
+
}
|
|
621
|
+
else {
|
|
622
|
+
if (key instanceof ArrayBuffer) {
|
|
623
|
+
ret = new Uint8Array(key);
|
|
624
|
+
}
|
|
625
|
+
else if (key instanceof Uint8Array) {
|
|
626
|
+
ret = key;
|
|
627
|
+
}
|
|
628
|
+
else {
|
|
629
|
+
throw new Error("Invalid key format");
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
if (isPublic && ret.byteLength !== this.publicKeySize) {
|
|
633
|
+
throw new Error("Invalid length of the key");
|
|
634
|
+
}
|
|
635
|
+
if (!isPublic && ret.byteLength !== this.privateKeySize) {
|
|
636
|
+
throw new Error("Invalid length of the key");
|
|
637
|
+
}
|
|
638
|
+
return new esm_mod.XCryptoKey(xWing_ALG_NAME, ret, isPublic ? "public" : "private", isPublic ? [] : esm_mod.KEM_USAGES);
|
|
639
|
+
}
|
|
640
|
+
catch (e) {
|
|
641
|
+
throw new esm_mod.DeserializeError(e);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Encapsulates the shared secret and the `ct` (ciphertext) as `enc`.
|
|
646
|
+
* @param params The parameters for encapsulation.
|
|
647
|
+
* @returns {Promise<{ sharedSecret: ArrayBuffer; enc: ArrayBuffer }>} A promise that resolves with the `ss` (shared secret) as `sharedSecret` and the `ct` (ciphertext) as `enc`.
|
|
648
|
+
* @throws {InvalidParamError} Thrown if the length of the `ekm` is not 64 bytes.
|
|
649
|
+
* @throws {EncapError} Thrown if the shared secret cannot be encapsulated.
|
|
650
|
+
*/
|
|
651
|
+
async encap(params) {
|
|
652
|
+
let ekm = undefined;
|
|
653
|
+
if (params.ekm !== undefined && !(0,esm_mod.isCryptoKeyPair)(params.ekm)) {
|
|
654
|
+
if (params.ekm.byteLength !== 64) {
|
|
655
|
+
throw new esm_mod.InvalidParamError("ekm must be 64 bytes in length");
|
|
656
|
+
}
|
|
657
|
+
ekm = params.ekm;
|
|
658
|
+
}
|
|
659
|
+
let ekM = undefined;
|
|
660
|
+
let ekX;
|
|
661
|
+
if (ekm !== undefined) {
|
|
662
|
+
const ek = new Uint8Array(ekm);
|
|
663
|
+
ekM = ek.subarray(0, 32);
|
|
664
|
+
ekX = ek.subarray(32, 64);
|
|
665
|
+
}
|
|
666
|
+
else {
|
|
667
|
+
ekX = new Uint8Array(32);
|
|
668
|
+
try {
|
|
669
|
+
this._api.getRandomValues(ekX);
|
|
670
|
+
}
|
|
671
|
+
catch (e) {
|
|
672
|
+
throw new esm_mod.NotSupportedError(e);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
const pk = new Uint8Array(await this.serializePublicKey(params.recipientPublicKey));
|
|
676
|
+
if (pk.byteLength !== 1216) {
|
|
677
|
+
throw new esm_mod.InvalidParamError("Invalid length of recipientPublicKey");
|
|
678
|
+
}
|
|
679
|
+
await this._setup();
|
|
680
|
+
try {
|
|
681
|
+
const pkM = pk.subarray(0, 1184);
|
|
682
|
+
const pkX = pk.subarray(1184, 1216);
|
|
683
|
+
const ctX = await this._x25519.derive(ekX, X25519_BASE);
|
|
684
|
+
const ssX = await this._x25519.derive(ekX, pkX);
|
|
685
|
+
const [ctM, ssM] = await this._m.encap(pkM, ekM);
|
|
686
|
+
return {
|
|
687
|
+
sharedSecret: combiner(ssM, ssX, ctX, pkX).buffer,
|
|
688
|
+
enc: (0,esm_mod.concat)(ctM, ctX).buffer,
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
catch (e) {
|
|
692
|
+
throw new esm_mod.EncapError(e);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* Decapsulates the `ss` (shared secret) from the `enc` and the recipient's private key.
|
|
697
|
+
* The `enc` is the same as the `ct` (ciphertext) resulting from `X-Wing::Encapsulate(),
|
|
698
|
+
* which is executed under the `encap()`.
|
|
699
|
+
* @param params The parameters for decapsulation.
|
|
700
|
+
* @returns {Promise<ArrayBuffer>} A promise that resolves with the shared secret.
|
|
701
|
+
* @throws {InvalidParamError} Thrown if the length of the `enc` is not 1120 bytes.
|
|
702
|
+
* @throws {DecapError} Thrown if the shared secret cannot be decapsulated.
|
|
703
|
+
*/
|
|
704
|
+
async decap(params) {
|
|
705
|
+
const rSk = (0,esm_mod.isCryptoKeyPair)(params.recipientKey)
|
|
706
|
+
? params.recipientKey.privateKey
|
|
707
|
+
: params.recipientKey;
|
|
708
|
+
if (params.enc.byteLength !== 1120) {
|
|
709
|
+
throw new esm_mod.InvalidParamError("Invalid length of enc");
|
|
710
|
+
}
|
|
711
|
+
const sk = new Uint8Array(await this.serializePrivateKey(rSk));
|
|
712
|
+
if (sk.byteLength !== 32) {
|
|
713
|
+
throw new esm_mod.InvalidParamError("Invalid length of recipientKey");
|
|
714
|
+
}
|
|
715
|
+
await this._setup();
|
|
716
|
+
try {
|
|
717
|
+
const [skM, skX, _pkM, pkX] = await this._expandDecapsulationKey(sk);
|
|
718
|
+
const ct = new Uint8Array(params.enc);
|
|
719
|
+
const ctM = ct.subarray(0, 1088);
|
|
720
|
+
const ctX = ct.subarray(1088);
|
|
721
|
+
const ssM = await this._m.decap(ctM, skM);
|
|
722
|
+
const ssX = await this._x25519.derive(skX, ctX);
|
|
723
|
+
return combiner(ssM, ssX, ctX, pkX).buffer;
|
|
724
|
+
}
|
|
725
|
+
catch (e) {
|
|
726
|
+
throw new esm_mod.DecapError(e);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* Sets up the MlKemBase instance by loading the necessary crypto library.
|
|
731
|
+
* If the crypto library is already loaded, this method does nothing.
|
|
732
|
+
* @returns {Promise<void>} A promise that resolves when the setup is complete.
|
|
733
|
+
*/
|
|
734
|
+
async _setup() {
|
|
735
|
+
if (this._api !== undefined) {
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
this._api = await (0,esm_mod.loadCrypto)();
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* Generates a key pair from the secret key.
|
|
742
|
+
* @param sk The secret key.
|
|
743
|
+
* @returns {Promise<[Uint8Array, Uint8Array]>} A promise that resolves with the key pair derived from the secret key.
|
|
744
|
+
*/
|
|
745
|
+
async _generateKeyPairDerand(sk) {
|
|
746
|
+
const [_skM, _skX, pkM, pkX] = await this._expandDecapsulationKey(sk);
|
|
747
|
+
return [sk, (0,esm_mod.concat)(pkM, pkX)];
|
|
748
|
+
}
|
|
749
|
+
/**
|
|
750
|
+
* Expands the decapsulation key.
|
|
751
|
+
* @param sk The secret key.
|
|
752
|
+
* @returns {Promise<[Uint8Array, Uint8Array, Uint8Array, Uint8Array]>} A promise that resolves with the keys derived by expanding the secret key.
|
|
753
|
+
*/
|
|
754
|
+
async _expandDecapsulationKey(sk) {
|
|
755
|
+
const expanded = mod.shake256.create({ dkLen: 96 }).update(sk).digest();
|
|
756
|
+
const [pkM, skM] = await this._m.deriveKeyPair(expanded.subarray(0, 64));
|
|
757
|
+
const skX = expanded.subarray(64, 96);
|
|
758
|
+
const pkX = await this._x25519.derive(skX, X25519_BASE);
|
|
759
|
+
return [skM, skX, pkM, pkX];
|
|
760
|
+
}
|
|
761
|
+
_serializePublicKey(k) {
|
|
762
|
+
return new Promise((resolve, reject) => {
|
|
763
|
+
if (k.type !== "public") {
|
|
764
|
+
reject(new Error("Not public key"));
|
|
765
|
+
}
|
|
766
|
+
if (k.algorithm.name !== this.name) {
|
|
767
|
+
reject(new Error(`Invalid algorithm name: ${k.algorithm.name}`));
|
|
768
|
+
}
|
|
769
|
+
if (k.key.byteLength !== this.publicKeySize) {
|
|
770
|
+
reject(new Error(`Invalid key length: ${k.key.byteLength}`));
|
|
771
|
+
}
|
|
772
|
+
resolve(k.key.buffer);
|
|
773
|
+
});
|
|
774
|
+
}
|
|
775
|
+
_deserializePublicKey(k) {
|
|
776
|
+
return new Promise((resolve, reject) => {
|
|
777
|
+
if (k.byteLength !== this.publicKeySize) {
|
|
778
|
+
reject(new Error(`Invalid key length: ${k.byteLength}`));
|
|
779
|
+
}
|
|
780
|
+
resolve(new esm_mod.XCryptoKey(this.name, new Uint8Array(k), "public"));
|
|
781
|
+
});
|
|
782
|
+
}
|
|
783
|
+
_serializePrivateKey(k) {
|
|
784
|
+
return new Promise((resolve, reject) => {
|
|
785
|
+
if (k.type !== "private") {
|
|
786
|
+
reject(new Error("Not private key"));
|
|
787
|
+
}
|
|
788
|
+
if (k.algorithm.name !== this.name) {
|
|
789
|
+
reject(new Error(`Invalid algorithm name: ${k.algorithm.name}`));
|
|
790
|
+
}
|
|
791
|
+
if (k.key.byteLength !== this.privateKeySize) {
|
|
792
|
+
reject(new Error(`Invalid key length: ${k.key.byteLength}`));
|
|
793
|
+
}
|
|
794
|
+
resolve(k.key.buffer);
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
_deserializePrivateKey(k) {
|
|
798
|
+
return new Promise((resolve, reject) => {
|
|
799
|
+
if (k.byteLength !== this.privateKeySize) {
|
|
800
|
+
reject(new Error(`Invalid key length: ${k.byteLength}`));
|
|
801
|
+
}
|
|
802
|
+
resolve(new esm_mod.XCryptoKey(this.name, new Uint8Array(k), "private", ["deriveBits"]));
|
|
803
|
+
});
|
|
804
|
+
}
|
|
805
|
+
_importJWK(key, isPublic) {
|
|
806
|
+
return new Promise((resolve, reject) => {
|
|
807
|
+
if (typeof key.kty === "undefined" || key.kty !== "AKP") {
|
|
808
|
+
reject(new Error(`Invalid kty: ${key.kty}`));
|
|
809
|
+
}
|
|
810
|
+
if (typeof key.alg === "undefined" || key.alg !== xWing_ALG_NAME) {
|
|
811
|
+
reject(new Error(`Invalid alg: ${key.alg}`));
|
|
812
|
+
}
|
|
813
|
+
if (!isPublic) {
|
|
814
|
+
if (typeof key.priv === "undefined") {
|
|
815
|
+
reject(new Error("Invalid key: `priv` not found"));
|
|
816
|
+
}
|
|
817
|
+
if (typeof key.key_ops !== "undefined" &&
|
|
818
|
+
(key.key_ops.length !== 1 || key.key_ops[0] !== "deriveBits")) {
|
|
819
|
+
reject(new Error("Invalid key: `key_ops` should be ['deriveBits']"));
|
|
820
|
+
}
|
|
821
|
+
resolve((0,esm_mod.base64UrlToBytes)(key.priv));
|
|
822
|
+
}
|
|
823
|
+
if (typeof key.priv !== "undefined") {
|
|
824
|
+
reject(new Error("Invalid key: `priv` should not be set"));
|
|
825
|
+
}
|
|
826
|
+
if (typeof key.pub === "undefined") {
|
|
827
|
+
reject(new Error("Invalid key: `pub` not found"));
|
|
828
|
+
}
|
|
829
|
+
if (typeof key.key_ops !== "undefined" && key.key_ops.length > 0) {
|
|
830
|
+
reject(new Error("Invalid key: `key_ops` should not be set"));
|
|
831
|
+
}
|
|
832
|
+
resolve((0,esm_mod.base64UrlToBytes)(key.pub));
|
|
833
|
+
});
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
;// ./node_modules/@hpke/hybridkem-x-wing/esm/mod.js
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
/***/ })
|
|
842
|
+
|
|
843
|
+
}]);
|
|
844
|
+
//# sourceMappingURL=defaultVendors-node_modules_hpke_hybridkem-x-wing_esm_mod_js.shogun-core.js.map
|