Coverage Report

Created: 2024-11-21 07:03

/src/boringssl/crypto/fipsmodule/cipher/e_aesccm.c.inc
Line
Count
Source (jump to first uncovered line)
1
/* ====================================================================
2
 * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or without
5
 * modification, are permitted provided that the following conditions
6
 * are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in
13
 *    the documentation and/or other materials provided with the
14
 *    distribution.
15
 *
16
 * 3. All advertising materials mentioning features or use of this
17
 *    software must display the following acknowledgment:
18
 *    "This product includes software developed by the OpenSSL Project
19
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20
 *
21
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22
 *    endorse or promote products derived from this software without
23
 *    prior written permission. For written permission, please contact
24
 *    openssl-core@openssl.org.
25
 *
26
 * 5. Products derived from this software may not be called "OpenSSL"
27
 *    nor may "OpenSSL" appear in their names without prior written
28
 *    permission of the OpenSSL Project.
29
 *
30
 * 6. Redistributions of any form whatsoever must retain the following
31
 *    acknowledgment:
32
 *    "This product includes software developed by the OpenSSL Project
33
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34
 *
35
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46
 * OF THE POSSIBILITY OF SUCH DAMAGE.
47
 * ==================================================================== */
48
49
#include <openssl/aead.h>
50
51
#include <assert.h>
52
53
#include <openssl/cipher.h>
54
#include <openssl/err.h>
55
#include <openssl/mem.h>
56
57
#include "../delocate.h"
58
#include "../modes/internal.h"
59
#include "../service_indicator/internal.h"
60
#include "internal.h"
61
62
63
struct ccm128_context {
64
  block128_f block;
65
  ctr128_f ctr;
66
  unsigned M, L;
67
};
68
69
struct ccm128_state {
70
  alignas(16) uint8_t nonce[16];
71
  alignas(16) uint8_t cmac[16];
72
};
73
74
static int CRYPTO_ccm128_init(struct ccm128_context *ctx, const AES_KEY *key,
75
                              block128_f block, ctr128_f ctr, unsigned M,
76
82
                              unsigned L) {
77
82
  if (M < 4 || M > 16 || (M & 1) != 0 || L < 2 || L > 8) {
78
0
    return 0;
79
0
  }
80
82
  ctx->block = block;
81
82
  ctx->ctr = ctr;
82
82
  ctx->M = M;
83
82
  ctx->L = L;
84
82
  return 1;
85
82
}
86
87
164
static size_t CRYPTO_ccm128_max_input(const struct ccm128_context *ctx) {
88
164
  return ctx->L >= sizeof(size_t) ? SIZE_MAX
89
164
                                  : (((size_t)1) << (ctx->L * 8)) - 1;
90
164
}
91
92
static int ccm128_init_state(const struct ccm128_context *ctx,
93
                             struct ccm128_state *state, const AES_KEY *key,
94
                             const uint8_t *nonce, size_t nonce_len,
95
                             const uint8_t *aad, size_t aad_len,
96
82
                             size_t plaintext_len) {
97
82
  const block128_f block = ctx->block;
98
82
  const unsigned M = ctx->M;
99
82
  const unsigned L = ctx->L;
100
101
  // |L| determines the expected |nonce_len| and the limit for |plaintext_len|.
102
82
  if (plaintext_len > CRYPTO_ccm128_max_input(ctx) ||
103
82
      nonce_len != 15 - L) {
104
0
    return 0;
105
0
  }
106
107
  // Assemble the first block for computing the MAC.
108
82
  OPENSSL_memset(state, 0, sizeof(*state));
109
82
  state->nonce[0] = (uint8_t)((L - 1) | ((M - 2) / 2) << 3);
110
82
  if (aad_len != 0) {
111
0
    state->nonce[0] |= 0x40;  // Set AAD Flag
112
0
  }
113
82
  OPENSSL_memcpy(&state->nonce[1], nonce, nonce_len);
114
246
  for (unsigned i = 0; i < L; i++) {
115
164
    state->nonce[15 - i] = (uint8_t)(plaintext_len >> (8 * i));
116
164
  }
117
118
82
  (*block)(state->nonce, state->cmac, key);
119
82
  size_t blocks = 1;
120
121
82
  if (aad_len != 0) {
122
0
    unsigned i;
123
    // Cast to u64 to avoid the compiler complaining about invalid shifts.
124
0
    uint64_t aad_len_u64 = aad_len;
125
0
    if (aad_len_u64 < 0x10000 - 0x100) {
126
0
      state->cmac[0] ^= (uint8_t)(aad_len_u64 >> 8);
127
0
      state->cmac[1] ^= (uint8_t)aad_len_u64;
128
0
      i = 2;
129
0
    } else if (aad_len_u64 <= 0xffffffff) {
130
0
      state->cmac[0] ^= 0xff;
131
0
      state->cmac[1] ^= 0xfe;
132
0
      state->cmac[2] ^= (uint8_t)(aad_len_u64 >> 24);
133
0
      state->cmac[3] ^= (uint8_t)(aad_len_u64 >> 16);
134
0
      state->cmac[4] ^= (uint8_t)(aad_len_u64 >> 8);
135
0
      state->cmac[5] ^= (uint8_t)aad_len_u64;
136
0
      i = 6;
137
0
    } else {
138
0
      state->cmac[0] ^= 0xff;
139
0
      state->cmac[1] ^= 0xff;
140
0
      state->cmac[2] ^= (uint8_t)(aad_len_u64 >> 56);
141
0
      state->cmac[3] ^= (uint8_t)(aad_len_u64 >> 48);
142
0
      state->cmac[4] ^= (uint8_t)(aad_len_u64 >> 40);
143
0
      state->cmac[5] ^= (uint8_t)(aad_len_u64 >> 32);
144
0
      state->cmac[6] ^= (uint8_t)(aad_len_u64 >> 24);
145
0
      state->cmac[7] ^= (uint8_t)(aad_len_u64 >> 16);
146
0
      state->cmac[8] ^= (uint8_t)(aad_len_u64 >> 8);
147
0
      state->cmac[9] ^= (uint8_t)aad_len_u64;
148
0
      i = 10;
149
0
    }
150
151
0
    do {
152
0
      for (; i < 16 && aad_len != 0; i++) {
153
0
        state->cmac[i] ^= *aad;
154
0
        aad++;
155
0
        aad_len--;
156
0
      }
157
0
      (*block)(state->cmac, state->cmac, key);
158
0
      blocks++;
159
0
      i = 0;
160
0
    } while (aad_len != 0);
161
0
  }
162
163
  // Per RFC 3610, section 2.6, the total number of block cipher operations done
164
  // must not exceed 2^61. There are two block cipher operations remaining per
165
  // message block, plus one block at the end to encrypt the MAC.
166
82
  size_t remaining_blocks = 2 * ((plaintext_len + 15) / 16) + 1;
167
82
  if (plaintext_len + 15 < plaintext_len ||
168
82
      remaining_blocks + blocks < blocks ||
169
82
      (uint64_t) remaining_blocks + blocks > UINT64_C(1) << 61) {
170
0
    return 0;
171
0
  }
172
173
  // Assemble the first block for encrypting and decrypting. The bottom |L|
174
  // bytes are replaced with a counter and all bit the encoding of |L| is
175
  // cleared in the first byte.
176
82
  state->nonce[0] &= 7;
177
82
  return 1;
178
82
}
179
180
static int ccm128_encrypt(const struct ccm128_context *ctx,
181
                          struct ccm128_state *state, const AES_KEY *key,
182
82
                          uint8_t *out, const uint8_t *in, size_t len) {
183
  // The counter for encryption begins at one.
184
246
  for (unsigned i = 0; i < ctx->L; i++) {
185
164
    state->nonce[15 - i] = 0;
186
164
  }
187
82
  state->nonce[15] = 1;
188
189
82
  uint8_t partial_buf[16];
190
82
  unsigned num = 0;
191
82
  if (ctx->ctr != NULL) {
192
82
    CRYPTO_ctr128_encrypt_ctr32(in, out, len, key, state->nonce, partial_buf,
193
82
                                &num, ctx->ctr);
194
82
  } else {
195
0
    CRYPTO_ctr128_encrypt(in, out, len, key, state->nonce, partial_buf, &num,
196
0
                          ctx->block);
197
0
  }
198
82
  return 1;
199
82
}
200
201
static int ccm128_compute_mac(const struct ccm128_context *ctx,
202
                              struct ccm128_state *state, const AES_KEY *key,
203
                              uint8_t *out_tag, size_t tag_len,
204
82
                              const uint8_t *in, size_t len) {
205
82
  block128_f block = ctx->block;
206
82
  if (tag_len != ctx->M) {
207
0
    return 0;
208
0
  }
209
210
  // Incorporate |in| into the MAC.
211
164
  while (len >= 16) {
212
82
    CRYPTO_xor16(state->cmac, state->cmac, in);
213
82
    (*block)(state->cmac, state->cmac, key);
214
82
    in += 16;
215
82
    len -= 16;
216
82
  }
217
82
  if (len > 0) {
218
0
    for (size_t i = 0; i < len; i++) {
219
0
      state->cmac[i] ^= in[i];
220
0
    }
221
0
    (*block)(state->cmac, state->cmac, key);
222
0
  }
223
224
  // Encrypt the MAC with counter zero.
225
246
  for (unsigned i = 0; i < ctx->L; i++) {
226
164
    state->nonce[15 - i] = 0;
227
164
  }
228
82
  alignas(16) uint8_t tmp[16];
229
82
  (*block)(state->nonce, tmp, key);
230
82
  CRYPTO_xor16(state->cmac, state->cmac, tmp);
231
232
82
  OPENSSL_memcpy(out_tag, state->cmac, tag_len);
233
82
  return 1;
234
82
}
235
236
static int CRYPTO_ccm128_encrypt(const struct ccm128_context *ctx,
237
                                 const AES_KEY *key, uint8_t *out,
238
                                 uint8_t *out_tag, size_t tag_len,
239
                                 const uint8_t *nonce, size_t nonce_len,
240
                                 const uint8_t *in, size_t len,
241
41
                                 const uint8_t *aad, size_t aad_len) {
242
41
  struct ccm128_state state;
243
41
  return ccm128_init_state(ctx, &state, key, nonce, nonce_len, aad, aad_len,
244
41
                           len) &&
245
41
         ccm128_compute_mac(ctx, &state, key, out_tag, tag_len, in, len) &&
246
41
         ccm128_encrypt(ctx, &state, key, out, in, len);
247
41
}
248
249
static int CRYPTO_ccm128_decrypt(const struct ccm128_context *ctx,
250
                                 const AES_KEY *key, uint8_t *out,
251
                                 uint8_t *out_tag, size_t tag_len,
252
                                 const uint8_t *nonce, size_t nonce_len,
253
                                 const uint8_t *in, size_t len,
254
41
                                 const uint8_t *aad, size_t aad_len) {
255
41
  struct ccm128_state state;
256
41
  return ccm128_init_state(ctx, &state, key, nonce, nonce_len, aad, aad_len,
257
41
                           len) &&
258
41
         ccm128_encrypt(ctx, &state, key, out, in, len) &&
259
41
         ccm128_compute_mac(ctx, &state, key, out_tag, tag_len, out, len);
260
41
}
261
262
#define EVP_AEAD_AES_CCM_MAX_TAG_LEN 16
263
264
struct aead_aes_ccm_ctx {
265
  union {
266
    double align;
267
    AES_KEY ks;
268
  } ks;
269
  struct ccm128_context ccm;
270
};
271
272
static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
273
                  sizeof(struct aead_aes_ccm_ctx),
