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