Coverage Report

Created: 2026-08-28 07:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/cipher/e_tls.cc
Line
Count
Source
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
#include <openssl/span.h>
27
28
#include "../fipsmodule/cipher/internal.h"
29
#include "../internal.h"
30
#include "../mem_internal.h"
31
#include "internal.h"
32
33
34
using namespace bssl;
35
36
typedef struct {
37
  EVP_CIPHER_CTX cipher_ctx;
38
  HMAC_CTX *hmac_ctx;
39
  // mac_key is the portion of the key used for the MAC. It is retained
40
  // separately for the constant-time CBC code.
41
  uint8_t mac_key[EVP_MAX_MD_SIZE];
42
  uint8_t mac_key_len;
43
  // implicit_iv is one iff this is a pre-TLS-1.1 CBC cipher without an explicit
44
  // IV.
45
  char implicit_iv;
46
} AEAD_TLS_CTX;
47
48
static_assert(EVP_MAX_MD_SIZE < 256, "mac_key_len does not fit in uint8_t");
49
50
static_assert(sizeof(((EVP_AEAD_CTX *)nullptr)->state) >= sizeof(AEAD_TLS_CTX),
51
              "AEAD state is too small");
52
static_assert(alignof(union evp_aead_ctx_st_state) >= alignof(AEAD_TLS_CTX),
53
              "AEAD state has insufficient alignment");
54
55
27.2k
static void aead_tls_cleanup(EVP_AEAD_CTX *ctx) {
56
27.2k
  AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
57
27.2k
  EVP_CIPHER_CTX_cleanup(&tls_ctx->cipher_ctx);
58
27.2k
  HMAC_CTX_free(tls_ctx->hmac_ctx);
59
27.2k
}
60
61
static int aead_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len,
62
                         size_t tag_len, enum evp_aead_direction_t dir,
63
                         const EVP_CIPHER *cipher, const EVP_MD *md,
64
27.2k
                         char implicit_iv) {
65
27.2k
  if (tag_len != EVP_AEAD_DEFAULT_TAG_LENGTH && tag_len != EVP_MD_size(md)) {
66
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_TAG_SIZE);
67
0
    return 0;
68
0
  }
69
70
27.2k
  if (key_len != EVP_AEAD_key_length(ctx->aead)) {
71
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
72
0
    return 0;
73
0
  }
74
75
27.2k
  size_t mac_key_len = EVP_MD_size(md);
76
27.2k
  size_t enc_key_len = EVP_CIPHER_key_length(cipher);
77
27.2k
  assert(mac_key_len + enc_key_len +
78
27.2k
             (implicit_iv ? EVP_CIPHER_iv_length(cipher) : 0) ==
79
27.2k
         key_len);
80
81
27.2k
  AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
82
27.2k
  tls_ctx->hmac_ctx = HMAC_CTX_new();
83
27.2k
  if (!tls_ctx->hmac_ctx) {
84
0
    return 0;
85
0
  }
86
27.2k
  EVP_CIPHER_CTX_init(&tls_ctx->cipher_ctx);
87
27.2k
  assert(mac_key_len <= EVP_MAX_MD_SIZE);
88
27.2k
  OPENSSL_memcpy(tls_ctx->mac_key, key, mac_key_len);
89
27.2k
  tls_ctx->mac_key_len = (uint8_t)mac_key_len;
90
27.2k
  tls_ctx->implicit_iv = implicit_iv;
91
92
27.2k
  if (!EVP_CipherInit_ex(
93
27.2k
          &tls_ctx->cipher_ctx, cipher, nullptr, &key[mac_key_len],
94
27.2k
          implicit_iv ? &key[mac_key_len + enc_key_len] : nullptr,
95
27.2k
          dir == evp_aead_seal) ||
96
27.2k
      !HMAC_Init_ex(tls_ctx->hmac_ctx, key, mac_key_len, md, nullptr)) {
97
0
    aead_tls_cleanup(ctx);
98
0
    return 0;
99
0
  }
100
27.2k
  EVP_CIPHER_CTX_set_padding(&tls_ctx->cipher_ctx, 0);
