Skip to content

Commit

Permalink
New translations log_encryption.md (Chinese Simplified)
Browse files Browse the repository at this point in the history
  • Loading branch information
PX4BuildBot committed Dec 11, 2024
1 parent 0f7e1ae commit a5b9649
Showing 1 changed file with 81 additions and 59 deletions.
140 changes: 81 additions & 59 deletions zh/dev_log/log_encryption.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,64 @@ Log encryption is not enabled by default in PX4 firmware builds.
To use it you will need to build firmware with this feature enabled and then upload it to the flight controller (see instructions below).
:::

:::info
The encryption algorithm used is set in [SDLOG_ALGORITHM](../advanced_config/parameter_reference.md#SDLOG_ALGORITHM).
At time of writing, only `XChaCha20` is supported (AES can be selected, but there is no implementation).
:::tip
Log encryption was has been improved in PX4 main (v1.16+) to generate a single encrypted log file that contains both encrypted log data, and an encrypted symmetric key that you can use to decrypt it (provided you can decrypt the symmetric key).

In earlier versions the encrypted symmetric key was stored in a separate file.
For more information see the [Log Encryption (PX4 v1.15)](https://docs.px4.io/v1.15/en/dev_log/log_encryption.html).
:::

## How ULog Encryption Works

:::info
This process assumes the default XChaCha20 algorithm is used.
If another [SDLOG_ALGORITHM](../advanced_config/parameter_reference.md#SDLOG_ALGORITHM) is used, the process is _likely_ to remain the same.
The encryption algorithm used is set in [SDLOG_ALGORITHM](../advanced_config/parameter_reference.md#SDLOG_ALGORITHM).
At time of writing, only `XChaCha20` is supported (AES can be selected, but there is no implementation).

If another algorithm is supported in future, the process is _likely_ to remain the same as documented here.
:::

The encryption process for each new ULog is:

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

After the flight, there are two files on the SD card:

- `.ulogc` (ulog cipher): the encrypted log file data.
- `.ulogk` (ulog wrapped key): the symmetric key used to encrypt the data, encrypted with an RSA public key.
After the flight, the `.ulge` file containing both the wrapped symmetric key and the encrypted log data can be found on the SD card.

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.
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).
This process is covered in [Download & Decrypt Log Files](#download-decrypt-log-files) below.
Decrypting the wrapped symmetric key file is only possible if the user has the corresponding RSA private key for the public key that was used to wrap it.

This process is covered in more detail in [Download & Decrypt Log Files](#download-decrypt-log-files) below.

## File Structure

Encrypted `.ulge` file contains following sections:

```plain
-------------------------
| Header |
-------------------------
| Wrapped symmetric key |
-------------------------
| Encrypted ulog data |
-------------------------
```

Header section (22 bytes) contains following fields:

| Bytes | Field |
| -------------------------------------- | --------------------- |
| 0..6 | File magic identifier |
| 7 | Header version |
| 8..15 | Timestamp |
| 16 | exchange algorithm |
| 17 | exchange key index |
| 18..19 | key size |
| 20..21 | nonce size |

The header part begins with magic string: `"ULogEnc"`, which identifies this is encrypted ulog file.
The file offset of the symmetric key section is `22` and the file offset of the log data section is `22 + key_size + nonce_size` (`key_size` and `nonce_size` are taken from the header section).

## Custom PX4 Firmware with Log Encryption

Expand All @@ -60,7 +90,7 @@ Crypto uses large amounts of flash memory, and is therefore not included in the
The easiest way to add support for encrypted logs is to define a custom `make` target that includes the required modules and your public RSA keys.

:::warning
Crypto uses a lot of flash memory, and many builds are close to their maximum capacity.
Many builds are close to their maximum capacity.
If you run into a build error telling you that you have gone above the maximum flash memory, you will need to disable other features in the `.px4board` file you are working on, or in the `default.px4board` file.
Be careful not to disable something you need.

Expand Down Expand Up @@ -88,21 +118,21 @@ This is not used in the current PX4 implementation and can be ignored.
:::details
Overview of crypto-relevant keys

| Argument | 描述 |
| ------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| CONFIG_BOARD_CRYPTO | Include crypto module in firmware.<br />= `y`: Enable log encryption.<br />= `n`: Disable log encryption. |
| CONFIG_DRIVERS_SW_CRYPTO | Include the PX4 crypto backend library (used by above library).<br />= `y`: Enable<br />= `n`: Disable |
| CONFIG_DRIVERS_STUB_KEYSTORE | Includes the PX4 stub keystore driver.<br />= `y`: Enable<br />= `n`: Disable |
| CONFIG_PUBLIC_KEY0 | Location of public key for keystore index 0. |
| CONFIG_PUBLIC_KEY1 | Location of public key for keystore index 1.<br />= `{path to key1}` |
| CONFIG_PUBLIC_KEY2 | Location of public key for keystore index 2.<br />= `{path to key2}` |
| CONFIG_PUBLIC_KEY3 | Location of public key for keystore index 3.<br />= `{path to key3}` |
| Argument | 描述 |
| ------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| CONFIG_BOARD_CRYPTO | Include crypto module in firmware.<br>= `y`: Enable log encryption.<br>= `n`: Disable log encryption. |
| CONFIG_DRIVERS_SW_CRYPTO | Include the PX4 crypto backend library (used by above library).<br>= `y`: Enable<br>= `n`: Disable |
| CONFIG_DRIVERS_STUB_KEYSTORE | Includes the PX4 stub keystore driver.<br>= `y`: Enable<br>= `n`: Disable |
| CONFIG_PUBLIC_KEY0 | Location of public key for keystore index 0. |
| CONFIG_PUBLIC_KEY1 | Location of public key for keystore index 1.<br>= `{path to key1}` |
| CONFIG_PUBLIC_KEY2 | Location of public key for keystore index 2.<br>= `{path to key2}` |
| CONFIG_PUBLIC_KEY3 | Location of public key for keystore index 3.<br>= `{path to key3}` |

The stub keystore is a keystore implementation that can store up to four keys.
The initial values of these keys are set in the locations defined by `CONFIG_PUBLIC_KEY0` to `CONFIG_PUBLIC_KEY3`.
The keys can be used for different cryptographic purposes, which are determined by parameters.

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.
The _exchange key_, which is the public key used for encrypting the symmetric key stored in the beginning of the `.ulge` file, is specified using [SDLOG_EXCH_KEY](../advanced_config/parameter_reference.md#SDLOG_EXCH_KEY) as an index value into the key store.
The value is `1` by default, which maps to the key defined in `CONFIG_PUBLIC_KEY1`.

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

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)).
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)).
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)).

