aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/aes/src/box.c
blob: c7e1e9076f7115207e0ccb3c02eefcdb12e07b2c (plain) (blame)
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
675
676
677
678
679
680
681
682
683
/*
 * Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
 * This file is part of the "AES tools" project.
 * For details, see https://github.com/egor-tensin/aes-tools.
 * Distributed under the MIT License.
 */

#include <aes/all.h>

#include <stdlib.h>
#include <string.h>

static const AES_BoxAlgorithmInterface* aes_box_algorithms[] =
{
    &aes_box_algorithm_aes128,
    &aes_box_algorithm_aes192,
    &aes_box_algorithm_aes256,
};

AES_StatusCode aes_box_init(
    AES_Box* box,
    AES_Algorithm algorithm,
    const AES_BoxKey* box_key,
    AES_Mode mode,
    const AES_BoxBlock* iv,
    AES_ErrorDetails* err_details)
{
    AES_StatusCode status = AES_SUCCESS;

    box->algorithm = aes_box_algorithms[algorithm];

    if (aes_is_error(status = box->algorithm->calc_round_keys(
            box_key,
            &box->encryption_keys,
            &box->decryption_keys,
            err_details)))
        return status;

    box->mode = mode;
    if (iv != NULL)
        box->iv = *iv;

    return status;
}

static AES_StatusCode aes_box_encrypt_block_ecb(
    AES_Box* box,
    const AES_BoxBlock* input,
    AES_BoxBlock* output,
    AES_ErrorDetails* err_details)
{
    return box->algorithm->encrypt_block(
        input, &box->encryption_keys, output, err_details);
}

static AES_StatusCode aes_box_encrypt_block_cbc(
    AES_Box* box,
    const AES_BoxBlock* input,
    AES_BoxBlock* output,
    AES_ErrorDetails* err_details)
{
    AES_StatusCode status = AES_SUCCESS;
    AES_BoxBlock xored_input = *input;

    if (aes_is_error(status = box->algorithm->xor_block(
            &xored_input, &box->iv, err_details)))
        return status;

    if (aes_is_error(status = box->algorithm->encrypt_block(
            &xored_input, &box->encryption_keys, output, err_details)))
        return status;

    box->iv = *output;
    return status;
}

static AES_StatusCode aes_box_encrypt_block_cfb(
    AES_Box* box,
    const AES_BoxBlock* input,
    AES_BoxBlock* output,
    AES_ErrorDetails* err_details)
{
    AES_StatusCode status = AES_SUCCESS;

    if (aes_is_error(status = box->algorithm->encrypt_block(
            &box->iv, &box->encryption_keys, output, err_details)))
        return status;

    if (aes_is_error(status = box->algorithm->xor_block(
            output, input, err_details)))
        return status;

    box->iv = *output;
    return status;
}

static AES_StatusCode aes_box_encrypt_block_ofb(
    AES_Box* box,
    const AES_BoxBlock* input,
    AES_BoxBlock* output,
    AES_ErrorDetails* err_details)
{
    AES_StatusCode status = AES_SUCCESS;

    if (aes_is_error(status = box->algorithm->encrypt_block(
            &box->iv, &box->encryption_keys, &box->iv, err_details)))
        return status;

    *output = box->iv;

    if (aes_is_error(status = box->algorithm->xor_block(
            output, input, err_details)))
        return status;

    return status;
}

static AES_StatusCode aes_box_encrypt_block_ctr(
    AES_Box* box,
    const AES_BoxBlock* input,
    AES_BoxBlock* output,
    AES_ErrorDetails* err_details)
{
    AES_StatusCode status = AES_SUCCESS;

    if (aes_is_error(status = box->algorithm->encrypt_block(
            &box->iv, &box->encryption_keys, output, err_details)))
        return status;

    if (aes_is_error(status = box->algorithm->xor_block(
            output, input, err_details)))
        return status;

    if (aes_is_error(status = box->algorithm->inc_block(
            &box->iv, err_details)))
        return status;

    return status;
}

typedef AES_StatusCode (*AES_BoxEncryptBlockInMode)(
    AES_Box*,
    const AES_BoxBlock*,
    AES_BoxBlock*,
    AES_ErrorDetails*);

static AES_BoxEncryptBlockInMode aes_box_encrypt_block_in_mode[] =
{
    &aes_box_encrypt_block_ecb,
    &aes_box_encrypt_block_cbc,
    &aes_box_encrypt_block_cfb,
    &aes_box_encrypt_block_ofb,
    &aes_box_encrypt_block_ctr,
};

AES_StatusCode aes_box_encrypt_block(
    AES_Box* box,
    const AES_BoxBlock* input,
    AES_BoxBlock* output,
    AES_ErrorDetails* err_details)
{
    return aes_box_encrypt_block_in_mode[box->mode](
        box, input, output, err_details);
}

static AES_StatusCode aes_box_decrypt_block_ecb(
    AES_Box* box,
    const AES_BoxBlock* input,
    AES_BoxBlock* output,
    AES_ErrorDetails* err_details)
{
    return box->algorithm->decrypt_block(
        input, &box->decryption_keys, output, err_details);
}

static AES_StatusCode aes_box_decrypt_block_cbc(
    AES_Box* box,
    const AES_BoxBlock* input,
    AES_BoxBlock* output,
    AES_ErrorDetails* err_details)
{
    AES_StatusCode status = AES_SUCCESS;

    if (aes_is_error(status = box->algorithm->decrypt_block(
            input, &box->decryption_keys, output, err_details)))
        return status;

    if (aes_is_error(status = box->algorithm->xor_block(
            output, &box->iv, err_details)))
        return status;

    box->iv = *input;
    return status;
}

static AES_StatusCode aes_box_decrypt_block_cfb(
    AES_Box* box,
    const AES_BoxBlock* input,
    AES_BoxBlock* output,
    AES_ErrorDetails* err_details)
{
    AES_StatusCode status = AES_SUCCESS;

    if (aes_is_error(status = box->algorithm->encrypt_block(
            &box->iv, &box->encryption_keys, output, err_details)))
        return status;

    if (aes_is_error(status = box->algorithm->xor_block(
            output, input, err_details)))
        return status;

    box->iv = *input;
    return status;
}

typedef AES_BoxEncryptBlockInMode AES_BoxDecryptBlockInMode;

static AES_BoxDecryptBlockInMode aes_box_decrypt_block_in_mode[] =
{
    &aes_box_decrypt_block_ecb,
    &aes_box_decrypt_block_cbc,
    &aes_box_decrypt_block_cfb,
    &aes_box_encrypt_block_ofb,
    &aes_box_encrypt_block_ctr,
};

AES_StatusCode aes_box_decrypt_block(
    AES_Box* box,
    const AES_BoxBlock* input,
    AES_BoxBlock* output,
    AES_ErrorDetails* err_details)
{
    return aes_box_decrypt_block_in_mode[box->mode](
        box, input, output, err_details);
}

static AES_StatusCode aes_box_get_encrypted_buffer_size(
    AES_Box* box,
    size_t src_size,
    size_t* dest_size,
    size_t* padding_size,
    AES_ErrorDetails* err_details)
{
    AES_StatusCode status = AES_SUCCESS;

    switch (box->mode)
    {
        case AES_ECB:
        case AES_CBC:
        {
            size_t block_size;

            if (aes_is_error(status = box->algorithm->get_block_size(
                    &block_size, err_details)))
                return status;

            *padding_size = block_size - src_size % block_size;
            *dest_size = src_size + *padding_size;
            return status;
        }

        case AES_CFB:
        case AES_OFB:
        case AES_CTR:
            *dest_size = src_size;
            *padding_size = 0;
            return status;

        default:
            return aes_error_not_implemented(
                err_details, "unsupported mode of operation");
    }
}

static AES_StatusCode aes_box_encrypt_buffer_block(
    AES_Box* box,
    const void* src,
    void* dest,
    AES_ErrorDetails* err_details)
{
    AES_StatusCode status = AES_SUCCESS;

    AES_BoxBlock plaintext;

    if (aes_is_error(status = box->algorithm->load_block(
            &plaintext, src, err_details)))
        return status;

    AES_BoxBlock ciphertext;

    if (aes_is_error(status = aes_box_encrypt_block(
            box, &plaintext, &ciphertext, err_details)))
        return status;

    if (aes_is_error(status = box->algorithm->store_block(
            dest, &ciphertext, err_details)))
        return status;

    return status;
}

static AES_StatusCode aes_box_encrypt_buffer_partial_block_with_padding(
    AES_Box* box,
    const void* src,
    size_t src_size,
    void* dest,
    size_t padding_size,
    AES_ErrorDetails* err_details)
{
    AES_StatusCode status = AES_SUCCESS;

    size_t block_size;

    if (aes_is_error(status = box->algorithm->get_block_size(
            &block_size, err_details)))
        return status;

    void* plaintext_buf = malloc(block_size);

    if (plaintext_buf == NULL)
        return status = aes_error_memory_allocation(err_details);

    memcpy(plaintext_buf, src, src_size);

    if (aes_is_error(status = aes_fill_with_padding(
            AES_PADDING_PKCS7,
            (char*) plaintext_buf + src_size,
            padding_size,
            err_details)))
        goto FREE_PLAINTEXT_BUF;

    if (aes_is_error(status = aes_box_encrypt_buffer_block(
            box, plaintext_buf, dest, err_details)))
        goto FREE_PLAINTEXT_BUF;

FREE_PLAINTEXT_BUF:
    free(plaintext_buf);

    return status;
}

