fips_rsavtest.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* fips_rsavtest.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. int rsa_test(FILE *out, FILE *in, int saltlen);
  78. static int rsa_printver(FILE *out,
  79. BIGNUM *n, BIGNUM *e,
  80. const EVP_MD *dgst,
  81. unsigned char *Msg, long Msglen,
  82. unsigned char *S, long Slen, int Saltlen);
  83. int main(int argc, char **argv)
  84. {
  85. FILE *in = NULL, *out = NULL;
  86. int ret = 1;
  87. int 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_test(out, in, Saltlen))
  125. {
  126. fprintf(stderr, "FATAL RSAVTEST 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_test(FILE *out, FILE *in, int Saltlen)
  140. {
  141. char *linebuf, *olinebuf, *p, *q;
  142. char *keyword, *value;
  143. const EVP_MD *dgst = NULL;
  144. BIGNUM *n = NULL, *e = NULL;
  145. unsigned char *Msg = NULL, *S = NULL;
  146. long Msglen, Slen;
  147. int ret = 0;
  148. int lnum = 0;
  149. olinebuf = OPENSSL_malloc(RSA_TEST_MAXLINELEN);
  150. linebuf = OPENSSL_malloc(RSA_TEST_MAXLINELEN);
  151. if (!linebuf || !olinebuf)
  152. goto error;
  153. while (fgets(olinebuf, RSA_TEST_MAXLINELEN, in))
  154. {
  155. lnum++;
  156. strcpy(linebuf, olinebuf);
  157. keyword = linebuf;
  158. /* Skip leading space */
  159. while (isspace((unsigned char)*keyword))
  160. keyword++;
  161. /* Look for = sign */
  162. p = strchr(linebuf, '=');
  163. /* If no = or starts with [ (for [foo = bar] line) just copy */
  164. if (!p || *keyword=='[')
  165. {
  166. if (fputs(olinebuf, out) < 0)
  167. goto error;
  168. continue;
  169. }
  170. q = p - 1;
  171. /* Remove trailing space */
  172. while (isspace((unsigned char)*q))
  173. *q-- = 0;
  174. *p = 0;
  175. value = p + 1;
  176. /* Remove leading space from value */
  177. while (isspace((unsigned char)*value))
  178. value++;
  179. /* Remove trailing space from value */
  180. p = value + strlen(value) - 1;
  181. while (*p == '\n' || isspace((unsigned char)*p))
  182. *p-- = 0;
  183. if (!strcmp(keyword, "n"))
  184. {
  185. if (!do_hex2bn(&n,value))
  186. goto parse_error;
  187. }
  188. else if (!strcmp(keyword, "e"))
  189. {
  190. if (!do_hex2bn(&e,value))
  191. goto parse_error;
  192. }
  193. else if (!strcmp(keyword, "SHAAlg"))
  194. {
  195. if (!strcmp(value, "SHA1"))
  196. dgst = EVP_sha1();
  197. else if (!strcmp(value, "SHA224"))
  198. dgst = EVP_sha224();
  199. else if (!strcmp(value, "SHA256"))
  200. dgst = EVP_sha256();
  201. else if (!strcmp(value, "SHA384"))
  202. dgst = EVP_sha384();
  203. else if (!strcmp(value, "SHA512"))
  204. dgst = EVP_sha512();
  205. else
  206. {
  207. fprintf(stderr,
  208. "FATAL: unsupported algorithm \"%s\"\n",
  209. value);
  210. goto parse_error;
  211. }
  212. }
  213. else if (!strcmp(keyword, "Msg"))
  214. {
  215. if (Msg)
  216. goto parse_error;
  217. if (strlen(value) & 1)
  218. *(--value) = '0';
  219. Msg = hex2bin_m(value, &Msglen);
  220. if (!Msg)
  221. goto parse_error;
  222. }
  223. else if (!strcmp(keyword, "S"))
  224. {
  225. if (S)
  226. goto parse_error;
  227. if (strlen(value) & 1)
  228. *(--value) = '0';
  229. S = hex2bin_m(value, &Slen);
  230. if (!S)
  231. goto parse_error;
  232. }
  233. else if (!strcmp(keyword, "Result"))
  234. continue;
  235. else
  236. goto parse_error;
  237. fputs(olinebuf, out);
  238. if (n && e && Msg && S && dgst)
  239. {
  240. if (!rsa_printver(out, n, e, dgst,
  241. Msg, Msglen, S, Slen, Saltlen))
  242. goto error;
  243. OPENSSL_free(Msg);
  244. Msg = NULL;
  245. OPENSSL_free(S);
  246. S = NULL;
  247. }
  248. }
  249. ret = 1;
  250. error:
  251. if (olinebuf)
  252. OPENSSL_free(olinebuf);
  253. if (linebuf)
  254. OPENSSL_free(linebuf);
  255. if (n)
  256. BN_free(n);
  257. if (e)
  258. BN_free(e);
  259. return ret;
  260. parse_error:
  261. fprintf(stderr, "FATAL parse error processing line %d\n", lnum);
  262. goto error;
  263. }
  264. static int rsa_printver(FILE *out,
  265. BIGNUM *n, BIGNUM *e,
  266. const EVP_MD *dgst,
  267. unsigned char *Msg, long Msglen,
  268. unsigned char *S, long Slen, int Saltlen)
  269. {
  270. int ret = 0, r, pad_mode;
  271. /* Setup RSA and EVP_PKEY structures */
  272. RSA *rsa_pubkey = NULL;
  273. EVP_MD_CTX ctx;
  274. unsigned char *buf = NULL;
  275. rsa_pubkey = FIPS_rsa_new();
  276. if (!rsa_pubkey)
  277. goto error;
  278. rsa_pubkey->n = BN_dup(n);
  279. rsa_pubkey->e = BN_dup(e);
  280. if (!rsa_pubkey->n || !rsa_pubkey->e)
  281. goto error;
  282. FIPS_md_ctx_init(&ctx);
  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_digestinit(&ctx, dgst))
  290. goto error;
  291. if (!FIPS_digestupdate(&ctx, Msg, Msglen))
  292. goto error;
  293. no_err = 1;
  294. r = FIPS_rsa_verify_ctx(rsa_pubkey, &ctx,
  295. pad_mode, Saltlen, NULL, S, Slen);
  296. no_err = 0;
  297. FIPS_md_ctx_cleanup(&ctx);
  298. if (r < 0)
  299. goto error;
  300. if (r == 0)
  301. fputs("Result = F\n", out);
  302. else
  303. fputs("Result = P\n", out);
  304. ret = 1;
  305. error:
  306. if (rsa_pubkey)
  307. FIPS_rsa_free(rsa_pubkey);
  308. if (buf)
  309. OPENSSL_free(buf);
  310. return ret;
  311. }
  312. #endif