Add single call public key sign and verify functions.
[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         Qx = BN_new();
312         Qy = BN_new();
313         while(fgets(buf, sizeof buf, in) != NULL)
314                 {
315                 fputs(buf, out);
316                 if (*buf == '[')
317                         {
318                         curve_nid = elookup_curve(buf, lbuf, &digest);
319                         if (curve_nid == NID_undef)
320                                 return 0;
321                         }
322                 if (!parse_line(&keyword, &value, lbuf, buf))
323                         continue;
324                 if (!strcmp(keyword, "Msg"))
325                         {
326                         msg = hex2bin_m(value, &mlen);
327                         if (!msg)
328                                 {
329                                 fprintf(stderr, "Invalid Message\n");
330                                 return 0;
331                                 }
332
333                         key = EC_KEY_new_by_curve_name(curve_nid);
334                         if (!EC_KEY_generate_key(key))
335                                 {
336                                 fprintf(stderr, "Error generating key\n");
337                                 return 0;
338                                 }
339
340                         if (!ec_get_pubkey(key, Qx, Qy))
341                                 {
342                                 fprintf(stderr, "Error getting public key\n");
343                                 return 0;
344                                 }
345
346                         sig = FIPS_ecdsa_sign(key, msg, mlen, digest);
347
348                         if (!sig)
349                                 {
350                                 fprintf(stderr, "Error signing message\n");
351                                 return 0;
352                                 }
353
354                         do_bn_print_name(out, "Qx", Qx);
355                         do_bn_print_name(out, "Qy", Qy);
356                         do_bn_print_name(out, "R", sig->r);
357                         do_bn_print_name(out, "S", sig->s);
358
359                         EC_KEY_free(key);
360                         OPENSSL_free(msg);
361                         FIPS_ecdsa_sig_free(sig);
362
363                         }
364
365                 }
366         BN_free(Qx);
367         BN_free(Qy);
368         return 1;
369         }
370
371 static int SigVer(FILE *in, FILE *out)
372         {
373         char buf[2048], lbuf[2048];
374         char *keyword, *value;
375         unsigned char *msg = NULL;
376         int curve_nid = NID_undef;
377         long mlen;
378         BIGNUM *Qx = NULL, *Qy = NULL;
379         EC_KEY *key = NULL;
380         ECDSA_SIG sg, *sig = &sg;
381         const EVP_MD *digest = NULL;
382         sig->r = NULL;
383         sig->s = NULL;
384         while(fgets(buf, sizeof buf, in) != NULL)
385                 {
386                 fputs(buf, out);
387                 if (*buf == '[')
388                         {
389                         curve_nid = elookup_curve(buf, lbuf, &digest);
390                         if (curve_nid == NID_undef)
391                                 return 0;
392                         }
393                 if (!parse_line(&keyword, &value, lbuf, buf))
394                         continue;
395                 if (!strcmp(keyword, "Msg"))
396                         {
397                         msg = hex2bin_m(value, &mlen);
398                         if (!msg)
399                                 {
400                                 fprintf(stderr, "Invalid Message\n");
401                                 return 0;
402                                 }
403                         }
404                         
405                 if (!strcmp(keyword, "Qx"))
406                         {
407                         if (!do_hex2bn(&Qx, value))
408                                 {
409                                 fprintf(stderr, "Invalid Qx value\n");
410                                 return 0;
411                                 }
412                         }
413                 if (!strcmp(keyword, "Qy"))
414                         {
415                         if (!do_hex2bn(&Qy, value))
416                                 {
417                                 fprintf(stderr, "Invalid Qy value\n");
418                                 return 0;
419                                 }
420                         }
421                 if (!strcmp(keyword, "R"))
422                         {
423                         if (!do_hex2bn(&sig->r, value))
424                                 {
425                                 fprintf(stderr, "Invalid R value\n");
426                                 return 0;
427                                 }
428                         }
429                 if (!strcmp(keyword, "S"))
430                         {
431                         int rv;
432                         if (!do_hex2bn(&sig->s, value))
433                                 {
434                                 fprintf(stderr, "Invalid S value\n");
435                                 return 0;
436                                 }
437                         key = EC_KEY_new_by_curve_name(curve_nid);
438                         rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
439
440                         if (rv != 1)
441                                 {
442                                 fprintf(stderr, "Error setting public key\n");
443                                 return 0;
444                                 }
445
446                         no_err = 1;
447                         rv = FIPS_ecdsa_verify(key, msg, mlen, digest, sig);
448                         EC_KEY_free(key);
449                         if (msg)
450                                 OPENSSL_free(msg);
451                         no_err = 0;
452
453                         fprintf(out, "Result = %s" RESP_EOL, rv ? "P":"F");
454                         }
455
456                 }
457         if (sig->r)
458                 BN_free(sig->r);
459         if (sig->s)
460                 BN_free(sig->s);
461         if (Qx)
462                 BN_free(Qx);
463         if (Qy)
464                 BN_free(Qy);
465         return 1;
466         }
467 #ifdef FIPS_ALGVS
468 int fips_ecdsavs_main(int argc, char **argv)
469 #else
470 int main(int argc, char **argv)
471 #endif
472         {
473         FILE *in = NULL, *out = NULL;
474         const char *cmd = argv[1];
475         int rv = 0;
476         fips_algtest_init();
477
478         if (argc == 4)
479                 {
480                 in = fopen(argv[2], "r");
481                 if (!in)
482                         {
483                         fprintf(stderr, "Error opening input file\n");
484                         exit(1);
485                         }
486                 out = fopen(argv[3], "w");
487                 if (!out)
488                         {
489                         fprintf(stderr, "Error opening output file\n");
490                         exit(1);
491                         }
492                 }
493         else if (argc == 2)
494                 {
495                 in = stdin;
496                 out = stdout;
497                 }
498
499         if (!cmd)
500                 {
501                 fprintf(stderr, "fips_ecdsavs [KeyPair|PKV|SigGen|SigVer]\n");
502                 return 1;
503                 }
504         if (!strcmp(cmd, "KeyPair"))
505                 rv = KeyPair(in, out);
506         else if (!strcmp(cmd, "PKV"))
507                 rv = PKV(in, out);
508         else if (!strcmp(cmd, "SigVer"))
509                 rv = SigVer(in, out);
510         else if (!strcmp(cmd, "SigGen"))
511                 rv = SigGen(in, out);
512         else
513                 {
514                 fprintf(stderr, "Unknown command %s\n", cmd);
515                 return 1;
516                 }
517
518         if (argc == 4)
519                 {
520                 fclose(in);
521                 fclose(out);
522                 }
523
524         if (rv <= 0)
525                 {
526                 fprintf(stderr, "Error running %s\n", cmd);
527                 return 1;
528                 }
529
530         return 0;
531         }
532
533 #endif