/src/openssl/providers/implementations/ciphers/ciphercommon_block.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2019-2026 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 | | #include <assert.h> |
11 | | /* For SSL3_VERSION, TLS1_VERSION etc */ |
12 | | #include <openssl/prov_ssl.h> |
13 | | #include <openssl/rand.h> |
14 | | #include <openssl/proverr.h> |
15 | | #include "internal/constant_time.h" |
16 | | #include "ciphercommon_local.h" |
17 | | |
18 | | /*- |
19 | | * ssl3_cbc_copy_mac copies |md_size| bytes from the end of the record in |
20 | | * |recdata| to |*mac| in constant time (independent of the concrete value of |
21 | | * the record length |reclen|, which may vary within a 256-byte window). |
22 | | * |
23 | | * On entry: |
24 | | * origreclen >= mac_size |
25 | | * mac_size <= EVP_MAX_MD_SIZE |
26 | | * |
27 | | * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with |
28 | | * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into |
29 | | * a single or pair of cache-lines, then the variable memory accesses don't |
30 | | * actually affect the timing. CPUs with smaller cache-lines [if any] are |
31 | | * not multi-core and are not considered vulnerable to cache-timing attacks. |
32 | | */ |
33 | | #define CBC_MAC_ROTATE_IN_PLACE |
34 | | |
35 | | static int ssl3_cbc_copy_mac(size_t *reclen, |
36 | | size_t origreclen, |
37 | | unsigned char *recdata, |
38 | | unsigned char **mac, |
39 | | int *alloced, |
40 | | size_t block_size, |
41 | | size_t mac_size, |
42 | | size_t good, |
43 | | OSSL_LIB_CTX *libctx) |
44 | 0 | { |
45 | 0 | #if defined(CBC_MAC_ROTATE_IN_PLACE) |
46 | 0 | unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE]; |
47 | 0 | unsigned char *rotated_mac; |
48 | 0 | char aux1, aux2, aux3, mask; |
49 | | #else |
50 | | unsigned char rotated_mac[EVP_MAX_MD_SIZE]; |
51 | | #endif |
52 | 0 | unsigned char randmac[EVP_MAX_MD_SIZE]; |
53 | 0 | unsigned char *out; |
54 | | |
55 | | /* |
56 | | * mac_end is the index of |recdata| just after the end of the MAC. |
57 | | */ |
58 | 0 | size_t mac_end = *reclen; |
59 | 0 | size_t mac_start = mac_end - mac_size; |
60 | 0 | size_t in_mac; |
61 | | /* |
62 | | * scan_start contains the number of bytes that we can ignore because the |
63 | | * MAC's position can only vary by 255 bytes. |
64 | | */ |
65 | 0 | size_t scan_start = 0; |
66 | 0 | size_t i, j; |
67 | 0 | size_t rotate_offset; |
68 | |
|
69 | 0 | if (!ossl_assert(origreclen >= mac_size && mac_size <= EVP_MAX_MD_SIZE)) |
70 | 0 | return 0; |
71 | | |
72 | | /* If no MAC then nothing to be done */ |
73 | 0 | if (mac_size == 0) { |
74 | | /* No MAC so we can do this in non-constant time */ |
75 | 0 | if (good == 0) |
76 | 0 | return 0; |
77 | 0 | return 1; |
78 | 0 | } |
79 | | |
80 | 0 | *reclen -= mac_size; |
81 | |
|
82 | 0 | if (block_size == 1) { |
83 | | /* There's no padding so the position of the MAC is fixed */ |
84 | 0 | if (mac != NULL) |
85 | 0 | *mac = &recdata[*reclen]; |
86 | 0 | if (alloced != NULL) |
87 | 0 | *alloced = 0; |
88 | 0 | return 1; |
89 | 0 | } |
90 | | |
91 | | /* Create the random MAC we will emit if padding is bad */ |
92 | 0 | if (RAND_bytes_ex(libctx, randmac, mac_size, 0) <= 0) |
93 | 0 | return 0; |
94 | | |
95 | 0 | if (!ossl_assert(mac != NULL && alloced != NULL)) |
96 | 0 | return 0; |
97 | 0 | *mac = out = OPENSSL_malloc(mac_size); |
98 | 0 | if (*mac == NULL) |
99 | 0 | return 0; |
100 | 0 | *alloced = 1; |
101 | |
|
102 | 0 | #if defined(CBC_MAC_ROTATE_IN_PLACE) |
103 | 0 | rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63); |
104 | 0 | #endif |
105 | | |
106 | | /* This information is public so it's safe to branch based on it. */ |
107 | 0 | if (origreclen > mac_size + 255 + 1) |
108 | 0 | scan_start = origreclen - (mac_size + 255 + 1); |
109 | |
|
110 | 0 | in_mac = 0; |
111 | 0 | rotate_offset = 0; |
112 | 0 | memset(rotated_mac, 0, mac_size); |
113 | 0 | for (i = scan_start, j = 0; i < origreclen; i++) { |
114 | 0 | size_t mac_started = constant_time_eq_s(i, mac_start); |
115 | 0 | size_t mac_ended = constant_time_lt_s(i, mac_end); |
116 | 0 | unsigned char b = recdata[i]; |
117 | |
|
118 | 0 | in_mac |= mac_started; |
119 | 0 | in_mac &= mac_ended; |
120 | 0 | rotate_offset |= j & mac_started; |
121 | 0 | rotated_mac[j++] |= b & in_mac; |
122 | 0 | j &= constant_time_lt_s(j, mac_size); |
123 | 0 | } |
124 | | |
125 | | /* Now rotate the MAC */ |
126 | 0 | #if defined(CBC_MAC_ROTATE_IN_PLACE) |
127 | 0 | j = 0; |
128 | 0 | for (i = 0; i < mac_size; i++) { |
129 | | /* |
130 | | * in case cache-line is 32 bytes, |
131 | | * load from both lines and select appropriately |
132 | | */ |
133 | 0 | aux1 = rotated_mac[rotate_offset & ~32]; |
134 | 0 | aux2 = rotated_mac[rotate_offset | 32]; |
135 | 0 | mask = constant_time_eq_8((unsigned int)(rotate_offset & ~32), |
136 | 0 | (unsigned int)rotate_offset); |
137 | 0 | aux3 = constant_time_select_8(mask, aux1, aux2); |
138 | 0 | rotate_offset++; |
139 | | |
140 | | /* If the padding wasn't good we emit a random MAC */ |
141 | 0 | out[j++] = constant_time_select_8((unsigned char)(good & 0xff), aux3, |
142 | 0 | randmac[i]); |
143 | 0 | rotate_offset &= constant_time_lt_s(rotate_offset, mac_size); |
144 | 0 | } |
145 | | #else |
146 | | memset(out, 0, mac_size); |
147 | | rotate_offset = mac_size - rotate_offset; |
148 | | rotate_offset &= constant_time_lt_s(rotate_offset, mac_size); |
149 | | for (i = 0; i < mac_size; i++) { |
150 | | for (j = 0; j < mac_size; j++) |
151 | | out[j] |= rotated_mac[i] & constant_time_eq_8_s((unsigned int)j, (unsigned int)rotate_offset); |
152 | | rotate_offset++; |
153 | | rotate_offset &= constant_time_lt_s(rotate_offset, mac_size); |
154 | | |
155 | | /* If the padding wasn't good we emit a random MAC */ |
156 | | out[i] = constant_time_select_8((unsigned char)(good & 0xff), out[i], |
157 | | randmac[i]); |
158 | | } |
159 | | #endif |
160 | |
|
161 | 0 | return 1; |
162 | 0 | } |
163 | | |
164 | | /*- |
165 | | * tls1_cbc_remove_padding_and_mac removes padding from the decrypted, TLS, CBC |
166 | | * record in |recdata| by updating |reclen| in constant time. It also extracts |
167 | | * the MAC from the underlying record and places a pointer to it in |mac|. The |
168 | | * MAC data can either be newly allocated memory, or a pointer inside the |
169 | | * |recdata| buffer. If allocated then |*alloced| is set to 1, otherwise it is |
170 | | * set to 0. |
171 | | * |
172 | | * origreclen: the original record length before any changes were made |
173 | | * block_size: the block size of the cipher used to encrypt the record. |
174 | | * mac_size: the size of the MAC to be extracted |
175 | | * aead: 1 if an AEAD cipher is in use, or 0 otherwise |
176 | | * returns: |
177 | | * 0: if the record is publicly invalid. |
178 | | * 1: if the record is publicly valid. If the padding removal fails then the |
179 | | * MAC returned is random. |
180 | | */ |
181 | | static int tls1_cbc_remove_padding_and_mac(size_t *reclen, |
182 | | size_t origreclen, |
183 | | unsigned char *recdata, |
184 | | unsigned char **mac, |
185 | | int *alloced, |
186 | | size_t block_size, size_t mac_size, |
187 | | int aead, |
188 | | OSSL_LIB_CTX *libctx) |
189 | 0 | { |
190 | 0 | size_t good = -1; |
191 | 0 | size_t padding_length, to_check, i; |
192 | 0 | size_t overhead = ((block_size == 1) ? 0 : 1) /* padding length byte */ |
193 | 0 | + mac_size; |
194 | | |
195 | | /* |
196 | | * These lengths are all public so we can test them in non-constant |
197 | | * time. |
198 | | */ |
199 | 0 | if (overhead > *reclen) |
200 | 0 | return 0; |
201 | | |
202 | 0 | if (block_size != 1) { |
203 | |
|
204 | 0 | padding_length = recdata[*reclen - 1]; |
205 | |
|
206 | 0 | if (aead) { |
207 | | /* padding is already verified and we don't need to check the MAC */ |
208 | 0 | *reclen -= padding_length + 1 + mac_size; |
209 | 0 | return 1; |
210 | 0 | } |
211 | | |
212 | 0 | good = constant_time_ge_s(*reclen, overhead + padding_length); |
213 | | /* |
214 | | * The padding consists of a length byte at the end of the record and |
215 | | * then that many bytes of padding, all with the same value as the |
216 | | * length byte. Thus, with the length byte included, there are i+1 bytes |
217 | | * of padding. We can't check just |padding_length+1| bytes because that |
218 | | * leaks decrypted information. Therefore we always have to check the |
219 | | * maximum amount of padding possible. (Again, the length of the record |
220 | | * is public information so we can use it.) |
221 | | */ |
222 | 0 | to_check = 256; /* maximum amount of padding, inc length byte. */ |
223 | 0 | if (to_check > *reclen) |
224 | 0 | to_check = *reclen; |
225 | |
|
226 | 0 | for (i = 0; i < to_check; i++) { |
227 | 0 | unsigned char mask = constant_time_ge_8_s(padding_length, i); |
228 | 0 | unsigned char b = recdata[*reclen - 1 - i]; |
229 | | /* |
230 | | * The final |padding_length+1| bytes should all have the value |
231 | | * |padding_length|. Therefore the XOR should be zero. |
232 | | */ |
233 | 0 | good &= ~(mask & (padding_length ^ b)); |
234 | 0 | } |
235 | | |
236 | | /* |
237 | | * If any of the final |padding_length+1| bytes had the wrong value, one |
238 | | * or more of the lower eight bits of |good| will be cleared. |
239 | | */ |
240 | 0 | good = constant_time_eq_s(0xff, good & 0xff); |
241 | 0 | *reclen -= good & (padding_length + 1); |
242 | 0 | } |
243 | | |
244 | 0 | return ssl3_cbc_copy_mac(reclen, origreclen, recdata, mac, alloced, |
245 | 0 | block_size, mac_size, good, libctx); |
246 | 0 | } |
247 | | |
248 | | /* |
249 | | * Fills a single block of buffered data from the input, and returns the amount |
250 | | * of data remaining in the input that is a multiple of the blocksize. The buffer |
251 | | * is only filled if it already has some data in it, isn't full already or we |
252 | | * don't have at least one block in the input. |
253 | | * |
254 | | * buf: a buffer of blocksize bytes |
255 | | * buflen: contains the amount of data already in buf on entry. Updated with the |
256 | | * amount of data in buf at the end. On entry *buflen must always be |
257 | | * less than the blocksize |
258 | | * blocksize: size of a block. Must be greater than 0 and a power of 2 |
259 | | * in: pointer to a pointer containing the input data |
260 | | * inlen: amount of input data available |
261 | | * |
262 | | * On return buf is filled with as much data as possible up to a full block, |
263 | | * *buflen is updated containing the amount of data in buf. *in is updated to |
264 | | * the new location where input data should be read from, *inlen is updated with |
265 | | * the remaining amount of data in *in. Returns the largest value <= *inlen |
266 | | * which is a multiple of the blocksize. |
267 | | */ |
268 | | size_t ossl_cipher_fillblock(unsigned char *buf, size_t *buflen, |
269 | | size_t blocksize, |
270 | | const unsigned char **in, size_t *inlen) |
271 | 0 | { |
272 | 0 | size_t blockmask = ~(blocksize - 1); |
273 | 0 | size_t bufremain = blocksize - *buflen; |
274 | |
|
275 | 0 | assert(*buflen <= blocksize); |
276 | 0 | assert(blocksize > 0 && (blocksize & (blocksize - 1)) == 0); |
277 | |
|
278 | 0 | if (*inlen < bufremain) |
279 | 0 | bufremain = *inlen; |
280 | 0 | memcpy(buf + *buflen, *in, bufremain); |
281 | 0 | *in += bufremain; |
282 | 0 | *inlen -= bufremain; |
283 | 0 | *buflen += bufremain; |
284 | |
|
285 | 0 | return *inlen & blockmask; |
286 | 0 | } |
287 | | |
288 | | /* |
289 | | * Fills the buffer with trailing data from an encryption/decryption that didn't |
290 | | * fit into a full block. |
291 | | */ |
292 | | int ossl_cipher_trailingdata(unsigned char *buf, size_t *buflen, size_t blocksize, |
293 | | const unsigned char **in, size_t *inlen) |
294 | 0 | { |
295 | 0 | if (*inlen == 0) |
296 | 0 | return 1; |
297 | | |
298 | 0 | if (*buflen + *inlen > blocksize) { |
299 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
300 | 0 | return 0; |
301 | 0 | } |
302 | | |
303 | 0 | memcpy(buf + *buflen, *in, *inlen); |
304 | 0 | *buflen += *inlen; |
305 | 0 | *inlen = 0; |
306 | |
|
307 | 0 | return 1; |
308 | 0 | } |
309 | | |
310 | | /* Pad the final block for encryption */ |
311 | | void ossl_cipher_padblock(unsigned char *buf, size_t *buflen, size_t blocksize) |
312 | 0 | { |
313 | 0 | size_t i; |
314 | 0 | unsigned char pad = (unsigned char)(blocksize - *buflen); |
315 | |
|
316 | 0 | for (i = *buflen; i < blocksize; i++) |
317 | 0 | buf[i] = pad; |
318 | 0 | } |
319 | | |
320 | | int ossl_cipher_unpadblock(unsigned char *buf, size_t *buflen, size_t blocksize) |
321 | 0 | { |
322 | 0 | size_t pad, i; |
323 | 0 | size_t len = *buflen; |
324 | |
|
325 | 0 | if (len != blocksize) { |
326 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
327 | 0 | return 0; |
328 | 0 | } |
329 | | |
330 | | /* |
331 | | * The following assumes that the ciphertext has been authenticated. |
332 | | * Otherwise it provides a padding oracle. |
333 | | */ |
334 | 0 | pad = buf[blocksize - 1]; |
335 | 0 | if (pad == 0 || pad > blocksize) { |
336 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_BAD_DECRYPT); |
337 | 0 | return 0; |
338 | 0 | } |
339 | 0 | for (i = 0; i < pad; i++) { |
340 | 0 | if (buf[--len] != pad) { |
341 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_BAD_DECRYPT); |
342 | 0 | return 0; |
343 | 0 | } |
344 | 0 | } |
345 | 0 | *buflen = len; |
346 | 0 | return 1; |
347 | 0 | } |
348 | | |
349 | | /*- |
350 | | * ossl_cipher_tlsunpadblock removes the CBC padding from the decrypted, TLS, CBC |
351 | | * record in constant time. Also removes the MAC from the record in constant |
352 | | * time. |
353 | | * |
354 | | * libctx: Our library context |
355 | | * tlsversion: The TLS version in use, e.g. TLS1_VERSION, etc |
356 | | * buf: The decrypted TLS record data |
357 | | * buflen: The length of the decrypted TLS record data. Updated with the new |
358 | | * length after the padding is removed |
359 | | * block_size: the block size of the cipher used to encrypt the record. |
360 | | * mac: Location to store the pointer to the MAC |
361 | | * alloced: Whether the MAC is stored in a newly allocated buffer, or whether |
362 | | * *mac points into *buf |
363 | | * macsize: the size of the MAC inside the record (or 0 if there isn't one) |
364 | | * aead: whether this is an aead cipher |
365 | | * returns: |
366 | | * 0: (in non-constant time) if the record is publicly invalid. |
367 | | * 1: (in constant time) Record is publicly valid. If padding is invalid then |
368 | | * the mac is random |
369 | | */ |
370 | | int ossl_cipher_tlsunpadblock(OSSL_LIB_CTX *libctx, unsigned int tlsversion, |
371 | | unsigned char *buf, size_t *buflen, |
372 | | size_t blocksize, |
373 | | unsigned char **mac, int *alloced, size_t macsize, |
374 | | int aead) |
375 | 0 | { |
376 | 0 | int ret; |
377 | |
|
378 | 0 | switch (tlsversion) { |
379 | 0 | case TLS1_2_VERSION: |
380 | 0 | case DTLS1_2_VERSION: |
381 | 0 | case TLS1_1_VERSION: |
382 | 0 | case DTLS1_VERSION: |
383 | 0 | case DTLS1_BAD_VER: |
384 | | /* Remove the explicit IV */ |
385 | 0 | buf += blocksize; |
386 | 0 | *buflen -= blocksize; |
387 | | /* Fall through */ |
388 | 0 | case TLS1_VERSION: |
389 | 0 | ret = tls1_cbc_remove_padding_and_mac(buflen, *buflen, buf, mac, |
390 | 0 | alloced, blocksize, macsize, |
391 | 0 | aead, libctx); |
392 | 0 | return ret; |
393 | | |
394 | 0 | default: |
395 | 0 | return 0; |
396 | 0 | } |
397 | 0 | } |