Newer
Older
Hardware / FaultInjection / examples / FaultyCat / 02_match_numbers / example_v2.0.ino
0xRoM on 11 Feb 2 KB initial commit
#include <SoftwareSerial.h>

#define RX    3   // *** D3, Pin 2
#define TX    4   // *** D4, Pin 3
SoftwareSerial Serial(RX, TX);

void setup() {
  Serial.begin(9600);
  Serial.println(" ");
  Serial.println("Initializing...");
  randomSeed(analogRead(0));  // Seed the random number generator for more randomness
  delay(2000);  // Delay for initialization
}

void loop() {
  // Generate one random number and assign it to both variables
  volatile int originalNumber = random(10, 100);  
  volatile int num1 = originalNumber;
  volatile int num2 = originalNumber;

  // Perform reversible operations to increase glitch susceptibility but keep values comparable
  num1 = num1 ^ 0x55;  // XOR num1 with 0x55
  num2 = num2 ^ 0x55;  // XOR num2 with 0x55
  
  delayMicroseconds(5);  // Critical timing for glitches

  num1 = num1 ^ 0x55;  // XOR again to reverse
  num2 = num2 ^ 0x55;  // XOR again to reverse

  delayMicroseconds(5);  // Another critical timing for glitches

  // Extract the first and second digits
  volatile int num1FirstDigit = num1 / 10;   // Get the first digit of num1
  volatile int num1SecondDigit = num1 % 10;  // Get the second digit of num1

  delayMicroseconds(5);  // More chances for glitches
  
  volatile int num2FirstDigit = num2 / 10;   // Get the first digit of num2
  volatile int num2SecondDigit = num2 % 10;  // Get the second digit of num2

  delayMicroseconds(5);  // Increased vulnerability

  // Check if the numbers still match after the potential glitch
  int match = 0;
  if (num1FirstDigit == num2FirstDigit) {
    if (num1SecondDigit == num2SecondDigit) {
       match = 1;
    }
  }
  
  if (match == 1) {
    Serial.print("Numbers match: "); Serial.print(num1); Serial.print(" "); Serial.print(num2); Serial.print("\r");
  } else {
    Serial.print("Glitch detected! Numbers do not match: ");
    Serial.print(num1); Serial.print(" "); Serial.print(num2);
    Serial.print(" ("); Serial.print(num1FirstDigit); Serial.print(":"); Serial.print(num1SecondDigit); Serial.print(") ");
    Serial.print("("); Serial.print(num2FirstDigit); Serial.print(":"); Serial.print(num2SecondDigit); Serial.println(")");
    Serial.println(" ");
  }
  
  delay(100);  // Shorter delay to increase glitch detection chances
}