From e442b29f6e59ad96f87e1043b34da08ca4893903 Mon Sep 17 00:00:00 2001 From: Vikram Rajkumar Date: Mon, 14 Jul 2014 11:40:13 -0400 Subject: [PATCH] Fix scrypt --- CMakeLists.txt | 3 +- include/fc/crypto/scrypt.hpp | 5 +-- src/crypto/scrypt.cpp | 65 +++++++------------------------ vendor/scrypt-jane/CMakeLists.txt | 8 ++++ vendor/scrypt-jane/scrypt-jane.h | 12 ++++-- 5 files changed, 36 insertions(+), 57 deletions(-) create mode 100644 vendor/scrypt-jane/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 87be292..3b0ed56 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -182,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 ) @@ -219,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 ) diff --git a/include/fc/crypto/scrypt.hpp b/include/fc/crypto/scrypt.hpp index 53d44df..b74708a 100644 --- a/include/fc/crypto/scrypt.hpp +++ b/include/fc/crypto/scrypt.hpp @@ -1,10 +1,9 @@ #pragma once -#include #include namespace fc { - void scrypt_derive_key ( const std::vector &passphrase, const std::vector &salt, - unsigned int n, unsigned int r, unsigned int p, std::vector &key ); + void scrypt_derive_key( const std::vector& passphrase, const std::vector& salt, + unsigned int n, unsigned int r, unsigned int p, std::vector& key ); } // namespace fc diff --git a/src/crypto/scrypt.cpp b/src/crypto/scrypt.cpp index 5eebe39..63b6710 100644 --- a/src/crypto/scrypt.cpp +++ b/src/crypto/scrypt.cpp @@ -1,57 +1,22 @@ -#include - -#include +#include #include - -#include - -#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 &passphrase, const std::vector &salt, - unsigned int n, unsigned int r, unsigned int p, std::vector &key ) - { - /* - unsigned int chunk_bytes = SCRYPT_BLOCK_BYTES * r * 2; - std::vector 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 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& passphrase, const std::vector& salt, + unsigned int n, unsigned int r, unsigned int p, std::vector& key ) + { + scrypt( passphrase.data(), passphrase.size(), salt.data(), salt.size(), + log2( n ) - 1, log2( r ), log2( p ), key.data(), key.capacity() ); + } } // namespace fc diff --git a/vendor/scrypt-jane/CMakeLists.txt b/vendor/scrypt-jane/CMakeLists.txt new file mode 100644 index 0000000..305c6e3 --- /dev/null +++ b/vendor/scrypt-jane/CMakeLists.txt @@ -0,0 +1,8 @@ +add_definitions( -DSCRYPT_SALSA ) +add_definitions( -DSCRYPT_SHA256 ) + +set( scrypt_sources + scrypt-jane.c + ) + +add_library( scrypt ${scrypt_sources} ) diff --git a/vendor/scrypt-jane/scrypt-jane.h b/vendor/scrypt-jane/scrypt-jane.h index 1c0df62..bebad72 100644 --- a/vendor/scrypt-jane/scrypt-jane.h +++ b/vendor/scrypt-jane/scrypt-jane.h @@ -19,9 +19,15 @@ #include -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 */