/* wrapper to call sph functions */ /* Copyright © 2015 Aleksey Cherepanov * * Redistribution and use in source and binary forms, with or without * modification, are permitted. */ #include #include #include #include #include #include #include #include #include #include #define hash_it(name, output) \ sph_ ## name ## _context ctx_ ## name; \ sph_ ## name ## _init (&ctx_ ## name); \ sph_ ## name (&ctx_ ## name, input, len); \ sph_ ## name ## _close(&ctx_ ## name, output); /* At the first run of hash, we hash twice with different "background" to find length of output. Also it is a sanity check that hashing of 1 value twice gives same results (sometime it may reveal use of uninitialized variables). */ #define auto_len(name) \ memset(output, 0, K); \ hash_it(name, output); \ static size_t out_len_ ## name = 0; \ if (out_len_ ## name == 0) { \ memset(output2, 0xFF, K); \ hash_it(name, output2); \ for (i = 0; output[i] == output2[i] && i < K; i++) { \ } \ out_len_ ## name = i; \ assert(i > 0); \ } \ printf("%s:", # name); \ for (i = 0; i < out_len_ ## name; i++) { \ printf("%02x", output[i]); \ } \ printf(":"); \ for (i = 0; i < len; i++) { \ printf("%c", input[i]); \ } \ printf("\n"); int main(int argc, char *argv[]) { #define K 10000 unsigned char output[K]; unsigned char output2[K]; unsigned char input[K]; if (argc != 2) { fprintf(stderr, "need file name as arg\n"); return 1; } FILE *f = fopen(argv[1], "r"); if (!f) { fprintf(stderr, "failed to open file\n"); return 2; } int c; size_t i = 0; while ((c = fgetc(f)) != EOF) { if (c == '\0') { fprintf(stderr, "zero byte in input\n"); return 3; } if (i == K) { fprintf(stderr, "input buffer overflow\n"); return 4; } #define al256(name) auto_len(name ## 256) #define al512(name) auto_len(name ## 512) #define al(name) al256(name) al512(name) /* we exit on eof, we need final \n */ if (c == '\n') { size_t len = i; /* auto_len(blake256); */ /* auto_len(blake512); */ al(blake); al(bmw); al(groestl); al(skein); al(jh); al(keccak); i = 0; } else { input[i] = c; i++; } } fclose(f); return 0; }