totp.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #ifdef NO_INLINE
  9. #include <wolfssl/wolfcrypt/misc.h>
  10. #else
  11. #define WOLFSSL_MISC_INCLUDED
  12. #include <wolfcrypt/src/misc.c>
  13. #endif
  14. #define HMAC_MAX_RESULT_SIZE WC_SHA512_DIGEST_SIZE
  15. /**
  16. * @brief Generates the timeblock for a time in seconds.
  17. * Timeblocks are the amount of intervals in a given time. For example,
  18. * if 1,000,000 seconds has passed for 30 second intervals, you would get
  19. * 33,333 timeblocks (intervals), where timeblock++ is effectively +30 seconds.
  20. * @param interval in seconds
  21. * @param for_time a time in seconds to get the current timeblocks
  22. * @return Timeblock given \p for_time using \p interval
  23. */
  24. uint64_t totp_timecode(uint8_t interval, uint64_t for_time) {
  25. return for_time / interval;
  26. }
  27. /**
  28. * @brief Generates an OTP (One Time Password)
  29. * @param algo hashing algorithm to be used
  30. * @param plain_secret plain token secret
  31. * @param plain_secret_length plain token secret length
  32. * @param input input data for OTP code generation
  33. * @return OTP code if code was successfully generated; 0 otherwise
  34. */
  35. uint64_t otp_generate(
  36. TOTP_ALGO algo,
  37. const uint8_t* plain_secret,
  38. size_t plain_secret_length,
  39. uint64_t input) {
  40. uint8_t hmac[HMAC_MAX_RESULT_SIZE] = {0};
  41. uint64_t input_swapped = ByteReverseWord64(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. return i_code;
  52. }
  53. uint64_t totp_at(
  54. TOTP_ALGO algo,
  55. const uint8_t* plain_secret,
  56. size_t plain_secret_length,
  57. uint64_t for_time,
  58. float timezone,
  59. uint8_t interval) {
  60. uint64_t for_time_adjusted =
  61. timezone_offset_apply(for_time, timezone_offset_from_hours(timezone));
  62. return otp_generate(
  63. algo, plain_secret, plain_secret_length, totp_timecode(interval, for_time_adjusted));
  64. }
  65. uint64_t hotp_at(
  66. TOTP_ALGO algo,
  67. const uint8_t* plain_secret,
  68. size_t plain_secret_length,
  69. uint64_t counter) {
  70. return otp_generate(algo, plain_secret, plain_secret_length, counter);
  71. }
  72. static int totp_algo_common(
  73. int type,
  74. const uint8_t* key,
  75. size_t key_length,
  76. const uint8_t* input,
  77. size_t input_length,
  78. uint8_t* output) {
  79. Hmac hmac;
  80. int ret = wc_HmacSetKey(&hmac, type, key, key_length);
  81. if(ret == 0) {
  82. ret = wc_HmacUpdate(&hmac, input, input_length);
  83. }
  84. if(ret == 0) {
  85. ret = wc_HmacFinal(&hmac, output);
  86. }
  87. wc_HmacFree(&hmac);
  88. return ret == 0 ? wc_HmacSizeByType(type) : 0;
  89. }
  90. static int totp_algo_sha1(
  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_SHA, key, key_length, input, input_length, output);
  97. }
  98. static int totp_algo_sha256(
  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_SHA256, key, key_length, input, input_length, output);
  105. }
  106. static int totp_algo_sha512(
  107. const uint8_t* key,
  108. size_t key_length,
  109. const uint8_t* input,
  110. size_t input_length,
  111. uint8_t* output) {
  112. return totp_algo_common(WC_SHA512, key, key_length, input, input_length, output);
  113. }
  114. const TOTP_ALGO TOTP_ALGO_SHA1 = (TOTP_ALGO)(&totp_algo_sha1);
  115. const TOTP_ALGO TOTP_ALGO_SHA256 = (TOTP_ALGO)(&totp_algo_sha256);
  116. const TOTP_ALGO TOTP_ALGO_SHA512 = (TOTP_ALGO)(&totp_algo_sha512);