• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2014, 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 <assert.h>
16 #include <limits.h>
17 #include <string.h>
18 
19 #include <openssl/aead.h>
20 #include <openssl/cipher.h>
21 #include <openssl/err.h>
22 #include <openssl/hmac.h>
23 #include <openssl/md5.h>
24 #include <openssl/mem.h>
25 #include <openssl/sha.h>
26 #include <openssl/type_check.h>
27 
28 #include "../fipsmodule/cipher/internal.h"
29 #include "../internal.h"
30 #include "internal.h"
31 
32 
33 typedef struct {
34   EVP_CIPHER_CTX cipher_ctx;
35   HMAC_CTX hmac_ctx;
36   // mac_key is the portion of the key used for the MAC. It is retained
37   // separately for the constant-time CBC code.
38   uint8_t mac_key[EVP_MAX_MD_SIZE];
39   uint8_t mac_key_len;
40   // implicit_iv is one iff this is a pre-TLS-1.1 CBC cipher without an explicit
41   // IV.
42   char implicit_iv;
43 } AEAD_TLS_CTX;
44 
45 OPENSSL_COMPILE_ASSERT(EVP_MAX_MD_SIZE < 256, mac_key_len_fits_in_uint8_t);
46 
aead_tls_cleanup(EVP_AEAD_CTX * ctx)47 static void aead_tls_cleanup(EVP_AEAD_CTX *ctx) {
48   AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state;
49   EVP_CIPHER_CTX_cleanup(&tls_ctx->cipher_ctx);
50   HMAC_CTX_cleanup(&tls_ctx->hmac_ctx);
51   OPENSSL_free(tls_ctx);
52   ctx->aead_state = NULL;
53 }
54 
aead_tls_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len,enum evp_aead_direction_t dir,const EVP_CIPHER * cipher,const EVP_MD * md,char implicit_iv)55 static int aead_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len,
56                          size_t tag_len, enum evp_aead_direction_t dir,
57                          const EVP_CIPHER *cipher, const EVP_MD *md,
58                          char implicit_iv) {
59   if (tag_len != EVP_AEAD_DEFAULT_TAG_LENGTH &&
60       tag_len != EVP_MD_size(md)) {
61     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_TAG_SIZE);
62     return 0;
63   }
64 
65   if (key_len != EVP_AEAD_key_length(ctx->aead)) {
66     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
67     return 0;
68   }
69 
70   size_t mac_key_len = EVP_MD_size(md);
71   size_t enc_key_len = EVP_CIPHER_key_length(cipher);
72   assert(mac_key_len + enc_key_len +
73          (implicit_iv ? EVP_CIPHER_iv_length(cipher) : 0) == key_len);
74 
75   AEAD_TLS_CTX *tls_ctx = OPENSSL_malloc(sizeof(AEAD_TLS_CTX));
76   if (tls_ctx == NULL) {
77     OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE);
78     return 0;
79   }
80   EVP_CIPHER_CTX_init(&tls_ctx->cipher_ctx);
81   HMAC_CTX_init(&tls_ctx->hmac_ctx);
82   assert(mac_key_len <= EVP_MAX_MD_SIZE);
83   OPENSSL_memcpy(tls_ctx->mac_key, key, mac_key_len);
84   tls_ctx->mac_key_len = (uint8_t)mac_key_len;
85   tls_ctx->implicit_iv = implicit_iv;
86 
87   ctx->aead_state = tls_ctx;
88   if (!EVP_CipherInit_ex(&tls_ctx->cipher_ctx, cipher, NULL, &key[mac_key_len],
89                          implicit_iv ? &key[mac_key_len + enc_key_len] : NULL,
90                          dir == evp_aead_seal) ||
91       !HMAC_Init_ex(&tls_ctx->hmac_ctx, key, mac_key_len, md, NULL)) {
92     aead_tls_cleanup(ctx);
93     ctx->aead_state = NULL;
94     return 0;
95   }
96   EVP_CIPHER_CTX_set_padding(&tls_ctx->cipher_ctx, 0);
97 
98   return 1;
99 }
100 
aead_tls_tag_len(const EVP_AEAD_CTX * ctx,const size_t in_len,const size_t extra_in_len)101 static size_t aead_tls_tag_len(const EVP_AEAD_CTX *ctx, const size_t in_len,
102                                const size_t extra_in_len) {
103   assert(extra_in_len == 0);
104   AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state;
105 
106   const size_t hmac_len = HMAC_size(&tls_ctx->hmac_ctx);
107   if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) != EVP_CIPH_CBC_MODE) {
108     // The NULL cipher.
109     return hmac_len;
110   }
111 
112   const size_t block_size = EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx);
113   // An overflow of |in_len + hmac_len| doesn't affect the result mod
114   // |block_size|, provided that |block_size| is a smaller power of two.
115   assert(block_size != 0 && (block_size & (block_size - 1)) == 0);
116   const size_t pad_len = block_size - (in_len + hmac_len) % block_size;
117   return hmac_len + pad_len;
118 }
119 
aead_tls_seal_scatter(const EVP_AEAD_CTX * ctx,uint8_t * out,uint8_t * out_tag,size_t * out_tag_len,const size_t max_out_tag_len,const uint8_t * nonce,const size_t nonce_len,const uint8_t * in,const size_t in_len,const uint8_t * extra_in,const size_t extra_in_len,const uint8_t * ad,const size_t ad_len)120 static int aead_tls_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out,
121                                  uint8_t *out_tag, size_t *out_tag_len,
122                                  const size_t max_out_tag_len,
123                                  const uint8_t *nonce, const size_t nonce_len,
124                                  const uint8_t *in, const size_t in_len,
125                                  const uint8_t *extra_in,
126                                  const size_t extra_in_len, const uint8_t *ad,
127                                  const size_t ad_len) {
128   AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state;
129 
130   if (!tls_ctx->cipher_ctx.encrypt) {
131     // Unlike a normal AEAD, a TLS AEAD may only be used in one direction.
132     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION);
133     return 0;
134   }
135 
136   if (in_len > INT_MAX) {
137     // EVP_CIPHER takes int as input.
138     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
139     return 0;
140   }
141 
142   if (max_out_tag_len < aead_tls_tag_len(ctx, in_len, extra_in_len)) {
143     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
144     return 0;
145   }
146 
147   if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
148     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
149     return 0;
150   }
151 
152   if (ad_len != 13 - 2 /* length bytes */) {
153     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE);
154     return 0;
155   }
156 
157   // To allow for CBC mode which changes cipher length, |ad| doesn't include the
158   // length for legacy ciphers.
159   uint8_t ad_extra[2];
160   ad_extra[0] = (uint8_t)(in_len >> 8);
161   ad_extra[1] = (uint8_t)(in_len & 0xff);
162 
163   // Compute the MAC. This must be first in case the operation is being done
164   // in-place.
165   uint8_t mac[EVP_MAX_MD_SIZE];
166   unsigned mac_len;
167   if (!HMAC_Init_ex(&tls_ctx->hmac_ctx, NULL, 0, NULL, NULL) ||
168       !HMAC_Update(&tls_ctx->hmac_ctx, ad, ad_len) ||
169       !HMAC_Update(&tls_ctx->hmac_ctx, ad_extra, sizeof(ad_extra)) ||
170       !HMAC_Update(&tls_ctx->hmac_ctx, in, in_len) ||
171       !HMAC_Final(&tls_ctx->hmac_ctx, mac, &mac_len)) {
172     return 0;
173   }
174 
175   // Configure the explicit IV.
176   if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
177       !tls_ctx->implicit_iv &&
178       !EVP_EncryptInit_ex(&tls_ctx->cipher_ctx, NULL, NULL, NULL, nonce)) {
179     return 0;
180   }
181 
182   // Encrypt the input.
183   int len;
184   if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out, &len, in, (int)in_len)) {
185     return 0;
186   }
187 
188   unsigned block_size = EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx);
189 
190   // Feed the MAC into the cipher in two steps. First complete the final partial
191   // block from encrypting the input and split the result between |out| and
192   // |out_tag|. Then feed the rest.
193 
194   const size_t early_mac_len = (block_size - (in_len % block_size)) % block_size;
195   if (early_mac_len != 0) {
196     assert(len + block_size - early_mac_len == in_len);
197     uint8_t buf[EVP_MAX_BLOCK_LENGTH];
198     int buf_len;
199     if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, buf, &buf_len, mac,
200                            (int)early_mac_len)) {
201       return 0;
202     }
203     assert(buf_len == (int)block_size);
204     OPENSSL_memcpy(out + len, buf, block_size - early_mac_len);
205     OPENSSL_memcpy(out_tag, buf + block_size - early_mac_len, early_mac_len);
206   }
207   size_t tag_len = early_mac_len;
208 
209   if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out_tag + tag_len, &len,
210                          mac + tag_len, mac_len - tag_len)) {
211     return 0;
212   }
213   tag_len += len;
214 
215   if (block_size > 1) {
216     assert(block_size <= 256);
217     assert(EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE);
218 
219     // Compute padding and feed that into the cipher.
220     uint8_t padding[256];
221     unsigned padding_len = block_size - ((in_len + mac_len) % block_size);
222     OPENSSL_memset(padding, padding_len - 1, padding_len);
223     if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out_tag + tag_len, &len,
224                            padding, (int)padding_len)) {
225       return 0;
226     }
227     tag_len += len;
228   }
229 
230   if (!EVP_EncryptFinal_ex(&tls_ctx->cipher_ctx, out_tag + tag_len, &len)) {
231     return 0;
232   }
233   assert(len == 0);  // Padding is explicit.
234   assert(tag_len == aead_tls_tag_len(ctx, in_len, extra_in_len));
235 
236   *out_tag_len = tag_len;
237   return 1;
238 }
239 
aead_tls_open(const EVP_AEAD_CTX * ctx,uint8_t * out,size_t * out_len,size_t max_out_len,const uint8_t * nonce,size_t nonce_len,const uint8_t * in,size_t in_len,const uint8_t * ad,size_t ad_len)240 static int aead_tls_open(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
241                          size_t max_out_len, const uint8_t *nonce,
242                          size_t nonce_len, const uint8_t *in, size_t in_len,
243                          const uint8_t *ad, size_t ad_len) {
244   AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state;
245 
246   if (tls_ctx->cipher_ctx.encrypt) {
247     // Unlike a normal AEAD, a TLS AEAD may only be used in one direction.
248     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION);
249     return 0;
250   }
251 
252   if (in_len < HMAC_size(&tls_ctx->hmac_ctx)) {
253     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
254     return 0;
255   }
256 
257   if (max_out_len < in_len) {
258     // This requires that the caller provide space for the MAC, even though it
259     // will always be removed on return.
260     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
261     return 0;
262   }
263 
264   if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
265     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
266     return 0;
267   }
268 
269   if (ad_len != 13 - 2 /* length bytes */) {
270     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE);
271     return 0;
272   }
273 
274   if (in_len > INT_MAX) {
275     // EVP_CIPHER takes int as input.
276     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
277     return 0;
278   }
279 
280   // Configure the explicit IV.
281   if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
282       !tls_ctx->implicit_iv &&
283       !EVP_DecryptInit_ex(&tls_ctx->cipher_ctx, NULL, NULL, NULL, nonce)) {
284     return 0;
285   }
286 
287   // Decrypt to get the plaintext + MAC + padding.
288   size_t total = 0;
289   int len;
290   if (!EVP_DecryptUpdate(&tls_ctx->cipher_ctx, out, &len, in, (int)in_len)) {
291     return 0;
292   }
293   total += len;
294   if (!EVP_DecryptFinal_ex(&tls_ctx->cipher_ctx, out + total, &len)) {
295     return 0;
296   }
297   total += len;
298   assert(total == in_len);
299 
300   // Remove CBC padding. Code from here on is timing-sensitive with respect to
301   // |padding_ok| and |data_plus_mac_len| for CBC ciphers.
302   size_t data_plus_mac_len;
303   crypto_word_t padding_ok;
304   if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE) {
305     if (!EVP_tls_cbc_remove_padding(
306             &padding_ok, &data_plus_mac_len, out, total,
307             EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx),
308             HMAC_size(&tls_ctx->hmac_ctx))) {
309       // Publicly invalid. This can be rejected in non-constant time.
310       OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
311       return 0;
312     }
313   } else {
314     padding_ok = CONSTTIME_TRUE_W;
315     data_plus_mac_len = total;
316     // |data_plus_mac_len| = |total| = |in_len| at this point. |in_len| has
317     // already been checked against the MAC size at the top of the function.
318     assert(data_plus_mac_len >= HMAC_size(&tls_ctx->hmac_ctx));
319   }
320   size_t data_len = data_plus_mac_len - HMAC_size(&tls_ctx->hmac_ctx);
321 
322   // At this point, if the padding is valid, the first |data_plus_mac_len| bytes
323   // after |out| are the plaintext and MAC. Otherwise, |data_plus_mac_len| is
324   // still large enough to extract a MAC, but it will be irrelevant.
325 
326   // To allow for CBC mode which changes cipher length, |ad| doesn't include the
327   // length for legacy ciphers.
328   uint8_t ad_fixed[13];
329   OPENSSL_memcpy(ad_fixed, ad, 11);
330   ad_fixed[11] = (uint8_t)(data_len >> 8);
331   ad_fixed[12] = (uint8_t)(data_len & 0xff);
332   ad_len += 2;
333 
334   // Compute the MAC and extract the one in the record.
335   uint8_t mac[EVP_MAX_MD_SIZE];
336   size_t mac_len;
337   uint8_t record_mac_tmp[EVP_MAX_MD_SIZE];
338   uint8_t *record_mac;
339   if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
340       EVP_tls_cbc_record_digest_supported(tls_ctx->hmac_ctx.md)) {
341     if (!EVP_tls_cbc_digest_record(tls_ctx->hmac_ctx.md, mac, &mac_len,
342                                    ad_fixed, out, data_plus_mac_len, total,
343                                    tls_ctx->mac_key, tls_ctx->mac_key_len)) {
344       OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
345       return 0;
346     }
347     assert(mac_len == HMAC_size(&tls_ctx->hmac_ctx));
348 
349     record_mac = record_mac_tmp;
350     EVP_tls_cbc_copy_mac(record_mac, mac_len, out, data_plus_mac_len, total);
351   } else {
352     // We should support the constant-time path for all CBC-mode ciphers
353     // implemented.
354     assert(EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) != EVP_CIPH_CBC_MODE);
355 
356     unsigned mac_len_u;
357     if (!HMAC_Init_ex(&tls_ctx->hmac_ctx, NULL, 0, NULL, NULL) ||
358         !HMAC_Update(&tls_ctx->hmac_ctx, ad_fixed, ad_len) ||
359         !HMAC_Update(&tls_ctx->hmac_ctx, out, data_len) ||
360         !HMAC_Final(&tls_ctx->hmac_ctx, mac, &mac_len_u)) {
361       return 0;
362     }
363     mac_len = mac_len_u;
364 
365     assert(mac_len == HMAC_size(&tls_ctx->hmac_ctx));
366     record_mac = &out[data_len];
367   }
368 
369   // Perform the MAC check and the padding check in constant-time. It should be
370   // safe to simply perform the padding check first, but it would not be under a
371   // different choice of MAC location on padding failure. See
372   // EVP_tls_cbc_remove_padding.
373   crypto_word_t good =
374       constant_time_eq_int(CRYPTO_memcmp(record_mac, mac, mac_len), 0);
375   good &= padding_ok;
376   if (!good) {
377     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
378     return 0;
379   }
380 
381   // End of timing-sensitive code.
382 
383   *out_len = data_len;
384   return 1;
385 }
386 
aead_aes_128_cbc_sha1_tls_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len,enum evp_aead_direction_t dir)387 static int aead_aes_128_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
388                                           size_t key_len, size_t tag_len,
389                                           enum evp_aead_direction_t dir) {
390   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
391                        EVP_sha1(), 0);
392 }
393 
aead_aes_128_cbc_sha1_tls_implicit_iv_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len,enum evp_aead_direction_t dir)394 static int aead_aes_128_cbc_sha1_tls_implicit_iv_init(
395     EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
396     enum evp_aead_direction_t dir) {
397   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
398                        EVP_sha1(), 1);
399 }
400 
aead_aes_128_cbc_sha256_tls_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len,enum evp_aead_direction_t dir)401 static int aead_aes_128_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx,
402                                             const uint8_t *key, size_t key_len,
403                                             size_t tag_len,
404                                             enum evp_aead_direction_t dir) {
405   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
406                        EVP_sha256(), 0);
407 }
408 
aead_aes_256_cbc_sha1_tls_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len,enum evp_aead_direction_t dir)409 static int aead_aes_256_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
410                                           size_t key_len, size_t tag_len,
411                                           enum evp_aead_direction_t dir) {
412   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
413                        EVP_sha1(), 0);
414 }
415 
aead_aes_256_cbc_sha1_tls_implicit_iv_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len,enum evp_aead_direction_t dir)416 static int aead_aes_256_cbc_sha1_tls_implicit_iv_init(
417     EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
418     enum evp_aead_direction_t dir) {
419   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
420                        EVP_sha1(), 1);
421 }
422 
aead_aes_256_cbc_sha256_tls_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len,enum evp_aead_direction_t dir)423 static int aead_aes_256_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx,
424                                             const uint8_t *key, size_t key_len,
425                                             size_t tag_len,
426                                             enum evp_aead_direction_t dir) {
427   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
428                        EVP_sha256(), 0);
429 }
430 
aead_aes_256_cbc_sha384_tls_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len,enum evp_aead_direction_t dir)431 static int aead_aes_256_cbc_sha384_tls_init(EVP_AEAD_CTX *ctx,
432                                             const uint8_t *key, size_t key_len,
433                                             size_t tag_len,
434                                             enum evp_aead_direction_t dir) {
435   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
436                        EVP_sha384(), 0);
437 }
438 
aead_des_ede3_cbc_sha1_tls_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len,enum evp_aead_direction_t dir)439 static int aead_des_ede3_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx,
440                                            const uint8_t *key, size_t key_len,
441                                            size_t tag_len,
442                                            enum evp_aead_direction_t dir) {
443   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
444                        EVP_sha1(), 0);
445 }
446 
aead_des_ede3_cbc_sha1_tls_implicit_iv_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len,enum evp_aead_direction_t dir)447 static int aead_des_ede3_cbc_sha1_tls_implicit_iv_init(
448     EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
449     enum evp_aead_direction_t dir) {
450   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
451                        EVP_sha1(), 1);
452 }
453 
aead_tls_get_iv(const EVP_AEAD_CTX * ctx,const uint8_t ** out_iv,size_t * out_iv_len)454 static int aead_tls_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv,
455                            size_t *out_iv_len) {
456   const AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX*) ctx->aead_state;
457   const size_t iv_len = EVP_CIPHER_CTX_iv_length(&tls_ctx->cipher_ctx);
458   if (iv_len <= 1) {
459     return 0;
460   }
461 
462   *out_iv = tls_ctx->cipher_ctx.iv;
463   *out_iv_len = iv_len;
464   return 1;
465 }
466 
aead_null_sha1_tls_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len,enum evp_aead_direction_t dir)467 static int aead_null_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
468                                    size_t key_len, size_t tag_len,
469                                    enum evp_aead_direction_t dir) {
470   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_enc_null(),
471                        EVP_sha1(), 1 /* implicit iv */);
472 }
473 
474 static const EVP_AEAD aead_aes_128_cbc_sha1_tls = {
475     SHA_DIGEST_LENGTH + 16,  // key len (SHA1 + AES128)
476     16,                      // nonce len (IV)
477     16 + SHA_DIGEST_LENGTH,  // overhead (padding + SHA1)
478     SHA_DIGEST_LENGTH,       // max tag length
479     0,                       // seal_scatter_supports_extra_in
480 
481     NULL,  // init
482     aead_aes_128_cbc_sha1_tls_init,
483     aead_tls_cleanup,
484     aead_tls_open,
485     aead_tls_seal_scatter,
486     NULL,  // open_gather
487     NULL,  // get_iv
488     aead_tls_tag_len,
489 };
490 
491 static const EVP_AEAD aead_aes_128_cbc_sha1_tls_implicit_iv = {
492     SHA_DIGEST_LENGTH + 16 + 16,  // key len (SHA1 + AES128 + IV)
493     0,                            // nonce len
494     16 + SHA_DIGEST_LENGTH,       // overhead (padding + SHA1)
495     SHA_DIGEST_LENGTH,            // max tag length
496     0,                            // seal_scatter_supports_extra_in
497 
498     NULL,  // init
499     aead_aes_128_cbc_sha1_tls_implicit_iv_init,
500     aead_tls_cleanup,
501     aead_tls_open,
502     aead_tls_seal_scatter,
503     NULL,             // open_gather
504     aead_tls_get_iv,  // get_iv
505     aead_tls_tag_len,
506 };
507 
508 static const EVP_AEAD aead_aes_128_cbc_sha256_tls = {
509     SHA256_DIGEST_LENGTH + 16,  // key len (SHA256 + AES128)
510     16,                         // nonce len (IV)
511     16 + SHA256_DIGEST_LENGTH,  // overhead (padding + SHA256)
512     SHA256_DIGEST_LENGTH,       // max tag length
513     0,                          // seal_scatter_supports_extra_in
514 
515     NULL,  // init
516     aead_aes_128_cbc_sha256_tls_init,
517     aead_tls_cleanup,
518     aead_tls_open,
519     aead_tls_seal_scatter,
520     NULL,  // open_gather
521     NULL,  // get_iv
522     aead_tls_tag_len,
523 };
524 
525 static const EVP_AEAD aead_aes_256_cbc_sha1_tls = {
526     SHA_DIGEST_LENGTH + 32,  // key len (SHA1 + AES256)
527     16,                      // nonce len (IV)
528     16 + SHA_DIGEST_LENGTH,  // overhead (padding + SHA1)
529     SHA_DIGEST_LENGTH,       // max tag length
530     0,                       // seal_scatter_supports_extra_in
531 
532     NULL,  // init
533     aead_aes_256_cbc_sha1_tls_init,
534     aead_tls_cleanup,
535     aead_tls_open,
536     aead_tls_seal_scatter,
537     NULL,  // open_gather
538     NULL,  // get_iv
539     aead_tls_tag_len,
540 };
541 
542 static const EVP_AEAD aead_aes_256_cbc_sha1_tls_implicit_iv = {
543     SHA_DIGEST_LENGTH + 32 + 16,  // key len (SHA1 + AES256 + IV)
544     0,                            // nonce len
545     16 + SHA_DIGEST_LENGTH,       // overhead (padding + SHA1)
546     SHA_DIGEST_LENGTH,            // max tag length
547     0,                            // seal_scatter_supports_extra_in
548 
549     NULL,  // init
550     aead_aes_256_cbc_sha1_tls_implicit_iv_init,
551     aead_tls_cleanup,
552     aead_tls_open,
553     aead_tls_seal_scatter,
554     NULL,             // open_gather
555     aead_tls_get_iv,  // get_iv
556     aead_tls_tag_len,
557 };
558 
559 static const EVP_AEAD aead_aes_256_cbc_sha256_tls = {
560     SHA256_DIGEST_LENGTH + 32,  // key len (SHA256 + AES256)
561     16,                         // nonce len (IV)
562     16 + SHA256_DIGEST_LENGTH,  // overhead (padding + SHA256)
563     SHA256_DIGEST_LENGTH,       // max tag length
564     0,                          // seal_scatter_supports_extra_in
565 
566     NULL,  // init
567     aead_aes_256_cbc_sha256_tls_init,
568     aead_tls_cleanup,
569     aead_tls_open,
570     aead_tls_seal_scatter,
571     NULL,  // open_gather
572     NULL,  // get_iv
573     aead_tls_tag_len,
574 };
575 
576 static const EVP_AEAD aead_aes_256_cbc_sha384_tls = {
577     SHA384_DIGEST_LENGTH + 32,  // key len (SHA384 + AES256)
578     16,                         // nonce len (IV)
579     16 + SHA384_DIGEST_LENGTH,  // overhead (padding + SHA384)
580     SHA384_DIGEST_LENGTH,       // max tag length
581     0,                          // seal_scatter_supports_extra_in
582 
583     NULL,  // init
584     aead_aes_256_cbc_sha384_tls_init,
585     aead_tls_cleanup,
586     aead_tls_open,
587     aead_tls_seal_scatter,
588     NULL,  // open_gather
589     NULL,  // get_iv
590     aead_tls_tag_len,
591 };
592 
593 static const EVP_AEAD aead_des_ede3_cbc_sha1_tls = {
594     SHA_DIGEST_LENGTH + 24,  // key len (SHA1 + 3DES)
595     8,                       // nonce len (IV)
596     8 + SHA_DIGEST_LENGTH,   // overhead (padding + SHA1)
597     SHA_DIGEST_LENGTH,       // max tag length
598     0,                       // seal_scatter_supports_extra_in
599 
600     NULL,  // init
601     aead_des_ede3_cbc_sha1_tls_init,
602     aead_tls_cleanup,
603     aead_tls_open,
604     aead_tls_seal_scatter,
605     NULL,  // open_gather
606     NULL,  // get_iv
607     aead_tls_tag_len,
608 };
609 
610 static const EVP_AEAD aead_des_ede3_cbc_sha1_tls_implicit_iv = {
611     SHA_DIGEST_LENGTH + 24 + 8,  // key len (SHA1 + 3DES + IV)
612     0,                           // nonce len
613     8 + SHA_DIGEST_LENGTH,       // overhead (padding + SHA1)
614     SHA_DIGEST_LENGTH,           // max tag length
615     0,                           // seal_scatter_supports_extra_in
616 
617     NULL,  // init
618     aead_des_ede3_cbc_sha1_tls_implicit_iv_init,
619     aead_tls_cleanup,
620     aead_tls_open,
621     aead_tls_seal_scatter,
622     NULL,             // open_gather
623     aead_tls_get_iv,  // get_iv
624     aead_tls_tag_len,
625 };
626 
627 static const EVP_AEAD aead_null_sha1_tls = {
628     SHA_DIGEST_LENGTH,  // key len
629     0,                  // nonce len
630     SHA_DIGEST_LENGTH,  // overhead (SHA1)
631     SHA_DIGEST_LENGTH,  // max tag length
632     0,                  // seal_scatter_supports_extra_in
633 
634     NULL,  // init
635     aead_null_sha1_tls_init,
636     aead_tls_cleanup,
637     aead_tls_open,
638     aead_tls_seal_scatter,
639     NULL,  // open_gather
640     NULL,  // get_iv
641     aead_tls_tag_len,
642 };
643 
EVP_aead_aes_128_cbc_sha1_tls(void)644 const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls(void) {
645   return &aead_aes_128_cbc_sha1_tls;
646 }
647 
EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(void)648 const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(void) {
649   return &aead_aes_128_cbc_sha1_tls_implicit_iv;
650 }
651 
EVP_aead_aes_128_cbc_sha256_tls(void)652 const EVP_AEAD *EVP_aead_aes_128_cbc_sha256_tls(void) {
653   return &aead_aes_128_cbc_sha256_tls;
654 }
655 
EVP_aead_aes_256_cbc_sha1_tls(void)656 const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls(void) {
657   return &aead_aes_256_cbc_sha1_tls;
658 }
659 
EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(void)660 const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(void) {
661   return &aead_aes_256_cbc_sha1_tls_implicit_iv;
662 }
663 
EVP_aead_aes_256_cbc_sha256_tls(void)664 const EVP_AEAD *EVP_aead_aes_256_cbc_sha256_tls(void) {
665   return &aead_aes_256_cbc_sha256_tls;
666 }
667 
EVP_aead_aes_256_cbc_sha384_tls(void)668 const EVP_AEAD *EVP_aead_aes_256_cbc_sha384_tls(void) {
669   return &aead_aes_256_cbc_sha384_tls;
670 }
671 
EVP_aead_des_ede3_cbc_sha1_tls(void)672 const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls(void) {
673   return &aead_des_ede3_cbc_sha1_tls;
674 }
675 
EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(void)676 const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(void) {
677   return &aead_des_ede3_cbc_sha1_tls_implicit_iv;
678 }
679 
EVP_aead_null_sha1_tls(void)680 const EVP_AEAD *EVP_aead_null_sha1_tls(void) { return &aead_null_sha1_tls; }
681