rsa_oaep.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. static int MGF1(unsigned char *mask, long len,
  27. const unsigned char *seed, long seedlen);
  28. int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
  29. const unsigned char *from, int flen,
  30. const unsigned char *param, int plen)
  31. {
  32. int i, emlen = tlen - 1;
  33. unsigned char *db, *seed;
  34. unsigned char *dbmask, seedmask[SHA_DIGEST_LENGTH];
  35. if (flen > emlen - 2 * SHA_DIGEST_LENGTH - 1) {
  36. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP,
  37. RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  38. return 0;
  39. }
  40. if (emlen < 2 * SHA_DIGEST_LENGTH + 1) {
  41. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, RSA_R_KEY_SIZE_TOO_SMALL);
  42. return 0;
  43. }
  44. to[0] = 0;
  45. seed = to + 1;
  46. db = to + SHA_DIGEST_LENGTH + 1;
  47. if (!EVP_Digest((void *)param, plen, db, NULL, EVP_sha1(), NULL))
  48. return 0;
  49. memset(db + SHA_DIGEST_LENGTH, 0,
  50. emlen - flen - 2 * SHA_DIGEST_LENGTH - 1);
  51. db[emlen - flen - SHA_DIGEST_LENGTH - 1] = 0x01;
  52. memcpy(db + emlen - flen - SHA_DIGEST_LENGTH, from, (unsigned int)flen);
  53. if (RAND_bytes(seed, SHA_DIGEST_LENGTH) <= 0)
  54. return 0;
  55. # ifdef PKCS_TESTVECT
  56. memcpy(seed,
  57. "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2\xf0\x6c\xb5\x8f",
  58. 20);
  59. # endif
  60. dbmask = OPENSSL_malloc(emlen - SHA_DIGEST_LENGTH);
  61. if (dbmask == NULL) {
  62. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
  63. return 0;
  64. }
  65. if (MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed, SHA_DIGEST_LENGTH) < 0)
  66. return 0;
  67. for (i = 0; i < emlen - SHA_DIGEST_LENGTH; i++)
  68. db[i] ^= dbmask[i];
  69. if (MGF1(seedmask, SHA_DIGEST_LENGTH, db, emlen - SHA_DIGEST_LENGTH) < 0)
  70. return 0;
  71. for (i = 0; i < SHA_DIGEST_LENGTH; i++)
  72. seed[i] ^= seedmask[i];
  73. OPENSSL_free(dbmask);
  74. return 1;
  75. }
  76. int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
  77. const unsigned char *from, int flen, int num,
  78. const unsigned char *param, int plen)
  79. {
  80. int i, dblen, mlen = -1, one_index = 0, msg_index;
  81. unsigned int good, found_one_byte;
  82. const unsigned char *maskedseed, *maskeddb;
  83. /*
  84. * |em| is the encoded message, zero-padded to exactly |num| bytes: em =
  85. * Y || maskedSeed || maskedDB
  86. */
  87. unsigned char *db = NULL, *em = NULL, seed[EVP_MAX_MD_SIZE],
  88. phash[EVP_MAX_MD_SIZE];
  89. if (tlen <= 0 || flen <= 0)
  90. return -1;
  91. /*
  92. * |num| is the length of the modulus; |flen| is the length of the
  93. * encoded message. Therefore, for any |from| that was obtained by
  94. * decrypting a ciphertext, we must have |flen| <= |num|. Similarly,
  95. * num < 2 * SHA_DIGEST_LENGTH + 2 must hold for the modulus
  96. * irrespective of the ciphertext, see PKCS #1 v2.2, section 7.1.2.
  97. * This does not leak any side-channel information.
  98. */
  99. if (num < flen || num < 2 * SHA_DIGEST_LENGTH + 2)
  100. goto decoding_err;
  101. dblen = num - SHA_DIGEST_LENGTH - 1;
  102. db = OPENSSL_malloc(dblen);
  103. em = OPENSSL_malloc(num);
  104. if (db == NULL || em == NULL) {
  105. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
  106. goto cleanup;
  107. }
  108. /*
  109. * Always do this zero-padding copy (even when num == flen) to avoid
  110. * leaking that information. The copy still leaks some side-channel
  111. * information, but it's impossible to have a fixed memory access
  112. * pattern since we can't read out of the bounds of |from|.
  113. *
  114. * TODO(emilia): Consider porting BN_bn2bin_padded from BoringSSL.
  115. */
  116. memset(em, 0, num);
  117. memcpy(em + num - flen, from, flen);
  118. /*
  119. * The first byte must be zero, however we must not leak if this is
  120. * true. See James H. Manger, "A Chosen Ciphertext Attack on RSA
  121. * Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001).
  122. */
  123. good = constant_time_is_zero(em[0]);
  124. maskedseed = em + 1;
  125. maskeddb = em + 1 + SHA_DIGEST_LENGTH;
  126. if (MGF1(seed, SHA_DIGEST_LENGTH, maskeddb, dblen))
  127. goto cleanup;
  128. for (i = 0; i < SHA_DIGEST_LENGTH; i++)
  129. seed[i] ^= maskedseed[i];
  130. if (MGF1(db, dblen, seed, SHA_DIGEST_LENGTH))
  131. goto cleanup;
  132. for (i = 0; i < dblen; i++)
  133. db[i] ^= maskeddb[i];
  134. if (!EVP_Digest((void *)param, plen, phash, NULL, EVP_sha1(), NULL))
  135. goto cleanup;
  136. good &=
  137. constant_time_is_zero(CRYPTO_memcmp(db, phash, SHA_DIGEST_LENGTH));
  138. found_one_byte = 0;
  139. for (i = SHA_DIGEST_LENGTH; i < dblen; i++) {
  140. /*
  141. * Padding consists of a number of 0-bytes, followed by a 1.
  142. */
  143. unsigned int equals1 = constant_time_eq(db[i], 1);
  144. unsigned int equals0 = constant_time_is_zero(db[i]);
  145. one_index = constant_time_select_int(~found_one_byte & equals1,
  146. i, one_index);
  147. found_one_byte |= equals1;
  148. good &= (found_one_byte | equals0);
  149. }
  150. good &= found_one_byte;
  151. /*
  152. * At this point |good| is zero unless the plaintext was valid,
  153. * so plaintext-awareness ensures timing side-channels are no longer a
  154. * concern.
  155. */
  156. if (!good)
  157. goto decoding_err;
  158. msg_index = one_index + 1;
  159. mlen = dblen - msg_index;
  160. if (tlen < mlen) {
  161. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE);
  162. mlen = -1;
  163. } else {
  164. memcpy(to, db + msg_index, mlen);
  165. goto cleanup;
  166. }
  167. decoding_err:
  168. /*
  169. * To avoid chosen ciphertext attacks, the error message should not
  170. * reveal which kind of decoding error happened.
  171. */
  172. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR);
  173. cleanup:
  174. if (db != NULL)
  175. OPENSSL_free(db);
  176. if (em != NULL)
  177. OPENSSL_free(em);
  178. return mlen;
  179. }
  180. int PKCS1_MGF1(unsigned char *mask, long len,
  181. const unsigned char *seed, long seedlen, const EVP_MD *dgst)
  182. {
  183. long i, outlen = 0;
  184. unsigned char cnt[4];
  185. EVP_MD_CTX c;
  186. unsigned char md[EVP_MAX_MD_SIZE];
  187. int mdlen;
  188. int rv = -1;
  189. EVP_MD_CTX_init(&c);
  190. mdlen = EVP_MD_size(dgst);
  191. if (mdlen < 0)
  192. goto err;
  193. for (i = 0; outlen < len; i++) {
  194. cnt[0] = (unsigned char)((i >> 24) & 255);
  195. cnt[1] = (unsigned char)((i >> 16) & 255);
  196. cnt[2] = (unsigned char)((i >> 8)) & 255;
  197. cnt[3] = (unsigned char)(i & 255);
  198. if (!EVP_DigestInit_ex(&c, dgst, NULL)
  199. || !EVP_DigestUpdate(&c, seed, seedlen)
  200. || !EVP_DigestUpdate(&c, cnt, 4))
  201. goto err;
  202. if (outlen + mdlen <= len) {
  203. if (!EVP_DigestFinal_ex(&c, mask + outlen, NULL))
  204. goto err;
  205. outlen += mdlen;
  206. } else {
  207. if (!EVP_DigestFinal_ex(&c, md, NULL))
  208. goto err;
  209. memcpy(mask + outlen, md, len - outlen);
  210. outlen = len;
  211. }
  212. }
  213. rv = 0;
  214. err:
  215. EVP_MD_CTX_cleanup(&c);
  216. return rv;
  217. }
  218. static int MGF1(unsigned char *mask, long len, const unsigned char *seed,
  219. long seedlen)
  220. {
  221. return PKCS1_MGF1(mask, len, seed, seedlen, EVP_sha1());
  222. }
  223. #endif