totp.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 OTP key using the totp algorithm.
  34. * @param algo hashing algorithm to be used
  35. * @param digits desired TOTP code length
  36. * @param plain_secret plain token secret
  37. * @param plain_secret_length plain token secret length
  38. * @param for_time the time the generated key will be created for
  39. * @param timezone UTC timezone adjustment for the generated key
  40. * @param interval token lifetime in seconds
  41. * @return TOTP code if code was successfully generated; 0 otherwise
  42. */
  43. uint32_t totp_at(
  44. TOTP_ALGO algo,
  45. uint8_t digits,
  46. const uint8_t* plain_secret,
  47. size_t plain_secret_length,
  48. uint64_t for_time,
  49. float timezone,
  50. uint8_t interval);