Introduction: Midbar V6.0

About: Hi, my name is Max. I'm a Computer Science student. I like making convenient devices. I know that nothing is perfect, but some things are far better than the others, for that reason I'm providing all of my pro…

Advancements in cryptanalysis and hacking techniques are continuously reducing the cost of accessing your data without your authorization, making it easier and more attractive for different sides to get it.

And while it's good to have a tool like Midbar that can significantly increase the cost of unauthorized access to your data, it's even better to have a convenient tool, that can do so. One of the inconveniences present in Midbar is that only one version of Midbar (Midbar (Teensy 4.1 Version) V2.0) can type your credentials as if it were a USB keyboard. Don't get me wrong, the Midbar (Teensy 4.1 Version) V2.0 is not fundamentally flawed or anything like that. It just so happened that the Teensy 4.1 board costs roughly $40. So, my solution to the "cost issue" is to replace a $40 board with two boards that you can buy for under $5 each.

As for the description of the Midbar project itself:

Midbar is a hardware data vault. Unlike hardware authentication devices, it can store your login credentials, credit card information, notes, and phone numbers. Midbar encrypts your data and requires a master password and, in some cases, four additional RFID cards to access it. With Midbar, you don't have to worry about memorizing the login credentials and credit card information for the services you use. It does that for you.

Compared to software vaults, Midbar provides more security because it does not have thousands of other processes running alongside it, significantly contributing to making it almost invulnerable to side-channel attacks. Additionally, Midbar utilizes sophisticated integrity verification and superencryption features.

The integrity verification feature ensures that any corruption or unauthorized modification of your data will not go unnoticed. Superencryption is a safeguard that would be useful in case a highly improbable black swan event renders one of the major encryption algorithms (AES or Serpent) insecure. Even after such an event, your data would still be encrypted with one secure and two "somewhat semi-secure" encryption algorithms, providing an extra layer of protection.

As for why this project is called Midbar - Midbar (מדבר) is a Hebrew word that means "pasture," "uninhabited land," "wilderness," "large tracts of wilderness (around cities)," and "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 this project. At first, I wanted to call it a "Password Vault," but it's more than that. So, I decided to call it Midbar.

You can also read this tutorial on Medium, Hackster, and Maker Pro.

Supplies

Supplies for the vault:

  • ESP32 x1
  • 2.4 Inch TFT LCD with ILI9341 x1
  • PS/2 Keyboard x1
  • PS/2 Port x1
  • STM32F103C8T6 x1


Supplies for the receiver:

  • ESP32 x1
  • 1.77 Inch TFT LCD with ST7735 x1
  • 4x4 Keypad x1


10µF capacitor x1 *optional

Step 1: 3DES + AES + Blowfish + Serpent 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.0Midbar V4.0KhadashPay V2.0Midbar (Raspberry Pi Pico Version) V2.0KhadashPay V2.0 (Raspberry Pi Pico Version)Midbar V5.0Midbar (STM32F401CCU6 Version)KhadashPay V3.0 (STM32F401CCU6 Version)KhadashPay V3.0Midbar (STM32F401CCU6 + Arduino Uno Version)KhadashPay V3.5Black Swan V2.0Midbar (Teensy 4.1 Version)Hash LatchMidbar (Teensy 4.1 Version) V2.0Midbar (ESP8266 Version) V2.0Midbar (STM32F407VET6 Version)Midbar (STM32F407VET6 + Arduino Uno Version)DIY IoT Data Vault With Google Firebase (Midbar Firebase Edition V1.0)DIY IoT Payment System With Google Firebase (KhadashPay Firebase Edition V1.0), and Midbar (STM32F407VET6 Version) V2.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).

Step 2: Integrity Verification

The Midbar V6.0 is the seventeenth 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 integrity verification feature of Midbar is based on "HMAC-SHA256." When you enter data into Midbar, it consolidates all the data into a single string, computes a tag for that string, and saves the newly computed tag in encrypted form. When you decrypt your data, Midbar also decrypts the previously saved tag and computes a new one for the decrypted data. It then compares both tags. If they don't match - Midbar notifies you that the integrity verification failed.

Step 3: Download Firmware

You can download the firmware for Midbar from one of these sources.

SourceForge: https://sourceforge.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 the archive that weighs roughly 9 Mb from SourceForge.

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 310 MB archive from GitHub.

Step 4: Download and Install the Libraries

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

PS2KeyAdvanced: https://github.com/techpaul/PS2KeyAdvanced

PS2KeyMap: https://github.com/techpaul/PS2KeyMap

