-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathodocrypt.cpp
302 lines (270 loc) · 8.63 KB
/
odocrypt.cpp
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2018 The DigiByte developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "odocrypt.h"
#include <algorithm>
struct OdoRandom
{
// LCG parameters from Knuth
const static uint64_t BASE_MULTIPLICAND = 6364136223846793005ull;
const static uint64_t BASE_ADDEND = 1442695040888963407ull;
OdoRandom(uint32_t seed):
current(seed),
multiplicand(1),
addend(0)
{}
// For a standard LCG, every seed produces the same sequence, but from a different
// starting point. This generator gives the 1st, 3rd, 6th, 10th, etc output from
// a standard LCG. This ensures that every seed produces a unique sequence.
inline uint32_t NextInt()
{
addend += multiplicand * BASE_ADDEND;
multiplicand *= BASE_MULTIPLICAND;
current = current * multiplicand + addend;
return current >> 32;
}
inline uint64_t NextLong()
{
uint64_t hi = NextInt();
return (hi << 32) | NextInt();
}
inline int Next(int N)
{
return ((uint64_t)NextInt() * N) >> 32;
}
template<class T, size_t sz>
void Permutation(T (&arr)[sz])
{
for (size_t i = 0; i < sz; i++)
arr[i] = i;
for (int i = 1; i < sz; i++)
std::swap(arr[i], arr[Next(i+1)]);
}
uint64_t current;
uint64_t multiplicand;
uint64_t addend;
};
OdoCrypt::OdoCrypt(uint32_t key)
{
OdoRandom r(key);
// Randomize each s-box
for (int i = 0; i < SMALL_SBOX_COUNT; i++)
{
r.Permutation(Sbox1[i]);
}
for (int i = 0; i < LARGE_SBOX_COUNT; i++)
{
r.Permutation(Sbox2[i]);
}
// Randomize each p-box
for (int i = 0; i < 2; i++)
{
Pbox& perm = Permutation[i];
for (int j = 0; j < PBOX_SUBROUNDS; j++)
for (int k = 0; k < STATE_SIZE/2; k++)
perm.mask[j][k] = r.NextLong();
for (int j = 0; j < PBOX_SUBROUNDS-1; j++)
for (int k = 0; k < STATE_SIZE/2; k++)
perm.rotation[j][k] = r.Next(63) + 1;
}
// Randomize rotations
// Rotations must be distinct, non-zero, and have odd sum
{
int bits[WORD_BITS-1];
r.Permutation(bits);
int sum = 0;
for (int j = 0; j < ROTATION_COUNT-1; j++)
{
Rotations[j] = bits[j] + 1;
sum += Rotations[j];
}
for (int j = ROTATION_COUNT-1; ; j++)
{
if ((bits[j] + 1 + sum) % 2)
{
Rotations[ROTATION_COUNT-1] = bits[j] + 1;
break;
}
}
}
// Randomize each round key
for (int i = 0; i < ROUNDS; i++)
RoundKey[i] = r.Next(1 << STATE_SIZE);
}
void OdoCrypt::Encrypt(char cipher[DIGEST_SIZE], const char plain[DIGEST_SIZE]) const
{
uint64_t state[STATE_SIZE];
Unpack(state, plain);
PreMix(state);
for (int round = 0; round < ROUNDS; round++)
{
ApplyPbox(state, Permutation[0]);
ApplySboxes(state, Sbox1, Sbox2);
ApplyPbox(state, Permutation[1]);
ApplyRotations(state, Rotations);
ApplyRoundKey(state, RoundKey[round]);
}
Pack(state, cipher);
}
template<class T, size_t sz1, size_t sz2>
void InvertMapping(T (&res)[sz1][sz2], const T (&mapping)[sz1][sz2])
{
for (size_t i = 0; i < sz1; i++)
for (size_t j = 0; j < sz2; j++)
res[i][mapping[i][j]] = j;
}
void OdoCrypt::Decrypt(char plain[DIGEST_SIZE], const char cipher[DIGEST_SIZE]) const
{
uint8_t invSbox1[SMALL_SBOX_COUNT][1 << SMALL_SBOX_WIDTH];
uint16_t invSbox2[LARGE_SBOX_COUNT][1 << LARGE_SBOX_WIDTH];
InvertMapping(invSbox1, Sbox1);
InvertMapping(invSbox2, Sbox2);
uint64_t state[STATE_SIZE];
Unpack(state, cipher);
for (int round = ROUNDS-1; round >= 0; round--)
{
ApplyRoundKey(state, RoundKey[round]);
// LCM(STATE_SIZE, WORD_BITS)-1 is enough iterations, but this will do.
for (int i = 0; i < STATE_SIZE*WORD_BITS-1; i++)
ApplyRotations(state, Rotations);
ApplyInvPbox(state, Permutation[1]);
ApplySboxes(state, invSbox1, invSbox2);
ApplyInvPbox(state, Permutation[0]);
}
PreMix(state);
Pack(state, plain);
}
void OdoCrypt::Unpack(uint64_t state[STATE_SIZE], const char bytes[DIGEST_SIZE])
{
std::fill(state, state+STATE_SIZE, 0);
for (int i = 0; i < STATE_SIZE; i++)
{
for (int j = 0; j < 8; j++)
{
state[i] |= (uint64_t)(uint8_t)bytes[8*i + j] << (8*j);
}
}
}
void OdoCrypt::Pack(const uint64_t state[STATE_SIZE], char bytes[DIGEST_SIZE])
{
std::fill(bytes, bytes+DIGEST_SIZE, 0);
for (int i = 0; i < STATE_SIZE; i++)
{
for (int j = 0; j < 8; j++)
{
bytes[8*i + j] = (state[i] >> (8*j)) & 0xff;
}
}
}
void OdoCrypt::PreMix(uint64_t state[STATE_SIZE])
{
uint64_t total = 0;
for (int i = 0; i < STATE_SIZE; i++)
total ^= state[i];
total ^= total >> 32;
for (int i = 0; i < STATE_SIZE; i++)
state[i] ^= total;
}
void OdoCrypt::ApplySboxes(
uint64_t state[STATE_SIZE],
const uint8_t sbox1[SMALL_SBOX_COUNT][1 << SMALL_SBOX_WIDTH],
const uint16_t sbox2[LARGE_SBOX_COUNT][1 << LARGE_SBOX_WIDTH])
{
const static uint64_t MASK1 = (1 << SMALL_SBOX_WIDTH) - 1;
const static uint64_t MASK2 = (1 << LARGE_SBOX_WIDTH) - 1;
int smallSboxIndex = 0;
for (int i = 0; i < STATE_SIZE; i++)
{
uint64_t next = 0;
int pos = 0;
int largeSboxIndex = i;
for (int j = 0; j < SMALL_SBOX_COUNT / STATE_SIZE; j++)
{
next |= (uint64_t)sbox1[smallSboxIndex][(state[i] >> pos) & MASK1] << pos;
pos += SMALL_SBOX_WIDTH;
next |= (uint64_t)sbox2[largeSboxIndex][(state[i] >> pos) & MASK2] << pos;
pos += LARGE_SBOX_WIDTH;
smallSboxIndex++;
}
state[i] = next;
}
}
void OdoCrypt::ApplyMaskedSwaps(uint64_t state[STATE_SIZE], const uint64_t mask[STATE_SIZE/2])
{
for (int i = 0; i < STATE_SIZE/2; i++)
{
uint64_t& a = state[2*i];
uint64_t& b = state[2*i+1];
// For each bit set in the mask, swap the corresponding bits in `a` and `b`
uint64_t swp = mask[i] & (a ^ b);
a ^= swp;
b ^= swp;
}
}
void OdoCrypt::ApplyWordShuffle(uint64_t state[STATE_SIZE], int m)
{
uint64_t next[STATE_SIZE];
for (int i = 0; i < STATE_SIZE; i++)
{
next[m*i % STATE_SIZE] = state[i];
}
std::copy(next, next+STATE_SIZE, state);
}
inline uint64_t Rot(uint64_t x, int r)
{
return r == 0 ? x : (x << r) ^ (x >> (64-r));
}
void OdoCrypt::ApplyPboxRotations(uint64_t state[STATE_SIZE], const int rotation[STATE_SIZE/2])
{
for (int i = 0; i < STATE_SIZE/2; i++)
{
// Only rotate the even words. Rotating the odd words wouldn't actually
// be useful - a transformation that rotates all the words can be
// transformed into one that only rotates the even words, then rotates
// the odd words once after the final iteration.
state[2*i] = Rot(state[2*i], rotation[i]);
}
}
void OdoCrypt::ApplyPbox(uint64_t state[STATE_SIZE], const Pbox& perm)
{
for (int i = 0; i < PBOX_SUBROUNDS-1; i++)
{
// Conditionally move bits between adjacent pairs of words
ApplyMaskedSwaps(state, perm.mask[i]);
// Move the words around
ApplyWordShuffle(state, PBOX_M);
// Rotate the bits within words
ApplyPboxRotations(state, perm.rotation[i]);
}
ApplyMaskedSwaps(state, perm.mask[PBOX_SUBROUNDS-1]);
}
void OdoCrypt::ApplyInvPbox(uint64_t state[STATE_SIZE], const Pbox& perm)
{
ApplyMaskedSwaps(state, perm.mask[PBOX_SUBROUNDS-1]);
for (int i = PBOX_SUBROUNDS-2; i >= 0; i--)
{
int invRotation[STATE_SIZE/2];
for (int j = 0; j < STATE_SIZE/2; j++)
invRotation[j] = WORD_BITS - perm.rotation[i][j];
ApplyPboxRotations(state, invRotation);
ApplyWordShuffle(state, INV_PBOX_M);
ApplyMaskedSwaps(state, perm.mask[i]);
}
}
void OdoCrypt::ApplyRotations(uint64_t state[STATE_SIZE], const int rotations[ROTATION_COUNT])
{
uint64_t next[STATE_SIZE];
std::rotate_copy(state, state+1, state+STATE_SIZE, next);
for (int i = 0; i < STATE_SIZE; i++)
for (int j = 0; j < ROTATION_COUNT; j++)
{
next[i] ^= Rot(state[i], rotations[j]);
}
std::copy(next, next+STATE_SIZE, state);
}
void OdoCrypt::ApplyRoundKey(uint64_t state[STATE_SIZE], int roundKey)
{
for (int i = 0; i < STATE_SIZE; i++)
state[i] ^= (roundKey >> i) & 1;
}