crypto_wrapper.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <furi_hal.h>
  2. #ifndef FURI_HAL_CRYPTO_ADVANCED_AVAIL
  3. #include "crypto/gcm.h"
  4. #endif /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
  5. #include "crypto_wrapper.h"
  6. struct ESugGhzChatCryptoCtx {
  7. uint8_t key[KEY_BITS / 8];
  8. #ifndef FURI_HAL_CRYPTO_ADVANCED_AVAIL
  9. gcm_context gcm_ctx;
  10. #endif /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
  11. };
  12. void crypto_init(void)
  13. {
  14. #ifndef FURI_HAL_CRYPTO_ADVANCED_AVAIL
  15. /* init the GCM and AES tables */
  16. gcm_initialize();
  17. #endif /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
  18. }
  19. void crypto_explicit_bzero(void *s, size_t len)
  20. {
  21. memset(s, 0, len);
  22. asm volatile("" ::: "memory");
  23. }
  24. ESubGhzChatCryptoCtx *crypto_ctx_alloc(void)
  25. {
  26. ESubGhzChatCryptoCtx *ret = malloc(sizeof(ESubGhzChatCryptoCtx));
  27. if (ret != NULL) {
  28. memset(ret, 0, sizeof(ESubGhzChatCryptoCtx));
  29. }
  30. return ret;
  31. }
  32. void crypto_ctx_free(ESubGhzChatCryptoCtx *ctx)
  33. {
  34. crypto_ctx_clear(ctx);
  35. free(ctx);
  36. }
  37. void crypto_ctx_clear(ESubGhzChatCryptoCtx *ctx)
  38. {
  39. crypto_explicit_bzero(ctx, sizeof(ESubGhzChatCryptoCtx));
  40. }
  41. bool crypto_ctx_set_key(ESubGhzChatCryptoCtx *ctx, const uint8_t *key)
  42. {
  43. memcpy(ctx->key, key, KEY_BITS / 8);
  44. #ifdef FURI_HAL_CRYPTO_ADVANCED_AVAIL
  45. return true;
  46. #else /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
  47. return (gcm_setkey(&(ctx->gcm_ctx), key, KEY_BITS / 8) == 0);
  48. #endif /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
  49. }
  50. void crypto_ctx_get_key(ESubGhzChatCryptoCtx *ctx, uint8_t *key)
  51. {
  52. memcpy(key, ctx->key, KEY_BITS / 8);
  53. }
  54. bool crypto_ctx_decrypt(ESubGhzChatCryptoCtx *ctx, uint8_t *in, size_t in_len,
  55. uint8_t *out)
  56. {
  57. if (in_len < MSG_OVERHEAD + 1) {
  58. return false;
  59. }
  60. #ifdef FURI_HAL_CRYPTO_ADVANCED_AVAIL
  61. return (furi_hal_crypto_gcm_decrypt_and_verify(ctx->key,
  62. in, in + IV_BYTES, out,
  63. in_len - MSG_OVERHEAD,
  64. in + in_len - TAG_BYTES) == FuriHalCryptoGCMStateOk);
  65. #else /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
  66. return (gcm_auth_decrypt(&(ctx->gcm_ctx),
  67. in, IV_BYTES,
  68. NULL, 0,
  69. in + IV_BYTES, out, in_len - MSG_OVERHEAD,
  70. in + in_len - TAG_BYTES, TAG_BYTES) == 0);
  71. #endif /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
  72. }
  73. bool crypto_ctx_encrypt(ESubGhzChatCryptoCtx *ctx, uint8_t *in, size_t in_len,
  74. uint8_t *out)
  75. {
  76. furi_hal_random_fill_buf(out, IV_BYTES);
  77. #ifdef FURI_HAL_CRYPTO_ADVANCED_AVAIL
  78. return (furi_hal_crypto_gcm_encrypt_and_tag(ctx->key,
  79. out, in, out + IV_BYTES,
  80. in_len,
  81. out + IV_BYTES + in_len) == FuriHalCryptoGCMStateOk);
  82. #else /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
  83. return (gcm_crypt_and_tag(&(ctx->gcm_ctx), ENCRYPT,
  84. out, IV_BYTES,
  85. NULL, 0,
  86. in, out + IV_BYTES, in_len,
  87. out + IV_BYTES + in_len, TAG_BYTES) == 0);
  88. #endif /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
  89. }