rsa_oaep.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* crypto/rsa/rsa_oaep.c */
  2. /* Written by Ulf Moeller. This software is distributed on an "AS IS"
  3. basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. */
  4. /* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */
  5. /* See Victor Shoup, "OAEP reconsidered," Nov. 2000,
  6. * <URL: http://www.shoup.net/papers/oaep.ps.Z>
  7. * for problems with the security proof for the
  8. * original OAEP scheme, which EME-OAEP is based on.
  9. *
  10. * A new proof can be found in E. Fujisaki, T. Okamoto,
  11. * D. Pointcheval, J. Stern, "RSA-OEAP is Still Alive!",
  12. * Dec. 2000, <URL: http://eprint.iacr.org/2000/061/>.
  13. * The new proof has stronger requirements for the
  14. * underlying permutation: "partial-one-wayness" instead
  15. * of one-wayness. For the RSA function, this is
  16. * an equivalent notion.
  17. */
  18. #define OPENSSL_FIPSAPI
  19. #if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1)
  20. #include <stdio.h>
  21. #include "cryptlib.h"
  22. #include <openssl/bn.h>
  23. #include <openssl/rsa.h>
  24. #include <openssl/evp.h>
  25. #include <openssl/rand.h>
  26. #include <openssl/sha.h>
  27. static int MGF1(unsigned char *mask, long len,
  28. const unsigned char *seed, long seedlen);
  29. int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
  30. const unsigned char *from, int flen,
  31. const unsigned char *param, int plen)
  32. {
  33. int i, emlen = tlen - 1;
  34. unsigned char *db, *seed;
  35. unsigned char *dbmask, seedmask[SHA_DIGEST_LENGTH];
  36. if (flen > emlen - 2 * SHA_DIGEST_LENGTH - 1)
  37. {
  38. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP,
  39. RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  40. return 0;
  41. }
  42. if (emlen < 2 * SHA_DIGEST_LENGTH + 1)
  43. {
  44. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, RSA_R_KEY_SIZE_TOO_SMALL);
  45. return 0;
  46. }
  47. to[0] = 0;
  48. seed = to + 1;
  49. db = to + SHA_DIGEST_LENGTH + 1;
  50. if (!EVP_Digest((void *)param, plen, db, NULL, EVP_sha1(), NULL))
  51. return 0;
  52. memset(db + SHA_DIGEST_LENGTH, 0,
  53. emlen - flen - 2 * SHA_DIGEST_LENGTH - 1);
  54. db[emlen - flen - SHA_DIGEST_LENGTH - 1] = 0x01;
  55. memcpy(db + emlen - flen - SHA_DIGEST_LENGTH, from, (unsigned int) flen);
  56. if (RAND_bytes(seed, SHA_DIGEST_LENGTH) <= 0)
  57. return 0;
  58. #ifdef PKCS_TESTVECT
  59. memcpy(seed,
  60. "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2\xf0\x6c\xb5\x8f",
  61. 20);
  62. #endif
  63. dbmask = OPENSSL_malloc(emlen - SHA_DIGEST_LENGTH);
  64. if (dbmask == NULL)
  65. {
  66. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
  67. return 0;
  68. }
  69. if (MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed, SHA_DIGEST_LENGTH) < 0)
  70. return 0;
  71. for (i = 0; i < emlen - SHA_DIGEST_LENGTH; i++)
  72. db[i] ^= dbmask[i];
  73. if (MGF1(seedmask, SHA_DIGEST_LENGTH, db, emlen - SHA_DIGEST_LENGTH) < 0)
  74. return 0;
  75. for (i = 0; i < SHA_DIGEST_LENGTH; i++)
  76. seed[i] ^= seedmask[i];
  77. OPENSSL_free(dbmask);
  78. return 1;
  79. }
  80. int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
  81. const unsigned char *from, int flen, int num,
  82. const unsigned char *param, int plen)
  83. {
  84. int i, dblen, mlen = -1;
  85. const unsigned char *maskeddb;
  86. int lzero;
  87. unsigned char *db = NULL, seed[SHA_DIGEST_LENGTH], phash[SHA_DIGEST_LENGTH];
  88. unsigned char *padded_from;
  89. int bad = 0;
  90. if (--num < 2 * SHA_DIGEST_LENGTH + 1)
  91. /* 'num' is the length of the modulus, i.e. does not depend on the
  92. * particular ciphertext. */
  93. goto decoding_err;
  94. lzero = num - flen;
  95. if (lzero < 0)
  96. {
  97. /* signalling this error immediately after detection might allow
  98. * for side-channel attacks (e.g. timing if 'plen' is huge
  99. * -- cf. James H. Manger, "A Chosen Ciphertext Attack on RSA Optimal
  100. * Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001),
  101. * so we use a 'bad' flag */
  102. bad = 1;
  103. lzero = 0;
  104. flen = num; /* don't overflow the memcpy to padded_from */
  105. }
  106. dblen = num - SHA_DIGEST_LENGTH;
  107. db = OPENSSL_malloc(dblen + num);
  108. if (db == NULL)
  109. {
  110. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
  111. return -1;
  112. }
  113. /* Always do this zero-padding copy (even when lzero == 0)
  114. * to avoid leaking timing info about the value of lzero. */
  115. padded_from = db + dblen;
  116. memset(padded_from, 0, lzero);
  117. memcpy(padded_from + lzero, from, flen);
  118. maskeddb = padded_from + SHA_DIGEST_LENGTH;
  119. if (MGF1(seed, SHA_DIGEST_LENGTH, maskeddb, dblen))
  120. return -1;
  121. for (i = 0; i < SHA_DIGEST_LENGTH; i++)
  122. seed[i] ^= padded_from[i];
  123. if (MGF1(db, dblen, seed, SHA_DIGEST_LENGTH))
  124. return -1;
  125. for (i = 0; i < dblen; i++)
  126. db[i] ^= maskeddb[i];
  127. if (!EVP_Digest((void *)param, plen, phash, NULL, EVP_sha1(), NULL))
  128. return -1;
  129. if (memcmp(db, phash, SHA_DIGEST_LENGTH) != 0 || bad)
  130. goto decoding_err;
  131. else
  132. {
  133. for (i = SHA_DIGEST_LENGTH; i < dblen; i++)
  134. if (db[i] != 0x00)
  135. break;
  136. if (i == dblen || db[i] != 0x01)
  137. goto decoding_err;
  138. else
  139. {
  140. /* everything looks OK */
  141. mlen = dblen - ++i;
  142. if (tlen < mlen)
  143. {
  144. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE);
  145. mlen = -1;
  146. }
  147. else
  148. memcpy(to, db + i, mlen);
  149. }
  150. }
  151. OPENSSL_free(db);
  152. return mlen;
  153. decoding_err:
  154. /* to avoid chosen ciphertext attacks, the error message should not reveal
  155. * which kind of decoding error happened */
  156. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR);
  157. if (db != NULL) OPENSSL_free(db);
  158. return -1;
  159. }
  160. int PKCS1_MGF1(unsigned char *mask, long len,
  161. const unsigned char *seed, long seedlen, const EVP_MD *dgst)
  162. {
  163. long i, outlen = 0;
  164. unsigned char cnt[4];
  165. EVP_MD_CTX c;
  166. unsigned char md[EVP_MAX_MD_SIZE];
  167. int mdlen;
  168. int rv = -1;
  169. EVP_MD_CTX_init(&c);
  170. mdlen = M_EVP_MD_size(dgst);
  171. if (mdlen < 0)
  172. goto err;
  173. for (i = 0; outlen < len; i++)
  174. {
  175. cnt[0] = (unsigned char)((i >> 24) & 255);
  176. cnt[1] = (unsigned char)((i >> 16) & 255);
  177. cnt[2] = (unsigned char)((i >> 8)) & 255;
  178. cnt[3] = (unsigned char)(i & 255);
  179. if (!EVP_DigestInit_ex(&c,dgst, NULL)
  180. || !EVP_DigestUpdate(&c, seed, seedlen)
  181. || !EVP_DigestUpdate(&c, cnt, 4))
  182. goto err;
  183. if (outlen + mdlen <= len)
  184. {
  185. if (!EVP_DigestFinal_ex(&c, mask + outlen, NULL))
  186. goto err;
  187. outlen += mdlen;
  188. }
  189. else
  190. {
  191. if (!EVP_DigestFinal_ex(&c, md, NULL))
  192. goto err;
  193. memcpy(mask + outlen, md, len - outlen);
  194. outlen = len;
  195. }
  196. }
  197. rv = 0;
  198. err:
  199. EVP_MD_CTX_cleanup(&c);
  200. return rv;
  201. }
  202. static int MGF1(unsigned char *mask, long len, const unsigned char *seed,
  203. long seedlen)
  204. {
  205. return PKCS1_MGF1(mask, len, seed, seedlen, EVP_sha1());
  206. }
  207. #endif