blob: 2cccd280947e509ad48e0678040ecc9e8c5232b7 [file] [log] [blame]
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02001/*
2 * NIST SP800-38C compliant CCM implementation
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02006 */
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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020017#include "common.h"
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020020
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/ccm.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050022#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000023#include "mbedtls/error.h"
Dave Rodgmand26a3d62023-09-11 18:25:16 +010024#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020025
Rich Evans00ab4702015-02-06 13:43:58 +000026#include <string.h>
27
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evans00ab4702015-02-06 13:43:58 +000030#else
Mateusz Starzyk22f7a352021-07-28 15:08:47 +020031#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_printf printf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Mateusz Starzyk22f7a352021-07-28 15:08:47 +020035#endif /* MBEDTLS_PLATFORM_C */
Rich Evans00ab4702015-02-06 13:43:58 +000036
Steven Cooreman222e2ff2017-04-04 11:37:15 +020037#if !defined(MBEDTLS_CCM_ALT)
38
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +020039
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020040/*
41 * Initialize context
42 */
Gilles Peskine449bd832023-01-11 14:50:10 +010043void mbedtls_ccm_init(mbedtls_ccm_context *ctx)
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020044{
Gilles Peskine449bd832023-01-11 14:50:10 +010045 memset(ctx, 0, sizeof(mbedtls_ccm_context));
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020046}
47
Gilles Peskine449bd832023-01-11 14:50:10 +010048int mbedtls_ccm_setkey(mbedtls_ccm_context *ctx,
49 mbedtls_cipher_id_t cipher,
50 const unsigned char *key,
51 unsigned int keybits)
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020052{
Janos Follath24eed8d2019-11-22 13:21:35 +000053 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 const mbedtls_cipher_info_t *cipher_info;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020055
Gilles Peskine449bd832023-01-11 14:50:10 +010056 cipher_info = mbedtls_cipher_info_from_values(cipher, keybits,
57 MBEDTLS_MODE_ECB);
58 if (cipher_info == NULL) {
59 return MBEDTLS_ERR_CCM_BAD_INPUT;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020060 }
61
Dave Rodgman85a88132023-06-24 11:41:50 +010062 if (mbedtls_cipher_info_get_block_size(cipher_info) != 16) {
Gilles Peskine449bd832023-01-11 14:50:10 +010063 return MBEDTLS_ERR_CCM_BAD_INPUT;
64 }
65
66 mbedtls_cipher_free(&ctx->cipher_ctx);
67
68 if ((ret = mbedtls_cipher_setup(&ctx->cipher_ctx, cipher_info)) != 0) {
69 return ret;
70 }
71
72 if ((ret = mbedtls_cipher_setkey(&ctx->cipher_ctx, key, keybits,
73 MBEDTLS_ENCRYPT)) != 0) {
74 return ret;
75 }
76
77 return 0;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020078}
79
80/*
81 * Free context
82 */
Gilles Peskine449bd832023-01-11 14:50:10 +010083void mbedtls_ccm_free(mbedtls_ccm_context *ctx)
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020084{
Gilles Peskine449bd832023-01-11 14:50:10 +010085 if (ctx == NULL) {
k-stachowiakfd42d532018-12-11 14:37:51 +010086 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010087 }
88 mbedtls_cipher_free(&ctx->cipher_ctx);
89 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ccm_context));
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020090}
91
Mateusz Starzyk793692c2021-06-22 20:34:20 +020092#define CCM_STATE__CLEAR 0
Mateusz Starzyka9cbdfb2021-07-27 13:49:54 +020093#define CCM_STATE__STARTED (1 << 0)
bootstrap-prime6dbbf442022-05-17 19:30:44 -040094#define CCM_STATE__LENGTHS_SET (1 << 1)
Mateusz Starzyk62d22f92021-08-09 15:53:41 +020095#define CCM_STATE__AUTH_DATA_STARTED (1 << 2)
96#define CCM_STATE__AUTH_DATA_FINISHED (1 << 3)
Mateusz Starzyk36d3b892021-07-28 14:14:58 +020097#define CCM_STATE__ERROR (1 << 4)
Mateusz Starzyk793692c2021-06-22 20:34:20 +020098
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +020099/*
100 * Encrypt or decrypt a partial block with CTR
101 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100102static int mbedtls_ccm_crypt(mbedtls_ccm_context *ctx,
103 size_t offset, size_t use_len,
104 const unsigned char *input,
105 unsigned char *output)
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200106{
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200107 size_t olen = 0;
108 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 unsigned char tmp_buf[16] = { 0 };
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 if ((ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->ctr, 16, tmp_buf,
112 &olen)) != 0) {
Mateusz Starzyk2d5652a2021-07-27 16:07:54 +0200113 ctx->state |= CCM_STATE__ERROR;
Mateusz Starzykc52220d2021-07-27 13:54:55 +0200114 mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf));
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200115 return ret;
116 }
117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 mbedtls_xor(output, input, tmp_buf + offset, use_len);
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200119
Mateusz Starzykc52220d2021-07-27 13:54:55 +0200120 mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf));
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200121 return ret;
122}
123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124static void mbedtls_ccm_clear_state(mbedtls_ccm_context *ctx)
125{
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200126 ctx->state = CCM_STATE__CLEAR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 memset(ctx->y, 0, 16);
128 memset(ctx->ctr, 0, 16);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200129}
130
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200131static int ccm_calculate_first_block_if_ready(mbedtls_ccm_context *ctx)
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200132{
Janos Follath24eed8d2019-11-22 13:21:35 +0000133 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200134 unsigned char i;
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200135 size_t len_left, olen;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200136
Tom Cosgrove1797b052022-12-04 17:19:59 +0000137 /* length calculation can be done only after both
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200138 * mbedtls_ccm_starts() and mbedtls_ccm_set_lengths() have been executed
139 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 if (!(ctx->state & CCM_STATE__STARTED) || !(ctx->state & CCM_STATE__LENGTHS_SET)) {
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200141 return 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200143
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200144 /* CCM expects non-empty tag.
145 * CCM* allows empty tag. For CCM* without tag, ignore plaintext length.
146 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 if (ctx->tag_len == 0) {
148 if (ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT || ctx->mode == MBEDTLS_CCM_STAR_DECRYPT) {
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200149 ctx->plaintext_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 } else {
151 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200152 }
153 }
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200154
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200155 /*
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200156 * First block:
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200157 * 0 .. 0 flags
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200158 * 1 .. iv_len nonce (aka iv) - set by: mbedtls_ccm_starts()
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200159 * iv_len+1 .. 15 length
160 *
161 * With flags as (bits):
162 * 7 0
163 * 6 add present?
164 * 5 .. 3 (t - 2) / 2
165 * 2 .. 0 q - 1
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200166 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 ctx->y[0] |= (ctx->add_len > 0) << 6;
168 ctx->y[0] |= ((ctx->tag_len - 2) / 2) << 3;
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200169 ctx->y[0] |= ctx->q - 1;
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 for (i = 0, len_left = ctx->plaintext_len; i < ctx->q; i++, len_left >>= 8) {
172 ctx->y[15-i] = MBEDTLS_BYTE_0(len_left);
173 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if (len_left > 0) {
Mateusz Starzyk88c4d622021-07-05 17:09:16 +0200176 ctx->state |= CCM_STATE__ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk88c4d622021-07-05 17:09:16 +0200178 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200179
180 /* Start CBC-MAC with first block*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if ((ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen)) != 0) {
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200182 ctx->state |= CCM_STATE__ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 return ret;
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200184 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 return 0;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200187}
188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189int mbedtls_ccm_starts(mbedtls_ccm_context *ctx,
190 int mode,
191 const unsigned char *iv,
192 size_t iv_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200193{
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200194 /* Also implies q is within bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 if (iv_len < 7 || iv_len > 13) {
196 return MBEDTLS_ERR_CCM_BAD_INPUT;
197 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200198
Mateusz Starzyk89d469c2021-06-22 16:24:28 +0200199 ctx->mode = mode;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200200 ctx->q = 16 - 1 - (unsigned char) iv_len;
201
202 /*
203 * Prepare counter block for encryption:
204 * 0 .. 0 flags
205 * 1 .. iv_len nonce (aka iv)
206 * iv_len+1 .. 15 counter (initially 1)
207 *
208 * With flags as (bits):
209 * 7 .. 3 0
210 * 2 .. 0 q - 1
211 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 memset(ctx->ctr, 0, 16);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200213 ctx->ctr[0] = ctx->q - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 memcpy(ctx->ctr + 1, iv, iv_len);
215 memset(ctx->ctr + 1 + iv_len, 0, ctx->q);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200216 ctx->ctr[15] = 1;
217
218 /*
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200219 * See ccm_calculate_first_block_if_ready() for block layout description
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200220 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 memcpy(ctx->y + 1, iv, iv_len);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200222
223 ctx->state |= CCM_STATE__STARTED;
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200224 return ccm_calculate_first_block_if_ready(ctx);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200225}
226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227int mbedtls_ccm_set_lengths(mbedtls_ccm_context *ctx,
228 size_t total_ad_len,
229 size_t plaintext_len,
230 size_t tag_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200231{
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200232 /*
233 * Check length requirements: SP800-38C A.1
234 * Additional requirement: a < 2^16 - 2^8 to simplify the code.
235 * 'length' checked later (when writing it to the first block)
Janos Follath997e85c2018-05-29 11:33:45 +0100236 *
237 * Also, loosen the requirements to enable support for CCM* (IEEE 802.15.4).
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200238 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if (tag_len == 2 || tag_len > 16 || tag_len % 2 != 0) {
240 return MBEDTLS_ERR_CCM_BAD_INPUT;
241 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if (total_ad_len >= 0xFF00) {
244 return MBEDTLS_ERR_CCM_BAD_INPUT;
245 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200246
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200247 ctx->plaintext_len = plaintext_len;
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200248 ctx->add_len = total_ad_len;
249 ctx->tag_len = tag_len;
250 ctx->processed = 0;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200251
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400252 ctx->state |= CCM_STATE__LENGTHS_SET;
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200253 return ccm_calculate_first_block_if_ready(ctx);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200254}
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256int mbedtls_ccm_update_ad(mbedtls_ccm_context *ctx,
257 const unsigned char *add,
258 size_t add_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200259{
260 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk33392452021-07-06 15:38:35 +0200261 size_t olen, use_len, offset;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if (ctx->state & CCM_STATE__ERROR) {
Mateusz Starzyk64f0b5f2021-09-02 11:50:38 +0200264 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk2d5652a2021-07-27 16:07:54 +0200265 }
266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (add_len > 0) {
268 if (ctx->state & CCM_STATE__AUTH_DATA_FINISHED) {
Mateusz Starzyk7251eda2021-09-01 13:26:44 +0200269 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200270 }
271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (!(ctx->state & CCM_STATE__AUTH_DATA_STARTED)) {
273 if (add_len > ctx->add_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200274 return MBEDTLS_ERR_CCM_BAD_INPUT;
275 }
276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 ctx->y[0] ^= (unsigned char) ((ctx->add_len >> 8) & 0xFF);
278 ctx->y[1] ^= (unsigned char) ((ctx->add_len) & 0xFF);
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200279
Mateusz Starzyk62d22f92021-08-09 15:53:41 +0200280 ctx->state |= CCM_STATE__AUTH_DATA_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 } else if (ctx->processed + add_len > ctx->add_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200282 return MBEDTLS_ERR_CCM_BAD_INPUT;
283 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 while (add_len > 0) {
Mateusz Starzyk62d22f92021-08-09 15:53:41 +0200286 offset = (ctx->processed + 2) % 16; /* account for y[0] and y[1]
287 * holding total auth data length */
Mateusz Starzyk33392452021-07-06 15:38:35 +0200288 use_len = 16 - offset;
289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if (use_len > add_len) {
Mateusz Starzyk33392452021-07-06 15:38:35 +0200291 use_len = add_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 mbedtls_xor(ctx->y + offset, ctx->y + offset, add, use_len);
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200295
Mateusz Starzyk33392452021-07-06 15:38:35 +0200296 ctx->processed += use_len;
297 add_len -= use_len;
298 add += use_len;
299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (use_len + offset == 16 || ctx->processed == ctx->add_len) {
301 if ((ret =
302 mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen)) != 0) {
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200303 ctx->state |= CCM_STATE__ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return ret;
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200305 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200306 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200307 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 if (ctx->processed == ctx->add_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200310 ctx->state |= CCM_STATE__AUTH_DATA_FINISHED;
311 ctx->processed = 0; // prepare for mbedtls_ccm_update()
312 }
313 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return 0;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200316}
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318int mbedtls_ccm_update(mbedtls_ccm_context *ctx,
319 const unsigned char *input, size_t input_len,
320 unsigned char *output, size_t output_size,
321 size_t *output_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200322{
Mateusz Starzykb73c3ec2021-08-09 15:55:38 +0200323 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200324 unsigned char i;
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200325 size_t use_len, offset, olen;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200326
Mateusz Starzykf3378502021-08-09 11:32:11 +0200327 unsigned char local_output[16];
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 if (ctx->state & CCM_STATE__ERROR) {
Mateusz Starzyk64f0b5f2021-09-02 11:50:38 +0200330 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk2d5652a2021-07-27 16:07:54 +0200331 }
332
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200333 /* Check against plaintext length only if performing operation with
334 * authentication
335 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (ctx->tag_len != 0 && ctx->processed + input_len > ctx->plaintext_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200337 return MBEDTLS_ERR_CCM_BAD_INPUT;
338 }
339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (output_size < input_len) {
341 return MBEDTLS_ERR_CCM_BAD_INPUT;
342 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200343 *output_len = input_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200344
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200345 ret = 0;
346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 while (input_len > 0) {
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200348 offset = ctx->processed % 16;
349
350 use_len = 16 - offset;
351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (use_len > input_len) {
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200353 use_len = input_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 }
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200355
356 ctx->processed += use_len;
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (ctx->mode == MBEDTLS_CCM_ENCRYPT || \
359 ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT) {
360 mbedtls_xor(ctx->y + offset, ctx->y + offset, input, use_len);
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) {
363 if ((ret =
364 mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen)) != 0) {
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200365 ctx->state |= CCM_STATE__ERROR;
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200366 goto exit;
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200367 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200368 }
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 ret = mbedtls_ccm_crypt(ctx, offset, use_len, input, output);
371 if (ret != 0) {
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200372 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200374 }
375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (ctx->mode == MBEDTLS_CCM_DECRYPT || \
377 ctx->mode == MBEDTLS_CCM_STAR_DECRYPT) {
Mateusz Starzyk2f175492021-08-09 16:05:14 +0200378 /* Since output may be in shared memory, we cannot be sure that
379 * it will contain what we wrote to it. Therefore, we should avoid using
380 * it as input to any operations.
381 * Write decrypted data to local_output to avoid using output variable as
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200382 * input in the XOR operation for Y.
383 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 ret = mbedtls_ccm_crypt(ctx, offset, use_len, input, local_output);
385 if (ret != 0) {
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200386 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 mbedtls_xor(ctx->y + offset, ctx->y + offset, local_output, use_len);
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 memcpy(output, local_output, use_len);
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) {
394 if ((ret =
395 mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen)) != 0) {
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200396 ctx->state |= CCM_STATE__ERROR;
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200397 goto exit;
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200398 }
399 }
400 }
401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) {
403 for (i = 0; i < ctx->q; i++) {
404 if (++(ctx->ctr)[15-i] != 0) {
405 break;
406 }
407 }
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200408 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200409
410 input_len -= use_len;
411 input += use_len;
412 output += use_len;
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200413 }
414
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200415exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 mbedtls_platform_zeroize(local_output, 16);
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200417
418 return ret;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200419}
420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421int mbedtls_ccm_finish(mbedtls_ccm_context *ctx,
422 unsigned char *tag, size_t tag_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200423{
Mateusz Starzykb73c3ec2021-08-09 15:55:38 +0200424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200425 unsigned char i;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (ctx->state & CCM_STATE__ERROR) {
Mateusz Starzyk4f2dd8a2021-08-09 15:37:47 +0200428 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk2d5652a2021-07-27 16:07:54 +0200429 }
430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if (ctx->add_len > 0 && !(ctx->state & CCM_STATE__AUTH_DATA_FINISHED)) {
Mateusz Starzyk7251eda2021-09-01 13:26:44 +0200432 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200433 }
434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (ctx->plaintext_len > 0 && ctx->processed != ctx->plaintext_len) {
Mateusz Starzyk7251eda2021-09-01 13:26:44 +0200436 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200437 }
438
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200439 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200440 * Authentication: reset counter and crypt/mask internal tag
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200441 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 for (i = 0; i < ctx->q; i++) {
Mateusz Starzyk89d469c2021-06-22 16:24:28 +0200443 ctx->ctr[15-i] = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 ret = mbedtls_ccm_crypt(ctx, 0, 16, ctx->y, ctx->y);
447 if (ret != 0) {
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200448 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 }
450 if (tag != NULL) {
451 memcpy(tag, ctx->y, tag_len);
452 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200453 mbedtls_ccm_clear_state(ctx);
454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 return 0;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200456}
457
458/*
459 * Authenticated encryption or decryption
460 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100461static int ccm_auth_crypt(mbedtls_ccm_context *ctx, int mode, size_t length,
462 const unsigned char *iv, size_t iv_len,
463 const unsigned char *add, size_t add_len,
464 const unsigned char *input, unsigned char *output,
465 unsigned char *tag, size_t tag_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200466{
467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
468 size_t olen;
469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if ((ret = mbedtls_ccm_starts(ctx, mode, iv, iv_len)) != 0) {
471 return ret;
472 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 if ((ret = mbedtls_ccm_set_lengths(ctx, add_len, length, tag_len)) != 0) {
475 return ret;
476 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 if ((ret = mbedtls_ccm_update_ad(ctx, add, add_len)) != 0) {
479 return ret;
480 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if ((ret = mbedtls_ccm_update(ctx, input, length,
483 output, length, &olen)) != 0) {
484 return ret;
485 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if ((ret = mbedtls_ccm_finish(ctx, tag, tag_len)) != 0) {
488 return ret;
489 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 return 0;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200492}
493
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200494/*
495 * Authenticated encryption
496 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497int mbedtls_ccm_star_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length,
498 const unsigned char *iv, size_t iv_len,
499 const unsigned char *add, size_t add_len,
500 const unsigned char *input, unsigned char *output,
501 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200502{
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 return ccm_auth_crypt(ctx, MBEDTLS_CCM_STAR_ENCRYPT, length, iv, iv_len,
504 add, add_len, input, output, tag, tag_len);
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200505}
506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507int mbedtls_ccm_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length,
508 const unsigned char *iv, size_t iv_len,
509 const unsigned char *add, size_t add_len,
510 const unsigned char *input, unsigned char *output,
511 unsigned char *tag, size_t tag_len)
Janos Follathb5734a22018-05-14 14:31:49 +0100512{
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 return ccm_auth_crypt(ctx, MBEDTLS_CCM_ENCRYPT, length, iv, iv_len,
514 add, add_len, input, output, tag, tag_len);
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200515}
Janos Follathb5734a22018-05-14 14:31:49 +0100516
Mateusz Starzykeb395c02021-07-28 15:10:54 +0200517/*
518 * Authenticated decryption
519 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100520static int mbedtls_ccm_compare_tags(const unsigned char *tag1,
521 const unsigned char *tag2,
522 size_t tag_len)
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200523{
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200524 /* Check tag in "constant-time" */
Dave Rodgmand26a3d62023-09-11 18:25:16 +0100525 int diff = mbedtls_ct_memcmp(tag1, tag2, tag_len);
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 if (diff != 0) {
528 return MBEDTLS_ERR_CCM_AUTH_FAILED;
529 }
530
531 return 0;
Janos Follathb5734a22018-05-14 14:31:49 +0100532}
533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534static int ccm_auth_decrypt(mbedtls_ccm_context *ctx, int mode, size_t length,
535 const unsigned char *iv, size_t iv_len,
536 const unsigned char *add, size_t add_len,
537 const unsigned char *input, unsigned char *output,
538 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200539{
Janos Follath24eed8d2019-11-22 13:21:35 +0000540 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200541 unsigned char check_tag[16];
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if ((ret = ccm_auth_crypt(ctx, mode, length,
544 iv, iv_len, add, add_len,
545 input, output, check_tag, tag_len)) != 0) {
546 return ret;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200547 }
548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if ((ret = mbedtls_ccm_compare_tags(tag, check_tag, tag_len)) != 0) {
550 mbedtls_platform_zeroize(output, length);
551 return ret;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200552 }
553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 return 0;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200555}
556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557int mbedtls_ccm_star_auth_decrypt(mbedtls_ccm_context *ctx, size_t length,
558 const unsigned char *iv, size_t iv_len,
559 const unsigned char *add, size_t add_len,
560 const unsigned char *input, unsigned char *output,
561 const unsigned char *tag, size_t tag_len)
Mateusz Starzyk1bda9452021-07-28 15:21:46 +0200562{
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 return ccm_auth_decrypt(ctx, MBEDTLS_CCM_STAR_DECRYPT, length,
564 iv, iv_len, add, add_len,
565 input, output, tag, tag_len);
Mateusz Starzyk1bda9452021-07-28 15:21:46 +0200566}
567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568int mbedtls_ccm_auth_decrypt(mbedtls_ccm_context *ctx, size_t length,
569 const unsigned char *iv, size_t iv_len,
570 const unsigned char *add, size_t add_len,
571 const unsigned char *input, unsigned char *output,
572 const unsigned char *tag, size_t tag_len)
Janos Follathb5734a22018-05-14 14:31:49 +0100573{
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 return ccm_auth_decrypt(ctx, MBEDTLS_CCM_DECRYPT, length,
575 iv, iv_len, add, add_len,
576 input, output, tag, tag_len);
Janos Follathb5734a22018-05-14 14:31:49 +0100577}
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200578#endif /* !MBEDTLS_CCM_ALT */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200581/*
582 * Examples 1 to 3 from SP800-38C Appendix C
583 */
584
585#define NB_TESTS 3
Ron Eldor1b9b2172018-04-26 14:15:01 +0300586#define CCM_SELFTEST_PT_MAX_LEN 24
587#define CCM_SELFTEST_CT_MAX_LEN 32
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200588/*
589 * The data is the same for all tests, only the used length changes
590 */
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100591static const unsigned char key_test_data[] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200592 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
593 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
594};
595
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100596static const unsigned char iv_test_data[] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200597 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
598 0x18, 0x19, 0x1a, 0x1b
599};
600
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100601static const unsigned char ad_test_data[] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200602 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
603 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
604 0x10, 0x11, 0x12, 0x13
605};
606
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100607static const unsigned char msg_test_data[CCM_SELFTEST_PT_MAX_LEN] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200608 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
609 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
610 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
611};
612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613static const size_t iv_len_test_data[NB_TESTS] = { 7, 8, 12 };
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100614static const size_t add_len_test_data[NB_TESTS] = { 8, 16, 20 };
615static const size_t msg_len_test_data[NB_TESTS] = { 4, 16, 24 };
616static const size_t tag_len_test_data[NB_TESTS] = { 4, 6, 8 };
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200617
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100618static const unsigned char res_test_data[NB_TESTS][CCM_SELFTEST_CT_MAX_LEN] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200619 { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
620 { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
621 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
622 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
623 { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
624 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
625 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
626 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
627};
628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629int mbedtls_ccm_self_test(int verbose)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200630{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_ccm_context ctx;
Ron Eldor1b9b2172018-04-26 14:15:01 +0300632 /*
633 * Some hardware accelerators require the input and output buffers
634 * would be in RAM, because the flash is not accessible.
635 * Use buffers on the stack to hold the test vectors data.
636 */
637 unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN];
638 unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200639 size_t i;
Janos Follath24eed8d2019-11-22 13:21:35 +0000640 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200641
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 mbedtls_ccm_init(&ctx);
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (mbedtls_ccm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key_test_data,
Dave Rodgman6dd757a2023-02-02 12:40:50 +0000645 8 * sizeof(key_test_data)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (verbose != 0) {
647 mbedtls_printf(" CCM: setup failed");
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200648 }
649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 return 1;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200651 }
652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 for (i = 0; i < NB_TESTS; i++) {
654 if (verbose != 0) {
655 mbedtls_printf(" CCM-AES #%u: ", (unsigned int) i + 1);
656 }
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN);
659 memset(ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN);
660 memcpy(plaintext, msg_test_data, msg_len_test_data[i]);
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 ret = mbedtls_ccm_encrypt_and_tag(&ctx, msg_len_test_data[i],
663 iv_test_data, iv_len_test_data[i],
664 ad_test_data, add_len_test_data[i],
665 plaintext, ciphertext,
666 ciphertext + msg_len_test_data[i],
667 tag_len_test_data[i]);
668
669 if (ret != 0 ||
670 memcmp(ciphertext, res_test_data[i],
671 msg_len_test_data[i] + tag_len_test_data[i]) != 0) {
672 if (verbose != 0) {
673 mbedtls_printf("failed\n");
674 }
675
676 return 1;
677 }
678 memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN);
679
680 ret = mbedtls_ccm_auth_decrypt(&ctx, msg_len_test_data[i],
681 iv_test_data, iv_len_test_data[i],
682 ad_test_data, add_len_test_data[i],
683 ciphertext, plaintext,
684 ciphertext + msg_len_test_data[i],
685 tag_len_test_data[i]);
686
687 if (ret != 0 ||
688 memcmp(plaintext, msg_test_data, msg_len_test_data[i]) != 0) {
689 if (verbose != 0) {
690 mbedtls_printf("failed\n");
691 }
692
693 return 1;
694 }
695
696 if (verbose != 0) {
697 mbedtls_printf("passed\n");
698 }
699 }
700
701 mbedtls_ccm_free(&ctx);
702
703 if (verbose != 0) {
704 mbedtls_printf("\n");
705 }
706
707 return 0;
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200708}
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712#endif /* MBEDTLS_CCM_C */