crypto_facade.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "crypto_facade.h"
  2. #include <furi_hal_crypto.h>
  3. #include <furi/core/check.h>
  4. #include "crypto_v1.h"
  5. #include "crypto_v2.h"
  6. #include "constants.h"
  7. bool totp_crypto_check_key_slot(uint8_t key_slot) {
  8. uint8_t empty_iv[CRYPTO_IV_LENGTH] = {0};
  9. if(key_slot < ACCEPTABLE_CRYPTO_KEY_SLOT_START || key_slot > ACCEPTABLE_CRYPTO_KEY_SLOT_END) {
  10. return false;
  11. }
  12. return furi_hal_crypto_verify_key(key_slot) &&
  13. furi_hal_crypto_store_load_key(key_slot, empty_iv) &&
  14. furi_hal_crypto_store_unload_key(key_slot);
  15. }
  16. uint8_t* totp_crypto_encrypt(
  17. const uint8_t* plain_data,
  18. const size_t plain_data_length,
  19. const CryptoSettings* crypto_settings,
  20. size_t* encrypted_data_length) {
  21. if(crypto_settings->crypto_version == 1) {
  22. return totp_crypto_encrypt_v1(
  23. plain_data, plain_data_length, crypto_settings, encrypted_data_length);
  24. }
  25. if(crypto_settings->crypto_version == 2) {
  26. return totp_crypto_encrypt_v2(
  27. plain_data, plain_data_length, crypto_settings, encrypted_data_length);
  28. }
  29. furi_crash("Unsupported crypto version");
  30. }
  31. uint8_t* totp_crypto_decrypt(
  32. const uint8_t* encrypted_data,
  33. const size_t encrypted_data_length,
  34. const CryptoSettings* crypto_settings,
  35. size_t* decrypted_data_length) {
  36. if(crypto_settings->crypto_version == 1) {
  37. return totp_crypto_decrypt_v1(
  38. encrypted_data, encrypted_data_length, crypto_settings, decrypted_data_length);
  39. }
  40. if(crypto_settings->crypto_version == 2) {
  41. return totp_crypto_decrypt_v2(
  42. encrypted_data, encrypted_data_length, crypto_settings, decrypted_data_length);
  43. }
  44. furi_crash("Unsupported crypto version");
  45. }
  46. CryptoSeedIVResult
  47. totp_crypto_seed_iv(CryptoSettings* crypto_settings, const uint8_t* pin, uint8_t pin_length) {
  48. if(crypto_settings->crypto_version == 1) {
  49. return totp_crypto_seed_iv_v1(crypto_settings, pin, pin_length);
  50. }
  51. if(crypto_settings->crypto_version == 2) {
  52. return totp_crypto_seed_iv_v2(crypto_settings, pin, pin_length);
  53. }
  54. furi_crash("Unsupported crypto version");
  55. }
  56. bool totp_crypto_verify_key(const CryptoSettings* crypto_settings) {
  57. if(crypto_settings->crypto_version == 1) {
  58. return totp_crypto_verify_key_v1(crypto_settings);
  59. }
  60. if(crypto_settings->crypto_version == 2) {
  61. return totp_crypto_verify_key_v2(crypto_settings);
  62. }
  63. furi_crash("Unsupported crypto version");
  64. }