# Voice-over-GSM Protocol Implementation

This implementation provides encrypted voice communication over standard GSM voice channels without requiring CSD/HSCSD.

## Architecture

### 1. Voice Codec (`voice_codec.py`)
- **Codec2Wrapper**: Simulates Codec2 compression
  - Supports multiple bitrates (700-3200 bps)
  - Default: 1200 bps for GSM robustness
  - 40ms frames (48 bits/frame at 1200 bps)

- **FSKModem**: 4-FSK modulation for voice channels
  - Frequency band: 300-3400 Hz (GSM compatible)
  - Symbol rate: 600 baud
  - 4 frequencies: 600, 1200, 1800, 2400 Hz
  - Preamble: 800 Hz for 100ms

- **VoiceProtocol**: Integration layer
  - Manages codec and modem
  - Handles encryption with ChaCha20-CTR
  - Frame-based processing

### 2. Protocol Messages (`messages.py`)
- **VoiceStart** (20 bytes): Initiates voice call
  - Version, codec mode, FEC type
  - Session ID (64 bits)
  - Initial sequence number

- **VoiceAck** (16 bytes): Accepts/rejects call
  - Status (accept/reject)
  - Negotiated codec and FEC

- **VoiceEnd** (12 bytes): Terminates call
  - Session ID for confirmation

- **VoiceSync** (20 bytes): Synchronization
  - Sequence number and timestamp
  - For jitter buffer management

### 3. Encryption (`encryption.py`)
- **ChaCha20-CTR**: Stream cipher for voice
  - No authentication overhead (HMAC per second)
  - 12-byte nonce with frame counter
  - Uses HKDF-derived key from main protocol

### 4. Protocol Integration (`protocol.py`)
- Voice session management
- Message handlers for all voice messages
- Methods:
  - `start_voice_call()`: Initiate call
  - `accept_voice_call()`: Accept incoming
  - `end_voice_call()`: Terminate
  - `send_voice_audio()`: Process audio

## Usage Example

```python
# After key exchange is complete
alice.start_voice_call(codec_mode=5, fec_type=0)

# Bob automatically accepts if in auto mode
# Or manually: bob.accept_voice_call(session_id, codec_mode, fec_type)

# Send audio
audio_samples = generate_audio()  # 8kHz, 16-bit PCM
alice.send_voice_audio(audio_samples)

# End call
alice.end_voice_call()
```

## Key Features

1. **Codec2 @ 1200 bps**
   - Optimal for GSM vocoder survival
   - Intelligible but "robotic" quality

2. **4-FSK Modulation**
   - Survives GSM/AMR/EVS vocoders
   - 2400 baud with FEC

3. **ChaCha20-CTR Encryption**
   - Low latency stream cipher
   - Frame-based IV management

4. **Forward Error Correction**
   - Repetition code (3x)
   - Future: Convolutional or LDPC

5. **No Special Requirements**
   - Works over standard voice calls
   - Compatible with any phone
   - Software-only solution

## Testing

Run the test scripts:
- `test_voice_simple.py`: Basic voice call setup
- `test_voice_protocol.py`: Full test with audio simulation (requires numpy)

## Implementation Notes

1. Message disambiguation: VoiceStart sets high bit in flags field to distinguish from VoiceSync (both 20 bytes)

2. The actual Codec2 library would need to be integrated for production use

3. FEC implementation is simplified (repetition code) - production would use convolutional codes

4. Audio I/O integration needed for real voice calls

5. Jitter buffer and timing recovery needed for production

## Security Considerations

- Voice frames use ChaCha20-CTR without per-frame authentication
- HMAC computed over 1-second blocks for efficiency
- Session binding through encrypted session ID
- PFS maintained through main protocol key rotation