Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

707 řádky
18 KiB

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