totp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "totp.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include <math.h>
  7. #include "../hmac/hmac_sha1.h"
  8. #include "../hmac/hmac_sha256.h"
  9. #include "../hmac/hmac_sha512.h"
  10. #include "../hmac/byteswap.h"
  11. #include "../../lib/timezone_utils/timezone_utils.h"
  12. #define HMAC_MAX_SIZE 64
  13. /**
  14. * @brief Generates the timeblock for a time in seconds.
  15. * Timeblocks are the amount of intervals in a given time. For example,
  16. * if 1,000,000 seconds has passed for 30 second intervals, you would get
  17. * 33,333 timeblocks (intervals), where timeblock++ is effectively +30 seconds.
  18. * @param interval in seconds
  19. * @param for_time a time in seconds to get the current timeblocks
  20. * @return Timeblock given \p for_time using \p interval
  21. */
  22. uint64_t totp_timecode(uint8_t interval, uint64_t for_time) {
  23. return for_time / interval;
  24. }
  25. /**
  26. * @brief Generates an OTP (One Time Password)
  27. * @param algo hashing algorithm to be used
  28. * @param digits desired TOTP code length
  29. * @param plain_secret plain token secret
  30. * @param plain_secret_length plain token secret length
  31. * @param input input data for OTP code generation
  32. * @return OTP code if code was successfully generated; 0 otherwise
  33. */
  34. uint32_t otp_generate(
  35. TOTP_ALGO algo,
  36. uint8_t digits,
  37. const uint8_t* plain_secret,
  38. size_t plain_secret_length,
  39. uint64_t input) {
  40. uint8_t hmac[HMAC_MAX_SIZE] = {0};
  41. uint64_t input_swapped = swap_uint64(input);
  42. int hmac_len =
  43. (*algo)(plain_secret, plain_secret_length, (uint8_t*)&input_swapped, 8, &hmac[0]);
  44. if(hmac_len == 0) {
  45. return OTP_ERROR;
  46. }
  47. uint64_t offset = (hmac[hmac_len - 1] & 0xF);
  48. uint64_t i_code =
  49. ((hmac[offset] & 0x7F) << 24 | (hmac[offset + 1] & 0xFF) << 16 |
  50. (hmac[offset + 2] & 0xFF) << 8 | (hmac[offset + 3] & 0xFF));
  51. i_code %= (uint64_t)pow(10, digits);
  52. return i_code;
  53. }
  54. uint32_t totp_at(
  55. TOTP_ALGO algo,
  56. uint8_t digits,
  57. const uint8_t* plain_secret,
  58. size_t plain_secret_length,
  59. uint64_t for_time,
  60. float timezone,
  61. uint8_t interval) {
  62. uint64_t for_time_adjusted =
  63. timezone_offset_apply(for_time, timezone_offset_from_hours(timezone));
  64. return otp_generate(
  65. algo,
  66. digits,
  67. plain_secret,
  68. plain_secret_length,
  69. totp_timecode(interval, for_time_adjusted));
  70. }
  71. static int totp_algo_sha1(
  72. const uint8_t* key,
  73. size_t key_length,
  74. const uint8_t* input,
  75. size_t input_length,
  76. uint8_t* output) {
  77. hmac_sha1(key, key_length, input, input_length, output);
  78. return HMAC_SHA1_RESULT_SIZE;
  79. }
  80. static int totp_algo_sha256(
  81. const uint8_t* key,
  82. size_t key_length,
  83. const uint8_t* input,
  84. size_t input_length,
  85. uint8_t* output) {
  86. hmac_sha256(key, key_length, input, input_length, output);
  87. return HMAC_SHA256_RESULT_SIZE;
  88. }
  89. static int totp_algo_sha512(
  90. const uint8_t* key,
  91. size_t key_length,
  92. const uint8_t* input,
  93. size_t input_length,
  94. uint8_t* output) {
  95. hmac_sha512(key, key_length, input, input_length, output);
  96. return HMAC_SHA512_RESULT_SIZE;
  97. }
  98. const TOTP_ALGO TOTP_ALGO_SHA1 = (TOTP_ALGO)(&totp_algo_sha1);
  99. const TOTP_ALGO TOTP_ALGO_SHA256 = (TOTP_ALGO)(&totp_algo_sha256);
  100. const TOTP_ALGO TOTP_ALGO_SHA512 = (TOTP_ALGO)(&totp_algo_sha512);