1
11
12
13
14
15
16
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
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
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
174
175
176
177
178
199
200
201
205
206
207
208
211
212
213
214
215
216
217
218
222
223
224
225
226
227
228
229
230
231
232
237
238
241
242
243
244
245
246
247
248
251
252
253
254
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
277
278
280
281
284
287
288
289
290
291
294
295
296
297
298
299
300
301
302
303
304
305
306
307
310
313
314
319
320
321
322
323
324
325
326
327
328
329
330
331
332
335
336
337
340
341
342
343
346
347
348
352
353
354
355
356
357
358
368
370
371
372
373
374
375
376
377
378
379
382
383
384
385
386
387
388
389
390
391
392
393
397
398
402
403
408
409
410
411
412
413
414
415
416
417
418
421
422
429
430
431
434
435
445
446
447
448
451
454
455
456
457
460
461
462
467
468
469
470
476
477
478
479
480
481
482
483
484
485
488
489
496
497
498
499
500
501
502
503
507
508
509
510
511
512
513
516
517
518
524
525
526
527
528
529
530
531
532
533
534
537
538
539
546
547
548
553
554
555
556
557
558
559
560
561
562
563
564
565
566
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
588
589
590
597
598
599
600
603
604
605
608
609
610
611
612
613
616
617
618
619
620
621
627
628
629
635
636
637
643
644
645
648
649
650
651
652
653
656
657
658
659
660
661
668
669
672
673
674
675
676
677
678
679
680
681
682
683
686
687
688
689
690
691
692
693
694
695
698
699
703
704
705
706
709
710
711
712
713
714
715
716
717
720
721
722
723
724
725
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
745
746
747
748
749
750
751
752
753
754
755
756
757
758
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
778
779
780
781
782
783
784
785
786
787
791
792
793
794
795
796
797
798
801
802
803
804
805
806
807
808
809
810
811
812
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
840
841
842
843
844
845
846
847
850
851
852
855
856
857
858
859
860
862
863
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
887
888
889
890
891
892
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
930
931
932
933
934
935
936
937
938
939
940
943
944
945
946
947
948
949
950
951
952
953
959
960
961
962
963
964
965
966
967
968
969
973
974
975
976
977
978
979
980
983
984
985
990
991
992
993
994
995
1001
1002
1003
1004
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1020
1021
1022
1029
1030
1031
1032
1033
1041
1042
1043
1044
1045
1046
1047
1048
1051
1052
1053
1056
1057
1064
1065
1066
1067
1068
1069
1070
1071
1078
1079
1080
1081
1092
1093
1094
1095
1096
1097
1098
1099
1100
1103
1104
1105
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1129
1130
1131
1132
1135
1138
1139
1140
1141
1142
1143
1144
1145
1149
1150
1151
1155
1156
1157
1161
1162
1163
1167
1168
1173
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1188
1189
1192
1193
1194
1201
1202
1203
1204
1205
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1237
1238
1241
1242
1243
1250
1251
1252
1253
1254
1255
1256
1257
1259
1260
1263
1264
1265
1266
1267
1270
1271
1272
1276
1277
1278
1279
1280
1281
1282
1285
1286
1287
1288
1291
1292
1293
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1324
1325
1326
1327
1328
1329
1330
1331
1342
1343
1347
1350
1351
1357
1358
1359
1360
1361
1364
1365
1366
1367
1368
1371
1372
1373
1374
1375
1376
1377
1380
1381
1384
1385
1386
1389
1390
1391
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1419
1420
1421
1422
1423
1424
1426
1429
1430
1431
1432
1433
1434
1435
1436
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1496
1497
1498
1499
1500
1501
1503
1506
1507
1508
1509
1510
1511
1512
1513
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1564
1565
1566
1567
1568
1569
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1605
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1656
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1677
1678
1679
1680
1681
1682
1683
/* ... */
#include "common.h"
#if defined(MBEDTLS_CIPHER_C)
#include "mbedtls/cipher.h"
#include "cipher_wrap.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) && !defined(MBEDTLS_DEPRECATED_REMOVED)
#include "psa/crypto.h"
#endif
#if defined(MBEDTLS_NIST_KW_C)
#include "mbedtls/nist_kw.h"
#endif
#include "mbedtls/platform.h"
static int supported_init = 0;
static inline const mbedtls_cipher_base_t *mbedtls_cipher_get_base(
const mbedtls_cipher_info_t *info)
{
return mbedtls_cipher_base_lookup_table[info->base_idx];
}{ ... }
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;
}{...}
*type = 0;
supported_init = 1;
}{...}
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;
}{...}
}{...}
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;
}{...}
for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
if (!strcmp(def->info->name, cipher_name)) {
return def->info;
}{...}
}{...}
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 (mbedtls_cipher_get_base(def->info)->cipher == cipher_id &&
mbedtls_cipher_info_get_key_bitlen(def->info) == (unsigned) key_bitlen &&
def->info->mode == mode) {
return def->info;
}{...}
}{...}
return NULL;
}{ ... }
#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
static inline psa_key_type_t mbedtls_psa_translate_cipher_type(
mbedtls_cipher_type_t cipher)
{
switch (cipher) {
case MBEDTLS_CIPHER_AES_128_CCM:
case MBEDTLS_CIPHER_AES_192_CCM:
case MBEDTLS_CIPHER_AES_256_CCM:
case MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG:
case MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG:
case MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG:
case MBEDTLS_CIPHER_AES_128_GCM:
case MBEDTLS_CIPHER_AES_192_GCM:
case MBEDTLS_CIPHER_AES_256_GCM:
case MBEDTLS_CIPHER_AES_128_CBC:
case MBEDTLS_CIPHER_AES_192_CBC:
case MBEDTLS_CIPHER_AES_256_CBC:
case MBEDTLS_CIPHER_AES_128_ECB:
case MBEDTLS_CIPHER_AES_192_ECB:
case MBEDTLS_CIPHER_AES_256_ECB:
return PSA_KEY_TYPE_AES;
/* ... */
...
default:
return 0;...
}{...}
}{...}
static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
mbedtls_cipher_mode_t mode, size_t taglen)
{
switch (mode) {
case MBEDTLS_MODE_ECB:
return PSA_ALG_ECB_NO_PADDING;...
case MBEDTLS_MODE_GCM:
return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, taglen);...
case MBEDTLS_MODE_CCM:
return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen);...
case MBEDTLS_MODE_CCM_STAR_NO_TAG:
return PSA_ALG_CCM_STAR_NO_TAG;...
case MBEDTLS_MODE_CBC:
if (taglen == 0) {
return PSA_ALG_CBC_NO_PADDING;
}{...} else {
return 0;
}{...}
... default:
return 0;...
}{...}
}{...}
/* ... */#endif
void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
{
memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
}{ ... }
void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
{
if (ctx == NULL) {
return;
}{...}
#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
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);
}{...}
mbedtls_zeroize_and_free(cipher_psa, sizeof(*cipher_psa));
}{...}
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
return;
}{...}
#endif/* ... */
#if defined(MBEDTLS_CMAC_C)
if (ctx->cmac_ctx) {
mbedtls_zeroize_and_free(ctx->cmac_ctx,
sizeof(mbedtls_cmac_context_t));
}{...}
#endif/* ... */
if (ctx->cipher_ctx) {
mbedtls_cipher_get_base(ctx->cipher_info)->ctx_free_func(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)
{
if (cipher_info == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
if (mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func != NULL) {
ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func();
if (ctx->cipher_ctx == NULL) {
return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
}{...}
}{...}
ctx->cipher_info = cipher_info;
return 0;
}{ ... }
#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
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;
}{...}
/* ... */
alg = mbedtls_psa_translate_cipher_mode(((mbedtls_cipher_mode_t) cipher_info->mode), taglen);
if (alg == 0) {
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{...}
if (mbedtls_psa_translate_cipher_type(((mbedtls_cipher_type_t) cipher_info->type)) == 0) {
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{...}
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;
}{...}
cipher_psa->alg = alg;
ctx->cipher_ctx = cipher_psa;
ctx->cipher_info = cipher_info;
ctx->psa_enabled = 1;
return 0;
}{...}
/* ... */#endif
int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
const unsigned char *key,
int key_bitlen,
const mbedtls_operation_t operation)
{
if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
if (ctx->cipher_info == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
#if defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) &&
MBEDTLS_DECRYPT == operation) {
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{...}
#endif/* ... */
#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
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 (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
key_type = mbedtls_psa_translate_cipher_type(
((mbedtls_cipher_type_t) ctx->cipher_info->type));
if (key_type == 0) {
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{...}
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_ERROR_INSUFFICIENT_MEMORY:
return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;...
case PSA_ERROR_NOT_SUPPORTED:
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;...
default:
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;...
}{...}
/* ... */
cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
ctx->key_bitlen = key_bitlen;
ctx->operation = operation;
return 0;
}{...}
#endif/* ... */
if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
(int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
ctx->key_bitlen = key_bitlen;
ctx->operation = operation;
#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
/* ... */
if (MBEDTLS_ENCRYPT == operation ||
MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
ctx->key_bitlen);
}{...}
if (MBEDTLS_DECRYPT == operation) {
return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
ctx->key_bitlen);
}{...}
#else/* ... */
if (operation == MBEDTLS_ENCRYPT || operation == MBEDTLS_DECRYPT) {
return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
ctx->key_bitlen);
}{...}
#endif/* ... */
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;
if (ctx->cipher_info == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
if (ctx->psa_enabled == 1) {
/* ... */
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{...}
#endif/* ... */
if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{...}
if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
actual_iv_size = iv_len;
}{...} else {
actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
if (actual_iv_size > iv_len) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
}{...}
#if defined(MBEDTLS_CHACHA20_C)
if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) {
/* ... */
if (iv_len != 12) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
iv,
0U)) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
}{...}
#if defined(MBEDTLS_CHACHAPOLY_C)
if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
iv_len != 12) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
#endif/* ... */
/* ... */#endif
#if defined(MBEDTLS_GCM_C)
if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
ctx->operation,
iv, iv_len);
}{...}
#endif/* ... */
#if defined(MBEDTLS_CCM_C)
if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
int set_lengths_result;
int ccm_star_mode;
set_lengths_result = mbedtls_ccm_set_lengths(
(mbedtls_ccm_context *) ctx->cipher_ctx,
0, 0, 0);
if (set_lengths_result != 0) {
return set_lengths_result;
}{...}
if (ctx->operation == MBEDTLS_DECRYPT) {
ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
}{...} else if (ctx->operation == MBEDTLS_ENCRYPT) {
ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
}{...} else {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
ccm_star_mode,
iv, iv_len);
}{...}
#endif/* ... */
if (actual_iv_size != 0) {
memcpy(ctx->iv, iv, actual_iv_size);
ctx->iv_size = actual_iv_size;
}{...}
return 0;
}{ ... }
int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
{
if (ctx->cipher_info == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
if (ctx->psa_enabled == 1) {
/* ... */
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{...}
#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)
{
if (ctx->cipher_info == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
if (ctx->psa_enabled == 1) {
/* ... */
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{...}
#endif/* ... */
#if defined(MBEDTLS_GCM_C)
if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
ad, ad_len);
}{...}
#endif/* ... */
#if defined(MBEDTLS_CHACHAPOLY_C)
if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) 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;
}{...}
return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
ad, ad_len);
}{...}
#endif/* ... */
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{ ... }
/* ... */#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;
if (ctx->cipher_info == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
if (ctx->psa_enabled == 1) {
/* ... */
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{...}
#endif/* ... */
*olen = 0;
block_size = mbedtls_cipher_get_block_size(ctx);
if (0 == block_size) {
return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
}{...}
if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_ECB) {
if (ilen != block_size) {
return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
}{...}
*olen = ilen;
if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
ctx->operation, input,
output))) {
return ret;
}{...}
return 0;
}{...}
#if defined(MBEDTLS_GCM_C)
if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) {
return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
input, ilen,
output, ilen, olen);
}{...}
#endif/* ... */
#if defined(MBEDTLS_CCM_C)
if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
input, ilen,
output, ilen, olen);
}{...}
#endif/* ... */
#if defined(MBEDTLS_CHACHAPOLY_C)
if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
*olen = ilen;
return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
ilen, input, output);
}{...}
#endif/* ... */
if (input == output &&
(ctx->unprocessed_len != 0 || ilen % block_size)) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
#if defined(MBEDTLS_CIPHER_MODE_CBC)
if (((mbedtls_cipher_mode_t) 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 (0 != ctx->unprocessed_len) {
copy_len = block_size - ctx->unprocessed_len;
memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
copy_len);
if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
ctx->operation,
block_size, ctx->iv,
ctx->
unprocessed_data,
output))) {
return ret;
}{...}
*olen += block_size;
output += block_size;
ctx->unprocessed_len = 0;
input += copy_len;
ilen -= copy_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;
}{...}
memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
copy_len);
ctx->unprocessed_len += copy_len;
ilen -= copy_len;
}{...}
/* ... */
if (ilen) {
if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
ctx->operation,
ilen, ctx->iv,
input,
output))) {
return ret;
}{...}
*olen += ilen;
}{...}
return 0;
}{...}
#endif/* ... */
#if defined(MBEDTLS_CIPHER_MODE_CFB)
if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CFB) {
if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
ctx->operation, ilen,
&ctx->unprocessed_len,
ctx->iv,
input, output))) {
return ret;
}{...}
*olen = ilen;
return 0;
}{...}
#endif/* ... */
#if defined(MBEDTLS_CIPHER_MODE_OFB)
if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_OFB) {
if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
ilen,
&ctx->unprocessed_len,
ctx->iv,
input, output))) {
return ret;
}{...}
*olen = ilen;
return 0;
}{...}
#endif/* ... */
#if defined(MBEDTLS_CIPHER_MODE_CTR)
if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CTR) {
if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
ilen,
&ctx->unprocessed_len,
ctx->iv,
ctx->unprocessed_data,
input, output))) {
return ret;
}{...}
*olen = ilen;
return 0;
}{...}
#endif/* ... */
#if defined(MBEDTLS_CIPHER_MODE_XTS)
if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_XTS) {
if (ctx->unprocessed_len > 0) {
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{...}
ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
ctx->operation,
ilen,
ctx->iv,
input,
output);
if (ret != 0) {
return ret;
}{...}
*olen = ilen;
return 0;
}{...}
#endif/* ... */
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_STREAM) {
if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
ilen, input,
output))) {
return ret;
}{...}
*olen = ilen;
return 0;
}{...}
#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;
}{...}
}{ ... }
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;
if (NULL == input || NULL == data_len) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
padding_len = input[input_len - 1];
if (padding_len == 0 || padding_len > input_len) {
return MBEDTLS_ERR_CIPHER_INVALID_PADDING;
}{...}
*data_len = input_len - padding_len;
mbedtls_ct_condition_t bad = mbedtls_ct_uint_gt(padding_len, input_len);
bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
/* ... */
pad_idx = input_len - padding_len;
for (i = 0; i < input_len; i++) {
mbedtls_ct_condition_t in_padding = mbedtls_ct_uint_ge(i, pad_idx);
mbedtls_ct_condition_t different = mbedtls_ct_uint_ne(input[i], padding_len);
bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_and(in_padding, different));
}{...}
return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
}{ ... }
#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;
}{...}
}{ ... }
static int get_one_and_zeros_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;
}{...}
mbedtls_ct_condition_t in_padding = MBEDTLS_CT_TRUE;
mbedtls_ct_condition_t bad = MBEDTLS_CT_TRUE;
*data_len = 0;
for (ptrdiff_t i = (ptrdiff_t) (input_len) - 1; i >= 0; i--) {
mbedtls_ct_condition_t is_nonzero = mbedtls_ct_bool(input[i]);
mbedtls_ct_condition_t hit_first_nonzero = mbedtls_ct_bool_and(is_nonzero, in_padding);
*data_len = mbedtls_ct_size_if(hit_first_nonzero, i, *data_len);
bad = mbedtls_ct_bool_if(hit_first_nonzero, mbedtls_ct_uint_ne(input[i], 0x80), bad);
in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_bool_not(is_nonzero));
}{...}
return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
}{ ... }
#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;
}{...}
output[output_len - 1] = (unsigned char) padding_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;
mbedtls_ct_condition_t bad;
if (NULL == input || NULL == data_len) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
padding_len = input[input_len - 1];
*data_len = input_len - padding_len;
bad = mbedtls_ct_uint_gt(padding_len, input_len);
bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
pad_idx = input_len - padding_len;
for (i = 0; i < input_len - 1; i++) {
mbedtls_ct_condition_t is_padding = mbedtls_ct_uint_ge(i, pad_idx);
mbedtls_ct_condition_t nonzero_pad_byte;
nonzero_pad_byte = mbedtls_ct_bool_if_else_0(is_padding, mbedtls_ct_bool(input[i]));
bad = mbedtls_ct_bool_or(bad, nonzero_pad_byte);
}{...}
return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
}{ ... }
#endif/* ... */
#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
/* ... */
static void add_zeros_padding(unsigned char *output,
size_t output_len, size_t data_len)
{
memset(output + data_len, 0, output_len - data_len);
}{ ... }
static int get_zeros_padding(unsigned char *input, size_t input_len,
size_t *data_len)
{
size_t i;
mbedtls_ct_condition_t done = MBEDTLS_CT_FALSE, prev_done;
if (NULL == input || NULL == data_len) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
*data_len = 0;
for (i = input_len; i > 0; i--) {
prev_done = done;
done = mbedtls_ct_bool_or(done, mbedtls_ct_uint_ne(input[i-1], 0));
*data_len = mbedtls_ct_size_if(mbedtls_ct_bool_ne(done, prev_done), i, *data_len);
}{...}
return 0;
}{ ... }
#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;
}{...}
*data_len = input_len;
return 0;
}{ ... }
#endif/* ... */
int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
unsigned char *output, size_t *olen)
{
if (ctx->cipher_info == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
if (ctx->psa_enabled == 1) {
/* ... */
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{...}
#endif/* ... */
*olen = 0;
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
/* ... */
if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
if (ctx->get_padding == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
}{...}
#endif/* ... */
if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
return 0;
}{...}
if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
(MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
return 0;
}{...}
if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
if (ctx->unprocessed_len != 0) {
return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
}{...}
return 0;
}{...}
#if defined(MBEDTLS_CIPHER_MODE_CBC)
if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) 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;
}{...}
return 0;
}{...}
ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
ctx->unprocessed_len);
}{...} else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
/* ... */
if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
return 0;
}{...}
return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
}{...}
if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
ctx->operation,
mbedtls_cipher_get_block_size(
ctx),
ctx->iv,
ctx->unprocessed_data,
output))) {
return ret;
}{...}
if (MBEDTLS_DECRYPT == ctx->operation) {
return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
olen);
}{...}
*olen = mbedtls_cipher_get_block_size(ctx);
return 0;
}{...}
#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)
{
if (NULL == ctx->cipher_info ||
MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
if (ctx->psa_enabled == 1) {
/* ... */
if (mode != MBEDTLS_PADDING_NONE) {
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{...}
return 0;
}{...}
#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_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_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:
ctx->add_padding = add_zeros_padding;
ctx->get_padding = get_zeros_padding;
break;/* ... */
#endif
case MBEDTLS_PADDING_NONE:
ctx->add_padding = NULL;
ctx->get_padding = get_no_padding;
break;
...
default:
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;...
}{...}
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)
{
if (ctx->cipher_info == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
if (MBEDTLS_ENCRYPT != ctx->operation) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
if (ctx->psa_enabled == 1) {
/* ... */
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{...}
#endif/* ... */
#if defined(MBEDTLS_GCM_C)
if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
size_t output_length;
/* ... */
return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
NULL, 0, &output_length,
tag, tag_len);
}{...}
#endif/* ... */
#if defined(MBEDTLS_CHACHAPOLY_C)
if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
if (tag_len != 16U) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
return mbedtls_chachapoly_finish(
(mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
}{...}
#endif/* ... */
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{ ... }
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;
if (ctx->cipher_info == NULL) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
if (MBEDTLS_DECRYPT != ctx->operation) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
if (ctx->psa_enabled == 1) {
/* ... */
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{...}
#endif/* ... */
ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
#if defined(MBEDTLS_GCM_C)
if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
size_t output_length;
/* ... */
if (tag_len > sizeof(check_tag)) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
if (0 != (ret = mbedtls_gcm_finish(
(mbedtls_gcm_context *) ctx->cipher_ctx,
NULL, 0, &output_length,
check_tag, tag_len))) {
return ret;
}{...}
if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
goto exit;
}{...}
}{...}
#endif/* ... */
#if defined(MBEDTLS_CHACHAPOLY_C)
if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
if (tag_len != sizeof(check_tag)) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
ret = mbedtls_chachapoly_finish(
(mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
if (ret != 0) {
return ret;
}{...}
if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
goto exit;
}{...}
}{...}
#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;
#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
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);
}{...} else if (ctx->operation == MBEDTLS_ENCRYPT) {
status = psa_cipher_encrypt_setup(&cipher_op,
cipher_psa->slot,
cipher_psa->alg);
}{...} else {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
/* ... */
if (status != PSA_SUCCESS) {
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
}{...}
if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) {
status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
if (status != PSA_SUCCESS) {
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
}{...}
}{...}
status = psa_cipher_update(&cipher_op,
input, ilen,
output, ilen, olen);
if (status != PSA_SUCCESS) {
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
}{...}
status = psa_cipher_finish(&cipher_op,
output + *olen, ilen - *olen,
&part_len);
if (status != PSA_SUCCESS) {
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
}{...}
*olen += part_len;
return 0;
}{...}
#endif/* ... */
if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
return ret;
}{...}
if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
return ret;
}{...}
if ((ret = mbedtls_cipher_update(ctx, input, ilen,
output, olen)) != 0) {
return ret;
}{...}
if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
&finish_olen)) != 0) {
return ret;
}{...}
*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) && !defined(MBEDTLS_DEPRECATED_REMOVED)
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;
}{...}
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_PLATFORM_HW_ACCEL_FAILED;
}{...}
*olen -= tag_len;
return 0;
}{...}
#endif/* ... */
#if defined(MBEDTLS_GCM_C)
if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) 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);
}{...}
#endif/* ... */
#if defined(MBEDTLS_CCM_C)
if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) 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);
}{...}
#endif/* ... */
#if defined(MBEDTLS_CHACHAPOLY_C)
if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
(tag_len != 16U)) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
*olen = ilen;
return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
ilen, iv, ad, ad_len, input, output, tag);
}{...}
#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) && !defined(MBEDTLS_DEPRECATED_REMOVED)
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;
}{...}
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;
}{...} else if (status != PSA_SUCCESS) {
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
}{...}
return 0;
}{...}
#endif/* ... */
#if defined(MBEDTLS_GCM_C)
if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) 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;
}{...}
return ret;
}{...}
#endif/* ... */
#if defined(MBEDTLS_CCM_C)
if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) 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;
}{...}
return ret;
}{...}
#endif/* ... */
#if defined(MBEDTLS_CHACHAPOLY_C)
if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
(tag_len != 16U)) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
*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;
}{...}
return ret;
}{...}
#endif/* ... */
return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
}{ ... }
#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)
{
#if defined(MBEDTLS_NIST_KW_C)
if (
#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
ctx->psa_enabled == 0 &&
#endif
(MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
mbedtls_nist_kw_mode_t mode =
(MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) 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;
}{...}
(void) iv;
(void) ad;
return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
output, olen, output_len);
}{...}
#endif/* ... */
#if defined(MBEDTLS_CIPHER_MODE_AEAD)
if (output_len < ilen + tag_len) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
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)
{
#if defined(MBEDTLS_NIST_KW_C)
if (
#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
ctx->psa_enabled == 0 &&
#endif
(MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
mbedtls_nist_kw_mode_t mode =
(MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) 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;
}{...}
(void) iv;
(void) ad;
return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
output, olen, output_len);
}{...}
#endif/* ... */
#if defined(MBEDTLS_CIPHER_MODE_AEAD)
if (ilen < tag_len || output_len < ilen - tag_len) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
}{...}
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