SchemaPin - Rust Implementation
Cryptographic schema integrity verification for AI tools - Rust implementation using ECDSA P-256.
Overview
SchemaPin provides a robust framework for verifying the integrity and authenticity of JSON schemas used by AI tools and services. This Rust implementation uses ECDSA P-256 cryptographic signatures to ensure that schemas haven't been tampered with and come from trusted sources.
This implementation is fully compatible with the Python, JavaScript, and Go implementations, using the same cryptographic standards:
- ECDSA with P-256 curve (secp256r1) for signatures
- SHA-256 for hashing
- PKCS#8 PEM format for key serialization
Features
- ECDSA P-256 Key Generation: Generate ECDSA P-256 key pairs for signing and verification
- Digital Signatures: Sign data using ECDSA with SHA-256
- Signature Verification: Verify signatures to ensure data integrity
- Key ID Calculation: Generate SHA-256 fingerprints for key identification
- PEM Format Support: Full support for PKCS#8 key formats
- Schema Canonicalization: Deterministic JSON serialization for consistent hashing
- Trust Bundles: Offline verification with pre-shared discovery documents
- Resolver Abstraction: Pluggable discovery via local files, trust bundles, or HTTP
- TOFU Key Pinning: Trust-on-first-use key pinning per tool+domain
- Revocation Checking: Combined simple list and standalone document revocation
- Cross-Language Compatibility: Compatible with Python, JavaScript, and Go implementations
Installation
Add this to your Cargo.toml:
[]
= "1.2.0"
To enable HTTP-based discovery (requires async runtime):
[]
= { = "1.2.0", = ["fetch"] }
Feature Flags
| Feature | Description |
|---|---|
default |
Everything except HTTP (all types, canonicalize, pinning, resolvers, offline verification) |
fetch |
Adds WellKnownResolver, AsyncSchemaResolver, fetch_well_known(), verify_schema(). Brings in reqwest, tokio, async-trait. |
Quick Start
Signing and Verifying Raw Data
use ;
// Generate a new key pair
let key_pair = generate_key_pair.unwrap;
// Sign some data
let data = b"Hello, World!";
let signature = sign_data.unwrap;
// Verify the signature
let is_valid = verify_signature.unwrap;
assert!;
// Calculate key ID
let key_id = calculate_key_id.unwrap;
println!;
Offline Schema Verification
use canonicalize_and_hash;
use ;
use build_well_known_response;
use KeyPinStore;
use verify_schema_offline;
use json;
// Developer: sign a schema
let key_pair = generate_key_pair.unwrap;
let schema = json!;
let hash = canonicalize_and_hash;
let signature = sign_data.unwrap;
// Build discovery response
let discovery = build_well_known_response;
// Client: verify the schema offline
let mut pin_store = new;
let result = verify_schema_offline;
assert!;
Using Trust Bundles
use TrustBundleResolver;
use verify_schema_with_resolver;
use KeyPinStore;
// Load a trust bundle from JSON
let bundle_json = read_to_string.unwrap;
let resolver = from_json.unwrap;
// Verify using the resolver
let mut pin_store = new;
let schema = json!;
let result = verify_schema_with_resolver;
Building and Testing
# Build (no HTTP dependencies)
# Build with HTTP support
# Run tests
# Run tests with all features
# Check code quality
# Format code
Security
This implementation uses:
- ECDSA with P-256 curve (secp256r1) for signatures
- SHA-256 for hashing and signature algorithms
- Secure random number generation via
OsRng - Constant-time operations where possible
The cryptographic operations are provided by the p256 crate, which implements the ECDSA algorithm according to industry standards.
Cross-Language Compatibility
This Rust implementation is designed to be fully compatible with other SchemaPin implementations:
- Identical signature format: Base64-encoded ECDSA signatures
- Compatible key formats: PKCS#8 PEM encoding
- Same fingerprint calculation: SHA-256 hash of DER-encoded public keys
- Interoperable signatures: Can verify signatures from Python/JavaScript/Go implementations
Error Handling
The crate provides two error types:
crypto::Error: Low-level cryptographic errors (Ecdsa,Pkcs8,Base64,Signature,InvalidKeyFormat)error::Error: Unified error type for the full verification workflow, wrappingcrypto::Errorand addingDiscovery,Revocation,Verification,Json,Io, and feature-gatedHttpvariants
Dependencies
p256: ECDSA P-256 cryptographic operationsrand: Secure random number generationsha2: SHA-256 hashingbase64: Base64 encoding/decodinghex: Hexadecimal encodingserde,serde_json: Serialization supportthiserror: Error derive macroschrono: Timestamp handlingreqwest(optional,fetch): HTTP clienttokio(optional,fetch): Async runtimeasync-trait(optional,fetch): Async trait support
License
MIT License - see the main project LICENSE file for details.