Coverage Report

Created: 2024-06-09 08:57

/src/mbedtls/library/cipher.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * \file cipher.c
3
 *
4
 * \brief Generic cipher wrapper for mbed TLS
5
 *
6
 * \author Adriaan de Jong <dejong@fox-it.com>
7
 *
8
 *  Copyright The Mbed TLS Contributors
9
 *  SPDX-License-Identifier: Apache-2.0
10
 *
11
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may
12
 *  not use this file except in compliance with the License.
13
 *  You may obtain a copy of the License at
14
 *
15
 *  http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 *  Unless required by applicable law or agreed to in writing, software
18
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19
 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 *  See the License for the specific language governing permissions and
21
 *  limitations under the License.
22
 */
23
24
#include "common.h"
25
26
#if defined(MBEDTLS_CIPHER_C)
27
28
#include "mbedtls/cipher.h"
29
#include "cipher_wrap.h"
30
#include "mbedtls/platform_util.h"
31
#include "mbedtls/error.h"
32
#include "mbedtls/constant_time.h"
33
34
#include <stdlib.h>
35
#include <string.h>
36
37
#if defined(MBEDTLS_CHACHAPOLY_C)
38
#include "mbedtls/chachapoly.h"
39
#endif
40
41
#if defined(MBEDTLS_GCM_C)
42
#include "mbedtls/gcm.h"
43
#endif
44
45
#if defined(MBEDTLS_CCM_C)
46
#include "mbedtls/ccm.h"
47
#endif
48
49
#if defined(MBEDTLS_CHACHA20_C)
50
#include "mbedtls/chacha20.h"
51
#endif
52
53
#if defined(MBEDTLS_CMAC_C)
54
#include "mbedtls/cmac.h"
55
#endif
56
57
#if defined(MBEDTLS_USE_PSA_CRYPTO)
58
#include "psa/crypto.h"
59
#include "mbedtls/psa_util.h"
60
#endif /* MBEDTLS_USE_PSA_CRYPTO */
61
62
#if defined(MBEDTLS_NIST_KW_C)
63
#include "mbedtls/nist_kw.h"
64
#endif
65
66
#include "mbedtls/platform.h"
67
68
static int supported_init = 0;
69
70
const int *mbedtls_cipher_list(void)
71
0
{
72
0
    const mbedtls_cipher_definition_t *def;
73
0
    int *type;
74
75
0
    if (!supported_init) {
76
0
        def = mbedtls_cipher_definitions;
77
0
        type = mbedtls_cipher_supported;
78
79
0
        while (def->type != 0) {
80
0
            *type++ = (*def++).type;
81
0
        }
82
83
0
        *type = 0;
84
85
0
        supported_init = 1;
86
0
    }
87
88
0
    return mbedtls_cipher_supported;
89
0
}
90
91
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
92
    const mbedtls_cipher_type_t cipher_type)
93
0
{
94
0
    const mbedtls_cipher_definition_t *def;
95
96
0
    for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
97
0
        if (def->type == cipher_type) {
98
0
            return def->info;
99
0
        }
100
0
    }
101
102
0
    return NULL;
103
0
}
104
105
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
106
    const char *cipher_name)
107
0
{
108
0
    const mbedtls_cipher_definition_t *def;
109
110
0
    if (NULL == cipher_name) {
111
0
        return NULL;
112
0
    }
113
114
0
    for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
115
0
        if (!strcmp(def->info->name, cipher_name)) {
116
0
            return def->info;
117
0
        }
118
0
    }
119
120
0
    return NULL;
121
0
}
122
123
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
124
    const mbedtls_cipher_id_t cipher_id,
125
    int key_bitlen,
126
    const mbedtls_cipher_mode_t mode)
127
0
{
128
0
    const mbedtls_cipher_definition_t *def;
129
130
0
    for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
131
0
        if (def->info->base->cipher == cipher_id &&
132
0
            def->info->key_bitlen == (unsigned) key_bitlen &&
133
0
            def->info->mode == mode) {
134
0
            return def->info;
135
0
        }
136
0
    }
137
138
0
    return NULL;
139
0
}
140
141
void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
142
43.2k
{
143
43.2k
    memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
144
43.2k
}
145
146
void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
147
43.2k
{
148
43.2k
    if (ctx == NULL) {
149
0
        return;
150
0
    }
151
152
#if defined(MBEDTLS_USE_PSA_CRYPTO)
153
    if (ctx->psa_enabled == 1) {
154
        if (ctx->cipher_ctx != NULL) {
155
            mbedtls_cipher_context_psa * const cipher_psa =
156
                (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
157
158
            if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
159
                /* xxx_free() doesn't allow to return failures. */
160
                (void) psa_destroy_key(cipher_psa->slot);
161
            }
162
163
            mbedtls_platform_zeroize(cipher_psa, sizeof(*cipher_psa));
164
            mbedtls_free(cipher_psa);
165
        }
166
167
        mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
168
        return;
169
    }
170
#endif /* MBEDTLS_USE_PSA_CRYPTO */
171
172
43.2k
#if defined(MBEDTLS_CMAC_C)
173
43.2k
    if (ctx->cmac_ctx) {
174
0
        mbedtls_platform_zeroize(ctx->cmac_ctx,
175
0
                                 sizeof(mbedtls_cmac_context_t));
176
0
        mbedtls_free(ctx->cmac_ctx);
177
0
    }
178
43.2k
#endif
179
180
43.2k
    if (ctx->cipher_ctx) {
181
0
        ctx->cipher_info->base->ctx_free_func(ctx->cipher_ctx);
182
0
    }
183
184
43.2k
    mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
185
43.2k
}
186
187
int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
188
                         const mbedtls_cipher_info_t *cipher_info)
189
0
{
190
0
    if (cipher_info == NULL) {
191
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
192
0
    }
193
194
0
    memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
195
196
0
    if (NULL == (ctx->cipher_ctx = cipher_info->base->ctx_alloc_func())) {
197
0
        return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
198
0
    }
199
200
0
    ctx->cipher_info = cipher_info;
201
202
0
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
203
    /*
204
     * Ignore possible errors caused by a cipher mode that doesn't use padding
205
     */
206
0
#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
207
0
    (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7);
208
#else
209
    (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);
210
#endif
211
0
#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
212
213
0
    return 0;
214
0
}
215
216
#if defined(MBEDTLS_USE_PSA_CRYPTO)
217
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
218
int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
219
                             const mbedtls_cipher_info_t *cipher_info,