static AES_StatusCode aes_box_encrypt_buffer_partial_block(
    AES_Box* box,
    const void* src,
    size_t src_size,
    void* dest,
    AES_ErrorDetails* err_details)
{
    AES_StatusCode status = AES_SUCCESS;

    if (src_size == 0)
        return status;

    size_t block_size;

    if (aes_is_error(status = box->algorithm->get_block_size(
            &block_size, err_details)))
        return status;

    void* plaintext_buf = malloc(block_size);

    if (plaintext_buf == NULL)
        return status = aes_error_memory_allocation(err_details);

    memset(plaintext_buf, 0x00, block_size);
    memcpy(plaintext_buf, src, src_size);

    void* ciphertext_buf = malloc(block_size);

    if (ciphertext_buf == NULL)
    {
        status = aes_error_memory_allocation(err_details);
        goto FREE_PLAINTEXT_BUF;
    }

    if (aes_is_error(status = aes_box_encrypt_buffer_block(
            box, plaintext_buf, ciphertext_buf, err_details)))
        goto FREE_CIPHERTEXT_BUF;

    memcpy(dest, ciphertext_buf, src_size);

FREE_CIPHERTEXT_BUF:
    free(ciphertext_buf);

FREE_PLAINTEXT_BUF:
    free(plaintext_buf);

    return status;
}

AES_StatusCode aes_box_encrypt_buffer(
    AES_Box* box,
    const void* src,
    size_t src_size,
    void* dest,
    size_t* dest_size,
    AES_ErrorDetails* err_details)
{
    AES_StatusCode status = AES_SUCCESS;

    if (box == NULL)
        return aes_error_null_argument(err_details, "box");
    if (dest_size == NULL)
        return aes_error_null_argument(err_details, "dest_size");

    size_t padding_size = 0;

    if (aes_is_error(status = aes_box_get_encrypted_buffer_size(
            box, src_size, dest_size, &padding_size, err_details)))
        return status;

    if (dest == NULL)
        return AES_SUCCESS;
    if (src == NULL && src_size != 0)
        return aes_error_null_argument(err_details, "src");

    size_t block_size;

    if (aes_is_error(status = box->algorithm->get_block_size(
            &block_size, err_details)))
        return status;

    const size_t src_len = src_size / block_size;

    for (size_t i = 0; i < src_len; ++i)
    {
        if (aes_is_error(status = aes_box_encrypt_buffer_block(
                box, src, dest, err_details)))
            return status;

        src = (char*) src + block_size;
        dest = (char*) dest + block_size;
    }

    if (padding_size == 0)
        return aes_box_encrypt_buffer_partial_block(
            box, src, src_size % block_size, dest, err_details);
    else
        return aes_box_encrypt_buffer_partial_block_with_padding(
            box, src, src_size % block_size, dest, padding_size, err_details);
}

static AES_StatusCode aes_box_get_decrypted_buffer_size(
    AES_Box* box,
    size_t src_size,
    size_t* dest_size,
    size_t* max_padding_size,
    AES_ErrorDetails* err_details)
{
    AES_StatusCode status = AES_SUCCESS;

    switch (box->mode)
    {
        case AES_ECB:
        case AES_CBC:
        {
            size_t block_size;

            if (aes_is_error(status = box->algorithm->get_block_size(
                    &block_size, err_details)))
                return status;

            if (src_size == 0 || src_size % block_size != 0)
                return aes_error_missing_padding(err_details);

            *dest_size = src_size;
            *max_padding_size = block_size;
            return status;
        }

        case AES_CFB:
        case AES_OFB:
        case AES_CTR:
            *dest_size = src_size;
            *max_padding_size = 0;
            return status;

        default:
            return aes_error_not_implemented(
                err_details, "unsupported mode of operation");
    }
}

static AES_StatusCode aes_box_decrypt_buffer_block(
    AES_Box* box,
    const void* src,
    void* dest,
    AES_ErrorDetails* err_details)
{
    AES_StatusCode status = AES_SUCCESS;

    AES_BoxBlock ciphertext;

    if (aes_is_error(status = box->algorithm->load_block(
            &ciphertext, src, err_details)))
        return status;

    AES_BoxBlock plaintext;

    if (aes_is_error(status = aes_box_decrypt_block(
            box, &ciphertext, &plaintext, err_details)))
        return status;

    if (aes_is_error(status = box->algorithm->store_block(
            dest, &plaintext, err_details)))
        return status;

    return status;
}

