Cifradopro: A baremetal Hardware Security Module using the STM32L4S5 Cortex-M4 MCU
What is CifradoPro?
Application Features
Random Number Generation
The device can generate random keys of sizes such as 128, 192, and 256 bits, utilizing the built-in Random Number Generator. The entropy source of the RNG peripheral is based on two analog noise sources, derived from three XORed Free-Running Ring Oscillator outputs. The generated keys are then outputted to the serial terminal in ASCII character format. Refer to the Random Number Generator Peripheral Driver for the STM32L4S5ZI microcontroller for further details.
AES Encryption and Decryption
The device utilizes the embedded AES Hardware Accelerator for encrypting plaintext and decrypting ciphertext in Cipher Block Chaining Mode. It supports both the 128-bit and 256-bit key variants. This mode helps prevent similar ciphertext blocks for similar plaintext data. The device outputs the encrypted and decrypted blocks to the serial terminal in ASCII character format. Refer to the AES Hardware Accelerator peripheral driver for the STM32L4S5ZI microcontroller for more information.
Hash Processing
One-Time Pad Generation
Secure Key Storage and Physical Tampering Protection
Each cryptographic key generated by the device is securely stored in plaintext on an EEPROM. A serial number is assigned to each stored cryptographic key. Users can request to view the cryptographic key database. The device maintains an internal lookup table that maps the serial number to the location of the key in the external memory device, which is interfaced via I2C. The device can generate keys up to 256 bits (32 bytes) in size and allocates 32 bytes for each key entry for alignment purposes. Therefore, a 256kbit Serial EEPROM from Microchip can store just over 1000 keys. The lookup table includes entries for the serial number and memory location, with the memory location pointing to the stored key.
The device is engineered to prevent unauthorized access to the external memory storing the cryptographic keys. A magnetic reed switch is positioned on the enclosure's top and walls. If the enclosure is opened, the magnetic reed switch triggers an interrupt to the microcontroller, prompting it to erase all cryptographic keys stored in the EEPROM, rendering them unusable.
Powering the device and Battery Backup
The tamper protection for the device remains effective as long as the device is powered on. To achieve this, I've implemented a setup using a 3.3V 1000mAh Lithium-ion battery and a TP4056 Battery Charger Module to power the HSM inside the enclosure. This setup allows the HSM to monitor if the enclosure is breached. The battery is recharged using a USB cable connected to the USB Port exposed by the TP4056 charger module. Communication with the serial terminal is facilitated by a USB TTL Adapter attached to the UART port of the device. Notably, the USB cable used for powering the device is isolated from the HSM's USB PHY, ensuring it cannot be used for communication with the HSM.
Firmware Design
This Hardware Security Module uses a layered software architecture. The peripheral drivers, namely the AES Hardware Accelerator, Random Number Generator and the HASH Processor are written on top of the CMSIS Abstraction. The I2C peripheral driver and the EEPROM library for the 24LC256 Serial EEPROM is written on top of the STM32 HAL. The custom HSM Application library is written on top of the aforementioned drivers. The HSM Library and the other custom peripheral drivers use the UART Peripheral Driver to print Error and Log messages to the Serial Terminal.
The HSM Library is written such that when the User opts for, for example, the Encryption Service, the HSM Library will call the AES_CBC_Encrypt() function in the AES driver file. Similarly, when the Key Generation Service is opted, the HSM Library will call the RNG_GetRandom() function in the RNG driver file. Moreover, the HSM Library will call the EEPROM_Write() function in the EEPROM library which in turn invokes the HAL_I2C_Write() function in the STM32 HAL I2C Driver file.