crypto_v2.c 6.5 KB

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