101
102
27.2k
  return 1;
103
27.2k
}
104
105
8.54k
static size_t aead_tls_tag_len(const EVP_AEAD_CTX *ctx, const size_t in_len) {
106
8.54k
  const AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
107
8.54k
  assert(EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE);
108
109
8.54k
  const size_t hmac_len = HMAC_size(tls_ctx->hmac_ctx);
110
8.54k
  const size_t block_size = EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx);
111
  // An overflow of `in_len + hmac_len` doesn't affect the result mod
112
  // `block_size`, provided that `block_size` is a smaller power of two.
113
8.54k
  assert(block_size == 8 /*3DES*/ || block_size == 16 /*AES*/);
114
8.54k
  const size_t pad_len = block_size - ((in_len + hmac_len) & (block_size - 1));
115
8.54k
  return hmac_len + pad_len;
116
8.54k
}
117
118
static int aead_tls_sealv(const EVP_AEAD_CTX *ctx,
119
                          Span<const CRYPTO_IOVEC> iovecs,
120
                          Span<uint8_t> out_tag, size_t *out_tag_len,
121
                          Span<const uint8_t> nonce,
122
1.42k
                          Span<const CRYPTO_IVEC> aadvecs) {
123
1.42k
  AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
124
125
1.42k
  if (!tls_ctx->cipher_ctx.encrypt) {
126
    // Unlike a normal AEAD, a TLS AEAD may only be used in one direction.
127
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION);
128
0
    return 0;
129
0
  }
130
131
1.42k
  size_t in_len = bssl::iovec::TotalLength(iovecs);
132
1.42k
  if (out_tag.size() < aead_tls_tag_len(ctx, in_len)) {
133
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
134
0
    return 0;
135
0
  }
136
137
1.42k
  if (nonce.size() != EVP_AEAD_nonce_length(ctx->aead)) {
138
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
139
0
    return 0;
140
0
  }
141
142
1.42k
  size_t ad_len = bssl::iovec::TotalLength(aadvecs);
143
1.42k
  if (ad_len != 13 - 2 /* length bytes */) {
144
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE);
145
0
    return 0;
146
0
  }
147
148
  // To allow for CBC mode which changes cipher length, `ad` doesn't include the
149
  // length for legacy ciphers.
150
1.42k
  uint8_t ad_extra[2];
151
1.42k
  CRYPTO_store_u16_be(ad_extra, static_cast<uint16_t>(in_len));
152
153
  // Compute the MAC. This must be first in case the operation is being done
154
  // in-place.
155
1.42k
  uint8_t mac[EVP_MAX_MD_SIZE];
156
1.42k
  if (!HMAC_Init_ex(tls_ctx->hmac_ctx, nullptr, 0, nullptr, nullptr)) {
157
0
    return 0;
158
0
  }
159
1.42k
  for (const CRYPTO_IVEC &aadvec : aadvecs) {
160
1.42k
    if (!HMAC_Update(tls_ctx->hmac_ctx, aadvec.in, aadvec.len)) {
161
0
      return 0;
162
0
    }
163
1.42k
  }
164
1.42k
  if (!HMAC_Update(tls_ctx->hmac_ctx, ad_extra, sizeof(ad_extra))) {
165
0
    return 0;
166
0
  }
167
1.42k
  for (const CRYPTO_IOVEC &iovec : iovecs) {
168
1.42k
    if (!HMAC_Update(tls_ctx->hmac_ctx, iovec.in, iovec.len)) {
169
0
      return 0;
170
0
    }
171
1.42k
  }
172
1.42k
  unsigned mac_len;
173
1.42k
  if (!HMAC_Final(tls_ctx->hmac_ctx, mac, &mac_len)) {
174
0
    return 0;
175
0
  }
176
177
  // Configure the explicit IV.
178
1.42k
  assert(EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE);
179
1.42k
  if (!tls_ctx->implicit_iv &&
180
1.09k
      !EVP_EncryptInit_ex(&tls_ctx->cipher_ctx, nullptr, nullptr, nullptr,
181
1.09k
                          nonce.data())) {
182
0
    return 0;
183
0
  }
