# DryBox Integrated Protocol

This directory contains the integrated DryBox system with Icing protocol support, featuring:
- End-to-end encryption using ChaCha20-Poly1305 or AES-256-GCM
- 4-FSK modulation for transmitting encrypted data over GSM voice channels
- Codec2 voice compression
- Full protocol key exchange with ECDH and HKDF
- PyQt5 UI for easy testing

## Architecture

```
┌─────────────────┐         ┌─────────────────┐
│    Phone 1      │         │    Phone 2      │
├─────────────────┤         ├─────────────────┤
│ Icing Protocol  │<------->│ Icing Protocol  │  (Key Exchange)
│   - ECDH        │         │   - ECDH        │
│   - ChaCha20    │         │   - ChaCha20    │
├─────────────────┤         ├─────────────────┤
│ Voice Protocol  │         │ Voice Protocol  │
│   - Codec2      │         │   - Codec2      │
│   - Encryption  │         │   - Encryption  │
│   - 4-FSK       │         │   - 4-FSK       │
├─────────────────┤         ├─────────────────┤
│ GSM Simulator   │<------->│ GSM Simulator   │  (Audio Channel)
└─────────────────┘         └─────────────────┘
```

## Quick Start

### 1. Using the Integrated UI (Recommended)

```bash
# Terminal 1: Start GSM simulator
python3 gsm_simulator.py

# Terminal 2: Start the integrated UI
python3 UI/integrated_ui.py
```

In the UI:
1. Click "Start GSM Simulator" (or manually start it)
2. Both phones will initialize automatically
3. Click "Connect" on Phone 1 (it will auto-detect Phone 2's port)
4. Click "Start Key Exchange" on Phone 1
5. Once secure, you can:
   - Send encrypted text messages
   - Send voice (requires input.wav in DryBox directory)

### 2. Using Command Line

```bash
# Terminal 1: Start GSM simulator
python3 gsm_simulator.py

# Terminal 2: Start receiver
python3 integrated_protocol.py receiver
# Note the protocol port shown (e.g., 35678)

# Terminal 3: Start sender
python3 integrated_protocol.py sender
# Enter the receiver's port when prompted
# Enter the receiver's identity key when prompted
```

### 3. Running Tests

```bash
# Run the automated test suite
cd ..
python3 test_drybox_integration.py

# Run manual interactive test
python3 test_drybox_integration.py --manual
```

## Features

### Encryption
- **ChaCha20-Poly1305**: Modern, fast stream cipher (recommended)
- **AES-256-GCM**: Industry standard block cipher
- **Key Exchange**: ECDH with secp256r1 curve
- **Key Derivation**: HKDF-SHA256

### Voice Processing
- **Codec2**: Ultra-low bitrate voice codec (1200 bps default)
- **4-FSK Modulation**: Robust against GSM codec distortion
  - Frequencies: 600, 1200, 1800, 2400 Hz
  - Baud rate: 600 symbols/second
  - 2 bits per symbol
- **FEC**: Forward error correction for reliability

### Protocol Flow
1. **Connection Setup**: Phones connect to GSM simulator
2. **Protocol Handshake**: 
   - PING request/response (cipher negotiation)
   - HANDSHAKE messages (ephemeral key exchange)
   - HKDF key derivation
3. **Secure Communication**:
   - Text messages: Encrypted with message headers
   - Voice: Compressed → Encrypted → Modulated → Transmitted

## File Structure

```
DryBox/
├── integrated_protocol.py    # Main integration module
├── gsm_simulator.py         # GSM channel simulator
├── protocol.py              # Original DryBox protocol (updated)
├── UI/
│   ├── integrated_ui.py    # PyQt5 UI with protocol integration
│   └── python_ui.py        # Original UI
├── input.wav               # Input audio file for testing
└── received.wav            # Output audio file (created by receiver)
```

## Creating Test Audio

If you don't have input.wav:

```bash
# Create a 1-second 440Hz test tone
sox -n input.wav synth 1 sine 440 rate 8000

# Or convert existing audio
sox your_audio.wav -r 8000 -c 1 input.wav trim 0 2
```

## Troubleshooting

1. **Import errors**: Make sure you're in the correct directory and the parent protocol modules are accessible
2. **GSM simulator already running**: Check for existing processes on port 12345
3. **No audio output**: Check that sox and required audio tools are installed
4. **Key exchange timeout**: Ensure both instances can communicate on their protocol ports (not just GSM ports)

## Security Notes

- Identity keys are generated fresh each run
- In production, identity keys should be persisted and verified out-of-band
- The current implementation uses predefined test keys for convenience
- All voice data is encrypted end-to-end before transmission