| 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> |
| 59 | #include <string.h> |
| 60 |
| 61 | #include <openssl/digest.h> |
| 62 | #include <openssl/err.h> |
| 63 | #include <openssl/mem.h> |
| 64 | #include <openssl/rand.h> |
| 65 | #include <openssl/sha.h> |
| 66 |
| 67 | #include "internal.h" |
| 68 |
| 69 | /* TODO(fork): don't the check functions have to be constant time? */ |
| 70 |
| 71 | int RSA_padding_add_PKCS1_type_1(uint8_t *to, unsigned tlen, |
| 72 | const uint8_t *from, unsigned flen) { |
| 73 | unsigned j; |
| 74 | uint8_t *p; |
| 75 |
| 76 | if (tlen < RSA_PKCS1_PADDING_SIZE) { |
| 77 | OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL); |
| 78 | return 0; |
| 79 | } |
| 80 |
| 81 | if (flen > tlen - RSA_PKCS1_PADDING_SIZE) { |
| 82 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
| 83 | return 0; |
| 84 | } |
| 85 |
| 86 | p = (uint8_t *)to; |
| 87 |
| 88 | *(p++) = 0; |
| 89 | *(p++) = 1; /* Private Key BT (Block Type) */ |
| 90 |
| 91 | /* pad out with 0xff data */ |
| 92 | j = tlen - 3 - flen; |
| 93 | memset(p, 0xff, j); |
| 94 | p += j; |
| 95 | *(p++) = 0; |
| 96 | memcpy(p, from, (unsigned int)flen); |
| 97 | return 1; |
| 98 | } |
| 99 |
| 100 | int RSA_padding_check_PKCS1_type_1(uint8_t *to, unsigned tlen, |
| 101 | const uint8_t *from, unsigned flen) { |
| 102 | unsigned i, j; |
| 103 | const uint8_t *p; |
| 104 |
| 105 | if (flen < 2) { |
| 106 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_SMALL); |
| 107 | return -1; |
| 108 | } |
| 109 |
| 110 | p = from; |
| 111 | if ((*(p++) != 0) || (*(p++) != 1)) { |
| 112 | OPENSSL_PUT_ERROR(RSA, RSA_R_BLOCK_TYPE_IS_NOT_01); |
| 113 | return -1; |
| 114 | } |
| 115 |
| 116 | /* scan over padding data */ |
| 117 | j = flen - 2; /* one for leading 00, one for type. */ |
| 118 | for (i = 0; i < j; i++) { |
| 119 | /* should decrypt to 0xff */ |
| 120 | if (*p != 0xff) { |
| 121 | if (*p == 0) { |
| 122 | p++; |
| 123 | break; |
| 124 | } else { |
| 125 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_FIXED_HEADER_DECRYPT); |
| 126 | return -1; |
| 127 | } |
| 128 | } |
| 129 | p++; |
| 130 | } |
| 131 |
| 132 | if (i == j) { |
| 133 | OPENSSL_PUT_ERROR(RSA, RSA_R_NULL_BEFORE_BLOCK_MISSING); |
| 134 | return -1; |
| 135 | } |
| 136 |
| 137 | if (i < 8) { |
| 138 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_PAD_BYTE_COUNT); |
| 139 | return -1; |
| 140 | } |
| 141 | i++; /* Skip over the '\0' */ |
| 142 | j -= i; |
| 143 | if (j > tlen) { |
| 144 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE); |
| 145 | return -1; |
| 146 | } |
| 147 | memcpy(to, p, j); |
| 148 |
| 149 | return j; |
| 150 | } |
| 151 |
| 152 | int RSA_padding_add_PKCS1_type_2(uint8_t *to, unsigned tlen, |
| 153 | const uint8_t *from, unsigned flen) { |
| 154 | unsigned i, j; |
| 155 | uint8_t *p; |
| 156 |
| 157 | if (tlen < RSA_PKCS1_PADDING_SIZE) { |
| 158 | OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL); |
| 159 | return 0; |
| 160 | } |
| 161 |
| 162 | if (flen > tlen - RSA_PKCS1_PADDING_SIZE) { |
| 163 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
| 164 | return 0; |
| 165 | } |
| 166 |
| 167 | p = (unsigned char *)to; |
| 168 |
| 169 | *(p++) = 0; |
| 170 | *(p++) = 2; /* Public Key BT (Block Type) */ |
| 171 |
| 172 | /* pad out with non-zero random data */ |
| 173 | j = tlen - 3 - flen; |
| 174 |
| 175 | if (!RAND_bytes(p, j)) { |
| 176 | return 0; |
| 177 | } |
| 178 |
| 179 | for (i = 0; i < j; i++) { |
| 180 | while (*p == 0) { |
| 181 | if (!RAND_bytes(p, 1)) { |
| 182 | return 0; |
| 183 | } |
| 184 | } |
| 185 | p++; |
| 186 | } |
| 187 |
| 188 | *(p++) = 0; |
| 189 |
| 190 | memcpy(p, from, (unsigned int)flen); |
| 191 | return 1; |
| 192 | } |
| 193 |
| 194 | /* constant_time_byte_eq returns 1 if |x| == |y| and 0 otherwise. */ |
| 195 | static int constant_time_byte_eq(unsigned char a, unsigned char b) { |
| 196 | unsigned char z = ~(a ^ b); |
| 197 | z &= z >> 4; |
| 198 | z &= z >> 2; |
| 199 | z &= z >> 1; |
| 200 |
| 201 | return z; |
| 202 | } |
| 203 |
| 204 | /* constant_time_select returns |x| if |v| is 1 and |y| if |v| is 0. |
| 205 | * Its behavior is undefined if |v| takes any other value. */ |
| 206 | static int constant_time_select(int v, int x, int y) { |
| 207 | return ((~(v - 1)) & x) | ((v - 1) & y); |
| 208 | } |
| 209 |
| 210 | /* constant_time_le returns 1 if |x| <= |y| and 0 otherwise. |
| 211 | * |x| and |y| must be positive. */ |
| 212 | static int constant_time_le(int x, int y) { |
| 213 | return ((x - y - 1) >> (sizeof(int) * 8 - 1)) & 1; |
| 214 | } |
| 215 |
| 216 | int RSA_message_index_PKCS1_type_2(const uint8_t *from, size_t from_len, |
| 217 | size_t *out_index) { |
| 218 | size_t i; |
| 219 | int first_byte_is_zero, second_byte_is_two, looking_for_index; |
| 220 | int valid_index, zero_index = 0; |
| 221 |
| 222 | /* PKCS#1 v1.5 decryption. See "PKCS #1 v2.2: RSA Cryptography |
| 223 | * Standard", section 7.2.2. */ |
| 224 | if (from_len < RSA_PKCS1_PADDING_SIZE) { |
| 225 | /* |from| is zero-padded to the size of the RSA modulus, a public value, so |
| 226 | * this can be rejected in non-constant time. */ |
| 227 | *out_index = 0; |
| 228 | return 0; |
| 229 | } |
| 230 |
| 231 | first_byte_is_zero = constant_time_byte_eq(from[0], 0); |
| 232 | second_byte_is_two = constant_time_byte_eq(from[1], 2); |
| 233 |
| 234 | looking_for_index = 1; |
| 235 | for (i = 2; i < from_len; i++) { |
| 236 | int equals0 = constant_time_byte_eq(from[i], 0); |
| 237 | zero_index = |
| 238 | constant_time_select(looking_for_index & equals0, i, zero_index); |
| 239 | looking_for_index = constant_time_select(equals0, 0, looking_for_index); |
| 240 | } |
| 241 |
| 242 | /* The input must begin with 00 02. */ |
| 243 | valid_index = first_byte_is_zero; |
| 244 | valid_index &= second_byte_is_two; |
| 245 |
| 246 | /* We must have found the end of PS. */ |
| 247 | valid_index &= ~looking_for_index; |
| 248 |
| 249 | /* PS must be at least 8 bytes long, and it starts two bytes into |from|. */ |
| 250 | valid_index &= constant_time_le(2 + 8, zero_index); |
| 251 |
| 252 | /* Skip the zero byte. */ |
| 253 | zero_index++; |
| 254 |
| 255 | *out_index = constant_time_select(valid_index, zero_index, 0); |
| 256 | return valid_index; |
| 257 | } |
| 258 |
| 259 | int RSA_padding_check_PKCS1_type_2(uint8_t *to, unsigned tlen, |
| 260 | const uint8_t *from, unsigned flen) { |
| 261 | size_t msg_index, msg_len; |
| 262 |
| 263 | if (flen == 0) { |
| 264 | OPENSSL_PUT_ERROR(RSA, RSA_R_EMPTY_PUBLIC_KEY); |
| 265 | return -1; |
| 266 | } |
| 267 |
| 268 | /* NOTE: Although |RSA_message_index_PKCS1_type_2| itself is constant time, |
| 269 | * the API contracts of this function and |RSA_decrypt| with |
| 270 | * |RSA_PKCS1_PADDING| make it impossible to completely avoid Bleichenbacher's |
| 271 | * attack. */ |
| 272 | if (!RSA_message_index_PKCS1_type_2(from, flen, &msg_index)) { |
| 273 | OPENSSL_PUT_ERROR(RSA, RSA_R_PKCS_DECODING_ERROR); |
| 274 | return -1; |
| 275 | } |
| 276 |
| 277 | msg_len = flen - msg_index; |
| 278 | if (msg_len > tlen) { |
| 279 | /* This shouldn't happen because this function is always called with |tlen| |
| 280 | * the key size and |flen| is bounded by the key size. */ |
| 281 | OPENSSL_PUT_ERROR(RSA, RSA_R_PKCS_DECODING_ERROR); |
| 282 | return -1; |
| 283 | } |
| 284 | memcpy(to, &from[msg_index], msg_len); |
| 285 | return msg_len; |
| 286 | } |
| 287 |
| 288 | int RSA_padding_add_none(uint8_t *to, unsigned tlen, const uint8_t *from, unsigned flen) { |
| 289 | if (flen > tlen) { |
| 290 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
| 291 | return 0; |
| 292 | } |
| 293 |
| 294 | if (flen < tlen) { |
| 295 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE); |
| 296 | return 0; |
| 297 | } |
| 298 |
| 299 | memcpy(to, from, (unsigned int)flen); |
| 300 | return 1; |
| 301 | } |
| 302 |
| 303 | int PKCS1_MGF1(uint8_t *mask, unsigned len, const uint8_t *seed, |
| 304 | unsigned seedlen, const EVP_MD *dgst) { |
| 305 | unsigned outlen = 0; |
| 306 | uint32_t i; |
| 307 | uint8_t cnt[4]; |
| 308 | EVP_MD_CTX c; |
| 309 | uint8_t md[EVP_MAX_MD_SIZE]; |
| 310 | unsigned mdlen; |
| 311 | int ret = -1; |
| 312 |
| 313 | EVP_MD_CTX_init(&c); |
| 314 | mdlen = EVP_MD_size(dgst); |
| 315 |
| 316 | for (i = 0; outlen < len; i++) { |
| 317 | cnt[0] = (uint8_t)((i >> 24) & 255); |
| 318 | cnt[1] = (uint8_t)((i >> 16) & 255); |
| 319 | cnt[2] = (uint8_t)((i >> 8)) & 255; |
| 320 | cnt[3] = (uint8_t)(i & 255); |
| 321 | if (!EVP_DigestInit_ex(&c, dgst, NULL) || |
| 322 | !EVP_DigestUpdate(&c, seed, seedlen) || !EVP_DigestUpdate(&c, cnt, 4)) { |
| 323 | goto err; |
| 324 | } |
| 325 |
| 326 | if (outlen + mdlen <= len) { |
| 327 | if (!EVP_DigestFinal_ex(&c, mask + outlen, NULL)) { |
| 328 | goto err; |
| 329 | } |
| 330 | outlen += mdlen; |
| 331 | } else { |
| 332 | if (!EVP_DigestFinal_ex(&c, md, NULL)) { |
| 333 | goto err; |
| 334 | } |
| 335 | memcpy(mask + outlen, md, len - outlen); |
| 336 | outlen = len; |
| 337 | } |
| 338 | } |
| 339 | ret = 0; |
| 340 |
| 341 | err: |
| 342 | EVP_MD_CTX_cleanup(&c); |
| 343 | return ret; |
| 344 | } |
| 345 |
| 346 | int RSA_padding_add_PKCS1_OAEP_mgf1(uint8_t *to, unsigned tlen, |
| 347 | const uint8_t *from, unsigned flen, |
| 348 | const uint8_t *param, unsigned plen, |
| 349 | const EVP_MD *md, const EVP_MD *mgf1md) { |
| 350 | unsigned i, emlen, mdlen; |
| 351 | uint8_t *db, *seed; |
| 352 | uint8_t *dbmask = NULL, seedmask[EVP_MAX_MD_SIZE]; |
| 353 | int ret = 0; |
| 354 |
| 355 | if (md == NULL) { |
| 356 | md = EVP_sha1(); |
| 357 | } |
| 358 | if (mgf1md == NULL) { |
| 359 | mgf1md = md; |
| 360 | } |
| 361 |
| 362 | mdlen = EVP_MD_size(md); |
| 363 |
| 364 | if (tlen < 2 * mdlen + 2) { |
| 365 | OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL); |
| 366 | return 0; |
| 367 | } |
| 368 |
| 369 | emlen = tlen - 1; |
| 370 | if (flen > emlen - 2 * mdlen - 1) { |
| 371 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
| 372 | return 0; |
| 373 | } |
| 374 |
| 375 | if (emlen < 2 * mdlen + 1) { |
| 376 | OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL); |
| 377 | return 0; |
| 378 | } |
| 379 |
| 380 | to[0] = 0; |
| 381 | seed = to + 1; |
| 382 | db = to + mdlen + 1; |
| 383 |
| 384 | if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL)) { |
| 385 | return 0; |
| 386 | } |
| 387 | memset(db + mdlen, 0, emlen - flen - 2 * mdlen - 1); |
| 388 | db[emlen - flen - mdlen - 1] = 0x01; |
| 389 | memcpy(db + emlen - flen - mdlen, from, flen); |
| 390 | if (!RAND_bytes(seed, mdlen)) { |
| 391 | return 0; |
| 392 | } |
| 393 |
| 394 | dbmask = OPENSSL_malloc(emlen - mdlen); |
| 395 | if (dbmask == NULL) { |
| 396 | OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE); |
| 397 | return 0; |
| 398 | } |
| 399 |
| 400 | if (PKCS1_MGF1(dbmask, emlen - mdlen, seed, mdlen, mgf1md) < 0) { |
| 401 | goto out; |
| 402 | } |
| 403 | for (i = 0; i < emlen - mdlen; i++) { |
| 404 | db[i] ^= dbmask[i]; |
| 405 | } |
| 406 |
| 407 | if (PKCS1_MGF1(seedmask, mdlen, db, emlen - mdlen, mgf1md) < 0) { |
| 408 | goto out; |
| 409 | } |
| 410 | for (i = 0; i < mdlen; i++) { |
| 411 | seed[i] ^= seedmask[i]; |
| 412 | } |
| 413 | ret = 1; |
| 414 |
| 415 | out: |
| 416 | OPENSSL_free(dbmask); |
| 417 | return ret; |
| 418 | } |
| 419 |
| 420 | int RSA_padding_check_PKCS1_OAEP_mgf1(uint8_t *to, unsigned tlen, |
| 421 | const uint8_t *from, unsigned flen, |
| 422 | const uint8_t *param, unsigned plen, |
| 423 | const EVP_MD *md, const EVP_MD *mgf1md) { |
| 424 | unsigned i, dblen, mlen = -1, mdlen; |
| 425 | const uint8_t *maskeddb, *maskedseed; |
| 426 | uint8_t *db = NULL, seed[EVP_MAX_MD_SIZE], phash[EVP_MAX_MD_SIZE]; |
| 427 | int bad, looking_for_one_byte, one_index = 0; |
| 428 |
| 429 | if (md == NULL) { |
| 430 | md = EVP_sha1(); |
| 431 | } |
| 432 | if (mgf1md == NULL) { |
| 433 | mgf1md = md; |
| 434 | } |
| 435 |
| 436 | mdlen = EVP_MD_size(md); |
| 437 |
| 438 | /* The encoded message is one byte smaller than the modulus to ensure that it |
| 439 | * doesn't end up greater than the modulus. Thus there's an extra "+1" here |
| 440 | * compared to https://tools.ietf.org/html/rfc2437#section-9.1.1.2. */ |
| 441 | if (flen < 1 + 2*mdlen + 1) { |
| 442 | /* 'flen' is the length of the modulus, i.e. does not depend on the |
| 443 | * particular ciphertext. */ |
| 444 | goto decoding_err; |
| 445 | } |
| 446 |
| 447 | dblen = flen - mdlen - 1; |
| 448 | db = OPENSSL_malloc(dblen); |
| 449 | if (db == NULL) { |
| 450 | OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE); |
| 451 | goto err; |
| 452 | } |
| 453 |
| 454 | maskedseed = from + 1; |
| 455 | maskeddb = from + 1 + mdlen; |
| 456 |
| 457 | if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md)) { |
| 458 | goto err; |
| 459 | } |
| 460 | for (i = 0; i < mdlen; i++) { |
| 461 | seed[i] ^= maskedseed[i]; |
| 462 | } |
| 463 |
| 464 | if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md)) { |
| 465 | goto err; |
| 466 | } |
| 467 | for (i = 0; i < dblen; i++) { |
| 468 | db[i] ^= maskeddb[i]; |
| 469 | } |
| 470 |
| 471 | if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL)) { |
| 472 | goto err; |
| 473 | } |
| 474 |
| 475 | bad = CRYPTO_memcmp(db, phash, mdlen); |
| 476 | bad |= from[0]; |
| 477 |
| 478 | looking_for_one_byte = 1; |
| 479 | for (i = mdlen; i < dblen; i++) { |
| 480 | int equals1 = constant_time_byte_eq(db[i], 1); |
| 481 | int equals0 = constant_time_byte_eq(db[i], 0); |
| 482 | one_index = |
| 483 | constant_time_select(looking_for_one_byte & equals1, i, one_index); |
| 484 | looking_for_one_byte = |
| 485 | constant_time_select(equals1, 0, looking_for_one_byte); |
| 486 | bad |= looking_for_one_byte & ~equals0; |
| 487 | } |
| 488 |
| 489 | bad |= looking_for_one_byte; |
| 490 |
| 491 | if (bad) { |
| 492 | goto decoding_err; |
| 493 | } |
| 494 |
| 495 | one_index++; |
| 496 | mlen = dblen - one_index; |
| 497 | if (tlen < mlen) { |
| 498 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE); |
| 499 | mlen = -1; |
| 500 | } else { |
| 501 | memcpy(to, db + one_index, mlen); |
| 502 | } |
| 503 |
| 504 | OPENSSL_free(db); |
| 505 | return mlen; |
| 506 |
| 507 | decoding_err: |
| 508 | /* to avoid chosen ciphertext attacks, the error message should not reveal |
| 509 | * which kind of decoding error happened */ |
| 510 | OPENSSL_PUT_ERROR(RSA, RSA_R_OAEP_DECODING_ERROR); |
| 511 | err: |
| 512 | OPENSSL_free(db); |
| 513 | return -1; |
| 514 | } |
| 515 |
| 516 | static const unsigned char zeroes[] = {0,0,0,0,0,0,0,0}; |
| 517 |
| 518 | int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const uint8_t *mHash, |
| 519 | const EVP_MD *Hash, const EVP_MD *mgf1Hash, |
| 520 | const uint8_t *EM, int sLen) { |
| 521 | int i; |
| 522 | int ret = 0; |
| 523 | int maskedDBLen, MSBits, emLen; |
| 524 | size_t hLen; |
| 525 | const uint8_t *H; |
| 526 | uint8_t *DB = NULL; |
| 527 | EVP_MD_CTX ctx; |
| 528 | uint8_t H_[EVP_MAX_MD_SIZE]; |
| 529 | EVP_MD_CTX_init(&ctx); |
| 530 |
| 531 | if (mgf1Hash == NULL) { |
| 532 | mgf1Hash = Hash; |
| 533 | } |
| 534 |
| 535 | hLen = EVP_MD_size(Hash); |
| 536 |
| 537 | /* Negative sLen has special meanings: |
| 538 | * -1 sLen == hLen |
| 539 | * -2 salt length is autorecovered from signature |
| 540 | * -N reserved */ |
| 541 | if (sLen == -1) { |
| 542 | sLen = hLen; |
| 543 | } else if (sLen == -2) { |
| 544 | sLen = -2; |
| 545 | } else if (sLen < -2) { |
| 546 | OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED); |
| 547 | goto err; |
| 548 | } |
| 549 |
| 550 | MSBits = (BN_num_bits(rsa->n) - 1) & 0x7; |
| 551 | emLen = RSA_size(rsa); |
| 552 | if (EM[0] & (0xFF << MSBits)) { |
| 553 | OPENSSL_PUT_ERROR(RSA, RSA_R_FIRST_OCTET_INVALID); |
| 554 | goto err; |
| 555 | } |
| 556 | if (MSBits == 0) { |
| 557 | EM++; |
| 558 | emLen--; |
| 559 | } |
| 560 | if (emLen < ((int)hLen + sLen + 2)) { |
| 561 | /* sLen can be small negative */ |
| 562 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE); |
| 563 | goto err; |
| 564 | } |
| 565 | if (EM[emLen - 1] != 0xbc) { |
| 566 | OPENSSL_PUT_ERROR(RSA, RSA_R_LAST_OCTET_INVALID); |
| 567 | goto err; |
| 568 | } |
| 569 | maskedDBLen = emLen - hLen - 1; |
| 570 | H = EM + maskedDBLen; |
| 571 | DB = OPENSSL_malloc(maskedDBLen); |
| 572 | if (!DB) { |
| 573 | OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE); |
| 574 | goto err; |
| 575 | } |
| 576 | if (PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash) < 0) { |
| 577 | goto err; |
| 578 | } |
| 579 | for (i = 0; i < maskedDBLen; i++) { |
| 580 | DB[i] ^= EM[i]; |
| 581 | } |
| 582 | if (MSBits) { |
| 583 | DB[0] &= 0xFF >> (8 - MSBits); |
| 584 | } |
| 585 | for (i = 0; DB[i] == 0 && i < (maskedDBLen - 1); i++) { |
| 586 | ; |
| 587 | } |
| 588 | if (DB[i++] != 0x1) { |
| 589 | OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_RECOVERY_FAILED); |
| 590 | goto err; |
| 591 | } |
| 592 | if (sLen >= 0 && (maskedDBLen - i) != sLen) { |
| 593 | OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED); |
| 594 | goto err; |
| 595 | } |
| 596 | if (!EVP_DigestInit_ex(&ctx, Hash, NULL) || |
| 597 | !EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes) || |
| 598 | !EVP_DigestUpdate(&ctx, mHash, hLen)) { |
| 599 | goto err; |
| 600 | } |
| 601 | if (maskedDBLen - i) { |
| 602 | if (!EVP_DigestUpdate(&ctx, DB + i, maskedDBLen - i)) { |
| 603 | goto err; |
| 604 | } |
| 605 | } |
| 606 | if (!EVP_DigestFinal_ex(&ctx, H_, NULL)) { |
| 607 | goto err; |
| 608 | } |
| 609 | if (memcmp(H_, H, hLen)) { |
| 610 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_SIGNATURE); |
| 611 | ret = 0; |
| 612 | } else { |
| 613 | ret = 1; |
| 614 | } |
| 615 |
| 616 | err: |
| 617 | OPENSSL_free(DB); |
| 618 | EVP_MD_CTX_cleanup(&ctx); |
| 619 |
| 620 | return ret; |
| 621 | } |
| 622 |
| 623 | int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM, |
| 624 | const unsigned char *mHash, |
| 625 | const EVP_MD *Hash, const EVP_MD *mgf1Hash, |
| 626 | int sLen) { |
| 627 | int i; |
| 628 | int ret = 0; |
| 629 | size_t maskedDBLen, MSBits, emLen; |
| 630 | size_t hLen; |
| 631 | unsigned char *H, *salt = NULL, *p; |
| 632 | EVP_MD_CTX ctx; |
| 633 |
| 634 | if (mgf1Hash == NULL) { |
| 635 | mgf1Hash = Hash; |
| 636 | } |
| 637 |
| 638 | hLen = EVP_MD_size(Hash); |
| 639 |
| 640 | /* Negative sLen has special meanings: |
| 641 | * -1 sLen == hLen |
| 642 | * -2 salt length is maximized |
| 643 | * -N reserved */ |
| 644 | if (sLen == -1) { |
| 645 | sLen = hLen; |
| 646 | } else if (sLen == -2) { |
| 647 | sLen = -2; |
| 648 | } else if (sLen < -2) { |
| 649 | OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED); |
| 650 | goto err; |
| 651 | } |
| 652 |
| 653 | if (BN_is_zero(rsa->n)) { |
| 654 | OPENSSL_PUT_ERROR(RSA, RSA_R_EMPTY_PUBLIC_KEY); |
| 655 | goto err; |
| 656 | } |
| 657 |
| 658 | MSBits = (BN_num_bits(rsa->n) - 1) & 0x7; |
| 659 | emLen = RSA_size(rsa); |
| 660 | if (MSBits == 0) { |
| 661 | assert(emLen >= 1); |
| 662 | *EM++ = 0; |
| 663 | emLen--; |
| 664 | } |
| 665 | if (sLen == -2) { |
| 666 | if (emLen < hLen + 2) { |
| 667 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
| 668 | goto err; |
| 669 | } |
| 670 | sLen = emLen - hLen - 2; |
| 671 | } else if (emLen < hLen + sLen + 2) { |
| 672 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
| 673 | goto err; |
| 674 | } |
| 675 | if (sLen > 0) { |
| 676 | salt = OPENSSL_malloc(sLen); |
| 677 | if (!salt) { |
| 678 | OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE); |
| 679 | goto err; |
| 680 | } |
| 681 | if (!RAND_bytes(salt, sLen)) { |
| 682 | goto err; |
| 683 | } |
| 684 | } |
| 685 | maskedDBLen = emLen - hLen - 1; |
| 686 | H = EM + maskedDBLen; |
| 687 | EVP_MD_CTX_init(&ctx); |
| 688 | if (!EVP_DigestInit_ex(&ctx, Hash, NULL) || |
| 689 | !EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes) || |
| 690 | !EVP_DigestUpdate(&ctx, mHash, hLen)) { |
| 691 | goto err; |
| 692 | } |
| 693 | if (sLen && !EVP_DigestUpdate(&ctx, salt, sLen)) { |
| 694 | goto err; |
| 695 | } |
| 696 | if (!EVP_DigestFinal_ex(&ctx, H, NULL)) { |
| 697 | goto err; |
| 698 | } |
| 699 | EVP_MD_CTX_cleanup(&ctx); |
| 700 |
| 701 | /* Generate dbMask in place then perform XOR on it */ |
| 702 | if (PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash)) { |
| 703 | goto err; |
| 704 | } |
| 705 |
| 706 | p = EM; |
| 707 |
| 708 | /* Initial PS XORs with all zeroes which is a NOP so just update |
| 709 | * pointer. Note from a test above this value is guaranteed to |
| 710 | * be non-negative. */ |
| 711 | p += emLen - sLen - hLen - 2; |
| 712 | *p++ ^= 0x1; |
| 713 | if (sLen > 0) { |
| 714 | for (i = 0; i < sLen; i++) { |
| 715 | *p++ ^= salt[i]; |
| 716 | } |
| 717 | } |
| 718 | if (MSBits) { |
| 719 | EM[0] &= 0xFF >> (8 - MSBits); |
| 720 | } |
| 721 |
| 722 | /* H is already in place so just set final 0xbc */ |
| 723 |
| 724 | EM[emLen - 1] = 0xbc; |
| 725 |
| 726 | ret = 1; |
| 727 |
| 728 | err: |
| 729 | OPENSSL_free(salt); |
| 730 |
| 731 | return ret; |
| 732 | } |
| 733 |