crypto_facade.c 2.3 KB

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