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
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(this["webpackChunkShogunCore"] = this["webpackChunkShogunCore"] || []).push([["node_modules_hpke_ml-kem_esm_mod_js"],{
|
|
3
|
+
|
|
4
|
+
/***/ "./node_modules/@hpke/ml-kem/esm/mod.js":
|
|
5
|
+
/*!**********************************************************!*\
|
|
6
|
+
!*** ./node_modules/@hpke/ml-kem/esm/mod.js + 1 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
|
+
MlKem1024: () => (/* reexport */ MlKem1024),
|
|
16
|
+
MlKem512: () => (/* reexport */ MlKem512),
|
|
17
|
+
MlKem768: () => (/* reexport */ MlKem768)
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
// EXTERNAL MODULE: ./node_modules/mlkem/esm/mod.js + 12 modules
|
|
21
|
+
var mod = __webpack_require__("./node_modules/mlkem/esm/mod.js");
|
|
22
|
+
// EXTERNAL MODULE: ./node_modules/@hpke/common/esm/mod.js + 23 modules
|
|
23
|
+
var esm_mod = __webpack_require__("./node_modules/@hpke/common/esm/mod.js");
|
|
24
|
+
;// ./node_modules/@hpke/ml-kem/esm/src/mlKem.js
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The base class of ML-KEM.
|
|
29
|
+
*/
|
|
30
|
+
class MlKemBase {
|
|
31
|
+
constructor() {
|
|
32
|
+
Object.defineProperty(this, "id", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
writable: true,
|
|
36
|
+
value: 0
|
|
37
|
+
});
|
|
38
|
+
Object.defineProperty(this, "name", {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
configurable: true,
|
|
41
|
+
writable: true,
|
|
42
|
+
value: ""
|
|
43
|
+
});
|
|
44
|
+
Object.defineProperty(this, "secretSize", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
configurable: true,
|
|
47
|
+
writable: true,
|
|
48
|
+
value: 0
|
|
49
|
+
});
|
|
50
|
+
Object.defineProperty(this, "encSize", {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
configurable: true,
|
|
53
|
+
writable: true,
|
|
54
|
+
value: 0
|
|
55
|
+
});
|
|
56
|
+
Object.defineProperty(this, "publicKeySize", {
|
|
57
|
+
enumerable: true,
|
|
58
|
+
configurable: true,
|
|
59
|
+
writable: true,
|
|
60
|
+
value: 0
|
|
61
|
+
});
|
|
62
|
+
Object.defineProperty(this, "privateKeySize", {
|
|
63
|
+
enumerable: true,
|
|
64
|
+
configurable: true,
|
|
65
|
+
writable: true,
|
|
66
|
+
value: 0
|
|
67
|
+
});
|
|
68
|
+
Object.defineProperty(this, "auth", {
|
|
69
|
+
enumerable: true,
|
|
70
|
+
configurable: true,
|
|
71
|
+
writable: true,
|
|
72
|
+
value: false
|
|
73
|
+
});
|
|
74
|
+
Object.defineProperty(this, "_prim", {
|
|
75
|
+
enumerable: true,
|
|
76
|
+
configurable: true,
|
|
77
|
+
writable: true,
|
|
78
|
+
value: undefined
|
|
79
|
+
});
|
|
80
|
+
Object.defineProperty(this, "_api", {
|
|
81
|
+
enumerable: true,
|
|
82
|
+
configurable: true,
|
|
83
|
+
writable: true,
|
|
84
|
+
value: undefined
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
async serializePublicKey(key) {
|
|
88
|
+
try {
|
|
89
|
+
return await this._serializePublicKey(key);
|
|
90
|
+
}
|
|
91
|
+
catch (e) {
|
|
92
|
+
throw new esm_mod.SerializeError(e);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async deserializePublicKey(key) {
|
|
96
|
+
try {
|
|
97
|
+
return await this._deserializePublicKey(key);
|
|
98
|
+
}
|
|
99
|
+
catch (e) {
|
|
100
|
+
throw new esm_mod.DeserializeError(e);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
async serializePrivateKey(key) {
|
|
104
|
+
try {
|
|
105
|
+
return await this._serializePrivateKey(key);
|
|
106
|
+
}
|
|
107
|
+
catch (e) {
|
|
108
|
+
throw new esm_mod.SerializeError(e);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async deserializePrivateKey(key) {
|
|
112
|
+
try {
|
|
113
|
+
return await this._deserializePrivateKey(key);
|
|
114
|
+
}
|
|
115
|
+
catch (e) {
|
|
116
|
+
throw new esm_mod.DeserializeError(e);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
async generateKeyPair() {
|
|
120
|
+
await this._setup();
|
|
121
|
+
const d = new Uint8Array(32);
|
|
122
|
+
const z = new Uint8Array(32);
|
|
123
|
+
try {
|
|
124
|
+
this._api.getRandomValues(d);
|
|
125
|
+
this._api.getRandomValues(z);
|
|
126
|
+
}
|
|
127
|
+
catch (e) {
|
|
128
|
+
throw new esm_mod.NotSupportedError(e);
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
const sk = (0,esm_mod.concat)(d, z);
|
|
132
|
+
const [pk, _sk] = await this._prim.deriveKeyPair(sk);
|
|
133
|
+
const dSk = await this.deserializePrivateKey(sk.buffer);
|
|
134
|
+
const dPk = await this.deserializePublicKey(pk.buffer);
|
|
135
|
+
return { privateKey: dSk, publicKey: dPk };
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
138
|
+
throw new esm_mod.DeriveKeyPairError(e);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
async deriveKeyPair(ikm) {
|
|
142
|
+
try {
|
|
143
|
+
const [pk, _] = await this._prim.deriveKeyPair(new Uint8Array(ikm));
|
|
144
|
+
const dSk = await this.deserializePrivateKey(ikm);
|
|
145
|
+
const dPk = await this.deserializePublicKey(pk.buffer);
|
|
146
|
+
return { privateKey: dSk, publicKey: dPk };
|
|
147
|
+
}
|
|
148
|
+
catch (e) {
|
|
149
|
+
throw new esm_mod.DeriveKeyPairError(e);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
async importKey(format, key, isPublic = true) {
|
|
153
|
+
await this._setup();
|
|
154
|
+
try {
|
|
155
|
+
let ret;
|
|
156
|
+
if (format === "jwk") {
|
|
157
|
+
if (key instanceof ArrayBuffer || key instanceof Uint8Array) {
|
|
158
|
+
throw new Error("Invalid jwk key format");
|
|
159
|
+
}
|
|
160
|
+
ret = await this._importJWK(key, isPublic);
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
if (key instanceof ArrayBuffer) {
|
|
164
|
+
ret = new Uint8Array(key);
|
|
165
|
+
}
|
|
166
|
+
else if (key instanceof Uint8Array) {
|
|
167
|
+
ret = key;
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
throw new Error("Invalid key format");
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (isPublic && ret.byteLength !== this.publicKeySize) {
|
|
174
|
+
throw new Error("Invalid length of the key");
|
|
175
|
+
}
|
|
176
|
+
if (!isPublic && ret.byteLength !== this.privateKeySize) {
|
|
177
|
+
throw new Error("Invalid length of the key");
|
|
178
|
+
}
|
|
179
|
+
return new esm_mod.XCryptoKey(this.name, ret, isPublic ? "public" : "private", isPublic ? [] : esm_mod.KEM_USAGES);
|
|
180
|
+
}
|
|
181
|
+
catch (e) {
|
|
182
|
+
throw new esm_mod.DeserializeError(e);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
async encap(params) {
|
|
186
|
+
let ekm = undefined;
|
|
187
|
+
if (params.ekm !== undefined) {
|
|
188
|
+
if (params.ekm instanceof ArrayBuffer) {
|
|
189
|
+
ekm = new Uint8Array(params.ekm);
|
|
190
|
+
}
|
|
191
|
+
else if (params.ekm instanceof Uint8Array) {
|
|
192
|
+
ekm = params.ekm;
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
throw new esm_mod.InvalidParamError("ekm must be 32 bytes in length");
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const pk = new Uint8Array(await this.serializePublicKey(params.recipientPublicKey));
|
|
199
|
+
if (pk.byteLength !== this.publicKeySize) {
|
|
200
|
+
throw new esm_mod.InvalidParamError("Invalid length of recipientKey");
|
|
201
|
+
}
|
|
202
|
+
try {
|
|
203
|
+
const [ct, ss] = await this._prim.encap(pk, ekm);
|
|
204
|
+
return {
|
|
205
|
+
sharedSecret: ss.buffer,
|
|
206
|
+
enc: ct.buffer,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
catch (e) {
|
|
210
|
+
throw new esm_mod.EncapError(e);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
async decap(params) {
|
|
214
|
+
const rSk = (0,esm_mod.isCryptoKeyPair)(params.recipientKey)
|
|
215
|
+
? params.recipientKey.privateKey
|
|
216
|
+
: params.recipientKey;
|
|
217
|
+
if (params.enc.byteLength !== this.encSize) {
|
|
218
|
+
throw new esm_mod.InvalidParamError("Invalid length of enc");
|
|
219
|
+
}
|
|
220
|
+
const ct = new Uint8Array(params.enc);
|
|
221
|
+
const sk = new Uint8Array(await this.serializePrivateKey(rSk));
|
|
222
|
+
if (sk.byteLength !== this.privateKeySize) {
|
|
223
|
+
throw new esm_mod.InvalidParamError("Invalid length of recipientKey");
|
|
224
|
+
}
|
|
225
|
+
try {
|
|
226
|
+
const [_, exSk] = await this._prim.deriveKeyPair(sk);
|
|
227
|
+
return (await this._prim.decap(ct, exSk))
|
|
228
|
+
.buffer;
|
|
229
|
+
}
|
|
230
|
+
catch (e) {
|
|
231
|
+
throw new esm_mod.DecapError(e);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
async _setup() {
|
|
235
|
+
if (this._api !== undefined) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
this._api = await (0,esm_mod.loadCrypto)();
|
|
239
|
+
}
|
|
240
|
+
_serializePublicKey(k) {
|
|
241
|
+
return new Promise((resolve, reject) => {
|
|
242
|
+
if (k.type !== "public") {
|
|
243
|
+
reject(new Error("Not public key"));
|
|
244
|
+
}
|
|
245
|
+
if (k.algorithm.name !== this.name) {
|
|
246
|
+
reject(new Error(`Invalid algorithm name: ${k.algorithm.name}`));
|
|
247
|
+
}
|
|
248
|
+
if (k.key.byteLength !== this.publicKeySize) {
|
|
249
|
+
reject(new Error(`Invalid key length: ${k.key.byteLength}`));
|
|
250
|
+
}
|
|
251
|
+
resolve(k.key.buffer);
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
_deserializePublicKey(k) {
|
|
255
|
+
return new Promise((resolve, reject) => {
|
|
256
|
+
if (k.byteLength !== this.publicKeySize) {
|
|
257
|
+
reject(new Error(`Invalid key length: ${k.byteLength}`));
|
|
258
|
+
}
|
|
259
|
+
resolve(new esm_mod.XCryptoKey(this.name, new Uint8Array(k), "public"));
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
_serializePrivateKey(k) {
|
|
263
|
+
return new Promise((resolve, reject) => {
|
|
264
|
+
if (k.type !== "private") {
|
|
265
|
+
reject(new Error("Not private key"));
|
|
266
|
+
}
|
|
267
|
+
if (k.algorithm.name !== this.name) {
|
|
268
|
+
reject(new Error(`Invalid algorithm name: ${k.algorithm.name}`));
|
|
269
|
+
}
|
|
270
|
+
if (k.key.byteLength !== this.privateKeySize) {
|
|
271
|
+
reject(new Error(`Invalid key length: ${k.key.byteLength}`));
|
|
272
|
+
}
|
|
273
|
+
resolve(k.key.buffer);
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
_deserializePrivateKey(k) {
|
|
277
|
+
return new Promise((resolve, reject) => {
|
|
278
|
+
if (k.byteLength !== this.privateKeySize) {
|
|
279
|
+
reject(new Error(`Invalid key length: ${k.byteLength}`));
|
|
280
|
+
}
|
|
281
|
+
resolve(new esm_mod.XCryptoKey(this.name, new Uint8Array(k), "private", ["deriveBits"]));
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
_importJWK(key, isPublic) {
|
|
285
|
+
return new Promise((resolve, reject) => {
|
|
286
|
+
if (typeof key.kty === "undefined" || key.kty !== "AKP") {
|
|
287
|
+
reject(new Error(`Invalid kty: ${key.kty}`));
|
|
288
|
+
}
|
|
289
|
+
if (typeof key.alg === "undefined" || key.alg !== this.name) {
|
|
290
|
+
reject(new Error(`Invalid alg: ${key.alg}`));
|
|
291
|
+
}
|
|
292
|
+
if (!isPublic) {
|
|
293
|
+
if (typeof key.priv === "undefined") {
|
|
294
|
+
reject(new Error("Invalid key: `priv` not found"));
|
|
295
|
+
}
|
|
296
|
+
if (typeof key.key_ops !== "undefined" &&
|
|
297
|
+
(key.key_ops.length !== 1 || key.key_ops[0] !== "deriveBits")) {
|
|
298
|
+
reject(new Error("Invalid key: `key_ops` should be ['deriveBits']"));
|
|
299
|
+
}
|
|
300
|
+
resolve((0,esm_mod.base64UrlToBytes)(key.priv));
|
|
301
|
+
}
|
|
302
|
+
if (typeof key.priv !== "undefined") {
|
|
303
|
+
reject(new Error("Invalid key: `priv` should not be set"));
|
|
304
|
+
}
|
|
305
|
+
if (typeof key.pub === "undefined") {
|
|
306
|
+
reject(new Error("Invalid key: `pub` not found"));
|
|
307
|
+
}
|
|
308
|
+
if (typeof key.key_ops !== "undefined" && key.key_ops.length > 0) {
|
|
309
|
+
reject(new Error("Invalid key: `key_ops` should not be set"));
|
|
310
|
+
}
|
|
311
|
+
resolve((0,esm_mod.base64UrlToBytes)(key.pub));
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* The ML-KEM-512 for HPKE KEM implementing {@link KemInterface}.
|
|
317
|
+
*
|
|
318
|
+
* The instance of this class can be specified to the
|
|
319
|
+
* {@link https://jsr.io/@hpke/core/doc/~/CipherSuiteParams | CipherSuiteParams} as follows:
|
|
320
|
+
*
|
|
321
|
+
* @example
|
|
322
|
+
*
|
|
323
|
+
* ```ts
|
|
324
|
+
* import { Aes128Gcm, CipherSuite, HkdfSha256 } from "@hpke/core";
|
|
325
|
+
* import { MlKem512 } from "@hpke/ml-kem";
|
|
326
|
+
* const suite = new CipherSuite({
|
|
327
|
+
* kem: new MlKem512(),
|
|
328
|
+
* kdf: new HkdfSha256(),
|
|
329
|
+
* aead: new Aes128Gcm(),
|
|
330
|
+
* });
|
|
331
|
+
* ```
|
|
332
|
+
*/
|
|
333
|
+
class MlKem512 extends MlKemBase {
|
|
334
|
+
constructor() {
|
|
335
|
+
super();
|
|
336
|
+
Object.defineProperty(this, "id", {
|
|
337
|
+
enumerable: true,
|
|
338
|
+
configurable: true,
|
|
339
|
+
writable: true,
|
|
340
|
+
value: esm_mod.KemId.MlKem512
|
|
341
|
+
});
|
|
342
|
+
Object.defineProperty(this, "name", {
|
|
343
|
+
enumerable: true,
|
|
344
|
+
configurable: true,
|
|
345
|
+
writable: true,
|
|
346
|
+
value: "ML-KEM-512"
|
|
347
|
+
});
|
|
348
|
+
Object.defineProperty(this, "secretSize", {
|
|
349
|
+
enumerable: true,
|
|
350
|
+
configurable: true,
|
|
351
|
+
writable: true,
|
|
352
|
+
value: 32
|
|
353
|
+
});
|
|
354
|
+
Object.defineProperty(this, "encSize", {
|
|
355
|
+
enumerable: true,
|
|
356
|
+
configurable: true,
|
|
357
|
+
writable: true,
|
|
358
|
+
value: 768
|
|
359
|
+
});
|
|
360
|
+
Object.defineProperty(this, "publicKeySize", {
|
|
361
|
+
enumerable: true,
|
|
362
|
+
configurable: true,
|
|
363
|
+
writable: true,
|
|
364
|
+
value: 800
|
|
365
|
+
});
|
|
366
|
+
Object.defineProperty(this, "privateKeySize", {
|
|
367
|
+
enumerable: true,
|
|
368
|
+
configurable: true,
|
|
369
|
+
writable: true,
|
|
370
|
+
value: 64
|
|
371
|
+
});
|
|
372
|
+
Object.defineProperty(this, "auth", {
|
|
373
|
+
enumerable: true,
|
|
374
|
+
configurable: true,
|
|
375
|
+
writable: true,
|
|
376
|
+
value: false
|
|
377
|
+
});
|
|
378
|
+
Object.defineProperty(this, "_prim", {
|
|
379
|
+
enumerable: true,
|
|
380
|
+
configurable: true,
|
|
381
|
+
writable: true,
|
|
382
|
+
value: undefined
|
|
383
|
+
});
|
|
384
|
+
this._prim = new mod.MlKem512();
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* The ML-KEM-768 for HPKE KEM implementing {@link KemInterface}.
|
|
389
|
+
*
|
|
390
|
+
* The instance of this class can be specified to the
|
|
391
|
+
* {@link https://jsr.io/@hpke/core/doc/~/CipherSuiteParams | CipherSuiteParams} as follows:
|
|
392
|
+
*
|
|
393
|
+
* @example
|
|
394
|
+
*
|
|
395
|
+
* ```ts
|
|
396
|
+
* import { Aes256Gcm, CipherSuite, HkdfSha384 } from "@hpke/core";
|
|
397
|
+
* import { MlKem768 } from "@hpke/ml-kem";
|
|
398
|
+
* const suite = new CipherSuite({
|
|
399
|
+
* kem: new MlKem768(),
|
|
400
|
+
* kdf: new HkdfSha384(),
|
|
401
|
+
* aead: new Aes256Gcm(),
|
|
402
|
+
* });
|
|
403
|
+
* ```
|
|
404
|
+
*/
|
|
405
|
+
class MlKem768 extends MlKemBase {
|
|
406
|
+
constructor() {
|
|
407
|
+
super();
|
|
408
|
+
Object.defineProperty(this, "id", {
|
|
409
|
+
enumerable: true,
|
|
410
|
+
configurable: true,
|
|
411
|
+
writable: true,
|
|
412
|
+
value: esm_mod.KemId.MlKem768
|
|
413
|
+
});
|
|
414
|
+
Object.defineProperty(this, "name", {
|
|
415
|
+
enumerable: true,
|
|
416
|
+
configurable: true,
|
|
417
|
+
writable: true,
|
|
418
|
+
value: "ML-KEM-768"
|
|
419
|
+
});
|
|
420
|
+
Object.defineProperty(this, "secretSize", {
|
|
421
|
+
enumerable: true,
|
|
422
|
+
configurable: true,
|
|
423
|
+
writable: true,
|
|
424
|
+
value: 32
|
|
425
|
+
});
|
|
426
|
+
Object.defineProperty(this, "encSize", {
|
|
427
|
+
enumerable: true,
|
|
428
|
+
configurable: true,
|
|
429
|
+
writable: true,
|
|
430
|
+
value: 1088
|
|
431
|
+
});
|
|
432
|
+
Object.defineProperty(this, "publicKeySize", {
|
|
433
|
+
enumerable: true,
|
|
434
|
+
configurable: true,
|
|
435
|
+
writable: true,
|
|
436
|
+
value: 1184
|
|
437
|
+
});
|
|
438
|
+
Object.defineProperty(this, "privateKeySize", {
|
|
439
|
+
enumerable: true,
|
|
440
|
+
configurable: true,
|
|
441
|
+
writable: true,
|
|
442
|
+
value: 64
|
|
443
|
+
});
|
|
444
|
+
Object.defineProperty(this, "auth", {
|
|
445
|
+
enumerable: true,
|
|
446
|
+
configurable: true,
|
|
447
|
+
writable: true,
|
|
448
|
+
value: false
|
|
449
|
+
});
|
|
450
|
+
Object.defineProperty(this, "_prim", {
|
|
451
|
+
enumerable: true,
|
|
452
|
+
configurable: true,
|
|
453
|
+
writable: true,
|
|
454
|
+
value: undefined
|
|
455
|
+
});
|
|
456
|
+
this._prim = new mod.MlKem768();
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* The ML-KEM-1024 for HPKE KEM implementing {@link KemInterface}.
|
|
461
|
+
*
|
|
462
|
+
* The instance of this class can be specified to the
|
|
463
|
+
* {@link https://jsr.io/@hpke/core/doc/~/CipherSuiteParams | CipherSuiteParams} as follows:
|
|
464
|
+
*
|
|
465
|
+
* @example
|
|
466
|
+
*
|
|
467
|
+
* ```ts
|
|
468
|
+
* import { Aes256Gcm, CipherSuite, HkdfSha512 } from "@hpke/core";
|
|
469
|
+
* import { MlKem1024 } from "@hpke/ml-kem";
|
|
470
|
+
* const suite = new CipherSuite({
|
|
471
|
+
* kem: new MlKem1024(),
|
|
472
|
+
* kdf: new HkdfSha512(),
|
|
473
|
+
* aead: new Aes256Gcm(),
|
|
474
|
+
* });
|
|
475
|
+
* ```
|
|
476
|
+
*/
|
|
477
|
+
class MlKem1024 extends MlKemBase {
|
|
478
|
+
constructor() {
|
|
479
|
+
super();
|
|
480
|
+
Object.defineProperty(this, "id", {
|
|
481
|
+
enumerable: true,
|
|
482
|
+
configurable: true,
|
|
483
|
+
writable: true,
|
|
484
|
+
value: esm_mod.KemId.MlKem1024
|
|
485
|
+
});
|
|
486
|
+
Object.defineProperty(this, "name", {
|
|
487
|
+
enumerable: true,
|
|
488
|
+
configurable: true,
|
|
489
|
+
writable: true,
|
|
490
|
+
value: "ML-KEM-1024"
|
|
491
|
+
});
|
|
492
|
+
Object.defineProperty(this, "secretSize", {
|
|
493
|
+
enumerable: true,
|
|
494
|
+
configurable: true,
|
|
495
|
+
writable: true,
|
|
496
|
+
value: 32
|
|
497
|
+
});
|
|
498
|
+
Object.defineProperty(this, "encSize", {
|
|
499
|
+
enumerable: true,
|
|
500
|
+
configurable: true,
|
|
501
|
+
writable: true,
|
|
502
|
+
value: 1568
|
|
503
|
+
});
|
|
504
|
+
Object.defineProperty(this, "publicKeySize", {
|
|
505
|
+
enumerable: true,
|
|
506
|
+
configurable: true,
|
|
507
|
+
writable: true,
|
|
508
|
+
value: 1568
|
|
509
|
+
});
|
|
510
|
+
Object.defineProperty(this, "privateKeySize", {
|
|
511
|
+
enumerable: true,
|
|
512
|
+
configurable: true,
|
|
513
|
+
writable: true,
|
|
514
|
+
value: 64
|
|
515
|
+
});
|
|
516
|
+
Object.defineProperty(this, "auth", {
|
|
517
|
+
enumerable: true,
|
|
518
|
+
configurable: true,
|
|
519
|
+
writable: true,
|
|
520
|
+
value: false
|
|
521
|
+
});
|
|
522
|
+
Object.defineProperty(this, "_prim", {
|
|
523
|
+
enumerable: true,
|
|
524
|
+
configurable: true,
|
|
525
|
+
writable: true,
|
|
526
|
+
value: undefined
|
|
527
|
+
});
|
|
528
|
+
this._prim = new mod.MlKem1024();
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
;// ./node_modules/@hpke/ml-kem/esm/mod.js
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
/***/ })
|
|
537
|
+
|
|
538
|
+
}]);
|
|
539
|
+
//# sourceMappingURL=node_modules_hpke_ml-kem_esm_mod_js.shogun-core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node_modules_hpke_ml-kem_esm_mod_js.shogun-core.js","mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAEgB;AACyN;AACzO;AACA;AACA;AACO;AACP;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA,sBAAsB,sBAAc;AACpC;AACA;AACA;AACA;AACA;AACA;AACA;AACA,sBAAsB,wBAAgB;AACtC;AACA;AACA;AACA;AACA;AACA;AACA;AACA,sBAAsB,sBAAc;AACpC;AACA;AACA;AACA;AACA;AACA;AACA;AACA,sBAAsB,wBAAgB;AACtC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,sBAAsB,yBAAiB;AACvC;AACA;AACA,uBAAuB,kBAAM;AAC7B;AACA;AACA;AACA,qBAAqB;AACrB;AACA;AACA,sBAAsB,0BAAkB;AACxC;AACA;AACA;AACA;AACA;AACA;AACA;AACA,qBAAqB;AACrB;AACA;AACA,sBAAsB,0BAAkB;AACxC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,uBAAuB,kBAAU,kEAAkE,kBAAU;AAC7G;AACA;AACA,sBAAsB,wBAAgB;AACtC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,0BAA0B,yBAAiB;AAC3C;AACA;AACA;AACA;AACA,sBAAsB,yBAAiB;AACvC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,sBAAsB,kBAAU;AAChC;AACA;AACA;AACA,oBAAoB,2BAAe;AACnC;AACA;AACA;AACA,sBAAsB,yBAAiB;AACvC;AACA;AACA;AACA;AACA,sBAAsB,yBAAiB;AACvC;AACA;AACA;AACA;AACA;AACA;AACA;AACA,sBAAsB,kBAAU;AAChC;AACA;AACA;AACA;AACA;AACA;AACA,0BAA0B,sBAAU;AACpC;AACA;AACA;AACA;AACA;AACA;AACA;AACA,4DAA4D,iBAAiB;AAC7E;AACA;AACA,wDAAwD,iBAAiB;AACzE;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA,wDAAwD,aAAa;AACrE;AACA,wBAAwB,kBAAU;AAClC,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA;AACA,4DAA4D,iBAAiB;AAC7E;AACA;AACA,wDAAwD,iBAAiB;AACzE;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA,wDAAwD,aAAa;AACrE;AACA,wBAAwB,kBAAU;AAClC,SAAS;AACT;AACA;AACA;AACA;AACA,iDAAiD,QAAQ;AACzD;AACA;AACA,iDAAiD,QAAQ;AACzD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,wBAAwB,4BAAgB;AACxC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,oBAAoB,4BAAgB;AACpC,SAAS;AACT;AACA;AACA;AACA,6CAA6C,mBAAmB;AAChE;AACA;AACA,IAAI,6EAA6E;AACjF;AACA;AACA;AACA;AACA,YAAY,qCAAqC;AACjD,YAAY,WAAW;AACvB;AACA;AACA;AACA;AACA,IAAI;AACJ;AACA;AACO;AACP;AACA;AACA;AACA;AACA;AACA;AACA,mBAAmB,aAAK;AACxB,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT,yBAAyB,YAAc;AACvC;AACA;AACA;AACA,6CAA6C,mBAAmB;AAChE;AACA;AACA,IAAI,6EAA6E;AACjF;AACA;AACA;AACA;AACA,YAAY,qCAAqC;AACjD,YAAY,WAAW;AACvB;AACA;AACA;AACA;AACA,IAAI;AACJ;AACA;AACO;AACP;AACA;AACA;AACA;AACA;AACA;AACA,mBAAmB,aAAK;AACxB,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT,yBAAyB,YAAc;AACvC;AACA;AACA;AACA,8CAA8C,mBAAmB;AACjE;AACA;AACA,IAAI,6EAA6E;AACjF;AACA;AACA;AACA;AACA,YAAY,qCAAqC;AACjD,YAAY,YAAY;AACxB;AACA;AACA;AACA;AACA,IAAI;AACJ;AACA;AACO;AACP;AACA;AACA;AACA;AACA;AACA;AACA,mBAAmB,aAAK;AACxB,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT,yBAAyB,aAAe;AACxC;AACA;;;AC3f+D","sources":["webpack://ShogunCore/./node_modules/@hpke/ml-kem/esm/src/mlKem.js","webpack://ShogunCore/./node_modules/@hpke/ml-kem/esm/mod.js"],"sourcesContent":["import { MlKem1024 as MlKem1024Native, MlKem512 as MlKem512Native, MlKem768 as MlKem768Native,\n// @ts-ignore: Unreachable code error\n } from \"mlkem\";\nimport { base64UrlToBytes, concat, DecapError, DeriveKeyPairError, DeserializeError, EncapError, InvalidParamError, isCryptoKeyPair, KEM_USAGES, KemId, loadCrypto, NotSupportedError, SerializeError, XCryptoKey, } from \"@hpke/common\";\n/**\n * The base class of ML-KEM.\n */\nexport class MlKemBase {\n constructor() {\n Object.defineProperty(this, \"id\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: 0\n });\n Object.defineProperty(this, \"name\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: \"\"\n });\n Object.defineProperty(this, \"secretSize\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: 0\n });\n Object.defineProperty(this, \"encSize\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: 0\n });\n Object.defineProperty(this, \"publicKeySize\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: 0\n });\n Object.defineProperty(this, \"privateKeySize\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: 0\n });\n Object.defineProperty(this, \"auth\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: false\n });\n Object.defineProperty(this, \"_prim\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: undefined\n });\n Object.defineProperty(this, \"_api\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: undefined\n });\n }\n async serializePublicKey(key) {\n try {\n return await this._serializePublicKey(key);\n }\n catch (e) {\n throw new SerializeError(e);\n }\n }\n async deserializePublicKey(key) {\n try {\n return await this._deserializePublicKey(key);\n }\n catch (e) {\n throw new DeserializeError(e);\n }\n }\n async serializePrivateKey(key) {\n try {\n return await this._serializePrivateKey(key);\n }\n catch (e) {\n throw new SerializeError(e);\n }\n }\n async deserializePrivateKey(key) {\n try {\n return await this._deserializePrivateKey(key);\n }\n catch (e) {\n throw new DeserializeError(e);\n }\n }\n async generateKeyPair() {\n await this._setup();\n const d = new Uint8Array(32);\n const z = new Uint8Array(32);\n try {\n this._api.getRandomValues(d);\n this._api.getRandomValues(z);\n }\n catch (e) {\n throw new NotSupportedError(e);\n }\n try {\n const sk = concat(d, z);\n const [pk, _sk] = await this._prim.deriveKeyPair(sk);\n const dSk = await this.deserializePrivateKey(sk.buffer);\n const dPk = await this.deserializePublicKey(pk.buffer);\n return { privateKey: dSk, publicKey: dPk };\n }\n catch (e) {\n throw new DeriveKeyPairError(e);\n }\n }\n async deriveKeyPair(ikm) {\n try {\n const [pk, _] = await this._prim.deriveKeyPair(new Uint8Array(ikm));\n const dSk = await this.deserializePrivateKey(ikm);\n const dPk = await this.deserializePublicKey(pk.buffer);\n return { privateKey: dSk, publicKey: dPk };\n }\n catch (e) {\n throw new DeriveKeyPairError(e);\n }\n }\n async importKey(format, key, isPublic = true) {\n await this._setup();\n try {\n let ret;\n if (format === \"jwk\") {\n if (key instanceof ArrayBuffer || key instanceof Uint8Array) {\n throw new Error(\"Invalid jwk key format\");\n }\n ret = await this._importJWK(key, isPublic);\n }\n else {\n if (key instanceof ArrayBuffer) {\n ret = new Uint8Array(key);\n }\n else if (key instanceof Uint8Array) {\n ret = key;\n }\n else {\n throw new Error(\"Invalid key format\");\n }\n }\n if (isPublic && ret.byteLength !== this.publicKeySize) {\n throw new Error(\"Invalid length of the key\");\n }\n if (!isPublic && ret.byteLength !== this.privateKeySize) {\n throw new Error(\"Invalid length of the key\");\n }\n return new XCryptoKey(this.name, ret, isPublic ? \"public\" : \"private\", isPublic ? [] : KEM_USAGES);\n }\n catch (e) {\n throw new DeserializeError(e);\n }\n }\n async encap(params) {\n let ekm = undefined;\n if (params.ekm !== undefined) {\n if (params.ekm instanceof ArrayBuffer) {\n ekm = new Uint8Array(params.ekm);\n }\n else if (params.ekm instanceof Uint8Array) {\n ekm = params.ekm;\n }\n else {\n throw new InvalidParamError(\"ekm must be 32 bytes in length\");\n }\n }\n const pk = new Uint8Array(await this.serializePublicKey(params.recipientPublicKey));\n if (pk.byteLength !== this.publicKeySize) {\n throw new InvalidParamError(\"Invalid length of recipientKey\");\n }\n try {\n const [ct, ss] = await this._prim.encap(pk, ekm);\n return {\n sharedSecret: ss.buffer,\n enc: ct.buffer,\n };\n }\n catch (e) {\n throw new EncapError(e);\n }\n }\n async decap(params) {\n const rSk = isCryptoKeyPair(params.recipientKey)\n ? params.recipientKey.privateKey\n : params.recipientKey;\n if (params.enc.byteLength !== this.encSize) {\n throw new InvalidParamError(\"Invalid length of enc\");\n }\n const ct = new Uint8Array(params.enc);\n const sk = new Uint8Array(await this.serializePrivateKey(rSk));\n if (sk.byteLength !== this.privateKeySize) {\n throw new InvalidParamError(\"Invalid length of recipientKey\");\n }\n try {\n const [_, exSk] = await this._prim.deriveKeyPair(sk);\n return (await this._prim.decap(ct, exSk))\n .buffer;\n }\n catch (e) {\n throw new DecapError(e);\n }\n }\n async _setup() {\n if (this._api !== undefined) {\n return;\n }\n this._api = await loadCrypto();\n }\n _serializePublicKey(k) {\n return new Promise((resolve, reject) => {\n if (k.type !== \"public\") {\n reject(new Error(\"Not public key\"));\n }\n if (k.algorithm.name !== this.name) {\n reject(new Error(`Invalid algorithm name: ${k.algorithm.name}`));\n }\n if (k.key.byteLength !== this.publicKeySize) {\n reject(new Error(`Invalid key length: ${k.key.byteLength}`));\n }\n resolve(k.key.buffer);\n });\n }\n _deserializePublicKey(k) {\n return new Promise((resolve, reject) => {\n if (k.byteLength !== this.publicKeySize) {\n reject(new Error(`Invalid key length: ${k.byteLength}`));\n }\n resolve(new XCryptoKey(this.name, new Uint8Array(k), \"public\"));\n });\n }\n _serializePrivateKey(k) {\n return new Promise((resolve, reject) => {\n if (k.type !== \"private\") {\n reject(new Error(\"Not private key\"));\n }\n if (k.algorithm.name !== this.name) {\n reject(new Error(`Invalid algorithm name: ${k.algorithm.name}`));\n }\n if (k.key.byteLength !== this.privateKeySize) {\n reject(new Error(`Invalid key length: ${k.key.byteLength}`));\n }\n resolve(k.key.buffer);\n });\n }\n _deserializePrivateKey(k) {\n return new Promise((resolve, reject) => {\n if (k.byteLength !== this.privateKeySize) {\n reject(new Error(`Invalid key length: ${k.byteLength}`));\n }\n resolve(new XCryptoKey(this.name, new Uint8Array(k), \"private\", [\"deriveBits\"]));\n });\n }\n _importJWK(key, isPublic) {\n return new Promise((resolve, reject) => {\n if (typeof key.kty === \"undefined\" || key.kty !== \"AKP\") {\n reject(new Error(`Invalid kty: ${key.kty}`));\n }\n if (typeof key.alg === \"undefined\" || key.alg !== this.name) {\n reject(new Error(`Invalid alg: ${key.alg}`));\n }\n if (!isPublic) {\n if (typeof key.priv === \"undefined\") {\n reject(new Error(\"Invalid key: `priv` not found\"));\n }\n if (typeof key.key_ops !== \"undefined\" &&\n (key.key_ops.length !== 1 || key.key_ops[0] !== \"deriveBits\")) {\n reject(new Error(\"Invalid key: `key_ops` should be ['deriveBits']\"));\n }\n resolve(base64UrlToBytes(key.priv));\n }\n if (typeof key.priv !== \"undefined\") {\n reject(new Error(\"Invalid key: `priv` should not be set\"));\n }\n if (typeof key.pub === \"undefined\") {\n reject(new Error(\"Invalid key: `pub` not found\"));\n }\n if (typeof key.key_ops !== \"undefined\" && key.key_ops.length > 0) {\n reject(new Error(\"Invalid key: `key_ops` should not be set\"));\n }\n resolve(base64UrlToBytes(key.pub));\n });\n }\n}\n/**\n * The ML-KEM-512 for HPKE KEM implementing {@link KemInterface}.\n *\n * The instance of this class can be specified to the\n * {@link https://jsr.io/@hpke/core/doc/~/CipherSuiteParams | CipherSuiteParams} as follows:\n *\n * @example\n *\n * ```ts\n * import { Aes128Gcm, CipherSuite, HkdfSha256 } from \"@hpke/core\";\n * import { MlKem512 } from \"@hpke/ml-kem\";\n * const suite = new CipherSuite({\n * kem: new MlKem512(),\n * kdf: new HkdfSha256(),\n * aead: new Aes128Gcm(),\n * });\n * ```\n */\nexport class MlKem512 extends MlKemBase {\n constructor() {\n super();\n Object.defineProperty(this, \"id\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: KemId.MlKem512\n });\n Object.defineProperty(this, \"name\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: \"ML-KEM-512\"\n });\n Object.defineProperty(this, \"secretSize\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: 32\n });\n Object.defineProperty(this, \"encSize\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: 768\n });\n Object.defineProperty(this, \"publicKeySize\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: 800\n });\n Object.defineProperty(this, \"privateKeySize\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: 64\n });\n Object.defineProperty(this, \"auth\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: false\n });\n Object.defineProperty(this, \"_prim\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: undefined\n });\n this._prim = new MlKem512Native();\n }\n}\n/**\n * The ML-KEM-768 for HPKE KEM implementing {@link KemInterface}.\n *\n * The instance of this class can be specified to the\n * {@link https://jsr.io/@hpke/core/doc/~/CipherSuiteParams | CipherSuiteParams} as follows:\n *\n * @example\n *\n * ```ts\n * import { Aes256Gcm, CipherSuite, HkdfSha384 } from \"@hpke/core\";\n * import { MlKem768 } from \"@hpke/ml-kem\";\n * const suite = new CipherSuite({\n * kem: new MlKem768(),\n * kdf: new HkdfSha384(),\n * aead: new Aes256Gcm(),\n * });\n * ```\n */\nexport class MlKem768 extends MlKemBase {\n constructor() {\n super();\n Object.defineProperty(this, \"id\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: KemId.MlKem768\n });\n Object.defineProperty(this, \"name\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: \"ML-KEM-768\"\n });\n Object.defineProperty(this, \"secretSize\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: 32\n });\n Object.defineProperty(this, \"encSize\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: 1088\n });\n Object.defineProperty(this, \"publicKeySize\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: 1184\n });\n Object.defineProperty(this, \"privateKeySize\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: 64\n });\n Object.defineProperty(this, \"auth\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: false\n });\n Object.defineProperty(this, \"_prim\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: undefined\n });\n this._prim = new MlKem768Native();\n }\n}\n/**\n * The ML-KEM-1024 for HPKE KEM implementing {@link KemInterface}.\n *\n * The instance of this class can be specified to the\n * {@link https://jsr.io/@hpke/core/doc/~/CipherSuiteParams | CipherSuiteParams} as follows:\n *\n * @example\n *\n * ```ts\n * import { Aes256Gcm, CipherSuite, HkdfSha512 } from \"@hpke/core\";\n * import { MlKem1024 } from \"@hpke/ml-kem\";\n * const suite = new CipherSuite({\n * kem: new MlKem1024(),\n * kdf: new HkdfSha512(),\n * aead: new Aes256Gcm(),\n * });\n * ```\n */\nexport class MlKem1024 extends MlKemBase {\n constructor() {\n super();\n Object.defineProperty(this, \"id\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: KemId.MlKem1024\n });\n Object.defineProperty(this, \"name\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: \"ML-KEM-1024\"\n });\n Object.defineProperty(this, \"secretSize\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: 32\n });\n Object.defineProperty(this, \"encSize\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: 1568\n });\n Object.defineProperty(this, \"publicKeySize\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: 1568\n });\n Object.defineProperty(this, \"privateKeySize\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: 64\n });\n Object.defineProperty(this, \"auth\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: false\n });\n Object.defineProperty(this, \"_prim\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: undefined\n });\n this._prim = new MlKem1024Native();\n }\n}\n","export { MlKem1024, MlKem512, MlKem768 } from \"./src/mlKem.js\";\n"],"names":[],"sourceRoot":""}
|