Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/ssl/s3_cbc.c
Line
Count
Source
1
/*
2
 * Copyright 2012-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/*
11
 * This file has no dependencies on the rest of libssl because it is shared
12
 * with the providers. It contains functions for low level MAC calculations.
13
 * Responsibility for this lies with the HMAC implementation in the
14
 * providers. However there are legacy code paths in libssl which also need to
15
 * do this. In time those legacy code paths can be removed and this file can be
16
 * moved out of libssl.
17
 */
18
19
/*
20
 * MD5 and SHA-1 low level APIs are deprecated for public use, but still ok for
21
 * internal use.
22
 */
23
#include "internal/deprecated.h"
24
25
#include "internal/constant_time.h"
26
#include "internal/cryptlib.h"
27
28
#include <openssl/evp.h>
29
#ifndef FIPS_MODULE
30
#include <openssl/md5.h>
31
#endif
32
#include <openssl/sha.h>
33
34
char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx);
35
int ssl3_cbc_digest_record(const EVP_MD *md,
36
    unsigned char *md_out,
37
    size_t *md_out_size,
38
    const unsigned char *header,
39
    const unsigned char *data,
40
    size_t data_size,
41
    size_t data_plus_mac_plus_padding_size,
42
    const unsigned char *mac_secret,
43
    size_t mac_secret_length, char is_sslv3);
44
45
6.28k
#define l2n(l, c) (*((c)++) = (unsigned char)(((l) >> 24) & 0xff), \
46
6.28k
    *((c)++) = (unsigned char)(((l) >> 16) & 0xff),                \
47
6.28k
    *((c)++) = (unsigned char)(((l) >> 8) & 0xff),                 \
48
6.28k
    *((c)++) = (unsigned char)(((l)) & 0xff))
49
50
#define l2n6(l, c) (*((c)++) = (unsigned char)(((l) >> 40) & 0xff), \
51
    *((c)++) = (unsigned char)(((l) >> 32) & 0xff),                 \
52
    *((c)++) = (unsigned char)(((l) >> 24) & 0xff),                 \
53
    *((c)++) = (unsigned char)(((l) >> 16) & 0xff),                 \
54
    *((c)++) = (unsigned char)(((l) >> 8) & 0xff),                  \
55
    *((c)++) = (unsigned char)(((l)) & 0xff))
56
57
600
#define l2n8(l, c) (*((c)++) = (unsigned char)(((l) >> 56) & 0xff), \
58
600
    *((c)++) = (unsigned char)(((l) >> 48) & 0xff),                 \
59
600
    *((c)++) = (unsigned char)(((l) >> 40) & 0xff),                 \
60
600
    *((c)++) = (unsigned char)(((l) >> 32) & 0xff),                 \
61
600
    *((c)++) = (unsigned char)(((l) >> 24) & 0xff),                 \
62
600
    *((c)++) = (unsigned char)(((l) >> 16) & 0xff),                 \
63
600
    *((c)++) = (unsigned char)(((l) >> 8) & 0xff),                  \
64
600
    *((c)++) = (unsigned char)(((l)) & 0xff))
65
66
/*
67
 * MAX_HASH_BIT_COUNT_BYTES is the maximum number of bytes in the hash's
68
 * length field. (SHA-384/512 have 128-bit length.)
69
 */
70
#define MAX_HASH_BIT_COUNT_BYTES 16
71
72
/*
73
 * MAX_HASH_BLOCK_SIZE is the maximum hash block size that we'll support.
74
 * Currently SHA-384/512 has a 128-byte block size and that's the largest
75
 * supported by TLS.)
76
 */
77
#define MAX_HASH_BLOCK_SIZE 128
78
79
#ifndef FIPS_MODULE
80
/*
81
 * u32toLE serializes an unsigned, 32-bit number (n) as four bytes at (p) in
82
 * little-endian order. The value of p is advanced by four.
83
 */
84
#define u32toLE(n, p)                        \
85
0
    (*((p)++) = (unsigned char)(n),          \
86
0
        *((p)++) = (unsigned char)(n >> 8),  \
87
0
        *((p)++) = (unsigned char)(n >> 16), \
88
0
        *((p)++) = (unsigned char)(n >> 24))
