Newer
Older
Hardware / SideChannel / ATtiny85_Timing_Attack / multi_digit.ino
0xRoM on 11 Feb 3 KB initial commit
  1. #include <avr/sleep.h>
  2. #include <avr/interrupt.h>
  3. #include <SoftwareSerial.h>
  4. const int rx = 1; // for serial input
  5. const int tx = 4; // pin 5 for serial.print()
  6. SoftwareSerial Serial(rx, tx); // rx, tx pins
  7.  
  8. int rgLed = 2; // red/green LED pin
  9. int button = 3; // buttons pin
  10. int buttonValue; // Stores analog value when button is pressed
  11.  
  12. String ourCode = ""; // Set the required PIN code.
  13. String currentCode = ""; // Stores entered code
  14.  
  15. void generateRandomPin() {
  16.  
  17. ourCode = ""; // ensure is empty
  18. // Use multiple entropy sources for the random seed
  19. int pinSeed = analogRead(0) + analogRead(1); // Read from pin 0 and pin 1 for more randomness
  20. unsigned long combinedSeed = pinSeed + random(100, 200); // Combine them all
  21. randomSeed(combinedSeed); // Seed the random number generator with the combined value
  22. int pinLength = random(4, 9); // Random length between 4 and 8
  23. for (int i = 0; i < pinLength; i++) {
  24. ourCode += String(random(1, 5)); // Random number between 1 and 4 (inclusive)
  25. }
  26. Serial.println("Generated PIN: " + ourCode); // Display the generated PIN for debugging
  27. }
  28.  
  29. void setup() {
  30. pinMode(rgLed, INPUT);
  31. pinMode(rx, INPUT); // setup and print to serial
  32. pinMode(tx, OUTPUT);
  33. Serial.begin(9600);
  34. Serial.println("Running");
  35. delay(500);
  36. generateRandomPin();
  37. red(500);
  38. green(500);
  39. }
  40.  
  41. #pragma GCC push_options
  42. #pragma GCC optimize ("O0") // Disable optimisations
  43.  
  44. void addkey(String keyPressed) {
  45. currentCode += keyPressed;
  46. Serial.print("Added: ");
  47. Serial.println(currentCode);
  48. if (currentCode.length() == ourCode.length()) {
  49. Serial.print("Checking: ");
  50. volatile bool correct = true;
  51. for (int i = 0; i < ourCode.length(); i++) {
  52. if (currentCode[i] != ourCode[i]) {
  53. correct = false;
  54. break;
  55. }
  56. do_login();
  57. }
  58. if (correct) {
  59. Serial.println("correct");
  60. green(500);
  61. delay(500);
  62. green(500);
  63. delay(500);
  64. green(500);
  65. } else {
  66. Serial.println("failed");
  67. red(500);
  68. }
  69. currentCode = ""; // Reset the entered code
  70. }
  71. }
  72.  
  73. #pragma GCC pop_options
  74.  
  75. void green(int del) { // turn on green LED for delay()
  76. pinMode(rgLed, OUTPUT);
  77. analogWrite(rgLed, HIGH);
  78. delay(del);
  79. pinMode(rgLed, INPUT);
  80. }
  81.  
  82. void red(int del) { // turn on red LED for delay()
  83. pinMode(rgLed, OUTPUT);
  84. analogWrite(rgLed, 255);
  85. delay(del);
  86. pinMode(rgLed, INPUT);
  87. }
  88.  
  89. void do_login(){
  90. delay(10);
  91. }
  92.  
  93. void loop() {
  94. buttonValue = analogRead(button); // Read analog value from A0 pin
  95. if (buttonValue > 100){
  96. Serial.println("Button analog value: " + String(buttonValue)); // Print button value
  97. }
  98. if (buttonValue >= 1011 && buttonValue <= 1030) { // For 1st button:
  99. addkey("1");
  100. } else if (buttonValue >= 1000 && buttonValue <= 1010) { // For 2nd button:
  101. addkey("2");
  102. } else if (buttonValue >= 950 && buttonValue <= 999) { // For 3rd button:
  103. addkey("3");
  104. } else if (buttonValue >= 870 && buttonValue <= 950) { // For 4th button:
  105. addkey("4");
  106. }
  107. if (Serial.available()) { // Check if data is available from Serial input
  108. delay(10);
  109. char key = Serial.read(); // Read the character
  110. if (key >= '1' && key <= '4') { // Ensure input is a valid key
  111. addkey(String(key));
  112. }
  113. }
  114. delay(100);
  115. }
Buy Me A Coffee