How to get a sha256 hash of a file using ANSI C - c

Simply what the topic states, what's a memory efficient way to compute a sha256 hash of a file that's variable in size? I'm also willing to compromise using more memory for faster computation.

I used a simple stand-alone implementation by Christophe Devine -- and while his site seems to be off the Net, Google Code Search finds it
in this place.
Using these sha256.c and sha256.h, the core of his main() function is simply
if( ! ( f = fopen( argv[1], "rb" ) ) )
{
perror( "fopen" );
return( 1 );
}
sha256_starts( &ctx );
while( ( i = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
{
sha256_update( &ctx, buf, i );
}
sha256_finish( &ctx, sha256sum );
for( j = 0; j < 32; j++ )
{
printf( "%02x", sha256sum[j] );
}
printf( " %s\n", argv[1] );
}
The rest of the main() function validates the FIPS-180-2 test vectors, so you get that warm and fuzzy feeling too/ ;-)

Or use the OpenSSL libcrypto:
https://wiki.openssl.org/index.php/EVP_Message_Digests

You may try Con Kolivas's implementation used in multi-threaded multi-pool FPGA and ASIC miner for bitcoin or well profiled Dr Brian Gladman's code.

Related

mbedtls: error on mbedtls_ctr_drbg_seed

I'm using mbedtls to run SSL server.
The function mbedtls_ctr_drbg_seed returned -34.
My code is below:
const char *pers = "ssl_server2";
mbedtls_havege_state hs;
mbedtls_ssl_session ssn;
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
// One HTTPS Request Handling
memset( &ssn, 0, sizeof( mbedtls_ssl_session ) );
/*
* 4. Setup stuff
*/
mbedtls_ssl_init( &ssl );
mbedtls_ssl_config_init( &conf );
mbedtls_ctr_drbg_init( &ctr_drbg );
mbedtls_entropy_init( &entropy );
printf( " . Setting up the RNG and SSL data...." );
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) pers, sizeof( pers ) ) ) != 0 )
{
printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n", -ret );
goto ExitFunction;
}
else
printf( " mbedtls_ctr_drbg_seed returned 0x%x ok\n", ret );
As #Gilles rightfully said, the error you are receiving is probably -0x34, which is MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED. This error is returned when the function mbedtls_entropy_func() fails. Please check the the entropy source you are using is strong enough, meaning you have at least one entropy source which is strong, when added with mbedtls_entropy_add_source(). You should also verify that the entropy source you are using can collect enough entropy, and exceeds the threshold set to the source.
There are other locations where mbedtls_entropy_func() might fail, therefore I suggest you check these locations as well.

Using z3 api to solve LRA runs slower than use z3 in terminal

I am trying to use Z3 to solve a random generalized strip-packing problem(LRA) and I call Z3 api in a c program, here is the code.
Z3_context ctx;
Z3_ast fs;
LOG_MSG("smt2parser_example");
FILE *fp = fopen("smttest","r");
if(fp == NULL)
{
perror("fopen()");
return;
}
int file_size;
fseek(fp,0,SEEK_END);
file_size = ftell( fp );
char *tmp;
fseek( fp , 0 , SEEK_SET);
tmp = (char *)malloc( (file_size+1) * sizeof( char ) );
tmp[file_size]='\0';
fread( tmp , file_size , sizeof(char) , fp);
ctx = mk_context();
fs = Z3_parse_smtlib2_string(ctx, tmp, 0, 0, 0, 0, 0, 0);
Z3_assert_cnstr(ctx, fs);
Z3_model m = 0;
Z3_check(ctx);
Z3_del_context(ctx);
I also try to solve smttest in terminal by command "z3 smttest". However, in terminal , it runs faster than calling api in c program. I wonder is there any configuration I need to set to make it run fast in api mode?(by the way, z3 runs two times faster in terminal than calling an api.)
The function "Z3_assert_cnstr(ctx, fs);" is no longer available, so you must be using a very old version of Z3. Use the "solver" objects to assert expressions, and also use the C++ API for improved reliability. You can create solvers for specified logics, such as "QF_LRA", in which case the initial setup is forced to be appropriate for that logic. By default Z3 will try to automatically find good settings by analyzing the asserted formulas before the first check-sat.

Program crashes when trying to scan a string