Adafruit-GFX-Library: https://github.com/adafruit/Adafruit-GFX-Library

Adafruit_BusIO: https://github.com/adafruit/Adafruit_BusIO

Adafruit-ST7735-Library: https://github.com/adafruit/Adafruit-ST7735-Library

Keypad: https://github.com/Chris--A/Keypad

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.

Step 5: 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 ILI9341 display to be properly used on the ESP32's HSPI. 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 "ESP32_Version\V6.0" folder and place it in the "C:\Program Files (x86)\Arduino\libraries\TFT_eSPI-master" folder.

Step 6: Install ESP32 Filesystem Uploader

The primary purpose of the ESP32 Filesystem Uploader is to let you upload files into ESP's filesystem. In this tutorial, the purpose of this tool is to upload an empty SPIFFS image into ESP.

Download the file called ESP32FS-1.0.zip from https://github.com/me-no-dev/arduino-esp32fs-plugin/releases/

And then extract the content of the archive into the "...\Arduino\Tools\" folder.

After that, restart the Arduino IDE.

Step 7: Switch the Partition Scheme to the "Huge APP (3MB No OTA/1MB SPIFFS)"

You have to switch the partition scheme to the "Huge APP (3MB No OTA/1MB SPIFFS)" before you continue working with ESP32 because the firmware for the vault is too big for the default partition.

Step 8: Format Vault'S Built-In Flash Memory

Connect the ESP32 that you're going to use a core of the vault to the computer. Click Tools -> ESP32 Sketch Data Upload. Then click Yes in the pop-up window. The program is going to format the built-in flash memory.

Some boards will flash without any problems.

Unfortunately, that's not the case for all boards. If you configured IDE correctly, installed drivers, selected the corresponding port, and still keep getting this error: A fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet header. Connect a 10µF capacitor to the board while flashing.

Connect the positive lead of the capacitor to the EN pin of the ESP32;

Connect the negative lead of the capacitor (usually indicated by the gray stripe) to the GND pin of the ESP32.

Don't forget to disconnect the capacitor after the board flashes.

Step 9: 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 "ESP32_Version\V6.0\Untested RNG" folder and click the "Generate keys for Midbar V6.0" button. The background turns from dark gray to light gray when you press that button.

Step 10: Get the Receiver's MAC Address

To get the receiver's MAC address, upload this code into the receiver board.

#include <WiFi.h>

void setup(){
Serial.begin(115200);
Serial.println();
Serial.println(WiFi.macAddress());
}

void loop(){

}

Then open the Serial Terminal, and reboot the board.

If done correctly, you should see the MAC address in the console.

The MAC address of this board is 94:E6:86:37:FF:D8

Step 11: Modify the Firmware

First, open the "Firmware_for_ESP32.ino" file from the "ESP32_Version\V6.0\Firmware_for_vault\Firmware_for_ESP32" folder and replace my keys with those you've generated as well as the receiver MAC address in the following line:

uint8_t broadcastAddress[] = {0x94, 0xE6, 0x86, 0x37, 0xFF, 0xD8}; // Receiver's MAC address

You can also tweak some parameters in the following lines:

#define MAX_NUM_OF_RECS 500

uint16_t colors[5] = { // Purple, Yellow, Green, Shade of Blue N1, Shade of Blue N2

  0xb81c, 0xfde0, 0x87a0, 0x041c, 0x051b

};

const uint16_t current_inact_clr = colors[1];

const uint16_t five_six_five_red_color = 0xf940;

bool display_moving_background = true;

bool display_pattern_while_adding_and_printing_record = true;


When you're done modifying firmware for the vault, open the "Firmware_for_receiver.ino" file from the "ESP32_Version\V6.0\Firmware_for_receiver" and replace the values of hmackey_for_session_key, projection_keyproj_serp_key with the ones you've generated.

Step 12: Flash ESPs

Upload the modified firmware from the "ESP32_Version\V6.0\Firmware_for_vault\Firmware_for_ESP32" and "ESP32_Version\V6.0\Firmware_for_receiver" folders into both ESPs.

Step 13: Flash STM32

If you want Midbar to emulate the USB keyboard, you'll need to upload the firmware from the "ESP32_Version\V6.0\Firmware_for_vault\Firmware_for_STM32F103C8T6" folder into STM32F103C8T6.

The process of setting up the Arduino IDE and flashing the STM32F103C8T6 is quite complex and relatively time-consuming.

Here's the link to the Google Translate version of the helpful tutorial that explains how to properly flash STM32F103C8T6: https://habr-com.translate.goog/ru/articles/395577/?_x_tr_sl=auto&_x_tr_tl=en&_x_tr_hl=en-US&_x_tr_pto=wapp&_x_tr_hist=true

