totp.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 "../timezone_utils/timezone_utils.h"
  12. /*
  13. Generates the timeblock for a time in seconds.
  14. Timeblocks are the amount of intervals in a given time. For example,
  15. if 1,000,000 seconds has passed for 30 second intervals, you would get
  16. 33,333 timeblocks (intervals), where timeblock++ is effectively +30 seconds.
  17. for_time is a time in seconds to get the current timeblocks
  18. Returns
  19. timeblock given for_time, using data->interval
  20. error, 0
  21. */
  22. uint64_t totp_timecode(uint8_t interval, uint64_t for_time) {
  23. return for_time / interval;
  24. }
  25. /*
  26. Generates an OTP (One Time Password).
  27. input is a number used to generate the OTP
  28. out_str is the null-terminated output string already allocated
  29. Returns
  30. OTP code if otp code was successfully generated
  31. 0 otherwise
  32. */
  33. uint32_t otp_generate(
  34. TOTP_ALGO algo,
  35. uint8_t digits,
  36. const uint8_t* plain_secret,
  37. size_t plain_secret_length,
  38. uint64_t input) {
  39. uint8_t* hmac = malloc(64);
  40. memset(hmac, 0, 64);
  41. uint64_t input_swapped = swap_uint64(input);
  42. int hmac_len = (*algo)(plain_secret, plain_secret_length, (uint8_t*)&input_swapped, 8, hmac);
  43. if(hmac_len == 0) {
  44. free(hmac);
  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. free(hmac);
  53. return i_code;
  54. }
  55. /*
  56. Generates a OTP key using the totp algorithm.
  57. for_time is the time the generated key will be created for
  58. offset is a timeblock adjustment for the generated key
  59. out_str is the null-terminated output string already allocated
  60. Returns
  61. TOTP code if otp code was successfully generated
  62. 0 otherwise
  63. */
  64. uint32_t totp_at(
  65. TOTP_ALGO algo,
  66. uint8_t digits,
  67. const uint8_t* plain_secret,
  68. size_t plain_secret_length,
  69. uint64_t for_time,
  70. float timezone,
  71. uint8_t interval) {
  72. uint64_t for_time_adjusted =
  73. timezone_offset_apply(for_time, timezone_offset_from_hours(timezone));
  74. return otp_generate(
  75. algo,
  76. digits,
  77. plain_secret,
  78. plain_secret_length,
  79. totp_timecode(interval, for_time_adjusted));
  80. }
  81. static int totp_algo_sha1(
  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_sha1(key, key_length, input, input_length, output);
  88. return HMAC_SHA1_RESULT_SIZE;
  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. hmac_sha256(key, key_length, input, input_length, output);
  97. return HMAC_SHA256_RESULT_SIZE;
  98. }
  99. static int totp_algo_sha512(
  100. const uint8_t* key,
  101. size_t key_length,
  102. const uint8_t* input,
  103. size_t input_length,
  104. uint8_t* output) {
  105. hmac_sha512(key, key_length, input, input_length, output);
  106. return HMAC_SHA512_RESULT_SIZE;
  107. }
  108. const TOTP_ALGO TOTP_ALGO_SHA1 = (TOTP_ALGO)(&totp_algo_sha1);
  109. const TOTP_ALGO TOTP_ALGO_SHA256 = (TOTP_ALGO)(&totp_algo_sha256);
  110. const TOTP_ALGO TOTP_ALGO_SHA512 = (TOTP_ALGO)(&totp_algo_sha512);