Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / deps / grpc / third_party / boringssl / crypto / cipher_extra / e_aesccm.c
1 /* Copyright (c) 2018, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #include <openssl/aead.h>
16
17 #include <assert.h>
18
19 #include <openssl/cipher.h>
20 #include <openssl/err.h>
21 #include <openssl/mem.h>
22
23 #include "../fipsmodule/cipher/internal.h"
24
25
26 #define EVP_AEAD_AES_CCM_MAX_TAG_LEN 16
27
28 struct aead_aes_ccm_ctx {
29   union {
30     double align;
31     AES_KEY ks;
32   } ks;
33   CCM128_CONTEXT ccm;
34 };
35
36 static int aead_aes_ccm_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
37                              size_t key_len, size_t tag_len, unsigned M,
38                              unsigned L) {
39   assert(M == EVP_AEAD_max_overhead(ctx->aead));
40   assert(M == EVP_AEAD_max_tag_len(ctx->aead));
41   assert(15 - L == EVP_AEAD_nonce_length(ctx->aead));
42
43   if (key_len != EVP_AEAD_key_length(ctx->aead)) {
44     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
45     return 0;  // EVP_AEAD_CTX_init should catch this.
46   }
47
48   if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) {
49     tag_len = M;
50   }
51
52   if (tag_len != M) {
53     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE);
54     return 0;
55   }
56
57   struct aead_aes_ccm_ctx *ccm_ctx =
58       OPENSSL_malloc(sizeof(struct aead_aes_ccm_ctx));
59   if (ccm_ctx == NULL) {
60     OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE);
61     return 0;
62   }
63
64   block128_f block;
65   ctr128_f ctr = aes_ctr_set_key(&ccm_ctx->ks.ks, NULL, &block, key, key_len);
66   ctx->tag_len = tag_len;
67   if (!CRYPTO_ccm128_init(&ccm_ctx->ccm, &ccm_ctx->ks.ks, block, ctr, M, L)) {
68     OPENSSL_PUT_ERROR(CIPHER, ERR_R_INTERNAL_ERROR);
69     OPENSSL_free(ccm_ctx);
70     return 0;
71   }
72
73   ctx->aead_state = ccm_ctx;
74   return 1;
75 }
76
77 static void aead_aes_ccm_cleanup(EVP_AEAD_CTX *ctx) {
78   OPENSSL_free(ctx->aead_state);
79 }
80
81 static int aead_aes_ccm_seal_scatter(
82     const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
83     size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
84     size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
85     size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
86   const struct aead_aes_ccm_ctx *ccm_ctx = ctx->aead_state;
87
88   if (in_len > CRYPTO_ccm128_max_input(&ccm_ctx->ccm)) {
89     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
90     return 0;
91   }
92
93   if (max_out_tag_len < ctx->tag_len) {
94     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
95     return 0;
96   }
97
98   if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
99     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
100     return 0;
101   }
102
103   if (!CRYPTO_ccm128_encrypt(&ccm_ctx->ccm, &ccm_ctx->ks.ks, out, out_tag,
104                              ctx->tag_len, nonce, nonce_len, in, in_len, ad,
105                              ad_len)) {
106     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
107     return 0;
108   }
109
110   *out_tag_len = ctx->tag_len;
111   return 1;
112 }
113
114 static int aead_aes_ccm_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out,
115                                     const uint8_t *nonce, size_t nonce_len,
116                                     const uint8_t *in, size_t in_len,
117                                     const uint8_t *in_tag, size_t in_tag_len,
118                                     const uint8_t *ad, size_t ad_len) {
119   const struct aead_aes_ccm_ctx *ccm_ctx = ctx->aead_state;
120
121   if (in_len > CRYPTO_ccm128_max_input(&ccm_ctx->ccm)) {
122     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
123     return 0;
124   }
125
126   if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
127     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
128     return 0;
129   }
130
131   if (in_tag_len != ctx->tag_len) {
132     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
133     return 0;
134   }
135
136   uint8_t tag[EVP_AEAD_AES_CCM_MAX_TAG_LEN];
137   assert(ctx->tag_len <= EVP_AEAD_AES_CCM_MAX_TAG_LEN);
138   if (!CRYPTO_ccm128_decrypt(&ccm_ctx->ccm, &ccm_ctx->ks.ks, out, tag,
139                              ctx->tag_len, nonce, nonce_len, in, in_len, ad,
140                              ad_len)) {
141     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
142     return 0;
143   }
144
145   if (CRYPTO_memcmp(tag, in_tag, ctx->tag_len) != 0) {
146     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
147     return 0;
148   }
149
150   return 1;
151 }
152
153 static int aead_aes_ccm_bluetooth_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
154                                        size_t key_len, size_t tag_len) {
155   return aead_aes_ccm_init(ctx, key, key_len, tag_len, 4, 2);
156 }
157
158 static const EVP_AEAD aead_aes_128_ccm_bluetooth = {
159     16,  // key length (AES-128)
160     13,  // nonce length
161     4,   // overhead
162     4,   // max tag length
163     0,   // seal_scatter_supports_extra_in
164
165     aead_aes_ccm_bluetooth_init,
166     NULL /* init_with_direction */,
167     aead_aes_ccm_cleanup,
168     NULL /* open */,
169     aead_aes_ccm_seal_scatter,
170     aead_aes_ccm_open_gather,
171     NULL /* get_iv */,
172     NULL /* tag_len */,
173 };
174
175 const EVP_AEAD *EVP_aead_aes_128_ccm_bluetooth(void) {
176   return &aead_aes_128_ccm_bluetooth;
177 }
178
179 static int aead_aes_ccm_bluetooth_8_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
180                                          size_t key_len, size_t tag_len) {
181   return aead_aes_ccm_init(ctx, key, key_len, tag_len, 8, 2);
182 }
183
184 static const EVP_AEAD aead_aes_128_ccm_bluetooth_8 = {
185     16,  // key length (AES-128)
186     13,  // nonce length
187     8,   // overhead
188     8,   // max tag length
189     0,   // seal_scatter_supports_extra_in
190
191     aead_aes_ccm_bluetooth_8_init,
192     NULL /* init_with_direction */,
193     aead_aes_ccm_cleanup,
194     NULL /* open */,
195     aead_aes_ccm_seal_scatter,
196     aead_aes_ccm_open_gather,
197     NULL /* get_iv */,
198     NULL /* tag_len */,
199 };
200
201 const EVP_AEAD *EVP_aead_aes_128_ccm_bluetooth_8(void) {
202   return &aead_aes_128_ccm_bluetooth_8;
203 }