totp.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "totp.h"
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <math.h>
  5. #include <timezone_utils.h>
  6. #include "../../config/wolfssl/config.h"
  7. #include <wolfssl/wolfcrypt/hmac.h>
  8. #define HMAC_MAX_RESULT_SIZE WC_SHA512_DIGEST_SIZE
  9. static uint64_t swap_uint64(uint64_t val) {
  10. val = ((val << 8) & 0xFF00FF00FF00FF00ULL) | ((val >> 8) & 0x00FF00FF00FF00FFULL);
  11. val = ((val << 16) & 0xFFFF0000FFFF0000ULL) | ((val >> 16) & 0x0000FFFF0000FFFFULL);
  12. return (val << 32) | (val >> 32);
  13. }
  14. /**
  15. * @brief Generates the timeblock for a time in seconds.
  16. * Timeblocks are the amount of intervals in a given time. For example,
  17. * if 1,000,000 seconds has passed for 30 second intervals, you would get
  18. * 33,333 timeblocks (intervals), where timeblock++ is effectively +30 seconds.
  19. * @param interval in seconds
  20. * @param for_time a time in seconds to get the current timeblocks
  21. * @return Timeblock given \p for_time using \p interval
  22. */
  23. uint64_t totp_timecode(uint8_t interval, uint64_t for_time) {
  24. return for_time / interval;
  25. }
  26. /**
  27. * @brief Generates an OTP (One Time Password)
  28. * @param algo hashing algorithm to be used
  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. uint64_t otp_generate(
  35. TOTP_ALGO algo,
  36. const uint8_t* plain_secret,
  37. size_t plain_secret_length,
  38. uint64_t input) {
  39. uint8_t hmac[HMAC_MAX_RESULT_SIZE] = {0};
  40. uint64_t input_swapped = swap_uint64(input);
  41. int hmac_len =
  42. (*algo)(plain_secret, plain_secret_length, (uint8_t*)&input_swapped, 8, &hmac[0]);
  43. if(hmac_len == 0) {
  44. return OTP_ERROR;
  45. }
  46. uint64_t offset = (hmac[hmac_len - 1] & 0xF);
  47. uint64_t i_code =
  48. ((hmac[offset] & 0x7F) << 24 | (hmac[offset + 1] & 0xFF) << 16 |
  49. (hmac[offset + 2] & 0xFF) << 8 | (hmac[offset + 3] & 0xFF));
  50. return i_code;
  51. }
  52. uint64_t totp_at(
  53. TOTP_ALGO algo,
  54. const uint8_t* plain_secret,
  55. size_t plain_secret_length,
  56. uint64_t for_time,
  57. float timezone,
  58. uint8_t interval) {
  59. uint64_t for_time_adjusted =
  60. timezone_offset_apply(for_time, timezone_offset_from_hours(timezone));
  61. return otp_generate(
  62. algo, plain_secret, plain_secret_length, totp_timecode(interval, for_time_adjusted));
  63. }
  64. static int totp_algo_common(
  65. int type,
  66. const uint8_t* key,
  67. size_t key_length,
  68. const uint8_t* input,
  69. size_t input_length,
  70. uint8_t* output) {
  71. Hmac hmac;
  72. int ret = wc_HmacSetKey(&hmac, type, key, key_length);
  73. if(ret == 0) {
  74. ret = wc_HmacUpdate(&hmac, input, input_length);
  75. }
  76. if(ret == 0) {
  77. ret = wc_HmacFinal(&hmac, output);
  78. }
  79. wc_HmacFree(&hmac);
  80. return ret == 0 ? wc_HmacSizeByType(type) : 0;
  81. }
  82. static int totp_algo_sha1(
  83. const uint8_t* key,
  84. size_t key_length,
  85. const uint8_t* input,
  86. size_t input_length,
  87. uint8_t* output) {
  88. return totp_algo_common(WC_SHA, key, key_length, input, input_length, output);
  89. }
  90. static int totp_algo_sha256(
  91. const uint8_t* key,
  92. size_t key_length,
  93. const uint8_t* input,
  94. size_t input_length,
  95. uint8_t* output) {
  96. return totp_algo_common(WC_SHA256, key, key_length, input, input_length, output);
  97. }
  98. static int totp_algo_sha512(
  99. const uint8_t* key,
  100. size_t key_length,
  101. const uint8_t* input,
  102. size_t input_length,
  103. uint8_t* output) {
  104. return totp_algo_common(WC_SHA512, key, key_length, input, input_length, output);
  105. }
  106. const TOTP_ALGO TOTP_ALGO_SHA1 = (TOTP_ALGO)(&totp_algo_sha1);
  107. const TOTP_ALGO TOTP_ALGO_SHA256 = (TOTP_ALGO)(&totp_algo_sha256);
  108. const TOTP_ALGO TOTP_ALGO_SHA512 = (TOTP_ALGO)(&totp_algo_sha512);