crypto_facade.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "crypto_facade.h"
  2. #include "../../config/app/config.h"
  3. #include <furi_hal_crypto.h>
  4. #include <furi/core/check.h>
  5. #ifdef TOTP_OBSOLETE_CRYPTO_V1_COMPATIBILITY_ENABLED
  6. #include "crypto_v1.h"
  7. #endif
  8. #ifdef TOTP_OBSOLETE_CRYPTO_V2_COMPATIBILITY_ENABLED
  9. #include "crypto_v2.h"
  10. #endif
  11. #include "crypto_v3.h"
  12. #include "constants.h"
  13. bool totp_crypto_check_key_slot(uint8_t key_slot) {
  14. uint8_t empty_iv[CRYPTO_IV_LENGTH] = {0};
  15. if(key_slot < ACCEPTABLE_CRYPTO_KEY_SLOT_START || key_slot > ACCEPTABLE_CRYPTO_KEY_SLOT_END) {
  16. return false;
  17. }
  18. return furi_hal_crypto_enclave_ensure_key(key_slot) &&
  19. furi_hal_crypto_enclave_load_key(key_slot, empty_iv) &&
  20. furi_hal_crypto_enclave_unload_key(key_slot);
  21. }
  22. uint8_t* totp_crypto_encrypt(
  23. const uint8_t* plain_data,
  24. const size_t plain_data_length,
  25. const CryptoSettings* crypto_settings,
  26. size_t* encrypted_data_length) {
  27. #ifdef TOTP_OBSOLETE_CRYPTO_V1_COMPATIBILITY_ENABLED
  28. if(crypto_settings->crypto_version == 1) {
  29. return totp_crypto_encrypt_v1(
  30. plain_data, plain_data_length, crypto_settings, encrypted_data_length);
  31. }
  32. #endif
  33. #ifdef TOTP_OBSOLETE_CRYPTO_V2_COMPATIBILITY_ENABLED
  34. if(crypto_settings->crypto_version == 2) {
  35. return totp_crypto_encrypt_v2(
  36. plain_data, plain_data_length, crypto_settings, encrypted_data_length);
  37. }
  38. #endif
  39. if(crypto_settings->crypto_version == 3) {
  40. return totp_crypto_encrypt_v3(
  41. plain_data, plain_data_length, crypto_settings, encrypted_data_length);
  42. }
  43. furi_crash("Unsupported crypto version");
  44. }
  45. uint8_t* totp_crypto_decrypt(
  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. #ifdef TOTP_OBSOLETE_CRYPTO_V1_COMPATIBILITY_ENABLED
  51. if(crypto_settings->crypto_version == 1) {
  52. return totp_crypto_decrypt_v1(
  53. encrypted_data, encrypted_data_length, crypto_settings, decrypted_data_length);
  54. }
  55. #endif
  56. #ifdef TOTP_OBSOLETE_CRYPTO_V2_COMPATIBILITY_ENABLED
  57. if(crypto_settings->crypto_version == 2) {
  58. return totp_crypto_decrypt_v2(
  59. encrypted_data, encrypted_data_length, crypto_settings, decrypted_data_length);
  60. }
  61. #endif
  62. if(crypto_settings->crypto_version == 3) {
  63. return totp_crypto_decrypt_v3(
  64. encrypted_data, encrypted_data_length, crypto_settings, decrypted_data_length);
  65. }
  66. furi_crash("Unsupported crypto version");
  67. }
  68. CryptoSeedIVResult
  69. totp_crypto_seed_iv(CryptoSettings* crypto_settings, const uint8_t* pin, uint8_t pin_length) {
  70. #ifdef TOTP_OBSOLETE_CRYPTO_V1_COMPATIBILITY_ENABLED
  71. if(crypto_settings->crypto_version == 1) {
  72. return totp_crypto_seed_iv_v1(crypto_settings, pin, pin_length);
  73. }
  74. #endif
  75. #ifdef TOTP_OBSOLETE_CRYPTO_V2_COMPATIBILITY_ENABLED
  76. if(crypto_settings->crypto_version == 2) {
  77. return totp_crypto_seed_iv_v2(crypto_settings, pin, pin_length);
  78. }
  79. #endif
  80. if(crypto_settings->crypto_version == 3) {
  81. return totp_crypto_seed_iv_v3(crypto_settings, pin, pin_length);
  82. }
  83. furi_crash("Unsupported crypto version");
  84. }
  85. bool totp_crypto_verify_key(const CryptoSettings* crypto_settings) {
  86. #ifdef TOTP_OBSOLETE_CRYPTO_V1_COMPATIBILITY_ENABLED
  87. if(crypto_settings->crypto_version == 1) {
  88. return totp_crypto_verify_key_v1(crypto_settings);
  89. }
  90. #endif
  91. #ifdef TOTP_OBSOLETE_CRYPTO_V2_COMPATIBILITY_ENABLED
  92. if(crypto_settings->crypto_version == 2) {
  93. return totp_crypto_verify_key_v2(crypto_settings);
  94. }
  95. #endif
  96. if(crypto_settings->crypto_version == 3) {
  97. return totp_crypto_verify_key_v3(crypto_settings);
  98. }
  99. furi_crash("Unsupported crypto version");
  100. }