crypto.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. furi_check(plain_data_aligned != NULL);
  22. memset(plain_data_aligned, 0, plain_data_aligned_length);
  23. memcpy(plain_data_aligned, plain_data, plain_data_length);
  24. encrypted_data = malloc(plain_data_aligned_length);
  25. furi_check(encrypted_data != NULL);
  26. *encrypted_data_length = plain_data_aligned_length;
  27. furi_hal_crypto_store_load_key(CRYPTO_KEY_SLOT, iv);
  28. furi_hal_crypto_encrypt(plain_data_aligned, encrypted_data, plain_data_aligned_length);
  29. furi_hal_crypto_store_unload_key(CRYPTO_KEY_SLOT);
  30. memset_s(plain_data_aligned, plain_data_aligned_length, 0, plain_data_aligned_length);
  31. free(plain_data_aligned);
  32. } else {
  33. encrypted_data = malloc(plain_data_length);
  34. furi_check(encrypted_data != NULL);
  35. *encrypted_data_length = plain_data_length;
  36. furi_hal_crypto_store_load_key(CRYPTO_KEY_SLOT, iv);
  37. furi_hal_crypto_encrypt(plain_data, encrypted_data, plain_data_length);
  38. furi_hal_crypto_store_unload_key(CRYPTO_KEY_SLOT);
  39. }
  40. return encrypted_data;
  41. }
  42. uint8_t* totp_crypto_decrypt(
  43. const uint8_t* encrypted_data,
  44. const size_t encrypted_data_length,
  45. const uint8_t* iv,
  46. size_t* decrypted_data_length) {
  47. *decrypted_data_length = encrypted_data_length;
  48. uint8_t* decrypted_data = malloc(*decrypted_data_length);
  49. furi_check(decrypted_data != NULL);
  50. furi_hal_crypto_store_load_key(CRYPTO_KEY_SLOT, iv);
  51. furi_hal_crypto_decrypt(encrypted_data, decrypted_data, encrypted_data_length);
  52. furi_hal_crypto_store_unload_key(CRYPTO_KEY_SLOT);
  53. return decrypted_data;
  54. }
  55. bool totp_crypto_seed_iv(PluginState* plugin_state, const uint8_t* pin, uint8_t pin_length) {
  56. if(plugin_state->crypto_verify_data == NULL) {
  57. FURI_LOG_D(LOGGING_TAG, "Generating new IV");
  58. furi_hal_random_fill_buf(&plugin_state->base_iv[0], TOTP_IV_SIZE);
  59. }
  60. memcpy(&plugin_state->iv[0], &plugin_state->base_iv[0], TOTP_IV_SIZE);
  61. if(pin != NULL && pin_length > 0) {
  62. uint8_t max_i;
  63. if(pin_length > TOTP_IV_SIZE) {
  64. max_i = TOTP_IV_SIZE;
  65. } else {
  66. max_i = pin_length;
  67. }
  68. for(uint8_t i = 0; i < max_i; i++) {
  69. plugin_state->iv[i] = plugin_state->iv[i] ^ (uint8_t)(pin[i] * (i + 1));
  70. }
  71. } else {
  72. uint8_t max_i;
  73. size_t uid_size = furi_hal_version_uid_size();
  74. if(uid_size > TOTP_IV_SIZE) {
  75. max_i = TOTP_IV_SIZE;
  76. } else {
  77. max_i = uid_size;
  78. }
  79. const uint8_t* uid = furi_hal_version_uid();
  80. for(uint8_t i = 0; i < max_i; i++) {
  81. plugin_state->iv[i] = plugin_state->iv[i] ^ uid[i];
  82. }
  83. }
  84. bool result = true;
  85. if(plugin_state->crypto_verify_data == NULL) {
  86. FURI_LOG_D(LOGGING_TAG, "Generating crypto verify data");
  87. plugin_state->crypto_verify_data = malloc(CRYPTO_VERIFY_KEY_LENGTH);
  88. furi_check(plugin_state->crypto_verify_data != NULL);
  89. plugin_state->crypto_verify_data_length = CRYPTO_VERIFY_KEY_LENGTH;
  90. plugin_state->crypto_verify_data = totp_crypto_encrypt(
  91. (uint8_t*)CRYPTO_VERIFY_KEY,
  92. CRYPTO_VERIFY_KEY_LENGTH,
  93. &plugin_state->iv[0],
  94. &plugin_state->crypto_verify_data_length);
  95. plugin_state->pin_set = pin != NULL && pin_length > 0;
  96. result = totp_config_file_update_crypto_signatures(plugin_state) ==
  97. TotpConfigFileUpdateSuccess;
  98. }
  99. return result;
  100. }
  101. bool totp_crypto_verify_key(const PluginState* plugin_state) {
  102. size_t decrypted_key_length;
  103. const uint8_t* decrypted_key = totp_crypto_decrypt(
  104. plugin_state->crypto_verify_data,
  105. plugin_state->crypto_verify_data_length,
  106. &plugin_state->iv[0],
  107. &decrypted_key_length);
  108. bool key_valid = true;
  109. for(uint8_t i = 0; i < CRYPTO_VERIFY_KEY_LENGTH && key_valid; i++) {
  110. if(decrypted_key[i] != CRYPTO_VERIFY_KEY[i]) key_valid = false;
  111. }
  112. return key_valid;
  113. }