Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tamper Detection #471

Merged
merged 73 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 72 commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
3aa202f
add pin and anti-tamper tool
odudex Sep 11, 2024
ae52c83
add colors flash snapshot
odudex Sep 11, 2024
0e6206c
stretch PIN with PBKDFD2
odudex Sep 12, 2024
17260c5
pin setting UI adjustments
odudex Sep 12, 2024
da3c692
add setting: Flash Hash at Boot
odudex Sep 12, 2024
e350940
rename flash snapshot to flash hash
odudex Sep 12, 2024
9eb52d5
flash hash feature: update doc and fix tests
odudex Sep 13, 2024
31432c3
set DIGITS order on keypads as it is done in phones
odudex Sep 13, 2024
26bfe8a
Flash Hash: warning and plans
odudex Sep 14, 2024
f493ddf
Merge branch 'system_optimizations' into pin_and_anti_tamper
odudex Sep 19, 2024
43b837b
fix tests for altered DIGITS keypad
odudex Sep 19, 2024
5d2dcd6
Merge branch 'system_optimizations' into pin_and_anti_tamper
odudex Sep 20, 2024
231f2f7
update version to 24.09.beta15
odudex Sep 20, 2024
2a8e442
Merge branch 'system_optimizations' into pin_and_anti_tamper
odudex Sep 23, 2024
942f0bc
Merge branch 'develop' into pin_and_anti_tamper
odudex Sep 30, 2024
9d28280
add option to fill flash with random entropy
odudex Sep 30, 2024
ac54d00
add flash tools
odudex Oct 1, 2024
c95a8b8
use camera's default sensitivity for entropy capture
odudex Oct 1, 2024
745a154
fill_flash code refactor
odudex Oct 2, 2024
20ac026
refactor flash_tools
odudex Oct 2, 2024
dcb57a5
simplified flash map with legends
odudex Oct 7, 2024
be2f222
index indicator for multi set keypad
odudex Oct 7, 2024
1fade01
fix tests and typos
odudex Oct 7, 2024
ecfa2ae
Anti-tamper: rename PIN to Integrity Code
odudex Oct 7, 2024
65eb6e7
rename "Wipe Device" to "Erase User's Data"
odudex Oct 8, 2024
8b26292
i18n: add option to fill missing translations to file
odudex Oct 8, 2024
5fdb083
fit 2 line tittles on keypad capture
odudex Oct 8, 2024
91f194d
tamper detection: rename "integrity code" to "tamper check code"
odudex Oct 8, 2024
9ba2110
tamper detection: add base translations
odudex Oct 8, 2024
b97e4f9
tamper detection: update doc
odudex Oct 9, 2024
7010c06
tamper detection docs: add SD card risks
odudex Oct 9, 2024
ff4024b
camera and binary grid refactor
odudex Oct 10, 2024
2c69abf
Binary grid scan: Otsu on blob detection and more tweaks
odudex Oct 11, 2024
5027874
fix camera and binary grid tests
odudex Oct 11, 2024
c9bf70e
autocomplete partial text mnemonics
odudex Oct 11, 2024
82272cc
rename Flash Hash - TC Flash Hash
odudex Oct 14, 2024
662b901
BIP85: Add option for base64 password
odudex Oct 14, 2024
263ddb7
allow export BIP85 passwords to SD and as QR code
odudex Oct 15, 2024
0b161d1
update BIP85 translations
odudex Oct 15, 2024
f1853c3
BIP85: fix tests
odudex Oct 15, 2024
417a3f6
Merge branch 'develop' into pin_and_anti_tamper
odudex Oct 15, 2024
a39ccc4
BIP85 passwords: add tests
odudex Oct 16, 2024
dc2a364
TC Flash Hash bugfix: flush input events after processing
odudex Oct 17, 2024
2340754
Merge branch 'develop' into pin_and_anti_tamper
odudex Oct 17, 2024
461978d
tamper detection: add tests
odudex Oct 17, 2024
23eb22a
change uPython working dir to flash and prevent loading modules from SD
odudex Oct 18, 2024
a270a6a
TC Code setting: Add tests
odudex Oct 18, 2024
f59a2ef
TC Code: add flash entropy fill tests
odudex Oct 19, 2024
8de3fdf
revert chdir change in favor of a Maixpy fix
odudex Oct 21, 2024
4e89600
bugfix: better define mnemonic type
odudex Oct 21, 2024
85a8692
add autocomplete mnemonic words and image render tests
odudex Oct 21, 2024
7c88d5f
update Maixpy: don't change working dir to SD
odudex Oct 21, 2024
4f45414
update Maixpy: don't change working dir to SD
odudex Oct 21, 2024
8d980fc
add CompactSeedQR tests
odudex Oct 21, 2024
28e8871
docs: add Krux ethos
odudex Oct 21, 2024
933825c
Merge branch 'develop' into pin_and_anti_tamper
odudex Oct 21, 2024
5128c74
Japanese: add TC code and flash hash translations
odudex Oct 21, 2024
ab1d40c
Japanese: add TC code and flash hash translations II
odudex Oct 21, 2024
15a43dd
add execution control to upython vfs mount points
odudex Oct 22, 2024
7d67e6b
adjust Japanese glyphs
odudex Oct 23, 2024
290cddc
BUGFIX: BIP85 passwords were getting wrong derivation
odudex Oct 24, 2024
ecf9342
BIP85 passwords: add spec test case
odudex Oct 24, 2024
2fbe698
Merge branch 'develop' into pin_and_anti_tamper
odudex Oct 25, 2024
9ab895d
refactor touch index calculation
odudex Oct 25, 2024
ceb658b
Docs: Add backup templates
odudex Oct 26, 2024
2e8ae38
Docs: Fix links of backup templates
odudex Oct 26, 2024
de8c941
Docs: Fix icon scale of backup templates
odudex Oct 26, 2024
f49e2bf
Update templates.en.md
odudex Oct 26, 2024
de1a303
Merge branch 'develop' into pin_and_anti_tamper
odudex Oct 29, 2024
b16ff03
Docs: Add TC Code and TC Flash Hash
odudex Oct 29, 2024
f0f735c
Tamper detection docs: Fix broken image link
odudex Oct 30, 2024
4c91316
Tag Tamper Check as experimental
odudex Oct 30, 2024
722f808
Tamper detection: Bugfixes for edge cases caught by @jdlcdl
odudex Oct 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions docs/getting-started/features/tamper-detection.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Tamper Detection Mechanism (Experimental)
Krux's tamper detection tool combines cryptographic hashes, a *Tamper Check Code*, and camera-generated entropy to create a tamper indicator that is unique to each device, represented by a memorable image and four words.

