fips_rsastest.c 8.1 KB

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