token_info.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #pragma once
  2. #include <inttypes.h>
  3. #define TOTP_TOKEN_DURATION_DEFAULT 30
  4. typedef uint8_t TokenHashAlgo;
  5. typedef uint8_t TokenDigitsCount;
  6. /**
  7. * @brief Hashing algorithm to be used to generate token
  8. */
  9. enum TokenHashAlgos {
  10. /**
  11. * @brief SHA1 hashing algorithm
  12. */
  13. SHA1,
  14. /**
  15. * @brief SHA256 hashing algorithm
  16. */
  17. SHA256,
  18. /**
  19. * @brief SHA512 hashing algorithm
  20. */
  21. SHA512
  22. };
  23. /**
  24. * @brief Token digits count to be generated.
  25. */
  26. enum TokenDigitsCounts {
  27. /**
  28. * @brief 6 digits
  29. */
  30. TOTP_6_DIGITS = 6,
  31. /**
  32. * @brief 8 digits
  33. */
  34. TOTP_8_DIGITS = 8
  35. };
  36. #define TOTP_TOKEN_DIGITS_MAX_COUNT 8
  37. /**
  38. * @brief TOTP token information
  39. */
  40. typedef struct {
  41. /**
  42. * @brief Encrypted token secret
  43. */
  44. uint8_t* token;
  45. /**
  46. * @brief Encrypted token secret length
  47. */
  48. size_t token_length;
  49. /**
  50. * @brief User-friendly token name
  51. */
  52. char* name;
  53. /**
  54. * @brief Hashing algorithm
  55. */
  56. TokenHashAlgo algo;
  57. /**
  58. * @brief Desired TOTP token length
  59. */
  60. TokenDigitsCount digits;
  61. /**
  62. * @brief Desired TOTP token duration in seconds
  63. */
  64. uint8_t duration;
  65. } TokenInfo;
  66. /**
  67. * @brief Allocates a new instance of \c TokenInfo
  68. * @return
  69. */
  70. TokenInfo* token_info_alloc();
  71. /**
  72. * @brief Disposes all the resources allocated by the given \c TokenInfo instance
  73. * @param token_info instance to be disposed
  74. */
  75. void token_info_free(TokenInfo* token_info);
  76. /**
  77. * @brief Encrypts & sets plain token secret to the given instance of \c TokenInfo
  78. * @param token_info instance where secret should be updated
  79. * @param base32_token_secret plain token secret in Base32 format
  80. * @param token_secret_length plain token secret length
  81. * @param iv initialization vecor (IV) to be used for encryption
  82. * @return \c true if token successfully set; \c false otherwise
  83. */
  84. bool token_info_set_secret(
  85. TokenInfo* token_info,
  86. const char* base32_token_secret,
  87. size_t token_secret_length,
  88. const uint8_t* iv);
  89. /**
  90. * @brief Sets token digits count from \c uint8_t value
  91. * @param token_info instance whichs token digits count length should be updated
  92. * @param digits desired token digits count length
  93. * @return \c true if token digits count length has been updated; \c false otherwise
  94. */
  95. bool token_info_set_digits_from_int(TokenInfo* token_info, uint8_t digits);
  96. /**
  97. * @brief Sets token duration from \c uint8_t value
  98. * @param token_info instance whichs token digits count length should be updated
  99. * @param duration desired token duration in seconds
  100. * @return \c true if token duration has been updated; \c false otherwise
  101. */
  102. bool token_info_set_duration_from_int(TokenInfo* token_info, uint8_t duration);