From 49ff83922bf6125b217eb02f699143da2da192ea Mon Sep 17 00:00:00 2001 From: BrownBear2 <-> Date: Thu, 13 Feb 2014 21:53:20 +0100 Subject: [PATCH] added romix kdf and cfb for aes --- CMakeLists.txt | 1 + include/fc/crypto/aes.hpp | 2 ++ src/crypto/aes.cpp | 47 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b43894..7514473 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -132,6 +132,7 @@ set( fc_sources src/crypto/elliptic.cpp src/crypto/salsa20.cpp src/crypto/scrypt.cpp + src/crypto/romix.cpp src/network/tcp_socket.cpp src/network/udp_socket.cpp src/network/http/http_connection.cpp diff --git a/include/fc/crypto/aes.hpp b/include/fc/crypto/aes.hpp index 8d4d043..70fdc7c 100644 --- a/include/fc/crypto/aes.hpp +++ b/include/fc/crypto/aes.hpp @@ -41,6 +41,8 @@ namespace fc { unsigned char *iv, unsigned char *ciphertext); int aes_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext); + int aes_cfb_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, + unsigned char *iv, unsigned char *plaintext); std::vector aes_encrypt( const fc::sha512& key, const std::vector& plain_text ); std::vector aes_decrypt( const fc::sha512& key, const std::vector& cipher_text ); diff --git a/src/crypto/aes.cpp b/src/crypto/aes.cpp index 72315f3..3f0933f 100644 --- a/src/crypto/aes.cpp +++ b/src/crypto/aes.cpp @@ -248,7 +248,54 @@ int aes_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *ke return plaintext_len; } +int aes_cfb_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, + unsigned char *iv, unsigned char *plaintext) +{ + evp_cipher_ctx ctx( EVP_CIPHER_CTX_new() ); + int len = 0; + int plaintext_len = 0; + /* Create and initialise the context */ + if(!ctx) + { + FC_THROW_EXCEPTION( exception, "error allocating evp cipher context", + ("s", ERR_error_string( ERR_get_error(), nullptr) ) ); + } + + /* Initialise the decryption 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_DecryptInit_ex(ctx, EVP_aes_256_cfb128(), NULL, key, iv)) + { + FC_THROW_EXCEPTION( exception, "error durring aes 256 cbc decrypt init", + ("s", ERR_error_string( ERR_get_error(), nullptr) ) ); + } + + /* Provide the message to be decrypted, and obtain the plaintext output. + * * EVP_DecryptUpdate can be called multiple times if necessary + * */ + if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) + { + FC_THROW_EXCEPTION( exception, "error durring aes 256 cbc decrypt update", + ("s", ERR_error_string( ERR_get_error(), nullptr) ) ); + } + + plaintext_len = len; + + /* Finalise the decryption. Further plaintext bytes may be written at + * * this stage. + * */ + if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) + { + FC_THROW_EXCEPTION( exception, "error durring aes 256 cbc decrypt final", + ("s", ERR_error_string( ERR_get_error(), nullptr) ) ); + } + plaintext_len += len; + + return plaintext_len; +} std::vector aes_encrypt( const fc::sha512& key, const std::vector& plain_text ) {