184
185
1.42k
  size_t block_size = EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx);
186
1.42k
  assert(block_size == 8 /*3DES*/ || block_size == 16 /*AES*/);
187
188
  // Encrypt the input.
189
1.42k
  size_t len = 0;
190
1.42k
  size_t tag_len = 0;
191
1.42k
  if (!bssl::iovec::ForEachBlockRange_Dynamic</*WriteOut=*/true>(
192
1.42k
          block_size, iovecs,
193
1.42k
          [&](const uint8_t *in, uint8_t *out, size_t chunk_len) {
194
            // Complete block(s).
195
0
            size_t out_len;
196
0
            if (!EVP_EncryptUpdate_ex(&tls_ctx->cipher_ctx, out, &out_len,
197
0
                                      chunk_len, in, chunk_len)) {
198
0
              return false;
199
0
            }
200
0
            assert(out_len == chunk_len);
201
0
            len += out_len;
202
0
            return true;
203
0
          },
204
1.42k
          [&](const uint8_t *in, uint8_t *out, size_t chunk_len) {
205
            // Final chunk, possibly with a partial block.
206
1.42k
            size_t out_len;
207
1.42k
            if (!EVP_EncryptUpdate_ex(&tls_ctx->cipher_ctx, out, &out_len,
208
1.42k
                                      chunk_len, in, chunk_len)) {
209
0
              return false;
210
0
            }
211
1.42k
            len += out_len;
212
1.42k
            size_t remaining = chunk_len - out_len;
213
1.42k
            assert(remaining < block_size);
214
1.42k
            if (remaining == 0) {
215
930
              return true;
216
930
            }
217
218
            // Feed the MAC into the cipher in two steps. First complete the
219
            // final partial block from encrypting the input and split the
220
            // result between `out` and `out_tag`. Then feed the rest.
221
494
            const size_t early_mac_len = block_size - remaining;
222
494
            assert(early_mac_len < block_size);
223
494
            assert(len + block_size - early_mac_len == in_len);
224
494
            uint8_t buf[EVP_MAX_BLOCK_LENGTH];
225
494
            size_t buf_len;
226
494
            if (!EVP_EncryptUpdate_ex(&tls_ctx->cipher_ctx, buf, &buf_len,
227
494
                                      sizeof(buf), mac, early_mac_len)) {
228
0
              return false;
229
0
            }
230
494
            assert(buf_len == block_size);
231
494
            OPENSSL_memcpy(out + out_len, buf, remaining);
232
494
            OPENSSL_memcpy(out_tag.data(), buf + remaining, early_mac_len);
233
494
            tag_len = early_mac_len;
234
494
            return true;
235
494
          })) {
236
0
    return 0;
237
0
  }
238
239
1.42k
  if (!EVP_EncryptUpdate_ex(&tls_ctx->cipher_ctx, out_tag.data() + tag_len,
240
1.42k
                            &len, out_tag.size() - tag_len, mac + tag_len,
241
1.42k
                            mac_len - tag_len)) {
242
0
    return 0;
243
0
  }
244
1.42k
  tag_len += len;
245
246
  // Compute padding and feed that into the cipher.
247
1.42k
  uint8_t padding[256];
248
1.42k
  unsigned padding_len = block_size - ((in_len + mac_len) & (block_size - 1));
249
1.42k
  OPENSSL_memset(padding, padding_len - 1, padding_len);
250
1.42k
  if (!EVP_EncryptUpdate_ex(&tls_ctx->cipher_ctx, out_tag.data() + tag_len,
251
1.42k
                            &len, out_tag.size() - tag_len, padding,
252
1.42k
                            padding_len)) {
253
0
    return 0;
254
0
  }
255
1.42k
  tag_len += len;
256
257
1.42k
  if (!EVP_EncryptFinal_ex2(&tls_ctx->cipher_ctx, out_tag.data() + tag_len,
258
1.42k
                            &len, out_tag.size() - tag_len)) {
259
0
    return 0;
260
0
  }
