KhadashPay V3.0

by Northstrix in Circuits > Microcontrollers

738 Views, 3 Favorites, 0 Comments

KhadashPay V3.0

IMG_20230427_145545.jpg

KhadashPay is an easy-to-use DIY payment system that enables business owners to set up a card payment system in their store without the need to buy an expensive terminal, pay fees for each transaction, and waste time and resources dealing with the bank.

As a customer of the store where KhadashPay is employed, you can know for sure that KhadashPay does not keep any logs and doesn't require a bit of your personal information to create an account.


The name KhadashPay consists of two parts: Khadash (which means "new" in Hebrew) and Pay.


Before you continue reading this instructable, please note that this payment system is not connected to any existing financial institution and operates completely offline. All the "money" kept in it is nothing more than just numbers entered by the operator, encrypted by the 3DES + AES + Blowfish + Serpent Encryption Algorithm In CBC Mode, and stored on an SD card.

Supplies

IMG_20230427_163253.jpg
  • ESP32 x1
  • 2.4 Inch TFT LCD with ILI9341 x1
  • Mifare RC522 RFID Reader x1
  • RFID cards x4
  • 4x4 Keypad x1
  • MH-SD Card Module x1
  • SD Card x1
  • 10µF capacitor x1 *optional

How It Came to Be

After making the KhadashPay V2.0 and KhadashPay V2.0 (Raspberry Pi Pico Version, I realized that even though these are functioning devices, both of them have a huge flaw - a small amount of memory available to store the client data (2Mb in KhadashPay V2.0 and 1Mb in KhadashPay V2.0 (Raspberry Pi Pico Version)). I've decided to get rid of that flaw by making the KhadashPay V3.0 (STM32F401CCU6 Version), and I did. But as it usually happens, I've added several new flaws while getting rid of an old one. And so, I put some effort into combining the best aspects of both versions of KhadashPay V2.0 and a STM32F401CCU6 version of KhadashPay V3.0 to make a new and improved version of the KhadashPay. And I think I've succeeded in that.

3DES + AES + Blowfish + Serpent Encryption Algorithm in CBC Mode

F4S3RIHLGO0E3B8.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)Midbar V5.0, Midbar (STM32F401CCU6 Version), and KhadashPay V3.0 (STM32F401CCU6 Version).

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

FP3OOR8LGO0E3BB.jpg

Another cool feature provided to you by KhadashPay is the "HMAC-SHA256"-based integrity verification.

Before encrypting the user data, KhadashPay computes the tag for that data and encrypts it alongside data.

Later on, when KhadashPay decrypts your data, it also decrypts the previously calculated tag and computes a new tag for the newly decrypted data. It then compares both tags. If they don't match - KhadashPay will promptly notify you that the integrity verification has failed.

"Belonging Check"

FX6SGG9LFFES1RI.jpg

Even though KhadashPay can't decrypt user data without the user's card, I still decided to add an additional check, just in case. 

