Newer
Older
Hardware / FaultInjection / examples / FaultyCat / 02_match_numbers / example_v2.0.ino
0xRoM on 11 Feb 2 KB initial commit
  1. #include <SoftwareSerial.h>
  2.  
  3. #define RX 3 // *** D3, Pin 2
  4. #define TX 4 // *** D4, Pin 3
  5. SoftwareSerial Serial(RX, TX);
  6.  
  7. void setup() {
  8. Serial.begin(9600);
  9. Serial.println(" ");
  10. Serial.println("Initializing...");
  11. randomSeed(analogRead(0)); // Seed the random number generator for more randomness
  12. delay(2000); // Delay for initialization
  13. }
  14.  
  15. void loop() {
  16. // Generate one random number and assign it to both variables
  17. volatile int originalNumber = random(10, 100);
  18. volatile int num1 = originalNumber;
  19. volatile int num2 = originalNumber;
  20.  
  21. // Perform reversible operations to increase glitch susceptibility but keep values comparable
  22. num1 = num1 ^ 0x55; // XOR num1 with 0x55
  23. num2 = num2 ^ 0x55; // XOR num2 with 0x55
  24. delayMicroseconds(5); // Critical timing for glitches
  25.  
  26. num1 = num1 ^ 0x55; // XOR again to reverse
  27. num2 = num2 ^ 0x55; // XOR again to reverse
  28.  
  29. delayMicroseconds(5); // Another critical timing for glitches
  30.  
  31. // Extract the first and second digits
  32. volatile int num1FirstDigit = num1 / 10; // Get the first digit of num1
  33. volatile int num1SecondDigit = num1 % 10; // Get the second digit of num1
  34.  
  35. delayMicroseconds(5); // More chances for glitches
  36. volatile int num2FirstDigit = num2 / 10; // Get the first digit of num2
  37. volatile int num2SecondDigit = num2 % 10; // Get the second digit of num2
  38.  
  39. delayMicroseconds(5); // Increased vulnerability
  40.  
  41. // Check if the numbers still match after the potential glitch
  42. int match = 0;
  43. if (num1FirstDigit == num2FirstDigit) {
  44. if (num1SecondDigit == num2SecondDigit) {
  45. match = 1;
  46. }
  47. }
  48. if (match == 1) {
  49. Serial.print("Numbers match: "); Serial.print(num1); Serial.print(" "); Serial.print(num2); Serial.print("\r");
  50. } else {
  51. Serial.print("Glitch detected! Numbers do not match: ");
  52. Serial.print(num1); Serial.print(" "); Serial.print(num2);
  53. Serial.print(" ("); Serial.print(num1FirstDigit); Serial.print(":"); Serial.print(num1SecondDigit); Serial.print(") ");
  54. Serial.print("("); Serial.print(num2FirstDigit); Serial.print(":"); Serial.print(num2SecondDigit); Serial.println(")");
  55. Serial.println(" ");
  56. }
  57. delay(100); // Shorter delay to increase glitch detection chances
  58. }
Buy Me A Coffee