220
                             size_t taglen)
221
{
222
    psa_algorithm_t alg;
223
    mbedtls_cipher_context_psa *cipher_psa;
224
225
    if (NULL == cipher_info || NULL == ctx) {
226
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
227
    }
228
229
    /* Check that the underlying cipher mode and cipher type are
230
     * supported by the underlying PSA Crypto implementation. */
231
    alg = mbedtls_psa_translate_cipher_mode(cipher_info->mode, taglen);
232
    if (alg == 0) {
233
        return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
234
    }
235
    if (mbedtls_psa_translate_cipher_type(cipher_info->type) == 0) {
236
        return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
237
    }
238
239
    memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
240
241
    cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
242
    if (cipher_psa == NULL) {
243
        return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
244
    }
245
    cipher_psa->alg  = alg;
246
    ctx->cipher_ctx  = cipher_psa;
247
    ctx->cipher_info = cipher_info;
248
    ctx->psa_enabled = 1;
249
    return 0;
250
}
251
#endif /* MBEDTLS_DEPRECATED_REMOVED */
252
#endif /* MBEDTLS_USE_PSA_CRYPTO */
253
254
int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
255
                          const unsigned char *key,
256
                          int key_bitlen,
257
                          const mbedtls_operation_t operation)
258
0
{
259
0
    if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
260
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
261
0
    }
262
0
    if (ctx->cipher_info == NULL) {
263
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
264
0
    }
265
266
#if defined(MBEDTLS_USE_PSA_CRYPTO)
267
    if (ctx->psa_enabled == 1) {
268
        mbedtls_cipher_context_psa * const cipher_psa =
269
            (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
270
271
        size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
272
273
        psa_status_t status;
274
        psa_key_type_t key_type;
275
        psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
276
277
        /* PSA Crypto API only accepts byte-aligned keys. */
278
        if (key_bitlen % 8 != 0) {
279
            return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
280
        }
281
282
        /* Don't allow keys to be set multiple times. */
283
        if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
284
            return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
285
        }
286
287
        key_type = mbedtls_psa_translate_cipher_type(
288
            ctx->cipher_info->type);
289
        if (key_type == 0) {
290
            return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
291
        }
292
        psa_set_key_type(&attributes, key_type);
293
294
        /* Mbed TLS' cipher layer doesn't enforce the mode of operation
295
         * (encrypt vs. decrypt): it is possible to setup a key for encryption
296
         * and use it for AEAD decryption. Until tests relying on this
297
         * are changed, allow any usage in PSA. */
298
        psa_set_key_usage_flags(&attributes,
299
                                /* mbedtls_psa_translate_cipher_operation( operation ); */
300
                                PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
301
        psa_set_key_algorithm(&attributes, cipher_psa->alg);
302
303
        status = psa_import_key(&attributes, key, key_bytelen,
304
                                &cipher_psa->slot);
305
        switch (status) {
306
            case PSA_SUCCESS:
307
                break;
308
            case PSA_ERROR_INSUFFICIENT_MEMORY:
309
                return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
310
            case PSA_ERROR_NOT_SUPPORTED:
311
                return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
312
            default:
313
                return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
314
        }
315
        /* Indicate that we own the key slot and need to
316
         * destroy it in mbedtls_cipher_free(). */
317
        cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
318
319
        ctx->key_bitlen = key_bitlen;
320
        ctx->operation = operation;
321
        return 0;
322
    }
323
#endif /* MBEDTLS_USE_PSA_CRYPTO */
324
325
0
    if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
326
0
        (int) ctx->cipher_info->key_bitlen != key_bitlen) {
327
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
328
0
    }
329
330
0
    ctx->key_bitlen = key_bitlen;
331
0
    ctx->operation = operation;
332
333
    /*
334
     * For OFB, CFB and CTR mode always use the encryption key schedule
335
     */
336
0
    if (MBEDTLS_ENCRYPT == operation ||
337
0
        MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
338
0
        MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
339
0
        MBEDTLS_MODE_CTR == ctx->cipher_info->mode) {
340
0
        return ctx->cipher_info->base->setkey_enc_func(ctx->cipher_ctx, key,
341
0
                                                       ctx->key_bitlen);
342
0
    }
343
344
0
    if (MBEDTLS_DECRYPT == operation) {
345
0
        return ctx->cipher_info->base->setkey_dec_func(ctx->cipher_ctx, key,
346
0
                                                       ctx->key_bitlen);
347
0
    }
348
349
0
    return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
350
0
}
351
352
int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
353
                          const unsigned char *iv,
354
                          size_t iv_len)
355
0
{
356
0
    size_t actual_iv_size;
357
358
0
    if (ctx->cipher_info == NULL) {
359
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
360
0
    }
361
#if defined(MBEDTLS_USE_PSA_CRYPTO)
362
    if (ctx->psa_enabled == 1) {
363
        /* While PSA Crypto has an API for multipart
364
         * operations, we currently don't make it
365
         * accessible through the cipher layer. */
366
        return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
367
    }
368
#endif /* MBEDTLS_USE_PSA_CRYPTO */
369
370
    /* avoid buffer overflow in ctx->iv */
371
0
    if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
372
0
        return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
373
0
    }
374
375
0
    if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
376
0
        actual_iv_size = iv_len;
377
0
    } else {
378
0
        actual_iv_size = ctx->cipher_info->iv_size;
379
380
        /* avoid reading past the end of input buffer */
381
0
        if (actual_iv_size > iv_len) {
382
0
            return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
383
0
        }
384
0
    }
385
386
0
#if defined(MBEDTLS_CHACHA20_C)
387
0
    if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20) {
388
        /* Even though the actual_iv_size is overwritten with a correct value
389
         * of 12 from the cipher info, return an error to indicate that
390
         * the input iv_len is wrong. */
391
0
        if (iv_len != 12) {
392
0
            return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
393
0
        }
394
395
0
        if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
396
0
                                         iv,
397
0
                                         0U)) {   /* Initial counter value */
398
0
            return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
399
0
        }
400
0
    }
401
0
#if defined(MBEDTLS_CHACHAPOLY_C)
402
0
    if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
403
0
        iv_len != 12) {
404
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
405
0
    }
