Coverage Report

Created: 2025-07-01 06:54

/work/mbedtls-2.28.8/library/ccm.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  NIST SP800-38C compliant CCM implementation
3
 *
4
 *  Copyright The Mbed TLS Contributors
5
 *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6
 */
7
8
/*
9
 * Definition of CCM:
10
 * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
11
 * RFC 3610 "Counter with CBC-MAC (CCM)"
12
 *
13
 * Related:
14
 * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
15
 */
16
17
#include "common.h"
18
19
#if defined(MBEDTLS_CCM_C)
20
21
#include "mbedtls/ccm.h"
22
#include "mbedtls/platform_util.h"
23
#include "mbedtls/error.h"
24
#include "mbedtls/constant_time.h"
25
26
#include <string.h>
27
28
#include "mbedtls/platform.h"
29
30
#if !defined(MBEDTLS_CCM_ALT)
31
32
#define CCM_VALIDATE_RET(cond) \
33
0
    MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_CCM_BAD_INPUT)
34
#define CCM_VALIDATE(cond) \
35
0
    MBEDTLS_INTERNAL_VALIDATE(cond)
36
37
0
#define CCM_ENCRYPT 0
38
0
#define CCM_DECRYPT 1
39
40
/*
41
 * Initialize context
42
 */
43
void mbedtls_ccm_init(mbedtls_ccm_context *ctx)
44
0
{
45
0
    CCM_VALIDATE(ctx != NULL);
46
0
    memset(ctx, 0, sizeof(mbedtls_ccm_context));
47
0
}
48
49
int mbedtls_ccm_setkey(mbedtls_ccm_context *ctx,
50
                       mbedtls_cipher_id_t cipher,
51
                       const unsigned char *key,
52
                       unsigned int keybits)
53
0
{
54
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
55
0
    const mbedtls_cipher_info_t *cipher_info;
56
57
0
    CCM_VALIDATE_RET(ctx != NULL);
58
0
    CCM_VALIDATE_RET(key != NULL);
59
60
0
    cipher_info = mbedtls_cipher_info_from_values(cipher, keybits,
61
0
                                                  MBEDTLS_MODE_ECB);
62
0
    if (cipher_info == NULL) {
63
0
        return MBEDTLS_ERR_CCM_BAD_INPUT;
64
0
    }
65
66
0
    if (cipher_info->block_size != 16) {
67
0
        return MBEDTLS_ERR_CCM_BAD_INPUT;
68
0
    }
69
70
0
    mbedtls_cipher_free(&ctx->cipher_ctx);
71
72
0
    if ((ret = mbedtls_cipher_setup(&ctx->cipher_ctx, cipher_info)) != 0) {
73
0
        return ret;
74
0
    }
75
76
0
    if ((ret = mbedtls_cipher_setkey(&ctx->cipher_ctx, key, keybits,
77
0
                                     MBEDTLS_ENCRYPT)) != 0) {
78
0
        return ret;
79
0
    }
80
81
0
    return 0;
82
0
}
83
84
/*
85
 * Free context
86
 */
87
void mbedtls_ccm_free(mbedtls_ccm_context *ctx)
88
0
{
89
0
    if (ctx == NULL) {
90
0
        return;
91
0
    }
92
0
    mbedtls_cipher_free(&ctx->cipher_ctx);
93
0
    mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ccm_context));
94
0
}
95
96
/*
97
 * Macros for common operations.
98
 * Results in smaller compiled code than static inline functions.
99
 */
100
101
/*
102
 * Update the CBC-MAC state in y using a block in b
103
 * (Always using b as the source helps the compiler optimise a bit better.)
104
 */
105
#define UPDATE_CBC_MAC                                                      \
106
0
    for (i = 0; i < 16; i++)                                               \
107
0
    y[i] ^= b[i];                                                       \
108
0
                                                                            \
109
0
    if ((ret = mbedtls_cipher_update(&ctx->cipher_ctx, y, 16, y, &olen)) != 0) \
110
0
    return ret;
111
112
/*
113
 * Encrypt or decrypt a partial block with CTR
114
 * Warning: using b for temporary storage! src and dst must not be b!
115
 * This avoids allocating one more 16 bytes buffer while allowing src == dst.
116
 */
