-
Notifications
You must be signed in to change notification settings - Fork 0
/
ioroutines.c
600 lines (559 loc) · 18.6 KB
/
ioroutines.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#include "ioroutines.h"
bdata read_input(FILE *instream)
{
bdata ret;
size_t bufsize = 200;
size_t len = 0;
uint8_t *buf, *tempbuf;
buf = malloc(bufsize*sizeof(uint8_t));
if (buf == NULL) {
fprintf(stderr, "Unable to allocate memory to read input. Terminating.\n");
exit(EXIT_FAILURE);
ret.blen = 0;
return ret;
}
int c;
while ((c = fgetc(instream)) != EOF) {
if (len + 1 > bufsize) {
bufsize *= 2;
tempbuf = realloc(buf, bufsize);
if (tempbuf == NULL) {
ungetc(c, instream);
len = 0;
fprintf(stderr, "Unable to allocate extra memory to read large input.\n");
}
buf = tempbuf;
}
*(buf + len++) = (uint8_t) c;
}
if (len == 0) {
free(buf);
fprintf(stderr, "Error - no data read. Terminating.\n");
}
tempbuf = realloc(buf, len);
if (tempbuf == NULL) {
fprintf(stderr, "Error shrinking memory to size after reading input.\n");
}
buf = tempbuf;
ret.bbuf = buf;
ret.blen = len;
return ret;
}
void free_bdata(bdata *data)
{
size_t i;
for (i = 0; i < data->blen; i++) {
*(data->bbuf + i) = 0;
}
data->blen = 0;
free(data->bbuf);
}
data16 prepare_data16(bdata *input, padmode_t padmode, opmode_t opmode, cmode_t cmode)
{
/* Take input buffer, return pointer to list of words padded to appropriate length for mode
*/
data16 ret;
ret.text = NULL;
size_t IVoffset = 0;
size_t get_rand_err;
int get_rand_err_no;
if (opmode == DECRYPT) {
if (padmode == PKCS7 && (input->blen % 4) != 0) {
fprintf(stderr, "Ciphertext not encrpyted with block-completing padding method. Terminating.\n");
ret.len = 0;
ret.text = NULL;
return ret;
}
}
size_t pad;
if (padmode == CTS || opmode == DECRYPT) {
pad = ((size_t) 4) - (((input->blen - 1) % 4) + 1);
//shouldn't pad in decrypt mode where encryption used PKCS7 as error check above guarantees last block will be full
}
else {
pad = ((size_t) 4) - (input->blen % 4);
}
input->blen += pad;
ret.pad = pad;
input->bbuf = realloc(input->bbuf, input->blen);
if (input->bbuf == NULL) {
fprintf(stderr, "Unable to allocate memory for padding input in data preparation routine. Terminating.\n");
ret.len = 0;
ret.text = NULL;
return ret;
}
size_t i;
for (i = input->blen - pad; i < input->blen; i++) {
if (padmode == CTS)
*(input->bbuf + i) = (uint8_t) 0;
else
*(input->bbuf + i) = (uint8_t) pad;
}
ret.len = input->blen/2; //return length in words rather than blocks or bytes
ret.IV = NULL;
if ( cmode == CBC && opmode == DECRYPT ) {
IVoffset = 2;
ret.IV = malloc(2*sizeof(uint16_t));
if (ret.IV == NULL) {
fprintf(stderr, "Unable to allocate memory for IV in data preparation routine. Terminating.\n");
ret.len = 0;
return ret; //make sure to handle error in main
}
*(ret.IV) = (*(input->bbuf) << 8) | *(input->bbuf + 1);
*(ret.IV + 1) = (*(input->bbuf + 2) << 8) | *(input->bbuf + 3);
}
else if ( cmode == CBC && opmode == ENCRYPT ) {
ret.IV = malloc(2*sizeof(uint16_t));
if (ret.IV == NULL) {
fprintf(stderr, "Unable to allocate memory for IV in data preparation routine. Terminating.\n");
ret.len = 0;
return ret; //make sure to handle error in main
}
if ((get_rand_err = getrandom(ret.IV, 4, GRND_RANDOM)) != 4) { //Endianess shouldn't matter here as long as we read the IV into the output buffer correctly
get_rand_err_no = errno;
fprintf(stderr, "Unable to acquire cryptographically random bytes to generate IV. Error code %ld returned. Errno is %d. Terminating.\n", get_rand_err, get_rand_err_no);
ret.len = 0;
return ret;
}
}
ret.len -= IVoffset;
ret.text = malloc(ret.len*sizeof(uint16_t));
if (ret.text == NULL) {
fprintf(stderr, "Unable to allocate memory in data preparation routine. Terminating.\n");
ret.len = 0;
return ret; //make sure to handle error in main
}
for (i = 0; i < ret.len; i++) {
*(ret.text + i) = ((*(input->bbuf + 2*IVoffset + 2*i)) << 8) | (*(input->bbuf + 2*IVoffset + 2*i + 1));
}
return ret;
}
data32 prepare_data32(bdata *input, padmode_t padmode, opmode_t opmode, cmode_t cmode)
{
/* Take input buffer, return pointer to list of words padded to appropriate length for mode
*/
data32 ret;
ret.text = NULL;
size_t IVoffset = 0;
size_t get_rand_err;
int get_rand_err_no;
if (opmode == DECRYPT) {
if (padmode == PKCS7 && (input->blen % 8) != 0) {
fprintf(stderr, "Ciphertext not encrpyted with block-completing padding method. Terminating.\n");
ret.len = 0;
ret.text = NULL;
return ret;
}
}
size_t pad;
if (padmode == CTS || opmode == DECRYPT)
pad = ((size_t) 8) - (((input->blen - 1) % 8) + 1);
//shouldn't pad in decrypt mode where encryption used PKCS7 as error check above guarantees last block will be full
else
pad = ((size_t) 8) - (input->blen % 8);
input->blen += pad;
ret.pad = pad;
input->bbuf = realloc(input->bbuf, input->blen);
if (input->bbuf == NULL) {
fprintf(stderr, "Unable to allocate memory for padding input in data preparation routine. Terminating.\n");
ret.len = 0;
ret.text = NULL;
return ret;
}
size_t i;
for (i = input->blen - pad; i < input->blen; i++) {
if (padmode == CTS)
*(input->bbuf + i) = (uint8_t) 0;
else
*(input->bbuf + i) = (uint8_t) pad;
}
ret.len = input->blen/4; //return length in words rather than blocks or bytes
ret.IV = NULL;
if ( cmode == CBC && opmode == DECRYPT) {
IVoffset = 2;
ret.IV = malloc(2*sizeof(uint32_t));
if (ret.IV == NULL) {
fprintf(stderr, "Unable to allocate memory for IV in data preparation routine. Terminating.\n");
ret.len = 0;
return ret; //make sure to handle error in main
}
*(ret.IV) = (*(input->bbuf) << 24) | (*(input->bbuf + 1) << 16) | (*(input->bbuf + 2) << 8) | *(input->bbuf + 3);
*(ret.IV + 1) = (*(input->bbuf + 4) << 24) | (*(input->bbuf + 5) << 16) | (*(input->bbuf + 6) << 8) | *(input->bbuf + 7);
}
else if ( cmode == CBC && opmode == ENCRYPT ) {
ret.IV = malloc(2*sizeof(uint32_t));
if (ret.IV == NULL) {
fprintf(stderr, "Unable to allocate memory for IV in data preparation routine. Terminating.\n");
ret.len = 0;
return ret; //make sure to handle error in main
}
if ((get_rand_err = getrandom(ret.IV, 8, GRND_RANDOM)) != 8) { //Endianess shouldn't matter here as long as we read the IV into the output buffer correctly
get_rand_err_no = errno;
fprintf(stderr, "Unable to acquire cryptographically random bytes to generate IV. Error code %ld returned. Errno is %d. Terminating.\n", get_rand_err, get_rand_err_no);
ret.len = 0;
return ret;
}
}
ret.len -= IVoffset;
ret.text = malloc(ret.len*sizeof(uint32_t));
if (ret.text == NULL) {
fprintf(stderr, "Unable to allocate memory in data preparation routine. Terminating.\n");
ret.len = 0;
return ret; //make sure to handle error in main
}
for (i = 0; i < ret.len; i++)
*(ret.text + i) = (( *(input->bbuf + 4*IVoffset + 4*i )) << 24) | \
((*(input->bbuf + 4*IVoffset + 4*i + 1)) << 16) | \
((*(input->bbuf + 4*IVoffset + 4*i + 2)) << 8 ) | \
((*(input->bbuf + 4*IVoffset + 4*i + 3)) );
return ret;
}
data64 prepare_data64(bdata *input, padmode_t padmode, opmode_t opmode, cmode_t cmode)
{
/* Take input buffer, return pointer to list of words padded to appropriate length for mode
*/
data64 ret;
ret.text = NULL;
size_t IVoffset = 0;
size_t get_rand_err;
int get_rand_err_no;
if (opmode == DECRYPT) {
if (padmode == PKCS7 && (input->blen % 16) != 0) {
fprintf(stderr, "Ciphertext not encrpyted with block-completing padding method. Terminating.\n");
ret.len = 0;
ret.text = NULL;
return ret;
}
}
size_t pad;
if (padmode == CTS || opmode == DECRYPT)
pad = ((size_t) 16) - (((input->blen - 1) % 16) + 1);
//shouldn't pad in decrypt mode where encryption used PKCS7 as error check above guarantees last block will be full
else
pad = ((size_t) 16) - (input->blen % 16);
input->blen += pad;
ret.pad = pad;
input->bbuf = realloc(input->bbuf, input->blen);
if (input->bbuf == NULL) {
fprintf(stderr, "Unable to allocate memory for padding input in data preparation routine. Terminating.\n");
ret.len = 0;
ret.text = NULL;
return ret;
}
size_t i;
for (i = input->blen - pad; i < input->blen; i++) {
if (padmode == CTS)
*(input->bbuf + i) = (uint8_t) 0;
else
*(input->bbuf + i) = (uint8_t) pad;
}
ret.len = input->blen/8; //return length in words rather than blocks or bytes
ret.IV = NULL;
if ( cmode == CBC && opmode == DECRYPT) {
IVoffset = 2;
ret.IV = malloc(2*sizeof(uint64_t));
if (ret.IV == NULL) {
fprintf(stderr, "Unable to allocate memory for IV in data preparation routine. Terminating.\n");
ret.len = 0;
return ret; //make sure to handle error in main
}
*(ret.IV) = (((uint64_t) (*(input->bbuf ))) << 56) | (((uint64_t) (*(input->bbuf + 1 ))) << 48) |\
(((uint64_t) (*(input->bbuf + 2 ))) << 40) | (((uint64_t) (*(input->bbuf + 3 ))) << 32) |\
(((uint64_t) (*(input->bbuf + 4 ))) << 24) | (((uint64_t) (*(input->bbuf + 5 ))) << 16) |\
(((uint64_t) (*(input->bbuf + 6 ))) << 8) | (((uint64_t) (*(input->bbuf + 7 ))));
*(ret.IV + 1) = (((uint64_t) (*(input->bbuf + 8 ))) << 56) | (((uint64_t) (*(input->bbuf + 9 ))) << 48) |\
(((uint64_t) (*(input->bbuf + 10))) << 40) | (((uint64_t) (*(input->bbuf + 11))) << 32) |\
(((uint64_t) (*(input->bbuf + 12))) << 24) | (((uint64_t) (*(input->bbuf + 13))) << 16) |\
(((uint64_t) (*(input->bbuf + 14))) << 8) | (((uint64_t) (*(input->bbuf + 15))));
}
else if ( cmode == CBC && opmode == ENCRYPT ) {
ret.IV = malloc(2*sizeof(uint64_t));
if (ret.IV == NULL) {
fprintf(stderr, "Unable to allocate memory for IV in data preparation routine. Terminating.\n");
ret.len = 0;
return ret; //make sure to handle error in main
}
if ((get_rand_err = getrandom(ret.IV, 16, GRND_RANDOM)) != 16) { //Endianess shouldn't matter here as long as we read the IV into the output buffer correctly
get_rand_err_no = errno;
fprintf(stderr, "Unable to acquire cryptographically random bytes to generate IV. Error code %ld returned. Errno is %d. Terminating.\n", get_rand_err, get_rand_err_no);
ret.len = 0;
return ret;
}
}
ret.len -= IVoffset;
ret.text = malloc(ret.len*sizeof(uint64_t));
if (ret.text == NULL) {
fprintf(stderr, "Unable to allocate memory in data preparation routine. Terminating.\n");
ret.len = 0;
return ret; //make sure to handle error in main
}
for (i = 0; i < ret.len; i++)
*(ret.text + i) = (((uint64_t) (*( input->bbuf + 8*IVoffset + 8*i ))) << 56) | \
(((uint64_t) (*(input->bbuf + 8*IVoffset + 8*i + 1))) << 48) | \
(((uint64_t) (*(input->bbuf + 8*IVoffset + 8*i + 2))) << 40) | \
(((uint64_t) (*(input->bbuf + 8*IVoffset + 8*i + 3))) << 32) | \
(((uint64_t) (*(input->bbuf + 8*IVoffset + 8*i + 4))) << 24) | \
(((uint64_t) (*(input->bbuf + 8*IVoffset + 8*i + 5))) << 16) | \
(((uint64_t) (*(input->bbuf + 8*IVoffset + 8*i + 6))) << 8) | \
(((uint64_t) (*(input->bbuf + 8*IVoffset + 8*i + 7))) );
return ret;
}
data16 prepare_output16(data16 *input, padmode_t padmode, cmode_t cmode)
{
data16 ret;
ret.len = input->len;
ret.pad = input->pad; //Value only used in CTS mode
ret.IV = NULL;
if (cmode == CBC) {
ret.IV = input->IV;
}
ret.text = malloc(ret.len*sizeof(uint16_t));
if (ret.text == NULL) {
fprintf(stderr, "Unable to allocate memory in output preparation routine. Terminating.\n");
ret.len = 0;
return ret; //make sure to handle error in main
}
ret.pad = input->pad;
return ret;
}
data32 prepare_output32(data32 *input, padmode_t padmode, cmode_t cmode)
{
data32 ret;
ret.len = input->len;
ret.pad = input->pad; //Value only used in CTS mode
ret.IV = NULL;
if (cmode == CBC) {
ret.IV = input->IV;
}
ret.text = malloc(ret.len*sizeof(uint32_t));
if (ret.text == NULL) {
fprintf(stderr, "Unable to allocate memory in output preparation routine. Terminating.\n");
ret.len = 0;
return ret; //make sure to handle error in main
}
ret.pad = input->pad;
return ret;
}
data64 prepare_output64(data64 *input, padmode_t padmode, cmode_t cmode)
{
data64 ret;
ret.len = input->len;
ret.pad = input->pad; //Value only used in CTS mode
ret.IV = NULL;
if (cmode == CBC) {
ret.IV = input->IV;
}
ret.text = malloc(ret.len*sizeof(uint64_t));
if (ret.text == NULL) {
fprintf(stderr, "Unable to allocate memory in output preparation routine. Terminating.\n");
ret.len = 0;
return ret; //make sure to handle error in main
}
ret.pad = input->pad;
return ret;
}
bdata output_data16(data16 *output, opmode_t opmode, padmode_t padmode, cmode_t cmode)
{
size_t IVoffset, pad;
IVoffset = 0;
if (opmode == ENCRYPT && cmode == CBC) {
IVoffset = 2;
}
pad = 0;
if (padmode == CTS) {
pad = output->pad;
}
bdata ret;
ret.blen = 2*(IVoffset + output->len) - pad;
ret.bbuf = malloc(ret.blen);
if (ret.bbuf == NULL) {
fprintf(stderr, "Unable to allocate memory to output->data. Terminating.\n");
ret.blen = 0;
return ret;
}
if (opmode == ENCRYPT && cmode == CBC) {
*(ret.bbuf ) = (*(output->IV ) >> 8 ) & 0xff;
*(ret.bbuf + 1) = (*(output->IV ) ) & 0xff;
*(ret.bbuf + 2) = (*(output->IV + 1 ) >> 8 ) & 0xff;
*(ret.bbuf + 3) = (*(output->IV + 1 ) ) & 0xff;
}
size_t i;
for (i = 2*IVoffset; i < ret.blen; i++) {
*(ret.bbuf + i) = (*(output->text - IVoffset + i/2) >> 8*(1 - (i % 2))) & 0xff;
}
size_t j;
if (padmode == PKCS7 && opmode == DECRYPT) {
pad = 0;
j = 4;
while (pad == 0 && j > 0) {
pad = unpad((ret.bbuf + ret.blen - j), j, j);
j -= 1;
}
if (pad == 0) {
fprintf(stderr, "Warning - decrypted text does not appear to be PKCS7 padded\n");
}
else {
ret.blen -= pad;
ret.bbuf = realloc(ret.bbuf, ret.blen);
}
}
return ret;
}
bdata output_data32(data32 *output, opmode_t opmode, padmode_t padmode, cmode_t cmode)
{
size_t IVoffset, pad;
IVoffset = 0;
if (opmode == ENCRYPT && cmode == CBC) {
IVoffset = 2;
}
pad = 0;
if (padmode == CTS) {
pad = output->pad;
}
bdata ret;
ret.blen = 4*(IVoffset + output->len) - pad;
ret.bbuf = malloc(ret.blen);
if (ret.bbuf == NULL) {
fprintf(stderr, "Unable to allocate memory to output->data. Terminating.\n");
ret.blen = 0;
return ret;
}
if (opmode == ENCRYPT && cmode == CBC) {
*(ret.bbuf ) = (*(output->IV ) >> 24 ) & 0xff;
*(ret.bbuf + 1) = (*(output->IV ) >> 16 ) & 0xff;
*(ret.bbuf + 2) = (*(output->IV ) >> 8 ) & 0xff;
*(ret.bbuf + 3) = (*(output->IV ) ) & 0xff;
*(ret.bbuf + 4) = (*(output->IV + 1 ) >> 24 ) & 0xff;
*(ret.bbuf + 5) = (*(output->IV + 1 ) >> 16 ) & 0xff;
*(ret.bbuf + 6) = (*(output->IV + 1 ) >> 8 ) & 0xff;
*(ret.bbuf + 7) = (*(output->IV + 1 ) ) & 0xff;
}
size_t i;
for (i = 4*IVoffset; i < ret.blen; i++) {
*(ret.bbuf + i) = (*(output->text - IVoffset + i/4) >> 8*(3 - (i % 4))) & 0xff;
}
size_t j;
if (padmode == PKCS7 && opmode == DECRYPT) {
pad = 0;
j = 8;
while (pad == 0 && j > 0) {
pad = unpad((ret.bbuf + ret.blen - j), j, j);
j -= 1;
}
if (pad == 0) {
fprintf(stderr, "Warning - decrypted text does not appear to be PKCS7 padded\n");
}
else {
ret.blen -= pad;
ret.bbuf = realloc(ret.bbuf, ret.blen);
}
}
return ret;
}
bdata output_data64(data64 *output, opmode_t opmode, padmode_t padmode, cmode_t cmode)
{
size_t IVoffset, pad;
IVoffset = 0;
if (opmode == ENCRYPT && cmode == CBC) {
IVoffset = 2;
}
pad = 0;
if (padmode == CTS) {
pad = output->pad;
}
bdata ret;
ret.blen = 8*(IVoffset + output->len) - pad;
ret.bbuf = malloc(ret.blen);
if (ret.bbuf == NULL) {
fprintf(stderr, "Unable to allocate memory to output->data. Terminating.\n");
ret.blen = 0;
return ret;
}
if (opmode == ENCRYPT && cmode == CBC) {
*(ret.bbuf ) = (*(output->IV ) >> 56 ) & 0xff;
*(ret.bbuf + 1) = (*(output->IV ) >> 48 ) & 0xff;
*(ret.bbuf + 2) = (*(output->IV ) >> 40 ) & 0xff;
*(ret.bbuf + 3) = (*(output->IV ) >> 32 ) & 0xff;
*(ret.bbuf + 4) = (*(output->IV ) >> 24 ) & 0xff;
*(ret.bbuf + 5) = (*(output->IV ) >> 16 ) & 0xff;
*(ret.bbuf + 6) = (*(output->IV ) >> 8 ) & 0xff;
*(ret.bbuf + 7) = (*(output->IV ) ) & 0xff;
*(ret.bbuf + 8) = (*(output->IV + 1 ) >> 56 ) & 0xff;
*(ret.bbuf + 9) = (*(output->IV + 1 ) >> 48 ) & 0xff;
*(ret.bbuf + 10) = (*(output->IV + 1 ) >> 40 ) & 0xff;
*(ret.bbuf + 11) = (*(output->IV + 1 ) >> 32 ) & 0xff;
*(ret.bbuf + 12) = (*(output->IV + 1 ) >> 24 ) & 0xff;
*(ret.bbuf + 13) = (*(output->IV + 1 ) >> 16 ) & 0xff;
*(ret.bbuf + 14) = (*(output->IV + 1 ) >> 8 ) & 0xff;
*(ret.bbuf + 15) = (*(output->IV + 1 ) ) & 0xff;
}
size_t i;
for (i = 8*IVoffset; i < ret.blen; i++) {
*(ret.bbuf + i) = (*(output->text - IVoffset + i/8) >> 8*(7 - (i % 8))) & 0xff;
}
size_t j;
if (padmode == PKCS7 && opmode == DECRYPT) {
pad = 0;
j = 16;
while (pad == 0 && j > 0) {
pad = unpad((ret.bbuf + ret.blen - j), j, j);
j -= 1;
}
if (pad == 0) {
fprintf(stderr, "Warning - decrypted text does not appear to be PKCS7 padded\n");
}
else {
ret.blen -= pad;
ret.bbuf = realloc(ret.bbuf, ret.blen);
}
}
return ret;
}
void free_data16(data16 data, cmode_t cmode, dmode_t dmode)
{
size_t i;
for (i = 0; i < data.len; i++) {
*(data.text + i) = 0;
}
free(data.text);
data.len = 0;
if (cmode == CBC && dmode == INDATA) {
free(data.IV);
}
}
void free_data32(data32 data, cmode_t cmode, dmode_t dmode)
{
size_t i;
for (i = 0; i < data.len; i++) {
*(data.text + i) = 0;
}
free(data.text);
data.len = 0;
if (cmode == CBC && dmode == INDATA) {
free(data.IV);
}
}
void free_data64(data64 data, cmode_t cmode, dmode_t dmode)
{
size_t i;
for (i = 0; i < data.len; i++) {
*(data.text + i) = 0;
}
free(data.text);
data.len = 0;
if (cmode == CBC && dmode == INDATA) {
free(data.IV);
}
}
size_t unpad(uint8_t *bbuf, size_t current_test, size_t count)
{
if (count == 0) {
return current_test;
}
if ( *bbuf != (uint8_t) current_test ) {
return 0;
}
else {
return unpad(bbuf + 1, current_test, count - 1);
}
}