Before we get into details, let's start with some limitations and necessary prerequisites to allow the feature to work.

## Krux Security Model - Good Practices and Limitations

To secure your Krux device, always verify firmware authenticity before installation, particularly when flashing via USB.

### Firmware Verification Methods

- **Using OpenSSL Command-Line Tool:** Follow the Krux documentation to verify the firmware's signature manually. This method provides a high level of assurance but requires familiarity with command-line operations.

- **Using Krux-Installer:** For a more user-friendly experience, verify the Krux-Installer's signature using GPG. Graphical interfaces like Sparrow Wallet can facilitate this process. Krux-Installer automatically downloads the firmware from Github, verifies the firmware signature, informs you of the results, and guides you through manual verification if desired.

### Recommendations

- **Learn Verification Tools:** Familiarize yourself with verification methods and tools to maintain control over your device's security.

- **Build from Source:** Consider building the firmware from source code and verifying its reproducibility for maximum assurance.

- **Use SD Card for Updates:** After the initial flash through USB, perform subsequent updates via the SD card. This keeps your device air-gapped and allows the existing firmware to verify new updates before installation.

Note: The effectiveness of the *TC Flash Hash* tamper detection feature relies on running legitimate, uncompromised firmware and safely protecting your *Tamper Check Code*.

## Tamper Check Code (TC Code)
A *Tamper Check Code*, composed of numbers, letters and special characters, with a minimum length of six characters, can be stored and required to execute *Tamper Check (TC) Flash Hash* tamper verification before Krux boots at the main application, or optionally as a feature available in `Tools -> Flash Tools`.

