change Aes256 to accept buffer

This commit is contained in:
nikola
2024-06-20 12:39:17 +02:00
parent 5e7e4eb40b
commit 9cda42ea4a
7 changed files with 101 additions and 154 deletions

View File

@@ -25,64 +25,60 @@
#define AES256_HPP
#include <vector>
typedef std::vector<unsigned char> ByteArray;
#include "buffer.hpp"
#define BLOCK_SIZE 16
class Aes256 {
class Aes256
{
public:
Aes256(const ByteArray& key);
~Aes256();
public:
Aes256(const std::string &key);
~Aes256();
static ByteArray::size_type encrypt(const ByteArray& key, const ByteArray& plain, ByteArray& encrypted);
static ByteArray::size_type encrypt(const ByteArray& key, const unsigned char* plain, const ByteArray::size_type plain_length, ByteArray& encrypted);
static ByteArray::size_type decrypt(const ByteArray& key, const ByteArray& encrypted, ByteArray& plain);
static ByteArray::size_type decrypt(const ByteArray& key, const unsigned char* encrypted, const ByteArray::size_type encrypted_length, ByteArray& plain);
static bool encrypt(const std::string &key, const Buffer &plain, Buffer &encrypted);
static bool decrypt(const std::string &key, const Buffer &encrypted, Buffer &plain);
ByteArray::size_type encrypt_start(const ByteArray::size_type plain_length, ByteArray& encrypted);
ByteArray::size_type encrypt_continue(const ByteArray& plain, ByteArray& encrypted);
ByteArray::size_type encrypt_continue(const unsigned char* plain, const ByteArray::size_type plain_length, ByteArray& encrypted);
ByteArray::size_type encrypt_end(ByteArray& encrypted);
std::size_t encrypt_start(const std::size_t plain_length, Buffer &encrypted);
std::size_t encrypt_continue(const Buffer &plain, Buffer &encrypted);
std::size_t encrypt_end(Buffer &encrypted);
ByteArray::size_type decrypt_start(const ByteArray::size_type encrypted_length);
ByteArray::size_type decrypt_continue(const ByteArray& encrypted, ByteArray& plain);
ByteArray::size_type decrypt_continue(const unsigned char* encrypted, const ByteArray::size_type encrypted_length, ByteArray& plain);
ByteArray::size_type decrypt_end(ByteArray& plain);
std::size_t decrypt_start(const std::size_t encrypted_length);
std::size_t decrypt_continue(const Buffer &encrypted, Buffer &plain);
std::size_t decrypt_end(Buffer &plain);
private:
ByteArray m_key;
ByteArray m_salt;
ByteArray m_rkey;
private:
std::vector<unsigned char> m_key;
std::vector<unsigned char> m_salt;
std::vector<unsigned char> m_rkey;
unsigned char m_buffer[3 * BLOCK_SIZE];
unsigned char m_buffer_pos;
ByteArray::size_type m_remainingLength;
unsigned char m_buffer[3 * BLOCK_SIZE];
unsigned char m_buffer_pos;
std::size_t m_remainingLength;
bool m_decryptInitialized;
bool m_decryptInitialized;
void check_and_encrypt_buffer(ByteArray& encrypted);
void check_and_decrypt_buffer(ByteArray& plain);
void check_and_encrypt_buffer(Buffer &encrypted);
void check_and_decrypt_buffer(Buffer &plain);
void encrypt(unsigned char *buffer);
void decrypt(unsigned char *buffer);
void encrypt(unsigned char *buffer);
void decrypt(unsigned char *buffer);
void expand_enc_key(unsigned char *rc);
void expand_dec_key(unsigned char *rc);
void expand_enc_key(unsigned char *rc);
void expand_dec_key(unsigned char *rc);
void sub_bytes(unsigned char *buffer);
void sub_bytes_inv(unsigned char *buffer);
void sub_bytes(unsigned char *buffer);
void sub_bytes_inv(unsigned char *buffer);
void copy_key();
void copy_key();
void add_round_key(unsigned char *buffer, const unsigned char round);
void add_round_key(unsigned char *buffer, const unsigned char round);
void shift_rows(unsigned char *buffer);
void shift_rows_inv(unsigned char *buffer);
void shift_rows(unsigned char *buffer);
void shift_rows_inv(unsigned char *buffer);
void mix_columns(unsigned char *buffer);
void mix_columns_inv(unsigned char *buffer);
void mix_columns(unsigned char *buffer);
void mix_columns_inv(unsigned char *buffer);
};
#endif /* AES256_HPP */

View File

@@ -9,7 +9,7 @@ class Buffer
public:
uint8_t *buffer = nullptr;
size_t taken = 0;
size_t size = 0;
std::string file_path;
Buffer(size_t size);
@@ -35,6 +35,8 @@ public:
bool load_from_file();
bool load_from_file(std::string file_path);
private:
size_t size = 0;
};
#endif
#endif //BUFFER_HPP

View File

@@ -11,8 +11,8 @@ public:
Cryptography(std::string password);
~Cryptography();
void change_pass(std::string password);
bool encrypt(Buffer *plain, Buffer *encrypted);
bool decrypt(Buffer *encrypted, Buffer *decrypted);
bool encrypt(Buffer &plain, Buffer &encrypted);
bool decrypt(Buffer &encrypted, Buffer &decrypted);
private:
std::string key;