totp.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_RESULT_SIZE HMAC_SHA512_RESULT_SIZE
  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 plain_secret plain token secret
  29. * @param plain_secret_length plain token secret length
  30. * @param input input data for OTP code generation
  31. * @return OTP code if code was successfully generated; 0 otherwise
  32. */
  33. uint64_t otp_generate(
  34. TOTP_ALGO algo,
  35. const uint8_t* plain_secret,
  36. size_t plain_secret_length,
  37. uint64_t input) {
  38. uint8_t hmac[HMAC_MAX_RESULT_SIZE] = {0};
  39. uint64_t input_swapped = swap_uint64(input);
  40. int hmac_len =
  41. (*algo)(plain_secret, plain_secret_length, (uint8_t*)&input_swapped, 8, &hmac[0]);
  42. if(hmac_len == 0) {
  43. return OTP_ERROR;
  44. }
  45. uint64_t offset = (hmac[hmac_len - 1] & 0xF);
  46. uint64_t i_code =
  47. ((hmac[offset] & 0x7F) << 24 | (hmac[offset + 1] & 0xFF) << 16 |
  48. (hmac[offset + 2] & 0xFF) << 8 | (hmac[offset + 3] & 0xFF));
  49. return i_code;
  50. }
  51. uint64_t totp_at(
  52. TOTP_ALGO algo,
  53. const uint8_t* plain_secret,
  54. size_t plain_secret_length,
  55. uint64_t for_time,
  56. float timezone,
  57. uint8_t interval) {
  58. uint64_t for_time_adjusted =
  59. timezone_offset_apply(for_time, timezone_offset_from_hours(timezone));
  60. return otp_generate(
  61. algo, plain_secret, plain_secret_length, totp_timecode(interval, for_time_adjusted));
  62. }
  63. static int totp_algo_sha1(
  64. const uint8_t* key,
  65. size_t key_length,
  66. const uint8_t* input,
  67. size_t input_length,
  68. uint8_t* output) {
  69. hmac_sha1(key, key_length, input, input_length, output);
  70. return HMAC_SHA1_RESULT_SIZE;
  71. }
  72. static int totp_algo_sha256(
  73. const uint8_t* key,
  74. size_t key_length,
  75. const uint8_t* input,
  76. size_t input_length,
  77. uint8_t* output) {
  78. hmac_sha256(key, key_length, input, input_length, output);
  79. return HMAC_SHA256_RESULT_SIZE;
  80. }
  81. static int totp_algo_sha512(
  82. const uint8_t* key,
  83. size_t key_length,
  84. const uint8_t* input,
  85. size_t input_length,
  86. uint8_t* output) {
  87. hmac_sha512(key, key_length, input, input_length, output);
  88. return HMAC_SHA512_RESULT_SIZE;
  89. }
  90. const TOTP_ALGO TOTP_ALGO_SHA1 = (TOTP_ALGO)(&totp_algo_sha1);
  91. const TOTP_ALGO TOTP_ALGO_SHA256 = (TOTP_ALGO)(&totp_algo_sha256);
  92. const TOTP_ALGO TOTP_ALGO_SHA512 = (TOTP_ALGO)(&totp_algo_sha512);