# 6.7 Bluetooth Security

### <span class="ng-star-inserted">Legacy Pairing vs. LE Secure Connections</span>

<span class="ng-star-inserted">Pairing is the process of creating a trusted relationship between two devices by generating and storing shared secret keys.</span>

- **<span class="ng-star-inserted">Legacy Pairing:</span>**<span class="ng-star-inserted"> Used in Bluetooth versions prior to 4.2. While it provided security, certain association models (like "Just Works") were vulnerable to passive eavesdropping and Man-in-the-Middle (MITM) attacks because they did not authenticate the user or device.</span>
- **<span class="ng-star-inserted">LE Secure Connections:</span>**<span class="ng-star-inserted"> The modern security standard for BLE. It is a significantly more robust pairing method that uses a government-grade cryptographic algorithm called </span>**<span class="ng-star-inserted">Elliptic Curve Diffie-Hellman (ECDH)</span>**<span class="ng-star-inserted"> for key exchange. This algorithm provides a very high level of protection against passive eavesdropping, even if an attacker manages to capture all the pairing packets. LE Secure Connections is the mandatory security foundation for modern BLE devices.</span>

### <span class="ng-star-inserted">Encryption, Privacy, and MITM Protection</span>

<span class="ng-star-inserted">Modern Bluetooth security is built on three core pillars:</span>

1. **<span class="ng-star-inserted">Encryption (Confidentiality):</span>**<span class="ng-star-inserted"> Once devices are paired, the connection can be encrypted. Bluetooth uses the </span>**<span class="ng-star-inserted">AES-CCM</span>**<span class="ng-star-inserted"> algorithm to encrypt all data sent over the link. This ensures that if an attacker were to listen to the radio traffic, they would only see unintelligible encrypted data, not the actual information.</span>
2. **<span class="ng-star-inserted">Privacy (Anti-Tracking):</span>**<span class="ng-star-inserted"> To prevent malicious actors from tracking a user by listening for their device's Bluetooth address, BLE uses </span>**<span class="ng-star-inserted">Resolvable Private Addresses (RPAs)</span>**<span class="ng-star-inserted">. A device with this feature enabled will periodically change its public Bluetooth address to a new, randomized one. Only devices that have previously paired with it possess the key (the IRK - Identity Resolving Key) needed to resolve this random address and identify the device.</span>
3. **<span class="ng-star-inserted">Authentication and MITM Protection:</span>**<span class="ng-star-inserted"> A Man-in-the-Middle (MITM) attack occurs when an attacker secretly sits between two devices and relays their communication, potentially altering it. LE Secure Connections protects against this by authenticating the connection during pairing. This is done using one of several association models:</span>
    
    
    - **<span class="ng-star-inserted">Passkey Entry:</span>**<span class="ng-star-inserted"> The user enters a 6-digit number on both devices.</span>
    - **<span class="ng-star-inserted">Numeric Comparison:</span>**<span class="ng-star-inserted"> A 6-digit number is displayed on both devices, and the user confirms they are the same. This is the most common method for devices with displays.</span>
    - <span class="ng-star-inserted">If a connection is authenticated, the devices have proven they are communicating directly with each other and not an imposter.</span>

### <span class="ng-star-inserted">Security Best Practices for Developers</span>

<span class="ng-star-inserted">For students building Bluetooth applications, security should be a primary concern.</span>

- **<span class="ng-star-inserted">Use LE Secure Connections:</span>**<span class="ng-star-inserted"> Always use the highest security mode available on your platform. Avoid legacy pairing if possible.</span>
- **<span class="ng-star-inserted">Authenticate When Possible:</span>**<span class="ng-star-inserted"> For devices with a display or keyboard, use Numeric Comparison or Passkey Entry to protect against MITM attacks. For devices without a user interface (like a sensor), you must be aware that the connection is unauthenticated.</span>
- **<span class="ng-star-inserted">Enable Privacy:</span>**<span class="ng-star-inserted"> Use Resolvable Private Addresses to prevent your device from being tracked over time.</span>
- **<span class="ng-star-inserted">Validate Data:</span>**<span class="ng-star-inserted"> Do not blindly trust the data received over a BLE link. Always validate it at the application layer to ensure it is in the expected format and range.</span>
- **<span class="ng-star-inserted">Use the Correct Security Level for Characteristics:</span>**<span class="ng-star-inserted"> Define the minimum security level (encryption, authentication) required to read or write specific GATT characteristics. Don't expose sensitive data on an open, unencrypted connection.</span>