Skip to content

make encryption/decryption faster with big files #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions crypto-openssl-10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "crypto.hpp"
#include "key.hpp"
#include "util.hpp"
#include <openssl/aes.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>
Expand All @@ -50,14 +49,16 @@ void init_crypto ()
}

struct Aes_ecb_encryptor::Aes_impl {
AES_KEY key;
EVP_CIPHER_CTX *ctx;
};

Aes_ecb_encryptor::Aes_ecb_encryptor (const unsigned char* raw_key)
: impl(new Aes_impl)
{
if (AES_set_encrypt_key(raw_key, KEY_LEN * 8, &(impl->key)) != 0) {
throw Crypto_error("Aes_ctr_encryptor::Aes_ctr_encryptor", "AES_set_encrypt_key failed");
impl->ctx = EVP_CIPHER_CTX_new();
if (!EVP_EncryptInit_ex(impl->ctx, EVP_aes_256_ecb(), NULL,
raw_key, NULL)) {
throw Crypto_error("Aes_ctr_encryptor::Aes_ctr_encryptor", "EVP_EncryptInit_ex failed");
}
}

Expand All @@ -66,12 +67,15 @@ Aes_ecb_encryptor::~Aes_ecb_encryptor ()
// Note: Explicit destructor necessary because class contains an unique_ptr
// which contains an incomplete type when the unique_ptr is declared.

explicit_memset(&impl->key, '\0', sizeof(impl->key));
EVP_CIPHER_CTX_free(impl->ctx);
}

void Aes_ecb_encryptor::encrypt(const unsigned char* plain, unsigned char* cipher)
{
AES_encrypt(plain, cipher, &(impl->key));
int ciphertext_len;
if (1!=EVP_EncryptUpdate(impl->ctx, cipher, &ciphertext_len, plain, BLOCK_LEN)) {
throw Crypto_error("Aes_ctr_encryptor::encrypt", "EVP_EncryptUpdate failed");
}
}

struct Hmac_sha1_state::Hmac_impl {
Expand Down
16 changes: 10 additions & 6 deletions crypto-openssl-11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "crypto.hpp"
#include "key.hpp"
#include "util.hpp"
#include <openssl/aes.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>
Expand All @@ -50,14 +49,16 @@ void init_crypto ()
}

struct Aes_ecb_encryptor::Aes_impl {
AES_KEY key;
EVP_CIPHER_CTX *ctx;
};

Aes_ecb_encryptor::Aes_ecb_encryptor (const unsigned char* raw_key)
: impl(new Aes_impl)
{
if (AES_set_encrypt_key(raw_key, KEY_LEN * 8, &(impl->key)) != 0) {
throw Crypto_error("Aes_ctr_encryptor::Aes_ctr_encryptor", "AES_set_encrypt_key failed");
impl->ctx = EVP_CIPHER_CTX_new();
if (!EVP_EncryptInit_ex(impl->ctx, EVP_aes_256_ecb(), NULL,
raw_key, NULL)) {
throw Crypto_error("Aes_ctr_encryptor::Aes_ctr_encryptor", "EVP_EncryptInit_ex failed");
}
}

Expand All @@ -66,12 +67,15 @@ Aes_ecb_encryptor::~Aes_ecb_encryptor ()
// Note: Explicit destructor necessary because class contains an unique_ptr
// which contains an incomplete type when the unique_ptr is declared.

explicit_memset(&impl->key, '\0', sizeof(impl->key));
EVP_CIPHER_CTX_free(impl->ctx);
}

void Aes_ecb_encryptor::encrypt(const unsigned char* plain, unsigned char* cipher)
{
AES_encrypt(plain, cipher, &(impl->key));
int ciphertext_len;
if (1!=EVP_EncryptUpdate(impl->ctx, cipher, &ciphertext_len, plain, BLOCK_LEN)) {
throw Crypto_error("Aes_ctr_encryptor::encrypt", "EVP_EncryptUpdate failed");
}
}

struct Hmac_sha1_state::Hmac_impl {
Expand Down
4 changes: 2 additions & 2 deletions crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ void Aes_ctr_encryptor::process (const unsigned char* in, unsigned char* out, si
for (size_t i = 0; i < len; ++i) {
if (byte_counter % BLOCK_LEN == 0) {
// Set last 4 bytes of CTR to the (big-endian) block number (sequentially increasing with each block)
store_be32(ctr_value + NONCE_LEN, byte_counter / BLOCK_LEN);
store_be32(ctr_value + NONCE_LEN, byte_counter >> 4);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use BLOCK_LEN here ?


// Generate a new pad
ecb.encrypt(ctr_value, pad);
}

// encrypt one byte
out[i] = in[i] ^ pad[byte_counter++ % BLOCK_LEN];
out[i] = in[i] ^ pad[byte_counter++ & (BLOCK_LEN -1)];

if (byte_counter == 0) {
throw Crypto_error("Aes_ctr_encryptor::process", "Too much data to encrypt securely");
Expand Down