token_info.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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(TokenInfo* token_info, const char* base32_token_secret, uint8_t token_secret_length, uint8_t* iv) {
  21. uint8_t* plain_secret = malloc(token_secret_length);
  22. int plain_secret_length = base32_decode((uint8_t *)base32_token_secret, plain_secret, token_secret_length);
  23. token_info->token = totp_crypto_encrypt(plain_secret, plain_secret_length, iv, &token_info->token_length);
  24. memset(plain_secret, 0, token_secret_length);
  25. free(plain_secret);
  26. }
  27. uint8_t token_info_get_digits_count(TokenInfo* token_info) {
  28. switch (token_info->digits) {
  29. case TOTP_6_DIGITS: return 6;
  30. case TOTP_8_DIGITS: return 8;
  31. }
  32. return 6;
  33. }