Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
m3sserschmitt committed Dec 19, 2021
1 parent 4e26632 commit f8e1c2c
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 102 deletions.
99 changes: 60 additions & 39 deletions include/aes.hh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* @file aes.hh
* @author Romulus-Emanuel Ruja
* @author Romulus-Emanuel Ruja <[email protected]>
* @brief This file contains functions for basic AES operations.
* @version 0.1
* @date 2021-07-06
*
* @copyright Copyright (c) 2021 MIT License.
Expand All @@ -17,73 +16,73 @@

namespace CRYPTO
{
/**
/**
* @brief Create new AES encryption / decryption context.
*
* @return AES_CRYPTO AES encryption / decryption context.
*/
AES_CRYPTO AES_CRYPTO_new();
AES_CRYPTO AES_CRYPTO_new();

/**
/**
* @brief Setup key to be used for AES encryption / decryption.
*
* @param key Key to be used.
* @param ctx AES context to setup encryption key.
* @return int 0 if success, -1 if failure.
*/
int AES_setup_key(const BYTE *key, SIZE keylen, AES_CRYPTO ctx);
int AES_setup_key(const BYTE *key, SIZE keylen, AES_CRYPTO ctx);

/**
/**
* @brief Enable or disable IV autoset. If IV autoset enabled, then a new IV is generated on every encryption cycle.
*
* @param autoset IV autoset flag
* @param ctx AES to enable IV autoset
*/
[[deprecated("This function might be removed in further releases; AES_auth_encrypt will automatically generate a new IV on every encryption cycle.")]] void AES_iv_autoset(bool autoset, AES_CRYPTO ctx);
[[deprecated("This function might be removed in further releases; AES_auth_encrypt will automatically generate a new IV on every encryption cycle.")]] void AES_iv_autoset(bool autoset, AES_CRYPTO ctx);

/**
/**
* @brief Enable or disable IV append. If IV append enabled, then IV will be put at the beginning of every ciphertext.
*
* @param append IV append flag
* @param ctx AES to enable IV append
*/
[[deprecated("This function might be removed in further releases; AES_auth_encrypt will automatically append IV to ciphertext")]] void AES_iv_append(bool append, AES_CRYPTO ctx);
[[deprecated("This function might be removed in further releases; AES_auth_encrypt will automatically append IV to ciphertext")]] void AES_iv_append(bool append, AES_CRYPTO ctx);

/**
/**
* @brief Setup initialization vector to be used for encryption / decryption.
*
* @param iv Initialization vector (tipically a 16 bytes random vector).
* @param ctx AES context to setup iv vector.
* @return int 0 if success, -1 if failure.
*/
int AES_setup_iv(const BYTE *iv, SIZE ivlen, AES_CRYPTO ctx);
int AES_setup_iv(const BYTE *iv, SIZE ivlen, AES_CRYPTO ctx);

/**
/**
* @brief Return current encryption key from AES context.
*
* @param ctx Context to retrieve key from.
* @return SIZE return length of key in bytes.
*/
int AES_read_key(const _AES_CRYPTO *ctx, SIZE keylen, BYTES *key);
int AES_read_key(const _AES_CRYPTO *ctx, SIZE keylen, BYTES *key);

/**
/**
* @brief Get initialization vector from AES context.
*
* @param ctx Context to retrieve iv from.
* @return SIZE Size of iv in bytes or -1 if no iv is used within this context.
*/
int AES_read_iv(const _AES_CRYPTO *ctx, SIZE ivlen, BYTES *iv);
int AES_read_iv(const _AES_CRYPTO *ctx, SIZE ivlen, BYTES *iv);

/**
/**
* @brief Initialize AES contex for specified operation.
*
* @param op Cryptographic operation: ENCRYPT / DECRYPT.
* @param ctx AES context to be initialized.
* @return int
*/
int AES_init_ctx(CRYPTO_OP op, AES_CRYPTO ctx);
int AES_init_ctx(CRYPTO_OP op, AES_CRYPTO ctx);

/**
/**
* @brief Initialize AES contex for specified operation.
*
* @param passphrase Passphrase to be used for encryption.
Expand All @@ -94,9 +93,9 @@ namespace CRYPTO
* @param ctx AES context to be initialized.
* @return int 0 for success, -1 for failure.
*/
[[deprecated("This function initializes AES context in CBC mode and it might be removed in further releases. Use AES_setup key & AES_init_ctx instead.")]] int AES_init(const BYTE *passphrase, SIZE passlen, const BYTE *salt, int rounds, CRYPTO_OP op, AES_CRYPTO ctx);
[[deprecated("This function initializes AES context in CBC mode and it might be removed in further releases. Use AES_setup key & AES_init_ctx instead.")]] int AES_init(const BYTE *passphrase, SIZE passlen, const BYTE *salt, int rounds, CRYPTO_OP op, AES_CRYPTO ctx);

/**
/**
* @brief Initialize AES context for both encryption and decryption.
*
* @param passphrase Passphrase to be used for encryption.
Expand All @@ -107,25 +106,25 @@ namespace CRYPTO
* @param ctx AES context to be initialized.
* @return int 0 for success, -1 for failure.
*/
[[deprecated("This function initializes AES context in CBC mode and it might be removed in further releases. Use AES_setup key & AES_init_ctx instead.")]] int AES_init(const BYTE *passphrase, SIZE passlen, const BYTE *salt, int rounds, AES_CRYPTO ctx);
[[deprecated("This function initializes AES context in CBC mode and it might be removed in further releases. Use AES_setup key & AES_init_ctx instead.")]] int AES_init(const BYTE *passphrase, SIZE passlen, const BYTE *salt, int rounds, AES_CRYPTO ctx);

/**
/**
* @brief Check if AES context is ready for encryption;
*
* @param ctx AES context to be checked
* @return int 1 if AES context is ready for encryption, 0 otherwise
*/
int AES_encrypt_ready(const _AES_CRYPTO *ctx);
int AES_encrypt_ready(const _AES_CRYPTO *ctx);

/**
/**
* @brief Check if AES context is ready for decryption;
*
* @param ctx AES context to be checked
* @return int 1 if AES context is ready for decryption, 0 otherwise
*/
int AES_decrypt_ready(const _AES_CRYPTO *ctx);
int AES_decrypt_ready(const _AES_CRYPTO *ctx);

/**
/**
* @brief Duplicates AES context. Destination context will use the same resources as source context.
* For example, if you clean up destination context, source context will be freed too.
* This function is used when you want to encrypt / decrypt using different keys, but you don't want to allocate
Expand All @@ -135,9 +134,9 @@ namespace CRYPTO
* @param src Source AES context.
* @return int 0 if success, -1 if failure.
*/
int AES_ctx_dup(AES_CRYPTO dest, const _AES_CRYPTO *src);
int AES_ctx_dup(AES_CRYPTO dest, const _AES_CRYPTO *src);

/**
/**
* @brief Perform AES encryption.
*
* @param ctx Initialized AES context.
Expand All @@ -148,9 +147,9 @@ namespace CRYPTO
* @param aadlen Additional authenticated data size in bytes
* @return int Size of encrypted data if success, -1 if failure.
*/
int AES_auth_encrypt(AES_CRYPTO ctx, const BYTE *in, SIZE inlen, const BYTE *aad, SIZE aadlen, BYTES *out);
int AES_auth_encrypt(AES_CRYPTO ctx, const BYTE *in, SIZE inlen, const BYTE *aad, SIZE aadlen, BYTES *out);

/**
/**
* @brief Perform AES encryption.
*
* @param ctx Initialized AES context.
Expand All @@ -159,9 +158,20 @@ namespace CRYPTO
* @param out Encrypted data (if null, then it is dynamically allocated).
* @return int Size of encrypted data if success, -1 if failure.
*/
[[deprecated("This function uses CBC mode for encryption and might be removed in further releases; Use AES_auth_encrypt instead.")]] int AES_encrypt(AES_CRYPTO ctx, const BYTE *in, SIZE inlen, BYTES *out);
int AES_auth_encrypt(AES_CRYPTO ctx, const BYTE *in, SIZE inlen, BYTES *out);

/**
/**
* @brief Perform AES encryption.
*
* @param ctx Initialized AES context.
* @param in Data to be encrypted.
* @param inlen Data length in bytes.
* @param out Encrypted data (if null, then it is dynamically allocated).
* @return int Size of encrypted data if success, -1 if failure.
*/
[[deprecated("This function uses CBC mode for encryption and might be removed in further releases; Use AES_auth_encrypt instead.")]] int AES_encrypt(AES_CRYPTO ctx, const BYTE *in, SIZE inlen, BYTES *out);

/**
* @brief Perform AES encryption.
*
* @param ctx Initialized AES context.
Expand All @@ -172,9 +182,20 @@ namespace CRYPTO
* @param aadlen Additional authenticated data size in bytes
* @return int Size of encrypted data if success, -1 if failure.
*/
int AES_auth_decrypt(AES_CRYPTO ctx, const BYTE *in, SIZE inlen, const BYTE *aad, SIZE aadlen, BYTES *out);
int AES_auth_decrypt(AES_CRYPTO ctx, const BYTE *in, SIZE inlen, const BYTE *aad, SIZE aadlen, BYTES *out);

/**
* @brief Perform AES decryption.
*
* @param ctx Initialized AES context.
* @param in Data to be decrypted.
* @param inlen Data length in bytes.
* @param out Decrypted data (if null, then it is dynamically allocated).
* @return int Size of decrypted data if success, -1 if failure.
*/
int AES_auth_decrypt(AES_CRYPTO ctx, const BYTE *in, SIZE inlen, BYTES *out);

/**
/**
* @brief Perform AES decryption.
*
* @param ctx Initialized AES context.
Expand All @@ -183,16 +204,16 @@ namespace CRYPTO
* @param out Decrypted data (if null, then it is dynamically allocated).
* @return int Size of decrypted data if success, -1 if failure.
*/
[[deprecated("This function uses CBC mode for decryption and might be removed in further releases; Use AES_auth_decrypt instead.")]] int AES_decrypt(AES_CRYPTO ctx, const BYTE *in, SIZE inlen, BYTES *out);
[[deprecated("This function uses CBC mode for decryption and might be removed in further releases; Use AES_auth_decrypt instead.")]] int AES_decrypt(AES_CRYPTO ctx, const BYTE *in, SIZE inlen, BYTES *out);

/**
/**
* @brief Frees memory allocated for AES context.
*
* @param ctx Context to be freed.
*/
void AES_CRYPTO_free(AES_CRYPTO ctx);
void AES_CRYPTO_free(AES_CRYPTO ctx);

void AES_CRYPTO_free_keys(AES_CRYPTO ctx);
void AES_CRYPTO_free_keys(AES_CRYPTO ctx);
}

