blob: 68af90337b6a5464e9a8c41f7d4af040d3295020 [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
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020041/*
42 * Initialize context
43 */
Gilles Peskine449bd832023-01-11 14:50:10 +010044void mbedtls_ccm_init(mbedtls_ccm_context *ctx)
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020045{
Gilles Peskine449bd832023-01-11 14:50:10 +010046 memset(ctx, 0, sizeof(mbedtls_ccm_context));
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020047}
48
Gilles Peskine449bd832023-01-11 14:50:10 +010049int mbedtls_ccm_setkey(mbedtls_ccm_context *ctx,
50 mbedtls_cipher_id_t cipher,
51 const unsigned char *key,
52 unsigned int keybits)
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020053{
Janos Follath24eed8d2019-11-22 13:21:35 +000054 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti9b7a8b22023-11-16 08:24:51 +010055
Valerio Settibd7528a2023-12-14 09:36:03 +010056#if defined(MBEDTLS_BLOCK_CIPHER_C)
57 mbedtls_block_cipher_free(&ctx->block_cipher_ctx);
58
59 if ((ret = mbedtls_block_cipher_setup(&ctx->block_cipher_ctx, cipher)) != 0) {
60 return MBEDTLS_ERR_CCM_BAD_INPUT;
61 }
62
63 if ((ret = mbedtls_block_cipher_setkey(&ctx->block_cipher_ctx, key, keybits)) != 0) {
64 return MBEDTLS_ERR_CCM_BAD_INPUT;
65 }
66#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 const mbedtls_cipher_info_t *cipher_info;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020068
Gilles Peskine449bd832023-01-11 14:50:10 +010069 cipher_info = mbedtls_cipher_info_from_values(cipher, keybits,
70 MBEDTLS_MODE_ECB);
71 if (cipher_info == NULL) {
72 return MBEDTLS_ERR_CCM_BAD_INPUT;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020073 }
74
Dave Rodgman85a88132023-06-24 11:41:50 +010075 if (mbedtls_cipher_info_get_block_size(cipher_info) != 16) {
Gilles Peskine449bd832023-01-11 14:50:10 +010076 return MBEDTLS_ERR_CCM_BAD_INPUT;
77 }
78
79 mbedtls_cipher_free(&ctx->cipher_ctx);
80
81 if ((ret = mbedtls_cipher_setup(&ctx->cipher_ctx, cipher_info)) != 0) {
82 return ret;
83 }
84
85 if ((ret = mbedtls_cipher_setkey(&ctx->cipher_ctx, key, keybits,
86 MBEDTLS_ENCRYPT)) != 0) {
87 return ret;
88 }
Valerio Setti9b7a8b22023-11-16 08:24:51 +010089#endif
Gilles Peskine449bd832023-01-11 14:50:10 +010090
Dave Rodgman69928db2023-12-14 12:09:18 +000091 return ret;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020092}
93
94/*
95 * Free context
96 */
Gilles Peskine449bd832023-01-11 14:50:10 +010097void mbedtls_ccm_free(mbedtls_ccm_context *ctx)
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020098{
Gilles Peskine449bd832023-01-11 14:50:10 +010099 if (ctx == NULL) {
k-stachowiakfd42d532018-12-11 14:37:51 +0100100 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 }
Valerio Settibd7528a2023-12-14 09:36:03 +0100102#if defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100103 mbedtls_block_cipher_free(&ctx->block_cipher_ctx);
Valerio Settibd7528a2023-12-14 09:36:03 +0100104#else
105 mbedtls_cipher_free(&ctx->cipher_ctx);
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100106#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ccm_context));
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200108}
109
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200110#define CCM_STATE__CLEAR 0
Mateusz Starzyka9cbdfb2021-07-27 13:49:54 +0200111#define CCM_STATE__STARTED (1 << 0)
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400112#define CCM_STATE__LENGTHS_SET (1 << 1)
Mateusz Starzyk62d22f92021-08-09 15:53:41 +0200113#define CCM_STATE__AUTH_DATA_STARTED (1 << 2)
114#define CCM_STATE__AUTH_DATA_FINISHED (1 << 3)
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200115#define CCM_STATE__ERROR (1 << 4)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200116
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200117/*
118 * Encrypt or decrypt a partial block with CTR
119 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120static int mbedtls_ccm_crypt(mbedtls_ccm_context *ctx,
121 size_t offset, size_t use_len,
122 const unsigned char *input,
123 unsigned char *output)
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200124{
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 unsigned char tmp_buf[16] = { 0 };
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200127
Valerio Settibd7528a2023-12-14 09:36:03 +0100128#if defined(MBEDTLS_BLOCK_CIPHER_C)
129 ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->ctr, tmp_buf);
130#else
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100131 size_t olen = 0;
Valerio Settid0eebc12023-11-20 15:17:53 +0100132 ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->ctr, 16, tmp_buf, &olen);
Valerio Settid0eebc12023-11-20 15:17:53 +0100133#endif
134 if (ret != 0) {
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100135 ctx->state |= CCM_STATE__ERROR;
136 mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf));
137 return ret;
138 }
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 mbedtls_xor(output, input, tmp_buf + offset, use_len);
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200141
Mateusz Starzykc52220d2021-07-27 13:54:55 +0200142 mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf));
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200143 return ret;
144}
145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146static void mbedtls_ccm_clear_state(mbedtls_ccm_context *ctx)
147{
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200148 ctx->state = CCM_STATE__CLEAR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 memset(ctx->y, 0, 16);
150 memset(ctx->ctr, 0, 16);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200151}
152
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200153static int ccm_calculate_first_block_if_ready(mbedtls_ccm_context *ctx)
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200154{
Janos Follath24eed8d2019-11-22 13:21:35 +0000155 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200156 unsigned char i;
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100157 size_t len_left;
Valerio Settibd7528a2023-12-14 09:36:03 +0100158#if !defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100159 size_t olen;
160#endif
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200161
Tom Cosgrove1797b052022-12-04 17:19:59 +0000162 /* length calculation can be done only after both
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200163 * mbedtls_ccm_starts() and mbedtls_ccm_set_lengths() have been executed
164 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (!(ctx->state & CCM_STATE__STARTED) || !(ctx->state & CCM_STATE__LENGTHS_SET)) {
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200166 return 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200168
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200169 /* CCM expects non-empty tag.
Minos Galanakis83dbac72024-08-22 09:21:53 +0100170 * CCM* allows empty tag. For CCM* without tag, the tag calculation is skipped.
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200171 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 if (ctx->tag_len == 0) {
173 if (ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT || ctx->mode == MBEDTLS_CCM_STAR_DECRYPT) {
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200174 ctx->plaintext_len = 0;
Minos Galanakis83dbac72024-08-22 09:21:53 +0100175 return 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 } else {
177 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200178 }
179 }
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200180
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200181 /*
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200182 * First block:
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200183 * 0 .. 0 flags
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200184 * 1 .. iv_len nonce (aka iv) - set by: mbedtls_ccm_starts()
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200185 * iv_len+1 .. 15 length
186 *
187 * With flags as (bits):
188 * 7 0
189 * 6 add present?
190 * 5 .. 3 (t - 2) / 2
191 * 2 .. 0 q - 1
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200192 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 ctx->y[0] |= (ctx->add_len > 0) << 6;
194 ctx->y[0] |= ((ctx->tag_len - 2) / 2) << 3;
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200195 ctx->y[0] |= ctx->q - 1;
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 for (i = 0, len_left = ctx->plaintext_len; i < ctx->q; i++, len_left >>= 8) {
198 ctx->y[15-i] = MBEDTLS_BYTE_0(len_left);
199 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if (len_left > 0) {
Mateusz Starzyk88c4d622021-07-05 17:09:16 +0200202 ctx->state |= CCM_STATE__ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk88c4d622021-07-05 17:09:16 +0200204 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200205
206 /* Start CBC-MAC with first block*/
Valerio Settibd7528a2023-12-14 09:36:03 +0100207#if defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Settid0eebc12023-11-20 15:17:53 +0100208 ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
Valerio Settibd7528a2023-12-14 09:36:03 +0100209#else
210 ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
Valerio Settid0eebc12023-11-20 15:17:53 +0100211#endif
212 if (ret != 0) {
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100213 ctx->state |= CCM_STATE__ERROR;
214 return ret;
215 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return 0;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200218}
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220int mbedtls_ccm_starts(mbedtls_ccm_context *ctx,
221 int mode,
222 const unsigned char *iv,
223 size_t iv_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200224{
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200225 /* Also implies q is within bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if (iv_len < 7 || iv_len > 13) {
227 return MBEDTLS_ERR_CCM_BAD_INPUT;
228 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200229
Mateusz Starzyk89d469c2021-06-22 16:24:28 +0200230 ctx->mode = mode;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200231 ctx->q = 16 - 1 - (unsigned char) iv_len;
232
233 /*
234 * Prepare counter block for encryption:
235 * 0 .. 0 flags
236 * 1 .. iv_len nonce (aka iv)
237 * iv_len+1 .. 15 counter (initially 1)
238 *
239 * With flags as (bits):
240 * 7 .. 3 0
241 * 2 .. 0 q - 1
242 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 memset(ctx->ctr, 0, 16);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200244 ctx->ctr[0] = ctx->q - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 memcpy(ctx->ctr + 1, iv, iv_len);
246 memset(ctx->ctr + 1 + iv_len, 0, ctx->q);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200247 ctx->ctr[15] = 1;
248
249 /*
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200250 * See ccm_calculate_first_block_if_ready() for block layout description
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200251 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 memcpy(ctx->y + 1, iv, iv_len);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200253
254 ctx->state |= CCM_STATE__STARTED;
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200255 return ccm_calculate_first_block_if_ready(ctx);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200256}
257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258int mbedtls_ccm_set_lengths(mbedtls_ccm_context *ctx,
259 size_t total_ad_len,
260 size_t plaintext_len,
261 size_t tag_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200262{
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200263 /*
264 * Check length requirements: SP800-38C A.1
265 * Additional requirement: a < 2^16 - 2^8 to simplify the code.
266 * 'length' checked later (when writing it to the first block)
Janos Follath997e85c2018-05-29 11:33:45 +0100267 *
268 * Also, loosen the requirements to enable support for CCM* (IEEE 802.15.4).
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200269 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if (tag_len == 2 || tag_len > 16 || tag_len % 2 != 0) {
271 return MBEDTLS_ERR_CCM_BAD_INPUT;
272 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 if (total_ad_len >= 0xFF00) {
275 return MBEDTLS_ERR_CCM_BAD_INPUT;
276 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200277
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200278 ctx->plaintext_len = plaintext_len;
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200279 ctx->add_len = total_ad_len;
280 ctx->tag_len = tag_len;
281 ctx->processed = 0;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200282
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400283 ctx->state |= CCM_STATE__LENGTHS_SET;
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200284 return ccm_calculate_first_block_if_ready(ctx);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200285}
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287int mbedtls_ccm_update_ad(mbedtls_ccm_context *ctx,
288 const unsigned char *add,
289 size_t add_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200290{
291 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100292 size_t use_len, offset;
Valerio Settibd7528a2023-12-14 09:36:03 +0100293#if !defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100294 size_t olen;
295#endif
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (ctx->state & CCM_STATE__ERROR) {
Mateusz Starzyk64f0b5f2021-09-02 11:50:38 +0200298 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk2d5652a2021-07-27 16:07:54 +0200299 }
300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (add_len > 0) {
302 if (ctx->state & CCM_STATE__AUTH_DATA_FINISHED) {
Mateusz Starzyk7251eda2021-09-01 13:26:44 +0200303 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200304 }
305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if (!(ctx->state & CCM_STATE__AUTH_DATA_STARTED)) {
307 if (add_len > ctx->add_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200308 return MBEDTLS_ERR_CCM_BAD_INPUT;
309 }
310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 ctx->y[0] ^= (unsigned char) ((ctx->add_len >> 8) & 0xFF);
312 ctx->y[1] ^= (unsigned char) ((ctx->add_len) & 0xFF);
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200313
Mateusz Starzyk62d22f92021-08-09 15:53:41 +0200314 ctx->state |= CCM_STATE__AUTH_DATA_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 } else if (ctx->processed + add_len > ctx->add_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200316 return MBEDTLS_ERR_CCM_BAD_INPUT;
317 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 while (add_len > 0) {
Mateusz Starzyk62d22f92021-08-09 15:53:41 +0200320 offset = (ctx->processed + 2) % 16; /* account for y[0] and y[1]
321 * holding total auth data length */
Mateusz Starzyk33392452021-07-06 15:38:35 +0200322 use_len = 16 - offset;
323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if (use_len > add_len) {
Mateusz Starzyk33392452021-07-06 15:38:35 +0200325 use_len = add_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 mbedtls_xor(ctx->y + offset, ctx->y + offset, add, use_len);
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200329
Mateusz Starzyk33392452021-07-06 15:38:35 +0200330 ctx->processed += use_len;
331 add_len -= use_len;
332 add += use_len;
333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (use_len + offset == 16 || ctx->processed == ctx->add_len) {
Valerio Settibd7528a2023-12-14 09:36:03 +0100335#if defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100336 ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
Valerio Settibd7528a2023-12-14 09:36:03 +0100337#else
338 ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100339#endif
340 if (ret != 0) {
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200341 ctx->state |= CCM_STATE__ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 return ret;
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200343 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200344 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200345 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (ctx->processed == ctx->add_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200348 ctx->state |= CCM_STATE__AUTH_DATA_FINISHED;
349 ctx->processed = 0; // prepare for mbedtls_ccm_update()
350 }
351 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return 0;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200354}
355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356int mbedtls_ccm_update(mbedtls_ccm_context *ctx,
357 const unsigned char *input, size_t input_len,
358 unsigned char *output, size_t output_size,
359 size_t *output_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200360{
Mateusz Starzykb73c3ec2021-08-09 15:55:38 +0200361 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200362 unsigned char i;
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100363 size_t use_len, offset;
Valerio Settibd7528a2023-12-14 09:36:03 +0100364#if !defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100365 size_t olen;
366#endif
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200367
Mateusz Starzykf3378502021-08-09 11:32:11 +0200368 unsigned char local_output[16];
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (ctx->state & CCM_STATE__ERROR) {
Mateusz Starzyk64f0b5f2021-09-02 11:50:38 +0200371 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk2d5652a2021-07-27 16:07:54 +0200372 }
373
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200374 /* Check against plaintext length only if performing operation with
375 * authentication
376 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 if (ctx->tag_len != 0 && ctx->processed + input_len > ctx->plaintext_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200378 return MBEDTLS_ERR_CCM_BAD_INPUT;
379 }
380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (output_size < input_len) {
382 return MBEDTLS_ERR_CCM_BAD_INPUT;
383 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200384 *output_len = input_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200385
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200386 ret = 0;
387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 while (input_len > 0) {
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200389 offset = ctx->processed % 16;
390
391 use_len = 16 - offset;
392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if (use_len > input_len) {
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200394 use_len = input_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 }
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200396
397 ctx->processed += use_len;
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if (ctx->mode == MBEDTLS_CCM_ENCRYPT || \
400 ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT) {
401 mbedtls_xor(ctx->y + offset, ctx->y + offset, input, use_len);
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) {
Valerio Settibd7528a2023-12-14 09:36:03 +0100404#if defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100405 ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
Valerio Settibd7528a2023-12-14 09:36:03 +0100406#else
407 ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100408#endif
409 if (ret != 0) {
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200410 ctx->state |= CCM_STATE__ERROR;
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200411 goto exit;
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200412 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200413 }
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 ret = mbedtls_ccm_crypt(ctx, offset, use_len, input, output);
416 if (ret != 0) {
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200417 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200419 }
420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 if (ctx->mode == MBEDTLS_CCM_DECRYPT || \
422 ctx->mode == MBEDTLS_CCM_STAR_DECRYPT) {
Mateusz Starzyk2f175492021-08-09 16:05:14 +0200423 /* Since output may be in shared memory, we cannot be sure that
424 * it will contain what we wrote to it. Therefore, we should avoid using
425 * it as input to any operations.
426 * Write decrypted data to local_output to avoid using output variable as
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200427 * input in the XOR operation for Y.
428 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 ret = mbedtls_ccm_crypt(ctx, offset, use_len, input, local_output);
430 if (ret != 0) {
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200431 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 mbedtls_xor(ctx->y + offset, ctx->y + offset, local_output, use_len);
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 memcpy(output, local_output, use_len);
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) {
Valerio Settibd7528a2023-12-14 09:36:03 +0100439#if defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100440 ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
Valerio Settibd7528a2023-12-14 09:36:03 +0100441#else
442 ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100443#endif
444 if (ret != 0) {
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200445 ctx->state |= CCM_STATE__ERROR;
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200446 goto exit;
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200447 }
448 }
449 }
450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) {
452 for (i = 0; i < ctx->q; i++) {
453 if (++(ctx->ctr)[15-i] != 0) {
454 break;
455 }
456 }
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200457 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200458
459 input_len -= use_len;
460 input += use_len;
461 output += use_len;
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200462 }
463
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200464exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 mbedtls_platform_zeroize(local_output, 16);
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200466
467 return ret;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200468}
469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470int mbedtls_ccm_finish(mbedtls_ccm_context *ctx,
471 unsigned char *tag, size_t tag_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200472{
Mateusz Starzykb73c3ec2021-08-09 15:55:38 +0200473 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200474 unsigned char i;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if (ctx->state & CCM_STATE__ERROR) {
Mateusz Starzyk4f2dd8a2021-08-09 15:37:47 +0200477 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk2d5652a2021-07-27 16:07:54 +0200478 }
479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 if (ctx->add_len > 0 && !(ctx->state & CCM_STATE__AUTH_DATA_FINISHED)) {
Mateusz Starzyk7251eda2021-09-01 13:26:44 +0200481 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200482 }
483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 if (ctx->plaintext_len > 0 && ctx->processed != ctx->plaintext_len) {
Mateusz Starzyk7251eda2021-09-01 13:26:44 +0200485 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200486 }
487
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200488 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200489 * Authentication: reset counter and crypt/mask internal tag
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200490 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 for (i = 0; i < ctx->q; i++) {
Mateusz Starzyk89d469c2021-06-22 16:24:28 +0200492 ctx->ctr[15-i] = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 ret = mbedtls_ccm_crypt(ctx, 0, 16, ctx->y, ctx->y);
496 if (ret != 0) {
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200497 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 }
499 if (tag != NULL) {
500 memcpy(tag, ctx->y, tag_len);
501 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200502 mbedtls_ccm_clear_state(ctx);
503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 return 0;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200505}
506
507/*
508 * Authenticated encryption or decryption
509 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100510static int ccm_auth_crypt(mbedtls_ccm_context *ctx, int mode, size_t length,
511 const unsigned char *iv, size_t iv_len,
512 const unsigned char *add, size_t add_len,
513 const unsigned char *input, unsigned char *output,
514 unsigned char *tag, size_t tag_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200515{
516 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
517 size_t olen;
518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if ((ret = mbedtls_ccm_starts(ctx, mode, iv, iv_len)) != 0) {
520 return ret;
521 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if ((ret = mbedtls_ccm_set_lengths(ctx, add_len, length, tag_len)) != 0) {
524 return ret;
525 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 if ((ret = mbedtls_ccm_update_ad(ctx, add, add_len)) != 0) {
528 return ret;
529 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if ((ret = mbedtls_ccm_update(ctx, input, length,
532 output, length, &olen)) != 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_finish(ctx, tag, tag_len)) != 0) {
537 return ret;
538 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return 0;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200541}
542
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200543/*
544 * Authenticated encryption
545 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100546int mbedtls_ccm_star_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length,
547 const unsigned char *iv, size_t iv_len,
548 const unsigned char *add, size_t add_len,
549 const unsigned char *input, unsigned char *output,
550 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200551{
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return ccm_auth_crypt(ctx, MBEDTLS_CCM_STAR_ENCRYPT, length, iv, iv_len,
553 add, add_len, input, output, tag, tag_len);
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200554}
555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556int mbedtls_ccm_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length,
557 const unsigned char *iv, size_t iv_len,
558 const unsigned char *add, size_t add_len,
559 const unsigned char *input, unsigned char *output,
560 unsigned char *tag, size_t tag_len)
Janos Follathb5734a22018-05-14 14:31:49 +0100561{
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 return ccm_auth_crypt(ctx, MBEDTLS_CCM_ENCRYPT, length, iv, iv_len,
563 add, add_len, input, output, tag, tag_len);
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200564}
Janos Follathb5734a22018-05-14 14:31:49 +0100565
Mateusz Starzykeb395c02021-07-28 15:10:54 +0200566/*
567 * Authenticated decryption
568 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100569static int mbedtls_ccm_compare_tags(const unsigned char *tag1,
570 const unsigned char *tag2,
571 size_t tag_len)
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200572{
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200573 /* Check tag in "constant-time" */
Dave Rodgmand26a3d62023-09-11 18:25:16 +0100574 int diff = mbedtls_ct_memcmp(tag1, tag2, tag_len);
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if (diff != 0) {
577 return MBEDTLS_ERR_CCM_AUTH_FAILED;
578 }
579
580 return 0;
Janos Follathb5734a22018-05-14 14:31:49 +0100581}
582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583static int ccm_auth_decrypt(mbedtls_ccm_context *ctx, int mode, size_t length,
584 const unsigned char *iv, size_t iv_len,
585 const unsigned char *add, size_t add_len,
586 const unsigned char *input, unsigned char *output,
587 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200588{
Janos Follath24eed8d2019-11-22 13:21:35 +0000589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200590 unsigned char check_tag[16];
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 if ((ret = ccm_auth_crypt(ctx, mode, length,
593 iv, iv_len, add, add_len,
594 input, output, check_tag, tag_len)) != 0) {
595 return ret;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200596 }
597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 if ((ret = mbedtls_ccm_compare_tags(tag, check_tag, tag_len)) != 0) {
599 mbedtls_platform_zeroize(output, length);
600 return ret;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200601 }
602
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 return 0;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200604}
605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606int mbedtls_ccm_star_auth_decrypt(mbedtls_ccm_context *ctx, size_t length,
607 const unsigned char *iv, size_t iv_len,
608 const unsigned char *add, size_t add_len,
609 const unsigned char *input, unsigned char *output,
610 const unsigned char *tag, size_t tag_len)
Mateusz Starzyk1bda9452021-07-28 15:21:46 +0200611{
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 return ccm_auth_decrypt(ctx, MBEDTLS_CCM_STAR_DECRYPT, length,
613 iv, iv_len, add, add_len,
614 input, output, tag, tag_len);
Mateusz Starzyk1bda9452021-07-28 15:21:46 +0200615}
616
Gilles Peskine449bd832023-01-11 14:50:10 +0100617int mbedtls_ccm_auth_decrypt(mbedtls_ccm_context *ctx, size_t length,
618 const unsigned char *iv, size_t iv_len,
619 const unsigned char *add, size_t add_len,
620 const unsigned char *input, unsigned char *output,
621 const unsigned char *tag, size_t tag_len)
Janos Follathb5734a22018-05-14 14:31:49 +0100622{
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 return ccm_auth_decrypt(ctx, MBEDTLS_CCM_DECRYPT, length,
624 iv, iv_len, add, add_len,
625 input, output, tag, tag_len);
Janos Follathb5734a22018-05-14 14:31:49 +0100626}
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200627
Valerio Setti689c0f72023-12-20 09:53:39 +0100628#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_CCM_GCM_CAN_AES)
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200629/*
630 * Examples 1 to 3 from SP800-38C Appendix C
631 */
632
633#define NB_TESTS 3
Ron Eldor1b9b2172018-04-26 14:15:01 +0300634#define CCM_SELFTEST_PT_MAX_LEN 24
635#define CCM_SELFTEST_CT_MAX_LEN 32
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200636/*
637 * The data is the same for all tests, only the used length changes
638 */
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100639static const unsigned char key_test_data[] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200640 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
641 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
642};
643
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100644static const unsigned char iv_test_data[] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200645 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
646 0x18, 0x19, 0x1a, 0x1b
647};
648
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100649static const unsigned char ad_test_data[] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200650 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
651 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
652 0x10, 0x11, 0x12, 0x13
653};
654
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100655static const unsigned char msg_test_data[CCM_SELFTEST_PT_MAX_LEN] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200656 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
657 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
658 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
659};
660
Gilles Peskine449bd832023-01-11 14:50:10 +0100661static const size_t iv_len_test_data[NB_TESTS] = { 7, 8, 12 };
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100662static const size_t add_len_test_data[NB_TESTS] = { 8, 16, 20 };
663static const size_t msg_len_test_data[NB_TESTS] = { 4, 16, 24 };
664static const size_t tag_len_test_data[NB_TESTS] = { 4, 6, 8 };
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200665
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100666static const unsigned char res_test_data[NB_TESTS][CCM_SELFTEST_CT_MAX_LEN] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200667 { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
668 { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
669 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
670 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
671 { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
672 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
673 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
674 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
675};
676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677int mbedtls_ccm_self_test(int verbose)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200678{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_ccm_context ctx;
Ron Eldor1b9b2172018-04-26 14:15:01 +0300680 /*
681 * Some hardware accelerators require the input and output buffers
682 * would be in RAM, because the flash is not accessible.
683 * Use buffers on the stack to hold the test vectors data.
684 */
685 unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN];
686 unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200687 size_t i;
Janos Follath24eed8d2019-11-22 13:21:35 +0000688 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 mbedtls_ccm_init(&ctx);
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 if (mbedtls_ccm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key_test_data,
Dave Rodgman6dd757a2023-02-02 12:40:50 +0000693 8 * sizeof(key_test_data)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 if (verbose != 0) {
695 mbedtls_printf(" CCM: setup failed");
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200696 }
697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 return 1;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200699 }
700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 for (i = 0; i < NB_TESTS; i++) {
702 if (verbose != 0) {
703 mbedtls_printf(" CCM-AES #%u: ", (unsigned int) i + 1);
704 }
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN);
707 memset(ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN);
708 memcpy(plaintext, msg_test_data, msg_len_test_data[i]);
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 ret = mbedtls_ccm_encrypt_and_tag(&ctx, msg_len_test_data[i],
711 iv_test_data, iv_len_test_data[i],
712 ad_test_data, add_len_test_data[i],
713 plaintext, ciphertext,
714 ciphertext + msg_len_test_data[i],
715 tag_len_test_data[i]);
716
717 if (ret != 0 ||
718 memcmp(ciphertext, res_test_data[i],
719 msg_len_test_data[i] + tag_len_test_data[i]) != 0) {
720 if (verbose != 0) {
721 mbedtls_printf("failed\n");
722 }
723
724 return 1;
725 }
726 memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN);
727
728 ret = mbedtls_ccm_auth_decrypt(&ctx, msg_len_test_data[i],
729 iv_test_data, iv_len_test_data[i],
730 ad_test_data, add_len_test_data[i],
731 ciphertext, plaintext,
732 ciphertext + msg_len_test_data[i],
733 tag_len_test_data[i]);
734
735 if (ret != 0 ||
736 memcmp(plaintext, msg_test_data, msg_len_test_data[i]) != 0) {
737 if (verbose != 0) {
738 mbedtls_printf("failed\n");
739 }
740
741 return 1;
742 }
743
744 if (verbose != 0) {
745 mbedtls_printf("passed\n");
746 }
747 }
748
749 mbedtls_ccm_free(&ctx);
750
751 if (verbose != 0) {
752 mbedtls_printf("\n");
753 }
754
755 return 0;
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200756}
757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#endif /* MBEDTLS_CCM_C */