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 | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2014, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * 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] | 7 | * |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * Definition of CCM: |
| 25 | * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf |
| 26 | * RFC 3610 "Counter with CBC-MAC (CCM)" |
| 27 | * |
| 28 | * Related: |
| 29 | * RFC 5116 "An Interface and Algorithms for Authenticated Encryption" |
| 30 | */ |
| 31 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 33 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 34 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 36 | #endif |
| 37 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 38 | #if defined(MBEDTLS_CCM_C) |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 39 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 40 | #include "mbedtls/ccm.h" |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 41 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 42 | #include <string.h> |
| 43 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 44 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
| 45 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 46 | #include "mbedtls/platform.h" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 47 | #else |
| 48 | #include <stdio.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | #define mbedtls_printf printf |
| 50 | #endif /* MBEDTLS_PLATFORM_C */ |
| 51 | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 52 | |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 53 | /* Implementation that should never be optimized out by the compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | static void mbedtls_zeroize( void *v, size_t n ) { |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 55 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 56 | } |
| 57 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 58 | #define CCM_ENCRYPT 0 |
| 59 | #define CCM_DECRYPT 1 |
| 60 | |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 61 | /* |
| 62 | * Initialize context |
| 63 | */ |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 64 | void mbedtls_ccm_init( mbedtls_ccm_context *ctx ) |
| 65 | { |
| 66 | memset( ctx, 0, sizeof( mbedtls_ccm_context ) ); |
| 67 | } |
| 68 | |
| 69 | int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx, |
| 70 | mbedtls_cipher_id_t cipher, |
| 71 | const unsigned char *key, |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 72 | unsigned int keybits ) |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 73 | { |
| 74 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 75 | const mbedtls_cipher_info_t *cipher_info; |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 76 | |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 77 | 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] | 78 | if( cipher_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 79 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 80 | |
| 81 | if( cipher_info->block_size != 16 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 83 | |
Manuel Pégourié-Gonnard | 43b0857 | 2015-05-27 17:23:30 +0200 | [diff] [blame] | 84 | mbedtls_cipher_free( &ctx->cipher_ctx ); |
| 85 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 86 | 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] | 87 | return( ret ); |
| 88 | |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 89 | if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | MBEDTLS_ENCRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 91 | { |
| 92 | return( ret ); |
| 93 | } |
| 94 | |
| 95 | return( 0 ); |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * Free context |
| 100 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 101 | void mbedtls_ccm_free( mbedtls_ccm_context *ctx ) |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 102 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | mbedtls_cipher_free( &ctx->cipher_ctx ); |
| 104 | mbedtls_zeroize( ctx, sizeof( mbedtls_ccm_context ) ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 107 | /* |
| 108 | * Macros for common operations. |
| 109 | * Results in smaller compiled code than static inline functions. |
| 110 | */ |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 111 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 112 | /* |
| 113 | * Update the CBC-MAC state in y using a block in b |
| 114 | * (Always using b as the source helps the compiler optimise a bit better.) |
| 115 | */ |
| 116 | #define UPDATE_CBC_MAC \ |
| 117 | for( i = 0; i < 16; i++ ) \ |
| 118 | y[i] ^= b[i]; \ |
| 119 | \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 120 | 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] | 121 | return( ret ); |
| 122 | |
| 123 | /* |
| 124 | * Encrypt or decrypt a partial block with CTR |
| 125 | * 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] | 126 | * 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] | 127 | */ |
| 128 | #define CTR_CRYPT( dst, src, len ) \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, 16, b, &olen ) ) != 0 ) \ |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 130 | return( ret ); \ |
| 131 | \ |
| 132 | for( i = 0; i < len; i++ ) \ |
| 133 | dst[i] = src[i] ^ b[i]; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 134 | |
| 135 | /* |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 136 | * Authenticated encryption or decryption |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 137 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 138 | 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] | 139 | const unsigned char *iv, size_t iv_len, |
| 140 | const unsigned char *add, size_t add_len, |
| 141 | const unsigned char *input, unsigned char *output, |
| 142 | unsigned char *tag, size_t tag_len ) |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 143 | { |
| 144 | int ret; |
| 145 | unsigned char i; |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 146 | unsigned char q; |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 147 | size_t len_left, olen; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 148 | unsigned char b[16]; |
| 149 | unsigned char y[16]; |
| 150 | unsigned char ctr[16]; |
| 151 | const unsigned char *src; |
| 152 | unsigned char *dst; |
| 153 | |
| 154 | /* |
| 155 | * Check length requirements: SP800-38C A.1 |
| 156 | * Additional requirement: a < 2^16 - 2^8 to simplify the code. |
| 157 | * 'length' checked later (when writing it to the first block) |
| 158 | */ |
| 159 | if( tag_len < 4 || tag_len > 16 || tag_len % 2 != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 160 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 161 | |
| 162 | /* Also implies q is within bounds */ |
| 163 | if( iv_len < 7 || iv_len > 13 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 164 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 165 | |
| 166 | if( add_len > 0xFF00 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 168 | |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 169 | q = 16 - 1 - (unsigned char) iv_len; |
| 170 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 171 | /* |
| 172 | * First block B_0: |
| 173 | * 0 .. 0 flags |
| 174 | * 1 .. iv_len nonce (aka iv) |
| 175 | * iv_len+1 .. 15 length |
| 176 | * |
| 177 | * With flags as (bits): |
| 178 | * 7 0 |
| 179 | * 6 add present? |
| 180 | * 5 .. 3 (t - 2) / 2 |
| 181 | * 2 .. 0 q - 1 |
| 182 | */ |
| 183 | b[0] = 0; |
| 184 | b[0] |= ( add_len > 0 ) << 6; |
| 185 | b[0] |= ( ( tag_len - 2 ) / 2 ) << 3; |
| 186 | b[0] |= q - 1; |
| 187 | |
| 188 | memcpy( b + 1, iv, iv_len ); |
| 189 | |
| 190 | for( i = 0, len_left = length; i < q; i++, len_left >>= 8 ) |
| 191 | b[15-i] = (unsigned char)( len_left & 0xFF ); |
| 192 | |
| 193 | if( len_left > 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 195 | |
| 196 | |
| 197 | /* Start CBC-MAC with first block */ |
| 198 | memset( y, 0, 16 ); |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 199 | UPDATE_CBC_MAC; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 200 | |
| 201 | /* |
| 202 | * If there is additional data, update CBC-MAC with |
| 203 | * add_len, add, 0 (padding to a block boundary) |
| 204 | */ |
| 205 | if( add_len > 0 ) |
| 206 | { |
| 207 | size_t use_len; |
| 208 | len_left = add_len; |
| 209 | src = add; |
| 210 | |
| 211 | memset( b, 0, 16 ); |
| 212 | b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF ); |
| 213 | b[1] = (unsigned char)( ( add_len ) & 0xFF ); |
| 214 | |
| 215 | use_len = len_left < 16 - 2 ? len_left : 16 - 2; |
| 216 | memcpy( b + 2, src, use_len ); |
| 217 | len_left -= use_len; |
| 218 | src += use_len; |
| 219 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 220 | UPDATE_CBC_MAC; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 221 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 222 | while( len_left > 0 ) |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 223 | { |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 224 | use_len = len_left > 16 ? 16 : len_left; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 225 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 226 | memset( b, 0, 16 ); |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 227 | memcpy( b, src, use_len ); |
| 228 | UPDATE_CBC_MAC; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 229 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 230 | len_left -= use_len; |
| 231 | src += use_len; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
| 235 | /* |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 236 | * Prepare counter block for encryption: |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 237 | * 0 .. 0 flags |
| 238 | * 1 .. iv_len nonce (aka iv) |
| 239 | * iv_len+1 .. 15 counter (initially 1) |
| 240 | * |
| 241 | * With flags as (bits): |
| 242 | * 7 .. 3 0 |
| 243 | * 2 .. 0 q - 1 |
| 244 | */ |
| 245 | ctr[0] = q - 1; |
| 246 | memcpy( ctr + 1, iv, iv_len ); |
| 247 | memset( ctr + 1 + iv_len, 0, q ); |
| 248 | ctr[15] = 1; |
| 249 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 250 | /* |
| 251 | * Authenticate and {en,de}crypt the message. |
| 252 | * |
| 253 | * The only difference between encryption and decryption is |
| 254 | * the respective order of authentication and {en,de}cryption. |
| 255 | */ |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 256 | len_left = length; |
| 257 | src = input; |
| 258 | dst = output; |
| 259 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 260 | while( len_left > 0 ) |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 261 | { |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 262 | size_t use_len = len_left > 16 ? 16 : len_left; |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 263 | |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 264 | if( mode == CCM_ENCRYPT ) |
| 265 | { |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 266 | memset( b, 0, 16 ); |
| 267 | memcpy( b, src, use_len ); |
| 268 | UPDATE_CBC_MAC; |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 271 | CTR_CRYPT( dst, src, use_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 272 | |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 273 | if( mode == CCM_DECRYPT ) |
| 274 | { |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 275 | memset( b, 0, 16 ); |
| 276 | memcpy( b, dst, use_len ); |
| 277 | UPDATE_CBC_MAC; |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 280 | dst += use_len; |
| 281 | src += use_len; |
| 282 | len_left -= use_len; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 283 | |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 284 | /* |
| 285 | * Increment counter. |
| 286 | * No need to check for overflow thanks to the length check above. |
| 287 | */ |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 288 | for( i = 0; i < q; i++ ) |
| 289 | if( ++ctr[15-i] != 0 ) |
| 290 | break; |
| 291 | } |
| 292 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 293 | /* |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 294 | * Authentication: reset counter and crypt/mask internal tag |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 295 | */ |
| 296 | for( i = 0; i < q; i++ ) |
| 297 | ctr[15-i] = 0; |
| 298 | |
Manuel Pégourié-Gonnard | aed6065 | 2014-05-07 13:14:42 +0200 | [diff] [blame] | 299 | CTR_CRYPT( y, y, 16 ); |
| 300 | memcpy( tag, y, tag_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 301 | |
| 302 | return( 0 ); |
| 303 | } |
| 304 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 305 | /* |
| 306 | * Authenticated encryption |
| 307 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 308 | int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 309 | const unsigned char *iv, size_t iv_len, |
| 310 | const unsigned char *add, size_t add_len, |
| 311 | const unsigned char *input, unsigned char *output, |
| 312 | unsigned char *tag, size_t tag_len ) |
| 313 | { |
| 314 | return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len, |
| 315 | add, add_len, input, output, tag, tag_len ) ); |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Authenticated decryption |
| 320 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 321 | int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length, |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 322 | const unsigned char *iv, size_t iv_len, |
| 323 | const unsigned char *add, size_t add_len, |
| 324 | const unsigned char *input, unsigned char *output, |
| 325 | const unsigned char *tag, size_t tag_len ) |
| 326 | { |
| 327 | int ret; |
| 328 | unsigned char check_tag[16]; |
| 329 | unsigned char i; |
| 330 | int diff; |
| 331 | |
| 332 | if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length, |
| 333 | iv, iv_len, add, add_len, |
| 334 | input, output, check_tag, tag_len ) ) != 0 ) |
| 335 | { |
| 336 | return( ret ); |
| 337 | } |
| 338 | |
| 339 | /* Check tag in "constant-time" */ |
| 340 | for( diff = 0, i = 0; i < tag_len; i++ ) |
| 341 | diff |= tag[i] ^ check_tag[i]; |
| 342 | |
| 343 | if( diff != 0 ) |
| 344 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 345 | mbedtls_zeroize( output, length ); |
| 346 | return( MBEDTLS_ERR_CCM_AUTH_FAILED ); |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | return( 0 ); |
| 350 | } |
| 351 | |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 352 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 353 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 354 | /* |
| 355 | * Examples 1 to 3 from SP800-38C Appendix C |
| 356 | */ |
| 357 | |
| 358 | #define NB_TESTS 3 |
| 359 | |
| 360 | /* |
| 361 | * The data is the same for all tests, only the used length changes |
| 362 | */ |
| 363 | static const unsigned char key[] = { |
| 364 | 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, |
| 365 | 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f |
| 366 | }; |
| 367 | |
| 368 | static const unsigned char iv[] = { |
| 369 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 370 | 0x18, 0x19, 0x1a, 0x1b |
| 371 | }; |
| 372 | |
| 373 | static const unsigned char ad[] = { |
| 374 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 375 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 376 | 0x10, 0x11, 0x12, 0x13 |
| 377 | }; |
| 378 | |
| 379 | static const unsigned char msg[] = { |
| 380 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, |
| 381 | 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, |
| 382 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 383 | }; |
| 384 | |
| 385 | static const size_t iv_len [NB_TESTS] = { 7, 8, 12 }; |
| 386 | static const size_t add_len[NB_TESTS] = { 8, 16, 20 }; |
| 387 | static const size_t msg_len[NB_TESTS] = { 4, 16, 24 }; |
| 388 | static const size_t tag_len[NB_TESTS] = { 4, 6, 8 }; |
| 389 | |
| 390 | static const unsigned char res[NB_TESTS][32] = { |
| 391 | { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d }, |
| 392 | { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62, |
| 393 | 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d, |
| 394 | 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd }, |
| 395 | { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a, |
| 396 | 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b, |
| 397 | 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5, |
| 398 | 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 } |
| 399 | }; |
| 400 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 401 | int mbedtls_ccm_self_test( int verbose ) |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 402 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 403 | mbedtls_ccm_context ctx; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 404 | unsigned char out[32]; |
| 405 | size_t i; |
| 406 | int ret; |
| 407 | |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 408 | mbedtls_ccm_init( &ctx ); |
| 409 | |
| 410 | 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] | 411 | { |
| 412 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 413 | mbedtls_printf( " CCM: setup failed" ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 414 | |
| 415 | return( 1 ); |
| 416 | } |
| 417 | |
| 418 | for( i = 0; i < NB_TESTS; i++ ) |
| 419 | { |
| 420 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 421 | mbedtls_printf( " CCM-AES #%u: ", (unsigned int) i + 1 ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 422 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 423 | ret = mbedtls_ccm_encrypt_and_tag( &ctx, msg_len[i], |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 424 | iv, iv_len[i], ad, add_len[i], |
| 425 | msg, out, |
| 426 | out + msg_len[i], tag_len[i] ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 427 | |
| 428 | if( ret != 0 || |
| 429 | memcmp( out, res[i], msg_len[i] + tag_len[i] ) != 0 ) |
| 430 | { |
| 431 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 432 | mbedtls_printf( "failed\n" ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 433 | |
| 434 | return( 1 ); |
| 435 | } |
| 436 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 437 | ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len[i], |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 438 | iv, iv_len[i], ad, add_len[i], |
| 439 | res[i], out, |
| 440 | res[i] + msg_len[i], tag_len[i] ); |
| 441 | |
| 442 | if( ret != 0 || |
| 443 | memcmp( out, msg, msg_len[i] ) != 0 ) |
| 444 | { |
| 445 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 446 | mbedtls_printf( "failed\n" ); |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 447 | |
| 448 | return( 1 ); |
| 449 | } |
| 450 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 451 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 452 | mbedtls_printf( "passed\n" ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 453 | } |
| 454 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 455 | mbedtls_ccm_free( &ctx ); |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 456 | |
| 457 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 458 | mbedtls_printf( "\n" ); |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 459 | |
| 460 | return( 0 ); |
| 461 | } |
| 462 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 463 | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 464 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 465 | #endif /* MBEDTLS_CCM_C */ |