1 /* fips/ecdsa/fips_ecdsavs.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
54 #define OPENSSL_FIPSAPI
55 #include <openssl/opensslconf.h>
60 int main(int argc, char **argv)
62 printf("No FIPS ECDSA support\n");
69 #include <openssl/err.h>
70 #include <openssl/bn.h>
71 #include <openssl/ecdsa.h>
72 #include <openssl/evp.h>
75 #include <openssl/objects.h>
78 static int lookup_curve(char *in, char *curve_name, const EVP_MD **pmd)
81 /* Copy buffer as we will change it */
82 strcpy(curve_name, in);
83 cname = curve_name + 1;
84 p = strchr(cname, ']');
87 fprintf(stderr, "Parse error: missing ]\n");
91 p = strchr(cname, ',');
96 fprintf(stderr, "Parse error: unexpected digest\n");
102 if (!strcmp(p, "SHA-1"))
104 else if (!strcmp(p, "SHA-224"))
106 else if (!strcmp(p, "SHA-256"))
108 else if (!strcmp(p, "SHA-384"))
110 else if (!strcmp(p, "SHA-512"))
114 fprintf(stderr, "Unknown digest %s\n", p);
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;
152 fprintf(stderr, "Unknown Curve name %s\n", cname);
156 static int ec_get_pubkey(EC_KEY *key, BIGNUM *x, BIGNUM *y)
160 const EC_METHOD *meth;
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);
172 #ifdef OPENSSL_NO_EC2M
174 fprintf(stderr, "ERROR: GF2m not supported\n");
178 rv = EC_POINT_get_affine_coordinates_GF2m(grp, pt, x, y, ctx);
187 static int KeyPair(FILE *in, FILE *out)
189 char buf[2048], lbuf[2048];
190 char *keyword, *value;
191 int curve_nid = NID_undef;
193 BIGNUM *Qx = NULL, *Qy = NULL;
194 const BIGNUM *d = NULL;
198 while(fgets(buf, sizeof buf, in) != NULL)
200 if (*buf == '[' && buf[2] == '-')
203 curve_nid = lookup_curve(buf, lbuf, NULL);
207 if (!parse_line(&keyword, &value, lbuf, buf))
212 if (!strcmp(keyword, "N"))
216 for (i = 0; i < count; i++)
219 key = EC_KEY_new_by_curve_name(curve_nid);
220 if (!EC_KEY_generate_key(key))
222 fprintf(stderr, "Error generating key\n");
226 if (!ec_get_pubkey(key, Qx, Qy))
228 fprintf(stderr, "Error getting public key\n");
232 d = EC_KEY_get0_private_key(key);
234 do_bn_print_name(out, "d", d);
235 do_bn_print_name(out, "Qx", Qx);
236 do_bn_print_name(out, "Qy", Qy);
250 static int PKV(FILE *in, FILE *out)
253 char buf[2048], lbuf[2048];
254 char *keyword, *value;
255 int curve_nid = NID_undef;
256 BIGNUM *Qx = NULL, *Qy = NULL;
258 while(fgets(buf, sizeof buf, in) != NULL)
261 if (*buf == '[' && buf[2] == '-')
263 curve_nid = lookup_curve(buf, lbuf, NULL);
264 if (curve_nid == NID_undef)
268 if (!parse_line(&keyword, &value, lbuf, buf))
270 if (!strcmp(keyword, "Qx"))
272 if (!do_hex2bn(&Qx, value))
274 fprintf(stderr, "Invalid Qx value\n");
278 if (!strcmp(keyword, "Qy"))
281 if (!do_hex2bn(&Qy, value))
283 fprintf(stderr, "Invalid Qy value\n");
286 key = EC_KEY_new_by_curve_name(curve_nid);
288 rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
290 fprintf(out, "Result = %s\n", rv ? "P":"F");
297 static int SigGen(FILE *in, FILE *out)
299 char buf[2048], lbuf[2048];
300 char *keyword, *value;
302 int curve_nid = NID_undef;
304 BIGNUM *Qx = NULL, *Qy = NULL;
306 ECDSA_SIG *sig = NULL;
307 const EVP_MD *digest = NULL;
309 EVP_MD_CTX_init(&mctx);
312 while(fgets(buf, sizeof buf, in) != NULL)
317 curve_nid = lookup_curve(buf, lbuf, &digest);
318 if (curve_nid == NID_undef)
321 if (!parse_line(&keyword, &value, lbuf, buf))
323 if (!strcmp(keyword, "Msg"))
325 msg = hex2bin_m(value, &mlen);
328 fprintf(stderr, "Invalid Message\n");
332 key = EC_KEY_new_by_curve_name(curve_nid);
333 if (!EC_KEY_generate_key(key))
335 fprintf(stderr, "Error generating key\n");
339 if (!ec_get_pubkey(key, Qx, Qy))
341 fprintf(stderr, "Error getting public key\n");
345 FIPS_digestinit(&mctx, digest);
346 FIPS_digestupdate(&mctx, msg, mlen);
347 sig = FIPS_ecdsa_sign_ctx(key, &mctx);
351 fprintf(stderr, "Error signing message\n");
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);
362 FIPS_ecdsa_sig_free(sig);
369 FIPS_md_ctx_cleanup(&mctx);
373 static int SigVer(FILE *in, FILE *out)
375 char buf[2048], lbuf[2048];
376 char *keyword, *value;
377 unsigned char *msg = NULL;
378 int curve_nid = NID_undef;
380 BIGNUM *Qx = NULL, *Qy = NULL;
382 ECDSA_SIG sg, *sig = &sg;
383 const EVP_MD *digest = NULL;
385 EVP_MD_CTX_init(&mctx);
388 while(fgets(buf, sizeof buf, in) != NULL)
393 curve_nid = lookup_curve(buf, lbuf, &digest);
394 if (curve_nid == NID_undef)
397 if (!parse_line(&keyword, &value, lbuf, buf))
399 if (!strcmp(keyword, "Msg"))
401 msg = hex2bin_m(value, &mlen);
404 fprintf(stderr, "Invalid Message\n");
409 if (!strcmp(keyword, "Qx"))
411 if (!do_hex2bn(&Qx, value))
413 fprintf(stderr, "Invalid Qx value\n");
417 if (!strcmp(keyword, "Qy"))
419 if (!do_hex2bn(&Qy, value))
421 fprintf(stderr, "Invalid Qy value\n");
425 if (!strcmp(keyword, "R"))
427 if (!do_hex2bn(&sig->r, value))
429 fprintf(stderr, "Invalid R value\n");
433 if (!strcmp(keyword, "S"))
436 if (!do_hex2bn(&sig->s, value))
438 fprintf(stderr, "Invalid S value\n");
441 key = EC_KEY_new_by_curve_name(curve_nid);
442 rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
446 fprintf(stderr, "Error setting public key\n");
450 FIPS_digestinit(&mctx, digest);
451 FIPS_digestupdate(&mctx, msg, mlen);
453 rv = FIPS_ecdsa_verify_ctx(key, &mctx, sig);
456 fprintf(out, "Result = %s\n", rv ? "P":"F");
463 int main(int argc, char **argv)
465 FILE *in = NULL, *out = NULL;
466 const char *cmd = argv[1];
472 in = fopen(argv[2], "r");
475 fprintf(stderr, "Error opening input file\n");
478 out = fopen(argv[3], "w");
481 fprintf(stderr, "Error opening output file\n");
493 fprintf(stderr, "fips_ecdsavs [KeyPair|PKV|SigGen|SigVer]\n");
496 if (!strcmp(cmd, "KeyPair"))
497 rv = KeyPair(in, out);
498 else if (!strcmp(cmd, "PKV"))
500 else if (!strcmp(cmd, "SigVer"))
501 rv = SigVer(in, out);
502 else if (!strcmp(cmd, "SigGen"))
503 rv = SigGen(in, out);
506 fprintf(stderr, "Unknown command %s\n", cmd);
518 fprintf(stderr, "Error running %s\n", cmd);