token_info.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 "../services/base32/base32.h"
  7. TokenInfo* token_info_alloc() {
  8. TokenInfo* tokenInfo = malloc(sizeof(TokenInfo));
  9. tokenInfo->algo = SHA1;
  10. tokenInfo->digits = TOTP_6_DIGITS;
  11. return tokenInfo;
  12. }
  13. void token_info_free(TokenInfo* token_info) {
  14. if (token_info == NULL) return;
  15. free(token_info->name);
  16. free(token_info->token);
  17. free(token_info);
  18. }
  19. void token_info_set_secret(TokenInfo* token_info, const char* base32_token_secret, uint8_t token_secret_length, uint8_t* iv) {
  20. uint8_t* plain_secret = malloc(token_secret_length);
  21. int plain_secret_length = base32_decode((uint8_t *)base32_token_secret, plain_secret, token_secret_length);
  22. token_info->token_length = plain_secret_length;
  23. size_t remain = token_info->token_length % 16;
  24. if(remain) {
  25. token_info->token_length = token_info->token_length - remain + 16;
  26. uint8_t* plain_secret_aligned = malloc(token_info->token_length);
  27. memcpy(plain_secret_aligned, plain_secret, plain_secret_length);
  28. memset(plain_secret, 0, plain_secret_length);
  29. free(plain_secret);
  30. plain_secret = plain_secret_aligned;
  31. }
  32. token_info->token = malloc(token_info->token_length);
  33. furi_hal_crypto_store_load_key(CRYPTO_KEY_SLOT, iv);
  34. furi_hal_crypto_encrypt(plain_secret, token_info->token, token_info->token_length);
  35. furi_hal_crypto_store_unload_key(CRYPTO_KEY_SLOT);
  36. memset(plain_secret, 0, token_info->token_length);
  37. free(plain_secret);
  38. }
  39. uint8_t token_info_get_digits_count(TokenInfo* token_info) {
  40. switch (token_info->digits) {
  41. case TOTP_6_DIGITS: return 6;
  42. case TOTP_8_DIGITS: return 8;
  43. }
  44. return 6;
  45. }