406
0
#endif
407
0
#endif
408
409
0
#if defined(MBEDTLS_GCM_C)
410
0
    if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
411
0
        return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
412
0
                                  ctx->operation,
413
0
                                  iv, iv_len);
414
0
    }
415
0
#endif
416
417
0
#if defined(MBEDTLS_CCM_C)
418
0
    if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode) {
419
0
        int set_lengths_result;
420
0
        int ccm_star_mode;
421
422
0
        set_lengths_result = mbedtls_ccm_set_lengths(
423
0
            (mbedtls_ccm_context *) ctx->cipher_ctx,
424
0
            0, 0, 0);
425
0
        if (set_lengths_result != 0) {
426
0
            return set_lengths_result;
427
0
        }
428
429
0
        if (ctx->operation == MBEDTLS_DECRYPT) {
430
0
            ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
431
0
        } else if (ctx->operation == MBEDTLS_ENCRYPT) {
432
0
            ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
433
0
        } else {
434
0
            return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
435
0
        }
436
437
0
        return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
438
0
                                  ccm_star_mode,
439
0
                                  iv, iv_len);
440
0
    }
441
0
#endif
442
443
0
    if (actual_iv_size != 0) {
444
0
        memcpy(ctx->iv, iv, actual_iv_size);
445
0
        ctx->iv_size = actual_iv_size;
446
0
    }
447
448
0
    return 0;
449
0
}
450
451
int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
452
0
{
453
0
    if (ctx->cipher_info == NULL) {
454
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
455
0
    }
456
457
#if defined(MBEDTLS_USE_PSA_CRYPTO)
458
    if (ctx->psa_enabled == 1) {
459
        /* We don't support resetting PSA-based
460
         * cipher contexts, yet. */
461
        return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
462
    }
463
#endif /* MBEDTLS_USE_PSA_CRYPTO */
464
465
0
    ctx->unprocessed_len = 0;
466
467
0
    return 0;
468
0
}
469
470
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
471
int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
472
                             const unsigned char *ad, size_t ad_len)
473
0
{
474
0
    if (ctx->cipher_info == NULL) {
475
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
476
0
    }
477
478
#if defined(MBEDTLS_USE_PSA_CRYPTO)
479
    if (ctx->psa_enabled == 1) {
480
        /* While PSA Crypto has an API for multipart
481
         * operations, we currently don't make it
482
         * accessible through the cipher layer. */
483
        return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
484
    }
485
#endif /* MBEDTLS_USE_PSA_CRYPTO */
486
487
0
#if defined(MBEDTLS_GCM_C)
488
0
    if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
489
0
        return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
490
0
                                     ad, ad_len);
491
0
    }
492
0
#endif
493
494
0
#if defined(MBEDTLS_CHACHAPOLY_C)
495
0
    if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
496
0
        int result;
497
0
        mbedtls_chachapoly_mode_t mode;
498
499
0
        mode = (ctx->operation == MBEDTLS_ENCRYPT)
500
0
                ? MBEDTLS_CHACHAPOLY_ENCRYPT
501
0
                : MBEDTLS_CHACHAPOLY_DECRYPT;
502
503
0
        result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
504
0
                                           ctx->iv,
505
0
                                           mode);
506
0
        if (result != 0) {
507
0
            return result;
508
0
        }
509
510
0
        return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
511
0
                                             ad, ad_len);
512
0
    }
513
0
#endif
514
515
0
    return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
516
0
}
517
#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
518
519
int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
520
                          size_t ilen, unsigned char *output, size_t *olen)
521
0
{
522
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
523
0
    size_t block_size;
524
525
0
    if (ctx->cipher_info == NULL) {
526
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
527
0
    }
528
529
#if defined(MBEDTLS_USE_PSA_CRYPTO)
530
    if (ctx->psa_enabled == 1) {
531
        /* While PSA Crypto has an API for multipart
532
         * operations, we currently don't make it
533
         * accessible through the cipher layer. */
534
        return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
535
    }
536
#endif /* MBEDTLS_USE_PSA_CRYPTO */
537
538
0
    *olen = 0;
539
0
    block_size = mbedtls_cipher_get_block_size(ctx);
540
0
    if (0 == block_size) {
541
0
        return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
542
0
    }
543
544
0
    if (ctx->cipher_info->mode == MBEDTLS_MODE_ECB) {
545
0
        if (ilen != block_size) {
546
0
            return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
547
0
        }
548
549
0
        *olen = ilen;
550
551
0
        if (0 != (ret = ctx->cipher_info->base->ecb_func(ctx->cipher_ctx,
552
0
                                                         ctx->operation, input, output))) {
553
0
            return ret;
554
0
        }
555
556
0
        return 0;
557
0
    }
558
559
0
#if defined(MBEDTLS_GCM_C)
560
0
    if (ctx->cipher_info->mode == MBEDTLS_MODE_GCM) {
561
0
        return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
562
0
                                  input, ilen,
563
0
                                  output, ilen, olen);
564
0
    }
565
0
#endif
566
567
0
#if defined(MBEDTLS_CCM_C)
568
0
    if (ctx->cipher_info->mode == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
569
0
        return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
570
0
                                  input, ilen,
571
0
                                  output, ilen, olen);
572
0
    }
573
0
#endif
574
575
0
#if defined(MBEDTLS_CHACHAPOLY_C)
576
0
    if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
577
0
        *olen = ilen;
578
0
        return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
579
0
                                         ilen, input, output);
580
0
    }
581
0
#endif
582
583
0
    if (input == output &&
584
0
        (ctx->unprocessed_len != 0 || ilen % block_size)) {
585
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
586
0
    }
587
588
0
#if defined(MBEDTLS_CIPHER_MODE_CBC)
589
0
    if (ctx->cipher_info->mode == MBEDTLS_MODE_CBC) {
590
0
        size_t copy_len = 0;
591
592
        /*
593
         * If there is not enough data for a full block, cache it.
594
         */
595
0
        if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
596
0
             ilen <= block_size - ctx->unprocessed_len) ||
597
0
            (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
598
0
             ilen < block_size - ctx->unprocessed_len) ||
599
0
            (ctx->operation == MBEDTLS_ENCRYPT &&
600
0
             ilen < block_size - ctx->unprocessed_len)) {
601
0
            memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
602
0
                   ilen);
603
604
0
            ctx->unprocessed_len += ilen;
605
0
            return 0;
606
0
        }
