Advertisement
lewcpe

Arduino-Google-Authenticator

Dec 7th, 2014
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. #include <Time.h>  
  2. #include <Wire.h>  
  3. #include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
  4. #include <crypto.h>
  5.  
  6. uint8_t * secretkey = (uint8_t *) "Hello!\xde\xad\xbe\xef";
  7. int secretlen = 80; //bit value
  8. hmac_sha1_ctx_t hctx;
  9.  
  10. const unsigned long VERIFICATION_CODE_MODULUS = 1000000;
  11.  
  12. uint32_t totp(uint64_t time){
  13.   uint8_t mac[20];
  14.   uint8_t challenge[8];
  15.   for (int i = 8; i--; time >>= 8) {
  16.     challenge[i] = time;
  17.   }
  18.  
  19.   hmac_sha1_init(&hctx, secretkey, secretlen);
  20.   hmac_sha1_lastBlock(&hctx, challenge, 64);
  21.   hmac_sha1_final(mac, &hctx);
  22.  
  23.   uint8_t offset = mac[19] & 0xF;
  24.   uint64_t truncated = 0;
  25.   for(int i = 0; i < 4; ++i){
  26.     truncated <<= 8;
  27.     truncated |= mac[offset + i];
  28.   }
  29.  
  30.   truncated &= 0x7FFFFFFF;
  31.   truncated %= VERIFICATION_CODE_MODULUS;
  32.  
  33.   return (uint32_t)truncated;
  34. }
  35.  
  36. void setup()  {
  37.   char totpbuf[10];
  38.   Keyboard.begin();
  39.   delay(1000);
  40.   setSyncProvider(RTC.get);   // the function to get the time from the RTC
  41.   if(timeStatus()!= timeSet)
  42.      Keyboard.println("ERROR");
  43.   else{
  44.     uint32_t timestamp = (now()-25200)/30;
  45.     uint32_t totpval = totp(timestamp);
  46.     sprintf(totpbuf,"%06lu",totpval);
  47.     Keyboard.println(totpbuf);
  48.   }
  49.      
  50. }
  51.  
  52. void loop()
  53. {
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement