diff --git a/CMakeLists.txt b/CMakeLists.txt index a556d70..3420b48 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,6 +57,7 @@ option( UNITY_BUILD OFF ) FIND_PACKAGE( OpenSSL ) include_directories( ${Boost_INCLUDE_DIR} ) include_directories( ${OPENSSL_INCLUDE_DIR} ) +include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/vendor/salsa20 ) SET( ALL_OPENSSL_LIBRARIES ${OPENSSL_LIBRARIES} ${SSL_EAY_RELEASE} ${LIB_EAY_RELEASE}) @@ -111,6 +112,7 @@ set( fc_sources src/crypto/dh.cpp src/crypto/blowfish.cpp src/crypto/elliptic.cpp + src/crypto/salsa20.cpp src/network/tcp_socket.cpp src/network/udp_socket.cpp src/network/http/http_connection.cpp @@ -121,7 +123,12 @@ set( fc_sources src/compress/smaz.cpp vendor/cyoencode-1.0.2/src/CyoDecode.c vendor/cyoencode-1.0.2/src/CyoEncode.c +# vendor/salsa20/ecrypt.c + vendor/salsa20/salsa20.s ) + SET_PROPERTY( SOURCE + vendor/salsa20/salsa20.s + PROPERTY LANGUAGE C) set( sources ${fc_sources} diff --git a/include/fc/crypto/aes.hpp b/include/fc/crypto/aes.hpp index 7a7bfa7..f6d6778 100644 --- a/include/fc/crypto/aes.hpp +++ b/include/fc/crypto/aes.hpp @@ -3,6 +3,8 @@ #include namespace fc { +int aes_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, + unsigned char *iv, unsigned char *ciphertext); 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/include/fc/crypto/city.hpp b/include/fc/crypto/city.hpp index 9f9c34a..4c050c8 100644 --- a/include/fc/crypto/city.hpp +++ b/include/fc/crypto/city.hpp @@ -45,6 +45,7 @@ #include #include #include +#include namespace fc { @@ -58,6 +59,7 @@ uint128 city_hash128(const char *s, size_t len); uint64_t city_hash_crc_64(const char *buf, size_t len); // Hash function for a byte array. -uint128 city_hash_crc_128(const char *s, size_t len); +uint128 city_hash_crc_128(const char *s, size_t len); +fc::array city_hash_crc_256(const char *s, size_t len); } // namespace fc diff --git a/include/fc/crypto/salsa20.hpp b/include/fc/crypto/salsa20.hpp new file mode 100644 index 0000000..a6840a2 --- /dev/null +++ b/include/fc/crypto/salsa20.hpp @@ -0,0 +1,8 @@ +#pragma once +#include + +namespace fc +{ + void salsa20_encrypt( const fc::sha256& key, uint64_t iv, const char* plain, char* cipher, uint64_t len ); + void salsa20_decrypt( const fc::sha256& key, uint64_t iv, const char* cipher, char* plain, uint64_t len ); +} diff --git a/src/crypto/city.cpp b/src/crypto/city.cpp index b89bfd1..67e77eb 100644 --- a/src/crypto/city.cpp +++ b/src/crypto/city.cpp @@ -33,8 +33,11 @@ #include #include // for memcpy and memset #include - +#ifdef __SSE4_2__ +#include +#else uint64_t _mm_crc32_u64(uint64_t a, uint64_t b ); +#endif namespace fc { @@ -470,6 +473,12 @@ void CityHashCrc256(const char *s, size_t len, uint64_t *result) { CityHashCrc256Short(s, len, result); } } +fc::array city_hash_crc_256(const char *s, size_t len) +{ + fc::array buf; + CityHashCrc256( s, len, (uint64_t*)&buf ); + return buf; +} uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed) { if (len <= 900) { diff --git a/src/crypto/salsa20.cpp b/src/crypto/salsa20.cpp new file mode 100644 index 0000000..573e9b1 --- /dev/null +++ b/src/crypto/salsa20.cpp @@ -0,0 +1,21 @@ +#include +extern "C" { +#include +} + +namespace fc +{ + static bool salsa20_init = [=]() -> bool { ECRYPT_init(); return true; }(); + + void salsa20_encrypt( const fc::sha256& key, uint64_t iv, const char* plain, char* cipher, uint64_t len ) + { + ECRYPT_ctx ctx; + ECRYPT_keysetup( &ctx, (unsigned char*)&key, ECRYPT_MAXIVSIZE, ECRYPT_MAXKEYSIZE ); + ECRYPT_ivsetup( &ctx, (unsigned char*)&iv ); + + ECRYPT_encrypt_bytes( &ctx, (const unsigned char*)plain, (unsigned char*)cipher, len ); + } + void salsa20_decrypt( const fc::sha256& key, uint64_t iv, const char* cipher, char* plain, uint64_t len ) + { + } +}