token_info.h 2.1 KB

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