rsa_pss.c 8.9 KB

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