Skip to content

Commit

Permalink
allow specifying cipher name to context
Browse files Browse the repository at this point in the history
  • Loading branch information
evido committed Dec 28, 2021
1 parent 7c899ab commit 90e834b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/cipher_context.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
#include <openssl/err.h>
#include <string>

#include "cipher_context.h"

#define SSL_SHOW_ERRORS(x) if ((x)) { ERR_print_errors_fp(stderr); }

CipherContext::CipherContext(const unsigned char* key_data, int key_size, const unsigned char* iv)
CipherContext::CipherContext(const char* cipher_name, const unsigned char* key_data, int key_size, const unsigned char* iv)
: legacy_provider(nullptr), default_provider(nullptr), ossl_ctx(nullptr), cipher(nullptr), ctx(nullptr)
{
OPENSSL_assert(ossl_ctx = OSSL_LIB_CTX_new());
OPENSSL_assert(legacy_provider = OSSL_PROVIDER_load(ossl_ctx, "legacy"));
OPENSSL_assert(default_provider = OSSL_PROVIDER_load(ossl_ctx, "default"));

OPENSSL_assert(cipher = EVP_CIPHER_fetch(ossl_ctx, "BF-ECB", nullptr));
OPENSSL_assert(cipher = EVP_CIPHER_fetch(ossl_ctx, cipher_name, nullptr));

OPENSSL_assert(ctx = EVP_CIPHER_CTX_new());
OPENSSL_assert(EVP_CipherInit_ex(ctx, cipher, nullptr, nullptr, nullptr, 0) == 1);
OPENSSL_assert(EVP_CIPHER_CTX_set_key_length(ctx, 16) == 1);
OPENSSL_assert(EVP_CIPHER_CTX_set_key_length(ctx, key_size) == 1);
OPENSSL_assert(EVP_CipherInit_ex(ctx, nullptr, nullptr, key_data, iv, 0) == 1);
OPENSSL_assert(EVP_CIPHER_CTX_set_padding(ctx, 0) == 1);
}
Expand All @@ -33,6 +32,9 @@ int CipherContext::finalize(unsigned char *out, int* out_len) {
}

CipherContext::~CipherContext() {
// show errors for debugging
ERR_print_errors_fp(stderr);

EVP_CIPHER_CTX_free(ctx);
EVP_CIPHER_free(cipher);
OSSL_PROVIDER_unload(legacy_provider);
Expand Down
2 changes: 1 addition & 1 deletion src/cipher_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class CipherContext {
public:
CipherContext(const unsigned char* key_data, int key_size, const unsigned char* iv);
CipherContext(const char* cipher_name, const unsigned char* key_data, int key_size, const unsigned char* iv);
int update(unsigned char* out, int* out_len, const unsigned char* in, int in_len);
int finalize(unsigned char* out, int* out_len);
~CipherContext();
Expand Down
2 changes: 1 addition & 1 deletion src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void parser_t::decrypt_replay(buffer_t &replay_data, const unsigned char *key_da
decrypted[block_size] = {0};

unsigned char iv[key_size] = {0};
CipherContext cipherContext(key_data, key_size, iv);
CipherContext cipherContext("BF-ECB", key_data, key_size, iv);


uint32_t pin = 0;
Expand Down

0 comments on commit 90e834b

Please sign in to comment.