xref: /aosp_15_r20/external/boringssl/src/crypto/cipher_extra/e_tls.c (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
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_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)403 static int aead_aes_128_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx,
404                                             const uint8_t *key, size_t key_len,
405                                             size_t tag_len,
406                                             enum evp_aead_direction_t dir) {
407   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
408                        EVP_sha256(), 0);
409 }
410 
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)411 static int aead_aes_256_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
412                                           size_t key_len, size_t tag_len,
413                                           enum evp_aead_direction_t dir) {
414   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
415                        EVP_sha1(), 0);
416 }
417 
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)418 static int aead_aes_256_cbc_sha1_tls_implicit_iv_init(
419     EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
420     enum evp_aead_direction_t dir) {
421   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
422                        EVP_sha1(), 1);
423 }
424 
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)425 static int aead_des_ede3_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx,
426                                            const uint8_t *key, size_t key_len,
427                                            size_t tag_len,
428                                            enum evp_aead_direction_t dir) {
429   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
430                        EVP_sha1(), 0);
431 }
432 
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)433 static int aead_des_ede3_cbc_sha1_tls_implicit_iv_init(
434     EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
435     enum evp_aead_direction_t dir) {
436   return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
437                        EVP_sha1(), 1);
438 }
439 
aead_tls_get_iv(const EVP_AEAD_CTX * ctx,const uint8_t ** out_iv,size_t * out_iv_len)440 static int aead_tls_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv,
441                            size_t *out_iv_len) {
442   const AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
443   const size_t iv_len = EVP_CIPHER_CTX_iv_length(&tls_ctx->cipher_ctx);
444   if (iv_len <= 1) {
445     return 0;
446   }
447 
448   *out_iv = tls_ctx->cipher_ctx.iv;
449   *out_iv_len = iv_len;
450   return 1;
451 }
452 
453 static const EVP_AEAD aead_aes_128_cbc_sha1_tls = {
454     SHA_DIGEST_LENGTH + 16,  // key len (SHA1 + AES128)
455     16,                      // nonce len (IV)
456     16 + SHA_DIGEST_LENGTH,  // overhead (padding + SHA1)
457     SHA_DIGEST_LENGTH,       // max tag length
458     0,                       // seal_scatter_supports_extra_in
459 
460     NULL,  // init
461     aead_aes_128_cbc_sha1_tls_init,
462     aead_tls_cleanup,
463     aead_tls_open,
464     aead_tls_seal_scatter,
465     NULL,  // open_gather
466     NULL,  // get_iv
467     aead_tls_tag_len,
468 };
469 
470 static const EVP_AEAD aead_aes_128_cbc_sha1_tls_implicit_iv = {
471     SHA_DIGEST_LENGTH + 16 + 16,  // key len (SHA1 + AES128 + IV)
472     0,                            // nonce len
473     16 + SHA_DIGEST_LENGTH,       // overhead (padding + SHA1)
474     SHA_DIGEST_LENGTH,            // max tag length
475     0,                            // seal_scatter_supports_extra_in
476 
477     NULL,  // init
478     aead_aes_128_cbc_sha1_tls_implicit_iv_init,
479     aead_tls_cleanup,
480     aead_tls_open,
481     aead_tls_seal_scatter,
482     NULL,             // open_gather
483     aead_tls_get_iv,  // get_iv
484     aead_tls_tag_len,
485 };
486 
487 static const EVP_AEAD aead_aes_128_cbc_sha256_tls = {
488     SHA256_DIGEST_LENGTH + 16,  // key len (SHA256 + AES128)
489     16,                         // nonce len (IV)
490     16 + SHA256_DIGEST_LENGTH,  // overhead (padding + SHA256)
491     SHA256_DIGEST_LENGTH,       // max tag length
492     0,                          // seal_scatter_supports_extra_in
493 
494     NULL,  // init
495     aead_aes_128_cbc_sha256_tls_init,
496     aead_tls_cleanup,
497     aead_tls_open,
498     aead_tls_seal_scatter,
499     NULL,  // open_gather
500     NULL,  // get_iv
501     aead_tls_tag_len,
502 };
503 
504 static const EVP_AEAD aead_aes_256_cbc_sha1_tls = {
505     SHA_DIGEST_LENGTH + 32,  // key len (SHA1 + AES256)
506     16,                      // nonce len (IV)
507     16 + SHA_DIGEST_LENGTH,  // overhead (padding + SHA1)
508     SHA_DIGEST_LENGTH,       // max tag length
509     0,                       // seal_scatter_supports_extra_in
510 
511     NULL,  // init
512     aead_aes_256_cbc_sha1_tls_init,
513     aead_tls_cleanup,
514     aead_tls_open,
515     aead_tls_seal_scatter,
516     NULL,  // open_gather
517     NULL,  // get_iv
518     aead_tls_tag_len,
519 };
520 
521 static const EVP_AEAD aead_aes_256_cbc_sha1_tls_implicit_iv = {
522     SHA_DIGEST_LENGTH + 32 + 16,  // key len (SHA1 + AES256 + IV)
523     0,                            // nonce len
524     16 + SHA_DIGEST_LENGTH,       // overhead (padding + SHA1)
525     SHA_DIGEST_LENGTH,            // max tag length
526     0,                            // seal_scatter_supports_extra_in
527 
528     NULL,  // init
529     aead_aes_256_cbc_sha1_tls_implicit_iv_init,
530     aead_tls_cleanup,
531     aead_tls_open,
532     aead_tls_seal_scatter,
533     NULL,             // open_gather
534     aead_tls_get_iv,  // get_iv
535     aead_tls_tag_len,
536 };
537 
538 static const EVP_AEAD aead_des_ede3_cbc_sha1_tls = {
539     SHA_DIGEST_LENGTH + 24,  // key len (SHA1 + 3DES)
540     8,                       // nonce len (IV)
541     8 + SHA_DIGEST_LENGTH,   // overhead (padding + SHA1)
542     SHA_DIGEST_LENGTH,       // max tag length
543     0,                       // seal_scatter_supports_extra_in
544 
545     NULL,  // init
546     aead_des_ede3_cbc_sha1_tls_init,
547     aead_tls_cleanup,
548     aead_tls_open,
549     aead_tls_seal_scatter,
550     NULL,  // open_gather
551     NULL,  // get_iv
552     aead_tls_tag_len,
553 };
554 
555 static const EVP_AEAD aead_des_ede3_cbc_sha1_tls_implicit_iv = {
556     SHA_DIGEST_LENGTH + 24 + 8,  // key len (SHA1 + 3DES + IV)
557     0,                           // nonce len
558     8 + SHA_DIGEST_LENGTH,       // overhead (padding + SHA1)
559     SHA_DIGEST_LENGTH,           // max tag length
560     0,                           // seal_scatter_supports_extra_in
561 
562     NULL,  // init
563     aead_des_ede3_cbc_sha1_tls_implicit_iv_init,
564     aead_tls_cleanup,
565     aead_tls_open,
566     aead_tls_seal_scatter,
567     NULL,             // open_gather
568     aead_tls_get_iv,  // get_iv
569     aead_tls_tag_len,
570 };
571 
EVP_aead_aes_128_cbc_sha1_tls(void)572 const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls(void) {
573   return &aead_aes_128_cbc_sha1_tls;
574 }
575 
EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(void)576 const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(void) {
577   return &aead_aes_128_cbc_sha1_tls_implicit_iv;
578 }
579 
EVP_aead_aes_128_cbc_sha256_tls(void)580 const EVP_AEAD *EVP_aead_aes_128_cbc_sha256_tls(void) {
581   return &aead_aes_128_cbc_sha256_tls;
582 }
583 
EVP_aead_aes_256_cbc_sha1_tls(void)584 const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls(void) {
585   return &aead_aes_256_cbc_sha1_tls;
586 }
587 
EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(void)588 const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(void) {
589   return &aead_aes_256_cbc_sha1_tls_implicit_iv;
590 }
591 
EVP_aead_des_ede3_cbc_sha1_tls(void)592 const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls(void) {
593   return &aead_des_ede3_cbc_sha1_tls;
594 }
595 
EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(void)596 const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(void) {
597   return &aead_des_ede3_cbc_sha1_tls_implicit_iv;
598 }
599