Coverage Report

Created: 2026-10-31 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/cipher/tls_cbc.cc
Line
Count
Source
1
// Copyright 2012-2016 The OpenSSL Project Authors. All Rights Reserved.
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 <string.h>
17
18
#include <openssl/digest.h>
19
#include <openssl/nid.h>
20
#include <openssl/sha.h>
21
#include <openssl/span.h>
22
23
#include "../fipsmodule/cipher/internal.h"
24
#include "../internal.h"
25
#include "internal.h"
26
27
28
using namespace bssl;
29
30
int bssl::EVP_tls_cbc_remove_padding(crypto_word_t *out_padding_ok,
31
                                     size_t *out_len, const uint8_t *in,
32
                                     size_t in_len, size_t block_size,
33
4.41k
                                     size_t mac_size) {
34
4.41k
  const size_t overhead = 1 /* padding length byte */ + mac_size;
35
36
  // These lengths are all public so we can test them in non-constant time.
37
4.41k
  if (overhead > in_len) {
38
0
    return 0;
39
0
  }
40
41
4.41k
  size_t padding_length = in[in_len - 1];
42
43
4.41k
  crypto_word_t good = constant_time_ge_w(in_len, overhead + padding_length);
44
  // The padding consists of a length byte at the end of the record and
45
  // then that many bytes of padding, all with the same value as the
46
  // length byte. Thus, with the length byte included, there are i+1
47
  // bytes of padding.
48
  //
49
  // We can't check just `padding_length+1` bytes because that leaks
50
  // decrypted information. Therefore we always have to check the maximum
51
  // amount of padding possible. (Again, the length of the record is
52
  // public information so we can use it.)
53
4.41k
  size_t to_check = 256;  // maximum amount of padding, inc length byte.
54
4.41k
  if (to_check > in_len) {
55
4.33k
    to_check = in_len;
56
4.33k
  }
57
58
173k
  for (size_t i = 0; i < to_check; i++) {
59
168k
    uint8_t mask = constant_time_ge_8(padding_length, i);
60
    // The value barrier on `(in_len - 1 - i)` isn't needed to enforce
61
    // constant-time. It is just there to prevent a false positive in
62
    // constant-time checks by valgrind.
63
168k
    uint8_t b = in[value_barrier_w(in_len - 1 - i)];
64
    // The final `padding_length+1` bytes should all have the value
65
    // `padding_length`. Therefore the XOR should be zero.
66
168k
    good &= ~(mask & (padding_length ^ b));
67
168k
  }
68
69
  // If any of the final `padding_length+1` bytes had the wrong value,
70
  // one or more of the lower eight bits of `good` will be cleared.
71
4.41k
  good = constant_time_eq_w(0xff, good & 0xff);
72
73
  // Always treat `padding_length` as zero on error. If, assuming block size of
74
  // 16, a padding of [<15 arbitrary bytes> 15] treated `padding_length` as 16
75
  // and returned -1, distinguishing good MAC and bad padding from bad MAC and
76
  // bad padding would give POODLE's padding oracle.
77
4.41k
  padding_length = good & (padding_length + 1);
78
4.41k
  *out_len = in_len - padding_length;
79
4.41k
  *out_padding_ok = good;
80
4.41k
  return 1;
81
4.41k
}
82
83
void bssl::EVP_tls_cbc_copy_mac(uint8_t *out, size_t md_size, const uint8_t *in,
84
4.41k
                                size_t in_len, size_t orig_len) {
85
4.41k
  uint8_t rotated_mac1[EVP_MAX_MD_SIZE], rotated_mac2[EVP_MAX_MD_SIZE];
86
4.41k
  uint8_t *rotated_mac = rotated_mac1;
87
4.41k
  uint8_t *rotated_mac_tmp = rotated_mac2;
88
89
  // mac_end is the index of `in` just after the end of the MAC.
90
4.41k
  size_t mac_end = in_len;
91
4.41k
  size_t mac_start = mac_end - md_size;
92
93
4.41k
  declassify_assert(orig_len >= in_len);
94
4.41k
  declassify_assert(in_len >= md_size);
95
4.41k
  assert(md_size <= EVP_MAX_MD_SIZE);
96
4.41k
  assert(md_size > 0);
97
98
  // scan_start contains the number of bytes that we can ignore because
99
  // the MAC's position can only vary by 255 bytes.
100
4.41k
  size_t scan_start = 0;
101
  // This information is public so it's safe to branch based on it.
102
4.41k
  if (orig_len > md_size + 255 + 1) {
103
0
    scan_start = orig_len - (md_size + 255 + 1);
104
0
  }
105
106
4.41k
  size_t rotate_offset = 0;
107
4.41k
  uint8_t mac_started = 0;
108
4.41k
  OPENSSL_memset(rotated_mac, 0, md_size);
109
174k
  for (size_t i = scan_start, j = 0; i < orig_len; i++, j++) {
110
170k
    if (j >= md_size) {
111
5.85k
      j -= md_size;
112
5.85k
    }
113
170k
    crypto_word_t is_mac_start = constant_time_eq_w(i, mac_start);
114
170k
    mac_started |= is_mac_start;
115
170k
    uint8_t mac_ended = constant_time_ge_8(i, mac_end);
116
170k
    rotated_mac[j] |= in[i] & mac_started & ~mac_ended;
117
    // Save the offset that `mac_start` is mapped to.
118
170k
    rotate_offset |= j & is_mac_start;
119
170k
  }
120
121
  // Now rotate the MAC. We rotate in log(md_size) steps, one for each bit
122
  // position.
123
26.5k
  for (size_t offset = 1; offset < md_size; offset <<= 1, rotate_offset >>= 1) {
124
    // Rotate by `offset` iff the corresponding bit is set in
125
    // `rotate_offset`, placing the result in `rotated_mac_tmp`.
126
22.0k
    const uint8_t skip_rotate = (rotate_offset & 1) - 1;
127
463k
    for (size_t i = 0, j = offset; i < md_size; i++, j++) {
128
441k
      if (j >= md_size) {
129
22.0k
        j -= md_size;
130
22.0k
      }
131
441k
      rotated_mac_tmp[i] =
132
441k
          constant_time_select_8(skip_rotate, rotated_mac[i], rotated_mac[j]);
133
441k
    }
134
135
    // Swap pointers so `rotated_mac` contains the (possibly) rotated value.
136
    // Note the number of iterations and thus the identity of these pointers is
137
    // public information.
138
22.0k
    uint8_t *tmp = rotated_mac;
139
22.0k
    rotated_mac = rotated_mac_tmp;
140
22.0k
    rotated_mac_tmp = tmp;
141
22.0k
  }
142
143
4.41k
  OPENSSL_memcpy(out, rotated_mac, md_size);
144
4.41k
}
145
146
int bssl::EVP_sha1_final_with_secret_suffix(SHA_CTX *ctx,
147
                                            uint8_t out[SHA_DIGEST_LENGTH],
148
                                            const uint8_t *in, size_t len,
149
4.41k
                                            size_t max_len) {
150
  // Bound the input length so `total_bits` below fits in four bytes. This is
151
  // redundant with TLS record size limits. This also ensures `input_idx` below
152
  // does not overflow.
153
4.41k
  size_t max_len_bits = max_len << 3;
154
4.41k
  if (ctx->Nh != 0 ||
155
4.41k
      (max_len_bits >> 3) != max_len ||  // Overflow
156
4.41k
      ctx->Nl + max_len_bits < max_len_bits ||
157
4.41k
      ctx->Nl + max_len_bits > UINT32_MAX) {
158
0
    return 0;
159
0
  }
160
161
  // We need to hash the following into `ctx`:
162
  //
163
  // - ctx->data[:ctx->num]
164
  // - in[:len]
165
  // - A 0x80 byte
166
  // - However many zero bytes are needed to pad up to a block.
167
  // - Eight bytes of length.
168
4.41k
  size_t num_blocks = (ctx->num + len + 1 + 8 + SHA_CBLOCK - 1) >> 6;
169
4.41k
  size_t last_block = num_blocks - 1;
170
4.41k
  size_t max_blocks = (ctx->num + max_len + 1 + 8 + SHA_CBLOCK - 1) >> 6;
171
172
  // The bounds above imply `total_bits` fits in four bytes.
173
4.41k
  size_t total_bits = ctx->Nl + (len << 3);
174
4.41k
  uint8_t length_bytes[4];
175
4.41k
  CRYPTO_store_u32_be(length_bytes, total_bits);
176
177
  // We now construct and process each expected block in constant-time.
178
4.41k
  uint8_t block[SHA_CBLOCK] = {0};
179
4.41k
  uint32_t result[5] = {0};
180
  // input_idx is the index into `in` corresponding to the current block.
181
  // However, we allow this index to overflow beyond `max_len`, to simplify the
182
  // 0x80 byte.
183
4.41k
  size_t input_idx = 0;
184
9.33k
  for (size_t i = 0; i < max_blocks; i++) {
185
    // Fill `block` with data from the partial block in `ctx` and `in`. We copy
186
    // as if we were hashing up to `max_len` and then zero the excess later.
187
4.91k
    size_t block_start = 0;
188
4.91k
    if (i == 0) {
189
4.41k
      OPENSSL_memcpy(block, ctx->data, ctx->num);
190
4.41k
      block_start = ctx->num;
191
4.41k
    }
192
4.91k
    if (input_idx < max_len) {
193
4.85k
      size_t to_copy = SHA_CBLOCK - block_start;
194
4.85k
      if (to_copy > max_len - input_idx) {
195
4.41k
        to_copy = max_len - input_idx;
196
4.41k
      }
197
4.85k
      OPENSSL_memcpy(block + block_start, in + input_idx, to_copy);
198
4.85k
    }
199
200
    // Zero any bytes beyond `len` and add the 0x80 byte.
201
260k
    for (size_t j = block_start; j < SHA_CBLOCK; j++) {
202
      // input[idx] corresponds to block[j].
203
255k
      size_t idx = input_idx + j - block_start;
204
      // The barriers on `len` are not strictly necessary. However, without
205
      // them, GCC compiles this code by incorporating `len` into the loop
206
      // counter and subtracting it out later. This is still constant-time, but
207
      // it frustrates attempts to validate this.
208
255k
      uint8_t is_in_bounds = constant_time_lt_8(idx, value_barrier_w(len));
209
255k
      uint8_t is_padding_byte = constant_time_eq_8(idx, value_barrier_w(len));
210
255k
      block[j] &= is_in_bounds;
211
255k
      block[j] |= 0x80 & is_padding_byte;
212
255k
    }
213
214
4.91k
    input_idx += SHA_CBLOCK - block_start;
215
216
    // Fill in the length if this is the last block. Use a value barrier to
217
    // prevent Clang from compiling the conditional select as a jump.
218
4.91k
    crypto_word_t is_last_block =
219
4.91k
        value_barrier_w(constant_time_eq_w(i, last_block));
220
24.5k
    for (size_t j = 0; j < 4; j++) {
221
19.6k
      block[SHA_CBLOCK - 4 + j] |= is_last_block & length_bytes[j];
222
19.6k
    }
223
224
    // Process the block and save the hash state if it is the final value.
225
4.91k
    SHA1_Transform(ctx, block);
226
29.5k
    for (size_t j = 0; j < 5; j++) {
227
24.5k
      result[j] |= is_last_block & ctx->h[j];
228
24.5k
    }
229
4.91k
  }
230
231
  // Write the output.
232
26.5k
  for (size_t i = 0; i < 5; i++) {
233
22.0k
    CRYPTO_store_u32_be(out + 4 * i, result[i]);
234
22.0k
  }
235
4.41k
  return 1;
236
4.41k
}
237
238
int bssl::EVP_sha256_final_with_secret_suffix(SHA256_CTX *ctx,
239
                                              uint8_t out[SHA256_DIGEST_LENGTH],
240
                                              const uint8_t *in, size_t len,
241
0
                                              size_t max_len) {
242
  // Bound the input length so `total_bits` below fits in four bytes. This is
243
  // redundant with TLS record size limits. This also ensures `input_idx` below
244
  // does not overflow.
245
0
  size_t max_len_bits = max_len << 3;
246
0
  if (ctx->Nh != 0 ||
247
0
      (max_len_bits >> 3) != max_len ||  // Overflow
248
0
      ctx->Nl + max_len_bits < max_len_bits ||
249
0
      ctx->Nl + max_len_bits > UINT32_MAX) {
250
0
    return 0;
251
0
  }
252
253
  // We need to hash the following into `ctx`:
254
  //
255
  // - ctx->data[:ctx->num]
256
  // - in[:len]
257
  // - A 0x80 byte
258
  // - However many zero bytes are needed to pad up to a block.
259
  // - Eight bytes of length.
260
0
  size_t num_blocks = (ctx->num + len + 1 + 8 + SHA256_CBLOCK - 1) >> 6;
261
0
  size_t last_block = num_blocks - 1;
262
0
  size_t max_blocks = (ctx->num + max_len + 1 + 8 + SHA256_CBLOCK - 1) >> 6;
263
264
  // The bounds above imply `total_bits` fits in four bytes.
265
0
  size_t total_bits = ctx->Nl + (len << 3);
266
0
  uint8_t length_bytes[4];
267
0
  CRYPTO_store_u32_be(length_bytes, total_bits);
268
269
  // We now construct and process each expected block in constant-time.
270
0
  uint8_t block[SHA256_CBLOCK] = {0};
271
0
  uint32_t result[8] = {0};
272
  // input_idx is the index into `in` corresponding to the current block.
273
  // However, we allow this index to overflow beyond `max_len`, to simplify the
274
  // 0x80 byte.
275
0
  size_t input_idx = 0;
276
0
  for (size_t i = 0; i < max_blocks; i++) {
277
    // Fill `block` with data from the partial block in `ctx` and `in`. We copy
278
    // as if we were hashing up to `max_len` and then zero the excess later.
279
0
    size_t block_start = 0;
280
0
    if (i == 0) {
281
0
      OPENSSL_memcpy(block, ctx->data, ctx->num);
282
0
      block_start = ctx->num;
283
0
    }
284
0
    if (input_idx < max_len) {
285
0
      size_t to_copy = SHA256_CBLOCK - block_start;
286
0
      if (to_copy > max_len - input_idx) {
287
0
        to_copy = max_len - input_idx;
288
0
      }
289
0
      OPENSSL_memcpy(block + block_start, in + input_idx, to_copy);
290
0
    }
291
292
    // Zero any bytes beyond `len` and add the 0x80 byte.
293
0
    for (size_t j = block_start; j < SHA256_CBLOCK; j++) {
294
      // input[idx] corresponds to block[j].
295
0
      size_t idx = input_idx + j - block_start;
296
      // The barriers on `len` are not strictly necessary. However, without
297
      // them, GCC compiles this code by incorporating `len` into the loop
298
      // counter and subtracting it out later. This is still constant-time, but
299
      // it frustrates attempts to validate this.
300
0
      uint8_t is_in_bounds = constant_time_lt_8(idx, value_barrier_w(len));
301
0
      uint8_t is_padding_byte = constant_time_eq_8(idx, value_barrier_w(len));
302
0
      block[j] &= is_in_bounds;
303
0
      block[j] |= 0x80 & is_padding_byte;
304
0
    }
305
306
0
    input_idx += SHA256_CBLOCK - block_start;
307
308
    // Fill in the length if this is the last block. Use a value barrier to
309
    // prevent Clang from compiling the conditional select as a jump.
310
0
    crypto_word_t is_last_block =
311
0
        value_barrier_w(constant_time_eq_w(i, last_block));
312
0
    for (size_t j = 0; j < 4; j++) {
313
0
      block[SHA256_CBLOCK - 4 + j] |= is_last_block & length_bytes[j];
314
0
    }
315
316
    // Process the block and save the hash state if it is the final value.
317
0
    SHA256_Transform(ctx, block);
318
0
    for (size_t j = 0; j < 8; j++) {
319
0
      result[j] |= is_last_block & ctx->h[j];
320
0
    }
321
0
  }
322
323
  // Write the output.
324
0
  for (size_t i = 0; i < 8; i++) {
325
0
    CRYPTO_store_u32_be(out + 4 * i, result[i]);
326
0
  }
327
0
  return 1;
328
0
}
329
330
4.41k
int bssl::EVP_tls_cbc_record_digest_supported(const EVP_MD *md) {
331
4.41k
  switch (EVP_MD_type(md)) {
332
4.41k
    case NID_sha1:
333
4.41k
    case NID_sha256:
334
4.41k
      return 1;
335
0
    default:
336
0
      return 0;
337
4.41k
  }
338
4.41k
}
339
340
static int tls_cbc_digest_record_sha1(
341
    uint8_t *md_out, size_t *md_out_size, const uint8_t len_header[2],
342
    Span<const CRYPTO_IVEC> aadvecs,
343
    Span<const CRYPTO_IOVEC> iovecs_without_trailer,
344
    Span<const uint8_t> trailer, size_t data_in_trailer_size,
345
4.41k
    const uint8_t *mac_secret, unsigned mac_secret_length) {
346
4.41k
  if (mac_secret_length > SHA_CBLOCK) {
347
    // HMAC pads small keys with zeros and hashes large keys down. This function
348
    // should never reach the large key case.
349
0
    assert(0);
350
0
    return 0;
351
0
  }
352
353
  // Compute the initial HMAC block.
354
4.41k
  uint8_t hmac_pad[SHA_CBLOCK];
355
4.41k
  OPENSSL_memset(hmac_pad, 0, sizeof(hmac_pad));
356
4.41k
  OPENSSL_memcpy(hmac_pad, mac_secret, mac_secret_length);
357
287k
  for (size_t i = 0; i < SHA_CBLOCK; i++) {
358
282k
    hmac_pad[i] ^= 0x36;
359
282k
  }
360
361
4.41k
  SHA_CTX ctx;
362
4.41k
  SHA1_Init(&ctx);
363
4.41k
  SHA1_Update(&ctx, hmac_pad, SHA_CBLOCK);
364
4.41k
  for (const CRYPTO_IVEC &aadvec : aadvecs) {
365
4.41k
    SHA1_Update(&ctx, aadvec.in, aadvec.len);
366
4.41k
  }
367
4.41k
  SHA1_Update(&ctx, len_header, 2);
368
369
  // Hash the public minimum length directly. This reduces the number of blocks
370
  // that must be computed in constant-time.
371
4.41k
  for (const CRYPTO_IOVEC &iovec : iovecs_without_trailer) {
372
4.41k
    SHA1_Update(&ctx, iovec.out, iovec.len);
373
4.41k
  }
374
375
  // Hash the remaining data without leaking `data_in_trailer_size`.
376
4.41k
  uint8_t mac_out[SHA_DIGEST_LENGTH];
377
4.41k
  if (!EVP_sha1_final_with_secret_suffix(&ctx, mac_out, trailer.data(),
378
4.41k
                                         data_in_trailer_size,
379
4.41k
                                         trailer.size())) {
380
0
    return 0;
381
0
  }
382
383
  // Complete the HMAC in the standard manner.
384
4.41k
  SHA1_Init(&ctx);
385
287k
  for (size_t i = 0; i < SHA_CBLOCK; i++) {
386
282k
    hmac_pad[i] ^= 0x6a;
387
282k
  }
388
389
4.41k
  SHA1_Update(&ctx, hmac_pad, SHA_CBLOCK);
390
4.41k
  SHA1_Update(&ctx, mac_out, SHA_DIGEST_LENGTH);
391
4.41k
  SHA1_Final(md_out, &ctx);
392
4.41k
  *md_out_size = SHA_DIGEST_LENGTH;
393
4.41k
  return 1;
394
4.41k
}
395
396
static int tls_cbc_digest_record_sha256(
397
    uint8_t *md_out, size_t *md_out_size, const uint8_t len_header[2],
398
    Span<const CRYPTO_IVEC> aadvecs,
399
    Span<const CRYPTO_IOVEC> iovecs_without_trailer,
400
    Span<const uint8_t> trailer, size_t data_in_trailer_size,
401
0
    const uint8_t *mac_secret, unsigned mac_secret_length) {
402
0
  if (mac_secret_length > SHA256_CBLOCK) {
403
    // HMAC pads small keys with zeros and hashes large keys down. This function
404
    // should never reach the large key case.
405
0
    assert(0);
406
0
    return 0;
407
0
  }
408
409
  // Compute the initial HMAC block.
410
0
  uint8_t hmac_pad[SHA256_CBLOCK];
411
0
  OPENSSL_memset(hmac_pad, 0, sizeof(hmac_pad));
412
0
  OPENSSL_memcpy(hmac_pad, mac_secret, mac_secret_length);
413
0
  for (size_t i = 0; i < SHA256_CBLOCK; i++) {
414
0
    hmac_pad[i] ^= 0x36;
415
0
  }
416
417
0
  SHA256_CTX ctx;
418
0
  SHA256_Init(&ctx);
419
0
  SHA256_Update(&ctx, hmac_pad, SHA256_CBLOCK);
420
0
  for (const CRYPTO_IVEC &aadvec : aadvecs) {
421
0
    SHA256_Update(&ctx, aadvec.in, aadvec.len);
422
0
  }
423
0
  SHA256_Update(&ctx, len_header, 2);
424
425
  // Hash the public minimum length directly. This reduces the number of blocks
426
  // that must be computed in constant-time.
427
0
  for (const CRYPTO_IOVEC &iovec : iovecs_without_trailer) {
428
0
    SHA256_Update(&ctx, iovec.out, iovec.len);
429
0
  }
430
431
  // Hash the remaining data without leaking `data_in_trailer_size`.
432
0
  uint8_t mac_out[SHA256_DIGEST_LENGTH];
433
0
  if (!EVP_sha256_final_with_secret_suffix(&ctx, mac_out, trailer.data(),
434
0
                                           data_in_trailer_size,
435
0
                                           trailer.size())) {
436
0
    return 0;
437
0
  }
438
439
  // Complete the HMAC in the standard manner.
440
0
  SHA256_Init(&ctx);
441
0
  for (size_t i = 0; i < SHA256_CBLOCK; i++) {
442
0
    hmac_pad[i] ^= 0x6a;
443
0
  }
444
445
0
  SHA256_Update(&ctx, hmac_pad, SHA256_CBLOCK);
446
0
  SHA256_Update(&ctx, mac_out, SHA256_DIGEST_LENGTH);
447
0
  SHA256_Final(md_out, &ctx);
448
0
  *md_out_size = SHA256_DIGEST_LENGTH;
449
0
  return 1;
450
0
}
451
452
int bssl::EVP_tls_cbc_digest_record(
453
    const EVP_MD *md, uint8_t *md_out, size_t *md_out_size,
454
    const uint8_t len_header[2], Span<const CRYPTO_IVEC> aadvecs,
455
    Span<const CRYPTO_IOVEC> iovecs_without_trailer,
456
    Span<const uint8_t> trailer, size_t data_in_trailer_size,
457
4.41k
    const uint8_t *mac_secret, unsigned mac_secret_length) {
458
4.41k
  switch (EVP_MD_type(md)) {
459
4.41k
    case NID_sha1:
460
4.41k
      return tls_cbc_digest_record_sha1(  //
461
4.41k
          md_out, md_out_size, len_header, aadvecs, iovecs_without_trailer,
462
4.41k
          trailer, data_in_trailer_size, mac_secret, mac_secret_length);
463
464
0
    case NID_sha256:
465
0
      return tls_cbc_digest_record_sha256(  //
466
0
          md_out, md_out_size, len_header, aadvecs, iovecs_without_trailer,
467
0
          trailer, data_in_trailer_size, mac_secret, mac_secret_length);
468
469
0
    default:
470
      // EVP_tls_cbc_record_digest_supported should have been called first to
471
      // check that the hash function is supported.
472
0
      assert(0);
473
0
      *md_out_size = 0;
474
0
      return 0;
475
4.41k
  }
476
4.41k
}