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