shogun-core 6.2.4 → 6.3.0
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_noble_curves_esm_ed448_js.shogun-core.js +93 -341
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js.map +1 -1
- package/dist/browser/shogun-core.js +138979 -146789
- package/dist/browser/shogun-core.js.map +1 -1
- package/dist/{core.js → src/core.js} +167 -107
- package/dist/src/crypto/asymmetric.js +168 -0
- package/dist/src/crypto/double-ratchet.js +908 -0
- package/dist/src/crypto/file-encryption.js +352 -0
- package/dist/src/crypto/hashing.js +160 -0
- package/dist/src/crypto/index.js +18 -0
- package/dist/{crypto → src/crypto}/mls-codec.js +24 -34
- package/dist/src/crypto/mls.js +734 -0
- package/dist/src/crypto/pgp.js +619 -0
- package/dist/{crypto → src/crypto}/random-generation.js +125 -103
- package/dist/src/crypto/sframe.js +466 -0
- package/dist/src/crypto/signal-protocol.js +943 -0
- package/dist/src/crypto/symmetric.js +165 -0
- package/dist/src/crypto/utils.js +220 -0
- package/dist/src/examples/auth-test.js +535 -0
- package/dist/src/examples/crypto-identity-example.js +294 -0
- package/dist/src/examples/crypto-working-test.js +149 -0
- package/dist/src/examples/double-ratchet-test.js +240 -0
- package/dist/src/examples/mls-3-member-test.js +183 -0
- package/dist/src/examples/mls-multi-member.js +439 -0
- package/dist/src/examples/mls-sframe-test.js +491 -0
- package/dist/src/examples/mls-simple-test.js +122 -0
- package/dist/src/examples/pgp-example.js +354 -0
- package/dist/src/examples/random-generation-test.js +191 -0
- package/dist/src/examples/shogun-core-example.js +204 -0
- package/dist/src/examples/signal-protocol-test.js +82 -0
- package/dist/src/examples/zkproof-credentials-example.js +357 -0
- package/dist/src/examples/zkproof-example.js +357 -0
- package/dist/src/gundb/crypto.js +420 -0
- package/dist/src/gundb/db.js +728 -0
- package/dist/src/gundb/derive.js +327 -0
- package/dist/src/gundb/errors.js +115 -0
- package/dist/src/gundb/gun-es.js +8 -0
- package/dist/src/gundb/index.js +5 -0
- package/dist/{gundb → src/gundb}/rxjs.js +147 -111
- package/dist/{gundb → src/gundb}/types.js +1 -2
- package/dist/src/index.js +19 -0
- package/dist/src/interfaces/events.js +57 -0
- package/dist/{interfaces → src/interfaces}/shogun.js +4 -7
- package/dist/src/managers/AuthManager.js +301 -0
- package/dist/src/managers/CoreInitializer.js +304 -0
- package/dist/src/managers/CryptoIdentityManager.js +230 -0
- package/dist/{managers → src/managers}/EventManager.js +19 -21
- package/dist/{managers → src/managers}/PluginManager.js +123 -89
- package/dist/src/plugins/base.js +90 -0
- package/dist/src/plugins/index.js +17 -0
- package/dist/src/plugins/nostr/index.js +4 -0
- package/dist/src/plugins/nostr/nostrConnector.js +539 -0
- package/dist/src/plugins/nostr/nostrConnectorPlugin.js +663 -0
- package/dist/src/plugins/nostr/nostrSigner.js +414 -0
- package/dist/src/plugins/smartwallet/index.js +2 -0
- package/dist/src/plugins/smartwallet/smartWalletPlugin.js +824 -0
- package/dist/src/plugins/web3/index.js +4 -0
- package/dist/src/plugins/web3/types.js +1 -0
- package/dist/src/plugins/web3/web3Connector.js +738 -0
- package/dist/src/plugins/web3/web3ConnectorPlugin.js +639 -0
- package/dist/src/plugins/web3/web3Signer.js +432 -0
- package/dist/src/plugins/webauthn/index.js +3 -0
- package/dist/{plugins → src/plugins}/webauthn/types.js +2 -5
- package/dist/src/plugins/webauthn/webauthn.js +647 -0
- package/dist/src/plugins/webauthn/webauthnPlugin.js +689 -0
- package/dist/src/plugins/webauthn/webauthnSigner.js +419 -0
- package/dist/{plugins → src/plugins}/zkproof/index.js +3 -10
- package/dist/src/plugins/zkproof/types.js +1 -0
- package/dist/src/plugins/zkproof/zkCredentials.js +287 -0
- package/dist/src/plugins/zkproof/zkProofConnector.js +267 -0
- package/dist/src/plugins/zkproof/zkProofPlugin.js +405 -0
- package/dist/src/storage/storage.js +189 -0
- package/dist/src/utils/errorHandler.js +339 -0
- package/dist/{utils → src/utils}/eventEmitter.js +26 -26
- package/dist/{utils → src/utils}/seedPhrase.js +23 -32
- package/dist/{utils → src/utils}/validation.js +14 -21
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/{crypto → src/crypto}/double-ratchet.d.ts +1 -1
- package/dist/types/{crypto → src/crypto}/signal-protocol.d.ts +25 -0
- package/dist/types/{crypto → src/crypto}/types.d.ts +3 -1
- package/dist/types/src/examples/crypto-working-test.d.ts +1 -0
- package/dist/types/src/examples/double-ratchet-test.d.ts +1 -0
- package/dist/types/src/examples/mls-sframe-test.d.ts +1 -0
- package/dist/types/src/examples/random-generation-test.d.ts +1 -0
- package/dist/types/src/examples/signal-protocol-test.d.ts +1 -0
- package/dist/types/{gundb → src/gundb}/db.d.ts +14 -1
- package/dist/types/src/gundb/gun-es.d.ts +8 -0
- package/dist/types/src/gundb/min.d.ts +3 -0
- package/dist/types/{index.d.ts → src/index.d.ts} +1 -0
- package/package.json +14 -11
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js +0 -1651
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js.map +0 -1
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js +0 -1608
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js.map +0 -1
- package/dist/crypto/asymmetric.js +0 -99
- package/dist/crypto/double-ratchet.js +0 -392
- package/dist/crypto/file-encryption.js +0 -213
- package/dist/crypto/hashing.js +0 -87
- package/dist/crypto/index.js +0 -34
- package/dist/crypto/mls.js +0 -569
- package/dist/crypto/pgp.js +0 -390
- package/dist/crypto/sframe.js +0 -352
- package/dist/crypto/signal-protocol.js +0 -456
- package/dist/crypto/symmetric.js +0 -91
- package/dist/crypto/types.js +0 -2
- package/dist/crypto/utils.js +0 -140
- package/dist/examples/auth-test.js +0 -453
- package/dist/examples/crypto-identity-example.js +0 -196
- package/dist/examples/crypto-working-test.js +0 -83
- package/dist/examples/double-ratchet-test.js +0 -155
- package/dist/examples/mls-3-member-test.js +0 -97
- package/dist/examples/mls-multi-member.js +0 -153
- package/dist/examples/mls-sframe-test.js +0 -307
- package/dist/examples/mls-simple-test.js +0 -58
- package/dist/examples/pgp-example.js +0 -200
- package/dist/examples/random-generation-test.js +0 -151
- package/dist/examples/shogun-core-example.js +0 -150
- package/dist/examples/signal-protocol-test.js +0 -38
- package/dist/examples/zkproof-credentials-example.js +0 -217
- package/dist/examples/zkproof-example.js +0 -242
- package/dist/gundb/crypto.js +0 -306
- package/dist/gundb/db.js +0 -485
- package/dist/gundb/derive.js +0 -232
- package/dist/gundb/errors.js +0 -76
- package/dist/gundb/gun-es.js +0 -12
- package/dist/gundb/index.js +0 -21
- package/dist/gundb/min.js +0 -10
- package/dist/index.esm.js +0 -22
- package/dist/index.js +0 -47
- package/dist/interfaces/common.js +0 -2
- package/dist/interfaces/events.js +0 -40
- package/dist/interfaces/plugin.js +0 -2
- package/dist/managers/AuthManager.js +0 -226
- package/dist/managers/CoreInitializer.js +0 -250
- package/dist/managers/CryptoIdentityManager.js +0 -138
- package/dist/plugins/base.js +0 -50
- package/dist/plugins/index.js +0 -32
- package/dist/plugins/nostr/index.js +0 -20
- package/dist/plugins/nostr/nostrConnector.js +0 -419
- package/dist/plugins/nostr/nostrConnectorPlugin.js +0 -453
- package/dist/plugins/nostr/nostrSigner.js +0 -319
- package/dist/plugins/nostr/types.js +0 -2
- package/dist/plugins/smartwallet/index.js +0 -18
- package/dist/plugins/smartwallet/smartWalletPlugin.js +0 -511
- package/dist/plugins/smartwallet/types.js +0 -2
- package/dist/plugins/web3/index.js +0 -20
- package/dist/plugins/web3/types.js +0 -2
- package/dist/plugins/web3/web3Connector.js +0 -533
- package/dist/plugins/web3/web3ConnectorPlugin.js +0 -455
- package/dist/plugins/web3/web3Signer.js +0 -314
- package/dist/plugins/webauthn/index.js +0 -19
- package/dist/plugins/webauthn/webauthn.js +0 -496
- package/dist/plugins/webauthn/webauthnPlugin.js +0 -490
- package/dist/plugins/webauthn/webauthnSigner.js +0 -310
- package/dist/plugins/zkproof/types.js +0 -2
- package/dist/plugins/zkproof/zkCredentials.js +0 -216
- package/dist/plugins/zkproof/zkProofConnector.js +0 -198
- package/dist/plugins/zkproof/zkProofPlugin.js +0 -272
- package/dist/storage/storage.js +0 -145
- package/dist/types/gundb/gun-es.d.ts +0 -8
- package/dist/utils/errorHandler.js +0 -246
- /package/dist/{types/examples/crypto-working-test.d.ts → src/crypto/types.js} +0 -0
- /package/dist/{types/gundb/min.d.ts → src/gundb/min.js} +0 -0
- /package/dist/{types/examples/double-ratchet-test.d.ts → src/interfaces/common.js} +0 -0
- /package/dist/{types/examples/mls-sframe-test.d.ts → src/interfaces/plugin.js} +0 -0
- /package/dist/{types/examples/random-generation-test.d.ts → src/plugins/nostr/types.js} +0 -0
- /package/dist/{types/examples/signal-protocol-test.d.ts → src/plugins/smartwallet/types.js} +0 -0
- /package/dist/types/{core.d.ts → src/core.d.ts} +0 -0
- /package/dist/types/{crypto → src/crypto}/asymmetric.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/file-encryption.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/hashing.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/index.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/mls-codec.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/mls.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/pgp.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/random-generation.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/sframe.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/symmetric.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/utils.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/auth-test.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/crypto-identity-example.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/mls-3-member-test.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/mls-multi-member.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/mls-simple-test.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/pgp-example.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/shogun-core-example.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/zkproof-credentials-example.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/zkproof-example.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/crypto.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/derive.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/errors.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/index.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/rxjs.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/types.d.ts +0 -0
- /package/dist/types/{interfaces → src/interfaces}/common.d.ts +0 -0
- /package/dist/types/{interfaces → src/interfaces}/events.d.ts +0 -0
- /package/dist/types/{interfaces → src/interfaces}/plugin.d.ts +0 -0
- /package/dist/types/{interfaces → src/interfaces}/shogun.d.ts +0 -0
- /package/dist/types/{managers → src/managers}/AuthManager.d.ts +0 -0
- /package/dist/types/{managers → src/managers}/CoreInitializer.d.ts +0 -0
- /package/dist/types/{managers → src/managers}/CryptoIdentityManager.d.ts +0 -0
- /package/dist/types/{managers → src/managers}/EventManager.d.ts +0 -0
- /package/dist/types/{managers → src/managers}/PluginManager.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/base.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/index.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/nostr/index.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/nostr/nostrConnector.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/nostr/nostrConnectorPlugin.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/nostr/nostrSigner.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/nostr/types.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/smartwallet/index.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/smartwallet/smartWalletPlugin.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/smartwallet/types.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/web3/index.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/web3/types.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/web3/web3Connector.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/web3/web3ConnectorPlugin.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/web3/web3Signer.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/webauthn/index.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/webauthn/types.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/webauthn/webauthn.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/webauthn/webauthnPlugin.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/webauthn/webauthnSigner.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/zkproof/index.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/zkproof/types.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/zkproof/zkCredentials.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/zkproof/zkProofConnector.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/zkproof/zkProofPlugin.d.ts +0 -0
- /package/dist/types/{storage → src/storage}/storage.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/errorHandler.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/eventEmitter.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/seedPhrase.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/validation.d.ts +0 -0
|
@@ -1,456 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.demonstrateSignalProtocol = exports.deriveSignalSharedSecret = exports.performSignalX3DHKeyExchange = exports.consumeSignalOneTimePrekey = exports.getSignalPublicKeyBundle = exports.initializeSignalUser = exports.bufferToSignalHex = exports.concatSignalArrayBuffers = exports.deriveSignalKey = exports.verifySignalSignature = exports.signSignalData = exports.performSignalDH = exports.importSignalSigningPublicKey = exports.importSignalPublicKey = exports.exportSignalPublicKey = exports.generateSignalSigningKeyPair = exports.generateSignalKeyPair = void 0;
|
|
4
|
-
const hashing_1 = require("./hashing");
|
|
5
|
-
// Signal Protocol X3DH Key Exchange Implementation
|
|
6
|
-
// Using X25519 for key agreement (matches actual Signal Protocol)
|
|
7
|
-
const signalKeyParams = {
|
|
8
|
-
name: "X25519",
|
|
9
|
-
};
|
|
10
|
-
const signalHkdfParams = {
|
|
11
|
-
name: "HKDF",
|
|
12
|
-
hash: "SHA-256",
|
|
13
|
-
};
|
|
14
|
-
const generateSignalKeyPair = async () => {
|
|
15
|
-
try {
|
|
16
|
-
const keyPair = await crypto.subtle.generateKey(signalKeyParams, true, [
|
|
17
|
-
"deriveBits",
|
|
18
|
-
]);
|
|
19
|
-
return keyPair;
|
|
20
|
-
}
|
|
21
|
-
catch (error) {
|
|
22
|
-
// Fallback for testing when crypto API is mocked
|
|
23
|
-
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
24
|
-
console.warn("generateSignalKeyPair failed, using fallback:", errorMessage);
|
|
25
|
-
return {
|
|
26
|
-
publicKey: {
|
|
27
|
-
algorithm: { name: "X25519" },
|
|
28
|
-
type: "public",
|
|
29
|
-
usages: [],
|
|
30
|
-
extractable: true,
|
|
31
|
-
},
|
|
32
|
-
privateKey: {
|
|
33
|
-
algorithm: { name: "X25519" },
|
|
34
|
-
type: "private",
|
|
35
|
-
usages: ["deriveBits"],
|
|
36
|
-
extractable: true,
|
|
37
|
-
},
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
exports.generateSignalKeyPair = generateSignalKeyPair;
|
|
42
|
-
const generateSignalSigningKeyPair = async () => {
|
|
43
|
-
try {
|
|
44
|
-
const keyPair = await crypto.subtle.generateKey({
|
|
45
|
-
name: "Ed25519", // Using Ed25519 for signatures (matches actual Signal Protocol)
|
|
46
|
-
}, true, ["sign", "verify"]);
|
|
47
|
-
return keyPair;
|
|
48
|
-
}
|
|
49
|
-
catch (error) {
|
|
50
|
-
// Fallback for testing when crypto API is mocked
|
|
51
|
-
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
52
|
-
console.warn("generateSignalSigningKeyPair failed, using fallback:", errorMessage);
|
|
53
|
-
return {
|
|
54
|
-
publicKey: {
|
|
55
|
-
algorithm: { name: "Ed25519" },
|
|
56
|
-
type: "public",
|
|
57
|
-
usages: ["verify"],
|
|
58
|
-
extractable: true,
|
|
59
|
-
},
|
|
60
|
-
privateKey: {
|
|
61
|
-
algorithm: { name: "Ed25519" },
|
|
62
|
-
type: "private",
|
|
63
|
-
usages: ["sign"],
|
|
64
|
-
extractable: true,
|
|
65
|
-
},
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
exports.generateSignalSigningKeyPair = generateSignalSigningKeyPair;
|
|
70
|
-
const exportSignalPublicKey = async (publicKey) => {
|
|
71
|
-
try {
|
|
72
|
-
// Check key properties
|
|
73
|
-
const algorithmName = publicKey?.algorithm?.name;
|
|
74
|
-
const isExtractable = publicKey?.extractable;
|
|
75
|
-
if (!isExtractable) {
|
|
76
|
-
throw new Error(`Cannot export non-extractable key. Algorithm: ${algorithmName}, Type: ${publicKey?.type}`);
|
|
77
|
-
}
|
|
78
|
-
// For Ed25519 keys, try raw format first, fallback to spki if needed
|
|
79
|
-
let exported;
|
|
80
|
-
if (algorithmName === "Ed25519") {
|
|
81
|
-
try {
|
|
82
|
-
exported = await crypto.subtle.exportKey("raw", publicKey);
|
|
83
|
-
// Validate that we got actual data (not all zeros)
|
|
84
|
-
const bytes = new Uint8Array(exported);
|
|
85
|
-
const isAllZeros = bytes.every((byte) => byte === 0);
|
|
86
|
-
if (isAllZeros) {
|
|
87
|
-
throw new Error("Export returned all zeros");
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
catch (rawError) {
|
|
91
|
-
// Try SPKI format as fallback for Ed25519
|
|
92
|
-
try {
|
|
93
|
-
const spki = await crypto.subtle.exportKey("spki", publicKey);
|
|
94
|
-
// Extract the raw 32-byte key from SPKI format (Ed25519 public key is last 32 bytes)
|
|
95
|
-
// SPKI structure: [header bytes...][32-byte public key]
|
|
96
|
-
const spkiBytes = new Uint8Array(spki);
|
|
97
|
-
if (spkiBytes.length < 32) {
|
|
98
|
-
throw new Error(`SPKI format too short: ${spkiBytes.length} bytes`);
|
|
99
|
-
}
|
|
100
|
-
// Extract last 32 bytes (Ed25519 public key)
|
|
101
|
-
exported = spkiBytes.slice(-32).buffer;
|
|
102
|
-
}
|
|
103
|
-
catch (spkiError) {
|
|
104
|
-
throw new Error(`Failed to export Ed25519 key in both raw and spki formats. Raw error: ${rawError instanceof Error ? rawError.message : rawError}, SPKI error: ${spkiError instanceof Error ? spkiError.message : spkiError}`);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
// For X25519 and other keys, use raw format
|
|
110
|
-
exported = await crypto.subtle.exportKey("raw", publicKey);
|
|
111
|
-
}
|
|
112
|
-
// Final validation
|
|
113
|
-
const finalBytes = new Uint8Array(exported);
|
|
114
|
-
const isAllZeros = finalBytes.every((byte) => byte === 0);
|
|
115
|
-
if (isAllZeros) {
|
|
116
|
-
throw new Error(`Exported key is all zeros. Algorithm: ${algorithmName}, Size: ${exported.byteLength} bytes`);
|
|
117
|
-
}
|
|
118
|
-
return exported;
|
|
119
|
-
}
|
|
120
|
-
catch (error) {
|
|
121
|
-
// If export fails, it might be a fallback/mock key
|
|
122
|
-
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
123
|
-
const algorithmName = publicKey?.algorithm?.name;
|
|
124
|
-
console.error("exportSignalPublicKey failed:", errorMessage, {
|
|
125
|
-
algorithm: algorithmName,
|
|
126
|
-
type: publicKey?.type,
|
|
127
|
-
extractable: publicKey?.extractable,
|
|
128
|
-
});
|
|
129
|
-
// Check if this is a fallback key object (doesn't have standard CryptoKey properties)
|
|
130
|
-
// Fallback keys are just plain objects, not real CryptoKey instances
|
|
131
|
-
if (publicKey &&
|
|
132
|
-
typeof publicKey === "object" &&
|
|
133
|
-
publicKey.algorithm &&
|
|
134
|
-
(!publicKey.extractable || !(publicKey instanceof CryptoKey))) {
|
|
135
|
-
// This is a fallback key object - we can't export it, so we need to generate a proper key
|
|
136
|
-
// This should not happen in production - it means Ed25519 is not supported
|
|
137
|
-
throw new Error(`Cannot export fallback ${algorithmName} key. ${algorithmName} is not supported in this environment. Error: ${errorMessage}`);
|
|
138
|
-
}
|
|
139
|
-
// Re-throw if it's not a fallback key issue
|
|
140
|
-
throw error;
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
exports.exportSignalPublicKey = exportSignalPublicKey;
|
|
144
|
-
const importSignalPublicKey = async (keyBytes) => {
|
|
145
|
-
return await crypto.subtle.importKey("raw", keyBytes, signalKeyParams, true, // Make public keys extractable for Double Ratchet key comparisons
|
|
146
|
-
[]);
|
|
147
|
-
};
|
|
148
|
-
exports.importSignalPublicKey = importSignalPublicKey;
|
|
149
|
-
const importSignalSigningPublicKey = async (keyBytes) => {
|
|
150
|
-
return await crypto.subtle.importKey("raw", keyBytes, {
|
|
151
|
-
name: "Ed25519",
|
|
152
|
-
}, true, // Make public keys extractable for re-export in bundles
|
|
153
|
-
["verify"]);
|
|
154
|
-
};
|
|
155
|
-
exports.importSignalSigningPublicKey = importSignalSigningPublicKey;
|
|
156
|
-
const performSignalDH = async (privateKey, publicKey) => {
|
|
157
|
-
console.log("🔄 Performing X25519 key agreement...");
|
|
158
|
-
try {
|
|
159
|
-
const result = await crypto.subtle.deriveBits({
|
|
160
|
-
name: "X25519",
|
|
161
|
-
public: publicKey,
|
|
162
|
-
}, privateKey, 256);
|
|
163
|
-
console.log("✓ X25519 key agreement successful, output length:", result.byteLength);
|
|
164
|
-
return result;
|
|
165
|
-
}
|
|
166
|
-
catch (error) {
|
|
167
|
-
console.error("❌ X25519 key agreement failed:", error);
|
|
168
|
-
throw error;
|
|
169
|
-
}
|
|
170
|
-
};
|
|
171
|
-
exports.performSignalDH = performSignalDH;
|
|
172
|
-
const signSignalData = async (privateKey, data) => {
|
|
173
|
-
return await crypto.subtle.sign({
|
|
174
|
-
name: "Ed25519",
|
|
175
|
-
}, privateKey, data);
|
|
176
|
-
};
|
|
177
|
-
exports.signSignalData = signSignalData;
|
|
178
|
-
const verifySignalSignature = async (publicKey, signature, data) => {
|
|
179
|
-
console.log("🔍 Verifying Ed25519 signature...");
|
|
180
|
-
try {
|
|
181
|
-
const result = await crypto.subtle.verify({
|
|
182
|
-
name: "Ed25519",
|
|
183
|
-
}, publicKey, signature, data);
|
|
184
|
-
console.log("✓ Signature verification result:", result);
|
|
185
|
-
return result;
|
|
186
|
-
}
|
|
187
|
-
catch (error) {
|
|
188
|
-
console.error("❌ Signature verification failed:", error);
|
|
189
|
-
throw error;
|
|
190
|
-
}
|
|
191
|
-
};
|
|
192
|
-
exports.verifySignalSignature = verifySignalSignature;
|
|
193
|
-
const deriveSignalKey = async (inputKeyMaterial, salt, info, length = 256) => {
|
|
194
|
-
const prk = await crypto.subtle.importKey("raw", inputKeyMaterial, signalHkdfParams.name, false, ["deriveKey"]);
|
|
195
|
-
return await crypto.subtle.deriveKey({
|
|
196
|
-
name: signalHkdfParams.name,
|
|
197
|
-
hash: signalHkdfParams.hash,
|
|
198
|
-
salt: salt,
|
|
199
|
-
info: info,
|
|
200
|
-
}, prk, {
|
|
201
|
-
name: "AES-GCM",
|
|
202
|
-
length: length,
|
|
203
|
-
}, true, ["encrypt", "decrypt"]);
|
|
204
|
-
};
|
|
205
|
-
exports.deriveSignalKey = deriveSignalKey;
|
|
206
|
-
const concatSignalArrayBuffers = (...buffers) => {
|
|
207
|
-
return (0, hashing_1.concatArrayBuffers)(...buffers);
|
|
208
|
-
};
|
|
209
|
-
exports.concatSignalArrayBuffers = concatSignalArrayBuffers;
|
|
210
|
-
const bufferToSignalHex = (buffer) => {
|
|
211
|
-
return (0, hashing_1.bufferToHex)(buffer);
|
|
212
|
-
};
|
|
213
|
-
exports.bufferToSignalHex = bufferToSignalHex;
|
|
214
|
-
const initializeSignalUser = async (name) => {
|
|
215
|
-
console.log(`🔐 [${name}] Starting user initialization...`);
|
|
216
|
-
try {
|
|
217
|
-
// Generate identity key pairs (separate for X25519 and Ed25519)
|
|
218
|
-
console.log(`🔑 [${name}] Generating identity signing key pair (Ed25519)...`);
|
|
219
|
-
const identitySigningKeyPair = await (0, exports.generateSignalSigningKeyPair)();
|
|
220
|
-
console.log(`🔑 [${name}] Generating identity X25519 key pair...`);
|
|
221
|
-
const identityKeyPair = await (0, exports.generateSignalKeyPair)();
|
|
222
|
-
// Generate signed prekey pair
|
|
223
|
-
console.log(`🔑 [${name}] Generating signed prekey pair...`);
|
|
224
|
-
const signedPrekeyPair = await (0, exports.generateSignalKeyPair)();
|
|
225
|
-
// Sign the prekey with identity signing key
|
|
226
|
-
console.log(`📝 [${name}] Exporting signed prekey for signing...`);
|
|
227
|
-
const prekeyBytes = await (0, exports.exportSignalPublicKey)(signedPrekeyPair.publicKey);
|
|
228
|
-
console.log(`✍️ [${name}] Signing prekey with identity signing key...`);
|
|
229
|
-
const signedPrekeySignature = await (0, exports.signSignalData)(identitySigningKeyPair.privateKey, prekeyBytes);
|
|
230
|
-
console.log(`✓ [${name}] Prekey signature generated, length:`, signedPrekeySignature.byteLength);
|
|
231
|
-
// Generate one-time prekeys
|
|
232
|
-
console.log(`🔑 [${name}] Generating one-time prekeys...`);
|
|
233
|
-
const oneTimePrekeyPairs = [];
|
|
234
|
-
for (let i = 0; i < 3; i++) {
|
|
235
|
-
const oneTimeKey = await (0, exports.generateSignalKeyPair)();
|
|
236
|
-
oneTimePrekeyPairs.push(oneTimeKey);
|
|
237
|
-
console.log(`✓ [${name}] One-time prekey ${i + 1}/3 generated`);
|
|
238
|
-
}
|
|
239
|
-
console.log(`✅ [${name}] User initialization completed successfully`);
|
|
240
|
-
return {
|
|
241
|
-
name,
|
|
242
|
-
identityKeyPair, // X25519 key pair
|
|
243
|
-
identitySigningKeyPair, // Ed25519 key pair
|
|
244
|
-
signedPrekeyPair,
|
|
245
|
-
signedPrekeySignature,
|
|
246
|
-
oneTimePrekeyPairs,
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
catch (error) {
|
|
250
|
-
console.error(`❌ [${name}] User initialization failed:`, error);
|
|
251
|
-
throw error;
|
|
252
|
-
}
|
|
253
|
-
};
|
|
254
|
-
exports.initializeSignalUser = initializeSignalUser;
|
|
255
|
-
const getSignalPublicKeyBundle = async (user) => {
|
|
256
|
-
console.log(`📦 Creating public key bundle for ${user.name}...`);
|
|
257
|
-
try {
|
|
258
|
-
console.log("Exporting identity X25519 key...");
|
|
259
|
-
const identityKey = await (0, exports.exportSignalPublicKey)(user.identityKeyPair.publicKey);
|
|
260
|
-
console.log(`✓ Identity X25519 key exported: ${identityKey.byteLength} bytes`);
|
|
261
|
-
console.log("Exporting identity signing key...");
|
|
262
|
-
const identitySigningKeyBefore = user.identitySigningKeyPair.publicKey;
|
|
263
|
-
console.log("Identity signing key properties:", {
|
|
264
|
-
algorithm: identitySigningKeyBefore?.algorithm?.name,
|
|
265
|
-
type: identitySigningKeyBefore?.type,
|
|
266
|
-
extractable: identitySigningKeyBefore?.extractable,
|
|
267
|
-
});
|
|
268
|
-
const identitySigningKey = await (0, exports.exportSignalPublicKey)(user.identitySigningKeyPair.publicKey);
|
|
269
|
-
const signingKeyBytes = new Uint8Array(identitySigningKey);
|
|
270
|
-
console.log(`✓ Identity signing key exported: ${identitySigningKey.byteLength} bytes, first 8 bytes:`, Array.from(signingKeyBytes.slice(0, 8)));
|
|
271
|
-
const isAllZeros = signingKeyBytes.every((byte) => byte === 0);
|
|
272
|
-
if (isAllZeros) {
|
|
273
|
-
throw new Error("Identity signing key export returned all zeros - this should have been caught by exportSignalPublicKey");
|
|
274
|
-
}
|
|
275
|
-
console.log("Exporting signed prekey...");
|
|
276
|
-
const signedPrekey = await (0, exports.exportSignalPublicKey)(user.signedPrekeyPair.publicKey);
|
|
277
|
-
console.log(`✓ Signed prekey exported: ${signedPrekey.byteLength} bytes`);
|
|
278
|
-
const oneTimePrekey = user.oneTimePrekeyPairs.length > 0
|
|
279
|
-
? await (0, exports.exportSignalPublicKey)(user.oneTimePrekeyPairs[0].publicKey)
|
|
280
|
-
: null;
|
|
281
|
-
if (oneTimePrekey) {
|
|
282
|
-
console.log(`✓ One-time prekey exported: ${oneTimePrekey.byteLength} bytes`);
|
|
283
|
-
}
|
|
284
|
-
const bundle = {
|
|
285
|
-
identityKey, // X25519 key
|
|
286
|
-
identitySigningKey, // Ed25519 key
|
|
287
|
-
signedPrekey,
|
|
288
|
-
signedPrekeySignature: user.signedPrekeySignature,
|
|
289
|
-
oneTimePrekey,
|
|
290
|
-
};
|
|
291
|
-
console.log(`✅ Public key bundle created for ${user.name}`);
|
|
292
|
-
return bundle;
|
|
293
|
-
}
|
|
294
|
-
catch (error) {
|
|
295
|
-
console.error(`❌ Failed to create public key bundle for ${user.name}:`, error);
|
|
296
|
-
throw error;
|
|
297
|
-
}
|
|
298
|
-
};
|
|
299
|
-
exports.getSignalPublicKeyBundle = getSignalPublicKeyBundle;
|
|
300
|
-
const consumeSignalOneTimePrekey = (user) => {
|
|
301
|
-
return user.oneTimePrekeyPairs.shift();
|
|
302
|
-
};
|
|
303
|
-
exports.consumeSignalOneTimePrekey = consumeSignalOneTimePrekey;
|
|
304
|
-
const performSignalX3DHKeyExchange = async (alice, bobBundle) => {
|
|
305
|
-
console.log(`🤝 Starting X3DH key exchange between ${alice.name} and Bob...`);
|
|
306
|
-
try {
|
|
307
|
-
// Step 1: Verify Bob's signed prekey signature using his signing key
|
|
308
|
-
console.log("📝 Step 1: Importing Bob's identity signing key...");
|
|
309
|
-
const bobIdentitySigningKey = await (0, exports.importSignalSigningPublicKey)(bobBundle.identitySigningKey);
|
|
310
|
-
console.log("🔍 Verifying Bob's signed prekey signature...");
|
|
311
|
-
const isValidSignature = await (0, exports.verifySignalSignature)(bobIdentitySigningKey, bobBundle.signedPrekeySignature, bobBundle.signedPrekey);
|
|
312
|
-
if (!isValidSignature) {
|
|
313
|
-
throw new Error("Invalid signed prekey signature!");
|
|
314
|
-
}
|
|
315
|
-
// Step 2: Generate ephemeral key pair
|
|
316
|
-
console.log("🔑 Step 2: Generating Alice's ephemeral key pair...");
|
|
317
|
-
const aliceEphemeralPair = await (0, exports.generateSignalKeyPair)();
|
|
318
|
-
// Step 3: Import Bob's public keys for DH operations
|
|
319
|
-
console.log("🔄 Step 3: Importing Bob's keys for DH operations...");
|
|
320
|
-
const bobSignedPrekey = await (0, exports.importSignalPublicKey)(bobBundle.signedPrekey);
|
|
321
|
-
const bobIdentityKeyDH = await (0, exports.importSignalPublicKey)(bobBundle.identityKey);
|
|
322
|
-
const bobOneTimePrekey = bobBundle.oneTimePrekey
|
|
323
|
-
? await (0, exports.importSignalPublicKey)(bobBundle.oneTimePrekey)
|
|
324
|
-
: null;
|
|
325
|
-
// Step 4: Perform the Triple (or Quadruple) Diffie-Hellman computation
|
|
326
|
-
console.log("🔄 Step 4: Performing DH computations...");
|
|
327
|
-
const dh1 = await (0, exports.performSignalDH)(alice.identityKeyPair.privateKey, bobSignedPrekey);
|
|
328
|
-
const dh2 = await (0, exports.performSignalDH)(aliceEphemeralPair.privateKey, bobIdentityKeyDH);
|
|
329
|
-
const dh3 = await (0, exports.performSignalDH)(aliceEphemeralPair.privateKey, bobSignedPrekey);
|
|
330
|
-
// DH4: Alice_Ephemeral_Private × Bob_OneTimePrekey_Public (if available)
|
|
331
|
-
let dh4 = null;
|
|
332
|
-
if (bobOneTimePrekey) {
|
|
333
|
-
console.log("DH4: Alice_Ephemeral_Private × Bob_OneTimePrekey_Public");
|
|
334
|
-
dh4 = await (0, exports.performSignalDH)(aliceEphemeralPair.privateKey, bobOneTimePrekey);
|
|
335
|
-
}
|
|
336
|
-
// Step 5: Combine all DH outputs
|
|
337
|
-
console.log("🔗 Step 5: Combining DH outputs...");
|
|
338
|
-
const dhOutputs = dh4
|
|
339
|
-
? (0, exports.concatSignalArrayBuffers)(dh1, dh2, dh3, dh4)
|
|
340
|
-
: (0, exports.concatSignalArrayBuffers)(dh1, dh2, dh3);
|
|
341
|
-
// Step 6: Derive the master secret using HKDF
|
|
342
|
-
console.log("🔑 Step 6: Deriving master secret using HKDF...");
|
|
343
|
-
const salt = new ArrayBuffer(32); // 32 zero bytes
|
|
344
|
-
const info = new TextEncoder().encode("Signal_X3DH_Key_Derivation");
|
|
345
|
-
const masterSecret = await (0, exports.deriveSignalKey)(dhOutputs, salt, info.buffer);
|
|
346
|
-
const secretBytes = await crypto.subtle.exportKey("raw", masterSecret);
|
|
347
|
-
const result = {
|
|
348
|
-
masterSecret: secretBytes,
|
|
349
|
-
aliceEphemeralPublic: await (0, exports.exportSignalPublicKey)(aliceEphemeralPair.publicKey),
|
|
350
|
-
usedOneTimePrekey: bobBundle.oneTimePrekey !== null,
|
|
351
|
-
};
|
|
352
|
-
console.log("✅ X3DH key exchange completed successfully!");
|
|
353
|
-
return result;
|
|
354
|
-
}
|
|
355
|
-
catch (error) {
|
|
356
|
-
console.error("❌ X3DH key exchange failed:", error);
|
|
357
|
-
throw error;
|
|
358
|
-
}
|
|
359
|
-
};
|
|
360
|
-
exports.performSignalX3DHKeyExchange = performSignalX3DHKeyExchange;
|
|
361
|
-
const deriveSignalSharedSecret = async (bob, aliceEphemeralPublic, aliceIdentityPublic, usedOneTimePrekey, oneTimePrekeyBytes) => {
|
|
362
|
-
console.log(`🔄 Bob deriving shared secret from Alice's message...`);
|
|
363
|
-
try {
|
|
364
|
-
// Import Alice's public keys
|
|
365
|
-
console.log("📥 Importing Alice's public keys...");
|
|
366
|
-
const aliceEphemeral = await (0, exports.importSignalPublicKey)(aliceEphemeralPublic);
|
|
367
|
-
const aliceIdentity = await (0, exports.importSignalPublicKey)(aliceIdentityPublic);
|
|
368
|
-
// Perform the same DH computations (but from Bob's perspective)
|
|
369
|
-
console.log("🔄 Bob performing DH computations...");
|
|
370
|
-
const dh1 = await (0, exports.performSignalDH)(bob.signedPrekeyPair.privateKey, aliceIdentity);
|
|
371
|
-
const dh2 = await (0, exports.performSignalDH)(bob.identityKeyPair.privateKey, aliceEphemeral);
|
|
372
|
-
const dh3 = await (0, exports.performSignalDH)(bob.signedPrekeyPair.privateKey, aliceEphemeral);
|
|
373
|
-
// DH4: Bob_OneTimePrekey_Private × Alice_Ephemeral_Public (if used)
|
|
374
|
-
let dh4 = null;
|
|
375
|
-
if (usedOneTimePrekey &&
|
|
376
|
-
oneTimePrekeyBytes &&
|
|
377
|
-
bob.oneTimePrekeyPairs.length > 0) {
|
|
378
|
-
console.log("Bob DH4: Bob_OneTimePrekey_Private × Alice_Ephemeral_Public");
|
|
379
|
-
// Find the matching one-time prekey in Bob's collection
|
|
380
|
-
let matchingKeyPair = null;
|
|
381
|
-
for (const keyPair of bob.oneTimePrekeyPairs) {
|
|
382
|
-
const publicKeyBytes = await (0, exports.exportSignalPublicKey)(keyPair.publicKey);
|
|
383
|
-
const publicKeyHex = (0, exports.bufferToSignalHex)(publicKeyBytes);
|
|
384
|
-
const providedKeyHex = (0, exports.bufferToSignalHex)(oneTimePrekeyBytes);
|
|
385
|
-
if (publicKeyHex === providedKeyHex) {
|
|
386
|
-
matchingKeyPair = keyPair;
|
|
387
|
-
console.log("✓ Found matching one-time prekey in Bob's collection");
|
|
388
|
-
break;
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
if (matchingKeyPair) {
|
|
392
|
-
dh4 = await (0, exports.performSignalDH)(matchingKeyPair.privateKey, aliceEphemeral);
|
|
393
|
-
}
|
|
394
|
-
else {
|
|
395
|
-
throw new Error("One-time prekey mismatch");
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
// Combine DH outputs in the same order
|
|
399
|
-
console.log("🔗 Bob combining DH outputs...");
|
|
400
|
-
const dhOutputs = dh4
|
|
401
|
-
? (0, exports.concatSignalArrayBuffers)(dh1, dh2, dh3, dh4)
|
|
402
|
-
: (0, exports.concatSignalArrayBuffers)(dh1, dh2, dh3);
|
|
403
|
-
// Derive the same master secret
|
|
404
|
-
console.log("🔑 Bob deriving master secret using HKDF...");
|
|
405
|
-
const salt = new ArrayBuffer(32);
|
|
406
|
-
const info = new TextEncoder().encode("Signal_X3DH_Key_Derivation");
|
|
407
|
-
const masterSecret = await (0, exports.deriveSignalKey)(dhOutputs, salt, info.buffer);
|
|
408
|
-
const secretBytes = await crypto.subtle.exportKey("raw", masterSecret);
|
|
409
|
-
return secretBytes;
|
|
410
|
-
}
|
|
411
|
-
catch (error) {
|
|
412
|
-
console.error("❌ Bob shared secret derivation failed:", error);
|
|
413
|
-
throw error;
|
|
414
|
-
}
|
|
415
|
-
};
|
|
416
|
-
exports.deriveSignalSharedSecret = deriveSignalSharedSecret;
|
|
417
|
-
const demonstrateSignalProtocol = async () => {
|
|
418
|
-
try {
|
|
419
|
-
// Create two users
|
|
420
|
-
const alice = await (0, exports.initializeSignalUser)("Alice");
|
|
421
|
-
const bob = await (0, exports.initializeSignalUser)("Bob");
|
|
422
|
-
// Get Bob's public key bundle
|
|
423
|
-
const bobBundle = await (0, exports.getSignalPublicKeyBundle)(bob);
|
|
424
|
-
// Perform the X3DH key exchange
|
|
425
|
-
const exchangeResult = await (0, exports.performSignalX3DHKeyExchange)(alice, bobBundle);
|
|
426
|
-
// Verify Bob can derive the same secret
|
|
427
|
-
const aliceIdentityPublic = await (0, exports.exportSignalPublicKey)(alice.identityKeyPair.publicKey);
|
|
428
|
-
// Get the one-time prekey that was actually used
|
|
429
|
-
const usedOneTimePrekey = exchangeResult.usedOneTimePrekey
|
|
430
|
-
? bobBundle.oneTimePrekey
|
|
431
|
-
: null;
|
|
432
|
-
const bobSecret = await (0, exports.deriveSignalSharedSecret)(bob, exchangeResult.aliceEphemeralPublic, aliceIdentityPublic, exchangeResult.usedOneTimePrekey, usedOneTimePrekey);
|
|
433
|
-
// Now consume Bob's one-time prekey after both sides have used it
|
|
434
|
-
if (exchangeResult.usedOneTimePrekey) {
|
|
435
|
-
(0, exports.consumeSignalOneTimePrekey)(bob);
|
|
436
|
-
}
|
|
437
|
-
// Verify both parties have the same secret
|
|
438
|
-
const aliceSecretHex = (0, exports.bufferToSignalHex)(exchangeResult.masterSecret);
|
|
439
|
-
const bobSecretHex = (0, exports.bufferToSignalHex)(bobSecret);
|
|
440
|
-
const success = aliceSecretHex === bobSecretHex;
|
|
441
|
-
return {
|
|
442
|
-
success,
|
|
443
|
-
aliceSecret: aliceSecretHex,
|
|
444
|
-
bobSecret: bobSecretHex,
|
|
445
|
-
usedOneTimePrekey: exchangeResult.usedOneTimePrekey,
|
|
446
|
-
alice,
|
|
447
|
-
bob,
|
|
448
|
-
exchangeResult,
|
|
449
|
-
};
|
|
450
|
-
}
|
|
451
|
-
catch (error) {
|
|
452
|
-
console.error("Error during Signal Protocol demonstration:", error);
|
|
453
|
-
throw error;
|
|
454
|
-
}
|
|
455
|
-
};
|
|
456
|
-
exports.demonstrateSignalProtocol = demonstrateSignalProtocol;
|
package/dist/crypto/symmetric.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.deriveKeyFromPassword = exports.decryptWithSymmetricKey = exports.encryptWithSymmetricKey = exports.deserializeSymmetricKey = exports.generateSymmetricKey = void 0;
|
|
4
|
-
const hashing_1 = require("./hashing");
|
|
5
|
-
// Symmetric Key Generation and Encryption/Decryption Methods
|
|
6
|
-
const generateSymmetricKey = async () => {
|
|
7
|
-
const key = await crypto.subtle.generateKey({
|
|
8
|
-
name: "AES-GCM",
|
|
9
|
-
length: 256, // can be 128, 192, or 256
|
|
10
|
-
}, true, // whether the key is extractable
|
|
11
|
-
["encrypt", "decrypt"]);
|
|
12
|
-
// Export key to JWK format for storage/transmission
|
|
13
|
-
const keyJWK = await crypto.subtle.exportKey("jwk", key);
|
|
14
|
-
return keyJWK;
|
|
15
|
-
};
|
|
16
|
-
exports.generateSymmetricKey = generateSymmetricKey;
|
|
17
|
-
const deserializeSymmetricKey = async (key) => {
|
|
18
|
-
try {
|
|
19
|
-
// If key is already a JWK object, use it directly
|
|
20
|
-
// If it's a string, parse it first
|
|
21
|
-
const jwkKey = typeof key === "string" ? JSON.parse(key) : key;
|
|
22
|
-
// Validate that required JWK properties exist for symmetric keys
|
|
23
|
-
if (!jwkKey.kty) {
|
|
24
|
-
throw new Error('Invalid JWK: missing "kty" property');
|
|
25
|
-
}
|
|
26
|
-
// Ensure the key type is correct for symmetric keys
|
|
27
|
-
if (jwkKey.kty !== "oct") {
|
|
28
|
-
jwkKey.kty = "oct";
|
|
29
|
-
}
|
|
30
|
-
const deSerializedSymmetricKey = await crypto.subtle.importKey("jwk", jwkKey, {
|
|
31
|
-
name: "AES-GCM",
|
|
32
|
-
}, true, ["encrypt", "decrypt"]);
|
|
33
|
-
return deSerializedSymmetricKey;
|
|
34
|
-
}
|
|
35
|
-
catch (error) {
|
|
36
|
-
console.error("Error deserializing symmetric key:", error);
|
|
37
|
-
throw error;
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
exports.deserializeSymmetricKey = deserializeSymmetricKey;
|
|
41
|
-
const encryptWithSymmetricKey = async (message, key) => {
|
|
42
|
-
const encodedMessage = new TextEncoder().encode(message);
|
|
43
|
-
const iv = crypto.getRandomValues(new Uint8Array(12)); // must be 12 bytes
|
|
44
|
-
const encrypted = await crypto.subtle.encrypt({
|
|
45
|
-
name: "AES-GCM",
|
|
46
|
-
iv: iv,
|
|
47
|
-
}, key, encodedMessage);
|
|
48
|
-
return {
|
|
49
|
-
ciphertext: (0, hashing_1.arrayBufferToBase64)(encrypted),
|
|
50
|
-
iv: (0, hashing_1.arrayBufferToBase64)(iv.buffer),
|
|
51
|
-
};
|
|
52
|
-
};
|
|
53
|
-
exports.encryptWithSymmetricKey = encryptWithSymmetricKey;
|
|
54
|
-
const decryptWithSymmetricKey = async (encryptedData, key) => {
|
|
55
|
-
const { ciphertext, iv } = encryptedData;
|
|
56
|
-
const buffer = (0, hashing_1.base64ToArrayBuffer)(ciphertext);
|
|
57
|
-
const ivBuffer = (0, hashing_1.base64ToArrayBuffer)(iv);
|
|
58
|
-
try {
|
|
59
|
-
const decrypted = await crypto.subtle.decrypt({
|
|
60
|
-
name: "AES-GCM",
|
|
61
|
-
iv: ivBuffer,
|
|
62
|
-
}, key, buffer);
|
|
63
|
-
const message = new TextDecoder().decode(decrypted);
|
|
64
|
-
return message;
|
|
65
|
-
}
|
|
66
|
-
catch (error) {
|
|
67
|
-
throw new Error("Unable to decrypt message. Incorrect key.");
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
exports.decryptWithSymmetricKey = decryptWithSymmetricKey;
|
|
71
|
-
// Password-based key derivation
|
|
72
|
-
const deriveKeyFromPassword = async (password, salt) => {
|
|
73
|
-
const encoder = new TextEncoder();
|
|
74
|
-
// Generate or use provided salt
|
|
75
|
-
const actualSalt = salt || (await crypto.subtle.digest("SHA-256", encoder.encode(password)));
|
|
76
|
-
// Import password as key material
|
|
77
|
-
const passwordKey = await crypto.subtle.importKey("raw", encoder.encode(password), "PBKDF2", false, ["deriveKey"]);
|
|
78
|
-
// Derive AES-GCM key using PBKDF2
|
|
79
|
-
const derivedKey = await crypto.subtle.deriveKey({
|
|
80
|
-
name: "PBKDF2",
|
|
81
|
-
salt: actualSalt,
|
|
82
|
-
iterations: 100000, // Strong iteration count
|
|
83
|
-
hash: "SHA-256",
|
|
84
|
-
}, passwordKey, {
|
|
85
|
-
name: "AES-GCM",
|
|
86
|
-
length: 256,
|
|
87
|
-
}, false, // Not extractable for security
|
|
88
|
-
["encrypt", "decrypt"]);
|
|
89
|
-
return { key: derivedKey, salt: actualSalt };
|
|
90
|
-
};
|
|
91
|
-
exports.deriveKeyFromPassword = deriveKeyFromPassword;
|
package/dist/crypto/types.js
DELETED