various updates
This commit is contained in:
parent
eb6c168d76
commit
3acfe018b1
4 changed files with 93 additions and 12 deletions
|
|
@ -1,12 +1,30 @@
|
|||
#pragma once
|
||||
#include <fc/crypto/sha512.hpp>
|
||||
#include <fc/crypto/sha256.hpp>
|
||||
#include <fc/uint128.hpp>
|
||||
#include <fc/fwd.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace fc {
|
||||
int aes_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
|
||||
unsigned char *iv, unsigned char *ciphertext);
|
||||
|
||||
std::vector<char> aes_encrypt( const fc::sha512& key, const std::vector<char>& plain_text );
|
||||
std::vector<char> aes_decrypt( const fc::sha512& key, const std::vector<char>& cipher_text );
|
||||
class aes_encoder
|
||||
{
|
||||
public:
|
||||
aes_encoder( const fc::sha256& key, const fc::uint128& init_value );
|
||||
~aes_encoder();
|
||||
|
||||
uint32_t encode( const char* plaintxt, uint32_t len, const char* ciphertxt );
|
||||
uint32_t final_encode( const char* ciphertxt );
|
||||
|
||||
private:
|
||||
struct impl;
|
||||
fc::fwd<impl,96> my;
|
||||
};
|
||||
|
||||
int aes_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
|
||||
unsigned char *iv, unsigned char *ciphertext);
|
||||
|
||||
std::vector<char> aes_encrypt( const fc::sha512& key, const std::vector<char>& plain_text );
|
||||
std::vector<char> aes_decrypt( const fc::sha512& key, const std::vector<char>& cipher_text );
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,10 +33,11 @@ namespace fc
|
|||
#define SSL_TYPE(name, ssl_type, free_func) \
|
||||
struct name : public ssl_wrapper<ssl_type> \
|
||||
{ \
|
||||
name(ssl_type* obj) \
|
||||
name(ssl_type* obj=nullptr) \
|
||||
: ssl_wrapper(obj) {} \
|
||||
~name() \
|
||||
{ \
|
||||
if( obj != nullptr ) \
|
||||
free_func(obj); \
|
||||
} \
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,11 +1,72 @@
|
|||
#include <fc/crypto/sha512.hpp>
|
||||
#include <fc/crypto/aes.hpp>
|
||||
#include <fc/crypto/openssl.hpp>
|
||||
#include <fc/exception/exception.hpp>
|
||||
#include <fc/fwd_impl.hpp>
|
||||
|
||||
namespace fc {
|
||||
|
||||
static int init = init_openssl();
|
||||
|
||||
struct aes_encoder::impl
|
||||
{
|
||||
evp_cipher_ctx ctx;
|
||||
};
|
||||
|
||||
aes_encoder::aes_encoder( const fc::sha256& key, const fc::uint128& init_value )
|
||||
{
|
||||
my->ctx.obj = EVP_CIPHER_CTX_new();
|
||||
/* Create and initialise the context */
|
||||
if(!my->ctx)
|
||||
{
|
||||
FC_THROW_EXCEPTION( exception, "error allocating evp cipher context",
|
||||
("s", ERR_error_string( ERR_get_error(), nullptr) ) );
|
||||
}
|
||||
|
||||
/* Initialise the encryption operation. IMPORTANT - ensure you use a key
|
||||
* and IV size appropriate for your cipher
|
||||
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
|
||||
* IV size for *most* modes is the same as the block size. For AES this
|
||||
* is 128 bits */
|
||||
if(1 != EVP_EncryptInit_ex(my->ctx, EVP_aes_256_cbc(), NULL, (unsigned char*)&key, (unsigned char*)&init_value))
|
||||
{
|
||||
FC_THROW_EXCEPTION( exception, "error durring aes 256 cbc encryption init",
|
||||
("s", ERR_error_string( ERR_get_error(), nullptr) ) );
|
||||
}
|
||||
}
|
||||
aes_encoder::~aes_encoder()
|
||||
{
|
||||
}
|
||||
|
||||
uint32_t aes_encoder::encode( const char* plaintxt, uint32_t plaintext_len, const char* ciphertxt )
|
||||
{
|
||||
int ciphertext_len = 0;
|
||||
/* Provide the message to be encrypted, and obtain the encrypted output.
|
||||
* * EVP_EncryptUpdate can be called multiple times if necessary
|
||||
* */
|
||||
if(1 != EVP_EncryptUpdate(my->ctx, (unsigned char*)ciphertxt, &ciphertext_len, (const unsigned char*)plaintxt, plaintext_len))
|
||||
{
|
||||
FC_THROW_EXCEPTION( exception, "error durring aes 256 cbc encryption update",
|
||||
("s", ERR_error_string( ERR_get_error(), nullptr) ) );
|
||||
}
|
||||
return ciphertext_len;
|
||||
}
|
||||
uint32_t aes_encoder::final_encode( const char* ciphertxt )
|
||||
{
|
||||
int ciphertext_len = 0;
|
||||
/* Finalise the encryption. Further ciphertext bytes may be written at
|
||||
* * this stage.
|
||||
* */
|
||||
if(1 != EVP_EncryptFinal_ex(my->ctx, (unsigned char*)ciphertxt, &ciphertext_len))
|
||||
{
|
||||
FC_THROW_EXCEPTION( exception, "error durring aes 256 cbc encryption final",
|
||||
("s", ERR_error_string( ERR_get_error(), nullptr) ) );
|
||||
}
|
||||
return ciphertext_len;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/** example method from wiki.opensslfoundation.com */
|
||||
int aes_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
|
||||
unsigned char *iv, unsigned char *ciphertext)
|
||||
|
|
@ -23,10 +84,10 @@ int aes_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
|
|||
}
|
||||
|
||||
/* Initialise the encryption operation. IMPORTANT - ensure you use a key
|
||||
* * and IV size appropriate for your cipher
|
||||
* * In this example we are using 256 bit AES (i.e. a 256 bit key). The
|
||||
* * IV size for *most* modes is the same as the block size. For AES this
|
||||
* * is 128 bits */
|
||||
* and IV size appropriate for your cipher
|
||||
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
|
||||
* IV size for *most* modes is the same as the block size. For AES this
|
||||
* is 128 bits */
|
||||
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
|
||||
{
|
||||
FC_THROW_EXCEPTION( exception, "error durring aes 256 cbc encryption init",
|
||||
|
|
|
|||
|
|
@ -14,8 +14,9 @@ namespace fc {
|
|||
return 0;
|
||||
}
|
||||
|
||||
fc::string to_hex( const char* d, uint32_t s ) {
|
||||
fc::string r;
|
||||
std::string to_hex( const char* d, uint32_t s )
|
||||
{
|
||||
std::string r;
|
||||
const char* to_hex="0123456789abcdef";
|
||||
uint8_t* c = (uint8_t*)d;
|
||||
for( uint32_t i = 0; i < s; ++i )
|
||||
|
|
|
|||
Loading…
Reference in a new issue