bring in keccak symbols
[mobile-com.git] / DH-Keccak / assets / KremKeccak / krem_keccak.c
CommitLineData
cc395669 1#include <stdio.h>
2#include <stdlib.h>
3#include <stdint.h>
4
5#include <sys/types.h>
6#include <sys/stat.h>
7
25220e07 8#include "KeccakSponge.c"
9#include "KeccakF-1600-reference.c"
10#include "displayIntermediateValues.c"
cc395669 11
12#define HI_NIBBLE(b) (((b) >> 4) & 0x0F)
13#define LO_NIBBLE(b) ((b) & 0x0F)
14
15int 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
c64e115f 40 state = calloc(1, sizeof(spongeState));
cc395669 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
c64e115f 58 input_buf = calloc(1, (size_t) input_stat.st_size);
59 output_buf = calloc(1, (size_t) 64);
cc395669 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, (unsigned long long) (8 * input_stat.st_size))) {
70
71 printf("encryption failure\n");
72 exit(1);
73
74 } else if(Squeeze(state, output_buf, (unsigned long long) (8 * input_stat.st_size))) {
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
86 return 0;
87}