crypto_v2.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "crypto_v2.h"
  2. #include <furi_hal_crypto.h>
  3. #include <furi_hal_random.h>
  4. #include <furi_hal_version.h>
  5. #include "../../types/common.h"
  6. #include "../hmac/hmac_sha512.h"
  7. #include "memset_s.h"
  8. #include "constants.h"
  9. #define CRYPTO_ALIGNMENT_FACTOR (16)
  10. static const uint8_t* get_device_uid() {
  11. return (const uint8_t*)UID64_BASE; //-V566
  12. }
  13. static uint8_t get_device_uid_length() {
  14. return furi_hal_version_uid_size();
  15. }
  16. static const uint8_t* get_crypto_verify_key() {
  17. return get_device_uid();
  18. }
  19. static uint8_t get_crypto_verify_key_length() {
  20. return get_device_uid_length();
  21. }
  22. uint8_t* totp_crypto_encrypt_v2(
  23. const uint8_t* plain_data,
  24. const size_t plain_data_length,
  25. const uint8_t* iv,
  26. uint8_t key_slot,
  27. size_t* encrypted_data_length) {
  28. uint8_t* encrypted_data;
  29. size_t remain = plain_data_length % CRYPTO_ALIGNMENT_FACTOR;
  30. if(remain) {
  31. size_t plain_data_aligned_length = plain_data_length - remain + CRYPTO_ALIGNMENT_FACTOR;
  32. uint8_t* plain_data_aligned = malloc(plain_data_aligned_length);
  33. furi_check(plain_data_aligned != NULL);
  34. memset(plain_data_aligned, 0, plain_data_aligned_length);
  35. memcpy(plain_data_aligned, plain_data, plain_data_length);
  36. encrypted_data = malloc(plain_data_aligned_length);
  37. furi_check(encrypted_data != NULL);
  38. *encrypted_data_length = plain_data_aligned_length;
  39. furi_check(
  40. furi_hal_crypto_store_load_key(key_slot, iv), "Encryption failed: store_load_key");
  41. furi_check(
  42. furi_hal_crypto_encrypt(plain_data_aligned, encrypted_data, plain_data_aligned_length),
  43. "Encryption failed: encrypt");
  44. furi_check(
  45. furi_hal_crypto_store_unload_key(key_slot), "Encryption failed: store_unload_key");
  46. memset_s(plain_data_aligned, plain_data_aligned_length, 0, plain_data_aligned_length);
  47. free(plain_data_aligned);
  48. } else {
  49. encrypted_data = malloc(plain_data_length);
  50. furi_check(encrypted_data != NULL);
  51. *encrypted_data_length = plain_data_length;
  52. furi_check(
  53. furi_hal_crypto_store_load_key(key_slot, iv), "Encryption failed: store_load_key");
  54. furi_check(
  55. furi_hal_crypto_encrypt(plain_data, encrypted_data, plain_data_length),
  56. "Encryption failed: encrypt");
  57. furi_check(
  58. furi_hal_crypto_store_unload_key(key_slot), "Encryption failed: store_unload_key");
  59. }
  60. return encrypted_data;
  61. }
  62. uint8_t* totp_crypto_decrypt_v2(
  63. const uint8_t* encrypted_data,
  64. const size_t encrypted_data_length,
  65. const uint8_t* iv,
  66. uint8_t key_slot,
  67. size_t* decrypted_data_length) {
  68. *decrypted_data_length = encrypted_data_length;
  69. uint8_t* decrypted_data = malloc(*decrypted_data_length);
  70. furi_check(decrypted_data != NULL);
  71. furi_check(furi_hal_crypto_store_load_key(key_slot, iv), "Decryption failed: store_load_key");
  72. furi_check(
  73. furi_hal_crypto_decrypt(encrypted_data, decrypted_data, encrypted_data_length),
  74. "Decryption failed: decrypt");
  75. furi_check(furi_hal_crypto_store_unload_key(key_slot), "Decryption failed: store_unload_key");
  76. return decrypted_data;
  77. }
  78. CryptoSeedIVResult
  79. totp_crypto_seed_iv_v2(PluginState* plugin_state, const uint8_t* pin, uint8_t pin_length) {
  80. CryptoSeedIVResult result;
  81. if(plugin_state->crypto_verify_data == NULL) {
  82. FURI_LOG_I(LOGGING_TAG, "Generating new IV");
  83. furi_hal_random_fill_buf(&plugin_state->base_iv[0], CRYPTO_IV_LENGTH);
  84. }
  85. memcpy(&plugin_state->iv[0], &plugin_state->base_iv[0], CRYPTO_IV_LENGTH);
  86. const uint8_t* device_uid = get_device_uid();
  87. uint8_t device_uid_length = get_device_uid_length();
  88. uint8_t hmac_key_length = device_uid_length;
  89. if(pin != NULL && pin_length > 0) {
  90. hmac_key_length += pin_length;
  91. }
  92. uint8_t* hmac_key = malloc(hmac_key_length);
  93. furi_check(hmac_key != NULL);
  94. memcpy(hmac_key, device_uid, device_uid_length);
  95. if(pin != NULL && pin_length > 0) {
  96. memcpy(hmac_key + device_uid_length, pin, pin_length);
  97. }
  98. uint8_t hmac[HMAC_SHA512_RESULT_SIZE] = {0};
  99. int hmac_result_code = hmac_sha512(
  100. hmac_key, hmac_key_length, &plugin_state->base_iv[0], CRYPTO_IV_LENGTH, &hmac[0]);
  101. memset_s(hmac_key, hmac_key_length, 0, hmac_key_length);
  102. free(hmac_key);
  103. if(hmac_result_code == 0) {
  104. uint8_t offset =
  105. hmac[HMAC_SHA512_RESULT_SIZE - 1] % (HMAC_SHA512_RESULT_SIZE - CRYPTO_IV_LENGTH - 1);
  106. memcpy(&plugin_state->iv[0], &hmac[offset], CRYPTO_IV_LENGTH);
  107. result = CryptoSeedIVResultFlagSuccess;
  108. if(plugin_state->crypto_verify_data == NULL) {
  109. const uint8_t* crypto_vkey = get_crypto_verify_key();
  110. uint8_t crypto_vkey_length = get_crypto_verify_key_length();
  111. FURI_LOG_I(LOGGING_TAG, "Generating crypto verify data");
  112. plugin_state->crypto_verify_data = malloc(crypto_vkey_length);
  113. furi_check(plugin_state->crypto_verify_data != NULL);
  114. plugin_state->crypto_verify_data_length = crypto_vkey_length;
  115. plugin_state->crypto_verify_data = totp_crypto_encrypt_v2(
  116. crypto_vkey,
  117. crypto_vkey_length,
  118. &plugin_state->iv[0],
  119. plugin_state->crypto_key_slot,
  120. &plugin_state->crypto_verify_data_length);
  121. plugin_state->pin_set = pin != NULL && pin_length > 0;
  122. result |= CryptoSeedIVResultFlagNewCryptoVerifyData;
  123. }
  124. } else {
  125. result = CryptoSeedIVResultFailed;
  126. }
  127. return result;
  128. }
  129. bool totp_crypto_verify_key_v2(const PluginState* plugin_state) {
  130. size_t decrypted_key_length;
  131. uint8_t* decrypted_key = totp_crypto_decrypt_v2(
  132. plugin_state->crypto_verify_data,
  133. plugin_state->crypto_verify_data_length,
  134. &plugin_state->iv[0],
  135. plugin_state->crypto_key_slot,
  136. &decrypted_key_length);
  137. const uint8_t* crypto_vkey = get_crypto_verify_key();
  138. uint8_t crypto_vkey_length = get_crypto_verify_key_length();
  139. bool key_valid = true;
  140. for(uint8_t i = 0; i < crypto_vkey_length && key_valid; i++) {
  141. if(decrypted_key[i] != crypto_vkey[i]) key_valid = false;
  142. }
  143. free(decrypted_key);
  144. return key_valid;
  145. }