|
| 1 | +# Introduction |
| 2 | +This `at_storage_security` example demonstrates how to implement secure storage for sensitive data in the ESP-AT firmware. It automatically encrypts sensitive data before storing it in NVS (Non-Volatile Storage) and decrypts it when reading, ensuring that critical information such as Wi-Fi credentials and keys are protected from unauthorized access. |
| 3 | + |
| 4 | +**Features:** |
| 5 | +- Automatic encryption/decryption of sensitive NVS data using AES-CTR algorithm. |
| 6 | +- Transparent operation - no changes needed to existing AT commands. |
| 7 | +- Custom AT command (`AT+STORAGE_NVS_KEY`) to manage encryption keys. |
| 8 | +- Selective encryption based on key names (e.g., keys containing "key", Wi-Fi passwords). |
| 9 | + |
| 10 | +# How It Works |
| 11 | +The example uses function wrapping to intercept NVS read/write operations: |
| 12 | +- `__wrap_nvs_set_blob/str` - Encrypts sensitive data before writing to NVS. |
| 13 | +- `__wrap_nvs_get_blob/str` - Decrypts sensitive data after reading from NVS. |
| 14 | + |
| 15 | +The encryption process: |
| 16 | +1. Generates a random IV (Initialization Vector) for each write operation. |
| 17 | +2. Encrypts the data using AES-CTR with the configured key. |
| 18 | +3. Stores IV + ciphertext together in NVS. |
| 19 | +4. On read, extracts the IV and decrypts the ciphertext. |
| 20 | + |
| 21 | +# Encrypted Data |
| 22 | +By default, the following data is automatically encrypted: |
| 23 | +- NVS keys containing the word "key" |
| 24 | +- Wi-Fi station password (`sta.pswd`) |
| 25 | +- Wi-Fi AP information (`sta.apinfo`) |
| 26 | + |
| 27 | +You can customize which keys are encrypted by modifying the `at_nvs_key_encrypt()` function in `custom/at_custom_cmd.c`. |
| 28 | + |
| 29 | +# Usage |
| 30 | + |
| 31 | +## 1. Add Component to Build System |
| 32 | + |
| 33 | +- Linux or macOS |
| 34 | +```bash |
| 35 | +export AT_CUSTOM_COMPONENTS=(path_of_at_storage_security) |
| 36 | +``` |
| 37 | + |
| 38 | +- Windows |
| 39 | +```cmd |
| 40 | +set AT_CUSTOM_COMPONENTS=(path_of_at_storage_security) |
| 41 | +``` |
| 42 | + |
| 43 | +**Notes:** |
| 44 | +- Replace `(path_of_at_storage_security)` with the real absolute path of your `at_storage_security` directory. |
| 45 | +- You can specify multiple components. For example: `export AT_CUSTOM_COMPONENTS="~/prefix/my_path1 ~/prefix/my_path2"`. |
| 46 | + |
| 47 | +## 2. Build and Flash |
| 48 | + |
| 49 | +Build the project and flash the firmware to the module according to the [Compile ESP-AT Project](https://docs.espressif.com/projects/esp-at/en/latest/esp32/Compile_and_Develop/How_to_clone_project_and_compile_it.html#compile-esp-at-project-locally) guide. |
| 50 | + |
| 51 | +## 3. Manage Encryption Key |
| 52 | + |
| 53 | +### Query Current Key |
| 54 | +``` |
| 55 | +AT+STORAGE_NVS_KEY? |
| 56 | +``` |
| 57 | + |
| 58 | +Response: |
| 59 | +``` |
| 60 | +++STORAGE_NVS_KEY:4553502d41542d4145532d4b45590000 |
| 61 | +OK |
| 62 | +``` |
| 63 | +This returns the current AES key in hexadecimal format (32 hex characters = 16 bytes / 128 bits). |
| 64 | + |
| 65 | +### Set New Key |
| 66 | +``` |
| 67 | +AT+STORAGE_NVS_KEY="0123456789abcdef0123456789abcdef" |
| 68 | +``` |
| 69 | + |
| 70 | +**Notes:** |
| 71 | +- The key must be exactly 32 hexadecimal characters (16 bytes / 128 bits). |
| 72 | +- After setting a new key, all subsequently stored data will be encrypted with the new key. |
| 73 | +- Previously stored data encrypted with the old key will become inaccessible unless you switch back to the old key. |
| 74 | +- **Important**: Store your encryption key securely! If the key is lost, encrypted data cannot be recovered. |
| 75 | + |
| 76 | +## 4. Test Encryption |
| 77 | + |
| 78 | +You can test the encryption by storing and retrieving Wi-Fi credentials: |
| 79 | + |
| 80 | +``` |
| 81 | +AT+CWJAP="ssid","password" |
| 82 | +``` |
| 83 | + |
| 84 | +The password "password" will be automatically encrypted before storing in NVS. To verify encryption is working, you can check the log output (requires setting log level to INFO): |
| 85 | +``` |
| 86 | +I (xxxxx) at-storage-sec: nvs_set_blob encrypted, key:sta.pswd, len:65, ret:0x0 |
| 87 | +I (xxxxx) plaintext: <addr> 70 61 73 73 77 6f 72 64 00 00 00 00 00 00 00 00 |password........| |
| 88 | +I (xxxxx) iv: ... |
| 89 | +I (xxxxx) ciphertext: ... |
| 90 | +``` |
| 91 | + |
| 92 | +## 5. Test Decryption |
| 93 | + |
| 94 | +After the Wi-Fi credentials are stored with encryption in step 4, you can verify that the decryption works correctly by restarting the device: |
| 95 | + |
| 96 | +``` |
| 97 | +AT+RST |
| 98 | +``` |
| 99 | + |
| 100 | +After the device restarts, it will automatically read the encrypted Wi-Fi password from NVS, decrypt it, and reconnect to the AP. You should see the device successfully connect to the Wi-Fi network. To verify the decryption is working, check the log output: |
| 101 | +``` |
| 102 | +I (xxxxx) at-storage-sec: nvs_get_blob decrypted, key:sta.pswd, len:65, ret:0x0 |
| 103 | +I (xxxxx) ciphertext: ... |
| 104 | +I (xxxxx) iv: ... |
| 105 | +I (xxxxx) plaintext: <addr> 70 61 73 73 77 6f 72 64 00 00 00 00 00 00 00 00 |password........| |
| 106 | +``` |
| 107 | + |
| 108 | +This confirms that the encrypted password was successfully decrypted and used for Wi-Fi connection. |
| 109 | + |
| 110 | +# Security Considerations |
| 111 | + |
| 112 | +## AES Key Management |
| 113 | +Instead of using the `AT+STORAGE_NVS_KEY` command to set the encryption key, you can implement dynamic key generation using device-specific data. For secure key generation methods, please refer to [AES Key and IV Management](https://github.com/espressif/esp-at/blob/master/examples/at_interface_security/README.md#aes-key-and-iv-management) in the at_interface_security example. |
| 114 | + |
| 115 | +## Enhanced Storage Security |
| 116 | +For even more secure storage, consider using ESP-IDF's built-in [NVS Encryption](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/storage/nvs_encryption.html) feature to encrypt the entire NVS partition. |
| 117 | + |
0 commit comments