89
90
/*
91
 * These functions serialize the state of a hash and thus perform the
92
 * standard "final" operation without adding the padding and length that such
93
 * a function typically does.
94
 */
95
static void tls1_md5_final_raw(void *ctx, unsigned char *md_out)
96
0
{
97
0
    MD5_CTX *md5 = ctx;
98
0
    u32toLE(md5->A, md_out);
99
0
    u32toLE(md5->B, md_out);
100
0
    u32toLE(md5->C, md_out);
101
0
    u32toLE(md5->D, md_out);
102
0
}
103
#endif /* FIPS_MODULE */
104
105
static void tls1_sha1_final_raw(void *ctx, unsigned char *md_out)
106
966
{
107
966
    SHA_CTX *sha1 = ctx;
108
966
    l2n(sha1->h0, md_out);
109
966
    l2n(sha1->h1, md_out);
110
966
    l2n(sha1->h2, md_out);
111
966
    l2n(sha1->h3, md_out);
112
966
    l2n(sha1->h4, md_out);
113
966
}
114
115
static void tls1_sha256_final_raw(void *ctx, unsigned char *md_out)
116
182
{
117
182
    SHA256_CTX *sha256 = ctx;
118
182
    unsigned i;
119
120
1.63k
    for (i = 0; i < 8; i++) {
121
1.45k
        l2n(sha256->h[i], md_out);
122
1.45k
    }
123
182
}
124
125
static void tls1_sha512_final_raw(void *ctx, unsigned char *md_out)
126
75
{
127
75
    SHA512_CTX *sha512 = ctx;
128
75
    unsigned i;
129
130
675
    for (i = 0; i < 8; i++) {
131
600
        l2n8(sha512->h[i], md_out);
132
600
    }
133
75
}
134
135
#undef LARGEST_DIGEST_CTX
136
#define LARGEST_DIGEST_CTX SHA512_CTX
137
138
/*-
139
 * ssl3_cbc_digest_record computes the MAC of a decrypted, padded SSLv3/TLS
140
 * record.
141
 *
142
 *   ctx: the EVP_MD_CTX from which we take the hash function.
143
 *     ssl3_cbc_record_digest_supported must return true for this EVP_MD_CTX.
144
 *   md_out: the digest output. At most EVP_MAX_MD_SIZE bytes will be written.
145
 *   md_out_size: if non-NULL, the number of output bytes is written here.
146
 *   header: the 13-byte, TLS record header.
147
 *   data: the record data itself, less any preceding explicit IV.
148
 *   data_size: the secret, reported length of the data once the MAC and padding
149
 *              has been removed.
150
 *   data_plus_mac_plus_padding_size: the public length of the whole
151
 *     record, including MAC and padding.
152
 *   is_sslv3: non-zero if we are to use SSLv3. Otherwise, TLS.
153
 *
154
 * On entry: we know that data is data_plus_mac_plus_padding_size in length
155
 * Returns 1 on success or 0 on error
156
 */
157
int ssl3_cbc_digest_record(const EVP_MD *md,
158
    unsigned char *md_out,
159
    size_t *md_out_size,
160
    const unsigned char *header,
161
    const unsigned char *data,
162
    size_t data_size,
163
    size_t data_plus_mac_plus_padding_size,
164
    const unsigned char *mac_secret,
165
    size_t mac_secret_length, char is_sslv3)