261
1.42k
  assert(len == 0);  // Padding is explicit.
262
1.42k
  assert(tag_len == aead_tls_tag_len(ctx, in_len));
263
264
1.42k
  *out_tag_len = tag_len;
265
1.42k
  return 1;
266
1.42k
}
267
268
static int aead_tls_openv(const EVP_AEAD_CTX *ctx,
269
                          Span<const CRYPTO_IOVEC> iovecs,
270
                          size_t *out_total_bytes, Span<const uint8_t> nonce,
271
3.66k
                          Span<const CRYPTO_IVEC> aadvecs) {
272
3.66k
  AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
273
274
3.66k
  if (tls_ctx->cipher_ctx.encrypt) {
275
    // Unlike a normal AEAD, a TLS AEAD may only be used in one direction.
276
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION);
277
0
    return 0;
278
0
  }
279
280
3.66k
  size_t in_len = bssl::iovec::TotalLength(iovecs);
281
3.66k
  if (in_len < HMAC_size(tls_ctx->hmac_ctx)) {
282
43
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
283
43
    return 0;
284
43
  }
285
286
3.62k
  if (nonce.size() != EVP_AEAD_nonce_length(ctx->aead)) {
287
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
288
0
    return 0;
289
0
  }
290
291
3.62k
  size_t ad_len = bssl::iovec::TotalLength(aadvecs);
292
3.62k
  if (ad_len != 13 - 2 /* length bytes */) {
293
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE);
294
0
    return 0;
295
0
  }
296
297
  // Configure the explicit IV.
298
3.62k
  assert(EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE);
299
3.62k
  if (!tls_ctx->implicit_iv &&
300
685
      !EVP_DecryptInit_ex(&tls_ctx->cipher_ctx, nullptr, nullptr, nullptr,
301
685
                          nonce.data())) {
302
0
    return 0;
303
0
  }
304
305
  // Decrypt to get the plaintext + MAC + padding.
306
3.62k
  size_t total = 0;
307
3.62k
  size_t block_size = EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx);
308
3.62k
  auto decrypt_update = [&](const uint8_t *in, uint8_t *out, size_t len) {
309
3.62k
    size_t out_len;
310
3.62k
    if (!EVP_DecryptUpdate_ex(&tls_ctx->cipher_ctx, out, &out_len, len, in,
311
3.62k
                              len)) {
312
0
      return false;
313
0
    }
314
3.62k
    CONSTTIME_SECRET(out, out_len);
315
3.62k
    if (out_len != len) {
316
      // A byte sequence that was not a multiple of the block size was provided
317
      // as ciphertext. This is generally invalid and thus should be rejected.
318
74
      OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
319
74
      return false;
320
74
    }
321
3.55k
    total += len;
322
3.55k
    return true;
323
3.62k
  };
324
3.62k
  if (!bssl::iovec::ForEachBlockRange_Dynamic</*WriteOut=*/true>(
325
3.62k
          block_size, iovecs, decrypt_update, decrypt_update)) {
326
74
    return false;
327
74
  }
328
3.62k
  assert(total == in_len);
329
330
3.55k
  const size_t mac_len = HMAC_size(tls_ctx->hmac_ctx);
331
332
  // Split the decrypted record into `iovecs_without_trailer` and `trailer`,
333
  // based on the public lower bound of where the plaintext ends. The plaintext
334
  // is followed by `mac_len` and then at most 256 bytes of padding.
335
3.55k
  bssl::iovec::MaybeInplaceArray iovecs_without_trailer;
336
3.55k
  if (!iovecs_without_trailer.CopyFrom(iovecs)) {
337
0
    return 0;
338
0
  }
339
3.55k
  uint8_t trailer_buf[EVP_MAX_MD_SIZE + 256];
340
3.55k
  const size_t trailer_len = std::min(in_len, mac_len + 256);
341
3.55k
  std::optional<Span<const uint8_t>> trailer =
342
3.55k
      bssl::iovec::GetAndRemoveOutSuffix(Span(trailer_buf).first(trailer_len),
343
3.55k
                                         Span(iovecs_without_trailer));
