@enclave-vm/types 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 ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@enclave-vm/types",
3
+ "version": "0.0.1",
4
+ "description": "Type definitions and Zod schemas for the EnclaveJS streaming runtime protocol",
5
+ "author": "AgentFront <info@agentfront.dev>",
6
+ "homepage": "https://github.com/agentfront/enclave",
7
+ "license": "Apache-2.0",
8
+ "keywords": [
9
+ "enclavejs",
10
+ "enclave",
11
+ "streaming",
12
+ "types",
13
+ "zod",
14
+ "protocol",
15
+ "runtime"
16
+ ],
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/agentfront/enclave.git"
20
+ },
21
+ "bugs": {
22
+ "url": "https://github.com/agentfront/enclave/issues"
23
+ },
24
+ "main": "./src/index.js",
25
+ "types": "./src/index.d.ts",
26
+ "exports": {
27
+ "./package.json": "./package.json",
28
+ ".": {
29
+ "types": "./src/index.d.ts",
30
+ "import": "./src/index.js",
31
+ "default": "./src/index.js"
32
+ }
33
+ },
34
+ "dependencies": {
35
+ "zod": "^4.1.13"
36
+ },
37
+ "type": "commonjs"
38
+ }
@@ -0,0 +1,191 @@
1
+ /**
2
+ * @enclave-vm/types - Encryption types
3
+ *
4
+ * Types for per-hop encryption using ECDH + AES-GCM.
5
+ * Encryption is optional but recommended to prevent intermediaries from reading payloads.
6
+ */
7
+ import type { ProtocolVersion, SessionId } from './protocol.js';
8
+ import type { EventType } from './events.js';
9
+ /**
10
+ * Supported elliptic curves for ECDH key exchange.
11
+ * Using P-256 for broad browser compatibility via Web Crypto API.
12
+ */
13
+ export declare const SupportedCurve: {
14
+ readonly P256: "P-256";
15
+ };
16
+ export type SupportedCurve = (typeof SupportedCurve)[keyof typeof SupportedCurve];
17
+ /**
18
+ * Encryption algorithm identifiers.
19
+ */
20
+ export declare const EncryptionAlgorithm: {
21
+ /** AES-GCM with 256-bit key */
22
+ readonly AES_GCM_256: "AES-GCM-256";
23
+ };
24
+ export type EncryptionAlgorithm = (typeof EncryptionAlgorithm)[keyof typeof EncryptionAlgorithm];
25
+ /**
26
+ * Key derivation function identifiers.
27
+ */
28
+ export declare const KeyDerivation: {
29
+ /** HKDF with SHA-256 */
30
+ readonly HKDF_SHA256: "HKDF-SHA-256";
31
+ };
32
+ export type KeyDerivation = (typeof KeyDerivation)[keyof typeof KeyDerivation];
33
+ /**
34
+ * Encrypted envelope payload.
35
+ * Contains the encrypted inner event.
36
+ */
37
+ export interface EncryptedEnvelopePayload {
38
+ /** Key ID identifying which session key to use */
39
+ kid: string;
40
+ /** Base64-encoded nonce/IV (12 bytes for AES-GCM) */
41
+ nonceB64: string;
42
+ /** Base64-encoded ciphertext (encrypted JSON of inner event) */
43
+ ciphertextB64: string;
44
+ /** Base64-encoded authentication tag (16 bytes for AES-GCM) */
45
+ tagB64?: string;
46
+ }
47
+ /**
48
+ * Encrypted envelope event.
49
+ * Wraps any other event type when encryption is enabled.
50
+ */
51
+ export interface EncryptedEnvelope {
52
+ protocolVersion: ProtocolVersion;
53
+ sessionId: SessionId;
54
+ /** Sequence number (visible even when encrypted for ordering) */
55
+ seq: number;
56
+ type: typeof EventType.Encrypted;
57
+ payload: EncryptedEnvelopePayload;
58
+ }
59
+ /**
60
+ * Client hello - first message in handshake.
61
+ * Sent by client with session creation request.
62
+ */
63
+ export interface ClientHello {
64
+ /** Protocol version */
65
+ protocolVersion: ProtocolVersion;
66
+ /** Client's ephemeral public key (base64 encoded) */
67
+ clientEphemeralPubKeyB64: string;
68
+ /** Elliptic curve used */
69
+ curve: SupportedCurve;
70
+ /** Encryption algorithms the client supports */
71
+ supportedAlgorithms: EncryptionAlgorithm[];
72
+ }
73
+ /**
74
+ * Server hello - response to client hello.
75
+ * Included in session_init event when encryption is negotiated.
76
+ */
77
+ export interface ServerHello {
78
+ /** Protocol version */
79
+ protocolVersion: ProtocolVersion;
80
+ /** Server's ephemeral public key (base64 encoded) */
81
+ serverEphemeralPubKeyB64: string;
82
+ /** Elliptic curve used */
83
+ curve: SupportedCurve;
84
+ /** Selected encryption algorithm */
85
+ selectedAlgorithm: EncryptionAlgorithm;
86
+ /** Key derivation function used */
87
+ kdf: KeyDerivation;
88
+ /** Key ID for the derived session key */
89
+ keyId: string;
90
+ /**
91
+ * Signature over the handshake transcript (base64 encoded).
92
+ * Allows client to verify server identity if server has a pinned key.
93
+ */
94
+ signatureB64?: string;
95
+ }
96
+ /**
97
+ * Encryption mode for session creation request.
98
+ */
99
+ export declare const EncryptionMode: {
100
+ /** Encryption is disabled */
101
+ readonly Disabled: "disabled";
102
+ /** Encryption is optional (server decides) */
103
+ readonly Optional: "optional";
104
+ /** Encryption is required */
105
+ readonly Required: "required";
106
+ };
107
+ export type EncryptionMode = (typeof EncryptionMode)[keyof typeof EncryptionMode];
108
+ /**
109
+ * Encryption request in session creation.
110
+ */
111
+ export interface EncryptionRequest {
112
+ /** Requested encryption mode */
113
+ mode: EncryptionMode;
114
+ /** Client hello (required if mode is not 'disabled') */
115
+ clientHello?: ClientHello;
116
+ }
117
+ /**
118
+ * Session encryption key information.
119
+ * Used internally by both client and server.
120
+ */
121
+ export interface SessionKeyInfo {
122
+ /** Key ID */
123
+ keyId: string;
124
+ /** Encryption algorithm */
125
+ algorithm: EncryptionAlgorithm;
126
+ /** Raw key bytes (32 bytes for AES-256) */
127
+ keyBytes: Uint8Array;
128
+ /** Counter for nonce generation (to avoid nonce reuse) */
129
+ nonceCounter: bigint;
130
+ /** Maximum nonces before key rotation required */
131
+ maxNonces: bigint;
132
+ /** Timestamp when key was derived */
133
+ createdAt: number;
134
+ }
135
+ /**
136
+ * Encryption-specific error codes.
137
+ */
138
+ export declare const EncryptionErrorCode: {
139
+ /** Handshake failed */
140
+ readonly HandshakeFailed: "ENCRYPTION_HANDSHAKE_FAILED";
141
+ /** Key derivation failed */
142
+ readonly KeyDerivationFailed: "ENCRYPTION_KEY_DERIVATION_FAILED";
143
+ /** Decryption failed (invalid ciphertext or authentication) */
144
+ readonly DecryptionFailed: "ENCRYPTION_DECRYPTION_FAILED";
145
+ /** Nonce reuse detected */
146
+ readonly NonceReuse: "ENCRYPTION_NONCE_REUSE";
147
+ /** Key expired or needs rotation */
148
+ readonly KeyExpired: "ENCRYPTION_KEY_EXPIRED";
149
+ /** Unsupported algorithm */
150
+ readonly UnsupportedAlgorithm: "ENCRYPTION_UNSUPPORTED_ALGORITHM";
151
+ /** Invalid public key */
152
+ readonly InvalidPublicKey: "ENCRYPTION_INVALID_PUBLIC_KEY";
153
+ /** Signature verification failed */
154
+ readonly SignatureVerificationFailed: "ENCRYPTION_SIGNATURE_VERIFICATION_FAILED";
155
+ };
156
+ export type EncryptionErrorCode = (typeof EncryptionErrorCode)[keyof typeof EncryptionErrorCode];
157
+ /**
158
+ * Input or output that can be either plaintext or encrypted.
159
+ */
160
+ export type MaybeEncrypted<T> = T | EncryptedEnvelope;
161
+ /**
162
+ * Check if a value is an encrypted envelope.
163
+ */
164
+ export declare function isEncryptedEnvelope(value: unknown): value is EncryptedEnvelope;
165
+ /**
166
+ * HKDF info strings for different key purposes.
167
+ */
168
+ export declare const HkdfInfo: {
169
+ /** Client -> Server encryption key */
170
+ readonly ClientToServer: "enclavejs-c2s-enc";
171
+ /** Server -> Client encryption key */
172
+ readonly ServerToClient: "enclavejs-s2c-enc";
173
+ };
174
+ export type HkdfInfo = (typeof HkdfInfo)[keyof typeof HkdfInfo];
175
+ /**
176
+ * Nonce/IV size in bytes for AES-GCM.
177
+ */
178
+ export declare const AES_GCM_NONCE_SIZE = 12;
179
+ /**
180
+ * Authentication tag size in bytes for AES-GCM.
181
+ */
182
+ export declare const AES_GCM_TAG_SIZE = 16;
183
+ /**
184
+ * Key size in bytes for AES-256.
185
+ */
186
+ export declare const AES_256_KEY_SIZE = 32;
187
+ /**
188
+ * Maximum number of messages with one key (2^32 for AES-GCM with random nonces).
189
+ * We use a lower limit (2^30) to be safe.
190
+ */
191
+ export declare const MAX_MESSAGES_PER_KEY = 1073741824n;
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ /**
3
+ * @enclave-vm/types - Encryption types
4
+ *
5
+ * Types for per-hop encryption using ECDH + AES-GCM.
6
+ * Encryption is optional but recommended to prevent intermediaries from reading payloads.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.MAX_MESSAGES_PER_KEY = exports.AES_256_KEY_SIZE = exports.AES_GCM_TAG_SIZE = exports.AES_GCM_NONCE_SIZE = exports.HkdfInfo = exports.EncryptionErrorCode = exports.EncryptionMode = exports.KeyDerivation = exports.EncryptionAlgorithm = exports.SupportedCurve = void 0;
10
+ exports.isEncryptedEnvelope = isEncryptedEnvelope;
11
+ /**
12
+ * Supported elliptic curves for ECDH key exchange.
13
+ * Using P-256 for broad browser compatibility via Web Crypto API.
14
+ */
15
+ exports.SupportedCurve = {
16
+ P256: 'P-256',
17
+ };
18
+ /**
19
+ * Encryption algorithm identifiers.
20
+ */
21
+ exports.EncryptionAlgorithm = {
22
+ /** AES-GCM with 256-bit key */
23
+ AES_GCM_256: 'AES-GCM-256',
24
+ };
25
+ /**
26
+ * Key derivation function identifiers.
27
+ */
28
+ exports.KeyDerivation = {
29
+ /** HKDF with SHA-256 */
30
+ HKDF_SHA256: 'HKDF-SHA-256',
31
+ };
32
+ /**
33
+ * Encryption mode for session creation request.
34
+ */
35
+ exports.EncryptionMode = {
36
+ /** Encryption is disabled */
37
+ Disabled: 'disabled',
38
+ /** Encryption is optional (server decides) */
39
+ Optional: 'optional',
40
+ /** Encryption is required */
41
+ Required: 'required',
42
+ };
43
+ // ============================================================================
44
+ // Encryption Errors
45
+ // ============================================================================
46
+ /**
47
+ * Encryption-specific error codes.
48
+ */
49
+ exports.EncryptionErrorCode = {
50
+ /** Handshake failed */
51
+ HandshakeFailed: 'ENCRYPTION_HANDSHAKE_FAILED',
52
+ /** Key derivation failed */
53
+ KeyDerivationFailed: 'ENCRYPTION_KEY_DERIVATION_FAILED',
54
+ /** Decryption failed (invalid ciphertext or authentication) */
55
+ DecryptionFailed: 'ENCRYPTION_DECRYPTION_FAILED',
56
+ /** Nonce reuse detected */
57
+ NonceReuse: 'ENCRYPTION_NONCE_REUSE',
58
+ /** Key expired or needs rotation */
59
+ KeyExpired: 'ENCRYPTION_KEY_EXPIRED',
60
+ /** Unsupported algorithm */
61
+ UnsupportedAlgorithm: 'ENCRYPTION_UNSUPPORTED_ALGORITHM',
62
+ /** Invalid public key */
63
+ InvalidPublicKey: 'ENCRYPTION_INVALID_PUBLIC_KEY',
64
+ /** Signature verification failed */
65
+ SignatureVerificationFailed: 'ENCRYPTION_SIGNATURE_VERIFICATION_FAILED',
66
+ };
67
+ /**
68
+ * Check if a value is an encrypted envelope.
69
+ */
70
+ function isEncryptedEnvelope(value) {
71
+ return (typeof value === 'object' &&
72
+ value !== null &&
73
+ 'type' in value &&
74
+ value.type === 'enc' &&
75
+ 'payload' in value &&
76
+ typeof value.payload === 'object' &&
77
+ value.payload !== null &&
78
+ 'kid' in value.payload &&
79
+ 'nonceB64' in value.payload &&
80
+ 'ciphertextB64' in value.payload);
81
+ }
82
+ /**
83
+ * HKDF info strings for different key purposes.
84
+ */
85
+ exports.HkdfInfo = {
86
+ /** Client -> Server encryption key */
87
+ ClientToServer: 'enclavejs-c2s-enc',
88
+ /** Server -> Client encryption key */
89
+ ServerToClient: 'enclavejs-s2c-enc',
90
+ };
91
+ /**
92
+ * Nonce/IV size in bytes for AES-GCM.
93
+ */
94
+ exports.AES_GCM_NONCE_SIZE = 12;
95
+ /**
96
+ * Authentication tag size in bytes for AES-GCM.
97
+ */
98
+ exports.AES_GCM_TAG_SIZE = 16;
99
+ /**
100
+ * Key size in bytes for AES-256.
101
+ */
102
+ exports.AES_256_KEY_SIZE = 32;
103
+ /**
104
+ * Maximum number of messages with one key (2^32 for AES-GCM with random nonces).
105
+ * We use a lower limit (2^30) to be safe.
106
+ */
107
+ exports.MAX_MESSAGES_PER_KEY = 1073741824n; // 2^30
108
+ //# sourceMappingURL=encryption.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encryption.js","sourceRoot":"","sources":["../../src/encryption.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAqMH,kDAaC;AA7MD;;;GAGG;AACU,QAAA,cAAc,GAAG;IAC5B,IAAI,EAAE,OAAO;CACL,CAAC;AAIX;;GAEG;AACU,QAAA,mBAAmB,GAAG;IACjC,+BAA+B;IAC/B,WAAW,EAAE,aAAa;CAClB,CAAC;AAIX;;GAEG;AACU,QAAA,aAAa,GAAG;IAC3B,wBAAwB;IACxB,WAAW,EAAE,cAAc;CACnB,CAAC;AA+EX;;GAEG;AACU,QAAA,cAAc,GAAG;IAC5B,6BAA6B;IAC7B,QAAQ,EAAE,UAAU;IACpB,8CAA8C;IAC9C,QAAQ,EAAE,UAAU;IACpB,6BAA6B;IAC7B,QAAQ,EAAE,UAAU;CACZ,CAAC;AAqCX,+EAA+E;AAC/E,oBAAoB;AACpB,+EAA+E;AAE/E;;GAEG;AACU,QAAA,mBAAmB,GAAG;IACjC,uBAAuB;IACvB,eAAe,EAAE,6BAA6B;IAC9C,4BAA4B;IAC5B,mBAAmB,EAAE,kCAAkC;IACvD,+DAA+D;IAC/D,gBAAgB,EAAE,8BAA8B;IAChD,2BAA2B;IAC3B,UAAU,EAAE,wBAAwB;IACpC,oCAAoC;IACpC,UAAU,EAAE,wBAAwB;IACpC,4BAA4B;IAC5B,oBAAoB,EAAE,kCAAkC;IACxD,yBAAyB;IACzB,gBAAgB,EAAE,+BAA+B;IACjD,oCAAoC;IACpC,2BAA2B,EAAE,0CAA0C;CAC/D,CAAC;AAaX;;GAEG;AACH,SAAgB,mBAAmB,CAAC,KAAc;IAChD,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,IAAI,KAAK;QACd,KAA2B,CAAC,IAAI,KAAK,KAAK;QAC3C,SAAS,IAAI,KAAK;QAClB,OAAQ,KAA2B,CAAC,OAAO,KAAK,QAAQ;QACvD,KAA2B,CAAC,OAAO,KAAK,IAAI;QAC7C,KAAK,IAAK,KAA2B,CAAC,OAAO;QAC7C,UAAU,IAAK,KAA2B,CAAC,OAAO;QAClD,eAAe,IAAK,KAA2B,CAAC,OAAO,CACxD,CAAC;AACJ,CAAC;AAED;;GAEG;AACU,QAAA,QAAQ,GAAG;IACtB,sCAAsC;IACtC,cAAc,EAAE,mBAAmB;IACnC,sCAAsC;IACtC,cAAc,EAAE,mBAAmB;CAC3B,CAAC;AAIX;;GAEG;AACU,QAAA,kBAAkB,GAAG,EAAE,CAAC;AAErC;;GAEG;AACU,QAAA,gBAAgB,GAAG,EAAE,CAAC;AAEnC;;GAEG;AACU,QAAA,gBAAgB,GAAG,EAAE,CAAC;AAEnC;;;GAGG;AACU,QAAA,oBAAoB,GAAG,WAAW,CAAC,CAAC,OAAO","sourcesContent":["/**\n * @enclave-vm/types - Encryption types\n *\n * Types for per-hop encryption using ECDH + AES-GCM.\n * Encryption is optional but recommended to prevent intermediaries from reading payloads.\n */\n\nimport type { ProtocolVersion, SessionId } from './protocol.js';\nimport type { EventType } from './events.js';\n\n/**\n * Supported elliptic curves for ECDH key exchange.\n * Using P-256 for broad browser compatibility via Web Crypto API.\n */\nexport const SupportedCurve = {\n P256: 'P-256',\n} as const;\n\nexport type SupportedCurve = (typeof SupportedCurve)[keyof typeof SupportedCurve];\n\n/**\n * Encryption algorithm identifiers.\n */\nexport const EncryptionAlgorithm = {\n /** AES-GCM with 256-bit key */\n AES_GCM_256: 'AES-GCM-256',\n} as const;\n\nexport type EncryptionAlgorithm = (typeof EncryptionAlgorithm)[keyof typeof EncryptionAlgorithm];\n\n/**\n * Key derivation function identifiers.\n */\nexport const KeyDerivation = {\n /** HKDF with SHA-256 */\n HKDF_SHA256: 'HKDF-SHA-256',\n} as const;\n\nexport type KeyDerivation = (typeof KeyDerivation)[keyof typeof KeyDerivation];\n\n// ============================================================================\n// Encrypted Envelope\n// ============================================================================\n\n/**\n * Encrypted envelope payload.\n * Contains the encrypted inner event.\n */\nexport interface EncryptedEnvelopePayload {\n /** Key ID identifying which session key to use */\n kid: string;\n /** Base64-encoded nonce/IV (12 bytes for AES-GCM) */\n nonceB64: string;\n /** Base64-encoded ciphertext (encrypted JSON of inner event) */\n ciphertextB64: string;\n /** Base64-encoded authentication tag (16 bytes for AES-GCM) */\n tagB64?: string;\n}\n\n/**\n * Encrypted envelope event.\n * Wraps any other event type when encryption is enabled.\n */\nexport interface EncryptedEnvelope {\n protocolVersion: ProtocolVersion;\n sessionId: SessionId;\n /** Sequence number (visible even when encrypted for ordering) */\n seq: number;\n type: typeof EventType.Encrypted;\n payload: EncryptedEnvelopePayload;\n}\n\n// ============================================================================\n// Key Exchange (Handshake)\n// ============================================================================\n\n/**\n * Client hello - first message in handshake.\n * Sent by client with session creation request.\n */\nexport interface ClientHello {\n /** Protocol version */\n protocolVersion: ProtocolVersion;\n /** Client's ephemeral public key (base64 encoded) */\n clientEphemeralPubKeyB64: string;\n /** Elliptic curve used */\n curve: SupportedCurve;\n /** Encryption algorithms the client supports */\n supportedAlgorithms: EncryptionAlgorithm[];\n}\n\n/**\n * Server hello - response to client hello.\n * Included in session_init event when encryption is negotiated.\n */\nexport interface ServerHello {\n /** Protocol version */\n protocolVersion: ProtocolVersion;\n /** Server's ephemeral public key (base64 encoded) */\n serverEphemeralPubKeyB64: string;\n /** Elliptic curve used */\n curve: SupportedCurve;\n /** Selected encryption algorithm */\n selectedAlgorithm: EncryptionAlgorithm;\n /** Key derivation function used */\n kdf: KeyDerivation;\n /** Key ID for the derived session key */\n keyId: string;\n /**\n * Signature over the handshake transcript (base64 encoded).\n * Allows client to verify server identity if server has a pinned key.\n */\n signatureB64?: string;\n}\n\n/**\n * Encryption mode for session creation request.\n */\nexport const EncryptionMode = {\n /** Encryption is disabled */\n Disabled: 'disabled',\n /** Encryption is optional (server decides) */\n Optional: 'optional',\n /** Encryption is required */\n Required: 'required',\n} as const;\n\nexport type EncryptionMode = (typeof EncryptionMode)[keyof typeof EncryptionMode];\n\n/**\n * Encryption request in session creation.\n */\nexport interface EncryptionRequest {\n /** Requested encryption mode */\n mode: EncryptionMode;\n /** Client hello (required if mode is not 'disabled') */\n clientHello?: ClientHello;\n}\n\n// ============================================================================\n// Session Key Info\n// ============================================================================\n\n/**\n * Session encryption key information.\n * Used internally by both client and server.\n */\nexport interface SessionKeyInfo {\n /** Key ID */\n keyId: string;\n /** Encryption algorithm */\n algorithm: EncryptionAlgorithm;\n /** Raw key bytes (32 bytes for AES-256) */\n keyBytes: Uint8Array;\n /** Counter for nonce generation (to avoid nonce reuse) */\n nonceCounter: bigint;\n /** Maximum nonces before key rotation required */\n maxNonces: bigint;\n /** Timestamp when key was derived */\n createdAt: number;\n}\n\n// ============================================================================\n// Encryption Errors\n// ============================================================================\n\n/**\n * Encryption-specific error codes.\n */\nexport const EncryptionErrorCode = {\n /** Handshake failed */\n HandshakeFailed: 'ENCRYPTION_HANDSHAKE_FAILED',\n /** Key derivation failed */\n KeyDerivationFailed: 'ENCRYPTION_KEY_DERIVATION_FAILED',\n /** Decryption failed (invalid ciphertext or authentication) */\n DecryptionFailed: 'ENCRYPTION_DECRYPTION_FAILED',\n /** Nonce reuse detected */\n NonceReuse: 'ENCRYPTION_NONCE_REUSE',\n /** Key expired or needs rotation */\n KeyExpired: 'ENCRYPTION_KEY_EXPIRED',\n /** Unsupported algorithm */\n UnsupportedAlgorithm: 'ENCRYPTION_UNSUPPORTED_ALGORITHM',\n /** Invalid public key */\n InvalidPublicKey: 'ENCRYPTION_INVALID_PUBLIC_KEY',\n /** Signature verification failed */\n SignatureVerificationFailed: 'ENCRYPTION_SIGNATURE_VERIFICATION_FAILED',\n} as const;\n\nexport type EncryptionErrorCode = (typeof EncryptionErrorCode)[keyof typeof EncryptionErrorCode];\n\n// ============================================================================\n// Utility Types\n// ============================================================================\n\n/**\n * Input or output that can be either plaintext or encrypted.\n */\nexport type MaybeEncrypted<T> = T | EncryptedEnvelope;\n\n/**\n * Check if a value is an encrypted envelope.\n */\nexport function isEncryptedEnvelope(value: unknown): value is EncryptedEnvelope {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'type' in value &&\n (value as { type: unknown }).type === 'enc' &&\n 'payload' in value &&\n typeof (value as EncryptedEnvelope).payload === 'object' &&\n (value as EncryptedEnvelope).payload !== null &&\n 'kid' in (value as EncryptedEnvelope).payload &&\n 'nonceB64' in (value as EncryptedEnvelope).payload &&\n 'ciphertextB64' in (value as EncryptedEnvelope).payload\n );\n}\n\n/**\n * HKDF info strings for different key purposes.\n */\nexport const HkdfInfo = {\n /** Client -> Server encryption key */\n ClientToServer: 'enclavejs-c2s-enc',\n /** Server -> Client encryption key */\n ServerToClient: 'enclavejs-s2c-enc',\n} as const;\n\nexport type HkdfInfo = (typeof HkdfInfo)[keyof typeof HkdfInfo];\n\n/**\n * Nonce/IV size in bytes for AES-GCM.\n */\nexport const AES_GCM_NONCE_SIZE = 12;\n\n/**\n * Authentication tag size in bytes for AES-GCM.\n */\nexport const AES_GCM_TAG_SIZE = 16;\n\n/**\n * Key size in bytes for AES-256.\n */\nexport const AES_256_KEY_SIZE = 32;\n\n/**\n * Maximum number of messages with one key (2^32 for AES-GCM with random nonces).\n * We use a lower limit (2^30) to be safe.\n */\nexport const MAX_MESSAGES_PER_KEY = 1073741824n; // 2^30\n"]}