| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdint.h> |
| 4 | |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/stat.h> |
| 7 | |
| 8 | #include "KeccakSponge.c" |
| 9 | #include "KeccakF-1600-reference.c" |
| 10 | #include "displayIntermediateValues.c" |
| 11 | |
| 12 | #define HI_NIBBLE(b) (((b) >> 4) & 0x0F) |
| 13 | #define LO_NIBBLE(b) ((b) & 0x0F) |
| 14 | |
| 15 | int main(int argc, char *argv[]) { |
| 16 | |
| 17 | unsigned int r, c, i; |
| 18 | spongeState *state; |
| 19 | |
| 20 | FILE *input; |
| 21 | struct stat input_stat; |
| 22 | unsigned char *input_buf, *output_buf; |
| 23 | size_t input_bytes_read; |
| 24 | |
| 25 | if(argc != 2) { |
| 26 | |
| 27 | printf("no input file provided\nUSAGE: %s <file>\n", argv[0]); |
| 28 | exit(1); |
| 29 | |
| 30 | } else if( ! (input = fopen(argv[1], "r"))) { |
| 31 | |
| 32 | printf("error opening file '%s'\n", argv[1]); |
| 33 | exit(1); |
| 34 | } |
| 35 | |
| 36 | /* significant values */ |
| 37 | r = 576; |
| 38 | c = 1024; |
| 39 | |
| 40 | state = calloc(1, sizeof(spongeState)); |
| 41 | |
| 42 | if(stat(argv[1], &input_stat) || input_stat.st_size <= 0) { |
| 43 | |
| 44 | printf("failed to stat '%s'\n", argv[1]); |
| 45 | exit(1); |
| 46 | |
| 47 | } else if(InitSponge(state, r, c) || !state) { |
| 48 | |
| 49 | printf("error during sponge construction\n"); |
| 50 | exit(1); |
| 51 | |
| 52 | } else if(input_stat.st_size > SIZE_MAX) { |
| 53 | |
| 54 | printf("large files not supported yet\n"); |
| 55 | exit(1); |
| 56 | } |
| 57 | |
| 58 | input_buf = calloc(input_stat.st_size, sizeof(unsigned char)); |
| 59 | output_buf = calloc(64, sizeof(unsigned char)); |
| 60 | |
| 61 | if( ! (input_bytes_read = fread(input_buf, 1, (size_t)input_stat.st_size, input))) { |
| 62 | |
| 63 | printf("error reading file\n"); |
| 64 | exit(1); |
| 65 | } |
| 66 | |
| 67 | fclose(input); |
| 68 | |
| 69 | if(Absorb(state, input_buf, 8 * (unsigned long long) input_stat.st_size)) { |
| 70 | |
| 71 | printf("encryption failure\n"); |
| 72 | exit(1); |
| 73 | |
| 74 | } else if(Squeeze(state, output_buf, 512)) { |
| 75 | |
| 76 | printf("decryption failure\n"); |
| 77 | exit(1); |
| 78 | } |
| 79 | |
| 80 | i = 0; |
| 81 | for(; i < 64; i++) |
| 82 | printf("%x%x", HI_NIBBLE(output_buf[i]), LO_NIBBLE(output_buf[i])); |
| 83 | |
| 84 | free(state); |
| 85 | free(input_buf); |
| 86 | free(output_buf); |
| 87 | |
| 88 | return 0; |
| 89 | } |