shamir_share
A secure and efficient Rust library for Shamir's Secret Sharing, built with a security-first, constant-time design to prevent side-channel attacks. Split sensitive data into multiple shares where only a threshold number is needed for reconstruction.
Overview
shamir_share provides a robust implementation of Shamir's Secret Sharing scheme as a Rust library. It allows you to split sensitive data (such as database files) into multiple shares, where the original data can only be reconstructed when a predetermined number of shares are combined.
Key Features
- Flexible Share Generation:
- Split data into customizable number of shares (n)
- Define threshold (k) for minimum shares needed for reconstruction
- Lazy iterator-based share generation with
Dealerfor memory efficiency - Support for up to 255 shares (GF(256) field limitation)
- Strong Security Features:
- Secure random number generation using
ChaCha20Rng - Constant-time cryptographic operations to prevent side-channel attacks
- SHA-256 integrity verification of reconstructed secrets
- Magic number and version checks for share file format
- Comprehensive fuzzing for robustness testing
- Secure random number generation using
- Performance & Usability:
- Zero-copy operations for efficient memory usage
- Streaming support for large files
- Builder pattern for ergonomic configuration
- Cross-platform compatibility
- Comprehensive error handling
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Quick Start
Basic Usage
use ;
// Create a scheme with 5 shares and threshold 3
let mut scheme = builder.build.unwrap;
// Split a secret
let secret = b"my secret data";
let shares = scheme.split.unwrap;
// Store shares
let temp_dir = tempdir.unwrap;
let mut store = new.unwrap;
for share in &shares
// Reconstruct from 3 shares
let loaded_shares = vec!;
let reconstructed = reconstruct.unwrap;
assert_eq!;
Lazy Share Generation with Dealer
use ShamirShare;
let mut scheme = builder.build.unwrap;
let secret = b"my secret data";
// Generate only the shares you need
let shares: = scheme.dealer.take.collect;
// Or use iterator methods for advanced filtering
let even_shares: = scheme.dealer
.filter
.take
.collect;
let reconstructed = reconstruct.unwrap;
assert_eq!;
Usage Examples
Basic Usage
use ;
use Path;
// Create a new ShamirShare instance with 5 shares, threshold of 3
let mut shamir = builder.build?;
// Split a secret
let secret = b"This is a secret message";
let shares = shamir.split?;
// Store shares in a directory
let store = new?;
for share in &shares
// Later, reconstruct the secret using at least 3 shares
let mut reconstruction_shares = Vecnew;
for i in 1..=3
let reconstructed = reconstruct?;
assert_eq!;
Security
This library is designed with security as the primary concern, implementing multiple layers of protection against various attack vectors:
Cryptographic Security Guarantees
- Constant-Time GF(2^8) Arithmetic: All finite field operations use constant-time algorithms with no lookup tables, preventing cache-timing side-channel attacks
- Resistant to Side-Channel Attacks: Russian Peasant Multiplication and Fermat's Little Theorem for inversion ensure consistent execution time regardless of input values
- Cryptographically Secure Random Number Generation: Uses
ChaCha20Rngseeded fromOsRngfor polynomial coefficient generation - Integrity Checking: SHA-256 hash verification with constant-time comparison prevents tampering detection
- No Information Leakage: Individual shares reveal no information about the secret without meeting the threshold
Implementation Security Features
- Memory Safety: Written in Rust with zero unsafe code blocks
- Constant-Time Hash Comparison: Prevents timing attacks during integrity verification
- Secure Share Format: Magic numbers and version checks prevent format confusion attacks
- Parallel Processing: Uses Rayon for safe parallel computation without compromising security
Security Considerations for Users
- Share Distribution: Distribute and store shares in different physical locations
- Access Control: Implement proper access controls for share storage locations
- Network Security: Use secure channels when transmitting shares over networks
Security-Performance Tradeoff
This library prioritizes security over raw performance. The constant-time implementations of cryptographic operations protect against side-channel attacks but are slower than lookup table-based approaches. For typical use cases (secrets under a few MB), the performance impact is minimal:
| Operation | 1KB Secret | 10KB Secret | 100KB Secret |
|---|---|---|---|
| Split | ~160 us | ~550 us | ~3.4 ms |
| Reconstruct | ~80 us | ~170 us | ~680 us |
| Total | ~240 us | ~720 us | ~4.1 ms |
API Documentation
For detailed API documentation, please visit docs.rs/shamir_share.
Core Types
ShamirShare: Main implementation of Shamir's Secret Sharing schemeShamirShareBuilder: Builder pattern for configuringShamirShareinstancesDealer: Lazy iterator for generating shares on-demandShare: Represents a single share of the secretFileShareStore: File-based storage for sharesShareStore: Trait for implementing custom storage backendsConfig: Configuration options for the sharing processSplitMode: Enum for specifying how data is split (Sequential or Parallel)
Development & Testing
Running Tests
# Run all tests
# Run tests with coverage
# Run benchmarks
Fuzzing for Security
This library includes comprehensive fuzzing targets to ensure robustness against malformed inputs:
# Install nightly Rust and cargo-fuzz
# Run fuzzing targets
# Or use the convenience script
The fuzzing targets test:
- Reconstruction robustness: Invalid shares, corrupted data, edge cases
- Storage parsing: Malformed files, truncated data, invalid headers
- Memory safety: Buffer overruns, integer overflows, panic conditions
Performance Testing
# Run streaming benchmarks
# Compare with baseline
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines on how to contribute to this project.
Security Considerations for Contributors
- All cryptographic operations must be constant-time
- New features should include comprehensive fuzzing targets
- Performance changes should be benchmarked
- Security-sensitive code requires additional review
License
This project is licensed under the MIT License - see the LICENSE file for details.