607
608
        /*
609
         * Process cached data first
610
         */
611
0
        if (0 != ctx->unprocessed_len) {
612
0
            copy_len = block_size - ctx->unprocessed_len;
613
614
0
            memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
615
0
                   copy_len);
616
617
0
            if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
618
0
                                                             ctx->operation, block_size, ctx->iv,
619
0
                                                             ctx->unprocessed_data, output))) {
620
0
                return ret;
621
0
            }
622
623
0
            *olen += block_size;
624
0
            output += block_size;
625
0
            ctx->unprocessed_len = 0;
626
627
0
            input += copy_len;
628
0
            ilen -= copy_len;
629
0
        }
630
631
        /*
632
         * Cache final, incomplete block
633
         */
634
0
        if (0 != ilen) {
635
            /* Encryption: only cache partial blocks
636
             * Decryption w/ padding: always keep at least one whole block
637
             * Decryption w/o padding: only cache partial blocks
638
             */
639
0
            copy_len = ilen % block_size;
640
0
            if (copy_len == 0 &&
641
0
                ctx->operation == MBEDTLS_DECRYPT &&
642
0
                NULL != ctx->add_padding) {
643
0
                copy_len = block_size;
644
0
            }
645
646
0
            memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
647
0
                   copy_len);
648
649
0
            ctx->unprocessed_len += copy_len;
650
0
            ilen -= copy_len;
651
0
        }
652
653
        /*
654
         * Process remaining full blocks
655
         */
656
0
        if (ilen) {
657
0
            if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
658
0
                                                             ctx->operation, ilen, ctx->iv, input,
659
0
                                                             output))) {
660
0
                return ret;
661
0
            }
662
663
0
            *olen += ilen;
664
0
        }
665
666
0
        return 0;
667
0
    }
668
0
#endif /* MBEDTLS_CIPHER_MODE_CBC */
669
670
0
#if defined(MBEDTLS_CIPHER_MODE_CFB)
671
0
    if (ctx->cipher_info->mode == MBEDTLS_MODE_CFB) {
672
0
        if (0 != (ret = ctx->cipher_info->base->cfb_func(ctx->cipher_ctx,
673
0
                                                         ctx->operation, ilen,
674
0
                                                         &ctx->unprocessed_len, ctx->iv,
675
0
                                                         input, output))) {
676
0
            return ret;
677
0
        }
678
679
0
        *olen = ilen;
680
681
0
        return 0;
682
0
    }
683
0
#endif /* MBEDTLS_CIPHER_MODE_CFB */
684
685
0
#if defined(MBEDTLS_CIPHER_MODE_OFB)
686
0
    if (ctx->cipher_info->mode == MBEDTLS_MODE_OFB) {
687
0
        if (0 != (ret = ctx->cipher_info->base->ofb_func(ctx->cipher_ctx,
688
0
                                                         ilen, &ctx->unprocessed_len, ctx->iv,
689
0
                                                         input, output))) {
690
0
            return ret;
691
0
        }
692
693
0
        *olen = ilen;
694
695
0
        return 0;
696
0
    }
697
0
#endif /* MBEDTLS_CIPHER_MODE_OFB */
698
699
0
#if defined(MBEDTLS_CIPHER_MODE_CTR)
700
0
    if (ctx->cipher_info->mode == MBEDTLS_MODE_CTR) {
701
0
        if (0 != (ret = ctx->cipher_info->base->ctr_func(ctx->cipher_ctx,
702
0
                                                         ilen, &ctx->unprocessed_len, ctx->iv,
703
0
                                                         ctx->unprocessed_data, input, output))) {
704
0
            return ret;
705
0
        }
706
707
0
        *olen = ilen;
708
709
0
        return 0;
710
0
    }
711
0
#endif /* MBEDTLS_CIPHER_MODE_CTR */
712
713
0
#if defined(MBEDTLS_CIPHER_MODE_XTS)
714
0
    if (ctx->cipher_info->mode == MBEDTLS_MODE_XTS) {
715
0
        if (ctx->unprocessed_len > 0) {
716
            /* We can only process an entire data unit at a time. */
717
0
            return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
718
0
        }
719
720
0
        ret = ctx->cipher_info->base->xts_func(ctx->cipher_ctx,
721
0
                                               ctx->operation, ilen, ctx->iv, input, output);
722
0
        if (ret != 0) {
723
0
            return ret;
724
0
        }
725
726
0
        *olen = ilen;
727
728
0
        return 0;
729
0
    }
730
0
#endif /* MBEDTLS_CIPHER_MODE_XTS */
731
732
0
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
733
0
    if (ctx->cipher_info->mode == MBEDTLS_MODE_STREAM) {
734
0
        if (0 != (ret = ctx->cipher_info->base->stream_func(ctx->cipher_ctx,
735
0
                                                            ilen, input, output))) {
736
0
            return ret;
737
0
        }
738
739
0
        *olen = ilen;
740
741
0
        return 0;
742
0
    }
743
0
#endif /* MBEDTLS_CIPHER_MODE_STREAM */
744
745
0
    return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
746
0
}
747
748
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
749
#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
750
/*
751
 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
752
 */
753
static void add_pkcs_padding(unsigned char *output, size_t output_len,
754
                             size_t data_len)
755
0
{
756
0
    size_t padding_len = output_len - data_len;
757
0
    unsigned char i;
758
759
0
    for (i = 0; i < padding_len; i++) {
760
0
        output[data_len + i] = (unsigned char) padding_len;
761
0
    }
762
0
}
763
764
static int get_pkcs_padding(unsigned char *input, size_t input_len,
765
                            size_t *data_len)
766
0
{
767
0
    size_t i, pad_idx;
768
0
    unsigned char padding_len, bad = 0;
769
770
0
    if (NULL == input || NULL == data_len) {
771
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
772
0
    }
773
774
0
    padding_len = input[input_len - 1];
775
0
    *data_len = input_len - padding_len;
776
777
    /* Avoid logical || since it results in a branch */
778
0
    bad |= padding_len > input_len;
779
0
    bad |= padding_len == 0;
780
781
    /* The number of bytes checked must be independent of padding_len,
782
     * so pick input_len, which is usually 8 or 16 (one block) */
783
0
    pad_idx = input_len - padding_len;
784
0
    for (i = 0; i < input_len; i++) {
785
0
        bad |= (input[i] ^ padding_len) * (i >= pad_idx);
786
0
    }
787
788
0
    return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
789
0
}
790
#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
791
792
#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
793
/*
794
 * One and zeros padding: fill with 80 00 ... 00
795
 */
