@7h3/protocol 0.5.0 → 0.5.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.
Files changed (294) hide show
  1. package/README.md +6 -1317
  2. package/agentAdapter.d.ts +26 -0
  3. package/auditLog.d.ts +54 -0
  4. package/capability.d.ts +67 -0
  5. package/cborCodec.d.ts +34 -0
  6. package/conformanceVectors.d.ts +20 -0
  7. package/encryption.d.ts +85 -0
  8. package/envelopeCbor.d.ts +33 -0
  9. package/frameworkAdapters.d.ts +72 -0
  10. package/gateway.d.ts +53 -0
  11. package/grpcBinding.d.ts +29 -0
  12. package/httpBinding.d.ts +50 -0
  13. package/index.d.ts +39 -0
  14. package/index.js +3662 -0
  15. package/keyInfra.d.ts +64 -0
  16. package/keyRegistry.d.ts +9 -0
  17. package/keyRotation.d.ts +20 -0
  18. package/mcpGateway.d.ts +37 -0
  19. package/mcpTransports.d.ts +62 -0
  20. package/mcpWrapper.d.ts +83 -0
  21. package/otel.d.ts +65 -0
  22. package/package.json +13 -121
  23. package/policyEnforcer.d.ts +50 -0
  24. package/policyTelemetryFeedback.d.ts +11 -0
  25. package/{src/protocol.d.ts → protocol.d.ts} +1 -2
  26. package/protocolAgent.d.ts +58 -0
  27. package/protocolBinary.d.ts +8 -0
  28. package/protocolCapabilities.d.ts +24 -0
  29. package/protocolReplay.d.ts +35 -0
  30. package/protocolTransport.d.ts +73 -0
  31. package/queueBinding.d.ts +43 -0
  32. package/rateLimiter.d.ts +18 -0
  33. package/{src/redisClient.ts → redisClient.d.ts} +25 -75
  34. package/replayStores.d.ts +98 -0
  35. package/revocation.d.ts +71 -0
  36. package/routePolicy.d.ts +30 -0
  37. package/runtimePolicy.d.ts +24 -0
  38. package/runtimePolicyManager.d.ts +15 -0
  39. package/runtimePolicyPresets.d.ts +11 -0
  40. package/signedResponse.d.ts +21 -0
  41. package/stream.d.ts +59 -0
  42. package/telemetry.d.ts +79 -0
  43. package/webhookBinding.d.ts +30 -0
  44. package/wsBinding.d.ts +52 -0
  45. package/.dockerignore +0 -19
  46. package/.github/dependabot.yml +0 -32
  47. package/.github/workflows/ci.yml +0 -31
  48. package/.github/workflows/publish.yml +0 -59
  49. package/.github/workflows/scorecard.yml +0 -37
  50. package/7h3.example.yaml +0 -125
  51. package/CHANGELOG.md +0 -152
  52. package/CONTRIBUTING.md +0 -82
  53. package/Dockerfile +0 -73
  54. package/GOVERNANCE.md +0 -62
  55. package/SECURITY.md +0 -70
  56. package/bench-results/replay-cache-full-1777891033256.json +0 -10
  57. package/bench-results/replay-cache-full-1777896317488.json +0 -10
  58. package/bench-results/replay-cache-full-1777900993184.json +0 -10
  59. package/bench-results/replay-cache-full-1777901019285.json +0 -10
  60. package/bench-results/replay-cache-quick-1777870170126.json +0 -10
  61. package/bench-results/signature-profiles-quick-1775875160079.json +0 -85
  62. package/bench-results/signature-profiles-quick-1775983539716.json +0 -85
  63. package/bench-results/signature-profiles-quick-1776237913190.json +0 -85
  64. package/bench-results/wire-codecs-full-1777891019803.json +0 -93
  65. package/bench-results/wire-codecs-full-1777896260964.json +0 -93
  66. package/bench-results/wire-codecs-full-1777901004247.json +0 -93
  67. package/bench-results/wire-codecs-quick-1775972879056.json +0 -93
  68. package/bench-results/wire-codecs-quick-1775983541111.json +0 -93
  69. package/bench-results/wire-codecs-quick-1776237914299.json +0 -93
  70. package/bench-results/wire-codecs-quick-1777841285236.json +0 -93
  71. package/bench-results/wire-codecs-quick-1777841321772.json +0 -93
  72. package/bench-results/wire-codecs-quick-1777841330408.json +0 -93
  73. package/bench-results/wire-codecs-quick-1777852886082.json +0 -93
  74. package/bench-results/wire-codecs-quick-1777852988773.json +0 -93
  75. package/bench-results/wire-codecs-quick-1777870188095.json +0 -93
  76. package/bench-results/wire-codecs-quick-1777870263918.json +0 -93
  77. package/bench-results/wire-codecs-quick-1777870455034.json +0 -93
  78. package/bench-results/wire-codecs-quick-1778816163081.json +0 -93
  79. package/bench-results/wire-codecs-quick-1778843936130.json +0 -93
  80. package/bin/7h3.ts +0 -406
  81. package/conformance/7h3_v0_1.json +0 -77
  82. package/conformance/7h3_v0_1_binary.json +0 -20
  83. package/conformance/aip_v0_1_binary.json +0 -20
  84. package/docker-compose.yaml +0 -77
  85. package/docs/ADOPTION_PLAN.md +0 -120
  86. package/docs/AGENTS.md +0 -77
  87. package/docs/AIP_RFC_v0.1.md +0 -97
  88. package/docs/AI_DECISION_CARD.md +0 -122
  89. package/docs/AI_RUNTIME_POLICY.json +0 -126
  90. package/docs/AI_RUNTIME_POLICY.yaml +0 -110
  91. package/docs/BACKPRESSURE_TUNING.md +0 -65
  92. package/docs/BENCHMARK_CLAIM_MATRIX.md +0 -42
  93. package/docs/BENCHMARK_REPORT_TEMPLATE.md +0 -169
  94. package/docs/BINARY_CODEC_BENCH.md +0 -23
  95. package/docs/CLEAN_CLONE_RUNBOOK.md +0 -36
  96. package/docs/CLOCK_SKEW_POLICY.md +0 -30
  97. package/docs/DISTRIBUTED_REPLAY.md +0 -142
  98. package/docs/FUZZ_CAMPAIGN.md +0 -121
  99. package/docs/GATEWAY.md +0 -195
  100. package/docs/KEY_MANAGEMENT_POLICY.md +0 -53
  101. package/docs/KEY_REVOCATION.md +0 -69
  102. package/docs/MCP_WRAPPER.md +0 -159
  103. package/docs/MIGRATION_GUIDE.md +0 -40
  104. package/docs/OPERATORS.md +0 -184
  105. package/docs/PERF_REGRESSION_POLICY.md +0 -34
  106. package/docs/PROJECT_EXAMINATION_2026-05-31.md +0 -219
  107. package/docs/RELEASE_BENCHMARK_REPORT_2026-05-15.md +0 -135
  108. package/docs/RELEASE_GATE.md +0 -25
  109. package/docs/RELEASE_NOTES_v0.1.0.md +0 -54
  110. package/docs/SECURITY_REVIEW_2026-06-05.md +0 -165
  111. package/docs/TELEMETRY.md +0 -41
  112. package/docs/THREAT_MODEL.md +0 -89
  113. package/docs/VERSIONING_POLICY.md +0 -30
  114. package/docs/assets/banner-github.png +0 -0
  115. package/docs/assets/banner.png +0 -0
  116. package/docs/assets/banner.svg +0 -123
  117. package/eslint.config.js +0 -15
  118. package/fuzz/ts/harness-decode.ts +0 -136
  119. package/fuzz/ts/harness-verify.ts +0 -121
  120. package/fuzz/ts/run.ts +0 -35
  121. package/mcp-server/README.md +0 -38
  122. package/mcp-server/package-lock.json +0 -1187
  123. package/mcp-server/package.json +0 -35
  124. package/mcp-server/src/index.ts +0 -236
  125. package/mcp-server/tsconfig.json +0 -14
  126. package/scripts/aip-framework-quickstart.ts +0 -110
  127. package/scripts/aip-mcp-gateway.ts +0 -38
  128. package/scripts/aip-mcp-wrap-demo.ts +0 -72
  129. package/scripts/aip-quickstart.ts +0 -60
  130. package/scripts/bench-diff.ts +0 -118
  131. package/scripts/bench-protocol-e2e.ts +0 -937
  132. package/scripts/bench-protocol-openloop.ts +0 -1397
  133. package/scripts/bench-replay-cache.ts +0 -76
  134. package/scripts/bench-signature-profiles.ts +0 -180
  135. package/scripts/bench-wire-codecs.ts +0 -161
  136. package/scripts/build-binary-conformance.ts +0 -36
  137. package/scripts/build-release-dashboard.ts +0 -175
  138. package/scripts/canary-rollout.ts +0 -38
  139. package/scripts/mcpGatewayCli.test.ts +0 -116
  140. package/scripts/prepare-aip-package.ts +0 -88
  141. package/scripts/regen-conformance-sigs.ts +0 -18
  142. package/scripts/release-gate.ts +0 -19
  143. package/scripts/validate-runtime-policy.ts +0 -18
  144. package/sdk/browser/index.test.ts +0 -162
  145. package/sdk/browser/index.ts +0 -257
  146. package/sdk/browser/package.json +0 -13
  147. package/sdk/go/cbor.go +0 -551
  148. package/sdk/go/cbor_test.go +0 -232
  149. package/sdk/go/encryption.go +0 -280
  150. package/sdk/go/encryption_test.go +0 -318
  151. package/sdk/go/go.mod +0 -7
  152. package/sdk/go/go.sum +0 -4
  153. package/sdk/go/http.go +0 -135
  154. package/sdk/go/protocol.go +0 -324
  155. package/sdk/go/protocol_test.go +0 -334
  156. package/sdk/go/replay.go +0 -121
  157. package/sdk/go/replay_test.go +0 -149
  158. package/sdk/go/webhook.go +0 -136
  159. package/sdk/pq/package-lock.json +0 -1358
  160. package/sdk/pq/package.json +0 -42
  161. package/sdk/pq/src/index.test.ts +0 -143
  162. package/sdk/pq/src/index.ts +0 -166
  163. package/sdk/pq/tsconfig.json +0 -14
  164. package/sdk/pq/vitest.config.ts +0 -7
  165. package/sdk/python/README.md +0 -18
  166. package/sdk/python/protocol_7h3/__init__.py +0 -46
  167. package/sdk/python/protocol_7h3/encryption.py +0 -252
  168. package/sdk/python/protocol_7h3/http.py +0 -212
  169. package/sdk/python/protocol_7h3/keys.py +0 -149
  170. package/sdk/python/protocol_7h3/pq.py +0 -244
  171. package/sdk/python/protocol_7h3/protocol.py +0 -525
  172. package/sdk/python/protocol_7h3/queue.py +0 -118
  173. package/sdk/python/protocol_7h3/replay.py +0 -98
  174. package/sdk/python/protocol_7h3/webhook.py +0 -116
  175. package/sdk/python/pyproject.toml +0 -40
  176. package/sdk/python/tests/test_conformance.py +0 -110
  177. package/sdk/python/tests/test_encryption.py +0 -206
  178. package/sdk/python/tests/test_http.py +0 -305
  179. package/sdk/python/tests/test_keys.py +0 -417
  180. package/sdk/python/tests/test_queue.py +0 -120
  181. package/sdk/python/tests/test_webhook.py +0 -345
  182. package/sdk/rust/Cargo.lock +0 -371
  183. package/sdk/rust/Cargo.toml +0 -25
  184. package/sdk/rust/README.md +0 -31
  185. package/sdk/rust/fuzz/Cargo.toml +0 -29
  186. package/sdk/rust/fuzz/fuzz_targets/fuzz_canonicalize.rs +0 -46
  187. package/sdk/rust/fuzz/fuzz_targets/fuzz_decode.rs +0 -11
  188. package/sdk/rust/src/bin/aip_mcp_gateway.rs +0 -59
  189. package/sdk/rust/src/http.rs +0 -145
  190. package/sdk/rust/src/keys.rs +0 -161
  191. package/sdk/rust/src/lib.rs +0 -688
  192. package/sdk/rust/src/queue.rs +0 -79
  193. package/sdk/rust/src/webhook.rs +0 -86
  194. package/sdk/rust/tests/conformance.rs +0 -148
  195. package/sdk/rust/tests/gateway.rs +0 -130
  196. package/sdk/rust/tests/http_webhook_queue.rs +0 -201
  197. package/sdk/rust/tests/keys.rs +0 -189
  198. package/sdk/threshold/index.d.ts +0 -68
  199. package/sdk/threshold/index.d.ts.map +0 -1
  200. package/sdk/threshold/index.js +0 -254
  201. package/sdk/threshold/package-lock.json +0 -1361
  202. package/sdk/threshold/package.json +0 -39
  203. package/sdk/threshold/src/index.d.ts +0 -68
  204. package/sdk/threshold/src/index.d.ts.map +0 -1
  205. package/sdk/threshold/src/index.js +0 -254
  206. package/sdk/threshold/src/index.test.ts +0 -238
  207. package/sdk/threshold/src/index.ts +0 -355
  208. package/sdk/threshold/tsconfig.json +0 -19
  209. package/sdk/threshold/vitest.config.ts +0 -12
  210. package/src/agentAdapter.test.ts +0 -48
  211. package/src/agentAdapter.ts +0 -56
  212. package/src/auditLog.test.ts +0 -145
  213. package/src/auditLog.ts +0 -147
  214. package/src/capability.test.ts +0 -504
  215. package/src/capability.ts +0 -380
  216. package/src/cborCodec.test.ts +0 -263
  217. package/src/cborCodec.ts +0 -339
  218. package/src/conformance.test.ts +0 -136
  219. package/src/conformanceVectors.ts +0 -99
  220. package/src/encryption.test.ts +0 -206
  221. package/src/encryption.ts +0 -245
  222. package/src/envelopeCbor.ts +0 -140
  223. package/src/frameworkAdapters.test.ts +0 -290
  224. package/src/frameworkAdapters.ts +0 -261
  225. package/src/gateway.test.ts +0 -343
  226. package/src/gateway.ts +0 -246
  227. package/src/grpcBinding.test.ts +0 -211
  228. package/src/grpcBinding.ts +0 -103
  229. package/src/httpBinding.test.ts +0 -376
  230. package/src/httpBinding.ts +0 -189
  231. package/src/index.ts +0 -39
  232. package/src/keyInfra.test.ts +0 -278
  233. package/src/keyInfra.ts +0 -228
  234. package/src/keyRegistry.ts +0 -59
  235. package/src/keyRotation.test.ts +0 -78
  236. package/src/keyRotation.ts +0 -72
  237. package/src/mcpGateway.test.ts +0 -129
  238. package/src/mcpGateway.ts +0 -250
  239. package/src/mcpTransports.test.ts +0 -92
  240. package/src/mcpTransports.ts +0 -169
  241. package/src/mcpWrapper.test.ts +0 -179
  242. package/src/mcpWrapper.ts +0 -206
  243. package/src/otel.ts +0 -136
  244. package/src/policyEnforcer.test.ts +0 -99
  245. package/src/policyEnforcer.ts +0 -169
  246. package/src/policyTelemetryFeedback.test.ts +0 -25
  247. package/src/policyTelemetryFeedback.ts +0 -38
  248. package/src/protocol.bench.ts +0 -37
  249. package/src/protocol.d.ts.map +0 -1
  250. package/src/protocol.js +0 -294
  251. package/src/protocol.test.ts +0 -155
  252. package/src/protocol.ts +0 -414
  253. package/src/protocolAgent.test.ts +0 -105
  254. package/src/protocolAgent.ts +0 -169
  255. package/src/protocolBinary.test.ts +0 -165
  256. package/src/protocolBinary.ts +0 -312
  257. package/src/protocolCapabilities.ts +0 -70
  258. package/src/protocolFuzz.advanced.test.ts +0 -235
  259. package/src/protocolFuzz.test.ts +0 -111
  260. package/src/protocolNegative.test.ts +0 -97
  261. package/src/protocolReplay.test.ts +0 -71
  262. package/src/protocolReplay.ts +0 -194
  263. package/src/protocolTransport.test.ts +0 -556
  264. package/src/protocolTransport.ts +0 -483
  265. package/src/queueBinding.test.ts +0 -130
  266. package/src/queueBinding.ts +0 -102
  267. package/src/rateLimiter.test.ts +0 -96
  268. package/src/rateLimiter.ts +0 -46
  269. package/src/redisIntegration.test.ts +0 -134
  270. package/src/replayStores.test.ts +0 -273
  271. package/src/replayStores.ts +0 -215
  272. package/src/revocation.test.ts +0 -98
  273. package/src/revocation.ts +0 -0
  274. package/src/routePolicy.test.ts +0 -87
  275. package/src/routePolicy.ts +0 -72
  276. package/src/runtimePolicy.test.ts +0 -49
  277. package/src/runtimePolicy.ts +0 -81
  278. package/src/runtimePolicyManager.test.ts +0 -29
  279. package/src/runtimePolicyManager.ts +0 -50
  280. package/src/runtimePolicyPresets.ts +0 -43
  281. package/src/signedResponse.test.ts +0 -111
  282. package/src/signedResponse.ts +0 -83
  283. package/src/stream.test.ts +0 -254
  284. package/src/stream.ts +0 -417
  285. package/src/telemetry.test.ts +0 -251
  286. package/src/telemetry.ts +0 -299
  287. package/src/webhookBinding.test.ts +0 -144
  288. package/src/webhookBinding.ts +0 -115
  289. package/src/wsBinding.test.ts +0 -221
  290. package/src/wsBinding.ts +0 -200
  291. package/tsconfig.json +0 -15
  292. package/tsconfig.lib.json +0 -23
  293. package/vite.lib.config.ts +0 -16
  294. package/vitest.config.ts +0 -11
