-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes.c
674 lines (567 loc) · 27.1 KB
/
aes.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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "Buffer.h"
uint8_t E[256] = {
0x01, 0x03, 0x05, 0x0F, 0x11, 0x33, 0x55, 0xFF, 0x1A, 0x2E, 0x72, 0x96, 0xA1, 0xF8, 0x13, 0x35,
0x5F, 0xE1, 0x38, 0x48, 0xD8, 0x73, 0x95, 0xA4, 0xF7, 0x02, 0x06, 0x0A, 0x1E, 0x22, 0x66, 0xAA,
0xE5, 0x34, 0x5C, 0xE4, 0x37, 0x59, 0xEB, 0x26, 0x6A, 0xBE, 0xD9, 0x70, 0x90, 0xAB, 0xE6, 0x31,
0x53, 0xF5, 0x04, 0x0C, 0x14, 0x3C, 0x44, 0xCC, 0x4F, 0xD1, 0x68, 0xB8, 0xD3, 0x6E, 0xB2, 0xCD,
0x4C, 0xD4, 0x67, 0xA9, 0xE0, 0x3B, 0x4D, 0xD7, 0x62, 0xA6, 0xF1, 0x08, 0x18, 0x28, 0x78, 0x88,
0x83, 0x9E, 0xB9, 0xD0, 0x6B, 0xBD, 0xDC, 0x7F, 0x81, 0x98, 0xB3, 0xCE, 0x49, 0xDB, 0x76, 0x9A,
0xB5, 0xC4, 0x57, 0xF9, 0x10, 0x30, 0x50, 0x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,
0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D};
uint8_t mul2[256] = {
0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e,
0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e,
0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e,
0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e,
0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe,
0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe,
0x1b, 0x19, 0x1f, 0x1d, 0x13, 0x11, 0x17, 0x15, 0x0b, 0x09, 0x0f, 0x0d, 0x03, 0x01, 0x07, 0x05,
0x3b, 0x39, 0x3f, 0x3d, 0x33, 0x31, 0x37, 0x35, 0x2b, 0x29, 0x2f, 0x2d, 0x23, 0x21, 0x27, 0x25,
0x5b, 0x59, 0x5f, 0x5d, 0x53, 0x51, 0x57, 0x55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xcb, 0x8d, 0x01, 0x02, 0x04,
0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63,
0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d};
// galois field multiplication
static inline uint8_t finiteFieldMultiply(uint8_t a, uint8_t b){
if (a == 0x00) return 0x00;
if (b == 0x00) return 0x00;
if (a == 0x01) return b;
if (b == 0x01) return a;
int ra = L[a];
int ba = L[b];
int r = ra + ba;
if (r > 255) r -= 255;
return E[r];
}
// alias for finiteFieldMultiply
static inline uint8_t mul(uint8_t a, uint8_t b){
return finiteFieldMultiply(a, b);
}
// =====================
// key expansion
// =====================
static void rotWord(uint8_t *word){
// rotate 4 bytes to the left: 1234 -> 2341
uint32_t *q = (uint32_t *)word;
*q = (*q >> 8) | ((*q & 0xff) << 24);
}
static void subWord(uint8_t *word) {
for(int i = 0; i < 4; i++){
word[i] = s_box[word[i]];
}
}
void keyExpansionCore(uint8_t *in, uint8_t i)
{
// // rotate
// uint32_t *q = (uint32_t *)in;
// *q = (*q >> 8) | ((*q & 0xff) << 24);
rotWord(in);
// s box
subWord(in);
// Rcon
in[0] ^= rcon[i];
}
void KeyExpansion(uint8_t *inputKey, uint8_t *expandedKeys)
{
// first 16 bytes are the original key
memcpy(expandedKeys, inputKey, 16);
size_t bytesGenerated = 16; // we've generated 16 bytes so far
size_t rconIteration = 1; // RCon iteration begins at 1
uint8_t temp[4]; // temporary storage for core
while (bytesGenerated < 176)
{
memcpy(temp, expandedKeys + bytesGenerated - 4, 4);
if (bytesGenerated % 16 == 0)
{
keyExpansionCore(temp, rconIteration);
rconIteration++;
}
for (int a = 0; a < 4; a++)
{
expandedKeys[bytesGenerated] = expandedKeys[bytesGenerated - 16] ^ temp[a];
bytesGenerated++;
}
}
}
/**
* @brief Substitutes each byte in the ciphertext with the corrsponding byte in the s_box table.
*
* @param state
*/
void subBytes(uint8_t *state) {
for(int i = 0; i < 16; i++){
state[i] = s_box[state[i]];
}
}
/**
* @brief Reverses the byte substitution stage using a inverse s_box table.
*
* @param state
*/
void subBytes_inverse(uint8_t *state) {
for( int i = 0; i < 16; i++){
state[i] = s_box_inverse[state[i]];
}
}
/**
* @brief Shifts the rows in the grid to the left. Each row is shifted 1 byte more than the previous with the first not not being shifted at all.
*
* @param state
*/
void shiftRows(uint8_t *state)
{
// 00 04 08 12 \ 00 04 08 12
// 01 05 09 13 ___________\ 05 09 13 01
// 02 06 10 14 / 10 14 02 06
// 03 07 11 15 / 15 03 07 11
uint8_t tmp[16];
tmp[0] = state[0];
tmp[1] = state[5];
tmp[2] = state[10];
tmp[3] = state[15];
tmp[4] = state[4];
tmp[5] = state[9];
tmp[6] = state[14];
tmp[7] = state[3];
tmp[8] = state[8];
tmp[9] = state[13];
tmp[10] = state[2];
tmp[11] = state[7];
tmp[12] = state[12];
tmp[13] = state[1];
tmp[14] = state[6];
tmp[15] = state[11];
memcpy(state, tmp, 16);
}
/**
* @brief Reversed the shiftRows staged by shifting them to the right.
*
* @param state
*/
void shiftRows_inverse(uint8_t *state)
{
// 00 04 08 12 \ 00 04 08 12
// 01 05 09 13 ___________\ 13 01 05 09
// 02 06 10 14 / 10 14 02 06
// 03 07 11 15 / 07 11 15 03
uint8_t tmp[16];
tmp[0] = state[0];
tmp[1] = state[13];
tmp[2] = state[10];
tmp[3] = state[7];
tmp[4] = state[4];
tmp[5] = state[1];
tmp[6] = state[14];
tmp[7] = state[11];
tmp[8] = state[8];
tmp[9] = state[5];
tmp[10] = state[2];
tmp[11] = state[15];
tmp[12] = state[12];
tmp[13] = state[9];
tmp[14] = state[6];
tmp[15] = state[3];
memcpy(state, tmp, 16);
}
/**
* @brief The mix columns stage, which is complicated.
*
* @param state
*/
void mixColumns(uint8_t *state)
{
//Galois field matrix multiplication by this matrix of the matrix made up by the state's columns
// |2 3 1 1| |00 05 09 13|
// |1 2 3 1| |01 04 08 12|
// |1 1 2 3| |02 06 10 14|
// |3 1 1 2| |03 07 11 15|
// special state columns
// matrix matrix
uint8_t result[16];
result[0] = mul(0x02, state[ 0]) ^ mul(0x03, state[ 1]) ^ mul(0x01, state[ 2]) ^ mul(0x01, state[ 3]);
result[1] = mul(0x01, state[ 0]) ^ mul(0x02, state[ 1]) ^ mul(0x03, state[ 2]) ^ mul(0x01, state[ 3]);
result[2] = mul(0x01, state[ 0]) ^ mul(0x01, state[ 1]) ^ mul(0x02, state[ 2]) ^ mul(0x03, state[ 3]);
result[3] = mul(0x03, state[ 0]) ^ mul(0x01, state[ 1]) ^ mul(0x01, state[ 2]) ^ mul(0x02, state[ 3]);
result[4] = mul(0x02, state[ 4]) ^ mul(0x03, state[ 5]) ^ mul(0x01, state[ 6]) ^ mul(0x01, state[ 7]);
result[5] = mul(0x01, state[ 4]) ^ mul(0x02, state[ 5]) ^ mul(0x03, state[ 6]) ^ mul(0x01, state[ 7]);
result[6] = mul(0x01, state[ 4]) ^ mul(0x01, state[ 5]) ^ mul(0x02, state[ 6]) ^ mul(0x03, state[ 7]);
result[7] = mul(0x03, state[ 4]) ^ mul(0x01, state[ 5]) ^ mul(0x01, state[ 6]) ^ mul(0x02, state[ 7]);
result[8] = mul(0x02, state[ 8]) ^ mul(0x03, state[ 9]) ^ mul(0x01, state[10]) ^ mul(0x01, state[11]);
result[9] = mul(0x01, state[ 8]) ^ mul(0x02, state[ 9]) ^ mul(0x03, state[10]) ^ mul(0x01, state[11]);
result[10] = mul(0x01, state[ 8]) ^ mul(0x01, state[ 9]) ^ mul(0x02, state[10]) ^ mul(0x03, state[11]);
result[11] = mul(0x03, state[ 8]) ^ mul(0x01, state[ 9]) ^ mul(0x01, state[10]) ^ mul(0x02, state[11]);
result[12] = mul(0x02, state[12]) ^ mul(0x03, state[13]) ^ mul(0x01, state[14]) ^ mul(0x01, state[15]);
result[13] = mul(0x01, state[12]) ^ mul(0x02, state[13]) ^ mul(0x03, state[14]) ^ mul(0x01, state[15]);
result[14] = mul(0x01, state[12]) ^ mul(0x01, state[13]) ^ mul(0x02, state[14]) ^ mul(0x03, state[15]);
result[15] = mul(0x03, state[12]) ^ mul(0x01, state[13]) ^ mul(0x01, state[14]) ^ mul(0x02, state[15]);
memcpy(state, result, 16);
}
/**
* @brief Reversed the mix columns stage.
*
* @param state
*/
void mixColumns_inverse(uint8_t *state) {
//Galois field matrix multiplication by this matrix (inverse of the one used for the non-reverse mixColumns) of the matrix made up by the state's columns
// |0E 0B 0D 09| |00 05 09 13|
// |09 0E 0B 0D| |01 04 08 12|
// |0D 09 0E 0B| |02 06 10 14|
// |0B 0D 09 0E| |03 07 11 15|
// special state columns
// matrix matrix
uint8_t result[16];
result[0] = mul(0x0E, state[ 0]) ^ mul(0x0B, state[ 1]) ^ mul(0x0D, state[ 2]) ^ mul(0x09, state[ 3]);
result[1] = mul(0x09, state[ 0]) ^ mul(0x0E, state[ 1]) ^ mul(0x0B, state[ 2]) ^ mul(0x0D, state[ 3]);
result[2] = mul(0x0D, state[ 0]) ^ mul(0x09, state[ 1]) ^ mul(0x0E, state[ 2]) ^ mul(0x0B, state[ 3]);
result[3] = mul(0x0B, state[ 0]) ^ mul(0x0D, state[ 1]) ^ mul(0x09, state[ 2]) ^ mul(0x0E, state[ 3]);
result[4] = mul(0x0E, state[ 4]) ^ mul(0x0B, state[ 5]) ^ mul(0x0D, state[ 6]) ^ mul(0x09, state[ 7]);
result[5] = mul(0x09, state[ 4]) ^ mul(0x0E, state[ 5]) ^ mul(0x0B, state[ 6]) ^ mul(0x0D, state[ 7]);
result[6] = mul(0x0D, state[ 4]) ^ mul(0x09, state[ 5]) ^ mul(0x0E, state[ 6]) ^ mul(0x0B, state[ 7]);
result[7] = mul(0x0B, state[ 4]) ^ mul(0x0D, state[ 5]) ^ mul(0x09, state[ 6]) ^ mul(0x0E, state[ 7]);
result[8] = mul(0x0E, state[ 8]) ^ mul(0x0B, state[ 9]) ^ mul(0x0D, state[10]) ^ mul(0x09, state[11]);
result[9] = mul(0x09, state[ 8]) ^ mul(0x0E, state[ 9]) ^ mul(0x0B, state[10]) ^ mul(0x0D, state[11]);
result[10] = mul(0x0D, state[ 8]) ^ mul(0x09, state[ 9]) ^ mul(0x0E, state[10]) ^ mul(0x0B, state[11]);
result[11] = mul(0x0B, state[ 8]) ^ mul(0x0D, state[ 9]) ^ mul(0x09, state[10]) ^ mul(0x0E, state[11]);
result[12] = mul(0x0E, state[12]) ^ mul(0x0B, state[13]) ^ mul(0x0D, state[14]) ^ mul(0x09, state[15]);
result[13] = mul(0x09, state[12]) ^ mul(0x0E, state[13]) ^ mul(0x0B, state[14]) ^ mul(0x0D, state[15]);
result[14] = mul(0x0D, state[12]) ^ mul(0x09, state[13]) ^ mul(0x0E, state[14]) ^ mul(0x0B, state[15]);
result[15] = mul(0x0B, state[12]) ^ mul(0x0D, state[13]) ^ mul(0x09, state[14]) ^ mul(0x0E, state[15]);
memcpy(state, result, 16);
}
/**
* @brief XORs each byte in the state with the corresponding byte in the key.
*
* @param state
* @param roundKey
*/
void addRoundKey(uint8_t *state, uint8_t *roundKey)
{
for (int i = 0; i < 16; i++)
{
state[i] ^= roundKey[i];
}
}
/**
* @brief Encrypt the given 16 byte block with the given 16 byte key using 128 bit AES.
*
* @param block
* @param key
*/
void encrypt_128(uint8_t *key, uint8_t *block)
{
uint8_t state[16];
memcpy(state, block, 16);
int numberOfRounds = 10;
uint8_t expandedKey[176];
KeyExpansion(key, expandedKey);
addRoundKey(state, key);
for (int i = 1; i <= numberOfRounds - 1; i++)
{
subBytes(state);
shiftRows(state);
mixColumns(state);
addRoundKey(state, expandedKey + 16 * i);
}
// final round
subBytes(state);
shiftRows(state);
addRoundKey(state, expandedKey + 16 * (numberOfRounds));
memcpy(block, state, 16);
}
/**
* @brief Decrypt the given 16 byte block with the given 16 byte key using 128 bit AES.
*
* @param block
* @param key
*/
void decrypt_128(uint8_t *key, uint8_t *block)
{
uint8_t state[16];
memcpy(state, block, 16);
int numberOfRounds = 10;
uint8_t expandedKey[176];
KeyExpansion(key, expandedKey);
// final round
addRoundKey(state, expandedKey + 16 * (numberOfRounds));
shiftRows_inverse(state);
subBytes_inverse(state);
for(int i = numberOfRounds - 1; i >= 1; i--){
addRoundKey(state, expandedKey + 16 * i);
mixColumns_inverse(state);
shiftRows_inverse(state);
subBytes_inverse(state);
}
addRoundKey(state, key);
memcpy(block, state, 16);
}
/**
* @brief pads the message by adding bytes.
* @param blockSize The size to pad to.
* @returns The message's new length. If this is greater than capacity,
* Nothing has been done, increase the capacity and run again
*
*/
size_t bytePad(uint8_t blockSize, uint8_t *message, size_t length, size_t capacity) {
// get amount of padding
// this will return the blocksize if the length
// is a multiple of the blocksize, which is desired
uint8_t padSize = blockSize - (length % blockSize);
size_t newLength = length + padSize;
// check capacity
if (newLength > capacity) {
// return needed capacity
return newLength;
}
// pad with zeros and then the number of padded bytes
// ie: fb 8c 0a 00 00 00 04
for(int i = 0; i < padSize - 1; i++){
message[length + i] = 0x00;
}
message[newLength - 1] = (uint8_t)padSize;
// return new length
return newLength;
}
/**
* @brief Removes the padding added by bytePad.
* @returns The new length of the message.
*
* @param blockSize
* @param message
* @param length
* @return size_t
*/
size_t byteUnPad(uint8_t blockSize, uint8_t *message, size_t length) {
uint8_t padSize = message[length - 1];
if (padSize < 0 || padSize > blockSize) {
// invalid block padding
return length;
}
return length - padSize;
// that was simple
}
/**
* @brief
*
* @param key 16 byte key (must be 16 bytes of known data). Will not pad if not long enough. Must be padded already.
* @param message
* @param length
* @param capacity
* @return The new length of the, now encrypted message, if greater than capacity, nothing has been done.
* increase capacity and run again.
*/
size_t encrypt_128_ecb(uint8_t *key, uint8_t *message, size_t length, size_t capacity) {
// pad message
size_t newLength = bytePad(16, message, length, capacity);
// check capacity;
if (newLength > capacity){
// return needed capacity
return newLength;
}
// encrypt the message in blocks
for(size_t i = 0; i < newLength; i += 16){
encrypt_128(key, message + i);
}
}
/**
* @brief decrypts the message using the ecb mode of operation. Assumes that the message's length is a multiple of 16.
*
* @param key
* @param message
* @param length
* @return size_t
*/
size_t decrypt_128_ecb(uint8_t *key, uint8_t *message, size_t length) {
// decrypt the message in bocks
for(size_t i = 0; i < length; i += 16) {
decrypt_128(key, message + i);
}
// un-pad and return new length
return byteUnPad(16, message, length);
}
int main(size_t argc, char **args) {
// get command like arguments
if (argc != 3){
fprintf(stderr, "invalid number of arguments, 2 expected");
return 1;
}
// mode, e for encrypt. d for decrypt
char mode = args[1][0];
if (mode != 'e' && mode != 'd'){
fprintf(stderr, "invalid mode, expected 'e' (encrypt) or 'd' (decrypt)");
return 1;
}
// key in ascii
char key[16];
for(int i = 0; i < 16; i++) {
key[i] = 0x00;
}
size_t keyLength = strlen(args[2]);
memcpy(key, args[2], keyLength < 16 ? keyLength : 16);
if (keyLength < 16){
fprintf(stderr, "key too short. padding with zeros\n");
} else if (keyLength > 16) {
fprintf(stderr, "key too long. extra bytes will be ignored\n");
}
// get message from std in
Buffer input = createBuffer();
// read stdin
int c;
while((c = getchar()) != EOF) {
BufferError err = bufferAppendByte(&input, (char)c);
if (err) {
destroyBuffer(&input);
fprintf(stderr, "failed to allocate memory while reading standard input\n");
return err;
}
}
size_t messageLength = input.size;
// add capacity for padding
BufferError err = bufferSetSize(&input, input.size + 16);
if (err) {
destroyBuffer(&input);
fprintf(stderr, "failed to allocate memory for padding\n");
return err;
};
// encrypt or decrypt depending on the mode
int newLength;
if (mode == 'e') {
// can ignore output as the capacity is definitely enough for padding
newLength = encrypt_128_ecb(key, input.data, messageLength, input.size);
} else {
newLength = decrypt_128_ecb(key, input.data, messageLength);
}
// write the encrypted or decrypted message to standard output
for(size_t i = 0; i < newLength; i++){
printf("%c", input.data[i]);
}
// cleanup and return 0
destroyBuffer(&input);
return 0;
}
void scratch()
{
// uint8_t *message = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog! The quick brown fox jumps over the lazy dog?";
// message that is 32 bytes long(including null terminator)
uint8_t *message = "0123456789abcdef0123456789abcd";
uint8_t *key = "aes rules!!!!!!"; /* key padded with exclamation points to be 16 bytes, including null terminator.*/
size_t messageLength = strlen(message) + 1 /* for null terminator */;
size_t messageCapacity = messageLength + 16 /* for potential extra block from padding */ + 1 /* for post encryption null terminator to print message, since I'm encrypting the null terminator as well. */;
uint8_t *messageBuffer = malloc(messageCapacity);
memcpy(messageBuffer, message, messageLength);
// print message pre-encryption
printf("pre encryption: %s\n", messageBuffer);
// encrypt
size_t encryptedLength = encrypt_128_ecb(key, messageBuffer, messageLength, messageCapacity);
// add null terminator so message can be printed
messageBuffer[encryptedLength] = '\0';
// print message post-encryption
printf("post encryption: %s\n", messageBuffer);
//decrypt
decrypt_128_ecb(key, messageBuffer, encryptedLength);
// print message post decryption
printf("post decryption: %s\n", messageBuffer);
printf("pre encryption length: %d\n", messageLength /* null term */);
printf("post encryption length: %d\n", encryptedLength);
free(messageBuffer);
}