2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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_locl.h"
13 #include "internal/cryptlib.h"
14 #include <openssl/bn.h>
15 #include <openssl/rsa.h>
16 #include <openssl/rand.h>
18 int RSA_padding_add_PKCS1_type_1(unsigned char *to
, int tlen
,
19 const unsigned char *from
, int flen
)
24 if (flen
> (tlen
- RSA_PKCS1_PADDING_SIZE
)) {
25 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1
,
26 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE
);
30 p
= (unsigned char *)to
;
33 *(p
++) = 1; /* Private Key BT (Block Type) */
35 /* pad out with 0xff data */
40 memcpy(p
, from
, (unsigned int)flen
);
44 int RSA_padding_check_PKCS1_type_1(unsigned char *to
, int tlen
,
45 const unsigned char *from
, int flen
,
49 const unsigned char *p
;
55 * 00 || 01 || PS || 00 || D
56 * PS - padding string, at least 8 bytes of FF
63 /* Accept inputs with and without the leading 0-byte. */
66 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1
,
67 RSA_R_INVALID_PADDING
);
73 if ((num
!= (flen
+ 1)) || (*(p
++) != 0x01)) {
74 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1
,
75 RSA_R_BLOCK_TYPE_IS_NOT_01
);
79 /* scan over padding data */
80 j
= flen
- 1; /* one for type. */
81 for (i
= 0; i
< j
; i
++) {
82 if (*p
!= 0xff) { /* should decrypt to 0xff */
87 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1
,
88 RSA_R_BAD_FIXED_HEADER_DECRYPT
);
96 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1
,
97 RSA_R_NULL_BEFORE_BLOCK_MISSING
);
102 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1
,
103 RSA_R_BAD_PAD_BYTE_COUNT
);
106 i
++; /* Skip over the '\0' */
109 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1
, RSA_R_DATA_TOO_LARGE
);
112 memcpy(to
, p
, (unsigned int)j
);
117 int RSA_padding_add_PKCS1_type_2(unsigned char *to
, int tlen
,
118 const unsigned char *from
, int flen
)
123 if (flen
> (tlen
- 11)) {
124 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2
,
125 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE
);
129 p
= (unsigned char *)to
;
132 *(p
++) = 2; /* Public Key BT (Block Type) */
134 /* pad out with non-zero random data */
137 if (RAND_bytes(p
, j
) <= 0)
139 for (i
= 0; i
< j
; i
++) {
142 if (RAND_bytes(p
, 1) <= 0)
144 } while (*p
== '\0');
150 memcpy(p
, from
, (unsigned int)flen
);
154 int RSA_padding_check_PKCS1_type_2(unsigned char *to
, int tlen
,
155 const unsigned char *from
, int flen
,
159 /* |em| is the encoded message, zero-padded to exactly |num| bytes */
160 unsigned char *em
= NULL
;
161 unsigned int good
, found_zero_byte
;
162 int zero_index
= 0, msg_index
, mlen
= -1;
164 if (tlen
< 0 || flen
< 0)
168 * PKCS#1 v1.5 decryption. See "PKCS #1 v2.2: RSA Cryptography Standard",
179 em
= OPENSSL_zalloc(num
);
181 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2
, ERR_R_MALLOC_FAILURE
);
185 * Caller is encouraged to pass zero-padded message created with
186 * BN_bn2binpad, but if it doesn't, we do this zero-padding copy
187 * to avoid leaking that information. The copy still leaks some
188 * side-channel information, but it's impossible to have a fixed
189 * memory access pattern since we can't read out of the bounds of
192 memcpy(em
+ num
- flen
, from
, flen
);
196 good
= constant_time_is_zero(from
[0]);
197 good
&= constant_time_eq(from
[1], 2);
200 for (i
= 2; i
< num
; i
++) {
201 unsigned int equals0
= constant_time_is_zero(from
[i
]);
203 constant_time_select_int(~found_zero_byte
& equals0
, i
,
205 found_zero_byte
|= equals0
;
209 * PS must be at least 8 bytes long, and it starts two bytes into |from|.
210 * If we never found a 0-byte, then |zero_index| is 0 and the check
213 good
&= constant_time_ge((unsigned int)(zero_index
), 2 + 8);
216 * Skip the zero byte. This is incorrect if we never found a zero-byte
217 * but in this case we also do not copy the message out.
219 msg_index
= zero_index
+ 1;
220 mlen
= num
- msg_index
;
223 * For good measure, do this check in constant time as well; it could
224 * leak something if |tlen| was assuming valid padding.
226 good
&= constant_time_ge((unsigned int)(tlen
), (unsigned int)(mlen
));
229 * We can't continue in constant-time because we need to copy the result
230 * and we cannot fake its length. This unavoidably leaks timing
231 * information at the API boundary.
238 memcpy(to
, from
+ msg_index
, mlen
);
241 OPENSSL_clear_free(em
, num
);
243 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2
,
244 RSA_R_PKCS_DECODING_ERROR
);