Before being stored in the device’s flash, the *TC Code* is hashed together with the K210 chip’s unique ID and stretched using PBKDF2. This ensures the *TC Code* is not retrievable via a flash dump and can only be brute-forced outside the device if the attacker also has access to the device’s unique ID. By allowing letters, special characters, and running 100k iterations of PBKDF2, brute-forcing the *TC Code* from dumped data becomes more time-consuming and resource-intensive.

### Enhancing Tamper Detection
After setting the *TC Code*, you are prompted to fill empty flash memory blocks with random entropy from the camera. This process ensures that attackers cannot exploit unused memory space.

## Tamper Check (TC) Flash Hash - A Tamper Detection Tool
### Introduction

The "TC Flash Hash" tool enables you to verify if the flash memory content has been altered.

### How It Works

The tool generates a unique image and four tamper detection words based on a hash of your *TC Code*, the device's UID, and the flash content. The flash memory is divided into two regions:

- **Firmware Region:** Generates the image and the first two words.

- **User's Region:** Generates the last two words.

<div style="text-align: center;">
<img src="../../img/flash_hash.bmp" alt="TC Flash Hash" width="200"/>
</div>

*Example: The blue symbol and words 'tail monkey' represent the firmware region, while 'wrestle over' reflects the user region.*

Any change in the flash content results in a different image or words:

- **Firmware Changes:** Alterations in the firmware region, including the bootloader, change the image and the first two words.

- **User's Data Changes:** Modifications in the user's region, such as new settings or stored mnemonics, change the last two words.

- ***TC Code* Changes:** Replacing the *TC Code* alters the image and all four words.

### Filling Empty Flash Blocks

Krux performs a memory sweep while simultaneously capturing a live feed from the camera. Whenever an empty block is found in the flash memory, Krux estimates the image's entropy by evaluating its color variance. Krux waits until minimum threshold is met, then uses the data from the image to fill these empty spaces with rich, random entropy.

### Ensuring Tamper Detection

The TC Flash Hash function securely hashes the combination of the *TC Code*, UID, and flash content:

`hash(TC Code,UID,Flash content)` -> Image + Words

Hash properties ensure that without knowing the *TC Code*, UID, and flash content, an attacker cannot reproduce the TC Flash Hash results.

## Executing *TC Flash Hash*

After setting a *TC Code* user can use the *TC Flash Hash* feature, available in `Tools -> Flash Tools -> TC Flash Hash`.

By navigating to `Settings -> Security -> TC Flash Hash at Boot`, you can set Krux to always require *TC Flash Hash* verification after device is turned on.

If a wrong *TC Code* is typed at boot, the device will turn off. As storing code typing attempts count on flash would change its contents, there will be no consequences if wrong *TC Code* is typed multiple times.

As *TC Code* verification data is stored in the user's region of memory, *TC Flash Hash* and *TC Code* requirement is disabled if the user wipes the device. Flashing an older firmware version will also disable the feature.

## Potential Attack Scenarios and Their Mitigation
### Challenge for an Attacker

An attacker faces major challenges in replacing the firmware:

- **Lack of Original Flash Data:** Without the exact original flash content, the attacker cannot reproduce the correct hash.

- **Sequential Hash Dependency:** The hash function processes data sequentially (*TC Code*, UID, flash content), preventing the attacker from injecting or rearranging data to produce the same hash.

- **One-Way Hash Functions:** Cryptographic hash functions like SHA-256 are one-way, making it infeasible to reverse-engineer or manipulate the hash without the original inputs.

### Why Tampered Firmware Cannot Bypass Verification

