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.
 
 
 
 
 
 

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