1/* 2 * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the OpenSSL license (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10#include <openssl/aead.h> 11 12#include <assert.h> 13 14#include <openssl/cipher.h> 15#include <openssl/err.h> 16#include <openssl/mem.h> 17 18#include "../aes/internal.h" 19#include "../delocate.h" 20#include "../modes/internal.h" 21#include "../service_indicator/internal.h" 22#include "internal.h" 23 24 25struct ccm128_context { 26 block128_f block; 27 ctr128_f ctr; 28 unsigned M, L; 29}; 30 31struct ccm128_state { 32 alignas(16) uint8_t nonce[16]; 33 alignas(16) uint8_t cmac[16]; 34}; 35 36static int CRYPTO_ccm128_init(struct ccm128_context *ctx, const AES_KEY *key, 37 block128_f block, ctr128_f ctr, unsigned M, 38 unsigned L) { 39 if (M < 4 || M > 16 || (M & 1) != 0 || L < 2 || L > 8) { 40 return 0; 41 } 42 ctx->block = block; 43 ctx->ctr = ctr; 44 ctx->M = M; 45 ctx->L = L; 46 return 1; 47} 48 49static size_t CRYPTO_ccm128_max_input(const struct ccm128_context *ctx) { 50 return ctx->L >= sizeof(size_t) ? SIZE_MAX 51 : (((size_t)1) << (ctx->L * 8)) - 1; 52} 53 54static int ccm128_init_state(const struct ccm128_context *ctx, 55 struct ccm128_state *state, const AES_KEY *key, 56 const uint8_t *nonce, size_t nonce_len, 57 const uint8_t *aad, size_t aad_len, 58 size_t plaintext_len) { 59 const block128_f block = ctx->block; 60 const unsigned M = ctx->M; 61 const unsigned L = ctx->L; 62 63 // |L| determines the expected |nonce_len| and the limit for |plaintext_len|. 64 if (plaintext_len > CRYPTO_ccm128_max_input(ctx) // 65 || nonce_len != 15 - L) { 66 return 0; 67 } 68 69 // Assemble the first block for computing the MAC. 70 OPENSSL_memset(state, 0, sizeof(*state)); 71 state->nonce[0] = (uint8_t)((L - 1) | ((M - 2) / 2) << 3); 72 if (aad_len != 0) { 73 state->nonce[0] |= 0x40; // Set AAD Flag 74 } 75 OPENSSL_memcpy(&state->nonce[1], nonce, nonce_len); 76 for (unsigned i = 0; i < L; i++) { 77 state->nonce[15 - i] = (uint8_t)(plaintext_len >> (8 * i)); 78 } 79 80 (*block)(state->nonce, state->cmac, key); 81 size_t blocks = 1; 82 83 if (aad_len != 0) { 84 unsigned i; 85 // Cast to u64 to avoid the compiler complaining about invalid shifts. 86 uint64_t aad_len_u64 = aad_len; 87 if (aad_len_u64 < 0x10000 - 0x100) { 88 state->cmac[0] ^= (uint8_t)(aad_len_u64 >> 8); 89 state->cmac[1] ^= (uint8_t)aad_len_u64; 90 i = 2; 91 } else if (aad_len_u64 <= 0xffffffff) { 92 state->cmac[0] ^= 0xff; 93 state->cmac[1] ^= 0xfe; 94 state->cmac[2] ^= (uint8_t)(aad_len_u64 >> 24); 95 state->cmac[3] ^= (uint8_t)(aad_len_u64 >> 16); 96 state->cmac[4] ^= (uint8_t)(aad_len_u64 >> 8); 97 state->cmac[5] ^= (uint8_t)aad_len_u64; 98 i = 6; 99 } else { 100 state->cmac[0] ^= 0xff; 101 state->cmac[1] ^= 0xff; 102 state->cmac[2] ^= (uint8_t)(aad_len_u64 >> 56); 103 state->cmac[3] ^= (uint8_t)(aad_len_u64 >> 48); 104 state->cmac[4] ^= (uint8_t)(aad_len_u64 >> 40); 105 state->cmac[5] ^= (uint8_t)(aad_len_u64 >> 32); 106 state->cmac[6] ^= (uint8_t)(aad_len_u64 >> 24); 107 state->cmac[7] ^= (uint8_t)(aad_len_u64 >> 16); 108 state->cmac[8] ^= (uint8_t)(aad_len_u64 >> 8); 109 state->cmac[9] ^= (uint8_t)aad_len_u64; 110 i = 10; 111 } 112 113 do { 114 for (; i < 16 && aad_len != 0; i++) { 115 state->cmac[i] ^= *aad; 116 aad++; 117 aad_len--; 118 } 119 (*block)(state->cmac, state->cmac, key); 120 blocks++; 121 i = 0; 122 } while (aad_len != 0); 123 } 124 125 // Per RFC 3610, section 2.6, the total number of block cipher operations done 126 // must not exceed 2^61. There are two block cipher operations remaining per 127 // message block, plus one block at the end to encrypt the MAC. 128 size_t remaining_blocks = 2 * ((plaintext_len + 15) / 16) + 1; 129 if (plaintext_len + 15 < plaintext_len || 130 remaining_blocks + blocks < blocks || 131 (uint64_t)remaining_blocks + blocks > UINT64_C(1) << 61) { 132 return 0; 133 } 134 135 // Assemble the first block for encrypting and decrypting. The bottom |L| 136 // bytes are replaced with a counter and all bit the encoding of |L| is 137 // cleared in the first byte. 138 state->nonce[0] &= 7; 139 return 1; 140} 141 142static int ccm128_encrypt(const struct ccm128_context *ctx, 143 struct ccm128_state *state, const AES_KEY *key, 144 uint8_t *out, const uint8_t *in, size_t len) { 145 // The counter for encryption begins at one. 146 for (unsigned i = 0; i < ctx->L; i++) { 147 state->nonce[15 - i] = 0; 148 } 149 state->nonce[15] = 1; 150 151 uint8_t partial_buf[16]; 152 unsigned num = 0; 153 CRYPTO_ctr128_encrypt_ctr32(in, out, len, key, state->nonce, partial_buf, 154 &num, ctx->ctr); 155 return 1; 156} 157 158static int ccm128_compute_mac(const struct ccm128_context *ctx, 159 struct ccm128_state *state, const AES_KEY *key, 160 uint8_t *out_tag, size_t tag_len, 161 const uint8_t *in, size_t len) { 162 block128_f block = ctx->block; 163 if (tag_len != ctx->M) { 164 return 0; 165 } 166 167 // Incorporate |in| into the MAC. 168 while (len >= 16) { 169 CRYPTO_xor16(state->cmac, state->cmac, in); 170 (*block)(state->cmac, state->cmac, key); 171 in += 16; 172 len -= 16; 173 } 174 if (len > 0) { 175 for (size_t i = 0; i < len; i++) { 176 state->cmac[i] ^= in[i]; 177 } 178 (*block)(state->cmac, state->cmac, key); 179 } 180 181 // Encrypt the MAC with counter zero. 182 for (unsigned i = 0; i < ctx->L; i++) { 183 state->nonce[15 - i] = 0; 184 } 185 alignas(16) uint8_t tmp[16]; 186 (*block)(state->nonce, tmp, key); 187 CRYPTO_xor16(state->cmac, state->cmac, tmp); 188 189 OPENSSL_memcpy(out_tag, state->cmac, tag_len); 190 return 1; 191} 192 193static int CRYPTO_ccm128_encrypt(const struct ccm128_context *ctx, 194 const AES_KEY *key, uint8_t *out, 195 uint8_t *out_tag, size_t tag_len, 196 const uint8_t *nonce, size_t nonce_len, 197 const uint8_t *in, size_t len, 198 const uint8_t *aad, size_t aad_len) { 199 struct ccm128_state state; 200 return ccm128_init_state(ctx, &state, key, nonce, nonce_len, aad, aad_len, 201 len) && 202 ccm128_compute_mac(ctx, &state, key, out_tag, tag_len, in, len) && 203 ccm128_encrypt(ctx, &state, key, out, in, len); 204} 205 206static int CRYPTO_ccm128_decrypt(const struct ccm128_context *ctx, 207 const AES_KEY *key, uint8_t *out, 208 uint8_t *out_tag, size_t tag_len, 209 const uint8_t *nonce, size_t nonce_len, 210 const uint8_t *in, size_t len, 211 const uint8_t *aad, size_t aad_len) { 212 struct ccm128_state state; 213 return ccm128_init_state(ctx, &state, key, nonce, nonce_len, aad, aad_len, 214 len) && 215 ccm128_encrypt(ctx, &state, key, out, in, len) && 216 ccm128_compute_mac(ctx, &state, key, out_tag, tag_len, out, len); 217} 218 219#define EVP_AEAD_AES_CCM_MAX_TAG_LEN 16 220 221namespace { 222struct aead_aes_ccm_ctx { 223 union { 224 double align; 225 AES_KEY ks; 226 } ks; 227 struct ccm128_context ccm; 228}; 229} // namespace 230 231static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) >= 232 sizeof(struct aead_aes_ccm_ctx), 233 "AEAD state is too small"); 234static_assert(alignof(union evp_aead_ctx_st_state) >= 235 alignof(struct aead_aes_ccm_ctx), 236 "AEAD state has insufficient alignment"); 237 238static int aead_aes_ccm_init(EVP_AEAD_CTX *ctx, const uint8_t *key, 239 size_t key_len, size_t tag_len, unsigned M, 240 unsigned L) { 241 assert(M == EVP_AEAD_max_overhead(ctx->aead)); 242 assert(M == EVP_AEAD_max_tag_len(ctx->aead)); 243 assert(15 - L == EVP_AEAD_nonce_length(ctx->aead)); 244 245 if (key_len != EVP_AEAD_key_length(ctx->aead)) { 246 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH); 247 return 0; // EVP_AEAD_CTX_init should catch this. 248 } 249 250 if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) { 251 tag_len = M; 252 } 253 254 if (tag_len != M) { 255 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE); 256 return 0; 257 } 258 259 struct aead_aes_ccm_ctx *ccm_ctx = (struct aead_aes_ccm_ctx *)&ctx->state; 260 261 block128_f block; 262 ctr128_f ctr = aes_ctr_set_key(&ccm_ctx->ks.ks, NULL, &block, key, key_len); 263 ctx->tag_len = tag_len; 264 if (!CRYPTO_ccm128_init(&ccm_ctx->ccm, &ccm_ctx->ks.ks, block, ctr, M, L)) { 265 OPENSSL_PUT_ERROR(CIPHER, ERR_R_INTERNAL_ERROR); 266 return 0; 267 } 268 269 return 1; 270} 271 272static void aead_aes_ccm_cleanup(EVP_AEAD_CTX *ctx) {} 273 274static int aead_aes_ccm_seal_scatter( 275 const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag, 276 size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce, 277 size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in, 278 size_t extra_in_len, const uint8_t *ad, size_t ad_len) { 279 const struct aead_aes_ccm_ctx *ccm_ctx = 280 (struct aead_aes_ccm_ctx *)&ctx->state; 281 282 if (in_len > CRYPTO_ccm128_max_input(&ccm_ctx->ccm)) { 283 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); 284 return 0; 285 } 286 287 if (max_out_tag_len < ctx->tag_len) { 288 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); 289 return 0; 290 } 291 292 if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) { 293 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE); 294 return 0; 295 } 296 297 if (!CRYPTO_ccm128_encrypt(&ccm_ctx->ccm, &ccm_ctx->ks.ks, out, out_tag, 298 ctx->tag_len, nonce, nonce_len, in, in_len, ad, 299 ad_len)) { 300 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); 301 return 0; 302 } 303 304 *out_tag_len = ctx->tag_len; 305 AEAD_CCM_verify_service_indicator(ctx); 306 return 1; 307} 308 309static int aead_aes_ccm_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out, 310 const uint8_t *nonce, size_t nonce_len, 311 const uint8_t *in, size_t in_len, 312 const uint8_t *in_tag, size_t in_tag_len, 313 const uint8_t *ad, size_t ad_len) { 314 const struct aead_aes_ccm_ctx *ccm_ctx = 315 (struct aead_aes_ccm_ctx *)&ctx->state; 316 317 if (in_len > CRYPTO_ccm128_max_input(&ccm_ctx->ccm)) { 318 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); 319 return 0; 320 } 321 322 if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) { 323 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE); 324 return 0; 325 } 326 327 if (in_tag_len != ctx->tag_len) { 328 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); 329 return 0; 330 } 331 332 uint8_t tag[EVP_AEAD_AES_CCM_MAX_TAG_LEN]; 333 assert(ctx->tag_len <= EVP_AEAD_AES_CCM_MAX_TAG_LEN); 334 if (!CRYPTO_ccm128_decrypt(&ccm_ctx->ccm, &ccm_ctx->ks.ks, out, tag, 335 ctx->tag_len, nonce, nonce_len, in, in_len, ad, 336 ad_len)) { 337 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); 338 return 0; 339 } 340 341 if (CRYPTO_memcmp(tag, in_tag, ctx->tag_len) != 0) { 342 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); 343 return 0; 344 } 345 346 AEAD_CCM_verify_service_indicator(ctx); 347 return 1; 348} 349 350static int aead_aes_ccm_bluetooth_init(EVP_AEAD_CTX *ctx, const uint8_t *key, 351 size_t key_len, size_t tag_len) { 352 return aead_aes_ccm_init(ctx, key, key_len, tag_len, 4, 2); 353} 354 355DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_ccm_bluetooth) { 356 memset(out, 0, sizeof(EVP_AEAD)); 357 358 out->key_len = 16; 359 out->nonce_len = 13; 360 out->overhead = 4; 361 out->max_tag_len = 4; 362 363 out->init = aead_aes_ccm_bluetooth_init; 364 out->cleanup = aead_aes_ccm_cleanup; 365 out->seal_scatter = aead_aes_ccm_seal_scatter; 366 out->open_gather = aead_aes_ccm_open_gather; 367} 368 369static int aead_aes_ccm_bluetooth_8_init(EVP_AEAD_CTX *ctx, const uint8_t *key, 370 size_t key_len, size_t tag_len) { 371 return aead_aes_ccm_init(ctx, key, key_len, tag_len, 8, 2); 372} 373 374DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_ccm_bluetooth_8) { 375 memset(out, 0, sizeof(EVP_AEAD)); 376 377 out->key_len = 16; 378 out->nonce_len = 13; 379 out->overhead = 8; 380 out->max_tag_len = 8; 381 382 out->init = aead_aes_ccm_bluetooth_8_init; 383 out->cleanup = aead_aes_ccm_cleanup; 384 out->seal_scatter = aead_aes_ccm_seal_scatter; 385 out->open_gather = aead_aes_ccm_open_gather; 386} 387 388static int aead_aes_ccm_matter_init(EVP_AEAD_CTX *ctx, const uint8_t *key, 389 size_t key_len, size_t tag_len) { 390 return aead_aes_ccm_init(ctx, key, key_len, tag_len, 16, 2); 391} 392 393DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_ccm_matter) { 394 memset(out, 0, sizeof(EVP_AEAD)); 395 396 out->key_len = 16; 397 out->nonce_len = 13; 398 out->overhead = 16; 399 out->max_tag_len = 16; 400 401 out->init = aead_aes_ccm_matter_init; 402 out->cleanup = aead_aes_ccm_cleanup; 403 out->seal_scatter = aead_aes_ccm_seal_scatter; 404 out->open_gather = aead_aes_ccm_open_gather; 405} 406