I'm having trouble scanning data from a .dat file containing arbitrary game results (for testing the program)
The results are formatted as follows: (# representing the score as an integer)
team_a # team_b #
team_a # team_b #
team_a # team_b #
.
.
.
team_a # team_b #
Each row is a different game.
What I'm trying to do with my code at the moment is to use the fgets() function to scan each game/row, then use the sscanf_s() function to take the data from each row (seeing as though i know the way it is formatted) and store it into the data structure i've defined.
I'm more than happy to take any advice on changes i should make to the way i'm going about getting the data into the struct if there is an easier, faster and/more reliable (foolproof) way to do it.
Any help is greatly appreciated.
Microsoft's secure sscanf_s has a slightly different way of interpreting format specifiers and arguments: In order to prevent buffer overflows, each string format (%s, %c and %[) must pass the corresponding buffer size after the buffer.
So your scan command should read:
sscanf_s(data_file_line[i],
"%s %d %s %d",
game_results[i].first_team_name,
sizeof(game_results[i].first_team_name),
&game_results[i].first_team_score,
game_results[i].second_team_name,
sizeof(game_results[i].second_team_name),
&game_results[i].second_team_score);
There are some other issues with your code:
You should check the return value of sscanf_s so you know that the line has been parsed successfully. The return value is the number of items converted, so in your case it should be 4. Also note that %s scans words and team names like "Man Utd" and "Stoke City" have two words and will not parse correctly.
As others have noted, the feof construct will make you read the file once too many. Forget about foef and use the return values of the reading functions instead. For eample, fgets returns NULL when the end of the file is reached, so you can use that as loop condition: while (fgets(buf, sizeof(buf), f)) ...
You don't check whether i overflows. If you have a long file, i might not be big enough.
If you are parsing and storing the lines right away, there's no need to have an array of lines; just use one line buffer over and over.
I simplified a little and it works fine. Your work is not finished. Please try this,
#include <usual.h>
#define MAX_NAME_CHARS 15
#define MAX_DATA_FILE_LINE_LENGTH 32
#define MAX_GAME_RESULTS 128
int main( void )
{
FILE *inp2b;
typedef struct game_results
{
char first_team_name[MAX_NAME_CHARS];
int first_team_score;
char second_team_name[MAX_NAME_CHARS];
int second_team_score;
} game_results_t;
game_results_t game_results[MAX_GAME_RESULTS];
char data_file_line[MAX_DATA_FILE_LINE_LENGTH][MAX_DATA_FILE_LINE_LENGTH];
int errorcode = 0;
int i = 0;
//errorcode = fopen_s(&inp2b,"C:\\Users\\Cody\\Documents\\Visual Studio 2012\\DATAFILES FOR PA2\\input2b.dat","r");
inp2b = fopen( "C:\\testdat\\input2b.dat", "r" );
if ( inp2b == NULL )
errorcode = 1;
if ( errorcode != 0 )
{
printf( "Error opening 2nd data file!\n\n" );
return ( 0 );
}
else
{
printf( "\n\n\nFile was opened successfully!\n\n" );
}
i = 0;
while ( !feof( inp2b ) )
{
fgets( data_file_line[i], MAX_DATA_FILE_LINE_LENGTH, inp2b );
puts( data_file_line[i] );
printf( "\n" );
// sscanf_s(data_file_line[i],"%s %d %s %d",game_results[i].first_team_name,&game_results[i].first_team_score,game_results[i].second_team_name,&game_results[i].second_team_score);
sscanf( data_file_line[i], "%s %d %s %d", game_results[i].first_team_name,
&game_results[i].first_team_score,
game_results[i].second_team_name,
&game_results[i].second_team_score );
printf( "\n\n %s %d %s %d \n\n", game_results[i].first_team_name,
game_results[i].first_team_score,
game_results[i].second_team_name,
game_results[i].second_team_score );
i++;
}
fclose( inp2b );
return ( 0 );
}

fgetpos failing in iOS 6.1 simulator when opening files in bundles

I'm working on some multiplatform C++ code. Hence the requirement to use fopen, fgetpos, etc. The following was working earlier on iOS and with my update to the latest version it stopped working.
I have a couple of text files Shader.vsh and Shader.fsh in a Shaders folder that is getting copied over to the bundle. In order to open the file, I do the following..
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFStringRef cfstrFilename = CFSTRFromConstCharPtr( "Shader" );
CFStringRef cfstrFileType = CFSTRFromConstCharPtr( "vsh" );
CFStringRef cfstrSubDir = CFSTRFromConstCharPtr( "Shaders" );
CFURLRef resourcesURL = CFBundleCopyResourceURL( mainBundle, cfstrFilename, cfstrFileType, cfstrSubDir );
CFStringRef str = CFURLCopyFileSystemPath( resourcesURL, kCFURLPOSIXPathStyle );
CFRelease( resourcesURL );
char path[ PATH_MAX ];
CFStringGetCString( str, path, FILENAME_MAX, kCFStringEncodingASCII );
CFRelease( str );
FILE* fp = fopen( path, "rb" );
At this point, fp is Non NULL. So I assume it succeeded. Later, when I try to do
fpos_t pos;
int result = fgetpos( fp, &fpos_t );
result = -1 and errno = 0x2, which I believe is file not found.
As I mentioned earlier, this used to work on a previous version at some point. I started working on this again and in the process updated to the latest XCode, etc and things stopped working.
The path for the file that I pass into fopen turns out to be /Users/shammi/Library/Application Support/iPhone Simulator/6.1/Applications/9132490F-71AC-4C61-A584-E8F6C5B261FF/TestApp.app/Shaders/Shader.vsh
I'm able to see and open that file in finder/console and confirmed that its valid.
What am I doing wrong? Is there another alternative that allows me to use portable IO functions?
Found the problem. What I failed to mention here is what happens between the 2 sections of code above. Prior to this, I used to have my own ref counting solution and I recently changed to using shared_ptr. My own ref counting solution allowed for implicit casts. With shared_ptr, you can't do that. So here is the exact code...
std::shared_ptr< BinaryStream > BundleNamespace::OpenStream( const char* _szPath,
BinaryStream::Mode _eMode )
{
std::shared_ptr< BinaryStream > pStream = __super::OpenStream( _szPath, _eMode );
if ( !pStream )
{
std::string strDir;
std::string strFile;
std::string strExt;
SplitPath( _szPath, strDir, strFile, strExt );
std::string strFullPath = GetResourcePathFor( strFile.c_str(), strExt.c_str(), strDir.c_str() );
FILE* fp = fopen( strFullPath.c_str(), _eMode == BinaryStream::Mode_Read ? "r" : "w+b" );
pStream = std::make_shared<FileStream>( fp, _eMode );
}
return pStream;
}
The problem here is with
pStream = std::make_shared<FileStream>( fp, _eMode );
My FileStream's destructor calls fclose(m_pFile). The fix here is to change it to..
pStream = std::static_pointer_cast< BinaryStream >( std::make_shared<FileStream>( fp, _eMode ) );`
Also, using perror() proved to be more useful compared to trying to decipher errno.

How to know the address range when searching for a function by its signature?

I'm trying to search for a function by its "signature".
However I can't figure out what address range I'm supposed to be searching?
I've had a look at VirtualQuery() and GetNativeSystemInfo() but I'm not if I'm on the right path or not.
Edit: Question re-attempt.
Using Win32 API I'm trying to find out how to get the start and end address of the executable pages of the process my code is executing in.
This is what I've tried:
SYSTEM_INFO info;
ZeroMemory( &info, sizeof( SYSTEM_INFO ) );
GetNativeSystemInfo( &info ); // GetSystemInfo() might be wrong on WOW64.
info.lpMinimumApplicationAddress;
info.lpMaximumApplicationAddress;
HANDLE thisProcess = GetCurrentProcess();
MEMORY_BASIC_INFORMATION memInfo;
ZeroMemory( &memInfo, sizeof( memInfo ) );
DWORD addr = (DWORD)info.lpMinimumApplicationAddress;
do
{
if ( VirtualQueryEx( thisProcess, (LPVOID)addr, &memInfo, sizeof( memInfo ) ) == 0 )
{
DWORD gle = GetLastError();
if ( gle != ERROR_INVALID_PARAMETER )
{
std::stringstream str;
str << "VirtualQueryEx failed with: " << gle;
MessageBoxA( NULL, str.str().c_str(), "Error", MB_OK );
}
break;
}
if ( memInfo.Type == MEM_IMAGE )
{
// TODO: Scan this memory block for the the sigature
}
addr += info.dwPageSize;
}
while ( addr < (DWORD)info.lpMaximumApplicationAddress );
The reason for doing this is that I'm looking for an un-exported function by its signature as asked here:
Find a function by it signature in Windows DLL
See the answer about "code signature scanning".
While this is enumerating an address range I don't know if this is correct or not since I don't know what the expected range should be. Its just the best I could come up with from looking around MSDN.
the address range when signature scanning a module is from the start of the code section to the start + the section size. the start of the code section and its size are in the PE. most tools take the lazy route and scan the entire module (again using the PE to get the size, but with the module handle as the start address).

Resources