You can find the original version of the tutorial at https://habr.com/ru/articles/395577/

Step 14: Assemble the Vault

Assembling the circuit shouldn't be hard, simply connect all required components to the ESP32 that you've decided to use as a core of the vault. When it comes to the "BL" pin of the ILI9341 display, different versions of the display have different requirements on what to do with it. Some versions specify that it must be connected to the +3.3V, some require it to be grounded, and others allow you to leave it unconnected.

Step 15: Assemble the Receiver

That should be even easier than the vault assembly. Simply connect the display and keypad to the ESP32 that you've chosen as a core of the receiver

Step 16: Power the Midbar Up

Midbar V6.0 has fifteen lock screens. It randomly chooses one at startup. Once the lock screen is chosen, the word "מדבר" with the shifting background is displayed alongside the "Midbar ESP32 V6.0" and "Press Any Key" inscriptions.

If you want the Midbar to emulate a USB keyboard, connect a data cable to the STM32.

*Credit for the photos:

Atlanta:

Photo by Kelly from Pexels

Buildings:

Photo by Francesco Ungaro from Pexels

Dallas:

Photo by R K on Unsplash

Doha:

Photo by Visit Qatar on Unsplash

Haifa:

Image by Volker Glätsch from Pixabay

Jakarta:

Photo by Afif Ramdhasuma from Pexels

Jerusalem:

Image by krystianwin from Pixabay

London:

Photo by Mike Stezycki on Unsplash

Los Angeles:

Image by herdzmedia from Pixabay

Miami:

Photo by Alejandro Luengo on Unsplash

Milan:

Photo by Samuel Agbetunsin on Unsplash

Pittsburgh:

Photo by Yuhan Du on Unsplash

Riyadh:

Image by apriltan18 from Pixabay

Singapore:

Photo by Jay Ang on Unsplash

Tel Aviv:

Photo by Shai Pal on Unsplash

Step 17: Set Master Password

To use the Midbar, you first need to set the master password.

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, press either the "Enter" or the "ESC" key on the PS/2 keyboard.

After you've unlocked the vault and got to the main menu:

  • Press the "" (DOWNWARDS ARROW) key on the PS/2 keyboard to go down the menu.
  • Press the "" (UPWARDS ARROW) key on the PS/2 keyboard to go up the menu.
  • Press either the "Enter" key on the PS/2 keyboard to open the selected menu.
  • While in the submenu, press either the "Esc" or the "Backspace" key on the PS/2 keyboard to return to the main menu.


While entering a text in a tab:

  • Press "Enter" on the PS/2 keyboard to continue.
  • Press the "Esc" button on the PS/2 keyboard to cancel the current operation.

Step 18: Add Login

Midbar V6.0, just like several other versions that came before it, utilizes the 3DES + AES + Blowfish + Serpent encryption algorithm in CBC mode with an integrity verification feature to encrypt your data. Encrypted data is stored in the ESP32's built-in flash memory.


To add a login:

  1. Select the "Logins" line in the main menu;
  2. Press the "Enter" key on the PS/2 keyboard;
  3. Select the "Add" line;
  4. Press the "Enter" key on the PS/2 keyboard;
  5. Choose the slot you want to put the login to by pressing the "←" (Leftwards Arrow) and "→" (Rightwards Arrow) keys on the PS/2 keyboard;
  6. Press the "Enter" key on the PS/2 keyboard;
  7. Enter the title;
  8. Press "Enter" on the PS/2 keyboard;
  9. Enter the username;
  10. Press "Enter" on the PS/2 keyboard;
  11. Enter the password;
  12. Press "Enter" on the PS/2 keyboard;
  13. Enter the website;
  14. Press "Enter" on the PS/2 keyboard.


*All credentials demonstrated here are entirely fictitious. Any similarity to actual credentials is purely coincidental.

Step 19: View Login

To view a login:

  1. Select the "Logins" line in the main menu;
  2. Press the "Enter" key on the PS/2 keyboard;
  3. Select the "View" line;
  4. Press the "Enter" key on the PS/2 keyboard;
  5. Choose the slot from which you want to view the login by pressing the "←" (Leftwards Arrow) and "→" (Rightwards Arrow) keys on the PS/2 keyboard;
  6. Press the "Enter" key on the PS/2 keyboard.


*All credentials demonstrated here are entirely fictitious. Any similarity to actual credentials is purely coincidental.

Step 20: Edit Login

