rsa_pss.c 9.1 KB

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