166
163k
{
167
163k
    union {
168
163k
        OSSL_UNION_ALIGN;
169
163k
        unsigned char c[sizeof(LARGEST_DIGEST_CTX)];
170
163k
    } md_state;
171
163k
    void (*md_final_raw)(void *ctx, unsigned char *md_out);
172
163k
    void (*md_transform)(void *ctx, const unsigned char *block);
173
163k
    size_t md_size, md_block_size = 64;
174
163k
    size_t sslv3_pad_length = 40, header_length, variance_blocks,
175
163k
           len, max_mac_bytes, num_blocks,
176
163k
           num_starting_blocks, k, mac_end_offset, c, index_a, index_b;
177
163k
    size_t bits; /* at most 18 bits */
178
163k
    unsigned char length_bytes[MAX_HASH_BIT_COUNT_BYTES];
179
    /* hmac_pad is the masked HMAC key. */
180
163k
    unsigned char hmac_pad[MAX_HASH_BLOCK_SIZE];
181
163k
    unsigned char first_block[MAX_HASH_BLOCK_SIZE];
182
163k
    unsigned char mac_out[EVP_MAX_MD_SIZE];
183
163k
    size_t i, j;
184
163k
    unsigned md_out_size_u;
185
163k
    EVP_MD_CTX *md_ctx = NULL;
186
    /*
187
     * mdLengthSize is the number of bytes in the length field that
188
     * terminates * the hash.
189
     */
190
163k
    size_t md_length_size = 8;
191
163k
    char length_is_big_endian = 1;
192
163k
    int ret = 0;
193
194
    /*
195
     * This is a, hopefully redundant, check that allows us to forget about
196
     * many possible overflows later in this function.
197
     */
198
163k
    if (!ossl_assert(data_plus_mac_plus_padding_size < 1024 * 1024))
199
0
        return 0;
200
201
163k
    if (EVP_MD_is_a(md, "MD5")) {
202
#ifdef FIPS_MODULE
203
        return 0;
204
#else
205
0
        if (MD5_Init((MD5_CTX *)md_state.c) <= 0)
206
0
            return 0;
207
0
        md_final_raw = tls1_md5_final_raw;
208
0
        md_transform = (void (*)(void *ctx, const unsigned char *block))MD5_Transform;
209
0
        md_size = 16;
210
0
        sslv3_pad_length = 48;
211
0
        length_is_big_endian = 0;
212
0
#endif
213
163k
    } else if (EVP_MD_is_a(md, "SHA1")) {
214
127k
        if (SHA1_Init((SHA_CTX *)md_state.c) <= 0)
215
0
            return 0;
216
127k
        md_final_raw = tls1_sha1_final_raw;
217
127k
        md_transform = (void (*)(void *ctx, const unsigned char *block))SHA1_Transform;
218
127k
        md_size = 20;
219
127k
    } else if (EVP_MD_is_a(md, "SHA2-224")) {
220
0
        if (SHA224_Init((SHA256_CTX *)md_state.c) <= 0)
221
0
            return 0;
222
0
        md_final_raw = tls1_sha256_final_raw;
223
0
        md_transform = (void (*)(void *ctx, const unsigned char *block))SHA256_Transform;
224
0
        md_size = 224 / 8;
225
35.8k
    } else if (EVP_MD_is_a(md, "SHA2-256")) {
226
20.8k
        if (SHA256_Init((SHA256_CTX *)md_state.c) <= 0)
227
0
            return 0;
228
20.8k
        md_final_raw = tls1_sha256_final_raw;
229
20.8k
        md_transform = (void (*)(void *ctx, const unsigned char *block))SHA256_Transform;
230
20.8k
        md_size = 32;
231
20.8k
    } else if (EVP_MD_is_a(md, "SHA2-384")) {
232
15.0k
        if (SHA384_Init((SHA512_CTX *)md_state.c) <= 0)
233
0
            return 0;
234
15.0k
        md_final_raw = tls1_sha512_final_raw;
235
15.0k
        md_transform = (void (*)(void *ctx, const unsigned char *block))SHA512_Transform;
236
15.0k
        md_size = 384 / 8;
237
15.0k
        md_block_size = 128;
238
15.0k
        md_length_size = 16;
239
15.0k
    } else if (EVP_MD_is_a(md, "SHA2-512")) {
240
0
        if (SHA512_Init((SHA512_CTX *)md_state.c) <= 0)
241
0
            return 0;
242
0
        md_final_raw = tls1_sha512_final_raw;
243
0
        md_transform = (void (*)(void *ctx, const unsigned char *block))SHA512_Transform;
244
0
        md_size = 64;
245
0
        md_block_size = 128;
246
0
        md_length_size = 16;
247
0
    } else {
248
        /*
249
         * ssl3_cbc_record_digest_supported should have been called first to
250
         * check that the hash function is supported.
251
         */
252
0
        if (md_out_size != NULL)
253
0
            *md_out_size = 0;
254
0
        return ossl_assert(0);
255
0
    }
256
257
163k
    if (!ossl_assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES)
258
163k
        || !ossl_assert(md_block_size <= MAX_HASH_BLOCK_SIZE)
259
163k
        || !ossl_assert(md_size <= EVP_MAX_MD_SIZE))
