1 /* fips/ecdh/fips_ecdhvs.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
55 #define OPENSSL_FIPSAPI
56 #include <openssl/opensslconf.h>
61 int main(int argc, char **argv)
63 printf("No FIPS ECDH support\n");
68 #include <openssl/crypto.h>
69 #include <openssl/bn.h>
70 #include <openssl/ecdh.h>
71 #include <openssl/fips.h>
72 #include <openssl/err.h>
73 #include <openssl/evp.h>
79 static const EVP_MD *parse_md(char *line)
82 if (line[0] != '[' || line[1] != 'E')
84 p = strchr(line, '-');
88 p = strchr(line, ']');
95 if (!strcmp(p, "SHA1"))
97 else if (!strcmp(p, "SHA224"))
99 else if (!strcmp(p, "SHA256"))
101 else if (!strcmp(p, "SHA384"))
103 else if (!strcmp(p, "SHA512"))
109 static int lookup_curve2(char *cname)
112 p = strchr(cname, ']');
115 fprintf(stderr, "Parse error: missing ]\n");
120 if (!strcmp(cname, "B-163"))
121 return NID_sect163r2;
122 if (!strcmp(cname, "B-233"))
123 return NID_sect233r1;
124 if (!strcmp(cname, "B-283"))
125 return NID_sect283r1;
126 if (!strcmp(cname, "B-409"))
127 return NID_sect409r1;
128 if (!strcmp(cname, "B-571"))
129 return NID_sect571r1;
130 if (!strcmp(cname, "K-163"))
131 return NID_sect163k1;
132 if (!strcmp(cname, "K-233"))
133 return NID_sect233k1;
134 if (!strcmp(cname, "K-283"))
135 return NID_sect283k1;
136 if (!strcmp(cname, "K-409"))
137 return NID_sect409k1;
138 if (!strcmp(cname, "K-571"))
139 return NID_sect571k1;
140 if (!strcmp(cname, "P-192"))
141 return NID_X9_62_prime192v1;
142 if (!strcmp(cname, "P-224"))
143 return NID_secp224r1;
144 if (!strcmp(cname, "P-256"))
145 return NID_X9_62_prime256v1;
146 if (!strcmp(cname, "P-384"))
147 return NID_secp384r1;
148 if (!strcmp(cname, "P-521"))
149 return NID_secp521r1;
151 fprintf(stderr, "Unknown Curve name %s\n", cname);
155 static int lookup_curve(char *cname)
158 p = strchr(cname, ':');
161 fprintf(stderr, "Parse error: missing :\n");
165 while(isspace(*cname))
167 return lookup_curve2(cname);
170 static EC_POINT *make_peer(EC_GROUP *group, BIGNUM *x, BIGNUM *y)
175 peer = EC_POINT_new(group);
179 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group))
180 == NID_X9_62_prime_field)
181 rv = EC_POINT_set_affine_coordinates_GFp(group, peer, x, y, c);
183 #ifdef OPENSSL_NO_EC2M
185 fprintf(stderr, "ERROR: GF2m not supported\n");
189 rv = EC_POINT_set_affine_coordinates_GF2m(group, peer, x, y, c);
199 static int ec_print_pubkey(FILE *out, EC_KEY *key, int add_e)
203 const EC_METHOD *meth;
210 tx = BN_CTX_get(ctx);
211 ty = BN_CTX_get(ctx);
214 grp = EC_KEY_get0_group(key);
215 pt = EC_KEY_get0_public_key(key);
216 meth = EC_GROUP_method_of(grp);
217 if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
218 rv = EC_POINT_get_affine_coordinates_GFp(grp, pt, tx, ty, ctx);
220 #ifdef OPENSSL_NO_EC2M
222 fprintf(stderr, "ERROR: GF2m not supported\n");
226 rv = EC_POINT_get_affine_coordinates_GF2m(grp, pt, tx, ty, ctx);
231 do_bn_print_name(out, "QeIUTx", tx);
232 do_bn_print_name(out, "QeIUTy", ty);
236 do_bn_print_name(out, "QIUTx", tx);
237 do_bn_print_name(out, "QIUTy", ty);
246 static void ec_output_Zhash(FILE *out, int exout, EC_GROUP *group,
247 BIGNUM *ix, BIGNUM *iy, BIGNUM *id, BIGNUM *cx,
248 BIGNUM *cy, const EVP_MD *md,
249 unsigned char *rhash, size_t rhashlen)
252 EC_POINT *peerkey = NULL;
254 unsigned char chash[EVP_MAX_MD_SIZE];
257 EC_KEY_set_group(ec, group);
258 peerkey = make_peer(group, cx, cy);
262 rhashlen = M_EVP_MD_size(md);
263 EC_KEY_generate_key(ec);
264 ec_print_pubkey(out, ec, md ? 1 : 0);
268 EC_KEY_set_public_key_affine_coordinates(ec, ix, iy);
269 EC_KEY_set_private_key(ec, id);
271 Zlen = (EC_GROUP_get_degree(group) + 7)/8;
272 Z = OPENSSL_malloc(Zlen);
275 ECDH_compute_key(Z, Zlen, peerkey, ec, 0);
279 OutputValue("Z", Z, Zlen, out, 0);
280 FIPS_digest(Z, Zlen, chash, NULL, md);
281 OutputValue(rhash ? "IUTHashZZ" : "HashZZ",
282 chash, rhashlen, out, 0);
285 fprintf(out, "Result = %s\n",
286 memcmp(chash, rhash, rhashlen) ? "F" : "P");
290 OutputValue("ZIUT", Z, Zlen, out, 0);
291 OPENSSL_cleanse(Z, Zlen);
294 EC_POINT_free(peerkey);
297 int main(int argc,char **argv)
299 char **args = argv + 1;
302 char buf[2048], lbuf[2048];
303 unsigned char *rhash = NULL;
305 BIGNUM *cx = NULL, *cy = NULL;
306 BIGNUM *id = NULL, *ix = NULL, *iy = NULL;
307 const EVP_MD *md = NULL;
308 EC_GROUP *group = NULL;
309 char *keyword = NULL, *value = NULL;
310 int do_verify = -1, exout = 0;
312 int curve_nids[5] = {0,0,0,0,0};
317 if (argn && !strcmp(*args, "ecdhver"))
323 else if (argn && !strcmp(*args, "ecdhgen"))
330 if (argn && !strcmp(*args, "-exout"))
339 fprintf(stderr,"%s [ecdhver|ecdhgen|] [-exout] (infile outfile)\n",argv[0]);
345 in = fopen(*args, "r");
348 fprintf(stderr, "Error opening input file\n");
351 out = fopen(args[1], "w");
354 fprintf(stderr, "Error opening output file\n");
365 fprintf(stderr,"%s [dhver|dhgen|] [-exout] (infile outfile)\n",argv[0]);
369 while (fgets(buf, sizeof(buf), in) != NULL)
372 if (buf[0] == '[' && buf[1] == 'E')
375 if (c < 'A' || c > 'E')
378 /* If just [E?] then initial paramset */
382 EC_GROUP_free(group);
383 group = EC_GROUP_new_by_curve_name(curve_nids[c - 'A']);
385 if (strlen(buf) > 10 && !strncmp(buf, "[Curve", 6))
390 nid = lookup_curve(buf);
391 if (nid == NID_undef)
393 curve_nids[param_set] = nid;
396 if (strlen(buf) > 4 && buf[0] == '[' && buf[2] == '-')
398 int nid = lookup_curve2(buf + 1);
399 if (nid == NID_undef)
402 EC_GROUP_free(group);
403 group = EC_GROUP_new_by_curve_name(nid);
406 if (strlen(buf) > 6 && !strncmp(buf, "[E", 2))
413 if (!parse_line(&keyword, &value, lbuf, buf))
415 if (!strcmp(keyword, "QeCAVSx") || !strcmp(keyword, "QCAVSx"))
417 if (!do_hex2bn(&cx, value))
420 else if (!strcmp(keyword, "QeCAVSy") || !strcmp(keyword, "QCAVSy"))
422 if (!do_hex2bn(&cy, value))
425 ec_output_Zhash(out, exout, group,
427 cx, cy, md, rhash, rhashlen);
429 else if (!strcmp(keyword, "deIUT"))
431 if (!do_hex2bn(&id, value))
434 else if (!strcmp(keyword, "QeIUTx"))
436 if (!do_hex2bn(&ix, value))
439 else if (!strcmp(keyword, "QeIUTy"))
441 if (!do_hex2bn(&iy, value))
444 else if (!strcmp(keyword, "CAVSHashZZ"))
448 rhash = hex2bin_m(value, &rhashlen);
449 if (!rhash || rhashlen != M_EVP_MD_size(md))
451 ec_output_Zhash(out, exout, group, ix, iy, id, cx, cy,
452 md, rhash, rhashlen);
457 fprintf(stderr, "Error Parsing request file\n");