rsa_oaep.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. /*
  21. * RSA low level APIs are deprecated for public use, but still ok for
  22. * internal use.
  23. */
  24. #include "internal/deprecated.h"
  25. #include "internal/constant_time.h"
  26. #include <stdio.h>
  27. #include "internal/cryptlib.h"
  28. #include <openssl/bn.h>
  29. #include <openssl/evp.h>
  30. #include <openssl/rand.h>
  31. #include <openssl/sha.h>
  32. #include "rsa_local.h"
  33. int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
  34. const unsigned char *from, int flen,
  35. const unsigned char *param, int plen)
  36. {
  37. return ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(NULL, to, tlen, from, flen,
  38. param, plen, NULL, NULL);
  39. }
  40. /*
  41. * Perform the padding as per NIST 800-56B 7.2.2.3
  42. * from (K) is the key material.
  43. * param (A) is the additional input.
  44. * Step numbers are included here but not in the constant time inverse below
  45. * to avoid complicating an already difficult enough function.
  46. */
  47. int ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(OSSL_LIB_CTX *libctx,
  48. unsigned char *to, int tlen,
  49. const unsigned char *from, int flen,
  50. const unsigned char *param,
  51. int plen, const EVP_MD *md,
  52. const EVP_MD *mgf1md)
  53. {
  54. int rv = 0;
  55. int i, emlen = tlen - 1;
  56. unsigned char *db, *seed;
  57. unsigned char *dbmask = NULL;
  58. unsigned char seedmask[EVP_MAX_MD_SIZE];
  59. int mdlen, dbmask_len = 0;
  60. if (md == NULL) {
  61. #ifndef FIPS_MODULE
  62. md = EVP_sha1();
  63. #else
  64. ERR_raise(ERR_LIB_RSA, ERR_R_PASSED_NULL_PARAMETER);
  65. return 0;
  66. #endif
  67. }
  68. if (mgf1md == NULL)
  69. mgf1md = md;
  70. mdlen = EVP_MD_get_size(md);
  71. if (mdlen <= 0) {
  72. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_LENGTH);
  73. return 0;
  74. }
  75. /* step 2b: check KLen > nLen - 2 HLen - 2 */
  76. if (flen > emlen - 2 * mdlen - 1) {
  77. ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  78. return 0;
  79. }
  80. if (emlen < 2 * mdlen + 1) {
  81. ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
  82. return 0;
  83. }
  84. /* step 3i: EM = 00000000 || maskedMGF || maskedDB */
  85. to[0] = 0;
  86. seed = to + 1;
  87. db = to + mdlen + 1;
  88. /* step 3a: hash the additional input */
  89. if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL))
  90. goto err;
  91. /* step 3b: zero bytes array of length nLen - KLen - 2 HLen -2 */
  92. memset(db + mdlen, 0, emlen - flen - 2 * mdlen - 1);
  93. /* step 3c: DB = HA || PS || 00000001 || K */
  94. db[emlen - flen - mdlen - 1] = 0x01;
  95. memcpy(db + emlen - flen - mdlen, from, (unsigned int)flen);
  96. /* step 3d: generate random byte string */
  97. if (RAND_bytes_ex(libctx, seed, mdlen, 0) <= 0)
  98. goto err;
  99. dbmask_len = emlen - mdlen;
  100. dbmask = OPENSSL_malloc(dbmask_len);
  101. if (dbmask == NULL) {
  102. ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
  103. goto err;
  104. }
  105. /* step 3e: dbMask = MGF(mgfSeed, nLen - HLen - 1) */
  106. if (PKCS1_MGF1(dbmask, dbmask_len, seed, mdlen, mgf1md) < 0)
  107. goto err;
  108. /* step 3f: maskedDB = DB XOR dbMask */
  109. for (i = 0; i < dbmask_len; i++)
  110. db[i] ^= dbmask[i];
  111. /* step 3g: mgfSeed = MGF(maskedDB, HLen) */
  112. if (PKCS1_MGF1(seedmask, mdlen, db, dbmask_len, mgf1md) < 0)
  113. goto err;
  114. /* stepo 3h: maskedMGFSeed = mgfSeed XOR mgfSeedMask */
  115. for (i = 0; i < mdlen; i++)
  116. seed[i] ^= seedmask[i];
  117. rv = 1;
  118. err:
  119. OPENSSL_cleanse(seedmask, sizeof(seedmask));
  120. OPENSSL_clear_free(dbmask, dbmask_len);
  121. return rv;
  122. }
  123. int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
  124. const unsigned char *from, int flen,
  125. const unsigned char *param, int plen,
  126. const EVP_MD *md, const EVP_MD *mgf1md)
  127. {
  128. return ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(NULL, to, tlen, from, flen,
  129. param, plen, md, mgf1md);
  130. }
  131. int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
  132. const unsigned char *from, int flen, int num,
  133. const unsigned char *param, int plen)
  134. {
  135. return RSA_padding_check_PKCS1_OAEP_mgf1(to, tlen, from, flen, num,
  136. param, plen, NULL, NULL);
  137. }
  138. int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
  139. const unsigned char *from, int flen,
  140. int num, const unsigned char *param,
  141. int plen, const EVP_MD *md,
  142. const EVP_MD *mgf1md)
  143. {
  144. int i, dblen = 0, mlen = -1, one_index = 0, msg_index;
  145. unsigned int good = 0, found_one_byte, mask;
  146. const unsigned char *maskedseed, *maskeddb;
  147. /*
  148. * |em| is the encoded message, zero-padded to exactly |num| bytes: em =
  149. * Y || maskedSeed || maskedDB
  150. */
  151. unsigned char *db = NULL, *em = NULL, seed[EVP_MAX_MD_SIZE],
  152. phash[EVP_MAX_MD_SIZE];
  153. int mdlen;
  154. if (md == NULL) {
  155. #ifndef FIPS_MODULE
  156. md = EVP_sha1();
  157. #else
  158. ERR_raise(ERR_LIB_RSA, ERR_R_PASSED_NULL_PARAMETER);
  159. return -1;
  160. #endif
  161. }
  162. if (mgf1md == NULL)
  163. mgf1md = md;
  164. mdlen = EVP_MD_get_size(md);
  165. if (tlen <= 0 || flen <= 0)
  166. return -1;
  167. /*
  168. * |num| is the length of the modulus; |flen| is the length of the
  169. * encoded message. Therefore, for any |from| that was obtained by
  170. * decrypting a ciphertext, we must have |flen| <= |num|. Similarly,
  171. * |num| >= 2 * |mdlen| + 2 must hold for the modulus irrespective of
  172. * the ciphertext, see PKCS #1 v2.2, section 7.1.2.
  173. * This does not leak any side-channel information.
  174. */
  175. if (num < flen || num < 2 * mdlen + 2) {
  176. ERR_raise(ERR_LIB_RSA, RSA_R_OAEP_DECODING_ERROR);
  177. return -1;
  178. }
  179. dblen = num - mdlen - 1;
  180. db = OPENSSL_malloc(dblen);
  181. if (db == NULL) {
  182. ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
  183. goto cleanup;
  184. }
  185. em = OPENSSL_malloc(num);
  186. if (em == NULL) {
  187. ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
  188. goto cleanup;
  189. }
  190. /*
  191. * Caller is encouraged to pass zero-padded message created with
  192. * BN_bn2binpad. Trouble is that since we can't read out of |from|'s
  193. * bounds, it's impossible to have an invariant memory access pattern
  194. * in case |from| was not zero-padded in advance.
  195. */
  196. for (from += flen, em += num, i = 0; i < num; i++) {
  197. mask = ~constant_time_is_zero(flen);
  198. flen -= 1 & mask;
  199. from -= 1 & mask;
  200. *--em = *from & mask;
  201. }
  202. /*
  203. * The first byte must be zero, however we must not leak if this is
  204. * true. See James H. Manger, "A Chosen Ciphertext Attack on RSA
  205. * Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001).
  206. */
  207. good = constant_time_is_zero(em[0]);
  208. maskedseed = em + 1;
  209. maskeddb = em + 1 + mdlen;
  210. if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md))
  211. goto cleanup;
  212. for (i = 0; i < mdlen; i++)
  213. seed[i] ^= maskedseed[i];
  214. if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md))
  215. goto cleanup;
  216. for (i = 0; i < dblen; i++)
  217. db[i] ^= maskeddb[i];
  218. if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL))
  219. goto cleanup;
  220. good &= constant_time_is_zero(CRYPTO_memcmp(db, phash, mdlen));
  221. found_one_byte = 0;
  222. for (i = mdlen; i < dblen; i++) {
  223. /*
  224. * Padding consists of a number of 0-bytes, followed by a 1.
  225. */
  226. unsigned int equals1 = constant_time_eq(db[i], 1);
  227. unsigned int equals0 = constant_time_is_zero(db[i]);
  228. one_index = constant_time_select_int(~found_one_byte & equals1,
  229. i, one_index);
  230. found_one_byte |= equals1;
  231. good &= (found_one_byte | equals0);
  232. }
  233. good &= found_one_byte;
  234. /*
  235. * At this point |good| is zero unless the plaintext was valid,
  236. * so plaintext-awareness ensures timing side-channels are no longer a
  237. * concern.
  238. */
  239. msg_index = one_index + 1;
  240. mlen = dblen - msg_index;
  241. /*
  242. * For good measure, do this check in constant time as well.
  243. */
  244. good &= constant_time_ge(tlen, mlen);
  245. /*
  246. * Move the result in-place by |dblen|-|mdlen|-1-|mlen| bytes to the left.
  247. * Then if |good| move |mlen| bytes from |db|+|mdlen|+1 to |to|.
  248. * Otherwise leave |to| unchanged.
  249. * Copy the memory back in a way that does not reveal the size of
  250. * the data being copied via a timing side channel. This requires copying
  251. * parts of the buffer multiple times based on the bits set in the real
  252. * length. Clear bits do a non-copy with identical access pattern.
  253. * The loop below has overall complexity of O(N*log(N)).
  254. */
  255. tlen = constant_time_select_int(constant_time_lt(dblen - mdlen - 1, tlen),
  256. dblen - mdlen - 1, tlen);
  257. for (msg_index = 1; msg_index < dblen - mdlen - 1; msg_index <<= 1) {
  258. mask = ~constant_time_eq(msg_index & (dblen - mdlen - 1 - mlen), 0);
  259. for (i = mdlen + 1; i < dblen - msg_index; i++)
  260. db[i] = constant_time_select_8(mask, db[i + msg_index], db[i]);
  261. }
  262. for (i = 0; i < tlen; i++) {
  263. mask = good & constant_time_lt(i, mlen);
  264. to[i] = constant_time_select_8(mask, db[i + mdlen + 1], to[i]);
  265. }
  266. #ifndef FIPS_MODULE
  267. /*
  268. * To avoid chosen ciphertext attacks, the error message should not
  269. * reveal which kind of decoding error happened.
  270. *
  271. * This trick doesn't work in the FIPS provider because libcrypto manages
  272. * the error stack. Instead we opt not to put an error on the stack at all
  273. * in case of padding failure in the FIPS provider.
  274. */
  275. ERR_raise(ERR_LIB_RSA, RSA_R_OAEP_DECODING_ERROR);
  276. err_clear_last_constant_time(1 & good);
  277. #endif
  278. cleanup:
  279. OPENSSL_cleanse(seed, sizeof(seed));
  280. OPENSSL_clear_free(db, dblen);
  281. OPENSSL_clear_free(em, num);
  282. return constant_time_select_int(good, mlen, -1);
  283. }
  284. /*
  285. * Mask Generation Function corresponding to section 7.2.2.2 of NIST SP 800-56B.
  286. * The variables are named differently to NIST:
  287. * mask (T) and len (maskLen)are the returned mask.
  288. * seed (mgfSeed).
  289. * The range checking steps inm the process are performed outside.
  290. */
  291. int PKCS1_MGF1(unsigned char *mask, long len,
  292. const unsigned char *seed, long seedlen, const EVP_MD *dgst)
  293. {
  294. long i, outlen = 0;
  295. unsigned char cnt[4];
  296. EVP_MD_CTX *c = EVP_MD_CTX_new();
  297. unsigned char md[EVP_MAX_MD_SIZE];
  298. int mdlen;
  299. int rv = -1;
  300. if (c == NULL)
  301. goto err;
  302. mdlen = EVP_MD_get_size(dgst);
  303. if (mdlen < 0)
  304. goto err;
  305. /* step 4 */
  306. for (i = 0; outlen < len; i++) {
  307. /* step 4a: D = I2BS(counter, 4) */
  308. cnt[0] = (unsigned char)((i >> 24) & 255);
  309. cnt[1] = (unsigned char)((i >> 16) & 255);
  310. cnt[2] = (unsigned char)((i >> 8)) & 255;
  311. cnt[3] = (unsigned char)(i & 255);
  312. /* step 4b: T =T || hash(mgfSeed || D) */
  313. if (!EVP_DigestInit_ex(c, dgst, NULL)
  314. || !EVP_DigestUpdate(c, seed, seedlen)
  315. || !EVP_DigestUpdate(c, cnt, 4))
  316. goto err;
  317. if (outlen + mdlen <= len) {
  318. if (!EVP_DigestFinal_ex(c, mask + outlen, NULL))
  319. goto err;
  320. outlen += mdlen;
  321. } else {
  322. if (!EVP_DigestFinal_ex(c, md, NULL))
  323. goto err;
  324. memcpy(mask + outlen, md, len - outlen);
  325. outlen = len;
  326. }
  327. }
  328. rv = 0;
  329. err:
  330. OPENSSL_cleanse(md, sizeof(md));
  331. EVP_MD_CTX_free(c);
  332. return rv;
  333. }