274
              "AEAD state is too small");
275
static_assert(alignof(union evp_aead_ctx_st_state) >=
276
                  alignof(struct aead_aes_ccm_ctx),
277
              "AEAD state has insufficient alignment");
278
279
static int aead_aes_ccm_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
280
                             size_t key_len, size_t tag_len, unsigned M,
281
102
                             unsigned L) {
282
102
  assert(M == EVP_AEAD_max_overhead(ctx->aead));
283
102
  assert(M == EVP_AEAD_max_tag_len(ctx->aead));
284
102
  assert(15 - L == EVP_AEAD_nonce_length(ctx->aead));
285
286
102
  if (key_len != EVP_AEAD_key_length(ctx->aead)) {
287
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
288
0
    return 0;  // EVP_AEAD_CTX_init should catch this.
289
0
  }
290
291
102
  if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) {
292
0
    tag_len = M;
293
0
  }
294
295
102
  if (tag_len != M) {
296
20
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE);
297
20
    return 0;
298
20
  }
299
300
82
  struct aead_aes_ccm_ctx *ccm_ctx = (struct aead_aes_ccm_ctx *)&ctx->state;
301
302
82
  block128_f block;
303
82
  ctr128_f ctr = aes_ctr_set_key(&ccm_ctx->ks.ks, NULL, &block, key, key_len);
