您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

709 行
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 *out, size_t len, const uint8_t *seed,
  234. size_t seed_len, const EVP_MD *md) {
  235. int ret = 0;
  236. EVP_MD_CTX ctx;
  237. EVP_MD_CTX_init(&ctx);
  238. size_t md_len = EVP_MD_size(md);
  239. for (uint32_t i = 0; len > 0; i++) {
  240. uint8_t counter[4];
  241. counter[0] = (uint8_t)(i >> 24);
  242. counter[1] = (uint8_t)(i >> 16);
  243. counter[2] = (uint8_t)(i >> 8);
  244. counter[3] = (uint8_t)i;
  245. if (!EVP_DigestInit_ex(&ctx, md, NULL) ||
  246. !EVP_DigestUpdate(&ctx, seed, seed_len) ||
  247. !EVP_DigestUpdate(&ctx, counter, sizeof(counter))) {
  248. goto err;
  249. }
  250. if (md_len <= len) {
  251. if (!EVP_DigestFinal_ex(&ctx, out, NULL)) {
  252. goto err;
  253. }
  254. out += md_len;
  255. len -= md_len;
  256. } else {
  257. uint8_t digest[EVP_MAX_MD_SIZE];
  258. if (!EVP_DigestFinal_ex(&ctx, digest, NULL)) {
  259. goto err;
  260. }
  261. memcpy(out, digest, len);
  262. len = 0;
  263. }
  264. }
  265. ret = 1;
  266. err:
  267. EVP_MD_CTX_cleanup(&ctx);
  268. return ret;
  269. }
  270. int RSA_padding_add_PKCS1_OAEP_mgf1(uint8_t *to, unsigned to_len,
  271. const uint8_t *from, unsigned from_len,
  272. const uint8_t *param, unsigned param_len,
  273. const EVP_MD *md, const EVP_MD *mgf1md) {
  274. unsigned i, emlen, mdlen;
  275. uint8_t *db, *seed;
  276. uint8_t *dbmask = NULL, seedmask[EVP_MAX_MD_SIZE];
  277. int ret = 0;
  278. if (md == NULL) {
  279. md = EVP_sha1();
  280. }
  281. if (mgf1md == NULL) {
  282. mgf1md = md;
  283. }
  284. mdlen = EVP_MD_size(md);
  285. if (to_len < 2 * mdlen + 2) {
  286. OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
  287. return 0;
  288. }
  289. emlen = to_len - 1;
  290. if (from_len > emlen - 2 * mdlen - 1) {
  291. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  292. return 0;
  293. }
  294. if (emlen < 2 * mdlen + 1) {
  295. OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
  296. return 0;
  297. }
  298. to[0] = 0;
  299. seed = to + 1;
  300. db = to + mdlen + 1;
  301. if (!EVP_Digest(param, param_len, db, NULL, md, NULL)) {
  302. return 0;
  303. }
  304. memset(db + mdlen, 0, emlen - from_len - 2 * mdlen - 1);
  305. db[emlen - from_len - mdlen - 1] = 0x01;
  306. memcpy(db + emlen - from_len - mdlen, from, from_len);
  307. if (!RAND_bytes(seed, mdlen)) {
  308. return 0;
  309. }
  310. dbmask = OPENSSL_malloc(emlen - mdlen);
  311. if (dbmask == NULL) {
  312. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  313. return 0;
  314. }
  315. if (!PKCS1_MGF1(dbmask, emlen - mdlen, seed, mdlen, mgf1md)) {
  316. goto out;
  317. }
  318. for (i = 0; i < emlen - mdlen; i++) {
  319. db[i] ^= dbmask[i];
  320. }
  321. if (!PKCS1_MGF1(seedmask, mdlen, db, emlen - mdlen, mgf1md)) {
  322. goto out;
  323. }
  324. for (i = 0; i < mdlen; i++) {
  325. seed[i] ^= seedmask[i];
  326. }
  327. ret = 1;
  328. out:
  329. OPENSSL_free(dbmask);
  330. return ret;
  331. }
  332. int RSA_padding_check_PKCS1_OAEP_mgf1(uint8_t *to, unsigned to_len,
  333. const uint8_t *from, unsigned from_len,
  334. const uint8_t *param, unsigned param_len,
  335. const EVP_MD *md, const EVP_MD *mgf1md) {
  336. unsigned i, dblen, mlen = -1, mdlen, bad, looking_for_one_byte, one_index = 0;
  337. const uint8_t *maskeddb, *maskedseed;
  338. uint8_t *db = NULL, seed[EVP_MAX_MD_SIZE], phash[EVP_MAX_MD_SIZE];
  339. if (md == NULL) {
  340. md = EVP_sha1();
  341. }
  342. if (mgf1md == NULL) {
  343. mgf1md = md;
  344. }
  345. mdlen = EVP_MD_size(md);
  346. /* The encoded message is one byte smaller than the modulus to ensure that it
  347. * doesn't end up greater than the modulus. Thus there's an extra "+1" here
  348. * compared to https://tools.ietf.org/html/rfc2437#section-9.1.1.2. */
  349. if (from_len < 1 + 2*mdlen + 1) {
  350. /* 'from_len' is the length of the modulus, i.e. does not depend on the
  351. * particular ciphertext. */
  352. goto decoding_err;
  353. }
  354. dblen = from_len - mdlen - 1;
  355. db = OPENSSL_malloc(dblen);
  356. if (db == NULL) {
  357. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  358. goto err;
  359. }
  360. maskedseed = from + 1;
  361. maskeddb = from + 1 + mdlen;
  362. if (!PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md)) {
  363. goto err;
  364. }
  365. for (i = 0; i < mdlen; i++) {
  366. seed[i] ^= maskedseed[i];
  367. }
  368. if (!PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md)) {
  369. goto err;
  370. }
  371. for (i = 0; i < dblen; i++) {
  372. db[i] ^= maskeddb[i];
  373. }
  374. if (!EVP_Digest(param, param_len, phash, NULL, md, NULL)) {
  375. goto err;
  376. }
  377. bad = ~constant_time_is_zero(CRYPTO_memcmp(db, phash, mdlen));
  378. bad |= ~constant_time_is_zero(from[0]);
  379. looking_for_one_byte = ~0u;
  380. for (i = mdlen; i < dblen; i++) {
  381. unsigned equals1 = constant_time_eq(db[i], 1);
  382. unsigned equals0 = constant_time_eq(db[i], 0);
  383. one_index = constant_time_select(looking_for_one_byte & equals1, i,
  384. one_index);
  385. looking_for_one_byte =
  386. constant_time_select(equals1, 0, looking_for_one_byte);
  387. bad |= looking_for_one_byte & ~equals0;
  388. }
  389. bad |= looking_for_one_byte;
  390. if (bad) {
  391. goto decoding_err;
  392. }
  393. one_index++;
  394. mlen = dblen - one_index;
  395. if (to_len < mlen) {
  396. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE);
  397. mlen = -1;
  398. } else {
  399. memcpy(to, db + one_index, mlen);
  400. }
  401. OPENSSL_free(db);
  402. return mlen;
  403. decoding_err:
  404. /* to avoid chosen ciphertext attacks, the error message should not reveal
  405. * which kind of decoding error happened */
  406. OPENSSL_PUT_ERROR(RSA, RSA_R_OAEP_DECODING_ERROR);
  407. err:
  408. OPENSSL_free(db);
  409. return -1;
  410. }
  411. static const unsigned char zeroes[] = {0,0,0,0,0,0,0,0};
  412. int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const uint8_t *mHash,
  413. const EVP_MD *Hash, const EVP_MD *mgf1Hash,
  414. const uint8_t *EM, int sLen) {
  415. int i;
  416. int ret = 0;
  417. int maskedDBLen, MSBits, emLen;
  418. size_t hLen;
  419. const uint8_t *H;
  420. uint8_t *DB = NULL;
  421. EVP_MD_CTX ctx;
  422. uint8_t H_[EVP_MAX_MD_SIZE];
  423. EVP_MD_CTX_init(&ctx);
  424. if (mgf1Hash == NULL) {
  425. mgf1Hash = Hash;
  426. }
  427. hLen = EVP_MD_size(Hash);
  428. /* Negative sLen has special meanings:
  429. * -1 sLen == hLen
  430. * -2 salt length is autorecovered from signature
  431. * -N reserved */
  432. if (sLen == -1) {
  433. sLen = hLen;
  434. } else if (sLen == -2) {
  435. sLen = -2;
  436. } else if (sLen < -2) {
  437. OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED);
  438. goto err;
  439. }
  440. MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
  441. emLen = RSA_size(rsa);
  442. if (EM[0] & (0xFF << MSBits)) {
  443. OPENSSL_PUT_ERROR(RSA, RSA_R_FIRST_OCTET_INVALID);
  444. goto err;
  445. }
  446. if (MSBits == 0) {
  447. EM++;
  448. emLen--;
  449. }
  450. if (emLen < ((int)hLen + sLen + 2)) {
  451. /* sLen can be small negative */
  452. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE);
  453. goto err;
  454. }
  455. if (EM[emLen - 1] != 0xbc) {
  456. OPENSSL_PUT_ERROR(RSA, RSA_R_LAST_OCTET_INVALID);
  457. goto err;
  458. }
  459. maskedDBLen = emLen - hLen - 1;
  460. H = EM + maskedDBLen;
  461. DB = OPENSSL_malloc(maskedDBLen);
  462. if (!DB) {
  463. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  464. goto err;
  465. }
  466. if (!PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash)) {
  467. goto err;
  468. }
  469. for (i = 0; i < maskedDBLen; i++) {
  470. DB[i] ^= EM[i];
  471. }
  472. if (MSBits) {
  473. DB[0] &= 0xFF >> (8 - MSBits);
  474. }
  475. for (i = 0; DB[i] == 0 && i < (maskedDBLen - 1); i++) {
  476. ;
  477. }
  478. if (DB[i++] != 0x1) {
  479. OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_RECOVERY_FAILED);
  480. goto err;
  481. }
  482. if (sLen >= 0 && (maskedDBLen - i) != sLen) {
  483. OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED);
  484. goto err;
  485. }
  486. if (!EVP_DigestInit_ex(&ctx, Hash, NULL) ||
  487. !EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes) ||
  488. !EVP_DigestUpdate(&ctx, mHash, hLen)) {
  489. goto err;
  490. }
  491. if (maskedDBLen - i) {
  492. if (!EVP_DigestUpdate(&ctx, DB + i, maskedDBLen - i)) {
  493. goto err;
  494. }
  495. }
  496. if (!EVP_DigestFinal_ex(&ctx, H_, NULL)) {
  497. goto err;
  498. }
  499. if (memcmp(H_, H, hLen)) {
  500. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_SIGNATURE);
  501. ret = 0;
  502. } else {
  503. ret = 1;
  504. }
  505. err:
  506. OPENSSL_free(DB);
  507. EVP_MD_CTX_cleanup(&ctx);
  508. return ret;
  509. }
  510. int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM,
  511. const unsigned char *mHash,
  512. const EVP_MD *Hash, const EVP_MD *mgf1Hash,
  513. int sLenRequested) {
  514. int ret = 0;
  515. size_t maskedDBLen, MSBits, emLen;
  516. size_t hLen;
  517. unsigned char *H, *salt = NULL, *p;
  518. EVP_MD_CTX ctx;
  519. if (mgf1Hash == NULL) {
  520. mgf1Hash = Hash;
  521. }
  522. hLen = EVP_MD_size(Hash);
  523. if (BN_is_zero(rsa->n)) {
  524. OPENSSL_PUT_ERROR(RSA, RSA_R_EMPTY_PUBLIC_KEY);
  525. goto err;
  526. }
  527. MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
  528. emLen = RSA_size(rsa);
  529. if (MSBits == 0) {
  530. assert(emLen >= 1);
  531. *EM++ = 0;
  532. emLen--;
  533. }
  534. if (emLen < hLen + 2) {
  535. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  536. goto err;
  537. }
  538. /* Negative sLenRequested has special meanings:
  539. * -1 sLen == hLen
  540. * -2 salt length is maximized
  541. * -N reserved */
  542. size_t sLen;
  543. if (sLenRequested == -1) {
  544. sLen = hLen;
  545. } else if (sLenRequested == -2) {
  546. sLen = emLen - hLen - 2;
  547. } else if (sLenRequested < 0) {
  548. OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED);
  549. goto err;
  550. } else {
  551. sLen = (size_t)sLenRequested;
  552. }
  553. if (emLen - hLen - 2 < sLen) {
  554. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  555. goto err;
  556. }
  557. if (sLen > 0) {
  558. salt = OPENSSL_malloc(sLen);
  559. if (!salt) {
  560. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  561. goto err;
  562. }
  563. if (!RAND_bytes(salt, sLen)) {
  564. goto err;
  565. }
  566. }
  567. maskedDBLen = emLen - hLen - 1;
  568. H = EM + maskedDBLen;
  569. EVP_MD_CTX_init(&ctx);
  570. if (!EVP_DigestInit_ex(&ctx, Hash, NULL) ||
  571. !EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes) ||
  572. !EVP_DigestUpdate(&ctx, mHash, hLen)) {
  573. goto err;
  574. }
  575. if (sLen && !EVP_DigestUpdate(&ctx, salt, sLen)) {
  576. goto err;
  577. }
  578. if (!EVP_DigestFinal_ex(&ctx, H, NULL)) {
  579. goto err;
  580. }
  581. EVP_MD_CTX_cleanup(&ctx);
  582. /* Generate dbMask in place then perform XOR on it */
  583. if (!PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash)) {
  584. goto err;
  585. }
  586. p = EM;
  587. /* Initial PS XORs with all zeroes which is a NOP so just update
  588. * pointer. Note from a test above this value is guaranteed to
  589. * be non-negative. */
  590. p += emLen - sLen - hLen - 2;
  591. *p++ ^= 0x1;
  592. if (sLen > 0) {
  593. for (size_t i = 0; i < sLen; i++) {
  594. *p++ ^= salt[i];
  595. }
  596. }
  597. if (MSBits) {
  598. EM[0] &= 0xFF >> (8 - MSBits);
  599. }
  600. /* H is already in place so just set final 0xbc */
  601. EM[emLen - 1] = 0xbc;
  602. ret = 1;
  603. err:
  604. OPENSSL_free(salt);
  605. return ret;
  606. }