796
static void add_one_and_zeros_padding(unsigned char *output,
797
                                      size_t output_len, size_t data_len)
798
0
{
799
0
    size_t padding_len = output_len - data_len;
800
0
    unsigned char i = 0;
801
802
0
    output[data_len] = 0x80;
803
0
    for (i = 1; i < padding_len; i++) {
804
0
        output[data_len + i] = 0x00;
805
0
    }
806
0
}
807
808
static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
809
                                     size_t *data_len)
810
0
{
811
0
    size_t i;
812
0
    unsigned char done = 0, prev_done, bad;
813
814
0
    if (NULL == input || NULL == data_len) {
815
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
816
0
    }
817
818
0
    bad = 0x80;
819
0
    *data_len = 0;
820
0
    for (i = input_len; i > 0; i--) {
821
0
        prev_done = done;
822
0
        done |= (input[i - 1] != 0);
823
0
        *data_len |= (i - 1) * (done != prev_done);
824
0
        bad ^= input[i - 1] * (done != prev_done);
825
0
    }
826
827
0
    return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
828
829
0
}
830
#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
831
832
#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
833
/*
834
 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
835
 */
836
static void add_zeros_and_len_padding(unsigned char *output,
837
                                      size_t output_len, size_t data_len)
838
0
{
839
0
    size_t padding_len = output_len - data_len;
840
0
    unsigned char i = 0;
841
842
0
    for (i = 1; i < padding_len; i++) {
843
0
        output[data_len + i - 1] = 0x00;
844
0
    }
845
0
    output[output_len - 1] = (unsigned char) padding_len;
846
0
}
847
848
static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
849
                                     size_t *data_len)
850
0
{
851
0
    size_t i, pad_idx;
852
0
    unsigned char padding_len, bad = 0;
853
854
0
    if (NULL == input || NULL == data_len) {
855
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
856
0
    }
857
858
0
    padding_len = input[input_len - 1];
859
0
    *data_len = input_len - padding_len;
860
861
    /* Avoid logical || since it results in a branch */
862
0
    bad |= padding_len > input_len;
863
0
    bad |= padding_len == 0;
864
865
    /* The number of bytes checked must be independent of padding_len */
866
0
    pad_idx = input_len - padding_len;
867
0
    for (i = 0; i < input_len - 1; i++) {
868
0
        bad |= input[i] * (i >= pad_idx);
869
0
    }
870
871
0
    return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
872
0
}
873
#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
874
875
#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
876
/*
877
 * Zero padding: fill with 00 ... 00
878
 */
879
static void add_zeros_padding(unsigned char *output,
880
                              size_t output_len, size_t data_len)
881
0
{
882
0
    size_t i;
883
884
0
    for (i = data_len; i < output_len; i++) {
885
0
        output[i] = 0x00;
886
0
    }
887
0
}
888
889
static int get_zeros_padding(unsigned char *input, size_t input_len,
890
                             size_t *data_len)
891
0
{
892
0
    size_t i;
893
0
    unsigned char done = 0, prev_done;
894
895
0
    if (NULL == input || NULL == data_len) {
896
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
897
0
    }
898
899
0
    *data_len = 0;
900
0
    for (i = input_len; i > 0; i--) {
901
0
        prev_done = done;
902
0
        done |= (input[i-1] != 0);
903
0
        *data_len |= i * (done != prev_done);
904
0
    }
905
906
0
    return 0;
907
0
}
908
#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
909
910
/*
911
 * No padding: don't pad :)
912
 *
913
 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
914
 * but a trivial get_padding function
915
 */
916
static int get_no_padding(unsigned char *input, size_t input_len,
917
                          size_t *data_len)
918
0
{
919
0
    if (NULL == input || NULL == data_len) {
920
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
921
0
    }
922
923
0
    *data_len = input_len;
924
925
0
    return 0;
926
0
}
927
#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
928
929
int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
930
                          unsigned char *output, size_t *olen)
931
0
{
932
0
    if (ctx->cipher_info == NULL) {
933
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
934
0
    }
935
936
#if defined(MBEDTLS_USE_PSA_CRYPTO)
937
    if (ctx->psa_enabled == 1) {
938
        /* While PSA Crypto has an API for multipart
939
         * operations, we currently don't make it
940
         * accessible through the cipher layer. */
941
        return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
942
    }
943
#endif /* MBEDTLS_USE_PSA_CRYPTO */
944
945
0
    *olen = 0;
946
947
0
    if (MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
948
0
        MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
949
0
        MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
950
0
        MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
951
0
        MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode ||
952
0
        MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
953
0
        MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) {
954
0
        return 0;
955
0
    }
956
957
0
    if ((MBEDTLS_CIPHER_CHACHA20          == ctx->cipher_info->type) ||
958
0
        (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) {
959
0
        return 0;
960
0
    }
961
962
0
    if (MBEDTLS_MODE_ECB == ctx->cipher_info->mode) {
963
0
        if (ctx->unprocessed_len != 0) {
964
0
            return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
965
0
        }
966
967
0
        return 0;
968
0
    }
969
970
0
#if defined(MBEDTLS_CIPHER_MODE_CBC)
971
0
    if (MBEDTLS_MODE_CBC == ctx->cipher_info->mode) {
972
0
        int ret = 0;
973
974
0
        if (MBEDTLS_ENCRYPT == ctx->operation) {
975
            /* check for 'no padding' mode */
976
0
            if (NULL == ctx->add_padding) {
977
0
                if (0 != ctx->unprocessed_len) {
978
0
                    return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
979
0
                }
980
981
0
                return 0;
982
0
            }
983
984
0
            ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
985
0
                             ctx->unprocessed_len);
986
0
        } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
987
            /*
988
             * For decrypt operations, expect a full block,
989
             * or an empty block if no padding
990
             */
991
0
            if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
992
0
                return 0;
993
0
            }
994
995
0
            return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
996
0
        }
997
998
        /* cipher block */
999
0
        if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
1000
0
                                                         ctx->operation,
1001
0
                                                         mbedtls_cipher_get_block_size(ctx),
1002
0
                                                         ctx->iv,
1003
0
                                                         ctx->unprocessed_data, output))) {
1004
0
            return ret;
1005
0
        }