static AES_StatusCode aes_box_decrypt_buffer_partial_block(
    AES_Box* box,
    const void* src,
    size_t src_size,
    void* dest,
    AES_ErrorDetails* err_details)
{
    AES_StatusCode status = AES_SUCCESS;

    if (src_size == 0)
        return status;

    size_t block_size;

    if (aes_is_error(status = box->algorithm->get_block_size(
            &block_size, err_details)))
        return status;

    void* ciphertext_buf = malloc(block_size);

    if (ciphertext_buf == NULL)
        return status = aes_error_memory_allocation(err_details);

    memset(ciphertext_buf, 0x00, block_size);
    memcpy(ciphertext_buf, src, src_size);

    void* plaintext_buf = malloc(block_size);

    if (plaintext_buf == NULL)
    {
        status = aes_error_memory_allocation(err_details);
        goto FREE_CIPHERTEXT_BUF;
    }

    if (aes_is_error(status = aes_box_decrypt_buffer_block(
            box, ciphertext_buf, plaintext_buf, err_details)))
        goto FREE_PLAINTEXT_BUF;

    memcpy(dest, plaintext_buf, src_size);

FREE_PLAINTEXT_BUF:
    free(plaintext_buf);

FREE_CIPHERTEXT_BUF:
    free(ciphertext_buf);

    return status;
}

AES_StatusCode aes_box_decrypt_buffer(
    AES_Box* box,
    const void* src,
    size_t src_size,
    void* dest,
    size_t* dest_size,
    AES_ErrorDetails* err_details)
{
    if (box == NULL)
        return aes_error_null_argument(err_details, "box");
    if (dest_size == NULL)
        return aes_error_null_argument(err_details, "dest_size");

    AES_StatusCode status = AES_SUCCESS;
    size_t max_padding_size = 0;

    if (aes_is_error(status = aes_box_get_decrypted_buffer_size(
            box, src_size, dest_size, &max_padding_size, err_details)))
        return status;

    if (dest == NULL)
        return AES_SUCCESS;
    if (src == NULL)
        return aes_error_null_argument(err_details, "src");

    size_t block_size;

    if (aes_is_error(status = box->algorithm->get_block_size(
            &block_size, err_details)))
        return status;

    const size_t src_len = src_size / block_size;

    for (size_t i = 0; i < src_len; ++i)
    {
        if (aes_is_error(status = aes_box_decrypt_buffer_block(
                box, src, dest, err_details)))
            return status;

        src = (char*) src + block_size;
        dest = (char*) dest + block_size;
    }

    if (max_padding_size == 0)
    {
        return aes_box_decrypt_buffer_partial_block(
            box, src, src_size % block_size, dest, err_details);
    }
    else
    {
        size_t padding_size;

        if (aes_is_error(status = aes_extract_padding_size(
                AES_PADDING_PKCS7,
                (char*) dest - block_size,
                block_size,
                &padding_size,
                err_details)))
            return status;

        *dest_size -= padding_size;
        return status;
    }
}

AES_StatusCode aes_box_parse_block(
    AES_BoxBlock* dest,
    AES_Algorithm algorithm,
    const char* src,
    AES_ErrorDetails* err_details)
{
    if (dest == NULL)
        return aes_error_null_argument(err_details, "dest");
    if (src == NULL)
        return aes_error_null_argument(err_details, "src");

    return aes_box_algorithms[algorithm]->parse_block(
        dest, src, err_details);
}

AES_StatusCode aes_box_parse_key(
    AES_BoxKey* dest,
    AES_Algorithm algorithm,
    const char* src,
    AES_ErrorDetails* err_details)
{
    if (dest == NULL)
        return aes_error_null_argument(err_details, "dest");
    if (src == NULL)
        return aes_error_null_argument(err_details, "src");

    return aes_box_algorithms[algorithm]->parse_key(
        dest, src, err_details);
}

AES_StatusCode aes_box_format_block(
    AES_BoxBlockString* dest,
    AES_Algorithm algorithm,
    const AES_BoxBlock* src,
    AES_ErrorDetails* err_details)
{
    if (dest == NULL)
        return aes_error_null_argument(err_details, "dest");
    if (src == NULL)
        return aes_error_null_argument(err_details, "src");

    return aes_box_algorithms[algorithm]->format_block(
        dest, src, err_details);
}

AES_StatusCode aes_box_format_key(
    AES_BoxKeyString* dest,
    AES_Algorithm algorithm,
    const AES_BoxKey* src,
    AES_ErrorDetails* err_details)
{
    if (dest == NULL)
        return aes_error_null_argument(err_details, "dest");
    if (src == NULL)
        return aes_error_null_argument(err_details, "src");

    return aes_box_algorithms[algorithm]->format_key(
        dest, src, err_details);
}