rsa_pss.c 7.1 KB

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