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