/src/openssl/crypto/rsa/rsa_pk1.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
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 |
8 | | */ |
9 | | |
10 | | #include "internal/constant_time_locl.h" |
11 | | |
12 | | #include <stdio.h> |
13 | | #include "internal/cryptlib.h" |
14 | | #include <openssl/bn.h> |
15 | | #include <openssl/rsa.h> |
16 | | #include <openssl/rand.h> |
17 | | |
18 | | int RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen, |
19 | | const unsigned char *from, int flen) |
20 | 0 | { |
21 | 0 | int j; |
22 | 0 | unsigned char *p; |
23 | 0 |
|
24 | 0 | if (flen > (tlen - RSA_PKCS1_PADDING_SIZE)) { |
25 | 0 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1, |
26 | 0 | RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
27 | 0 | return 0; |
28 | 0 | } |
29 | 0 |
|
30 | 0 | p = (unsigned char *)to; |
31 | 0 |
|
32 | 0 | *(p++) = 0; |
33 | 0 | *(p++) = 1; /* Private Key BT (Block Type) */ |
34 | 0 |
|
35 | 0 | /* pad out with 0xff data */ |
36 | 0 | j = tlen - 3 - flen; |
37 | 0 | memset(p, 0xff, j); |
38 | 0 | p += j; |
39 | 0 | *(p++) = '\0'; |
40 | 0 | memcpy(p, from, (unsigned int)flen); |
41 | 0 | return 1; |
42 | 0 | } |
43 | | |
44 | | int RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen, |
45 | | const unsigned char *from, int flen, |
46 | | int num) |
47 | 0 | { |
48 | 0 | int i, j; |
49 | 0 | const unsigned char *p; |
50 | 0 |
|
51 | 0 | p = from; |
52 | 0 |
|
53 | 0 | /* |
54 | 0 | * The format is |
55 | 0 | * 00 || 01 || PS || 00 || D |
56 | 0 | * PS - padding string, at least 8 bytes of FF |
57 | 0 | * D - data. |
58 | 0 | */ |
59 | 0 |
|
60 | 0 | if (num < 11) |
61 | 0 | return -1; |
62 | 0 | |
63 | 0 | /* Accept inputs with and without the leading 0-byte. */ |
64 | 0 | if (num == flen) { |
65 | 0 | if ((*p++) != 0x00) { |
66 | 0 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, |
67 | 0 | RSA_R_INVALID_PADDING); |
68 | 0 | return -1; |
69 | 0 | } |
70 | 0 | flen--; |
71 | 0 | } |
72 | 0 |
|
73 | 0 | if ((num != (flen + 1)) || (*(p++) != 0x01)) { |
74 | 0 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, |
75 | 0 | RSA_R_BLOCK_TYPE_IS_NOT_01); |
76 | 0 | return -1; |
77 | 0 | } |
78 | 0 |
|
79 | 0 | /* scan over padding data */ |
80 | 0 | j = flen - 1; /* one for type. */ |
81 | 0 | for (i = 0; i < j; i++) { |
82 | 0 | if (*p != 0xff) { /* should decrypt to 0xff */ |
83 | 0 | if (*p == 0) { |
84 | 0 | p++; |
85 | 0 | break; |
86 | 0 | } else { |
87 | 0 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, |
88 | 0 | RSA_R_BAD_FIXED_HEADER_DECRYPT); |
89 | 0 | return -1; |
90 | 0 | } |
91 | 0 | } |
92 | 0 | p++; |
93 | 0 | } |
94 | 0 |
|
95 | 0 | if (i == j) { |
96 | 0 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, |
97 | 0 | RSA_R_NULL_BEFORE_BLOCK_MISSING); |
98 | 0 | return -1; |
99 | 0 | } |
100 | 0 |
|
101 | 0 | if (i < 8) { |
102 | 0 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, |
103 | 0 | RSA_R_BAD_PAD_BYTE_COUNT); |
104 | 0 | return -1; |
105 | 0 | } |
106 | 0 | i++; /* Skip over the '\0' */ |
107 | 0 | j -= i; |
108 | 0 | if (j > tlen) { |
109 | 0 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, RSA_R_DATA_TOO_LARGE); |
110 | 0 | return -1; |
111 | 0 | } |
112 | 0 | memcpy(to, p, (unsigned int)j); |
113 | 0 |
|
114 | 0 | return j; |
115 | 0 | } |
116 | | |
117 | | int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen, |
118 | | const unsigned char *from, int flen) |
119 | 0 | { |
120 | 0 | int i, j; |
121 | 0 | unsigned char *p; |
122 | 0 |
|
123 | 0 | if (flen > (tlen - 11)) { |
124 | 0 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2, |
125 | 0 | RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
126 | 0 | return 0; |
127 | 0 | } |
128 | 0 |
|
129 | 0 | p = (unsigned char *)to; |
130 | 0 |
|
131 | 0 | *(p++) = 0; |
132 | 0 | *(p++) = 2; /* Public Key BT (Block Type) */ |
133 | 0 |
|
134 | 0 | /* pad out with non-zero random data */ |
135 | 0 | j = tlen - 3 - flen; |
136 | 0 |
|
137 | 0 | if (RAND_bytes(p, j) <= 0) |
138 | 0 | return 0; |
139 | 0 | for (i = 0; i < j; i++) { |
140 | 0 | if (*p == '\0') |
141 | 0 | do { |
142 | 0 | if (RAND_bytes(p, 1) <= 0) |
143 | 0 | return 0; |
144 | 0 | } while (*p == '\0'); |
145 | 0 | p++; |
146 | 0 | } |
147 | 0 |
|
148 | 0 | *(p++) = '\0'; |
149 | 0 |
|
150 | 0 | memcpy(p, from, (unsigned int)flen); |
151 | 0 | return 1; |
152 | 0 | } |
153 | | |
154 | | int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen, |
155 | | const unsigned char *from, int flen, |
156 | | int num) |
157 | 0 | { |
158 | 0 | int i; |
159 | 0 | /* |em| is the encoded message, zero-padded to exactly |num| bytes */ |
160 | 0 | unsigned char *em = NULL; |
161 | 0 | unsigned int good, found_zero_byte; |
162 | 0 | int zero_index = 0, msg_index, mlen = -1; |
163 | 0 |
|
164 | 0 | if (tlen < 0 || flen < 0) |
165 | 0 | return -1; |
166 | 0 | |
167 | 0 | /* |
168 | 0 | * PKCS#1 v1.5 decryption. See "PKCS #1 v2.2: RSA Cryptography Standard", |
169 | 0 | * section 7.2.2. |
170 | 0 | */ |
171 | 0 | |
172 | 0 | if (flen > num) |
173 | 0 | goto err; |
174 | 0 | |
175 | 0 | if (num < 11) |
176 | 0 | goto err; |
177 | 0 | |
178 | 0 | if (flen != num) { |
179 | 0 | em = OPENSSL_zalloc(num); |
180 | 0 | if (em == NULL) { |
181 | 0 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, ERR_R_MALLOC_FAILURE); |
182 | 0 | return -1; |
183 | 0 | } |
184 | 0 | /* |
185 | 0 | * Caller is encouraged to pass zero-padded message created with |
186 | 0 | * BN_bn2binpad, but if it doesn't, we do this zero-padding copy |
187 | 0 | * to avoid leaking that information. The copy still leaks some |
188 | 0 | * side-channel information, but it's impossible to have a fixed |
189 | 0 | * memory access pattern since we can't read out of the bounds of |
190 | 0 | * |from|. |
191 | 0 | */ |
192 | 0 | memcpy(em + num - flen, from, flen); |
193 | 0 | from = em; |
194 | 0 | } |
195 | 0 |
|
196 | 0 | good = constant_time_is_zero(from[0]); |
197 | 0 | good &= constant_time_eq(from[1], 2); |
198 | 0 |
|
199 | 0 | found_zero_byte = 0; |
200 | 0 | for (i = 2; i < num; i++) { |
201 | 0 | unsigned int equals0 = constant_time_is_zero(from[i]); |
202 | 0 | zero_index = |
203 | 0 | constant_time_select_int(~found_zero_byte & equals0, i, |
204 | 0 | zero_index); |
205 | 0 | found_zero_byte |= equals0; |
206 | 0 | } |
207 | 0 |
|
208 | 0 | /* |
209 | 0 | * PS must be at least 8 bytes long, and it starts two bytes into |from|. |
210 | 0 | * If we never found a 0-byte, then |zero_index| is 0 and the check |
211 | 0 | * also fails. |
212 | 0 | */ |
213 | 0 | good &= constant_time_ge((unsigned int)(zero_index), 2 + 8); |
214 | 0 |
|
215 | 0 | /* |
216 | 0 | * Skip the zero byte. This is incorrect if we never found a zero-byte |
217 | 0 | * but in this case we also do not copy the message out. |
218 | 0 | */ |
219 | 0 | msg_index = zero_index + 1; |
220 | 0 | mlen = num - msg_index; |
221 | 0 |
|
222 | 0 | /* |
223 | 0 | * For good measure, do this check in constant time as well; it could |
224 | 0 | * leak something if |tlen| was assuming valid padding. |
225 | 0 | */ |
226 | 0 | good &= constant_time_ge((unsigned int)(tlen), (unsigned int)(mlen)); |
227 | 0 |
|
228 | 0 | /* |
229 | 0 | * We can't continue in constant-time because we need to copy the result |
230 | 0 | * and we cannot fake its length. This unavoidably leaks timing |
231 | 0 | * information at the API boundary. |
232 | 0 | */ |
233 | 0 | if (!good) { |
234 | 0 | mlen = -1; |
235 | 0 | goto err; |
236 | 0 | } |
237 | 0 | |
238 | 0 | memcpy(to, from + msg_index, mlen); |
239 | 0 |
|
240 | 0 | err: |
241 | 0 | OPENSSL_clear_free(em, num); |
242 | 0 | if (mlen == -1) |
243 | 0 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, |
244 | 0 | RSA_R_PKCS_DECODING_ERROR); |
245 | 0 | return mlen; |
246 | 0 | } |