rsa_pss.c 11 KB

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