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