padding.c 18 KB

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