260
0
        return 0;
261
262
163k
    header_length = 13;
263
163k
    if (is_sslv3) {
264
0
        header_length = mac_secret_length + sslv3_pad_length + 8 /* sequence
265
                                                                  * number */
266
0
            + 1 /* record type */ + 2 /* record length */;
267
0
    }
268
269
    /*
270
     * variance_blocks is the number of blocks of the hash that we have to
271
     * calculate in constant time because they could be altered by the
272
     * padding value. In SSLv3, the padding must be minimal so the end of
273
     * the plaintext varies by, at most, 15+20 = 35 bytes. (We conservatively
274
     * assume that the MAC size varies from 0..20 bytes.) In case the 9 bytes
275
     * of hash termination (0x80 + 64-bit length) don't fit in the final
276
     * block, we say that the final two blocks can vary based on the padding.
277
     * TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not
278
     * required to be minimal. Therefore we say that the final |variance_blocks|
279
     * blocks can
280
     * vary based on the padding. Later in the function, if the message is
281
     * short and there obviously cannot be this many blocks then
282
     * variance_blocks can be reduced.
283
     */
284
163k
    variance_blocks = is_sslv3 ? 2 : (((255 + 1 + md_size + md_block_size - 1) / md_block_size) + 1);
285
    /*
286
     * From now on we're dealing with the MAC, which conceptually has 13
287
     * bytes of `header' before the start of the data (TLS) or 71/75 bytes
288
     * (SSLv3)
289
     */
290
163k
    len = data_plus_mac_plus_padding_size + header_length;
291
    /*
292
     * max_mac_bytes contains the maximum bytes of bytes in the MAC,
293
     * including * |header|, assuming that there's no padding.
294
     */
295
163k
    max_mac_bytes = len - md_size - 1;
296
    /* num_blocks is the maximum number of hash blocks. */
297
163k
    num_blocks = (max_mac_bytes + 1 + md_length_size + md_block_size - 1) / md_block_size;
298
    /*
299
     * In order to calculate the MAC in constant time we have to handle the
300
     * final blocks specially because the padding value could cause the end
301
     * to appear somewhere in the final |variance_blocks| blocks and we can't
302
     * leak where. However, |num_starting_blocks| worth of data can be hashed
303
     * right away because no padding value can affect whether they are
304
     * plaintext.
305
     */
306
163k
    num_starting_blocks = 0;
307
    /*
308
     * k is the starting byte offset into the conceptual header||data where
309
     * we start processing.
310
     */
311
163k
    k = 0;
312
    /*
313
     * mac_end_offset is the index just past the end of the data to be MACed.
314
     */
315
163k
    mac_end_offset = data_size + header_length;
316
    /*
317
     * c is the index of the 0x80 byte in the final hash block that contains
318
     * application data.
319
     */
320
163k
    c = mac_end_offset % md_block_size;
321
    /*
322
     * index_a is the hash block number that contains the 0x80 terminating
323
     * value.
324
     */
325
163k
    index_a = mac_end_offset / md_block_size;
326
    /*
327
     * index_b is the hash block number that contains the 64-bit hash length,
328
     * in bits.
329
     */
330
163k
    index_b = (mac_end_offset + md_length_size) / md_block_size;
331
    /*
332
     * bits is the hash-length in bits. It includes the additional hash block
333
     * for the masked HMAC key, or whole of |header| in the case of SSLv3.
334
     */
335
336
    /*
337
     * For SSLv3, if we're going to have any starting blocks then we need at
338
     * least two because the header is larger than a single block.
339
     */
