crypto: Use secure_allocator for AES256_ctx

This commit is contained in:
David Gumberg
2025-01-31 11:48:20 -08:00
parent 8c6fedaa81
commit d53852be31
2 changed files with 14 additions and 8 deletions

View File

@@ -3,6 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <crypto/aes.h>
#include <support/allocators/secure.h>
#include <cstring>
@@ -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);
}

View File

@@ -7,6 +7,7 @@
#ifndef BITCOIN_CRYPTO_AES_H
#define BITCOIN_CRYPTO_AES_H
#include <support/allocators/secure.h>
extern "C" {
#include <crypto/ctaes/ctaes.h>
}
@@ -18,7 +19,8 @@ static const int AES256_KEYSIZE = 32;
class AES256Encrypt
{
private:
AES256_ctx ctx;
secure_allocator<AES256_ctx> 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<AES256_ctx> allocator;
AES256_ctx *ctx;
public:
explicit AES256Decrypt(const unsigned char key[32]);