token_info.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #include "../services/crypto/crypto.h"
  8. #include "../services/crypto/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. return tokenInfo;
  15. }
  16. void token_info_free(TokenInfo* token_info) {
  17. if(token_info == NULL) return;
  18. free(token_info->name);
  19. free(token_info->token);
  20. free(token_info);
  21. }
  22. bool token_info_set_secret(
  23. TokenInfo* token_info,
  24. const char* base32_token_secret,
  25. size_t token_secret_length,
  26. const uint8_t* iv) {
  27. uint8_t* plain_secret = malloc(token_secret_length);
  28. furi_check(plain_secret != NULL);
  29. int plain_secret_length =
  30. base32_decode((const uint8_t*)base32_token_secret, plain_secret, token_secret_length);
  31. bool result;
  32. if(plain_secret_length >= 0) {
  33. token_info->token =
  34. totp_crypto_encrypt(plain_secret, plain_secret_length, iv, &token_info->token_length);
  35. result = true;
  36. } else {
  37. result = false;
  38. }
  39. memset_s(plain_secret, token_secret_length, 0, token_secret_length);
  40. free(plain_secret);
  41. return result;
  42. }
  43. uint8_t token_info_get_digits_count(const TokenInfo* token_info) {
  44. switch(token_info->digits) {
  45. case TOTP_6_DIGITS:
  46. return 6;
  47. case TOTP_8_DIGITS:
  48. return 8;
  49. default:
  50. break;
  51. }
  52. return 6;
  53. }