340
163k
    if (num_blocks > variance_blocks + (is_sslv3 ? 1 : 0)) {
341
3.73k
        num_starting_blocks = num_blocks - variance_blocks;
342
3.73k
        k = md_block_size * num_starting_blocks;
343
3.73k
    }
344
345
163k
    bits = 8 * mac_end_offset;
346
163k
    if (!is_sslv3) {
347
        /*
348
         * Compute the initial HMAC block. For SSLv3, the padding and secret
349
         * bytes are included in |header| because they take more than a
350
         * single block.
351
         */
352
163k
        bits += 8 * md_block_size;
353
163k
        memset(hmac_pad, 0, md_block_size);
354
163k
        if (!ossl_assert(mac_secret_length <= sizeof(hmac_pad)))
355
0
            return 0;
356
163k
        memcpy(hmac_pad, mac_secret, mac_secret_length);
357
11.6M
        for (i = 0; i < md_block_size; i++)
358
11.4M
            hmac_pad[i] ^= 0x36;
359
360
163k
        md_transform(md_state.c, hmac_pad);
361
163k
    }
362
363
163k
    if (length_is_big_endian) {
364
163k
        memset(length_bytes, 0, md_length_size - 4);
365
163k
        length_bytes[md_length_size - 4] = (unsigned char)(bits >> 24);
366
163k
        length_bytes[md_length_size - 3] = (unsigned char)(bits >> 16);
367
163k
        length_bytes[md_length_size - 2] = (unsigned char)(bits >> 8);
368
163k
        length_bytes[md_length_size - 1] = (unsigned char)bits;
369
163k
    } else {
370
0
        memset(length_bytes, 0, md_length_size);
371
0
        length_bytes[md_length_size - 5] = (unsigned char)(bits >> 24);
372
0
        length_bytes[md_length_size - 6] = (unsigned char)(bits >> 16);
373
0
        length_bytes[md_length_size - 7] = (unsigned char)(bits >> 8);
374
0
        length_bytes[md_length_size - 8] = (unsigned char)bits;
375
0
    }
376
377
163k
    if (k > 0) {
378
3.73k
        if (is_sslv3) {
379
0
            size_t overhang;
380
381
            /*
382
             * The SSLv3 header is larger than a single block. overhang is
383
             * the number of bytes beyond a single block that the header
384
             * consumes: either 7 bytes (SHA1) or 11 bytes (MD5). There are no
385
             * ciphersuites in SSLv3 that are not SHA1 or MD5 based and
386
             * therefore we can be confident that the header_length will be
387
             * greater than |md_block_size|. However we add a sanity check just
388
             * in case
389
             */
390
0
            if (header_length <= md_block_size) {
391
                /* Should never happen */
392
0
                return 0;
393
0
            }
394
0
            overhang = header_length - md_block_size;
395
0
            md_transform(md_state.c, header);
396
0
            memcpy(first_block, header + md_block_size, overhang);
397
0
            memcpy(first_block + overhang, data, md_block_size - overhang);
398
0
            md_transform(md_state.c, first_block);
399
0
            for (i = 1; i < k / md_block_size - 1; i++)
400
0
                md_transform(md_state.c, data + md_block_size * i - overhang);
401
3.73k
        } else {
402
            /* k is a multiple of md_block_size. */
403
3.73k
            memcpy(first_block, header, 13);
404
3.73k
            memcpy(first_block + 13, data, md_block_size - 13);
405
3.73k
            md_transform(md_state.c, first_block);
406
61.0k
            for (i = 1; i < k / md_block_size; i++)
407
57.2k
                md_transform(md_state.c, data + md_block_size * i - 13);
408
3.73k
        }
409
3.73k
    }
410
411
163k
    memset(mac_out, 0, sizeof(mac_out));
412
413
    /*
414
     * We now process the final hash blocks. For each block, we construct it
415
     * in constant time. If the |i==index_a| then we'll include the 0x80
416
     * bytes and zero pad etc. For each block we selectively copy it, in
417
     * constant time, to |mac_out|.
418
     */