304
82
  ctx->tag_len = tag_len;
305
82
  if (!CRYPTO_ccm128_init(&ccm_ctx->ccm, &ccm_ctx->ks.ks, block, ctr, M, L)) {
306
0
    OPENSSL_PUT_ERROR(CIPHER, ERR_R_INTERNAL_ERROR);
307
0
    return 0;
308
0
  }
309
310
82
  return 1;
311
82
}
312
313
82
static void aead_aes_ccm_cleanup(EVP_AEAD_CTX *ctx) {}
314
315
static int aead_aes_ccm_seal_scatter(
316
    const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
317
    size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
318
    size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
319
41
    size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
320
41
  const struct aead_aes_ccm_ctx *ccm_ctx =
321
41
      (struct aead_aes_ccm_ctx *)&ctx->state;
322
323
41
  if (in_len > CRYPTO_ccm128_max_input(&ccm_ctx->ccm)) {
324
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
325
0
    return 0;
326
0
  }
327
328
41
  if (max_out_tag_len < ctx->tag_len) {
329
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
330
0
    return 0;
331
0
  }
332
333
41
  if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
334
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
335
0
    return 0;
336
0
  }
337
338
41
  if (!CRYPTO_ccm128_encrypt(&ccm_ctx->ccm, &ccm_ctx->ks.ks, out, out_tag,
339
41
                             ctx->tag_len, nonce, nonce_len, in, in_len, ad,
340
41
                             ad_len)) {
341
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
342
0
    return 0;
343
0
  }