The so-called "Belonging Check" (didn't really know what else to call it) is a feature that allows the KhadashPay to verify whether the record with the balance belongs to the card that is taped on the RFID reader or not.

Install CP210x Driver and Configure Arduino IDE *Optional

If you've never flashed ESP32 before you need to configure Arduino IDE and install CP210x driver to upload the firmware into the board, you can download the CP210x driver for ESP32 here: https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers

Configuring IDE isn't a part of this tutorial. You can read about it here: https://randomnerdtutorials.com/installing-the-esp32-board-in-arduino-ide-windows-instructions/

Download Firmware

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

SourceForge: https://sourceforge.net/projects/khadashpay/

OSDN: https://osdn.net/projects/khadashpay/

GitHub: https://github.com/Northstrix/KhadashPay

If you just need the firmware for the device alongside the RNG, then I would advise you to download the archive that weighs less than 2Mb either from SourceForge or OSDN.

But if you need the firmware for all versions of KhadashPay alongside the extra code, photos, and diagrams, in that case, I would advise you to download the 120 MB archive from GitHub.

Download and Install the Libraries

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

rfid: https://github.com/miguelbalboa/rfid

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.

Replace the Preset File for the TFT_eSPI Library

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

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

partsc.png

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.

Generate Keys

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

Modify the Firmware

md_fir.png

Open the "Firmware.ino" file from the "V3.0\ESP32_Version\Firmware" folder and replace my keys with those you've generated.

Flash ESP32

flesp.png

Upload the firmware from the "V3.0\ESP32_Version\Firmware" folder into the ESP32. Don't forget to hold the "BOOT" button when the firmware download starts.

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.

Assemble the Device

KhadashPay Circuit Diagram.png

The assembly process for KhadashPay V3.0 is a bit more complex than for KhadashPay V2.0 and KhadashPay V2.0 (Raspberry Pi Pico Version), but it's still relatively uncomplicated compared to KhadashPay V1.0.

Power the KhadashPay Up

IMG_20230427_154221_hdr.jpg
IMG_20230427_154607_hdr.jpg
IMG_20230427_154729_hdr.jpg
IMG_20230427_154952_hdr.jpg
IMG_20230427_155215_hdr.jpg
IMG_20230427_155533_hdr.jpg
IMG_20230427_155633_hdr.jpg
IMG_20230427_155833_hdr.jpg
IMG_20230427_160122_hdr.jpg
IMG_20230427_160223_hdr.jpg
IMG_20230427_160350_hdr.jpg
IMG_20230427_160900_hdr.jpg

Just like the Midbar V5.0, KhadashPay V3.0 has twelve lock screens. KhadashPay V3.0 randomly chooses the lock screen at startup.

After the KhadashPay has chosen the lock screen, it displays the "חדשפיי" inscription with the shifting background and the "Tap RFID card N1" inscription on the bottom of the lock screen.


*Credit for photos:

Austin:

Photo by MJ Tangonan on Unsplash

Beirut:

Photo by Piotr Chrobot on Unsplash

Bogota:

Photo by Random Institute on Unsplash

Frankfurt:

Photo by Jo Heubeck & Domi Pfenninger on Unsplash

Greenwich:

Photo by Fas Khan on Unsplash

Lion Monument:

Photo by Gotta Be Worth It

London:

Photo by Robert Bye on Unsplash

Miami:

Image by JORGE TAPIA from Pixabay

Montreal:

Photo by Michael Beener on Unsplash

Reykjavik:

Photo by Einar H. Reynis on Unsplash

Tel Aviv:

Photo by Shai Pal on Unsplash

Vienna:

Photo by Ahmad Tolba on Unsplash

Tap Four RFID Cards on the RFID Reader

IMG_20230427_150758.jpg
IMG_20230427_151136.jpg
IMG_20230427_151431_hdr.jpg
IMG_20230427_151740_hdr.jpg

After you've powered the device up, 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 KhadashPay. Otherwise, it just won't unlock.

If you don't have four cards, you can tap one card four times.

And by the way, the first card you tap on the RFID reader becomes the "operator card" right after you set the master password. You'll need this information later in this tutorial. For now, just remember it.

Set the Master Password

IMG_20230427_152041_hdr.jpg
FY9WP89LFFES1VQ.jpg
FOIIXSZLFFES1VR.jpg

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

While entering the master password on the 4x4 keypad, note that the '*' key serves as a backspace.

And remember that you can't change the master password without losing the ability to decrypt all user data stored on the SD card!

KhadashPay won't be able to decrypt the user data without the 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 the master password, press either the '#" or 'C' key on the keypad to finish the input and set the master password.


After you've unlocked the KhadashPay and got to the menu:

  • Press the "0" key on the 4x4 keypad to go down the menu;
  • Press the "8" key on the 4x4 keypad to go up the menu;
  • Press the "#" key on the 4x4 keypad to perform the selected action;
  • Press the 'D' key on the 4x4 keypad to show the random lock screen. After that, press any key to return to the menu.

Create an Account

IMG_20230427_162151.jpg
FAR6B9NLFFESE58.jpg
FTVPQ5MLFFESE59.jpg
F0OU8HJLFFESE6D.jpg
FGBXOJ7LFFESE6E.jpg
F8HU6HWLFFESE6F.jpg
FLIDUSBLFFESE6G.jpg

Ok, let's start with the facts about the KhadashPay account.

  • Each account is bound to the RFID card and PIN;
  • The PIN can have a length from 1 to 8 characters;
  • The PIN can have the following characters: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'D';
  • A client can create several accounts using the same card as long as the pins are different;
  • The maximum amount of money stored in the account is only limited by the double variable type (don't worry about that, the KhadashPay can still store a 15-digit number without any problems);
  • Not even the owner of the KhadashPay can see your balance without your card.


To create an account on KhadashPay V3.0:

Operator:

  • Select the "New Account" line in the menu;
  • Press the '#' key on the 4x4 keypad;
  • Tap the operator RFID card on the RFID reader;
  • Press the '#' key on the 4x4 keypad and give the device to the client.

Client:

  • Tap your RFID card on the RFID reader;
  • Set your PIN;
  • Press the '#' key on the 4x4 keypad;
  • Enter the PIN that you've set again;
  • Press the '#' key on the 4x4 keypad.

Put Money Into the Account

IMG_20230427_162143.jpg
F6D0BX6LFFESE9C.jpg
F4EMVY6LFFESE9D.jpg
FRE44JULFFESE9E.jpg
FMPWVNLLFFESE9J.jpg
FODQ3L9LFFESE9K.jpg
FVEM3NYLFFESE9S.jpg

To put money into the account:

Operator:

  • Select the "Put Money In" line in the menu;
  • Press the '#' key on the 4x4 keypad;
  • Tap the operator RFID card on the RFID reader;
  • Enter the amount of money to be put into the client's account;
  • Press the '#' key on the 4x4 keypad;
  • Press the '#' key on the 4x4 keypad and give the device to the client.

Client:

  • Tap your RFID card on the RFID reader;
  • Enter your PIN;
  • Press the '#' key on the 4x4 keypad.

Make a Sale

IMG_20230427_162130.jpg
FGGEIV5LFFESEC1.jpg
F7EMPEALFFESEC2.jpg
F05Q5LKLFFESEC3.jpg
FD6NUCOLFFESEC4.jpg
FGVOO8GLFFESEC5.jpg
FA56R6LLFFESEC6.jpg

To make a sale:

Operator:

  • Select the "Make A Sale" line in the menu;
  • Press the '#' key on the 4x4 keypad;
  • Tap the operator RFID card on the RFID reader;
  • Enter the sale amount;
  • Press the '#' key on the 4x4 keypad;
  • Press the '#' key on the 4x4 keypad and give the device to the client.

Client:

  • Tap your RFID card on the RFID reader;
  • Enter your PIN;
  • Press the '#' key on the 4x4 keypad.

View Balance

IMG_20230427_162213.jpg
FKEOYUVLFFESEKZ.jpg
FDE7IM8LFFESEL1.jpg
FF3PDLHLFFESEL2.jpg
F1DVZG8LFFESEL3.jpg
FA6ATWULFFESEL4.jpg

To view balance:

Operator:

  • Select the "View Balance" line in the menu;
  • Press the '#' key on the 4x4 keypad;
  • Tap the operator RFID card on the RFID reader;
  • Press the '#' key on the 4x4 keypad and give the device to the client.

Client:

  • Tap your RFID card on the RFID reader;
  • Enter your PIN;
  • Press the '#' key on the 4x4 keypad.


*As you might've noticed, I've reused some photos from the tutorial for KhadashPay V2.0 because the GUI is didn't really change since then.

Find a Good Use for KhadashPay

IMG_20230427_162238.jpg

Although this version of the KhadashPay is fast and a bit more secure than the Raspberry Pi Pico and STM32F401CCU6 versions of KhadashPay, it's far from perfect. It's a bit less stable than the KhadashPay V3.0 (STM32F401CCU6 Version), and sometimes you might have to press the key on the keypad several times before the KhadashPay processes it. Apart from that, I think it's the best version of the KhadashPay I've ever made.


Anyway, KhadashPay's source code is distributed under the MIT license, so if you want to make your own version of the KhadashPay 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.