token_info.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. if(token_secret_length == 0) return false;
  28. uint8_t* plain_secret = malloc(token_secret_length);
  29. furi_check(plain_secret != NULL);
  30. int plain_secret_length =
  31. base32_decode((const uint8_t*)base32_token_secret, plain_secret, token_secret_length);
  32. bool result;
  33. if(plain_secret_length > 0) {
  34. token_info->token =
  35. totp_crypto_encrypt(plain_secret, plain_secret_length, iv, &token_info->token_length);
  36. result = true;
  37. } else {
  38. result = false;
  39. }
  40. memset_s(plain_secret, token_secret_length, 0, token_secret_length);
  41. free(plain_secret);
  42. return result;
  43. }
  44. bool token_info_set_digits_from_int(TokenInfo* token_info, uint8_t digits) {
  45. switch(digits) {
  46. case 6:
  47. token_info->digits = TOTP_6_DIGITS;
  48. return true;
  49. case 8:
  50. token_info->digits = TOTP_8_DIGITS;
  51. return true;
  52. default:
  53. break;
  54. }
  55. return false;
  56. }
  57. bool token_info_set_duration_from_int(TokenInfo* token_info, uint8_t duration) {
  58. if(duration >= 15) {
  59. token_info->duration = duration;
  60. return true;
  61. }
  62. return false;
  63. }