token_info.h 6.9 KB

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