| Crates.io | codive-tunnel |
| lib.rs | codive-tunnel |
| version | 0.1.0 |
| created_at | 2026-01-05 11:04:30.988353+00 |
| updated_at | 2026-01-05 11:04:30.988353+00 |
| description | Shared types and cryptography for secure tunneling |
| homepage | |
| repository | https://github.com/toliaqat/codive |
| max_upload_size | |
| id | 2023636 |
| size | 90,052 |
A Rust library providing end-to-end encrypted tunneling protocol types, cryptographic primitives, and wire format encoding for secure Codive communication.
This crate is the shared foundation for the Codive tunneling system:
Zeroize trait implementation for memory safetyAdd to your Cargo.toml:
[dependencies]
codive-tunnel = { path = "../codive-tunnel" }
use codive_tunnel::{TunnelKey, TunnelCrypto};
// Generate a new encryption key
let key = TunnelKey::generate();
// Share via URL fragment (never sent to server)
let url = format!("https://tunnel.example.com#E2EKey={}", key.to_base64());
// Create crypto instance
let crypto = TunnelCrypto::new(&key);
// Encrypt data
let plaintext = b"Hello, World!";
let ciphertext = crypto.encrypt(plaintext)?;
// Decrypt data
let decrypted = crypto.decrypt(&ciphertext)?;
assert_eq!(plaintext.as_slice(), decrypted.as_slice());
use codive_tunnel::{ControlMessage, DataMessage, WireMessage, PROTOCOL_VERSION};
// Create a Hello message
let hello = ControlMessage::Hello {
version: PROTOCOL_VERSION,
requested_id: Some("my-tunnel".to_string()),
auth_token: Some("secret".to_string()),
};
// Encode for transmission
let encoded = WireMessage::encode_control(&hello)?;
// Decode received message
let decoded = WireMessage::decode_control(&encoded)?;
use codive_tunnel::DataMessage;
use std::collections::HashMap;
// Create an HTTP request
let request = DataMessage::HttpRequest {
request_id: "req-123".to_string(),
method: "GET".to_string(),
path: "/api/status".to_string(),
headers: HashMap::new(),
body: None,
};
// Serialize to JSON
let json = serde_json::to_vec(&request)?;
// Create a response
let response = DataMessage::HttpResponse {
request_id: "req-123".to_string(),
status: 200,
headers: [("Content-Type".to_string(), "application/json".to_string())]
.into_iter().collect(),
body: Some(base64::encode(b"{\"status\":\"ok\"}")),
streaming: false,
};
use codive_tunnel::{WireMessage, message_type};
// Encode encrypted data with routing header
let wire_msg = WireMessage::encode_encrypted_with_routing(
message_type::ENCRYPTED_RESPONSE,
"request-id-123",
encrypted_payload,
);
// Decode with routing header
let (msg_type, request_id, payload) =
WireMessage::decode_encrypted_with_routing(&wire_msg)?;
| Type | Description |
|---|---|
TunnelKey |
256-bit encryption key with Zeroize |
TunnelCrypto |
XChaCha20-Poly1305 encrypt/decrypt |
ControlMessage |
Protocol control messages (Hello, Welcome, etc.) |
DataMessage |
Data messages (HttpRequest, HttpResponse, etc.) |
WireMessage |
Binary wire format encoding/decoding |
| Message | Direction | Purpose |
|---|---|---|
Hello |
Agent → Relay | Initial handshake with auth |
Welcome |
Relay → Agent | Tunnel ID and URL assignment |
Ping |
Both | Keep-alive request |
Pong |
Both | Keep-alive response |
Close |
Both | Graceful disconnect |
Error |
Relay → Agent | Error notification |
ClientConnected |
Relay → Agent | Client connected to tunnel |
ClientDisconnected |
Relay → Agent | Client disconnected |
| Message | Direction | Purpose |
|---|---|---|
HttpRequest |
Relay → Agent | Incoming HTTP request |
HttpResponse |
Agent → Relay | HTTP response |
HttpResponseChunk |
Agent → Relay | Streaming response chunk |
RequestError |
Agent → Relay | Request processing error |
pub mod message_type {
pub const CONTROL: u8 = 0x00;
pub const ENCRYPTED_REQUEST: u8 = 0x01;
pub const ENCRYPTED_RESPONSE: u8 = 0x02;
pub const ENCRYPTED_CHUNK: u8 = 0x03;
}
Control Messages:
┌─────────┬─────────────────────┐
│ 0x00 │ JSON payload │
└─────────┴─────────────────────┘
Encrypted Messages:
┌─────────┬─────────────────────┐
│ type │ encrypted payload │
└─────────┴─────────────────────┘
Encrypted with Routing Header:
┌─────────┬─────────┬───────────────┬─────────────────────┐
│ type │ id_len │ request_id │ encrypted payload │
└─────────┴─────────┴───────────────┴─────────────────────┘
Ciphertext:
┌─────────────────────┬─────────────────────┬─────────────────────┐
│ nonce (24 bytes) │ ciphertext │ auth tag (16 bytes) │
└─────────────────────┴─────────────────────┴─────────────────────┘
#E2EKey=...)TunnelKey implements Zeroize and ZeroizeOnDropDebug shows [REDACTED])# Run all tests
cargo test -p codive-tunnel
# Run with output
cargo test -p codive-tunnel -- --nocapture
# Run specific test
cargo test -p codive-tunnel test_encrypt_decrypt
Test coverage: 62 tests
chacha20poly1305 - AEAD encryptionzeroize - Secure memory clearingrand - Cryptographic RNGbase64 - URL-safe encodingserde / serde_json - SerializationSee the codive-relay README for contribution guidelines.
ControlMessage or DataMessage in protocol.rsserde attributes if neededMIT License - see LICENSE file for details.