-
Notifications
You must be signed in to change notification settings - Fork 3
/
speck.cpp
49 lines (37 loc) · 1.07 KB
/
speck.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
#include "common.h"
static inline word_t speckKS_RecoverOne(word_t A1, word_t A2) {
word_t B2 = ROTL(A1, BETA) ^ A2;
return B2;
}
vector<word_t> speckKS_Revert(const vector<word_t> &last_subkeys_rev, int nrounds) {
assert(last_subkeys_rev.size() == 4); // currently supported
vector<word_t> subkeys = last_subkeys_rev;
nrounds--;
word_t A1, A2, B, C, D;
A2 = subkeys[0];
A1 = subkeys[1];
D = speckKS_RecoverOne(A1, A2);
DR(D, A2, --nrounds);
assert(A2 == A1);
A1 = subkeys[2];
C = speckKS_RecoverOne(A1, A2);
DR(C, A2, --nrounds);
assert(A2 == A1);
A1 = subkeys[3];
B = speckKS_RecoverOne(A1, A2);
DR(B, A2, --nrounds);
assert(A2 == A1);
word_t A = A2;
while (nrounds >= 1) {
DR(D, A, --nrounds);
subkeys.push_back(A);
if (!nrounds) break;
DR(C, A, --nrounds);
subkeys.push_back(A);
if (!nrounds) break;
DR(B, A, --nrounds);
subkeys.push_back(A);
}
reverse(subkeys.begin(), subkeys.end());
return subkeys;
}