You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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