117
#define CTR_CRYPT(dst, src, len)                                            \
118
0
    do                                                                  \
119
0
    {                                                                   \
120
0
        if ((ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctr,       \
121
0
                                         16, b, &olen)) != 0)      \
122
0
        {                                                               \
123
0
            return ret;                                              \
124
0
        }                                                               \
125
0
                                                                      \
126
0
        for (i = 0; i < (len); i++)                                    \
127
0
        (dst)[i] = (src)[i] ^ b[i];                                 \
128
0
    } while (0)
129
130
/*
131
 * Authenticated encryption or decryption
132
 */
133
static int ccm_auth_crypt(mbedtls_ccm_context *ctx, int mode, size_t length,
134
                          const unsigned char *iv, size_t iv_len,
135
                          const unsigned char *add, size_t add_len,
136
                          const unsigned char *input, unsigned char *output,
137
                          unsigned char *tag, size_t tag_len)
138
0
{
139
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
140
0
    unsigned char i;
141
0
    unsigned char q;
142
0
    size_t len_left, olen;
143
0
    unsigned char b[16];
144
0
    unsigned char y[16];
145
0
    unsigned char ctr[16];
146
0
    const unsigned char *src;
147
0
    unsigned char *dst;
148
149
    /*
150
     * Check length requirements: SP800-38C A.1
151
     * Additional requirement: a < 2^16 - 2^8 to simplify the code.
152
     * 'length' checked later (when writing it to the first block)
153
     *
154
     * Also, loosen the requirements to enable support for CCM* (IEEE 802.15.4).
155
     */
156
0
    if (tag_len == 2 || tag_len > 16 || tag_len % 2 != 0) {
157
0
        return MBEDTLS_ERR_CCM_BAD_INPUT;
158
0
    }
159
160
    /* Also implies q is within bounds */
161
0
    if (iv_len < 7 || iv_len > 13) {
162
0
        return MBEDTLS_ERR_CCM_BAD_INPUT;
163
0
    }
164
165
0
    if (add_len >= 0xFF00) {
166
0
        return MBEDTLS_ERR_CCM_BAD_INPUT;
167
0
    }
168
169
0
    q = 16 - 1 - (unsigned char) iv_len;
170
171
    /*
172
     * First block B_0:
173
     * 0        .. 0        flags
174
     * 1        .. iv_len   nonce (aka iv)
175
     * iv_len+1 .. 15       length
176
     *
177
     * With flags as (bits):
178
     * 7        0
179
     * 6        add present?
180
     * 5 .. 3   (t - 2) / 2
181
     * 2 .. 0   q - 1
182
     */
183
0
    b[0] = 0;
184
0
    b[0] |= (add_len > 0) << 6;
185
0
    b[0] |= ((tag_len - 2) / 2) << 3;
186
0
    b[0] |= q - 1;
187
188
0
    memcpy(b + 1, iv, iv_len);
189
190
0
    for (i = 0, len_left = length; i < q; i++, len_left >>= 8) {
191
0
        b[15-i] = MBEDTLS_BYTE_0(len_left);
192
0
    }
193
194
0
    if (len_left > 0) {
195
0
        return MBEDTLS_ERR_CCM_BAD_INPUT;
196
0
    }
197
198
199
    /* Start CBC-MAC with first block */
200
0
    memset(y, 0, 16);
201
0
    UPDATE_CBC_MAC;
202
203
    /*
204
     * If there is additional data, update CBC-MAC with
205
     * add_len, add, 0 (padding to a block boundary)
206
     */
207
0
    if (add_len > 0) {
208
0
        size_t use_len;
209
0
        len_left = add_len;
210
0
        src = add;
211
212
0
        memset(b, 0, 16);
213
0
        MBEDTLS_PUT_UINT16_BE(add_len, b, 0);
214
215
0
        use_len = len_left < 16 - 2 ? len_left : 16 - 2;
216
0
        memcpy(b + 2, src, use_len);
217
0
        len_left -= use_len;
218
0
        src += use_len;
219
220
0
        UPDATE_CBC_MAC;
221
222
0
        while (len_left > 0) {
223
0
            use_len = len_left > 16 ? 16 : len_left;
224
225
0
            memset(b, 0, 16);
226
0
            memcpy(b, src, use_len);
227
0
            UPDATE_CBC_MAC;
228
229
0
            len_left -= use_len;
230
0
            src += use_len;
231
0
        }
232
0
    }
233
234
    /*
235
     * Prepare counter block for encryption:
236
     * 0        .. 0        flags
237
     * 1        .. iv_len   nonce (aka iv)
238
     * iv_len+1 .. 15       counter (initially 1)
239
     *
240
     * With flags as (bits):
241
     * 7 .. 3   0
242
     * 2 .. 0   q - 1
243
     */
244
0
    ctr[0] = q - 1;
245
0
    memcpy(ctr + 1, iv, iv_len);
246
0
    memset(ctr + 1 + iv_len, 0, q);
247
0
    ctr[15] = 1;
248
249
    /*
250
     * Authenticate and {en,de}crypt the message.
251
     *
252
     * The only difference between encryption and decryption is
253
     * the respective order of authentication and {en,de}cryption.
254
     */
255
0
    len_left = length;
256
0
    src = input;
257
0
    dst = output;
258
259
0
    while (len_left > 0) {
260
0
        size_t use_len = len_left > 16 ? 16 : len_left;
261
262
0
        if (mode == CCM_ENCRYPT) {
263
0
            memset(b, 0, 16);
264
0
            memcpy(b, src, use_len);
265
0
            UPDATE_CBC_MAC;
266
0
        }
267
268
0
        CTR_CRYPT(dst, src, use_len);
269
270
0
        if (mode == CCM_DECRYPT) {
271
0
            memset(b, 0, 16);
272
0
            memcpy(b, dst, use_len);
273
0
            UPDATE_CBC_MAC;
274
0
        }
275
276
0
        dst += use_len;
277
0
        src += use_len;
278
0
        len_left -= use_len;
279
280
        /*
281
         * Increment counter.
282
         * No need to check for overflow thanks to the length check above.
283
         */
284
0
        for (i = 0; i < q; i++) {
285
0
            if (++ctr[15-i] != 0) {
286
0
                break;
287
0
            }
288
0
        }
289
0
    }
290
291
    /*
292
     * Authentication: reset counter and crypt/mask internal tag
293
     */
294
0
    for (i = 0; i < q; i++) {
295
0
        ctr[15-i] = 0;
296
0
    }
297
298
0
    CTR_CRYPT(y, y, 16);
299
0
    memcpy(tag, y, tag_len);
300
301
0
    return 0;
302
0
}
303
304
/*
305
 * Authenticated encryption
306
 */
