rsa_pss.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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_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. * -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. goto err;
  92. if (PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash) < 0)
  93. goto err;
  94. for (i = 0; i < maskedDBLen; i++)
  95. DB[i] ^= EM[i];
  96. if (MSBits)
  97. DB[0] &= 0xFF >> (8 - MSBits);
  98. for (i = 0; DB[i] == 0 && i < (maskedDBLen - 1); i++) ;
  99. if (DB[i++] != 0x1) {
  100. ERR_raise(ERR_LIB_RSA, RSA_R_SLEN_RECOVERY_FAILED);
  101. goto err;
  102. }
  103. if (sLen != RSA_PSS_SALTLEN_AUTO && (maskedDBLen - i) != sLen) {
  104. ERR_raise_data(ERR_LIB_RSA, RSA_R_SLEN_CHECK_FAILED,
  105. "expected: %d retrieved: %d", sLen,
  106. maskedDBLen - i);
  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. ERR_raise(ERR_LIB_RSA, 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_get_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. ERR_raise(ERR_LIB_RSA, 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. ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  174. goto err;
  175. }
  176. if (sLen == RSA_PSS_SALTLEN_MAX) {
  177. sLen = emLen - hLen - 2;
  178. } else if (sLen > emLen - hLen - 2) {
  179. ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  180. goto err;
  181. }
  182. if (sLen > 0) {
  183. salt = OPENSSL_malloc(sLen);
  184. if (salt == NULL)
  185. goto err;
  186. if (RAND_bytes_ex(rsa->libctx, salt, sLen, 0) <= 0)
  187. goto err;
  188. }
  189. maskedDBLen = emLen - hLen - 1;
  190. H = EM + maskedDBLen;
  191. ctx = EVP_MD_CTX_new();
  192. if (ctx == NULL)
  193. goto err;
  194. if (!EVP_DigestInit_ex(ctx, Hash, NULL)
  195. || !EVP_DigestUpdate(ctx, zeroes, sizeof(zeroes))
  196. || !EVP_DigestUpdate(ctx, mHash, hLen))
  197. goto err;
  198. if (sLen && !EVP_DigestUpdate(ctx, salt, sLen))
  199. goto err;
  200. if (!EVP_DigestFinal_ex(ctx, H, NULL))
  201. goto err;
  202. /* Generate dbMask in place then perform XOR on it */
  203. if (PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash))
  204. goto err;
  205. p = EM;
  206. /*
  207. * Initial PS XORs with all zeroes which is a NOP so just update pointer.
  208. * Note from a test above this value is guaranteed to be non-negative.
  209. */
  210. p += emLen - sLen - hLen - 2;
  211. *p++ ^= 0x1;
  212. if (sLen > 0) {
  213. for (i = 0; i < sLen; i++)
  214. *p++ ^= salt[i];
  215. }
  216. if (MSBits)
  217. EM[0] &= 0xFF >> (8 - MSBits);
  218. /* H is already in place so just set final 0xbc */
  219. EM[emLen - 1] = 0xbc;
  220. ret = 1;
  221. err:
  222. EVP_MD_CTX_free(ctx);
  223. OPENSSL_clear_free(salt, (size_t)sLen); /* salt != NULL implies sLen > 0 */
  224. return ret;
  225. }
  226. /*
  227. * The defaults for PSS restrictions are defined in RFC 8017, A.2.3 RSASSA-PSS
  228. * (https://tools.ietf.org/html/rfc8017#appendix-A.2.3):
  229. *
  230. * If the default values of the hashAlgorithm, maskGenAlgorithm, and
  231. * trailerField fields of RSASSA-PSS-params are used, then the algorithm
  232. * identifier will have the following value:
  233. *
  234. * rSASSA-PSS-Default-Identifier RSASSA-AlgorithmIdentifier ::= {
  235. * algorithm id-RSASSA-PSS,
  236. * parameters RSASSA-PSS-params : {
  237. * hashAlgorithm sha1,
  238. * maskGenAlgorithm mgf1SHA1,
  239. * saltLength 20,
  240. * trailerField trailerFieldBC
  241. * }
  242. * }
  243. *
  244. * RSASSA-AlgorithmIdentifier ::= AlgorithmIdentifier {
  245. * {PKCS1Algorithms}
  246. * }
  247. */
  248. static const RSA_PSS_PARAMS_30 default_RSASSA_PSS_params = {
  249. NID_sha1, /* default hashAlgorithm */
  250. {
  251. NID_mgf1, /* default maskGenAlgorithm */
  252. NID_sha1 /* default MGF1 hash */
  253. },
  254. 20, /* default saltLength */
  255. 1 /* default trailerField (0xBC) */
  256. };
  257. int ossl_rsa_pss_params_30_set_defaults(RSA_PSS_PARAMS_30 *rsa_pss_params)
  258. {
  259. if (rsa_pss_params == NULL)
  260. return 0;
  261. *rsa_pss_params = default_RSASSA_PSS_params;
  262. return 1;
  263. }
  264. int ossl_rsa_pss_params_30_is_unrestricted(const RSA_PSS_PARAMS_30 *rsa_pss_params)
  265. {
  266. static RSA_PSS_PARAMS_30 pss_params_cmp = { 0, };
  267. return rsa_pss_params == NULL
  268. || memcmp(rsa_pss_params, &pss_params_cmp,
  269. sizeof(*rsa_pss_params)) == 0;
  270. }
  271. int ossl_rsa_pss_params_30_copy(RSA_PSS_PARAMS_30 *to,
  272. const RSA_PSS_PARAMS_30 *from)
  273. {
  274. memcpy(to, from, sizeof(*to));
  275. return 1;
  276. }
  277. int ossl_rsa_pss_params_30_set_hashalg(RSA_PSS_PARAMS_30 *rsa_pss_params,
  278. int hashalg_nid)
  279. {
  280. if (rsa_pss_params == NULL)
  281. return 0;
  282. rsa_pss_params->hash_algorithm_nid = hashalg_nid;
  283. return 1;
  284. }
  285. int ossl_rsa_pss_params_30_set_maskgenalg(RSA_PSS_PARAMS_30 *rsa_pss_params,
  286. int maskgenalg_nid)
  287. {
  288. if (rsa_pss_params == NULL)
  289. return 0;
  290. rsa_pss_params->mask_gen.algorithm_nid = maskgenalg_nid;
  291. return 1;
  292. }
  293. int ossl_rsa_pss_params_30_set_maskgenhashalg(RSA_PSS_PARAMS_30 *rsa_pss_params,
  294. int maskgenhashalg_nid)
  295. {
  296. if (rsa_pss_params == NULL)
  297. return 0;
  298. rsa_pss_params->mask_gen.hash_algorithm_nid = maskgenhashalg_nid;
  299. return 1;
  300. }
  301. int ossl_rsa_pss_params_30_set_saltlen(RSA_PSS_PARAMS_30 *rsa_pss_params,
  302. int saltlen)
  303. {
  304. if (rsa_pss_params == NULL)
  305. return 0;
  306. rsa_pss_params->salt_len = saltlen;
  307. return 1;
  308. }
  309. int ossl_rsa_pss_params_30_set_trailerfield(RSA_PSS_PARAMS_30 *rsa_pss_params,
  310. int trailerfield)
  311. {
  312. if (rsa_pss_params == NULL)
  313. return 0;
  314. rsa_pss_params->trailer_field = trailerfield;
  315. return 1;
  316. }
  317. int ossl_rsa_pss_params_30_hashalg(const RSA_PSS_PARAMS_30 *rsa_pss_params)
  318. {
  319. if (rsa_pss_params == NULL)
  320. return default_RSASSA_PSS_params.hash_algorithm_nid;
  321. return rsa_pss_params->hash_algorithm_nid;
  322. }
  323. int ossl_rsa_pss_params_30_maskgenalg(const RSA_PSS_PARAMS_30 *rsa_pss_params)
  324. {
  325. if (rsa_pss_params == NULL)
  326. return default_RSASSA_PSS_params.mask_gen.algorithm_nid;
  327. return rsa_pss_params->mask_gen.algorithm_nid;
  328. }
  329. int ossl_rsa_pss_params_30_maskgenhashalg(const RSA_PSS_PARAMS_30 *rsa_pss_params)
  330. {
  331. if (rsa_pss_params == NULL)
  332. return default_RSASSA_PSS_params.hash_algorithm_nid;
  333. return rsa_pss_params->mask_gen.hash_algorithm_nid;
  334. }
  335. int ossl_rsa_pss_params_30_saltlen(const RSA_PSS_PARAMS_30 *rsa_pss_params)
  336. {
  337. if (rsa_pss_params == NULL)
  338. return default_RSASSA_PSS_params.salt_len;
  339. return rsa_pss_params->salt_len;
  340. }
  341. int ossl_rsa_pss_params_30_trailerfield(const RSA_PSS_PARAMS_30 *rsa_pss_params)
  342. {
  343. if (rsa_pss_params == NULL)
  344. return default_RSASSA_PSS_params.trailer_field;
  345. return rsa_pss_params->trailer_field;
  346. }
  347. #if defined(_MSC_VER)
  348. # pragma optimize("",on)
  349. #endif