rsa_oaep.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */
  10. /*
  11. * See Victor Shoup, "OAEP reconsidered," Nov. 2000, <URL:
  12. * http://www.shoup.net/papers/oaep.ps.Z> for problems with the security
  13. * proof for the original OAEP scheme, which EME-OAEP is based on. A new
  14. * proof can be found in E. Fujisaki, T. Okamoto, D. Pointcheval, J. Stern,
  15. * "RSA-OEAP is Still Alive!", Dec. 2000, <URL:
  16. * http://eprint.iacr.org/2000/061/>. The new proof has stronger requirements
  17. * for the underlying permutation: "partial-one-wayness" instead of
  18. * one-wayness. For the RSA function, this is an equivalent notion.
  19. */
  20. #include "internal/constant_time_locl.h"
  21. #include <stdio.h>
  22. #include "internal/cryptlib.h"
  23. #include <openssl/bn.h>
  24. #include <openssl/evp.h>
  25. #include <openssl/rand.h>
  26. #include <openssl/sha.h>
  27. #include "rsa_locl.h"
  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. return RSA_padding_add_PKCS1_OAEP_mgf1(to, tlen, from, flen,
  33. param, plen, NULL, NULL);
  34. }
  35. int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
  36. const unsigned char *from, int flen,
  37. const unsigned char *param, int plen,
  38. const EVP_MD *md, const EVP_MD *mgf1md)
  39. {
  40. int rv = 0;
  41. int i, emlen = tlen - 1;
  42. unsigned char *db, *seed;
  43. unsigned char *dbmask = NULL;
  44. unsigned char seedmask[EVP_MAX_MD_SIZE];
  45. int mdlen, dbmask_len = 0;
  46. if (md == NULL)
  47. md = EVP_sha1();
  48. if (mgf1md == NULL)
  49. mgf1md = md;
  50. mdlen = EVP_MD_size(md);
  51. if (flen > emlen - 2 * mdlen - 1) {
  52. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
  53. RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  54. return 0;
  55. }
  56. if (emlen < 2 * mdlen + 1) {
  57. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
  58. RSA_R_KEY_SIZE_TOO_SMALL);
  59. return 0;
  60. }
  61. to[0] = 0;
  62. seed = to + 1;
  63. db = to + mdlen + 1;
  64. if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL))
  65. goto err;
  66. memset(db + mdlen, 0, emlen - flen - 2 * mdlen - 1);
  67. db[emlen - flen - mdlen - 1] = 0x01;
  68. memcpy(db + emlen - flen - mdlen, from, (unsigned int)flen);
  69. if (RAND_bytes(seed, mdlen) <= 0)
  70. goto err;
  71. dbmask_len = emlen - mdlen;
  72. dbmask = OPENSSL_malloc(dbmask_len);
  73. if (dbmask == NULL) {
  74. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
  75. goto err;
  76. }
  77. if (PKCS1_MGF1(dbmask, dbmask_len, seed, mdlen, mgf1md) < 0)
  78. goto err;
  79. for (i = 0; i < dbmask_len; i++)
  80. db[i] ^= dbmask[i];
  81. if (PKCS1_MGF1(seedmask, mdlen, db, dbmask_len, mgf1md) < 0)
  82. goto err;
  83. for (i = 0; i < mdlen; i++)
  84. seed[i] ^= seedmask[i];
  85. rv = 1;
  86. err:
  87. OPENSSL_cleanse(seedmask, sizeof(seedmask));
  88. OPENSSL_clear_free(dbmask, dbmask_len);
  89. return rv;
  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 = 0, mlen = -1, one_index = 0, msg_index;
  105. unsigned int good = 0, found_one_byte, mask;
  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. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1,
  131. RSA_R_OAEP_DECODING_ERROR);
  132. return -1;
  133. }
  134. dblen = num - mdlen - 1;
  135. db = OPENSSL_malloc(dblen);
  136. if (db == NULL) {
  137. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
  138. goto cleanup;
  139. }
  140. em = OPENSSL_malloc(num);
  141. if (em == NULL) {
  142. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1,
  143. ERR_R_MALLOC_FAILURE);
  144. goto cleanup;
  145. }
  146. /*
  147. * Caller is encouraged to pass zero-padded message created with
  148. * BN_bn2binpad. Trouble is that since we can't read out of |from|'s
  149. * bounds, it's impossible to have an invariant memory access pattern
  150. * in case |from| was not zero-padded in advance.
  151. */
  152. for (from += flen, em += num, i = 0; i < num; i++) {
  153. mask = ~constant_time_is_zero(flen);
  154. flen -= 1 & mask;
  155. from -= 1 & mask;
  156. *--em = *from & mask;
  157. }
  158. from = em;
  159. /*
  160. * The first byte must be zero, however we must not leak if this is
  161. * true. See James H. Manger, "A Chosen Ciphertext Attack on RSA
  162. * Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001).
  163. */
  164. good = constant_time_is_zero(from[0]);
  165. maskedseed = from + 1;
  166. maskeddb = from + 1 + mdlen;
  167. if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md))
  168. goto cleanup;
  169. for (i = 0; i < mdlen; i++)
  170. seed[i] ^= maskedseed[i];
  171. if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md))
  172. goto cleanup;
  173. for (i = 0; i < dblen; i++)
  174. db[i] ^= maskeddb[i];
  175. if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL))
  176. goto cleanup;
  177. good &= constant_time_is_zero(CRYPTO_memcmp(db, phash, mdlen));
  178. found_one_byte = 0;
  179. for (i = mdlen; i < dblen; i++) {
  180. /*
  181. * Padding consists of a number of 0-bytes, followed by a 1.
  182. */
  183. unsigned int equals1 = constant_time_eq(db[i], 1);
  184. unsigned int equals0 = constant_time_is_zero(db[i]);
  185. one_index = constant_time_select_int(~found_one_byte & equals1,
  186. i, one_index);
  187. found_one_byte |= equals1;
  188. good &= (found_one_byte | equals0);
  189. }
  190. good &= found_one_byte;
  191. /*
  192. * At this point |good| is zero unless the plaintext was valid,
  193. * so plaintext-awareness ensures timing side-channels are no longer a
  194. * concern.
  195. */
  196. msg_index = one_index + 1;
  197. mlen = dblen - msg_index;
  198. /*
  199. * For good measure, do this check in constant tine as well.
  200. */
  201. good &= constant_time_ge(tlen, mlen);
  202. /*
  203. * Even though we can't fake result's length, we can pretend copying
  204. * |tlen| bytes where |mlen| bytes would be real. Last |tlen| of |dblen|
  205. * bytes are viewed as circular buffer with start at |tlen|-|mlen'|,
  206. * where |mlen'| is "saturated" |mlen| value. Deducing information
  207. * about failure or |mlen| would take attacker's ability to observe
  208. * memory access pattern with byte granularity *as it occurs*. It
  209. * should be noted that failure is indistinguishable from normal
  210. * operation if |tlen| is fixed by protocol.
  211. */
  212. tlen = constant_time_select_int(constant_time_lt(dblen, tlen), dblen, tlen);
  213. msg_index = constant_time_select_int(good, msg_index, dblen - tlen);
  214. mlen = dblen - msg_index;
  215. for (from = db + msg_index, mask = good, i = 0; i < tlen; i++) {
  216. unsigned int equals = constant_time_eq(i, mlen);
  217. from -= dblen & equals; /* if (i == dblen) rewind */
  218. mask &= mask ^ equals; /* if (i == dblen) mask = 0 */
  219. to[i] = constant_time_select_8(mask, from[i], to[i]);
  220. }
  221. /*
  222. * To avoid chosen ciphertext attacks, the error message should not
  223. * reveal which kind of decoding error happened.
  224. */
  225. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1,
  226. RSA_R_OAEP_DECODING_ERROR);
  227. err_clear_last_constant_time(1 & good);
  228. cleanup:
  229. OPENSSL_cleanse(seed, sizeof(seed));
  230. OPENSSL_clear_free(db, dblen);
  231. OPENSSL_clear_free(em, num);
  232. return constant_time_select_int(good, mlen, -1);
  233. }
  234. int PKCS1_MGF1(unsigned char *mask, long len,
  235. const unsigned char *seed, long seedlen, const EVP_MD *dgst)
  236. {
  237. long i, outlen = 0;
  238. unsigned char cnt[4];
  239. EVP_MD_CTX *c = EVP_MD_CTX_new();
  240. unsigned char md[EVP_MAX_MD_SIZE];
  241. int mdlen;
  242. int rv = -1;
  243. if (c == NULL)
  244. goto err;
  245. mdlen = EVP_MD_size(dgst);
  246. if (mdlen < 0)
  247. goto err;
  248. for (i = 0; outlen < len; i++) {
  249. cnt[0] = (unsigned char)((i >> 24) & 255);
  250. cnt[1] = (unsigned char)((i >> 16) & 255);
  251. cnt[2] = (unsigned char)((i >> 8)) & 255;
  252. cnt[3] = (unsigned char)(i & 255);
  253. if (!EVP_DigestInit_ex(c, dgst, NULL)
  254. || !EVP_DigestUpdate(c, seed, seedlen)
  255. || !EVP_DigestUpdate(c, cnt, 4))
  256. goto err;
  257. if (outlen + mdlen <= len) {
  258. if (!EVP_DigestFinal_ex(c, mask + outlen, NULL))
  259. goto err;
  260. outlen += mdlen;
  261. } else {
  262. if (!EVP_DigestFinal_ex(c, md, NULL))
  263. goto err;
  264. memcpy(mask + outlen, md, len - outlen);
  265. outlen = len;
  266. }
  267. }
  268. rv = 0;
  269. err:
  270. OPENSSL_cleanse(md, sizeof(md));
  271. EVP_MD_CTX_free(c);
  272. return rv;
  273. }