#endif
11 changes: 11 additions & 0 deletions include/aes_const.hh
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/**
* @file aes_const.hh
* @author Romulus-Emanuel Ruja <[email protected]>
* @brief This file contains some constants used in AES GCM Mode.
* @date 2021-12-19
*
* @copyright Copyright (c) 2021 MIT License.
*
*/


#ifndef AES_CONST_HH
#define AES_CONST_HH

Expand Down
22 changes: 22 additions & 0 deletions include/aes_types.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef AES_TYPES_HH
#define AES_TYPES_HH

#include "types.hh"

#include <openssl/evp.h>

struct _AES_CRYPTO
{
BYTES key;
BYTES iv;
EVP_CIPHER_CTX *encr;
EVP_CIPHER_CTX *decr;
bool encrinit;
bool decrinit;
bool iv_autoset;
bool iv_append;

_AES_CRYPTO *ref;
};

#endif
3 changes: 1 addition & 2 deletions include/base64.hh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* @file base64.hh
* @author Romulus-Emanuel Ruja
* @author Romulus-Emanuel Ruja <[email protected]>
* @brief This file contain functions for base64 encoding & decoding.
* @version 0.1
* @date 2021-07-06
*
* @copyright Copyright (c) 2021 MIT License.
Expand Down
9 changes: 5 additions & 4 deletions include/cryptography.hh
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/**
* @file cryptography.hh
* @author Romulus-Emanuel Ruja
* @author Romulus-Emanuel Ruja <[email protected]>
* @brief You should include this file in your project in order to have access to all cryptographic operations.
* @version 0.1
* @date 2021-07-06
*
* @copyright Copyright (c) 2021 MIT License.
*
*/


