token_info.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <furi/furi.h>
  2. #include <furi_hal.h>
  3. #include "token_info.h"
  4. #include "stdlib.h"
  5. #include "common.h"
  6. #include "../lib/base32/base32.h"
  7. #include "../services/crypto/crypto.h"
  8. #include "../lib/polyfills/memset_s.h"
  9. TokenInfo* token_info_alloc() {
  10. TokenInfo* tokenInfo = malloc(sizeof(TokenInfo));
  11. furi_check(tokenInfo != NULL);
  12. tokenInfo->algo = SHA1;
  13. tokenInfo->digits = TOTP_6_DIGITS;
  14. tokenInfo->duration = TOTP_TOKEN_DURATION_DEFAULT;
  15. return tokenInfo;
  16. }
  17. void token_info_free(TokenInfo* token_info) {
  18. if(token_info == NULL) return;
  19. free(token_info->name);
  20. free(token_info->token);
  21. free(token_info);
  22. }
  23. bool token_info_set_secret(
  24. TokenInfo* token_info,
  25. const char* base32_token_secret,
  26. size_t token_secret_length,
  27. const uint8_t* iv) {
  28. if(token_secret_length == 0) return false;
  29. uint8_t* plain_secret = malloc(token_secret_length);
  30. furi_check(plain_secret != NULL);
  31. int plain_secret_length =
  32. base32_decode((const uint8_t*)base32_token_secret, plain_secret, token_secret_length);
  33. bool result;
  34. if(plain_secret_length > 0) {
  35. token_info->token =
  36. totp_crypto_encrypt(plain_secret, plain_secret_length, iv, &token_info->token_length);
  37. result = true;
  38. } else {
  39. result = false;
  40. }
  41. memset_s(plain_secret, token_secret_length, 0, token_secret_length);
  42. free(plain_secret);
  43. return result;
  44. }
  45. bool token_info_set_digits_from_int(TokenInfo* token_info, uint8_t digits) {
  46. switch(digits) {
  47. case 6:
  48. token_info->digits = TOTP_6_DIGITS;
  49. return true;
  50. case 8:
  51. token_info->digits = TOTP_8_DIGITS;
  52. return true;
  53. default:
  54. break;
  55. }
  56. return false;
  57. }
  58. bool token_info_set_duration_from_int(TokenInfo* token_info, uint8_t duration) {
  59. if(duration >= 15) {
  60. token_info->duration = duration;
  61. return true;
  62. }
  63. return false;
  64. }