totp.h 1006 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #pragma once
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #define OTP_ERROR (0)
  5. /*
  6. Must compute HMAC using passed arguments,
  7. output as char array through output.
  8. key is secret key.
  9. input is input number.
  10. output is an output buffer of the resulting HMAC operation.
  11. Must return 0 if error, or the length in bytes of the HMAC operation.
  12. */
  13. typedef int (*TOTP_ALGO)(
  14. const uint8_t* key,
  15. size_t key_length,
  16. const uint8_t* input,
  17. size_t input_length,
  18. uint8_t* output);
  19. /*
  20. Computes HMAC using SHA1
  21. */
  22. extern const TOTP_ALGO TOTP_ALGO_SHA1;
  23. /*
  24. Computes HMAC using SHA256
  25. */
  26. extern const TOTP_ALGO TOTP_ALGO_SHA256;
  27. /*
  28. Computes HMAC using SHA512
  29. */
  30. extern const TOTP_ALGO TOTP_ALGO_SHA512;
  31. /*
  32. Computes TOTP token
  33. Returns:
  34. TOTP token on success
  35. 0 otherwise
  36. */
  37. uint32_t totp_at(
  38. TOTP_ALGO algo,
  39. uint8_t digits,
  40. const uint8_t* plain_secret,
  41. size_t plain_secret_length,
  42. uint64_t for_time,
  43. float timezone,
  44. uint8_t interval);