35ff251060439ec53f82003a0668a487f7c3487f
[openssl.git] / fips / ecdsa / fips_ecdsavs.c
1 /* fips/ecdsa/fips_ecdsavs.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 #define OPENSSL_FIPSAPI
55 #include <openssl/opensslconf.h>
56 #include <stdio.h>
57
58 #ifndef OPENSSL_FIPS
59
60 int main(int argc, char **argv)
61 {
62     printf("No FIPS ECDSA support\n");
63     return(0);
64 }
65 #else
66
67 #include <string.h>
68 #include <ctype.h>
69 #include <openssl/err.h>
70 #include <openssl/bn.h>
71 #include <openssl/ecdsa.h>
72 #include <openssl/evp.h>
73 #include "fips_utl.h"
74
75 #include <openssl/objects.h>
76
77
78 static int elookup_curve(char *in, char *curve_name, const EVP_MD **pmd)
79         {
80         char *cname, *p;
81         /* Copy buffer as we will change it */
82         strcpy(curve_name, in);
83         cname = curve_name + 1;
84         p = strchr(cname, ']');
85         if (!p)
86                 {
87                 fprintf(stderr, "Parse error: missing ]\n");
88                 return NID_undef;
89                 }
90         *p = 0;
91         p = strchr(cname, ',');
92         if (p)
93                 {
94                 if (!pmd)
95                         {
96                         fprintf(stderr, "Parse error: unexpected digest\n");
97                         return NID_undef;
98                         }
99                 *p = 0;
100                 p++;
101
102                 if (!strcmp(p, "SHA-1"))
103                         *pmd = EVP_sha1();
104                 else if (!strcmp(p, "SHA-224"))
105                         *pmd = EVP_sha224();
106                 else if (!strcmp(p, "SHA-256"))
107                         *pmd = EVP_sha256();
108                 else if (!strcmp(p, "SHA-384"))
109                         *pmd = EVP_sha384();
110                 else if (!strcmp(p, "SHA-512"))
111                         *pmd = EVP_sha512();
112                 else
113                         {
114                         fprintf(stderr, "Unknown digest %s\n", p);
115                         return NID_undef;
116                         }
117                 }
118         else if(pmd)
119                 *pmd = EVP_sha1();
120
121         if (!strcmp(cname, "B-163"))
122                 return NID_sect163r2;
123         if (!strcmp(cname, "B-233"))
124                 return NID_sect233r1;
125         if (!strcmp(cname, "B-283"))
126                 return NID_sect283r1;
127         if (!strcmp(cname, "B-409"))
128                 return NID_sect409r1;
129         if (!strcmp(cname, "B-571"))
130                 return NID_sect571r1;
131         if (!strcmp(cname, "K-163"))
132                 return NID_sect163k1;
133         if (!strcmp(cname, "K-233"))
134                 return NID_sect233k1;
135         if (!strcmp(cname, "K-283"))
136                 return NID_sect283k1;
137         if (!strcmp(cname, "K-409"))
138                 return NID_sect409k1;
139         if (!strcmp(cname, "K-571"))
140                 return NID_sect571k1;
141         if (!strcmp(cname, "P-192"))
142                 return NID_X9_62_prime192v1;
143         if (!strcmp(cname, "P-224"))
144                 return NID_secp224r1;
145         if (!strcmp(cname, "P-256"))
146                 return NID_X9_62_prime256v1;
147         if (!strcmp(cname, "P-384"))
148                 return NID_secp384r1;
149         if (!strcmp(cname, "P-521"))
150                 return NID_secp521r1;
151
152         fprintf(stderr, "Unknown Curve name %s\n", cname);
153         return NID_undef;
154         }
155
156 static int ec_get_pubkey(EC_KEY *key, BIGNUM *x, BIGNUM *y)
157         {
158         const EC_POINT *pt;
159         const EC_GROUP *grp;
160         const EC_METHOD *meth;
161         int rv;
162         BN_CTX *ctx;
163         ctx = BN_CTX_new();
164         if (!ctx)
165                 return 0;
166         grp = EC_KEY_get0_group(key);
167         pt = EC_KEY_get0_public_key(key);
168         meth = EC_GROUP_method_of(grp);
169         if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
170                 rv = EC_POINT_get_affine_coordinates_GFp(grp, pt, x, y, ctx);
171         else
172 #ifdef OPENSSL_NO_EC2M
173                 {
174                 fprintf(stderr, "ERROR: GF2m not supported\n");
175                 exit(1);
176                 }
177 #else
178                 rv = EC_POINT_get_affine_coordinates_GF2m(grp, pt, x, y, ctx);
179 #endif
180
181         BN_CTX_free(ctx);
182
183         return rv;
184
185         }
186
187 static int KeyPair(FILE *in, FILE *out)
188         {
189         char buf[2048], lbuf[2048];
190         char *keyword, *value;
191         int curve_nid = NID_undef;
192         int i, count;
193         BIGNUM *Qx = NULL, *Qy = NULL;
194         const BIGNUM *d = NULL;
195         EC_KEY *key = NULL;
196         Qx = BN_new();
197         Qy = BN_new();
198         while(fgets(buf, sizeof buf, in) != NULL)
199                 {
200                 if (*buf == '[' && buf[2] == '-')
201                         {
202                         if (buf[2] == '-')
203                         curve_nid = elookup_curve(buf, lbuf, NULL);
204                         fputs(buf, out);
205                         continue;
206                         }
207                 if (!parse_line(&keyword, &value, lbuf, buf))
208                         {
209                         fputs(buf, out);
210                         continue;
211                         }
212                 if (!strcmp(keyword, "N"))
213                         {
214                         count = atoi(value);
215
216                         for (i = 0; i < count; i++)
217                                 {
218
219                                 key = EC_KEY_new_by_curve_name(curve_nid);
220                                 if (!EC_KEY_generate_key(key))
221                                         {
222                                         fprintf(stderr, "Error generating key\n");
223                                         return 0;
224                                         }
225
226                                 if (!ec_get_pubkey(key, Qx, Qy))
227                                         {
228                                         fprintf(stderr, "Error getting public key\n");
229                                         return 0;
230                                         }
231
232                                 d = EC_KEY_get0_private_key(key);
233
234                                 do_bn_print_name(out, "d", d);
235                                 do_bn_print_name(out, "Qx", Qx);
236                                 do_bn_print_name(out, "Qy", Qy);
237                                 fputs(RESP_EOL, out);
238                                 EC_KEY_free(key);
239
240                                 }
241
242                         }
243
244                 }
245         BN_free(Qx);
246         BN_free(Qy);
247         return 1;
248         }
249
250 static int PKV(FILE *in, FILE *out)
251         {
252
253         char buf[2048], lbuf[2048];
254         char *keyword, *value;
255         int curve_nid = NID_undef;
256         BIGNUM *Qx = NULL, *Qy = NULL;
257         EC_KEY *key = NULL;
258         while(fgets(buf, sizeof buf, in) != NULL)
259                 {
260                 fputs(buf, out);
261                 if (*buf == '[' && buf[2] == '-')
262                         {
263                         curve_nid = elookup_curve(buf, lbuf, NULL);
264                         if (curve_nid == NID_undef)
265                                 return 0;
266                                 
267                         }
268                 if (!parse_line(&keyword, &value, lbuf, buf))
269                         continue;
270                 if (!strcmp(keyword, "Qx"))
271                         {
272                         if (!do_hex2bn(&Qx, value))
273                                 {
274                                 fprintf(stderr, "Invalid Qx value\n");
275                                 return 0;
276                                 }
277                         }
278                 if (!strcmp(keyword, "Qy"))
279                         {
280                         int rv;
281                         if (!do_hex2bn(&Qy, value))
282                                 {
283                                 fprintf(stderr, "Invalid Qy value\n");
284                                 return 0;
285                                 }
286                         key = EC_KEY_new_by_curve_name(curve_nid);
287                         no_err = 1;
288                         rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
289                         no_err = 0;
290                         EC_KEY_free(key);
291                         fprintf(out, "Result = %s" RESP_EOL, rv ? "P":"F");
292                         }
293
294                 }
295         BN_free(Qx);
296         BN_free(Qy);
297         return 1;
298         }
299
300 static int SigGen(FILE *in, FILE *out)
301         {
302         char buf[2048], lbuf[2048];
303         char *keyword, *value;
304         unsigned char *msg;
305         int curve_nid = NID_undef;
306         long mlen;
307         BIGNUM *Qx = NULL, *Qy = NULL;
308         EC_KEY *key = NULL;
309         ECDSA_SIG *sig = NULL;
310         const EVP_MD *digest = NULL;
311         EVP_MD_CTX mctx;
312         EVP_MD_CTX_init(&mctx);
313         Qx = BN_new();
314         Qy = BN_new();
315         while(fgets(buf, sizeof buf, in) != NULL)
316                 {
317                 fputs(buf, out);
318                 if (*buf == '[')
319                         {
320                         curve_nid = elookup_curve(buf, lbuf, &digest);
321                         if (curve_nid == NID_undef)
322                                 return 0;
323                         }
324                 if (!parse_line(&keyword, &value, lbuf, buf))
325                         continue;
326                 if (!strcmp(keyword, "Msg"))
327                         {
328                         msg = hex2bin_m(value, &mlen);
329                         if (!msg)
330                                 {
331                                 fprintf(stderr, "Invalid Message\n");
332                                 return 0;
333                                 }
334
335                         key = EC_KEY_new_by_curve_name(curve_nid);
336                         if (!EC_KEY_generate_key(key))
337                                 {
338                                 fprintf(stderr, "Error generating key\n");
339                                 return 0;
340                                 }
341
342                         if (!ec_get_pubkey(key, Qx, Qy))
343                                 {
344                                 fprintf(stderr, "Error getting public key\n");
345                                 return 0;
346                                 }
347
348                         FIPS_digestinit(&mctx, digest);
349                         FIPS_digestupdate(&mctx, msg, mlen);
350                         sig = FIPS_ecdsa_sign_ctx(key, &mctx);
351
352                         if (!sig)
353                                 {
354                                 fprintf(stderr, "Error signing message\n");
355                                 return 0;
356                                 }
357
358                         do_bn_print_name(out, "Qx", Qx);
359                         do_bn_print_name(out, "Qy", Qy);
360                         do_bn_print_name(out, "R", sig->r);
361                         do_bn_print_name(out, "S", sig->s);
362
363                         EC_KEY_free(key);
364                         OPENSSL_free(msg);
365                         FIPS_ecdsa_sig_free(sig);
366
367                         }
368
369                 }
370         BN_free(Qx);
371         BN_free(Qy);
372         FIPS_md_ctx_cleanup(&mctx);
373         return 1;
374         }
375
376 static int SigVer(FILE *in, FILE *out)
377         {
378         char buf[2048], lbuf[2048];
379         char *keyword, *value;
380         unsigned char *msg = NULL;
381         int curve_nid = NID_undef;
382         long mlen;
383         BIGNUM *Qx = NULL, *Qy = NULL;
384         EC_KEY *key = NULL;
385         ECDSA_SIG sg, *sig = &sg;
386         const EVP_MD *digest = NULL;
387         EVP_MD_CTX mctx;
388         EVP_MD_CTX_init(&mctx);
389         sig->r = NULL;
390         sig->s = NULL;
391         while(fgets(buf, sizeof buf, in) != NULL)
392                 {
393                 fputs(buf, out);
394                 if (*buf == '[')
395                         {
396                         curve_nid = elookup_curve(buf, lbuf, &digest);
397                         if (curve_nid == NID_undef)
398                                 return 0;
399                         }
400                 if (!parse_line(&keyword, &value, lbuf, buf))
401                         continue;
402                 if (!strcmp(keyword, "Msg"))
403                         {
404                         msg = hex2bin_m(value, &mlen);
405                         if (!msg)
406                                 {
407                                 fprintf(stderr, "Invalid Message\n");
408                                 return 0;
409                                 }
410                         }
411                         
412                 if (!strcmp(keyword, "Qx"))
413                         {
414                         if (!do_hex2bn(&Qx, value))
415                                 {
416                                 fprintf(stderr, "Invalid Qx value\n");
417                                 return 0;
418                                 }
419                         }
420                 if (!strcmp(keyword, "Qy"))
421                         {
422                         if (!do_hex2bn(&Qy, value))
423                                 {
424                                 fprintf(stderr, "Invalid Qy value\n");
425                                 return 0;
426                                 }
427                         }
428                 if (!strcmp(keyword, "R"))
429                         {
430                         if (!do_hex2bn(&sig->r, value))
431                                 {
432                                 fprintf(stderr, "Invalid R value\n");
433                                 return 0;
434                                 }
435                         }
436                 if (!strcmp(keyword, "S"))
437                         {
438                         int rv;
439                         if (!do_hex2bn(&sig->s, value))
440                                 {
441                                 fprintf(stderr, "Invalid S value\n");
442                                 return 0;
443                                 }
444                         key = EC_KEY_new_by_curve_name(curve_nid);
445                         rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
446
447                         if (rv != 1)
448                                 {
449                                 fprintf(stderr, "Error setting public key\n");
450                                 return 0;
451                                 }
452
453                         FIPS_digestinit(&mctx, digest);
454                         FIPS_digestupdate(&mctx, msg, mlen);
455                         no_err = 1;
456                         rv = FIPS_ecdsa_verify_ctx(key, &mctx, sig);
457                         EC_KEY_free(key);
458                         if (msg)
459                                 OPENSSL_free(msg);
460                         no_err = 0;
461
462                         fprintf(out, "Result = %s" RESP_EOL, rv ? "P":"F");
463                         }
464
465                 }
466         if (sig->r)
467                 BN_free(sig->r);
468         if (sig->s)
469                 BN_free(sig->s);
470         if (Qx)
471                 BN_free(Qx);
472         if (Qy)
473                 BN_free(Qy);
474         EVP_MD_CTX_cleanup(&mctx);
475         return 1;
476         }
477 #ifdef FIPS_ALGVS
478 int fips_ecdsavs_main(int argc, char **argv)
479 #else
480 int main(int argc, char **argv)
481 #endif
482         {
483         FILE *in = NULL, *out = NULL;
484         const char *cmd = argv[1];
485         int rv = 0;
486         fips_algtest_init();
487
488         if (argc == 4)
489                 {
490                 in = fopen(argv[2], "r");
491                 if (!in)
492                         {
493                         fprintf(stderr, "Error opening input file\n");
494                         exit(1);
495                         }
496                 out = fopen(argv[3], "w");
497                 if (!out)
498                         {
499                         fprintf(stderr, "Error opening output file\n");
500                         exit(1);
501                         }
502                 }
503         else if (argc == 2)
504                 {
505                 in = stdin;
506                 out = stdout;
507                 }
508
509         if (!cmd)
510                 {
511                 fprintf(stderr, "fips_ecdsavs [KeyPair|PKV|SigGen|SigVer]\n");
512                 return 1;
513                 }
514         if (!strcmp(cmd, "KeyPair"))
515                 rv = KeyPair(in, out);
516         else if (!strcmp(cmd, "PKV"))
517                 rv = PKV(in, out);
518         else if (!strcmp(cmd, "SigVer"))
519                 rv = SigVer(in, out);
520         else if (!strcmp(cmd, "SigGen"))
521                 rv = SigGen(in, out);
522         else
523                 {
524                 fprintf(stderr, "Unknown command %s\n", cmd);
525                 return 1;
526                 }
527
528         if (argc == 4)
529                 {
530                 fclose(in);
531                 fclose(out);
532                 }
533
534         if (rv <= 0)
535                 {
536                 fprintf(stderr, "Error running %s\n", cmd);
537                 return 1;
538                 }
539
540         return 0;
541         }
542
543 #endif