307
int mbedtls_ccm_star_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length,
308
                                     const unsigned char *iv, size_t iv_len,
309
                                     const unsigned char *add, size_t add_len,
310
                                     const unsigned char *input, unsigned char *output,
311
                                     unsigned char *tag, size_t tag_len)
312
0
{
313
0
    CCM_VALIDATE_RET(ctx != NULL);
314
0
    CCM_VALIDATE_RET(iv != NULL);
315
0
    CCM_VALIDATE_RET(add_len == 0 || add != NULL);
316
0
    CCM_VALIDATE_RET(length == 0 || input != NULL);
317
0
    CCM_VALIDATE_RET(length == 0 || output != NULL);
318
0
    CCM_VALIDATE_RET(tag_len == 0 || tag != NULL);
319
0
    return ccm_auth_crypt(ctx, CCM_ENCRYPT, length, iv, iv_len,
320
0
                          add, add_len, input, output, tag, tag_len);
321
0
}
322
323
int mbedtls_ccm_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length,
324
                                const unsigned char *iv, size_t iv_len,
325
                                const unsigned char *add, size_t add_len,
326
                                const unsigned char *input, unsigned char *output,
327
                                unsigned char *tag, size_t tag_len)
328
0
{
329
0
    CCM_VALIDATE_RET(ctx != NULL);
330
0
    CCM_VALIDATE_RET(iv != NULL);
331
0
    CCM_VALIDATE_RET(add_len == 0 || add != NULL);
332
0
    CCM_VALIDATE_RET(length == 0 || input != NULL);
333
0
    CCM_VALIDATE_RET(length == 0 || output != NULL);
334
0
    CCM_VALIDATE_RET(tag_len == 0 || tag != NULL);
335
0
    if (tag_len == 0) {
336
0
        return MBEDTLS_ERR_CCM_BAD_INPUT;
337
0
    }
338
339
0
    return mbedtls_ccm_star_encrypt_and_tag(ctx, length, iv, iv_len, add,
340
0
                                            add_len, input, output, tag, tag_len);
341
0
}
342
343
/*
344
 * Authenticated decryption
345
 */
