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.

padding.c 21 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. OPENSSL_free(dbmask);
  380. return ret;
  381. }
  382. int RSA_padding_check_PKCS1_OAEP_mgf1(uint8_t *to, unsigned tlen,
  383. const uint8_t *from, unsigned flen,
  384. const uint8_t *param, unsigned plen,
  385. const EVP_MD *md, const EVP_MD *mgf1md) {
  386. unsigned i, dblen, mlen = -1, mdlen;
  387. const uint8_t *maskeddb, *maskedseed;
  388. uint8_t *db = NULL, seed[EVP_MAX_MD_SIZE], phash[EVP_MAX_MD_SIZE];
  389. int bad, looking_for_one_byte, one_index = 0;
  390. if (md == NULL) {
  391. md = EVP_sha1();
  392. }
  393. if (mgf1md == NULL) {
  394. mgf1md = md;
  395. }
  396. mdlen = EVP_MD_size(md);
  397. /* The encoded message is one byte smaller than the modulus to ensure that it
  398. * doesn't end up greater than the modulus. Thus there's an extra "+1" here
  399. * compared to https://tools.ietf.org/html/rfc2437#section-9.1.1.2. */
  400. if (flen < 1 + 2*mdlen + 1) {
  401. /* 'flen' is the length of the modulus, i.e. does not depend on the
  402. * particular ciphertext. */
  403. goto decoding_err;
  404. }
  405. dblen = flen - mdlen - 1;
  406. db = OPENSSL_malloc(dblen);
  407. if (db == NULL) {
  408. OPENSSL_PUT_ERROR(RSA, RSA_padding_check_PKCS1_OAEP_mgf1,
  409. ERR_R_MALLOC_FAILURE);
  410. goto err;
  411. }
  412. maskedseed = from + 1;
  413. maskeddb = from + 1 + mdlen;
  414. if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md)) {
  415. goto err;
  416. }
  417. for (i = 0; i < mdlen; i++) {
  418. seed[i] ^= maskedseed[i];
  419. }
  420. if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md)) {
  421. goto err;
  422. }
  423. for (i = 0; i < dblen; i++) {
  424. db[i] ^= maskeddb[i];
  425. }
  426. if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL)) {
  427. goto err;
  428. }
  429. bad = CRYPTO_memcmp(db, phash, mdlen);
  430. bad |= from[0];
  431. looking_for_one_byte = 1;
  432. for (i = mdlen; i < dblen; i++) {
  433. int equals1 = constant_time_byte_eq(db[i], 1);
  434. int equals0 = constant_time_byte_eq(db[i], 0);
  435. one_index =
  436. constant_time_select(looking_for_one_byte & equals1, i, one_index);
  437. looking_for_one_byte =
  438. constant_time_select(equals1, 0, looking_for_one_byte);
  439. bad |= looking_for_one_byte & ~equals0;
  440. }
  441. bad |= looking_for_one_byte;
  442. if (bad) {
  443. goto decoding_err;
  444. }
  445. one_index++;
  446. mlen = dblen - one_index;
  447. if (tlen < mlen) {
  448. OPENSSL_PUT_ERROR(RSA, RSA_padding_check_PKCS1_OAEP_mgf1,
  449. RSA_R_DATA_TOO_LARGE);
  450. mlen = -1;
  451. } else {
  452. memcpy(to, db + one_index, mlen);
  453. }
  454. OPENSSL_free(db);
  455. return mlen;
  456. decoding_err:
  457. /* to avoid chosen ciphertext attacks, the error message should not reveal
  458. * which kind of decoding error happened */
  459. OPENSSL_PUT_ERROR(RSA, RSA_padding_check_PKCS1_OAEP_mgf1,
  460. RSA_R_OAEP_DECODING_ERROR);
  461. err:
  462. OPENSSL_free(db);
  463. return -1;
  464. }
  465. static const unsigned char zeroes[] = {0,0,0,0,0,0,0,0};
  466. int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const uint8_t *mHash,
  467. const EVP_MD *Hash, const EVP_MD *mgf1Hash,
  468. const uint8_t *EM, int sLen) {
  469. int i;
  470. int ret = 0;
  471. int maskedDBLen, MSBits, emLen;
  472. size_t hLen;
  473. const uint8_t *H;
  474. uint8_t *DB = NULL;
  475. EVP_MD_CTX ctx;
  476. uint8_t H_[EVP_MAX_MD_SIZE];
  477. EVP_MD_CTX_init(&ctx);
  478. if (mgf1Hash == NULL) {
  479. mgf1Hash = Hash;
  480. }
  481. hLen = EVP_MD_size(Hash);
  482. /* Negative sLen has special meanings:
  483. * -1 sLen == hLen
  484. * -2 salt length is autorecovered from signature
  485. * -N reserved */
  486. if (sLen == -1) {
  487. sLen = hLen;
  488. } else if (sLen == -2) {
  489. sLen = -2;
  490. } else if (sLen < -2) {
  491. OPENSSL_PUT_ERROR(RSA, RSA_verify_PKCS1_PSS_mgf1, RSA_R_SLEN_CHECK_FAILED);
  492. goto err;
  493. }
  494. MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
  495. emLen = RSA_size(rsa);
  496. if (EM[0] & (0xFF << MSBits)) {
  497. OPENSSL_PUT_ERROR(RSA, RSA_verify_PKCS1_PSS_mgf1,
  498. RSA_R_FIRST_OCTET_INVALID);
  499. goto err;
  500. }
  501. if (MSBits == 0) {
  502. EM++;
  503. emLen--;
  504. }
  505. if (emLen < ((int)hLen + sLen + 2)) {
  506. /* sLen can be small negative */
  507. OPENSSL_PUT_ERROR(RSA, RSA_verify_PKCS1_PSS_mgf1, RSA_R_DATA_TOO_LARGE);
  508. goto err;
  509. }
  510. if (EM[emLen - 1] != 0xbc) {
  511. OPENSSL_PUT_ERROR(RSA, RSA_verify_PKCS1_PSS_mgf1, RSA_R_LAST_OCTET_INVALID);
  512. goto err;
  513. }
  514. maskedDBLen = emLen - hLen - 1;
  515. H = EM + maskedDBLen;
  516. DB = OPENSSL_malloc(maskedDBLen);
  517. if (!DB) {
  518. OPENSSL_PUT_ERROR(RSA, RSA_verify_PKCS1_PSS_mgf1, ERR_R_MALLOC_FAILURE);
  519. goto err;
  520. }
  521. if (PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash) < 0) {
  522. goto err;
  523. }
  524. for (i = 0; i < maskedDBLen; i++) {
  525. DB[i] ^= EM[i];
  526. }
  527. if (MSBits) {
  528. DB[0] &= 0xFF >> (8 - MSBits);
  529. }
  530. for (i = 0; DB[i] == 0 && i < (maskedDBLen - 1); i++) {
  531. ;
  532. }
  533. if (DB[i++] != 0x1) {
  534. OPENSSL_PUT_ERROR(RSA, RSA_verify_PKCS1_PSS_mgf1,
  535. RSA_R_SLEN_RECOVERY_FAILED);
  536. goto err;
  537. }
  538. if (sLen >= 0 && (maskedDBLen - i) != sLen) {
  539. OPENSSL_PUT_ERROR(RSA, RSA_verify_PKCS1_PSS_mgf1, RSA_R_SLEN_CHECK_FAILED);
  540. goto err;
  541. }
  542. if (!EVP_DigestInit_ex(&ctx, Hash, NULL) ||
  543. !EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes) ||
  544. !EVP_DigestUpdate(&ctx, mHash, hLen)) {
  545. goto err;
  546. }
  547. if (maskedDBLen - i) {
  548. if (!EVP_DigestUpdate(&ctx, DB + i, maskedDBLen - i)) {
  549. goto err;
  550. }
  551. }
  552. if (!EVP_DigestFinal_ex(&ctx, H_, NULL)) {
  553. goto err;
  554. }
  555. if (memcmp(H_, H, hLen)) {
  556. OPENSSL_PUT_ERROR(RSA, RSA_verify_PKCS1_PSS_mgf1, RSA_R_BAD_SIGNATURE);
  557. ret = 0;
  558. } else {
  559. ret = 1;
  560. }
  561. err:
  562. OPENSSL_free(DB);
  563. EVP_MD_CTX_cleanup(&ctx);
  564. return ret;
  565. }
  566. int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM,
  567. const unsigned char *mHash,
  568. const EVP_MD *Hash, const EVP_MD *mgf1Hash,
  569. int sLen) {
  570. int i;
  571. int ret = 0;
  572. size_t maskedDBLen, MSBits, emLen;
  573. size_t hLen;
  574. unsigned char *H, *salt = NULL, *p;
  575. EVP_MD_CTX ctx;
  576. if (mgf1Hash == NULL) {
  577. mgf1Hash = Hash;
  578. }
  579. hLen = EVP_MD_size(Hash);
  580. /* Negative sLen has special meanings:
  581. * -1 sLen == hLen
  582. * -2 salt length is maximized
  583. * -N reserved */
  584. if (sLen == -1) {
  585. sLen = hLen;
  586. } else if (sLen == -2) {
  587. sLen = -2;
  588. } else if (sLen < -2) {
  589. OPENSSL_PUT_ERROR(RSA, RSA_padding_add_PKCS1_PSS_mgf1,
  590. RSA_R_SLEN_CHECK_FAILED);
  591. goto err;
  592. }
  593. if (BN_is_zero(rsa->n)) {
  594. OPENSSL_PUT_ERROR(RSA, RSA_padding_add_PKCS1_PSS_mgf1,
  595. RSA_R_EMPTY_PUBLIC_KEY);
  596. goto err;
  597. }
  598. MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
  599. emLen = RSA_size(rsa);
  600. if (MSBits == 0) {
  601. assert(emLen >= 1);
  602. *EM++ = 0;
  603. emLen--;
  604. }
  605. if (sLen == -2) {
  606. if (emLen < hLen + 2) {
  607. OPENSSL_PUT_ERROR(RSA, RSA_padding_add_PKCS1_PSS_mgf1,
  608. RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  609. goto err;
  610. }
  611. sLen = emLen - hLen - 2;
  612. } else if (emLen < hLen + sLen + 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. if (sLen > 0) {
  618. salt = OPENSSL_malloc(sLen);
  619. if (!salt) {
  620. OPENSSL_PUT_ERROR(RSA, RSA_padding_add_PKCS1_PSS_mgf1,
  621. ERR_R_MALLOC_FAILURE);
  622. goto err;
  623. }
  624. if (!RAND_bytes(salt, sLen)) {
  625. goto err;
  626. }
  627. }
  628. maskedDBLen = emLen - hLen - 1;
  629. H = EM + maskedDBLen;
  630. EVP_MD_CTX_init(&ctx);
  631. if (!EVP_DigestInit_ex(&ctx, Hash, NULL) ||
  632. !EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes) ||
  633. !EVP_DigestUpdate(&ctx, mHash, hLen)) {
  634. goto err;
  635. }
  636. if (sLen && !EVP_DigestUpdate(&ctx, salt, sLen)) {
  637. goto err;
  638. }
  639. if (!EVP_DigestFinal_ex(&ctx, H, NULL)) {
  640. goto err;
  641. }
  642. EVP_MD_CTX_cleanup(&ctx);
  643. /* Generate dbMask in place then perform XOR on it */
  644. if (PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash)) {
  645. goto err;
  646. }
  647. p = EM;
  648. /* Initial PS XORs with all zeroes which is a NOP so just update
  649. * pointer. Note from a test above this value is guaranteed to
  650. * be non-negative. */
  651. p += emLen - sLen - hLen - 2;
  652. *p++ ^= 0x1;
  653. if (sLen > 0) {
  654. for (i = 0; i < sLen; i++) {
  655. *p++ ^= salt[i];
  656. }
  657. }
  658. if (MSBits) {
  659. EM[0] &= 0xFF >> (8 - MSBits);
  660. }
  661. /* H is already in place so just set final 0xbc */
  662. EM[emLen - 1] = 0xbc;
  663. ret = 1;
  664. err:
  665. OPENSSL_free(salt);
  666. return ret;
  667. }