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