Merge branch 'master' of github.com:InvictusInnovations/fc

This commit is contained in:
Eric Frias 2014-07-14 19:26:17 -04:00
commit f2cbb650c6
6 changed files with 48 additions and 66 deletions

View file

@ -147,8 +147,7 @@ set( fc_sources
src/crypto/rand.cpp
src/crypto/salsa20.cpp
src/crypto/scrypt.cpp
# commented out until committed
# src/crypto/romix.cpp
src/crypto/romix.cpp
src/network/tcp_socket.cpp
src/network/udp_socket.cpp
src/network/udt_socket.cpp
@ -183,6 +182,7 @@ list(APPEND sources "${CMAKE_CURRENT_BINARY_DIR}/git_revision.cpp")
list(APPEND sources ${fc_headers})
add_subdirectory( vendor/easylzma )
add_subdirectory( vendor/scrypt-jane )
add_subdirectory( vendor/udt4 )
setup_library( fc SOURCES ${sources} LIBRARY_TYPE STATIC DONT_INSTALL_LIBRARY )
@ -200,10 +200,9 @@ ELSE()
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall")
IF(APPLE)
SET(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++11 -stdlib=libc++ -Wall")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++ -Wall")
ELSE()
target_compile_options(fc PUBLIC -std=c++11 -Wall -fnon-call-exceptions)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -fnon-call-exceptions")
ENDIF()
ENDIF()
@ -221,7 +220,7 @@ target_include_directories(fc
${CMAKE_CURRENT_SOURCE_DIR}/vendor/udt4/src
)
target_link_libraries( fc PUBLIC udt easylzma_static ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} ${ZLIB_LIBRARIES} ${PLATFORM_SPECIFIC_LIBS} ${RPCRT4} ${CMAKE_DL_LIBS} ${rt_library})
target_link_libraries( fc PUBLIC easylzma_static scrypt udt ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} ${ZLIB_LIBRARIES} ${PLATFORM_SPECIFIC_LIBS} ${RPCRT4} ${CMAKE_DL_LIBS} ${rt_library})
add_executable( ntp_test ntp_test.cpp )
target_link_libraries( ntp_test fc )

View file

@ -1,10 +1,9 @@
#pragma once
#include <string>
#include <vector>
namespace fc {
void scrypt_derive_key ( const std::vector<unsigned char> &passphrase, const std::vector<unsigned char> &salt,
unsigned int n, unsigned int r, unsigned int p, std::vector<unsigned char> &key );
void scrypt_derive_key( const std::vector<unsigned char>& passphrase, const std::vector<unsigned char>& salt,
unsigned int n, unsigned int r, unsigned int p, std::vector<unsigned char>& key );
} // namespace fc

View file

@ -1,57 +1,22 @@
#include <algorithm>
#include <fc/crypto/openssl.hpp>
#include <fc/crypto/scrypt.hpp>
#include <fc/exception/exception.hpp>
#include <openssl/evp.h>
#define SCRYPT_SALSA 1
#define SCRYPT_SHA256 1
/*
#include "code/scrypt-jane-portable.h"
#include "code/scrypt-jane-romix.h"
*/
#include "scrypt-jane.h"
namespace fc {
void scrypt_derive_key( const std::vector<unsigned char> &passphrase, const std::vector<unsigned char> &salt,
unsigned int n, unsigned int r, unsigned int p, std::vector<unsigned char> &key )
{
/*
unsigned int chunk_bytes = SCRYPT_BLOCK_BYTES * r * 2;
std::vector<unsigned char> yx((p+1) * chunk_bytes);
unsigned log2( unsigned n )
{
if( n <= 0 ) FC_THROW_EXCEPTION( exception, "cannot take log2(${n})", ("n",n) );
unsigned i = 0;
while( n >>= 1 ) ++i;
return i;
}
unsigned char *Y = &yx[0];
unsigned char *X = &yx[chunk_bytes];
if(PKCS5_PBKDF2_HMAC( (const char*)&passphrase[0], passphrase.size(),
&salt[0], salt.size(), 1,
EVP_sha256(), chunk_bytes * p, X) != 1 )
{
std::fill( yx.begin(), yx.end(), 0 );
FC_THROW_EXCEPTION( exception, "error generating key material",
("s", ERR_error_string( ERR_get_error(), nullptr) ) );
}
std::vector<unsigned char> v(n * chunk_bytes);
for( unsigned int i = 0; i < p; i++ )
scrypt_ROMix_basic( (uint32_t*)(X+(chunk_bytes*i)), (uint32_t*)Y, (uint32_t*)&v[0], n, r );
if(PKCS5_PBKDF2_HMAC( (const char*)&passphrase[0], passphrase.size(),
X, chunk_bytes * p, 1,
EVP_sha256(), key.size(), &key[0]) != 1 )
{
std::fill( yx.begin(), yx.end(), 0 );
std::fill( v.begin(), v.end(), 0 );
FC_THROW_EXCEPTION( exception, "error generating key material",
("s", ERR_error_string( ERR_get_error(), nullptr) ) );
}
std::fill( yx.begin(), yx.end(), 0 );
std::fill( v.begin(), v.end(), 0 );
*/
}
void scrypt_derive_key( const std::vector<unsigned char>& passphrase, const std::vector<unsigned char>& salt,
unsigned int n, unsigned int r, unsigned int p, std::vector<unsigned char>& key )
{
scrypt( passphrase.data(), passphrase.size(), salt.data(), salt.size(),
log2( n ) - 1, log2( r ), log2( p ), key.data(), key.capacity() );
}
} // namespace fc

View file

@ -52,8 +52,8 @@ ENDIF (APPLE)
### copy the two required headers into our output dir as a post build step
# copy public headers to output directory
FOREACH (header ${PUB_HDRS})
#FOREACH (header ${PUB_HDRS})
# preserve relative pathing
ADD_CUSTOM_COMMAND(TARGET easylzma_s POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${header} ${incDir})
ENDFOREACH (header ${PUB_HDRS})
#ADD_CUSTOM_COMMAND(TARGET easylzma_s POST_BUILD
#COMMAND ${CMAKE_COMMAND} -E copy_if_different ${header} ${incDir})
#ENDFOREACH (header ${PUB_HDRS})

13
vendor/scrypt-jane/CMakeLists.txt vendored Normal file
View file

@ -0,0 +1,13 @@
IF (WIN32)
ELSE (WIN32)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -no-integrated-as")
ENDIF (WIN32)
add_definitions( -DSCRYPT_SALSA )
add_definitions( -DSCRYPT_SHA256 )
set( scrypt_sources
scrypt-jane.c
)
add_library( scrypt ${scrypt_sources} )

View file

@ -19,9 +19,15 @@
#include <stdlib.h>
typedef void (*scrypt_fatal_errorfn)(const char *msg);
void scrypt_set_fatal_error(scrypt_fatal_errorfn fn);
#ifdef __cplusplus
extern "C" {
#endif
typedef void (*scrypt_fatal_errorfn)(const char *msg);
void scrypt_set_fatal_error(scrypt_fatal_errorfn fn);
void scrypt(const unsigned char *password, size_t password_len, const unsigned char *salt, size_t salt_len, unsigned char Nfactor, unsigned char rfactor, unsigned char pfactor, unsigned char *out, size_t bytes);
void scrypt(const unsigned char *password, size_t password_len, const unsigned char *salt, size_t salt_len, unsigned char Nfactor, unsigned char rfactor, unsigned char pfactor, unsigned char *out, size_t bytes);
#ifdef __cplusplus
}
#endif
#endif /* SCRYPT_JANE_H */