token_info.h 2.2 KB

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