Overview
VoicePact implements a defense-in-depth security model designed for the unique challenges of African informal markets:- Cryptographic integrity - Ed25519 signatures and hash verification
- Immutable audit logs - Complete event trail for dispute resolution
- Data encryption - Sensitive information protected at rest
- Webhook verification - HMAC signatures for external API callbacks
- Rate limiting - Protection against SMS/USSD abuse (planned)
Security Principles
From the README (README.md:107-121):
Cryptographic Services
TheCryptoService class (crypto_service.py) provides all cryptographic operations.
Contract Hashing
Purpose: Ensure contract immutability and detect tampering Implementation (crypto_service.py:51-63):
contract_generator.py:54-60):
crypto_service.py:238-244):
BLAKE2b vs SHA-256:
- BLAKE2b - Faster, smaller output, modern
- SHA-256 - Wider compatibility, FIPS approved
Ed25519 Digital Signatures
Purpose: Cryptographically bind parties to contract terms Key pair generation (crypto_service.py:27-49):
crypto_service.py:246-259):
Why derive keys from phone numbers?
- No key storage - Keys regenerated on-demand from phone number
- Consistent - Same phone always produces same key
- Isolated - Each phone has unique cryptographic identity
- Recovery - No key backup needed; derived from master secret
crypto_service.py:65-76):
SMS OTP Generation
Purpose: Phone-based identity verification Code generation (crypto_service.py:101-111):
- Deterministic - Same contract + phone + day = same code
- Time-limited - Changes daily (no database needed)
- Collision-resistant - SHA-256 hash prevents guessing
Payment Reference Generation
Purpose: Unique, verifiable payment identifiers Implementation (crypto_service.py:121-128):
AG-260306-A1B2C3 + 150000.00 + +254712345678 → 4F7A3B9D1E2C8A6F
Webhook Signature Verification
Purpose: Validate callbacks from Africa’s Talking are authentic Signature generation (crypto_service.py:162-174):
crypto_service.py:176-182):
Why HMAC for webhooks?
- Shared secret - Both parties know the webhook secret
- No PKI - Simpler than certificate management
- Fast - Critical for real-time webhook processing
- Standard - Used by GitHub, Stripe, etc.
Data Encryption
Purpose: Protect sensitive data at rest (PII, payment details) Encryption (crypto_service.py:184-203):
crypto_service.py:205-225):
Audit Trail
Purpose: Immutable evidence chain for disputes Audit signature creation (crypto_service.py:130-144):
contract.py:271-309):
- Append-only - Logs never deleted or modified
- Timestamped - Every action has precise timing
- Actor tracking - Know who did what
- State diff - Record old and new values
- Cryptographically signed - Tamper detection
Database Security Constraints
Integrity checks (contract.py:133-139):
Session Security
USSD session tokens (crypto_service.py:227-236):
Contract Verification Codes
Purpose: Human-readable integrity checks Implementation (crypto_service.py:265-275):
AG-260306-A1B2C3 → Verification code VC-4F7A3B9D
Parties can verify contract authenticity by comparing codes.
Security Best Practices
Implementation guidelines:
- Never log secrets - Redact keys, tokens, passwords in logs
- Constant-time comparison - Always use
hmac.compare_digest()for equality checks - Key rotation - Rotate master secrets periodically (not yet implemented)
- Secure randomness - Use
secretsmodule, neverrandom - Input validation - Sanitize all user inputs before processing
- Rate limiting - Prevent brute-force attacks (planned)
- HTTPS only - Never transmit sensitive data over HTTP
- PII minimization - Only store necessary personal data
Configuration
Security settings (.env):
Threat Model
Mitigated threats:- ✅ Contract tampering - Cryptographic hashes detect modifications
- ✅ Signature forgery - Ed25519 prevents impersonation
- ✅ Webhook spoofing - HMAC verification blocks fake callbacks
- ✅ Replay attacks - Timestamped signatures expire
- ✅ Timing attacks - Constant-time comparisons
- ⏳ Rate limiting - Prevent SMS/USSD flooding
- ⏳ Audio encryption - Encrypt voice recordings at rest
- ⏳ Key rotation - Automated master key updates
- ⏳ 2FA - Optional second factor for high-value contracts
Related
- Verification - How cryptography enables multi-modal verification
- Contract Lifecycle - Audit trail throughout contract states
- Escrow Payments - Payment integrity and security