padding.c 19 KB

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