Make no-ec2m work again.
[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 *parse_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_curve(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         cname = p + 1;
119         while(isspace(*cname))
120                 cname++;
121         p = strchr(cname, ']');
122         if (!p)
123                 {
124                 fprintf(stderr, "Parse error: missing ]\n");
125                 return NID_undef;
126                 }
127         *p = 0;
128
129         if (!strcmp(cname, "B-163"))
130                 return NID_sect163r2;
131         if (!strcmp(cname, "B-233"))
132                 return NID_sect233r1;
133         if (!strcmp(cname, "B-283"))
134                 return NID_sect283r1;
135         if (!strcmp(cname, "B-409"))
136                 return NID_sect409r1;
137         if (!strcmp(cname, "B-571"))
138                 return NID_sect571r1;
139         if (!strcmp(cname, "K-163"))
140                 return NID_sect163k1;
141         if (!strcmp(cname, "K-233"))
142                 return NID_sect233k1;
143         if (!strcmp(cname, "K-283"))
144                 return NID_sect283k1;
145         if (!strcmp(cname, "K-409"))
146                 return NID_sect409k1;
147         if (!strcmp(cname, "K-571"))
148                 return NID_sect571k1;
149         if (!strcmp(cname, "P-192"))
150                 return NID_X9_62_prime192v1;
151         if (!strcmp(cname, "P-224"))
152                 return NID_secp224r1;
153         if (!strcmp(cname, "P-256"))
154                 return NID_X9_62_prime256v1;
155         if (!strcmp(cname, "P-384"))
156                 return NID_secp384r1;
157         if (!strcmp(cname, "P-521"))
158                 return NID_secp521r1;
159
160         fprintf(stderr, "Unknown Curve name %s\n", cname);
161         return NID_undef;
162         }
163
164 static EC_POINT *make_peer(EC_GROUP *group, BIGNUM *x, BIGNUM *y)
165         {
166         EC_POINT *peer;
167         int rv;
168         BN_CTX *c;
169         peer = EC_POINT_new(group);
170         if (!peer)
171                 return NULL;
172         c = BN_CTX_new();
173         if (EC_METHOD_get_field_type(EC_GROUP_method_of(group))
174                 == NID_X9_62_prime_field)
175                 rv = EC_POINT_set_affine_coordinates_GFp(group, peer, x, y, c);
176         else
177 #ifdef OPENSSL_NO_EC2M
178                 {
179                 fprintf(stderr, "ERROR: GF2m not supported\n");
180                 exit(1);
181                 }
182 #else
183                 rv = EC_POINT_set_affine_coordinates_GF2m(group, peer, x, y, c);
184 #endif
185
186         BN_CTX_free(c);
187         if (rv)
188                 return peer;
189         EC_POINT_free(peer);
190         return NULL;
191         }
192
193 static int ec_print_pubkey(FILE *out, EC_KEY *key)
194         {
195         const EC_POINT *pt;
196         const EC_GROUP *grp;
197         const EC_METHOD *meth;
198         int rv;
199         BIGNUM *tx, *ty;
200         BN_CTX *ctx;
201         ctx = BN_CTX_new();
202         if (!ctx)
203                 return 0;
204         tx = BN_CTX_get(ctx);
205         ty = BN_CTX_get(ctx);
206         if (!tx || !ty)
207                 return 0;
208         grp = EC_KEY_get0_group(key);
209         pt = EC_KEY_get0_public_key(key);
210         meth = EC_GROUP_method_of(grp);
211         if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
212                 rv = EC_POINT_get_affine_coordinates_GFp(grp, pt, tx, ty, ctx);
213         else
214 #ifdef OPENSSL_NO_EC2M
215                 {
216                 fprintf(stderr, "ERROR: GF2m not supported\n");
217                 exit(1);
218                 }
219 #else
220                 rv = EC_POINT_get_affine_coordinates_GF2m(grp, pt, tx, ty, ctx);
221 #endif
222
223         do_bn_print_name(out, "QeIUTx", tx);
224         do_bn_print_name(out, "QeIUTy", ty);
225
226         BN_CTX_free(ctx);
227
228         return rv;
229
230         }
231
232 static void ec_output_Zhash(FILE *out, int exout, EC_GROUP *group,
233                         BIGNUM *ix, BIGNUM *iy, BIGNUM *id, BIGNUM *cx,
234                         BIGNUM *cy, const EVP_MD *md,
235                                 unsigned char *rhash, size_t rhashlen)
236         {
237         EC_KEY *ec = NULL;
238         EC_POINT *peerkey = NULL;
239         unsigned char *Z;
240         unsigned char chash[EVP_MAX_MD_SIZE];
241         int Zlen;
242         ec = EC_KEY_new();
243         EC_KEY_set_group(ec, group);
244         peerkey = make_peer(group, cx, cy);
245         if (rhash == NULL)
246                 {
247                 rhashlen = M_EVP_MD_size(md);
248                 EC_KEY_generate_key(ec);
249                 ec_print_pubkey(out, ec);
250                 }
251         else
252                 {
253                 EC_KEY_set_public_key_affine_coordinates(ec, ix, iy);
254                 EC_KEY_set_private_key(ec, id);
255                 }
256         Zlen = (EC_GROUP_get_degree(group) + 7)/8;
257         Z = OPENSSL_malloc(Zlen);
258         if (!Z)
259                 exit(1);
260         ECDH_compute_key(Z, Zlen, peerkey, ec, 0);
261         if (exout)
262                 OutputValue("Z", Z, Zlen, out, 0);
263         FIPS_digest(Z, Zlen, chash, NULL, md);
264         OutputValue(rhash ? "IUTHashZZ" : "HashZZ", chash, rhashlen, out, 0);
265         if (rhash)
266                 {
267                 fprintf(out, "Result = %s\n",
268                                 memcmp(chash, rhash, rhashlen) ? "F" : "P");
269                 }
270         OPENSSL_cleanse(Z, Zlen);
271         OPENSSL_free(Z);
272         EC_KEY_free(ec);
273         EC_POINT_free(peerkey);
274         }
275                 
276 int main(int argc,char **argv)
277         {
278         char **args = argv + 1;
279         int argn = argc - 1;
280         FILE *in, *out;
281         char buf[2048], lbuf[2048];
282         unsigned char *rhash;
283         long rhashlen;
284         BIGNUM *cx = NULL, *cy = NULL;
285         BIGNUM *id = NULL, *ix = NULL, *iy = NULL;
286         const EVP_MD *md = NULL;
287         EC_GROUP *group = NULL;
288         char *keyword = NULL, *value = NULL;
289         int do_verify = -1, exout = 0;
290
291         int curve_nids[5] = {0,0,0,0,0};
292         int param_set = -1;
293
294         fips_set_error_print();
295         if(!FIPS_mode_set(1))
296                 exit(1);
297
298         if (argn && !strcmp(*args, "ecdhver"))
299                 {
300                 do_verify = 1;
301                 args++;
302                 argn--;
303                 }
304         else if (argn && !strcmp(*args, "ecdhgen"))
305                 {
306                 do_verify = 0;
307                 args++;
308                 argn--;
309                 }
310
311         if (argn && !strcmp(*args, "-exout"))
312                 {
313                 exout = 1;
314                 args++;
315                 argn--;
316                 }
317
318         if (do_verify == -1)
319                 {
320                 fprintf(stderr,"%s [ecdhver|ecdhgen|] [-exout] (infile outfile)\n",argv[0]);
321                 exit(1);
322                 }
323
324         if (argn == 2)
325                 {
326                 in = fopen(*args, "r");
327                 if (!in)
328                         {
329                         fprintf(stderr, "Error opening input file\n");
330                         exit(1);
331                         }
332                 out = fopen(args[1], "w");
333                 if (!out)
334                         {
335                         fprintf(stderr, "Error opening output file\n");
336                         exit(1);
337                         }
338                 }
339         else if (argn == 0)
340                 {
341                 in = stdin;
342                 out = stdout;
343                 }
344         else
345                 {
346                 fprintf(stderr,"%s [dhver|dhgen|] [-exout] (infile outfile)\n",argv[0]);
347                 exit(1);
348                 }
349
350         while (fgets(buf, sizeof(buf), in) != NULL)
351                 {
352                 fputs(buf, out);
353                 if (buf[0] == '[' && buf[1] == 'E')
354                         {
355                         int c = buf[2];
356                         if (c < 'A' || c > 'E')
357                                 goto parse_error;
358                         param_set = c - 'A';
359                         /* If just [E?] then initial paramset */
360                         if (buf[3] == ']')
361                                 continue;
362                         if (group)
363                                 EC_GROUP_free(group);
364                         group = EC_GROUP_new_by_curve_name(curve_nids[c - 'A']);
365                         }
366                 if (strlen(buf) > 10 && !strncmp(buf, "[Curve", 6))
367                         {
368                         int nid;
369                         if (param_set == -1)
370                                 goto parse_error;
371                         nid = lookup_curve(buf);
372                         if (nid == NID_undef)
373                                 goto parse_error;
374                         curve_nids[param_set] = nid;
375                         }
376
377                 if (strlen(buf) > 6 && !strncmp(buf, "[E", 2))
378                         {
379                         md = parse_md(buf);
380                         if (md == NULL)
381                                 goto parse_error;
382                         continue;
383                         }
384                 if (!parse_line(&keyword, &value, lbuf, buf))
385                         continue;
386                 if (!strcmp(keyword, "QeCAVSx"))
387                         {
388                         if (!do_hex2bn(&cx, value))
389                                 goto parse_error;
390                         }
391                 else if (!strcmp(keyword, "QeCAVSy"))
392                         {
393                         if (!do_hex2bn(&cy, value))
394                                 goto parse_error;
395                         if (do_verify == 0)
396                                 ec_output_Zhash(out, exout, group,
397                                                 NULL, NULL, NULL,
398                                                 cx, cy, md, rhash, rhashlen);
399                         }
400                 else if (!strcmp(keyword, "deIUT"))
401                         {
402                         if (!do_hex2bn(&id, value))
403                                 goto parse_error;
404                         }
405                 else if (!strcmp(keyword, "QeIUTx"))
406                         {
407                         if (!do_hex2bn(&ix, value))
408                                 goto parse_error;
409                         }
410                 else if (!strcmp(keyword, "QeIUTy"))
411                         {
412                         if (!do_hex2bn(&iy, value))
413                                 goto parse_error;
414                         }
415                 else if (!strcmp(keyword, "CAVSHashZZ"))
416                         {
417                         if (!md)
418                                 goto parse_error;
419                         rhash = hex2bin_m(value, &rhashlen);
420                         if (!rhash || rhashlen != M_EVP_MD_size(md))
421                                 goto parse_error;
422                         ec_output_Zhash(out, exout, group, ix, iy, id, cx, cy,
423                                         md, rhash, rhashlen);
424                         }
425                 }
426         return 0;
427         parse_error:
428         fprintf(stderr, "Error Parsing request file\n");
429         exit(1);
430         }
431
432 #endif