-
Notifications
You must be signed in to change notification settings - Fork 4
/
sodark.h
227 lines (208 loc) · 9.32 KB
/
sodark.h
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
/* sodark.h
Copyright (C) 2016-2017 Marcus Dansarie <marcus@dansarie.se>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef _SODARK_H_
#define _SODARK_H_
#include <inttypes.h>
#include <stdbool.h>
/* Lookup tables for the SoDark s-box. */
uint32_t g_sbox_dec[256];
const uint32_t g_sbox_enc[] = {0x9c, 0xf2, 0x14, 0xc1, 0x8e, 0xcb, 0xb2, 0x65,
0x97, 0x7a, 0x60, 0x17, 0x92, 0xf9, 0x78, 0x41,
0x07, 0x4c, 0x67, 0x6d, 0x66, 0x4a, 0x30, 0x7d,
0x53, 0x9d, 0xb5, 0xbc, 0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, 0xf7, 0xfa, 0x52, 0x7f,
0xff, 0x32, 0x45, 0x20, 0x79, 0xce, 0xea, 0xbe,
0xcd, 0x15, 0x21, 0x23, 0xd8, 0xb6, 0x0c, 0x3f,
0x54, 0x1a, 0xbf, 0x98, 0x48, 0x3a, 0x75, 0x77,
0x2b, 0xae, 0x36, 0xda, 0x7e, 0x86, 0x35, 0x51,
0x05, 0x12, 0xb8, 0xa6, 0x9a, 0x2c, 0x06, 0x4b};
/* Initializes the SoDark decryption S-box. */
static inline void create_sodark_dec_sbox() {
/* Fill lookup table for the inverse s-box. */
for (uint16_t i = 0; i < 256; i++) {
g_sbox_dec[g_sbox_enc[i]] = i;
}
}
/* Do one round of encryption with the SoDark-3 algorithm.
pt Plaintext (24 bits).
rkey Round key, i.e. three key bytes xored with three bytes of tweak. */
static inline uint32_t enc_one_round_3(uint32_t pt, uint32_t rkey) {
uint32_t pa = pt >> 16;
uint32_t pb = (pt >> 8) & 0xff;
uint32_t pc = pt & 0xff;
uint32_t ka = rkey >> 16;
uint32_t kc = (rkey >> 8) & 0xff;
uint32_t kb = rkey & 0xff;
uint32_t ca = g_sbox_enc[pa ^ pb ^ ka];
uint32_t cc = g_sbox_enc[pc ^ pb ^ kc];
uint32_t cb = g_sbox_enc[ca ^ pb ^ cc ^ kb];
return (ca << 16) | (cb << 8) | cc;
}
/* Do one round of decryption with the SoDark-3 algorithm.
ct Ciphertext (24 bits).
rkey Round key, i.e. three key bytes xored with three bytes of tweak. */
static inline uint32_t dec_one_round_3(uint32_t ct, uint32_t rkey) {
uint32_t ca = ct >> 16;
uint32_t cb = (ct >> 8) & 0xff;
uint32_t cc = ct & 0xff;
uint32_t ka = rkey >> 16;
uint32_t kc = (rkey >> 8) & 0xff;
uint32_t kb = rkey & 0xff;
uint32_t pb = g_sbox_dec[cb] ^ ca ^ cc ^ kb;
uint32_t pc = g_sbox_dec[cc] ^ pb ^ kc;
uint32_t pa = g_sbox_dec[ca] ^ pb ^ ka;
return (pa << 16) | (pb << 8) | pc;
}
/* Do one round of encryption with the SoDark-6 algorithm.
pt Plaintext (48 bits).
rkey Round key, i.e. six key bytes xored with six bytes of tweak. */
static inline uint64_t enc_one_round_6(uint64_t pt, uint64_t rkey) {
uint64_t pa = pt >> 40;
uint64_t pb = (pt >> 32) & 0xff;
uint64_t pc = (pt >> 24) & 0xff;
uint64_t pd = (pt >> 16) & 0xff;
uint64_t pe = (pt >> 8) & 0xff;
uint64_t pf = pt & 0xff;
uint64_t ka = rkey >> 40;
uint64_t kc = (rkey >> 32) & 0xff;
uint64_t ke = (rkey >> 24) & 0xff;
uint64_t kb = (rkey >> 16) & 0xff;
uint64_t kd = (rkey >> 8) & 0xff;
uint64_t kf = rkey & 0xff;
uint64_t ca = g_sbox_enc[pa ^ pb ^ pf ^ ka];
uint64_t cc = g_sbox_enc[pb ^ pc ^ pd ^ kc];
uint64_t ce = g_sbox_enc[pd ^ pe ^ pf ^ ke];
uint64_t cb = g_sbox_enc[ca ^ pb ^ cc ^ kb];
uint64_t cd = g_sbox_enc[cc ^ pd ^ ce ^ kd];
uint64_t cf = g_sbox_enc[ca ^ pf ^ ce ^ kf];
return (ca << 40) | (cb << 32) | (cc << 24) | (cd << 16) | (ce << 8) | cf;
}
/* Do one round of decryption with the SoDark-6 algorithm.
ct Ciphertext (48 bits).
rkey Round key, i.e. six key bytes xored with six bytes of tweak. */
static inline uint64_t dec_one_round_6(uint64_t ct, uint64_t rkey) {
uint64_t ca = ct >> 40;
uint64_t cb = (ct >> 32) & 0xff;
uint64_t cc = (ct >> 24) & 0xff;
uint64_t cd = (ct >> 16) & 0xff;
uint64_t ce = (ct >> 8) & 0xff;
uint64_t cf = ct & 0xff;
uint64_t ka = rkey >> 40;
uint64_t kc = (rkey >> 32) & 0xff;
uint64_t ke = (rkey >> 24) & 0xff;
uint64_t kb = (rkey >> 16) & 0xff;
uint64_t kd = (rkey >> 8) & 0xff;
uint64_t kf = rkey & 0xff;
uint64_t pb = g_sbox_dec[cb] ^ ca ^ cc ^ kb;
uint64_t pd = g_sbox_dec[cd] ^ cc ^ ce ^ kd;
uint64_t pf = g_sbox_dec[cf] ^ ca ^ ce ^ kf;
uint64_t pa = g_sbox_dec[ca] ^ pb ^ pf ^ ka;
uint64_t pc = g_sbox_dec[cc] ^ pb ^ pd ^ kc;
uint64_t pe = g_sbox_dec[ce] ^ pd ^ pf ^ ke;
return (pa << 40) | (pb << 32) | (pc << 24) | (pd << 16) | (pe << 8) | pf;
}
/* Encrypt using the the Sodark-3 algorithm.
rounds Number of rounds.
pt Plaintext (24 bits).
key Encryption key (56 bits).
tweak Tweak (64 bits). */
static inline uint32_t encrypt_sodark_3(uint32_t rounds, uint32_t pt, uint64_t key,
uint64_t tweak) {
uint32_t ct = pt;
for (uint32_t round = 0; round < rounds; round++) {
uint32_t rkey = ((key >> 32) ^ (tweak >> 40)) & 0xffffff;
tweak = (tweak << 24) | (tweak >> 40);
key = ((key << 24) | (key >> 32)) & 0xffffffffffffffL;
ct = enc_one_round_3(ct, rkey);
}
return ct;
}
/* Decrypt using the the Sodark-3 algorithm.
rounds Number of rounds.
ct Ciphertext (24 bits).
key Encryption key (56 bits).
tweak Tweak (64 bits). */
static inline uint32_t decrypt_sodark_3(uint32_t rounds, uint32_t ct, uint64_t key,
uint64_t tweak) {
uint32_t tshift = (24 * (rounds - 1)) % 64;
uint32_t kshift = (24 * (rounds - 1)) % 56;
tweak = (tweak >> (64 - tshift)) | (tweak << tshift);
key = ((key >> (56 - kshift)) | (key << kshift)) & 0xffffffffffffffL;
uint32_t pt = ct;
for (uint32_t round = 0; round < rounds; round++) {
uint32_t rkey = ((key >> 32) ^ (tweak >> 40)) & 0xffffff;
tweak = (tweak >> 24) | (tweak << 40);
key = ((key >> 24) | (key << 32)) & 0xffffffffffffffL;
pt = dec_one_round_3(pt, rkey);
}
return pt;
}
/* Encrypt using the the Sodark-6 algorithm.
rounds Number of rounds.
pt Plaintext (48 bits).
key Encryption key (56 bits).
tweak Tweak (64 bits). */
static inline uint64_t encrypt_sodark_6(uint32_t rounds, uint64_t pt, uint64_t key,
uint64_t tweak) {
uint64_t ct = pt;
for (uint32_t round = 0; round < rounds; round++) {
uint64_t rkey = ((key >> 8) ^ (tweak >> 16)) % 0xffffffffffffL;
tweak = (tweak << 48) | (tweak >> 16);
key = ((key << 48) | (key >> 8)) & 0xffffffffffffffL;
ct = enc_one_round_6(ct, rkey);
}
return ct;
}
/* Decrypt using the the Sodark-6 algorithm.
rounds Number of rounds.
ct Ciphertext (48 bits).
key Encryption key (56 bits).
tweak Tweak (64 bits). */
static inline uint64_t decrypt_sodark_6(uint32_t rounds, uint64_t ct, uint64_t key,
uint64_t tweak) {
uint32_t tshift = (48 * (rounds - 1)) % 64;
uint32_t kshift = (48 * (rounds - 1)) % 56;
tweak = (tweak >> (64 - tshift)) | (tweak << tshift);
key = ((key >> (56 - kshift)) | (key << kshift)) & 0xffffffffffffffL;
uint64_t pt = ct;
for (uint32_t round = 0; round < rounds; round++) {
uint64_t rkey = ((key >> 8) ^ (tweak >> 16)) & 0xffffffffffffL;
tweak = (tweak >> 48) | (tweak << 16);
key = ((key >> 48) | (key << 8)) & 0xffffffffffffffL;
pt = dec_one_round_6(pt, rkey);
}
return pt;
}
#endif /* _SODARK_H_ */