-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarp.c
179 lines (152 loc) · 3.38 KB
/
warp.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*********************************************
* Reference implementation by WARP Team *
**********************************************/
/*
Modified version of the refernce implementation
to simulation boomerang attack on WARP
Modifier:
*/
#include "warp.h"
void printState(int *state)
{
printf("L: ");
for (int x = 0; x < BR_HALF; x++)
{
printf("%x ", state[2 * x + 0]);
}
printf("R: ");
for (int x = 0; x < BR_HALF; x++)
{
printf("%x ", state[2 * x + 1]);
}
printf("\n");
}
void sboxkey(int *state, int *k, int r)
{
for (int i = 0; i < BR_HALF; i++)
{
state[i] = Sbox[state[i]] ^ k[(r % 2) * 16 + i];
}
}
void permutation(int *state)
{
int temp[BR];
for (int j = 0; j < BR; j++)
{
temp[j] = state[j];
}
for (int j = 0; j < BR; j++)
{
state[perm[j]] = temp[j];
}
}
void inv_permutation(int *state)
{
int temp[BR];
for (int j = 0; j < BR; j++)
{
temp[j] = state[j];
}
for (int j = 0; j < BR; j++)
{
state[j] = temp[perm[j]];
}
}
void enc(int *m, int *c, int *k, int R)
{
/*intermediate value*/
int state[BR];
/*left half intermediate value*/
int temp[BR_HALF];
for (int i = 0; i < BR; i++)
{
state[i] = m[i];
}
/*round function(1 to 40 round)*/
for (int i = 0; i < R; i++)
{
#if PRINT_INTER
printf("%d round\n", i + 1);
printState(state);
#endif
for (int j = 0; j < BR_HALF; j++)
{
temp[j] = state[j * 2];
}
/*insert key and Sbox*/
sboxkey(temp, k, i);
/*XOR*/
for (int j = 0; j < BR_HALF; j++)
{
state[2 * j + 1] = state[2 * j + 1] ^ temp[j];
}
/*add round constants*/
state[1] = state[1] ^ RC0[i];
state[3] = state[3] ^ RC1[i];
/*permutation*/
permutation(state);
}
/*last round function */
#if PRINT_INTER
printf("%d round\n", R);
printState(state);
#endif
#if PRINT_INTER
printState(state);
#endif
/*no permutation in the last round*/
/*copy ciphertext*/
for (int i = 0; i < BR; i++)
{
c[i] = state[i];
}
}
void dec(int *m, int *c, int *k, int R)
{
/*intermediate value*/
int state[BR];
/*left half intermediate value*/
int temp[BR_HALF];
for (int i = 0; i < BR; i++)
{
state[i] = c[i];
}
/*round function(1 to 40 round)*/
for (int i = R - 1; i >= 0; i--)
{
#if PRINT_INTER
printf("%d round\n", i + 1);
printState(state);
#endif
/*inverse of permutation*/
inv_permutation(state);
/*add round constants*/
state[1] = state[1] ^ RC0[i];
state[3] = state[3] ^ RC1[i];
for (int j = 0; j < BR_HALF; j++)
{
temp[j] = state[j * 2];
}
/*insert key and Sbox*/
sboxkey(temp, k, i);
/*XOR*/
for (int j = 0; j < BR_HALF; j++)
{
state[2 * j + 1] = state[2 * j + 1] ^ temp[j];
}
}
/*last round function */
#if PRINT_INTER
printf("%d round\n", R);
printState(state);
#endif
#if PRINT_INTER
printState(state);
#endif
/*no permutation in the last round*/
/*copy plaintext*/
for (int i = 0; i < BR; i++)
{
m[i] = state[i];
}
}