crypto_v1.c 5.0 KB

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