1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
58 #include "internal/constant_time_locl.h"
61 #include "internal/cryptlib.h"
62 #include <openssl/bn.h>
63 #include <openssl/rsa.h>
64 #include <openssl/rand.h>
66 int RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen,
67 const unsigned char *from, int flen)
72 if (flen > (tlen - RSA_PKCS1_PADDING_SIZE)) {
73 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1,
74 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
78 p = (unsigned char *)to;
81 *(p++) = 1; /* Private Key BT (Block Type) */
83 /* pad out with 0xff data */
88 memcpy(p, from, (unsigned int)flen);
92 int RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen,
93 const unsigned char *from, int flen,
97 const unsigned char *p;
103 * 00 || 01 || PS || 00 || D
104 * PS - padding string, at least 8 bytes of FF
111 /* Accept inputs with and without the leading 0-byte. */
113 if ((*p++) != 0x00) {
114 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
115 RSA_R_INVALID_PADDING);
121 if ((num != (flen + 1)) || (*(p++) != 0x01)) {
122 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
123 RSA_R_BLOCK_TYPE_IS_NOT_01);
127 /* scan over padding data */
128 j = flen - 1; /* one for type. */
129 for (i = 0; i < j; i++) {
130 if (*p != 0xff) { /* should decrypt to 0xff */
135 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
136 RSA_R_BAD_FIXED_HEADER_DECRYPT);
144 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
145 RSA_R_NULL_BEFORE_BLOCK_MISSING);
150 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
151 RSA_R_BAD_PAD_BYTE_COUNT);
154 i++; /* Skip over the '\0' */
157 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, RSA_R_DATA_TOO_LARGE);
160 memcpy(to, p, (unsigned int)j);
165 int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen,
166 const unsigned char *from, int flen)
171 if (flen > (tlen - 11)) {
172 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2,
173 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
177 p = (unsigned char *)to;
180 *(p++) = 2; /* Public Key BT (Block Type) */
182 /* pad out with non-zero random data */
185 if (RAND_bytes(p, j) <= 0)
187 for (i = 0; i < j; i++) {
190 if (RAND_bytes(p, 1) <= 0)
192 } while (*p == '\0');
198 memcpy(p, from, (unsigned int)flen);
202 int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen,
203 const unsigned char *from, int flen,
207 /* |em| is the encoded message, zero-padded to exactly |num| bytes */
208 unsigned char *em = NULL;
209 unsigned int good, found_zero_byte;
210 int zero_index = 0, msg_index, mlen = -1;
212 if (tlen < 0 || flen < 0)
216 * PKCS#1 v1.5 decryption. See "PKCS #1 v2.2: RSA Cryptography Standard",
226 em = OPENSSL_zalloc(num);
228 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, ERR_R_MALLOC_FAILURE);
232 * Always do this zero-padding copy (even when num == flen) to avoid
233 * leaking that information. The copy still leaks some side-channel
234 * information, but it's impossible to have a fixed memory access
235 * pattern since we can't read out of the bounds of |from|.
237 * TODO(emilia): Consider porting BN_bn2bin_padded from BoringSSL.
239 memcpy(em + num - flen, from, flen);
241 good = constant_time_is_zero(em[0]);
242 good &= constant_time_eq(em[1], 2);
245 for (i = 2; i < num; i++) {
246 unsigned int equals0 = constant_time_is_zero(em[i]);
248 constant_time_select_int(~found_zero_byte & equals0, i,
250 found_zero_byte |= equals0;
254 * PS must be at least 8 bytes long, and it starts two bytes into |em|.
255 * If we never found a 0-byte, then |zero_index| is 0 and the check
258 good &= constant_time_ge((unsigned int)(zero_index), 2 + 8);
261 * Skip the zero byte. This is incorrect if we never found a zero-byte
262 * but in this case we also do not copy the message out.
264 msg_index = zero_index + 1;
265 mlen = num - msg_index;
268 * For good measure, do this check in constant time as well; it could
269 * leak something if |tlen| was assuming valid padding.
271 good &= constant_time_ge((unsigned int)(tlen), (unsigned int)(mlen));
274 * We can't continue in constant-time because we need to copy the result
275 * and we cannot fake its length. This unavoidably leaks timing
276 * information at the API boundary.
277 * TODO(emilia): this could be addressed at the call site,
278 * see BoringSSL commit 0aa0767340baf925bda4804882aab0cb974b2d26.
285 memcpy(to, em + msg_index, mlen);
290 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,
291 RSA_R_PKCS_DECODING_ERROR);