0

I've been trying to use the SHA_256 functions in sha256.h on a FreeBSD 9.1 system but it seems to be corrupting memory space of my program causing all kinds of manic behavior. I wrote up a quick program to just to play with those functions and still am having problems.

In the example below, int i is changed when I call SHA256_Init(), as shown by the output from the printf() statements surrounding it.

This is what I get running the code.

$ ./miner "hello world"  
i = 0  
i = 32  
0000000032      9010a9cf81ce2c28a642fd03ddf6da5790c65c30cd4a148c4257d3fe488bacc7

Why is this value changing to 32? Am I missing something? The code is below...

#include <sha256.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#define DIFFICULTY 0

int main(int argc, const char **argv) {
    uint nonce, i, j;
    SHA256_CTX ctx;
    size_t arglen;
    unsigned char digest[32];
    char * data;

    if(argc < 1) exit(1);
    arglen = strlen(argv[1]);
    data = malloc(arglen + 1);
    char digestStr[65];
    i = 0;
    do {
        nonce = i;
        strncpy(data, argv[1], arglen + 1);

        printf("i = %i\n", i);
        SHA256_Init(&ctx);
        printf("i = %i\n", i);

        SHA256_Update(&ctx, data, arglen);
        SHA256_Update(&ctx, (unsigned char *) &nonce, sizeof(nonce));
        SHA256_Final(digest, &ctx);
        SHA256_End(&ctx, digestStr);
        printf("%010i\t%s\n", i, digestStr);

        j = 0;
        while(j < 32 && digest[j] == '\0') {
            j++;
        }
        i++;
    } while(j < DIFFICULTY);

    free(data);
    return 0;
}
  • "SHA_256 functions " or a bug in your code? - I know where my money is... – Mitch Wheat Apr 25 '13 at 02:57
  • I never said the SHA265 functions are the problem, just that they 'seem' to be corrupting memory space. Whether its because I screwed a pointer somewhere or not is irrelevant. Its pretty simple code and I'm asking because someone who has used these functions before may be familiar with something I'm not, or quickly pick out some error in my code that I'm missing. Obviously somethings wrong here because the functions work, and memory usually doesn't rewrite itself... Well, unless this is a much bigger problem ;) – user1842941 Apr 25 '13 at 03:16
  • I don't know what's causing your error, but you should be checking `if(argc < 2)` and you should use `%u` to `printf` a `uint`. – Joshua Green Apr 25 '13 at 04:39

1 Answers1

1

I just had this exact same problem and solved it.

The issue is that your are including a different header in your code than the SHA2 library you linked into your application is using.

In my case the SHA256_CTX struct was a different size in the openSSL library. The openSSL library's struct was 8 bytes bigger than the struct length in the file.

The function SHA256_Init(&ctx) does a memset on the SHA256_CTX struct which then corrupts 8 extra random bytes after the struct. I say random because it will do different things in a release vs debug build because the optimizing compiler will move your variables around.