token_info.h 4.8 KB

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