344
345
41
  *out_tag_len = ctx->tag_len;
346
41
  AEAD_CCM_verify_service_indicator(ctx);
347
41
  return 1;
348
41
}
349
350
static int aead_aes_ccm_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out,
351
                                    const uint8_t *nonce, size_t nonce_len,
352
                                    const uint8_t *in, size_t in_len,
353
                                    const uint8_t *in_tag, size_t in_tag_len,
354
41
                                    const uint8_t *ad, size_t ad_len) {
355
41
  const struct aead_aes_ccm_ctx *ccm_ctx =
356
41
      (struct aead_aes_ccm_ctx *)&ctx->state;
357
358
41
  if (in_len > CRYPTO_ccm128_max_input(&ccm_ctx->ccm)) {
359
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
360
0
    return 0;
361
0
  }
362
363
41
  if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
364
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
365
0
    return 0;
366
0
  }
367
368
41
  if (in_tag_len != ctx->tag_len) {
369
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
370
0
    return 0;
371
0
  }
372
373
41
  uint8_t tag[EVP_AEAD_AES_CCM_MAX_TAG_LEN];
374
41
  assert(ctx->tag_len <= EVP_AEAD_AES_CCM_MAX_TAG_LEN);
375
41
  if (!CRYPTO_ccm128_decrypt(&ccm_ctx->ccm, &ccm_ctx->ks.ks, out, tag,
376
41
                             ctx->tag_len, nonce, nonce_len, in, in_len, ad,
377
41
                             ad_len)) {
378
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
379
0
    return 0;
380
0
  }
381
382
41
  if (CRYPTO_memcmp(tag, in_tag, ctx->tag_len) != 0) {
383
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
384
0
    return 0;
385
0
  }
386
387
41
  AEAD_CCM_verify_service_indicator(ctx);
388
41
  return 1;
389
41
}
390
391
static int aead_aes_ccm_bluetooth_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
392
102
                                       size_t key_len, size_t tag_len) {
393
102
  return aead_aes_ccm_init(ctx, key, key_len, tag_len, 4, 2);
394
102
}
395
396
2
DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_ccm_bluetooth) {
397
2
  memset(out, 0, sizeof(EVP_AEAD));
398
399
2
  out->key_len = 16;
400
2
  out->nonce_len = 13;
401
2
  out->overhead = 4;
402
2
  out->max_tag_len = 4;
403
404
2
  out->init = aead_aes_ccm_bluetooth_init;
405
2
  out->cleanup = aead_aes_ccm_cleanup;
406
2
  out->seal_scatter = aead_aes_ccm_seal_scatter;
407
2
  out->open_gather = aead_aes_ccm_open_gather;
408
2
}
409
410
static int aead_aes_ccm_bluetooth_8_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
411
0
                                         size_t key_len, size_t tag_len) {
412
0
  return aead_aes_ccm_init(ctx, key, key_len, tag_len, 8, 2);
413
0
}
414
415
2
DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_ccm_bluetooth_8) {
416
2
  memset(out, 0, sizeof(EVP_AEAD));
417
418
2
  out->key_len = 16;
419
2
  out->nonce_len = 13;
420
2
  out->overhead = 8;
421
2
  out->max_tag_len = 8;
422
423
2
  out->init = aead_aes_ccm_bluetooth_8_init;
424
2
  out->cleanup = aead_aes_ccm_cleanup;
425
2
  out->seal_scatter = aead_aes_ccm_seal_scatter;
426
2
  out->open_gather = aead_aes_ccm_open_gather;
427
2
}
428
429
static int aead_aes_ccm_matter_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
430
0
                                    size_t key_len, size_t tag_len) {
431
0
  return aead_aes_ccm_init(ctx, key, key_len, tag_len, 16, 2);
432
0
}
433
434
0
DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_ccm_matter) {
435
0
  memset(out, 0, sizeof(EVP_AEAD));
436
437
0
  out->key_len = 16;
438
0
  out->nonce_len = 13;
439
0
  out->overhead = 16;
440
0
  out->max_tag_len = 16;
441
442
0
  out->init = aead_aes_ccm_matter_init;
443
0
  out->cleanup = aead_aes_ccm_cleanup;
444
0
  out->seal_scatter = aead_aes_ccm_seal_scatter;
445
0
  out->open_gather = aead_aes_ccm_open_gather;
446
0
}