|
| 1 | +/* |
| 2 | + * ccm_alt.c |
| 3 | + * |
| 4 | + * Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 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. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +#include "mbedtls/ccm.h" |
| 22 | +#if defined(MBEDTLS_CCM_ALT) |
| 23 | +#include <string.h> |
| 24 | +#include "mbedtls/platform.h" |
| 25 | + |
| 26 | +/* Implementation that should never be optimized out by the compiler */ |
| 27 | +static void mbedtls_zeroize( void *v, size_t n ) { |
| 28 | + volatile unsigned char *p = (unsigned char*)v; |
| 29 | + while( n-- ) *p++ = 0; |
| 30 | +} |
| 31 | + |
| 32 | +#define CCM_ENCRYPT 0 |
| 33 | +#define CCM_DECRYPT 1 |
| 34 | + |
| 35 | + |
| 36 | +/* |
| 37 | + * Macros for common operations. |
| 38 | + * Results in smaller compiled code than static inline functions. |
| 39 | + */ |
| 40 | + |
| 41 | +/* |
| 42 | + * Update the CBC-MAC state in y using a block in b |
| 43 | + * (Always using b as the source helps the compiler optimise a bit better.) |
| 44 | + */ |
| 45 | +#define UPDATE_CBC_MAC \ |
| 46 | + for( i = 0; i < 16; i++ ) \ |
| 47 | + y[i] ^= b[i]; \ |
| 48 | + \ |
| 49 | + if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \ |
| 50 | + return ( ret ); |
| 51 | + |
| 52 | +/* |
| 53 | + * Encrypt or decrypt a partial block with CTR |
| 54 | + * Warning: using b for temporary storage! src and dst must not be b! |
| 55 | + * This avoids allocating one more 16 bytes buffer while allowing src == dst. |
| 56 | + */ |
| 57 | +#define CTR_CRYPT( dst, src, len ) \ |
| 58 | + if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, 16, b, &olen ) ) != 0 ) \ |
| 59 | + return ( ret ); \ |
| 60 | + \ |
| 61 | + for( i = 0; i < len; i++ ) \ |
| 62 | + dst[i] = src[i] ^ b[i]; |
| 63 | + |
| 64 | +void mbedtls_ccm_init( mbedtls_ccm_context *ctx ) |
| 65 | +{ |
| 66 | + memset( ctx, 0, sizeof( mbedtls_ccm_context ) ); |
| 67 | +} |
| 68 | + |
| 69 | +void mbedtls_ccm_free( mbedtls_ccm_context *ctx ) |
| 70 | +{ |
| 71 | + mbedtls_zeroize( ctx, sizeof( mbedtls_ccm_context ) ); |
| 72 | +} |
| 73 | + |
| 74 | +static int mbedtls_ccm_setkey_sw( mbedtls_ccm_sw_context *ctx, |
| 75 | + mbedtls_cipher_id_t cipher, |
| 76 | + const unsigned char *key, |
| 77 | + unsigned int keybits ) |
| 78 | +{ |
| 79 | + |
| 80 | + int ret = 0; |
| 81 | + const mbedtls_cipher_info_t *cipher_info = NULL; |
| 82 | + |
| 83 | + cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, MBEDTLS_MODE_ECB ); |
| 84 | + if( cipher_info == NULL ) |
| 85 | + return ( MBEDTLS_ERR_CCM_BAD_INPUT ); |
| 86 | + |
| 87 | + if( cipher_info->block_size != 16 ) |
| 88 | + return ( MBEDTLS_ERR_CCM_BAD_INPUT ); |
| 89 | + |
| 90 | + mbedtls_cipher_free( &ctx->cipher_ctx ); |
| 91 | + |
| 92 | + if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 ) |
| 93 | + return ( ret ); |
| 94 | + |
| 95 | + return ( mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits, MBEDTLS_ENCRYPT ) ); |
| 96 | +} |
| 97 | + |
| 98 | +int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx, |
| 99 | + mbedtls_cipher_id_t cipher, |
| 100 | + const unsigned char *key, |
| 101 | + unsigned int keybits ) |
| 102 | +{ |
| 103 | + if( ctx == NULL ) |
| 104 | + return ( MBEDTLS_ERR_CCM_BAD_INPUT ); |
| 105 | + |
| 106 | + if( cipher != MBEDTLS_CIPHER_ID_AES || |
| 107 | + keybits != 128 ) |
| 108 | + { |
| 109 | + ctx->is_sw = 1; |
| 110 | + return ( mbedtls_ccm_setkey_sw( &ctx->u_ctx.ccm_sw_ctx, cipher, key, keybits ) ); |
| 111 | + } |
| 112 | + |
| 113 | + ctx->is_sw = 0; |
| 114 | + |
| 115 | + memcpy( ctx->u_ctx.ccm_alt_ctx.cipher_key , key, keybits/8 ); |
| 116 | + ctx->u_ctx.ccm_alt_ctx.keySize_ID = CRYS_AES_Key128BitSize; |
| 117 | + |
| 118 | + return ( 0 ); |
| 119 | + |
| 120 | +} |
| 121 | + |
| 122 | +/* |
| 123 | + * Authenticated encryption or decryption |
| 124 | + */ |
| 125 | +static int ccm_auth_crypt_sw( mbedtls_ccm_sw_context *ctx, int mode, size_t length, |
| 126 | + const unsigned char *iv, size_t iv_len, |
| 127 | + const unsigned char *add, size_t add_len, |
| 128 | + const unsigned char *input, unsigned char *output, |
| 129 | + unsigned char *tag, size_t tag_len ) |
| 130 | +{ |
| 131 | + int ret; |
| 132 | + unsigned char i; |
| 133 | + unsigned char q; |
| 134 | + size_t len_left, olen; |
| 135 | + unsigned char b[16]; |
| 136 | + unsigned char y[16]; |
| 137 | + unsigned char ctr[16]; |
| 138 | + const unsigned char *src; |
| 139 | + unsigned char *dst; |
| 140 | + |
| 141 | + if( add_len > 0xFF00 ) |
| 142 | + return ( MBEDTLS_ERR_CCM_BAD_INPUT ); |
| 143 | + |
| 144 | + q = 16 - 1 - (unsigned char) iv_len; |
| 145 | + |
| 146 | + /* |
| 147 | + * First block B_0: |
| 148 | + * 0 .. 0 flags |
| 149 | + * 1 .. iv_len nonce (aka iv) |
| 150 | + * iv_len+1 .. 15 length |
| 151 | + * |
| 152 | + * With flags as (bits): |
| 153 | + * 7 0 |
| 154 | + * 6 add present? |
| 155 | + * 5 .. 3 (t - 2) / 2 |
| 156 | + * 2 .. 0 q - 1 |
| 157 | + */ |
| 158 | + b[0] = 0; |
| 159 | + b[0] |= ( add_len > 0 ) << 6; |
| 160 | + b[0] |= ( ( tag_len - 2 ) / 2 ) << 3; |
| 161 | + b[0] |= q - 1; |
| 162 | + |
| 163 | + memcpy( b + 1, iv, iv_len ); |
| 164 | + |
| 165 | + for( i = 0, len_left = length; i < q; i++, len_left >>= 8 ) |
| 166 | + b[15-i] = (unsigned char)( len_left & 0xFF ); |
| 167 | + |
| 168 | + if( len_left > 0 ) |
| 169 | + return ( MBEDTLS_ERR_CCM_BAD_INPUT ); |
| 170 | + |
| 171 | + |
| 172 | + /* Start CBC-MAC with first block */ |
| 173 | + memset( y, 0, 16 ); |
| 174 | + UPDATE_CBC_MAC; |
| 175 | + |
| 176 | + /* |
| 177 | + * If there is additional data, update CBC-MAC with |
| 178 | + * add_len, add, 0 (padding to a block boundary) |
| 179 | + */ |
| 180 | + if( add_len > 0 ) |
| 181 | + { |
| 182 | + size_t use_len; |
| 183 | + len_left = add_len; |
| 184 | + src = add; |
| 185 | + |
| 186 | + memset( b, 0, 16 ); |
| 187 | + b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF ); |
| 188 | + b[1] = (unsigned char)( ( add_len ) & 0xFF ); |
| 189 | + |
| 190 | + use_len = len_left < 16 - 2 ? len_left : 16 - 2; |
| 191 | + memcpy( b + 2, src, use_len ); |
| 192 | + len_left -= use_len; |
| 193 | + src += use_len; |
| 194 | + |
| 195 | + UPDATE_CBC_MAC; |
| 196 | + while( len_left > 0 ) |
| 197 | + { |
| 198 | + use_len = len_left > 16 ? 16 : len_left; |
| 199 | + |
| 200 | + memset( b, 0, 16 ); |
| 201 | + memcpy( b, src, use_len ); |
| 202 | + UPDATE_CBC_MAC; |
| 203 | + |
| 204 | + len_left -= use_len; |
| 205 | + src += use_len; |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + /* |
| 210 | + * Prepare counter block for encryption: |
| 211 | + * 0 .. 0 flags |
| 212 | + * 1 .. iv_len nonce (aka iv) |
| 213 | + * iv_len+1 .. 15 counter (initially 1) |
| 214 | + * |
| 215 | + * With flags as (bits): |
| 216 | + * 7 .. 3 0 |
| 217 | + * 2 .. 0 q - 1 |
| 218 | + */ |
| 219 | + ctr[0] = q - 1; |
| 220 | + memcpy( ctr + 1, iv, iv_len ); |
| 221 | + memset( ctr + 1 + iv_len, 0, q ); |
| 222 | + ctr[15] = 1; |
| 223 | + |
| 224 | + /* |
| 225 | + * Authenticate and {en,de}crypt the message. |
| 226 | + * |
| 227 | + * The only difference between encryption and decryption is |
| 228 | + * the respective order of authentication and {en,de}cryption. |
| 229 | + */ |
| 230 | + len_left = length; |
| 231 | + src = input; |
| 232 | + dst = output; |
| 233 | + |
| 234 | + while( len_left > 0 ) |
| 235 | + { |
| 236 | + size_t use_len = len_left > 16 ? 16 : len_left; |
| 237 | + |
| 238 | + if( mode == CCM_ENCRYPT ) |
| 239 | + { |
| 240 | + memset( b, 0, 16 ); |
| 241 | + memcpy( b, src, use_len ); |
| 242 | + UPDATE_CBC_MAC; |
| 243 | + } |
| 244 | + |
| 245 | + CTR_CRYPT( dst, src, use_len ); |
| 246 | + |
| 247 | + if( mode == CCM_DECRYPT ) |
| 248 | + { |
| 249 | + memset( b, 0, 16 ); |
| 250 | + memcpy( b, dst, use_len ); |
| 251 | + UPDATE_CBC_MAC; |
| 252 | + } |
| 253 | + |
| 254 | + dst += use_len; |
| 255 | + src += use_len; |
| 256 | + len_left -= use_len; |
| 257 | + |
| 258 | + /* |
| 259 | + * Increment counter. |
| 260 | + * No need to check for overflow thanks to the length check above. |
| 261 | + */ |
| 262 | + for( i = 0; i < q; i++ ) |
| 263 | + if( ++ctr[15-i] != 0 ) |
| 264 | + break; |
| 265 | + } |
| 266 | + |
| 267 | + /* |
| 268 | + * Authentication: reset counter and crypt/mask internal tag |
| 269 | + */ |
| 270 | + for( i = 0; i < q; i++ ) |
| 271 | + ctr[15-i] = 0; |
| 272 | + |
| 273 | + CTR_CRYPT( y, y, 16 ); |
| 274 | + memcpy( tag, y, tag_len ); |
| 275 | + |
| 276 | + return ( 0 ); |
| 277 | +} |
| 278 | + |
| 279 | +int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, |
| 280 | + const unsigned char *iv, size_t iv_len, |
| 281 | + const unsigned char *add, size_t add_len, |
| 282 | + const unsigned char *input, unsigned char *output, |
| 283 | + unsigned char *tag, size_t tag_len ) |
| 284 | + |
| 285 | +{ |
| 286 | + CRYSError_t CrysRet = CRYS_OK; |
| 287 | + /* |
| 288 | + * Check length requirements: SP800-38C A.1 |
| 289 | + * Additional requirement: a < 2^16 - 2^8 to simplify the code. |
| 290 | + * 'length' checked later (when writing it to the first block) |
| 291 | + */ |
| 292 | + if( tag_len < 4 || tag_len > 16 || tag_len % 2 != 0 ) |
| 293 | + return ( MBEDTLS_ERR_CCM_BAD_INPUT ); |
| 294 | + |
| 295 | + /* Also implies q is within bounds */ |
| 296 | + if( iv_len < 7 || iv_len > 13 ) |
| 297 | + return ( MBEDTLS_ERR_CCM_BAD_INPUT ); |
| 298 | + |
| 299 | + if( ctx->is_sw ) |
| 300 | + return ( ccm_auth_crypt_sw( &ctx->u_ctx.ccm_sw_ctx, CCM_ENCRYPT, length, iv, iv_len, |
| 301 | + add, add_len, input, output, |
| 302 | + tag, tag_len ) ); |
| 303 | + |
| 304 | + if( length > 0xFFFFFFFF || add_len > 0xFFFFFFFF ) |
| 305 | + return ( MBEDTLS_ERR_CCM_BAD_INPUT ); |
| 306 | + |
| 307 | + CrysRet = CRYS_AESCCM( SASI_AES_ENCRYPT, ctx->u_ctx.ccm_alt_ctx.cipher_key, ctx->u_ctx.ccm_alt_ctx.keySize_ID, |
| 308 | + (uint8_t*)iv, iv_len, (uint8_t*)add, add_len, (uint8_t*)input, length, output, tag_len, tag ); |
| 309 | + if( CrysRet != CRYS_OK ) |
| 310 | + return ( MBEDTLS_ERR_CCM_HW_ACCEL_FAILED ); |
| 311 | + |
| 312 | + return ( 0 ); |
| 313 | + |
| 314 | +} |
| 315 | + |
| 316 | +/* |
| 317 | + * Authenticated decryption |
| 318 | + */ |
| 319 | +static int mbedtls_ccm_auth_decrypt_sw( mbedtls_ccm_sw_context *ctx, size_t length, |
| 320 | + const unsigned char *iv, size_t iv_len, |
| 321 | + const unsigned char *add, size_t add_len, |
| 322 | + const unsigned char *input, unsigned char *output, |
| 323 | + const unsigned char *tag, size_t tag_len ) |
| 324 | +{ |
| 325 | + int ret; |
| 326 | + unsigned char check_tag[16]; |
| 327 | + unsigned char i; |
| 328 | + int diff; |
| 329 | + |
| 330 | + if( ( ret = ccm_auth_crypt_sw( ctx, CCM_DECRYPT, length, |
| 331 | + iv, iv_len, add, add_len, |
| 332 | + input, output, check_tag, tag_len ) ) != 0 ) |
| 333 | + { |
| 334 | + return ( ret ); |
| 335 | + } |
| 336 | + |
| 337 | + /* Check tag in "constant-time" */ |
| 338 | + for( diff = 0, i = 0; i < tag_len; i++ ) |
| 339 | + diff |= tag[i] ^ check_tag[i]; |
| 340 | + |
| 341 | + if( diff != 0 ) |
| 342 | + { |
| 343 | + mbedtls_zeroize( output, length ); |
| 344 | + return ( MBEDTLS_ERR_CCM_AUTH_FAILED ); |
| 345 | + } |
| 346 | + |
| 347 | + return ( 0 ); |
| 348 | +} |
| 349 | + |
| 350 | +int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length, |
| 351 | + const unsigned char *iv, size_t iv_len, |
| 352 | + const unsigned char *add, size_t add_len, |
| 353 | + const unsigned char *input, unsigned char *output, |
| 354 | + const unsigned char *tag, size_t tag_len ) |
| 355 | + |
| 356 | +{ |
| 357 | + CRYSError_t CrysRet = CRYS_OK; |
| 358 | + /* |
| 359 | + * Check length requirements: SP800-38C A.1 |
| 360 | + * Additional requirement: a < 2^16 - 2^8 to simplify the code. |
| 361 | + * 'length' checked later (when writing it to the first block) |
| 362 | + */ |
| 363 | + if( tag_len < 4 || tag_len > 16 || tag_len % 2 != 0 ) |
| 364 | + return ( MBEDTLS_ERR_CCM_BAD_INPUT ); |
| 365 | + |
| 366 | + /* Also implies q is within bounds */ |
| 367 | + if( iv_len < 7 || iv_len > 13 ) |
| 368 | + return ( MBEDTLS_ERR_CCM_BAD_INPUT ); |
| 369 | + |
| 370 | + if ( ctx->is_sw ) |
| 371 | + return ( mbedtls_ccm_auth_decrypt_sw( &ctx->u_ctx.ccm_sw_ctx, length, iv, iv_len, |
| 372 | + add, add_len, input, output, |
| 373 | + tag, tag_len ) ); |
| 374 | + |
| 375 | + if( length > 0xFFFFFFFF || add_len > 0xFFFFFFFF ) |
| 376 | + return ( MBEDTLS_ERR_CCM_BAD_INPUT ); |
| 377 | + |
| 378 | + CrysRet = CRYS_AESCCM( SASI_AES_DECRYPT, ctx->u_ctx.ccm_alt_ctx.cipher_key, ctx->u_ctx.ccm_alt_ctx.keySize_ID, |
| 379 | + (uint8_t*)iv, iv_len, (uint8_t*)add, add_len, (uint8_t*)input, length, output, tag_len, (uint8_t*)tag ); |
| 380 | + if ( CrysRet != CRYS_OK ) |
| 381 | + return ( MBEDTLS_ERR_CCM_HW_ACCEL_FAILED ); |
| 382 | + |
| 383 | + return ( 0 ); |
| 384 | + |
| 385 | +} |
| 386 | + |
| 387 | +#endif |
0 commit comments