Midbar (STM32F401CCU6 Version)

by Northstrix in Circuits > Microcontrollers

1180 Views, 5 Favorites, 0 Comments

Midbar (STM32F401CCU6 Version)

IMG_20230414_141811.jpg

A week ago, I released Midbar V5.0 - the first version of Midbar that utilized the SD card to store user data. And I found it so convenient that I made the STM32F401CCU6 version of it. So, now there's one more version of Midbar that gives you the ability to back up your data without literally hacking the device.


In case you want to know why I called this project Midbar - Midbar (מדבר) is a Hebrew word that means "pasture," "uninhabited land," "wilderness," "large tracts of wilderness (around cities)," "desert." I had two reasons for choosing the word Midbar as the name of this project. 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 that project. At first, I wanted to call it a "Password Vault," but then I realized it's more than just a password vault. So, I just called it Midbar!

You can also read this tutorial on Medium and Hackster.

Supplies

IMG_20230415_162144.jpg
  • STM32F401CCU6 x1
  • 240x320 TFT LCD with ST7789 (with slot for SD card) x1
  • EC11 Rotary Encoder x1
  • PS/2 Keyboard x1
  • PS/2 Port x1
  • SD Card x1
  • 4.7k resistors x7
  • 100nf capacitors x2
  • Buttons x2

3DES + AES + Blowfish + Serpent in CBC Mode

F4FCFX3LG6K5NYP.png

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.0Midbar V4.0KhadashPay V2.0Midbar (Raspberry Pi Pico Version) V2.0KhadashPay V2.0 (Raspberry Pi Pico Version), and Midbar V5.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

FQKYEGBLG6K5NZL.png

The Midbar (STM32F401CCU6 Version) is the eighth 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, 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.25 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 158 MB archive from GitHub.

Download and Install the Libraries

TFT_eSPI: https://github.com/Bodmer/TFT_eSPI

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

F2O86IULG7ZLRRM.png

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_Version\V1.0" folder and place it in the "C:\Program Files (x86)\Arduino\libraries\TFT_eSPI-master" folder.

Generate Keys

Genks.png

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

mf.png

Open the "Firmware.ino" file from the "STM32F401CCU6_Version\V1.0" folder, and then replace my keys with those you've generated.

Flash STM32F401CCU6

flash.png

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_Version\V1.0\Firmware" folder into the STM32.


You don't need to select the COM port to flash STM32 Black Pill.

Assemble Midbar

Midbar Circuit Diagram.png

Assembling the Midbar shouldn't be hard. In my opinion, the most tangled part of the process is to connect the encoder with its periphery the right way.


As for the possible component replacements:

  • You can replace 4.7k resistors with 4.7k - 10k resistors;
  • And you can replace the capacitors with 22nf - 100nf capacitors.

Power the Midbar Up

IMG_20230414_143103.jpg

After powering the Midbar up, you should see the word "מדבר" alongside the "Midbar STM32F401CCU6" and "Press Any Key" inscriptions.

Press any key to get to the next tab.


*Credit for the photo of Denver:

Photo by Acton Crawford on Unsplash

Set the Master Password

IMG_20230414_145232.jpg
IMG_20230414_145316.jpg
IMG_20230414_145349.jpg

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

IMG_20230414_151242.jpg
IMG_20230414_145803.jpg
IMG_20230414_145930.jpg
IMG_20230414_150122.jpg
IMG_20230414_150219.jpg
IMG_20230414_150416.jpg
IMG_20230414_150613.jpg

The Midbar STM32F401CCU6 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 from the encoder and PS/2 keyboard:

  1. Select the "Logins" line in the main menu;
  2. Press either the "A" button or the "Enter" key on the PS/2 keyboard;
  3. Select the "Add" line;
  4. Press either the "A" button or the "Enter" key on the PS/2 keyboard;
  5. 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;
  6. Press either the "A" button or the "Enter" key on the PS/2 keyboard;
  7. Enter the title;
  8. Either quad-click the encoder button or press "Enter" on the PS/2 keyboard;
  9. Enter the username;
  10. Either quad-click the encoder button or press "Enter" on the PS/2 keyboard;
  11. Enter the password;
  12. Either quad-click the encoder button or press "Enter" on the PS/2 keyboard;
  13. Enter the website;
  14. 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

IMG_20230414_151242.jpg
IMG_20230414_151038.jpg
IMG_20230414_151057.jpg
IMG_20230414_151116.jpg
Login printed to the serial terminal.png

To view a login:

  1. Select the "Logins" line in the main menu;
  2. Press either the "A" button or the "Enter" key on the PS/2 keyboard;
  3. Select the "View" line;
  4. Press either the "A" button or the "Enter" key on the PS/2 keyboard;
  5. 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;
  6. Press either the "A" button or the "Enter" key on the PS/2 keyboard;
  7. 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

IMG_20230414_151242.jpg
IMG_20230414_151405.jpg
IMG_20230414_151447.jpg
IMG_20230414_151505.jpg
IMG_20230414_151915.jpg
IMG_20230414_152014.jpg

To edit a login:

  1. Select the "Logins" line in the main menu;
  2. Press either the "A" button or the "Enter" key on the PS/2 keyboard;
  3. Select the "Edit" line;
  4. Press either the "A" button or the "Enter" key on the PS/2 keyboard;
  5. 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;
  6. Press either the "A" button or the "Enter" key on the PS/2 keyboard;
  7. Enter the new password you'd like to set on the encoder and PS/2 keyboard;
  8. 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

IMG_20230414_151242.jpg
IMG_20230414_152316.jpg
IMG_20230414_152442.jpg

To delete a login:

  1. Select the "Logins" line;
  2. Press either the "A" button or the "Enter" key on the PS/2 keyboard;
  3. Select the "Delete" line;
  4. Press either the "A" button or the "Enter" key on the PS/2 keyboard;
  5. 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;
  6. 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

IMG_20230414_152933.jpg
IMG_20230414_152953.jpg
IMG_20230414_153024.jpg
IMG_20230414_153043.jpg

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

IMG_20230414_144903.jpg

Now that you've finished reading about the capabilities and the upsides of the Midbar (STM32F401CCU6 Version) - let me tell you about some known flaws that this version of Midbar has. First of all, it's slower than the Midbar V5.0. It doesn't enable you to use it as a data encrypter and decrypter the way, for example, the Midbar (Raspberry Pi Pico Version) or Midbar (Raspberry Pi Pico Version) V2.0 do. And, of course, "the icing on the cake" (quite frankly, I don't even know how much of a security breach it actually is), I hadn't figured out how to use the STM32F401CCU6's random number generator in the Arduino IDE and used the Arduino's "random()" function to generate the random numbers. So, use the Midbar (STM32F401CCU6 Version) and any other version of Midbar at your own risk. 

By the way, let me remind you that the Midbar project isn't there to give you a 100% guarantee for something. It's not about that, and it never was. It's about raising the cost of unauthorized access to your data as high as possible!

If you want to make your own version of the Midbar or change something in it, please do so! Don't forget that there's always room for improvement, even if it seems that there's none. Ok, that's it for this tutorial.

If you like this tutorial, please share it.

And if you have any Midbar-related questions, don't hesitate to ask them in the comment section.

Thank you for reading this tutorial.