2 * Copyright 1995-2019 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 "internal/constant_time.h"
13 #include <openssl/bn.h>
14 #include <openssl/rsa.h>
15 #include <openssl/rand.h>
16 /* Just for the SSL_MAX_MASTER_KEY_LENGTH value */
17 #include <openssl/ssl.h>
18 #include "internal/cryptlib.h"
19 #include "crypto/rsa.h"
21 int RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen,
22 const unsigned char *from, int flen)
27 if (flen > (tlen - RSA_PKCS1_PADDING_SIZE)) {
28 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1,
29 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
33 p = (unsigned char *)to;
36 *(p++) = 1; /* Private Key BT (Block Type) */
38 /* pad out with 0xff data */
43 memcpy(p, from, (unsigned int)flen);
47 int RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen,
48 const unsigned char *from, int flen,
52 const unsigned char *p;
58 * 00 || 01 || PS || 00 || D
59 * PS - padding string, at least 8 bytes of FF
63 if (num < RSA_PKCS1_PADDING_SIZE)
66 /* Accept inputs with and without the leading 0-byte. */
69 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
70 RSA_R_INVALID_PADDING);
76 if ((num != (flen + 1)) || (*(p++) != 0x01)) {
77 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
78 RSA_R_BLOCK_TYPE_IS_NOT_01);
82 /* scan over padding data */
83 j = flen - 1; /* one for type. */
84 for (i = 0; i < j; i++) {
85 if (*p != 0xff) { /* should decrypt to 0xff */
90 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
91 RSA_R_BAD_FIXED_HEADER_DECRYPT);
99 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
100 RSA_R_NULL_BEFORE_BLOCK_MISSING);
105 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
106 RSA_R_BAD_PAD_BYTE_COUNT);
109 i++; /* Skip over the '\0' */
112 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, RSA_R_DATA_TOO_LARGE);
115 memcpy(to, p, (unsigned int)j);
120 int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen,
121 const unsigned char *from, int flen)
126 if (flen > (tlen - RSA_PKCS1_PADDING_SIZE)) {
127 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2,
128 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
132 p = (unsigned char *)to;
135 *(p++) = 2; /* Public Key BT (Block Type) */
137 /* pad out with non-zero random data */
140 if (RAND_bytes(p, j) <= 0)
142 for (i = 0; i < j; i++) {
145 if (RAND_bytes(p, 1) <= 0)
147 } while (*p == '\0');
153 memcpy(p, from, (unsigned int)flen);
157 int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen,
158 const unsigned char *from, int flen,
162 /* |em| is the encoded message, zero-padded to exactly |num| bytes */
163 unsigned char *em = NULL;
164 unsigned int good, found_zero_byte, mask;
165 int zero_index = 0, msg_index, mlen = -1;
167 if (tlen <= 0 || flen <= 0)
171 * PKCS#1 v1.5 decryption. See "PKCS #1 v2.2: RSA Cryptography Standard",
175 if (flen > num || num < RSA_PKCS1_PADDING_SIZE) {
176 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,
177 RSA_R_PKCS_DECODING_ERROR);
181 em = OPENSSL_malloc(num);
183 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, ERR_R_MALLOC_FAILURE);
187 * Caller is encouraged to pass zero-padded message created with
188 * BN_bn2binpad. Trouble is that since we can't read out of |from|'s
189 * bounds, it's impossible to have an invariant memory access pattern
190 * in case |from| was not zero-padded in advance.
192 for (from += flen, em += num, i = 0; i < num; i++) {
193 mask = ~constant_time_is_zero(flen);
196 *--em = *from & mask;
199 good = constant_time_is_zero(em[0]);
200 good &= constant_time_eq(em[1], 2);
202 /* scan over padding data */
204 for (i = 2; i < num; i++) {
205 unsigned int equals0 = constant_time_is_zero(em[i]);
207 zero_index = constant_time_select_int(~found_zero_byte & equals0,
209 found_zero_byte |= equals0;
213 * PS must be at least 8 bytes long, and it starts two bytes into |em|.
214 * If we never found a 0-byte, then |zero_index| is 0 and the check
217 good &= constant_time_ge(zero_index, 2 + 8);
220 * Skip the zero byte. This is incorrect if we never found a zero-byte
221 * but in this case we also do not copy the message out.
223 msg_index = zero_index + 1;
224 mlen = num - msg_index;
227 * For good measure, do this check in constant time as well.
229 good &= constant_time_ge(tlen, mlen);
232 * Move the result in-place by |num|-RSA_PKCS1_PADDING_SIZE-|mlen| bytes to the left.
233 * Then if |good| move |mlen| bytes from |em|+RSA_PKCS1_PADDING_SIZE to |to|.
234 * Otherwise leave |to| unchanged.
235 * Copy the memory back in a way that does not reveal the size of
236 * the data being copied via a timing side channel. This requires copying
237 * parts of the buffer multiple times based on the bits set in the real
238 * length. Clear bits do a non-copy with identical access pattern.
239 * The loop below has overall complexity of O(N*log(N)).
241 tlen = constant_time_select_int(constant_time_lt(num - RSA_PKCS1_PADDING_SIZE, tlen),
242 num - RSA_PKCS1_PADDING_SIZE, tlen);
243 for (msg_index = 1; msg_index < num - RSA_PKCS1_PADDING_SIZE; msg_index <<= 1) {
244 mask = ~constant_time_eq(msg_index & (num - RSA_PKCS1_PADDING_SIZE - mlen), 0);
245 for (i = RSA_PKCS1_PADDING_SIZE; i < num - msg_index; i++)
246 em[i] = constant_time_select_8(mask, em[i + msg_index], em[i]);
248 for (i = 0; i < tlen; i++) {
249 mask = good & constant_time_lt(i, mlen);
250 to[i] = constant_time_select_8(mask, em[i + RSA_PKCS1_PADDING_SIZE], to[i]);
253 OPENSSL_clear_free(em, num);
254 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, RSA_R_PKCS_DECODING_ERROR);
255 err_clear_last_constant_time(1 & good);
257 return constant_time_select_int(good, mlen, -1);
261 * rsa_padding_check_PKCS1_type_2_TLS() checks and removes the PKCS1 type 2
262 * padding from a decrypted RSA message in a TLS signature. The result is stored
263 * in the buffer pointed to by |to| which should be |tlen| bytes long. |tlen|
264 * must be at least SSL_MAX_MASTER_KEY_LENGTH. The original decrypted message
265 * should be stored in |from| which must be |flen| bytes in length and padded
266 * such that |flen == RSA_size()|. The TLS protocol version that the client
267 * originally requested should be passed in |client_version|. Some buggy clients
268 * can exist which use the negotiated version instead of the originally
269 * requested protocol version. If it is necessary to work around this bug then
270 * the negotiated protocol version can be passed in |alt_version|, otherwise 0
273 * If the passed message is publicly invalid or some other error that can be
274 * treated in non-constant time occurs then -1 is returned. On success the
275 * length of the decrypted data is returned. This will always be
276 * SSL_MAX_MASTER_KEY_LENGTH. If an error occurs that should be treated in
277 * constant time then this function will appear to return successfully, but the
278 * decrypted data will be randomly generated (as per
279 * https://tools.ietf.org/html/rfc5246#section-7.4.7.1).
281 int rsa_padding_check_PKCS1_type_2_TLS(unsigned char *to, size_t tlen,
282 const unsigned char *from, size_t flen,
283 int client_version, int alt_version)
285 unsigned int i, good, version_good;
286 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
289 * If these checks fail then either the message in publicly invalid, or
290 * we've been called incorrectly. We can fail immediately.
292 if (flen < RSA_PKCS1_PADDING_SIZE + SSL_MAX_MASTER_KEY_LENGTH
293 || tlen < SSL_MAX_MASTER_KEY_LENGTH) {
294 ERR_raise(ERR_LIB_RSA, RSA_R_PKCS_DECODING_ERROR);
299 * Generate a random premaster secret to use in the event that we fail
302 if (RAND_priv_bytes(rand_premaster_secret,
303 sizeof(rand_premaster_secret)) <= 0) {
304 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
308 good = constant_time_is_zero(from[0]);
309 good &= constant_time_eq(from[1], 2);
311 /* Check we have the expected padding data */
312 for (i = 2; i < flen - SSL_MAX_MASTER_KEY_LENGTH - 1; i++)
313 good &= ~constant_time_is_zero_8(from[i]);
314 good &= constant_time_is_zero_8(from[flen - SSL_MAX_MASTER_KEY_LENGTH - 1]);
318 * If the version in the decrypted pre-master secret is correct then
319 * version_good will be 0xff, otherwise it'll be zero. The
320 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
321 * (http://eprint.iacr.org/2003/052/) exploits the version number
322 * check as a "bad version oracle". Thus version checks are done in
323 * constant time and are treated like any other decryption error.
326 constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH],
327 (client_version >> 8) & 0xff);
329 constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH + 1],
330 client_version & 0xff);
333 * The premaster secret must contain the same version number as the
334 * ClientHello to detect version rollback attacks (strangely, the
335 * protocol does not offer such protection for DH ciphersuites).
336 * However, buggy clients exist that send the negotiated protocol
337 * version instead if the server does not support the requested
338 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set then we tolerate
339 * such clients. In that case alt_version will be non-zero and set to
340 * the negotiated version.
342 if (alt_version > 0) {
343 unsigned int workaround_good;
346 constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH],
347 (alt_version >> 8) & 0xff);
349 constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH + 1],
351 version_good |= workaround_good;
354 good &= version_good;
358 * Now copy the result over to the to buffer if good, or random data if
361 for (i = 0; i < SSL_MAX_MASTER_KEY_LENGTH; i++) {
363 constant_time_select_8(good,
364 from[flen - SSL_MAX_MASTER_KEY_LENGTH + i],
365 rand_premaster_secret[i]);
369 * We must not leak whether a decryption failure occurs because of
370 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
371 * section 7.4.7.1). The code follows that advice of the TLS RFC and
372 * generates a random premaster secret for the case that the decrypt
373 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
374 * So, whether we actually succeeded or not, return success.
377 return SSL_MAX_MASTER_KEY_LENGTH;