1006
1007
        /* Set output size for decryption */
1008
0
        if (MBEDTLS_DECRYPT == ctx->operation) {
1009
0
            return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1010
0
                                    olen);
1011
0
        }
1012
1013
        /* Set output size for encryption */
1014
0
        *olen = mbedtls_cipher_get_block_size(ctx);
1015
0
        return 0;
1016
0
    }
1017
#else
1018
    ((void) output);
1019
#endif /* MBEDTLS_CIPHER_MODE_CBC */
1020
1021
0
    return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1022
0
}
1023
1024
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
1025
int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1026
                                    mbedtls_cipher_padding_t mode)
1027
0
{
1028
0
    if (NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode) {
1029
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1030
0
    }
1031
1032
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1033
    if (ctx->psa_enabled == 1) {
1034
        /* While PSA Crypto knows about CBC padding
1035
         * schemes, we currently don't make them
1036
         * accessible through the cipher layer. */
1037
        if (mode != MBEDTLS_PADDING_NONE) {
1038
            return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1039
        }
1040
1041
        return 0;
1042
    }
1043
#endif /* MBEDTLS_USE_PSA_CRYPTO */
1044
1045
0
    switch (mode) {
1046
0
#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
1047
0
        case MBEDTLS_PADDING_PKCS7:
1048
0
            ctx->add_padding = add_pkcs_padding;
1049
0
            ctx->get_padding = get_pkcs_padding;
1050
0
            break;
1051
0
#endif
1052
0
#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
1053
0
        case MBEDTLS_PADDING_ONE_AND_ZEROS:
1054
0
            ctx->add_padding = add_one_and_zeros_padding;
1055
0
            ctx->get_padding = get_one_and_zeros_padding;
1056
0
            break;
1057
0
#endif
1058
0
#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
1059
0
        case MBEDTLS_PADDING_ZEROS_AND_LEN:
1060
0
            ctx->add_padding = add_zeros_and_len_padding;
1061
0
            ctx->get_padding = get_zeros_and_len_padding;
1062
0
            break;
1063
0
#endif
1064
0
#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
1065
0
        case MBEDTLS_PADDING_ZEROS:
1066
0
            ctx->add_padding = add_zeros_padding;
1067
0
            ctx->get_padding = get_zeros_padding;
1068
0
            break;
1069
0
#endif
1070
0
        case MBEDTLS_PADDING_NONE:
1071
0
            ctx->add_padding = NULL;
1072
0
            ctx->get_padding = get_no_padding;
1073
0
            break;
1074
1075
0
        default:
1076
0
            return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1077
0
    }
1078
1079
0
    return 0;
1080
0
}
1081
#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
1082
1083
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
1084
int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1085
                             unsigned char *tag, size_t tag_len)
1086
0
{
1087
0
    if (ctx->cipher_info == NULL) {
1088
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1089
0
    }
1090
1091
0
    if (MBEDTLS_ENCRYPT != ctx->operation) {
1092
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1093
0
    }
1094
1095
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1096
    if (ctx->psa_enabled == 1) {
1097
        /* While PSA Crypto has an API for multipart
1098
         * operations, we currently don't make it
1099
         * accessible through the cipher layer. */
1100
        return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1101
    }
1102
#endif /* MBEDTLS_USE_PSA_CRYPTO */
1103
1104
0
#if defined(MBEDTLS_GCM_C)
1105
0
    if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1106
0
        size_t output_length;
1107
        /* The code here doesn't yet support alternative implementations
1108
         * that can delay up to a block of output. */
1109
0
        return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1110
0
                                  NULL, 0, &output_length,
1111
0
                                  tag, tag_len);
1112
0
    }
1113
0
#endif
1114
1115
0
#if defined(MBEDTLS_CHACHAPOLY_C)
1116
0
    if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
1117
        /* Don't allow truncated MAC for Poly1305 */
1118
0
        if (tag_len != 16U) {
1119
0
            return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1120
0
        }
1121
1122
0
        return mbedtls_chachapoly_finish(
1123
0
            (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
1124
0
    }
1125
0
#endif
1126
1127
0
    return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1128
0
}
1129
1130
int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1131
                             const unsigned char *tag, size_t tag_len)
1132
0
{
1133
0
    unsigned char check_tag[16];
1134
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1135
1136
0
    if (ctx->cipher_info == NULL) {
1137
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1138
0
    }
1139
1140
0
    if (MBEDTLS_DECRYPT != ctx->operation) {
1141
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1142
0
    }
1143
1144
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1145
    if (ctx->psa_enabled == 1) {
1146
        /* While PSA Crypto has an API for multipart
1147
         * operations, we currently don't make it
1148
         * accessible through the cipher layer. */
1149
        return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1150
    }
1151
#endif /* MBEDTLS_USE_PSA_CRYPTO */
1152
1153
    /* Status to return on a non-authenticated algorithm. */
1154
0
    ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1155
1156
0
#if defined(MBEDTLS_GCM_C)
1157
0
    if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1158
0
        size_t output_length;
1159
        /* The code here doesn't yet support alternative implementations
1160
         * that can delay up to a block of output. */
1161
1162
0
        if (tag_len > sizeof(check_tag)) {
1163
0
            return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1164
0
        }
1165
1166
0
        if (0 != (ret = mbedtls_gcm_finish(
1167
0
                      (mbedtls_gcm_context *) ctx->cipher_ctx,
1168
0
                      NULL, 0, &output_length,
1169
0
                      check_tag, tag_len))) {
1170
0
            return ret;
1171
0
        }
1172
1173
        /* Check the tag in "constant-time" */
1174
0
        if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
1175
0
            ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1176
0
            goto exit;
1177
0
        }
1178
0
    }
1179
0
#endif /* MBEDTLS_GCM_C */
1180
1181
0
#if defined(MBEDTLS_CHACHAPOLY_C)
1182
0
    if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
1183
        /* Don't allow truncated MAC for Poly1305 */
1184
0
        if (tag_len != sizeof(check_tag)) {
1185
0
            return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1186
0
        }
1187
1188
0
        ret = mbedtls_chachapoly_finish(
1189
0
            (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1190
0
        if (ret != 0) {
1191
0
            return ret;
1192
0
        }
1193
1194
        /* Check the tag in "constant-time" */
1195
0
        if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
1196
0
            ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1197
0
            goto exit;
1198
0
        }
1199
0
    }
