e_rc4_hmac_md5.c: last commit was inappropriate for non-x86[_64] platforms.
[openssl.git] / fips / ecdh / fips_ecdhvs.c
1 /* fips/ecdh/fips_ecdhvs.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
54
55 #define OPENSSL_FIPSAPI
56 #include <openssl/opensslconf.h>
57
58 #ifndef OPENSSL_FIPS
59 #include <stdio.h>
60
61 int main(int argc, char **argv)
62 {
63     printf("No FIPS ECDH support\n");
64     return(0);
65 }
66 #else
67
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>
74 #include <string.h>
75 #include <ctype.h>
76
77 #include "fips_utl.h"
78
79 static const EVP_MD *eparse_md(char *line)
80         {
81         char *p;
82         if (line[0] != '[' || line[1] != 'E')
83                 return NULL;
84         p = strchr(line, '-');
85         if (!p)
86                 return NULL;
87         line = p + 1;
88         p = strchr(line, ']');
89         if (!p)
90                 return NULL;
91         *p = 0;
92         p = line;
93         while(isspace(*p))
94                 p++;
95         if (!strcmp(p, "SHA1"))
96                 return EVP_sha1();
97         else if (!strcmp(p, "SHA224"))
98                 return EVP_sha224();
99         else if (!strcmp(p, "SHA256"))
100                 return EVP_sha256();
101         else if (!strcmp(p, "SHA384"))
102                 return EVP_sha384();
103         else if (!strcmp(p, "SHA512"))
104                 return EVP_sha512();
105         else
106                 return NULL;
107         }
108
109 static int lookup_curve2(char *cname)
110         {
111         char *p;
112         p = strchr(cname, ']');
113         if (!p)
114                 {
115                 fprintf(stderr, "Parse error: missing ]\n");
116                 return NID_undef;
117                 }
118         *p = 0;
119
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;
150
151         fprintf(stderr, "Unknown Curve name %s\n", cname);
152         return NID_undef;
153         }
154
155 static int lookup_curve(char *cname)
156         {
157         char *p;
158         p = strchr(cname, ':');
159         if (!p)
160                 {
161                 fprintf(stderr, "Parse error: missing :\n");
162                 return NID_undef;
163                 }
164         cname = p + 1;
165         while(isspace(*cname))
166                 cname++;
167         return lookup_curve2(cname);
168         }
169
170 static EC_POINT *make_peer(EC_GROUP *group, BIGNUM *x, BIGNUM *y)
171         {
172         EC_POINT *peer;
173         int rv;
174         BN_CTX *c;
175         peer = EC_POINT_new(group);
176         if (!peer)
177                 return NULL;
178         c = BN_CTX_new();
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);
182         else
183 #ifdef OPENSSL_NO_EC2M
184                 {
185                 fprintf(stderr, "ERROR: GF2m not supported\n");
186                 exit(1);
187                 }
188 #else
189                 rv = EC_POINT_set_affine_coordinates_GF2m(group, peer, x, y, c);
190 #endif
191
192         BN_CTX_free(c);
193         if (rv)
194                 return peer;
195         EC_POINT_free(peer);
196         return NULL;
197         }
198
199 static int ec_print_key(FILE *out, EC_KEY *key, int add_e, int exout)
200         {
201         const EC_POINT *pt;
202         const EC_GROUP *grp;
203         const EC_METHOD *meth;
204         int rv;
205         BIGNUM *tx, *ty;
206         const BIGNUM *d = NULL;
207         BN_CTX *ctx;
208         ctx = BN_CTX_new();
209         if (!ctx)
210                 return 0;
211         tx = BN_CTX_get(ctx);
212         ty = BN_CTX_get(ctx);
213         if (!tx || !ty)
214                 return 0;
215         grp = EC_KEY_get0_group(key);
216         pt = EC_KEY_get0_public_key(key);
217         if (exout)
218                 d = EC_KEY_get0_private_key(key);
219         meth = EC_GROUP_method_of(grp);
220         if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
221                 rv = EC_POINT_get_affine_coordinates_GFp(grp, pt, tx, ty, ctx);
222         else
223 #ifdef OPENSSL_NO_EC2M
224                 {
225                 fprintf(stderr, "ERROR: GF2m not supported\n");
226                 exit(1);
227                 }
228 #else
229                 rv = EC_POINT_get_affine_coordinates_GF2m(grp, pt, tx, ty, ctx);
230 #endif
231
232         if (add_e)
233                 {
234                 do_bn_print_name(out, "QeIUTx", tx);
235                 do_bn_print_name(out, "QeIUTy", ty);
236                 if (d)
237                         do_bn_print_name(out, "QeIUTd", d);
238                 }
239         else
240                 {
241                 do_bn_print_name(out, "QIUTx", tx);
242                 do_bn_print_name(out, "QIUTy", ty);
243                 if (d)
244                         do_bn_print_name(out, "QIUTd", d);
245                 }
246
247         BN_CTX_free(ctx);
248
249         return rv;
250
251         }
252
253 static void ec_output_Zhash(FILE *out, int exout, EC_GROUP *group,
254                         BIGNUM *ix, BIGNUM *iy, BIGNUM *id, BIGNUM *cx,
255                         BIGNUM *cy, const EVP_MD *md,
256                                 unsigned char *rhash, size_t rhashlen)
257         {
258         EC_KEY *ec = NULL;
259         EC_POINT *peerkey = NULL;
260         unsigned char *Z;
261         unsigned char chash[EVP_MAX_MD_SIZE];
262         int Zlen;
263         ec = EC_KEY_new();
264         EC_KEY_set_flags(ec, EC_FLAG_COFACTOR_ECDH);
265         EC_KEY_set_group(ec, group);
266         peerkey = make_peer(group, cx, cy);
267         if (rhash == NULL)
268                 {
269                 if (md)
270                         rhashlen = M_EVP_MD_size(md);
271                 EC_KEY_generate_key(ec);
272                 ec_print_key(out, ec, md ? 1 : 0, exout);
273                 }
274         else
275                 {
276                 EC_KEY_set_public_key_affine_coordinates(ec, ix, iy);
277                 EC_KEY_set_private_key(ec, id);
278                 }
279         Zlen = (EC_GROUP_get_degree(group) + 7)/8;
280         Z = OPENSSL_malloc(Zlen);
281         if (!Z)
282                 exit(1);
283         ECDH_compute_key(Z, Zlen, peerkey, ec, 0);
284         if (md)
285                 {
286                 if (exout)
287                         OutputValue("Z", Z, Zlen, out, 0);
288                 FIPS_digest(Z, Zlen, chash, NULL, md);
289                 OutputValue(rhash ? "IUTHashZZ" : "HashZZ",
290                                                 chash, rhashlen, out, 0);
291                 if (rhash)
292                         {
293                         fprintf(out, "Result = %s\n",
294                                 memcmp(chash, rhash, rhashlen) ? "F" : "P");
295                         }
296                 }
297         else
298                 OutputValue("ZIUT", Z, Zlen, out, 0);
299         OPENSSL_cleanse(Z, Zlen);
300         OPENSSL_free(Z);
301         EC_KEY_free(ec);
302         EC_POINT_free(peerkey);
303         }
304                 
305 #ifdef FIPS_ALGVS
306 int fips_ecdhvs_main(int argc, char **argv)
307 #else
308 int main(int argc, char **argv)
309 #endif
310         {
311         char **args = argv + 1;
312         int argn = argc - 1;
313         FILE *in, *out;
314         char buf[2048], lbuf[2048];
315         unsigned char *rhash = NULL;
316         long rhashlen;
317         BIGNUM *cx = NULL, *cy = NULL;
318         BIGNUM *id = NULL, *ix = NULL, *iy = NULL;
319         const EVP_MD *md = NULL;
320         EC_GROUP *group = NULL;
321         char *keyword = NULL, *value = NULL;
322         int do_verify = -1, exout = 0;
323         int rv = 1;
324
325         int curve_nids[5] = {0,0,0,0,0};
326         int param_set = -1;
327
328         fips_algtest_init();
329
330         if (argn && !strcmp(*args, "ecdhver"))
331                 {
332                 do_verify = 1;
333                 args++;
334                 argn--;
335                 }
336         else if (argn && !strcmp(*args, "ecdhgen"))
337                 {
338                 do_verify = 0;
339                 args++;
340                 argn--;
341                 }
342
343         if (argn && !strcmp(*args, "-exout"))
344                 {
345                 exout = 1;
346                 args++;
347                 argn--;
348                 }
349
350         if (do_verify == -1)
351                 {
352                 fprintf(stderr,"%s [ecdhver|ecdhgen|] [-exout] (infile outfile)\n",argv[0]);
353                 exit(1);
354                 }
355
356         if (argn == 2)
357                 {
358                 in = fopen(*args, "r");
359                 if (!in)
360                         {
361                         fprintf(stderr, "Error opening input file\n");
362                         exit(1);
363                         }
364                 out = fopen(args[1], "w");
365                 if (!out)
366                         {
367                         fprintf(stderr, "Error opening output file\n");
368                         exit(1);
369                         }
370                 }
371         else if (argn == 0)
372                 {
373                 in = stdin;
374                 out = stdout;
375                 }
376         else
377                 {
378                 fprintf(stderr,"%s [dhver|dhgen|] [-exout] (infile outfile)\n",argv[0]);
379                 exit(1);
380                 }
381
382         while (fgets(buf, sizeof(buf), in) != NULL)
383                 {
384                 fputs(buf, out);
385                 if (buf[0] == '[' && buf[1] == 'E')
386                         {
387                         int c = buf[2];
388                         if (c < 'A' || c > 'E')
389                                 goto parse_error;
390                         param_set = c - 'A';
391                         /* If just [E?] then initial paramset */
392                         if (buf[3] == ']')
393                                 continue;
394                         if (group)
395                                 EC_GROUP_free(group);
396                         group = EC_GROUP_new_by_curve_name(curve_nids[c - 'A']);
397                         }
398                 if (strlen(buf) > 10 && !strncmp(buf, "[Curve", 6))
399                         {
400                         int nid;
401                         if (param_set == -1)
402                                 goto parse_error;
403                         nid = lookup_curve(buf);
404                         if (nid == NID_undef)
405                                 goto parse_error;
406                         curve_nids[param_set] = nid;
407                         }
408
409                 if (strlen(buf) > 4 && buf[0] == '[' && buf[2] == '-')
410                         {
411                         int nid = lookup_curve2(buf + 1);
412                         if (nid == NID_undef)
413                                 goto parse_error;
414                         if (group)
415                                 EC_GROUP_free(group);
416                         group = EC_GROUP_new_by_curve_name(nid);
417                         if (!group)
418                                 {
419                                 fprintf(stderr, "ERROR: unsupported curve %s\n", buf + 1);
420                                 return 1;
421                                 }
422                         }
423
424                 if (strlen(buf) > 6 && !strncmp(buf, "[E", 2))
425                         {
426                         md = eparse_md(buf);
427                         if (md == NULL)
428                                 goto parse_error;
429                         continue;
430                         }
431                 if (!parse_line(&keyword, &value, lbuf, buf))
432                         continue;
433                 if (!strcmp(keyword, "QeCAVSx") || !strcmp(keyword, "QCAVSx"))
434                         {
435                         if (!do_hex2bn(&cx, value))
436                                 goto parse_error;
437                         }
438                 else if (!strcmp(keyword, "QeCAVSy") || !strcmp(keyword, "QCAVSy"))
439                         {
440                         if (!do_hex2bn(&cy, value))
441                                 goto parse_error;
442                         if (do_verify == 0)
443                                 ec_output_Zhash(out, exout, group,
444                                                 NULL, NULL, NULL,
445                                                 cx, cy, md, rhash, rhashlen);
446                         }
447                 else if (!strcmp(keyword, "deIUT"))
448                         {
449                         if (!do_hex2bn(&id, value))
450                                 goto parse_error;
451                         }
452                 else if (!strcmp(keyword, "QeIUTx"))
453                         {
454                         if (!do_hex2bn(&ix, value))
455                                 goto parse_error;
456                         }
457                 else if (!strcmp(keyword, "QeIUTy"))
458                         {
459                         if (!do_hex2bn(&iy, value))
460                                 goto parse_error;
461                         }
462                 else if (!strcmp(keyword, "CAVSHashZZ"))
463                         {
464                         if (!md)
465                                 goto parse_error;
466                         rhash = hex2bin_m(value, &rhashlen);
467                         if (!rhash || rhashlen != M_EVP_MD_size(md))
468                                 goto parse_error;
469                         ec_output_Zhash(out, exout, group, ix, iy, id, cx, cy,
470                                         md, rhash, rhashlen);
471                         }
472                 }
473         rv = 0;
474         parse_error:
475         if (id)
476                 BN_free(id);
477         if (ix)
478                 BN_free(ix);
479         if (iy)
480                 BN_free(iy);
481         if (cx)
482                 BN_free(cx);
483         if (cy)
484                 BN_free(cy);
485         if (group)
486                 EC_GROUP_free(group);
487         if (in && in != stdin)
488                 fclose(in);
489         if (out && out != stdout)
490                 fclose(out);
491         if (rv)
492                 fprintf(stderr, "Error Parsing request file\n");
493         return rv;
494         }
495
496 #endif