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