token_info.c 1.4 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. #include "../services/crypto/crypto.h"
  8. TokenInfo* token_info_alloc() {
  9. TokenInfo* tokenInfo = malloc(sizeof(TokenInfo));
  10. tokenInfo->algo = SHA1;
  11. tokenInfo->digits = TOTP_6_DIGITS;
  12. return tokenInfo;
  13. }
  14. void token_info_free(TokenInfo* token_info) {
  15. if(token_info == NULL) return;
  16. free(token_info->name);
  17. free(token_info->token);
  18. free(token_info);
  19. }
  20. bool token_info_set_secret(
  21. TokenInfo* token_info,
  22. const char* base32_token_secret,
  23. uint8_t token_secret_length,
  24. uint8_t* iv) {
  25. uint8_t* plain_secret = malloc(token_secret_length);
  26. int plain_secret_length =
  27. base32_decode((uint8_t*)base32_token_secret, plain_secret, token_secret_length);
  28. bool result;
  29. if(plain_secret_length >= 0) {
  30. token_info->token =
  31. totp_crypto_encrypt(plain_secret, plain_secret_length, iv, &token_info->token_length);
  32. result = true;
  33. } else {
  34. result = false;
  35. }
  36. memset(plain_secret, 0, token_secret_length);
  37. free(plain_secret);
  38. return result;
  39. }
  40. uint8_t token_info_get_digits_count(TokenInfo* token_info) {
  41. switch(token_info->digits) {
  42. case TOTP_6_DIGITS:
  43. return 6;
  44. case TOTP_8_DIGITS:
  45. return 8;
  46. }
  47. return 6;
  48. }