346
int mbedtls_ccm_star_auth_decrypt(mbedtls_ccm_context *ctx, size_t length,
347
                                  const unsigned char *iv, size_t iv_len,
348
                                  const unsigned char *add, size_t add_len,
349
                                  const unsigned char *input, unsigned char *output,
350
                                  const unsigned char *tag, size_t tag_len)
351
0
{
352
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
353
0
    unsigned char check_tag[16];
354
0
    int diff;
355
356
0
    CCM_VALIDATE_RET(ctx != NULL);
357
0
    CCM_VALIDATE_RET(iv != NULL);
358
0
    CCM_VALIDATE_RET(add_len == 0 || add != NULL);
359
0
    CCM_VALIDATE_RET(length == 0 || input != NULL);
360
0
    CCM_VALIDATE_RET(length == 0 || output != NULL);
361
0
    CCM_VALIDATE_RET(tag_len == 0 || tag != NULL);
362
363
0
    if ((ret = ccm_auth_crypt(ctx, CCM_DECRYPT, length,
364
0
                              iv, iv_len, add, add_len,
365
0
                              input, output, check_tag, tag_len)) != 0) {
366
0
        return ret;
367
0
    }
368
369
    /* Check tag in "constant-time" */
370
0
    diff = mbedtls_ct_memcmp(tag, check_tag, tag_len);
371
372
0
    if (diff != 0) {
373
0
        mbedtls_platform_zeroize(output, length);
374
0
        return MBEDTLS_ERR_CCM_AUTH_FAILED;
375
0
    }
376
377
0
    return 0;
378
0
}
379
380
int mbedtls_ccm_auth_decrypt(mbedtls_ccm_context *ctx, size_t length,
381
                             const unsigned char *iv, size_t iv_len,
382
                             const unsigned char *add, size_t add_len,
383
                             const unsigned char *input, unsigned char *output,
384
                             const unsigned char *tag, size_t tag_len)
385
0
{
386
0
    CCM_VALIDATE_RET(ctx != NULL);
387
0
    CCM_VALIDATE_RET(iv != NULL);
388
0
    CCM_VALIDATE_RET(add_len == 0 || add != NULL);
389
0
    CCM_VALIDATE_RET(length == 0 || input != NULL);
390
0
    CCM_VALIDATE_RET(length == 0 || output != NULL);
391
0
    CCM_VALIDATE_RET(tag_len == 0 || tag != NULL);
392
393
0
    if (tag_len == 0) {
394
0
        return MBEDTLS_ERR_CCM_BAD_INPUT;
395
0
    }
396
397
0
    return mbedtls_ccm_star_auth_decrypt(ctx, length, iv, iv_len, add,
398
0
                                         add_len, input, output, tag, tag_len);
399
0
}
400
#endif /* !MBEDTLS_CCM_ALT */
401
402
#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
403
/*
404
 * Examples 1 to 3 from SP800-38C Appendix C
405
 */
406
407
0
#define NB_TESTS 3
408
0
#define CCM_SELFTEST_PT_MAX_LEN 24
409
0
#define CCM_SELFTEST_CT_MAX_LEN 32
410
/*
411
 * The data is the same for all tests, only the used length changes
412
 */
