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 rv = EC_POINT_get_affine_coordinates_GF2m(grp, pt, x, y, ctx);
180 static int KeyPair(FILE *in, FILE *out)
182 char buf[2048], lbuf[2048];
183 char *keyword, *value;
184 int curve_nid = NID_undef;
186 BIGNUM *Qx = NULL, *Qy = NULL;
187 const BIGNUM *d = NULL;
191 while(fgets(buf, sizeof buf, in) != NULL)
193 if (*buf == '[' && buf[2] == '-')
196 curve_nid = lookup_curve(buf, lbuf, NULL);
200 if (!parse_line(&keyword, &value, lbuf, buf))
205 if (!strcmp(keyword, "N"))
209 for (i = 0; i < count; i++)
212 key = EC_KEY_new_by_curve_name(curve_nid);
213 if (!EC_KEY_generate_key(key))
215 fprintf(stderr, "Error generating key\n");
219 if (!ec_get_pubkey(key, Qx, Qy))
221 fprintf(stderr, "Error getting public key\n");
225 d = EC_KEY_get0_private_key(key);
227 do_bn_print_name(out, "d", d);
228 do_bn_print_name(out, "Qx", Qx);
229 do_bn_print_name(out, "Qy", Qy);
243 static int PKV(FILE *in, FILE *out)
246 char buf[2048], lbuf[2048];
247 char *keyword, *value;
248 int curve_nid = NID_undef;
249 BIGNUM *Qx = NULL, *Qy = NULL;
251 while(fgets(buf, sizeof buf, in) != NULL)
254 if (*buf == '[' && buf[2] == '-')
256 curve_nid = lookup_curve(buf, lbuf, NULL);
257 if (curve_nid == NID_undef)
261 if (!parse_line(&keyword, &value, lbuf, buf))
263 if (!strcmp(keyword, "Qx"))
265 if (!do_hex2bn(&Qx, value))
267 fprintf(stderr, "Invalid Qx value\n");
271 if (!strcmp(keyword, "Qy"))
274 if (!do_hex2bn(&Qy, value))
276 fprintf(stderr, "Invalid Qy value\n");
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");
288 static int SigGen(FILE *in, FILE *out)
290 char buf[2048], lbuf[2048];
291 char *keyword, *value;
293 int curve_nid = NID_undef;
295 BIGNUM *Qx = NULL, *Qy = NULL;
297 ECDSA_SIG *sig = NULL;
298 const EVP_MD *digest = NULL;
300 EVP_MD_CTX_init(&mctx);
303 while(fgets(buf, sizeof buf, in) != NULL)
308 curve_nid = lookup_curve(buf, lbuf, &digest);
309 if (curve_nid == NID_undef)
312 if (!parse_line(&keyword, &value, lbuf, buf))
314 if (!strcmp(keyword, "Msg"))
316 msg = hex2bin_m(value, &mlen);
319 fprintf(stderr, "Invalid Message\n");
323 key = EC_KEY_new_by_curve_name(curve_nid);
324 if (!EC_KEY_generate_key(key))
326 fprintf(stderr, "Error generating key\n");
330 if (!ec_get_pubkey(key, Qx, Qy))
332 fprintf(stderr, "Error getting public key\n");
336 FIPS_digestinit(&mctx, digest);
337 FIPS_digestupdate(&mctx, msg, mlen);
338 sig = FIPS_ecdsa_sign_ctx(key, &mctx);
342 fprintf(stderr, "Error signing message\n");
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);
353 FIPS_ecdsa_sig_free(sig);
360 FIPS_md_ctx_cleanup(&mctx);
364 static int SigVer(FILE *in, FILE *out)
366 char buf[2048], lbuf[2048];
367 char *keyword, *value;
368 unsigned char *msg = NULL;
369 int curve_nid = NID_undef;
371 BIGNUM *Qx = NULL, *Qy = NULL;
373 ECDSA_SIG sg, *sig = &sg;
374 const EVP_MD *digest = NULL;
376 EVP_MD_CTX_init(&mctx);
379 while(fgets(buf, sizeof buf, in) != NULL)
384 curve_nid = lookup_curve(buf, lbuf, &digest);
385 if (curve_nid == NID_undef)
388 if (!parse_line(&keyword, &value, lbuf, buf))
390 if (!strcmp(keyword, "Msg"))
392 msg = hex2bin_m(value, &mlen);
395 fprintf(stderr, "Invalid Message\n");
400 if (!strcmp(keyword, "Qx"))
402 if (!do_hex2bn(&Qx, value))
404 fprintf(stderr, "Invalid Qx value\n");
408 if (!strcmp(keyword, "Qy"))
410 if (!do_hex2bn(&Qy, value))
412 fprintf(stderr, "Invalid Qy value\n");
416 if (!strcmp(keyword, "R"))
418 if (!do_hex2bn(&sig->r, value))
420 fprintf(stderr, "Invalid R value\n");
424 if (!strcmp(keyword, "S"))
427 if (!do_hex2bn(&sig->s, value))
429 fprintf(stderr, "Invalid S value\n");
432 key = EC_KEY_new_by_curve_name(curve_nid);
433 rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
437 fprintf(stderr, "Error setting public key\n");
441 FIPS_digestinit(&mctx, digest);
442 FIPS_digestupdate(&mctx, msg, mlen);
444 rv = FIPS_ecdsa_verify_ctx(key, &mctx, sig);
447 fprintf(out, "Result = %s\n", rv ? "P":"F");
454 int main(int argc, char **argv)
457 const char *cmd = argv[1];
459 fips_set_error_print();
460 if (!FIPS_mode_set(1))
465 in = fopen(argv[2], "r");
468 fprintf(stderr, "Error opening input file\n");
471 out = fopen(argv[3], "w");
474 fprintf(stderr, "Error opening output file\n");
486 fprintf(stderr, "fips_ecdsavs [KeyPair|PKV|SigGen|SigVer]\n");
489 if (!strcmp(cmd, "KeyPair"))
490 rv = KeyPair(in, out);
491 else if (!strcmp(cmd, "PKV"))
493 else if (!strcmp(cmd, "SigVer"))
494 rv = SigVer(in, out);
495 else if (!strcmp(cmd, "SigGen"))
496 rv = SigGen(in, out);
499 fprintf(stderr, "Unknown command %s\n", cmd);
511 fprintf(stderr, "Error running %s\n", cmd);