token_info.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. void 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. token_info->token =
  29. totp_crypto_encrypt(plain_secret, plain_secret_length, iv, &token_info->token_length);
  30. memset(plain_secret, 0, token_secret_length);
  31. free(plain_secret);
  32. }
  33. uint8_t token_info_get_digits_count(TokenInfo* token_info) {
  34. switch(token_info->digits) {
  35. case TOTP_6_DIGITS:
  36. return 6;
  37. case TOTP_8_DIGITS:
  38. return 8;
  39. }
  40. return 6;
  41. }