fips_dhvs.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* fips/dh/fips_dhvs.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2011 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. #define OPENSSL_FIPSAPI
  54. #include <openssl/opensslconf.h>
  55. #ifndef OPENSSL_FIPS
  56. #include <stdio.h>
  57. int main(int argc, char **argv)
  58. {
  59. printf("No FIPS DH support\n");
  60. return(0);
  61. }
  62. #else
  63. #include <openssl/crypto.h>
  64. #include <openssl/bn.h>
  65. #include <openssl/dh.h>
  66. #include <openssl/fips.h>
  67. #include <openssl/err.h>
  68. #include <openssl/evp.h>
  69. #include <string.h>
  70. #include <ctype.h>
  71. #include "fips_utl.h"
  72. static const EVP_MD *parse_md(char *line)
  73. {
  74. char *p;
  75. if (line[0] != '[' || line[1] != 'F')
  76. return NULL;
  77. p = strchr(line, '-');
  78. if (!p)
  79. return NULL;
  80. line = p + 1;
  81. p = strchr(line, ']');
  82. if (!p)
  83. return NULL;
  84. *p = 0;
  85. p = line;
  86. while(isspace(*p))
  87. p++;
  88. if (!strcmp(p, "SHA1"))
  89. return EVP_sha1();
  90. else if (!strcmp(p, "SHA224"))
  91. return EVP_sha224();
  92. else if (!strcmp(p, "SHA256"))
  93. return EVP_sha256();
  94. else if (!strcmp(p, "SHA384"))
  95. return EVP_sha384();
  96. else if (!strcmp(p, "SHA512"))
  97. return EVP_sha512();
  98. else
  99. return NULL;
  100. }
  101. static void output_Zhash(FILE *out, int exout,
  102. DH *dh, BIGNUM *peerkey, const EVP_MD *md,
  103. unsigned char *rhash, size_t rhashlen)
  104. {
  105. unsigned char *Z;
  106. unsigned char chash[EVP_MAX_MD_SIZE];
  107. int Zlen;
  108. if (rhash == NULL)
  109. {
  110. rhashlen = M_EVP_MD_size(md);
  111. if (!DH_generate_key(dh))
  112. exit (1);
  113. do_bn_print_name(out, "YephemIUT", dh->pub_key);
  114. if (exout)
  115. do_bn_print_name(out, "XephemIUT", dh->priv_key);
  116. }
  117. Z = OPENSSL_malloc(BN_num_bytes(dh->p));
  118. if (!Z)
  119. exit(1);
  120. Zlen = DH_compute_key_padded(Z, peerkey, dh);
  121. if (exout)
  122. OutputValue("Z", Z, Zlen, out, 0);
  123. FIPS_digest(Z, Zlen, chash, NULL, md);
  124. OutputValue(rhash ? "IUTHashZZ" : "HashZZ", chash, rhashlen, out, 0);
  125. if (rhash)
  126. {
  127. fprintf(out, "Result = %s\n",
  128. memcmp(chash, rhash, rhashlen) ? "F" : "P");
  129. }
  130. else
  131. {
  132. BN_clear_free(dh->priv_key);
  133. BN_clear_free(dh->pub_key);
  134. dh->priv_key = NULL;
  135. dh->pub_key = NULL;
  136. }
  137. OPENSSL_cleanse(Z, Zlen);
  138. OPENSSL_free(Z);
  139. }
  140. int main(int argc,char **argv)
  141. {
  142. char **args = argv + 1;
  143. int argn = argc - 1;
  144. FILE *in, *out;
  145. char buf[2048], lbuf[2048];
  146. unsigned char *rhash;
  147. long rhashlen;
  148. DH *dh = NULL;
  149. const EVP_MD *md = NULL;
  150. BIGNUM *peerkey = NULL;
  151. char *keyword = NULL, *value = NULL;
  152. int do_verify = -1, exout = 0;
  153. fips_algtest_init();
  154. if (argn && !strcmp(*args, "dhver"))
  155. {
  156. do_verify = 1;
  157. args++;
  158. argn--;
  159. }
  160. else if (argn && !strcmp(*args, "dhgen"))
  161. {
  162. do_verify = 0;
  163. args++;
  164. argn--;
  165. }
  166. if (argn && !strcmp(*args, "-exout"))
  167. {
  168. exout = 1;
  169. args++;
  170. argn--;
  171. }
  172. if (do_verify == -1)
  173. {
  174. fprintf(stderr,"%s [dhver|dhgen|] [-exout] (infile outfile)\n",argv[0]);
  175. exit(1);
  176. }
  177. if (argn == 2)
  178. {
  179. in = fopen(*args, "r");
  180. if (!in)
  181. {
  182. fprintf(stderr, "Error opening input file\n");
  183. exit(1);
  184. }
  185. out = fopen(args[1], "w");
  186. if (!out)
  187. {
  188. fprintf(stderr, "Error opening output file\n");
  189. exit(1);
  190. }
  191. }
  192. else if (argn == 0)
  193. {
  194. in = stdin;
  195. out = stdout;
  196. }
  197. else
  198. {
  199. fprintf(stderr,"%s [dhver|dhgen|] [-exout] (infile outfile)\n",argv[0]);
  200. exit(1);
  201. }
  202. dh = FIPS_dh_new();
  203. while (fgets(buf, sizeof(buf), in) != NULL)
  204. {
  205. fputs(buf, out);
  206. if (strlen(buf) > 6 && !strncmp(buf, "[F", 2))
  207. {
  208. md = parse_md(buf);
  209. if (md == NULL)
  210. goto parse_error;
  211. if (dh)
  212. FIPS_dh_free(dh);
  213. dh = FIPS_dh_new();
  214. continue;
  215. }
  216. if (!parse_line(&keyword, &value, lbuf, buf))
  217. continue;
  218. if (!strcmp(keyword, "P"))
  219. {
  220. if (!do_hex2bn(&dh->p, value))
  221. goto parse_error;
  222. }
  223. else if (!strcmp(keyword, "Q"))
  224. {
  225. if (!do_hex2bn(&dh->q, value))
  226. goto parse_error;
  227. }
  228. else if (!strcmp(keyword, "G"))
  229. {
  230. if (!do_hex2bn(&dh->g, value))
  231. goto parse_error;
  232. }
  233. else if (!strcmp(keyword, "XephemIUT"))
  234. {
  235. if (!do_hex2bn(&dh->priv_key, value))
  236. goto parse_error;
  237. }
  238. else if (!strcmp(keyword, "YephemIUT"))
  239. {
  240. if (!do_hex2bn(&dh->pub_key, value))
  241. goto parse_error;
  242. }
  243. else if (!strcmp(keyword, "YephemCAVS"))
  244. {
  245. if (!do_hex2bn(&peerkey, value))
  246. goto parse_error;
  247. if (do_verify == 0)
  248. output_Zhash(out, exout, dh, peerkey, md,
  249. NULL, 0);
  250. }
  251. else if (!strcmp(keyword, "CAVSHashZZ"))
  252. {
  253. if (!md)
  254. goto parse_error;
  255. rhash = hex2bin_m(value, &rhashlen);
  256. if (!rhash || rhashlen != M_EVP_MD_size(md))
  257. goto parse_error;
  258. output_Zhash(out, exout, dh, peerkey, md,
  259. rhash, rhashlen);
  260. }
  261. }
  262. return 0;
  263. parse_error:
  264. fprintf(stderr, "Error Parsing request file\n");
  265. exit(1);
  266. }
  267. #endif