token_info.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #pragma once
  2. #include <inttypes.h>
  3. #include <stdbool.h>
  4. #include <furi/core/string.h>
  5. #define TOTP_TOKEN_DURATION_DEFAULT (30)
  6. #define TOTP_TOKEN_DURATION_MIN (15)
  7. #define TOTP_TOKEN_DURATION_MAX UINT8_MAX
  8. #define TOTP_TOKEN_ALGO_SHA1_NAME "sha1"
  9. #define TOTP_TOKEN_ALGO_STEAM_NAME "steam"
  10. #define TOTP_TOKEN_ALGO_SHA256_NAME "sha256"
  11. #define TOTP_TOKEN_ALGO_SHA512_NAME "sha512"
  12. #define PLAIN_TOKEN_ENCODING_BASE32_NAME "base32"
  13. #define PLAIN_TOKEN_ENCODING_BASE64_NAME "base64"
  14. #define TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME "none"
  15. #define TOTP_TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME "enter"
  16. #define TOTP_TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME "tab"
  17. #define TOTP_TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER_NAME "slower"
  18. #define TOTP_TOKEN_DIGITS_MAX_COUNT (8)
  19. typedef uint8_t TokenHashAlgo;
  20. typedef uint8_t TokenDigitsCount;
  21. typedef uint8_t TokenAutomationFeature;
  22. typedef uint8_t PlainTokenSecretEncoding;
  23. /**
  24. * @brief Hashing algorithm to be used to generate token
  25. */
  26. enum TokenHashAlgos {
  27. /**
  28. * @brief SHA1 hashing algorithm
  29. */
  30. SHA1 = 0,
  31. /**
  32. * @brief SHA256 hashing algorithm
  33. */
  34. SHA256 = 1,
  35. /**
  36. * @brief SHA512 hashing algorithm
  37. */
  38. SHA512 = 2,
  39. /**
  40. * @brief Algorithm used by Steam (Valve)
  41. */
  42. STEAM = 3
  43. };
  44. /**
  45. * @brief Token digits count to be generated.
  46. */
  47. enum TokenDigitsCounts {
  48. /**
  49. * @brief 5 digits
  50. */
  51. TotpFiveDigitsCount = 5,
  52. /**
  53. * @brief 6 digits
  54. */
  55. TotpSixDigitsCount = 6,
  56. /**
  57. * @brief 8 digits
  58. */
  59. TotpEightDigitsCount = 8
  60. };
  61. /**
  62. * @brief Token automation features.
  63. */
  64. enum TokenAutomationFeatures {
  65. /**
  66. * @brief No features enabled
  67. */
  68. TokenAutomationFeatureNone = 0b000,
  69. /**
  70. * @brief Press "Enter" key at the end as a part of token input automation
  71. */
  72. TokenAutomationFeatureEnterAtTheEnd = 0b001,
  73. /**
  74. * @brief Press "Tab" key at the end as a part of token input automation
  75. */
  76. TokenAutomationFeatureTabAtTheEnd = 0b010,
  77. /**
  78. * @brief Press keys slower and wait longer between keystrokes
  79. */
  80. TokenAutomationFeatureTypeSlower = 0b100
  81. };
  82. /**
  83. * @brief Plain token secret encodings.
  84. */
  85. enum PlainTokenSecretEncodings {
  86. /**
  87. * @brief Base32 encoding
  88. */
  89. PlainTokenSecretEncodingBase32 = 0,
  90. /**
  91. * @brief Base64 encoding
  92. */
  93. PlainTokenSecretEncodingBase64 = 1
  94. };
  95. /**
  96. * @brief TOTP token information
  97. */
  98. typedef struct {
  99. /**
  100. * @brief Encrypted token secret
  101. */
  102. uint8_t* token;
  103. /**
  104. * @brief Encrypted token secret length
  105. */
  106. size_t token_length;
  107. /**
  108. * @brief User-friendly token name
  109. */
  110. FuriString* name;
  111. /**
  112. * @brief Hashing algorithm
  113. */
  114. TokenHashAlgo algo;
  115. /**
  116. * @brief Desired TOTP token length
  117. */
  118. TokenDigitsCount digits;
  119. /**
  120. * @brief Desired TOTP token duration in seconds
  121. */
  122. uint8_t duration;
  123. /**
  124. * @brief Token input automation features
  125. */
  126. TokenAutomationFeature automation_features;
  127. } TokenInfo;
  128. /**
  129. * @brief Allocates a new instance of \c TokenInfo
  130. * @return
  131. */
  132. TokenInfo* token_info_alloc();
  133. /**
  134. * @brief Disposes all the resources allocated by the given \c TokenInfo instance
  135. * @param token_info instance to be disposed
  136. */
  137. void token_info_free(TokenInfo* token_info);
  138. /**
  139. * @brief Encrypts & sets plain token secret to the given instance of \c TokenInfo
  140. * @param token_info instance where secret should be updated
  141. * @param plain_token_secret plain token secret
  142. * @param token_secret_length plain token secret length
  143. * @param plain_token_secret_encoding plain token secret encoding
  144. * @param iv initialization vecor (IV) to be used for encryption
  145. * @return \c true if token successfully set; \c false otherwise
  146. */
  147. bool token_info_set_secret(
  148. TokenInfo* token_info,
  149. const char* plain_token_secret,
  150. size_t token_secret_length,
  151. PlainTokenSecretEncoding plain_token_secret_encoding,
  152. const uint8_t* iv);
  153. /**
  154. * @brief Sets token digits count from \c uint8_t value
  155. * @param token_info instance whichs token digits count length should be updated
  156. * @param digits desired token digits count length
  157. * @return \c true if token digits count length has been updated; \c false otherwise
  158. */
  159. bool token_info_set_digits_from_int(TokenInfo* token_info, uint8_t digits);
  160. /**
  161. * @brief Sets token duration from \c uint8_t value
  162. * @param token_info instance whichs token digits count length should be updated
  163. * @param duration desired token duration in seconds
  164. * @return \c true if token duration has been updated; \c false otherwise
  165. */
  166. bool token_info_set_duration_from_int(TokenInfo* token_info, uint8_t duration);
  167. /**
  168. * @brief Sets token hashing algorithm from \c str value
  169. * @param token_info instance whichs token hashing algorithm should be updated
  170. * @param str desired token algorithm
  171. * @return \c true if token hashing algorithm has been updated; \c false otherwise
  172. */
  173. bool token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str);
  174. /**
  175. * @brief Sets token hashing algorithm from \c algo_code code
  176. * @param token_info instance whichs token hashing algorithm should be updated
  177. * @param algo_code desired token algorithm code
  178. * @return \c true if token hashing algorithm has been updated; \c false otherwise
  179. */
  180. bool token_info_set_algo_from_int(TokenInfo* token_info, uint8_t algo_code);
  181. /**
  182. * @brief Gets token hahsing algorithm name as C-string
  183. * @param token_info instance which token hahsing algorithm name should be returned
  184. * @return token hashing algorithm name as C-string
  185. */
  186. char* token_info_get_algo_as_cstr(const TokenInfo* token_info);
  187. /**
  188. * @brief Sets token automation feature from \c str value
  189. * @param token_info instance whichs token automation feature should be updated
  190. * @param str desired token automation feature
  191. * @return \c true if token automation feature has been set; \c false otherwise
  192. */
  193. bool token_info_set_automation_feature_from_str(TokenInfo* token_info, const FuriString* str);
  194. /**
  195. * @brief Clones \c TokenInfo instance
  196. * @param src instance to clone
  197. * @return cloned instance
  198. */
  199. TokenInfo* token_info_clone(const TokenInfo* src);
  200. /**
  201. * @brief Sets default values to all the properties of \c token_info
  202. * @param token_info instance to set defaults to
  203. */
  204. void token_info_set_defaults(TokenInfo* token_info);