Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
| 2 | * project 2005. |
| 3 | */ |
| 4 | /* ==================================================================== |
| 5 | * Copyright (c) 2005 The OpenSSL Project. All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * |
| 19 | * 3. All advertising materials mentioning features or use of this |
| 20 | * software must display the following acknowledgment: |
| 21 | * "This product includes software developed by the OpenSSL Project |
| 22 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
| 23 | * |
| 24 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 25 | * endorse or promote products derived from this software without |
| 26 | * prior written permission. For written permission, please contact |
| 27 | * licensing@OpenSSL.org. |
| 28 | * |
| 29 | * 5. Products derived from this software may not be called "OpenSSL" |
| 30 | * nor may "OpenSSL" appear in their names without prior written |
| 31 | * permission of the OpenSSL Project. |
| 32 | * |
| 33 | * 6. Redistributions of any form whatsoever must retain the following |
| 34 | * acknowledgment: |
| 35 | * "This product includes software developed by the OpenSSL Project |
| 36 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
| 37 | * |
| 38 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 39 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 41 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 42 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 43 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 44 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 45 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 46 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 47 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 48 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 49 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 50 | * ==================================================================== |
| 51 | * |
| 52 | * This product includes cryptographic software written by Eric Young |
| 53 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 54 | * Hudson (tjh@cryptsoft.com). */ |
| 55 | |
| 56 | #include <openssl/rsa.h> |
| 57 | |
| 58 | #include <assert.h> |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 59 | #include <limits.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 60 | #include <string.h> |
| 61 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 62 | #include <openssl/bn.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 63 | #include <openssl/digest.h> |
| 64 | #include <openssl/err.h> |
| 65 | #include <openssl/mem.h> |
| 66 | #include <openssl/rand.h> |
| 67 | #include <openssl/sha.h> |
| 68 | |
| 69 | #include "internal.h" |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 70 | #include "../internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 71 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 72 | |
| 73 | #define RSA_PKCS1_PADDING_SIZE 11 |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 74 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 75 | int RSA_padding_add_PKCS1_type_1(uint8_t *to, size_t to_len, |
| 76 | const uint8_t *from, size_t from_len) { |
| 77 | /* See RFC 8017, section 9.2. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 78 | if (to_len < RSA_PKCS1_PADDING_SIZE) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 79 | OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 80 | return 0; |
| 81 | } |
| 82 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 83 | if (from_len > to_len - RSA_PKCS1_PADDING_SIZE) { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 84 | OPENSSL_PUT_ERROR(RSA, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 88 | to[0] = 0; |
| 89 | to[1] = 1; |
| 90 | OPENSSL_memset(to + 2, 0xff, to_len - 3 - from_len); |
| 91 | to[to_len - from_len - 1] = 0; |
| 92 | OPENSSL_memcpy(to + to_len - from_len, from, from_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 93 | return 1; |
| 94 | } |
| 95 | |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 96 | int RSA_padding_check_PKCS1_type_1(uint8_t *out, size_t *out_len, |
| 97 | size_t max_out, const uint8_t *from, |
| 98 | size_t from_len) { |
| 99 | /* See RFC 8017, section 9.2. This is part of signature verification and thus |
| 100 | * does not need to run in constant-time. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 101 | if (from_len < 2) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 102 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_SMALL); |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 103 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 106 | /* Check the header. */ |
| 107 | if (from[0] != 0 || from[1] != 1) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 108 | OPENSSL_PUT_ERROR(RSA, RSA_R_BLOCK_TYPE_IS_NOT_01); |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 109 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 112 | /* Scan over padded data, looking for the 00. */ |
| 113 | size_t pad; |
| 114 | for (pad = 2 /* header */; pad < from_len; pad++) { |
| 115 | if (from[pad] == 0x00) { |
| 116 | break; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 117 | } |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 118 | |
| 119 | if (from[pad] != 0xff) { |
| 120 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_FIXED_HEADER_DECRYPT); |
| 121 | return 0; |
| 122 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 125 | if (pad == from_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 126 | OPENSSL_PUT_ERROR(RSA, RSA_R_NULL_BEFORE_BLOCK_MISSING); |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 127 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 130 | if (pad < 2 /* header */ + 8) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 131 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_PAD_BYTE_COUNT); |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 132 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 133 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 134 | |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 135 | /* Skip over the 00. */ |
| 136 | pad++; |
| 137 | |
| 138 | if (from_len - pad > max_out) { |
| 139 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | OPENSSL_memcpy(out, from + pad, from_len - pad); |
| 144 | *out_len = from_len - pad; |
| 145 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 148 | static int rand_nonzero(uint8_t *out, size_t len) { |
| 149 | if (!RAND_bytes(out, len)) { |
| 150 | return 0; |
| 151 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 152 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 153 | for (size_t i = 0; i < len; i++) { |
| 154 | while (out[i] == 0) { |
| 155 | if (!RAND_bytes(out + i, 1)) { |
| 156 | return 0; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | int RSA_padding_add_PKCS1_type_2(uint8_t *to, size_t to_len, |
| 165 | const uint8_t *from, size_t from_len) { |
| 166 | /* See RFC 8017, section 7.2.1. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 167 | if (to_len < RSA_PKCS1_PADDING_SIZE) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 168 | OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 172 | if (from_len > to_len - RSA_PKCS1_PADDING_SIZE) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 173 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 174 | return 0; |
| 175 | } |
| 176 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 177 | to[0] = 0; |
| 178 | to[1] = 2; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 179 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 180 | size_t padding_len = to_len - 3 - from_len; |
| 181 | if (!rand_nonzero(to + 2, padding_len)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 185 | to[2 + padding_len] = 0; |
| 186 | OPENSSL_memcpy(to + to_len - from_len, from, from_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 187 | return 1; |
| 188 | } |
| 189 | |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 190 | int RSA_padding_check_PKCS1_type_2(uint8_t *out, size_t *out_len, |
| 191 | size_t max_out, const uint8_t *from, |
| 192 | size_t from_len) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 193 | if (from_len == 0) { |
| 194 | OPENSSL_PUT_ERROR(RSA, RSA_R_EMPTY_PUBLIC_KEY); |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 195 | return 0; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 196 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 197 | |
| 198 | /* PKCS#1 v1.5 decryption. See "PKCS #1 v2.2: RSA Cryptography |
| 199 | * Standard", section 7.2.2. */ |
| 200 | if (from_len < RSA_PKCS1_PADDING_SIZE) { |
| 201 | /* |from| is zero-padded to the size of the RSA modulus, a public value, so |
| 202 | * this can be rejected in non-constant time. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 203 | OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL); |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 204 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 207 | crypto_word_t first_byte_is_zero = constant_time_eq_w(from[0], 0); |
| 208 | crypto_word_t second_byte_is_two = constant_time_eq_w(from[1], 2); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 209 | |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 210 | crypto_word_t zero_index = 0, looking_for_index = CONSTTIME_TRUE_W; |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 211 | for (size_t i = 2; i < from_len; i++) { |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 212 | crypto_word_t equals0 = constant_time_is_zero_w(from[i]); |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 213 | zero_index = |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 214 | constant_time_select_w(looking_for_index & equals0, i, zero_index); |
| 215 | looking_for_index = constant_time_select_w(equals0, 0, looking_for_index); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* The input must begin with 00 02. */ |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 219 | crypto_word_t valid_index = first_byte_is_zero; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 220 | valid_index &= second_byte_is_two; |
| 221 | |
| 222 | /* We must have found the end of PS. */ |
| 223 | valid_index &= ~looking_for_index; |
| 224 | |
| 225 | /* PS must be at least 8 bytes long, and it starts two bytes into |from|. */ |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 226 | valid_index &= constant_time_ge_w(zero_index, 2 + 8); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 227 | |
| 228 | /* Skip the zero byte. */ |
| 229 | zero_index++; |
| 230 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 231 | /* NOTE: Although this logic attempts to be constant time, the API contracts |
| 232 | * of this function and |RSA_decrypt| with |RSA_PKCS1_PADDING| make it |
| 233 | * impossible to completely avoid Bleichenbacher's attack. Consumers should |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 234 | * use |RSA_PADDING_NONE| and perform the padding check in constant-time |
| 235 | * combined with a swap to a random session key or other mitigation. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 236 | if (!valid_index) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 237 | OPENSSL_PUT_ERROR(RSA, RSA_R_PKCS_DECODING_ERROR); |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 238 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 241 | const size_t msg_len = from_len - zero_index; |
| 242 | if (msg_len > max_out) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 243 | /* This shouldn't happen because this function is always called with |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 244 | * |max_out| as the key size and |from_len| is bounded by the key size. */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 245 | OPENSSL_PUT_ERROR(RSA, RSA_R_PKCS_DECODING_ERROR); |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 246 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 247 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 248 | |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 249 | OPENSSL_memcpy(out, &from[zero_index], msg_len); |
| 250 | *out_len = msg_len; |
| 251 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 254 | int RSA_padding_add_none(uint8_t *to, size_t to_len, const uint8_t *from, |
| 255 | size_t from_len) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 256 | if (from_len > to_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 257 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 258 | return 0; |
| 259 | } |
| 260 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 261 | if (from_len < to_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 262 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 263 | return 0; |
| 264 | } |
| 265 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 266 | OPENSSL_memcpy(to, from, from_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 267 | return 1; |
| 268 | } |
| 269 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 270 | static int PKCS1_MGF1(uint8_t *out, size_t len, const uint8_t *seed, |
| 271 | size_t seed_len, const EVP_MD *md) { |
| 272 | int ret = 0; |
| 273 | EVP_MD_CTX ctx; |
| 274 | EVP_MD_CTX_init(&ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 275 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 276 | size_t md_len = EVP_MD_size(md); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 277 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 278 | for (uint32_t i = 0; len > 0; i++) { |
| 279 | uint8_t counter[4]; |
| 280 | counter[0] = (uint8_t)(i >> 24); |
| 281 | counter[1] = (uint8_t)(i >> 16); |
| 282 | counter[2] = (uint8_t)(i >> 8); |
| 283 | counter[3] = (uint8_t)i; |
| 284 | if (!EVP_DigestInit_ex(&ctx, md, NULL) || |
| 285 | !EVP_DigestUpdate(&ctx, seed, seed_len) || |
| 286 | !EVP_DigestUpdate(&ctx, counter, sizeof(counter))) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 287 | goto err; |
| 288 | } |
| 289 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 290 | if (md_len <= len) { |
| 291 | if (!EVP_DigestFinal_ex(&ctx, out, NULL)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 292 | goto err; |
| 293 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 294 | out += md_len; |
| 295 | len -= md_len; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 296 | } else { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 297 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 298 | if (!EVP_DigestFinal_ex(&ctx, digest, NULL)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 299 | goto err; |
| 300 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 301 | OPENSSL_memcpy(out, digest, len); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 302 | len = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 303 | } |
| 304 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 305 | |
| 306 | ret = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 307 | |
| 308 | err: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 309 | EVP_MD_CTX_cleanup(&ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 310 | return ret; |
| 311 | } |
| 312 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 313 | int RSA_padding_add_PKCS1_OAEP_mgf1(uint8_t *to, size_t to_len, |
| 314 | const uint8_t *from, size_t from_len, |
| 315 | const uint8_t *param, size_t param_len, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 316 | const EVP_MD *md, const EVP_MD *mgf1md) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 317 | if (md == NULL) { |
| 318 | md = EVP_sha1(); |
| 319 | } |
| 320 | if (mgf1md == NULL) { |
| 321 | mgf1md = md; |
| 322 | } |
| 323 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 324 | size_t mdlen = EVP_MD_size(md); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 325 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 326 | if (to_len < 2 * mdlen + 2) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 327 | OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 331 | size_t emlen = to_len - 1; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 332 | if (from_len > emlen - 2 * mdlen - 1) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 333 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | if (emlen < 2 * mdlen + 1) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 338 | OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | to[0] = 0; |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 343 | uint8_t *seed = to + 1; |
| 344 | uint8_t *db = to + mdlen + 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 345 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 346 | if (!EVP_Digest(param, param_len, db, NULL, md, NULL)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 347 | return 0; |
| 348 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 349 | OPENSSL_memset(db + mdlen, 0, emlen - from_len - 2 * mdlen - 1); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 350 | db[emlen - from_len - mdlen - 1] = 0x01; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 351 | OPENSSL_memcpy(db + emlen - from_len - mdlen, from, from_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 352 | if (!RAND_bytes(seed, mdlen)) { |
| 353 | return 0; |
| 354 | } |
| 355 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 356 | uint8_t *dbmask = OPENSSL_malloc(emlen - mdlen); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 357 | if (dbmask == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 358 | OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 362 | int ret = 0; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 363 | if (!PKCS1_MGF1(dbmask, emlen - mdlen, seed, mdlen, mgf1md)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 364 | goto out; |
| 365 | } |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 366 | for (size_t i = 0; i < emlen - mdlen; i++) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 367 | db[i] ^= dbmask[i]; |
| 368 | } |
| 369 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 370 | uint8_t seedmask[EVP_MAX_MD_SIZE]; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 371 | if (!PKCS1_MGF1(seedmask, mdlen, db, emlen - mdlen, mgf1md)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 372 | goto out; |
| 373 | } |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 374 | for (size_t i = 0; i < mdlen; i++) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 375 | seed[i] ^= seedmask[i]; |
| 376 | } |
| 377 | ret = 1; |
| 378 | |
| 379 | out: |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 380 | OPENSSL_free(dbmask); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 381 | return ret; |
| 382 | } |
| 383 | |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 384 | int RSA_padding_check_PKCS1_OAEP_mgf1(uint8_t *out, size_t *out_len, |
| 385 | size_t max_out, const uint8_t *from, |
| 386 | size_t from_len, const uint8_t *param, |
| 387 | size_t param_len, const EVP_MD *md, |
| 388 | const EVP_MD *mgf1md) { |
| 389 | uint8_t *db = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 390 | |
| 391 | if (md == NULL) { |
| 392 | md = EVP_sha1(); |
| 393 | } |
| 394 | if (mgf1md == NULL) { |
| 395 | mgf1md = md; |
| 396 | } |
| 397 | |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 398 | size_t mdlen = EVP_MD_size(md); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 399 | |
| 400 | /* The encoded message is one byte smaller than the modulus to ensure that it |
| 401 | * doesn't end up greater than the modulus. Thus there's an extra "+1" here |
| 402 | * compared to https://tools.ietf.org/html/rfc2437#section-9.1.1.2. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 403 | if (from_len < 1 + 2*mdlen + 1) { |
| 404 | /* 'from_len' is the length of the modulus, i.e. does not depend on the |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 405 | * particular ciphertext. */ |
| 406 | goto decoding_err; |
| 407 | } |
| 408 | |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 409 | size_t dblen = from_len - mdlen - 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 410 | db = OPENSSL_malloc(dblen); |
| 411 | if (db == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 412 | OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 413 | goto err; |
| 414 | } |
| 415 | |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 416 | const uint8_t *maskedseed = from + 1; |
| 417 | const uint8_t *maskeddb = from + 1 + mdlen; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 418 | |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 419 | uint8_t seed[EVP_MAX_MD_SIZE]; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 420 | if (!PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 421 | goto err; |
| 422 | } |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 423 | for (size_t i = 0; i < mdlen; i++) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 424 | seed[i] ^= maskedseed[i]; |
| 425 | } |
| 426 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 427 | if (!PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 428 | goto err; |
| 429 | } |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 430 | for (size_t i = 0; i < dblen; i++) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 431 | db[i] ^= maskeddb[i]; |
| 432 | } |
| 433 | |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 434 | uint8_t phash[EVP_MAX_MD_SIZE]; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 435 | if (!EVP_Digest(param, param_len, phash, NULL, md, NULL)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 436 | goto err; |
| 437 | } |
| 438 | |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 439 | crypto_word_t bad = ~constant_time_is_zero_w(CRYPTO_memcmp(db, phash, mdlen)); |
| 440 | bad |= ~constant_time_is_zero_w(from[0]); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 441 | |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 442 | crypto_word_t looking_for_one_byte = CONSTTIME_TRUE_W; |
| 443 | size_t one_index = 0; |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 444 | for (size_t i = mdlen; i < dblen; i++) { |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 445 | crypto_word_t equals1 = constant_time_eq_w(db[i], 1); |
| 446 | crypto_word_t equals0 = constant_time_eq_w(db[i], 0); |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 447 | one_index = |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 448 | constant_time_select_w(looking_for_one_byte & equals1, i, one_index); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 449 | looking_for_one_byte = |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 450 | constant_time_select_w(equals1, 0, looking_for_one_byte); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 451 | bad |= looking_for_one_byte & ~equals0; |
| 452 | } |
| 453 | |
| 454 | bad |= looking_for_one_byte; |
| 455 | |
| 456 | if (bad) { |
| 457 | goto decoding_err; |
| 458 | } |
| 459 | |
| 460 | one_index++; |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 461 | size_t mlen = dblen - one_index; |
| 462 | if (max_out < mlen) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 463 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE); |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 464 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 465 | } |
| 466 | |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 467 | OPENSSL_memcpy(out, db + one_index, mlen); |
| 468 | *out_len = mlen; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 469 | OPENSSL_free(db); |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 470 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 471 | |
| 472 | decoding_err: |
| 473 | /* to avoid chosen ciphertext attacks, the error message should not reveal |
| 474 | * which kind of decoding error happened */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 475 | OPENSSL_PUT_ERROR(RSA, RSA_R_OAEP_DECODING_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 476 | err: |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 477 | OPENSSL_free(db); |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 478 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 481 | static const uint8_t kPSSZeroes[] = {0, 0, 0, 0, 0, 0, 0, 0}; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 482 | |
| 483 | int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const uint8_t *mHash, |
| 484 | const EVP_MD *Hash, const EVP_MD *mgf1Hash, |
| 485 | const uint8_t *EM, int sLen) { |
| 486 | int i; |
| 487 | int ret = 0; |
| 488 | int maskedDBLen, MSBits, emLen; |
| 489 | size_t hLen; |
| 490 | const uint8_t *H; |
| 491 | uint8_t *DB = NULL; |
| 492 | EVP_MD_CTX ctx; |
| 493 | uint8_t H_[EVP_MAX_MD_SIZE]; |
| 494 | EVP_MD_CTX_init(&ctx); |
| 495 | |
| 496 | if (mgf1Hash == NULL) { |
| 497 | mgf1Hash = Hash; |
| 498 | } |
| 499 | |
| 500 | hLen = EVP_MD_size(Hash); |
| 501 | |
| 502 | /* Negative sLen has special meanings: |
| 503 | * -1 sLen == hLen |
| 504 | * -2 salt length is autorecovered from signature |
| 505 | * -N reserved */ |
| 506 | if (sLen == -1) { |
| 507 | sLen = hLen; |
| 508 | } else if (sLen == -2) { |
| 509 | sLen = -2; |
| 510 | } else if (sLen < -2) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 511 | OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 512 | goto err; |
| 513 | } |
| 514 | |
| 515 | MSBits = (BN_num_bits(rsa->n) - 1) & 0x7; |
| 516 | emLen = RSA_size(rsa); |
| 517 | if (EM[0] & (0xFF << MSBits)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 518 | OPENSSL_PUT_ERROR(RSA, RSA_R_FIRST_OCTET_INVALID); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 519 | goto err; |
| 520 | } |
| 521 | if (MSBits == 0) { |
| 522 | EM++; |
| 523 | emLen--; |
| 524 | } |
Robert Sloan | 8ecb7cd | 2017-03-21 09:39:01 -0700 | [diff] [blame] | 525 | if (emLen < (int)hLen + 2 || emLen < ((int)hLen + sLen + 2)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 526 | /* sLen can be small negative */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 527 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 528 | goto err; |
| 529 | } |
| 530 | if (EM[emLen - 1] != 0xbc) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 531 | OPENSSL_PUT_ERROR(RSA, RSA_R_LAST_OCTET_INVALID); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 532 | goto err; |
| 533 | } |
| 534 | maskedDBLen = emLen - hLen - 1; |
| 535 | H = EM + maskedDBLen; |
| 536 | DB = OPENSSL_malloc(maskedDBLen); |
| 537 | if (!DB) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 538 | OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 539 | goto err; |
| 540 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 541 | if (!PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 542 | goto err; |
| 543 | } |
| 544 | for (i = 0; i < maskedDBLen; i++) { |
| 545 | DB[i] ^= EM[i]; |
| 546 | } |
| 547 | if (MSBits) { |
| 548 | DB[0] &= 0xFF >> (8 - MSBits); |
| 549 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 550 | for (i = 0; DB[i] == 0 && i < (maskedDBLen - 1); i++) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 551 | ; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 552 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 553 | if (DB[i++] != 0x1) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 554 | OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_RECOVERY_FAILED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 555 | goto err; |
| 556 | } |
| 557 | if (sLen >= 0 && (maskedDBLen - i) != sLen) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 558 | OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 559 | goto err; |
| 560 | } |
| 561 | if (!EVP_DigestInit_ex(&ctx, Hash, NULL) || |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 562 | !EVP_DigestUpdate(&ctx, kPSSZeroes, sizeof(kPSSZeroes)) || |
| 563 | !EVP_DigestUpdate(&ctx, mHash, hLen) || |
| 564 | !EVP_DigestUpdate(&ctx, DB + i, maskedDBLen - i) || |
| 565 | !EVP_DigestFinal_ex(&ctx, H_, NULL)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 566 | goto err; |
| 567 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 568 | if (OPENSSL_memcmp(H_, H, hLen)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 569 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_SIGNATURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 570 | ret = 0; |
| 571 | } else { |
| 572 | ret = 1; |
| 573 | } |
| 574 | |
| 575 | err: |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 576 | OPENSSL_free(DB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 577 | EVP_MD_CTX_cleanup(&ctx); |
| 578 | |
| 579 | return ret; |
| 580 | } |
| 581 | |
| 582 | int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM, |
| 583 | const unsigned char *mHash, |
| 584 | const EVP_MD *Hash, const EVP_MD *mgf1Hash, |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 585 | int sLenRequested) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 586 | int ret = 0; |
| 587 | size_t maskedDBLen, MSBits, emLen; |
| 588 | size_t hLen; |
| 589 | unsigned char *H, *salt = NULL, *p; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 590 | |
| 591 | if (mgf1Hash == NULL) { |
| 592 | mgf1Hash = Hash; |
| 593 | } |
| 594 | |
| 595 | hLen = EVP_MD_size(Hash); |
| 596 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 597 | if (BN_is_zero(rsa->n)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 598 | OPENSSL_PUT_ERROR(RSA, RSA_R_EMPTY_PUBLIC_KEY); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 599 | goto err; |
| 600 | } |
| 601 | |
| 602 | MSBits = (BN_num_bits(rsa->n) - 1) & 0x7; |
| 603 | emLen = RSA_size(rsa); |
| 604 | if (MSBits == 0) { |
| 605 | assert(emLen >= 1); |
| 606 | *EM++ = 0; |
| 607 | emLen--; |
| 608 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 609 | |
| 610 | if (emLen < hLen + 2) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 611 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 612 | goto err; |
| 613 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 614 | |
| 615 | /* Negative sLenRequested has special meanings: |
| 616 | * -1 sLen == hLen |
| 617 | * -2 salt length is maximized |
| 618 | * -N reserved */ |
| 619 | size_t sLen; |
| 620 | if (sLenRequested == -1) { |
| 621 | sLen = hLen; |
| 622 | } else if (sLenRequested == -2) { |
| 623 | sLen = emLen - hLen - 2; |
| 624 | } else if (sLenRequested < 0) { |
| 625 | OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED); |
| 626 | goto err; |
| 627 | } else { |
| 628 | sLen = (size_t)sLenRequested; |
| 629 | } |
| 630 | |
| 631 | if (emLen - hLen - 2 < sLen) { |
| 632 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
| 633 | goto err; |
| 634 | } |
| 635 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 636 | if (sLen > 0) { |
| 637 | salt = OPENSSL_malloc(sLen); |
| 638 | if (!salt) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 639 | OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 640 | goto err; |
| 641 | } |
| 642 | if (!RAND_bytes(salt, sLen)) { |
| 643 | goto err; |
| 644 | } |
| 645 | } |
| 646 | maskedDBLen = emLen - hLen - 1; |
| 647 | H = EM + maskedDBLen; |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 648 | |
| 649 | EVP_MD_CTX ctx; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 650 | EVP_MD_CTX_init(&ctx); |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 651 | int digest_ok = EVP_DigestInit_ex(&ctx, Hash, NULL) && |
| 652 | EVP_DigestUpdate(&ctx, kPSSZeroes, sizeof(kPSSZeroes)) && |
| 653 | EVP_DigestUpdate(&ctx, mHash, hLen) && |
| 654 | EVP_DigestUpdate(&ctx, salt, sLen) && |
| 655 | EVP_DigestFinal_ex(&ctx, H, NULL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 656 | EVP_MD_CTX_cleanup(&ctx); |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 657 | if (!digest_ok) { |
| 658 | goto err; |
| 659 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 660 | |
| 661 | /* Generate dbMask in place then perform XOR on it */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 662 | if (!PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 663 | goto err; |
| 664 | } |
| 665 | |
| 666 | p = EM; |
| 667 | |
| 668 | /* Initial PS XORs with all zeroes which is a NOP so just update |
| 669 | * pointer. Note from a test above this value is guaranteed to |
| 670 | * be non-negative. */ |
| 671 | p += emLen - sLen - hLen - 2; |
| 672 | *p++ ^= 0x1; |
| 673 | if (sLen > 0) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 674 | for (size_t i = 0; i < sLen; i++) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 675 | *p++ ^= salt[i]; |
| 676 | } |
| 677 | } |
| 678 | if (MSBits) { |
| 679 | EM[0] &= 0xFF >> (8 - MSBits); |
| 680 | } |
| 681 | |
| 682 | /* H is already in place so just set final 0xbc */ |
| 683 | |
| 684 | EM[emLen - 1] = 0xbc; |
| 685 | |
| 686 | ret = 1; |
| 687 | |
| 688 | err: |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 689 | OPENSSL_free(salt); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 690 | |
| 691 | return ret; |
| 692 | } |