Simple Power Analysis with ESP32 and PSLab

From Embedded Lab Vienna for IoT & Security
Jump to navigation Jump to search

Introduction

This article provides a step-by-step guide to performing a Simple Power Analysis (SPA), a passive side-channel attack technique, using an ESP32 microcontroller and the Pocket Science Lab (PSLab). The goal is to observe differences in power consumption patterns between two cryptographic algorithms (AES-128 and RSA-4096) and understand how SPA can reveal system vulnerabilities.

Side-Channel Attacks (SCAs) exploit unintended physical emissions or behaviors of cryptographic systems, such as power consumption, electromagnetic radiation, or timing, to extract sensitive data. Unlike traditional attacks, SCAs do not target the cryptographic algorithm itself but its physical implementation.

Simple Power Analysis (SPA) is a fundamental SCA technique where power consumption patterns are measured and analyzed to infer system operations. This demonstration focuses on SPA using the ESP32 microcontroller and PSLab, highlighting the differences in power usage between lightweight block encryption (AES-128) and computationally intensive public-key cryptography (RSA-4096).

Requirements

Hardware

  • ESP32 Microcontroller: For running the cryptographic algorithms.
  • Pocket Science Lab (PSLab): Used as a power supply, oscilloscope, and multimeter.
  • Breadboard: For circuit assembly.
  • 10-Ohm Resistor: To measure voltage drop for power analysis.
  • Jumper Wires: For making connections.
  • USB Cable: For powering and flashing the ESP32.

Software

  • Arduino IDE: For programming the ESP32 microcontroller.
  • PSLab App: Desktop software to control the Pocket Science Lab.

Why Use ESP32 and PSLab for SPA?

ESP32 Microcontroller

The ESP32 is an affordable and powerful microcontroller, making it ideal for SPA demonstrations.

ESP32 Microcontroller [1]

Key Features:

  • Dual-Core Processor: Handles complex cryptographic algorithms efficiently.
  • Clear Power Profiles: Distinct power consumption for AES-128 and RSA-4096.
  • Arduino IDE Support: Simplifies programming and flashing.

Why ESP32 for SPA?

  • Supports alternating between lightweight and computationally heavy algorithms.
  • Affordable and widely available, making it accessible for beginners.

Pocket Science Lab (PSLab)

The PSLab is an all-in-one portable tool for scientific experiments, ideal for SPA.

PCB Layout [2]

Key Features:

  • Oscilloscope: Displays real-time power consumption patterns.
  • Multimeter: Measures voltage drops for current calculation.
  • Power Supply: Provides stable 3.3V/5V output for ESP32.

Why PSLab for SPA?

  • Combines essential tools in one device.
  • Open-source and affordable, lowering entry barriers for researchers and students.

Step-by-Step Guide

To begin, you need to set up the PSLab, which will serve as a power supply, oscilloscope, and multimeter for the experiment.

Step 1 - Setting Up the Pocket Science Lab (PSLab)

Install Python:

Download and install the latest version of Python from the official website (https://www.python.org/downloads/) and ensure you include Python in your system path during installation.

Download the PSLab Application:

Visit the PSLab Downloads page and install the PSLab desktop application (https://pslab.io/downloads/). Follow the installation instructions for your operating system (Windows, Linux, or macOS).

Install the Required Drivers:

Download the CP210x USB-to-UART drivers from Silicon Labs (https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers?tab=downloads). After installation, connect the PSLab to your computer using a USB cable. If the device is not recognized, open Device Manager, locate the PSLab under "Other Devices," and update the driver manually.

Install PSLab Python Libraries:

Open a terminal (e.g. PowerShell or CMD) and install the required Python libraries using the following commands:

   $ pip install pyserial numpy pslab

Verify PSLab Connection:

Launch the PSLab application. If the device is connected successfully, you will see a green connection status in the top-right corner of the interface.

Step 2 - Programming the ESP32

The ESP32 will run two cryptographic algorithms (AES-128 and RSA-4096) alternately to demonstrate the differences in power consumption.

Install the Arduino IDE:

Download and install the Arduino IDE from the official website (https://www.arduino.cc/en/software).

Configure ESP32 Board Support:

  • Open File → Preferences in the Arduino IDE.
  • Add the following URL under "Additional Board Manager URLs":
   https://dl.espressif.com/dl/package_esp32_index.json
  • Go to Tools → Board → Boards Manager, search for "ESP32," and install the ESP32 board package.

Connect and Program the ESP32:

  • Use a USB cable to connect the ESP32 to your computer. Ensure the correct COM port is selected under Tools → Port.
  • Upload the following code to alternate between AES-128 and RSA-4096 every 10 seconds:
   #include <Arduino.h>
   #include "mbedtls/aes.h"
   #include "mbedtls/rsa.h"
   #include "mbedtls/entropy.h"
   #include "mbedtls/ctr_drbg.h"
   #include "mbedtls/pk.h"
   
   // RSA context
   mbedtls_pk_context pk;
   mbedtls_entropy_context entropy;
   mbedtls_ctr_drbg_context ctr_drbg;
   
   // Timing variables
   unsigned long start_time;
   bool using_aes = true;  // Beginne mit AES
   
   // AES context
   mbedtls_aes_context aes;
   
   // AES-128 encryption
   void aes_encryption() {
     unsigned char key[16] = {0};  // 128-bit key (16 bytes)
     unsigned char plaintext[16] = {'T', 'e', 's', 't', 'A', 'E', 'S', '1', '2', '3', '4', '5', '6', '7', '8', '9'};  // 16-byte block
     unsigned char ciphertext[16];
   
     mbedtls_aes_init(&aes);
     mbedtls_aes_setkey_enc(&aes, key, 128);
   
     for (int i = 0; i < 1000; i++) {
       mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, plaintext, ciphertext);
     }
   
     mbedtls_aes_free(&aes);
   }
   
   // RSA-4096 encryption
   void rsa_encryption() {
     unsigned char plaintext[256] = "This is a test message for RSA-4096 encryption.";
     unsigned char ciphertext[512];
     size_t olen;
   
     // Perform RSA-4096 multiple times within the 10-second loop 
     for (int i = 0; i < 5; i++) {  // Execute 5 times for higher load
         mbedtls_pk_encrypt(&pk, plaintext, sizeof(plaintext), ciphertext, &olen, sizeof(ciphertext), mbedtls_ctr_drbg_random, &ctr_drbg);
   
         // Additional load to increase power consumption
         volatile long dummy = 0;
         for (int j = 0; j < 50000; j++) {
             dummy += j * j;
         }
     }
   }
   
   // Initialize RSA context
   void initialize_rsa() {
     const char *pers = "rsa_genkey";
     mbedtls_pk_init(&pk);
     mbedtls_entropy_init(&entropy);
     mbedtls_ctr_drbg_init(&ctr_drbg);
     mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *)pers, strlen(pers));
     mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA));
     mbedtls_rsa_gen_key(mbedtls_pk_rsa(pk), mbedtls_ctr_drbg_random, &ctr_drbg, 4096, 65537);
   }
   
   void setup() {
     Serial.begin(115200);
     start_time = millis();  // Start the timer
     initialize_rsa();  // Initialize RSA-4096
   }
   
   void loop() {
     // Switch between AES-128 and RSA-4096 every 10 seconds
     if (using_aes) {
       aes_encryption();
     } else {
       rsa_encryption();
     }
   
     // Check if 10 seconds have passed
     if (millis() - start_time >= 10000) {  // Switch after 10 seconds
       start_time = millis();  // Reset timer
       using_aes = !using_aes;  // Toggle between AES and RSA
   
       if (using_aes) {
         Serial.println("Switched to AES-128.");
       } else {
         Serial.println("Switched to RSA-4096.");
       }
     }
   }
  • You can use libraries like AESLib for AES encryption and RSA libraries for RSA operations. Upload the code and open the Serial Monitor to verify the operations.

Step 3 - Building the Circuit

To measure power consumption, you will set up a circuit that includes the PSLab, ESP32, a resistor, a breadboard, and jumper wires. While the specific rows (e.g., A1, B1) on the breadboard are not crucial, it is essential to maintain the correct sequence of connections to ensure the circuit functions properly.

Connect the PSLab Power Supply:

  • PV1 (PSLab Power Output) → Breadboard row A1.
  • GND (PSLab) → Ground rail of the breadboard.

Place the Resistor:

  • Connect a 10-Ohm resistor between rows B1 and B3 on the breadboard.

Connect the ESP32:

  • 3.3V/5V (ESP32) → Breadboard row A3.
  • GND (ESP32) → Breadboard ground rail.

Set up Voltage Measurement:

  • Connect CH1 (PSLab) → Breadboard row C1 (before the resistor).
  • Connect CH2 (PSLab) → Breadboard row C3 (after the resistor).

Step 4 - Measuring Power Consumption

Power the ESP32:
Open the PSLab app and set PV1 to output a stable 3.3V/5V power supply.

Observe Voltage Drop:
Use the Oscilloscope function in the PSLab app to measure the voltage drop across the resistor. This voltage drop is proportional to the current flowing through the ESP32.

Analyze the Power Patterns: Observe the oscilloscope output during AES-128 and RSA-4096 operations:

  • AES-128: You should see lower, consistent power consumption patterns.
  • RSA-4096: Notice significantly higher and irregular power usage due to its computational complexity.

Step 5 - Interpreting the Results

To calculate the current flowing through the circuit, use Ohm's Law:

   

Where:

  • V: Voltage drop across the resistor (difference between CH1 and CH2).
  • R: Resistor value (10 Ohm).

Key Observations:

  • AES-128 exhibits consistent, low power consumption, typical for symmetric encryption.
  • RSA-4096 shows higher, irregular power usage due to its heavy computational workload.

Conclusion

This guide demonstrates how Simple Power Analysis (SPA) can reveal differences in power consumption during cryptographic operations. By using affordable tools like the ESP32 and the Pocket Science Lab, we can highlight vulnerabilities in cryptographic implementations. Understanding these weaknesses is essential for designing secure systems and developing countermeasures such as masking and noise injection.

References