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 * [email protected].
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
CRYPTO_ccm128_init(struct ccm128_context * ctx,const AES_KEY * key,block128_f block,ctr128_f ctr,unsigned M,unsigned L)74 static int CRYPTO_ccm128_init(struct ccm128_context *ctx, const AES_KEY *key,
75 block128_f block, ctr128_f ctr, unsigned M,
76 unsigned L) {
77 if (M < 4 || M > 16 || (M & 1) != 0 || L < 2 || L > 8) {
78 return 0;
79 }
80 ctx->block = block;
81 ctx->ctr = ctr;
82 ctx->M = M;
83 ctx->L = L;
84 return 1;
85 }
86
CRYPTO_ccm128_max_input(const struct ccm128_context * ctx)87 static size_t CRYPTO_ccm128_max_input(const struct ccm128_context *ctx) {
88 return ctx->L >= sizeof(size_t) ? SIZE_MAX
89 : (((size_t)1) << (ctx->L * 8)) - 1;
90 }
91
ccm128_init_state(const struct ccm128_context * ctx,struct ccm128_state * state,const AES_KEY * key,const uint8_t * nonce,size_t nonce_len,const uint8_t * aad,size_t aad_len,size_t plaintext_len)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 size_t plaintext_len) {
97 const block128_f block = ctx->block;
98 const unsigned M = ctx->M;
99 const unsigned L = ctx->L;
100
101 // |L| determines the expected |nonce_len| and the limit for |plaintext_len|.
102 if (plaintext_len > CRYPTO_ccm128_max_input(ctx) ||
103 nonce_len != 15 - L) {
104 return 0;
105 }
106
107 // Assemble the first block for computing the MAC.
108 OPENSSL_memset(state, 0, sizeof(*state));
109 state->nonce[0] = (uint8_t)((L - 1) | ((M - 2) / 2) << 3);
110 if (aad_len != 0) {
111 state->nonce[0] |= 0x40; // Set AAD Flag
112 }
113 OPENSSL_memcpy(&state->nonce[1], nonce, nonce_len);
114 for (unsigned i = 0; i < L; i++) {
115 state->nonce[15 - i] = (uint8_t)(plaintext_len >> (8 * i));
116 }
117
118 (*block)(state->nonce, state->cmac, key);
119 size_t blocks = 1;
120
121 if (aad_len != 0) {
122 unsigned i;
123 // Cast to u64 to avoid the compiler complaining about invalid shifts.
124 uint64_t aad_len_u64 = aad_len;
125 if (aad_len_u64 < 0x10000 - 0x100) {
126 state->cmac[0] ^= (uint8_t)(aad_len_u64 >> 8);
127 state->cmac[1] ^= (uint8_t)aad_len_u64;
128 i = 2;
129 } else if (aad_len_u64 <= 0xffffffff) {
130 state->cmac[0] ^= 0xff;
131 state->cmac[1] ^= 0xfe;
132 state->cmac[2] ^= (uint8_t)(aad_len_u64 >> 24);
133 state->cmac[3] ^= (uint8_t)(aad_len_u64 >> 16);
134 state->cmac[4] ^= (uint8_t)(aad_len_u64 >> 8);
135 state->cmac[5] ^= (uint8_t)aad_len_u64;
136 i = 6;
137 } else {
138 state->cmac[0] ^= 0xff;
139 state->cmac[1] ^= 0xff;
140 state->cmac[2] ^= (uint8_t)(aad_len_u64 >> 56);
141 state->cmac[3] ^= (uint8_t)(aad_len_u64 >> 48);
142 state->cmac[4] ^= (uint8_t)(aad_len_u64 >> 40);
143 state->cmac[5] ^= (uint8_t)(aad_len_u64 >> 32);
144 state->cmac[6] ^= (uint8_t)(aad_len_u64 >> 24);
145 state->cmac[7] ^= (uint8_t)(aad_len_u64 >> 16);
146 state->cmac[8] ^= (uint8_t)(aad_len_u64 >> 8);
147 state->cmac[9] ^= (uint8_t)aad_len_u64;
148 i = 10;
149 }
150
151 do {
152 for (; i < 16 && aad_len != 0; i++) {
153 state->cmac[i] ^= *aad;
154 aad++;
155 aad_len--;
156 }
157 (*block)(state->cmac, state->cmac, key);
158 blocks++;
159 i = 0;
160 } while (aad_len != 0);
161 }
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 size_t remaining_blocks = 2 * ((plaintext_len + 15) / 16) + 1;
167 if (plaintext_len + 15 < plaintext_len ||
168 remaining_blocks + blocks < blocks ||
169 (uint64_t) remaining_blocks + blocks > UINT64_C(1) << 61) {
170 return 0;
171 }
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 state->nonce[0] &= 7;
177 return 1;
178 }
179
ccm128_encrypt(const struct ccm128_context * ctx,struct ccm128_state * state,const AES_KEY * key,uint8_t * out,const uint8_t * in,size_t len)180 static int ccm128_encrypt(const struct ccm128_context *ctx,
181 struct ccm128_state *state, const AES_KEY *key,
182 uint8_t *out, const uint8_t *in, size_t len) {
183 // The counter for encryption begins at one.
184 for (unsigned i = 0; i < ctx->L; i++) {
185 state->nonce[15 - i] = 0;
186 }
187 state->nonce[15] = 1;
188
189 uint8_t partial_buf[16];
190 unsigned num = 0;
191 if (ctx->ctr != NULL) {
192 CRYPTO_ctr128_encrypt_ctr32(in, out, len, key, state->nonce, partial_buf,
193 &num, ctx->ctr);
194 } else {
195 CRYPTO_ctr128_encrypt(in, out, len, key, state->nonce, partial_buf, &num,
196 ctx->block);
197 }
198 return 1;
199 }
200
ccm128_compute_mac(const struct ccm128_context * ctx,struct ccm128_state * state,const AES_KEY * key,uint8_t * out_tag,size_t tag_len,const uint8_t * in,size_t len)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 const uint8_t *in, size_t len) {
205 block128_f block = ctx->block;
206 if (tag_len != ctx->M) {
207 return 0;
208 }
209
210 // Incorporate |in| into the MAC.
211 while (len >= 16) {
212 CRYPTO_xor16(state->cmac, state->cmac, in);
213 (*block)(state->cmac, state->cmac, key);
214 in += 16;
215 len -= 16;
216 }
217 if (len > 0) {
218 for (size_t i = 0; i < len; i++) {
219 state->cmac[i] ^= in[i];
220 }
221 (*block)(state->cmac, state->cmac, key);
222 }
223
224 // Encrypt the MAC with counter zero.
225 for (unsigned i = 0; i < ctx->L; i++) {
226 state->nonce[15 - i] = 0;
227 }
228 alignas(16) uint8_t tmp[16];
229 (*block)(state->nonce, tmp, key);
230 CRYPTO_xor16(state->cmac, state->cmac, tmp);
231
232 OPENSSL_memcpy(out_tag, state->cmac, tag_len);
233 return 1;
234 }
235
CRYPTO_ccm128_encrypt(const struct ccm128_context * ctx,const AES_KEY * key,uint8_t * out,uint8_t * out_tag,size_t tag_len,const uint8_t * nonce,size_t nonce_len,const uint8_t * in,size_t len,const uint8_t * aad,size_t aad_len)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 const uint8_t *aad, size_t aad_len) {
242 struct ccm128_state state;
243 return ccm128_init_state(ctx, &state, key, nonce, nonce_len, aad, aad_len,
244 len) &&
245 ccm128_compute_mac(ctx, &state, key, out_tag, tag_len, in, len) &&
246 ccm128_encrypt(ctx, &state, key, out, in, len);
247 }
248
CRYPTO_ccm128_decrypt(const struct ccm128_context * ctx,const AES_KEY * key,uint8_t * out,uint8_t * out_tag,size_t tag_len,const uint8_t * nonce,size_t nonce_len,const uint8_t * in,size_t len,const uint8_t * aad,size_t aad_len)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 const uint8_t *aad, size_t aad_len) {
255 struct ccm128_state state;
256 return ccm128_init_state(ctx, &state, key, nonce, nonce_len, aad, aad_len,
257 len) &&
258 ccm128_encrypt(ctx, &state, key, out, in, len) &&
259 ccm128_compute_mac(ctx, &state, key, out_tag, tag_len, out, len);
260 }
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
aead_aes_ccm_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len,unsigned M,unsigned L)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 unsigned L) {
282 assert(M == EVP_AEAD_max_overhead(ctx->aead));
283 assert(M == EVP_AEAD_max_tag_len(ctx->aead));
284 assert(15 - L == EVP_AEAD_nonce_length(ctx->aead));
285
286 if (key_len != EVP_AEAD_key_length(ctx->aead)) {
287 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
288 return 0; // EVP_AEAD_CTX_init should catch this.
289 }
290
291 if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) {
292 tag_len = M;
293 }
294
295 if (tag_len != M) {
296 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE);
297 return 0;
298 }
299
300 struct aead_aes_ccm_ctx *ccm_ctx = (struct aead_aes_ccm_ctx *)&ctx->state;
301
302 block128_f block;
303 ctr128_f ctr = aes_ctr_set_key(&ccm_ctx->ks.ks, NULL, &block, key, key_len);
304 ctx->tag_len = tag_len;
305 if (!CRYPTO_ccm128_init(&ccm_ctx->ccm, &ccm_ctx->ks.ks, block, ctr, M, L)) {
306 OPENSSL_PUT_ERROR(CIPHER, ERR_R_INTERNAL_ERROR);
307 return 0;
308 }
309
310 return 1;
311 }
312
aead_aes_ccm_cleanup(EVP_AEAD_CTX * ctx)313 static void aead_aes_ccm_cleanup(EVP_AEAD_CTX *ctx) {}
314
aead_aes_ccm_seal_scatter(const EVP_AEAD_CTX * ctx,uint8_t * out,uint8_t * out_tag,size_t * out_tag_len,size_t max_out_tag_len,const uint8_t * nonce,size_t nonce_len,const uint8_t * in,size_t in_len,const uint8_t * extra_in,size_t extra_in_len,const uint8_t * ad,size_t ad_len)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 size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
320 const struct aead_aes_ccm_ctx *ccm_ctx =
321 (struct aead_aes_ccm_ctx *)&ctx->state;
322
323 if (in_len > CRYPTO_ccm128_max_input(&ccm_ctx->ccm)) {
324 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
325 return 0;
326 }
327
328 if (max_out_tag_len < ctx->tag_len) {
329 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
330 return 0;
331 }
332
333 if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
334 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
335 return 0;
336 }
337
338 if (!CRYPTO_ccm128_encrypt(&ccm_ctx->ccm, &ccm_ctx->ks.ks, out, out_tag,
339 ctx->tag_len, nonce, nonce_len, in, in_len, ad,
340 ad_len)) {
341 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
342 return 0;
343 }
344
345 *out_tag_len = ctx->tag_len;
346 AEAD_CCM_verify_service_indicator(ctx);
347 return 1;
348 }
349
aead_aes_ccm_open_gather(const EVP_AEAD_CTX * ctx,uint8_t * out,const uint8_t * nonce,size_t nonce_len,const uint8_t * in,size_t in_len,const uint8_t * in_tag,size_t in_tag_len,const uint8_t * ad,size_t ad_len)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 const uint8_t *ad, size_t ad_len) {
355 const struct aead_aes_ccm_ctx *ccm_ctx =
356 (struct aead_aes_ccm_ctx *)&ctx->state;
357
358 if (in_len > CRYPTO_ccm128_max_input(&ccm_ctx->ccm)) {
359 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
360 return 0;
361 }
362
363 if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
364 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
365 return 0;
366 }
367
368 if (in_tag_len != ctx->tag_len) {
369 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
370 return 0;
371 }
372
373 uint8_t tag[EVP_AEAD_AES_CCM_MAX_TAG_LEN];
374 assert(ctx->tag_len <= EVP_AEAD_AES_CCM_MAX_TAG_LEN);
375 if (!CRYPTO_ccm128_decrypt(&ccm_ctx->ccm, &ccm_ctx->ks.ks, out, tag,
376 ctx->tag_len, nonce, nonce_len, in, in_len, ad,
377 ad_len)) {
378 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
379 return 0;
380 }
381
382 if (CRYPTO_memcmp(tag, in_tag, ctx->tag_len) != 0) {
383 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
384 return 0;
385 }
386
387 AEAD_CCM_verify_service_indicator(ctx);
388 return 1;
389 }
390
aead_aes_ccm_bluetooth_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len)391 static int aead_aes_ccm_bluetooth_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
392 size_t key_len, size_t tag_len) {
393 return aead_aes_ccm_init(ctx, key, key_len, tag_len, 4, 2);
394 }
395
DEFINE_METHOD_FUNCTION(EVP_AEAD,EVP_aead_aes_128_ccm_bluetooth)396 DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_ccm_bluetooth) {
397 memset(out, 0, sizeof(EVP_AEAD));
398
399 out->key_len = 16;
400 out->nonce_len = 13;
401 out->overhead = 4;
402 out->max_tag_len = 4;
403
404 out->init = aead_aes_ccm_bluetooth_init;
405 out->cleanup = aead_aes_ccm_cleanup;
406 out->seal_scatter = aead_aes_ccm_seal_scatter;
407 out->open_gather = aead_aes_ccm_open_gather;
408 }
409
aead_aes_ccm_bluetooth_8_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len)410 static int aead_aes_ccm_bluetooth_8_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
411 size_t key_len, size_t tag_len) {
412 return aead_aes_ccm_init(ctx, key, key_len, tag_len, 8, 2);
413 }
414
DEFINE_METHOD_FUNCTION(EVP_AEAD,EVP_aead_aes_128_ccm_bluetooth_8)415 DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_ccm_bluetooth_8) {
416 memset(out, 0, sizeof(EVP_AEAD));
417
418 out->key_len = 16;
419 out->nonce_len = 13;
420 out->overhead = 8;
421 out->max_tag_len = 8;
422
423 out->init = aead_aes_ccm_bluetooth_8_init;
424 out->cleanup = aead_aes_ccm_cleanup;
425 out->seal_scatter = aead_aes_ccm_seal_scatter;
426 out->open_gather = aead_aes_ccm_open_gather;
427 }
428
aead_aes_ccm_matter_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len)429 static int aead_aes_ccm_matter_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
430 size_t key_len, size_t tag_len) {
431 return aead_aes_ccm_init(ctx, key, key_len, tag_len, 16, 2);
432 }
433
DEFINE_METHOD_FUNCTION(EVP_AEAD,EVP_aead_aes_128_ccm_matter)434 DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_ccm_matter) {
435 memset(out, 0, sizeof(EVP_AEAD));
436
437 out->key_len = 16;
438 out->nonce_len = 13;
439 out->overhead = 16;
440 out->max_tag_len = 16;
441
442 out->init = aead_aes_ccm_matter_init;
443 out->cleanup = aead_aes_ccm_cleanup;
444 out->seal_scatter = aead_aes_ccm_seal_scatter;
445 out->open_gather = aead_aes_ccm_open_gather;
446 }
447