blob: 2503e9f6acd731150b0c363a6e7a13c619dbacba [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.
170 * CCM* allows empty tag. For CCM* without tag, ignore plaintext length.
171 */
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;
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 } else {
176 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200177 }
178 }
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200179
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200180 /*
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200181 * First block:
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200182 * 0 .. 0 flags
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200183 * 1 .. iv_len nonce (aka iv) - set by: mbedtls_ccm_starts()
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200184 * iv_len+1 .. 15 length
185 *
186 * With flags as (bits):
187 * 7 0
188 * 6 add present?
189 * 5 .. 3 (t - 2) / 2
190 * 2 .. 0 q - 1
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200191 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 ctx->y[0] |= (ctx->add_len > 0) << 6;
193 ctx->y[0] |= ((ctx->tag_len - 2) / 2) << 3;
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200194 ctx->y[0] |= ctx->q - 1;
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 for (i = 0, len_left = ctx->plaintext_len; i < ctx->q; i++, len_left >>= 8) {
197 ctx->y[15-i] = MBEDTLS_BYTE_0(len_left);
198 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if (len_left > 0) {
Mateusz Starzyk88c4d622021-07-05 17:09:16 +0200201 ctx->state |= CCM_STATE__ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk88c4d622021-07-05 17:09:16 +0200203 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200204
205 /* Start CBC-MAC with first block*/
Valerio Settibd7528a2023-12-14 09:36:03 +0100206#if defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Settid0eebc12023-11-20 15:17:53 +0100207 ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
Valerio Settibd7528a2023-12-14 09:36:03 +0100208#else
209 ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
Valerio Settid0eebc12023-11-20 15:17:53 +0100210#endif
211 if (ret != 0) {
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100212 ctx->state |= CCM_STATE__ERROR;
213 return ret;
214 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 return 0;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200217}
218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219int mbedtls_ccm_starts(mbedtls_ccm_context *ctx,
220 int mode,
221 const unsigned char *iv,
222 size_t iv_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200223{
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200224 /* Also implies q is within bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (iv_len < 7 || iv_len > 13) {
226 return MBEDTLS_ERR_CCM_BAD_INPUT;
227 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200228
Mateusz Starzyk89d469c2021-06-22 16:24:28 +0200229 ctx->mode = mode;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200230 ctx->q = 16 - 1 - (unsigned char) iv_len;
231
232 /*
233 * Prepare counter block for encryption:
234 * 0 .. 0 flags
235 * 1 .. iv_len nonce (aka iv)
236 * iv_len+1 .. 15 counter (initially 1)
237 *
238 * With flags as (bits):
239 * 7 .. 3 0
240 * 2 .. 0 q - 1
241 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 memset(ctx->ctr, 0, 16);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200243 ctx->ctr[0] = ctx->q - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 memcpy(ctx->ctr + 1, iv, iv_len);
245 memset(ctx->ctr + 1 + iv_len, 0, ctx->q);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200246 ctx->ctr[15] = 1;
247
248 /*
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200249 * See ccm_calculate_first_block_if_ready() for block layout description
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200250 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 memcpy(ctx->y + 1, iv, iv_len);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200252
253 ctx->state |= CCM_STATE__STARTED;
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200254 return ccm_calculate_first_block_if_ready(ctx);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200255}
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257int mbedtls_ccm_set_lengths(mbedtls_ccm_context *ctx,
258 size_t total_ad_len,
259 size_t plaintext_len,
260 size_t tag_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200261{
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200262 /*
263 * Check length requirements: SP800-38C A.1
264 * Additional requirement: a < 2^16 - 2^8 to simplify the code.
265 * 'length' checked later (when writing it to the first block)
Janos Follath997e85c2018-05-29 11:33:45 +0100266 *
267 * Also, loosen the requirements to enable support for CCM* (IEEE 802.15.4).
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200268 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (tag_len == 2 || tag_len > 16 || tag_len % 2 != 0) {
270 return MBEDTLS_ERR_CCM_BAD_INPUT;
271 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (total_ad_len >= 0xFF00) {
274 return MBEDTLS_ERR_CCM_BAD_INPUT;
275 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200276
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200277 ctx->plaintext_len = plaintext_len;
Mateusz Starzykeb2ca962021-07-06 12:45:11 +0200278 ctx->add_len = total_ad_len;
279 ctx->tag_len = tag_len;
280 ctx->processed = 0;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200281
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400282 ctx->state |= CCM_STATE__LENGTHS_SET;
Mateusz Starzykca9dc8d2021-07-27 14:03:53 +0200283 return ccm_calculate_first_block_if_ready(ctx);
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200284}
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286int mbedtls_ccm_update_ad(mbedtls_ccm_context *ctx,
287 const unsigned char *add,
288 size_t add_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200289{
290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100291 size_t use_len, offset;
Valerio Settibd7528a2023-12-14 09:36:03 +0100292#if !defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100293 size_t olen;
294#endif
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if (ctx->state & CCM_STATE__ERROR) {
Mateusz Starzyk64f0b5f2021-09-02 11:50:38 +0200297 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk2d5652a2021-07-27 16:07:54 +0200298 }
299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (add_len > 0) {
301 if (ctx->state & CCM_STATE__AUTH_DATA_FINISHED) {
Mateusz Starzyk7251eda2021-09-01 13:26:44 +0200302 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200303 }
304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if (!(ctx->state & CCM_STATE__AUTH_DATA_STARTED)) {
306 if (add_len > ctx->add_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200307 return MBEDTLS_ERR_CCM_BAD_INPUT;
308 }
309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 ctx->y[0] ^= (unsigned char) ((ctx->add_len >> 8) & 0xFF);
311 ctx->y[1] ^= (unsigned char) ((ctx->add_len) & 0xFF);
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200312
Mateusz Starzyk62d22f92021-08-09 15:53:41 +0200313 ctx->state |= CCM_STATE__AUTH_DATA_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 } else if (ctx->processed + add_len > ctx->add_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200315 return MBEDTLS_ERR_CCM_BAD_INPUT;
316 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 while (add_len > 0) {
Mateusz Starzyk62d22f92021-08-09 15:53:41 +0200319 offset = (ctx->processed + 2) % 16; /* account for y[0] and y[1]
320 * holding total auth data length */
Mateusz Starzyk33392452021-07-06 15:38:35 +0200321 use_len = 16 - offset;
322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if (use_len > add_len) {
Mateusz Starzyk33392452021-07-06 15:38:35 +0200324 use_len = add_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 mbedtls_xor(ctx->y + offset, ctx->y + offset, add, use_len);
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200328
Mateusz Starzyk33392452021-07-06 15:38:35 +0200329 ctx->processed += use_len;
330 add_len -= use_len;
331 add += use_len;
332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (use_len + offset == 16 || ctx->processed == ctx->add_len) {
Valerio Settibd7528a2023-12-14 09:36:03 +0100334#if defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100335 ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
Valerio Settibd7528a2023-12-14 09:36:03 +0100336#else
337 ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100338#endif
339 if (ret != 0) {
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200340 ctx->state |= CCM_STATE__ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return ret;
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200342 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200343 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200344 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (ctx->processed == ctx->add_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200347 ctx->state |= CCM_STATE__AUTH_DATA_FINISHED;
348 ctx->processed = 0; // prepare for mbedtls_ccm_update()
349 }
350 }
Mateusz Starzyk33392452021-07-06 15:38:35 +0200351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 return 0;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200353}
354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355int mbedtls_ccm_update(mbedtls_ccm_context *ctx,
356 const unsigned char *input, size_t input_len,
357 unsigned char *output, size_t output_size,
358 size_t *output_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200359{
Mateusz Starzykb73c3ec2021-08-09 15:55:38 +0200360 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200361 unsigned char i;
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100362 size_t use_len, offset;
Valerio Settibd7528a2023-12-14 09:36:03 +0100363#if !defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100364 size_t olen;
365#endif
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200366
Mateusz Starzykf3378502021-08-09 11:32:11 +0200367 unsigned char local_output[16];
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (ctx->state & CCM_STATE__ERROR) {
Mateusz Starzyk64f0b5f2021-09-02 11:50:38 +0200370 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk2d5652a2021-07-27 16:07:54 +0200371 }
372
Mateusz Starzykbb2ced32021-10-13 13:37:30 +0200373 /* Check against plaintext length only if performing operation with
374 * authentication
375 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (ctx->tag_len != 0 && ctx->processed + input_len > ctx->plaintext_len) {
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200377 return MBEDTLS_ERR_CCM_BAD_INPUT;
378 }
379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (output_size < input_len) {
381 return MBEDTLS_ERR_CCM_BAD_INPUT;
382 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200383 *output_len = input_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200384
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200385 ret = 0;
386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 while (input_len > 0) {
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200388 offset = ctx->processed % 16;
389
390 use_len = 16 - offset;
391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if (use_len > input_len) {
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200393 use_len = input_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 }
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200395
396 ctx->processed += use_len;
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 if (ctx->mode == MBEDTLS_CCM_ENCRYPT || \
399 ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT) {
400 mbedtls_xor(ctx->y + offset, ctx->y + offset, input, use_len);
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) {
Valerio Settibd7528a2023-12-14 09:36:03 +0100403#if defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100404 ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
Valerio Settibd7528a2023-12-14 09:36:03 +0100405#else
406 ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100407#endif
408 if (ret != 0) {
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200409 ctx->state |= CCM_STATE__ERROR;
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200410 goto exit;
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200411 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200412 }
Mateusz Starzyk663055f2021-07-12 19:13:52 +0200413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 ret = mbedtls_ccm_crypt(ctx, offset, use_len, input, output);
415 if (ret != 0) {
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200416 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200418 }
419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 if (ctx->mode == MBEDTLS_CCM_DECRYPT || \
421 ctx->mode == MBEDTLS_CCM_STAR_DECRYPT) {
Mateusz Starzyk2f175492021-08-09 16:05:14 +0200422 /* Since output may be in shared memory, we cannot be sure that
423 * it will contain what we wrote to it. Therefore, we should avoid using
424 * it as input to any operations.
425 * Write decrypted data to local_output to avoid using output variable as
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200426 * input in the XOR operation for Y.
427 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 ret = mbedtls_ccm_crypt(ctx, offset, use_len, input, local_output);
429 if (ret != 0) {
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200430 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 mbedtls_xor(ctx->y + offset, ctx->y + offset, local_output, use_len);
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 memcpy(output, local_output, use_len);
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) {
Valerio Settibd7528a2023-12-14 09:36:03 +0100438#if defined(MBEDTLS_BLOCK_CIPHER_C)
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100439 ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
Valerio Settibd7528a2023-12-14 09:36:03 +0100440#else
441 ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
Valerio Setti9b7a8b22023-11-16 08:24:51 +0100442#endif
443 if (ret != 0) {
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200444 ctx->state |= CCM_STATE__ERROR;
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200445 goto exit;
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200446 }
447 }
448 }
449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) {
451 for (i = 0; i < ctx->q; i++) {
452 if (++(ctx->ctr)[15-i] != 0) {
453 break;
454 }
455 }
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200456 }
Mateusz Starzyk20bac2f2021-07-12 14:52:44 +0200457
458 input_len -= use_len;
459 input += use_len;
460 output += use_len;
Mateusz Starzyk6a15bcf2021-07-07 13:41:30 +0200461 }
462
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200463exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 mbedtls_platform_zeroize(local_output, 16);
Mateusz Starzyk22f7a352021-07-28 15:08:47 +0200465
466 return ret;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200467}
468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469int mbedtls_ccm_finish(mbedtls_ccm_context *ctx,
470 unsigned char *tag, size_t tag_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200471{
Mateusz Starzykb73c3ec2021-08-09 15:55:38 +0200472 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200473 unsigned char i;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if (ctx->state & CCM_STATE__ERROR) {
Mateusz Starzyk4f2dd8a2021-08-09 15:37:47 +0200476 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk2d5652a2021-07-27 16:07:54 +0200477 }
478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if (ctx->add_len > 0 && !(ctx->state & CCM_STATE__AUTH_DATA_FINISHED)) {
Mateusz Starzyk7251eda2021-09-01 13:26:44 +0200480 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200481 }
482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if (ctx->plaintext_len > 0 && ctx->processed != ctx->plaintext_len) {
Mateusz Starzyk7251eda2021-09-01 13:26:44 +0200484 return MBEDTLS_ERR_CCM_BAD_INPUT;
Mateusz Starzyk36d3b892021-07-28 14:14:58 +0200485 }
486
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200487 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200488 * Authentication: reset counter and crypt/mask internal tag
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200489 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 for (i = 0; i < ctx->q; i++) {
Mateusz Starzyk89d469c2021-06-22 16:24:28 +0200491 ctx->ctr[15-i] = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 ret = mbedtls_ccm_crypt(ctx, 0, 16, ctx->y, ctx->y);
495 if (ret != 0) {
Mateusz Starzyk2ad7d8e2021-07-07 11:05:45 +0200496 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 }
498 if (tag != NULL) {
499 memcpy(tag, ctx->y, tag_len);
500 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200501 mbedtls_ccm_clear_state(ctx);
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 return 0;
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200504}
505
506/*
507 * Authenticated encryption or decryption
508 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100509static int ccm_auth_crypt(mbedtls_ccm_context *ctx, int mode, size_t length,
510 const unsigned char *iv, size_t iv_len,
511 const unsigned char *add, size_t add_len,
512 const unsigned char *input, unsigned char *output,
513 unsigned char *tag, size_t tag_len)
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200514{
515 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
516 size_t olen;
517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 if ((ret = mbedtls_ccm_starts(ctx, mode, iv, iv_len)) != 0) {
519 return ret;
520 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if ((ret = mbedtls_ccm_set_lengths(ctx, add_len, length, tag_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_update_ad(ctx, add, add_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(ctx, input, length,
531 output, length, &olen)) != 0) {
532 return ret;
533 }
Mateusz Starzyk793692c2021-06-22 20:34:20 +0200534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if ((ret = mbedtls_ccm_finish(ctx, tag, tag_len)) != 0) {
536 return ret;
537 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 return 0;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200540}
541
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200542/*
543 * Authenticated encryption
544 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100545int mbedtls_ccm_star_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length,
546 const unsigned char *iv, size_t iv_len,
547 const unsigned char *add, size_t add_len,
548 const unsigned char *input, unsigned char *output,
549 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200550{
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 return ccm_auth_crypt(ctx, MBEDTLS_CCM_STAR_ENCRYPT, length, iv, iv_len,
552 add, add_len, input, output, tag, tag_len);
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200553}
554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555int mbedtls_ccm_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)
Janos Follathb5734a22018-05-14 14:31:49 +0100560{
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return ccm_auth_crypt(ctx, MBEDTLS_CCM_ENCRYPT, length, iv, iv_len,
562 add, add_len, input, output, tag, tag_len);
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200563}
Janos Follathb5734a22018-05-14 14:31:49 +0100564
Mateusz Starzykeb395c02021-07-28 15:10:54 +0200565/*
566 * Authenticated decryption
567 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100568static int mbedtls_ccm_compare_tags(const unsigned char *tag1,
569 const unsigned char *tag2,
570 size_t tag_len)
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200571{
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200572 /* Check tag in "constant-time" */
Dave Rodgmand26a3d62023-09-11 18:25:16 +0100573 int diff = mbedtls_ct_memcmp(tag1, tag2, tag_len);
Mateusz Starzyk05e92d62021-07-09 12:44:07 +0200574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if (diff != 0) {
576 return MBEDTLS_ERR_CCM_AUTH_FAILED;
577 }
578
579 return 0;
Janos Follathb5734a22018-05-14 14:31:49 +0100580}
581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582static int ccm_auth_decrypt(mbedtls_ccm_context *ctx, int mode, size_t length,
583 const unsigned char *iv, size_t iv_len,
584 const unsigned char *add, size_t add_len,
585 const unsigned char *input, unsigned char *output,
586 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200587{
Janos Follath24eed8d2019-11-22 13:21:35 +0000588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200589 unsigned char check_tag[16];
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 if ((ret = ccm_auth_crypt(ctx, mode, length,
592 iv, iv_len, add, add_len,
593 input, output, check_tag, tag_len)) != 0) {
594 return ret;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200595 }
596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 if ((ret = mbedtls_ccm_compare_tags(tag, check_tag, tag_len)) != 0) {
598 mbedtls_platform_zeroize(output, length);
599 return ret;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200600 }
601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 return 0;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200603}
604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605int mbedtls_ccm_star_auth_decrypt(mbedtls_ccm_context *ctx, size_t length,
606 const unsigned char *iv, size_t iv_len,
607 const unsigned char *add, size_t add_len,
608 const unsigned char *input, unsigned char *output,
609 const unsigned char *tag, size_t tag_len)
Mateusz Starzyk1bda9452021-07-28 15:21:46 +0200610{
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 return ccm_auth_decrypt(ctx, MBEDTLS_CCM_STAR_DECRYPT, length,
612 iv, iv_len, add, add_len,
613 input, output, tag, tag_len);
Mateusz Starzyk1bda9452021-07-28 15:21:46 +0200614}
615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616int mbedtls_ccm_auth_decrypt(mbedtls_ccm_context *ctx, size_t length,
617 const unsigned char *iv, size_t iv_len,
618 const unsigned char *add, size_t add_len,
619 const unsigned char *input, unsigned char *output,
620 const unsigned char *tag, size_t tag_len)
Janos Follathb5734a22018-05-14 14:31:49 +0100621{
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 return ccm_auth_decrypt(ctx, MBEDTLS_CCM_DECRYPT, length,
623 iv, iv_len, add, add_len,
624 input, output, tag, tag_len);
Janos Follathb5734a22018-05-14 14:31:49 +0100625}
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200626
Valerio Setti689c0f72023-12-20 09:53:39 +0100627#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_CCM_GCM_CAN_AES)
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200628/*
629 * Examples 1 to 3 from SP800-38C Appendix C
630 */
631
632#define NB_TESTS 3
Ron Eldor1b9b2172018-04-26 14:15:01 +0300633#define CCM_SELFTEST_PT_MAX_LEN 24
634#define CCM_SELFTEST_CT_MAX_LEN 32
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200635/*
636 * The data is the same for all tests, only the used length changes
637 */
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100638static const unsigned char key_test_data[] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200639 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
640 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
641};
642
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100643static const unsigned char iv_test_data[] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200644 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
645 0x18, 0x19, 0x1a, 0x1b
646};
647
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100648static const unsigned char ad_test_data[] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200649 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
650 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
651 0x10, 0x11, 0x12, 0x13
652};
653
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100654static const unsigned char msg_test_data[CCM_SELFTEST_PT_MAX_LEN] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200655 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
656 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
657 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
658};
659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660static const size_t iv_len_test_data[NB_TESTS] = { 7, 8, 12 };
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100661static const size_t add_len_test_data[NB_TESTS] = { 8, 16, 20 };
662static const size_t msg_len_test_data[NB_TESTS] = { 4, 16, 24 };
663static const size_t tag_len_test_data[NB_TESTS] = { 4, 6, 8 };
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200664
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100665static const unsigned char res_test_data[NB_TESTS][CCM_SELFTEST_CT_MAX_LEN] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200666 { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
667 { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
668 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
669 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
670 { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
671 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
672 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
673 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
674};
675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676int mbedtls_ccm_self_test(int verbose)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200677{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_ccm_context ctx;
Ron Eldor1b9b2172018-04-26 14:15:01 +0300679 /*
680 * Some hardware accelerators require the input and output buffers
681 * would be in RAM, because the flash is not accessible.
682 * Use buffers on the stack to hold the test vectors data.
683 */
684 unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN];
685 unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200686 size_t i;
Janos Follath24eed8d2019-11-22 13:21:35 +0000687 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 mbedtls_ccm_init(&ctx);
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (mbedtls_ccm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key_test_data,
Dave Rodgman6dd757a2023-02-02 12:40:50 +0000692 8 * sizeof(key_test_data)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (verbose != 0) {
694 mbedtls_printf(" CCM: setup failed");
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200695 }
696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 return 1;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200698 }
699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 for (i = 0; i < NB_TESTS; i++) {
701 if (verbose != 0) {
702 mbedtls_printf(" CCM-AES #%u: ", (unsigned int) i + 1);
703 }
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200704
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN);
706 memset(ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN);
707 memcpy(plaintext, msg_test_data, msg_len_test_data[i]);
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 ret = mbedtls_ccm_encrypt_and_tag(&ctx, msg_len_test_data[i],
710 iv_test_data, iv_len_test_data[i],
711 ad_test_data, add_len_test_data[i],
712 plaintext, ciphertext,
713 ciphertext + msg_len_test_data[i],
714 tag_len_test_data[i]);
715
716 if (ret != 0 ||
717 memcmp(ciphertext, res_test_data[i],
718 msg_len_test_data[i] + tag_len_test_data[i]) != 0) {
719 if (verbose != 0) {
720 mbedtls_printf("failed\n");
721 }
722
723 return 1;
724 }
725 memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN);
726
727 ret = mbedtls_ccm_auth_decrypt(&ctx, msg_len_test_data[i],
728 iv_test_data, iv_len_test_data[i],
729 ad_test_data, add_len_test_data[i],
730 ciphertext, plaintext,
731 ciphertext + msg_len_test_data[i],
732 tag_len_test_data[i]);
733
734 if (ret != 0 ||
735 memcmp(plaintext, msg_test_data, msg_len_test_data[i]) != 0) {
736 if (verbose != 0) {
737 mbedtls_printf("failed\n");
738 }
739
740 return 1;
741 }
742
743 if (verbose != 0) {
744 mbedtls_printf("passed\n");
745 }
746 }
747
748 mbedtls_ccm_free(&ctx);
749
750 if (verbose != 0) {
751 mbedtls_printf("\n");
752 }
753
754 return 0;
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200755}
756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#endif /* MBEDTLS_CCM_C */