fips_rsagtest.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* fips_rsagtest.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project 2005.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2005,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 <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. int rsa_test(FILE *out, FILE *in);
  78. static int rsa_printkey1(FILE *out, RSA *rsa,
  79. BIGNUM *Xp1, BIGNUM *Xp2, BIGNUM *Xp,
  80. BIGNUM *e);
  81. static int rsa_printkey2(FILE *out, RSA *rsa,
  82. BIGNUM *Xq1, BIGNUM *Xq2, BIGNUM *Xq);
  83. int main(int argc, char **argv)
  84. {
  85. FILE *in = NULL, *out = NULL;
  86. int ret = 1;
  87. fips_algtest_init();
  88. if (argc == 1)
  89. in = stdin;
  90. else
  91. in = fopen(argv[1], "r");
  92. if (argc < 2)
  93. out = stdout;
  94. else
  95. out = fopen(argv[2], "w");
  96. if (!in)
  97. {
  98. fprintf(stderr, "FATAL input initialization error\n");
  99. goto end;
  100. }
  101. if (!out)
  102. {
  103. fprintf(stderr, "FATAL output initialization error\n");
  104. goto end;
  105. }
  106. if (!rsa_test(out, in))
  107. {
  108. fprintf(stderr, "FATAL RSAGTEST file processing error\n");
  109. goto end;
  110. }
  111. else
  112. ret = 0;
  113. end:
  114. if (in && (in != stdin))
  115. fclose(in);
  116. if (out && (out != stdout))
  117. fclose(out);
  118. return ret;
  119. }
  120. #define RSA_TEST_MAXLINELEN 10240
  121. int rsa_test(FILE *out, FILE *in)
  122. {
  123. char *linebuf, *olinebuf, *p, *q;
  124. char *keyword, *value;
  125. RSA *rsa = NULL;
  126. BIGNUM *Xp1 = NULL, *Xp2 = NULL, *Xp = NULL;
  127. BIGNUM *Xq1 = NULL, *Xq2 = NULL, *Xq = NULL;
  128. BIGNUM *e = NULL;
  129. int ret = 0;
  130. int lnum = 0;
  131. olinebuf = OPENSSL_malloc(RSA_TEST_MAXLINELEN);
  132. linebuf = OPENSSL_malloc(RSA_TEST_MAXLINELEN);
  133. if (!linebuf || !olinebuf)
  134. goto error;
  135. while (fgets(olinebuf, RSA_TEST_MAXLINELEN, in))
  136. {
  137. lnum++;
  138. strcpy(linebuf, olinebuf);
  139. keyword = linebuf;
  140. /* Skip leading space */
  141. while (isspace((unsigned char)*keyword))
  142. keyword++;
  143. /* Look for = sign */
  144. p = strchr(linebuf, '=');
  145. /* If no = or starts with [ (for [foo = bar] line) just copy */
  146. if (!p || *keyword=='[')
  147. {
  148. if (fputs(olinebuf, out) < 0)
  149. goto error;
  150. continue;
  151. }
  152. q = p - 1;
  153. /* Remove trailing space */
  154. while (isspace((unsigned char)*q))
  155. *q-- = 0;
  156. *p = 0;
  157. value = p + 1;
  158. /* Remove leading space from value */
  159. while (isspace((unsigned char)*value))
  160. value++;
  161. /* Remove trailing space from value */
  162. p = value + strlen(value) - 1;
  163. while (*p == '\n' || isspace((unsigned char)*p))
  164. *p-- = 0;
  165. if (!strcmp(keyword, "xp1"))
  166. {
  167. if (Xp1 || !do_hex2bn(&Xp1,value))
  168. goto parse_error;
  169. }
  170. else if (!strcmp(keyword, "xp2"))
  171. {
  172. if (Xp2 || !do_hex2bn(&Xp2,value))
  173. goto parse_error;
  174. }
  175. else if (!strcmp(keyword, "Xp"))
  176. {
  177. if (Xp || !do_hex2bn(&Xp,value))
  178. goto parse_error;
  179. }
  180. else if (!strcmp(keyword, "xq1"))
  181. {
  182. if (Xq1 || !do_hex2bn(&Xq1,value))
  183. goto parse_error;
  184. }
  185. else if (!strcmp(keyword, "xq2"))
  186. {
  187. if (Xq2 || !do_hex2bn(&Xq2,value))
  188. goto parse_error;
  189. }
  190. else if (!strcmp(keyword, "Xq"))
  191. {
  192. if (Xq || !do_hex2bn(&Xq,value))
  193. goto parse_error;
  194. }
  195. else if (!strcmp(keyword, "e"))
  196. {
  197. if (e || !do_hex2bn(&e,value))
  198. goto parse_error;
  199. }
  200. else if (!strcmp(keyword, "p1"))
  201. continue;
  202. else if (!strcmp(keyword, "p2"))
  203. continue;
  204. else if (!strcmp(keyword, "p"))
  205. continue;
  206. else if (!strcmp(keyword, "q1"))
  207. continue;
  208. else if (!strcmp(keyword, "q2"))
  209. continue;
  210. else if (!strcmp(keyword, "q"))
  211. continue;
  212. else if (!strcmp(keyword, "n"))
  213. continue;
  214. else if (!strcmp(keyword, "d"))
  215. continue;
  216. else
  217. goto parse_error;
  218. fputs(olinebuf, out);
  219. if (e && Xp1 && Xp2 && Xp)
  220. {
  221. rsa = FIPS_rsa_new();
  222. if (!rsa)
  223. goto error;
  224. if (!rsa_printkey1(out, rsa, Xp1, Xp2, Xp, e))
  225. goto error;
  226. BN_free(Xp1);
  227. Xp1 = NULL;
  228. BN_free(Xp2);
  229. Xp2 = NULL;
  230. BN_free(Xp);
  231. Xp = NULL;
  232. BN_free(e);
  233. e = NULL;
  234. }
  235. if (rsa && Xq1 && Xq2 && Xq)
  236. {
  237. if (!rsa_printkey2(out, rsa, Xq1, Xq2, Xq))
  238. goto error;
  239. BN_free(Xq1);
  240. Xq1 = NULL;
  241. BN_free(Xq2);
  242. Xq2 = NULL;
  243. BN_free(Xq);
  244. Xq = NULL;
  245. FIPS_rsa_free(rsa);
  246. rsa = NULL;
  247. }
  248. }
  249. ret = 1;
  250. error:
  251. if (olinebuf)
  252. OPENSSL_free(olinebuf);
  253. if (linebuf)
  254. OPENSSL_free(linebuf);
  255. if (Xp1)
  256. BN_free(Xp1);
  257. if (Xp2)
  258. BN_free(Xp2);
  259. if (Xp)
  260. BN_free(Xp);
  261. if (Xq1)
  262. BN_free(Xq1);
  263. if (Xq1)
  264. BN_free(Xq1);
  265. if (Xq2)
  266. BN_free(Xq2);
  267. if (Xq)
  268. BN_free(Xq);
  269. if (e)
  270. BN_free(e);
  271. if (rsa)
  272. FIPS_rsa_free(rsa);
  273. return ret;
  274. parse_error:
  275. fprintf(stderr, "FATAL parse error processing line %d\n", lnum);
  276. goto error;
  277. }
  278. static int rsa_printkey1(FILE *out, RSA *rsa,
  279. BIGNUM *Xp1, BIGNUM *Xp2, BIGNUM *Xp,
  280. BIGNUM *e)
  281. {
  282. int ret = 0;
  283. BIGNUM *p1 = NULL, *p2 = NULL;
  284. p1 = BN_new();
  285. p2 = BN_new();
  286. if (!p1 || !p2)
  287. goto error;
  288. if (!RSA_X931_derive_ex(rsa, p1, p2, NULL, NULL, Xp1, Xp2, Xp,
  289. NULL, NULL, NULL, e, NULL))
  290. goto error;
  291. do_bn_print_name(out, "p1", p1);
  292. do_bn_print_name(out, "p2", p2);
  293. do_bn_print_name(out, "p", rsa->p);
  294. ret = 1;
  295. error:
  296. if (p1)
  297. BN_free(p1);
  298. if (p2)
  299. BN_free(p2);
  300. return ret;
  301. }
  302. static int rsa_printkey2(FILE *out, RSA *rsa,
  303. BIGNUM *Xq1, BIGNUM *Xq2, BIGNUM *Xq)
  304. {
  305. int ret = 0;
  306. BIGNUM *q1 = NULL, *q2 = NULL;
  307. q1 = BN_new();
  308. q2 = BN_new();
  309. if (!q1 || !q2)
  310. goto error;
  311. if (!RSA_X931_derive_ex(rsa, NULL, NULL, q1, q2, NULL, NULL, NULL,
  312. Xq1, Xq2, Xq, NULL, NULL))
  313. goto error;
  314. do_bn_print_name(out, "q1", q1);
  315. do_bn_print_name(out, "q2", q2);
  316. do_bn_print_name(out, "q", rsa->q);
  317. do_bn_print_name(out, "n", rsa->n);
  318. do_bn_print_name(out, "d", rsa->d);
  319. ret = 1;
  320. error:
  321. if (q1)
  322. BN_free(q1);
  323. if (q2)
  324. BN_free(q2);
  325. return ret;
  326. }
  327. #endif