1200
0
#endif /* MBEDTLS_CHACHAPOLY_C */
1201
1202
0
exit:
1203
0
    mbedtls_platform_zeroize(check_tag, tag_len);
1204
0
    return ret;
1205
0
}
1206
#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
1207
1208
/*
1209
 * Packet-oriented wrapper for non-AEAD modes
1210
 */
1211
int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1212
                         const unsigned char *iv, size_t iv_len,
1213
                         const unsigned char *input, size_t ilen,
1214
                         unsigned char *output, size_t *olen)
1215
0
{
1216
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1217
0
    size_t finish_olen;
1218
1219
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1220
    if (ctx->psa_enabled == 1) {
1221
        /* As in the non-PSA case, we don't check that
1222
         * a key has been set. If not, the key slot will
1223
         * still be in its default state of 0, which is
1224
         * guaranteed to be invalid, hence the PSA-call
1225
         * below will gracefully fail. */
1226
        mbedtls_cipher_context_psa * const cipher_psa =
1227
            (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1228
1229
        psa_status_t status;
1230
        psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1231
        size_t part_len;
1232
1233
        if (ctx->operation == MBEDTLS_DECRYPT) {
1234
            status = psa_cipher_decrypt_setup(&cipher_op,
1235
                                              cipher_psa->slot,
1236
                                              cipher_psa->alg);
1237
        } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1238
            status = psa_cipher_encrypt_setup(&cipher_op,
1239
                                              cipher_psa->slot,
1240
                                              cipher_psa->alg);
1241
        } else {
1242
            return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1243
        }
1244
1245
        /* In the following, we can immediately return on an error,
1246
         * because the PSA Crypto API guarantees that cipher operations
1247
         * are terminated by unsuccessful calls to psa_cipher_update(),
1248
         * and by any call to psa_cipher_finish(). */
1249
        if (status != PSA_SUCCESS) {
1250
            return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1251
        }
1252
1253
        if (ctx->cipher_info->mode != MBEDTLS_MODE_ECB) {
1254
            status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1255
            if (status != PSA_SUCCESS) {
1256
                return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1257
            }
1258
        }
1259
1260
        status = psa_cipher_update(&cipher_op,
1261
                                   input, ilen,
1262
                                   output, ilen, olen);
1263
        if (status != PSA_SUCCESS) {
1264
            return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1265
        }
1266
1267
        status = psa_cipher_finish(&cipher_op,
1268
                                   output + *olen, ilen - *olen,
1269
                                   &part_len);
1270
        if (status != PSA_SUCCESS) {
1271
            return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1272
        }
1273
1274
        *olen += part_len;
1275
        return 0;
1276
    }
1277
#endif /* MBEDTLS_USE_PSA_CRYPTO */
1278
1279
0
    if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1280
0
        return ret;
1281
0
    }
1282
1283
0
    if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1284
0
        return ret;
1285
0
    }
1286
1287
0
    if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1288
0
                                     output, olen)) != 0) {
1289
0
        return ret;
1290
0
    }
1291
1292
0
    if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1293
0
                                     &finish_olen)) != 0) {
1294
0
        return ret;
1295
0
    }
1296
1297
0
    *olen += finish_olen;
1298
1299
0
    return 0;
1300
0
}
1301
1302
#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1303
/*
1304
 * Packet-oriented encryption for AEAD modes: internal function used by
1305
 * mbedtls_cipher_auth_encrypt_ext().
1306
 */
1307
static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1308
                                       const unsigned char *iv, size_t iv_len,
1309
                                       const unsigned char *ad, size_t ad_len,
1310
                                       const unsigned char *input, size_t ilen,
1311
                                       unsigned char *output, size_t *olen,
1312
                                       unsigned char *tag, size_t tag_len)
1313
0
{
1314
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1315
    if (ctx->psa_enabled == 1) {
1316
        /* As in the non-PSA case, we don't check that
1317
         * a key has been set. If not, the key slot will
1318
         * still be in its default state of 0, which is
1319
         * guaranteed to be invalid, hence the PSA-call
1320
         * below will gracefully fail. */
1321
        mbedtls_cipher_context_psa * const cipher_psa =
1322
            (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1323
1324
        psa_status_t status;
1325
1326
        /* PSA Crypto API always writes the authentication tag
1327
         * at the end of the encrypted message. */
1328
        if (output == NULL || tag != output + ilen) {
1329
            return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1330
        }
1331
1332
        status = psa_aead_encrypt(cipher_psa->slot,
1333
                                  cipher_psa->alg,
1334
                                  iv, iv_len,
1335
                                  ad, ad_len,
1336
                                  input, ilen,
1337
                                  output, ilen + tag_len, olen);
1338
        if (status != PSA_SUCCESS) {
1339
            return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1340
        }
1341
1342
        *olen -= tag_len;
1343
        return 0;
1344
    }
1345
#endif /* MBEDTLS_USE_PSA_CRYPTO */
1346
1347
0
#if defined(MBEDTLS_GCM_C)
1348
0
    if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1349
0
        *olen = ilen;
1350
0
        return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1351
0
                                         ilen, iv, iv_len, ad, ad_len,
1352
0
                                         input, output, tag_len, tag);
1353
0
    }
1354
0
#endif /* MBEDTLS_GCM_C */
1355
0
#if defined(MBEDTLS_CCM_C)
1356
0
    if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
1357
0
        *olen = ilen;
1358
0
        return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1359
0
                                           iv, iv_len, ad, ad_len, input, output,
1360
0
                                           tag, tag_len);
1361
0
    }
1362
0
#endif /* MBEDTLS_CCM_C */
1363
0
#if defined(MBEDTLS_CHACHAPOLY_C)
1364
0
    if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
1365
        /* ChachaPoly has fixed length nonce and MAC (tag) */
1366
0
        if ((iv_len != ctx->cipher_info->iv_size) ||
1367
0
            (tag_len != 16U)) {
1368
0
            return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1369
0
        }
1370
1371
0
        *olen = ilen;
1372
0
        return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1373
0
                                                  ilen, iv, ad, ad_len, input, output, tag);
1374
0
    }
1375
0
#endif /* MBEDTLS_CHACHAPOLY_C */
1376
1377
0
    return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1378
0
}
1379
1380
/*
1381
 * Packet-oriented encryption for AEAD modes: internal function used by
1382
 * mbedtls_cipher_auth_encrypt_ext().
1383
 */
