f67f07503afb133b67ca1293aee6dc209cf26dfe
[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 lookup_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                 rv = EC_POINT_get_affine_coordinates_GF2m(grp, pt, x, y, ctx);
173
174         BN_CTX_free(ctx);
175
176         return rv;
177
178         }
179
180 static int KeyPair(FILE *in, FILE *out)
181         {
182         char buf[2048], lbuf[2048];
183         char *keyword, *value;
184         int curve_nid = NID_undef;
185         int i, count;
186         BIGNUM *Qx = NULL, *Qy = NULL;
187         const BIGNUM *d = NULL;
188         EC_KEY *key = NULL;
189         Qx = BN_new();
190         Qy = BN_new();
191         while(fgets(buf, sizeof buf, in) != NULL)
192                 {
193                 if (*buf == '[' && buf[2] == '-')
194                         {
195                         if (buf[2] == '-')
196                         curve_nid = lookup_curve(buf, lbuf, NULL);
197                         fputs(buf, out);
198                         continue;
199                         }
200                 if (!parse_line(&keyword, &value, lbuf, buf))
201                         {
202                         fputs(buf, out);
203                         continue;
204                         }
205                 if (!strcmp(keyword, "N"))
206                         {
207                         count = atoi(value);
208
209                         for (i = 0; i < count; i++)
210                                 {
211
212                                 key = EC_KEY_new_by_curve_name(curve_nid);
213                                 if (!EC_KEY_generate_key(key))
214                                         {
215                                         fprintf(stderr, "Error generating key\n");
216                                         return 0;
217                                         }
218
219                                 if (!ec_get_pubkey(key, Qx, Qy))
220                                         {
221                                         fprintf(stderr, "Error getting public key\n");
222                                         return 0;
223                                         }
224
225                                 d = EC_KEY_get0_private_key(key);
226
227                                 do_bn_print_name(out, "d", d);
228                                 do_bn_print_name(out, "Qx", Qx);
229                                 do_bn_print_name(out, "Qy", Qy);
230                                 fputs("\n", out);
231                                 EC_KEY_free(key);
232
233                                 }
234
235                         }
236
237                 }
238         BN_free(Qx);
239         BN_free(Qy);
240         return 1;
241         }
242
243 static int PKV(FILE *in, FILE *out)
244         {
245
246         char buf[2048], lbuf[2048];
247         char *keyword, *value;
248         int curve_nid = NID_undef;
249         BIGNUM *Qx = NULL, *Qy = NULL;
250         EC_KEY *key = NULL;
251         while(fgets(buf, sizeof buf, in) != NULL)
252                 {
253                 fputs(buf, out);
254                 if (*buf == '[' && buf[2] == '-')
255                         {
256                         curve_nid = lookup_curve(buf, lbuf, NULL);
257                         if (curve_nid == NID_undef)
258                                 return 0;
259                                 
260                         }
261                 if (!parse_line(&keyword, &value, lbuf, buf))
262                         continue;
263                 if (!strcmp(keyword, "Qx"))
264                         {
265                         if (!do_hex2bn(&Qx, value))
266                                 {
267                                 fprintf(stderr, "Invalid Qx value\n");
268                                 return 0;
269                                 }
270                         }
271                 if (!strcmp(keyword, "Qy"))
272                         {
273                         int rv;
274                         if (!do_hex2bn(&Qy, value))
275                                 {
276                                 fprintf(stderr, "Invalid Qy value\n");
277                                 return 0;
278                                 }
279                         key = EC_KEY_new_by_curve_name(curve_nid);
280                         rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
281                         fprintf(out, "Result = %s\n", rv ? "P":"F");
282                         }
283
284                 }
285         return 1;
286         }
287
288 static int SigGen(FILE *in, FILE *out)
289         {
290         char buf[2048], lbuf[2048];
291         char *keyword, *value;
292         unsigned char *msg;
293         int curve_nid = NID_undef;
294         long mlen;
295         BIGNUM *Qx = NULL, *Qy = NULL;
296         EC_KEY *key = NULL;
297         ECDSA_SIG *sig = NULL;
298         const EVP_MD *digest = NULL;
299         EVP_MD_CTX mctx;
300         EVP_MD_CTX_init(&mctx);
301         Qx = BN_new();
302         Qy = BN_new();
303         while(fgets(buf, sizeof buf, in) != NULL)
304                 {
305                 fputs(buf, out);
306                 if (*buf == '[')
307                         {
308                         curve_nid = lookup_curve(buf, lbuf, &digest);
309                         if (curve_nid == NID_undef)
310                                 return 0;
311                         }
312                 if (!parse_line(&keyword, &value, lbuf, buf))
313                         continue;
314                 if (!strcmp(keyword, "Msg"))
315                         {
316                         msg = hex2bin_m(value, &mlen);
317                         if (!msg)
318                                 {
319                                 fprintf(stderr, "Invalid Message\n");
320                                 return 0;
321                                 }
322
323                         key = EC_KEY_new_by_curve_name(curve_nid);
324                         if (!EC_KEY_generate_key(key))
325                                 {
326                                 fprintf(stderr, "Error generating key\n");
327                                 return 0;
328                                 }
329
330                         if (!ec_get_pubkey(key, Qx, Qy))
331                                 {
332                                 fprintf(stderr, "Error getting public key\n");
333                                 return 0;
334                                 }
335
336                         FIPS_digestinit(&mctx, digest);
337                         FIPS_digestupdate(&mctx, msg, mlen);
338                         sig = FIPS_ecdsa_sign_ctx(key, &mctx);
339
340                         if (!sig)
341                                 {
342                                 fprintf(stderr, "Error signing message\n");
343                                 return 0;
344                                 }
345
346                         do_bn_print_name(out, "Qx", Qx);
347                         do_bn_print_name(out, "Qy", Qy);
348                         do_bn_print_name(out, "R", sig->r);
349                         do_bn_print_name(out, "S", sig->s);
350
351                         EC_KEY_free(key);
352
353                         FIPS_ecdsa_sig_free(sig);
354
355                         }
356
357                 }
358         BN_free(Qx);
359         BN_free(Qy);
360         FIPS_md_ctx_cleanup(&mctx);
361         return 1;
362         }
363
364 static int SigVer(FILE *in, FILE *out)
365         {
366         char buf[2048], lbuf[2048];
367         char *keyword, *value;
368         unsigned char *msg = NULL;
369         int curve_nid = NID_undef;
370         long mlen;
371         BIGNUM *Qx = NULL, *Qy = NULL;
372         EC_KEY *key = NULL;
373         ECDSA_SIG sg, *sig = &sg;
374         const EVP_MD *digest = NULL;
375         EVP_MD_CTX mctx;
376         EVP_MD_CTX_init(&mctx);
377         sig->r = NULL;
378         sig->s = NULL;
379         while(fgets(buf, sizeof buf, in) != NULL)
380                 {
381                 fputs(buf, out);
382                 if (*buf == '[')
383                         {
384                         curve_nid = lookup_curve(buf, lbuf, &digest);
385                         if (curve_nid == NID_undef)
386                                 return 0;
387                         }
388                 if (!parse_line(&keyword, &value, lbuf, buf))
389                         continue;
390                 if (!strcmp(keyword, "Msg"))
391                         {
392                         msg = hex2bin_m(value, &mlen);
393                         if (!msg)
394                                 {
395                                 fprintf(stderr, "Invalid Message\n");
396                                 return 0;
397                                 }
398                         }
399                         
400                 if (!strcmp(keyword, "Qx"))
401                         {
402                         if (!do_hex2bn(&Qx, value))
403                                 {
404                                 fprintf(stderr, "Invalid Qx value\n");
405                                 return 0;
406                                 }
407                         }
408                 if (!strcmp(keyword, "Qy"))
409                         {
410                         if (!do_hex2bn(&Qy, value))
411                                 {
412                                 fprintf(stderr, "Invalid Qy value\n");
413                                 return 0;
414                                 }
415                         }
416                 if (!strcmp(keyword, "R"))
417                         {
418                         if (!do_hex2bn(&sig->r, value))
419                                 {
420                                 fprintf(stderr, "Invalid R value\n");
421                                 return 0;
422                                 }
423                         }
424                 if (!strcmp(keyword, "S"))
425                         {
426                         int rv;
427                         if (!do_hex2bn(&sig->s, value))
428                                 {
429                                 fprintf(stderr, "Invalid S value\n");
430                                 return 0;
431                                 }
432                         key = EC_KEY_new_by_curve_name(curve_nid);
433                         rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
434
435                         if (rv != 1)
436                                 {
437                                 fprintf(stderr, "Error setting public key\n");
438                                 return 0;
439                                 }
440
441                         FIPS_digestinit(&mctx, digest);
442                         FIPS_digestupdate(&mctx, msg, mlen);
443                         no_err = 1;
444                         rv = FIPS_ecdsa_verify_ctx(key, &mctx, sig);
445                         no_err = 0;
446
447                         fprintf(out, "Result = %s\n", rv ? "P":"F");
448                         }
449
450                 }
451         return 1;
452         }
453
454 int main(int argc, char **argv)
455         {
456         FILE *in, *out;
457         const char *cmd = argv[1];
458         int rv = 0;
459         fips_set_error_print();
460         if (!FIPS_mode_set(1))
461                 exit(1);
462
463         if (argc == 4)
464                 {
465                 in = fopen(argv[2], "r");
466                 if (!in)
467                         {
468                         fprintf(stderr, "Error opening input file\n");
469                         exit(1);
470                         }
471                 out = fopen(argv[3], "w");
472                 if (!out)
473                         {
474                         fprintf(stderr, "Error opening output file\n");
475                         exit(1);
476                         }
477                 }
478         else if (argc == 2)
479                 {
480                 in = stdin;
481                 out = stdout;
482                 }
483
484         if (!cmd)
485                 {
486                 fprintf(stderr, "fips_ecdsavs [KeyPair|PKV|SigGen|SigVer]\n");
487                 return 1;
488                 }
489         if (!strcmp(cmd, "KeyPair"))
490                 rv = KeyPair(in, out);
491         else if (!strcmp(cmd, "PKV"))
492                 rv = PKV(in, out);
493         else if (!strcmp(cmd, "SigVer"))
494                 rv = SigVer(in, out);
495         else if (!strcmp(cmd, "SigGen"))
496                 rv = SigGen(in, out);
497         else
498                 {
499                 fprintf(stderr, "Unknown command %s\n", cmd);
500                 return 1;
501                 }
502
503         if (argc == 4)
504                 {
505                 fclose(in);
506                 fclose(out);
507                 }
508
509         if (rv <= 0)
510                 {
511                 fprintf(stderr, "Error running %s\n", cmd);
512                 return 1;
513                 }
514
515         return 0;
516         }
517
518 #endif