413
static const unsigned char key_test_data[] = {
414
    0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
415
    0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
416
};
417
418
static const unsigned char iv_test_data[] = {
419
    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
420
    0x18, 0x19, 0x1a, 0x1b
421
};
422
423
static const unsigned char ad_test_data[] = {
424
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
425
    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
426
    0x10, 0x11, 0x12, 0x13
427
};
428
429
static const unsigned char msg_test_data[CCM_SELFTEST_PT_MAX_LEN] = {
430
    0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
431
    0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
432
    0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
433
};
434
435
static const size_t iv_len_test_data[NB_TESTS] = { 7, 8,  12 };
436
static const size_t add_len_test_data[NB_TESTS] = { 8, 16, 20 };
437
static const size_t msg_len_test_data[NB_TESTS] = { 4, 16, 24 };
438
static const size_t tag_len_test_data[NB_TESTS] = { 4, 6,  8  };
439
440
static const unsigned char res_test_data[NB_TESTS][CCM_SELFTEST_CT_MAX_LEN] = {
441
    {   0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
442
    {   0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
443
        0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
444
        0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
445
    {   0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
446
        0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
447
        0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
448
        0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
449
};
450
451
int mbedtls_ccm_self_test(int verbose)
452
0
{
453
0
    mbedtls_ccm_context ctx;
454
    /*
455
     * Some hardware accelerators require the input and output buffers
456
     * would be in RAM, because the flash is not accessible.
457
     * Use buffers on the stack to hold the test vectors data.
458
     */
459
0
    unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN];
460
0
    unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN];
461
0
    size_t i;
462
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
463
464
0
    mbedtls_ccm_init(&ctx);
465
466
0
    if (mbedtls_ccm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key_test_data,
467
0
                           8 * sizeof(key_test_data)) != 0) {
468
0
        if (verbose != 0) {
469
0
            mbedtls_printf("  CCM: setup failed");
470
0
        }
471
472
0
        return 1;
473
0
    }
474
475
0
    for (i = 0; i < NB_TESTS; i++) {
476
0
        if (verbose != 0) {
477
0
            mbedtls_printf("  CCM-AES #%u: ", (unsigned int) i + 1);
478
0
        }
479
480
0
        memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN);
481
0
        memset(ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN);
482
0
        memcpy(plaintext, msg_test_data, msg_len_test_data[i]);
483
484
0
        ret = mbedtls_ccm_encrypt_and_tag(&ctx, msg_len_test_data[i],
485
0
                                          iv_test_data, iv_len_test_data[i],
486
0
                                          ad_test_data, add_len_test_data[i],
487
0
                                          plaintext, ciphertext,
488
0
                                          ciphertext + msg_len_test_data[i],
489
0
                                          tag_len_test_data[i]);
490
491
0
        if (ret != 0 ||
492
0
            memcmp(ciphertext, res_test_data[i],
493
0
                   msg_len_test_data[i] + tag_len_test_data[i]) != 0) {
494
0
            if (verbose != 0) {
495
0
                mbedtls_printf("failed\n");
496
0
            }
497
498
0
            return 1;
499
0
        }
500
0
        memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN);
501
502
0
        ret = mbedtls_ccm_auth_decrypt(&ctx, msg_len_test_data[i],
503
0
                                       iv_test_data, iv_len_test_data[i],
504
0
                                       ad_test_data, add_len_test_data[i],
505
0
                                       ciphertext, plaintext,
506
0
                                       ciphertext + msg_len_test_data[i],
507
0
                                       tag_len_test_data[i]);
508
509
0
        if (ret != 0 ||
510
0
            memcmp(plaintext, msg_test_data, msg_len_test_data[i]) != 0) {
511
0
            if (verbose != 0) {
512
0
                mbedtls_printf("failed\n");
513
0
            }
514
515
0
            return 1;
516
0
        }
517
518
0
        if (verbose != 0) {
519
0
            mbedtls_printf("passed\n");
520
0
        }
521
0
    }
522
523
0
    mbedtls_ccm_free(&ctx);
524
525
0
    if (verbose != 0) {
526
0
        mbedtls_printf("\n");
527
0
    }
528
529
0
    return 0;
530
0
}
531
532
#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
533
534
#endif /* MBEDTLS_CCM_C */