344
3.55k
  BSSL_CHECK(trailer.has_value());
345
346
  // Remove CBC padding. Code from here on is timing-sensitive with respect to
347
  // `padding_ok`, `trailer_minus_padding`, and derived values.
348
3.55k
  crypto_word_t padding_ok;
349
3.55k
  size_t trailer_minus_padding;
350
3.55k
  if (!EVP_tls_cbc_remove_padding(&padding_ok, &trailer_minus_padding,
351
3.55k
                                  trailer->data(), trailer->size(), block_size,
352
3.55k
                                  mac_len)) {
353
    // Publicly invalid. This can be rejected in non-constant time.
354
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
355
0
    return 0;
356
0
  }
357
358
  // If the padding is valid, `trailer->first(trailer_minus_padding)` is the
359
  // last bytes of plaintext and the MAC. Otherwise, it is still large enough to
360
  // extract a MAC, but it will be irrelevant. Note that `trailer_minus_padding`
361
  // is secret.
362
3.55k
  declassify_assert(trailer_minus_padding >= mac_len);
363
3.55k
  size_t data_in_trailer_len = trailer_minus_padding - mac_len;
364
3.55k
  size_t max_data_in_trailer_len = trailer->size() - mac_len;
365
3.55k
  size_t data_len = total - trailer->size() + data_in_trailer_len;
366
367
  // To allow for CBC mode which changes cipher length, `ad_len` doesn't
368
  // include the length for legacy ciphers.
369
3.55k
  uint8_t ad_extra[2];
370
3.55k
  CRYPTO_store_u16_be(ad_extra, static_cast<uint16_t>(data_len));
371
372
  // Compute the MAC and extract the one in the record.
373
3.55k
  uint8_t mac[EVP_MAX_MD_SIZE];
374
3.55k
  size_t got_mac_len;
375
3.55k
  assert(EVP_tls_cbc_record_digest_supported(tls_ctx->hmac_ctx->md));
376
3.55k
  if (!EVP_tls_cbc_digest_record(
377
3.55k
          tls_ctx->hmac_ctx->md, mac, &got_mac_len, ad_extra, aadvecs,
378
3.55k
          iovecs_without_trailer, trailer->first(max_data_in_trailer_len),
379
3.55k
          data_in_trailer_len, tls_ctx->mac_key, tls_ctx->mac_key_len)) {
380
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
381
0
    return 0;
382
0
  }
383
3.55k
  assert(got_mac_len == mac_len);
384
385
3.55k
  uint8_t record_mac[EVP_MAX_MD_SIZE];
386
3.55k
  EVP_tls_cbc_copy_mac(record_mac, mac_len, trailer->data(),
387
3.55k
                       trailer_minus_padding, trailer->size());
388
389
  // Perform the MAC check and the padding check in constant-time. It should be
390
  // safe to simply perform the padding check first, but it would not be under a
391
  // different choice of MAC location on padding failure. See
392
  // EVP_tls_cbc_remove_padding. The value barrier seems to be necessary to
393
  // prevent a branch in Clang.
394
3.55k
  crypto_word_t good = value_barrier_w(
395
3.55k
      constant_time_eq_int(CRYPTO_memcmp(record_mac, mac, mac_len), 0));
396
3.55k
  good &= padding_ok;
397
3.55k
  if (!constant_time_declassify_w(good)) {
398
232
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
399
232
    return 0;
400
232
  }
401
402
  // End of timing-sensitive code.
403
3.31k
  CONSTTIME_DECLASSIFY(&data_len, sizeof(data_len));
404
3.31k
  for (const CRYPTO_IOVEC &iovec : iovecs) {
405
3.31k
    CONSTTIME_DECLASSIFY(iovec.out, iovec.len);
406
3.31k
  }
407
408
3.31k
  *out_total_bytes = data_len;
409
3.31k
  return 1;
410
3.55k
}
411
412
static int aead_aes_128_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
413
                                          size_t key_len, size_t tag_len,
