totp.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #pragma once
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #define OTP_ERROR (0)
  5. /**
  6. * @brief Must compute HMAC using passed arguments, output as char array through output.
  7. * \p key is secret key buffer.
  8. * \p key_length is secret key buffer length.
  9. * \p input is input buffer.
  10. * \p input_length is input buffer length.
  11. * \p output is an output buffer of the resulting HMAC operation.
  12. * Must return 0 if error, or the length in bytes of the HMAC operation.
  13. */
  14. typedef int (*TOTP_ALGO)(
  15. const uint8_t* key,
  16. size_t key_length,
  17. const uint8_t* input,
  18. size_t input_length,
  19. uint8_t* output);
  20. /**
  21. * @brief Computes HMAC using SHA1
  22. */
  23. extern const TOTP_ALGO TOTP_ALGO_SHA1;
  24. /**
  25. * @brief Computes HMAC using SHA256
  26. */
  27. extern const TOTP_ALGO TOTP_ALGO_SHA256;
  28. /**
  29. * @brief Computes HMAC using SHA512
  30. */
  31. extern const TOTP_ALGO TOTP_ALGO_SHA512;
  32. /**
  33. * @brief Generates a TOTP key using the totp algorithm.
  34. * @param algo hashing algorithm to be used
  35. * @param plain_secret plain token secret
  36. * @param plain_secret_length plain token secret length
  37. * @param for_time the time the generated key will be created for
  38. * @param timezone UTC timezone adjustment for the generated key
  39. * @param interval token lifetime in seconds
  40. * @return TOTP code if code was successfully generated; 0 otherwise
  41. */
  42. uint64_t totp_at(
  43. TOTP_ALGO algo,
  44. const uint8_t* plain_secret,
  45. size_t plain_secret_length,
  46. uint64_t for_time,
  47. float timezone,
  48. uint8_t interval);
  49. /**
  50. * @brief Generates a HOTP key using the hotp algorithm.
  51. * @param algo hashing algorithm to be used
  52. * @param plain_secret plain token secret
  53. * @param plain_secret_length plain token secret length
  54. * @param counter the HOTP counter
  55. * @return HOTP code if code was successfully generated; 0 otherwise
  56. */
  57. uint64_t hotp_at(
  58. TOTP_ALGO algo,
  59. const uint8_t* plain_secret,
  60. size_t plain_secret_length,
  61. uint64_t counter);