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 | 8bef87e | 2020-10-30 12:18:21 +0100 | [diff] [blame] | 117 | /* Durstenfeld's version of Fisher-Yates shuffle */ |
| 118 | static void mbedtls_generate_permutation( unsigned char* table, size_t size ) |
| 119 | { |
| 120 | size_t i, j; |
| 121 | |
| 122 | for( i = 0; i < size; i++ ) |
| 123 | { |
| 124 | table[i] = (unsigned char) i; |
| 125 | } |
| 126 | |
| 127 | if( size < 2 ) |
| 128 | { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | for( i = size - 1; i > 0; i-- ) |
| 133 | { |
| 134 | unsigned char tmp; |
Andrzej Kurek | 18c60aa | 2020-11-17 12:16:42 +0100 | [diff] [blame] | 135 | j = mbedtls_platform_random_in_range( (uint32_t) i + 1 ); |
Andrzej Kurek | 8bef87e | 2020-10-30 12:18:21 +0100 | [diff] [blame] | 136 | tmp = table[i]; |
| 137 | table[i] = table[j]; |
| 138 | table[j] = tmp; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | static void mbedtls_generate_masks( unsigned char* table, size_t size ) |
| 143 | { |
| 144 | size_t i; |
| 145 | |
| 146 | for( i = 0; i < size; i++ ) |
| 147 | { |
Andrzej Kurek | 18c60aa | 2020-11-17 12:16:42 +0100 | [diff] [blame] | 148 | table[i] = mbedtls_platform_random_in_range( 256 ); |
Andrzej Kurek | 8bef87e | 2020-10-30 12:18:21 +0100 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 152 | /* |
| 153 | * Macros for common operations. |
| 154 | * Results in smaller compiled code than static inline functions. |
| 155 | */ |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 156 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 157 | /* |
| 158 | * Update the CBC-MAC state in y using a block in b |
| 159 | * (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] | 160 | * 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] | 161 | */ |
| 162 | #define UPDATE_CBC_MAC \ |
| 163 | for( i = 0; i < 16; i++ ) \ |
Andrzej Kurek | 8bef87e | 2020-10-30 12:18:21 +0100 | [diff] [blame] | 164 | { \ |
| 165 | y[perm_table[i]] ^= b[perm_table[i]]; \ |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 166 | y[perm_table[i]] ^= mask_table[perm_table[i]]; \ |
Andrzej Kurek | 8bef87e | 2020-10-30 12:18:21 +0100 | [diff] [blame] | 167 | } \ |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 168 | \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 169 | 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] | 170 | return( ret ); |
| 171 | |
| 172 | /* |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 173 | * Copy src to dst starting at a random offset, while masking the whole dst buffer. |
| 174 | */ |
| 175 | #define COPY_MASK( dst, src, mask, len_src, len_dst ) \ |
| 176 | do \ |
| 177 | { \ |
Andrzej Kurek | 18c60aa | 2020-11-17 12:16:42 +0100 | [diff] [blame] | 178 | unsigned j, offset = mbedtls_platform_random_in_range( 256 ); \ |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 179 | for( i = 0; i < len_src; i++ ) \ |
| 180 | { \ |
| 181 | j = (i + offset) % len_src; \ |
| 182 | (dst)[j] = (src)[j] ^ (mask)[j]; \ |
| 183 | } \ |
| 184 | for( ; i < len_dst; i++ ) \ |
| 185 | (dst)[i] ^= (mask)[i]; \ |
| 186 | } while( 0 ) |
| 187 | /* |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 188 | * Encrypt or decrypt a partial block with CTR |
| 189 | * 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] | 190 | * 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] | 191 | */ |
Andrzej Kurek | 8bef87e | 2020-10-30 12:18:21 +0100 | [diff] [blame] | 192 | #define CTR_CRYPT( dst, src, len ) \ |
Hanno Becker | d6028a1 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 193 | do \ |
| 194 | { \ |
Andrzej Kurek | 8bef87e | 2020-10-30 12:18:21 +0100 | [diff] [blame] | 195 | mbedtls_generate_permutation( perm_table, len ); \ |
| 196 | mbedtls_generate_masks( mask_table, len ); \ |
Hanno Becker | d6028a1 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 197 | if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, \ |
| 198 | 16, b, &olen ) ) != 0 ) \ |
| 199 | { \ |
| 200 | return( ret ); \ |
| 201 | } \ |
| 202 | \ |
Andrzej Kurek | 8bef87e | 2020-10-30 12:18:21 +0100 | [diff] [blame] | 203 | for( i = 0; i < len; i++ ) \ |
| 204 | { \ |
| 205 | (dst)[perm_table[i]] = (src)[perm_table[i]] ^ mask_table[perm_table[i]];\ |
| 206 | (dst)[perm_table[i]] ^= b[perm_table[i]]; \ |
| 207 | (dst)[perm_table[i]] ^= mask_table[perm_table[i]]; \ |
| 208 | } \ |
Hanno Becker | d6028a1 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 209 | } while( 0 ) |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 210 | |
| 211 | /* |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 212 | * Authenticated encryption or decryption |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 213 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 214 | 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] | 215 | const unsigned char *iv, size_t iv_len, |
| 216 | const unsigned char *add, size_t add_len, |
| 217 | const unsigned char *input, unsigned char *output, |
| 218 | unsigned char *tag, size_t tag_len ) |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 219 | { |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 220 | int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED; |
Kevin Bracey | a967a58 | 2020-11-04 16:28:48 +0200 | [diff] [blame] | 221 | uint_fast8_t i; |
| 222 | uint_fast8_t q; |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 223 | size_t len_left, olen; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 224 | unsigned char b[16]; |
| 225 | unsigned char y[16]; |
| 226 | unsigned char ctr[16]; |
Andrzej Kurek | 8bef87e | 2020-10-30 12:18:21 +0100 | [diff] [blame] | 227 | unsigned char perm_table[16]; |
| 228 | unsigned char mask_table[16]; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 229 | const unsigned char *src; |
| 230 | unsigned char *dst; |
| 231 | |
| 232 | /* |
| 233 | * Check length requirements: SP800-38C A.1 |
| 234 | * Additional requirement: a < 2^16 - 2^8 to simplify the code. |
| 235 | * 'length' checked later (when writing it to the first block) |
Janos Follath | 997e85c | 2018-05-29 11:33:45 +0100 | [diff] [blame] | 236 | * |
| 237 | * 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] | 238 | */ |
Janos Follath | 997e85c | 2018-05-29 11:33:45 +0100 | [diff] [blame] | 239 | if( tag_len == 2 || tag_len > 16 || tag_len % 2 != 0 ) |
Janos Follath | 997e85c | 2018-05-29 11:33:45 +0100 | [diff] [blame] | 240 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 241 | |
| 242 | /* Also implies q is within bounds */ |
| 243 | if( iv_len < 7 || iv_len > 13 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 244 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 245 | |
| 246 | if( add_len > 0xFF00 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 247 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 248 | |
Andrzej Kurek | 142f09f | 2020-11-04 13:20:24 +0100 | [diff] [blame] | 249 | mbedtls_platform_zeroize( b, 16 ); |
| 250 | mbedtls_platform_zeroize( y, 16 ); |
| 251 | mbedtls_platform_zeroize( ctr, 16 ); |
| 252 | |
Kevin Bracey | a967a58 | 2020-11-04 16:28:48 +0200 | [diff] [blame] | 253 | q = (uint_fast8_t) (16 - 1 - iv_len); |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 254 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 255 | /* |
| 256 | * First block B_0: |
| 257 | * 0 .. 0 flags |
| 258 | * 1 .. iv_len nonce (aka iv) |
| 259 | * iv_len+1 .. 15 length |
| 260 | * |
| 261 | * With flags as (bits): |
| 262 | * 7 0 |
| 263 | * 6 add present? |
| 264 | * 5 .. 3 (t - 2) / 2 |
| 265 | * 2 .. 0 q - 1 |
| 266 | */ |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 267 | mbedtls_generate_masks( mask_table, 16 ); |
| 268 | mbedtls_generate_permutation( perm_table, 16 ); |
| 269 | b[0] = (unsigned char) ( ( ( add_len > 0 ) << 6 ) | |
| 270 | ( ( ( tag_len - 2 ) / 2 ) << 3 ) | |
| 271 | ( q - 1 ) ) ^ mask_table[0]; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 272 | |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 273 | for( i = 0; i < iv_len; i++ ) |
| 274 | b[i+1] = iv[i] ^ mask_table[i+1]; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 275 | for( i = 0, len_left = length; i < q; i++, len_left >>= 8 ) |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 276 | b[15-i] = (unsigned char)( ( len_left & 0xFF ) ) ^ mask_table[15-i]; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 277 | |
| 278 | if( len_left > 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 280 | |
| 281 | |
| 282 | /* Start CBC-MAC with first block */ |
| 283 | memset( y, 0, 16 ); |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 284 | UPDATE_CBC_MAC; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 285 | |
| 286 | /* |
| 287 | * If there is additional data, update CBC-MAC with |
| 288 | * add_len, add, 0 (padding to a block boundary) |
| 289 | */ |
| 290 | if( add_len > 0 ) |
| 291 | { |
| 292 | size_t use_len; |
| 293 | len_left = add_len; |
| 294 | src = add; |
| 295 | |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 296 | mbedtls_generate_masks( mask_table, 16 ); |
| 297 | mbedtls_generate_permutation( perm_table, 16 ); |
Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 298 | mbedtls_platform_memset( b, 0, 16 ); |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 299 | b[0] = (unsigned char)( ( ( add_len >> 8 ) & 0xFF ) ^ mask_table[0] ); |
| 300 | b[1] = (unsigned char)( ( ( add_len ) & 0xFF ) ^ mask_table[1] ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 301 | |
| 302 | use_len = len_left < 16 - 2 ? len_left : 16 - 2; |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 303 | |
| 304 | COPY_MASK( b+2, src, mask_table+2, use_len, 14 ); |
| 305 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 306 | len_left -= use_len; |
| 307 | src += use_len; |
| 308 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 309 | UPDATE_CBC_MAC; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 310 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 311 | while( len_left > 0 ) |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 312 | { |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 313 | mbedtls_generate_masks( mask_table, 16 ); |
| 314 | mbedtls_generate_permutation( perm_table, 16 ); |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 315 | use_len = len_left > 16 ? 16 : len_left; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 316 | |
Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 317 | mbedtls_platform_memset( b, 0, 16 ); |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 318 | COPY_MASK( b, src, mask_table, use_len, 16); |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 319 | UPDATE_CBC_MAC; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 320 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 321 | len_left -= use_len; |
| 322 | src += use_len; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | /* |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 327 | * Prepare counter block for encryption: |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 328 | * 0 .. 0 flags |
| 329 | * 1 .. iv_len nonce (aka iv) |
| 330 | * iv_len+1 .. 15 counter (initially 1) |
| 331 | * |
| 332 | * With flags as (bits): |
| 333 | * 7 .. 3 0 |
| 334 | * 2 .. 0 q - 1 |
| 335 | */ |
| 336 | ctr[0] = q - 1; |
Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 337 | mbedtls_platform_memcpy( ctr + 1, iv, iv_len ); |
Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 338 | mbedtls_platform_memset( ctr + 1 + iv_len, 0, q ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 339 | ctr[15] = 1; |
| 340 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 341 | /* |
| 342 | * Authenticate and {en,de}crypt the message. |
| 343 | * |
| 344 | * The only difference between encryption and decryption is |
| 345 | * the respective order of authentication and {en,de}cryption. |
| 346 | */ |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 347 | len_left = length; |
| 348 | src = input; |
| 349 | dst = output; |
| 350 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 351 | while( len_left > 0 ) |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 352 | { |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 353 | size_t use_len = len_left > 16 ? 16 : len_left; |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 354 | |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 355 | if( mode == CCM_ENCRYPT ) |
| 356 | { |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 357 | mbedtls_generate_masks( mask_table, 16 ); |
| 358 | mbedtls_generate_permutation( perm_table, 16 ); |
Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 359 | mbedtls_platform_memset( b, 0, 16 ); |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 360 | COPY_MASK( b, src, mask_table, use_len, 16 ); |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 361 | UPDATE_CBC_MAC; |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 362 | } |
| 363 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 364 | CTR_CRYPT( dst, src, use_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 365 | |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 366 | if( mode == CCM_DECRYPT ) |
| 367 | { |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 368 | mbedtls_generate_masks( mask_table, 16 ); |
| 369 | mbedtls_generate_permutation( perm_table, 16 ); |
Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 370 | mbedtls_platform_memset( b, 0, 16 ); |
Andrzej Kurek | 0fa427b | 2020-10-30 14:41:12 +0100 | [diff] [blame] | 371 | COPY_MASK( b, dst, mask_table, use_len, 16 ); |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 372 | UPDATE_CBC_MAC; |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 375 | dst += use_len; |
| 376 | src += use_len; |
| 377 | len_left -= use_len; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 378 | |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 379 | /* |
| 380 | * Increment counter. |
| 381 | * No need to check for overflow thanks to the length check above. |
| 382 | */ |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 383 | for( i = 0; i < q; i++ ) |
| 384 | if( ++ctr[15-i] != 0 ) |
| 385 | break; |
| 386 | } |
| 387 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 388 | /* |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 389 | * Authentication: reset counter and crypt/mask internal tag |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 390 | */ |
| 391 | for( i = 0; i < q; i++ ) |
| 392 | ctr[15-i] = 0; |
| 393 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 394 | CTR_CRYPT( y, y, 16 ); |
Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 395 | mbedtls_platform_memcpy( tag, y, tag_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 396 | |
Andrzej Kurek | 142f09f | 2020-11-04 13:20:24 +0100 | [diff] [blame] | 397 | mbedtls_platform_zeroize( b, 16 ); |
| 398 | mbedtls_platform_zeroize( y, 16 ); |
| 399 | mbedtls_platform_zeroize( ctr, 16 ); |
| 400 | |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 401 | return( ret ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 402 | } |
| 403 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 404 | /* |
| 405 | * Authenticated encryption |
| 406 | */ |
Janos Follath | b5734a2 | 2018-05-14 14:31:49 +0100 | [diff] [blame] | 407 | 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] | 408 | const unsigned char *iv, size_t iv_len, |
| 409 | const unsigned char *add, size_t add_len, |
| 410 | const unsigned char *input, unsigned char *output, |
| 411 | unsigned char *tag, size_t tag_len ) |
| 412 | { |
k-stachowiak | 26d365e | 2018-12-11 12:22:16 +0100 | [diff] [blame] | 413 | CCM_VALIDATE_RET( ctx != NULL ); |
| 414 | CCM_VALIDATE_RET( iv != NULL ); |
k-stachowiak | ff8a098 | 2018-12-11 15:56:55 +0100 | [diff] [blame] | 415 | CCM_VALIDATE_RET( add_len == 0 || add != NULL ); |
| 416 | CCM_VALIDATE_RET( length == 0 || input != NULL ); |
| 417 | CCM_VALIDATE_RET( length == 0 || output != NULL ); |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 418 | CCM_VALIDATE_RET( tag_len == 0 || tag != NULL ); |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 419 | return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len, |
| 420 | add, add_len, input, output, tag, tag_len ) ); |
| 421 | } |
| 422 | |
Janos Follath | b5734a2 | 2018-05-14 14:31:49 +0100 | [diff] [blame] | 423 | int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, |
| 424 | const unsigned char *iv, size_t iv_len, |
| 425 | const unsigned char *add, size_t add_len, |
| 426 | const unsigned char *input, unsigned char *output, |
| 427 | unsigned char *tag, size_t tag_len ) |
| 428 | { |
k-stachowiak | 26d365e | 2018-12-11 12:22:16 +0100 | [diff] [blame] | 429 | CCM_VALIDATE_RET( ctx != NULL ); |
| 430 | CCM_VALIDATE_RET( iv != NULL ); |
k-stachowiak | ff8a098 | 2018-12-11 15:56:55 +0100 | [diff] [blame] | 431 | CCM_VALIDATE_RET( add_len == 0 || add != NULL ); |
| 432 | CCM_VALIDATE_RET( length == 0 || input != NULL ); |
| 433 | CCM_VALIDATE_RET( length == 0 || output != NULL ); |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 434 | CCM_VALIDATE_RET( tag_len == 0 || tag != NULL ); |
Janos Follath | b5734a2 | 2018-05-14 14:31:49 +0100 | [diff] [blame] | 435 | if( tag_len == 0 ) |
| 436 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
| 437 | |
| 438 | return( mbedtls_ccm_star_encrypt_and_tag( ctx, length, iv, iv_len, add, |
| 439 | add_len, input, output, tag, tag_len ) ); |
| 440 | } |
| 441 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 442 | /* |
| 443 | * Authenticated decryption |
| 444 | */ |
Janos Follath | b5734a2 | 2018-05-14 14:31:49 +0100 | [diff] [blame] | 445 | 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] | 446 | const unsigned char *iv, size_t iv_len, |
| 447 | const unsigned char *add, size_t add_len, |
| 448 | const unsigned char *input, unsigned char *output, |
| 449 | const unsigned char *tag, size_t tag_len ) |
| 450 | { |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 451 | int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED; |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 452 | unsigned char check_tag[16]; |
Kevin Bracey | a967a58 | 2020-11-04 16:28:48 +0200 | [diff] [blame] | 453 | uint_fast8_t i; |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 454 | int diff; |
| 455 | |
k-stachowiak | 26d365e | 2018-12-11 12:22:16 +0100 | [diff] [blame] | 456 | CCM_VALIDATE_RET( ctx != NULL ); |
| 457 | CCM_VALIDATE_RET( iv != NULL ); |
k-stachowiak | ff8a098 | 2018-12-11 15:56:55 +0100 | [diff] [blame] | 458 | CCM_VALIDATE_RET( add_len == 0 || add != NULL ); |
| 459 | CCM_VALIDATE_RET( length == 0 || input != NULL ); |
| 460 | CCM_VALIDATE_RET( length == 0 || output != NULL ); |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 461 | CCM_VALIDATE_RET( tag_len == 0 || tag != NULL ); |
k-stachowiak | 26d365e | 2018-12-11 12:22:16 +0100 | [diff] [blame] | 462 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 463 | if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length, |
| 464 | iv, iv_len, add, add_len, |
| 465 | input, output, check_tag, tag_len ) ) != 0 ) |
| 466 | { |
| 467 | return( ret ); |
| 468 | } |
| 469 | |
| 470 | /* Check tag in "constant-time" */ |
| 471 | for( diff = 0, i = 0; i < tag_len; i++ ) |
| 472 | diff |= tag[i] ^ check_tag[i]; |
| 473 | |
| 474 | if( diff != 0 ) |
| 475 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 476 | mbedtls_platform_zeroize( output, length ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 477 | return( MBEDTLS_ERR_CCM_AUTH_FAILED ); |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 478 | } |
| 479 | |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 480 | return( ret ); |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 481 | } |
| 482 | |
Janos Follath | b5734a2 | 2018-05-14 14:31:49 +0100 | [diff] [blame] | 483 | int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length, |
| 484 | const unsigned char *iv, size_t iv_len, |
| 485 | const unsigned char *add, size_t add_len, |
| 486 | const unsigned char *input, unsigned char *output, |
| 487 | const unsigned char *tag, size_t tag_len ) |
| 488 | { |
k-stachowiak | f712534 | 2018-12-11 15:57:19 +0100 | [diff] [blame] | 489 | CCM_VALIDATE_RET( ctx != NULL ); |
| 490 | CCM_VALIDATE_RET( iv != NULL ); |
| 491 | CCM_VALIDATE_RET( add_len == 0 || add != NULL ); |
| 492 | CCM_VALIDATE_RET( length == 0 || input != NULL ); |
| 493 | CCM_VALIDATE_RET( length == 0 || output != NULL ); |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 494 | CCM_VALIDATE_RET( tag_len == 0 || tag != NULL ); |
k-stachowiak | f712534 | 2018-12-11 15:57:19 +0100 | [diff] [blame] | 495 | |
Janos Follath | b5734a2 | 2018-05-14 14:31:49 +0100 | [diff] [blame] | 496 | if( tag_len == 0 ) |
| 497 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
| 498 | |
| 499 | return( mbedtls_ccm_star_auth_decrypt( ctx, length, iv, iv_len, add, |
| 500 | add_len, input, output, tag, tag_len ) ); |
| 501 | } |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 502 | #endif /* !MBEDTLS_CCM_ALT */ |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 503 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 504 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 505 | /* |
| 506 | * Examples 1 to 3 from SP800-38C Appendix C |
| 507 | */ |
| 508 | |
| 509 | #define NB_TESTS 3 |
Ron Eldor | 1b9b217 | 2018-04-26 14:15:01 +0300 | [diff] [blame] | 510 | #define CCM_SELFTEST_PT_MAX_LEN 24 |
| 511 | #define CCM_SELFTEST_CT_MAX_LEN 32 |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 512 | /* |
| 513 | * The data is the same for all tests, only the used length changes |
| 514 | */ |
| 515 | static const unsigned char key[] = { |
| 516 | 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, |
| 517 | 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f |
| 518 | }; |
| 519 | |
| 520 | static const unsigned char iv[] = { |
| 521 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 522 | 0x18, 0x19, 0x1a, 0x1b |
| 523 | }; |
| 524 | |
| 525 | static const unsigned char ad[] = { |
| 526 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 527 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 528 | 0x10, 0x11, 0x12, 0x13 |
| 529 | }; |
| 530 | |
Ron Eldor | 1b9b217 | 2018-04-26 14:15:01 +0300 | [diff] [blame] | 531 | static const unsigned char msg[CCM_SELFTEST_PT_MAX_LEN] = { |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 532 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, |
| 533 | 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, |
| 534 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 535 | }; |
| 536 | |
| 537 | static const size_t iv_len [NB_TESTS] = { 7, 8, 12 }; |
| 538 | static const size_t add_len[NB_TESTS] = { 8, 16, 20 }; |
| 539 | static const size_t msg_len[NB_TESTS] = { 4, 16, 24 }; |
| 540 | static const size_t tag_len[NB_TESTS] = { 4, 6, 8 }; |
| 541 | |
Ron Eldor | 1b9b217 | 2018-04-26 14:15:01 +0300 | [diff] [blame] | 542 | 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] | 543 | { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d }, |
| 544 | { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62, |
| 545 | 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d, |
| 546 | 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd }, |
| 547 | { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a, |
| 548 | 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b, |
| 549 | 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5, |
| 550 | 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 } |
| 551 | }; |
| 552 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 553 | int mbedtls_ccm_self_test( int verbose ) |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 554 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 555 | mbedtls_ccm_context ctx; |
Ron Eldor | 1b9b217 | 2018-04-26 14:15:01 +0300 | [diff] [blame] | 556 | /* |
| 557 | * Some hardware accelerators require the input and output buffers |
| 558 | * would be in RAM, because the flash is not accessible. |
| 559 | * Use buffers on the stack to hold the test vectors data. |
| 560 | */ |
| 561 | unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN]; |
| 562 | unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN]; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 563 | size_t i; |
| 564 | int ret; |
| 565 | |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 566 | mbedtls_ccm_init( &ctx ); |
| 567 | |
| 568 | 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] | 569 | { |
| 570 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 571 | mbedtls_printf( " CCM: setup failed" ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 572 | |
| 573 | return( 1 ); |
| 574 | } |
| 575 | |
| 576 | for( i = 0; i < NB_TESTS; i++ ) |
| 577 | { |
| 578 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 579 | mbedtls_printf( " CCM-AES #%u: ", (unsigned int) i + 1 ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 580 | |
Teppo Järvelin | b5c4671 | 2019-10-04 13:35:55 +0300 | [diff] [blame] | 581 | memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN ); |
| 582 | memset( ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN ); |
| 583 | memcpy( plaintext, msg, msg_len[i] ); |
Ron Eldor | 1b9b217 | 2018-04-26 14:15:01 +0300 | [diff] [blame] | 584 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 585 | ret = mbedtls_ccm_encrypt_and_tag( &ctx, msg_len[i], |
Ron Eldor | 1b9b217 | 2018-04-26 14:15:01 +0300 | [diff] [blame] | 586 | iv, iv_len[i], ad, add_len[i], |
| 587 | plaintext, ciphertext, |
| 588 | ciphertext + msg_len[i], tag_len[i] ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 589 | |
| 590 | if( ret != 0 || |
Teppo Järvelin | 61f412e | 2019-10-03 12:25:22 +0300 | [diff] [blame] | 591 | 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] | 592 | { |
| 593 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 594 | mbedtls_printf( "failed\n" ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 595 | |
| 596 | return( 1 ); |
| 597 | } |
Teppo Järvelin | d49d2b6 | 2019-10-30 13:48:12 +0200 | [diff] [blame] | 598 | memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 599 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 600 | ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len[i], |
Ron Eldor | 1b9b217 | 2018-04-26 14:15:01 +0300 | [diff] [blame] | 601 | iv, iv_len[i], ad, add_len[i], |
| 602 | ciphertext, plaintext, |
| 603 | ciphertext + msg_len[i], tag_len[i] ); |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 604 | |
| 605 | if( ret != 0 || |
Teppo Järvelin | 61f412e | 2019-10-03 12:25:22 +0300 | [diff] [blame] | 606 | memcmp( plaintext, msg, msg_len[i] ) != 0 ) |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 607 | { |
| 608 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 609 | mbedtls_printf( "failed\n" ); |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 610 | |
| 611 | return( 1 ); |
| 612 | } |
| 613 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 614 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 615 | mbedtls_printf( "passed\n" ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 616 | } |
| 617 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 618 | mbedtls_ccm_free( &ctx ); |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 619 | |
| 620 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 621 | mbedtls_printf( "\n" ); |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 622 | |
| 623 | return( 0 ); |
| 624 | } |
| 625 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 626 | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 627 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 628 | #endif /* MBEDTLS_CCM_C */ |