-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitives.c
474 lines (439 loc) · 12.5 KB
/
primitives.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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include "primitives.h"
/*
* Parameters: the word length w in bits;
* the number of rounds r;
* the key size b in (8-bit) bytes.
* Derived parameters: u = w/8 is the word length in bytes;
* c = ceil(b/u) is the length of the array L, the key
* reorganized from bytes to words (with trailing zeros if
* necessary);
* t = 2*(r+1) is the length of the array S, the expanded
* key table.
* Array labels: K is the byte-array containing the key;
* L is the equivalent word array;
* S is the expanded key table both before and after key-mixing.
* A and B are temporary words.
* This program performs an rc5 encryption on a file using a key specified by
* the user. These are both passed to the program as command line arguments.
* The above parameters can be set via flags, with default values w = 32,
* r = 16 and b = 10.
*/
uint16_t rotl16(uint16_t val, unsigned int rot)
{
const unsigned int mask = 15;
rot &= mask;
return (val << rot | val >> ( (-rot) & mask));
}
uint16_t rotr16(uint16_t val, unsigned int rot)
{
const unsigned int mask = 15;
rot &= mask;
return (val >> rot | val << ( (-rot) & mask));
}
uint32_t rotl32(uint32_t val, unsigned int rot)
{
const unsigned int mask = 31;
rot &= mask;
return (val << rot | val >> ( (-rot) & mask));
}
uint32_t rotr32(uint32_t val, unsigned int rot)
{
const unsigned int mask = 31;
rot &= mask;
return (val >> rot | val << ( (-rot) & mask));
}
uint64_t rotl64(uint64_t val, unsigned int rot)
{
const unsigned int mask = 63;
rot &= mask;
return (val << rot | val >> ( (-rot) & mask));
}
uint64_t rotr64(uint64_t val, unsigned int rot)
{
const unsigned int mask = 63;
rot &= mask;
return (val >> rot | val << ( (-rot) & mask));
}
data16 key_expand16(bdata *raw_key, size_t r)
{
size_t b = raw_key->blen;
data16 ret;
ret.IV = NULL;
ret.pad = 0;
//Copy raw_key->bbuf[0],...,raw_key->bbuf[b-1] into L[0],...,L[ceil((b-1)/2)]
float temp = ((float) b)/2.0;
size_t c = ceil(temp);
uint8_t *tempbuf;
if (b % 2 == 1) {
tempbuf = realloc(raw_key->bbuf, b + 1);
if (tempbuf == NULL) {
ret.len = 0;
fprintf(stderr, "Unable to increase memory size of key to multiple of wordsize in key expand subroutine. Terminating.\n");
return ret;
}
raw_key->bbuf = tempbuf;
raw_key->bbuf[b] = 0;
}
size_t t = 2*(r + 1);
uint16_t *L;
L = malloc(sizeof(uint16_t)*c);
size_t i;
for ( i = 0; i < c; i++) {
*(L + i) = ((0xffff & (uint16_t) raw_key->bbuf[2*i]) << 8) | (uint16_t)
raw_key->bbuf[2*i+1];
}
/* Fill S[0],...,S[2*r+1] with pseudo-random bits generated by P16 and
* Q16
*/
uint16_t *S;
S = malloc(sizeof(uint16_t)*t);
//Free in main routine
*S = P16;
for ( i = 1; i < t; i++) {
*(S + i) = (*(S + (i - 1))) + Q16;
}
//Mix the secret key L in with S using three passes of length max(c,t)
size_t m = 0, n = 0;
uint16_t A = 0, B = 0;
size_t k = fmax(c,t);
for (i = 0; i < 3*k; i++) {
A = *(S + m) = rotl16(((*(S + m)) + A + B),3);
B = *(L + n) = rotl16(((*(L + n)) + A + B),(A + B));
m = (m + 1) % t;
n = (n + 1) % c;
}
for (i = 0; i < c; i++)
*(L + i) = 0;
//zero first to avoid potentially leaking secret
free(L);
ret.len = t;
ret.text = S;
return ret;
}
data32 key_expand32(bdata *raw_key, size_t r)
{
size_t b = raw_key->blen;
data32 ret;
ret.IV = NULL;
ret.pad = 0;
//Copy raw_key->bbuf[0],...,raw_key->bbuf[b-1] into L[0],...,L[ceil((b-1)/4)]
float temp = ((float) b)/4.0;
size_t c = ceil(temp);
uint8_t *tempbuf;
if (b % 4 == 3) {
tempbuf = realloc(raw_key->bbuf, b + 1);
if (tempbuf == NULL) {
ret.len = 0;
fprintf(stderr, "Unable to increase memory size of key to multiple of wordsize in key expand subroutine. Terminating.\n");
return ret;
}
raw_key->bbuf = tempbuf;
raw_key->bbuf[b] = 0;
}
if (b % 4 == 2) {
tempbuf = realloc(raw_key->bbuf, b + 2);
if (tempbuf == NULL) {
ret.len = 0;
fprintf(stderr, "Unable to increase memory size of key to multiple of wordsize in key expand subroutine. Terminating.\n");
return ret;
}
raw_key->bbuf = tempbuf;
raw_key->bbuf[b] = 0;
raw_key->bbuf[b+1] = 0;
}
if (b % 4 == 1) {
tempbuf = realloc(raw_key->bbuf, b + 3);
if (tempbuf == NULL) {
ret.len = 0;
fprintf(stderr, "Unable to increase memory size of key to multiple of wordsize in key expand subroutine. Terminating.\n");
return ret;
}
raw_key->bbuf = tempbuf;
raw_key->bbuf[b] = 0;
raw_key->bbuf[b+1] = 0;
raw_key->bbuf[b+2] = 0;
}
size_t t = 2*(r + 1);
uint32_t *L;
L = malloc(sizeof(uint32_t)*c);
size_t i;
for ( i = 0; i < c; i++) {
*(L + i) = ((0x000000ff & (uint32_t) raw_key->bbuf[4*i] ) << 24) |
((0x000000ff & (uint32_t) raw_key->bbuf[4*i+1]) << 16) |
((0x000000ff & (uint32_t) raw_key->bbuf[4*i+2]) << 8 ) |
((0x000000ff & (uint32_t) raw_key->bbuf[4*i+3]));
}
/* Fill S[0],...,S[2*r+1] with pseudo-random bits generated by P32 and
* Q32
*/
uint32_t *S;
S = malloc(sizeof(uint32_t)*t);
//Free in main routine
*S = P32;
for ( i = 1; i < t; i++) {
*(S + i) = (*(S + (i - 1))) + Q32;
}
//Mix the secret key L in with S using three passes of length max(c,t)
size_t m = 0, n = 0;
uint32_t A = 0, B = 0;
size_t k = fmax(c,t);
for (i = 0; i < 3*k; i++) {
A = *(S + m) = rotl32(((*(S + m)) + A + B),3);
B = *(L + n) = rotl32(((*(L + n)) + A + B),(A + B));
m = (m + 1) % t;
n = (n + 1) % c;
}
for (i = 0; i < c; i++)
*(L + i) = 0;
//zero first to avoid potentially leaking secret
free(L);
ret.len = t;
ret.text = S;
return ret;
}
data64 key_expand64(bdata *raw_key, size_t r)
{
size_t b = raw_key->blen;
data64 ret;
ret.IV = NULL;
ret.pad = 0;
//Copy raw_key->bbuf[0],...,raw_key->bbuf[b-1] into L[0],...,L[ceil((b-1)/8)]
float temp = ((float) b)/8.0;
uint64_t c = ceil(temp);
uint8_t *tempbuf;
if (b % 8 == 7) {
tempbuf = realloc(raw_key->bbuf, b + 1);
if (tempbuf == NULL) {
ret.len = 0;
fprintf(stderr, "Unable to increase memory size of key to multiple of wordsize in key expand subroutine. Terminating.\n");
return ret;
}
raw_key->bbuf = tempbuf;
raw_key->bbuf[b] = 0;
}
if (b % 8 == 6) {
tempbuf = realloc(raw_key->bbuf, b + 2);
if (tempbuf == NULL) {
ret.len = 0;
fprintf(stderr, "Unable to increase memory size of key to multiple of wordsize in key expand subroutine. Terminating.\n");
return ret;
}
raw_key->bbuf = tempbuf;
raw_key->bbuf[b] = 0;
raw_key->bbuf[b+1] = 0;
}
if (b % 8 == 5) {
tempbuf = realloc(raw_key->bbuf, b + 3);
if (tempbuf == NULL) {
ret.len = 0;
fprintf(stderr, "Unable to increase memory size of key to multiple of wordsize in key expand subroutine. Terminating.\n");
return ret;
}
raw_key->bbuf = tempbuf;
raw_key->bbuf[b] = 0;
raw_key->bbuf[b+1] = 0;
raw_key->bbuf[b+2] = 0;
}
if (b % 8 == 4) {
tempbuf = realloc(raw_key->bbuf, b + 4);
if (tempbuf == NULL) {
ret.len = 0;
fprintf(stderr, "Unable to increase memory size of key to multiple of wordsize in key expand subroutine. Terminating.\n");
return ret;
}
raw_key->bbuf = tempbuf;
raw_key->bbuf[b] = 0;
raw_key->bbuf[b+1] = 0;
raw_key->bbuf[b+2] = 0;
raw_key->bbuf[b+3] = 0;
}
if (b % 8 == 3) {
tempbuf = realloc(raw_key->bbuf, b + 5);
if (tempbuf == NULL) {
ret.len = 0;
fprintf(stderr, "Unable to increase memory size of key to multiple of wordsize in key expand subroutine. Terminating.\n");
return ret;
}
raw_key->bbuf = tempbuf;
raw_key->bbuf[b] = 0;
raw_key->bbuf[b+1] = 0;
raw_key->bbuf[b+2] = 0;
raw_key->bbuf[b+3] = 0;
raw_key->bbuf[b+4] = 0;
}
if (b % 8 == 2) {
tempbuf = realloc(raw_key->bbuf, b + 6);
if (tempbuf == NULL) {
ret.len = 0;
fprintf(stderr, "Unable to increase memory size of key to multiple of wordsize in key expand subroutine. Terminating.\n");
return ret;
}
raw_key->bbuf = tempbuf;
raw_key->bbuf[b] = 0;
raw_key->bbuf[b+1] = 0;
raw_key->bbuf[b+2] = 0;
raw_key->bbuf[b+3] = 0;
raw_key->bbuf[b+4] = 0;
raw_key->bbuf[b+5] = 0;
}
if (b % 8 == 1) {
tempbuf = realloc(raw_key->bbuf, b + 7);
if (tempbuf == NULL) {
ret.len = 0;
fprintf(stderr, "Unable to increase memory size of key to multiple of wordsize in key expand subroutine. Terminating.\n");
return ret;
}
raw_key->bbuf = tempbuf;
raw_key->bbuf[b] = 0;
raw_key->bbuf[b+1] = 0;
raw_key->bbuf[b+2] = 0;
raw_key->bbuf[b+3] = 0;
raw_key->bbuf[b+4] = 0;
raw_key->bbuf[b+5] = 0;
raw_key->bbuf[b+6] = 0;
}
size_t t = 2*(r + 1);
uint64_t *L;
L = malloc(sizeof(uint64_t)*c);
size_t i;
for ( i = 0; i < c; i++) {
*(L + i) = ((0x00000000000000ff & (uint64_t) raw_key->bbuf[8*i] ) << 56) |
((0x00000000000000ff & (uint64_t) raw_key->bbuf[8*i+1]) << 48) |
((0x00000000000000ff & (uint64_t) raw_key->bbuf[8*i+2]) << 40) |
((0x00000000000000ff & (uint64_t) raw_key->bbuf[8*i+3]) << 32) |
((0x00000000000000ff & (uint64_t) raw_key->bbuf[8*i+4]) << 24) |
((0x00000000000000ff & (uint64_t) raw_key->bbuf[8*i+5]) << 16) |
((0x00000000000000ff & (uint64_t) raw_key->bbuf[8*i+6]) << 8 ) |
((0x00000000000000ff & (uint64_t) raw_key->bbuf[8*i+7]));
}
//This doesn't work for key sizes not a multiple of b - tries to read in bytes that don't exist
/* Fill S[0],...,S[2*r+1] with pseudo-random bits generated by P64 and
* Q64
*/
uint64_t *S;
S = malloc(sizeof(uint64_t)*t);
// Free in main routine
*S = P64;
for ( i = 1; i < t; i++) {
*(S + i) = (*(S + (i - 1))) + Q64;
}
//Mix the secret key L in with S using three passes of length max(c,t)
size_t m = 0, n = 0;
uint64_t A = 0, B = 0;
size_t k = fmax(c,t);
for (i = 0; i < 3*k; i++) {
A = *(S + m) = rotl64(((*(S + m)) + A + B),3);
B = *(L + n) = rotl64(((*(L + n)) + A + B),(A + B));
m = (m + 1) % t;
n = (n + 1) % c;
}
for (i = 0; i < c; i++)
*(L + i) = 0;
//zero first to avoid potentially leaking secret
free(L);
ret.len = t;
ret.text = S;
return ret;
}
void encrypt16(uint16_t *ptext, uint16_t *ctext, uint16_t *S, size_t r)
{
/* Takes two pointers to blocks of two plaintext words and places the enciphered plaintext
* into the ciphertext block.
*/
uint16_t A, B;
A = * ptext + * S;
B = *(ptext + 1) + *(S + 1);
size_t i;
for (i = 1; i <= r; i++) {
A = rotl16((A ^ B), B) + *(S + 2*i);
B = rotl16((B ^ A), A) + *(S + 2*i + 1);
}
*ctext = A;
*(ctext + 1) = B;
}
void decrypt16(uint16_t *ctext, uint16_t *ptext, uint16_t *S, size_t r)
{
/* Takes two pointers to blocks of two plaintext words and places the deciphered ciphertext
* into the plaintext block.
*/
uint16_t A, B;
A = *ctext;
B = *(ctext + 1);
size_t i;
for (i = r; i > 0; i--) {
B = rotr16((B - *(S + 2*i + 1)), A)^A;
A = rotr16((A - *(S + 2*i)), B)^B;
}
B = B - *(S + 1);
A = A - *S;
*ptext = A;
*(ptext + 1) = B;
}
void encrypt32(uint32_t *ptext, uint32_t *ctext, uint32_t *S, size_t r)
{
/* Takes two pointers to blocks of two plaintext words and places the enciphered plaintext
* into the ciphertext block.
*/
uint32_t A, B;
A = * ptext + * S;
B = *(ptext + 1) + *(S + 1);
size_t i;
for (i = 1; i <= r; i++) {
A = rotl32((A ^ B), B) + *(S + 2*i);
B = rotl32((B ^ A), A) + *(S + 2*i + 1);
}
*ctext = A;
*(ctext + 1) = B;
}
void decrypt32(uint32_t *ctext, uint32_t *ptext, uint32_t *S, size_t r)
{
/* Takes two pointers to blocks of two plaintext words and places the deciphered ciphertext
* into the plaintext block.
*/
uint32_t A, B;
A = *ctext;
B = *(ctext + 1);
size_t i;
for (i = r; i > 0; i--) {
B = rotr32((B - *(S + 2*i + 1)), A)^A;
A = rotr32((A - *(S + 2*i)), B)^B;
}
B = B - *(S + 1);
A = A - *S;
*ptext = A;
*(ptext + 1) = B;
}
void encrypt64(uint64_t *ptext, uint64_t *ctext, uint64_t *S, size_t r)
{
/* Takes two pointers to blocks of two plaintext words and places the enciphered plaintext
* into the ciphertext block.
*/
uint64_t A, B;
A = * ptext + * S;
B = *(ptext + 1) + *(S + 1);
size_t i;
for (i = 1; i <= r; i++) {
A = rotl64((A ^ B), B) + *(S + 2*i);
B = rotl64((B ^ A), A) + *(S + 2*i + 1);
}
*ctext = A;
*(ctext + 1) = B;
}
void decrypt64(uint64_t *ctext, uint64_t *ptext, uint64_t *S, size_t r)
{
/* Takes two pointers to blocks of two plaintext words and places the deciphered ciphertext
* into the plaintext block.
*/
uint64_t A, B;
A = *ctext;
B = *(ctext + 1);
size_t i;
for (i = r; i > 0; i--) {
B = rotr64((B - *(S + 2*i + 1)), A)^A;
A = rotr64((A - *(S + 2*i)), B)^B;
}
B = B - *(S + 1);
A = A - *S;
*ptext = A;
*(ptext + 1) = B;
}