#include "aes_const.hh"
#include "aes.hh"
#include "rsa.hh"
#include "base64.hh"
#include "rsa.hh"
#include "random.hh"
#include "types.hh"
#include "sha.hh"
#include "random.hh"
#include "types.hh"
3 changes: 1 addition & 2 deletions include/rsa.hh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* @file rsa.hh
* @author Romulus-Emanuel Ruja
* @author Romulus-Emanuel Ruja <[email protected]>
* @brief This file contains functions for basic RSA operations.
* @version 0.1
* @date 2021-07-06
*
* @copyright Copyright (c) 2021 MIT License.
Expand Down
3 changes: 1 addition & 2 deletions include/sha.hh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* @file sha.hh
* @author Romulus-Emanuel Ruja
* @author Romulus-Emanuel Ruja <[email protected]>
* @brief This file contains functions for SHA hashing.
* @version 0.1
* @date 2021-07-06
*
* @copyright Copyright (c) 2021 MIT License.
Expand Down
3 changes: 1 addition & 2 deletions include/types.hh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* @file types.hh
* @author Romulus-Emanuel Ruja
* @author Romulus-Emanuel Ruja <[email protected]>
* @brief This file contain some type definitions.
* @version 0.1
* @date 2021-07-06
*
* @copyright Copyright (c) 2021 MIT License.
Expand Down
15 changes: 1 addition & 14 deletions src/aes.cc
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
#include "aes_types.hh"
#include "aes.hh"
#include "random.hh"

#include <openssl/aes.h>
#include <openssl/evp.h>
#include <string.h>

struct _AES_CRYPTO
{
BYTES key;
BYTES iv;
EVP_CIPHER_CTX *encr;
EVP_CIPHER_CTX *decr;
bool encrinit;
bool decrinit;
bool iv_autoset;
bool iv_append;

_AES_CRYPTO *ref;
};

static inline SIZE AES_get_encrypted_size(SIZE inlen)
{
return inlen + AES_BLOCK_SIZE;
Expand Down
Loading

0 comments on commit f8e1c2c

Please sign in to comment.