2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/rand.h>
11 #include <openssl/evp.h>
12 #include "internal/constant_time.h"
13 #include "internal/cryptlib.h"
16 * This file has no dependencies on the rest of libssl because it is shared
17 * with the providers. It contains functions for low level CBC TLS padding
18 * removal. Responsibility for this lies with the cipher implementations in the
19 * providers. However there are legacy code paths in libssl which also need to
20 * do this. In time those legacy code paths can be removed and this file can be
21 * moved out of libssl.
24 static int ssl3_cbc_copy_mac(size_t *reclen,
26 unsigned char *recdata,
32 OSSL_LIB_CTX *libctx);
34 int ssl3_cbc_remove_padding_and_mac(size_t *reclen,
36 unsigned char *recdata,
39 size_t block_size, size_t mac_size,
40 OSSL_LIB_CTX *libctx);
42 int tls1_cbc_remove_padding_and_mac(size_t *reclen,
44 unsigned char *recdata,
47 size_t block_size, size_t mac_size,
49 OSSL_LIB_CTX *libctx);
52 * ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC
53 * record in |recdata| by updating |reclen| in constant time. It also extracts
54 * the MAC from the underlying record and places a pointer to it in |mac|. The
55 * MAC data can either be newly allocated memory, or a pointer inside the
56 * |recdata| buffer. If allocated then |*alloced| is set to 1, otherwise it is
59 * origreclen: the original record length before any changes were made
60 * block_size: the block size of the cipher used to encrypt the record.
61 * mac_size: the size of the MAC to be extracted
62 * aead: 1 if an AEAD cipher is in use, or 0 otherwise
64 * 0: if the record is publicly invalid.
65 * 1: if the record is publicly valid. If the padding removal fails then the
66 * MAC returned is random.
68 int ssl3_cbc_remove_padding_and_mac(size_t *reclen,
70 unsigned char *recdata,
73 size_t block_size, size_t mac_size,
76 size_t padding_length;
78 const size_t overhead = 1 /* padding length byte */ + mac_size;
81 * These lengths are all public so we can test them in non-constant time.
83 if (overhead > *reclen)
86 padding_length = recdata[*reclen - 1];
87 good = constant_time_ge_s(*reclen, padding_length + overhead);
88 /* SSLv3 requires that the padding is minimal. */
89 good &= constant_time_ge_s(block_size, padding_length + 1);
90 *reclen -= good & (padding_length + 1);
92 return ssl3_cbc_copy_mac(reclen, origreclen, recdata, mac, alloced,
93 block_size, mac_size, good, libctx);
97 * tls1_cbc_remove_padding_and_mac removes padding from the decrypted, TLS, CBC
98 * record in |recdata| by updating |reclen| in constant time. It also extracts
99 * the MAC from the underlying record and places a pointer to it in |mac|. The
100 * MAC data can either be newly allocated memory, or a pointer inside the
101 * |recdata| buffer. If allocated then |*alloced| is set to 1, otherwise it is
104 * origreclen: the original record length before any changes were made
105 * block_size: the block size of the cipher used to encrypt the record.
106 * mac_size: the size of the MAC to be extracted
107 * aead: 1 if an AEAD cipher is in use, or 0 otherwise
109 * 0: if the record is publicly invalid.
110 * 1: if the record is publicly valid. If the padding removal fails then the
111 * MAC returned is random.
113 int tls1_cbc_remove_padding_and_mac(size_t *reclen,
115 unsigned char *recdata,
118 size_t block_size, size_t mac_size,
120 OSSL_LIB_CTX *libctx)
123 size_t padding_length, to_check, i;
124 size_t overhead = ((block_size == 1) ? 0 : 1) /* padding length byte */
128 * These lengths are all public so we can test them in non-constant
131 if (overhead > *reclen)
134 if (block_size != 1) {
136 padding_length = recdata[*reclen - 1];
139 /* padding is already verified and we don't need to check the MAC */
140 *reclen -= padding_length + 1 + mac_size;
146 good = constant_time_ge_s(*reclen, overhead + padding_length);
148 * The padding consists of a length byte at the end of the record and
149 * then that many bytes of padding, all with the same value as the
150 * length byte. Thus, with the length byte included, there are i+1 bytes
151 * of padding. We can't check just |padding_length+1| bytes because that
152 * leaks decrypted information. Therefore we always have to check the
153 * maximum amount of padding possible. (Again, the length of the record
154 * is public information so we can use it.)
156 to_check = 256; /* maximum amount of padding, inc length byte. */
157 if (to_check > *reclen)
160 for (i = 0; i < to_check; i++) {
161 unsigned char mask = constant_time_ge_8_s(padding_length, i);
162 unsigned char b = recdata[*reclen - 1 - i];
164 * The final |padding_length+1| bytes should all have the value
165 * |padding_length|. Therefore the XOR should be zero.
167 good &= ~(mask & (padding_length ^ b));
171 * If any of the final |padding_length+1| bytes had the wrong value, one
172 * or more of the lower eight bits of |good| will be cleared.
174 good = constant_time_eq_s(0xff, good & 0xff);
175 *reclen -= good & (padding_length + 1);
178 return ssl3_cbc_copy_mac(reclen, origreclen, recdata, mac, alloced,
179 block_size, mac_size, good, libctx);
183 * ssl3_cbc_copy_mac copies |md_size| bytes from the end of the record in
184 * |recdata| to |*mac| in constant time (independent of the concrete value of
185 * the record length |reclen|, which may vary within a 256-byte window).
188 * origreclen >= mac_size
189 * mac_size <= EVP_MAX_MD_SIZE
191 * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
192 * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
193 * a single or pair of cache-lines, then the variable memory accesses don't
194 * actually affect the timing. CPUs with smaller cache-lines [if any] are
195 * not multi-core and are not considered vulnerable to cache-timing attacks.
197 #define CBC_MAC_ROTATE_IN_PLACE
199 static int ssl3_cbc_copy_mac(size_t *reclen,
201 unsigned char *recdata,
207 OSSL_LIB_CTX *libctx)
209 #if defined(CBC_MAC_ROTATE_IN_PLACE)
210 unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
211 unsigned char *rotated_mac;
213 unsigned char rotated_mac[EVP_MAX_MD_SIZE];
215 unsigned char randmac[EVP_MAX_MD_SIZE];
219 * mac_end is the index of |recdata| just after the end of the MAC.
221 size_t mac_end = *reclen;
222 size_t mac_start = mac_end - mac_size;
225 * scan_start contains the number of bytes that we can ignore because the
226 * MAC's position can only vary by 255 bytes.
228 size_t scan_start = 0;
230 size_t rotate_offset;
232 if (!ossl_assert(origreclen >= mac_size
233 && mac_size <= EVP_MAX_MD_SIZE))
236 /* If no MAC then nothing to be done */
238 /* No MAC so we can do this in non-constant time */
246 if (block_size == 1) {
247 /* There's no padding so the position of the MAC is fixed */
249 *mac = &recdata[*reclen];
255 /* Create the random MAC we will emit if padding is bad */
256 if (!RAND_bytes_ex(libctx, randmac, mac_size, 0))
259 if (!ossl_assert(mac != NULL && alloced != NULL))
261 *mac = out = OPENSSL_malloc(mac_size);
266 #if defined(CBC_MAC_ROTATE_IN_PLACE)
267 rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
270 /* This information is public so it's safe to branch based on it. */
271 if (origreclen > mac_size + 255 + 1)
272 scan_start = origreclen - (mac_size + 255 + 1);
276 memset(rotated_mac, 0, mac_size);
277 for (i = scan_start, j = 0; i < origreclen; i++) {
278 size_t mac_started = constant_time_eq_s(i, mac_start);
279 size_t mac_ended = constant_time_lt_s(i, mac_end);
280 unsigned char b = recdata[i];
282 in_mac |= mac_started;
284 rotate_offset |= j & mac_started;
285 rotated_mac[j++] |= b & in_mac;
286 j &= constant_time_lt_s(j, mac_size);
289 /* Now rotate the MAC */
290 #if defined(CBC_MAC_ROTATE_IN_PLACE)
292 for (i = 0; i < mac_size; i++) {
293 /* in case cache-line is 32 bytes, touch second line */
294 ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];
296 /* If the padding wasn't good we emit a random MAC */
297 out[j++] = constant_time_select_8((unsigned char)(good & 0xff),
298 rotated_mac[rotate_offset++],
300 rotate_offset &= constant_time_lt_s(rotate_offset, mac_size);
303 memset(out, 0, mac_size);
304 rotate_offset = mac_size - rotate_offset;
305 rotate_offset &= constant_time_lt_s(rotate_offset, mac_size);
306 for (i = 0; i < mac_size; i++) {
307 for (j = 0; j < mac_size; j++)
308 out[j] |= rotated_mac[i] & constant_time_eq_8_s(j, rotate_offset);
310 rotate_offset &= constant_time_lt_s(rotate_offset, mac_size);
312 /* If the padding wasn't good we emit a random MAC */
313 out[i] = constant_time_select_8((unsigned char)(good & 0xff), out[i],