Skip to content

Commit af38942

Browse files
committed
dev_log: log encryption update
combined .ulge file instead of separate .ulgc/ulgk files
1 parent 1938c2e commit af38942

File tree

1 file changed

+43
-32
lines changed

1 file changed

+43
-32
lines changed

en/dev_log/log_encryption.md

+43-32
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,45 @@ If another [SDLOG_ALGORITHM](../advanced_config/parameter_reference.md#SDLOG_ALG
2525

2626
The encryption process for each new ULog is:
2727

28-
1. A ULog file is created and opened for writing on the SD card.
29-
This is named with the file extension `.ulogc`(ulog cipher).
30-
2. A XChaCha20 symmetric key is generated and encrypted using an RSA2048 public key.
31-
This encrypted/wrapped key is stored on the SD card in a file that has the suffix `.ulgk` (ulog wrapped key).
32-
3. The unencrypted symmetric key is used to encrypt ULog data blocks before they are written to disk (the `.ulogc` file).
28+
1. A XChaCha20 symmetric key is generated and encrypted using an RSA2048 public key.
29+
This encrypted/wrapped key is stored on the SD card in the beginning of a file that has the suffix `.ulge` (ulog encrypted).
30+
2. A ULog data is encrypted with unwrapped symmetric key and the resulting data is appended into the end of the `.ulge` file immediately after the stored key data.
3331

34-
After the flight, there are two files on the SD card:
35-
36-
- `.ulogc` (ulog cipher): the encrypted log file data.
37-
- `.ulogk` (ulog wrapped key): the symmetric key used to encrypt the data, encrypted with an RSA public key.
32+
After the flight, the `.ulge` file containing both symmetric key and the encrypted log data can be found on the SD card.
3833

3934
In order to extract the log file, a user must first decrypt the wrapped symmetric key, which can then be used to decrypt the log.
4035
Note that decrypting the symmetric key file is only possible if the user has the appropriate RSA private key (corresponding to the public key that was used to wrap it).
4136
This process is covered in [Download & Decrypt Log Files](#download-decrypt-log-files) below.
4237

38+
## File structure
39+
40+
Encrypted `.ulge` file contains following sections:
41+
```
42+
-------------------------
43+
| Header |
44+
-------------------------
45+
| Wrapped symmetric key |
46+
-------------------------
47+
| Encrypted ulog data |
48+
-------------------------
49+
50+
```
51+
52+
Header section (22 bytes) contains following fields:
53+
54+
| Bytes | Field |
55+
|--------|------------------------|
56+
| 0..6 | File magic identifier |
57+
| 7 | Header version |
58+
| 8..15 | Timestamp |
59+
| 16 | exchange algorithm |
60+
| 17 | exchange key index |
61+
| 18..19 | key size |
62+
| 20..21 | nonce size |
63+
64+
The header part begins with magic string: `"ULogEnc"`, which identifies this is encrypted ulog file.
65+
File offset of the symmetric key section is 22 and file offset of the log data section is 22+key_size+nonce_size (key_size and nonce_size are taken from header section).
66+
4367
## Custom PX4 Firmware with Log Encryption
4468

4569
You will need to build custom firmware that contains your own public RSA key and the required Crypto API modules to support log encryption.
@@ -101,7 +125,7 @@ The stub keystore is a keystore implementation that can store up to four keys.
101125
The initial values of these keys are set in the locations defined by `CONFIG_PUBLIC_KEY0` to `CONFIG_PUBLIC_KEY3`.
102126
The keys can be used for different cryptographic purposes, which are determined by parameters.
103127

104-
The _exchange key_, which is the public key used for encrypting the symmetric key stored in the `.ulgk` file, is specified using [SDLOG_EXCH_KEY](../advanced_config/parameter_reference.md#SDLOG_EXCH_KEY) as an index value into the key store.
128+
The _exchange key_, which is the public key used for encrypting the symmetric key stored in the beginning of `.ulge` file, is specified using [SDLOG_EXCH_KEY](../advanced_config/parameter_reference.md#SDLOG_EXCH_KEY) as an index value into the key store.
105129
The value is `1` by default, which maps to the key defined in `CONFIG_PUBLIC_KEY1`.
106130

107131
The _logging key_ is the unencrypted symmetric key.
@@ -111,7 +135,7 @@ Note that the value is generated fresh for each log, and any value specified in
111135
You can use choose different locations for your keys as long as they aren't used by anything else.
112136
:::
113137

114-
The key in `CONFIG_PUBLIC_KEY1` is the public key used to wrap the symmetric key in the `.ulgk` file (by default: see [SDLOG_EXCH_KEY](../advanced_config/parameter_reference.md#SDLOG_EXCH_KEY)).
138+
The key in `CONFIG_PUBLIC_KEY1` is the public key used to wrap the symmetric key in the the beginning of `.ulge` file (by default: see [SDLOG_EXCH_KEY](../advanced_config/parameter_reference.md#SDLOG_EXCH_KEY)).
115139
You can use the `rsa2048.pub` key for testing, or replace it with the path to your own public key in the file (see [Generate RSA Public & Private Keys](#generate-rsa-public-private-keys)).
116140

117141
Build the firmware like this:
@@ -160,19 +184,8 @@ You can now build and test.
160184
## Download & Decrypt Log Files
161185

162186
Encrypted log files are downloaded using the QGroundControl [Log Download](https://docs.qgroundcontrol.com/master/en/qgc-user-guide/analyze_view/log_download.html) view (**Analyze Tools > Log Download**) just like ordinary log files.
163-
The only difference is that for each flight you will need to download both the encrypted log file, and the file containing the encrypted symmetric key.
164-
165-
The encrypted log file and encrypted symmetric key file are displayed with a timestamp (but no filename) in QGroundControl, as shown below.
166-
You can determine which files are associated based on their timestamps.
167-
168-
![QGroundControl ULog Download](../../assets/qgc/analyze/encrypted_log.png)
169-
170-
Select and download both files.
171-
172-
Note that both files will be downloaded with the `.ulg` suffix.
173-
You can identify the symmetric key file, as it is usually much smaller than the log file (about 300 bytes)
174187

175-
For convenience in the decryption step, you might rename the file extensions to add back the `.ulgc` (log) and `.ulgk` (key) file extensions.
188+
Note that the encrypted files will be downloaded with the `.ulg` suffix, instead of `.ulge`.
176189

177190
### Decrypt ULogs
178191

@@ -181,18 +194,18 @@ There is a Python script that can be used to decrypt logs in `Tools/decrypt_ulog
181194

182195
`decrypt_ulog.py` takes 3 arguments:
183196

184-
1. The encrypted `.ulogc` file.
185-
2. The symmetric key `.ulogk` file.
186-
3. The decryption key (the RSA2048 `.pem` private key which is used to unwrap the `.ulogk` file).
197+
1. The encrypted ulog file. Supporting both `.ulge` and the old legacy `.ulgc` file format.
198+
2. Optional symmetric key `.ulgk` file. Give empty string `''` for decrypting `.ulge`. This is for supporting legacy `.ulgc/.ulgk` log files.
199+
3. The decryption key (the RSA2048 `.pem` private key which is used to unwrap the symmetric key).
187200

188201
```sh
189202
usage: decrypt_ulog.py [-h] [ulog_file] [ulog_key] [rsa_key]
190203

191204
CLI tool to decrypt an ulog file
192205

193206
positional arguments:
194-
ulog_file .ulog file
195-
ulog_key .ulogk, encrypted key
207+
ulog_file .ulge/.ulgc, encrypted log file
208+
ulog_key .ulgk, legacy encrypted key (give empty string '' to ignore for .ulge)
196209
rsa_key .pem format key for decrypting the ulog key
197210

198211
optional arguments:
@@ -204,13 +217,11 @@ As an example:
204217

205218
```sh
206219
python3 decrypt_ulog.py \
207-
/home/john/Downloads/log_24_2024-10-6-23-39-50.ulgc \
208-
/home/john/Downloads/log_23_2024-10-6-23-39-48.ulgk \
220+
/home/john/Downloads/log_24_2024-10-6-23-39-50.ulg '' \
209221
new_keys/private_key.pem
210222
```
211223

212-
On success the decrypted log file is created with the `.ul` suffix instead of `.ulg`.
213-
Rename the file back to `.ulg` and it is now ready for flight review.
224+
On success the decrypted log file is created with the `.ulog` suffix.
214225

215226
## Generate RSA Public & Private Keys
216227

0 commit comments

Comments
 (0)