To edit a login:

  1. Select the "Logins" line in the main menu;
  2. Press the "Enter" key on the PS/2 keyboard;
  3. Select the "Edit" line;
  4. Press the "Enter" key on the PS/2 keyboard;
  5. Select the login you would like to edit by pressing the "←" (Leftwards Arrow) and "→" (Rightwards Arrow) keys on the PS/2 keyboard;
  6. Press the "Enter" key on the PS/2 keyboard;
  7. Enter the new password;
  8. Press "Enter" on the PS/2 keyboard.


*All credentials demonstrated here are entirely fictitious. Any similarity to actual credentials is purely coincidental.

Step 21: Type Login

To type a login:

  1. Select the "Logins" line in the main menu;
  2. Press the "Enter" key on the PS/2 keyboard;
  3. Select the "Type" line;
  4. Press the "Enter" key on the PS/2 keyboard;
  5. Choose the slot you want to type login from by pressing the "←" (Leftwards Arrow) and "→" (Rightwards Arrow) keys on the PS/2 keyboard;
  6. Press the "Enter" key on the PS/2 keyboard;
  7. Press either the "" (Upwards Arrow) key on the PS/2 keyboard to type the website;
  8. Press either the "" (Upwards Arrow) key on the PS/2 keyboard to type the username;
  9. Press either the "" (Upwards Arrow) key on the PS/2 keyboard to type the password.

*All credentials demonstrated here are entirely fictitious. Any similarity to actual credentials is purely coincidental.


*As for the origin of the patterns:

I made 80x80px patterns myself, the full-height flower pattern was generated by Bing Image Creator on December 5th, 2023.

You can view each pattern individually if you want.

Step 22: Delete Login

To delete a login:

  1. Select the "Logins" line;
  2. Press the "Enter" key on the PS/2 keyboard;
  3. Select the "Delete" line;
  4. Press the "Enter" key on the PS/2 keyboard;
  5. Select the login you would like to delete either by pressing the "←" (Leftwards Arrow) and "→" (Rightwards Arrow) keys on the PS/2 keyboard;
  6. Press 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.

Step 23: Send Password to the Receiver

While reading the Espressif documentation on random number generation, I've learned that RNG only works under one of the three conditions, one such condition is that the "RF subsystem is enabled."

Since I didn't want to simply enable the Wi-Fi or Bluetooth on ESP32 without doing anything with it, I made a feature that enables you to securely send a password to a distance of up to 650 feet (200 meters).

To protect the receiver from the replay attack (ensure that the receiver won't correctly decrypt any package that is resent to it) - I've combined the key incrementation with the session key. The session key consists of 20 characters generated by the vault. The session key is valid until you either reboot the vault or power it off. After the key derivation process is completed, both devices present you with the three verification numbers. These numbers must match on both the vault and the receiver! If these numbers don't match, I would advise you to reboot the receiver board and enter the session key again. If that won't work, open the firmware and check if the keys are the same.


To send the password to the receiver:

  • Select the "Other Options" line in the main menu;
  • Press the "Enter" key on the PS/2 keyboard;
  • Select the "Send Password" line;
  • Press the "Enter" key on the PS/2 keyboard;
  • Enter the key on the receiver's keypad;
  • Compare the verification numbers on both displays. They must match;
  • Press the "Enter" key on the PS/2 keyboard;
  • Enter the password (string) you'd like to send on the PS/2 keyboard;
  • Press the "Enter" key on the PS/2 keyboard to send the password to the receiver.

The receiver also prints the received text to the Serial Terminal.

According to the quick tests I've conducted, the Midbar V6.0 can transfer a 200-character long string over the air without any problems.

Step 24: Final Thoughts

Midbar V6.0 is the first ESP32-based version of Midbar that can act as if it were a USB keyboard. This feature isn't new for the Midbar project itself, but it's new for that particular branch. Quite frankly, the only advantage that this version of Midbar has over the Midbar (Teensy 4.1 Version) V2.0 is the lower cost of components. So, if you can afford to buy a Teensy 4.1, I suggest you make a Midbar (Teensy 4.1 Version) V2.0 instead.


And while Midbar is no guarantee of world peace or social harmony, I do believe that it's an important contribution to the protection of your data from unauthorized access.

I think it's also worth mentioning that Midbar's source code is distributed under the MIT license. That grants you the freedom to customize, adapt, and modify Midbar according to your needs and preferences. In other words, you can create your own version of Midbar or use it as a starting point for building new projects without the need for external permission.

If you found this tutorial to be useful, please consider sharing it.

Thank you for reading this tutorial.