414
2.68k
                                          enum evp_aead_direction_t dir) {
415
2.68k
  return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
416
2.68k
                       EVP_sha1(), 0);
417
2.68k
}
418
419
static int aead_aes_128_cbc_sha1_tls_implicit_iv_init(
420
    EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
421
713
    enum evp_aead_direction_t dir) {
422
713
  return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
423
713
                       EVP_sha1(), 1);
424
713
}
425
426
static int aead_aes_128_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx,
427
                                            const uint8_t *key, size_t key_len,
428
                                            size_t tag_len,
429
0
                                            enum evp_aead_direction_t dir) {
430
0
  return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
431
0
                       EVP_sha256(), 0);
432
0
}
433
434
static int aead_aes_256_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
435
                                          size_t key_len, size_t tag_len,
436
18.3k
                                          enum evp_aead_direction_t dir) {
437
18.3k
  return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
438
18.3k
                       EVP_sha1(), 0);
439
18.3k
}
440
441
static int aead_aes_256_cbc_sha1_tls_implicit_iv_init(
442
    EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
443
1.67k
    enum evp_aead_direction_t dir) {
444
1.67k
  return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
445
1.67k
                       EVP_sha1(), 1);
446
1.67k
}
447
448
static int aead_des_ede3_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx,
449
                                           const uint8_t *key, size_t key_len,
450
                                           size_t tag_len,
451
3.76k
                                           enum evp_aead_direction_t dir) {
452
3.76k
  return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
453
3.76k
                       EVP_sha1(), 0);
454
3.76k
}
455
456
static int aead_des_ede3_cbc_sha1_tls_implicit_iv_init(
457
    EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
458
137
    enum evp_aead_direction_t dir) {
459
137
  return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
460
137
                       EVP_sha1(), 1);
