token_info.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 "../lib/base32/base32.h"
  7. #include "../services/crypto/crypto.h"
  8. #include "../lib/polyfills/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. bool token_info_set_digits_from_int(TokenInfo* token_info, uint8_t digits) {
  44. switch(digits) {
  45. case 6:
  46. token_info->digits = TOTP_6_DIGITS;
  47. return true;
  48. case 8:
  49. token_info->digits = TOTP_8_DIGITS;
  50. return true;
  51. default:
  52. break;
  53. }
  54. return false;
  55. }