rsa_pss.c 11 KB

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