461
137
}
462
463
static int aead_tls_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv,
464
0
                           size_t *out_iv_len) {
465
0
  const AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
466
0
  const size_t iv_len = EVP_CIPHER_CTX_iv_length(&tls_ctx->cipher_ctx);
467
0
  if (iv_len <= 1) {
468
0
    OPENSSL_PUT_ERROR(CIPHER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
469
0
    return 0;
470
0
  }
471
472
0
  *out_iv = tls_ctx->cipher_ctx.iv;
473
0
  *out_iv_len = iv_len;
474
0
  return 1;
475
0
}
476
477
static const EVP_AEAD aead_aes_128_cbc_sha1_tls = {
478
    SHA_DIGEST_LENGTH + 16,  // key len (SHA1 + AES128)
479
    16,                      // nonce len (IV)
480
    16 + SHA_DIGEST_LENGTH,  // overhead (padding + SHA1)
481
    SHA_DIGEST_LENGTH,       // max tag length
482
483
    nullptr,  // init
484
    aead_aes_128_cbc_sha1_tls_init,
485
    aead_tls_cleanup,
486
    aead_tls_openv,
487
    aead_tls_sealv,
488
    nullptr,  // openv_detached
489
    nullptr,  // get_iv
490
    aead_tls_tag_len,
491
};
492
493
static const EVP_AEAD aead_aes_128_cbc_sha1_tls_implicit_iv = {
494
    SHA_DIGEST_LENGTH + 16 + 16,  // key len (SHA1 + AES128 + IV)
495
    0,                            // nonce len
496
    16 + SHA_DIGEST_LENGTH,       // overhead (padding + SHA1)
497
    SHA_DIGEST_LENGTH,            // max tag length
498
499
    nullptr,  // init
500
    aead_aes_128_cbc_sha1_tls_implicit_iv_init,
501
    aead_tls_cleanup,
502
    aead_tls_openv,
503
    aead_tls_sealv,
504
    nullptr,          // openv_detached
505
    aead_tls_get_iv,  // get_iv
506
    aead_tls_tag_len,
507
};
508
509
static const EVP_AEAD aead_aes_128_cbc_sha256_tls = {
510
    SHA256_DIGEST_LENGTH + 16,  // key len (SHA256 + AES128)
511
    16,                         // nonce len (IV)
512
    16 + SHA256_DIGEST_LENGTH,  // overhead (padding + SHA256)
513
    SHA256_DIGEST_LENGTH,       // max tag length
514
515
    nullptr,  // init
516
    aead_aes_128_cbc_sha256_tls_init,
517
    aead_tls_cleanup,
518
    aead_tls_openv,
519
    aead_tls_sealv,
520
    nullptr,  // openv_detached
521
    nullptr,  // 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
531
    nullptr,  // init
532
    aead_aes_256_cbc_sha1_tls_init,
533
    aead_tls_cleanup,
534
    aead_tls_openv,
535
    aead_tls_sealv,
536
    nullptr,  // openv_detached
537
    nullptr,  // get_iv
538
    aead_tls_tag_len,
539
};
540
541
static const EVP_AEAD aead_aes_256_cbc_sha1_tls_implicit_iv = {
542
    SHA_DIGEST_LENGTH + 32 + 16,  // key len (SHA1 + AES256 + IV)
543
    0,                            // nonce len
544
    16 + SHA_DIGEST_LENGTH,       // overhead (padding + SHA1)
545
    SHA_DIGEST_LENGTH,            // max tag length
546
547
    nullptr,  // init
548
    aead_aes_256_cbc_sha1_tls_implicit_iv_init,
549
    aead_tls_cleanup,
550
    aead_tls_openv,
551
    aead_tls_sealv,
552
    nullptr,          // openv_detached
553
    aead_tls_get_iv,  // get_iv
554
    aead_tls_tag_len,
555
};
556
557
static const EVP_AEAD aead_des_ede3_cbc_sha1_tls = {
558
    SHA_DIGEST_LENGTH + 24,  // key len (SHA1 + 3DES)
559
    8,                       // nonce len (IV)
560
    8 + SHA_DIGEST_LENGTH,   // overhead (padding + SHA1)
561
    SHA_DIGEST_LENGTH,       // max tag length
562
563
    nullptr,  // init
564
    aead_des_ede3_cbc_sha1_tls_init,
565
    aead_tls_cleanup,
566
    aead_tls_openv,
567
    aead_tls_sealv,
568
    nullptr,  // openv_detached
569
    nullptr,  // get_iv
570
    aead_tls_tag_len,
571
};
572
573
static const EVP_AEAD aead_des_ede3_cbc_sha1_tls_implicit_iv = {
574
    SHA_DIGEST_LENGTH + 24 + 8,  // key len (SHA1 + 3DES + IV)
575
    0,                           // nonce len
576
    8 + SHA_DIGEST_LENGTH,       // overhead (padding + SHA1)
577
    SHA_DIGEST_LENGTH,           // max tag length
578
579
    nullptr,  // init
580
    aead_des_ede3_cbc_sha1_tls_implicit_iv_init,
581
    aead_tls_cleanup,
582
    aead_tls_openv,
583
    aead_tls_sealv,
584
    nullptr,          // openv_detached
585
    aead_tls_get_iv,  // get_iv
586
    aead_tls_tag_len,
587
};
588
589
5.36k
const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls() {
590
5.36k
  return &aead_aes_128_cbc_sha1_tls;
591
5.36k
}
592
593
1.42k
const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls_implicit_iv() {
594
1.42k
  return &aead_aes_128_cbc_sha1_tls_implicit_iv;
595
1.42k
}
596
597
0
const EVP_AEAD *EVP_aead_aes_128_cbc_sha256_tls() {
598
0
  return &aead_aes_128_cbc_sha256_tls;
599
0
}
600
601
36.6k
const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls() {
602
36.6k
  return &aead_aes_256_cbc_sha1_tls;
603
36.6k
}
604
605
3.35k
const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls_implicit_iv() {
606
3.35k
  return &aead_aes_256_cbc_sha1_tls_implicit_iv;
607
3.35k
}
608
609
7.53k
const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls() {
610
7.53k
  return &aead_des_ede3_cbc_sha1_tls;
611
7.53k
}
612
613
274
const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv() {
614
274
  return &aead_des_ede3_cbc_sha1_tls_implicit_iv;
615
274
}