blob: 444351df0ec5eaf962e71ca3f37f7ebb9f017c60 [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
Valerio Setti9b7a8b22023-11-16 08:24:51 +010026#if !defined(MBEDTLS_CIPHER_C)
27#include "block_cipher_internal.h"
28#endif
29
Rich Evans00ab4702015-02-06 13:43:58 +000030#include <string.h>
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/platform.h"
Rich Evans00ab4702015-02-06 13:43:58 +000034#else
Mateusz Starzyk22f7a352021-07-28 15:08:47 +020035#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Rich Evans00ab4702015-02-06 13:43:58 +000036#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define mbedtls_printf printf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Mateusz Starzyk22f7a352021-07-28 15:08:47 +020039#endif /* MBEDTLS_PLATFORM_C */
Rich Evans00ab4702015-02-06 13:43:58 +000040
Steven Cooreman222e2ff2017-04-04 11:37:15 +020041#if !defined(MBEDTLS_CCM_ALT)
42
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +020043
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020044/*
45 * Initialize context
46 */
Gilles Peskine449bd832023-01-11 14:50:10 +010047void mbedtls_ccm_init(mbedtls_ccm_context *ctx)
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020048{
Gilles Peskine449bd832023-01-11 14:50:10 +010049 memset(ctx, 0, sizeof(mbedtls_ccm_context));
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020050}
51
Gilles Peskine449bd832023-01-11 14:50:10 +010052int mbedtls_ccm_setkey(mbedtls_ccm_context *ctx,
53 mbedtls_cipher_id_t cipher,
54 const unsigned char *key,
55 unsigned int keybits)
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020056{
Janos Follath24eed8d2019-11-22 13:21:35 +000057 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti9b7a8b22023-11-16 08:24:51 +010058
59#if defined(MBEDTLS_CIPHER_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 const mbedtls_cipher_info_t *cipher_info;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020061
Gilles Peskine449bd832023-01-11 14:50:10 +010062 cipher_info = mbedtls_cipher_info_from_values(cipher, keybits,
63 MBEDTLS_MODE_ECB);
64 if (cipher_info == NULL) {
65 return MBEDTLS_ERR_CCM_BAD_INPUT;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020066 }
67
Dave Rodgman85a88132023-06-24 11:41:50 +010068 if (mbedtls_cipher_info_get_block_size(cipher_info) != 16) {
Gilles Peskine449bd832023-01-11 14:50:10 +010069 return MBEDTLS_ERR_CCM_BAD_INPUT;
70 }
71
72 mbedtls_cipher_free(&ctx->cipher_ctx);
73
74 if ((ret = mbedtls_cipher_setup(&ctx->cipher_ctx, cipher_info)) != 0) {
75 return ret;
76 }
77
78 if ((ret = mbedtls_cipher_setkey(&ctx->cipher_ctx, key, keybits,
79 MBEDTLS_ENCRYPT)) != 0) {
80 return ret;
81 }
Valerio Setti9b7a8b22023-11-16 08:24:51 +010082#else
83 mbedtls_block_cipher_free(&ctx->block_cipher_ctx);
84
85 if ((ret = mbedtls_block_cipher_setup(&ctx->block_cipher_ctx, cipher)) != 0) {
86 return MBEDTLS_ERR_CCM_BAD_INPUT;
87 }
88
89 if ((ret = mbedtls_block_cipher_setkey(&ctx->block_cipher_ctx, key, keybits)) != 0) {
90 return MBEDTLS_ERR_CCM_BAD_INPUT;
91 }
92#endif
Gilles Peskine449bd832023-01-11 14:50:10 +010093
94 return 0;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020095}
96
97/*
98 * Free context
99 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100100void mbedtls_ccm_free(mbedtls_ccm_context *ctx)
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200101{
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 if (ctx == NULL) {
k-stachowiakfd42d532018-12-11 14:37:51 +0100103 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 }
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100105#if defined(MBEDTLS_CIPHER_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 mbedtls_cipher_free(&ctx->cipher_ctx);
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100107#else
108 mbedtls_block_cipher_free(&ctx->block_cipher_ctx);
109#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ccm_context));
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200111}
112
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200113#define CCM_STATE__CLEAR 0
Mateusz Starzyka9cbdfb2021-07-27 13:49:54 +0200114#define CCM_STATE__STARTED (1 << 0)
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400115#define CCM_STATE__LENGTHS_SET (1 << 1)
Mateusz Starzyk62d22f92021-08-09 15:53:41 +0200116#define CCM_STATE__AUTH_DATA_STARTED (1 << 2)
117#define CCM_STATE__AUTH_DATA_FINISHED (1 << 3)
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200118#define CCM_STATE__ERROR (1 << 4)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200119
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200120/*
121 * Encrypt or decrypt a partial block with CTR
122 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100123static int mbedtls_ccm_crypt(mbedtls_ccm_context *ctx,
124 size_t offset, size_t use_len,
125 const unsigned char *input,
126 unsigned char *output)
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200127{
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200128 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 unsigned char tmp_buf[16] = { 0 };
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200130
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100131#if defined(MBEDTLS_CIPHER_C)
132 size_t olen = 0;
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 if ((ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->ctr, 16, tmp_buf,
135 &olen)) != 0) {
Mateusz Starzyk2d5652a2021-07-27 16:07:54 +0200136 ctx->state |= CCM_STATE__ERROR;
Mateusz Starzykc52220d2021-07-27 13:54:55 +0200137 mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf));
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200138 return ret;
139 }
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100140#else
141 if ((ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->ctr, tmp_buf)) != 0) {
142 ctx->state |= CCM_STATE__ERROR;
143 mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf));
144 return ret;
145 }
146#endif
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 mbedtls_xor(output, input, tmp_buf + offset, use_len);
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200149
Mateusz Starzykc52220d2021-07-27 13:54:55 +0200150 mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf));
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200151 return ret;
152}
153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154static void mbedtls_ccm_clear_state(mbedtls_ccm_context *ctx)
155{
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200156 ctx->state = CCM_STATE__CLEAR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 memset(ctx->y, 0, 16);
158 memset(ctx->ctr, 0, 16);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200159}
160
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200161static int ccm_calculate_first_block_if_ready(mbedtls_ccm_context *ctx)
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200162{
Janos Follath24eed8d2019-11-22 13:21:35 +0000163 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200164 unsigned char i;
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100165 size_t len_left;
166#if defined(MBEDTLS_CIPHER_C)
167 size_t olen;
168#endif
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200169
Tom Cosgrove1797b052022-12-04 17:19:59 +0000170 /* length calculation can be done only after both
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200171 * mbedtls_ccm_starts() and mbedtls_ccm_set_lengths() have been executed
172 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 if (!(ctx->state & CCM_STATE__STARTED) || !(ctx->state & CCM_STATE__LENGTHS_SET)) {
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200174 return 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200176
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200177 /* CCM expects non-empty tag.
178 * CCM* allows empty tag. For CCM* without tag, ignore plaintext length.
179 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (ctx->tag_len == 0) {
181 if (ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT || ctx->mode == MBEDTLS_CCM_STAR_DECRYPT) {
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200182 ctx->plaintext_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 } else {
184 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200185 }
186 }
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200187
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200188 /*
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200189 * First block:
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200190 * 0 .. 0 flags
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200191 * 1 .. iv_len nonce (aka iv) - set by: mbedtls_ccm_starts()
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200192 * iv_len+1 .. 15 length
193 *
194 * With flags as (bits):
195 * 7 0
196 * 6 add present?
197 * 5 .. 3 (t - 2) / 2
198 * 2 .. 0 q - 1
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200199 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 ctx->y[0] |= (ctx->add_len > 0) << 6;
201 ctx->y[0] |= ((ctx->tag_len - 2) / 2) << 3;
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200202 ctx->y[0] |= ctx->q - 1;
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 for (i = 0, len_left = ctx->plaintext_len; i < ctx->q; i++, len_left >>= 8) {
205 ctx->y[15-i] = MBEDTLS_BYTE_0(len_left);
206 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if (len_left > 0) {
Mateusz Starzyk88c4d622021-07-05 17:09:16 +0200209 ctx->state |= CCM_STATE__ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk88c4d622021-07-05 17:09:16 +0200211 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200212
213 /* Start CBC-MAC with first block*/
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100214#if defined(MBEDTLS_CIPHER_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 if ((ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen)) != 0) {
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200216 ctx->state |= CCM_STATE__ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return ret;
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200218 }
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100219#else
220 if ((ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y)) != 0) {
221 ctx->state |= CCM_STATE__ERROR;
222 return ret;
223 }
224#endif
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return 0;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200227}
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229int mbedtls_ccm_starts(mbedtls_ccm_context *ctx,
230 int mode,
231 const unsigned char *iv,
232 size_t iv_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200233{
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200234 /* Also implies q is within bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 if (iv_len < 7 || iv_len > 13) {
236 return MBEDTLS_ERR_CCM_BAD_INPUT;
237 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200238
Mateusz Starzyk89d469c2021-06-22 16:24:28 +0200239 ctx->mode = mode;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200240 ctx->q = 16 - 1 - (unsigned char) iv_len;
241
242 /*
243 * Prepare counter block for encryption:
244 * 0 .. 0 flags
245 * 1 .. iv_len nonce (aka iv)
246 * iv_len+1 .. 15 counter (initially 1)
247 *
248 * With flags as (bits):
249 * 7 .. 3 0
250 * 2 .. 0 q - 1
251 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 memset(ctx->ctr, 0, 16);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200253 ctx->ctr[0] = ctx->q - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 memcpy(ctx->ctr + 1, iv, iv_len);
255 memset(ctx->ctr + 1 + iv_len, 0, ctx->q);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200256 ctx->ctr[15] = 1;
257
258 /*
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200259 * See ccm_calculate_first_block_if_ready() for block layout description
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200260 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 memcpy(ctx->y + 1, iv, iv_len);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200262
263 ctx->state |= CCM_STATE__STARTED;
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200264 return ccm_calculate_first_block_if_ready(ctx);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200265}
266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267int mbedtls_ccm_set_lengths(mbedtls_ccm_context *ctx,
268 size_t total_ad_len,
269 size_t plaintext_len,
270 size_t tag_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200271{
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200272 /*
273 * Check length requirements: SP800-38C A.1
274 * Additional requirement: a < 2^16 - 2^8 to simplify the code.
275 * 'length' checked later (when writing it to the first block)
Janos Follath997e85c2018-05-29 11:33:45 +0100276 *
277 * Also, loosen the requirements to enable support for CCM* (IEEE 802.15.4).
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200278 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (tag_len == 2 || tag_len > 16 || tag_len % 2 != 0) {
280 return MBEDTLS_ERR_CCM_BAD_INPUT;
281 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 if (total_ad_len >= 0xFF00) {
284 return MBEDTLS_ERR_CCM_BAD_INPUT;
285 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200286
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200287 ctx->plaintext_len = plaintext_len;
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200288 ctx->add_len = total_ad_len;
289 ctx->tag_len = tag_len;
290 ctx->processed = 0;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200291
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400292 ctx->state |= CCM_STATE__LENGTHS_SET;
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200293 return ccm_calculate_first_block_if_ready(ctx);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200294}
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296int mbedtls_ccm_update_ad(mbedtls_ccm_context *ctx,
297 const unsigned char *add,
298 size_t add_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200299{
300 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100301 size_t use_len, offset;
302#if defined(MBEDTLS_CIPHER_C)
303 size_t olen;
304#endif
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if (ctx->state & CCM_STATE__ERROR) {
Mateusz Starzyk64f0b5f2021-09-02 11:50:38 +0200307 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk2d5652a2021-07-27 16:07:54 +0200308 }
309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if (add_len > 0) {
311 if (ctx->state & CCM_STATE__AUTH_DATA_FINISHED) {
Mateusz Starzyk7251eda2021-09-01 13:26:44 +0200312 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200313 }
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if (!(ctx->state & CCM_STATE__AUTH_DATA_STARTED)) {
316 if (add_len > ctx->add_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200317 return MBEDTLS_ERR_CCM_BAD_INPUT;
318 }
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 ctx->y[0] ^= (unsigned char) ((ctx->add_len >> 8) & 0xFF);
321 ctx->y[1] ^= (unsigned char) ((ctx->add_len) & 0xFF);
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200322
Mateusz Starzyk62d22f92021-08-09 15:53:41 +0200323 ctx->state |= CCM_STATE__AUTH_DATA_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 } else if (ctx->processed + add_len > ctx->add_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200325 return MBEDTLS_ERR_CCM_BAD_INPUT;
326 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 while (add_len > 0) {
Mateusz Starzyk62d22f92021-08-09 15:53:41 +0200329 offset = (ctx->processed + 2) % 16; /* account for y[0] and y[1]
330 * holding total auth data length */
Mateusz Starzyk33392452021-07-06 15:38:35 +0200331 use_len = 16 - offset;
332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (use_len > add_len) {
Mateusz Starzyk33392452021-07-06 15:38:35 +0200334 use_len = add_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 mbedtls_xor(ctx->y + offset, ctx->y + offset, add, use_len);
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200338
Mateusz Starzyk33392452021-07-06 15:38:35 +0200339 ctx->processed += use_len;
340 add_len -= use_len;
341 add += use_len;
342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 if (use_len + offset == 16 || ctx->processed == ctx->add_len) {
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100344#if defined(MBEDTLS_CIPHER_C)
345 ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
346#else
347 ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
348#endif
349 if (ret != 0) {
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200350 ctx->state |= CCM_STATE__ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 return ret;
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200352 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200353 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200354 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 if (ctx->processed == ctx->add_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200357 ctx->state |= CCM_STATE__AUTH_DATA_FINISHED;
358 ctx->processed = 0; // prepare for mbedtls_ccm_update()
359 }
360 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 return 0;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200363}
364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365int mbedtls_ccm_update(mbedtls_ccm_context *ctx,
366 const unsigned char *input, size_t input_len,
367 unsigned char *output, size_t output_size,
368 size_t *output_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200369{
Mateusz Starzykb73c3ec2021-08-09 15:55:38 +0200370 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200371 unsigned char i;
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100372 size_t use_len, offset;
373#if defined(MBEDTLS_CIPHER_C)
374 size_t olen;
375#endif
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200376
Mateusz Starzykf3378502021-08-09 11:32:11 +0200377 unsigned char local_output[16];
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 if (ctx->state & CCM_STATE__ERROR) {
Mateusz Starzyk64f0b5f2021-09-02 11:50:38 +0200380 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk2d5652a2021-07-27 16:07:54 +0200381 }
382
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200383 /* Check against plaintext length only if performing operation with
384 * authentication
385 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (ctx->tag_len != 0 && ctx->processed + input_len > ctx->plaintext_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200387 return MBEDTLS_ERR_CCM_BAD_INPUT;
388 }
389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 if (output_size < input_len) {
391 return MBEDTLS_ERR_CCM_BAD_INPUT;
392 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200393 *output_len = input_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200394
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200395 ret = 0;
396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 while (input_len > 0) {
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200398 offset = ctx->processed % 16;
399
400 use_len = 16 - offset;
401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (use_len > input_len) {
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200403 use_len = input_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 }
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200405
406 ctx->processed += use_len;
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (ctx->mode == MBEDTLS_CCM_ENCRYPT || \
409 ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT) {
410 mbedtls_xor(ctx->y + offset, ctx->y + offset, input, use_len);
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) {
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100413#if defined(MBEDTLS_CIPHER_C)
414 ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
415#else
416 ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
417#endif
418 if (ret != 0) {
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200419 ctx->state |= CCM_STATE__ERROR;
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200420 goto exit;
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200421 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200422 }
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 ret = mbedtls_ccm_crypt(ctx, offset, use_len, input, output);
425 if (ret != 0) {
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200426 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200428 }
429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if (ctx->mode == MBEDTLS_CCM_DECRYPT || \
431 ctx->mode == MBEDTLS_CCM_STAR_DECRYPT) {
Mateusz Starzyk2f175492021-08-09 16:05:14 +0200432 /* Since output may be in shared memory, we cannot be sure that
433 * it will contain what we wrote to it. Therefore, we should avoid using
434 * it as input to any operations.
435 * Write decrypted data to local_output to avoid using output variable as
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200436 * input in the XOR operation for Y.
437 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 ret = mbedtls_ccm_crypt(ctx, offset, use_len, input, local_output);
439 if (ret != 0) {
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200440 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 mbedtls_xor(ctx->y + offset, ctx->y + offset, local_output, use_len);
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 memcpy(output, local_output, use_len);
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) {
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100448#if defined(MBEDTLS_CIPHER_C)
449 ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
450#else
451 ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
452#endif
453 if (ret != 0) {
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200454 ctx->state |= CCM_STATE__ERROR;
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200455 goto exit;
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200456 }
457 }
458 }
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) {
461 for (i = 0; i < ctx->q; i++) {
462 if (++(ctx->ctr)[15-i] != 0) {
463 break;
464 }
465 }
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200466 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200467
468 input_len -= use_len;
469 input += use_len;
470 output += use_len;
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200471 }
472
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200473exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 mbedtls_platform_zeroize(local_output, 16);
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200475
476 return ret;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200477}
478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479int mbedtls_ccm_finish(mbedtls_ccm_context *ctx,
480 unsigned char *tag, size_t tag_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200481{
Mateusz Starzykb73c3ec2021-08-09 15:55:38 +0200482 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200483 unsigned char i;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (ctx->state & CCM_STATE__ERROR) {
Mateusz Starzyk4f2dd8a2021-08-09 15:37:47 +0200486 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk2d5652a2021-07-27 16:07:54 +0200487 }
488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (ctx->add_len > 0 && !(ctx->state & CCM_STATE__AUTH_DATA_FINISHED)) {
Mateusz Starzyk7251eda2021-09-01 13:26:44 +0200490 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200491 }
492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 if (ctx->plaintext_len > 0 && ctx->processed != ctx->plaintext_len) {
Mateusz Starzyk7251eda2021-09-01 13:26:44 +0200494 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200495 }
496
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200497 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200498 * Authentication: reset counter and crypt/mask internal tag
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200499 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 for (i = 0; i < ctx->q; i++) {
Mateusz Starzyk89d469c2021-06-22 16:24:28 +0200501 ctx->ctr[15-i] = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 ret = mbedtls_ccm_crypt(ctx, 0, 16, ctx->y, ctx->y);
505 if (ret != 0) {
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200506 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 }
508 if (tag != NULL) {
509 memcpy(tag, ctx->y, tag_len);
510 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200511 mbedtls_ccm_clear_state(ctx);
512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 return 0;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200514}
515
516/*
517 * Authenticated encryption or decryption
518 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100519static int ccm_auth_crypt(mbedtls_ccm_context *ctx, int mode, size_t length,
520 const unsigned char *iv, size_t iv_len,
521 const unsigned char *add, size_t add_len,
522 const unsigned char *input, unsigned char *output,
523 unsigned char *tag, size_t tag_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200524{
525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
526 size_t olen;
527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if ((ret = mbedtls_ccm_starts(ctx, mode, iv, iv_len)) != 0) {
529 return ret;
530 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if ((ret = mbedtls_ccm_set_lengths(ctx, add_len, length, tag_len)) != 0) {
533 return ret;
534 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if ((ret = mbedtls_ccm_update_ad(ctx, add, add_len)) != 0) {
537 return ret;
538 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 if ((ret = mbedtls_ccm_update(ctx, input, length,
541 output, length, &olen)) != 0) {
542 return ret;
543 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if ((ret = mbedtls_ccm_finish(ctx, tag, tag_len)) != 0) {
546 return ret;
547 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return 0;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200550}
551
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200552/*
553 * Authenticated encryption
554 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100555int mbedtls_ccm_star_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length,
556 const unsigned char *iv, size_t iv_len,
557 const unsigned char *add, size_t add_len,
558 const unsigned char *input, unsigned char *output,
559 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200560{
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return ccm_auth_crypt(ctx, MBEDTLS_CCM_STAR_ENCRYPT, length, iv, iv_len,
562 add, add_len, input, output, tag, tag_len);
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200563}
564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565int mbedtls_ccm_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length,
566 const unsigned char *iv, size_t iv_len,
567 const unsigned char *add, size_t add_len,
568 const unsigned char *input, unsigned char *output,
569 unsigned char *tag, size_t tag_len)
Janos Follathb5734a22018-05-14 14:31:49 +0100570{
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 return ccm_auth_crypt(ctx, MBEDTLS_CCM_ENCRYPT, length, iv, iv_len,
572 add, add_len, input, output, tag, tag_len);
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200573}
Janos Follathb5734a22018-05-14 14:31:49 +0100574
Mateusz Starzykeb395c02021-07-28 15:10:54 +0200575/*
576 * Authenticated decryption
577 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100578static int mbedtls_ccm_compare_tags(const unsigned char *tag1,
579 const unsigned char *tag2,
580 size_t tag_len)
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200581{
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200582 /* Check tag in "constant-time" */
Dave Rodgmand26a3d62023-09-11 18:25:16 +0100583 int diff = mbedtls_ct_memcmp(tag1, tag2, tag_len);
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 if (diff != 0) {
586 return MBEDTLS_ERR_CCM_AUTH_FAILED;
587 }
588
589 return 0;
Janos Follathb5734a22018-05-14 14:31:49 +0100590}
591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592static int ccm_auth_decrypt(mbedtls_ccm_context *ctx, int mode, size_t length,
593 const unsigned char *iv, size_t iv_len,
594 const unsigned char *add, size_t add_len,
595 const unsigned char *input, unsigned char *output,
596 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200597{
Janos Follath24eed8d2019-11-22 13:21:35 +0000598 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200599 unsigned char check_tag[16];
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 if ((ret = ccm_auth_crypt(ctx, mode, length,
602 iv, iv_len, add, add_len,
603 input, output, check_tag, tag_len)) != 0) {
604 return ret;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200605 }
606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 if ((ret = mbedtls_ccm_compare_tags(tag, check_tag, tag_len)) != 0) {
608 mbedtls_platform_zeroize(output, length);
609 return ret;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200610 }
611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 return 0;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200613}
614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615int mbedtls_ccm_star_auth_decrypt(mbedtls_ccm_context *ctx, size_t length,
616 const unsigned char *iv, size_t iv_len,
617 const unsigned char *add, size_t add_len,
618 const unsigned char *input, unsigned char *output,
619 const unsigned char *tag, size_t tag_len)
Mateusz Starzyk1bda9452021-07-28 15:21:46 +0200620{
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 return ccm_auth_decrypt(ctx, MBEDTLS_CCM_STAR_DECRYPT, length,
622 iv, iv_len, add, add_len,
623 input, output, tag, tag_len);
Mateusz Starzyk1bda9452021-07-28 15:21:46 +0200624}
625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626int mbedtls_ccm_auth_decrypt(mbedtls_ccm_context *ctx, size_t length,
627 const unsigned char *iv, size_t iv_len,
628 const unsigned char *add, size_t add_len,
629 const unsigned char *input, unsigned char *output,
630 const unsigned char *tag, size_t tag_len)
Janos Follathb5734a22018-05-14 14:31:49 +0100631{
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 return ccm_auth_decrypt(ctx, MBEDTLS_CCM_DECRYPT, length,
633 iv, iv_len, add, add_len,
634 input, output, tag, tag_len);
Janos Follathb5734a22018-05-14 14:31:49 +0100635}
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200636#endif /* !MBEDTLS_CCM_ALT */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200639/*
640 * Examples 1 to 3 from SP800-38C Appendix C
641 */
642
643#define NB_TESTS 3
Ron Eldor1b9b2172018-04-26 14:15:01 +0300644#define CCM_SELFTEST_PT_MAX_LEN 24
645#define CCM_SELFTEST_CT_MAX_LEN 32
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200646/*
647 * The data is the same for all tests, only the used length changes
648 */
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100649static const unsigned char key_test_data[] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200650 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
651 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
652};
653
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100654static const unsigned char iv_test_data[] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200655 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
656 0x18, 0x19, 0x1a, 0x1b
657};
658
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100659static const unsigned char ad_test_data[] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200660 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
661 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
662 0x10, 0x11, 0x12, 0x13
663};
664
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100665static const unsigned char msg_test_data[CCM_SELFTEST_PT_MAX_LEN] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200666 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
667 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
668 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
669};
670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671static const size_t iv_len_test_data[NB_TESTS] = { 7, 8, 12 };
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100672static const size_t add_len_test_data[NB_TESTS] = { 8, 16, 20 };
673static const size_t msg_len_test_data[NB_TESTS] = { 4, 16, 24 };
674static const size_t tag_len_test_data[NB_TESTS] = { 4, 6, 8 };
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200675
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100676static const unsigned char res_test_data[NB_TESTS][CCM_SELFTEST_CT_MAX_LEN] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200677 { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
678 { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
679 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
680 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
681 { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
682 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
683 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
684 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
685};
686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687int mbedtls_ccm_self_test(int verbose)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200688{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_ccm_context ctx;
Ron Eldor1b9b2172018-04-26 14:15:01 +0300690 /*
691 * Some hardware accelerators require the input and output buffers
692 * would be in RAM, because the flash is not accessible.
693 * Use buffers on the stack to hold the test vectors data.
694 */
695 unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN];
696 unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200697 size_t i;
Janos Follath24eed8d2019-11-22 13:21:35 +0000698 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 mbedtls_ccm_init(&ctx);
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 if (mbedtls_ccm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key_test_data,
Dave Rodgman6dd757a2023-02-02 12:40:50 +0000703 8 * sizeof(key_test_data)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 if (verbose != 0) {
705 mbedtls_printf(" CCM: setup failed");
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200706 }
707
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 return 1;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200709 }
710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 for (i = 0; i < NB_TESTS; i++) {
712 if (verbose != 0) {
713 mbedtls_printf(" CCM-AES #%u: ", (unsigned int) i + 1);
714 }
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN);
717 memset(ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN);
718 memcpy(plaintext, msg_test_data, msg_len_test_data[i]);
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 ret = mbedtls_ccm_encrypt_and_tag(&ctx, msg_len_test_data[i],
721 iv_test_data, iv_len_test_data[i],
722 ad_test_data, add_len_test_data[i],
723 plaintext, ciphertext,
724 ciphertext + msg_len_test_data[i],
725 tag_len_test_data[i]);
726
727 if (ret != 0 ||
728 memcmp(ciphertext, res_test_data[i],
729 msg_len_test_data[i] + tag_len_test_data[i]) != 0) {
730 if (verbose != 0) {
731 mbedtls_printf("failed\n");
732 }
733
734 return 1;
735 }
736 memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN);
737
738 ret = mbedtls_ccm_auth_decrypt(&ctx, msg_len_test_data[i],
739 iv_test_data, iv_len_test_data[i],
740 ad_test_data, add_len_test_data[i],
741 ciphertext, plaintext,
742 ciphertext + msg_len_test_data[i],
743 tag_len_test_data[i]);
744
745 if (ret != 0 ||
746 memcmp(plaintext, msg_test_data, msg_len_test_data[i]) != 0) {
747 if (verbose != 0) {
748 mbedtls_printf("failed\n");
749 }
750
751 return 1;
752 }
753
754 if (verbose != 0) {
755 mbedtls_printf("passed\n");
756 }
757 }
758
759 mbedtls_ccm_free(&ctx);
760
761 if (verbose != 0) {
762 mbedtls_printf("\n");
763 }
764
765 return 0;
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200766}
767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#endif /* MBEDTLS_CCM_C */