cc395669 |
1 | /* |
2 | The Keccak sponge function, designed by Guido Bertoni, Joan Daemen, |
3 | Michaƫl Peeters and Gilles Van Assche. For more information, feedback or |
4 | questions, please refer to our website: http://keccak.noekeon.org/ |
5 | |
6 | Implementation by the designers, |
7 | hereby denoted as "the implementer". |
8 | |
9 | To the extent possible under law, the implementer has waived all copyright |
10 | and related or neighboring rights to the source code in this file. |
11 | http://creativecommons.org/publicdomain/zero/1.0/ |
12 | */ |
13 | |
14 | #include <stdio.h> |
15 | #include "displayIntermediateValues.h" |
16 | #include "KeccakNISTInterface.h" |
17 | |
18 | FILE *intermediateValueFile = 0; |
19 | int displayLevel = 0; |
20 | |
21 | void displaySetIntermediateValueFile(FILE *f) |
22 | { |
23 | intermediateValueFile = f; |
24 | } |
25 | |
26 | void displaySetLevel(int level) |
27 | { |
28 | displayLevel = level; |
29 | } |
30 | |
31 | void displayBytes(int level, const char *text, const unsigned char *bytes, unsigned int size) |
32 | { |
33 | unsigned int i; |
34 | |
35 | if ((intermediateValueFile) && (level <= displayLevel)) { |
36 | fprintf(intermediateValueFile, "%s:\n", text); |
37 | for(i=0; i<size; i++) |
38 | fprintf(intermediateValueFile, "%02X ", bytes[i]); |
39 | fprintf(intermediateValueFile, "\n"); |
40 | fprintf(intermediateValueFile, "\n"); |
41 | } |
42 | } |
43 | |
44 | void displayBits(int level, const char *text, const unsigned char *data, unsigned int size, int MSBfirst) |
45 | { |
46 | unsigned int i, iByte, iBit; |
47 | |
48 | if ((intermediateValueFile) && (level <= displayLevel)) { |
49 | fprintf(intermediateValueFile, "%s:\n", text); |
50 | for(i=0; i<size; i++) { |
51 | iByte = i/8; |
52 | iBit = i%8; |
53 | if (MSBfirst) |
54 | fprintf(intermediateValueFile, "%d ", ((data[iByte] << iBit) & 0x80) != 0); |
55 | else |
56 | fprintf(intermediateValueFile, "%d ", ((data[iByte] >> iBit) & 0x01) != 0); |
57 | } |
58 | fprintf(intermediateValueFile, "\n"); |
59 | fprintf(intermediateValueFile, "\n"); |
60 | } |
61 | } |
62 | |
63 | void displayStateAsBytes(int level, const char *text, const unsigned char *state) |
64 | { |
65 | displayBytes(level, text, state, KeccakPermutationSizeInBytes); |
66 | } |
67 | |
68 | void displayStateAs32bitWords(int level, const char *text, const unsigned int *state) |
69 | { |
70 | unsigned int i; |
71 | |
72 | if ((intermediateValueFile) && (level <= displayLevel)) { |
73 | fprintf(intermediateValueFile, "%s:\n", text); |
74 | for(i=0; i<KeccakPermutationSize/64; i++) { |
75 | fprintf(intermediateValueFile, "%08X:%08X", (unsigned int)state[2*i+0], (unsigned int)state[2*i+1]); |
76 | if ((i%5) == 4) |
77 | fprintf(intermediateValueFile, "\n"); |
78 | else |
79 | fprintf(intermediateValueFile, " "); |
80 | } |
81 | } |
82 | } |
83 | |
84 | void displayStateAs64bitWords(int level, const char *text, const unsigned long long int *state) |
85 | { |
86 | unsigned int i; |
87 | |
88 | if ((intermediateValueFile) && (level <= displayLevel)) { |
89 | fprintf(intermediateValueFile, "%s:\n", text); |
90 | for(i=0; i<KeccakPermutationSize/64; i++) { |
91 | fprintf(intermediateValueFile, "%08X", (unsigned int)(state[i] >> 32)); |
92 | fprintf(intermediateValueFile, "%08X", (unsigned int)(state[i] & 0xFFFFFFFFULL)); |
93 | if ((i%5) == 4) |
94 | fprintf(intermediateValueFile, "\n"); |
95 | else |
96 | fprintf(intermediateValueFile, " "); |
97 | } |
98 | } |
99 | } |
100 | |
101 | void displayRoundNumber(int level, unsigned int i) |
102 | { |
103 | if ((intermediateValueFile) && (level <= displayLevel)) { |
104 | fprintf(intermediateValueFile, "\n"); |
105 | fprintf(intermediateValueFile, "--- Round %d ---\n", i); |
106 | fprintf(intermediateValueFile, "\n"); |
107 | } |
108 | } |
109 | |
110 | void displayText(int level, const char *text) |
111 | { |
112 | if ((intermediateValueFile) && (level <= displayLevel)) { |
113 | fprintf(intermediateValueFile, text); |
114 | fprintf(intermediateValueFile, "\n"); |
115 | fprintf(intermediateValueFile, "\n"); |
116 | } |
117 | } |