crypto_v1.c 5.0 KB

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