-
Notifications
You must be signed in to change notification settings - Fork 4
/
sodark.c
218 lines (196 loc) · 7.7 KB
/
sodark.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* sodark
Utility for encryption and decryption with the SoDark family of ciphers.
Copyright (C) 2017, 2020 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/>. */
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sodark.h"
void printusage(const char *name) {
fprintf(stderr, "Usage:\n");
fprintf(stderr, "%s mode rounds pt/ct key tweak\n", name);
fprintf(stderr, " -3d SoDark-3 decryption\n");
fprintf(stderr, " -3e SoDark-3 encryption\n");
fprintf(stderr, " -6d SoDark-6 decryption\n");
fprintf(stderr, " -6e SoDark-6 encryption\n");
fprintf(stderr, "%s -r {3|6} rounds key ntuples\n", name);
fprintf(stderr, " Generate ntuples random tuples with random tweaks.\n");
fprintf(stderr, "%s -r {3|6} rounds key ntuples tweak\n", name);
fprintf(stderr, " Generate ntuples random tuples with a specific tweak.\n");
fprintf(stderr, "%s -c key\n", name);
fprintf(stderr, " Generate chosen ciphertexts.\n");
}
int get_random(FILE *rand, uint64_t *ptr) {
if (fread(ptr, sizeof(uint64_t), 1, rand) != 1) {
fprintf(stderr, "Error when reading from /dev/urandom.\n");
fclose(rand);
return 1;
}
return 0;
}
int generate_chosen_ciphertexts(const char *progname, const char *keystr) {
uint64_t key = strtoull(keystr, NULL, 16);
key &= 0xffffffffffffffULL;
fprintf(stderr, "Key: %014" PRIx64 "\n", key);
FILE *randp = fopen("/dev/urandom", "r");
if (randp == NULL) {
fprintf(stderr, "Error when opening /dev/urandom.\n");
return 1;
}
uint64_t rand = 0;
while ((rand & 0xff) == 0) {
if (get_random(randp, &rand)) {
return 1;
}
}
uint8_t ds5 = rand & 0xff;
rand >>= 8;
uint8_t b7a = rand & 0xff;
rand >>= 8;
uint8_t b7b = b7a ^ ds5;
b7a = g_sbox_enc[b7a];
b7b = g_sbox_enc[b7b];
uint8_t db7 = b7a ^ b7b;
uint8_t a8a = rand & 0xff;
rand >>= 8;
uint8_t c8a = rand & 0xff;
rand >>= 8;
uint8_t a8b = a8a ^ db7;
uint8_t c8b = c8a ^ db7;
a8a = g_sbox_enc[a8a];
a8b = g_sbox_enc[a8b];
c8a = g_sbox_enc[c8a];
c8b = g_sbox_enc[c8b];
uint64_t tweak1 = 0;
if (get_random(randp, &tweak1)) {
return 1;
}
uint64_t tweak2 = tweak1 ^ (((uint64_t)ds5) << 24);
for (int k3 = 0; k3 < 0x100; k3++) {
uint8_t b8a = g_sbox_enc[b7a ^ a8a ^ c8a ^ k3 ^ (tweak1 & 0xff)];
uint8_t b8b = g_sbox_enc[b7b ^ a8b ^ c8b ^ k3 ^ (tweak2 & 0xff)];
uint32_t ct1 = (((uint32_t)a8a) << 16) | (((uint32_t)b8a) << 8) | ((uint32_t)c8a);
uint32_t ct2 = (((uint32_t)a8b) << 16) | (((uint32_t)b8b) << 8) | ((uint32_t)c8b);
uint32_t pt1 = decrypt_sodark_3(8, ct1, key, tweak1);
uint32_t pt2 = decrypt_sodark_3(8, ct2, key, tweak2);
printf("%06x %06x %016" PRIx64 " %06x %06x %016" PRIx64 " %02x\n",
pt1, ct1, tweak1, pt2, ct2, tweak2, k3);
}
return 0;
}
int generate_tuples(const char *progname, const char *versionstr, const char *roundstr,
const char *keystr, const char *ntuplestr, const char *tweakstr) {
uint32_t rounds = atoi(roundstr);
uint64_t key = strtoull(keystr, NULL, 16);
uint32_t tuples = atoi(ntuplestr);
uint64_t tweak = 0;
if (tweakstr != NULL) {
tweak = strtoull(tweakstr, NULL, 16);
}
FILE *randp = fopen("/dev/urandom", "r");
if (randp == NULL) {
fprintf(stderr, "Error when opening /dev/urandom.\n");
return 1;
}
int version = 0;
if (strcmp(versionstr, "3") == 0) {
version = 3;
} else if (strcmp(versionstr, "6") == 0) {
version = 6;
} else {
printusage(progname);
fclose(randp);
return 1;
}
fprintf(stderr, "Generating %d random tuples with SoDark-%d.\n", tuples, version);
fprintf(stderr, "Rounds: %d\n", rounds);
fprintf(stderr, "Key: %014" PRIx64 "\n", key);
if (tweakstr != NULL) {
fprintf(stderr, "Tweak: %016" PRIx64 "\n", tweak);
}
uint64_t pt;
for (int i = 0; i < tuples; i++) {
if (get_random(randp, &pt) || (tweakstr == NULL && get_random(randp, &tweak))) {
return 1;
}
if (version == 3) {
pt &= 0xffffff;
uint32_t ct = encrypt_sodark_3(rounds, (uint32_t)pt, key, tweak);
printf("%06x %06x %016" PRIx64 "\n", (uint32_t)pt, ct, tweak);
} else {
pt &= 0xffffffffffffL;
uint64_t ct = encrypt_sodark_6(rounds, pt, key, tweak);
printf("%012" PRIx64 " %012" PRIx64 " %016" PRIx64 "\n", pt, ct, tweak);
}
}
fclose(randp);
return 0;
}
int main(int argc, char **argv) {
create_sodark_dec_sbox();
assert(enc_one_round_3(0x54e0cd, 0xc2284a ^ 0x543bd8) == 0xd0721d);
assert(dec_one_round_3(0xd0721d, 0xc2284a ^ 0x543bd8) == 0x54e0cd);
assert(dec_one_round_3(dec_one_round_3(0xd0721d, 0xc2284a ^ 0x543bd8), 0) == 0x2ac222);
assert(dec_one_round_6(enc_one_round_6(0x1234567890ab, 0x6d7dddd48390), 0x6d7dddd48390)
== 0x1234567890ab);
assert(encrypt_sodark_3(3, 0x54e0cd, 0xc2284a1ce7be2f, 0x543bd88000017550) == 0x41db0c);
assert(encrypt_sodark_3(4, 0x54e0cd, 0xc2284a1ce7be2f, 0x543bd88000017550) == 0x987c6d);
assert(decrypt_sodark_3(1, 0xd0721d, 0xc2284a1ce7be2f, 0x543bd88000017550) == 0x54e0cd);
assert(decrypt_sodark_3(3, 0x41db0c, 0xc2284a1ce7be2f, 0x543bd88000017550) == 0x54e0cd);
assert(decrypt_sodark_3(4, 0x987c6d, 0xc2284a1ce7be2f, 0x543bd88000017550) == 0x54e0cd);
assert(decrypt_sodark_6(1, encrypt_sodark_6(1, 0xdeafcafebabe, 0xc2284a1ce7be2f,
0x543bd88000017550), 0xc2284a1ce7be2f, 0x543bd88000017550) == 0xdeafcafebabe);
if (argc == 3 && strcmp(argv[1], "-c") == 0) {
return generate_chosen_ciphertexts(argv[0], argv[2]);
} else if (argc == 6 && strcmp(argv[1], "-r") == 0) {
return generate_tuples(argv[0], argv[2], argv[3], argv[4], argv[5], NULL);
} else if (argc == 6) {
uint32_t rounds = atoi(argv[2]);
uint64_t pt_ct = strtoull(argv[3], NULL, 16);
uint64_t key = strtoull(argv[4], NULL, 16);
uint64_t tweak = strtoull(argv[5], NULL, 16);
if (rounds <= 0) {
fprintf(stderr, "Bad number of rounds.\n\n");
printusage(argv[0]);
return 1;
}
printf("Rounds: %d\n", rounds);
if (strcmp(argv[1], "-3d") == 0 || strcmp(argv[1], "-3e") == 0) {
printf("PT/CT: %06" PRIx64 "\n", pt_ct);
} else {
printf("PT/CT: %012" PRIx64 "\n", pt_ct);
}
printf("Key: %014" PRIx64 "\n", key);
printf("Tweak: %016" PRIx64 "\n", tweak);
if (strcmp(argv[1], "-3d") == 0) {
printf("PT: %06" PRIx32 "\n", decrypt_sodark_3(rounds, (uint32_t)pt_ct, key, tweak));
} else if (strcmp(argv[1], "-3e") == 0) {
printf("CT: %06" PRIx32 "\n", encrypt_sodark_3(rounds, (uint32_t)pt_ct, key, tweak));
} else if (strcmp(argv[1], "-6d") == 0) {
printf("PT: %012" PRIx64 "\n", decrypt_sodark_6(rounds, pt_ct, key, tweak));
} else if (strcmp(argv[1], "-6e") == 0) {
printf("CT: %012" PRIx64 "\n", encrypt_sodark_6(rounds, pt_ct, key, tweak));
} else {
fprintf(stderr, "Bad mode.\n\n");
printusage(argv[0]);
return 1;
}
} else if (argc == 7 && strcmp(argv[1], "-r") == 0) {
return generate_tuples(argv[0], argv[2], argv[3], argv[4], argv[5], argv[6]);
} else {
printusage(argv[0]);
return 1;
}
return 0;
}