fips_rsa_sign.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /* fips_rsa_sign.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project 2007.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2007 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 <string.h>
  60. #include <openssl/evp.h>
  61. #include <openssl/rsa.h>
  62. #include <openssl/err.h>
  63. #include <openssl/sha.h>
  64. #include <openssl/fips.h>
  65. #ifdef OPENSSL_FIPS
  66. /* FIPS versions of RSA_sign() and RSA_verify().
  67. * These will only have to deal with SHA* signatures and by including
  68. * pregenerated encodings all ASN1 dependencies can be avoided
  69. */
  70. /* Standard encodings including NULL parameter */
  71. __fips_constseg
  72. static const unsigned char sha1_bin[] = {
  73. 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  74. 0x00, 0x04, 0x14
  75. };
  76. __fips_constseg
  77. static const unsigned char sha224_bin[] = {
  78. 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  79. 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c
  80. };
  81. __fips_constseg
  82. static const unsigned char sha256_bin[] = {
  83. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  84. 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20
  85. };
  86. __fips_constseg
  87. static const unsigned char sha384_bin[] = {
  88. 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  89. 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30
  90. };
  91. __fips_constseg
  92. static const unsigned char sha512_bin[] = {
  93. 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  94. 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40
  95. };
  96. /* Alternate encodings with absent parameters. We don't generate signature
  97. * using this format but do tolerate received signatures of this form.
  98. */
  99. __fips_constseg
  100. static const unsigned char sha1_nn_bin[] = {
  101. 0x30, 0x1f, 0x30, 0x07, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x04,
  102. 0x14
  103. };
  104. __fips_constseg
  105. static const unsigned char sha224_nn_bin[] = {
  106. 0x30, 0x2b, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  107. 0x04, 0x02, 0x04, 0x04, 0x1c
  108. };
  109. __fips_constseg
  110. static const unsigned char sha256_nn_bin[] = {
  111. 0x30, 0x2f, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  112. 0x04, 0x02, 0x01, 0x04, 0x20
  113. };
  114. __fips_constseg
  115. static const unsigned char sha384_nn_bin[] = {
  116. 0x30, 0x3f, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  117. 0x04, 0x02, 0x02, 0x04, 0x30
  118. };
  119. __fips_constseg
  120. static const unsigned char sha512_nn_bin[] = {
  121. 0x30, 0x4f, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  122. 0x04, 0x02, 0x03, 0x04, 0x40
  123. };
  124. static const unsigned char *fips_digestinfo_encoding(int nid, unsigned int *len)
  125. {
  126. switch (nid)
  127. {
  128. case NID_sha1:
  129. *len = sizeof(sha1_bin);
  130. return sha1_bin;
  131. case NID_sha224:
  132. *len = sizeof(sha224_bin);
  133. return sha224_bin;
  134. case NID_sha256:
  135. *len = sizeof(sha256_bin);
  136. return sha256_bin;
  137. case NID_sha384:
  138. *len = sizeof(sha384_bin);
  139. return sha384_bin;
  140. case NID_sha512:
  141. *len = sizeof(sha512_bin);
  142. return sha512_bin;
  143. default:
  144. return NULL;
  145. }
  146. }
  147. static const unsigned char *fips_digestinfo_nn_encoding(int nid, unsigned int *len)
  148. {
  149. switch (nid)
  150. {
  151. case NID_sha1:
  152. *len = sizeof(sha1_nn_bin);
  153. return sha1_nn_bin;
  154. case NID_sha224:
  155. *len = sizeof(sha224_nn_bin);
  156. return sha224_nn_bin;
  157. case NID_sha256:
  158. *len = sizeof(sha256_nn_bin);
  159. return sha256_nn_bin;
  160. case NID_sha384:
  161. *len = sizeof(sha384_nn_bin);
  162. return sha384_nn_bin;
  163. case NID_sha512:
  164. *len = sizeof(sha512_nn_bin);
  165. return sha512_nn_bin;
  166. default:
  167. return NULL;
  168. }
  169. }
  170. int FIPS_rsa_sign_ctx(RSA *rsa, EVP_MD_CTX *ctx,
  171. int rsa_pad_mode, int saltlen, const EVP_MD *mgf1Hash,
  172. unsigned char *sigret, unsigned int *siglen)
  173. {
  174. unsigned int md_len, rv;
  175. unsigned char md[EVP_MAX_MD_SIZE];
  176. FIPS_digestfinal(ctx, md, &md_len);
  177. rv = FIPS_rsa_sign_digest(rsa, md, md_len,
  178. M_EVP_MD_CTX_md(ctx),
  179. rsa_pad_mode, saltlen,
  180. mgf1Hash, sigret, siglen);
  181. OPENSSL_cleanse(md, md_len);
  182. return rv;
  183. }
  184. int FIPS_rsa_sign_digest(RSA *rsa, const unsigned char *md, int md_len,
  185. const EVP_MD *mhash, int rsa_pad_mode, int saltlen,
  186. const EVP_MD *mgf1Hash,
  187. unsigned char *sigret, unsigned int *siglen)
  188. {
  189. int i=0,j,ret=0;
  190. unsigned int dlen;
  191. const unsigned char *der;
  192. int md_type;
  193. /* Largest DigestInfo: 19 (max encoding) + max MD */
  194. unsigned char tmpdinfo[19 + EVP_MAX_MD_SIZE];
  195. if (FIPS_selftest_failed())
  196. {
  197. FIPSerr(FIPS_F_FIPS_RSA_SIGN_DIGEST, FIPS_R_SELFTEST_FAILED);
  198. return 0;
  199. }
  200. if (!mhash && rsa_pad_mode == RSA_PKCS1_PADDING)
  201. md_type = saltlen;
  202. else
  203. md_type = M_EVP_MD_type(mhash);
  204. if (rsa_pad_mode == RSA_X931_PADDING)
  205. {
  206. int hash_id;
  207. memcpy(tmpdinfo, md, md_len);
  208. hash_id = RSA_X931_hash_id(md_type);
  209. if (hash_id == -1)
  210. {
  211. RSAerr(RSA_F_FIPS_RSA_SIGN_DIGEST,RSA_R_UNKNOWN_ALGORITHM_TYPE);
  212. return 0;
  213. }
  214. tmpdinfo[md_len] = (unsigned char)hash_id;
  215. i = md_len + 1;
  216. }
  217. else if (rsa_pad_mode == RSA_PKCS1_PADDING)
  218. {
  219. der = fips_digestinfo_encoding(md_type, &dlen);
  220. if (!der)
  221. {
  222. RSAerr(RSA_F_FIPS_RSA_SIGN_DIGEST,RSA_R_UNKNOWN_ALGORITHM_TYPE);
  223. return 0;
  224. }
  225. memcpy(tmpdinfo, der, dlen);
  226. memcpy(tmpdinfo + dlen, md, md_len);
  227. i = dlen + md_len;
  228. }
  229. else if (rsa_pad_mode == RSA_PKCS1_PSS_PADDING)
  230. {
  231. unsigned char *sbuf;
  232. i = RSA_size(rsa);
  233. sbuf = OPENSSL_malloc(RSA_size(rsa));
  234. if (!sbuf)
  235. {
  236. RSAerr(RSA_F_FIPS_RSA_SIGN_DIGEST,ERR_R_MALLOC_FAILURE);
  237. goto psserr;
  238. }
  239. if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa, sbuf, md, mhash,
  240. mgf1Hash, saltlen))
  241. goto psserr;
  242. j=rsa->meth->rsa_priv_enc(i,sbuf,sigret,rsa,RSA_NO_PADDING);
  243. if (j > 0)
  244. {
  245. ret=1;
  246. *siglen=j;
  247. }
  248. psserr:
  249. OPENSSL_cleanse(sbuf, i);
  250. OPENSSL_free(sbuf);
  251. return ret;
  252. }
  253. j=RSA_size(rsa);
  254. if (i > (j-RSA_PKCS1_PADDING_SIZE))
  255. {
  256. RSAerr(RSA_F_FIPS_RSA_SIGN_DIGEST,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
  257. goto done;
  258. }
  259. /* NB: call underlying method directly to avoid FIPS blocking */
  260. j=rsa->meth->rsa_priv_enc(i,tmpdinfo,sigret,rsa,rsa_pad_mode);
  261. if (j > 0)
  262. {
  263. ret=1;
  264. *siglen=j;
  265. }
  266. done:
  267. OPENSSL_cleanse(tmpdinfo,i);
  268. return ret;
  269. }
  270. int FIPS_rsa_verify_ctx(RSA *rsa, EVP_MD_CTX *ctx,
  271. int rsa_pad_mode, int saltlen, const EVP_MD *mgf1Hash,
  272. const unsigned char *sigbuf, unsigned int siglen)
  273. {
  274. unsigned int md_len, rv;
  275. unsigned char md[EVP_MAX_MD_SIZE];
  276. FIPS_digestfinal(ctx, md, &md_len);
  277. rv = FIPS_rsa_verify_digest(rsa, md, md_len, M_EVP_MD_CTX_md(ctx),
  278. rsa_pad_mode, saltlen, mgf1Hash,
  279. sigbuf, siglen);
  280. OPENSSL_cleanse(md, md_len);
  281. return rv;
  282. }
  283. int FIPS_rsa_verify_digest(RSA *rsa, const unsigned char *dig, int diglen,
  284. const EVP_MD *mhash, int rsa_pad_mode, int saltlen,
  285. const EVP_MD *mgf1Hash,
  286. const unsigned char *sigbuf, unsigned int siglen)
  287. {
  288. int i,ret=0;
  289. unsigned int dlen;
  290. unsigned char *s;
  291. const unsigned char *der;
  292. int md_type;
  293. int rsa_dec_pad_mode;
  294. if (FIPS_selftest_failed())
  295. {
  296. FIPSerr(FIPS_F_FIPS_RSA_VERIFY_DIGEST, FIPS_R_SELFTEST_FAILED);
  297. return 0;
  298. }
  299. if (siglen != (unsigned int)RSA_size(rsa))
  300. {
  301. RSAerr(RSA_F_FIPS_RSA_VERIFY_DIGEST,RSA_R_WRONG_SIGNATURE_LENGTH);
  302. return(0);
  303. }
  304. if (!mhash && rsa_pad_mode == RSA_PKCS1_PADDING)
  305. md_type = saltlen;
  306. else
  307. md_type = M_EVP_MD_type(mhash);
  308. s= OPENSSL_malloc((unsigned int)siglen);
  309. if (s == NULL)
  310. {
  311. RSAerr(RSA_F_FIPS_RSA_VERIFY_DIGEST,ERR_R_MALLOC_FAILURE);
  312. goto err;
  313. }
  314. if (rsa_pad_mode == RSA_PKCS1_PSS_PADDING)
  315. rsa_dec_pad_mode = RSA_NO_PADDING;
  316. else
  317. rsa_dec_pad_mode = rsa_pad_mode;
  318. /* NB: call underlying method directly to avoid FIPS blocking */
  319. i=rsa->meth->rsa_pub_dec((int)siglen,sigbuf,s, rsa, rsa_dec_pad_mode);
  320. if (i <= 0) goto err;
  321. if (rsa_pad_mode == RSA_X931_PADDING)
  322. {
  323. int hash_id;
  324. if (i != (int)(diglen + 1))
  325. {
  326. RSAerr(RSA_F_FIPS_RSA_VERIFY_DIGEST,RSA_R_BAD_SIGNATURE);
  327. goto err;
  328. }
  329. hash_id = RSA_X931_hash_id(md_type);
  330. if (hash_id == -1)
  331. {
  332. RSAerr(RSA_F_FIPS_RSA_VERIFY_DIGEST,RSA_R_UNKNOWN_ALGORITHM_TYPE);
  333. goto err;
  334. }
  335. if (s[diglen] != (unsigned char)hash_id)
  336. {
  337. RSAerr(RSA_F_FIPS_RSA_VERIFY_DIGEST,RSA_R_BAD_SIGNATURE);
  338. goto err;
  339. }
  340. if (memcmp(s, dig, diglen))
  341. {
  342. RSAerr(RSA_F_FIPS_RSA_VERIFY_DIGEST,RSA_R_BAD_SIGNATURE);
  343. goto err;
  344. }
  345. ret = 1;
  346. }
  347. else if (rsa_pad_mode == RSA_PKCS1_PADDING)
  348. {
  349. der = fips_digestinfo_encoding(md_type, &dlen);
  350. if (!der)
  351. {
  352. RSAerr(RSA_F_FIPS_RSA_VERIFY_DIGEST,RSA_R_UNKNOWN_ALGORITHM_TYPE);
  353. return(0);
  354. }
  355. /* Compare, DigestInfo length, DigestInfo header and finally
  356. * digest value itself
  357. */
  358. /* If length mismatch try alternate encoding */
  359. if (i != (int)(dlen + diglen))
  360. der = fips_digestinfo_nn_encoding(md_type, &dlen);
  361. if ((i != (int)(dlen + diglen)) || memcmp(der, s, dlen)
  362. || memcmp(s + dlen, dig, diglen))
  363. {
  364. RSAerr(RSA_F_FIPS_RSA_VERIFY_DIGEST,RSA_R_BAD_SIGNATURE);
  365. goto err;
  366. }
  367. ret = 1;
  368. }
  369. else if (rsa_pad_mode == RSA_PKCS1_PSS_PADDING)
  370. {
  371. ret = RSA_verify_PKCS1_PSS_mgf1(rsa, dig, mhash, mgf1Hash,
  372. s, saltlen);
  373. if (ret < 0)
  374. ret = 0;
  375. }
  376. err:
  377. if (s != NULL)
  378. {
  379. OPENSSL_cleanse(s, siglen);
  380. OPENSSL_free(s);
  381. }
  382. return(ret);
  383. }
  384. #endif