-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathduplex.h
More file actions
240 lines (202 loc) · 6.68 KB
/
duplex.h
File metadata and controls
240 lines (202 loc) · 6.68 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* duplex.h from Pocketcrypt: https://github.com/arachsys/pocketcrypt */
#ifndef DUPLEX_H
#define DUPLEX_H
#include <stddef.h>
#include <stdint.h>
#if defined __clang_major__ && __clang_major__ >= 4
#define duplex_swap(x, ...) __builtin_shufflevector(x, x, __VA_ARGS__)
#elif defined __GNUC__ && __GNUC__ >= 5
#define duplex_swap(x, ...) __builtin_shuffle(x, (typeof(x)) { __VA_ARGS__ })
#else
#error Vector extensions require clang >= 4.0.0 or gcc >= 5.1.0
#endif
#if defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define duplex_byte(state, i) ((uint8_t *) state)[i]
#define duplex_r24 1, 2, 3, 0, 5, 6, 7, 4, 9, 10, 11, 8, 13, 14, 15, 12
#define duplex_rho 11, 8, 9, 10, 15, 12, 13, 14, 3, 0, 1, 2, 7, 4, 5, 6
#elif defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define duplex_byte(state, i) ((uint8_t *) state)[(i) ^ ((i) < 48 ? 3 : 7)]
#define duplex_r24 3, 0, 1, 2, 7, 4, 5, 6, 11, 8, 9, 10, 15, 12, 13, 14
#define duplex_rho 9, 10, 11, 8, 13, 14, 15, 12, 1, 2, 3, 0, 5, 6, 7, 4
#else
#error Byte order could not be determined
#endif
#define duplex_counter(state) ((uint64_t *) state)[6]
#define duplex_extra(state) ((uint64_t *) state)[7]
#ifndef duplex_permute
#define duplex_permute duplex_xoodoo
#endif
typedef uint8_t uint8x16_t __attribute__((vector_size(16)));
typedef uint32_t uint32x4_t __attribute__((vector_size(16)));
typedef uint32x4_t duplex_t[4];
enum {
duplex_rate = sizeof(uint32x4_t),
duplex_size = sizeof(duplex_t)
};
static inline void duplex_gimli(uint32x4_t state[3]) {
for (int round = 24; round > 0; round--) {
uint32x4_t x = (uint32x4_t) duplex_swap((uint8x16_t) state[0], duplex_r24);
uint32x4_t y = state[1] << 9 | state[1] >> 23;
uint32x4_t z = state[2];
state[2] = x ^ (z << 1) ^ ((y & z) << 2);
state[1] = y ^ x ^ ((x | z) << 1);
state[0] = z ^ y ^ ((x & y) << 3);
switch (round & 3) {
case 0:
state[0] = duplex_swap(state[0], 1, 0, 3, 2);
state[0] ^= (uint32x4_t) { 0x9e377900 | round, 0, 0, 0 };
break;
case 2:
state[0] = duplex_swap(state[0], 2, 3, 0, 1);
break;
}
}
}
static inline void duplex_xoodoo(uint32x4_t state[3]) {
const uint32_t rk[12] = {
0x058, 0x038, 0x3c0, 0x0d0, 0x120, 0x014,
0x060, 0x02c, 0x380, 0x0f0, 0x1a0, 0x012
};
for (int round = 0; round < 12; round++) {
uint32x4_t p = duplex_swap(state[0] ^ state[1] ^ state[2], 3, 0, 1, 2);
uint32x4_t e = (p << 5 | p >> 27) ^ (p << 14 | p >> 18);
state[0] ^= e, state[1] ^= e, state[2] ^= e;
uint32x4_t x = state[0] ^ (uint32x4_t) { rk[round], 0, 0, 0 };
uint32x4_t y = duplex_swap(state[1], 3, 0, 1, 2);
uint32x4_t z = state[2] << 11 | state[2] >> 21;
state[0] = x ^ (~y & z);
state[1] = y ^ (~z & x);
state[2] = z ^ (~x & y);
state[1] = state[1] << 1 | state[1] >> 31;
state[2] = (uint32x4_t) duplex_swap((uint8x16_t) state[2], duplex_rho);
}
}
static inline uint32x4_t duplex_get(const uint8_t in[16]) {
uint32x4_t out;
for (int i = 0; i < 16; i++)
duplex_byte(&out, i) = in[i];
return out;
}
static inline void duplex_put(uint8_t out[16], uint32x4_t in) {
for (int i = 0; i < 16; i++)
out[i] = duplex_byte(&in, i);
}
static inline void duplex_absorb(duplex_t state, const void *data,
size_t length) {
const uint8_t *bytes = data;
uint8_t offset = duplex_counter(state) & 15;
duplex_counter(state) += length;
while (1) {
if (length < 16 || offset > 0) {
for (int i = offset; i < 16; i++, length--) {
if (length == 0)
return;
duplex_byte(state, i) ^= bytes[i - offset];
}
bytes += 16 - offset, offset = 0;
duplex_permute(state);
}
while (length >= 16) {
state[0] ^= duplex_get(bytes);
bytes += 16, length -= 16;
duplex_permute(state);
}
}
}
static inline int duplex_compare(const void *a, const void *b,
size_t length) {
const uint8_t *as = a, *bs = b;
uint8_t result = 0;
for (size_t i = 0; i < length; i++)
result |= (a ? as[i] : 0) ^ (b ? bs[i] : 0);
return result ? -1 : 0;
}
static inline void duplex_decrypt(duplex_t state, void *data,
size_t length) {
uint8_t *bytes = data, offset = duplex_counter(state) & 15;
duplex_counter(state) += length;
while (1) {
if (length < 16 || offset > 0) {
for (int i = offset; i < 16; i++, length--) {
if (length == 0)
return;
bytes[i - offset] ^= duplex_byte(state, i);
duplex_byte(state, i) ^= bytes[i - offset];
}
bytes += 16 - offset, offset = 0;
duplex_permute(state);
}
while (length >= 16) {
uint32x4_t words = duplex_get(bytes);
words ^= state[0], state[0] ^= words;
duplex_put(bytes, words);
bytes += 16, length -= 16;
duplex_permute(state);
}
}
}
static inline void duplex_encrypt(duplex_t state, void *data,
size_t length) {
uint8_t *bytes = data, offset = duplex_counter(state) & 15;
duplex_counter(state) += length;
while (1) {
if (length < 16 || offset > 0) {
for (int i = offset; i < 16; i++, length--) {
if (length == 0)
return;
duplex_byte(state, i) ^= bytes[i - offset];
bytes[i - offset] = duplex_byte(state, i);
}
bytes += 16 - offset, offset = 0;
duplex_permute(state);
}
while (length >= 16) {
state[0] ^= duplex_get(bytes);
duplex_put(bytes, state[0]);
bytes += 16, length -= 16;
duplex_permute(state);
}
}
}
static inline void duplex_pad(duplex_t state) {
uint8_t offset = duplex_counter(state) & 15;
duplex_counter(state) += 16 - offset;
duplex_byte(state, offset) ^= 1;
duplex_byte(state, 47) ^= 1;
duplex_permute(state);
}
static inline void duplex_ratchet(duplex_t state) {
uint8_t offset = duplex_counter(state) & 15;
duplex_counter(state) += 16;
for (int i = offset; i < 16; i++)
duplex_byte(state, i) = 0;
duplex_permute(state);
for (int i = 0; i < offset; i++)
duplex_byte(state, i) = 0;
}
static inline void duplex_squeeze(duplex_t state, void *data,
size_t length) {
uint8_t *bytes = data, offset = duplex_counter(state) & 15;
duplex_counter(state) += length;
while (1) {
if (length < 16 || offset > 0) {
for (int i = offset; i < 16; i++, length--) {
if (length == 0)
return;
bytes[i - offset] = duplex_byte(state, i);
}
bytes += 16 - offset, offset = 0;
duplex_permute(state);
}
while (length >= 16) {
duplex_put(bytes, state[0]);
bytes += 16, length -= 16;
duplex_permute(state);
}
}
}
static inline void duplex_zero(void *data, size_t length) {
__builtin_memset(data, 0, length);
__asm__ __volatile__ ("" :: "r" (data) : "memory");
}
#endif