Build the firmware like this:
Expand Down Expand Up @@ -161,61 +191,53 @@ You can now build and test.
## Download & Decrypt Log Files

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.
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.

The encrypted log file and encrypted symmetric key file are displayed with a timestamp (but no filename) in QGroundControl, as shown below.
You can determine which files are associated based on their timestamps.
Note that the encrypted files will be downloaded with the `.ulg` suffix, instead of `.ulge`.

![QGroundControl ULog Download](../../assets/qgc/analyze/encrypted_log.png)
### Decrypt ULogs

Select and download both files.
Before you can analyze your encrypted logs, you will need to decrypt them.
There is a Python script that can be used to decrypt logs in `Tools/decrypt_ulog.py`.

Note that both files will be downloaded with the `.ulg` suffix.
You can identify the symmetric key file, as it is usually much smaller than the log file (about 300 bytes)
When decrypting a `.ulge` file the script takes 3 arguments:

For convenience in the decryption step, you might rename the file extensions to add back the `.ulgc` (log) and `.ulgk` (key) file extensions.
1. The encrypted log file.
2. An empty string `''`.
3. The decryption key (the RSA2048 `.pem` private key which is used to unwrap the symmetric key).

### Decrypt ULogs
例如:

Before you can analyze your encrypted logs, you will need to decrypt them.
There is a Python script that can be used to decrypt logs in `Tools/decrypt_ulog.py`.
```sh
python3 decrypt_ulog.py \
/home/john/Downloads/log_24_2024-10-6-23-39-50.ulg '' \
new_keys/private_key.pem
```

`decrypt_ulog.py` takes 3 arguments:
On success the decrypted log file is created with the `.ulog` suffix.

1. The encrypted `.ulogc` file.
2. The symmetric key `.ulogk` file.
3. The decryption key (the RSA2048 `.pem` private key which is used to unwrap the `.ulogk` file).
:::info
The script can be used with both `.ulge` logs and the `.ulgc`/`.ulgk` files used in [PX4 v1.15 Log Encryption](https://docs.px4.io/v1.15/en/dev_log/log_encryption.html).
The full command line syntax is given below:

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

CLI tool to decrypt an ulog file

positional arguments:
ulog_file .ulog file
ulog_key .ulogk, encrypted key
ulog_file .ulge/.ulgc, encrypted log file
ulog_key .ulgk, legacy encrypted key (give empty string '' to ignore for .ulge)
rsa_key .pem format key for decrypting the ulog key

optional arguments:
-h, --help show this help message and exit

```

As an example:

```sh
python3 decrypt_ulog.py \
/home/john/Downloads/log_24_2024-10-6-23-39-50.ulgc \
/home/john/Downloads/log_23_2024-10-6-23-39-48.ulgk \
new_keys/private_key.pem
```

On success the decrypted log file is created with the `.ul` suffix instead of `.ulg`.
Rename the file back to `.ulg` and it is now ready for flight review.
:::

## Generate RSA Public & Private Keys

To generate a rsa2048 private and public key, you can use OpenSSL:
To generate a RSA2048 private and public key, you can use OpenSSL:

```sh
openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048
Expand All @@ -226,7 +248,7 @@ Then you can create a public key from this private key:
```sh
# Convert private_key.pem to a DER file
openssl rsa -pubout -in private_key.pem -outform DER -out public_key.der
# From the DER file, generate a public key in hex format, seperated by commas
# From the DER file, generate a public key in hex format, separated by commas
xxd -p public_key.der | tr -d '\n' | sed 's/\(..\)/0x\1, /g' > public_key.pub
```

Expand Down

0 comments on commit a5b9649

Please sign in to comment.