1384
static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1385
                                       const unsigned char *iv, size_t iv_len,
1386
                                       const unsigned char *ad, size_t ad_len,
1387
                                       const unsigned char *input, size_t ilen,
1388
                                       unsigned char *output, size_t *olen,
1389
                                       const unsigned char *tag, size_t tag_len)
1390
0
{
1391
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1392
    if (ctx->psa_enabled == 1) {
1393
        /* As in the non-PSA case, we don't check that
1394
         * a key has been set. If not, the key slot will
1395
         * still be in its default state of 0, which is
1396
         * guaranteed to be invalid, hence the PSA-call
1397
         * below will gracefully fail. */
1398
        mbedtls_cipher_context_psa * const cipher_psa =
1399
            (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1400
1401
        psa_status_t status;
1402
1403
        /* PSA Crypto API always writes the authentication tag
1404
         * at the end of the encrypted message. */
1405
        if (input == NULL || tag != input + ilen) {
1406
            return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1407
        }
1408
1409
        status = psa_aead_decrypt(cipher_psa->slot,
1410
                                  cipher_psa->alg,
1411
                                  iv, iv_len,
1412
                                  ad, ad_len,
1413
                                  input, ilen + tag_len,
1414
                                  output, ilen, olen);
1415
        if (status == PSA_ERROR_INVALID_SIGNATURE) {
1416
            return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1417
        } else if (status != PSA_SUCCESS) {
1418
            return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1419
        }
1420
1421
        return 0;
1422
    }
1423
#endif /* MBEDTLS_USE_PSA_CRYPTO */
1424
1425
0
#if defined(MBEDTLS_GCM_C)
1426
0
    if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1427
0
        int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1428
1429
0
        *olen = ilen;
1430
0
        ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1431
0
                                       iv, iv_len, ad, ad_len,
1432
0
                                       tag, tag_len, input, output);
1433
1434
0
        if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
1435
0
            ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1436
0
        }
1437
1438
0
        return ret;
1439
0
    }
1440
0
#endif /* MBEDTLS_GCM_C */
1441
0
#if defined(MBEDTLS_CCM_C)
1442
0
    if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
1443
0
        int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1444
1445
0
        *olen = ilen;
1446
0
        ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1447
0
                                       iv, iv_len, ad, ad_len,
1448
0
                                       input, output, tag, tag_len);
1449
1450
0
        if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
1451
0
            ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1452
0
        }
1453
1454
0
        return ret;
1455
0
    }
1456
0
#endif /* MBEDTLS_CCM_C */
1457
0
#if defined(MBEDTLS_CHACHAPOLY_C)
1458
0
    if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
1459
0
        int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1460
1461
        /* ChachaPoly has fixed length nonce and MAC (tag) */
1462
0
        if ((iv_len != ctx->cipher_info->iv_size) ||
1463
0
            (tag_len != 16U)) {
1464
0
            return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1465
0
        }
1466
1467
0
        *olen = ilen;
1468
0
        ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1469
0
                                              iv, ad, ad_len, tag, input, output);
1470
1471
0
        if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
1472
0
            ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1473
0
        }
1474
1475
0
        return ret;
1476
0
    }
1477
0
#endif /* MBEDTLS_CHACHAPOLY_C */
1478
1479
0
    return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1480
0
}
1481
#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1482
1483
#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1484
/*
1485
 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1486
 */
1487
int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1488
                                    const unsigned char *iv, size_t iv_len,
1489
                                    const unsigned char *ad, size_t ad_len,
1490
                                    const unsigned char *input, size_t ilen,
1491
                                    unsigned char *output, size_t output_len,
1492
                                    size_t *olen, size_t tag_len)
1493
0
{
1494
0
#if defined(MBEDTLS_NIST_KW_C)
1495
0
    if (
1496
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1497
        ctx->psa_enabled == 0 &&
1498
#endif
1499
0
        (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1500
0
         MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1501
0
        mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1502
0
                                      MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1503
1504
        /* There is no iv, tag or ad associated with KW and KWP,
1505
         * so these length should be 0 as documented. */
1506
0
        if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1507
0
            return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1508
0
        }
1509
1510
0
        (void) iv;
1511
0
        (void) ad;
1512
1513
0
        return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1514
0
                                    output, olen, output_len);
1515
0
    }
1516
0
#endif /* MBEDTLS_NIST_KW_C */
1517
1518
0
#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1519
    /* AEAD case: check length before passing on to shared function */
1520
0
    if (output_len < ilen + tag_len) {
1521
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1522
0
    }
1523
1524
0
    int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1525
0
                                          input, ilen, output, olen,
1526
0
                                          output + ilen, tag_len);
1527
0
    *olen += tag_len;
1528
0
    return ret;
1529
#else
1530
    return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1531
#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1532
0
}
1533
1534
/*
1535
 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1536
 */
1537
int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1538
                                    const unsigned char *iv, size_t iv_len,
1539
                                    const unsigned char *ad, size_t ad_len,
1540
                                    const unsigned char *input, size_t ilen,
1541
                                    unsigned char *output, size_t output_len,
1542
                                    size_t *olen, size_t tag_len)
1543
0
{
1544
0
#if defined(MBEDTLS_NIST_KW_C)
1545
0
    if (
1546
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1547
        ctx->psa_enabled == 0 &&
1548
#endif
1549
0
        (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1550
0
         MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1551
0
        mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1552
0
                                      MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1553
1554
        /* There is no iv, tag or ad associated with KW and KWP,
1555
         * so these length should be 0 as documented. */
1556
0
        if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1557
0
            return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1558
0
        }
1559
1560
0
        (void) iv;
1561
0
        (void) ad;
1562
1563
0
        return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1564
0
                                      output, olen, output_len);
1565
0
    }
1566
0
#endif /* MBEDTLS_NIST_KW_C */
1567
1568
0
#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1569
    /* AEAD case: check length before passing on to shared function */
1570
0
    if (ilen < tag_len || output_len < ilen - tag_len) {
1571
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1572
0
    }
1573
1574
0
    return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1575
0
                                       input, ilen - tag_len, output, olen,
1576
0
                                       input + ilen - tag_len, tag_len);
1577
#else
1578
    return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1579
#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1580
0
}
1581
#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1582
1583
#endif /* MBEDTLS_CIPHER_C */