rsa_oaep.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* crypto/rsa/rsa_oaep.c */
  2. /*
  3. * Written by Ulf Moeller. This software is distributed on an "AS IS" basis,
  4. * WITHOUT WARRANTY OF ANY KIND, either express or implied.
  5. */
  6. /* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */
  7. /*
  8. * See Victor Shoup, "OAEP reconsidered," Nov. 2000, <URL:
  9. * http://www.shoup.net/papers/oaep.ps.Z> for problems with the security
  10. * proof for the original OAEP scheme, which EME-OAEP is based on. A new
  11. * proof can be found in E. Fujisaki, T. Okamoto, D. Pointcheval, J. Stern,
  12. * "RSA-OEAP is Still Alive!", Dec. 2000, <URL:
  13. * http://eprint.iacr.org/2000/061/>. The new proof has stronger requirements
  14. * for the underlying permutation: "partial-one-wayness" instead of
  15. * one-wayness. For the RSA function, this is an equivalent notion.
  16. */
  17. #include "constant_time_locl.h"
  18. #if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1)
  19. # include <stdio.h>
  20. # include "cryptlib.h"
  21. # include <openssl/bn.h>
  22. # include <openssl/rsa.h>
  23. # include <openssl/evp.h>
  24. # include <openssl/rand.h>
  25. # include <openssl/sha.h>
  26. int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
  27. const unsigned char *from, int flen,
  28. const unsigned char *param, int plen)
  29. {
  30. return RSA_padding_add_PKCS1_OAEP_mgf1(to, tlen, from, flen,
  31. param, plen, NULL, NULL);
  32. }
  33. int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
  34. const unsigned char *from, int flen,
  35. const unsigned char *param, int plen,
  36. const EVP_MD *md, const EVP_MD *mgf1md)
  37. {
  38. int i, emlen = tlen - 1;
  39. unsigned char *db, *seed;
  40. unsigned char *dbmask, seedmask[EVP_MAX_MD_SIZE];
  41. int mdlen;
  42. if (md == NULL)
  43. md = EVP_sha1();
  44. if (mgf1md == NULL)
  45. mgf1md = md;
  46. mdlen = EVP_MD_size(md);
  47. if (flen > emlen - 2 * mdlen - 1) {
  48. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
  49. RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  50. return 0;
  51. }
  52. if (emlen < 2 * mdlen + 1) {
  53. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
  54. RSA_R_KEY_SIZE_TOO_SMALL);
  55. return 0;
  56. }
  57. to[0] = 0;
  58. seed = to + 1;
  59. db = to + mdlen + 1;
  60. if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL))
  61. return 0;
  62. memset(db + mdlen, 0, emlen - flen - 2 * mdlen - 1);
  63. db[emlen - flen - mdlen - 1] = 0x01;
  64. memcpy(db + emlen - flen - mdlen, from, (unsigned int)flen);
  65. if (RAND_bytes(seed, mdlen) <= 0)
  66. return 0;
  67. # ifdef PKCS_TESTVECT
  68. memcpy(seed,
  69. "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2\xf0\x6c\xb5\x8f",
  70. 20);
  71. # endif
  72. dbmask = OPENSSL_malloc(emlen - mdlen);
  73. if (dbmask == NULL) {
  74. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
  75. return 0;
  76. }
  77. if (PKCS1_MGF1(dbmask, emlen - mdlen, seed, mdlen, mgf1md) < 0)
  78. goto err;
  79. for (i = 0; i < emlen - mdlen; i++)
  80. db[i] ^= dbmask[i];
  81. if (PKCS1_MGF1(seedmask, mdlen, db, emlen - mdlen, mgf1md) < 0)
  82. goto err;
  83. for (i = 0; i < mdlen; i++)
  84. seed[i] ^= seedmask[i];
  85. OPENSSL_free(dbmask);
  86. return 1;
  87. err:
  88. OPENSSL_free(dbmask);
  89. return 0;
  90. }
  91. int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
  92. const unsigned char *from, int flen, int num,
  93. const unsigned char *param, int plen)
  94. {
  95. return RSA_padding_check_PKCS1_OAEP_mgf1(to, tlen, from, flen, num,
  96. param, plen, NULL, NULL);
  97. }
  98. int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
  99. const unsigned char *from, int flen,
  100. int num, const unsigned char *param,
  101. int plen, const EVP_MD *md,
  102. const EVP_MD *mgf1md)
  103. {
  104. int i, dblen, mlen = -1, one_index = 0, msg_index;
  105. unsigned int good, found_one_byte;
  106. const unsigned char *maskedseed, *maskeddb;
  107. /*
  108. * |em| is the encoded message, zero-padded to exactly |num| bytes: em =
  109. * Y || maskedSeed || maskedDB
  110. */
  111. unsigned char *db = NULL, *em = NULL, seed[EVP_MAX_MD_SIZE],
  112. phash[EVP_MAX_MD_SIZE];
  113. int mdlen;
  114. if (md == NULL)
  115. md = EVP_sha1();
  116. if (mgf1md == NULL)
  117. mgf1md = md;
  118. mdlen = EVP_MD_size(md);
  119. if (tlen <= 0 || flen <= 0)
  120. return -1;
  121. /*
  122. * |num| is the length of the modulus; |flen| is the length of the
  123. * encoded message. Therefore, for any |from| that was obtained by
  124. * decrypting a ciphertext, we must have |flen| <= |num|. Similarly,
  125. * num < 2 * mdlen + 2 must hold for the modulus irrespective of
  126. * the ciphertext, see PKCS #1 v2.2, section 7.1.2.
  127. * This does not leak any side-channel information.
  128. */
  129. if (num < flen || num < 2 * mdlen + 2)
  130. goto decoding_err;
  131. dblen = num - mdlen - 1;
  132. db = OPENSSL_malloc(dblen);
  133. em = OPENSSL_malloc(num);
  134. if (db == NULL || em == NULL) {
  135. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
  136. goto cleanup;
  137. }
  138. /*
  139. * Always do this zero-padding copy (even when num == flen) to avoid
  140. * leaking that information. The copy still leaks some side-channel
  141. * information, but it's impossible to have a fixed memory access
  142. * pattern since we can't read out of the bounds of |from|.
  143. *
  144. * TODO(emilia): Consider porting BN_bn2bin_padded from BoringSSL.
  145. */
  146. memset(em, 0, num);
  147. memcpy(em + num - flen, from, flen);
  148. /*
  149. * The first byte must be zero, however we must not leak if this is
  150. * true. See James H. Manger, "A Chosen Ciphertext Attack on RSA
  151. * Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001).
  152. */
  153. good = constant_time_is_zero(em[0]);
  154. maskedseed = em + 1;
  155. maskeddb = em + 1 + mdlen;
  156. if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md))
  157. goto cleanup;
  158. for (i = 0; i < mdlen; i++)
  159. seed[i] ^= maskedseed[i];
  160. if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md))
  161. goto cleanup;
  162. for (i = 0; i < dblen; i++)
  163. db[i] ^= maskeddb[i];
  164. if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL))
  165. goto cleanup;
  166. good &= constant_time_is_zero(CRYPTO_memcmp(db, phash, mdlen));
  167. found_one_byte = 0;
  168. for (i = mdlen; i < dblen; i++) {
  169. /*
  170. * Padding consists of a number of 0-bytes, followed by a 1.
  171. */
  172. unsigned int equals1 = constant_time_eq(db[i], 1);
  173. unsigned int equals0 = constant_time_is_zero(db[i]);
  174. one_index = constant_time_select_int(~found_one_byte & equals1,
  175. i, one_index);
  176. found_one_byte |= equals1;
  177. good &= (found_one_byte | equals0);
  178. }
  179. good &= found_one_byte;
  180. /*
  181. * At this point |good| is zero unless the plaintext was valid,
  182. * so plaintext-awareness ensures timing side-channels are no longer a
  183. * concern.
  184. */
  185. if (!good)
  186. goto decoding_err;
  187. msg_index = one_index + 1;
  188. mlen = dblen - msg_index;
  189. if (tlen < mlen) {
  190. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, RSA_R_DATA_TOO_LARGE);
  191. mlen = -1;
  192. } else {
  193. memcpy(to, db + msg_index, mlen);
  194. goto cleanup;
  195. }
  196. decoding_err:
  197. /*
  198. * To avoid chosen ciphertext attacks, the error message should not
  199. * reveal which kind of decoding error happened.
  200. */
  201. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1,
  202. RSA_R_OAEP_DECODING_ERROR);
  203. cleanup:
  204. if (db != NULL)
  205. OPENSSL_free(db);
  206. if (em != NULL)
  207. OPENSSL_free(em);
  208. return mlen;
  209. }
  210. int PKCS1_MGF1(unsigned char *mask, long len,
  211. const unsigned char *seed, long seedlen, const EVP_MD *dgst)
  212. {
  213. long i, outlen = 0;
  214. unsigned char cnt[4];
  215. EVP_MD_CTX c;
  216. unsigned char md[EVP_MAX_MD_SIZE];
  217. int mdlen;
  218. int rv = -1;
  219. EVP_MD_CTX_init(&c);
  220. mdlen = EVP_MD_size(dgst);
  221. if (mdlen < 0)
  222. goto err;
  223. for (i = 0; outlen < len; i++) {
  224. cnt[0] = (unsigned char)((i >> 24) & 255);
  225. cnt[1] = (unsigned char)((i >> 16) & 255);
  226. cnt[2] = (unsigned char)((i >> 8)) & 255;
  227. cnt[3] = (unsigned char)(i & 255);
  228. if (!EVP_DigestInit_ex(&c, dgst, NULL)
  229. || !EVP_DigestUpdate(&c, seed, seedlen)
  230. || !EVP_DigestUpdate(&c, cnt, 4))
  231. goto err;
  232. if (outlen + mdlen <= len) {
  233. if (!EVP_DigestFinal_ex(&c, mask + outlen, NULL))
  234. goto err;
  235. outlen += mdlen;
  236. } else {
  237. if (!EVP_DigestFinal_ex(&c, md, NULL))
  238. goto err;
  239. memcpy(mask + outlen, md, len - outlen);
  240. outlen = len;
  241. }
  242. }
  243. rv = 0;
  244. err:
  245. EVP_MD_CTX_cleanup(&c);
  246. return rv;
  247. }
  248. #endif