Midbar (STM32F401CCU6 + Arduino Uno Version)
by Northstrix in Circuits > Microcontrollers
1546 Views, 5 Favorites, 0 Comments
Midbar (STM32F401CCU6 + Arduino Uno Version)
Advancements in cryptanalysis and hacking techniques are constantly reducing the cost of accessing your data without your authorization, making it easier and more attractive for different sides to get it.
I'm not going to get into details about the motivation of each side and the goals they're trying to achieve by obtaining your data. Instead, I would like to focus on the solution to that problem.
In my opinion, the only way to keep your data private is to raise the cost of unauthorized access to it as high as possible. Ideally - raise it so high that it would significantly outweigh any potential reward for the third party.
To raise the cost of unauthorized access to your data - I've developed Midbar (which later on "evolved" into a multi-user Cipherbox, and then it kinda turned back into Midbar because I realized that a "multi-user Midbar" is superfluous and not as stable as a single-user one).
Midbar (מדבר) is a Hebrew word that means "pasture," "uninhabited land," "wilderness," "large tracts of wilderness (around cities)," and "desert." When I started this project, I decided to name it Midbar for two reasons. First - while working on my previous projects, I noticed that the so-called "device that keeps your personal data secure in an encrypted form" market is pretty much a "desert around the oasis of the password manager market." Second - I couldn't find a better word to describe this project. Initially, I thought of calling it a "Password Vault," but it's more than that. So, I decided to call it Midbar.
Supplies
- STM32F401CCU6 x1
- 240x320 TFT LCD with ST7789 (with slot for SD card) x1
- Mifare RC522 RFID Reader x1
- RFID cards x4
- Arduino Uno x1
- EC11 Rotary Encoder x1
- PS/2 Keyboard x1
- PS/2 Port x1
- SD Card x1
- 100nf capacitors x2
- Buttons x2
- 4.7k resistors x8
- 1k resistor x1
- 580 ohm resistor x1
- LED x1
How It Came to Be That Way
This version of Midbar is my attempt to combine the best aspects of Midbar V4.0 and Midbar V5.0 while eliminating the RNG problem present in the Midbar (STM32F401CCU6 Version).
While trying to achieve the stated objective, I've stumbled upon two major issues:
1) I still haven't figured out how to use the STM32F401CCU6's random number generator in the Arduino IDE;
2) I haven't figured out how to utilize the third SPI bus on the STM32F401CCU6 (I didn't want to connect two devices to the same SPI bus because that could've made Midbar less stable).
And so, I've added Arduino Uno to the project to eliminate both issues stated above. In this version of Midbar, the Arduino Uno performs the function of a random number generator and serves as a bridge between the RFID card reader and the STM32F401CCU6.
3DES + AES + Blowfish + Serpent Encryption Algorithm in CBC Mode
The "3DES + AES + Blowfish + Serpent" encryption algorithm in cipher block chaining mode first appeared in the Midbar V2.5. And since then has been utilized by the Midbar (Raspberry Pi Pico Version), Midbar V3.0, Midbar V4.0, KhadashPay V2.0, Midbar (Raspberry Pi Pico Version) V2.0, KhadashPay V2.0 (Raspberry Pi Pico Version), Midbar V5.0, Midbar (STM32F401CCU6 Version), KhadashPay V3.0 (STM32F401CCU6 Version), and KhadashPay V3.0.
Although the "3DES + AES + Blowfish + Serpent" encryption algorithm ain't exactly what I would call "a cryptographically weak encryption algorithm," operating it in a weird derivation of the ECB mode, the way it was done by the Midbar V2.0 wasn't the best idea that I had. Even though that wouldn't've allowed the attacker to produce the legitimate ciphertext by swapping the blocks within the ciphertext, an attacker could still make a legitimate ciphertext by replacing the nth block of the ciphertext N1 with the nth block of the ciphertext N2. To fix that vulnerability (instead of just notifying the user that the decrypted ciphertext might've been forged), I made the "3DES + AES + Blowfish + Serpent" encryption algorithm work in CBC mode. So, if an attacker replaces a block of ciphertext, they spoil not just that block but also the subsequent one.
I'll be honest with you, the bit-flipping attack "kinda works," but I doubt that it would ever go unnoticed because of the "HMAC-SHA256"-based integrity verification feature
And let's not forget that this encryption algorithm performs the operation called superencryption.
As defined by NIST, superencryption is an encryption operation for which the plaintext input to be transformed is the ciphertext output of a previous encryption operation.
Such organization of the encryption algorithms makes a combined encryption algorithm that is at least as strong as the strongest one in the cascade, has a longer key, might be more resistant to some attacks, and might produce a ciphertext with higher entropy. Anyway. It won't hurt to have an additional layer of security (or several of them).
Integrity Verification
The Midbar (STM32F401CCU6 + Arduino Uno Version) is the ninth version of Midbar that verifies the integrity of the whole record. So, the legitimate ciphertexts moved between the cells aren't much of a threat to it.
The "HMAC-SHA256"-based integrity verification feature works as follows:
When you add a record to Midbar, it concatenates all the data you've entered into a single string, computes a tag for it, and then stores that tag in the encrypted form.
When Midbar decrypts your data, it also decrypts the previously calculated tag and computes a new tag for the decrypted data. It then compares both tags and if they don't match - Midbar notifies you that the integrity verification failed.
Prepare the Software
Before flashing STM32 Black Pill and Arduino Uno, you need to install STM32CubeProg and set the Arduino IDE up. You can read about all of it here: https://www.sgbotic.com/index.php?dispatch=pages.view&page_id=49
Download Firmware
You can download the firmware for Midbar from one of these sources.
SourceForge: https://sourceforge.net/projects/midbar/
OSDN: https://osdn.net/projects/midbar/
GitHub: https://github.com/Northstrix/Midbar
If you just need the firmware for the device alongside the RNG, then I would advise you to download a 0.2 MB archive either from SourceForge or OSDN.
But if you need the firmware for all versions of Midbar alongside the extra code, photos, and diagrams, in that case, I would advise you to download the 168 MB archive from GitHub.
Download and Install the Libraries
TFT_eSPI: https://github.com/Bodmer/TFT_eSPI
rfid: https://github.com/miguelbalboa/rfid
EncButton: https://github.com/GyverLibs/EncButton
PS2KeyAdvanced: https://github.com/techpaul/PS2KeyAdvanced
PS2KeyMap: https://github.com/techpaul/PS2KeyMap
SdFat: https://github.com/adafruit/SdFat
The process of unpacking libraries is typical. You can unpack the content of the archive into the folder: ...\Arduino\libraries. Or open the Arduino IDE, click to the Sketch -> Include Library -> Add .ZIP Library... and select every archive with libraries.
Other required libraries are already present in one way or another.
Replace the Preset File for the TFT_eSPI Library
TFT_eSPI Library requires the config to be adjusted depending on the display and the board that drives that display. Initially, I wanted to write the mini-guide on adjusting the config for the ST7789 display to be properly used with STM32F401CCU6. But then I realized that it would be more convenient to attach the adjusted config to the firmware and tell you where to place it.
Take the "User_Setup.h" file from the "STM32F401CCU6_and_Arduino_Uno_Version\V1.0" folder and place it in the "C:\Program Files (x86)\Arduino\libraries\TFT_eSPI-master" folder.
Generate Keys
To make the unauthorized deciphering of your data computationally infeasible - It is crucial to generate your own keys and never reuse them
It's entirely up to you how to generate the keys. I can only offer you an option to do so.
I've modified one of my previous projects to work as a random number generator, the generated output seems "random enough" for me, but I haven't run any tests. So, I can't guarantee that it's random.
Use it at your own risk!
To generate the keys - launch gen.exe from the "STM32F401CCU6_and_Arduino_Uno_Version\V1.0\Untested RNG" folder and click the "Generate keys for Midbar" button. The background turns from dark gray to light gray when you press that button.
Modify the Firmware
Open the "Firmware_for_STM32F401CCU6.ino" file from the "STM32F401CCU6_and_Arduino_Uno_Version\V1.0\Firmware_for_STM32F401CCU6" folder, and then replace my keys with those you've generated.
Flash STM32F401CCU6
Set the STM32 Black Pill into the bootloader mode by doing the following:
- Hold the BOOT0 button;
- Press the NRST button;
- Release the BOOT0 button.
Make sure that the settings in the "Tools" tab match those on the screen, especially the following settings:
Board: "Generic STM32F4 series"
Board part number: "BlackPill F401CC"
U(S)ART support: "Enabled (generic 'Serial')"
USB support (if available): "CDC (generic 'Serial' supersede U(S)ART)"
Upload method: "STM32CubeProgrammer (DFU)"
And then, upload firmware from the "STM32F401CCU6_and_Arduino_Uno_Version\V1.0\Firmware_for_STM32F401CCU6" folder into the STM32.
You don't need to select the COM port to flash STM32 Black Pill.
Flash Arduino Uno
Upload firmware from the "STM32F401CCU6_and_Arduino_Uno_Version\V1.0\Firmware_for_Arduino_Uno" folder into the Arduino Uno.
Assemble Midbar
Assembling the Midbar (STM32F401CCU6 + Arduino Uno Version) is a bit harder than assembling Midbar (STM32F401CCU6 Version), but nevertheless, it's not as hard at it might seem.
*Updated: Fixed an error in the circuit diagram.
Power the Midbar Up and Tap Four RFID Cards on the RFID Reader
After being powered up, the Midbar displays the "Midbar STM32F401CCU6" and "Tap RFID card N1" inscriptions alongside the lock screen.
Tap four RFID cards on the RFID reader one after another. The most important thing here is to tap the cards in the same order every time you unlock Midbar. Otherwise, it just won't unlock.
If you don't have four cards, you can tap one card four times.
*Credit for the photo of Dallas:
Photo by Talena Reese: https://www.pexels.com/photo/gray-concrete-building-under-blue-sky-10482416/
Set the Master Password
To use the Midbar, you first need to set the master password.
Remember that you can't change your master password without performing the factory reset first!
Midbar won't be able to decrypt your data without your master password because the keys for the encryption algorithms are partially derived from it. Perhaps, it won't even unlock without the correct master password.
When you're done entering your master password, either quad-click the encoder button or press the "Enter" on the PS/2 keyboard.
After you've unlocked the Midbar and got to the main menu:
- Either turn the rotary encoder to the right or press the "↓" (DOWNWARDS ARROW) key on the PS/2 keyboard to go down the menu.
- Either turn the rotary encoder to the left or press the "↑" (UPWARDS ARROW) key on the PS/2 keyboard to go up the menu.
- Press either the "A" button or the "Enter" key on the PS/2 keyboard to open the selected menu.
- While in the submenu, press either the "B" button or the "Esc" key on the PS/2 keyboard to return to the main menu.
While entering a text in a tab:
- Either quad-click the encoder button four or press "Enter" on the PS/2 keyboard to continue;
- Either quintuple-click the encoder button (click it five times in quick succession) or press the "Esc" button on the PS/2 keyboard to cancel the current operation.
*If the hex value decreases when you rotate the encoder to the right, I would advise you to swap the wires connected to the STM32's A0 and A1 pins.
And by the way, pressing the "Caps Lock," "Num Lock," or "Scroll Lock" buttons makes the keyboard unresponsive until you remove the power from the STM32 and put it back again.
Add Login
The Midbar STM32F401CCU6 + Arduino Uno version utilizes the 3DES + AES + Blowfish + Serpent encryption algorithm in CBC mode with an integrity verification feature alongside the SD card to keep your data safe and organized.
To add a login:
- Select the "Logins" line in the main menu;
- Press either the "A" button or the "Enter" key on the PS/2 keyboard;
- Select the "Add" line;
- Press either the "A" button or the "Enter" key on the PS/2 keyboard;
- Choose the slot you want to put the login to either by rotating the encoder or by pressing the "←" (Leftwards Arrow) and "→" (Rightwards Arrow) keys on the PS/2 keyboard;
- Press either the "A" button or the "Enter" key on the PS/2 keyboard;
- Enter the title;
- Either quad-click the encoder button or press "Enter" on the PS/2 keyboard;
- Enter the username;
- Either quad-click the encoder button or press "Enter" on the PS/2 keyboard;
- Enter the password;
- Either quad-click the encoder button or press "Enter" on the PS/2 keyboard;
- Enter the website;
- Either quad-click the encoder button or press "Enter" on the PS/2 keyboard.
According to the tests I've conducted - STM32F401CCU6 can encrypt and decrypt a 2500-character long string without any problems using the 3DES + AES + Blowfish + Serpent encryption algorithm. So, technically, you can put 2500 characters into a field of a record.
*All credentials demonstrated here are entirely fictitious. Any similarity to actual credentials is purely coincidental.
View Login
To view a login:
- Select the "Logins" line in the main menu;
- Press either the "A" button or the "Enter" key on the PS/2 keyboard;
- Select the "View" line;
- Press either the "A" button or the "Enter" key on the PS/2 keyboard;
- Choose the slot you want to view login from either by rotating the encoder or by pressing the "←" (Leftwards Arrow) and "→" (Rightwards Arrow) keys on the PS/2 keyboard;
- Press either the "A" button or the "Enter" key on the PS/2 keyboard;
- Press either the "Tab" key on the PS/2 keyboard or the encoder button to print the record to the serial terminal.
*All credentials demonstrated here are entirely fictitious. Any similarity to actual credentials is purely coincidental.
Edit Login
To edit a login:
- Select the "Logins" line in the main menu;
- Press either the "A" button or the "Enter" key on the PS/2 keyboard;
- Select the "Edit" line;
- Press either the "A" button or the "Enter" key on the PS/2 keyboard;
- Select the login you would like to edit either by rotating the encoder or by pressing the "←" (Leftwards Arrow) and "→" (Rightwards Arrow) keys on the PS/2 keyboard;
- Press either the "A" button or the "Enter" key on the PS/2 keyboard;
- Enter the new password you'd like to set on the encoder and PS/2 keyboard;
- Press either the "A" button or the "Enter" key on the PS/2 keyboard.
*All credentials demonstrated here are entirely fictitious. Any similarity to actual credentials is purely coincidental.
Delete Login
To delete a login:
- Select the "Logins" line;
- Press either the "A" button or the "Enter" key on the PS/2 keyboard;
- Select the "Delete" line;
- Press either the "A" button or the "Enter" key on the PS/2 keyboard;
- Select the login you would like to delete either by rotating the encoder or by pressing the "←" (Leftwards Arrow) and "→" (Rightwards Arrow) keys on the PS/2 keyboard;
- Press either the "A" button or the "Enter" key on the PS/2 keyboard.
The process of working with records of other types is very similar to the process of working with logins.
Hash String
Let's not forget that Midbar also utilizes two hash functions: SHA-256 and SHA-512.
If you're not familiar with the hash functions, let me give you a simple explanation of what the hash function is.
The hash function is an algorithm that takes your input (in that case, it's a string) and gives you a fixed-length output derived from that input. One of the essential properties of the hash function is that it always produces the same result for the same input. That allows you to use the hash function to verify the data integrity.
To hash a string:
- Select the "Hash Functions" line in the main menu;
- Press either the "A" button or the "Enter" key on the PS/2 keyboard;
- Select the "SHA-256" or "SHA-512" line depending on the hash function you'd like to use;
- Press either the "A" button or the "Enter" key on the PS/2 keyboard;
- Enter the string you'd like to hash;
- Either quad-click the encoder button or press "Enter" on the PS/2 keyboard.
Input:
Midbar
Hash:
0415af21a07eb50be9ce02417f73c1d6b8c30631a5a4240b662ff89e3531fcfa02dfe06a808acacf81b523066f81495dc3004e375f08e71943cad0589807b160
Find a Good Use for Midbar
With all of its flaws and imperfections, this version of Midbar is definitely better than Midbar (STM32F401CCU6 Version). It has a functioning RNG, and just like the Midbar V4.0, it's locked with four rfid cards and a master password. And while it may not be as fast as Midbar V5.0, it's a racing horse compared to KhadashPay V3.0 (STM32F401CCU6 Version) (maybe except for the speed of the encryption part).
Anyway, Midbar's source code is distributed under the MIT license, so if you want to make your own version of the Midbar or change something in it, don't wait for anyone else to do it for you. Take the initiative into your own hands and do it! Don't forget that there's always room for improvement, even if it seems that there's none.
If you like this tutorial, please consider sharing it.
Thank you for reading this tutorial.