- **Cannot Reconstruct the Hash:** Without the original flash data, the attacker cannot generate the correct hash, even if they know the UID and *TC Code* after the user enters it.

- **Hash Sensitivity:** Any alteration in the flash content changes the hash output, which will be evident through a different image or tamper detection words.

- **Entropy Filling:** Filling empty flash blocks with camera-generated entropy leaves no space for malicious code and any changes to these blocks will alter the hash.

### Possible Attack Strategies and Failures

- **Precomputing Hashes:** The attacker cannot precompute the correct hash without the *TC Code*, UID, and exact flash content.

- **Storing Hashes:** Storing `hash(flash content)` is ineffective because the overall hash depends on the sequential combination of *TC Code*, UID, and flash data.

- **Inserting Malicious Code:** Attempting to insert code into empty spaces fails because the entropy filling process and hash verification will detect any changes.

- **Using an SD Card to Store a Copy of Original Flash Content:** An attacker could extract an exact copy of the flash contents to an SD card and subsequently install malicious firmware. This firmware could capture the chip's UID and the user's TC Code, then hash the content of the SD card instead of the flash memory. Although this would make the verification process slower, it introduces a potential security risk. To mitigate this vulnerability, it is advisable to avoid performing verifications while an SD card is inserted.

## Conclusion

The *TC Flash Hash* tool significantly enhances security by making it infeasible for attackers to tamper with the firmware without detection. By combining *TC Code* hashing, filling empty memory with random entropy, and verifying flash integrity through unique images and words, Krux significantly enhances the detection of any tamper attempts.

Note: The strength of this defense strategy depends on maintaining a strong, confidential *TC Code* and following secure practices when unlocking the device.
50 changes: 40 additions & 10 deletions docs/getting-started/features/tools.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,52 @@ Enter text to create, print or transcribe a QR code that can later be used as an

Verify if an address or list of addresses belong to a wallet without needing to load private keys. Simply load a trusted wallet descriptor from a QR code or SD card.

### Remove Mnemonic
<img src="../../../img/maixpy_m5stickv/load-mnemonic-storage-options-125.png" align="right">
<img src="../../../img/maixpy_amigo/load-mnemonic-storage-options-150.png" align="right">
<div style="clear: both"></div>

This option allows you to remove any stored encrypted mnemonic from the device's internal memory or an SD card. For more information, see [Krux Encrypted Mnemonics](./encrypted-mnemonics.md).
### Flash Tools
Tools to inspect the content of device's flash memory and clear user's area

When mnemonics are removed from the device's flash memory, Krux will no longer be able to access them. However, as with most operating systems, the data may still be recoverable using specialized tools. If you stored any important keys with a weak encryption key, it is recommended to use the "Wipe Device" feature below to ensure that the data is irrecoverable.
<img src="../../../img/maixpy_m5stickv/flash-tools-125.png" align="right">
<img src="../../../img/maixpy_amigo/flash-tools-150.png" align="right">

When mnemonics are removed from an SD card, Krux will overwrite the region where the encrypted mnemonic was stored with empty data. This makes it more secure to delete mnemonics from SD cards using Krux rather than a PC or another device. However, Krux does not have a "Wipe" feature for SD cards; you can find this feature in third-party applications.
<div style="clear: both"></div>

#### Flash Map
Flash map indicates which memory blocks (4086 Bytes each) are empty. Memory is separated in two regions: Firmware and User's Data. White or colored blocks contain data, while grey blocks are empty.

This is an interesting tool to visualize the effects of filling the memory with ramdom entropy, what is done during the setup of a new `TC Code`, used with `TC Flash Hash` tool, described below.

<img src="../../../img/maixpy_m5stickv/flash-map-125.png" align="right">
<img src="../../../img/maixpy_amigo/flash-map-150.png" align="right">

<div style="clear: both"></div>

