Fix scrypt

This commit is contained in:
Vikram Rajkumar 2014-07-14 11:40:13 -04:00
parent 506f896de4
commit e442b29f6e
5 changed files with 36 additions and 57 deletions

View file

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

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 log2( unsigned n )
{
/*
unsigned int chunk_bytes = SCRYPT_BLOCK_BYTES * r * 2;
std::vector<unsigned char> yx((p+1) * chunk_bytes);
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) ) );
if( n <= 0 ) FC_THROW_EXCEPTION( exception, "cannot take log2(${n})", ("n",n) );
unsigned i = 0;
while( n >>= 1 ) ++i;
return i;
}
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 )
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 )
{
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 );
*/
scrypt( passphrase.data(), passphrase.size(), salt.data(), salt.size(),
log2( n ) - 1, log2( r ), log2( p ), key.data(), key.capacity() );
}
} // namespace fc

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

@ -0,0 +1,8 @@
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 */