@enclave-vm/stream 0.0.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/LICENSE +201 -0
- package/package.json +38 -0
- package/src/crypto/aes-gcm.d.ts +115 -0
- package/src/crypto/aes-gcm.js +293 -0
- package/src/crypto/aes-gcm.js.map +1 -0
- package/src/crypto/ecdh.d.ts +70 -0
- package/src/crypto/ecdh.js +166 -0
- package/src/crypto/ecdh.js.map +1 -0
- package/src/crypto/hkdf.d.ts +44 -0
- package/src/crypto/hkdf.js +123 -0
- package/src/crypto/hkdf.js.map +1 -0
- package/src/crypto/index.d.ts +9 -0
- package/src/crypto/index.js +40 -0
- package/src/crypto/index.js.map +1 -0
- package/src/index.d.ts +15 -0
- package/src/index.js +63 -0
- package/src/index.js.map +1 -0
- package/src/ndjson.d.ts +97 -0
- package/src/ndjson.js +263 -0
- package/src/ndjson.js.map +1 -0
- package/src/reconnect.d.ts +242 -0
- package/src/reconnect.js +372 -0
- package/src/reconnect.js.map +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aes-gcm.js","sourceRoot":"","sources":["../../../src/crypto/aes-gcm.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAqDH,sCAIC;AAMD,oDAgBC;AAKD,4BAEC;AAKD,gCAEC;AAWD,0BAkCC;AAWD,0BAkCC;AAKD,kCAgBC;AAKD,kCAQC;AAKD,0DAiBC;AA7OD,6CAY2B;AAE3B;;;GAGG;AACH,SAAS,aAAa,CAAC,GAAe;IACpC,IAAI,GAAG,CAAC,UAAU,KAAK,CAAC,IAAI,GAAG,CAAC,UAAU,KAAK,GAAG,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;QACrE,OAAO,GAAG,CAAC,MAAqB,CAAC;IACnC,CAAC;IACD,OAAO,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,UAAU,GAAG,GAAG,CAA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AED;;OAEG;IACH,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC,QAAoB,EAAE,KAAa;QAC3D,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;QAE3B,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,SAAS,CAChC,KAAK,EACL,aAAa,CAAC,QAAQ,CAAC,EACvB;YACE,IAAI,EAAE,SAAS;YACf,MAAM,EAAE,QAAQ,CAAC,MAAM,GAAG,CAAC;SAC5B,EACD,KAAK,EACL,CAAC,SAAS,EAAE,SAAS,CAAC,CACvB,CAAC;QAEF,MAAM,OAAO,GAAmB;YAC9B,KAAK;YACL,SAAS,EAAE,2BAAmB,CAAC,WAAW;YAC1C,QAAQ;YACR,YAAY,EAAE,EAAE;YAChB,SAAS,EAAE,4BAAoB;YAC/B,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;SACtB,CAAC;QAEF,OAAO,IAAI,wBAAwB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IACpD,CAAC;CACF;AA3ID,4DA2IC","sourcesContent":["/**\n * @enclave-vm/stream - AES-GCM Encryption\n *\n * AES-GCM authenticated encryption using Web Crypto API.\n */\n\nimport {\n AES_GCM_NONCE_SIZE,\n AES_GCM_TAG_SIZE,\n MAX_MESSAGES_PER_KEY,\n EncryptionErrorCode,\n type EncryptedEnvelopePayload,\n type SessionKeyInfo,\n PROTOCOL_VERSION,\n EventType,\n type EncryptedEnvelope,\n type SessionId,\n EncryptionAlgorithm,\n} from '@enclave-vm/types';\n\n/**\n * Convert a Uint8Array to an ArrayBuffer.\n * Handles the case where the Uint8Array is a view into a larger buffer.\n */\nfunction toArrayBuffer(arr: Uint8Array): ArrayBuffer {\n if (arr.byteOffset === 0 && arr.byteLength === arr.buffer.byteLength) {\n return arr.buffer as ArrayBuffer;\n }\n return arr.buffer.slice(arr.byteOffset, arr.byteOffset + arr.byteLength) as ArrayBuffer;\n}\n\n/**\n * AES-GCM encryption error.\n */\nexport class AesGcmError extends Error {\n constructor(\n message: string,\n public readonly code: string,\n ) {\n super(message);\n this.name = 'AesGcmError';\n }\n}\n\n/**\n * Get the Web Crypto subtle API.\n */\nfunction getSubtle(): SubtleCrypto {\n if (typeof globalThis.crypto?.subtle !== 'undefined') {\n return globalThis.crypto.subtle;\n }\n throw new AesGcmError('Web Crypto API not available', EncryptionErrorCode.DecryptionFailed);\n}\n\n/**\n * Generate a random nonce for AES-GCM.\n */\nexport function generateNonce(): Uint8Array {\n const nonce = new Uint8Array(AES_GCM_NONCE_SIZE);\n crypto.getRandomValues(nonce);\n return nonce;\n}\n\n/**\n * Generate a counter-based nonce for deterministic nonce generation.\n * Uses 8 bytes of random prefix + 4 bytes of counter.\n */\nexport function generateCounterNonce(prefix: Uint8Array, counter: bigint): Uint8Array {\n if (prefix.length !== 8) {\n throw new AesGcmError('Nonce prefix must be 8 bytes', EncryptionErrorCode.NonceReuse);\n }\n\n const nonce = new Uint8Array(AES_GCM_NONCE_SIZE);\n nonce.set(prefix);\n\n // Write counter as big-endian 4 bytes\n const counterValue = Number(counter & 0xffffffffn);\n nonce[8] = (counterValue >> 24) & 0xff;\n nonce[9] = (counterValue >> 16) & 0xff;\n nonce[10] = (counterValue >> 8) & 0xff;\n nonce[11] = counterValue & 0xff;\n\n return nonce;\n}\n\n/**\n * Encode bytes to base64.\n */\nexport function toBase64(bytes: Uint8Array): string {\n return btoa(String.fromCharCode(...bytes));\n}\n\n/**\n * Decode base64 to bytes.\n */\nexport function fromBase64(base64: string): Uint8Array {\n return Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));\n}\n\n/**\n * Encrypt data using AES-GCM.\n *\n * @param key - CryptoKey for AES-GCM\n * @param plaintext - Data to encrypt\n * @param nonce - 12-byte nonce/IV\n * @param additionalData - Optional additional authenticated data (AAD)\n * @returns Ciphertext with appended authentication tag\n */\nexport async function encrypt(\n key: CryptoKey,\n plaintext: Uint8Array,\n nonce: Uint8Array,\n additionalData?: Uint8Array,\n): Promise<Uint8Array> {\n const subtle = getSubtle();\n\n if (nonce.length !== AES_GCM_NONCE_SIZE) {\n throw new AesGcmError(\n `Invalid nonce length: expected ${AES_GCM_NONCE_SIZE}, got ${nonce.length}`,\n EncryptionErrorCode.DecryptionFailed,\n );\n }\n\n try {\n const ciphertext = await subtle.encrypt(\n {\n name: 'AES-GCM',\n iv: toArrayBuffer(nonce),\n tagLength: AES_GCM_TAG_SIZE * 8, // bits\n additionalData: additionalData ? toArrayBuffer(additionalData) : undefined,\n },\n key,\n toArrayBuffer(plaintext),\n );\n\n return new Uint8Array(ciphertext);\n } catch (err) {\n throw new AesGcmError(\n `Encryption failed: ${err instanceof Error ? err.message : String(err)}`,\n EncryptionErrorCode.DecryptionFailed,\n );\n }\n}\n\n/**\n * Decrypt data using AES-GCM.\n *\n * @param key - CryptoKey for AES-GCM\n * @param ciphertext - Data to decrypt (with authentication tag)\n * @param nonce - 12-byte nonce/IV used during encryption\n * @param additionalData - Optional additional authenticated data (AAD)\n * @returns Decrypted plaintext\n */\nexport async function decrypt(\n key: CryptoKey,\n ciphertext: Uint8Array,\n nonce: Uint8Array,\n additionalData?: Uint8Array,\n): Promise<Uint8Array> {\n const subtle = getSubtle();\n\n if (nonce.length !== AES_GCM_NONCE_SIZE) {\n throw new AesGcmError(\n `Invalid nonce length: expected ${AES_GCM_NONCE_SIZE}, got ${nonce.length}`,\n EncryptionErrorCode.DecryptionFailed,\n );\n }\n\n try {\n const plaintext = await subtle.decrypt(\n {\n name: 'AES-GCM',\n iv: toArrayBuffer(nonce),\n tagLength: AES_GCM_TAG_SIZE * 8, // bits\n additionalData: additionalData ? toArrayBuffer(additionalData) : undefined,\n },\n key,\n toArrayBuffer(ciphertext),\n );\n\n return new Uint8Array(plaintext);\n } catch (err) {\n throw new AesGcmError(\n `Decryption failed: ${err instanceof Error ? err.message : String(err)}`,\n EncryptionErrorCode.DecryptionFailed,\n );\n }\n}\n\n/**\n * Encrypt a JSON object and create an encrypted envelope payload.\n */\nexport async function encryptJson(\n key: CryptoKey,\n keyId: string,\n data: unknown,\n nonce?: Uint8Array,\n): Promise<EncryptedEnvelopePayload> {\n const actualNonce = nonce ?? generateNonce();\n const plaintext = new TextEncoder().encode(JSON.stringify(data));\n\n const ciphertext = await encrypt(key, plaintext, actualNonce);\n\n return {\n kid: keyId,\n nonceB64: toBase64(actualNonce),\n ciphertextB64: toBase64(ciphertext),\n };\n}\n\n/**\n * Decrypt an encrypted envelope payload and parse as JSON.\n */\nexport async function decryptJson<T>(key: CryptoKey, payload: EncryptedEnvelopePayload): Promise<T> {\n const nonce = fromBase64(payload.nonceB64);\n const ciphertext = fromBase64(payload.ciphertextB64);\n\n const plaintext = await decrypt(key, ciphertext, nonce);\n const json = new TextDecoder().decode(plaintext);\n\n return JSON.parse(json) as T;\n}\n\n/**\n * Create an encrypted envelope from an event.\n */\nexport async function createEncryptedEnvelope(\n key: CryptoKey,\n keyId: string,\n sessionId: SessionId,\n seq: number,\n innerEvent: unknown,\n nonce?: Uint8Array,\n): Promise<EncryptedEnvelope> {\n const payload = await encryptJson(key, keyId, innerEvent, nonce);\n\n return {\n protocolVersion: PROTOCOL_VERSION,\n sessionId,\n seq,\n type: EventType.Encrypted,\n payload,\n };\n}\n\n/**\n * Session encryption context for managing key state.\n */\nexport class SessionEncryptionContext {\n private readonly key: CryptoKey;\n private readonly keyInfo: SessionKeyInfo;\n private nonceCounter: bigint;\n private readonly noncePrefix: Uint8Array;\n\n constructor(key: CryptoKey, keyInfo: SessionKeyInfo) {\n this.key = key;\n this.keyInfo = keyInfo;\n this.nonceCounter = keyInfo.nonceCounter;\n this.noncePrefix = crypto.getRandomValues(new Uint8Array(8));\n }\n\n /**\n * Get the key ID.\n */\n get keyId(): string {\n return this.keyInfo.keyId;\n }\n\n /**\n * Check if the key needs rotation.\n */\n needsRotation(): boolean {\n return this.nonceCounter >= this.keyInfo.maxNonces;\n }\n\n /**\n * Encrypt data and increment the nonce counter.\n */\n async encrypt(plaintext: Uint8Array): Promise<{\n ciphertext: Uint8Array;\n nonce: Uint8Array;\n }> {\n if (this.needsRotation()) {\n throw new AesGcmError('Key needs rotation: nonce limit reached', EncryptionErrorCode.KeyExpired);\n }\n\n const nonce = generateCounterNonce(this.noncePrefix, this.nonceCounter);\n this.nonceCounter++;\n\n const ciphertext = await encrypt(this.key, plaintext, nonce);\n\n return { ciphertext, nonce };\n }\n\n /**\n * Encrypt JSON data.\n */\n async encryptJson(data: unknown): Promise<EncryptedEnvelopePayload> {\n const plaintext = new TextEncoder().encode(JSON.stringify(data));\n const { ciphertext, nonce } = await this.encrypt(plaintext);\n\n return {\n kid: this.keyId,\n nonceB64: toBase64(nonce),\n ciphertextB64: toBase64(ciphertext),\n };\n }\n\n /**\n * Create an encrypted envelope.\n */\n async createEnvelope(sessionId: SessionId, seq: number, innerEvent: unknown): Promise<EncryptedEnvelope> {\n const payload = await this.encryptJson(innerEvent);\n\n return {\n protocolVersion: PROTOCOL_VERSION,\n sessionId,\n seq,\n type: EventType.Encrypted,\n payload,\n };\n }\n\n /**\n * Decrypt an encrypted envelope payload.\n */\n async decrypt(payload: EncryptedEnvelopePayload): Promise<Uint8Array> {\n const nonce = fromBase64(payload.nonceB64);\n const ciphertext = fromBase64(payload.ciphertextB64);\n\n return decrypt(this.key, ciphertext, nonce);\n }\n\n /**\n * Decrypt and parse JSON from an encrypted envelope payload.\n */\n async decryptJson<T>(payload: EncryptedEnvelopePayload): Promise<T> {\n const plaintext = await this.decrypt(payload);\n const json = new TextDecoder().decode(plaintext);\n return JSON.parse(json) as T;\n }\n\n /**\n * Get the current nonce counter value.\n */\n getNonceCounter(): bigint {\n return this.nonceCounter;\n }\n\n /**\n * Create session key info for serialization.\n */\n toKeyInfo(): SessionKeyInfo {\n return {\n ...this.keyInfo,\n nonceCounter: this.nonceCounter,\n };\n }\n\n /**\n * Create a new encryption context from raw key bytes.\n */\n static async fromKeyBytes(keyBytes: Uint8Array, keyId: string): Promise<SessionEncryptionContext> {\n const subtle = getSubtle();\n\n const key = await subtle.importKey(\n 'raw',\n toArrayBuffer(keyBytes),\n {\n name: 'AES-GCM',\n length: keyBytes.length * 8,\n },\n false,\n ['encrypt', 'decrypt'],\n );\n\n const keyInfo: SessionKeyInfo = {\n keyId,\n algorithm: EncryptionAlgorithm.AES_GCM_256,\n keyBytes,\n nonceCounter: 0n,\n maxNonces: MAX_MESSAGES_PER_KEY,\n createdAt: Date.now(),\n };\n\n return new SessionEncryptionContext(key, keyInfo);\n }\n}\n"]}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @enclave-vm/stream - ECDH Key Exchange
|
|
3
|
+
*
|
|
4
|
+
* Elliptic Curve Diffie-Hellman key exchange using Web Crypto API.
|
|
5
|
+
* Uses P-256 curve for broad browser compatibility.
|
|
6
|
+
*/
|
|
7
|
+
import { SupportedCurve, type ClientHello, type ServerHello } from '@enclave-vm/types';
|
|
8
|
+
/**
|
|
9
|
+
* ECDH key pair.
|
|
10
|
+
*/
|
|
11
|
+
export interface EcdhKeyPair {
|
|
12
|
+
publicKey: CryptoKey;
|
|
13
|
+
privateKey: CryptoKey;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Serialized ECDH public key.
|
|
17
|
+
*/
|
|
18
|
+
export interface SerializedPublicKey {
|
|
19
|
+
/** Base64-encoded public key (raw format) */
|
|
20
|
+
publicKeyB64: string;
|
|
21
|
+
/** Curve used */
|
|
22
|
+
curve: SupportedCurve;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* ECDH encryption error.
|
|
26
|
+
*/
|
|
27
|
+
export declare class EcdhError extends Error {
|
|
28
|
+
readonly code: string;
|
|
29
|
+
constructor(message: string, code: string);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Generate an ephemeral ECDH key pair.
|
|
33
|
+
*/
|
|
34
|
+
export declare function generateKeyPair(curve?: SupportedCurve): Promise<EcdhKeyPair>;
|
|
35
|
+
/**
|
|
36
|
+
* Export a public key to base64 format.
|
|
37
|
+
*/
|
|
38
|
+
export declare function exportPublicKey(publicKey: CryptoKey): Promise<SerializedPublicKey>;
|
|
39
|
+
/**
|
|
40
|
+
* Import a public key from base64 format.
|
|
41
|
+
*/
|
|
42
|
+
export declare function importPublicKey(publicKeyB64: string, curve?: SupportedCurve): Promise<CryptoKey>;
|
|
43
|
+
/**
|
|
44
|
+
* Derive shared secret from private key and peer's public key.
|
|
45
|
+
*/
|
|
46
|
+
export declare function deriveSharedSecret(privateKey: CryptoKey, peerPublicKey: CryptoKey): Promise<Uint8Array>;
|
|
47
|
+
/**
|
|
48
|
+
* Create a client hello message for the encryption handshake.
|
|
49
|
+
*/
|
|
50
|
+
export declare function createClientHello(keyPair: EcdhKeyPair): Promise<ClientHello>;
|
|
51
|
+
/**
|
|
52
|
+
* Create a server hello message for the encryption handshake.
|
|
53
|
+
*/
|
|
54
|
+
export declare function createServerHello(keyPair: EcdhKeyPair, keyId: string): Promise<ServerHello>;
|
|
55
|
+
/**
|
|
56
|
+
* Process a client hello and generate server response.
|
|
57
|
+
*/
|
|
58
|
+
export declare function processClientHello(clientHello: ClientHello): Promise<{
|
|
59
|
+
serverKeyPair: EcdhKeyPair;
|
|
60
|
+
peerPublicKey: CryptoKey;
|
|
61
|
+
serverHello: ServerHello;
|
|
62
|
+
keyId: string;
|
|
63
|
+
}>;
|
|
64
|
+
/**
|
|
65
|
+
* Process a server hello and extract peer's public key.
|
|
66
|
+
*/
|
|
67
|
+
export declare function processServerHello(serverHello: ServerHello): Promise<{
|
|
68
|
+
peerPublicKey: CryptoKey;
|
|
69
|
+
keyId: string;
|
|
70
|
+
}>;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @enclave-vm/stream - ECDH Key Exchange
|
|
4
|
+
*
|
|
5
|
+
* Elliptic Curve Diffie-Hellman key exchange using Web Crypto API.
|
|
6
|
+
* Uses P-256 curve for broad browser compatibility.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.EcdhError = void 0;
|
|
10
|
+
exports.generateKeyPair = generateKeyPair;
|
|
11
|
+
exports.exportPublicKey = exportPublicKey;
|
|
12
|
+
exports.importPublicKey = importPublicKey;
|
|
13
|
+
exports.deriveSharedSecret = deriveSharedSecret;
|
|
14
|
+
exports.createClientHello = createClientHello;
|
|
15
|
+
exports.createServerHello = createServerHello;
|
|
16
|
+
exports.processClientHello = processClientHello;
|
|
17
|
+
exports.processServerHello = processServerHello;
|
|
18
|
+
const types_1 = require("@enclave-vm/types");
|
|
19
|
+
/**
|
|
20
|
+
* ECDH encryption error.
|
|
21
|
+
*/
|
|
22
|
+
class EcdhError extends Error {
|
|
23
|
+
code;
|
|
24
|
+
constructor(message, code) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.code = code;
|
|
27
|
+
this.name = 'EcdhError';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.EcdhError = EcdhError;
|
|
31
|
+
/**
|
|
32
|
+
* Get the Web Crypto subtle API.
|
|
33
|
+
* Works in both browser and Node.js 20+.
|
|
34
|
+
*/
|
|
35
|
+
function getSubtle() {
|
|
36
|
+
if (typeof globalThis.crypto?.subtle !== 'undefined') {
|
|
37
|
+
return globalThis.crypto.subtle;
|
|
38
|
+
}
|
|
39
|
+
throw new EcdhError('Web Crypto API not available', types_1.EncryptionErrorCode.HandshakeFailed);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Generate an ephemeral ECDH key pair.
|
|
43
|
+
*/
|
|
44
|
+
async function generateKeyPair(curve = types_1.SupportedCurve.P256) {
|
|
45
|
+
const subtle = getSubtle();
|
|
46
|
+
try {
|
|
47
|
+
const keyPair = await subtle.generateKey({
|
|
48
|
+
name: 'ECDH',
|
|
49
|
+
namedCurve: curve,
|
|
50
|
+
}, true, // extractable for export
|
|
51
|
+
['deriveBits']);
|
|
52
|
+
return {
|
|
53
|
+
publicKey: keyPair.publicKey,
|
|
54
|
+
privateKey: keyPair.privateKey,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
throw new EcdhError(`Failed to generate key pair: ${err instanceof Error ? err.message : String(err)}`, types_1.EncryptionErrorCode.HandshakeFailed);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Export a public key to base64 format.
|
|
63
|
+
*/
|
|
64
|
+
async function exportPublicKey(publicKey) {
|
|
65
|
+
const subtle = getSubtle();
|
|
66
|
+
try {
|
|
67
|
+
const rawKey = await subtle.exportKey('raw', publicKey);
|
|
68
|
+
const publicKeyB64 = btoa(String.fromCharCode(...new Uint8Array(rawKey)));
|
|
69
|
+
// Extract curve from key algorithm
|
|
70
|
+
const algorithm = publicKey.algorithm;
|
|
71
|
+
const curve = algorithm.namedCurve;
|
|
72
|
+
return { publicKeyB64, curve };
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
throw new EcdhError(`Failed to export public key: ${err instanceof Error ? err.message : String(err)}`, types_1.EncryptionErrorCode.InvalidPublicKey);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Import a public key from base64 format.
|
|
80
|
+
*/
|
|
81
|
+
async function importPublicKey(publicKeyB64, curve = types_1.SupportedCurve.P256) {
|
|
82
|
+
const subtle = getSubtle();
|
|
83
|
+
try {
|
|
84
|
+
const rawKey = Uint8Array.from(atob(publicKeyB64), (c) => c.charCodeAt(0));
|
|
85
|
+
return await subtle.importKey('raw', rawKey, {
|
|
86
|
+
name: 'ECDH',
|
|
87
|
+
namedCurve: curve,
|
|
88
|
+
}, true, []);
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
throw new EcdhError(`Failed to import public key: ${err instanceof Error ? err.message : String(err)}`, types_1.EncryptionErrorCode.InvalidPublicKey);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Derive shared secret from private key and peer's public key.
|
|
96
|
+
*/
|
|
97
|
+
async function deriveSharedSecret(privateKey, peerPublicKey) {
|
|
98
|
+
const subtle = getSubtle();
|
|
99
|
+
try {
|
|
100
|
+
// Derive 256 bits (32 bytes) of shared secret
|
|
101
|
+
const sharedBits = await subtle.deriveBits({
|
|
102
|
+
name: 'ECDH',
|
|
103
|
+
public: peerPublicKey,
|
|
104
|
+
}, privateKey, 256);
|
|
105
|
+
return new Uint8Array(sharedBits);
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
throw new EcdhError(`Failed to derive shared secret: ${err instanceof Error ? err.message : String(err)}`, types_1.EncryptionErrorCode.KeyDerivationFailed);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Create a client hello message for the encryption handshake.
|
|
113
|
+
*/
|
|
114
|
+
async function createClientHello(keyPair) {
|
|
115
|
+
const { publicKeyB64, curve } = await exportPublicKey(keyPair.publicKey);
|
|
116
|
+
return {
|
|
117
|
+
protocolVersion: types_1.PROTOCOL_VERSION,
|
|
118
|
+
clientEphemeralPubKeyB64: publicKeyB64,
|
|
119
|
+
curve,
|
|
120
|
+
supportedAlgorithms: [types_1.EncryptionAlgorithm.AES_GCM_256],
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Create a server hello message for the encryption handshake.
|
|
125
|
+
*/
|
|
126
|
+
async function createServerHello(keyPair, keyId) {
|
|
127
|
+
const { publicKeyB64, curve } = await exportPublicKey(keyPair.publicKey);
|
|
128
|
+
return {
|
|
129
|
+
protocolVersion: types_1.PROTOCOL_VERSION,
|
|
130
|
+
serverEphemeralPubKeyB64: publicKeyB64,
|
|
131
|
+
curve,
|
|
132
|
+
selectedAlgorithm: types_1.EncryptionAlgorithm.AES_GCM_256,
|
|
133
|
+
kdf: types_1.KeyDerivation.HKDF_SHA256,
|
|
134
|
+
keyId,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Process a client hello and generate server response.
|
|
139
|
+
*/
|
|
140
|
+
async function processClientHello(clientHello) {
|
|
141
|
+
// Generate server's ephemeral key pair
|
|
142
|
+
const serverKeyPair = await generateKeyPair(clientHello.curve);
|
|
143
|
+
// Import client's public key
|
|
144
|
+
const peerPublicKey = await importPublicKey(clientHello.clientEphemeralPubKeyB64, clientHello.curve);
|
|
145
|
+
// Generate a key ID
|
|
146
|
+
const keyId = `k_${crypto.randomUUID()}`;
|
|
147
|
+
// Create server hello
|
|
148
|
+
const serverHello = await createServerHello(serverKeyPair, keyId);
|
|
149
|
+
return {
|
|
150
|
+
serverKeyPair,
|
|
151
|
+
peerPublicKey,
|
|
152
|
+
serverHello,
|
|
153
|
+
keyId,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Process a server hello and extract peer's public key.
|
|
158
|
+
*/
|
|
159
|
+
async function processServerHello(serverHello) {
|
|
160
|
+
const peerPublicKey = await importPublicKey(serverHello.serverEphemeralPubKeyB64, serverHello.curve);
|
|
161
|
+
return {
|
|
162
|
+
peerPublicKey,
|
|
163
|
+
keyId: serverHello.keyId,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
//# sourceMappingURL=ecdh.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ecdh.js","sourceRoot":"","sources":["../../../src/crypto/ecdh.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAyDH,0CAuBC;AAKD,0CAkBC;AAKD,0CAyBC;AAKD,gDAqBC;AAKD,8CASC;AAKD,8CAWC;AAKD,gDAwBC;AAKD,gDAUC;AAvOD,6CAQ2B;AAoB3B;;GAEG;AACH,MAAa,SAAU,SAAQ,KAAK;IAGhB;IAFlB,YACE,OAAe,EACC,IAAY;QAE5B,KAAK,CAAC,OAAO,CAAC,CAAC;QAFC,SAAI,GAAJ,IAAI,CAAQ;QAG5B,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC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E/D,6BAA6B;IAC7B,MAAM,aAAa,GAAG,MAAM,eAAe,CAAC,WAAW,CAAC,wBAAwB,EAAE,WAAW,CAAC,KAAK,CAAC,CAAC;IAErG,oBAAoB;IACpB,MAAM,KAAK,GAAG,KAAK,MAAM,CAAC,UAAU,EAAE,EAAE,CAAC;IAEzC,sBAAsB;IACtB,MAAM,WAAW,GAAG,MAAM,iBAAiB,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC;IAElE,OAAO;QACL,aAAa;QACb,aAAa;QACb,WAAW;QACX,KAAK;KACN,CAAC;AACJ,CAAC;AAED;;GAEG;AACI,KAAK,UAAU,kBAAkB,CAAC,WAAwB;IAI/D,MAAM,aAAa,GAAG,MAAM,eAAe,CAAC,WAAW,CAAC,wBAAwB,EAAE,WAAW,CAAC,KAAK,CAAC,CAAC;IAErG,OAAO;QACL,aAAa;QACb,KAAK,EAAE,WAAW,CAAC,KAAK;KACzB,CAAC;AACJ,CAAC","sourcesContent":["/**\n * @enclave-vm/stream - ECDH Key Exchange\n *\n * Elliptic Curve Diffie-Hellman key exchange using Web Crypto API.\n * Uses P-256 curve for broad browser compatibility.\n */\n\nimport {\n SupportedCurve,\n EncryptionErrorCode,\n type ClientHello,\n type ServerHello,\n PROTOCOL_VERSION,\n EncryptionAlgorithm,\n KeyDerivation,\n} from '@enclave-vm/types';\n\n/**\n * ECDH key pair.\n */\nexport interface EcdhKeyPair {\n publicKey: CryptoKey;\n privateKey: CryptoKey;\n}\n\n/**\n * Serialized ECDH public key.\n */\nexport interface SerializedPublicKey {\n /** Base64-encoded public key (raw format) */\n publicKeyB64: string;\n /** Curve used */\n curve: SupportedCurve;\n}\n\n/**\n * ECDH encryption error.\n */\nexport class EcdhError extends Error {\n constructor(\n message: string,\n public readonly code: string,\n ) {\n super(message);\n this.name = 'EcdhError';\n }\n}\n\n/**\n * Get the Web Crypto subtle API.\n * Works in both browser and Node.js 20+.\n */\nfunction getSubtle(): SubtleCrypto {\n if (typeof globalThis.crypto?.subtle !== 'undefined') {\n return globalThis.crypto.subtle;\n }\n throw new EcdhError('Web Crypto API not available', EncryptionErrorCode.HandshakeFailed);\n}\n\n/**\n * Generate an ephemeral ECDH key pair.\n */\nexport async function generateKeyPair(curve: SupportedCurve = SupportedCurve.P256): Promise<EcdhKeyPair> {\n const subtle = getSubtle();\n\n try {\n const keyPair = await subtle.generateKey(\n {\n name: 'ECDH',\n namedCurve: curve,\n },\n true, // extractable for export\n ['deriveBits'],\n );\n\n return {\n publicKey: keyPair.publicKey,\n privateKey: keyPair.privateKey,\n };\n } catch (err) {\n throw new EcdhError(\n `Failed to generate key pair: ${err instanceof Error ? err.message : String(err)}`,\n EncryptionErrorCode.HandshakeFailed,\n );\n }\n}\n\n/**\n * Export a public key to base64 format.\n */\nexport async function exportPublicKey(publicKey: CryptoKey): Promise<SerializedPublicKey> {\n const subtle = getSubtle();\n\n try {\n const rawKey = await subtle.exportKey('raw', publicKey);\n const publicKeyB64 = btoa(String.fromCharCode(...new Uint8Array(rawKey)));\n\n // Extract curve from key algorithm\n const algorithm = publicKey.algorithm as EcKeyAlgorithm;\n const curve = algorithm.namedCurve as SupportedCurve;\n\n return { publicKeyB64, curve };\n } catch (err) {\n throw new EcdhError(\n `Failed to export public key: ${err instanceof Error ? err.message : String(err)}`,\n EncryptionErrorCode.InvalidPublicKey,\n );\n }\n}\n\n/**\n * Import a public key from base64 format.\n */\nexport async function importPublicKey(\n publicKeyB64: string,\n curve: SupportedCurve = SupportedCurve.P256,\n): Promise<CryptoKey> {\n const subtle = getSubtle();\n\n try {\n const rawKey = Uint8Array.from(atob(publicKeyB64), (c) => c.charCodeAt(0));\n\n return await subtle.importKey(\n 'raw',\n rawKey,\n {\n name: 'ECDH',\n namedCurve: curve,\n },\n true,\n [],\n );\n } catch (err) {\n throw new EcdhError(\n `Failed to import public key: ${err instanceof Error ? err.message : String(err)}`,\n EncryptionErrorCode.InvalidPublicKey,\n );\n }\n}\n\n/**\n * Derive shared secret from private key and peer's public key.\n */\nexport async function deriveSharedSecret(privateKey: CryptoKey, peerPublicKey: CryptoKey): Promise<Uint8Array> {\n const subtle = getSubtle();\n\n try {\n // Derive 256 bits (32 bytes) of shared secret\n const sharedBits = await subtle.deriveBits(\n {\n name: 'ECDH',\n public: peerPublicKey,\n },\n privateKey,\n 256,\n );\n\n return new Uint8Array(sharedBits);\n } catch (err) {\n throw new EcdhError(\n `Failed to derive shared secret: ${err instanceof Error ? err.message : String(err)}`,\n EncryptionErrorCode.KeyDerivationFailed,\n );\n }\n}\n\n/**\n * Create a client hello message for the encryption handshake.\n */\nexport async function createClientHello(keyPair: EcdhKeyPair): Promise<ClientHello> {\n const { publicKeyB64, curve } = await exportPublicKey(keyPair.publicKey);\n\n return {\n protocolVersion: PROTOCOL_VERSION,\n clientEphemeralPubKeyB64: publicKeyB64,\n curve,\n supportedAlgorithms: [EncryptionAlgorithm.AES_GCM_256],\n };\n}\n\n/**\n * Create a server hello message for the encryption handshake.\n */\nexport async function createServerHello(keyPair: EcdhKeyPair, keyId: string): Promise<ServerHello> {\n const { publicKeyB64, curve } = await exportPublicKey(keyPair.publicKey);\n\n return {\n protocolVersion: PROTOCOL_VERSION,\n serverEphemeralPubKeyB64: publicKeyB64,\n curve,\n selectedAlgorithm: EncryptionAlgorithm.AES_GCM_256,\n kdf: KeyDerivation.HKDF_SHA256,\n keyId,\n };\n}\n\n/**\n * Process a client hello and generate server response.\n */\nexport async function processClientHello(clientHello: ClientHello): Promise<{\n serverKeyPair: EcdhKeyPair;\n peerPublicKey: CryptoKey;\n serverHello: ServerHello;\n keyId: string;\n}> {\n // Generate server's ephemeral key pair\n const serverKeyPair = await generateKeyPair(clientHello.curve);\n\n // Import client's public key\n const peerPublicKey = await importPublicKey(clientHello.clientEphemeralPubKeyB64, clientHello.curve);\n\n // Generate a key ID\n const keyId = `k_${crypto.randomUUID()}`;\n\n // Create server hello\n const serverHello = await createServerHello(serverKeyPair, keyId);\n\n return {\n serverKeyPair,\n peerPublicKey,\n serverHello,\n keyId,\n };\n}\n\n/**\n * Process a server hello and extract peer's public key.\n */\nexport async function processServerHello(serverHello: ServerHello): Promise<{\n peerPublicKey: CryptoKey;\n keyId: string;\n}> {\n const peerPublicKey = await importPublicKey(serverHello.serverEphemeralPubKeyB64, serverHello.curve);\n\n return {\n peerPublicKey,\n keyId: serverHello.keyId,\n };\n}\n"]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @enclave-vm/stream - HKDF Key Derivation
|
|
3
|
+
*
|
|
4
|
+
* HMAC-based Key Derivation Function using Web Crypto API.
|
|
5
|
+
* Derives encryption keys from ECDH shared secret.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* HKDF derivation error.
|
|
9
|
+
*/
|
|
10
|
+
export declare class HkdfError extends Error {
|
|
11
|
+
readonly code: string;
|
|
12
|
+
constructor(message: string, code: string);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Derive a key using HKDF-SHA256.
|
|
16
|
+
*
|
|
17
|
+
* @param sharedSecret - The shared secret from ECDH
|
|
18
|
+
* @param salt - Optional salt (defaults to zeros)
|
|
19
|
+
* @param info - Context info string
|
|
20
|
+
* @param keyLength - Output key length in bytes (default: 32 for AES-256)
|
|
21
|
+
*/
|
|
22
|
+
export declare function deriveKey(sharedSecret: Uint8Array, salt: Uint8Array | null, info: string, keyLength?: number): Promise<Uint8Array>;
|
|
23
|
+
/**
|
|
24
|
+
* Derive session keys for bidirectional communication.
|
|
25
|
+
*
|
|
26
|
+
* @param sharedSecret - The shared secret from ECDH
|
|
27
|
+
* @param sessionId - Session ID for domain separation
|
|
28
|
+
* @returns Client-to-server and server-to-client encryption keys
|
|
29
|
+
*/
|
|
30
|
+
export declare function deriveSessionKeys(sharedSecret: Uint8Array, sessionId: string): Promise<{
|
|
31
|
+
clientToServerKey: Uint8Array;
|
|
32
|
+
serverToClientKey: Uint8Array;
|
|
33
|
+
}>;
|
|
34
|
+
/**
|
|
35
|
+
* Import a raw key as a CryptoKey for AES-GCM.
|
|
36
|
+
*/
|
|
37
|
+
export declare function importAesGcmKey(keyBytes: Uint8Array): Promise<CryptoKey>;
|
|
38
|
+
/**
|
|
39
|
+
* Derive and import session keys as CryptoKeys.
|
|
40
|
+
*/
|
|
41
|
+
export declare function deriveSessionCryptoKeys(sharedSecret: Uint8Array, sessionId: string): Promise<{
|
|
42
|
+
clientToServerKey: CryptoKey;
|
|
43
|
+
serverToClientKey: CryptoKey;
|
|
44
|
+
}>;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @enclave-vm/stream - HKDF Key Derivation
|
|
4
|
+
*
|
|
5
|
+
* HMAC-based Key Derivation Function using Web Crypto API.
|
|
6
|
+
* Derives encryption keys from ECDH shared secret.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.HkdfError = void 0;
|
|
10
|
+
exports.deriveKey = deriveKey;
|
|
11
|
+
exports.deriveSessionKeys = deriveSessionKeys;
|
|
12
|
+
exports.importAesGcmKey = importAesGcmKey;
|
|
13
|
+
exports.deriveSessionCryptoKeys = deriveSessionCryptoKeys;
|
|
14
|
+
const types_1 = require("@enclave-vm/types");
|
|
15
|
+
/**
|
|
16
|
+
* Convert a Uint8Array to an ArrayBuffer.
|
|
17
|
+
* Handles the case where the Uint8Array is a view into a larger buffer.
|
|
18
|
+
*/
|
|
19
|
+
function toArrayBuffer(arr) {
|
|
20
|
+
if (arr.byteOffset === 0 && arr.byteLength === arr.buffer.byteLength) {
|
|
21
|
+
return arr.buffer;
|
|
22
|
+
}
|
|
23
|
+
return arr.buffer.slice(arr.byteOffset, arr.byteOffset + arr.byteLength);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* HKDF derivation error.
|
|
27
|
+
*/
|
|
28
|
+
class HkdfError extends Error {
|
|
29
|
+
code;
|
|
30
|
+
constructor(message, code) {
|
|
31
|
+
super(message);
|
|
32
|
+
this.code = code;
|
|
33
|
+
this.name = 'HkdfError';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.HkdfError = HkdfError;
|
|
37
|
+
/**
|
|
38
|
+
* Get the Web Crypto subtle API.
|
|
39
|
+
*/
|
|
40
|
+
function getSubtle() {
|
|
41
|
+
if (typeof globalThis.crypto?.subtle !== 'undefined') {
|
|
42
|
+
return globalThis.crypto.subtle;
|
|
43
|
+
}
|
|
44
|
+
throw new HkdfError('Web Crypto API not available', types_1.EncryptionErrorCode.KeyDerivationFailed);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Derive a key using HKDF-SHA256.
|
|
48
|
+
*
|
|
49
|
+
* @param sharedSecret - The shared secret from ECDH
|
|
50
|
+
* @param salt - Optional salt (defaults to zeros)
|
|
51
|
+
* @param info - Context info string
|
|
52
|
+
* @param keyLength - Output key length in bytes (default: 32 for AES-256)
|
|
53
|
+
*/
|
|
54
|
+
async function deriveKey(sharedSecret, salt, info, keyLength = types_1.AES_256_KEY_SIZE) {
|
|
55
|
+
const subtle = getSubtle();
|
|
56
|
+
try {
|
|
57
|
+
// Import the shared secret as a raw key for HKDF
|
|
58
|
+
const baseKey = await subtle.importKey('raw', toArrayBuffer(sharedSecret), 'HKDF', false, ['deriveBits']);
|
|
59
|
+
// Use empty salt if not provided
|
|
60
|
+
const saltBytes = salt ?? new Uint8Array(32);
|
|
61
|
+
// Encode info string as bytes
|
|
62
|
+
const infoBytes = new TextEncoder().encode(info);
|
|
63
|
+
// Derive the key bits
|
|
64
|
+
const derivedBits = await subtle.deriveBits({
|
|
65
|
+
name: 'HKDF',
|
|
66
|
+
hash: 'SHA-256',
|
|
67
|
+
salt: toArrayBuffer(saltBytes),
|
|
68
|
+
info: toArrayBuffer(infoBytes),
|
|
69
|
+
}, baseKey, keyLength * 8);
|
|
70
|
+
return new Uint8Array(derivedBits);
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
throw new HkdfError(`Failed to derive key: ${err instanceof Error ? err.message : String(err)}`, types_1.EncryptionErrorCode.KeyDerivationFailed);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Derive session keys for bidirectional communication.
|
|
78
|
+
*
|
|
79
|
+
* @param sharedSecret - The shared secret from ECDH
|
|
80
|
+
* @param sessionId - Session ID for domain separation
|
|
81
|
+
* @returns Client-to-server and server-to-client encryption keys
|
|
82
|
+
*/
|
|
83
|
+
async function deriveSessionKeys(sharedSecret, sessionId) {
|
|
84
|
+
// Use session ID as salt for domain separation
|
|
85
|
+
const salt = new TextEncoder().encode(sessionId);
|
|
86
|
+
// Derive separate keys for each direction
|
|
87
|
+
const [clientToServerKey, serverToClientKey] = await Promise.all([
|
|
88
|
+
deriveKey(sharedSecret, salt, types_1.HkdfInfo.ClientToServer),
|
|
89
|
+
deriveKey(sharedSecret, salt, types_1.HkdfInfo.ServerToClient),
|
|
90
|
+
]);
|
|
91
|
+
return {
|
|
92
|
+
clientToServerKey,
|
|
93
|
+
serverToClientKey,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Import a raw key as a CryptoKey for AES-GCM.
|
|
98
|
+
*/
|
|
99
|
+
async function importAesGcmKey(keyBytes) {
|
|
100
|
+
const subtle = getSubtle();
|
|
101
|
+
try {
|
|
102
|
+
return await subtle.importKey('raw', toArrayBuffer(keyBytes), {
|
|
103
|
+
name: 'AES-GCM',
|
|
104
|
+
length: keyBytes.length * 8,
|
|
105
|
+
}, false, // not extractable
|
|
106
|
+
['encrypt', 'decrypt']);
|
|
107
|
+
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
throw new HkdfError(`Failed to import AES key: ${err instanceof Error ? err.message : String(err)}`, types_1.EncryptionErrorCode.KeyDerivationFailed);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Derive and import session keys as CryptoKeys.
|
|
114
|
+
*/
|
|
115
|
+
async function deriveSessionCryptoKeys(sharedSecret, sessionId) {
|
|
116
|
+
const { clientToServerKey, serverToClientKey } = await deriveSessionKeys(sharedSecret, sessionId);
|
|
117
|
+
const [c2sKey, s2cKey] = await Promise.all([importAesGcmKey(clientToServerKey), importAesGcmKey(serverToClientKey)]);
|
|
118
|
+
return {
|
|
119
|
+
clientToServerKey: c2sKey,
|
|
120
|
+
serverToClientKey: s2cKey,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=hkdf.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hkdf.js","sourceRoot":"","sources":["../../../src/crypto/hkdf.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AA8CH,8BAqCC;AASD,8CAoBC;AAKD,0CAoBC;AAKD,0DAeC;AA3JD,6CAAoF;AAEpF;;;GAGG;AACH,SAAS,aAAa,CAAC,GAAe;IACpC,IAAI,GAAG,CAAC,UAAU,KAAK,CAAC,IAAI,GAAG,CAAC,UAAU,KAAK,GAAG,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;QACrE,OAAO,GAAG,CAAC,MAAqB,CAAC;IACnC,CAAC;IACD,OAAO,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,UAAU,GAAG,GAAG,CAAC,UAAU,CAAgB,CAAC;AAC1F,CAAC;AAED;;GAEG;AACH,MAAa,SAAU,SAAQ,KAAK;IAGhB;IAFlB,YACE,OAAe,EACC,IAAY;QAE5B,KAAK,CAAC,OAAO,CAAC,CAAC;QAFC,SAAI,GAAJ,IAAI,CAAQ;QAG5B,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC;IAC1B,CAAC;CACF;AARD,8BAQC;AAED;;GAEG;AACH,SAAS,SAAS;IAChB,IAAI,OAAO,UAAU,CAAC,MAAM,EAAE,MAAM,KAAK,WAAW,EAAE,CAAC;QACrD,OAAO,UAAU,CAAC,MAA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CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,eAAe,CAAC,iBAAiB,CAAC,EAAE,eAAe,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC;IAErH,OAAO;QACL,iBAAiB,EAAE,MAAM;QACzB,iBAAiB,EAAE,MAAM;KAC1B,CAAC;AACJ,CAAC","sourcesContent":["/**\n * @enclave-vm/stream - HKDF Key Derivation\n *\n * HMAC-based Key Derivation Function using Web Crypto API.\n * Derives encryption keys from ECDH shared secret.\n */\n\nimport { HkdfInfo, AES_256_KEY_SIZE, EncryptionErrorCode } from '@enclave-vm/types';\n\n/**\n * Convert a Uint8Array to an ArrayBuffer.\n * Handles the case where the Uint8Array is a view into a larger buffer.\n */\nfunction toArrayBuffer(arr: Uint8Array): ArrayBuffer {\n if (arr.byteOffset === 0 && arr.byteLength === arr.buffer.byteLength) {\n return arr.buffer as ArrayBuffer;\n }\n return arr.buffer.slice(arr.byteOffset, arr.byteOffset + arr.byteLength) as ArrayBuffer;\n}\n\n/**\n * HKDF derivation error.\n */\nexport class HkdfError extends Error {\n constructor(\n message: string,\n public readonly code: string,\n ) {\n super(message);\n this.name = 'HkdfError';\n }\n}\n\n/**\n * Get the Web Crypto subtle API.\n */\nfunction getSubtle(): SubtleCrypto {\n if (typeof globalThis.crypto?.subtle !== 'undefined') {\n return globalThis.crypto.subtle;\n }\n throw new HkdfError('Web Crypto API not available', EncryptionErrorCode.KeyDerivationFailed);\n}\n\n/**\n * Derive a key using HKDF-SHA256.\n *\n * @param sharedSecret - The shared secret from ECDH\n * @param salt - Optional salt (defaults to zeros)\n * @param info - Context info string\n * @param keyLength - Output key length in bytes (default: 32 for AES-256)\n */\nexport async function deriveKey(\n sharedSecret: Uint8Array,\n salt: Uint8Array | null,\n info: string,\n keyLength: number = AES_256_KEY_SIZE,\n): Promise<Uint8Array> {\n const subtle = getSubtle();\n\n try {\n // Import the shared secret as a raw key for HKDF\n const baseKey = await subtle.importKey('raw', toArrayBuffer(sharedSecret), 'HKDF', false, ['deriveBits']);\n\n // Use empty salt if not provided\n const saltBytes = salt ?? new Uint8Array(32);\n\n // Encode info string as bytes\n const infoBytes = new TextEncoder().encode(info);\n\n // Derive the key bits\n const derivedBits = await subtle.deriveBits(\n {\n name: 'HKDF',\n hash: 'SHA-256',\n salt: toArrayBuffer(saltBytes),\n info: toArrayBuffer(infoBytes),\n },\n baseKey,\n keyLength * 8, // bits\n );\n\n return new Uint8Array(derivedBits);\n } catch (err) {\n throw new HkdfError(\n `Failed to derive key: ${err instanceof Error ? err.message : String(err)}`,\n EncryptionErrorCode.KeyDerivationFailed,\n );\n }\n}\n\n/**\n * Derive session keys for bidirectional communication.\n *\n * @param sharedSecret - The shared secret from ECDH\n * @param sessionId - Session ID for domain separation\n * @returns Client-to-server and server-to-client encryption keys\n */\nexport async function deriveSessionKeys(\n sharedSecret: Uint8Array,\n sessionId: string,\n): Promise<{\n clientToServerKey: Uint8Array;\n serverToClientKey: Uint8Array;\n}> {\n // Use session ID as salt for domain separation\n const salt = new TextEncoder().encode(sessionId);\n\n // Derive separate keys for each direction\n const [clientToServerKey, serverToClientKey] = await Promise.all([\n deriveKey(sharedSecret, salt, HkdfInfo.ClientToServer),\n deriveKey(sharedSecret, salt, HkdfInfo.ServerToClient),\n ]);\n\n return {\n clientToServerKey,\n serverToClientKey,\n };\n}\n\n/**\n * Import a raw key as a CryptoKey for AES-GCM.\n */\nexport async function importAesGcmKey(keyBytes: Uint8Array): Promise<CryptoKey> {\n const subtle = getSubtle();\n\n try {\n return await subtle.importKey(\n 'raw',\n toArrayBuffer(keyBytes),\n {\n name: 'AES-GCM',\n length: keyBytes.length * 8,\n },\n false, // not extractable\n ['encrypt', 'decrypt'],\n );\n } catch (err) {\n throw new HkdfError(\n `Failed to import AES key: ${err instanceof Error ? err.message : String(err)}`,\n EncryptionErrorCode.KeyDerivationFailed,\n );\n }\n}\n\n/**\n * Derive and import session keys as CryptoKeys.\n */\nexport async function deriveSessionCryptoKeys(\n sharedSecret: Uint8Array,\n sessionId: string,\n): Promise<{\n clientToServerKey: CryptoKey;\n serverToClientKey: CryptoKey;\n}> {\n const { clientToServerKey, serverToClientKey } = await deriveSessionKeys(sharedSecret, sessionId);\n\n const [c2sKey, s2cKey] = await Promise.all([importAesGcmKey(clientToServerKey), importAesGcmKey(serverToClientKey)]);\n\n return {\n clientToServerKey: c2sKey,\n serverToClientKey: s2cKey,\n };\n}\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @enclave-vm/stream - Crypto module
|
|
3
|
+
*
|
|
4
|
+
* Per-hop encryption using ECDH key exchange and AES-GCM encryption.
|
|
5
|
+
*/
|
|
6
|
+
export { generateKeyPair, exportPublicKey, importPublicKey, deriveSharedSecret, createClientHello, createServerHello, processClientHello, processServerHello, EcdhError, } from './ecdh.js';
|
|
7
|
+
export type { EcdhKeyPair, SerializedPublicKey } from './ecdh.js';
|
|
8
|
+
export { deriveKey, deriveSessionKeys, importAesGcmKey, deriveSessionCryptoKeys, HkdfError } from './hkdf.js';
|
|
9
|
+
export { generateNonce, generateCounterNonce, toBase64, fromBase64, encrypt, decrypt, encryptJson, decryptJson, createEncryptedEnvelope, SessionEncryptionContext, AesGcmError, } from './aes-gcm.js';
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @enclave-vm/stream - Crypto module
|
|
4
|
+
*
|
|
5
|
+
* Per-hop encryption using ECDH key exchange and AES-GCM encryption.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.AesGcmError = exports.SessionEncryptionContext = exports.createEncryptedEnvelope = exports.decryptJson = exports.encryptJson = exports.decrypt = exports.encrypt = exports.fromBase64 = exports.toBase64 = exports.generateCounterNonce = exports.generateNonce = exports.HkdfError = exports.deriveSessionCryptoKeys = exports.importAesGcmKey = exports.deriveSessionKeys = exports.deriveKey = exports.EcdhError = exports.processServerHello = exports.processClientHello = exports.createServerHello = exports.createClientHello = exports.deriveSharedSecret = exports.importPublicKey = exports.exportPublicKey = exports.generateKeyPair = void 0;
|
|
9
|
+
// ECDH key exchange
|
|
10
|
+
var ecdh_js_1 = require("./ecdh.js");
|
|
11
|
+
Object.defineProperty(exports, "generateKeyPair", { enumerable: true, get: function () { return ecdh_js_1.generateKeyPair; } });
|
|
12
|
+
Object.defineProperty(exports, "exportPublicKey", { enumerable: true, get: function () { return ecdh_js_1.exportPublicKey; } });
|
|
13
|
+
Object.defineProperty(exports, "importPublicKey", { enumerable: true, get: function () { return ecdh_js_1.importPublicKey; } });
|
|
14
|
+
Object.defineProperty(exports, "deriveSharedSecret", { enumerable: true, get: function () { return ecdh_js_1.deriveSharedSecret; } });
|
|
15
|
+
Object.defineProperty(exports, "createClientHello", { enumerable: true, get: function () { return ecdh_js_1.createClientHello; } });
|
|
16
|
+
Object.defineProperty(exports, "createServerHello", { enumerable: true, get: function () { return ecdh_js_1.createServerHello; } });
|
|
17
|
+
Object.defineProperty(exports, "processClientHello", { enumerable: true, get: function () { return ecdh_js_1.processClientHello; } });
|
|
18
|
+
Object.defineProperty(exports, "processServerHello", { enumerable: true, get: function () { return ecdh_js_1.processServerHello; } });
|
|
19
|
+
Object.defineProperty(exports, "EcdhError", { enumerable: true, get: function () { return ecdh_js_1.EcdhError; } });
|
|
20
|
+
// HKDF key derivation
|
|
21
|
+
var hkdf_js_1 = require("./hkdf.js");
|
|
22
|
+
Object.defineProperty(exports, "deriveKey", { enumerable: true, get: function () { return hkdf_js_1.deriveKey; } });
|
|
23
|
+
Object.defineProperty(exports, "deriveSessionKeys", { enumerable: true, get: function () { return hkdf_js_1.deriveSessionKeys; } });
|
|
24
|
+
Object.defineProperty(exports, "importAesGcmKey", { enumerable: true, get: function () { return hkdf_js_1.importAesGcmKey; } });
|
|
25
|
+
Object.defineProperty(exports, "deriveSessionCryptoKeys", { enumerable: true, get: function () { return hkdf_js_1.deriveSessionCryptoKeys; } });
|
|
26
|
+
Object.defineProperty(exports, "HkdfError", { enumerable: true, get: function () { return hkdf_js_1.HkdfError; } });
|
|
27
|
+
// AES-GCM encryption
|
|
28
|
+
var aes_gcm_js_1 = require("./aes-gcm.js");
|
|
29
|
+
Object.defineProperty(exports, "generateNonce", { enumerable: true, get: function () { return aes_gcm_js_1.generateNonce; } });
|
|
30
|
+
Object.defineProperty(exports, "generateCounterNonce", { enumerable: true, get: function () { return aes_gcm_js_1.generateCounterNonce; } });
|
|
31
|
+
Object.defineProperty(exports, "toBase64", { enumerable: true, get: function () { return aes_gcm_js_1.toBase64; } });
|
|
32
|
+
Object.defineProperty(exports, "fromBase64", { enumerable: true, get: function () { return aes_gcm_js_1.fromBase64; } });
|
|
33
|
+
Object.defineProperty(exports, "encrypt", { enumerable: true, get: function () { return aes_gcm_js_1.encrypt; } });
|
|
34
|
+
Object.defineProperty(exports, "decrypt", { enumerable: true, get: function () { return aes_gcm_js_1.decrypt; } });
|
|
35
|
+
Object.defineProperty(exports, "encryptJson", { enumerable: true, get: function () { return aes_gcm_js_1.encryptJson; } });
|
|
36
|
+
Object.defineProperty(exports, "decryptJson", { enumerable: true, get: function () { return aes_gcm_js_1.decryptJson; } });
|
|
37
|
+
Object.defineProperty(exports, "createEncryptedEnvelope", { enumerable: true, get: function () { return aes_gcm_js_1.createEncryptedEnvelope; } });
|
|
38
|
+
Object.defineProperty(exports, "SessionEncryptionContext", { enumerable: true, get: function () { return aes_gcm_js_1.SessionEncryptionContext; } });
|
|
39
|
+
Object.defineProperty(exports, "AesGcmError", { enumerable: true, get: function () { return aes_gcm_js_1.AesGcmError; } });
|
|
40
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/crypto/index.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAEH,oBAAoB;AACpB,qCAUmB;AATjB,0GAAA,eAAe,OAAA;AACf,0GAAA,eAAe,OAAA;AACf,0GAAA,eAAe,OAAA;AACf,6GAAA,kBAAkB,OAAA;AAClB,4GAAA,iBAAiB,OAAA;AACjB,4GAAA,iBAAiB,OAAA;AACjB,6GAAA,kBAAkB,OAAA;AAClB,6GAAA,kBAAkB,OAAA;AAClB,oGAAA,SAAS,OAAA;AAKX,sBAAsB;AACtB,qCAA8G;AAArG,oGAAA,SAAS,OAAA;AAAE,4GAAA,iBAAiB,OAAA;AAAE,0GAAA,eAAe,OAAA;AAAE,kHAAA,uBAAuB,OAAA;AAAE,oGAAA,SAAS,OAAA;AAE1F,qBAAqB;AACrB,2CAYsB;AAXpB,2GAAA,aAAa,OAAA;AACb,kHAAA,oBAAoB,OAAA;AACpB,sGAAA,QAAQ,OAAA;AACR,wGAAA,UAAU,OAAA;AACV,qGAAA,OAAO,OAAA;AACP,qGAAA,OAAO,OAAA;AACP,yGAAA,WAAW,OAAA;AACX,yGAAA,WAAW,OAAA;AACX,qHAAA,uBAAuB,OAAA;AACvB,sHAAA,wBAAwB,OAAA;AACxB,yGAAA,WAAW,OAAA","sourcesContent":["/**\n * @enclave-vm/stream - Crypto module\n *\n * Per-hop encryption using ECDH key exchange and AES-GCM encryption.\n */\n\n// ECDH key exchange\nexport {\n generateKeyPair,\n exportPublicKey,\n importPublicKey,\n deriveSharedSecret,\n createClientHello,\n createServerHello,\n processClientHello,\n processServerHello,\n EcdhError,\n} from './ecdh.js';\n\nexport type { EcdhKeyPair, SerializedPublicKey } from './ecdh.js';\n\n// HKDF key derivation\nexport { deriveKey, deriveSessionKeys, importAesGcmKey, deriveSessionCryptoKeys, HkdfError } from './hkdf.js';\n\n// AES-GCM encryption\nexport {\n generateNonce,\n generateCounterNonce,\n toBase64,\n fromBase64,\n encrypt,\n decrypt,\n encryptJson,\n decryptJson,\n createEncryptedEnvelope,\n SessionEncryptionContext,\n AesGcmError,\n} from './aes-gcm.js';\n"]}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @enclave-vm/stream
|
|
3
|
+
*
|
|
4
|
+
* Streaming protocol implementation for EnclaveJS runtime.
|
|
5
|
+
* Includes NDJSON parsing, encryption, and reconnection handling.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
export * from '@enclave-vm/types';
|
|
10
|
+
export { serializeEvent, serializeEvents, parseLine, parseLines, NdjsonStreamParser, createNdjsonParseStream, createNdjsonSerializeStream, parseNdjsonStream, } from './ndjson.js';
|
|
11
|
+
export type { ParseResult } from './ndjson.js';
|
|
12
|
+
export { generateKeyPair, exportPublicKey, importPublicKey, deriveSharedSecret, createClientHello, createServerHello, processClientHello, processServerHello, EcdhError, deriveKey, deriveSessionKeys, importAesGcmKey, deriveSessionCryptoKeys, HkdfError, generateNonce, generateCounterNonce, toBase64, fromBase64, encrypt, decrypt, encryptJson, decryptJson, createEncryptedEnvelope, SessionEncryptionContext, AesGcmError, } from './crypto/index.js';
|
|
13
|
+
export type { EcdhKeyPair, SerializedPublicKey } from './crypto/index.js';
|
|
14
|
+
export { ConnectionState, DEFAULT_RECONNECTION_CONFIG, ReconnectionStateMachine, SequenceTracker, EventBuffer, HeartbeatMonitor, } from './reconnect.js';
|
|
15
|
+
export type { ReconnectionConfig, ReconnectionEvent } from './reconnect.js';
|
package/src/index.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @enclave-vm/stream
|
|
4
|
+
*
|
|
5
|
+
* Streaming protocol implementation for EnclaveJS runtime.
|
|
6
|
+
* Includes NDJSON parsing, encryption, and reconnection handling.
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.HeartbeatMonitor = exports.EventBuffer = exports.SequenceTracker = exports.ReconnectionStateMachine = exports.DEFAULT_RECONNECTION_CONFIG = exports.ConnectionState = exports.AesGcmError = exports.SessionEncryptionContext = exports.createEncryptedEnvelope = exports.decryptJson = exports.encryptJson = exports.decrypt = exports.encrypt = exports.fromBase64 = exports.toBase64 = exports.generateCounterNonce = exports.generateNonce = exports.HkdfError = exports.deriveSessionCryptoKeys = exports.importAesGcmKey = exports.deriveSessionKeys = exports.deriveKey = exports.EcdhError = exports.processServerHello = exports.processClientHello = exports.createServerHello = exports.createClientHello = exports.deriveSharedSecret = exports.importPublicKey = exports.exportPublicKey = exports.generateKeyPair = exports.parseNdjsonStream = exports.createNdjsonSerializeStream = exports.createNdjsonParseStream = exports.NdjsonStreamParser = exports.parseLines = exports.parseLine = exports.serializeEvents = exports.serializeEvent = void 0;
|
|
12
|
+
const tslib_1 = require("tslib");
|
|
13
|
+
// Re-export types from @enclave-vm/types
|
|
14
|
+
tslib_1.__exportStar(require("@enclave-vm/types"), exports);
|
|
15
|
+
// NDJSON exports
|
|
16
|
+
var ndjson_js_1 = require("./ndjson.js");
|
|
17
|
+
Object.defineProperty(exports, "serializeEvent", { enumerable: true, get: function () { return ndjson_js_1.serializeEvent; } });
|
|
18
|
+
Object.defineProperty(exports, "serializeEvents", { enumerable: true, get: function () { return ndjson_js_1.serializeEvents; } });
|
|
19
|
+
Object.defineProperty(exports, "parseLine", { enumerable: true, get: function () { return ndjson_js_1.parseLine; } });
|
|
20
|
+
Object.defineProperty(exports, "parseLines", { enumerable: true, get: function () { return ndjson_js_1.parseLines; } });
|
|
21
|
+
Object.defineProperty(exports, "NdjsonStreamParser", { enumerable: true, get: function () { return ndjson_js_1.NdjsonStreamParser; } });
|
|
22
|
+
Object.defineProperty(exports, "createNdjsonParseStream", { enumerable: true, get: function () { return ndjson_js_1.createNdjsonParseStream; } });
|
|
23
|
+
Object.defineProperty(exports, "createNdjsonSerializeStream", { enumerable: true, get: function () { return ndjson_js_1.createNdjsonSerializeStream; } });
|
|
24
|
+
Object.defineProperty(exports, "parseNdjsonStream", { enumerable: true, get: function () { return ndjson_js_1.parseNdjsonStream; } });
|
|
25
|
+
// Crypto exports
|
|
26
|
+
var index_js_1 = require("./crypto/index.js");
|
|
27
|
+
// ECDH
|
|
28
|
+
Object.defineProperty(exports, "generateKeyPair", { enumerable: true, get: function () { return index_js_1.generateKeyPair; } });
|
|
29
|
+
Object.defineProperty(exports, "exportPublicKey", { enumerable: true, get: function () { return index_js_1.exportPublicKey; } });
|
|
30
|
+
Object.defineProperty(exports, "importPublicKey", { enumerable: true, get: function () { return index_js_1.importPublicKey; } });
|
|
31
|
+
Object.defineProperty(exports, "deriveSharedSecret", { enumerable: true, get: function () { return index_js_1.deriveSharedSecret; } });
|
|
32
|
+
Object.defineProperty(exports, "createClientHello", { enumerable: true, get: function () { return index_js_1.createClientHello; } });
|
|
33
|
+
Object.defineProperty(exports, "createServerHello", { enumerable: true, get: function () { return index_js_1.createServerHello; } });
|
|
34
|
+
Object.defineProperty(exports, "processClientHello", { enumerable: true, get: function () { return index_js_1.processClientHello; } });
|
|
35
|
+
Object.defineProperty(exports, "processServerHello", { enumerable: true, get: function () { return index_js_1.processServerHello; } });
|
|
36
|
+
Object.defineProperty(exports, "EcdhError", { enumerable: true, get: function () { return index_js_1.EcdhError; } });
|
|
37
|
+
// HKDF
|
|
38
|
+
Object.defineProperty(exports, "deriveKey", { enumerable: true, get: function () { return index_js_1.deriveKey; } });
|
|
39
|
+
Object.defineProperty(exports, "deriveSessionKeys", { enumerable: true, get: function () { return index_js_1.deriveSessionKeys; } });
|
|
40
|
+
Object.defineProperty(exports, "importAesGcmKey", { enumerable: true, get: function () { return index_js_1.importAesGcmKey; } });
|
|
41
|
+
Object.defineProperty(exports, "deriveSessionCryptoKeys", { enumerable: true, get: function () { return index_js_1.deriveSessionCryptoKeys; } });
|
|
42
|
+
Object.defineProperty(exports, "HkdfError", { enumerable: true, get: function () { return index_js_1.HkdfError; } });
|
|
43
|
+
// AES-GCM
|
|
44
|
+
Object.defineProperty(exports, "generateNonce", { enumerable: true, get: function () { return index_js_1.generateNonce; } });
|
|
45
|
+
Object.defineProperty(exports, "generateCounterNonce", { enumerable: true, get: function () { return index_js_1.generateCounterNonce; } });
|
|
46
|
+
Object.defineProperty(exports, "toBase64", { enumerable: true, get: function () { return index_js_1.toBase64; } });
|
|
47
|
+
Object.defineProperty(exports, "fromBase64", { enumerable: true, get: function () { return index_js_1.fromBase64; } });
|
|
48
|
+
Object.defineProperty(exports, "encrypt", { enumerable: true, get: function () { return index_js_1.encrypt; } });
|
|
49
|
+
Object.defineProperty(exports, "decrypt", { enumerable: true, get: function () { return index_js_1.decrypt; } });
|
|
50
|
+
Object.defineProperty(exports, "encryptJson", { enumerable: true, get: function () { return index_js_1.encryptJson; } });
|
|
51
|
+
Object.defineProperty(exports, "decryptJson", { enumerable: true, get: function () { return index_js_1.decryptJson; } });
|
|
52
|
+
Object.defineProperty(exports, "createEncryptedEnvelope", { enumerable: true, get: function () { return index_js_1.createEncryptedEnvelope; } });
|
|
53
|
+
Object.defineProperty(exports, "SessionEncryptionContext", { enumerable: true, get: function () { return index_js_1.SessionEncryptionContext; } });
|
|
54
|
+
Object.defineProperty(exports, "AesGcmError", { enumerable: true, get: function () { return index_js_1.AesGcmError; } });
|
|
55
|
+
// Reconnection exports
|
|
56
|
+
var reconnect_js_1 = require("./reconnect.js");
|
|
57
|
+
Object.defineProperty(exports, "ConnectionState", { enumerable: true, get: function () { return reconnect_js_1.ConnectionState; } });
|
|
58
|
+
Object.defineProperty(exports, "DEFAULT_RECONNECTION_CONFIG", { enumerable: true, get: function () { return reconnect_js_1.DEFAULT_RECONNECTION_CONFIG; } });
|
|
59
|
+
Object.defineProperty(exports, "ReconnectionStateMachine", { enumerable: true, get: function () { return reconnect_js_1.ReconnectionStateMachine; } });
|
|
60
|
+
Object.defineProperty(exports, "SequenceTracker", { enumerable: true, get: function () { return reconnect_js_1.SequenceTracker; } });
|
|
61
|
+
Object.defineProperty(exports, "EventBuffer", { enumerable: true, get: function () { return reconnect_js_1.EventBuffer; } });
|
|
62
|
+
Object.defineProperty(exports, "HeartbeatMonitor", { enumerable: true, get: function () { return reconnect_js_1.HeartbeatMonitor; } });
|
|
63
|
+
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;;AAEH,yCAAyC;AACzC,4DAAkC;AAElC,iBAAiB;AACjB,yCASqB;AARnB,2GAAA,cAAc,OAAA;AACd,4GAAA,eAAe,OAAA;AACf,sGAAA,SAAS,OAAA;AACT,uGAAA,UAAU,OAAA;AACV,+GAAA,kBAAkB,OAAA;AAClB,oHAAA,uBAAuB,OAAA;AACvB,wHAAA,2BAA2B,OAAA;AAC3B,8GAAA,iBAAiB,OAAA;AAKnB,iBAAiB;AACjB,8CA6B2B;AA5BzB,OAAO;AACP,2GAAA,eAAe,OAAA;AACf,2GAAA,eAAe,OAAA;AACf,2GAAA,eAAe,OAAA;AACf,8GAAA,kBAAkB,OAAA;AAClB,6GAAA,iBAAiB,OAAA;AACjB,6GAAA,iBAAiB,OAAA;AACjB,8GAAA,kBAAkB,OAAA;AAClB,8GAAA,kBAAkB,OAAA;AAClB,qGAAA,SAAS,OAAA;AACT,OAAO;AACP,qGAAA,SAAS,OAAA;AACT,6GAAA,iBAAiB,OAAA;AACjB,2GAAA,eAAe,OAAA;AACf,mHAAA,uBAAuB,OAAA;AACvB,qGAAA,SAAS,OAAA;AACT,UAAU;AACV,yGAAA,aAAa,OAAA;AACb,gHAAA,oBAAoB,OAAA;AACpB,oGAAA,QAAQ,OAAA;AACR,sGAAA,UAAU,OAAA;AACV,mGAAA,OAAO,OAAA;AACP,mGAAA,OAAO,OAAA;AACP,uGAAA,WAAW,OAAA;AACX,uGAAA,WAAW,OAAA;AACX,mHAAA,uBAAuB,OAAA;AACvB,oHAAA,wBAAwB,OAAA;AACxB,uGAAA,WAAW,OAAA;AAKb,uBAAuB;AACvB,+CAOwB;AANtB,+GAAA,eAAe,OAAA;AACf,2HAAA,2BAA2B,OAAA;AAC3B,wHAAA,wBAAwB,OAAA;AACxB,+GAAA,eAAe,OAAA;AACf,2GAAA,WAAW,OAAA;AACX,gHAAA,gBAAgB,OAAA","sourcesContent":["/**\n * @enclave-vm/stream\n *\n * Streaming protocol implementation for EnclaveJS runtime.\n * Includes NDJSON parsing, encryption, and reconnection handling.\n *\n * @packageDocumentation\n */\n\n// Re-export types from @enclave-vm/types\nexport * from '@enclave-vm/types';\n\n// NDJSON exports\nexport {\n serializeEvent,\n serializeEvents,\n parseLine,\n parseLines,\n NdjsonStreamParser,\n createNdjsonParseStream,\n createNdjsonSerializeStream,\n parseNdjsonStream,\n} from './ndjson.js';\n\nexport type { ParseResult } from './ndjson.js';\n\n// Crypto exports\nexport {\n // ECDH\n generateKeyPair,\n exportPublicKey,\n importPublicKey,\n deriveSharedSecret,\n createClientHello,\n createServerHello,\n processClientHello,\n processServerHello,\n EcdhError,\n // HKDF\n deriveKey,\n deriveSessionKeys,\n importAesGcmKey,\n deriveSessionCryptoKeys,\n HkdfError,\n // AES-GCM\n generateNonce,\n generateCounterNonce,\n toBase64,\n fromBase64,\n encrypt,\n decrypt,\n encryptJson,\n decryptJson,\n createEncryptedEnvelope,\n SessionEncryptionContext,\n AesGcmError,\n} from './crypto/index.js';\n\nexport type { EcdhKeyPair, SerializedPublicKey } from './crypto/index.js';\n\n// Reconnection exports\nexport {\n ConnectionState,\n DEFAULT_RECONNECTION_CONFIG,\n ReconnectionStateMachine,\n SequenceTracker,\n EventBuffer,\n HeartbeatMonitor,\n} from './reconnect.js';\n\nexport type { ReconnectionConfig, ReconnectionEvent } from './reconnect.js';\n"]}
|