1
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
50
51
52
53
54
55
56
57
59
61
62
63
64
65
66
67
68
69
70
71
72
73
76
77
78
79
80
81
82
83
84
85
86
98
99
100
101
102
103
104
107
108
113
114
115
116
117
118
119
120
121
122
123
124
131
132
133
134
135
140
141
142
143
146
147
148
149
150
151
152
153
157
158
159
160
161
162
163
164
165
166
167
168
174
175
178
179
180
181
182
183
184
185
186
189
190
191
192
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
223
224
226
227
230
233
234
235
236
237
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
260
261
262
263
264
265
266
267
268
269
270
271
272
273
276
277
278
281
282
283
284
287
288
289
293
294
295
296
297
298
299
300
310
312
313
314
315
316
317
318
319
320
324
325
326
327
328
331
338
339
343
344
345
346
347
348
349
350
351
352
353
354
355
358
359
366
367
368
371
372
382
383
384
385
388
391
392
397
398
399
404
405
406
410
411
412
413
414
415
416
417
420
421
422
428
429
430
431
432
433
434
435
436
437
438
439
440
443
444
445
452
453
454
459
460
461
462
463
464
465
466
467
468
469
470
471
472
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
498
499
500
507
508
509
510
513
514
515
518
519
520
521
525
526
527
528
529
530
536
537
538
544
545
549
550
551
552
553
554
557
558
559
560
561
562
563
564
565
566
567
568
569
570
573
574
575
576
577
578
579
584
585
586
587
588
589
590
591
592
593
596
597
601
602
607
608
609
610
611
612
613
614
615
618
627
628
629
630
631
632
633
634
640
641
642
643
644
645
646
647
648
649
654
655
656
657
658
659
660
661
662
663
668
669
670
671
672
673
674
675
676
677
681
682
683
684
687
688
689
690
691
692
693
694
695
696
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
724
725
726
727
728
729
730
731
732
735
736
737
738
739
740
741
742
743
745
746
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
768
769
770
771
772
773
774
775
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
812
813
814
815
816
817
818
819
820
821
824
825
826
827
828
829
830
831
832
833
834
838
839
840
841
842
843
844
845
846
847
856
857
858
859
860
861
862
863
866
867
868
874
875
876
877
878
879
885
886
887
888
891
892
893
894
895
896
897
898
899
900
901
902
903
904
907
908
909
916
917
918
919
920
921
922
923
924
925
926
927
928
932
933
940
941
942
943
944
945
968
969
970
977
978
979
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1004
1005
1006
1007
1010
1013
1014
1015
1016
1017
1018
1019
1020
1026
1032
1038
1043
1048
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1065
1066
1069
1070
1071
1078
1079
1080
1085
1086
1087
1088
1089
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1112
1113
1116
1117
1118
1125
1126
1130
1131
1132
1133
1134
1137
1138
1143
1144
1145
1149
1150
1151
1152
1153
1154
1155
1158
1159
1160
1161
1164
1165
1166
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1203
1204
1205
1206
1207
1208
1209
1210
1221
1222
1226
1229
1230
1236
1237
1238
1239
1240
1243
1244
1245
1246
1247
1250
1251
1252
1253
1254
1255
1256
1259
1260
1263
1264
1268
1269
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1298
1299
1300
1301
1302
1303
1305
1308
1309
1310
1311
1312
1313
1314
1315
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1375
1376
1377
1378
1379
1380
1382
1385
1386
1387
1388
1389
1390
1391
1392
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1443
1444
1445
1446
1447
1448
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1538
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1595
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1616
1617
1618
1619
1620
1621
1622
/* ... */
#include "common.h"
#if defined(MBEDTLS_CIPHER_C)
#include "mbedtls/cipher.h"
#include "mbedtls/cipher_internal.h"
#include "mbedtls/platform_util.h"
#include "mbedtls/error.h"
#include "mbedtls/constant_time.h"
#include "constant_time_internal.h"
#include <stdlib.h>
#include <string.h>
8 includes
#if defined(MBEDTLS_CHACHAPOLY_C)
#include "mbedtls/chachapoly.h"
#endif
#if defined(MBEDTLS_GCM_C)
#include "mbedtls/gcm.h"
#endif
#if defined(MBEDTLS_CCM_C)
#include "mbedtls/ccm.h"
#endif
#if defined(MBEDTLS_CHACHA20_C)
#include "mbedtls/chacha20.h"
#endif
#if defined(MBEDTLS_CMAC_C)
#include "mbedtls/cmac.h"
#endif
#if defined(MBEDTLS_USE_PSA_CRYPTO)
#include "psa/crypto.h"
#include "mbedtls/psa_util.h"
/* ... */#endif
#if defined(MBEDTLS_NIST_KW_C)
#include "mbedtls/nist_kw.h"
#endif
#include "mbedtls/platform.h"
#define CIPHER_VALIDATE_RET(cond) \
MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA)...
#define CIPHER_VALIDATE(cond) \
MBEDTLS_INTERNAL_VALIDATE(cond)...
static int supported_init = 0;
const int *mbedtls_cipher_list(void)
{
const mbedtls_cipher_definition_t *def;
int *type;
if (!supported_init) {
def = mbedtls_cipher_definitions;
type = mbedtls_cipher_supported;
while (def->type != 0) {
*type++ = (*def++).type;
}while (def->type != 0) { ... }
*type = 0;
supported_init = 1;
}if (!supported_init) { ... }
return mbedtls_cipher_supported;
}{ ... }
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
const mbedtls_cipher_type_t cipher_type)
{
const mbedtls_cipher_definition_t *def;
for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
if (def->type == cipher_type) {
return def->info;
}if (def->type == cipher_type) { ... }
}for (def = mbedtls_cipher_definitions; def->info != NULL; def++) { ... }
return NULL;
}{ ... }
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
const char *cipher_name)
{
const mbedtls_cipher_definition_t *def;
if (NULL == cipher_name) {
return NULL;
}if (NULL == cipher_name) { ... }
for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
if (!strcmp(def->info->name, cipher_name)) {
return def->info;
}if (!strcmp(def->info->name, cipher_name)) { ... }
}for (def = mbedtls_cipher_definitions; def->info != NULL; def++) { ... }
return NULL;
}{ ... }
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
const mbedtls_cipher_id_t cipher_id,
int key_bitlen,
const mbedtls_cipher_mode_t mode)
{
const mbedtls_cipher_definition_t *def;
for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
if (def->info->base->cipher == cipher_id &&
def->info->key_bitlen == (unsigned) key_bitlen &&
def->info->mode == mode) {
return def->info;
}if (def->info->base->cipher == cipher_id && def->info->key_bitlen == (unsigned) key_bitlen && def->info->mode == mode) { ... }
}for (def = mbedtls_cipher_definitions; def->info != NULL; def++) { ... }
return NULL;
}{ ... }
void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
{
CIPHER_VALIDATE(ctx != NULL);
memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
}{ ... }
void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
{
if (ctx == NULL) {
return;
}if (ctx == NULL) { ... }
#if defined(MBEDTLS_USE_PSA_CRYPTO)
if (ctx->psa_enabled == 1) {
if (ctx->cipher_ctx != NULL) {
mbedtls_cipher_context_psa * const cipher_psa =
(mbedtls_cipher_context_psa *) ctx->cipher_ctx;
if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
(void) psa_destroy_key(cipher_psa->slot);
}if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) { ... }
mbedtls_platform_zeroize(cipher_psa, sizeof(*cipher_psa));
mbedtls_free(cipher_psa);
}if (ctx->cipher_ctx != NULL) { ... }
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
return;
}if (ctx->psa_enabled == 1) { ... }
/* ... */#endif
#if defined(MBEDTLS_CMAC_C)
if (ctx->cmac_ctx) {
mbedtls_platform_zeroize(ctx->cmac_ctx,
sizeof(mbedtls_cmac_context_t));
mbedtls_free(ctx->cmac_ctx);
}if (ctx->cmac_ctx) { ... }
/* ... */#endif
if (ctx->cipher_ctx) {
ctx->cipher_info->base->ctx_free_func(ctx->cipher_ctx);
}if (ctx->cipher_ctx) { ... }
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
}{ ... }
int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
const mbedtls_cipher_info_t *cipher_info)
{
CIPHER_VALIDATE_RET(ctx != NULL);
if (cipher_info == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (cipher_info == NULL) { ... }
memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
if (NULL == (ctx->cipher_ctx = cipher_info->base->ctx_alloc_func())) {
return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
}if (NULL == (ctx->cipher_ctx = cipher_info->base->ctx_alloc_func())) { ... }
ctx->cipher_info = cipher_info;
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
/* ... */
#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
(void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7);
#else
(void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);
#endif/* ... */
#endif
return 0;
}{ ... }
#if defined(MBEDTLS_USE_PSA_CRYPTO)
int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
const mbedtls_cipher_info_t *cipher_info,
size_t taglen)
{
psa_algorithm_t alg;
mbedtls_cipher_context_psa *cipher_psa;
if (NULL == cipher_info || NULL == ctx) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (NULL == cipher_info || NULL == ctx) { ... }
/* ... */
alg = mbedtls_psa_translate_cipher_mode(cipher_info->mode, taglen);
if (alg == 0) {
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}if (alg == 0) { ... }
if (mbedtls_psa_translate_cipher_type(cipher_info->type) == 0) {
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}if (mbedtls_psa_translate_cipher_type(cipher_info->type) == 0) { ... }
memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
if (cipher_psa == NULL) {
return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
}if (cipher_psa == NULL) { ... }
cipher_psa->alg = alg;
ctx->cipher_ctx = cipher_psa;
ctx->cipher_info = cipher_info;
ctx->psa_enabled = 1;
return 0;
}mbedtls_cipher_setup_psa (mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info, size_t taglen) { ... }
/* ... */#endif
int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
const unsigned char *key,
int key_bitlen,
const mbedtls_operation_t operation)
{
CIPHER_VALIDATE_RET(ctx != NULL);
CIPHER_VALIDATE_RET(key != NULL);
CIPHER_VALIDATE_RET(operation == MBEDTLS_ENCRYPT ||
operation == MBEDTLS_DECRYPT);
if (ctx->cipher_info == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (ctx->cipher_info == NULL) { ... }
#if defined(MBEDTLS_USE_PSA_CRYPTO)
if (ctx->psa_enabled == 1) {
mbedtls_cipher_context_psa * const cipher_psa =
(mbedtls_cipher_context_psa *) ctx->cipher_ctx;
size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
psa_status_t status;
psa_key_type_t key_type;
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
if (key_bitlen % 8 != 0) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (key_bitlen % 8 != 0) { ... }
if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) { ... }
key_type = mbedtls_psa_translate_cipher_type(
ctx->cipher_info->type);
if (key_type == 0) {
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}if (key_type == 0) { ... }
psa_set_key_type(&attributes, key_type);
/* ... */
psa_set_key_usage_flags(&attributes,
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
psa_set_key_algorithm(&attributes, cipher_psa->alg);
status = psa_import_key(&attributes, key, key_bytelen,
&cipher_psa->slot);
switch (status) {
case PSA_SUCCESS:
break;case PSA_SUCCESS:
case PSA_ERROR_INSUFFICIENT_MEMORY:
return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;case PSA_ERROR_INSUFFICIENT_MEMORY:
case PSA_ERROR_NOT_SUPPORTED:
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;case PSA_ERROR_NOT_SUPPORTED:
default:
return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;default
}switch (status) { ... }
/* ... */
cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
ctx->key_bitlen = key_bitlen;
ctx->operation = operation;
return 0;
}if (ctx->psa_enabled == 1) { ... }
/* ... */#endif
if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
(int) ctx->cipher_info->key_bitlen != key_bitlen) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 && (int) ctx->cipher_info->key_bitlen != key_bitlen) { ... }
ctx->key_bitlen = key_bitlen;
ctx->operation = operation;
/* ... */
if (MBEDTLS_ENCRYPT == operation ||
MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
MBEDTLS_MODE_CTR == ctx->cipher_info->mode) {
return ctx->cipher_info->base->setkey_enc_func(ctx->cipher_ctx, key,
ctx->key_bitlen);
}if (MBEDTLS_ENCRYPT == operation || MBEDTLS_MODE_CFB == ctx->cipher_info->mode || MBEDTLS_MODE_OFB == ctx->cipher_info->mode || MBEDTLS_MODE_CTR == ctx->cipher_info->mode) { ... }
if (MBEDTLS_DECRYPT == operation) {
return ctx->cipher_info->base->setkey_dec_func(ctx->cipher_ctx, key,
ctx->key_bitlen);
}if (MBEDTLS_DECRYPT == operation) { ... }
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{ ... }
int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
const unsigned char *iv,
size_t iv_len)
{
size_t actual_iv_size;
CIPHER_VALIDATE_RET(ctx != NULL);
CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
if (ctx->cipher_info == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (ctx->cipher_info == NULL) { ... }
#if defined(MBEDTLS_USE_PSA_CRYPTO)
if (ctx->psa_enabled == 1) {
/* ... */
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}if (ctx->psa_enabled == 1) { ... }
/* ... */#endif
if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}if (iv_len > MBEDTLS_MAX_IV_LENGTH) { ... }
if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
actual_iv_size = iv_len;
}if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) { ... } else {
actual_iv_size = ctx->cipher_info->iv_size;
if (actual_iv_size > iv_len) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (actual_iv_size > iv_len) { ... }
}else { ... }
#if defined(MBEDTLS_CHACHA20_C)
if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20) {
/* ... */
if (iv_len != 12) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (iv_len != 12) { ... }
if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
iv,
0U)) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx, iv, 0U)) { ... }
}if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20) { ... }
#if defined(MBEDTLS_CHACHAPOLY_C)
if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
iv_len != 12) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 && iv_len != 12) { ... }
/* ... */#endif/* ... */
#endif
if (actual_iv_size != 0) {
memcpy(ctx->iv, iv, actual_iv_size);
ctx->iv_size = actual_iv_size;
}if (actual_iv_size != 0) { ... }
return 0;
}{ ... }
int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
{
CIPHER_VALIDATE_RET(ctx != NULL);
if (ctx->cipher_info == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (ctx->cipher_info == NULL) { ... }
#if defined(MBEDTLS_USE_PSA_CRYPTO)
if (ctx->psa_enabled == 1) {
/* ... */
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}if (ctx->psa_enabled == 1) { ... }
/* ... */#endif
ctx->unprocessed_len = 0;
return 0;
}{ ... }
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
const unsigned char *ad, size_t ad_len)
{
CIPHER_VALIDATE_RET(ctx != NULL);
CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
if (ctx->cipher_info == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (ctx->cipher_info == NULL) { ... }
#if defined(MBEDTLS_USE_PSA_CRYPTO)
if (ctx->psa_enabled == 1) {
/* ... */
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}if (ctx->psa_enabled == 1) { ... }
/* ... */#endif
#if defined(MBEDTLS_GCM_C)
if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
ctx->iv, ctx->iv_size, ad, ad_len);
}if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) { ... }
/* ... */#endif
#if defined(MBEDTLS_CHACHAPOLY_C)
if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
int result;
mbedtls_chachapoly_mode_t mode;
mode = (ctx->operation == MBEDTLS_ENCRYPT)
? MBEDTLS_CHACHAPOLY_ENCRYPT
: MBEDTLS_CHACHAPOLY_DECRYPT;
result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
ctx->iv,
mode);
if (result != 0) {
return result;
}if (result != 0) { ... }
return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
ad, ad_len);
}if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) { ... }
/* ... */#endif
return 0;
}{ ... }
/* ... */#endif
int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
size_t ilen, unsigned char *output, size_t *olen)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t block_size;
CIPHER_VALIDATE_RET(ctx != NULL);
CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
CIPHER_VALIDATE_RET(output != NULL);
CIPHER_VALIDATE_RET(olen != NULL);
if (ctx->cipher_info == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (ctx->cipher_info == NULL) { ... }
#if defined(MBEDTLS_USE_PSA_CRYPTO)
if (ctx->psa_enabled == 1) {
/* ... */
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}if (ctx->psa_enabled == 1) { ... }
/* ... */#endif
*olen = 0;
block_size = mbedtls_cipher_get_block_size(ctx);
if (0 == block_size) {
return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
}if (0 == block_size) { ... }
if (ctx->cipher_info->mode == MBEDTLS_MODE_ECB) {
if (ilen != block_size) {
return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
}if (ilen != block_size) { ... }
*olen = ilen;
if (0 != (ret = ctx->cipher_info->base->ecb_func(ctx->cipher_ctx,
ctx->operation, input, output))) {
return ret;
}if (0 != (ret = ctx->cipher_info->base->ecb_func(ctx->cipher_ctx, ctx->operation, input, output))) { ... }
return 0;
}if (ctx->cipher_info->mode == MBEDTLS_MODE_ECB) { ... }
#if defined(MBEDTLS_GCM_C)
if (ctx->cipher_info->mode == MBEDTLS_MODE_GCM) {
*olen = ilen;
return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
output);
}if (ctx->cipher_info->mode == MBEDTLS_MODE_GCM) { ... }
/* ... */#endif
#if defined(MBEDTLS_CHACHAPOLY_C)
if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
*olen = ilen;
return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
ilen, input, output);
}if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) { ... }
/* ... */#endif
if (input == output &&
(ctx->unprocessed_len != 0 || ilen % block_size)) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (input == output && (ctx->unprocessed_len != 0 || ilen % block_size)) { ... }
#if defined(MBEDTLS_CIPHER_MODE_CBC)
if (ctx->cipher_info->mode == MBEDTLS_MODE_CBC) {
size_t copy_len = 0;
/* ... */
if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
ilen <= block_size - ctx->unprocessed_len) ||
(ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
ilen < block_size - ctx->unprocessed_len) ||
(ctx->operation == MBEDTLS_ENCRYPT &&
ilen < block_size - ctx->unprocessed_len)) {
memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
ilen);
ctx->unprocessed_len += ilen;
return 0;
}if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding && ilen <= block_size - ctx->unprocessed_len) || (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding && ilen < block_size - ctx->unprocessed_len) || (ctx->operation == MBEDTLS_ENCRYPT && ilen < block_size - ctx->unprocessed_len)) { ... }
/* ... */
if (0 != ctx->unprocessed_len) {
copy_len = block_size - ctx->unprocessed_len;
memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
copy_len);
if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
ctx->operation, block_size, ctx->iv,
ctx->unprocessed_data, output))) {
return ret;
}if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx, ctx->operation, block_size, ctx->iv, ctx->unprocessed_data, output))) { ... }
*olen += block_size;
output += block_size;
ctx->unprocessed_len = 0;
input += copy_len;
ilen -= copy_len;
}if (0 != ctx->unprocessed_len) { ... }
/* ... */
if (0 != ilen) {
/* ... */
copy_len = ilen % block_size;
if (copy_len == 0 &&
ctx->operation == MBEDTLS_DECRYPT &&
NULL != ctx->add_padding) {
copy_len = block_size;
}if (copy_len == 0 && ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding) { ... }
memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
copy_len);
ctx->unprocessed_len += copy_len;
ilen -= copy_len;
}if (0 != ilen) { ... }
/* ... */
if (ilen) {
if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
ctx->operation, ilen, ctx->iv, input,
output))) {
return ret;
}if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx, ctx->operation, ilen, ctx->iv, input, output))) { ... }
*olen += ilen;
}if (ilen) { ... }
return 0;
}if (ctx->cipher_info->mode == MBEDTLS_MODE_CBC) { ... }
/* ... */#endif
#if defined(MBEDTLS_CIPHER_MODE_CFB)
if (ctx->cipher_info->mode == MBEDTLS_MODE_CFB) {
if (0 != (ret = ctx->cipher_info->base->cfb_func(ctx->cipher_ctx,
ctx->operation, ilen,
&ctx->unprocessed_len, ctx->iv,
input, output))) {
return ret;
}if (0 != (ret = ctx->cipher_info->base->cfb_func(ctx->cipher_ctx, ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv, input, output))) { ... }
*olen = ilen;
return 0;
}if (ctx->cipher_info->mode == MBEDTLS_MODE_CFB) { ... }
/* ... */#endif
#if defined(MBEDTLS_CIPHER_MODE_OFB)
if (ctx->cipher_info->mode == MBEDTLS_MODE_OFB) {
if (0 != (ret = ctx->cipher_info->base->ofb_func(ctx->cipher_ctx,
ilen, &ctx->unprocessed_len, ctx->iv,
input, output))) {
return ret;
}if (0 != (ret = ctx->cipher_info->base->ofb_func(ctx->cipher_ctx, ilen, &ctx->unprocessed_len, ctx->iv, input, output))) { ... }
*olen = ilen;
return 0;
}if (ctx->cipher_info->mode == MBEDTLS_MODE_OFB) { ... }
/* ... */#endif
#if defined(MBEDTLS_CIPHER_MODE_CTR)
if (ctx->cipher_info->mode == MBEDTLS_MODE_CTR) {
if (0 != (ret = ctx->cipher_info->base->ctr_func(ctx->cipher_ctx,
ilen, &ctx->unprocessed_len, ctx->iv,
ctx->unprocessed_data, input, output))) {
return ret;
}if (0 != (ret = ctx->cipher_info->base->ctr_func(ctx->cipher_ctx, ilen, &ctx->unprocessed_len, ctx->iv, ctx->unprocessed_data, input, output))) { ... }
*olen = ilen;
return 0;
}if (ctx->cipher_info->mode == MBEDTLS_MODE_CTR) { ... }
/* ... */#endif
#if defined(MBEDTLS_CIPHER_MODE_XTS)
if (ctx->cipher_info->mode == MBEDTLS_MODE_XTS) {
if (ctx->unprocessed_len > 0) {
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}if (ctx->unprocessed_len > 0) { ... }
ret = ctx->cipher_info->base->xts_func(ctx->cipher_ctx,
ctx->operation, ilen, ctx->iv, input, output);
if (ret != 0) {
return ret;
}if (ret != 0) { ... }
*olen = ilen;
return 0;
}if (ctx->cipher_info->mode == MBEDTLS_MODE_XTS) { ... }
/* ... */#endif
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
if (ctx->cipher_info->mode == MBEDTLS_MODE_STREAM) {
if (0 != (ret = ctx->cipher_info->base->stream_func(ctx->cipher_ctx,
ilen, input, output))) {
return ret;
}if (0 != (ret = ctx->cipher_info->base->stream_func(ctx->cipher_ctx, ilen, input, output))) { ... }
*olen = ilen;
return 0;
}if (ctx->cipher_info->mode == MBEDTLS_MODE_STREAM) { ... }
/* ... */#endif
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{ ... }
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
/* ... */
static void add_pkcs_padding(unsigned char *output, size_t output_len,
size_t data_len)
{
size_t padding_len = output_len - data_len;
unsigned char i;
for (i = 0; i < padding_len; i++) {
output[data_len + i] = (unsigned char) padding_len;
}for (i = 0; i < padding_len; i++) { ... }
}add_pkcs_padding (unsigned char *output, size_t output_len, size_t data_len) { ... }
static int get_pkcs_padding(unsigned char *input, size_t input_len,
size_t *data_len)
{
size_t i, pad_idx;
unsigned char padding_len, bad = 0;
if (NULL == input || NULL == data_len) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (NULL == input || NULL == data_len) { ... }
padding_len = input[input_len - 1];
*data_len = input_len - padding_len;
bad |= ~mbedtls_ct_size_mask_ge(input_len, padding_len);
bad |= mbedtls_ct_size_bool_eq(padding_len, 0);
/* ... */
pad_idx = input_len - padding_len;
for (i = 0; i < input_len; i++) {
size_t mask = mbedtls_ct_size_mask_ge(i, pad_idx);
bad |= (input[i] ^ padding_len) & mask;
}for (i = 0; i < input_len; i++) { ... }
return -(int) mbedtls_ct_uint_if(bad, -MBEDTLS_ERR_CIPHER_INVALID_PADDING, 0);
}get_pkcs_padding (unsigned char *input, size_t input_len, size_t *data_len) { ... }
/* ... */#endif
#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
/* ... */
static void add_one_and_zeros_padding(unsigned char *output,
size_t output_len, size_t data_len)
{
size_t padding_len = output_len - data_len;
unsigned char i = 0;
output[data_len] = 0x80;
for (i = 1; i < padding_len; i++) {
output[data_len + i] = 0x00;
}for (i = 1; i < padding_len; i++) { ... }
}add_one_and_zeros_padding (unsigned char *output, size_t output_len, size_t data_len) { ... }
static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
size_t *data_len)
{
unsigned int bad = 1;
if (NULL == input || NULL == data_len) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (NULL == input || NULL == data_len) { ... }
*data_len = 0;
size_t in_padding = ~0;
for (ptrdiff_t i = (ptrdiff_t) (input_len) - 1; i >= 0; i--) {
size_t is_nonzero = mbedtls_ct_uint_mask(input[i]);
size_t hit_first_nonzero = is_nonzero & in_padding;
*data_len = (*data_len & ~hit_first_nonzero) | ((size_t) i & hit_first_nonzero);
bad = mbedtls_ct_uint_if((unsigned int) hit_first_nonzero,
!mbedtls_ct_size_bool_eq(input[i], 0x80), bad);
in_padding = in_padding & ~is_nonzero;
}for (ptrdiff_t i = (ptrdiff_t) (input_len) - 1; i >= 0; i--) { ... }
return -(int) mbedtls_ct_uint_if(bad, -MBEDTLS_ERR_CIPHER_INVALID_PADDING, 0);
}get_one_and_zeros_padding (unsigned char *input, size_t input_len, size_t *data_len) { ... }
/* ... */#endif
#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
/* ... */
static void add_zeros_and_len_padding(unsigned char *output,
size_t output_len, size_t data_len)
{
size_t padding_len = output_len - data_len;
unsigned char i = 0;
for (i = 1; i < padding_len; i++) {
output[data_len + i - 1] = 0x00;
}for (i = 1; i < padding_len; i++) { ... }
output[output_len - 1] = (unsigned char) padding_len;
}add_zeros_and_len_padding (unsigned char *output, size_t output_len, size_t data_len) { ... }
static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
size_t *data_len)
{
size_t i, pad_idx;
unsigned char padding_len, bad = 0;
if (NULL == input || NULL == data_len) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (NULL == input || NULL == data_len) { ... }
padding_len = input[input_len - 1];
*data_len = input_len - padding_len;
bad |= mbedtls_ct_size_mask_ge(padding_len, input_len + 1);
bad |= mbedtls_ct_size_bool_eq(padding_len, 0);
pad_idx = input_len - padding_len;
for (i = 0; i < input_len - 1; i++) {
size_t mask = mbedtls_ct_size_mask_ge(i, pad_idx);
bad |= input[i] & mask;
}for (i = 0; i < input_len - 1; i++) { ... }
return -(int) mbedtls_ct_uint_if(bad, -MBEDTLS_ERR_CIPHER_INVALID_PADDING, 0);
}get_zeros_and_len_padding (unsigned char *input, size_t input_len, size_t *data_len) { ... }
/* ... */#endif
#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
/* ... */
static void add_zeros_padding(unsigned char *output,
size_t output_len, size_t data_len)
{
size_t i;
for (i = data_len; i < output_len; i++) {
output[i] = 0x00;
}for (i = data_len; i < output_len; i++) { ... }
}add_zeros_padding (unsigned char *output, size_t output_len, size_t data_len) { ... }
static int get_zeros_padding(unsigned char *input, size_t input_len,
size_t *data_len)
{
size_t i;
unsigned char done = 0, prev_done;
if (NULL == input || NULL == data_len) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (NULL == input || NULL == data_len) { ... }
*data_len = 0;
for (i = input_len; i > 0; i--) {
prev_done = done;
done |= !mbedtls_ct_size_bool_eq(input[i-1], 0);
size_t mask = mbedtls_ct_size_mask(done ^ prev_done);
*data_len |= i & mask;
}for (i = input_len; i > 0; i--) { ... }
return 0;
}get_zeros_padding (unsigned char *input, size_t input_len, size_t *data_len) { ... }
/* ... */#endif
/* ... */
static int get_no_padding(unsigned char *input, size_t input_len,
size_t *data_len)
{
if (NULL == input || NULL == data_len) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (NULL == input || NULL == data_len) { ... }
*data_len = input_len;
return 0;
}{ ... }
#endif/* ... */
int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
unsigned char *output, size_t *olen)
{
CIPHER_VALIDATE_RET(ctx != NULL);
CIPHER_VALIDATE_RET(output != NULL);
CIPHER_VALIDATE_RET(olen != NULL);
if (ctx->cipher_info == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (ctx->cipher_info == NULL) { ... }
#if defined(MBEDTLS_USE_PSA_CRYPTO)
if (ctx->psa_enabled == 1) {
/* ... */
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}if (ctx->psa_enabled == 1) { ... }
/* ... */#endif
*olen = 0;
if (MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) {
return 0;
}if (MBEDTLS_MODE_CFB == ctx->cipher_info->mode || MBEDTLS_MODE_OFB == ctx->cipher_info->mode || MBEDTLS_MODE_CTR == ctx->cipher_info->mode || MBEDTLS_MODE_GCM == ctx->cipher_info->mode || MBEDTLS_MODE_XTS == ctx->cipher_info->mode || MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) { ... }
if ((MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type) ||
(MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) {
return 0;
}if ((MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type) || (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) { ... }
if (MBEDTLS_MODE_ECB == ctx->cipher_info->mode) {
if (ctx->unprocessed_len != 0) {
return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
}if (ctx->unprocessed_len != 0) { ... }
return 0;
}if (MBEDTLS_MODE_ECB == ctx->cipher_info->mode) { ... }
#if defined(MBEDTLS_CIPHER_MODE_CBC)
if (MBEDTLS_MODE_CBC == ctx->cipher_info->mode) {
int ret = 0;
if (MBEDTLS_ENCRYPT == ctx->operation) {
if (NULL == ctx->add_padding) {
if (0 != ctx->unprocessed_len) {
return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
}if (0 != ctx->unprocessed_len) { ... }
return 0;
}if (NULL == ctx->add_padding) { ... }
ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
ctx->unprocessed_len);
}if (MBEDTLS_ENCRYPT == ctx->operation) { ... } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
/* ... */
if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
return 0;
}if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) { ... }
return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
}else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) { ... }
if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
ctx->operation,
mbedtls_cipher_get_block_size(ctx),
ctx->iv,
ctx->unprocessed_data, output))) {
return ret;
}if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx, ctx->operation, mbedtls_cipher_get_block_size(ctx), ctx->iv, ctx->unprocessed_data, output))) { ... }
if (MBEDTLS_DECRYPT == ctx->operation) {
return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
olen);
}if (MBEDTLS_DECRYPT == ctx->operation) { ... }
*olen = mbedtls_cipher_get_block_size(ctx);
return 0;
}if (MBEDTLS_MODE_CBC == ctx->cipher_info->mode) { ... }
/* ... */#else
((void) output);
#endif
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{ ... }
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
mbedtls_cipher_padding_t mode)
{
CIPHER_VALIDATE_RET(ctx != NULL);
if (NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode) { ... }
#if defined(MBEDTLS_USE_PSA_CRYPTO)
if (ctx->psa_enabled == 1) {
/* ... */
if (mode != MBEDTLS_PADDING_NONE) {
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}if (mode != MBEDTLS_PADDING_NONE) { ... }
return 0;
}if (ctx->psa_enabled == 1) { ... }
/* ... */#endif
switch (mode) {
#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
case MBEDTLS_PADDING_PKCS7:
ctx->add_padding = add_pkcs_padding;
ctx->get_padding = get_pkcs_padding;
break;/* ... */
#endif
#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)case MBEDTLS_PADDING_PKCS7:
case MBEDTLS_PADDING_ONE_AND_ZEROS:
ctx->add_padding = add_one_and_zeros_padding;
ctx->get_padding = get_one_and_zeros_padding;
break;/* ... */
#endif
#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)case MBEDTLS_PADDING_ONE_AND_ZEROS:
case MBEDTLS_PADDING_ZEROS_AND_LEN:
ctx->add_padding = add_zeros_and_len_padding;
ctx->get_padding = get_zeros_and_len_padding;
break;/* ... */
#endif
#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)case MBEDTLS_PADDING_ZEROS_AND_LEN:
case MBEDTLS_PADDING_ZEROS:
ctx->add_padding = add_zeros_padding;
ctx->get_padding = get_zeros_padding;
break;/* ... */
#endifcase MBEDTLS_PADDING_ZEROS:
case MBEDTLS_PADDING_NONE:
ctx->add_padding = NULL;
ctx->get_padding = get_no_padding;
break;
case MBEDTLS_PADDING_NONE:
default:
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;default
}switch (mode) { ... }
return 0;
}{ ... }
/* ... */#endif
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
unsigned char *tag, size_t tag_len)
{
CIPHER_VALIDATE_RET(ctx != NULL);
CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
if (ctx->cipher_info == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (ctx->cipher_info == NULL) { ... }
if (MBEDTLS_ENCRYPT != ctx->operation) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (MBEDTLS_ENCRYPT != ctx->operation) { ... }
#if defined(MBEDTLS_USE_PSA_CRYPTO)
if (ctx->psa_enabled == 1) {
/* ... */
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}if (ctx->psa_enabled == 1) { ... }
/* ... */#endif
#if defined(MBEDTLS_GCM_C)
if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
tag, tag_len);
}if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) { ... }
/* ... */#endif
#if defined(MBEDTLS_CHACHAPOLY_C)
if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
if (tag_len != 16U) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (tag_len != 16U) { ... }
return mbedtls_chachapoly_finish(
(mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
}if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) { ... }
/* ... */#endif
return 0;
}{ ... }
int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
const unsigned char *tag, size_t tag_len)
{
unsigned char check_tag[16];
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
CIPHER_VALIDATE_RET(ctx != NULL);
CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
if (ctx->cipher_info == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (ctx->cipher_info == NULL) { ... }
if (MBEDTLS_DECRYPT != ctx->operation) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (MBEDTLS_DECRYPT != ctx->operation) { ... }
#if defined(MBEDTLS_USE_PSA_CRYPTO)
if (ctx->psa_enabled == 1) {
/* ... */
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}if (ctx->psa_enabled == 1) { ... }
/* ... */#endif
/* ... */
ret = 0;
#if defined(MBEDTLS_GCM_C)
if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
if (tag_len > sizeof(check_tag)) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (tag_len > sizeof(check_tag)) { ... }
if (0 != (ret = mbedtls_gcm_finish(
(mbedtls_gcm_context *) ctx->cipher_ctx,
check_tag, tag_len))) {
return ret;
}if (0 != (ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, check_tag, tag_len))) { ... }
if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
goto exit;
}if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) { ... }
}if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) { ... }
/* ... */#endif
#if defined(MBEDTLS_CHACHAPOLY_C)
if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
if (tag_len != sizeof(check_tag)) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (tag_len != sizeof(check_tag)) { ... }
ret = mbedtls_chachapoly_finish(
(mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
if (ret != 0) {
return ret;
}if (ret != 0) { ... }
if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
goto exit;
}if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) { ... }
}if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) { ... }
/* ... */#endif
exit:
mbedtls_platform_zeroize(check_tag, tag_len);
return ret;
}{ ... }
#endif/* ... */
/* ... */
int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
const unsigned char *iv, size_t iv_len,
const unsigned char *input, size_t ilen,
unsigned char *output, size_t *olen)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t finish_olen;
CIPHER_VALIDATE_RET(ctx != NULL);
CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
CIPHER_VALIDATE_RET(output != NULL);
CIPHER_VALIDATE_RET(olen != NULL);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
if (ctx->psa_enabled == 1) {
/* ... */
mbedtls_cipher_context_psa * const cipher_psa =
(mbedtls_cipher_context_psa *) ctx->cipher_ctx;
psa_status_t status;
psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
size_t part_len;
if (ctx->operation == MBEDTLS_DECRYPT) {
status = psa_cipher_decrypt_setup(&cipher_op,
cipher_psa->slot,
cipher_psa->alg);
}if (ctx->operation == MBEDTLS_DECRYPT) { ... } else if (ctx->operation == MBEDTLS_ENCRYPT) {
status = psa_cipher_encrypt_setup(&cipher_op,
cipher_psa->slot,
cipher_psa->alg);
}else if (ctx->operation == MBEDTLS_ENCRYPT) { ... } else {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}else { ... }
/* ... */
if (status != PSA_SUCCESS) {
return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
}if (status != PSA_SUCCESS) { ... }
if (ctx->cipher_info->mode != MBEDTLS_MODE_ECB) {
status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
if (status != PSA_SUCCESS) {
return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
}if (status != PSA_SUCCESS) { ... }
}if (ctx->cipher_info->mode != MBEDTLS_MODE_ECB) { ... }
status = psa_cipher_update(&cipher_op,
input, ilen,
output, ilen, olen);
if (status != PSA_SUCCESS) {
return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
}if (status != PSA_SUCCESS) { ... }
status = psa_cipher_finish(&cipher_op,
output + *olen, ilen - *olen,
&part_len);
if (status != PSA_SUCCESS) {
return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
}if (status != PSA_SUCCESS) { ... }
*olen += part_len;
return 0;
}if (ctx->psa_enabled == 1) { ... }
/* ... */#endif
if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
return ret;
}if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) { ... }
if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
return ret;
}if ((ret = mbedtls_cipher_reset(ctx)) != 0) { ... }
if ((ret = mbedtls_cipher_update(ctx, input, ilen,
output, olen)) != 0) {
return ret;
}if ((ret = mbedtls_cipher_update(ctx, input, ilen, output, olen)) != 0) { ... }
if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
&finish_olen)) != 0) {
return ret;
}if ((ret = mbedtls_cipher_finish(ctx, output + *olen, &finish_olen)) != 0) { ... }
*olen += finish_olen;
return 0;
}{ ... }
#if defined(MBEDTLS_CIPHER_MODE_AEAD)
/* ... */
static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
const unsigned char *iv, size_t iv_len,
const unsigned char *ad, size_t ad_len,
const unsigned char *input, size_t ilen,
unsigned char *output, size_t *olen,
unsigned char *tag, size_t tag_len)
{
#if defined(MBEDTLS_USE_PSA_CRYPTO)
if (ctx->psa_enabled == 1) {
/* ... */
mbedtls_cipher_context_psa * const cipher_psa =
(mbedtls_cipher_context_psa *) ctx->cipher_ctx;
psa_status_t status;
/* ... */
if (output == NULL || tag != output + ilen) {
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}if (output == NULL || tag != output + ilen) { ... }
status = psa_aead_encrypt(cipher_psa->slot,
cipher_psa->alg,
iv, iv_len,
ad, ad_len,
input, ilen,
output, ilen + tag_len, olen);
if (status != PSA_SUCCESS) {
return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
}if (status != PSA_SUCCESS) { ... }
*olen -= tag_len;
return 0;
}if (ctx->psa_enabled == 1) { ... }
/* ... */#endif
#if defined(MBEDTLS_GCM_C)
if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
*olen = ilen;
return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
ilen, iv, iv_len, ad, ad_len,
input, output, tag_len, tag);
}if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) { ... }
/* ... */#endif
#if defined(MBEDTLS_CCM_C)
if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
*olen = ilen;
return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
iv, iv_len, ad, ad_len, input, output,
tag, tag_len);
}if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) { ... }
/* ... */#endif
#if defined(MBEDTLS_CHACHAPOLY_C)
if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
if ((iv_len != ctx->cipher_info->iv_size) ||
(tag_len != 16U)) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if ((iv_len != ctx->cipher_info->iv_size) || (tag_len != 16U)) { ... }
*olen = ilen;
return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
ilen, iv, ad, ad_len, input, output, tag);
}if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) { ... }
/* ... */#endif
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{ ... }
/* ... */
static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
const unsigned char *iv, size_t iv_len,
const unsigned char *ad, size_t ad_len,
const unsigned char *input, size_t ilen,
unsigned char *output, size_t *olen,
const unsigned char *tag, size_t tag_len)
{
#if defined(MBEDTLS_USE_PSA_CRYPTO)
if (ctx->psa_enabled == 1) {
/* ... */
mbedtls_cipher_context_psa * const cipher_psa =
(mbedtls_cipher_context_psa *) ctx->cipher_ctx;
psa_status_t status;
/* ... */
if (input == NULL || tag != input + ilen) {
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}if (input == NULL || tag != input + ilen) { ... }
status = psa_aead_decrypt(cipher_psa->slot,
cipher_psa->alg,
iv, iv_len,
ad, ad_len,
input, ilen + tag_len,
output, ilen, olen);
if (status == PSA_ERROR_INVALID_SIGNATURE) {
return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
}if (status == PSA_ERROR_INVALID_SIGNATURE) { ... } else if (status != PSA_SUCCESS) {
return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
}else if (status != PSA_SUCCESS) { ... }
return 0;
}if (ctx->psa_enabled == 1) { ... }
/* ... */#endif
#if defined(MBEDTLS_GCM_C)
if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
*olen = ilen;
ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
iv, iv_len, ad, ad_len,
tag, tag_len, input, output);
if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
}if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) { ... }
return ret;
}if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) { ... }
/* ... */#endif
#if defined(MBEDTLS_CCM_C)
if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
*olen = ilen;
ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
iv, iv_len, ad, ad_len,
input, output, tag, tag_len);
if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
}if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) { ... }
return ret;
}if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) { ... }
/* ... */#endif
#if defined(MBEDTLS_CHACHAPOLY_C)
if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
if ((iv_len != ctx->cipher_info->iv_size) ||
(tag_len != 16U)) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if ((iv_len != ctx->cipher_info->iv_size) || (tag_len != 16U)) { ... }
*olen = ilen;
ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
iv, ad, ad_len, tag, input, output);
if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
}if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) { ... }
return ret;
}if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) { ... }
/* ... */#endif
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{ ... }
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
/* ... */
int mbedtls_cipher_auth_encrypt(mbedtls_cipher_context_t *ctx,
const unsigned char *iv, size_t iv_len,
const unsigned char *ad, size_t ad_len,
const unsigned char *input, size_t ilen,
unsigned char *output, size_t *olen,
unsigned char *tag, size_t tag_len)
{
CIPHER_VALIDATE_RET(ctx != NULL);
CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
CIPHER_VALIDATE_RET(ilen == 0 || output != NULL);
CIPHER_VALIDATE_RET(olen != NULL);
CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
return mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
input, ilen, output, olen,
tag, tag_len);
}{ ... }
/* ... */
int mbedtls_cipher_auth_decrypt(mbedtls_cipher_context_t *ctx,
const unsigned char *iv, size_t iv_len,
const unsigned char *ad, size_t ad_len,
const unsigned char *input, size_t ilen,
unsigned char *output, size_t *olen,
const unsigned char *tag, size_t tag_len)
{
CIPHER_VALIDATE_RET(ctx != NULL);
CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
CIPHER_VALIDATE_RET(ilen == 0 || output != NULL);
CIPHER_VALIDATE_RET(olen != NULL);
CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
input, ilen, output, olen,
tag, tag_len);
}{ ... }
#endif/* ... */ /* ... */
#endif
#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
/* ... */
int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
const unsigned char *iv, size_t iv_len,
const unsigned char *ad, size_t ad_len,
const unsigned char *input, size_t ilen,
unsigned char *output, size_t output_len,
size_t *olen, size_t tag_len)
{
CIPHER_VALIDATE_RET(ctx != NULL);
CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
CIPHER_VALIDATE_RET(output != NULL);
CIPHER_VALIDATE_RET(olen != NULL);
#if defined(MBEDTLS_NIST_KW_C)
if (
#if defined(MBEDTLS_USE_PSA_CRYPTO)
ctx->psa_enabled == 0 &&
#endif
(MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
/* ... */
if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (iv_len != 0 || tag_len != 0 || ad_len != 0) { ... }
(void) iv;
(void) ad;
return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
output, olen, output_len);
}if (#if defined(MBEDTLS_USE_PSA_CRYPTO) ctx->psa_enabled == 0 && #endif (MBEDTLS_MODE_KW == ctx->cipher_info->mode || MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) { ... }
/* ... */#endif
#if defined(MBEDTLS_CIPHER_MODE_AEAD)
if (output_len < ilen + tag_len) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (output_len < ilen + tag_len) { ... }
int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
input, ilen, output, olen,
output + ilen, tag_len);
*olen += tag_len;
return ret;/* ... */
#else
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
#endif
}{ ... }
/* ... */
int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
const unsigned char *iv, size_t iv_len,
const unsigned char *ad, size_t ad_len,
const unsigned char *input, size_t ilen,
unsigned char *output, size_t output_len,
size_t *olen, size_t tag_len)
{
CIPHER_VALIDATE_RET(ctx != NULL);
CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
CIPHER_VALIDATE_RET(output_len == 0 || output != NULL);
CIPHER_VALIDATE_RET(olen != NULL);
#if defined(MBEDTLS_NIST_KW_C)
if (
#if defined(MBEDTLS_USE_PSA_CRYPTO)
ctx->psa_enabled == 0 &&
#endif
(MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
/* ... */
if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (iv_len != 0 || tag_len != 0 || ad_len != 0) { ... }
(void) iv;
(void) ad;
return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
output, olen, output_len);
}if (#if defined(MBEDTLS_USE_PSA_CRYPTO) ctx->psa_enabled == 0 && #endif (MBEDTLS_MODE_KW == ctx->cipher_info->mode || MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) { ... }
/* ... */#endif
#if defined(MBEDTLS_CIPHER_MODE_AEAD)
if (ilen < tag_len || output_len < ilen - tag_len) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}if (ilen < tag_len || output_len < ilen - tag_len) { ... }
return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
input, ilen - tag_len, output, olen,
input + ilen - tag_len, tag_len);/* ... */
#else
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
#endif
}{ ... }
#endif/* ... */
/* ... */
#endif