rsa_pss.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Copyright 2005-2023 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. /*
  10. * RSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include <openssl/bn.h>
  17. #include <openssl/rsa.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/rand.h>
  20. #include <openssl/sha.h>
  21. #include "rsa_local.h"
  22. static const unsigned char zeroes[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  23. #if defined(_MSC_VER) && defined(_ARM_)
  24. # pragma optimize("g", off)
  25. #endif
  26. int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
  27. const EVP_MD *Hash, const unsigned char *EM,
  28. int sLen)
  29. {
  30. return RSA_verify_PKCS1_PSS_mgf1(rsa, mHash, Hash, NULL, EM, sLen);
  31. }
  32. int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash,
  33. const EVP_MD *Hash, const EVP_MD *mgf1Hash,
  34. const unsigned char *EM, int sLen)
  35. {
  36. int i;
  37. int ret = 0;
  38. int hLen, maskedDBLen, MSBits, emLen;
  39. const unsigned char *H;
  40. unsigned char *DB = NULL;
  41. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  42. unsigned char H_[EVP_MAX_MD_SIZE];
  43. if (ctx == NULL)
  44. goto err;
  45. if (mgf1Hash == NULL)
  46. mgf1Hash = Hash;
  47. hLen = EVP_MD_get_size(Hash);
  48. if (hLen < 0)
  49. goto err;
  50. /*-
  51. * Negative sLen has special meanings:
  52. * -1 sLen == hLen
  53. * -2 salt length is autorecovered from signature
  54. * -3 salt length is maximized
  55. * -4 salt length is autorecovered from signature
  56. * -N reserved
  57. */
  58. if (sLen == RSA_PSS_SALTLEN_DIGEST) {
  59. sLen = hLen;
  60. } else if (sLen < RSA_PSS_SALTLEN_AUTO_DIGEST_MAX) {
  61. ERR_raise(ERR_LIB_RSA, RSA_R_SLEN_CHECK_FAILED);
  62. goto err;
  63. }
  64. MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
  65. emLen = RSA_size(rsa);
  66. if (EM[0] & (0xFF << MSBits)) {
  67. ERR_raise(ERR_LIB_RSA, RSA_R_FIRST_OCTET_INVALID);
  68. goto err;
  69. }
  70. if (MSBits == 0) {
  71. EM++;
  72. emLen--;
  73. }
  74. if (emLen < hLen + 2) {
  75. ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE);
  76. goto err;
  77. }
  78. if (sLen == RSA_PSS_SALTLEN_MAX) {
  79. sLen = emLen - hLen - 2;
  80. } else if (sLen > emLen - hLen - 2) { /* sLen can be small negative */
  81. ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE);
  82. goto err;
  83. }
  84. if (EM[emLen - 1] != 0xbc) {
  85. ERR_raise(ERR_LIB_RSA, RSA_R_LAST_OCTET_INVALID);
  86. goto err;
  87. }
  88. maskedDBLen = emLen - hLen - 1;
  89. H = EM + maskedDBLen;
  90. DB = OPENSSL_malloc(maskedDBLen);
  91. if (DB == NULL)
  92. goto err;
  93. if (PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash) < 0)
  94. goto err;
  95. for (i = 0; i < maskedDBLen; i++)
  96. DB[i] ^= EM[i];
  97. if (MSBits)
  98. DB[0] &= 0xFF >> (8 - MSBits);
  99. for (i = 0; DB[i] == 0 && i < (maskedDBLen - 1); i++) ;
  100. if (DB[i++] != 0x1) {
  101. ERR_raise(ERR_LIB_RSA, RSA_R_SLEN_RECOVERY_FAILED);
  102. goto err;
  103. }
  104. if (sLen != RSA_PSS_SALTLEN_AUTO
  105. && sLen != RSA_PSS_SALTLEN_AUTO_DIGEST_MAX
  106. && (maskedDBLen - i) != sLen) {
  107. ERR_raise_data(ERR_LIB_RSA, RSA_R_SLEN_CHECK_FAILED,
  108. "expected: %d retrieved: %d", sLen,
  109. maskedDBLen - i);
  110. goto err;
  111. }
  112. if (!EVP_DigestInit_ex(ctx, Hash, NULL)
  113. || !EVP_DigestUpdate(ctx, zeroes, sizeof(zeroes))
  114. || !EVP_DigestUpdate(ctx, mHash, hLen))
  115. goto err;
  116. if (maskedDBLen - i) {
  117. if (!EVP_DigestUpdate(ctx, DB + i, maskedDBLen - i))
  118. goto err;
  119. }
  120. if (!EVP_DigestFinal_ex(ctx, H_, NULL))
  121. goto err;
  122. if (memcmp(H_, H, hLen)) {
  123. ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
  124. ret = 0;
  125. } else {
  126. ret = 1;
  127. }
  128. err:
  129. OPENSSL_free(DB);
  130. EVP_MD_CTX_free(ctx);
  131. return ret;
  132. }
  133. int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
  134. const unsigned char *mHash,
  135. const EVP_MD *Hash, int sLen)
  136. {
  137. return RSA_padding_add_PKCS1_PSS_mgf1(rsa, EM, mHash, Hash, NULL, sLen);
  138. }
  139. int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM,
  140. const unsigned char *mHash,
  141. const EVP_MD *Hash, const EVP_MD *mgf1Hash,
  142. int sLen)
  143. {
  144. int i;
  145. int ret = 0;
  146. int hLen, maskedDBLen, MSBits, emLen;
  147. unsigned char *H, *salt = NULL, *p;
  148. EVP_MD_CTX *ctx = NULL;
  149. int sLenMax = -1;
  150. if (mgf1Hash == NULL)
  151. mgf1Hash = Hash;
  152. hLen = EVP_MD_get_size(Hash);
  153. if (hLen < 0)
  154. goto err;
  155. /*-
  156. * Negative sLen has special meanings:
  157. * -1 sLen == hLen
  158. * -2 salt length is maximized
  159. * -3 same as above (on signing)
  160. * -4 salt length is min(hLen, maximum salt length)
  161. * -N reserved
  162. */
  163. /* FIPS 186-4 section 5 "The RSA Digital Signature Algorithm", subsection
  164. * 5.5 "PKCS #1" says: "For RSASSA-PSS […] the length (in bytes) of the
  165. * salt (sLen) shall satisfy 0 <= sLen <= hLen, where hLen is the length of
  166. * the hash function output block (in bytes)."
  167. *
  168. * Provide a way to use at most the digest length, so that the default does
  169. * not violate FIPS 186-4. */
  170. if (sLen == RSA_PSS_SALTLEN_DIGEST) {
  171. sLen = hLen;
  172. } else if (sLen == RSA_PSS_SALTLEN_MAX_SIGN
  173. || sLen == RSA_PSS_SALTLEN_AUTO) {
  174. sLen = RSA_PSS_SALTLEN_MAX;
  175. } else if (sLen == RSA_PSS_SALTLEN_AUTO_DIGEST_MAX) {
  176. sLen = RSA_PSS_SALTLEN_MAX;
  177. sLenMax = hLen;
  178. } else if (sLen < RSA_PSS_SALTLEN_AUTO_DIGEST_MAX) {
  179. ERR_raise(ERR_LIB_RSA, RSA_R_SLEN_CHECK_FAILED);
  180. goto err;
  181. }
  182. MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
  183. emLen = RSA_size(rsa);
  184. if (MSBits == 0) {
  185. *EM++ = 0;
  186. emLen--;
  187. }
  188. if (emLen < hLen + 2) {
  189. ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  190. goto err;
  191. }
  192. if (sLen == RSA_PSS_SALTLEN_MAX) {
  193. sLen = emLen - hLen - 2;
  194. if (sLenMax >= 0 && sLen > sLenMax)
  195. sLen = sLenMax;
  196. } else if (sLen > emLen - hLen - 2) {
  197. ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  198. goto err;
  199. }
  200. if (sLen > 0) {
  201. salt = OPENSSL_malloc(sLen);
  202. if (salt == NULL)
  203. goto err;
  204. if (RAND_bytes_ex(rsa->libctx, salt, sLen, 0) <= 0)
  205. goto err;
  206. }
  207. maskedDBLen = emLen - hLen - 1;
  208. H = EM + maskedDBLen;
  209. ctx = EVP_MD_CTX_new();
  210. if (ctx == NULL)
  211. goto err;
  212. if (!EVP_DigestInit_ex(ctx, Hash, NULL)
  213. || !EVP_DigestUpdate(ctx, zeroes, sizeof(zeroes))
  214. || !EVP_DigestUpdate(ctx, mHash, hLen))
  215. goto err;
  216. if (sLen && !EVP_DigestUpdate(ctx, salt, sLen))
  217. goto err;
  218. if (!EVP_DigestFinal_ex(ctx, H, NULL))
  219. goto err;
  220. /* Generate dbMask in place then perform XOR on it */
  221. if (PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash))
  222. goto err;
  223. p = EM;
  224. /*
  225. * Initial PS XORs with all zeroes which is a NOP so just update pointer.
  226. * Note from a test above this value is guaranteed to be non-negative.
  227. */
  228. p += emLen - sLen - hLen - 2;
  229. *p++ ^= 0x1;
  230. if (sLen > 0) {
  231. for (i = 0; i < sLen; i++)
  232. *p++ ^= salt[i];
  233. }
  234. if (MSBits)
  235. EM[0] &= 0xFF >> (8 - MSBits);
  236. /* H is already in place so just set final 0xbc */
  237. EM[emLen - 1] = 0xbc;
  238. ret = 1;
  239. err:
  240. EVP_MD_CTX_free(ctx);
  241. OPENSSL_clear_free(salt, (size_t)sLen); /* salt != NULL implies sLen > 0 */
  242. return ret;
  243. }
  244. /*
  245. * The defaults for PSS restrictions are defined in RFC 8017, A.2.3 RSASSA-PSS
  246. * (https://tools.ietf.org/html/rfc8017#appendix-A.2.3):
  247. *
  248. * If the default values of the hashAlgorithm, maskGenAlgorithm, and
  249. * trailerField fields of RSASSA-PSS-params are used, then the algorithm
  250. * identifier will have the following value:
  251. *
  252. * rSASSA-PSS-Default-Identifier RSASSA-AlgorithmIdentifier ::= {
  253. * algorithm id-RSASSA-PSS,
  254. * parameters RSASSA-PSS-params : {
  255. * hashAlgorithm sha1,
  256. * maskGenAlgorithm mgf1SHA1,
  257. * saltLength 20,
  258. * trailerField trailerFieldBC
  259. * }
  260. * }
  261. *
  262. * RSASSA-AlgorithmIdentifier ::= AlgorithmIdentifier {
  263. * {PKCS1Algorithms}
  264. * }
  265. */
  266. static const RSA_PSS_PARAMS_30 default_RSASSA_PSS_params = {
  267. NID_sha1, /* default hashAlgorithm */
  268. {
  269. NID_mgf1, /* default maskGenAlgorithm */
  270. NID_sha1 /* default MGF1 hash */
  271. },
  272. 20, /* default saltLength */
  273. 1 /* default trailerField (0xBC) */
  274. };
  275. int ossl_rsa_pss_params_30_set_defaults(RSA_PSS_PARAMS_30 *rsa_pss_params)
  276. {
  277. if (rsa_pss_params == NULL)
  278. return 0;
  279. *rsa_pss_params = default_RSASSA_PSS_params;
  280. return 1;
  281. }
  282. int ossl_rsa_pss_params_30_is_unrestricted(const RSA_PSS_PARAMS_30 *rsa_pss_params)
  283. {
  284. static RSA_PSS_PARAMS_30 pss_params_cmp = { 0, };
  285. return rsa_pss_params == NULL
  286. || memcmp(rsa_pss_params, &pss_params_cmp,
  287. sizeof(*rsa_pss_params)) == 0;
  288. }
  289. int ossl_rsa_pss_params_30_copy(RSA_PSS_PARAMS_30 *to,
  290. const RSA_PSS_PARAMS_30 *from)
  291. {
  292. memcpy(to, from, sizeof(*to));
  293. return 1;
  294. }
  295. int ossl_rsa_pss_params_30_set_hashalg(RSA_PSS_PARAMS_30 *rsa_pss_params,
  296. int hashalg_nid)
  297. {
  298. if (rsa_pss_params == NULL)
  299. return 0;
  300. rsa_pss_params->hash_algorithm_nid = hashalg_nid;
  301. return 1;
  302. }
  303. int ossl_rsa_pss_params_30_set_maskgenhashalg(RSA_PSS_PARAMS_30 *rsa_pss_params,
  304. int maskgenhashalg_nid)
  305. {
  306. if (rsa_pss_params == NULL)
  307. return 0;
  308. rsa_pss_params->mask_gen.hash_algorithm_nid = maskgenhashalg_nid;
  309. return 1;
  310. }
  311. int ossl_rsa_pss_params_30_set_saltlen(RSA_PSS_PARAMS_30 *rsa_pss_params,
  312. int saltlen)
  313. {
  314. if (rsa_pss_params == NULL)
  315. return 0;
  316. rsa_pss_params->salt_len = saltlen;
  317. return 1;
  318. }
  319. int ossl_rsa_pss_params_30_set_trailerfield(RSA_PSS_PARAMS_30 *rsa_pss_params,
  320. int trailerfield)
  321. {
  322. if (rsa_pss_params == NULL)
  323. return 0;
  324. rsa_pss_params->trailer_field = trailerfield;
  325. return 1;
  326. }
  327. int ossl_rsa_pss_params_30_hashalg(const RSA_PSS_PARAMS_30 *rsa_pss_params)
  328. {
  329. if (rsa_pss_params == NULL)
  330. return default_RSASSA_PSS_params.hash_algorithm_nid;
  331. return rsa_pss_params->hash_algorithm_nid;
  332. }
  333. int ossl_rsa_pss_params_30_maskgenalg(const RSA_PSS_PARAMS_30 *rsa_pss_params)
  334. {
  335. if (rsa_pss_params == NULL)
  336. return default_RSASSA_PSS_params.mask_gen.algorithm_nid;
  337. return rsa_pss_params->mask_gen.algorithm_nid;
  338. }
  339. int ossl_rsa_pss_params_30_maskgenhashalg(const RSA_PSS_PARAMS_30 *rsa_pss_params)
  340. {
  341. if (rsa_pss_params == NULL)
  342. return default_RSASSA_PSS_params.hash_algorithm_nid;
  343. return rsa_pss_params->mask_gen.hash_algorithm_nid;
  344. }
  345. int ossl_rsa_pss_params_30_saltlen(const RSA_PSS_PARAMS_30 *rsa_pss_params)
  346. {
  347. if (rsa_pss_params == NULL)
  348. return default_RSASSA_PSS_params.salt_len;
  349. return rsa_pss_params->salt_len;
  350. }
  351. int ossl_rsa_pss_params_30_trailerfield(const RSA_PSS_PARAMS_30 *rsa_pss_params)
  352. {
  353. if (rsa_pss_params == NULL)
  354. return default_RSASSA_PSS_params.trailer_field;
  355. return rsa_pss_params->trailer_field;
  356. }
  357. #if defined(_MSC_VER)
  358. # pragma optimize("",on)
  359. #endif