rsa_pss.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. static const unsigned char zeroes[] = {0,0,0,0,0,0,0,0};
  71. #if defined(_MSC_VER) && defined(_ARM_)
  72. #pragma optimize("g", off)
  73. #endif
  74. int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
  75. const EVP_MD *Hash, const unsigned char *EM, int sLen)
  76. {
  77. return RSA_verify_PKCS1_PSS_mgf1(rsa, mHash, Hash, NULL, EM, sLen);
  78. }
  79. int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash,
  80. const EVP_MD *Hash, const EVP_MD *mgf1Hash,
  81. const unsigned char *EM, int sLen)
  82. {
  83. int i;
  84. int ret = 0;
  85. int hLen, maskedDBLen, MSBits, emLen;
  86. const unsigned char *H;
  87. unsigned char *DB = NULL;
  88. EVP_MD_CTX ctx;
  89. unsigned char H_[EVP_MAX_MD_SIZE];
  90. EVP_MD_CTX_init(&ctx);
  91. if (mgf1Hash == NULL)
  92. mgf1Hash = Hash;
  93. hLen = M_EVP_MD_size(Hash);
  94. if (hLen < 0)
  95. goto err;
  96. /*
  97. * Negative sLen has special meanings:
  98. * -1 sLen == hLen
  99. * -2 salt length is autorecovered from signature
  100. * -N reserved
  101. */
  102. if (sLen == -1) sLen = hLen;
  103. else if (sLen == -2) sLen = -2;
  104. else if (sLen < -2)
  105. {
  106. RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_SLEN_CHECK_FAILED);
  107. goto err;
  108. }
  109. MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
  110. emLen = RSA_size(rsa);
  111. if (EM[0] & (0xFF << MSBits))
  112. {
  113. RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_FIRST_OCTET_INVALID);
  114. goto err;
  115. }
  116. if (MSBits == 0)
  117. {
  118. EM++;
  119. emLen--;
  120. }
  121. if (emLen < (hLen + sLen + 2)) /* sLen can be small negative */
  122. {
  123. RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_DATA_TOO_LARGE);
  124. goto err;
  125. }
  126. if (EM[emLen - 1] != 0xbc)
  127. {
  128. RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_LAST_OCTET_INVALID);
  129. goto err;
  130. }
  131. maskedDBLen = emLen - hLen - 1;
  132. H = EM + maskedDBLen;
  133. DB = OPENSSL_malloc(maskedDBLen);
  134. if (!DB)
  135. {
  136. RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, ERR_R_MALLOC_FAILURE);
  137. goto err;
  138. }
  139. if (PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash) < 0)
  140. goto err;
  141. for (i = 0; i < maskedDBLen; i++)
  142. DB[i] ^= EM[i];
  143. if (MSBits)
  144. DB[0] &= 0xFF >> (8 - MSBits);
  145. for (i = 0; DB[i] == 0 && i < (maskedDBLen-1); i++) ;
  146. if (DB[i++] != 0x1)
  147. {
  148. RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_SLEN_RECOVERY_FAILED);
  149. goto err;
  150. }
  151. if (sLen >= 0 && (maskedDBLen - i) != sLen)
  152. {
  153. RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_SLEN_CHECK_FAILED);
  154. goto err;
  155. }
  156. if (!EVP_DigestInit_ex(&ctx, Hash, NULL)
  157. || !EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes)
  158. || !EVP_DigestUpdate(&ctx, mHash, hLen))
  159. goto err;
  160. if (maskedDBLen - i)
  161. {
  162. if (!EVP_DigestUpdate(&ctx, DB + i, maskedDBLen - i))
  163. goto err;
  164. }
  165. if (!EVP_DigestFinal_ex(&ctx, H_, NULL))
  166. goto err;
  167. if (memcmp(H_, H, hLen))
  168. {
  169. RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_BAD_SIGNATURE);
  170. ret = 0;
  171. }
  172. else
  173. ret = 1;
  174. err:
  175. if (DB)
  176. OPENSSL_free(DB);
  177. EVP_MD_CTX_cleanup(&ctx);
  178. return ret;
  179. }
  180. int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
  181. const unsigned char *mHash,
  182. const EVP_MD *Hash, int sLen)
  183. {
  184. return RSA_padding_add_PKCS1_PSS_mgf1(rsa, EM, mHash, Hash, NULL, sLen);
  185. }
  186. int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM,
  187. const unsigned char *mHash,
  188. const EVP_MD *Hash, const EVP_MD *mgf1Hash, int sLen)
  189. {
  190. int i;
  191. int ret = 0;
  192. int hLen, maskedDBLen, MSBits, emLen;
  193. unsigned char *H, *salt = NULL, *p;
  194. EVP_MD_CTX ctx;
  195. if (mgf1Hash == NULL)
  196. mgf1Hash = Hash;
  197. hLen = M_EVP_MD_size(Hash);
  198. if (hLen < 0)
  199. goto err;
  200. /*
  201. * Negative sLen has special meanings:
  202. * -1 sLen == hLen
  203. * -2 salt length is maximized
  204. * -N reserved
  205. */
  206. if (sLen == -1) sLen = hLen;
  207. else if (sLen == -2) sLen = -2;
  208. else if (sLen < -2)
  209. {
  210. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1, RSA_R_SLEN_CHECK_FAILED);
  211. goto err;
  212. }
  213. MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
  214. emLen = RSA_size(rsa);
  215. if (MSBits == 0)
  216. {
  217. *EM++ = 0;
  218. emLen--;
  219. }
  220. if (sLen == -2)
  221. {
  222. sLen = emLen - hLen - 2;
  223. }
  224. else if (emLen < (hLen + sLen + 2))
  225. {
  226. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  227. goto err;
  228. }
  229. if (sLen > 0)
  230. {
  231. salt = OPENSSL_malloc(sLen);
  232. if (!salt)
  233. {
  234. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1,ERR_R_MALLOC_FAILURE);
  235. goto err;
  236. }
  237. if (RAND_bytes(salt, sLen) <= 0)
  238. goto err;
  239. }
  240. maskedDBLen = emLen - hLen - 1;
  241. H = EM + maskedDBLen;
  242. EVP_MD_CTX_init(&ctx);
  243. if (!EVP_DigestInit_ex(&ctx, Hash, NULL)
  244. || !EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes)
  245. || !EVP_DigestUpdate(&ctx, mHash, hLen))
  246. goto err;
  247. if (sLen && !EVP_DigestUpdate(&ctx, salt, sLen))
  248. goto err;
  249. if (!EVP_DigestFinal_ex(&ctx, H, NULL))
  250. goto err;
  251. EVP_MD_CTX_cleanup(&ctx);
  252. /* Generate dbMask in place then perform XOR on it */
  253. if (PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash))
  254. goto err;
  255. p = EM;
  256. /* Initial PS XORs with all zeroes which is a NOP so just update
  257. * pointer. Note from a test above this value is guaranteed to
  258. * be non-negative.
  259. */
  260. p += emLen - sLen - hLen - 2;
  261. *p++ ^= 0x1;
  262. if (sLen > 0)
  263. {
  264. for (i = 0; i < sLen; i++)
  265. *p++ ^= salt[i];
  266. }
  267. if (MSBits)
  268. EM[0] &= 0xFF >> (8 - MSBits);
  269. /* H is already in place so just set final 0xbc */
  270. EM[emLen - 1] = 0xbc;
  271. ret = 1;
  272. err:
  273. if (salt)
  274. OPENSSL_free(salt);
  275. return ret;
  276. }
  277. #if defined(_MSC_VER)
  278. #pragma optimize("",on)
  279. #endif