crypto.c 4.7 KB

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