token_info.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #pragma once
  2. #include <inttypes.h>
  3. #include <stdbool.h>
  4. #include <furi/furi.h>
  5. #define TOTP_TOKEN_DURATION_DEFAULT 30
  6. #define TOTP_TOKEN_ALGO_SHA1_NAME "sha1"
  7. #define TOTP_TOKEN_ALGO_SHA256_NAME "sha256"
  8. #define TOTP_TOKEN_ALGO_SHA512_NAME "sha512"
  9. #define TOTP_TOKEN_MAX_LENGTH 255
  10. #define TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME "none"
  11. #define TOTP_TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME "enter"
  12. typedef uint8_t TokenHashAlgo;
  13. typedef uint8_t TokenDigitsCount;
  14. typedef uint8_t TokenAutomationFeature;
  15. /**
  16. * @brief Hashing algorithm to be used to generate token
  17. */
  18. enum TokenHashAlgos {
  19. /**
  20. * @brief SHA1 hashing algorithm
  21. */
  22. SHA1,
  23. /**
  24. * @brief SHA256 hashing algorithm
  25. */
  26. SHA256,
  27. /**
  28. * @brief SHA512 hashing algorithm
  29. */
  30. SHA512
  31. };
  32. /**
  33. * @brief Token digits count to be generated.
  34. */
  35. enum TokenDigitsCounts {
  36. /**
  37. * @brief 6 digits
  38. */
  39. TOTP_6_DIGITS = 6,
  40. /**
  41. * @brief 8 digits
  42. */
  43. TOTP_8_DIGITS = 8
  44. };
  45. /**
  46. * @brief Token automation features.
  47. */
  48. enum TokenAutomationFeatures {
  49. /**
  50. * @brief No features enabled
  51. */
  52. TOKEN_AUTOMATION_FEATURE_NONE = 0b00,
  53. /**
  54. * @brief Press "Enter" key at the end as a part of token input automation
  55. */
  56. TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END = 0b01
  57. };
  58. #define TOTP_TOKEN_DIGITS_MAX_COUNT 8
  59. /**
  60. * @brief TOTP token information
  61. */
  62. typedef struct {
  63. /**
  64. * @brief Encrypted token secret
  65. */
  66. uint8_t* token;
  67. /**
  68. * @brief Encrypted token secret length
  69. */
  70. size_t token_length;
  71. /**
  72. * @brief User-friendly token name
  73. */
  74. char* name;
  75. /**
  76. * @brief Hashing algorithm
  77. */
  78. TokenHashAlgo algo;
  79. /**
  80. * @brief Desired TOTP token length
  81. */
  82. TokenDigitsCount digits;
  83. /**
  84. * @brief Desired TOTP token duration in seconds
  85. */
  86. uint8_t duration;
  87. /**
  88. * @brief Token input automation features
  89. */
  90. TokenAutomationFeature automation_features;
  91. } TokenInfo;
  92. /**
  93. * @brief Allocates a new instance of \c TokenInfo
  94. * @return
  95. */
  96. TokenInfo* token_info_alloc();
  97. /**
  98. * @brief Disposes all the resources allocated by the given \c TokenInfo instance
  99. * @param token_info instance to be disposed
  100. */
  101. void token_info_free(TokenInfo* token_info);
  102. /**
  103. * @brief Encrypts & sets plain token secret to the given instance of \c TokenInfo
  104. * @param token_info instance where secret should be updated
  105. * @param base32_token_secret plain token secret in Base32 format
  106. * @param token_secret_length plain token secret length
  107. * @param iv initialization vecor (IV) to be used for encryption
  108. * @return \c true if token successfully set; \c false otherwise
  109. */
  110. bool token_info_set_secret(
  111. TokenInfo* token_info,
  112. const char* base32_token_secret,
  113. size_t token_secret_length,
  114. const uint8_t* iv);
  115. /**
  116. * @brief Sets token digits count from \c uint8_t value
  117. * @param token_info instance whichs token digits count length should be updated
  118. * @param digits desired token digits count length
  119. * @return \c true if token digits count length has been updated; \c false otherwise
  120. */
  121. bool token_info_set_digits_from_int(TokenInfo* token_info, uint8_t digits);
  122. /**
  123. * @brief Sets token duration from \c uint8_t value
  124. * @param token_info instance whichs token digits count length should be updated
  125. * @param duration desired token duration in seconds
  126. * @return \c true if token duration has been updated; \c false otherwise
  127. */
  128. bool token_info_set_duration_from_int(TokenInfo* token_info, uint8_t duration);
  129. /**
  130. * @brief Sets token hashing algorithm from \c str value
  131. * @param token_info instance whichs token hashing algorithm should be updated
  132. * @param str desired token algorithm
  133. * @return \c true if token hahsing algorithm has been updated; \c false otherwise
  134. */
  135. bool token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str);
  136. /**
  137. * @brief Gets token hahsing algorithm name as C-string
  138. * @param token_info instance which token hahsing algorithm name should be returned
  139. * @return token hashing algorithm name as C-string
  140. */
  141. char* token_info_get_algo_as_cstr(const TokenInfo* token_info);
  142. /**
  143. * @brief Sets token automation feature from \c str value
  144. * @param token_info instance whichs token automation feature should be updated
  145. * @param str desired token automation feature
  146. * @return \c true if token automation feature has been set; \c false otherwise
  147. */
  148. bool token_info_set_automation_feature_from_str(TokenInfo* token_info, const FuriString* str);
  149. /**
  150. * @brief Clones \c TokenInfo instance
  151. * @param src instance to clone
  152. * @return cloned instance
  153. */
  154. TokenInfo* token_info_clone(const TokenInfo* src);