crypto.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "crypto.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include "../config/config.h"
  5. #include "../../types/common.h"
  6. #include "memset_s.h"
  7. #define CRYPTO_KEY_SLOT 2
  8. #define CRYPTO_VERIFY_KEY "FFF_Crypto_pass"
  9. #define CRYPTO_VERIFY_KEY_LENGTH 16
  10. #define CRYPTO_ALIGNMENT_FACTOR 16
  11. uint8_t* totp_crypto_encrypt(
  12. const uint8_t* plain_data,
  13. const size_t plain_data_length,
  14. const uint8_t* iv,
  15. size_t* encrypted_data_length) {
  16. uint8_t* encrypted_data;
  17. size_t remain = plain_data_length % CRYPTO_ALIGNMENT_FACTOR;
  18. if(remain) {
  19. size_t plain_data_aligned_length = plain_data_length - remain + CRYPTO_ALIGNMENT_FACTOR;
  20. uint8_t* plain_data_aligned = malloc(plain_data_aligned_length);
  21. memset(plain_data_aligned, 0, plain_data_aligned_length);
  22. memcpy(plain_data_aligned, plain_data, plain_data_length);
  23. encrypted_data = malloc(plain_data_aligned_length);
  24. *encrypted_data_length = plain_data_aligned_length;
  25. furi_hal_crypto_store_load_key(CRYPTO_KEY_SLOT, iv);
  26. furi_hal_crypto_encrypt(plain_data_aligned, encrypted_data, plain_data_aligned_length);
  27. furi_hal_crypto_store_unload_key(CRYPTO_KEY_SLOT);
  28. memset_s(plain_data_aligned, sizeof(plain_data_aligned), 0, plain_data_aligned_length);
  29. free(plain_data_aligned);
  30. } else {
  31. encrypted_data = malloc(plain_data_length);
  32. *encrypted_data_length = plain_data_length;
  33. furi_hal_crypto_store_load_key(CRYPTO_KEY_SLOT, iv);
  34. furi_hal_crypto_encrypt(plain_data, encrypted_data, plain_data_length);
  35. furi_hal_crypto_store_unload_key(CRYPTO_KEY_SLOT);
  36. }
  37. return encrypted_data;
  38. }
  39. uint8_t* totp_crypto_decrypt(
  40. const uint8_t* encrypted_data,
  41. const size_t encrypted_data_length,
  42. const uint8_t* iv,
  43. size_t* decrypted_data_length) {
  44. *decrypted_data_length = encrypted_data_length;
  45. uint8_t* decrypted_data = malloc(*decrypted_data_length);
  46. furi_hal_crypto_store_load_key(CRYPTO_KEY_SLOT, iv);
  47. furi_hal_crypto_decrypt(encrypted_data, decrypted_data, encrypted_data_length);
  48. furi_hal_crypto_store_unload_key(CRYPTO_KEY_SLOT);
  49. return decrypted_data;
  50. }
  51. void totp_crypto_seed_iv(PluginState* plugin_state, const uint8_t* pin, uint8_t pin_length) {
  52. if(plugin_state->crypto_verify_data == NULL) {
  53. FURI_LOG_D(LOGGING_TAG, "Generating new IV");
  54. furi_hal_random_fill_buf(&plugin_state->base_iv[0], TOTP_IV_SIZE);
  55. }
  56. memcpy(&plugin_state->iv[0], &plugin_state->base_iv[0], TOTP_IV_SIZE);
  57. if(pin != NULL && pin_length > 0) {
  58. uint8_t max_i;
  59. if(pin_length > TOTP_IV_SIZE) {
  60. max_i = TOTP_IV_SIZE;
  61. } else {
  62. max_i = pin_length;
  63. }
  64. for(uint8_t i = 0; i < max_i; i++) {
  65. plugin_state->iv[i] = plugin_state->iv[i] ^ (uint8_t)(pin[i] * (i + 1));
  66. }
  67. } else {
  68. uint8_t max_i;
  69. size_t uid_size = furi_hal_version_uid_size();
  70. if(uid_size > TOTP_IV_SIZE) {
  71. max_i = TOTP_IV_SIZE;
  72. } else {
  73. max_i = uid_size;
  74. }
  75. const uint8_t* uid = furi_hal_version_uid();
  76. for(uint8_t i = 0; i < max_i; i++) {
  77. plugin_state->iv[i] = plugin_state->iv[i] ^ uid[i];
  78. }
  79. }
  80. if(plugin_state->crypto_verify_data == NULL) {
  81. FURI_LOG_D(LOGGING_TAG, "Generating crypto verify data");
  82. plugin_state->crypto_verify_data = malloc(CRYPTO_VERIFY_KEY_LENGTH);
  83. plugin_state->crypto_verify_data_length = CRYPTO_VERIFY_KEY_LENGTH;
  84. Storage* storage = totp_open_storage();
  85. FlipperFormat* config_file = totp_open_config_file(storage);
  86. plugin_state->crypto_verify_data = totp_crypto_encrypt(
  87. (uint8_t*)CRYPTO_VERIFY_KEY,
  88. CRYPTO_VERIFY_KEY_LENGTH,
  89. &plugin_state->iv[0],
  90. &plugin_state->crypto_verify_data_length);
  91. flipper_format_insert_or_update_hex(
  92. config_file, TOTP_CONFIG_KEY_BASE_IV, plugin_state->base_iv, TOTP_IV_SIZE);
  93. flipper_format_insert_or_update_hex(
  94. config_file,
  95. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  96. plugin_state->crypto_verify_data,
  97. CRYPTO_VERIFY_KEY_LENGTH);
  98. plugin_state->pin_set = pin != NULL && pin_length > 0;
  99. flipper_format_insert_or_update_bool(
  100. config_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1);
  101. totp_close_config_file(config_file);
  102. totp_close_storage();
  103. }
  104. }
  105. bool totp_crypto_verify_key(const PluginState* plugin_state) {
  106. size_t decrypted_key_length;
  107. const uint8_t* decrypted_key = totp_crypto_decrypt(
  108. plugin_state->crypto_verify_data,
  109. plugin_state->crypto_verify_data_length,
  110. &plugin_state->iv[0],
  111. &decrypted_key_length);
  112. bool key_valid = true;
  113. for(uint8_t i = 0; i < CRYPTO_VERIFY_KEY_LENGTH && key_valid; i++) {
  114. if(decrypted_key[i] != CRYPTO_VERIFY_KEY[i]) key_valid = false;
  115. }
  116. return key_valid;
  117. }