rsa_pss.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* rsa_pss.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project 2005.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2005 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #define OPENSSL_FIPSAPI
  59. #include <stdio.h>
  60. #include "cryptlib.h"
  61. #include <openssl/bn.h>
  62. #include <openssl/rsa.h>
  63. #include <openssl/evp.h>
  64. #include <openssl/rand.h>
  65. #include <openssl/sha.h>
  66. #include "rsa_locl.h"
  67. #ifdef OPENSSL_FIPS
  68. #include <openssl/fips.h>
  69. #endif
  70. __fips_constseg
  71. static const unsigned char zeroes[] = {0,0,0,0,0,0,0,0};
  72. #if defined(_MSC_VER) && defined(_ARM_)
  73. #pragma optimize("g", off)
  74. #endif
  75. int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
  76. const EVP_MD *Hash, const unsigned char *EM, int sLen)
  77. {
  78. return RSA_verify_PKCS1_PSS_mgf1(rsa, mHash, Hash, NULL, EM, sLen);
  79. }
  80. int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash,
  81. const EVP_MD *Hash, const EVP_MD *mgf1Hash,
  82. const unsigned char *EM, int sLen)
  83. {
  84. int i;
  85. int ret = 0;
  86. int hLen, maskedDBLen, MSBits, emLen;
  87. const unsigned char *H;
  88. unsigned char *DB = NULL;
  89. EVP_MD_CTX ctx;
  90. unsigned char H_[EVP_MAX_MD_SIZE];
  91. EVP_MD_CTX_init(&ctx);
  92. if (mgf1Hash == NULL)
  93. mgf1Hash = Hash;
  94. hLen = M_EVP_MD_size(Hash);
  95. if (hLen < 0)
  96. goto err;
  97. /*
  98. * Negative sLen has special meanings:
  99. * -1 sLen == hLen
  100. * -2 salt length is autorecovered from signature
  101. * -N reserved
  102. */
  103. if (sLen == -1) sLen = hLen;
  104. else if (sLen == -2) sLen = -2;
  105. else if (sLen < -2)
  106. {
  107. RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_SLEN_CHECK_FAILED);
  108. goto err;
  109. }
  110. MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
  111. emLen = RSA_size(rsa);
  112. if (EM[0] & (0xFF << MSBits))
  113. {
  114. RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_FIRST_OCTET_INVALID);
  115. goto err;
  116. }
  117. if (MSBits == 0)
  118. {
  119. EM++;
  120. emLen--;
  121. }
  122. if (emLen < (hLen + sLen + 2)) /* sLen can be small negative */
  123. {
  124. RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_DATA_TOO_LARGE);
  125. goto err;
  126. }
  127. if (EM[emLen - 1] != 0xbc)
  128. {
  129. RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_LAST_OCTET_INVALID);
  130. goto err;
  131. }
  132. maskedDBLen = emLen - hLen - 1;
  133. H = EM + maskedDBLen;
  134. DB = OPENSSL_malloc(maskedDBLen);
  135. if (!DB)
  136. {
  137. RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, ERR_R_MALLOC_FAILURE);
  138. goto err;
  139. }
  140. if (PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash) < 0)
  141. goto err;
  142. for (i = 0; i < maskedDBLen; i++)
  143. DB[i] ^= EM[i];
  144. if (MSBits)
  145. DB[0] &= 0xFF >> (8 - MSBits);
  146. for (i = 0; DB[i] == 0 && i < (maskedDBLen-1); i++) ;
  147. if (DB[i++] != 0x1)
  148. {
  149. RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_SLEN_RECOVERY_FAILED);
  150. goto err;
  151. }
  152. if (sLen >= 0 && (maskedDBLen - i) != sLen)
  153. {
  154. RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_SLEN_CHECK_FAILED);
  155. goto err;
  156. }
  157. if (!EVP_DigestInit_ex(&ctx, Hash, NULL)
  158. || !EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes)
  159. || !EVP_DigestUpdate(&ctx, mHash, hLen))
  160. goto err;
  161. if (maskedDBLen - i)
  162. {
  163. if (!EVP_DigestUpdate(&ctx, DB + i, maskedDBLen - i))
  164. goto err;
  165. }
  166. if (!EVP_DigestFinal_ex(&ctx, H_, NULL))
  167. goto err;
  168. if (memcmp(H_, H, hLen))
  169. {
  170. RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_BAD_SIGNATURE);
  171. ret = 0;
  172. }
  173. else
  174. ret = 1;
  175. err:
  176. if (DB)
  177. OPENSSL_free(DB);
  178. EVP_MD_CTX_cleanup(&ctx);
  179. return ret;
  180. }
  181. int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
  182. const unsigned char *mHash,
  183. const EVP_MD *Hash, int sLen)
  184. {
  185. return RSA_padding_add_PKCS1_PSS_mgf1(rsa, EM, mHash, Hash, NULL, sLen);
  186. }
  187. int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM,
  188. const unsigned char *mHash,
  189. const EVP_MD *Hash, const EVP_MD *mgf1Hash, int sLen)
  190. {
  191. int i;
  192. int ret = 0;
  193. int hLen, maskedDBLen, MSBits, emLen;
  194. unsigned char *H, *salt = NULL, *p;
  195. EVP_MD_CTX ctx;
  196. if (mgf1Hash == NULL)
  197. mgf1Hash = Hash;
  198. hLen = M_EVP_MD_size(Hash);
  199. if (hLen < 0)
  200. goto err;
  201. /*
  202. * Negative sLen has special meanings:
  203. * -1 sLen == hLen
  204. * -2 salt length is maximized
  205. * -N reserved
  206. */
  207. if (sLen == -1) sLen = hLen;
  208. else if (sLen == -2) sLen = -2;
  209. else if (sLen < -2)
  210. {
  211. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1, RSA_R_SLEN_CHECK_FAILED);
  212. goto err;
  213. }
  214. MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
  215. emLen = RSA_size(rsa);
  216. if (MSBits == 0)
  217. {
  218. *EM++ = 0;
  219. emLen--;
  220. }
  221. if (sLen == -2)
  222. {
  223. sLen = emLen - hLen - 2;
  224. }
  225. else if (emLen < (hLen + sLen + 2))
  226. {
  227. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  228. goto err;
  229. }
  230. if (sLen > 0)
  231. {
  232. salt = OPENSSL_malloc(sLen);
  233. if (!salt)
  234. {
  235. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1,ERR_R_MALLOC_FAILURE);
  236. goto err;
  237. }
  238. if (RAND_bytes(salt, sLen) <= 0)
  239. goto err;
  240. }
  241. maskedDBLen = emLen - hLen - 1;
  242. H = EM + maskedDBLen;
  243. EVP_MD_CTX_init(&ctx);
  244. if (!EVP_DigestInit_ex(&ctx, Hash, NULL)
  245. || !EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes)
  246. || !EVP_DigestUpdate(&ctx, mHash, hLen))
  247. goto err;
  248. if (sLen && !EVP_DigestUpdate(&ctx, salt, sLen))
  249. goto err;
  250. if (!EVP_DigestFinal_ex(&ctx, H, NULL))
  251. goto err;
  252. EVP_MD_CTX_cleanup(&ctx);
  253. /* Generate dbMask in place then perform XOR on it */
  254. if (PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash))
  255. goto err;
  256. p = EM;
  257. /* Initial PS XORs with all zeroes which is a NOP so just update
  258. * pointer. Note from a test above this value is guaranteed to
  259. * be non-negative.
  260. */
  261. p += emLen - sLen - hLen - 2;
  262. *p++ ^= 0x1;
  263. if (sLen > 0)
  264. {
  265. for (i = 0; i < sLen; i++)
  266. *p++ ^= salt[i];
  267. }
  268. if (MSBits)
  269. EM[0] &= 0xFF >> (8 - MSBits);
  270. /* H is already in place so just set final 0xbc */
  271. EM[emLen - 1] = 0xbc;
  272. ret = 1;
  273. err:
  274. if (salt)
  275. OPENSSL_free(salt);
  276. return ret;
  277. }
  278. #if defined(_MSC_VER)
  279. #pragma optimize("",on)
  280. #endif