adding new crypto apis

This commit is contained in:
Daniel Larimer 2013-08-24 04:25:03 -04:00
parent 113d63c8b2
commit 6a1f42aba0
6 changed files with 51 additions and 2 deletions

View file

@ -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}

View file

@ -3,6 +3,8 @@
#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 );

View file

@ -45,6 +45,7 @@
#include <stdint.h>
#include <utility>
#include <fc/uint128.hpp>
#include <fc/array.hpp>
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<uint64_t,4> city_hash_crc_256(const char *s, size_t len);
} // namespace fc

View file

@ -0,0 +1,8 @@
#pragma once
#include <fc/crypto/sha256.hpp>
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 );
}

View file

@ -33,8 +33,11 @@
#include <algorithm>
#include <string.h> // for memcpy and memset
#include <fc/crypto/city.hpp>
#ifdef __SSE4_2__
#include <nmmintrin.h>
#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<uint64_t,4> city_hash_crc_256(const char *s, size_t len)
{
fc::array<uint64_t,4> buf;
CityHashCrc256( s, len, (uint64_t*)&buf );
return buf;
}
uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed) {
if (len <= 900) {

21
src/crypto/salsa20.cpp Normal file
View file

@ -0,0 +1,21 @@
#include <fc/crypto/salsa20.hpp>
extern "C" {
#include <ecrypt-sync.h>
}
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 )
{
}
}