Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * NIST SP800-38C compliant CCM implementation |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 6 | */ |
| 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 Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 17 | #include "common.h" |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 18 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 19 | #if defined(MBEDTLS_CCM_C) |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 20 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 21 | #include "mbedtls/ccm.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 22 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 23 | #include "mbedtls/error.h" |
Dave Rodgman | d26a3d6 | 2023-09-11 18:25:16 +0100 | [diff] [blame] | 24 | #include "mbedtls/constant_time.h" |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 25 | |
Valerio Setti | 9b7a8b2 | 2023-11-16 08:24:51 +0100 | [diff] [blame] | 26 | #if !defined(MBEDTLS_CIPHER_C) |
| 27 | #include "block_cipher_internal.h" |
| 28 | #endif |
| 29 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 30 | #include <string.h> |
| 31 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 33 | #include "mbedtls/platform.h" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 34 | #else |
Mateusz Starzyk | 22f7a35 | 2021-07-28 15:08:47 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 36 | #include <stdio.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #define mbedtls_printf printf |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 38 | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
Mateusz Starzyk | 22f7a35 | 2021-07-28 15:08:47 +0200 | [diff] [blame] | 39 | #endif /* MBEDTLS_PLATFORM_C */ |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 40 | |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 41 | #if !defined(MBEDTLS_CCM_ALT) |
| 42 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 43 | |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 44 | /* |
| 45 | * Initialize context |
| 46 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 47 | void mbedtls_ccm_init(mbedtls_ccm_context *ctx) |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 48 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | memset(ctx, 0, sizeof(mbedtls_ccm_context)); |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 50 | } |
| 51 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 52 | int 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é-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 56 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 57 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Valerio Setti | 9b7a8b2 | 2023-11-16 08:24:51 +0100 | [diff] [blame] | 58 | |
| 59 | #if defined(MBEDTLS_CIPHER_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | const mbedtls_cipher_info_t *cipher_info; |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 61 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 62 | cipher_info = mbedtls_cipher_info_from_values(cipher, keybits, |
| 63 | MBEDTLS_MODE_ECB); |
| 64 | if (cipher_info == NULL) { |
| 65 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Dave Rodgman | 85a8813 | 2023-06-24 11:41:50 +0100 | [diff] [blame] | 68 | if (mbedtls_cipher_info_get_block_size(cipher_info) != 16) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
| 70 | } |
| 71 | |
| 72 | mbedtls_cipher_free(&ctx->cipher_ctx); |
| 73 | |
| 74 | if ((ret = mbedtls_cipher_setup(&ctx->cipher_ctx, cipher_info)) != 0) { |
| 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | if ((ret = mbedtls_cipher_setkey(&ctx->cipher_ctx, key, keybits, |
| 79 | MBEDTLS_ENCRYPT)) != 0) { |
| 80 | return ret; |
| 81 | } |
Valerio Setti | 9b7a8b2 | 2023-11-16 08:24:51 +0100 | [diff] [blame] | 82 | #else |
| 83 | mbedtls_block_cipher_free(&ctx->block_cipher_ctx); |
| 84 | |
| 85 | if ((ret = mbedtls_block_cipher_setup(&ctx->block_cipher_ctx, cipher)) != 0) { |
| 86 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
| 87 | } |
| 88 | |
| 89 | if ((ret = mbedtls_block_cipher_setkey(&ctx->block_cipher_ctx, key, keybits)) != 0) { |
| 90 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
| 91 | } |
| 92 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 93 | |
| 94 | return 0; |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Free context |
| 99 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | void mbedtls_ccm_free(mbedtls_ccm_context *ctx) |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 101 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 102 | if (ctx == NULL) { |
k-stachowiak | fd42d53 | 2018-12-11 14:37:51 +0100 | [diff] [blame] | 103 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 104 | } |
Valerio Setti | 9b7a8b2 | 2023-11-16 08:24:51 +0100 | [diff] [blame] | 105 | #if defined(MBEDTLS_CIPHER_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 106 | mbedtls_cipher_free(&ctx->cipher_ctx); |
Valerio Setti | 9b7a8b2 | 2023-11-16 08:24:51 +0100 | [diff] [blame] | 107 | #else |
| 108 | mbedtls_block_cipher_free(&ctx->block_cipher_ctx); |
| 109 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 110 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ccm_context)); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 113 | #define CCM_STATE__CLEAR 0 |
Mateusz Starzyk | a9cbdfb | 2021-07-27 13:49:54 +0200 | [diff] [blame] | 114 | #define CCM_STATE__STARTED (1 << 0) |
bootstrap-prime | 6dbbf44 | 2022-05-17 19:30:44 -0400 | [diff] [blame] | 115 | #define CCM_STATE__LENGTHS_SET (1 << 1) |
Mateusz Starzyk | 62d22f9 | 2021-08-09 15:53:41 +0200 | [diff] [blame] | 116 | #define CCM_STATE__AUTH_DATA_STARTED (1 << 2) |
| 117 | #define CCM_STATE__AUTH_DATA_FINISHED (1 << 3) |
Mateusz Starzyk | 36d3b89 | 2021-07-28 14:14:58 +0200 | [diff] [blame] | 118 | #define CCM_STATE__ERROR (1 << 4) |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 119 | |
Mateusz Starzyk | 2ad7d8e | 2021-07-07 11:05:45 +0200 | [diff] [blame] | 120 | /* |
| 121 | * Encrypt or decrypt a partial block with CTR |
| 122 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 123 | static 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 Starzyk | 2ad7d8e | 2021-07-07 11:05:45 +0200 | [diff] [blame] | 127 | { |
Mateusz Starzyk | 2ad7d8e | 2021-07-07 11:05:45 +0200 | [diff] [blame] | 128 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 129 | unsigned char tmp_buf[16] = { 0 }; |
Mateusz Starzyk | 2ad7d8e | 2021-07-07 11:05:45 +0200 | [diff] [blame] | 130 | |
Valerio Setti | 9b7a8b2 | 2023-11-16 08:24:51 +0100 | [diff] [blame] | 131 | #if defined(MBEDTLS_CIPHER_C) |
| 132 | size_t olen = 0; |
Valerio Setti | d0eebc1 | 2023-11-20 15:17:53 +0100 | [diff] [blame^] | 133 | ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->ctr, 16, tmp_buf, &olen); |
Valerio Setti | 9b7a8b2 | 2023-11-16 08:24:51 +0100 | [diff] [blame] | 134 | #else |
Valerio Setti | d0eebc1 | 2023-11-20 15:17:53 +0100 | [diff] [blame^] | 135 | ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->ctr, tmp_buf); |
| 136 | #endif |
| 137 | if (ret != 0) { |
Valerio Setti | 9b7a8b2 | 2023-11-16 08:24:51 +0100 | [diff] [blame] | 138 | ctx->state |= CCM_STATE__ERROR; |
| 139 | mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf)); |
| 140 | return ret; |
| 141 | } |
Mateusz Starzyk | 2ad7d8e | 2021-07-07 11:05:45 +0200 | [diff] [blame] | 142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 143 | mbedtls_xor(output, input, tmp_buf + offset, use_len); |
Mateusz Starzyk | 2ad7d8e | 2021-07-07 11:05:45 +0200 | [diff] [blame] | 144 | |
Mateusz Starzyk | c52220d | 2021-07-27 13:54:55 +0200 | [diff] [blame] | 145 | mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf)); |
Mateusz Starzyk | 2ad7d8e | 2021-07-07 11:05:45 +0200 | [diff] [blame] | 146 | return ret; |
| 147 | } |
| 148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 149 | static void mbedtls_ccm_clear_state(mbedtls_ccm_context *ctx) |
| 150 | { |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 151 | ctx->state = CCM_STATE__CLEAR; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | memset(ctx->y, 0, 16); |
| 153 | memset(ctx->ctr, 0, 16); |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Mateusz Starzyk | ca9dc8d | 2021-07-27 14:03:53 +0200 | [diff] [blame] | 156 | static int ccm_calculate_first_block_if_ready(mbedtls_ccm_context *ctx) |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 157 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 158 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 159 | unsigned char i; |
Valerio Setti | 9b7a8b2 | 2023-11-16 08:24:51 +0100 | [diff] [blame] | 160 | size_t len_left; |
| 161 | #if defined(MBEDTLS_CIPHER_C) |
| 162 | size_t olen; |
| 163 | #endif |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 164 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 165 | /* length calculation can be done only after both |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 166 | * mbedtls_ccm_starts() and mbedtls_ccm_set_lengths() have been executed |
| 167 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 168 | if (!(ctx->state & CCM_STATE__STARTED) || !(ctx->state & CCM_STATE__LENGTHS_SET)) { |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 169 | return 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | } |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 171 | |
Mateusz Starzyk | bb2ced3 | 2021-10-13 13:37:30 +0200 | [diff] [blame] | 172 | /* CCM expects non-empty tag. |
| 173 | * CCM* allows empty tag. For CCM* without tag, ignore plaintext length. |
| 174 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 175 | if (ctx->tag_len == 0) { |
| 176 | if (ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT || ctx->mode == MBEDTLS_CCM_STAR_DECRYPT) { |
Mateusz Starzyk | bb2ced3 | 2021-10-13 13:37:30 +0200 | [diff] [blame] | 177 | ctx->plaintext_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 178 | } else { |
| 179 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
Mateusz Starzyk | bb2ced3 | 2021-10-13 13:37:30 +0200 | [diff] [blame] | 180 | } |
| 181 | } |
Mateusz Starzyk | 05e92d6 | 2021-07-09 12:44:07 +0200 | [diff] [blame] | 182 | |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 183 | /* |
Mateusz Starzyk | 663055f | 2021-07-12 19:13:52 +0200 | [diff] [blame] | 184 | * First block: |
Mateusz Starzyk | eb2ca96 | 2021-07-06 12:45:11 +0200 | [diff] [blame] | 185 | * 0 .. 0 flags |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 186 | * 1 .. iv_len nonce (aka iv) - set by: mbedtls_ccm_starts() |
Mateusz Starzyk | eb2ca96 | 2021-07-06 12:45:11 +0200 | [diff] [blame] | 187 | * iv_len+1 .. 15 length |
| 188 | * |
| 189 | * With flags as (bits): |
| 190 | * 7 0 |
| 191 | * 6 add present? |
| 192 | * 5 .. 3 (t - 2) / 2 |
| 193 | * 2 .. 0 q - 1 |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 194 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | ctx->y[0] |= (ctx->add_len > 0) << 6; |
| 196 | ctx->y[0] |= ((ctx->tag_len - 2) / 2) << 3; |
Mateusz Starzyk | 663055f | 2021-07-12 19:13:52 +0200 | [diff] [blame] | 197 | ctx->y[0] |= ctx->q - 1; |
Mateusz Starzyk | eb2ca96 | 2021-07-06 12:45:11 +0200 | [diff] [blame] | 198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 199 | for (i = 0, len_left = ctx->plaintext_len; i < ctx->q; i++, len_left >>= 8) { |
| 200 | ctx->y[15-i] = MBEDTLS_BYTE_0(len_left); |
| 201 | } |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | if (len_left > 0) { |
Mateusz Starzyk | 88c4d62 | 2021-07-05 17:09:16 +0200 | [diff] [blame] | 204 | ctx->state |= CCM_STATE__ERROR; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 205 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
Mateusz Starzyk | 88c4d62 | 2021-07-05 17:09:16 +0200 | [diff] [blame] | 206 | } |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 207 | |
| 208 | /* Start CBC-MAC with first block*/ |
Valerio Setti | 9b7a8b2 | 2023-11-16 08:24:51 +0100 | [diff] [blame] | 209 | #if defined(MBEDTLS_CIPHER_C) |
Valerio Setti | d0eebc1 | 2023-11-20 15:17:53 +0100 | [diff] [blame^] | 210 | ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen); |
Valerio Setti | 9b7a8b2 | 2023-11-16 08:24:51 +0100 | [diff] [blame] | 211 | #else |
Valerio Setti | d0eebc1 | 2023-11-20 15:17:53 +0100 | [diff] [blame^] | 212 | ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y); |
| 213 | #endif |
| 214 | if (ret != 0) { |
Valerio Setti | 9b7a8b2 | 2023-11-16 08:24:51 +0100 | [diff] [blame] | 215 | ctx->state |= CCM_STATE__ERROR; |
| 216 | return ret; |
| 217 | } |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 219 | return 0; |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 222 | int mbedtls_ccm_starts(mbedtls_ccm_context *ctx, |
| 223 | int mode, |
| 224 | const unsigned char *iv, |
| 225 | size_t iv_len) |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 226 | { |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 227 | /* Also implies q is within bounds */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 228 | if (iv_len < 7 || iv_len > 13) { |
| 229 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
| 230 | } |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 231 | |
Mateusz Starzyk | 89d469c | 2021-06-22 16:24:28 +0200 | [diff] [blame] | 232 | ctx->mode = mode; |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 233 | ctx->q = 16 - 1 - (unsigned char) iv_len; |
| 234 | |
| 235 | /* |
| 236 | * Prepare counter block for encryption: |
| 237 | * 0 .. 0 flags |
| 238 | * 1 .. iv_len nonce (aka iv) |
| 239 | * iv_len+1 .. 15 counter (initially 1) |
| 240 | * |
| 241 | * With flags as (bits): |
| 242 | * 7 .. 3 0 |
| 243 | * 2 .. 0 q - 1 |
| 244 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 245 | memset(ctx->ctr, 0, 16); |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 246 | ctx->ctr[0] = ctx->q - 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 247 | memcpy(ctx->ctr + 1, iv, iv_len); |
| 248 | memset(ctx->ctr + 1 + iv_len, 0, ctx->q); |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 249 | ctx->ctr[15] = 1; |
| 250 | |
| 251 | /* |
Mateusz Starzyk | ca9dc8d | 2021-07-27 14:03:53 +0200 | [diff] [blame] | 252 | * See ccm_calculate_first_block_if_ready() for block layout description |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 253 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | memcpy(ctx->y + 1, iv, iv_len); |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 255 | |
| 256 | ctx->state |= CCM_STATE__STARTED; |
Mateusz Starzyk | ca9dc8d | 2021-07-27 14:03:53 +0200 | [diff] [blame] | 257 | return ccm_calculate_first_block_if_ready(ctx); |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 258 | } |
| 259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 260 | int mbedtls_ccm_set_lengths(mbedtls_ccm_context *ctx, |
| 261 | size_t total_ad_len, |
| 262 | size_t plaintext_len, |
| 263 | size_t tag_len) |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 264 | { |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 265 | /* |
| 266 | * Check length requirements: SP800-38C A.1 |
| 267 | * Additional requirement: a < 2^16 - 2^8 to simplify the code. |
| 268 | * 'length' checked later (when writing it to the first block) |
Janos Follath | 997e85c | 2018-05-29 11:33:45 +0100 | [diff] [blame] | 269 | * |
| 270 | * Also, loosen the requirements to enable support for CCM* (IEEE 802.15.4). |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 271 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 272 | if (tag_len == 2 || tag_len > 16 || tag_len % 2 != 0) { |
| 273 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
| 274 | } |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 276 | if (total_ad_len >= 0xFF00) { |
| 277 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
| 278 | } |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 279 | |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 280 | ctx->plaintext_len = plaintext_len; |
Mateusz Starzyk | eb2ca96 | 2021-07-06 12:45:11 +0200 | [diff] [blame] | 281 | ctx->add_len = total_ad_len; |
| 282 | ctx->tag_len = tag_len; |
| 283 | ctx->processed = 0; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 284 | |
bootstrap-prime | 6dbbf44 | 2022-05-17 19:30:44 -0400 | [diff] [blame] | 285 | ctx->state |= CCM_STATE__LENGTHS_SET; |
Mateusz Starzyk | ca9dc8d | 2021-07-27 14:03:53 +0200 | [diff] [blame] | 286 | return ccm_calculate_first_block_if_ready(ctx); |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 287 | } |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 289 | int mbedtls_ccm_update_ad(mbedtls_ccm_context *ctx, |
| 290 | const unsigned char *add, |
| 291 | size_t add_len) |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 292 | { |
| 293 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Valerio Setti | 9b7a8b2 | 2023-11-16 08:24:51 +0100 | [diff] [blame] | 294 | size_t use_len, offset; |
| 295 | #if defined(MBEDTLS_CIPHER_C) |
| 296 | size_t olen; |
| 297 | #endif |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 299 | if (ctx->state & CCM_STATE__ERROR) { |
Mateusz Starzyk | 64f0b5f | 2021-09-02 11:50:38 +0200 | [diff] [blame] | 300 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
Mateusz Starzyk | 2d5652a | 2021-07-27 16:07:54 +0200 | [diff] [blame] | 301 | } |
| 302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | if (add_len > 0) { |
| 304 | if (ctx->state & CCM_STATE__AUTH_DATA_FINISHED) { |
Mateusz Starzyk | 7251eda | 2021-09-01 13:26:44 +0200 | [diff] [blame] | 305 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
Mateusz Starzyk | 36d3b89 | 2021-07-28 14:14:58 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 308 | if (!(ctx->state & CCM_STATE__AUTH_DATA_STARTED)) { |
| 309 | if (add_len > ctx->add_len) { |
Mateusz Starzyk | 36d3b89 | 2021-07-28 14:14:58 +0200 | [diff] [blame] | 310 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
| 311 | } |
| 312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 313 | ctx->y[0] ^= (unsigned char) ((ctx->add_len >> 8) & 0xFF); |
| 314 | ctx->y[1] ^= (unsigned char) ((ctx->add_len) & 0xFF); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 315 | |
Mateusz Starzyk | 62d22f9 | 2021-08-09 15:53:41 +0200 | [diff] [blame] | 316 | ctx->state |= CCM_STATE__AUTH_DATA_STARTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 317 | } else if (ctx->processed + add_len > ctx->add_len) { |
Mateusz Starzyk | 36d3b89 | 2021-07-28 14:14:58 +0200 | [diff] [blame] | 318 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
| 319 | } |
Mateusz Starzyk | 3339245 | 2021-07-06 15:38:35 +0200 | [diff] [blame] | 320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 321 | while (add_len > 0) { |
Mateusz Starzyk | 62d22f9 | 2021-08-09 15:53:41 +0200 | [diff] [blame] | 322 | offset = (ctx->processed + 2) % 16; /* account for y[0] and y[1] |
| 323 | * holding total auth data length */ |
Mateusz Starzyk | 3339245 | 2021-07-06 15:38:35 +0200 | [diff] [blame] | 324 | use_len = 16 - offset; |
| 325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 326 | if (use_len > add_len) { |
Mateusz Starzyk | 3339245 | 2021-07-06 15:38:35 +0200 | [diff] [blame] | 327 | use_len = add_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 328 | } |
Mateusz Starzyk | 3339245 | 2021-07-06 15:38:35 +0200 | [diff] [blame] | 329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 330 | mbedtls_xor(ctx->y + offset, ctx->y + offset, add, use_len); |
Mateusz Starzyk | 663055f | 2021-07-12 19:13:52 +0200 | [diff] [blame] | 331 | |
Mateusz Starzyk | 3339245 | 2021-07-06 15:38:35 +0200 | [diff] [blame] | 332 | ctx->processed += use_len; |
| 333 | add_len -= use_len; |
| 334 | add += use_len; |
| 335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 336 | if (use_len + offset == 16 || ctx->processed == ctx->add_len) { |
Valerio Setti | 9b7a8b2 | 2023-11-16 08:24:51 +0100 | [diff] [blame] | 337 | #if defined(MBEDTLS_CIPHER_C) |
| 338 | ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen); |
| 339 | #else |
| 340 | ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y); |
| 341 | #endif |
| 342 | if (ret != 0) { |
Mateusz Starzyk | 663055f | 2021-07-12 19:13:52 +0200 | [diff] [blame] | 343 | ctx->state |= CCM_STATE__ERROR; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 344 | return ret; |
Mateusz Starzyk | 663055f | 2021-07-12 19:13:52 +0200 | [diff] [blame] | 345 | } |
Mateusz Starzyk | 3339245 | 2021-07-06 15:38:35 +0200 | [diff] [blame] | 346 | } |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 347 | } |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 349 | if (ctx->processed == ctx->add_len) { |
Mateusz Starzyk | 36d3b89 | 2021-07-28 14:14:58 +0200 | [diff] [blame] | 350 | ctx->state |= CCM_STATE__AUTH_DATA_FINISHED; |
| 351 | ctx->processed = 0; // prepare for mbedtls_ccm_update() |
| 352 | } |
| 353 | } |
Mateusz Starzyk | 3339245 | 2021-07-06 15:38:35 +0200 | [diff] [blame] | 354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 355 | return 0; |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 356 | } |
| 357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 358 | int mbedtls_ccm_update(mbedtls_ccm_context *ctx, |
| 359 | const unsigned char *input, size_t input_len, |
| 360 | unsigned char *output, size_t output_size, |
| 361 | size_t *output_len) |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 362 | { |
Mateusz Starzyk | b73c3ec | 2021-08-09 15:55:38 +0200 | [diff] [blame] | 363 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 364 | unsigned char i; |
Valerio Setti | 9b7a8b2 | 2023-11-16 08:24:51 +0100 | [diff] [blame] | 365 | size_t use_len, offset; |
| 366 | #if defined(MBEDTLS_CIPHER_C) |
| 367 | size_t olen; |
| 368 | #endif |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 369 | |
Mateusz Starzyk | f337850 | 2021-08-09 11:32:11 +0200 | [diff] [blame] | 370 | unsigned char local_output[16]; |
Mateusz Starzyk | 22f7a35 | 2021-07-28 15:08:47 +0200 | [diff] [blame] | 371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | if (ctx->state & CCM_STATE__ERROR) { |
Mateusz Starzyk | 64f0b5f | 2021-09-02 11:50:38 +0200 | [diff] [blame] | 373 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
Mateusz Starzyk | 2d5652a | 2021-07-27 16:07:54 +0200 | [diff] [blame] | 374 | } |
| 375 | |
Mateusz Starzyk | bb2ced3 | 2021-10-13 13:37:30 +0200 | [diff] [blame] | 376 | /* Check against plaintext length only if performing operation with |
| 377 | * authentication |
| 378 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 379 | if (ctx->tag_len != 0 && ctx->processed + input_len > ctx->plaintext_len) { |
Mateusz Starzyk | 36d3b89 | 2021-07-28 14:14:58 +0200 | [diff] [blame] | 380 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
| 381 | } |
| 382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 383 | if (output_size < input_len) { |
| 384 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
| 385 | } |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 386 | *output_len = input_len; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 387 | |
Mateusz Starzyk | 22f7a35 | 2021-07-28 15:08:47 +0200 | [diff] [blame] | 388 | ret = 0; |
| 389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 390 | while (input_len > 0) { |
Mateusz Starzyk | 6a15bcf | 2021-07-07 13:41:30 +0200 | [diff] [blame] | 391 | offset = ctx->processed % 16; |
| 392 | |
| 393 | use_len = 16 - offset; |
| 394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 395 | if (use_len > input_len) { |
Mateusz Starzyk | 6a15bcf | 2021-07-07 13:41:30 +0200 | [diff] [blame] | 396 | use_len = input_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 397 | } |
Mateusz Starzyk | 6a15bcf | 2021-07-07 13:41:30 +0200 | [diff] [blame] | 398 | |
| 399 | ctx->processed += use_len; |
Mateusz Starzyk | 6a15bcf | 2021-07-07 13:41:30 +0200 | [diff] [blame] | 400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 401 | if (ctx->mode == MBEDTLS_CCM_ENCRYPT || \ |
| 402 | ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT) { |
| 403 | mbedtls_xor(ctx->y + offset, ctx->y + offset, input, use_len); |
Mateusz Starzyk | 663055f | 2021-07-12 19:13:52 +0200 | [diff] [blame] | 404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 405 | if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) { |
Valerio Setti | 9b7a8b2 | 2023-11-16 08:24:51 +0100 | [diff] [blame] | 406 | #if defined(MBEDTLS_CIPHER_C) |
| 407 | ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen); |
| 408 | #else |
| 409 | ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y); |
| 410 | #endif |
| 411 | if (ret != 0) { |
Mateusz Starzyk | 663055f | 2021-07-12 19:13:52 +0200 | [diff] [blame] | 412 | ctx->state |= CCM_STATE__ERROR; |
Mateusz Starzyk | 22f7a35 | 2021-07-28 15:08:47 +0200 | [diff] [blame] | 413 | goto exit; |
Mateusz Starzyk | 663055f | 2021-07-12 19:13:52 +0200 | [diff] [blame] | 414 | } |
Mateusz Starzyk | 20bac2f | 2021-07-12 14:52:44 +0200 | [diff] [blame] | 415 | } |
Mateusz Starzyk | 663055f | 2021-07-12 19:13:52 +0200 | [diff] [blame] | 416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | ret = mbedtls_ccm_crypt(ctx, offset, use_len, input, output); |
| 418 | if (ret != 0) { |
Mateusz Starzyk | 22f7a35 | 2021-07-28 15:08:47 +0200 | [diff] [blame] | 419 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 420 | } |
Mateusz Starzyk | 20bac2f | 2021-07-12 14:52:44 +0200 | [diff] [blame] | 421 | } |
| 422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 423 | if (ctx->mode == MBEDTLS_CCM_DECRYPT || \ |
| 424 | ctx->mode == MBEDTLS_CCM_STAR_DECRYPT) { |
Mateusz Starzyk | 2f17549 | 2021-08-09 16:05:14 +0200 | [diff] [blame] | 425 | /* Since output may be in shared memory, we cannot be sure that |
| 426 | * it will contain what we wrote to it. Therefore, we should avoid using |
| 427 | * it as input to any operations. |
| 428 | * Write decrypted data to local_output to avoid using output variable as |
Mateusz Starzyk | 22f7a35 | 2021-07-28 15:08:47 +0200 | [diff] [blame] | 429 | * input in the XOR operation for Y. |
| 430 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 431 | ret = mbedtls_ccm_crypt(ctx, offset, use_len, input, local_output); |
| 432 | if (ret != 0) { |
Mateusz Starzyk | 22f7a35 | 2021-07-28 15:08:47 +0200 | [diff] [blame] | 433 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 434 | } |
Mateusz Starzyk | 20bac2f | 2021-07-12 14:52:44 +0200 | [diff] [blame] | 435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 436 | mbedtls_xor(ctx->y + offset, ctx->y + offset, local_output, use_len); |
Mateusz Starzyk | 22f7a35 | 2021-07-28 15:08:47 +0200 | [diff] [blame] | 437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 438 | memcpy(output, local_output, use_len); |
Mateusz Starzyk | 20bac2f | 2021-07-12 14:52:44 +0200 | [diff] [blame] | 439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 440 | if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) { |
Valerio Setti | 9b7a8b2 | 2023-11-16 08:24:51 +0100 | [diff] [blame] | 441 | #if defined(MBEDTLS_CIPHER_C) |
| 442 | ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen); |
| 443 | #else |
| 444 | ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y); |
| 445 | #endif |
| 446 | if (ret != 0) { |
Mateusz Starzyk | 20bac2f | 2021-07-12 14:52:44 +0200 | [diff] [blame] | 447 | ctx->state |= CCM_STATE__ERROR; |
Mateusz Starzyk | 22f7a35 | 2021-07-28 15:08:47 +0200 | [diff] [blame] | 448 | goto exit; |
Mateusz Starzyk | 20bac2f | 2021-07-12 14:52:44 +0200 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 453 | if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) { |
| 454 | for (i = 0; i < ctx->q; i++) { |
| 455 | if (++(ctx->ctr)[15-i] != 0) { |
| 456 | break; |
| 457 | } |
| 458 | } |
Mateusz Starzyk | 6a15bcf | 2021-07-07 13:41:30 +0200 | [diff] [blame] | 459 | } |
Mateusz Starzyk | 20bac2f | 2021-07-12 14:52:44 +0200 | [diff] [blame] | 460 | |
| 461 | input_len -= use_len; |
| 462 | input += use_len; |
| 463 | output += use_len; |
Mateusz Starzyk | 6a15bcf | 2021-07-07 13:41:30 +0200 | [diff] [blame] | 464 | } |
| 465 | |
Mateusz Starzyk | 22f7a35 | 2021-07-28 15:08:47 +0200 | [diff] [blame] | 466 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 467 | mbedtls_platform_zeroize(local_output, 16); |
Mateusz Starzyk | 22f7a35 | 2021-07-28 15:08:47 +0200 | [diff] [blame] | 468 | |
| 469 | return ret; |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 472 | int mbedtls_ccm_finish(mbedtls_ccm_context *ctx, |
| 473 | unsigned char *tag, size_t tag_len) |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 474 | { |
Mateusz Starzyk | b73c3ec | 2021-08-09 15:55:38 +0200 | [diff] [blame] | 475 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 476 | unsigned char i; |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 478 | if (ctx->state & CCM_STATE__ERROR) { |
Mateusz Starzyk | 4f2dd8a | 2021-08-09 15:37:47 +0200 | [diff] [blame] | 479 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Mateusz Starzyk | 2d5652a | 2021-07-27 16:07:54 +0200 | [diff] [blame] | 480 | } |
| 481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | if (ctx->add_len > 0 && !(ctx->state & CCM_STATE__AUTH_DATA_FINISHED)) { |
Mateusz Starzyk | 7251eda | 2021-09-01 13:26:44 +0200 | [diff] [blame] | 483 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
Mateusz Starzyk | 36d3b89 | 2021-07-28 14:14:58 +0200 | [diff] [blame] | 484 | } |
| 485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 486 | if (ctx->plaintext_len > 0 && ctx->processed != ctx->plaintext_len) { |
Mateusz Starzyk | 7251eda | 2021-09-01 13:26:44 +0200 | [diff] [blame] | 487 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
Mateusz Starzyk | 36d3b89 | 2021-07-28 14:14:58 +0200 | [diff] [blame] | 488 | } |
| 489 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 490 | /* |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 491 | * Authentication: reset counter and crypt/mask internal tag |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 492 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 493 | for (i = 0; i < ctx->q; i++) { |
Mateusz Starzyk | 89d469c | 2021-06-22 16:24:28 +0200 | [diff] [blame] | 494 | ctx->ctr[15-i] = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 495 | } |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 497 | ret = mbedtls_ccm_crypt(ctx, 0, 16, ctx->y, ctx->y); |
| 498 | if (ret != 0) { |
Mateusz Starzyk | 2ad7d8e | 2021-07-07 11:05:45 +0200 | [diff] [blame] | 499 | return ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 500 | } |
| 501 | if (tag != NULL) { |
| 502 | memcpy(tag, ctx->y, tag_len); |
| 503 | } |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 504 | mbedtls_ccm_clear_state(ctx); |
| 505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 506 | return 0; |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Authenticated encryption or decryption |
| 511 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 512 | static int ccm_auth_crypt(mbedtls_ccm_context *ctx, int mode, size_t length, |
| 513 | const unsigned char *iv, size_t iv_len, |
| 514 | const unsigned char *add, size_t add_len, |
| 515 | const unsigned char *input, unsigned char *output, |
| 516 | unsigned char *tag, size_t tag_len) |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 517 | { |
| 518 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 519 | size_t olen; |
| 520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 521 | if ((ret = mbedtls_ccm_starts(ctx, mode, iv, iv_len)) != 0) { |
| 522 | return ret; |
| 523 | } |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 525 | if ((ret = mbedtls_ccm_set_lengths(ctx, add_len, length, tag_len)) != 0) { |
| 526 | return ret; |
| 527 | } |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 529 | if ((ret = mbedtls_ccm_update_ad(ctx, add, add_len)) != 0) { |
| 530 | return ret; |
| 531 | } |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 533 | if ((ret = mbedtls_ccm_update(ctx, input, length, |
| 534 | output, length, &olen)) != 0) { |
| 535 | return ret; |
| 536 | } |
Mateusz Starzyk | 793692c | 2021-06-22 20:34:20 +0200 | [diff] [blame] | 537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | if ((ret = mbedtls_ccm_finish(ctx, tag, tag_len)) != 0) { |
| 539 | return ret; |
| 540 | } |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 542 | return 0; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 543 | } |
| 544 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 545 | /* |
| 546 | * Authenticated encryption |
| 547 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 548 | int mbedtls_ccm_star_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length, |
| 549 | const unsigned char *iv, size_t iv_len, |
| 550 | const unsigned char *add, size_t add_len, |
| 551 | const unsigned char *input, unsigned char *output, |
| 552 | unsigned char *tag, size_t tag_len) |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 553 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 554 | return ccm_auth_crypt(ctx, MBEDTLS_CCM_STAR_ENCRYPT, length, iv, iv_len, |
| 555 | add, add_len, input, output, tag, tag_len); |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 556 | } |
| 557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 558 | int mbedtls_ccm_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length, |
| 559 | const unsigned char *iv, size_t iv_len, |
| 560 | const unsigned char *add, size_t add_len, |
| 561 | const unsigned char *input, unsigned char *output, |
| 562 | unsigned char *tag, size_t tag_len) |
Janos Follath | b5734a2 | 2018-05-14 14:31:49 +0100 | [diff] [blame] | 563 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 564 | return ccm_auth_crypt(ctx, MBEDTLS_CCM_ENCRYPT, length, iv, iv_len, |
| 565 | add, add_len, input, output, tag, tag_len); |
Mateusz Starzyk | 05e92d6 | 2021-07-09 12:44:07 +0200 | [diff] [blame] | 566 | } |
Janos Follath | b5734a2 | 2018-05-14 14:31:49 +0100 | [diff] [blame] | 567 | |
Mateusz Starzyk | eb395c0 | 2021-07-28 15:10:54 +0200 | [diff] [blame] | 568 | /* |
| 569 | * Authenticated decryption |
| 570 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 571 | static int mbedtls_ccm_compare_tags(const unsigned char *tag1, |
| 572 | const unsigned char *tag2, |
| 573 | size_t tag_len) |
Mateusz Starzyk | 05e92d6 | 2021-07-09 12:44:07 +0200 | [diff] [blame] | 574 | { |
Mateusz Starzyk | 05e92d6 | 2021-07-09 12:44:07 +0200 | [diff] [blame] | 575 | /* Check tag in "constant-time" */ |
Dave Rodgman | d26a3d6 | 2023-09-11 18:25:16 +0100 | [diff] [blame] | 576 | int diff = mbedtls_ct_memcmp(tag1, tag2, tag_len); |
Mateusz Starzyk | 05e92d6 | 2021-07-09 12:44:07 +0200 | [diff] [blame] | 577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 578 | if (diff != 0) { |
| 579 | return MBEDTLS_ERR_CCM_AUTH_FAILED; |
| 580 | } |
| 581 | |
| 582 | return 0; |
Janos Follath | b5734a2 | 2018-05-14 14:31:49 +0100 | [diff] [blame] | 583 | } |
| 584 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 585 | static int ccm_auth_decrypt(mbedtls_ccm_context *ctx, int mode, size_t length, |
| 586 | const unsigned char *iv, size_t iv_len, |
| 587 | const unsigned char *add, size_t add_len, |
| 588 | const unsigned char *input, unsigned char *output, |
| 589 | const unsigned char *tag, size_t tag_len) |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 590 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 591 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 592 | unsigned char check_tag[16]; |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 594 | if ((ret = ccm_auth_crypt(ctx, mode, length, |
| 595 | iv, iv_len, add, add_len, |
| 596 | input, output, check_tag, tag_len)) != 0) { |
| 597 | return ret; |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 598 | } |
| 599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 600 | if ((ret = mbedtls_ccm_compare_tags(tag, check_tag, tag_len)) != 0) { |
| 601 | mbedtls_platform_zeroize(output, length); |
| 602 | return ret; |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 603 | } |
| 604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 605 | return 0; |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 606 | } |
| 607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 608 | int mbedtls_ccm_star_auth_decrypt(mbedtls_ccm_context *ctx, size_t length, |
| 609 | const unsigned char *iv, size_t iv_len, |
| 610 | const unsigned char *add, size_t add_len, |
| 611 | const unsigned char *input, unsigned char *output, |
| 612 | const unsigned char *tag, size_t tag_len) |
Mateusz Starzyk | 1bda945 | 2021-07-28 15:21:46 +0200 | [diff] [blame] | 613 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 614 | return ccm_auth_decrypt(ctx, MBEDTLS_CCM_STAR_DECRYPT, length, |
| 615 | iv, iv_len, add, add_len, |
| 616 | input, output, tag, tag_len); |
Mateusz Starzyk | 1bda945 | 2021-07-28 15:21:46 +0200 | [diff] [blame] | 617 | } |
| 618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 619 | int mbedtls_ccm_auth_decrypt(mbedtls_ccm_context *ctx, size_t length, |
| 620 | const unsigned char *iv, size_t iv_len, |
| 621 | const unsigned char *add, size_t add_len, |
| 622 | const unsigned char *input, unsigned char *output, |
| 623 | const unsigned char *tag, size_t tag_len) |
Janos Follath | b5734a2 | 2018-05-14 14:31:49 +0100 | [diff] [blame] | 624 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 625 | return ccm_auth_decrypt(ctx, MBEDTLS_CCM_DECRYPT, length, |
| 626 | iv, iv_len, add, add_len, |
| 627 | input, output, tag, tag_len); |
Janos Follath | b5734a2 | 2018-05-14 14:31:49 +0100 | [diff] [blame] | 628 | } |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 629 | #endif /* !MBEDTLS_CCM_ALT */ |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 630 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 631 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 632 | /* |
| 633 | * Examples 1 to 3 from SP800-38C Appendix C |
| 634 | */ |
| 635 | |
| 636 | #define NB_TESTS 3 |
Ron Eldor | 1b9b217 | 2018-04-26 14:15:01 +0300 | [diff] [blame] | 637 | #define CCM_SELFTEST_PT_MAX_LEN 24 |
| 638 | #define CCM_SELFTEST_CT_MAX_LEN 32 |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 639 | /* |
| 640 | * The data is the same for all tests, only the used length changes |
| 641 | */ |
Michał Janiszewski | c79e92b | 2018-10-31 20:43:05 +0100 | [diff] [blame] | 642 | static const unsigned char key_test_data[] = { |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 643 | 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, |
| 644 | 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f |
| 645 | }; |
| 646 | |
Michał Janiszewski | c79e92b | 2018-10-31 20:43:05 +0100 | [diff] [blame] | 647 | static const unsigned char iv_test_data[] = { |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 648 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 649 | 0x18, 0x19, 0x1a, 0x1b |
| 650 | }; |
| 651 | |
Michał Janiszewski | c79e92b | 2018-10-31 20:43:05 +0100 | [diff] [blame] | 652 | static const unsigned char ad_test_data[] = { |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 653 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 654 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 655 | 0x10, 0x11, 0x12, 0x13 |
| 656 | }; |
| 657 | |
Michał Janiszewski | c79e92b | 2018-10-31 20:43:05 +0100 | [diff] [blame] | 658 | static const unsigned char msg_test_data[CCM_SELFTEST_PT_MAX_LEN] = { |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 659 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, |
| 660 | 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, |
| 661 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 662 | }; |
| 663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 664 | static const size_t iv_len_test_data[NB_TESTS] = { 7, 8, 12 }; |
Michał Janiszewski | 9aeea93 | 2018-10-30 23:00:15 +0100 | [diff] [blame] | 665 | static const size_t add_len_test_data[NB_TESTS] = { 8, 16, 20 }; |
| 666 | static const size_t msg_len_test_data[NB_TESTS] = { 4, 16, 24 }; |
| 667 | static const size_t tag_len_test_data[NB_TESTS] = { 4, 6, 8 }; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 668 | |
Michał Janiszewski | c79e92b | 2018-10-31 20:43:05 +0100 | [diff] [blame] | 669 | static const unsigned char res_test_data[NB_TESTS][CCM_SELFTEST_CT_MAX_LEN] = { |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 670 | { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d }, |
| 671 | { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62, |
| 672 | 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d, |
| 673 | 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd }, |
| 674 | { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a, |
| 675 | 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b, |
| 676 | 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5, |
| 677 | 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 } |
| 678 | }; |
| 679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 680 | int mbedtls_ccm_self_test(int verbose) |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 681 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 682 | mbedtls_ccm_context ctx; |
Ron Eldor | 1b9b217 | 2018-04-26 14:15:01 +0300 | [diff] [blame] | 683 | /* |
| 684 | * Some hardware accelerators require the input and output buffers |
| 685 | * would be in RAM, because the flash is not accessible. |
| 686 | * Use buffers on the stack to hold the test vectors data. |
| 687 | */ |
| 688 | unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN]; |
| 689 | unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN]; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 690 | size_t i; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 691 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 693 | mbedtls_ccm_init(&ctx); |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 695 | if (mbedtls_ccm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key_test_data, |
Dave Rodgman | 6dd757a | 2023-02-02 12:40:50 +0000 | [diff] [blame] | 696 | 8 * sizeof(key_test_data)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 697 | if (verbose != 0) { |
| 698 | mbedtls_printf(" CCM: setup failed"); |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 699 | } |
| 700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 701 | return 1; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 702 | } |
| 703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 704 | for (i = 0; i < NB_TESTS; i++) { |
| 705 | if (verbose != 0) { |
| 706 | mbedtls_printf(" CCM-AES #%u: ", (unsigned int) i + 1); |
| 707 | } |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 709 | memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN); |
| 710 | memset(ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN); |
| 711 | memcpy(plaintext, msg_test_data, msg_len_test_data[i]); |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 713 | ret = mbedtls_ccm_encrypt_and_tag(&ctx, msg_len_test_data[i], |
| 714 | iv_test_data, iv_len_test_data[i], |
| 715 | ad_test_data, add_len_test_data[i], |
| 716 | plaintext, ciphertext, |
| 717 | ciphertext + msg_len_test_data[i], |
| 718 | tag_len_test_data[i]); |
| 719 | |
| 720 | if (ret != 0 || |
| 721 | memcmp(ciphertext, res_test_data[i], |
| 722 | msg_len_test_data[i] + tag_len_test_data[i]) != 0) { |
| 723 | if (verbose != 0) { |
| 724 | mbedtls_printf("failed\n"); |
| 725 | } |
| 726 | |
| 727 | return 1; |
| 728 | } |
| 729 | memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN); |
| 730 | |
| 731 | ret = mbedtls_ccm_auth_decrypt(&ctx, msg_len_test_data[i], |
| 732 | iv_test_data, iv_len_test_data[i], |
| 733 | ad_test_data, add_len_test_data[i], |
| 734 | ciphertext, plaintext, |
| 735 | ciphertext + msg_len_test_data[i], |
| 736 | tag_len_test_data[i]); |
| 737 | |
| 738 | if (ret != 0 || |
| 739 | memcmp(plaintext, msg_test_data, msg_len_test_data[i]) != 0) { |
| 740 | if (verbose != 0) { |
| 741 | mbedtls_printf("failed\n"); |
| 742 | } |
| 743 | |
| 744 | return 1; |
| 745 | } |
| 746 | |
| 747 | if (verbose != 0) { |
| 748 | mbedtls_printf("passed\n"); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | mbedtls_ccm_free(&ctx); |
| 753 | |
| 754 | if (verbose != 0) { |
| 755 | mbedtls_printf("\n"); |
| 756 | } |
| 757 | |
| 758 | return 0; |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 759 | } |
| 760 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 761 | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 762 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 763 | #endif /* MBEDTLS_CCM_C */ |