@@ -1,232 +0,0 @@
1
- package protocol7h3
2
-
3
- import (
4
- "bytes"
5
- "encoding/json"
6
- "testing"
7
- "time"
8
- )
9
-
10
- func TestEncodeDecodeEnvelopeCBOR_RoundTrip(t *testing.T) {
11
- env := ProtocolEnvelope{
12
- Header: ProtocolHeader{
13
- Version: WireVersion,
14
- MessageID: "msg-1234567890-abc12345",
15
- TimestampMs: time.Now().UnixMilli(),
16
- TTLMs: 60000,
17
- Sender: "agent:alice@example.com",
18
- Nonce: "abcdefghij",
19
- },
20
- Body: ProtocolBody{
21
- Intent: "TASK",
22
- Content: "Hello, agent!",
23
- },
24
- }
25
-
26
- encoded, err := EncodeEnvelopeCBOR(env)
27
- if err != nil {
28
- t.Fatalf("EncodeEnvelopeCBOR error: %v", err)
29
- }
30
-
31
- decoded, err := DecodeEnvelopeCBOR(encoded)
32
- if err != nil {
33
- t.Fatalf("DecodeEnvelopeCBOR error: %v", err)
34
- }
35
-
36
- if decoded.Header.Version != env.Header.Version {
37
- t.Errorf("version mismatch: got %q want %q", decoded.Header.Version, env.Header.Version)
38
- }
39
- if decoded.Header.MessageID != env.Header.MessageID {
40
- t.Errorf("messageId mismatch: got %q want %q", decoded.Header.MessageID, env.Header.MessageID)
41
- }
42
- if decoded.Header.TimestampMs != env.Header.TimestampMs {
43
- t.Errorf("timestampMs mismatch: got %d want %d", decoded.Header.TimestampMs, env.Header.TimestampMs)
44
- }
45
- if decoded.Header.TTLMs != env.Header.TTLMs {
46
- t.Errorf("ttlMs mismatch: got %d want %d", decoded.Header.TTLMs, env.Header.TTLMs)
47
- }
48
- if decoded.Header.Sender != env.Header.Sender {
49
- t.Errorf("sender mismatch: got %q want %q", decoded.Header.Sender, env.Header.Sender)
50
- }
51
- if decoded.Header.Nonce != env.Header.Nonce {
52
- t.Errorf("nonce mismatch: got %q want %q", decoded.Header.Nonce, env.Header.Nonce)
53
- }
54
- if decoded.Body.Intent != env.Body.Intent {
55
- t.Errorf("intent mismatch: got %q want %q", decoded.Body.Intent, env.Body.Intent)
56
- }
57
- if decoded.Body.Content != env.Body.Content {
58
- t.Errorf("content mismatch: got %q want %q", decoded.Body.Content, env.Body.Content)
59
- }
60
- if decoded.Signature != nil {
61
- t.Errorf("expected nil signature, got %+v", decoded.Signature)
62
- }
63
- }
64
-
65
- func TestEncodeDecodeEnvelopeCBOR_AllOptionalFields(t *testing.T) {
66
- env := ProtocolEnvelope{
67
- Header: ProtocolHeader{
68
- Version: WireVersion,
69
- MessageID: "msg-full",
70
- TimestampMs: 1700000000000,
71
- TTLMs: 30000,
72
- Sender: "agent:alice",
73
- Recipient: "agent:bob",
74
- Nonce: "nonce-xyz",
75
- },
76
- Body: ProtocolBody{
77
- Intent: "TASK",
78
- Content: "do something",
79
- Capability: "file.read",
80
- CorrelationID: "corr-abc",
81
- },
82
- Signature: &ProtocolSignature{
83
- Alg: "HS256",
84
- KeyID: "my-key-id",
85
- Value: "base64urlencodedvalue",
86
- },
87
- }
88
-
89
- encoded, err := EncodeEnvelopeCBOR(env)
90
- if err != nil {
91
- t.Fatalf("EncodeEnvelopeCBOR error: %v", err)
92
- }
93
-
94
- decoded, err := DecodeEnvelopeCBOR(encoded)
95
- if err != nil {
96
- t.Fatalf("DecodeEnvelopeCBOR error: %v", err)
97
- }
98
-
99
- if decoded.Header.Recipient != env.Header.Recipient {
100
- t.Errorf("recipient: got %q want %q", decoded.Header.Recipient, env.Header.Recipient)
101
- }
102
- if decoded.Body.Capability != env.Body.Capability {
103
- t.Errorf("capability: got %q want %q", decoded.Body.Capability, env.Body.Capability)
104
- }
105
- if decoded.Body.CorrelationID != env.Body.CorrelationID {
106
- t.Errorf("correlationId: got %q want %q", decoded.Body.CorrelationID, env.Body.CorrelationID)
107
- }
108
- if decoded.Signature == nil {
109
- t.Fatal("expected signature, got nil")
110
- }
111
- if decoded.Signature.Alg != env.Signature.Alg {
112
- t.Errorf("sig.alg: got %q want %q", decoded.Signature.Alg, env.Signature.Alg)
113
- }
114
- if decoded.Signature.KeyID != env.Signature.KeyID {
115
- t.Errorf("sig.keyId: got %q want %q", decoded.Signature.KeyID, env.Signature.KeyID)
116
- }
117
- if decoded.Signature.Value != env.Signature.Value {
118
- t.Errorf("sig.value: got %q want %q", decoded.Signature.Value, env.Signature.Value)
119
- }
120
- }
121
-
122
- func TestEncodeDecodeEnvelopeCBOR_Deterministic(t *testing.T) {
123
- env := ProtocolEnvelope{
124
- Header: ProtocolHeader{
125
- Version: WireVersion,
126
- MessageID: "msg-det",
127
- TimestampMs: 1700000000000,
128
- TTLMs: 60000,
129
- Sender: "agent:sender",
130
- Nonce: "nonce-det",
131
- },
132
- Body: ProtocolBody{
133
- Intent: "PING",
134
- Content: "ping",
135
- },
136
- }
137
-
138
- enc1, err := EncodeEnvelopeCBOR(env)
139
- if err != nil {
140
- t.Fatalf("first encode error: %v", err)
141
- }
142
- enc2, err := EncodeEnvelopeCBOR(env)
143
- if err != nil {
144
- t.Fatalf("second encode error: %v", err)
145
- }
146
-
147
- if !bytes.Equal(enc1, enc2) {
148
- t.Error("encoding same envelope twice gave different bytes (not deterministic)")
149
- }
150
- }
151
-
152
- func TestEncodeEnvelopeCBOR_SmallerThanJSON(t *testing.T) {
153
- env := ProtocolEnvelope{
154
- Header: ProtocolHeader{
155
- Version: WireVersion,
156
- MessageID: "msg-size-test-1234567890",
157
- TimestampMs: 1700000000000,
158
- TTLMs: 60000,
159
- Sender: "agent:alice@example.com",
160
- Recipient: "agent:bob@example.com",
161
- Nonce: "nonce-abcdefgh",
162
- },
163
- Body: ProtocolBody{
164
- Intent: "TASK",
165
- Content: "Hello, this is a test message content",
166
- Capability: "file.read",
167
- CorrelationID: "corr-xyz-1234",
168
- },
169
- Signature: &ProtocolSignature{
170
- Alg: "HS256",
171
- KeyID: "my-key-id-v1",
172
- Value: "base64urlencodedvalueofthesignature",
173
- },
174
- }
175
-
176
- cborBytes, err := EncodeEnvelopeCBOR(env)
177
- if err != nil {
178
- t.Fatalf("EncodeEnvelopeCBOR error: %v", err)
179
- }
180
- jsonBytes, err := json.Marshal(env)
181
- if err != nil {
182
- t.Fatalf("json.Marshal error: %v", err)
183
- }
184
-
185
- t.Logf("CBOR size: %d bytes, JSON size: %d bytes, ratio: %.2f",
186
- len(cborBytes), len(jsonBytes), float64(len(cborBytes))/float64(len(jsonBytes)))
187
-
188
- if len(cborBytes) >= len(jsonBytes) {
189
- t.Errorf("expected CBOR (%d bytes) to be smaller than JSON (%d bytes)", len(cborBytes), len(jsonBytes))
190
- }
191
- }
192
-
193
- func TestEncodeDecodeEnvelopeCBOR_OptionalFieldsAbsent(t *testing.T) {
194
- // Minimal envelope - no optional fields
195
- env := ProtocolEnvelope{
196
- Header: ProtocolHeader{
197
- Version: WireVersion,
198
- MessageID: "msg-min",
199
- TimestampMs: 1700000000000,
200
- TTLMs: 30000,
201
- Sender: "agent:sender",
202
- Nonce: "nonce123",
203
- },
204
- Body: ProtocolBody{
205
- Intent: "PING",
206
- Content: "ping",
207
- },
208
- }
209
-
210
- encoded, err := EncodeEnvelopeCBOR(env)
211
- if err != nil {
212
- t.Fatalf("EncodeEnvelopeCBOR error: %v", err)
213
- }
214
-
215
- decoded, err := DecodeEnvelopeCBOR(encoded)
216
- if err != nil {
217
- t.Fatalf("DecodeEnvelopeCBOR error: %v", err)
218
- }
219
-
220
- if decoded.Header.Recipient != "" {
221
- t.Errorf("expected empty recipient, got %q", decoded.Header.Recipient)
222
- }
223
- if decoded.Body.Capability != "" {
224
- t.Errorf("expected empty capability, got %q", decoded.Body.Capability)
225
- }
226
- if decoded.Body.CorrelationID != "" {
227
- t.Errorf("expected empty correlationId, got %q", decoded.Body.CorrelationID)
228
- }
229
- if decoded.Signature != nil {
230
- t.Errorf("expected nil signature, got %+v", decoded.Signature)
231
- }
232
- }
@@ -1,280 +0,0 @@
1
- // Package protocol7h3 — E2E Encryption using X25519 + ChaCha20-Poly1305.
2
- //
3
- // Uses only stdlib crypto/ecdh for X25519 and golang.org/x/crypto for ChaCha20-Poly1305
4
- // and HKDF (no other external dependencies).
5
- //
6
- // EncryptedEnvelope = SignedEnvelope where body.Content is a base64url-encoded
7
- // EncryptedPayload JSON, body.Intent = "ENCRYPTED",
8
- // body.Capability = "x25519-chacha20poly1305"
9
- package protocol7h3
10
-
11
- import (
12
- "crypto/ecdh"
13
- "crypto/rand"
14
- "encoding/base64"
15
- "encoding/json"
16
- "errors"
17
- "fmt"
18
- "io"
19
-
20
- "golang.org/x/crypto/chacha20poly1305"
21
- "golang.org/x/crypto/hkdf"
22
-
23
- "crypto/sha256"
24
- )
25
-
26
- // ---------------------------------------------------------------------------
27
- // Types
28
- // ---------------------------------------------------------------------------
29
-
30
- // EncryptedPayload holds the fields serialized into body.Content for encrypted envelopes.
31
- type EncryptedPayload struct {
32
- EphemeralPublic string `json:"ephemeralPublic"`
33
- Nonce string `json:"nonce"`
34
- Ciphertext string `json:"ciphertext"`
35
- Tag string `json:"tag"`
36
- }
37
-
38
- // ---------------------------------------------------------------------------
39
- // Key helpers
40
- // ---------------------------------------------------------------------------
41
-
42
- func encodeBase64UrlRaw(b []byte) string {
43
- return base64.RawURLEncoding.EncodeToString(b)
44
- }
45
-
46
- func decodeBase64UrlRaw(s string) ([]byte, error) {
47
- return base64.RawURLEncoding.DecodeString(s)
48
- }
49
-
50
- // GenerateX25519KeyPair generates a fresh X25519 keypair.
51
- // Returns publicKey and privateKey as raw 32-byte values encoded as base64url (no padding).
52
- func GenerateX25519KeyPair() (publicKey, privateKey string, err error) {
53
- curve := ecdh.X25519()
54
- privKey, err := curve.GenerateKey(rand.Reader)
55
- if err != nil {
56
- return "", "", fmt.Errorf("protocol7h3: GenerateX25519KeyPair: %w", err)
57
- }
58
- pubKey := privKey.PublicKey()
59
-
60
- // Raw bytes: private key = privKey.Bytes() (32 bytes), public key = pubKey.Bytes() (32 bytes)
61
- return encodeBase64UrlRaw(pubKey.Bytes()), encodeBase64UrlRaw(privKey.Bytes()), nil
62
- }
63
-
64
- // importX25519Private imports a raw 32-byte X25519 private key (base64url).
65
- func importX25519Private(privRaw32Base64Url string) (*ecdh.PrivateKey, error) {
66
- rawBytes, err := decodeBase64UrlRaw(privRaw32Base64Url)
67
- if err != nil {
68
- return nil, fmt.Errorf("protocol7h3: decode X25519 private key: %w", err)
69
- }
70
- curve := ecdh.X25519()
71
- return curve.NewPrivateKey(rawBytes)
72
- }
73
-
74
- // importX25519Public imports a raw 32-byte X25519 public key (base64url).
75
- func importX25519Public(pubRaw32Base64Url string) (*ecdh.PublicKey, error) {
76
- rawBytes, err := decodeBase64UrlRaw(pubRaw32Base64Url)
77
- if err != nil {
78
- return nil, fmt.Errorf("protocol7h3: decode X25519 public key: %w", err)
79
- }
80
- curve := ecdh.X25519()
81
- return curve.NewPublicKey(rawBytes)
82
- }
83
-
84
- // ---------------------------------------------------------------------------
85
- // HKDF key derivation
86
- // ---------------------------------------------------------------------------
87
-
88
- // deriveEncryptionKey performs X25519 DH + HKDF-SHA256 to derive a 32-byte key.
89
- // nonce is the base64url-encoded 12-byte ChaCha nonce used as HKDF salt.
90
- func deriveEncryptionKey(privateKeyB64, peerPublicKeyB64, nonceB64 string) ([]byte, error) {
91
- privKey, err := importX25519Private(privateKeyB64)
92
- if err != nil {
93
- return nil, err
94
- }
95
- pubKey, err := importX25519Public(peerPublicKeyB64)
96
- if err != nil {
97
- return nil, err
98
- }
99
-
100
- sharedSecret, err := privKey.ECDH(pubKey)
101
- if err != nil {
102
- return nil, fmt.Errorf("protocol7h3: X25519 ECDH: %w", err)
103
- }
104
-
105
- nonceBytes, err := decodeBase64UrlRaw(nonceB64)
106
- if err != nil {
107
- return nil, fmt.Errorf("protocol7h3: decode nonce: %w", err)
108
- }
109
-
110
- kdfReader := hkdf.New(sha256.New, sharedSecret, nonceBytes, []byte("7h3-enc/1"))
111
- key := make([]byte, 32)
112
- if _, err = io.ReadFull(kdfReader, key); err != nil {
113
- return nil, fmt.Errorf("protocol7h3: HKDF: %w", err)
114
- }
115
- return key, nil
116
- }
117
-
118
- // ---------------------------------------------------------------------------
119
- // Body encryption / decryption
120
- // ---------------------------------------------------------------------------
121
-
122
- // encryptBody encrypts a ProtocolBody for the given recipient X25519 public key.
123
- // Returns the EncryptedPayload struct.
124
- func encryptBody(body ProtocolBody, recipientPublicB64 string) (EncryptedPayload, error) {
125
- // Generate ephemeral X25519 keypair for forward secrecy
126
- ephemeralPub, ephemeralPriv, err := GenerateX25519KeyPair()
127
- if err != nil {
128
- return EncryptedPayload{}, err
129
- }
130
-
131
- // Random 12-byte ChaCha nonce (also used as HKDF salt)
132
- nonce12 := make([]byte, chacha20poly1305.NonceSize) // 12 bytes
133
- if _, err = rand.Read(nonce12); err != nil {
134
- return EncryptedPayload{}, fmt.Errorf("protocol7h3: generate nonce: %w", err)
135
- }
136
- nonceB64 := encodeBase64UrlRaw(nonce12)
137
-
138
- // Derive encryption key
139
- key, err := deriveEncryptionKey(ephemeralPriv, recipientPublicB64, nonceB64)
140
- if err != nil {
141
- return EncryptedPayload{}, err
142
- }
143
-
144
- // Encrypt body as JSON
145
- plaintext, err := json.Marshal(body)
146
- if err != nil {
147
- return EncryptedPayload{}, fmt.Errorf("protocol7h3: marshal body: %w", err)
148
- }
149
-
150
- // ChaCha20-Poly1305 (12-byte nonce variant, NOT XChaCha which uses 24 bytes)
151
- aead, err := chacha20poly1305.New(key)
152
- if err != nil {
153
- return EncryptedPayload{}, fmt.Errorf("protocol7h3: create chacha20poly1305: %w", err)
154
- }
155
-
156
- // Seal appends ciphertext‖tag (tag is last 16 bytes)
157
- ctWithTag := aead.Seal(nil, nonce12, plaintext, nil)
158
- ciphertext := ctWithTag[:len(ctWithTag)-16]
159
- tag := ctWithTag[len(ctWithTag)-16:]
160
-
161
- return EncryptedPayload{
162
- EphemeralPublic: ephemeralPub,
163
- Nonce: nonceB64,
164
- Ciphertext: encodeBase64UrlRaw(ciphertext),
165
- Tag: encodeBase64UrlRaw(tag),
166
- }, nil
167
- }
168
-
169
- // decryptBody decrypts an EncryptedPayload back to a ProtocolBody.
170
- // Returns an error if AEAD tag verification fails.
171
- func decryptBody(encryptedContentB64, recipientPrivateB64 string) (ProtocolBody, error) {
172
- payloadJSON, err := decodeBase64UrlRaw(encryptedContentB64)
173
- if err != nil {
174
- return ProtocolBody{}, fmt.Errorf("protocol7h3: decode encrypted content: %w", err)
175
- }
176
-
177
- var payload EncryptedPayload
178
- if err = json.Unmarshal(payloadJSON, &payload); err != nil {
179
- return ProtocolBody{}, fmt.Errorf("protocol7h3: unmarshal encrypted payload: %w", err)
180
- }
181
-
182
- key, err := deriveEncryptionKey(recipientPrivateB64, payload.EphemeralPublic, payload.Nonce)
183
- if err != nil {
184
- return ProtocolBody{}, err
185
- }
186
-
187
- nonce12, err := decodeBase64UrlRaw(payload.Nonce)
188
- if err != nil {
189
- return ProtocolBody{}, fmt.Errorf("protocol7h3: decode nonce: %w", err)
190
- }
191
-
192
- ciphertext, err := decodeBase64UrlRaw(payload.Ciphertext)
193
- if err != nil {
194
- return ProtocolBody{}, fmt.Errorf("protocol7h3: decode ciphertext: %w", err)
195
- }
196
-
197
- tag, err := decodeBase64UrlRaw(payload.Tag)
198
- if err != nil {
199
- return ProtocolBody{}, fmt.Errorf("protocol7h3: decode tag: %w", err)
200
- }
201
-
202
- aead, err := chacha20poly1305.New(key)
203
- if err != nil {
204
- return ProtocolBody{}, fmt.Errorf("protocol7h3: create chacha20poly1305: %w", err)
205
- }
206
-
207
- // Re-concatenate ciphertext‖tag for decryption (Open expects this format)
208
- ctWithTag := append(ciphertext, tag...)
209
- plaintext, err := aead.Open(nil, nonce12, ctWithTag, nil)
210
- if err != nil {
211
- return ProtocolBody{}, fmt.Errorf("protocol7h3: decrypt: %w", err)
212
- }
213
-
214
- var body ProtocolBody
215
- if err = json.Unmarshal(plaintext, &body); err != nil {
216
- return ProtocolBody{}, fmt.Errorf("protocol7h3: unmarshal decrypted body: %w", err)
217
- }
218
- return body, nil
219
- }
220
-
221
- // ---------------------------------------------------------------------------
222
- // Envelope-level seal / open
223
- // ---------------------------------------------------------------------------
224
-
225
- // SealEnvelope encrypts the envelope body and signs the result with Ed25519.
226
- //
227
- // The original body is encrypted; the envelope body is replaced with:
228
- //
229
- // { Intent: "ENCRYPTED", Content: <encrypted-payload>, Capability: "x25519-chacha20poly1305" }
230
- //
231
- // The modified envelope is then signed with Ed25519.
232
- func SealEnvelope(env ProtocolEnvelope, recipientX25519Public, senderEd25519Private string) (ProtocolEnvelope, error) {
233
- encPayload, err := encryptBody(env.Body, recipientX25519Public)
234
- if err != nil {
235
- return env, err
236
- }
237
-
238
- payloadJSON, err := json.Marshal(encPayload)
239
- if err != nil {
240
- return env, fmt.Errorf("protocol7h3: marshal encrypted payload: %w", err)
241
- }
242
- encryptedContent := encodeBase64UrlRaw(payloadJSON)
243
-
244
- encBody := ProtocolBody{
245
- Intent: "ENCRYPTED",
246
- Content: encryptedContent,
247
- Capability: "x25519-chacha20poly1305",
248
- CorrelationID: env.Body.CorrelationID,
249
- }
250
-
251
- encEnv := ProtocolEnvelope{
252
- Header: env.Header,
253
- Body: encBody,
254
- }
255
-
256
- return SignEnvelopeEd25519(encEnv, senderEd25519Private)
257
- }
258
-
259
- // OpenEnvelope verifies the Ed25519 signature and decrypts the envelope body.
260
- //
261
- // Signature is verified FIRST — decryption only proceeds if valid.
262
- // Returns the signed envelope (with encrypted body) and the decrypted original body.
263
- func OpenEnvelope(env ProtocolEnvelope, recipientX25519Private, senderEd25519Public string) (ProtocolEnvelope, ProtocolBody, error) {
264
- // 1. Verify Ed25519 signature FIRST
265
- valid, err := VerifyEnvelopeEd25519(env, senderEd25519Public)
266
- if err != nil {
267
- return env, ProtocolBody{}, fmt.Errorf("protocol7h3: signature verification error: %w", err)
268
- }
269
- if !valid {
270
- return env, ProtocolBody{}, errors.New("protocol7h3: Ed25519 signature verification failed")
271
- }
272
-
273
- // 2. Decrypt body
274
- body, err := decryptBody(env.Body.Content, recipientX25519Private)
275
- if err != nil {
276
- return env, ProtocolBody{}, err
277
- }
278
-
279
- return env, body, nil
280
- }