1 // Copyright 2014 The BoringSSL Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
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_free(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 && tag_len != EVP_MD_size(md)) {
62     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_TAG_SIZE);
63     return 0;
64   }
65 
66   if (key_len != EVP_AEAD_key_length(ctx->aead)) {
67     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
68     return 0;
69   }
70 
71   size_t mac_key_len = EVP_MD_size(md);
72   size_t enc_key_len = EVP_CIPHER_key_length(cipher);
73   assert(mac_key_len + enc_key_len +
74              (implicit_iv ? EVP_CIPHER_iv_length(cipher) : 0) ==
75          key_len);
76 
77   AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
78   tls_ctx->hmac_ctx = HMAC_CTX_new();
79   if (!tls_ctx->hmac_ctx) {
80     return 0;
81   }
82   EVP_CIPHER_CTX_init(&tls_ctx->cipher_ctx);
83   assert(mac_key_len <= EVP_MAX_MD_SIZE);
84   OPENSSL_memcpy(tls_ctx->mac_key, key, mac_key_len);
85   tls_ctx->mac_key_len = (uint8_t)mac_key_len;
86   tls_ctx->implicit_iv = implicit_iv;
87 
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     return 0;
94   }
95   EVP_CIPHER_CTX_set_padding(&tls_ctx->cipher_ctx, 0);
96 
97   return 1;
98 }
99 
aead_tls_tag_len(const EVP_AEAD_CTX * ctx,const size_t in_len,const size_t extra_in_len)100 static size_t aead_tls_tag_len(const EVP_AEAD_CTX *ctx, const size_t in_len,
101                                const size_t extra_in_len) {
102   assert(extra_in_len == 0);
103   const AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
104 
105   const size_t hmac_len = HMAC_size(tls_ctx->hmac_ctx);
106   if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) != EVP_CIPH_CBC_MODE) {
107     // The NULL cipher.
108     return hmac_len;
109   }
110 
111   const size_t block_size = EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx);
112   // An overflow of |in_len + hmac_len| doesn't affect the result mod
113   // |block_size|, provided that |block_size| is a smaller power of two.
114   assert(block_size != 0 && (block_size & (block_size - 1)) == 0);
115   const size_t pad_len = block_size - (in_len + hmac_len) % block_size;
116   return hmac_len + pad_len;
117 }
118 
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)119 static int aead_tls_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out,
120                                  uint8_t *out_tag, size_t *out_tag_len,
121                                  const size_t max_out_tag_len,
122                                  const uint8_t *nonce, const size_t nonce_len,
123                                  const uint8_t *in, const size_t in_len,
124                                  const uint8_t *extra_in,
125                                  const size_t extra_in_len, const uint8_t *ad,
126                                  const size_t ad_len) {
127   AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
128 
129   if (!tls_ctx->cipher_ctx.encrypt) {
130     // Unlike a normal AEAD, a TLS AEAD may only be used in one direction.
131     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION);
132     return 0;
133   }
134 
135   if (in_len > INT_MAX) {
136     // EVP_CIPHER takes int as input.
137     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
138     return 0;
139   }
140 
141   if (max_out_tag_len < aead_tls_tag_len(ctx, in_len, extra_in_len)) {
142     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
143     return 0;
144   }
145 
146   if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
147     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
148     return 0;
149   }
150 
151   if (ad_len != 13 - 2 /* length bytes */) {
152     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE);
153     return 0;
154   }
155 
156   // To allow for CBC mode which changes cipher length, |ad| doesn't include the
157   // length for legacy ciphers.
158   uint8_t ad_extra[2];
159   ad_extra[0] = (uint8_t)(in_len >> 8);
160   ad_extra[1] = (uint8_t)(in_len & 0xff);
161 
162   // Compute the MAC. This must be first in case the operation is being done
163   // in-place.
164   uint8_t mac[EVP_MAX_MD_SIZE];
165   unsigned mac_len;
166   if (!HMAC_Init_ex(tls_ctx->hmac_ctx, NULL, 0, NULL, NULL) ||
167       !HMAC_Update(tls_ctx->hmac_ctx, ad, ad_len) ||
168       !HMAC_Update(tls_ctx->hmac_ctx, ad_extra, sizeof(ad_extra)) ||
169       !HMAC_Update(tls_ctx->hmac_ctx, in, in_len) ||
170       !HMAC_Final(tls_ctx->hmac_ctx, mac, &mac_len)) {
171     return 0;
172   }
173 
174   // Configure the explicit IV.
175   if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
176       !tls_ctx->implicit_iv &&
177       !EVP_EncryptInit_ex(&tls_ctx->cipher_ctx, NULL, NULL, NULL, nonce)) {
178     return 0;
179   }
180 
181   // Encrypt the input.
182   int len;
183   if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out, &len, in, (int)in_len)) {
184     return 0;
185   }
186 
187   unsigned block_size = EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx);
188 
189   // Feed the MAC into the cipher in two steps. First complete the final partial
190   // block from encrypting the input and split the result between |out| and
191   // |out_tag|. Then feed the rest.
192 
193   const size_t early_mac_len =
194       (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->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   CONSTTIME_SECRET(out, total);
301 
302   // Remove CBC padding. Code from here on is timing-sensitive with respect to
303   // |padding_ok| and |data_plus_mac_len| for CBC ciphers.
304   size_t data_plus_mac_len;
305   crypto_word_t padding_ok;
306   if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE) {
307     if (!EVP_tls_cbc_remove_padding(
308             &padding_ok, &data_plus_mac_len, out, total,
309             EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx),
310             HMAC_size(tls_ctx->hmac_ctx))) {
311       // Publicly invalid. This can be rejected in non-constant time.
312       OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
313       return 0;
314     }
315   } else {
316     padding_ok = CONSTTIME_TRUE_W;
317     data_plus_mac_len = total;
318     // |data_plus_mac_len| = |total| = |in_len| at this point. |in_len| has
319     // already been checked against the MAC size at the top of the function.
320     assert(data_plus_mac_len >= HMAC_size(tls_ctx->hmac_ctx));
321   }
322   size_t data_len = data_plus_mac_len - HMAC_size(tls_ctx->hmac_ctx);
323 
324   // At this point, if the padding is valid, the first |data_plus_mac_len| bytes
325   // after |out| are the plaintext and MAC. Otherwise, |data_plus_mac_len| is
326   // still large enough to extract a MAC, but it will be irrelevant.
327 
328   // To allow for CBC mode which changes cipher length, |ad| doesn't include the
329   // length for legacy ciphers.
330   uint8_t ad_fixed[13];
331   OPENSSL_memcpy(ad_fixed, ad, 11);
332   ad_fixed[11] = (uint8_t)(data_len >> 8);
333   ad_fixed[12] = (uint8_t)(data_len & 0xff);
334   ad_len += 2;
335 
336   // Compute the MAC and extract the one in the record.
337   uint8_t mac[EVP_MAX_MD_SIZE];
338   size_t mac_len;
339   uint8_t record_mac_tmp[EVP_MAX_MD_SIZE];
340   uint8_t *record_mac;
341   if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
342       EVP_tls_cbc_record_digest_supported(tls_ctx->hmac_ctx->md)) {
343     if (!EVP_tls_cbc_digest_record(tls_ctx->hmac_ctx->md, mac, &mac_len,
344                                    ad_fixed, out, data_len, total,
345                                    tls_ctx->mac_key, tls_ctx->mac_key_len)) {
346       OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
347       return 0;
348     }
349     assert(mac_len == HMAC_size(tls_ctx->hmac_ctx));
350 
351     record_mac = record_mac_tmp;
352     EVP_tls_cbc_copy_mac(record_mac, mac_len, out, data_plus_mac_len, total);
353   } else {
354     // We should support the constant-time path for all CBC-mode ciphers
355     // implemented.
356     assert(EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) != EVP_CIPH_CBC_MODE);
357 
358     unsigned mac_len_u;
359     if (!HMAC_Init_ex(tls_ctx->hmac_ctx, NULL, 0, NULL, NULL) ||
360         !HMAC_Update(tls_ctx->hmac_ctx, ad_fixed, ad_len) ||
361         !HMAC_Update(tls_ctx->hmac_ctx, out, data_len) ||
362         !HMAC_Final(tls_ctx->hmac_ctx, mac, &mac_len_u)) {
363       return 0;
364     }
365     mac_len = mac_len_u;
366 
367     assert(mac_len == HMAC_size(tls_ctx->hmac_ctx));
368     record_mac = &out[data_len];
369   }
370 
371   // Perform the MAC check and the padding check in constant-time. It should be
372   // safe to simply perform the padding check first, but it would not be under a
373   // different choice of MAC location on padding failure. See
374   // EVP_tls_cbc_remove_padding.
375   crypto_word_t good =
376       constant_time_eq_int(CRYPTO_memcmp(record_mac, mac, mac_len), 0);
377   good &= padding_ok;
378   CONSTTIME_DECLASSIFY(&good, sizeof(good));
379   if (!good) {
380     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
381     return 0;
382   }
383 
384   CONSTTIME_DECLASSIFY(&data_len, sizeof(data_len));
385   CONSTTIME_DECLASSIFY(out, data_len);
386 
387   // End of timing-sensitive code.
388 
389   *out_len = data_len;
390   return 1;
391 }
392 
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)393 static int aead_aes_128_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
394                                           size_t key_len, size_t tag_len,
395                                           enum evp_aead_direction_t dir) {
396   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
397                        EVP_sha1(), 0);
398 }
399 
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)400 static int aead_aes_128_cbc_sha1_tls_implicit_iv_init(
401     EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
402     enum evp_aead_direction_t dir) {
403   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
404                        EVP_sha1(), 1);
405 }
406 
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)407 static int aead_aes_128_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx,
408                                             const uint8_t *key, size_t key_len,
409                                             size_t tag_len,
410                                             enum evp_aead_direction_t dir) {
411   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
412                        EVP_sha256(), 0);
413 }
414 
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)415 static int aead_aes_256_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
416                                           size_t key_len, size_t tag_len,
417                                           enum evp_aead_direction_t dir) {
418   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
419                        EVP_sha1(), 0);
420 }
421 
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)422 static int aead_aes_256_cbc_sha1_tls_implicit_iv_init(
423     EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
424     enum evp_aead_direction_t dir) {
425   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
426                        EVP_sha1(), 1);
427 }
428 
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)429 static int aead_des_ede3_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx,
430                                            const uint8_t *key, size_t key_len,
431                                            size_t tag_len,
432                                            enum evp_aead_direction_t dir) {
433   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
434                        EVP_sha1(), 0);
435 }
436 
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)437 static int aead_des_ede3_cbc_sha1_tls_implicit_iv_init(
438     EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
439     enum evp_aead_direction_t dir) {
440   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
441                        EVP_sha1(), 1);
442 }
443 
aead_tls_get_iv(const EVP_AEAD_CTX * ctx,const uint8_t ** out_iv,size_t * out_iv_len)444 static int aead_tls_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv,
445                            size_t *out_iv_len) {
446   const AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
447   const size_t iv_len = EVP_CIPHER_CTX_iv_length(&tls_ctx->cipher_ctx);
448   if (iv_len <= 1) {
449     OPENSSL_PUT_ERROR(CIPHER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
450     return 0;
451   }
452 
453   *out_iv = tls_ctx->cipher_ctx.iv;
454   *out_iv_len = iv_len;
455   return 1;
456 }
457 
458 static const EVP_AEAD aead_aes_128_cbc_sha1_tls = {
459     SHA_DIGEST_LENGTH + 16,  // key len (SHA1 + AES128)
460     16,                      // nonce len (IV)
461     16 + SHA_DIGEST_LENGTH,  // overhead (padding + SHA1)
462     SHA_DIGEST_LENGTH,       // max tag length
463     0,                       // seal_scatter_supports_extra_in
464 
465     NULL,  // init
466     aead_aes_128_cbc_sha1_tls_init,
467     aead_tls_cleanup,
468     aead_tls_open,
469     aead_tls_seal_scatter,
470     NULL,  // open_gather
471     NULL,  // get_iv
472     aead_tls_tag_len,
473 };
474 
475 static const EVP_AEAD aead_aes_128_cbc_sha1_tls_implicit_iv = {
476     SHA_DIGEST_LENGTH + 16 + 16,  // key len (SHA1 + AES128 + IV)
477     0,                            // nonce len
478     16 + SHA_DIGEST_LENGTH,       // overhead (padding + SHA1)
479     SHA_DIGEST_LENGTH,            // max tag length
480     0,                            // seal_scatter_supports_extra_in
481 
482     NULL,  // init
483     aead_aes_128_cbc_sha1_tls_implicit_iv_init,
484     aead_tls_cleanup,
485     aead_tls_open,
486     aead_tls_seal_scatter,
487     NULL,             // open_gather
488     aead_tls_get_iv,  // get_iv
489     aead_tls_tag_len,
490 };
491 
492 static const EVP_AEAD aead_aes_128_cbc_sha256_tls = {
493     SHA256_DIGEST_LENGTH + 16,  // key len (SHA256 + AES128)
494     16,                         // nonce len (IV)
495     16 + SHA256_DIGEST_LENGTH,  // overhead (padding + SHA256)
496     SHA256_DIGEST_LENGTH,       // max tag length
497     0,                          // seal_scatter_supports_extra_in
498 
499     NULL,  // init
500     aead_aes_128_cbc_sha256_tls_init,
501     aead_tls_cleanup,
502     aead_tls_open,
503     aead_tls_seal_scatter,
504     NULL,  // open_gather
505     NULL,  // get_iv
506     aead_tls_tag_len,
507 };
508 
509 static const EVP_AEAD aead_aes_256_cbc_sha1_tls = {
510     SHA_DIGEST_LENGTH + 32,  // key len (SHA1 + AES256)
511     16,                      // nonce len (IV)
512     16 + SHA_DIGEST_LENGTH,  // overhead (padding + SHA1)
513     SHA_DIGEST_LENGTH,       // max tag length
514     0,                       // seal_scatter_supports_extra_in
515 
516     NULL,  // init
517     aead_aes_256_cbc_sha1_tls_init,
518     aead_tls_cleanup,
519     aead_tls_open,
520     aead_tls_seal_scatter,
521     NULL,  // open_gather
522     NULL,  // get_iv
523     aead_tls_tag_len,
524 };
525 
526 static const EVP_AEAD aead_aes_256_cbc_sha1_tls_implicit_iv = {
527     SHA_DIGEST_LENGTH + 32 + 16,  // key len (SHA1 + AES256 + IV)
528     0,                            // nonce len
529     16 + SHA_DIGEST_LENGTH,       // overhead (padding + SHA1)
530     SHA_DIGEST_LENGTH,            // max tag length
531     0,                            // seal_scatter_supports_extra_in
532 
533     NULL,  // init
534     aead_aes_256_cbc_sha1_tls_implicit_iv_init,
535     aead_tls_cleanup,
536     aead_tls_open,
537     aead_tls_seal_scatter,
538     NULL,             // open_gather
539     aead_tls_get_iv,  // get_iv
540     aead_tls_tag_len,
541 };
542 
543 static const EVP_AEAD aead_des_ede3_cbc_sha1_tls = {
544     SHA_DIGEST_LENGTH + 24,  // key len (SHA1 + 3DES)
545     8,                       // nonce len (IV)
546     8 + SHA_DIGEST_LENGTH,   // overhead (padding + SHA1)
547     SHA_DIGEST_LENGTH,       // max tag length
548     0,                       // seal_scatter_supports_extra_in
549 
550     NULL,  // init
551     aead_des_ede3_cbc_sha1_tls_init,
552     aead_tls_cleanup,
553     aead_tls_open,
554     aead_tls_seal_scatter,
555     NULL,  // open_gather
556     NULL,  // get_iv
557     aead_tls_tag_len,
558 };
559 
560 static const EVP_AEAD aead_des_ede3_cbc_sha1_tls_implicit_iv = {
561     SHA_DIGEST_LENGTH + 24 + 8,  // key len (SHA1 + 3DES + IV)
562     0,                           // nonce len
563     8 + SHA_DIGEST_LENGTH,       // overhead (padding + SHA1)
564     SHA_DIGEST_LENGTH,           // max tag length
565     0,                           // seal_scatter_supports_extra_in
566 
567     NULL,  // init
568     aead_des_ede3_cbc_sha1_tls_implicit_iv_init,
569     aead_tls_cleanup,
570     aead_tls_open,
571     aead_tls_seal_scatter,
572     NULL,             // open_gather
573     aead_tls_get_iv,  // get_iv
574     aead_tls_tag_len,
575 };
576 
EVP_aead_aes_128_cbc_sha1_tls(void)577 const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls(void) {
578   return &aead_aes_128_cbc_sha1_tls;
579 }
580 
EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(void)581 const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(void) {
582   return &aead_aes_128_cbc_sha1_tls_implicit_iv;
583 }
584 
EVP_aead_aes_128_cbc_sha256_tls(void)585 const EVP_AEAD *EVP_aead_aes_128_cbc_sha256_tls(void) {
586   return &aead_aes_128_cbc_sha256_tls;
587 }
588 
EVP_aead_aes_256_cbc_sha1_tls(void)589 const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls(void) {
590   return &aead_aes_256_cbc_sha1_tls;
591 }
592 
EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(void)593 const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(void) {
594   return &aead_aes_256_cbc_sha1_tls_implicit_iv;
595 }
596 
EVP_aead_des_ede3_cbc_sha1_tls(void)597 const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls(void) {
598   return &aead_des_ede3_cbc_sha1_tls;
599 }
600 
EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(void)601 const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(void) {
602   return &aead_des_ede3_cbc_sha1_tls_implicit_iv;
603 }
604