#include #include #include #include #include #include "../keccak-ref/Sources/KeccakSponge.c" #include "../keccak-ref/Sources/KeccakF-1600-reference.c" #include "../keccak-ref/Sources/displayIntermediateValues.c" #define HI_NIBBLE(b) (((b) >> 4) & 0x0F) #define LO_NIBBLE(b) ((b) & 0x0F) int main(int argc, char *argv[]) { unsigned int r, c, i; spongeState *state; FILE *input; struct stat input_stat; unsigned char *input_buf, *output_buf; size_t input_bytes_read; if(argc != 2) { printf("no input file provided\nUSAGE: %s \n", argv[0]); exit(1); } else if( ! (input = fopen(argv[1], "r"))) { printf("error opening file '%s'\n", argv[1]); exit(1); } /* significant values */ r = 576; c = 1024; state = (spongeState*) calloc(1, sizeof(spongeState)); if(stat(argv[1], &input_stat) || input_stat.st_size <= 0) { printf("failed to stat '%s'\n", argv[1]); exit(1); } else if(InitSponge(state, r, c) || !state) { printf("error during sponge construction\n"); exit(1); } else if(input_stat.st_size > SIZE_MAX) { printf("large files not supported yet\n"); exit(1); } input_buf = (unsigned char *) calloc(1, (size_t) input_stat.st_size); output_buf = (unsigned char *) calloc(1, (size_t) 64); if( ! (input_bytes_read = fread(input_buf, 1, (size_t)input_stat.st_size, input))) { printf("error reading file\n"); exit(1); } fclose(input); if(Absorb(state, input_buf, (unsigned long long) (8 * input_stat.st_size))) { printf("encryption failure\n"); exit(1); } else if(Squeeze(state, output_buf, (unsigned long long) (8 * input_stat.st_size))) { printf("decryption failure\n"); exit(1); } i = 0; for(; i < 64; i++) printf("%x%x", HI_NIBBLE(output_buf[i]), LO_NIBBLE(output_buf[i])); free(state); return 0; }