This page documents BitChat's implementation of the Noise Protocol Framework, specifically the Noise_XX_25519_ChaChaPoly_SHA256 pattern used for Bluetooth mesh encryption. The implementation is found in bitchat/Noise/NoiseProtocol.swift1-123 and provides:
Scope: This page covers the low-level Noise protocol primitives (NoiseCipherState, NoiseSymmetricState, NoiseHandshakeState) and the NoiseSession lifecycle. For high-level service integration, see page 4.2.
Core Classes:
| Class | File Location | Purpose |
|---|---|---|
NoiseCipherState | bitchat/Noise/NoiseProtocol.swift132-160 | ChaCha20-Poly1305 encryption with nonce management and replay protection |
NoiseSymmetricState | bitchat/Noise/NoiseProtocol.swift395-405 | HKDF key derivation and transcript hashing |
NoiseHandshakeState | bitchat/Noise/NoiseProtocol.swift522-540 | XX pattern state machine and message processing |
NoiseSession | bitchat/Noise/NoiseSession.swift14-46 | Manages state and transitions for a specific peer connection |
SecureNoiseSession | bitchat/Noise/SecureNoiseSession.swift11-15 | Extends NoiseSession with security guardrails (timeouts, limits) |
Sources: bitchat/Noise/NoiseProtocol.swift1-123 bitchat/Noise/NoiseSession.swift14-46 bitchat/Noise/SecureNoiseSession.swift11-15
BitChat utilizes the Noise Protocol specifically for its peer-to-peer Bluetooth mesh transport. While Nostr transport uses NIP-17/NIP-44, the BLE transport requires a stateful, mutually authenticated session to ensure privacy in local mesh environments.
This diagram maps the natural language "Encryption Flow" to the specific code entities that handle each stage of the Noise lifecycle.
Diagram: Cryptographic Component Relationships
Sources: bitchat/Noise/NoiseProtocol.swift522-540 bitchat/Noise/NoiseSession.swift14-32 bitchat/Services/NoiseEncryptionService.swift149-173 bitchat/Noise/NoiseSessionManager.swift14-32 bitchat/Noise/SecureNoiseSession.swift11-15
BitChat implements the Noise_XX_25519_ChaChaPoly_SHA256 configuration bitchat/Noise/NoiseProtocol.swift114-123:
| Component | Implementation | Purpose |
|---|---|---|
| Pattern | XX | Mutual authentication; identities are encrypted bitchat/Noise/NoiseProtocol.swift93 |
| DH Function | Curve25519 | X25519 Elliptic Curve Diffie-Hellman bitchat/Noise/NoiseProtocol.swift116 |
| Cipher | ChaChaPoly | ChaCha20-Poly1305 AEAD bitchat/Noise/NoiseProtocol.swift117 |
| Hash | SHA256 | SHA-256 for HKDF and transcript hashing bitchat/Noise/NoiseProtocol.swift118 |
The XX pattern consists of three messages bitchat/Noise/NoiseProtocol.swift38-45:
-> e: Initiator sends ephemeral public key.<- e, ee, s, es: Responder sends ephemeral key, performs DH (ee), sends encrypted static key (s), performs DH (es).-> s, se: Initiator sends encrypted static key (s), performs DH (se).The NoiseSession class orchestrates these transitions, starting with startHandshake() bitchat/Noise/NoiseSession.swift50-77 and continuing with processHandshakeMessage(_:) bitchat/Noise/NoiseSession.swift79-153
Sources: bitchat/Noise/NoiseProtocol.swift25-45 bitchat/Noise/NoiseProtocol.swift114-123 bitchat/Noise/NoiseSession.swift50-153
The NoiseCipherState manages symmetric encryption and the sliding window replay protection bitchat/Noise/NoiseProtocol.swift132-151
UInt64 nonce bitchat/Noise/NoiseProtocol.swift139isValidNonce to prevent integer wrap-around vulnerabilities bitchat/Noise/NoiseProtocol.swift171-188Diagram: NoiseCipherState Decryption Logic
Sources: bitchat/Noise/NoiseProtocol.swift132-145 bitchat/Noise/NoiseProtocol.swift171-188
This class handles the evolution of the chainingKey and the hash (transcript) bitchat/Noise/NoiseProtocol.swift395-405 It provides mixKey for HKDF updates and split for generating final transport ciphers.
Sources: bitchat/Noise/NoiseProtocol.swift395-405
Orchestrates the XX state machine bitchat/Noise/NoiseProtocol.swift522-540 It handles writeMessage and readMessage operations, processing tokens like e, s, ee, es, and se.
Sources: bitchat/Noise/NoiseProtocol.swift522-540
Static identity keys are generated as Curve25519.KeyAgreement.PrivateKey and stored securely.
KeychainManager provides specific methods for saving and retrieving identity keys bitchat/Services/KeychainManager.swift90-100NoiseSession is initialized with a localStaticKey bitchat/Noise/NoiseSession.swift38-44NoiseEncryptionService handles identity generation and loading via the KeychainManager, ensuring keys persist across app restarts bitchat/Services/NoiseEncryptionService.swift153-160Sources: bitchat/Noise/NoiseSession.swift34-46 bitchat/Services/KeychainManager.swift90-100 bitchat/Services/NoiseEncryptionService.swift153-160
Noise sessions are managed by NoiseSessionManager, which provides thread-safe access to peer sessions bitchat/Noise/NoiseSessionManager.swift14-18
Diagram: NoiseSession Handshake Exchange
Sources: bitchat/Noise/NoiseSession.swift50-153 bitchat/Noise/NoiseSessionManager.swift71-166
To maintain protocol integrity and protect against identity loss, BitChat integrates the Noise framework with secure identity state management.
SecureIdentityStateManager ensures that identity mappings (Ephemeral PeerID to Cryptographic Fingerprint) are encrypted at rest using AES-GCM and stored in the Keychain bitchat/Identity/SecureIdentityStateManager.swift32-36KeychainManager implements a one-time upgrade of items to AfterFirstUnlock accessibility to ensure noise keys are readable while the device is locked bitchat/Services/KeychainManager.swift59-62NoiseSessionManager for fast access bitchat/Noise/NoiseSessionManager.swift15SecureNoiseSession adds security guardrails, including session timeouts and message count limits bitchat/Noise/SecureNoiseSession.swift16-54NoiseRateLimiter prevents DoS attacks by restricting handshake and message frequency per peer and globally bitchat/Noise/NoiseRateLimiter.swift13-21Sources: bitchat/Identity/SecureIdentityStateManager.swift32-48 bitchat/Services/KeychainManager.swift59-62 bitchat/Noise/NoiseSessionManager.swift15 bitchat/Noise/SecureNoiseSession.swift16-54 bitchat/Noise/NoiseRateLimiter.swift13-21
The implementation is verified via the NoiseProtocolTests and NoiseCoverageTests suites.
aliceSession (initiator) and bobSession (responder) bitchatTests/Noise/NoiseProtocolTests.swift95-129NoiseCoverageTests specifically validates that NoiseCipherState rejects duplicate and stale nonces bitchatTests/Noise/NoiseCoverageTests.swift58-88Noise_XX_25519_ChaChaPoly_SHA256 protocol bitchatTests/Noise/NoiseProtocolTests.swift17-37Sources: bitchatTests/Noise/NoiseProtocolTests.swift95-177 bitchatTests/Noise/NoiseCoverageTests.swift58-88
Refresh this wiki
This wiki was recently refreshed. Please wait 6 days to refresh again.