forked from ctz/cryptopals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util-1.h
executable file
·375 lines (315 loc) · 7.28 KB
/
util-1.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#ifndef UTIL_1_H
#define UTIL_1_H
#include <stdint.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <time.h>
typedef struct block
{
size_t ulen;
struct block *next;
uint8_t u[1];
} block;
typedef struct pool
{
block *blocks;
void * (*alloc)(struct pool *p, size_t nbytes);
void (*finish)(struct pool *p);
} pool;
void * pool_alloc(pool *p, size_t nbytes)
{
size_t sz = nbytes + sizeof(block);
assert(sz > nbytes);
block *b = malloc(sz);
if (!b)
{
perror("allocation failed");
abort();
}
memset(b, 0, sz);
b->ulen = nbytes;
b->next = p->blocks;
p->blocks = b;
return b->u;
}
void pool_report(const pool *p)
{
size_t nblocks = 0;
size_t nbytes = 0;
for (block *b = p->blocks;
b != NULL;
b = b->next)
{
nblocks++;
nbytes += b->ulen;
}
printf("pool has %zu bytes in %zu blocks\n", nbytes, nblocks);
}
void pool_finish(pool *p)
{
for (block *b = p->blocks;
b != NULL;
)
{
block *next = b->next;
free(b);
b = next;
}
memset(p, 0, sizeof(*p));
}
pool pool_create(void)
{
pool r = { NULL, pool_alloc, pool_finish };
return r;
}
typedef struct
{
uint8_t *buf;
size_t len;
} byteblock;
// return x ^ y. x may be longer than y, in which case y wraps.
byteblock byteblock_xor(pool *p, const byteblock *x, const byteblock *y)
{
assert(x->len >= y->len);
byteblock out = { p->alloc(p, x->len), x->len };
for (size_t i = 0; i < x->len; i++)
out.buf[i] = x->buf[i] ^ y->buf[i % y->len];
return out;
}
int byte_hamming(uint8_t x, uint8_t y)
{
#define BIT(n) ( (x & (1 << n)) == (y & (1 << n)) ? 0 : 1 )
return BIT(7) + BIT(6) + BIT(5) + BIT(4) + BIT(3) + BIT(2) + BIT(1) + BIT(0);
#undef BIT
}
int byteblock_hamming(const byteblock *x, const byteblock *y)
{
assert(x->len == y->len);
int r = 0;
for (size_t i = 0; i < x->len; i++)
r += byte_hamming(x->buf[i], y->buf[i]);
return r;
}
int byteblock_eq(const byteblock *x, const byteblock *y)
{
if (x->len != y->len)
return 0;
for (size_t i = 0; i < x->len; i++)
if (x->buf[i] != y->buf[i])
return 0;
return 1;
}
byteblock byteblock_concat(pool *p, const byteblock *x, const byteblock *y)
{
size_t len = x->len + y->len;
byteblock out = { p->alloc(p, len), len };
memcpy(out.buf, x->buf, x->len);
memcpy(out.buf + x->len, y->buf, y->len);
return out;
}
void random_init(void)
{
srand((int) time(NULL));
}
void random_fill(uint8_t *out, size_t len)
{
for (size_t i = 0; i < len; i++)
out[i] = rand() & 0xff;
}
byteblock byteblock_random(pool *p, size_t len)
{
byteblock out = { p->alloc(p, len), len };
random_fill(out.buf, out.len);
return out;
}
const char *hex_table = "0123456789abcdef";
const char *b64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char b64_pad = '=';
uint8_t hex_nibble(int v)
{
v = tolower(v);
if (v >= '0' && v <= '9')
return v - '0';
if (v >= 'a' && v <= 'f')
return 10 + v - 'a';
else {
assert(!"invalid hex");
abort();
}
}
byteblock from_hex(pool *p, const char *hex)
{
size_t hlen = strlen(hex);
assert(hlen % 2 == 0);
size_t rlen = hlen / 2;
byteblock out = { p->alloc(p, rlen), rlen };
for (size_t i = 0; i < out.len; i++)
{
uint8_t v = hex_nibble(hex[i * 2]) << 4 |
hex_nibble(hex[i * 2 + 1]);
out.buf[i] = v;
}
return out;
}
const char *to_hex(pool *p, const byteblock *bytes)
{
size_t hlen = bytes->len * 2 + 1;
char *h = p->alloc(p, hlen);
for (size_t i = 0; i < bytes->len; i++)
{
uint8_t b = bytes->buf[i];
h[i * 2] = hex_table[b >> 4 & 0xf];
h[i * 2 + 1] = hex_table[b & 0xf];
}
return h;
}
const char *to_ascii(pool *p, const byteblock *bytes)
{
size_t alen = bytes->len + 1;
char *h = p->alloc(p, alen);
for (size_t i = 0; i < bytes->len; i++)
{
if (isprint(bytes->buf[i]))
h[i] = bytes->buf[i];
else
h[i] = '.';
}
return h;
}
void b64_encode_triple(uint8_t buf[3], char out[4])
{
out[0] = b64_table[buf[0] >> 2];
out[1] = b64_table[(buf[0] & 0x3) << 4 | (buf[1] >> 4)];
out[2] = b64_table[(buf[1] & 0xf) << 2 | (buf[2] >> 6)];
out[3] = b64_table[buf[2] & 0x3f];
}
size_t min(size_t x, size_t y)
{
return x > y ? y : x;
}
const char *to_base64(pool *p, const byteblock *bytes)
{
size_t triples = (bytes->len + 2) / 3;
size_t blen = triples * 4 + 1;
char *b = p->alloc(p, blen);
for (size_t i = 0; i < triples; i++)
{
size_t offs = i * 3;
size_t copy = min(3, bytes->len - offs);
uint8_t buf[3] = { 0 };
switch (copy)
{
case 3:
buf[2] = bytes->buf[offs + 2];
/* fallthru */
case 2:
buf[1] = bytes->buf[offs + 1];
/* fallthru */
case 1:
buf[0] = bytes->buf[offs + 0];
}
b64_encode_triple(buf, &b[i * 4]);
/* pad if we don't have a full block */
switch (copy)
{
case 1:
b[i * 4 + 2] = b64_pad;
/* fallthru */
case 2:
b[i * 4 + 3] = b64_pad;
}
}
return b;
}
char next_b64(const char **p)
{
while (1)
{
char c = **p;
if (c)
*p = *p + 1;
switch (c)
{
case '\n':
case '\r':
case '\t':
case ' ':
continue;
case 0:
return 0;
default:
return c;
}
}
}
uint8_t b64_decode_char(char x)
{
if (x == b64_pad)
return 0;
const char *offs = strchr(b64_table, x);
assert(offs != NULL);
return offs - b64_table;
}
void b64_decode_triple(char a, char b, char c, char d, uint8_t out[3])
{
uint8_t xa = b64_decode_char(a);
uint8_t xb = b64_decode_char(b);
uint8_t xc = b64_decode_char(c);
uint8_t xd = b64_decode_char(d);
out[0] = (xa << 2) | (xb >> 4);
out[1] = ((xb & 0xf) << 4) | (xc >> 2);
out[2] = ((xc & 0x3) << 6) | xd;
}
byteblock from_base64(pool *p, const char *b64)
{
size_t b64len = strlen(b64);
size_t quads = (b64len + 3) / 4; // probably over estimate
byteblock out = { p->alloc(p, quads * 3), 0 };
for (size_t q = 0; q < quads; q++)
{
char a = next_b64(&b64);
char b = next_b64(&b64);
char c = next_b64(&b64);
char d = next_b64(&b64);
if (a == 0 && b == 0 && c == 0 && d == 0)
break; // eof
b64_decode_triple(a, b, c, d, &out.buf[q * 3]);
out.len += 3;
if (d == b64_pad)
out.len -= 1;
if (c == b64_pad)
out.len -= 1;
}
return out;
}
// common ascii characters, in order of descending frequency(ish)
const char *english_letter_scores = "etaonrishd .,\nlfcmugypwbvkjxqz-_!?'\"/1234567890*";
const int weird_punishment = 255;
int score_english_char(uint8_t c)
{
int uppercase_punishment = 0;
if (isupper(c))
{
// slightly punish uppercase letters
c = tolower(c);
uppercase_punishment = 3;
}
const char *where = strchr(english_letter_scores, c);
if (where == NULL)
return weird_punishment; // probably control or weird character: punish
else
return uppercase_punishment + (where - english_letter_scores) * 2;
}
// low scores reflect byteblocks containing probably-english ASCII text
int score_english(const byteblock *bytes)
{
int r = 0;
for (size_t i = 0; i < bytes->len; i++)
r += score_english_char(bytes->buf[i]);
return r;
}
#endif