419
1.27M
    for (i = num_starting_blocks; i <= num_starting_blocks + variance_blocks;
420
1.11M
        i++) {
421
1.11M
        unsigned char block[MAX_HASH_BLOCK_SIZE];
422
1.11M
        unsigned char is_block_a = constant_time_eq_8_s(i, index_a);
423
1.11M
        unsigned char is_block_b = constant_time_eq_8_s(i, index_b);
424
77.3M
        for (j = 0; j < md_block_size; j++) {
425
76.2M
            unsigned char b = 0, is_past_c, is_past_cp1;
426
76.2M
            if (k < header_length)
427
2.07M
                b = header[k];
428
74.1M
            else if (k < data_plus_mac_plus_padding_size + header_length)
429
9.81M
                b = data[k - header_length];
430
76.2M
            k++;
431
432
76.2M
            is_past_c = is_block_a & constant_time_ge_8_s(j, c);
433
76.2M
            is_past_cp1 = is_block_a & constant_time_ge_8_s(j, c + 1);
434
            /*
435
             * If this is the block containing the end of the application
436
             * data, and we are at the offset for the 0x80 value, then
437
             * overwrite b with 0x80.
438
             */
439
76.2M
            b = constant_time_select_8(is_past_c, 0x80, b);
440
            /*
441
             * If this block contains the end of the application data
442
             * and we're past the 0x80 value then just write zero.
443
             */
444
76.2M
            b = b & ~is_past_cp1;
445
            /*
446
             * If this is index_b (the final block), but not index_a (the end
447
             * of the data), then the 64-bit length didn't fit into index_a
448
             * and we're having to add an extra block of zeros.
449
             */
450
76.2M
            b &= ~is_block_b | is_block_a;
451
452
            /*
453
             * The final bytes of one of the blocks contains the length.
454
             */
455
76.2M
            if (j >= md_block_size - md_length_size) {
456
                /* If this is index_b, write a length byte. */
457
9.52M
                b = constant_time_select_8(is_block_b,
458
9.52M
                    length_bytes[j - (md_block_size - md_length_size)], b);
459
9.52M
            }
460
76.2M
            block[j] = b;
461
76.2M
        }
462
463
1.11M
        md_transform(md_state.c, block);
464
1.11M
        md_final_raw(md_state.c, block);
465
        /* If this is index_b, copy the hash value to |mac_out|. */
466
27.2M
        for (j = 0; j < md_size; j++)
467
26.1M
            mac_out[j] |= block[j] & is_block_b;
468
1.11M
    }
469
470
163k
    md_ctx = EVP_MD_CTX_new();
471
163k
    if (md_ctx == NULL)
472
0
        goto err;
473
474
163k
    if (EVP_DigestInit_ex(md_ctx, md, NULL /* engine */) <= 0)
475
0
        goto err;
476
163k
    if (is_sslv3) {
477
        /* We repurpose |hmac_pad| to contain the SSLv3 pad2 block. */
478
0
        memset(hmac_pad, 0x5c, sslv3_pad_length);
479
480
0
        if (EVP_DigestUpdate(md_ctx, mac_secret, mac_secret_length) <= 0
481
0
            || EVP_DigestUpdate(md_ctx, hmac_pad, sslv3_pad_length) <= 0
482
0
            || EVP_DigestUpdate(md_ctx, mac_out, md_size) <= 0)
483
0
            goto err;
484
163k
    } else {
485
        /* Complete the HMAC in the standard manner. */
486
11.6M
        for (i = 0; i < md_block_size; i++)
487
11.4M
            hmac_pad[i] ^= 0x6a;
488
489
163k
        if (EVP_DigestUpdate(md_ctx, hmac_pad, md_block_size) <= 0
490
163k
            || EVP_DigestUpdate(md_ctx, mac_out, md_size) <= 0)
491
0
            goto err;
492
163k
    }
493
163k
    ret = EVP_DigestFinal(md_ctx, md_out, &md_out_size_u);
494
163k
    if (ret && md_out_size)
495
163k
        *md_out_size = md_out_size_u;
496
497
163k
    ret = 1;
498
163k
err:
499
163k
    EVP_MD_CTX_free(md_ctx);
500
163k
    return ret;
501
163k
}