-
Notifications
You must be signed in to change notification settings - Fork 6
/
f_xy.c
199 lines (161 loc) · 4 KB
/
f_xy.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "types.h"
#include "utils.h"
//#define DEBUG
// flip each word and return as a u64 array
void aes_flip_to_64(u32 *in, u64* out)
{
u32 endian_flip[4];
u32 i;
for(i = 0; i < 4; i++)
endian_flip[i] = getbe32((u8*)&in[i]);
out[0] = (u64)endian_flip[1] | ((u64)endian_flip[0] << 32);
out[1] = (u64)endian_flip[3] | ((u64)endian_flip[2] << 32);
}
void aes_unflip_to_32(u64* in, u32* out)
{
out[0] = getbe32((u8*)&in[0]+4);
out[1] = getbe32((u8*)&in[0]);
out[2] = getbe32((u8*)&in[1]+4);
out[3] = getbe32((u8*)&in[1]);
}
void n128_lrot_3ds_internal(u32 *num, u32 shift)
{
u64 tmp[2];
u64 num_work[2];
aes_flip_to_64(num, num_work);
tmp[0] = num_work[0]<<shift;
tmp[1] = num_work[1]<<shift;
tmp[0] |= num_work[1]>>(64-shift);
tmp[1] |= num_work[0]>>(64-shift);
aes_unflip_to_32(tmp, num);
}
void n128_rrot_3ds_internal(u32 *num, u32 shift)
{
u64 tmp[2];
u64 num_work[2];
aes_flip_to_64(num, num_work);
tmp[0] = num_work[0]>>shift;
tmp[1] = num_work[1]>>shift;
tmp[0] |= (num_work[1]<<(64-shift));
tmp[1] |= (num_work[0]<<(64-shift));
aes_unflip_to_32(tmp, num);
}
void n128_lrot_3ds(u32 *num, u32 shift)
{
u32 shift_cycle;
while(shift > 0)
{
if(shift >= 32)
{
shift_cycle = 32;
shift -= 32;
}
else
{
shift_cycle = shift;
shift = 0;
}
n128_lrot_3ds_internal(num, shift_cycle);
}
}
void n128_rrot_3ds(u32 *num, u32 shift)
{
u32 shift_cycle;
while(shift > 0)
{
if(shift >= 32)
{
shift_cycle = 32;
shift -= 32;
}
else
{
shift_cycle = shift;
shift = 0;
}
n128_rrot_3ds_internal(num, shift_cycle);
}
}
void n128_add_3ds(u32 *a, u32 *b)
{
u64 a64[4];
u64 b64[4];
aes_flip_to_64(a, a64);
aes_flip_to_64(b, b64);
uint64_t tmp = (a64[0]>>1)+(b64[0]>>1) + (a64[0] & b64[0] & 1);
tmp = tmp >> 63;
a64[0] = a64[0] + b64[0];
a64[1] = a64[1] + b64[1] + tmp;
aes_unflip_to_32(a64, a);
}
void n128_lrot(uint64_t *num, uint32_t shift)
{
uint64_t tmp[2];
tmp[0] = num[0]<<shift;
tmp[1] = num[1]<<shift;
tmp[0] |= (num[1]>>(64-shift));
tmp[1] |= (num[0]>>(64-shift));
num[0] = tmp[0];
num[1] = tmp[1];
}
void n128_rrot(uint64_t *num, uint32_t shift)
{
uint64_t tmp[2];
tmp[0] = num[0]>>shift;
tmp[1] = num[1]>>shift;
tmp[0] |= (num[1]<<(64-shift));
tmp[1] |= (num[0]<<(64-shift));
num[0] = tmp[0];
num[1] = tmp[1];
}
void n128_add(uint64_t *a, uint64_t *b)
{
uint64_t *a64 = a;
uint64_t *b64 = b;
uint64_t tmp = (a64[0]>>1)+(b64[0]>>1) + (a64[0] & b64[0] & 1);
tmp = tmp >> 63;
a64[0] = a64[0] + b64[0];
a64[1] = a64[1] + b64[1] + tmp;
}
void n128_sub(uint64_t *a, uint64_t *b)
{
uint64_t *a64 = a;
uint64_t *b64 = b;
uint64_t tmp = (a64[0]>>1)-(b64[0]>>1) - ((a64[0]>>63) & (b64[0]>>63) & 1);
tmp = tmp >> 63;
a64[0] = a64[0] - b64[0];
a64[1] = a64[1] - b64[1] - tmp;
}
void F_XY(uint32_t *key, uint32_t *key_x, uint32_t *key_y)
{
int i;
unsigned char key_xy[16];
memset(key_xy, 0, 16);
memset(key, 0, 16);
for(i=0; i<16; i++)key_xy[i] = ((unsigned char*)key_x)[i] ^ ((unsigned char*)key_y)[i];
key[0] = 0x1a4f3e79;
key[1] = 0x2a680f5f;
key[2] = 0x29590258;
key[3] = 0xfffefb4e;
n128_add((uint64_t*)key, (uint64_t*)key_xy);
n128_lrot((uint64_t*)key, 42);
}
//F_XY_reverse does the reverse of F(X^Y): takes (normal)key, and does F in reverse to generate the original X^Y key_xy.
void F_XY_reverse(uint32_t *key, uint32_t *key_xy)
{
uint32_t tmpkey[4];
memset(key_xy, 0, 16);
memset(tmpkey, 0, 16);
memcpy(tmpkey, key, 16);
key_xy[0] = 0x1a4f3e79;
key_xy[1] = 0x2a680f5f;
key_xy[2] = 0x29590258;
key_xy[3] = 0xfffefb4e;
n128_rrot((uint64_t*)tmpkey, 42);
n128_sub((uint64_t*)tmpkey, (uint64_t*)key_xy);
memcpy(key_xy, tmpkey, 16);
}