Newer
Older
Hardware / SideChannel / ATtiny85_Timing_Attack / 4_digit.ino
0xRoM on 11 Feb 2 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 = "1324"; // Set the required PIN code.
  13. String currentCode = ""; // Stores entered code
  14.  
  15. void setup() {
  16. pinMode(rgLed, INPUT);
  17. pinMode(rx, INPUT); // setup and print to serial
  18. pinMode(tx, OUTPUT);
  19. Serial.begin(9600);
  20. Serial.println("Running");
  21.  
  22. red(500);
  23. green(500);
  24. }
  25.  
  26. #pragma GCC push_options
  27. #pragma GCC optimize ("O0") // Disable optimisations
  28.  
  29. void addkey(String keyPressed) {
  30. currentCode += keyPressed;
  31. Serial.println("Entered so far: " + currentCode);
  32. if (currentCode.length() == ourCode.length()) {
  33. Serial.print("Checking: ");
  34. volatile bool correct = true;
  35. for (int i = 0; i < ourCode.length(); i++) {
  36. if (currentCode[i] != ourCode[i]) {
  37. correct = false;
  38. break;
  39. }
  40. do_login();
  41. }
  42. if (correct) {
  43. Serial.println("correct");
  44. green(500);
  45. delay(500);
  46. green(500);
  47. delay(500);
  48. green(500);
  49. } else {
  50. Serial.println("failed");
  51. red(500);
  52. }
  53. currentCode = ""; // Reset the entered code
  54. }
  55. }
  56.  
  57. #pragma GCC pop_options
  58.  
  59. void green(int del) { // turn on green LED for delay()
  60. pinMode(rgLed, OUTPUT);
  61. analogWrite(rgLed, HIGH);
  62. delay(del);
  63. pinMode(rgLed, INPUT);
  64. }
  65.  
  66. void red(int del) { // turn on red LED for delay()
  67. pinMode(rgLed, OUTPUT);
  68. analogWrite(rgLed, 255);
  69. delay(del);
  70. pinMode(rgLed, INPUT);
  71. }
  72.  
  73. void do_login(){
  74. delay(5);
  75. }
  76.  
  77. void loop() {
  78. buttonValue = analogRead(button); // Read analog value from A0 pin
  79. if (buttonValue >= 1015 && buttonValue <= 1030) { // For 1st button:
  80. addkey("1");
  81. } else if (buttonValue >= 1000 && buttonValue <= 1014) { // For 2nd button:
  82. addkey("2");
  83. } else if (buttonValue >= 950 && buttonValue <= 999) { // For 3rd button:
  84. addkey("3");
  85. } else if (buttonValue >= 870 && buttonValue <= 950) { // For 4th button:
  86. addkey("4");
  87. }
  88. if (Serial.available()) { // Check if data is available from Serial input
  89. char key = Serial.read(); // Read the character
  90. if (key >= '1' && key <= '4') { // Ensure input is a valid key
  91. addkey(String(key));
  92. }
  93. }
  94. delay(100);
  95. }
Buy Me A Coffee