### Wipe Device
<img src="../../../img/maixpy_m5stickv/wipe-device-125.png" align="right">
<img src="../../../img/maixpy_amigo/wipe-device-150.png" align="right">
#### TC Flash Hash
*Tamper Check Flash Hash* is a tamper detection mechanism that enables you to verify if the flash memory content has been altered. To use it first, need to create a `TC Code` on `Settings -> Security -> Tamper Check Code`.
*TC Flash Hash* will hash this code, K210 chip's unique ID and the content of the whole flash memory together and produce an image.
The tool generates a unique image and four tamper detection words based on a hash of your *TC Code*, the device's UID, and the flash content. The flash memory is divided into two regions:

- **Firmware Region:** Generates the image and the first two words.

- **User's Region:** Generates the last two words.

This option permanently removes all stored encrypted mnemonics and settings from the device's internal flash memory. It ensures that the data is irrecoverable, making it an adequate measure to take if any important mnemonics were stored with a weak encryption key.
Learn more about *Tamper Check Flash Hash* on [Tamper Detection](tamper-detection.md)

#### Erase User's Data
<img src="../../../img/maixpy_m5stickv/erase-data-125.png" align="right">
<img src="../../../img/maixpy_amigo/erase-data-150.png" align="right">

This option permanently removes all stored encrypted mnemonics, settings and `TC Code` from the device's internal flash memory. It ensures that the data is irrecoverable, making it an adequate measure to take if any important mnemonics were stored with a weak encryption key.

<div style="clear: both"></div>


### Remove Mnemonic
<img src="../../../img/maixpy_m5stickv/load-mnemonic-storage-options-125.png" align="right">
<img src="../../../img/maixpy_amigo/load-mnemonic-storage-options-150.png" align="right">

This option allows you to remove any stored encrypted mnemonic from the device's internal memory or an SD card. For more information, see [Krux Encrypted Mnemonics](./encrypted-mnemonics.md).

When mnemonics are removed from the device's flash memory, Krux will no longer be able to access them. However, as with most operating systems, the data may still be recoverable using specialized tools. If you stored any important keys with a weak encryption key, it is recommended to use the "Wipe Device" feature below to ensure that the data is irrecoverable.

When mnemonics are removed from an SD card, Krux will overwrite the region where the encrypted mnemonic was stored with empty data. This makes it more secure to delete mnemonics from SD cards using Krux rather than a PC or another device. However, Krux does not have a "Wipe" feature for SD cards; you can find this feature in third-party applications.
12 changes: 12 additions & 0 deletions docs/getting-started/settings.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,21 @@ Set the time it takes for Krux to automatically shut down. This feature not only

Please note that devices without batteries and power management will not shut down but will reboot instead, which is sufficient to unload private keys.

#### TC Flash Hash at Boot
Chose if you would like to run [Tamper Check Flash Hash](features/tamper-detection.en.md) every time the device is powered on.

Activating *TC Flash Hash* at boot helps prevent unauthorized use by requiring the *TC Code*. But is important to note, unlike a PIN, the TC Code does not provide access control over USB. This means that the device's memory remains accessible for reading and writing via USB, allowing it to be flashed with firmware that does not require the *TC Code*, which could then allow unauthorized use through its human interface.

#### Hide Mnemonics
When "Hide Mnemonics" mode is set to "True", your device will not display private key data or backup tools when a key is loaded. It will only show public key information and allow signing operations.

#### Tamper Check Code
Create or modify a Tamper Check Code. This code will be required every time [Tamper Check Flash Hash](features/tamper-detection.en.md) is executed.

After creating the code, you will be prompted to fill the empty memory spaces with random entropy from the camera. This step is important to make *TC Flash Hash* more resilient to data manipulation by eliminating empty memory spaces that could be exploited in a sophisticated tamper attempt.

The *TC Code* will be deleted if the device is wiped or user data is erased, which will consequently disable *TC Flash Hash*.

### Appearance
<img src="../../img/maixpy_m5stickv/settings-options-appearance-125.png" align="right">
<img src="../../img/maixpy_amigo/settings-options-appearance-150.png" align="right">
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading