forked from strawbrary/php-sha3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
displayIntermediateValues.c
158 lines (141 loc) · 4.8 KB
/
displayIntermediateValues.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni,
Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby
denoted as "the implementer".
For more information, feedback or questions, please refer to our websites:
http://keccak.noekeon.org/
http://keyak.noekeon.org/
http://ketje.noekeon.org/
To the extent possible under law, the implementer has waived all copyright
and related or neighboring rights to the source code in this file.
http://creativecommons.org/publicdomain/zero/1.0/
*/
#include <stdio.h>
#include "displayIntermediateValues.h"
#include "SnP-interface.h"
FILE *intermediateValueFile = 0;
int displayLevel = 0;
void displaySetIntermediateValueFile(FILE *f)
{
intermediateValueFile = f;
}
void displaySetLevel(int level)
{
displayLevel = level;
}
void displayBytes(int level, const char *text, const unsigned char *bytes, unsigned int size)
{
unsigned int i;
if ((intermediateValueFile) && (level <= displayLevel)) {
fprintf(intermediateValueFile, "%s:\n", text);
for(i=0; i<size; i++)
fprintf(intermediateValueFile, "%02X ", bytes[i]);
fprintf(intermediateValueFile, "\n");
fprintf(intermediateValueFile, "\n");
}
}
void displayBits(int level, const char *text, const unsigned char *data, unsigned int size, int MSBfirst)
{
unsigned int i, iByte, iBit;
if ((intermediateValueFile) && (level <= displayLevel)) {
fprintf(intermediateValueFile, "%s:\n", text);
for(i=0; i<size; i++) {
iByte = i/8;
iBit = i%8;
if (MSBfirst)
fprintf(intermediateValueFile, "%d ", ((data[iByte] << iBit) & 0x80) != 0);
else
fprintf(intermediateValueFile, "%d ", ((data[iByte] >> iBit) & 0x01) != 0);
}
fprintf(intermediateValueFile, "\n");
fprintf(intermediateValueFile, "\n");
}
}
void displayStateAsBytes(int level, const char *text, const unsigned char *state)
{
displayBytes(level, text, state, SnP_width/8);
}
#if (SnP_laneLengthInBytes == 8)
void displayStateAs32bitWords(int level, const char *text, const unsigned int *state)
{
unsigned int i;
if ((intermediateValueFile) && (level <= displayLevel)) {
fprintf(intermediateValueFile, "%s:\n", text);
for(i=0; i<SnP_width/64; i++) {
fprintf(intermediateValueFile, "%08X:%08X", (unsigned int)state[2*i+0], (unsigned int)state[2*i+1]);
if ((i%5) == 4)
fprintf(intermediateValueFile, "\n");
else
fprintf(intermediateValueFile, " ");
}
}
}
#endif
void displayStateAsLanes(int level, const char *text, void *statePointer)
{
unsigned int i;
#if (SnP_laneLengthInBytes == 8)
unsigned long long int *state = statePointer;
#endif
#if (SnP_laneLengthInBytes == 4)
unsigned int *state = statePointer;
#endif
#if (SnP_laneLengthInBytes == 2)
unsigned short *state = statePointer;
#endif
#if (SnP_laneLengthInBytes == 1)
unsigned char *state = statePointer;
#endif
if ((intermediateValueFile) && (level <= displayLevel)) {
fprintf(intermediateValueFile, "%s:\n", text);
#if (SnP_laneLengthInBytes == 8)
for(i=0; i<25; i++) {
fprintf(intermediateValueFile, "%08X", (unsigned int)(state[i] >> 32));
fprintf(intermediateValueFile, "%08X", (unsigned int)(state[i] & 0xFFFFFFFFULL));
if ((i%5) == 4)
fprintf(intermediateValueFile, "\n");
else
fprintf(intermediateValueFile, " ");
}
#endif
#if (SnP_laneLengthInBytes == 4)
for(i=0; i<25; i++) {
fprintf(intermediateValueFile, "%08X", state[i]);
if ((i%5) == 4)
fprintf(intermediateValueFile, "\n");
else
fprintf(intermediateValueFile, " ");
}
#endif
#if (SnP_laneLengthInBytes == 2)
for(i=0; i<25; i++) {
fprintf(intermediateValueFile, "%04X ", state[i]);
if ((i%5) == 4)
fprintf(intermediateValueFile, "\n");
}
#endif
#if (SnP_laneLengthInBytes == 1)
for(i=0; i<25; i++) {
fprintf(intermediateValueFile, "%02X ", state[i]);
if ((i%5) == 4)
fprintf(intermediateValueFile, "\n");
}
#endif
}
}
void displayRoundNumber(int level, unsigned int i)
{
if ((intermediateValueFile) && (level <= displayLevel)) {
fprintf(intermediateValueFile, "\n");
fprintf(intermediateValueFile, "--- Round %d ---\n", i);
fprintf(intermediateValueFile, "\n");
}
}
void displayText(int level, const char *text)
{
if ((intermediateValueFile) && (level <= displayLevel)) {
fprintf(intermediateValueFile, "%s", text);
fprintf(intermediateValueFile, "\n");
fprintf(intermediateValueFile, "\n");
}
}