From d53852be316dd2322a7e621a81ee33e1b234c530 Mon Sep 17 00:00:00 2001 From: David Gumberg Date: Fri, 31 Jan 2025 11:48:20 -0800 Subject: [PATCH] crypto: Use `secure_allocator` for `AES256_ctx` --- src/crypto/aes.cpp | 15 +++++++++------ src/crypto/aes.h | 7 +++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/crypto/aes.cpp b/src/crypto/aes.cpp index 40df5690e0f..207a8e8a85d 100644 --- a/src/crypto/aes.cpp +++ b/src/crypto/aes.cpp @@ -3,6 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include +#include #include @@ -12,32 +13,34 @@ extern "C" { AES256Encrypt::AES256Encrypt(const unsigned char key[32]) { - AES256_init(&ctx, key); + ctx = allocator.allocate(1); + AES256_init(ctx, key); } AES256Encrypt::~AES256Encrypt() { - memset(&ctx, 0, sizeof(ctx)); + allocator.deallocate(ctx, 1); } void AES256Encrypt::Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const { - AES256_encrypt(&ctx, 1, ciphertext, plaintext); + AES256_encrypt(ctx, 1, ciphertext, plaintext); } AES256Decrypt::AES256Decrypt(const unsigned char key[32]) { - AES256_init(&ctx, key); + ctx = allocator.allocate(1); + AES256_init(ctx, key); } AES256Decrypt::~AES256Decrypt() { - memset(&ctx, 0, sizeof(ctx)); + allocator.deallocate(ctx, 1); } void AES256Decrypt::Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const { - AES256_decrypt(&ctx, 1, plaintext, ciphertext); + AES256_decrypt(ctx, 1, plaintext, ciphertext); } diff --git a/src/crypto/aes.h b/src/crypto/aes.h index 4eae9a3bf7f..191cffd9110 100644 --- a/src/crypto/aes.h +++ b/src/crypto/aes.h @@ -7,6 +7,7 @@ #ifndef BITCOIN_CRYPTO_AES_H #define BITCOIN_CRYPTO_AES_H +#include extern "C" { #include } @@ -18,7 +19,8 @@ static const int AES256_KEYSIZE = 32; class AES256Encrypt { private: - AES256_ctx ctx; + secure_allocator allocator; + AES256_ctx *ctx; public: explicit AES256Encrypt(const unsigned char key[32]); @@ -30,7 +32,8 @@ public: class AES256Decrypt { private: - AES256_ctx ctx; + secure_allocator allocator; + AES256_ctx *ctx; public: explicit AES256Decrypt(const unsigned char key[32]);