blob: 969c6c781ba58d8bbfea4d912c6a88c03f1311ec [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 Settibd7528a2023-12-14 09:36:03 +010026#if defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +010027#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
Valerio Settibd7528a2023-12-14 09:36:03 +010059#if defined(MBEDTLS_BLOCK_CIPHER_C)
60 mbedtls_block_cipher_free(&ctx->block_cipher_ctx);
61
62 if ((ret = mbedtls_block_cipher_setup(&ctx->block_cipher_ctx, cipher)) != 0) {
63 return MBEDTLS_ERR_CCM_BAD_INPUT;
64 }
65
66 if ((ret = mbedtls_block_cipher_setkey(&ctx->block_cipher_ctx, key, keybits)) != 0) {
67 return MBEDTLS_ERR_CCM_BAD_INPUT;
68 }
69#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 const mbedtls_cipher_info_t *cipher_info;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020071
Gilles Peskine449bd832023-01-11 14:50:10 +010072 cipher_info = mbedtls_cipher_info_from_values(cipher, keybits,
73 MBEDTLS_MODE_ECB);
74 if (cipher_info == NULL) {
75 return MBEDTLS_ERR_CCM_BAD_INPUT;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020076 }
77
Dave Rodgman85a88132023-06-24 11:41:50 +010078 if (mbedtls_cipher_info_get_block_size(cipher_info) != 16) {
Gilles Peskine449bd832023-01-11 14:50:10 +010079 return MBEDTLS_ERR_CCM_BAD_INPUT;
80 }
81
82 mbedtls_cipher_free(&ctx->cipher_ctx);
83
84 if ((ret = mbedtls_cipher_setup(&ctx->cipher_ctx, cipher_info)) != 0) {
85 return ret;
86 }
87
88 if ((ret = mbedtls_cipher_setkey(&ctx->cipher_ctx, key, keybits,
89 MBEDTLS_ENCRYPT)) != 0) {
90 return ret;
91 }
Valerio Setti9b7a8b22023-11-16 08:24:51 +010092#endif
Gilles Peskine449bd832023-01-11 14:50:10 +010093
Dave Rodgman69928db2023-12-14 12:09:18 +000094 return ret;
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 Settibd7528a2023-12-14 09:36:03 +0100105#if defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100106 mbedtls_block_cipher_free(&ctx->block_cipher_ctx);
Valerio Settibd7528a2023-12-14 09:36:03 +0100107#else
108 mbedtls_cipher_free(&ctx->cipher_ctx);
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100109#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 Settibd7528a2023-12-14 09:36:03 +0100131#if defined(MBEDTLS_BLOCK_CIPHER_C)
132 ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->ctr, tmp_buf);
133#else
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100134 size_t olen = 0;
Valerio Settid0eebc12023-11-20 15:17:53 +0100135 ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->ctr, 16, tmp_buf, &olen);
Valerio Settid0eebc12023-11-20 15:17:53 +0100136#endif
137 if (ret != 0) {
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100138 ctx->state |= CCM_STATE__ERROR;
139 mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf));
140 return ret;
141 }
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 mbedtls_xor(output, input, tmp_buf + offset, use_len);
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200144
Mateusz Starzykc52220d2021-07-27 13:54:55 +0200145 mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf));
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200146 return ret;
147}
148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149static void mbedtls_ccm_clear_state(mbedtls_ccm_context *ctx)
150{
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200151 ctx->state = CCM_STATE__CLEAR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 memset(ctx->y, 0, 16);
153 memset(ctx->ctr, 0, 16);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200154}
155
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200156static int ccm_calculate_first_block_if_ready(mbedtls_ccm_context *ctx)
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200157{
Janos Follath24eed8d2019-11-22 13:21:35 +0000158 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200159 unsigned char i;
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100160 size_t len_left;
Valerio Settibd7528a2023-12-14 09:36:03 +0100161#if !defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100162 size_t olen;
163#endif
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200164
Tom Cosgrove1797b052022-12-04 17:19:59 +0000165 /* length calculation can be done only after both
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200166 * mbedtls_ccm_starts() and mbedtls_ccm_set_lengths() have been executed
167 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 if (!(ctx->state & CCM_STATE__STARTED) || !(ctx->state & CCM_STATE__LENGTHS_SET)) {
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200169 return 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200171
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200172 /* CCM expects non-empty tag.
Minos Galanakis837af432024-08-22 09:21:53 +0100173 * CCM* allows empty tag. For CCM* without tag, the tag calculation is skipped.
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200174 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if (ctx->tag_len == 0) {
176 if (ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT || ctx->mode == MBEDTLS_CCM_STAR_DECRYPT) {
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200177 ctx->plaintext_len = 0;
Minos Galanakis837af432024-08-22 09:21:53 +0100178 return 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 } else {
180 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200181 }
182 }
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200183
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200184 /*
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200185 * First block:
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200186 * 0 .. 0 flags
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200187 * 1 .. iv_len nonce (aka iv) - set by: mbedtls_ccm_starts()
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200188 * iv_len+1 .. 15 length
189 *
190 * With flags as (bits):
191 * 7 0
192 * 6 add present?
193 * 5 .. 3 (t - 2) / 2
194 * 2 .. 0 q - 1
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200195 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 ctx->y[0] |= (ctx->add_len > 0) << 6;
197 ctx->y[0] |= ((ctx->tag_len - 2) / 2) << 3;
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200198 ctx->y[0] |= ctx->q - 1;
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 for (i = 0, len_left = ctx->plaintext_len; i < ctx->q; i++, len_left >>= 8) {
201 ctx->y[15-i] = MBEDTLS_BYTE_0(len_left);
202 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if (len_left > 0) {
Mateusz Starzyk88c4d622021-07-05 17:09:16 +0200205 ctx->state |= CCM_STATE__ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk88c4d622021-07-05 17:09:16 +0200207 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200208
209 /* Start CBC-MAC with first block*/
Valerio Settibd7528a2023-12-14 09:36:03 +0100210#if defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Settid0eebc12023-11-20 15:17:53 +0100211 ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
Valerio Settibd7528a2023-12-14 09:36:03 +0100212#else
213 ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
Valerio Settid0eebc12023-11-20 15:17:53 +0100214#endif
215 if (ret != 0) {
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100216 ctx->state |= CCM_STATE__ERROR;
217 return ret;
218 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return 0;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200221}
222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223int mbedtls_ccm_starts(mbedtls_ccm_context *ctx,
224 int mode,
225 const unsigned char *iv,
226 size_t iv_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200227{
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200228 /* Also implies q is within bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 if (iv_len < 7 || iv_len > 13) {
230 return MBEDTLS_ERR_CCM_BAD_INPUT;
231 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200232
Mateusz Starzyk89d469c2021-06-22 16:24:28 +0200233 ctx->mode = mode;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200234 ctx->q = 16 - 1 - (unsigned char) iv_len;
235
236 /*
237 * Prepare counter block for encryption:
238 * 0 .. 0 flags
239 * 1 .. iv_len nonce (aka iv)
240 * iv_len+1 .. 15 counter (initially 1)
241 *
242 * With flags as (bits):
243 * 7 .. 3 0
244 * 2 .. 0 q - 1
245 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 memset(ctx->ctr, 0, 16);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200247 ctx->ctr[0] = ctx->q - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 memcpy(ctx->ctr + 1, iv, iv_len);
249 memset(ctx->ctr + 1 + iv_len, 0, ctx->q);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200250 ctx->ctr[15] = 1;
251
252 /*
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200253 * See ccm_calculate_first_block_if_ready() for block layout description
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200254 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 memcpy(ctx->y + 1, iv, iv_len);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200256
257 ctx->state |= CCM_STATE__STARTED;
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200258 return ccm_calculate_first_block_if_ready(ctx);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200259}
260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261int mbedtls_ccm_set_lengths(mbedtls_ccm_context *ctx,
262 size_t total_ad_len,
263 size_t plaintext_len,
264 size_t tag_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200265{
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200266 /*
267 * Check length requirements: SP800-38C A.1
268 * Additional requirement: a < 2^16 - 2^8 to simplify the code.
269 * 'length' checked later (when writing it to the first block)
Janos Follath997e85c2018-05-29 11:33:45 +0100270 *
271 * Also, loosen the requirements to enable support for CCM* (IEEE 802.15.4).
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200272 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (tag_len == 2 || tag_len > 16 || tag_len % 2 != 0) {
274 return MBEDTLS_ERR_CCM_BAD_INPUT;
275 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 if (total_ad_len >= 0xFF00) {
278 return MBEDTLS_ERR_CCM_BAD_INPUT;
279 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200280
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200281 ctx->plaintext_len = plaintext_len;
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200282 ctx->add_len = total_ad_len;
283 ctx->tag_len = tag_len;
284 ctx->processed = 0;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200285
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400286 ctx->state |= CCM_STATE__LENGTHS_SET;
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200287 return ccm_calculate_first_block_if_ready(ctx);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200288}
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290int mbedtls_ccm_update_ad(mbedtls_ccm_context *ctx,
291 const unsigned char *add,
292 size_t add_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200293{
294 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100295 size_t use_len, offset;
Valerio Settibd7528a2023-12-14 09:36:03 +0100296#if !defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100297 size_t olen;
298#endif
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (ctx->state & CCM_STATE__ERROR) {
Mateusz Starzyk64f0b5f2021-09-02 11:50:38 +0200301 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk2d5652a2021-07-27 16:07:54 +0200302 }
303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (add_len > 0) {
305 if (ctx->state & CCM_STATE__AUTH_DATA_FINISHED) {
Mateusz Starzyk7251eda2021-09-01 13:26:44 +0200306 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200307 }
308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 if (!(ctx->state & CCM_STATE__AUTH_DATA_STARTED)) {
310 if (add_len > ctx->add_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200311 return MBEDTLS_ERR_CCM_BAD_INPUT;
312 }
313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 ctx->y[0] ^= (unsigned char) ((ctx->add_len >> 8) & 0xFF);
315 ctx->y[1] ^= (unsigned char) ((ctx->add_len) & 0xFF);
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200316
Mateusz Starzyk62d22f92021-08-09 15:53:41 +0200317 ctx->state |= CCM_STATE__AUTH_DATA_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 } else if (ctx->processed + add_len > ctx->add_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200319 return MBEDTLS_ERR_CCM_BAD_INPUT;
320 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 while (add_len > 0) {
Mateusz Starzyk62d22f92021-08-09 15:53:41 +0200323 offset = (ctx->processed + 2) % 16; /* account for y[0] and y[1]
324 * holding total auth data length */
Mateusz Starzyk33392452021-07-06 15:38:35 +0200325 use_len = 16 - offset;
326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 if (use_len > add_len) {
Mateusz Starzyk33392452021-07-06 15:38:35 +0200328 use_len = add_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 mbedtls_xor(ctx->y + offset, ctx->y + offset, add, use_len);
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200332
Mateusz Starzyk33392452021-07-06 15:38:35 +0200333 ctx->processed += use_len;
334 add_len -= use_len;
335 add += use_len;
336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (use_len + offset == 16 || ctx->processed == ctx->add_len) {
Valerio Settibd7528a2023-12-14 09:36:03 +0100338#if defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100339 ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
Valerio Settibd7528a2023-12-14 09:36:03 +0100340#else
341 ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100342#endif
343 if (ret != 0) {
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200344 ctx->state |= CCM_STATE__ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 return ret;
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200346 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200347 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200348 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (ctx->processed == ctx->add_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200351 ctx->state |= CCM_STATE__AUTH_DATA_FINISHED;
352 ctx->processed = 0; // prepare for mbedtls_ccm_update()
353 }
354 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 return 0;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200357}
358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359int mbedtls_ccm_update(mbedtls_ccm_context *ctx,
360 const unsigned char *input, size_t input_len,
361 unsigned char *output, size_t output_size,
362 size_t *output_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200363{
Mateusz Starzykb73c3ec2021-08-09 15:55:38 +0200364 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200365 unsigned char i;
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100366 size_t use_len, offset;
Valerio Settibd7528a2023-12-14 09:36:03 +0100367#if !defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100368 size_t olen;
369#endif
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200370
Mateusz Starzykf3378502021-08-09 11:32:11 +0200371 unsigned char local_output[16];
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if (ctx->state & CCM_STATE__ERROR) {
Mateusz Starzyk64f0b5f2021-09-02 11:50:38 +0200374 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk2d5652a2021-07-27 16:07:54 +0200375 }
376
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200377 /* Check against plaintext length only if performing operation with
378 * authentication
379 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (ctx->tag_len != 0 && ctx->processed + input_len > ctx->plaintext_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200381 return MBEDTLS_ERR_CCM_BAD_INPUT;
382 }
383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (output_size < input_len) {
385 return MBEDTLS_ERR_CCM_BAD_INPUT;
386 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200387 *output_len = input_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200388
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200389 ret = 0;
390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 while (input_len > 0) {
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200392 offset = ctx->processed % 16;
393
394 use_len = 16 - offset;
395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if (use_len > input_len) {
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200397 use_len = input_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 }
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200399
400 ctx->processed += use_len;
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (ctx->mode == MBEDTLS_CCM_ENCRYPT || \
403 ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT) {
404 mbedtls_xor(ctx->y + offset, ctx->y + offset, input, use_len);
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) {
Valerio Settibd7528a2023-12-14 09:36:03 +0100407#if defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100408 ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
Valerio Settibd7528a2023-12-14 09:36:03 +0100409#else
410 ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100411#endif
412 if (ret != 0) {
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200413 ctx->state |= CCM_STATE__ERROR;
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200414 goto exit;
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200415 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200416 }
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 ret = mbedtls_ccm_crypt(ctx, offset, use_len, input, output);
419 if (ret != 0) {
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200420 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200422 }
423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if (ctx->mode == MBEDTLS_CCM_DECRYPT || \
425 ctx->mode == MBEDTLS_CCM_STAR_DECRYPT) {
Mateusz Starzyk2f175492021-08-09 16:05:14 +0200426 /* Since output may be in shared memory, we cannot be sure that
427 * it will contain what we wrote to it. Therefore, we should avoid using
428 * it as input to any operations.
429 * Write decrypted data to local_output to avoid using output variable as
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200430 * input in the XOR operation for Y.
431 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 ret = mbedtls_ccm_crypt(ctx, offset, use_len, input, local_output);
433 if (ret != 0) {
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200434 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 mbedtls_xor(ctx->y + offset, ctx->y + offset, local_output, use_len);
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 memcpy(output, local_output, use_len);
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) {
Valerio Settibd7528a2023-12-14 09:36:03 +0100442#if defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100443 ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
Valerio Settibd7528a2023-12-14 09:36:03 +0100444#else
445 ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100446#endif
447 if (ret != 0) {
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200448 ctx->state |= CCM_STATE__ERROR;
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200449 goto exit;
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200450 }
451 }
452 }
453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) {
455 for (i = 0; i < ctx->q; i++) {
456 if (++(ctx->ctr)[15-i] != 0) {
457 break;
458 }
459 }
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200460 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200461
462 input_len -= use_len;
463 input += use_len;
464 output += use_len;
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200465 }
466
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200467exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 mbedtls_platform_zeroize(local_output, 16);
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200469
470 return ret;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200471}
472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473int mbedtls_ccm_finish(mbedtls_ccm_context *ctx,
474 unsigned char *tag, size_t tag_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200475{
Mateusz Starzykb73c3ec2021-08-09 15:55:38 +0200476 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200477 unsigned char i;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if (ctx->state & CCM_STATE__ERROR) {
Mateusz Starzyk4f2dd8a2021-08-09 15:37:47 +0200480 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk2d5652a2021-07-27 16:07:54 +0200481 }
482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if (ctx->add_len > 0 && !(ctx->state & CCM_STATE__AUTH_DATA_FINISHED)) {
Mateusz Starzyk7251eda2021-09-01 13:26:44 +0200484 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200485 }
486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if (ctx->plaintext_len > 0 && ctx->processed != ctx->plaintext_len) {
Mateusz Starzyk7251eda2021-09-01 13:26:44 +0200488 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200489 }
490
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200491 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200492 * Authentication: reset counter and crypt/mask internal tag
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200493 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 for (i = 0; i < ctx->q; i++) {
Mateusz Starzyk89d469c2021-06-22 16:24:28 +0200495 ctx->ctr[15-i] = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 ret = mbedtls_ccm_crypt(ctx, 0, 16, ctx->y, ctx->y);
499 if (ret != 0) {
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200500 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 }
502 if (tag != NULL) {
503 memcpy(tag, ctx->y, tag_len);
504 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200505 mbedtls_ccm_clear_state(ctx);
506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 return 0;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200508}
509
510/*
511 * Authenticated encryption or decryption
512 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100513static int ccm_auth_crypt(mbedtls_ccm_context *ctx, int mode, size_t length,
514 const unsigned char *iv, size_t iv_len,
515 const unsigned char *add, size_t add_len,
516 const unsigned char *input, unsigned char *output,
517 unsigned char *tag, size_t tag_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200518{
519 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
520 size_t olen;
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if ((ret = mbedtls_ccm_starts(ctx, mode, iv, iv_len)) != 0) {
523 return ret;
524 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if ((ret = mbedtls_ccm_set_lengths(ctx, add_len, length, tag_len)) != 0) {
527 return ret;
528 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 if ((ret = mbedtls_ccm_update_ad(ctx, add, add_len)) != 0) {
531 return ret;
532 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if ((ret = mbedtls_ccm_update(ctx, input, length,
535 output, length, &olen)) != 0) {
536 return ret;
537 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 if ((ret = mbedtls_ccm_finish(ctx, tag, tag_len)) != 0) {
540 return ret;
541 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return 0;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200544}
545
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200546/*
547 * Authenticated encryption
548 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100549int mbedtls_ccm_star_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length,
550 const unsigned char *iv, size_t iv_len,
551 const unsigned char *add, size_t add_len,
552 const unsigned char *input, unsigned char *output,
553 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200554{
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return ccm_auth_crypt(ctx, MBEDTLS_CCM_STAR_ENCRYPT, length, iv, iv_len,
556 add, add_len, input, output, tag, tag_len);
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200557}
558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559int mbedtls_ccm_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length,
560 const unsigned char *iv, size_t iv_len,
561 const unsigned char *add, size_t add_len,
562 const unsigned char *input, unsigned char *output,
563 unsigned char *tag, size_t tag_len)
Janos Follathb5734a22018-05-14 14:31:49 +0100564{
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 return ccm_auth_crypt(ctx, MBEDTLS_CCM_ENCRYPT, length, iv, iv_len,
566 add, add_len, input, output, tag, tag_len);
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200567}
Janos Follathb5734a22018-05-14 14:31:49 +0100568
Mateusz Starzykeb395c02021-07-28 15:10:54 +0200569/*
570 * Authenticated decryption
571 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100572static int mbedtls_ccm_compare_tags(const unsigned char *tag1,
573 const unsigned char *tag2,
574 size_t tag_len)
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200575{
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200576 /* Check tag in "constant-time" */
Dave Rodgmand26a3d62023-09-11 18:25:16 +0100577 int diff = mbedtls_ct_memcmp(tag1, tag2, tag_len);
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 if (diff != 0) {
580 return MBEDTLS_ERR_CCM_AUTH_FAILED;
581 }
582
583 return 0;
Janos Follathb5734a22018-05-14 14:31:49 +0100584}
585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586static int ccm_auth_decrypt(mbedtls_ccm_context *ctx, int mode, size_t length,
587 const unsigned char *iv, size_t iv_len,
588 const unsigned char *add, size_t add_len,
589 const unsigned char *input, unsigned char *output,
590 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200591{
Janos Follath24eed8d2019-11-22 13:21:35 +0000592 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200593 unsigned char check_tag[16];
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 if ((ret = ccm_auth_crypt(ctx, mode, length,
596 iv, iv_len, add, add_len,
597 input, output, check_tag, tag_len)) != 0) {
598 return ret;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200599 }
600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 if ((ret = mbedtls_ccm_compare_tags(tag, check_tag, tag_len)) != 0) {
602 mbedtls_platform_zeroize(output, length);
603 return ret;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200604 }
605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 return 0;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200607}
608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609int mbedtls_ccm_star_auth_decrypt(mbedtls_ccm_context *ctx, size_t length,
610 const unsigned char *iv, size_t iv_len,
611 const unsigned char *add, size_t add_len,
612 const unsigned char *input, unsigned char *output,
613 const unsigned char *tag, size_t tag_len)
Mateusz Starzyk1bda9452021-07-28 15:21:46 +0200614{
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 return ccm_auth_decrypt(ctx, MBEDTLS_CCM_STAR_DECRYPT, length,
616 iv, iv_len, add, add_len,
617 input, output, tag, tag_len);
Mateusz Starzyk1bda9452021-07-28 15:21:46 +0200618}
619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620int mbedtls_ccm_auth_decrypt(mbedtls_ccm_context *ctx, size_t length,
621 const unsigned char *iv, size_t iv_len,
622 const unsigned char *add, size_t add_len,
623 const unsigned char *input, unsigned char *output,
624 const unsigned char *tag, size_t tag_len)
Janos Follathb5734a22018-05-14 14:31:49 +0100625{
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 return ccm_auth_decrypt(ctx, MBEDTLS_CCM_DECRYPT, length,
627 iv, iv_len, add, add_len,
628 input, output, tag, tag_len);
Janos Follathb5734a22018-05-14 14:31:49 +0100629}
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200630#endif /* !MBEDTLS_CCM_ALT */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200631
Valerio Setti689c0f72023-12-20 09:53:39 +0100632#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_CCM_GCM_CAN_AES)
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200633/*
634 * Examples 1 to 3 from SP800-38C Appendix C
635 */
636
637#define NB_TESTS 3
Ron Eldor1b9b2172018-04-26 14:15:01 +0300638#define CCM_SELFTEST_PT_MAX_LEN 24
639#define CCM_SELFTEST_CT_MAX_LEN 32
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200640/*
641 * The data is the same for all tests, only the used length changes
642 */
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100643static const unsigned char key_test_data[] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200644 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
645 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
646};
647
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100648static const unsigned char iv_test_data[] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200649 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
650 0x18, 0x19, 0x1a, 0x1b
651};
652
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100653static const unsigned char ad_test_data[] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200654 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
655 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
656 0x10, 0x11, 0x12, 0x13
657};
658
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100659static const unsigned char msg_test_data[CCM_SELFTEST_PT_MAX_LEN] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200660 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
661 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
662 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
663};
664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665static const size_t iv_len_test_data[NB_TESTS] = { 7, 8, 12 };
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100666static const size_t add_len_test_data[NB_TESTS] = { 8, 16, 20 };
667static const size_t msg_len_test_data[NB_TESTS] = { 4, 16, 24 };
668static const size_t tag_len_test_data[NB_TESTS] = { 4, 6, 8 };
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200669
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100670static const unsigned char res_test_data[NB_TESTS][CCM_SELFTEST_CT_MAX_LEN] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200671 { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
672 { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
673 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
674 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
675 { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
676 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
677 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
678 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
679};
680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681int mbedtls_ccm_self_test(int verbose)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200682{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_ccm_context ctx;
Ron Eldor1b9b2172018-04-26 14:15:01 +0300684 /*
685 * Some hardware accelerators require the input and output buffers
686 * would be in RAM, because the flash is not accessible.
687 * Use buffers on the stack to hold the test vectors data.
688 */
689 unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN];
690 unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200691 size_t i;
Janos Follath24eed8d2019-11-22 13:21:35 +0000692 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 mbedtls_ccm_init(&ctx);
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (mbedtls_ccm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key_test_data,
Dave Rodgman6dd757a2023-02-02 12:40:50 +0000697 8 * sizeof(key_test_data)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (verbose != 0) {
699 mbedtls_printf(" CCM: setup failed");
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200700 }
701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 return 1;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200703 }
704
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 for (i = 0; i < NB_TESTS; i++) {
706 if (verbose != 0) {
707 mbedtls_printf(" CCM-AES #%u: ", (unsigned int) i + 1);
708 }
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN);
711 memset(ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN);
712 memcpy(plaintext, msg_test_data, msg_len_test_data[i]);
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 ret = mbedtls_ccm_encrypt_and_tag(&ctx, msg_len_test_data[i],
715 iv_test_data, iv_len_test_data[i],
716 ad_test_data, add_len_test_data[i],
717 plaintext, ciphertext,
718 ciphertext + msg_len_test_data[i],
719 tag_len_test_data[i]);
720
721 if (ret != 0 ||
722 memcmp(ciphertext, res_test_data[i],
723 msg_len_test_data[i] + tag_len_test_data[i]) != 0) {
724 if (verbose != 0) {
725 mbedtls_printf("failed\n");
726 }
727
728 return 1;
729 }
730 memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN);
731
732 ret = mbedtls_ccm_auth_decrypt(&ctx, msg_len_test_data[i],
733 iv_test_data, iv_len_test_data[i],
734 ad_test_data, add_len_test_data[i],
735 ciphertext, plaintext,
736 ciphertext + msg_len_test_data[i],
737 tag_len_test_data[i]);
738
739 if (ret != 0 ||
740 memcmp(plaintext, msg_test_data, msg_len_test_data[i]) != 0) {
741 if (verbose != 0) {
742 mbedtls_printf("failed\n");
743 }
744
745 return 1;
746 }
747
748 if (verbose != 0) {
749 mbedtls_printf("passed\n");
750 }
751 }
752
753 mbedtls_ccm_free(&ctx);
754
755 if (verbose != 0) {
756 mbedtls_printf("\n");
757 }
758
759 return 0;
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200760}
761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#endif /* MBEDTLS_CCM_C */