Help Needed with ChipWhisperer and ESP32 Power Trace Capture

I’m working on a project to capture power traces from an ESP32 using ChipWhisperer. The ESP32 performs AES encryption and sends a trigger signal to the ChipWhisperer for capturing the power trace. However, I’m facing issues with capturing the power traces and synchronizing the trigger signal.

Below is the code for both the ChipWhisperer setup and the ESP32 firmware. I would appreciate any insights or suggestions on how to resolve these issues.

import time
import chipwhisperer as cw
import matplotlib.pyplot as plt

# Initialize ChipWhisperer scope and target
scope = cw.scope()
target = cw.target(scope)

# Default setup for the scope
scope.default_setup()

# Configure scope settings
scope.gain.mode = 'high'
scope.gain.gain = 22
scope.adc.samples = 5000
scope.clock.clkgen_freq = 7363636.363636363
scope.clock.adc_freq = 29454545.454545453
scope.clock.freq_ctr = 2498394
scope.io.tio1 = 'serial_rx'
scope.io.tio2 = 'serial_tx'
scope.io.hs2 = 'clkgen'
scope.io.cdc_settings = bytearray(b'\x00\x00\x00\x00')
scope.glitch.phase_shift_steps = 4592
scope.trace.capture.trigger_source = 'firmware trigger'
scope.io.tio4 = 'high_z'
scope.io.tio2 = 'serial_rx'
scope.io.tio1 = 'serial_tx'
target.baud = 115200

# Arm the scope
scope.arm()

while True:
    print("Password catching phase")

    # Send password to the target
    password = "1234\n"
    print(f"Sending password: {password.strip()}")
    target.write(password)

    # Read response from the target
    response = target.read(timeout=500)  # Timeout in milliseconds
    if response:
        print(f"Response from ESP32: {response.strip()}")
    else:
        print("No response received from ESP32. Check UART connection.")

    # Capture power trace
    if scope.capture():
        print("Capture failed! Check trigger and timing.")
    else:
        trace = scope.get_last_trace()
        print("Power trace captured successfully!")

        # Plot the power trace
        plt.plot(trace)
        plt.title("Power Trace")
        plt.xlabel("Sample Number")
        plt.ylabel("Power Consumption (arbitrary units)")
        plt.show()

    time.sleep(9)
#include <AES.h>  // Include an AES library (such as Crypto, ESP32-specific)

#define TRIGGER_PIN 5
#define LED_PIN 15  // Define the LED pin

String correctPassword = "1234";
String inputPassword = "";

// AES setup
AES aes;
byte key[] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
              0xab, 0xf7, 0x09, 0xcf, 0x4f, 0x3c, 0x76, 0x2e}; // 128-bit key
byte plaintext[] = {0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d,
                    0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34}; // Example input
byte ciphertext[16]; // Buffer to store encrypted data

void setup() {
  Serial.begin(115200);
  pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);  // Set the LED pin as an output

  digitalWrite(TRIGGER_PIN, LOW);
  aes.set_key(key, sizeof(key)); // Set the AES encryption key

  Serial.println("Enter password:");
}

void loop() {
  if (Serial.available() > 0) {
    char receivedChar = Serial.read();

    if (receivedChar == '\n') {
      digitalWrite(TRIGGER_PIN, HIGH);  // Trigger signal for capturing
      delay(10);
      digitalWrite(TRIGGER_PIN, LOW);

      // Perform AES encryption
      aes.encrypt(plaintext, ciphertext);

      if (inputPassword == correctPassword) {
        Serial.println("Access Granted");
        digitalWrite(LED_PIN, HIGH);
      } else {
        Serial.println("Access Denied");
        digitalWrite(LED_PIN, LOW);
      }

      inputPassword = "";
      Serial.println("Enter password:");
    } else {
      inputPassword += receivedChar;
    }
  }
}

Issues Faced:

  1. Trigger Signal: The trigger signal from the ESP32 is not being captured correctly by the ChipWhisperer.
  2. Power Trace Capture: The power traces captured do not show the expected spikes, even though the password authentication and AES encryption are working correctly.