fips_rsastest.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* fips_rsastest.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 <ctype.h>
  61. #include <string.h>
  62. #include <openssl/bio.h>
  63. #include <openssl/evp.h>
  64. #include <openssl/hmac.h>
  65. #include <openssl/err.h>
  66. #include <openssl/bn.h>
  67. #ifndef OPENSSL_FIPS
  68. int main(int argc, char *argv[])
  69. {
  70. printf("No FIPS RSA support\n");
  71. return(0);
  72. }
  73. #else
  74. #include <openssl/rsa.h>
  75. #include <openssl/fips.h>
  76. #include "fips_utl.h"
  77. static int rsa_stest(FILE *out, FILE *in, int Saltlen);
  78. static int rsa_printsig(FILE *out, RSA *rsa, const EVP_MD *dgst,
  79. unsigned char *Msg, long Msglen, int Saltlen);
  80. #ifdef FIPS_ALGVS
  81. int fips_rsastest_main(int argc, char **argv)
  82. #else
  83. int main(int argc, char **argv)
  84. #endif
  85. {
  86. FILE *in = NULL, *out = NULL;
  87. int ret = 1, Saltlen = -1;
  88. fips_algtest_init();
  89. if ((argc > 2) && !strcmp("-saltlen", argv[1]))
  90. {
  91. Saltlen = atoi(argv[2]);
  92. if (Saltlen < 0)
  93. {
  94. fprintf(stderr, "FATAL: Invalid salt length\n");
  95. goto end;
  96. }
  97. argc -= 2;
  98. argv += 2;
  99. }
  100. else if ((argc > 1) && !strcmp("-x931", argv[1]))
  101. {
  102. Saltlen = -2;
  103. argc--;
  104. argv++;
  105. }
  106. if (argc == 1)
  107. in = stdin;
  108. else
  109. in = fopen(argv[1], "r");
  110. if (argc < 2)
  111. out = stdout;
  112. else
  113. out = fopen(argv[2], "w");
  114. if (!in)
  115. {
  116. fprintf(stderr, "FATAL input initialization error\n");
  117. goto end;
  118. }
  119. if (!out)
  120. {
  121. fprintf(stderr, "FATAL output initialization error\n");
  122. goto end;
  123. }
  124. if (!rsa_stest(out, in, Saltlen))
  125. {
  126. fprintf(stderr, "FATAL RSASTEST file processing error\n");
  127. goto end;
  128. }
  129. else
  130. ret = 0;
  131. end:
  132. if (in && (in != stdin))
  133. fclose(in);
  134. if (out && (out != stdout))
  135. fclose(out);
  136. return ret;
  137. }
  138. #define RSA_TEST_MAXLINELEN 10240
  139. int rsa_stest(FILE *out, FILE *in, int Saltlen)
  140. {
  141. char *linebuf, *olinebuf, *p, *q;
  142. char *keyword, *value;
  143. RSA *rsa = NULL;
  144. const EVP_MD *dgst = NULL;
  145. unsigned char *Msg = NULL;
  146. long Msglen = -1;
  147. int keylen = -1, current_keylen = -1;
  148. int ret = 0;
  149. int lnum = 0;
  150. olinebuf = OPENSSL_malloc(RSA_TEST_MAXLINELEN);
  151. linebuf = OPENSSL_malloc(RSA_TEST_MAXLINELEN);
  152. if (!linebuf || !olinebuf)
  153. goto error;
  154. while (fgets(olinebuf, RSA_TEST_MAXLINELEN, in))
  155. {
  156. lnum++;
  157. strcpy(linebuf, olinebuf);
  158. keyword = linebuf;
  159. /* Skip leading space */
  160. while (isspace((unsigned char)*keyword))
  161. keyword++;
  162. /* Look for = sign */
  163. p = strchr(linebuf, '=');
  164. /* If no = just copy */
  165. if (!p)
  166. {
  167. if (fputs(olinebuf, out) < 0)
  168. goto error;
  169. continue;
  170. }
  171. q = p - 1;
  172. /* Remove trailing space */
  173. while (isspace((unsigned char)*q))
  174. *q-- = 0;
  175. *p = 0;
  176. value = p + 1;
  177. /* Remove leading space from value */
  178. while (isspace((unsigned char)*value))
  179. value++;
  180. /* Remove trailing space from value */
  181. p = value + strlen(value) - 1;
  182. while (*p == '\n' || isspace((unsigned char)*p))
  183. *p-- = 0;
  184. /* Look for [mod = XXX] for key length */
  185. if (!strcmp(keyword, "[mod"))
  186. {
  187. p = value + strlen(value) - 1;
  188. if (*p != ']')
  189. goto parse_error;
  190. *p = 0;
  191. keylen = atoi(value);
  192. if (keylen < 0)
  193. goto parse_error;
  194. }
  195. else if (!strcmp(keyword, "SHAAlg"))
  196. {
  197. if (!strcmp(value, "SHA1"))
  198. dgst = EVP_sha1();
  199. else if (!strcmp(value, "SHA224"))
  200. dgst = EVP_sha224();
  201. else if (!strcmp(value, "SHA256"))
  202. dgst = EVP_sha256();
  203. else if (!strcmp(value, "SHA384"))
  204. dgst = EVP_sha384();
  205. else if (!strcmp(value, "SHA512"))
  206. dgst = EVP_sha512();
  207. else
  208. {
  209. fprintf(stderr,
  210. "FATAL: unsupported algorithm \"%s\"\n",
  211. value);
  212. goto parse_error;
  213. }
  214. }
  215. else if (!strcmp(keyword, "Msg"))
  216. {
  217. if (Msg)
  218. goto parse_error;
  219. if (strlen(value) & 1)
  220. *(--value) = '0';
  221. Msg = hex2bin_m(value, &Msglen);
  222. if (!Msg)
  223. goto parse_error;
  224. }
  225. fputs(olinebuf, out);
  226. /* If key length has changed, generate and output public
  227. * key components of new RSA private key.
  228. */
  229. if (keylen != current_keylen)
  230. {
  231. BIGNUM *bn_e;
  232. if (rsa)
  233. FIPS_rsa_free(rsa);
  234. rsa = FIPS_rsa_new();
  235. if (!rsa)
  236. goto error;
  237. bn_e = BN_new();
  238. if (!bn_e || !BN_set_word(bn_e, 0x1001))
  239. goto error;
  240. if (!RSA_X931_generate_key_ex(rsa, keylen, bn_e, NULL))
  241. goto error;
  242. BN_free(bn_e);
  243. fputs("n = ", out);
  244. do_bn_print(out, rsa->n);
  245. fputs(RESP_EOL "e = ", out);
  246. do_bn_print(out, rsa->e);
  247. fputs(RESP_EOL, out);
  248. current_keylen = keylen;
  249. }
  250. if (Msg && dgst)
  251. {
  252. if (!rsa_printsig(out, rsa, dgst, Msg, Msglen,
  253. Saltlen))
  254. goto error;
  255. OPENSSL_free(Msg);
  256. Msg = NULL;
  257. }
  258. }
  259. ret = 1;
  260. error:
  261. if (olinebuf)
  262. OPENSSL_free(olinebuf);
  263. if (linebuf)
  264. OPENSSL_free(linebuf);
  265. if (rsa)
  266. FIPS_rsa_free(rsa);
  267. return ret;
  268. parse_error:
  269. fprintf(stderr, "FATAL parse error processing line %d\n", lnum);
  270. goto error;
  271. }
  272. static int rsa_printsig(FILE *out, RSA *rsa, const EVP_MD *dgst,
  273. unsigned char *Msg, long Msglen, int Saltlen)
  274. {
  275. int ret = 0;
  276. unsigned char *sigbuf = NULL;
  277. int i, siglen, pad_mode;
  278. /* EVP_PKEY structure */
  279. siglen = RSA_size(rsa);
  280. sigbuf = OPENSSL_malloc(siglen);
  281. if (!sigbuf)
  282. goto error;
  283. if (Saltlen >= 0)
  284. pad_mode = RSA_PKCS1_PSS_PADDING;
  285. else if (Saltlen == -2)
  286. pad_mode = RSA_X931_PADDING;
  287. else
  288. pad_mode = RSA_PKCS1_PADDING;
  289. if (!FIPS_rsa_sign(rsa, Msg, Msglen, dgst, pad_mode, Saltlen, NULL,
  290. sigbuf, (unsigned int *)&siglen))
  291. goto error;
  292. fputs("S = ", out);
  293. for (i = 0; i < siglen; i++)
  294. fprintf(out, "%02X", sigbuf[i]);
  295. fputs(RESP_EOL, out);
  296. ret = 1;
  297. error:
  298. if (sigbuf)
  299. OPENSSL_free(sigbuf);
  300. return ret;
  301. }
  302. #endif