Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * NIST SP800-38C compliant CCM implementation |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * Definition of CCM: |
| 24 | * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf |
| 25 | * RFC 3610 "Counter with CBC-MAC (CCM)" |
| 26 | * |
| 27 | * Related: |
| 28 | * RFC 5116 "An Interface and Algorithms for Authenticated Encryption" |
| 29 | */ |
| 30 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 32 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 33 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 34 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 35 | #endif |
| 36 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #if defined(MBEDTLS_CCM_C) |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 38 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 39 | #include "mbedtls/ccm.h" |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 40 | #include "mbedtls/platform.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 41 | #include "mbedtls/platform_util.h" |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 42 | |
Kevin Bracey | a967a58 | 2020-11-04 16:28:48 +0200 | [diff] [blame] | 43 | #include <stdint.h> |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 44 | #include <string.h> |
| 45 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
| 47 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 48 | #include "mbedtls/platform.h" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 49 | #else |
| 50 | #include <stdio.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | #define mbedtls_printf printf |
| 52 | #endif /* MBEDTLS_PLATFORM_C */ |
| 53 | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 54 | |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 55 | #if !defined(MBEDTLS_CCM_ALT) |
| 56 | |
k-stachowiak | 26d365e | 2018-12-11 12:22:16 +0100 | [diff] [blame] | 57 | #define CCM_VALIDATE_RET( cond ) \ |
| 58 | MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CCM_BAD_INPUT ) |
| 59 | #define CCM_VALIDATE( cond ) \ |
| 60 | MBEDTLS_INTERNAL_VALIDATE( cond ) |
| 61 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 62 | #define CCM_ENCRYPT 0 |
| 63 | #define CCM_DECRYPT 1 |
| 64 | |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 65 | /* |
| 66 | * Initialize context |
| 67 | */ |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 68 | void mbedtls_ccm_init( mbedtls_ccm_context *ctx ) |
| 69 | { |
k-stachowiak | 26d365e | 2018-12-11 12:22:16 +0100 | [diff] [blame] | 70 | CCM_VALIDATE( ctx != NULL ); |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 71 | memset( ctx, 0, sizeof( mbedtls_ccm_context ) ); |
| 72 | } |
| 73 | |
| 74 | int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx, |
| 75 | mbedtls_cipher_id_t cipher, |
| 76 | const unsigned char *key, |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 77 | unsigned int keybits ) |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 78 | { |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 79 | int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 80 | const mbedtls_cipher_info_t *cipher_info; |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 81 | |
k-stachowiak | 26d365e | 2018-12-11 12:22:16 +0100 | [diff] [blame] | 82 | CCM_VALIDATE_RET( ctx != NULL ); |
| 83 | CCM_VALIDATE_RET( key != NULL ); |
| 84 | |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 85 | cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, MBEDTLS_MODE_ECB ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 86 | if( cipher_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 87 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 88 | |
| 89 | if( cipher_info->block_size != 16 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 91 | |
Manuel Pégourié-Gonnard | 43b0857 | 2015-05-27 17:23:30 +0200 | [diff] [blame] | 92 | mbedtls_cipher_free( &ctx->cipher_ctx ); |
| 93 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 94 | if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 95 | return( ret ); |
| 96 | |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 97 | if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | MBEDTLS_ENCRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 99 | { |
| 100 | return( ret ); |
| 101 | } |
| 102 | |
Andrzej Kurek | afec885 | 2020-07-15 16:31:27 -0400 | [diff] [blame] | 103 | return( ret ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Free context |
| 108 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | void mbedtls_ccm_free( mbedtls_ccm_context *ctx ) |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 110 | { |
k-stachowiak | fd42d53 | 2018-12-11 14:37:51 +0100 | [diff] [blame] | 111 | if( ctx == NULL ) |
| 112 | return; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | mbedtls_cipher_free( &ctx->cipher_ctx ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 114 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ccm_context ) ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 117 | #if defined(MBEDTLS_CCM_SHUFFLING_MASKING) |
Andrzej Kurek | 8bef87e | 2020-10-30 12:18:21 +0100 | [diff] [blame] | 118 | /* Durstenfeld's version of Fisher-Yates shuffle */ |
| 119 | static void mbedtls_generate_permutation( unsigned char* table, size_t size ) |
| 120 | { |
| 121 | size_t i, j; |
| 122 | |
| 123 | for( i = 0; i < size; i++ ) |
| 124 | { |
| 125 | table[i] = (unsigned char) i; |
| 126 | } |
| 127 | |
| 128 | if( size < 2 ) |
| 129 | { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | for( i = size - 1; i > 0; i-- ) |
| 134 | { |
| 135 | unsigned char tmp; |
Andrzej Kurek | 18c60aa | 2020-11-17 12:16:42 +0100 | [diff] [blame] | 136 | j = mbedtls_platform_random_in_range( (uint32_t) i + 1 ); |
Andrzej Kurek | 8bef87e | 2020-10-30 12:18:21 +0100 | [diff] [blame] | 137 | tmp = table[i]; |
| 138 | table[i] = table[j]; |
| 139 | table[j] = tmp; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | static void mbedtls_generate_masks( unsigned char* table, size_t size ) |
| 144 | { |
| 145 | size_t i; |
| 146 | |
| 147 | for( i = 0; i < size; i++ ) |
| 148 | { |
Andrzej Kurek | 18c60aa | 2020-11-17 12:16:42 +0100 | [diff] [blame] | 149 | table[i] = mbedtls_platform_random_in_range( 256 ); |
Andrzej Kurek | 8bef87e | 2020-10-30 12:18:21 +0100 | [diff] [blame] | 150 | } |
| 151 | } |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 152 | #endif /* MBEDTLS_CCM_SHUFFLING_MASKING */ |
Andrzej Kurek | 8bef87e | 2020-10-30 12:18:21 +0100 | [diff] [blame] | 153 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 154 | /* |
| 155 | * Macros for common operations. |
| 156 | * Results in smaller compiled code than static inline functions. |
| 157 | */ |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 158 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 159 | /* |
| 160 | * Update the CBC-MAC state in y using a block in b |
| 161 | * (Always using b as the source helps the compiler optimise a bit better.) |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 162 | * Initial b masking happens outside of this macro due to various sources of it. |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 163 | */ |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 164 | #if defined(MBEDTLS_CCM_SHUFFLING_MASKING) |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 165 | #define UPDATE_CBC_MAC \ |
| 166 | for( i = 0; i < 16; i++ ) \ |
Andrzej Kurek | 8bef87e | 2020-10-30 12:18:21 +0100 | [diff] [blame] | 167 | { \ |
| 168 | y[perm_table[i]] ^= b[perm_table[i]]; \ |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 169 | y[perm_table[i]] ^= mask_table[perm_table[i]]; \ |
Andrzej Kurek | 8bef87e | 2020-10-30 12:18:21 +0100 | [diff] [blame] | 170 | } \ |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 171 | \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 172 | if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \ |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 173 | return( ret ); |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 174 | #else |
| 175 | #define UPDATE_CBC_MAC \ |
| 176 | for( i = 0; i < 16; i++ ) \ |
| 177 | y[i] ^= b[i]; \ |
| 178 | \ |
| 179 | if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \ |
| 180 | return( ret ); |
| 181 | #endif /* MBEDTLS_CCM_SHUFFLING_MASKING */ |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 182 | |
| 183 | /* |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 184 | * Copy src to dst starting at a random offset, while masking the whole dst buffer. |
| 185 | */ |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 186 | #if defined(MBEDTLS_CCM_SHUFFLING_MASKING) |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 187 | #define COPY_MASK( dst, src, mask, len_src, len_dst ) \ |
| 188 | do \ |
| 189 | { \ |
Andrzej Kurek | 18c60aa | 2020-11-17 12:16:42 +0100 | [diff] [blame] | 190 | unsigned j, offset = mbedtls_platform_random_in_range( 256 ); \ |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 191 | mbedtls_generate_masks( mask_table, 16 ); \ |
| 192 | mbedtls_generate_permutation( perm_table, 16 ); \ |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 193 | for( i = 0; i < len_src; i++ ) \ |
| 194 | { \ |
| 195 | j = (i + offset) % len_src; \ |
| 196 | (dst)[j] = (src)[j] ^ (mask)[j]; \ |
| 197 | } \ |
| 198 | for( ; i < len_dst; i++ ) \ |
| 199 | (dst)[i] ^= (mask)[i]; \ |
| 200 | } while( 0 ) |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 201 | #endif /* MBEDTLS_CCM_SHUFFLING_MASKING */ |
| 202 | |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 203 | /* |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 204 | * Encrypt or decrypt a partial block with CTR |
| 205 | * Warning: using b for temporary storage! src and dst must not be b! |
Manuel Pégourié-Gonnard | 0f6b66d | 2014-05-07 14:43:46 +0200 | [diff] [blame] | 206 | * This avoids allocating one more 16 bytes buffer while allowing src == dst. |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 207 | */ |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 208 | #if defined(MBEDTLS_CCM_SHUFFLING_MASKING) |
Andrzej Kurek | 8bef87e | 2020-10-30 12:18:21 +0100 | [diff] [blame] | 209 | #define CTR_CRYPT( dst, src, len ) \ |
Hanno Becker | d6028a1 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 210 | do \ |
| 211 | { \ |
Andrzej Kurek | 8bef87e | 2020-10-30 12:18:21 +0100 | [diff] [blame] | 212 | mbedtls_generate_permutation( perm_table, len ); \ |
| 213 | mbedtls_generate_masks( mask_table, len ); \ |
Hanno Becker | d6028a1 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 214 | if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, \ |
| 215 | 16, b, &olen ) ) != 0 ) \ |
| 216 | { \ |
| 217 | return( ret ); \ |
| 218 | } \ |
| 219 | \ |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 220 | for( i = 0; i < (len); i++ ) \ |
Andrzej Kurek | 8bef87e | 2020-10-30 12:18:21 +0100 | [diff] [blame] | 221 | { \ |
| 222 | (dst)[perm_table[i]] = (src)[perm_table[i]] ^ mask_table[perm_table[i]];\ |
| 223 | (dst)[perm_table[i]] ^= b[perm_table[i]]; \ |
| 224 | (dst)[perm_table[i]] ^= mask_table[perm_table[i]]; \ |
| 225 | } \ |
Hanno Becker | d6028a1 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 226 | } while( 0 ) |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 227 | #else |
| 228 | #define CTR_CRYPT( dst, src, len ) \ |
| 229 | do \ |
| 230 | { \ |
| 231 | if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, \ |
| 232 | 16, b, &olen ) ) != 0 ) \ |
| 233 | { \ |
| 234 | return( ret ); \ |
| 235 | } \ |
| 236 | \ |
| 237 | for( i = 0; i < (len); i++ ) \ |
| 238 | (dst)[i] = (src)[i] ^ b[i]; \ |
| 239 | } while( 0 ) |
| 240 | #endif /* MBEDTLS_CCM_SHUFFLING_MASKING */ |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 241 | |
| 242 | /* |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 243 | * Authenticated encryption or decryption |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 244 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 245 | static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length, |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 246 | const unsigned char *iv, size_t iv_len, |
| 247 | const unsigned char *add, size_t add_len, |
| 248 | const unsigned char *input, unsigned char *output, |
| 249 | unsigned char *tag, size_t tag_len ) |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 250 | { |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 251 | int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED; |
Kevin Bracey | a967a58 | 2020-11-04 16:28:48 +0200 | [diff] [blame] | 252 | uint_fast8_t i; |
| 253 | uint_fast8_t q; |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 254 | size_t len_left, olen; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 255 | unsigned char b[16]; |
| 256 | unsigned char y[16]; |
| 257 | unsigned char ctr[16]; |
| 258 | const unsigned char *src; |
| 259 | unsigned char *dst; |
Andrzej Kurek | ab3de1d | 2020-11-10 10:52:56 +0100 | [diff] [blame] | 260 | volatile size_t flow_ctrl = 0; |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 261 | #if defined(MBEDTLS_CCM_SHUFFLING_MASKING) |
| 262 | unsigned char perm_table[16]; |
| 263 | unsigned char mask_table[16]; |
| 264 | #endif /* MBEDTLS_CCM_SHUFFLING_MASKING */ |
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 | */ |
Janos Follath | 997e85c | 2018-05-29 11:33:45 +0100 | [diff] [blame] | 272 | if( tag_len == 2 || tag_len > 16 || tag_len % 2 != 0 ) |
Janos Follath | 997e85c | 2018-05-29 11:33:45 +0100 | [diff] [blame] | 273 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 274 | |
| 275 | /* Also implies q is within bounds */ |
| 276 | if( iv_len < 7 || iv_len > 13 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 277 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 278 | |
| 279 | if( add_len > 0xFF00 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 281 | |
Andrzej Kurek | 142f09f | 2020-11-04 13:20:24 +0100 | [diff] [blame] | 282 | mbedtls_platform_zeroize( b, 16 ); |
| 283 | mbedtls_platform_zeroize( y, 16 ); |
| 284 | mbedtls_platform_zeroize( ctr, 16 ); |
| 285 | |
Kevin Bracey | a967a58 | 2020-11-04 16:28:48 +0200 | [diff] [blame] | 286 | q = (uint_fast8_t) (16 - 1 - iv_len); |
Andrzej Kurek | ab3de1d | 2020-11-10 10:52:56 +0100 | [diff] [blame] | 287 | flow_ctrl++; /* 1 */ |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 288 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 289 | /* |
| 290 | * First block B_0: |
| 291 | * 0 .. 0 flags |
| 292 | * 1 .. iv_len nonce (aka iv) |
| 293 | * iv_len+1 .. 15 length |
| 294 | * |
| 295 | * With flags as (bits): |
| 296 | * 7 0 |
| 297 | * 6 add present? |
| 298 | * 5 .. 3 (t - 2) / 2 |
| 299 | * 2 .. 0 q - 1 |
| 300 | */ |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 301 | #if defined(MBEDTLS_CCM_SHUFFLING_MASKING) |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 302 | mbedtls_generate_masks( mask_table, 16 ); |
| 303 | mbedtls_generate_permutation( perm_table, 16 ); |
| 304 | b[0] = (unsigned char) ( ( ( add_len > 0 ) << 6 ) | |
| 305 | ( ( ( tag_len - 2 ) / 2 ) << 3 ) | |
| 306 | ( q - 1 ) ) ^ mask_table[0]; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 307 | |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 308 | for( i = 0; i < iv_len; i++ ) |
Andrzej Kurek | ab3de1d | 2020-11-10 10:52:56 +0100 | [diff] [blame] | 309 | { |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 310 | b[i+1] = iv[i] ^ mask_table[i+1]; |
Andrzej Kurek | ab3de1d | 2020-11-10 10:52:56 +0100 | [diff] [blame] | 311 | flow_ctrl++; /* iv_len + 1 */ |
| 312 | } |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 313 | for( i = 0, len_left = length; i < q; i++, len_left >>= 8 ) |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 314 | b[15-i] = (unsigned char)( ( len_left & 0xFF ) ) ^ mask_table[15-i]; |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 315 | #else |
| 316 | b[0] = 0; |
| 317 | b[0] |= ( add_len > 0 ) << 6; |
| 318 | b[0] |= ( ( tag_len - 2 ) / 2 ) << 3; |
| 319 | b[0] |= q - 1; |
| 320 | |
| 321 | mbedtls_platform_memcpy( b + 1, iv, iv_len ); |
| 322 | flow_ctrl += iv_len; /* iv_len + 1 */ |
| 323 | for( i = 0, len_left = length; i < q; i++, len_left >>= 8 ) |
| 324 | b[15-i] = (unsigned char)( len_left & 0xFF ); |
| 325 | #endif /* MBEDTLS_CCM_SHUFFLING_MASKING */ |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 326 | |
| 327 | if( len_left > 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 328 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 329 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 330 | /* Start CBC-MAC with first block */ |
| 331 | memset( y, 0, 16 ); |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 332 | UPDATE_CBC_MAC; |
Andrzej Kurek | ab3de1d | 2020-11-10 10:52:56 +0100 | [diff] [blame] | 333 | flow_ctrl++; /* iv_len + 2 */ |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 334 | |
| 335 | /* |
| 336 | * If there is additional data, update CBC-MAC with |
| 337 | * add_len, add, 0 (padding to a block boundary) |
| 338 | */ |
| 339 | if( add_len > 0 ) |
| 340 | { |
| 341 | size_t use_len; |
| 342 | len_left = add_len; |
| 343 | src = add; |
Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 344 | mbedtls_platform_memset( b, 0, 16 ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 345 | |
| 346 | use_len = len_left < 16 - 2 ? len_left : 16 - 2; |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 347 | |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 348 | #if defined(MBEDTLS_CCM_SHUFFLING_MASKING) |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 349 | COPY_MASK( b+2, src, mask_table+2, use_len, 14 ); |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 350 | b[0] = (unsigned char)( ( ( add_len >> 8 ) & 0xFF ) ^ mask_table[0] ); |
| 351 | b[1] = (unsigned char)( ( ( add_len ) & 0xFF ) ^ mask_table[1] ); |
| 352 | #else |
| 353 | b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF ); |
| 354 | b[1] = (unsigned char)( ( add_len ) & 0xFF ); |
| 355 | |
| 356 | mbedtls_platform_memcpy( b + 2, src, use_len ); |
| 357 | #endif /* MBEDTLS_CCM_SHUFFLING_MASKING */ |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 358 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 359 | len_left -= use_len; |
| 360 | src += use_len; |
| 361 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 362 | UPDATE_CBC_MAC; |
Andrzej Kurek | ab3de1d | 2020-11-10 10:52:56 +0100 | [diff] [blame] | 363 | flow_ctrl++; /* iv_len + 2 + ( add_len? 1 : 0 ) */ |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 364 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 365 | while( len_left > 0 ) |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 366 | { |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 367 | use_len = len_left > 16 ? 16 : len_left; |
Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 368 | mbedtls_platform_memset( b, 0, 16 ); |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 369 | |
| 370 | #if defined(MBEDTLS_CCM_SHUFFLING_MASKING) |
| 371 | COPY_MASK( b, src, mask_table, use_len, 16 ); |
| 372 | #else |
| 373 | mbedtls_platform_memcpy( b, src, use_len ); |
| 374 | #endif /* MBEDTLS_CCM_SHUFFLING_MASKING */ |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 375 | UPDATE_CBC_MAC; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 376 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 377 | len_left -= use_len; |
| 378 | src += use_len; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
| 382 | /* |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 383 | * Prepare counter block for encryption: |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 384 | * 0 .. 0 flags |
| 385 | * 1 .. iv_len nonce (aka iv) |
| 386 | * iv_len+1 .. 15 counter (initially 1) |
| 387 | * |
| 388 | * With flags as (bits): |
| 389 | * 7 .. 3 0 |
| 390 | * 2 .. 0 q - 1 |
| 391 | */ |
| 392 | ctr[0] = q - 1; |
Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 393 | mbedtls_platform_memcpy( ctr + 1, iv, iv_len ); |
Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 394 | mbedtls_platform_memset( ctr + 1 + iv_len, 0, q ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 395 | ctr[15] = 1; |
Andrzej Kurek | ab3de1d | 2020-11-10 10:52:56 +0100 | [diff] [blame] | 396 | flow_ctrl++; /* iv_len + 3 + ( add_len? 1 : 0 ) */ |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 397 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 398 | /* |
| 399 | * Authenticate and {en,de}crypt the message. |
| 400 | * |
| 401 | * The only difference between encryption and decryption is |
| 402 | * the respective order of authentication and {en,de}cryption. |
| 403 | */ |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 404 | len_left = length; |
| 405 | src = input; |
| 406 | dst = output; |
| 407 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 408 | while( len_left > 0 ) |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 409 | { |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 410 | size_t use_len = len_left > 16 ? 16 : len_left; |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 411 | |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 412 | if( mode == CCM_ENCRYPT ) |
| 413 | { |
Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 414 | mbedtls_platform_memset( b, 0, 16 ); |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 415 | #if defined(MBEDTLS_CCM_SHUFFLING_MASKING) |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 416 | COPY_MASK( b, src, mask_table, use_len, 16 ); |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 417 | #else |
| 418 | mbedtls_platform_memcpy( b, src, use_len ); |
| 419 | #endif /* MBEDTLS_CCM_SHUFFLING_MASKING */ |
| 420 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 421 | UPDATE_CBC_MAC; |
Andrzej Kurek | ab3de1d | 2020-11-10 10:52:56 +0100 | [diff] [blame] | 422 | flow_ctrl++; /* iv_len + 3 + ( add_len? 1 : 0 ) + encryptions */ |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 423 | } |
| 424 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 425 | CTR_CRYPT( dst, src, use_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 426 | |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 427 | if( mode == CCM_DECRYPT ) |
| 428 | { |
Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 429 | mbedtls_platform_memset( b, 0, 16 ); |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 430 | #if defined(MBEDTLS_CCM_SHUFFLING_MASKING) |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 431 | COPY_MASK( b, dst, mask_table, use_len, 16 ); |
Andrzej Kurek | 7d0a686 | 2020-11-26 06:34:04 -0500 | [diff] [blame] | 432 | #else |
| 433 | mbedtls_platform_memcpy( b, dst, use_len ); |
| 434 | #endif /* MBEDTLS_CCM_SHUFFLING_MASKING */ |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 435 | UPDATE_CBC_MAC; |
Andrzej Kurek | ab3de1d | 2020-11-10 10:52:56 +0100 | [diff] [blame] | 436 | flow_ctrl++; /* iv_len + 3 + ( add_len? 1 : 0 ) + decryptions */ |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 439 | dst += use_len; |
| 440 | src += use_len; |
| 441 | len_left -= use_len; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 442 | |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 443 | /* |
| 444 | * Increment counter. |
| 445 | * No need to check for overflow thanks to the length check above. |
| 446 | */ |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 447 | for( i = 0; i < q; i++ ) |
| 448 | if( ++ctr[15-i] != 0 ) |
| 449 | break; |
| 450 | } |
Andrzej Kurek | ab3de1d | 2020-11-10 10:52:56 +0100 | [diff] [blame] | 451 | flow_ctrl++; /* iv_len + 4 + ( add_len? 1 : 0 ) + enc/dec */ |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 452 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 453 | /* |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 454 | * Authentication: reset counter and crypt/mask internal tag |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 455 | */ |
| 456 | for( i = 0; i < q; i++ ) |
| 457 | ctr[15-i] = 0; |
| 458 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 459 | CTR_CRYPT( y, y, 16 ); |
Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 460 | mbedtls_platform_memcpy( tag, y, tag_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 461 | |
Andrzej Kurek | ab3de1d | 2020-11-10 10:52:56 +0100 | [diff] [blame] | 462 | flow_ctrl++; /* iv_len + 5 + ( add_len? 1 : 0 ) + enc/dec */ |
| 463 | |
Andrzej Kurek | 142f09f | 2020-11-04 13:20:24 +0100 | [diff] [blame] | 464 | mbedtls_platform_zeroize( b, 16 ); |
| 465 | mbedtls_platform_zeroize( y, 16 ); |
| 466 | mbedtls_platform_zeroize( ctr, 16 ); |
| 467 | |
Andrzej Kurek | ab3de1d | 2020-11-10 10:52:56 +0100 | [diff] [blame] | 468 | { |
| 469 | size_t operations = length / 16; |
| 470 | operations += ( length % 16 ? 1 : 0 ); |
| 471 | operations += ( add_len > 0 ? 1 : 0 ); |
| 472 | /* See comments above on steps in calculating flow_ctrl */ |
| 473 | if( flow_ctrl == iv_len + 5 + operations ) |
| 474 | { |
| 475 | mbedtls_platform_random_delay(); |
| 476 | if( flow_ctrl == iv_len + 5 + operations ) |
| 477 | return( ret ); |
| 478 | } |
| 479 | } |
| 480 | return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 481 | } |
| 482 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 483 | /* |
| 484 | * Authenticated encryption |
| 485 | */ |
Janos Follath | b5734a2 | 2018-05-14 14:31:49 +0100 | [diff] [blame] | 486 | int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 487 | const unsigned char *iv, size_t iv_len, |
| 488 | const unsigned char *add, size_t add_len, |
| 489 | const unsigned char *input, unsigned char *output, |
| 490 | unsigned char *tag, size_t tag_len ) |
| 491 | { |
k-stachowiak | 26d365e | 2018-12-11 12:22:16 +0100 | [diff] [blame] | 492 | CCM_VALIDATE_RET( ctx != NULL ); |
| 493 | CCM_VALIDATE_RET( iv != NULL ); |
k-stachowiak | ff8a098 | 2018-12-11 15:56:55 +0100 | [diff] [blame] | 494 | CCM_VALIDATE_RET( add_len == 0 || add != NULL ); |
| 495 | CCM_VALIDATE_RET( length == 0 || input != NULL ); |
| 496 | CCM_VALIDATE_RET( length == 0 || output != NULL ); |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 497 | CCM_VALIDATE_RET( tag_len == 0 || tag != NULL ); |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 498 | return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len, |
| 499 | add, add_len, input, output, tag, tag_len ) ); |
| 500 | } |
| 501 | |
Janos Follath | b5734a2 | 2018-05-14 14:31:49 +0100 | [diff] [blame] | 502 | int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, |
| 503 | const unsigned char *iv, size_t iv_len, |
| 504 | const unsigned char *add, size_t add_len, |
| 505 | const unsigned char *input, unsigned char *output, |
| 506 | unsigned char *tag, size_t tag_len ) |
| 507 | { |
k-stachowiak | 26d365e | 2018-12-11 12:22:16 +0100 | [diff] [blame] | 508 | CCM_VALIDATE_RET( ctx != NULL ); |
| 509 | CCM_VALIDATE_RET( iv != NULL ); |
k-stachowiak | ff8a098 | 2018-12-11 15:56:55 +0100 | [diff] [blame] | 510 | CCM_VALIDATE_RET( add_len == 0 || add != NULL ); |
| 511 | CCM_VALIDATE_RET( length == 0 || input != NULL ); |
| 512 | CCM_VALIDATE_RET( length == 0 || output != NULL ); |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 513 | CCM_VALIDATE_RET( tag_len == 0 || tag != NULL ); |
Janos Follath | b5734a2 | 2018-05-14 14:31:49 +0100 | [diff] [blame] | 514 | if( tag_len == 0 ) |
| 515 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
| 516 | |
| 517 | return( mbedtls_ccm_star_encrypt_and_tag( ctx, length, iv, iv_len, add, |
| 518 | add_len, input, output, tag, tag_len ) ); |
| 519 | } |
| 520 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 521 | /* |
| 522 | * Authenticated decryption |
| 523 | */ |
Janos Follath | b5734a2 | 2018-05-14 14:31:49 +0100 | [diff] [blame] | 524 | int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length, |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 525 | const unsigned char *iv, size_t iv_len, |
| 526 | const unsigned char *add, size_t add_len, |
| 527 | const unsigned char *input, unsigned char *output, |
| 528 | const unsigned char *tag, size_t tag_len ) |
| 529 | { |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 530 | int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED; |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 531 | unsigned char check_tag[16]; |
Kevin Bracey | a967a58 | 2020-11-04 16:28:48 +0200 | [diff] [blame] | 532 | uint_fast8_t i; |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 533 | int diff; |
| 534 | |
k-stachowiak | 26d365e | 2018-12-11 12:22:16 +0100 | [diff] [blame] | 535 | CCM_VALIDATE_RET( ctx != NULL ); |
| 536 | CCM_VALIDATE_RET( iv != NULL ); |
k-stachowiak | ff8a098 | 2018-12-11 15:56:55 +0100 | [diff] [blame] | 537 | CCM_VALIDATE_RET( add_len == 0 || add != NULL ); |
| 538 | CCM_VALIDATE_RET( length == 0 || input != NULL ); |
| 539 | CCM_VALIDATE_RET( length == 0 || output != NULL ); |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 540 | CCM_VALIDATE_RET( tag_len == 0 || tag != NULL ); |
k-stachowiak | 26d365e | 2018-12-11 12:22:16 +0100 | [diff] [blame] | 541 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 542 | if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length, |
| 543 | iv, iv_len, add, add_len, |
| 544 | input, output, check_tag, tag_len ) ) != 0 ) |
| 545 | { |
| 546 | return( ret ); |
| 547 | } |
| 548 | |
| 549 | /* Check tag in "constant-time" */ |
| 550 | for( diff = 0, i = 0; i < tag_len; i++ ) |
| 551 | diff |= tag[i] ^ check_tag[i]; |
| 552 | |
| 553 | if( diff != 0 ) |
| 554 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 555 | mbedtls_platform_zeroize( output, length ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 556 | return( MBEDTLS_ERR_CCM_AUTH_FAILED ); |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 557 | } |
| 558 | |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 559 | return( ret ); |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 560 | } |
| 561 | |
Janos Follath | b5734a2 | 2018-05-14 14:31:49 +0100 | [diff] [blame] | 562 | int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length, |
| 563 | const unsigned char *iv, size_t iv_len, |
| 564 | const unsigned char *add, size_t add_len, |
| 565 | const unsigned char *input, unsigned char *output, |
| 566 | const unsigned char *tag, size_t tag_len ) |
| 567 | { |
k-stachowiak | f712534 | 2018-12-11 15:57:19 +0100 | [diff] [blame] | 568 | CCM_VALIDATE_RET( ctx != NULL ); |
| 569 | CCM_VALIDATE_RET( iv != NULL ); |
| 570 | CCM_VALIDATE_RET( add_len == 0 || add != NULL ); |
| 571 | CCM_VALIDATE_RET( length == 0 || input != NULL ); |
| 572 | CCM_VALIDATE_RET( length == 0 || output != NULL ); |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 573 | CCM_VALIDATE_RET( tag_len == 0 || tag != NULL ); |
k-stachowiak | f712534 | 2018-12-11 15:57:19 +0100 | [diff] [blame] | 574 | |
Janos Follath | b5734a2 | 2018-05-14 14:31:49 +0100 | [diff] [blame] | 575 | if( tag_len == 0 ) |
| 576 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
| 577 | |
| 578 | return( mbedtls_ccm_star_auth_decrypt( ctx, length, iv, iv_len, add, |
| 579 | add_len, input, output, tag, tag_len ) ); |
| 580 | } |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 581 | #endif /* !MBEDTLS_CCM_ALT */ |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 582 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 583 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 584 | /* |
| 585 | * Examples 1 to 3 from SP800-38C Appendix C |
| 586 | */ |
| 587 | |
| 588 | #define NB_TESTS 3 |
Ron Eldor | 1b9b217 | 2018-04-26 14:15:01 +0300 | [diff] [blame] | 589 | #define CCM_SELFTEST_PT_MAX_LEN 24 |
| 590 | #define CCM_SELFTEST_CT_MAX_LEN 32 |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 591 | /* |
| 592 | * The data is the same for all tests, only the used length changes |
| 593 | */ |
| 594 | static const unsigned char key[] = { |
| 595 | 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, |
| 596 | 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f |
| 597 | }; |
| 598 | |
| 599 | static const unsigned char iv[] = { |
| 600 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 601 | 0x18, 0x19, 0x1a, 0x1b |
| 602 | }; |
| 603 | |
| 604 | static const unsigned char ad[] = { |
| 605 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 606 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 607 | 0x10, 0x11, 0x12, 0x13 |
| 608 | }; |
| 609 | |
Ron Eldor | 1b9b217 | 2018-04-26 14:15:01 +0300 | [diff] [blame] | 610 | static const unsigned char msg[CCM_SELFTEST_PT_MAX_LEN] = { |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 611 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, |
| 612 | 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, |
| 613 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 614 | }; |
| 615 | |
| 616 | static const size_t iv_len [NB_TESTS] = { 7, 8, 12 }; |
| 617 | static const size_t add_len[NB_TESTS] = { 8, 16, 20 }; |
| 618 | static const size_t msg_len[NB_TESTS] = { 4, 16, 24 }; |
| 619 | static const size_t tag_len[NB_TESTS] = { 4, 6, 8 }; |
| 620 | |
Ron Eldor | 1b9b217 | 2018-04-26 14:15:01 +0300 | [diff] [blame] | 621 | static const unsigned char res[NB_TESTS][CCM_SELFTEST_CT_MAX_LEN] = { |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 622 | { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d }, |
| 623 | { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62, |
| 624 | 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d, |
| 625 | 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd }, |
| 626 | { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a, |
| 627 | 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b, |
| 628 | 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5, |
| 629 | 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 } |
| 630 | }; |
| 631 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 632 | int mbedtls_ccm_self_test( int verbose ) |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 633 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 634 | mbedtls_ccm_context ctx; |
Ron Eldor | 1b9b217 | 2018-04-26 14:15:01 +0300 | [diff] [blame] | 635 | /* |
| 636 | * Some hardware accelerators require the input and output buffers |
| 637 | * would be in RAM, because the flash is not accessible. |
| 638 | * Use buffers on the stack to hold the test vectors data. |
| 639 | */ |
| 640 | unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN]; |
| 641 | unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN]; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 642 | size_t i; |
| 643 | int ret; |
| 644 | |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 645 | mbedtls_ccm_init( &ctx ); |
| 646 | |
| 647 | if( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 ) |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 648 | { |
| 649 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 650 | mbedtls_printf( " CCM: setup failed" ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 651 | |
| 652 | return( 1 ); |
| 653 | } |
| 654 | |
| 655 | for( i = 0; i < NB_TESTS; i++ ) |
| 656 | { |
| 657 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 658 | mbedtls_printf( " CCM-AES #%u: ", (unsigned int) i + 1 ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 659 | |
Teppo Järvelin | b5c4671 | 2019-10-04 13:35:55 +0300 | [diff] [blame] | 660 | memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN ); |
| 661 | memset( ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN ); |
| 662 | memcpy( plaintext, msg, msg_len[i] ); |
Ron Eldor | 1b9b217 | 2018-04-26 14:15:01 +0300 | [diff] [blame] | 663 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 664 | ret = mbedtls_ccm_encrypt_and_tag( &ctx, msg_len[i], |
Ron Eldor | 1b9b217 | 2018-04-26 14:15:01 +0300 | [diff] [blame] | 665 | iv, iv_len[i], ad, add_len[i], |
| 666 | plaintext, ciphertext, |
| 667 | ciphertext + msg_len[i], tag_len[i] ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 668 | |
| 669 | if( ret != 0 || |
Teppo Järvelin | 61f412e | 2019-10-03 12:25:22 +0300 | [diff] [blame] | 670 | memcmp( ciphertext, res[i], msg_len[i] + tag_len[i] ) != 0 ) |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 671 | { |
| 672 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 673 | mbedtls_printf( "failed\n" ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 674 | |
| 675 | return( 1 ); |
| 676 | } |
Teppo Järvelin | d49d2b6 | 2019-10-30 13:48:12 +0200 | [diff] [blame] | 677 | memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 678 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 679 | ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len[i], |
Ron Eldor | 1b9b217 | 2018-04-26 14:15:01 +0300 | [diff] [blame] | 680 | iv, iv_len[i], ad, add_len[i], |
| 681 | ciphertext, plaintext, |
| 682 | ciphertext + msg_len[i], tag_len[i] ); |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 683 | |
| 684 | if( ret != 0 || |
Teppo Järvelin | 61f412e | 2019-10-03 12:25:22 +0300 | [diff] [blame] | 685 | memcmp( plaintext, msg, msg_len[i] ) != 0 ) |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 686 | { |
| 687 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 688 | mbedtls_printf( "failed\n" ); |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 689 | |
| 690 | return( 1 ); |
| 691 | } |
| 692 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 693 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 694 | mbedtls_printf( "passed\n" ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 695 | } |
| 696 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 697 | mbedtls_ccm_free( &ctx ); |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 698 | |
| 699 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 700 | mbedtls_printf( "\n" ); |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 701 | |
| 702 | return( 0 ); |
| 703 | } |
| 704 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 705 | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 706 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 707 | #endif /* MBEDTLS_CCM_C */ |