1 #define OPENSSL_FIPSAPI
2 #include <openssl/opensslconf.h>
7 int main(int argc, char **argv)
9 printf("No FIPS ECDSA support\n");
16 #include <openssl/err.h>
17 #include <openssl/bn.h>
18 #include <openssl/ecdsa.h>
19 #include <openssl/evp.h>
22 #include <openssl/objects.h>
25 static int lookup_curve(char *in, char *curve_name, const EVP_MD **pmd)
28 /* Copy buffer as we will change it */
29 strcpy(curve_name, in);
30 cname = curve_name + 1;
31 p = strchr(cname, ']');
34 fprintf(stderr, "Parse error: missing ]\n");
38 p = strchr(cname, ',');
43 fprintf(stderr, "Parse error: unexpected digest\n");
49 if (!strcmp(p, "SHA-1"))
51 else if (!strcmp(p, "SHA-224"))
53 else if (!strcmp(p, "SHA-256"))
55 else if (!strcmp(p, "SHA-384"))
57 else if (!strcmp(p, "SHA-512"))
61 fprintf(stderr, "Unknown digest %s\n", p);
68 if (!strcmp(cname, "B-163"))
70 if (!strcmp(cname, "B-233"))
72 if (!strcmp(cname, "B-283"))
74 if (!strcmp(cname, "B-409"))
76 if (!strcmp(cname, "B-571"))
78 if (!strcmp(cname, "K-163"))
80 if (!strcmp(cname, "K-233"))
82 if (!strcmp(cname, "K-283"))
84 if (!strcmp(cname, "K-409"))
86 if (!strcmp(cname, "K-571"))
88 if (!strcmp(cname, "P-192"))
89 return NID_X9_62_prime192v1;
90 if (!strcmp(cname, "P-224"))
92 if (!strcmp(cname, "P-256"))
93 return NID_X9_62_prime256v1;
94 if (!strcmp(cname, "P-384"))
96 if (!strcmp(cname, "P-521"))
99 fprintf(stderr, "Unknown Curve name %s\n", cname);
103 static int ec_get_pubkey(EC_KEY *key, BIGNUM *x, BIGNUM *y)
107 const EC_METHOD *meth;
113 grp = EC_KEY_get0_group(key);
114 pt = EC_KEY_get0_public_key(key);
115 meth = EC_GROUP_method_of(grp);
116 if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
117 rv = EC_POINT_get_affine_coordinates_GFp(grp, pt, x, y, ctx);
119 rv = EC_POINT_get_affine_coordinates_GF2m(grp, pt, x, y, ctx);
127 static int KeyPair(void)
129 char buf[2048], lbuf[2048];
130 char *keyword, *value;
131 int curve_nid = NID_undef;
133 BIGNUM *Qx = NULL, *Qy = NULL;
134 const BIGNUM *d = NULL;
138 while(fgets(buf, sizeof buf, stdin) != NULL)
140 if (*buf == '[' && buf[2] == '-')
143 curve_nid = lookup_curve(buf, lbuf, NULL);
147 if (!parse_line(&keyword, &value, lbuf, buf))
152 if (!strcmp(keyword, "N"))
156 for (i = 0; i < count; i++)
159 key = EC_KEY_new_by_curve_name(curve_nid);
160 if (!EC_KEY_generate_key(key))
162 fprintf(stderr, "Error generating key\n");
166 if (!ec_get_pubkey(key, Qx, Qy))
168 fprintf(stderr, "Error getting public key\n");
172 d = EC_KEY_get0_private_key(key);
174 do_bn_print_name(stdout, "d", d);
175 do_bn_print_name(stdout, "Qx", Qx);
176 do_bn_print_name(stdout, "Qy", Qy);
193 char buf[2048], lbuf[2048];
194 char *keyword, *value;
195 int curve_nid = NID_undef;
196 BIGNUM *Qx = NULL, *Qy = NULL;
198 while(fgets(buf, sizeof buf, stdin) != NULL)
201 if (*buf == '[' && buf[2] == '-')
203 curve_nid = lookup_curve(buf, lbuf, NULL);
204 if (curve_nid == NID_undef)
208 if (!parse_line(&keyword, &value, lbuf, buf))
210 if (!strcmp(keyword, "Qx"))
212 if (!do_hex2bn(&Qx, value))
214 fprintf(stderr, "Invalid Qx value\n");
218 if (!strcmp(keyword, "Qy"))
221 if (!do_hex2bn(&Qy, value))
223 fprintf(stderr, "Invalid Qy value\n");
226 key = EC_KEY_new_by_curve_name(curve_nid);
227 rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
228 printf("Result = %s\n", rv ? "P":"F");
235 static int SigGen(void)
237 char buf[2048], lbuf[2048];
238 char *keyword, *value;
240 int curve_nid = NID_undef;
242 BIGNUM *Qx = NULL, *Qy = NULL;
244 ECDSA_SIG *sig = NULL;
245 const EVP_MD *digest = NULL;
247 EVP_MD_CTX_init(&mctx);
250 while(fgets(buf, sizeof buf, stdin) != NULL)
255 curve_nid = lookup_curve(buf, lbuf, &digest);
256 if (curve_nid == NID_undef)
259 if (!parse_line(&keyword, &value, lbuf, buf))
261 if (!strcmp(keyword, "Msg"))
263 msg = hex2bin_m(value, &mlen);
266 fprintf(stderr, "Invalid Message\n");
270 key = EC_KEY_new_by_curve_name(curve_nid);
271 if (!EC_KEY_generate_key(key))
273 fprintf(stderr, "Error generating key\n");
277 if (!ec_get_pubkey(key, Qx, Qy))
279 fprintf(stderr, "Error getting public key\n");
283 FIPS_digestinit(&mctx, digest);
284 FIPS_digestupdate(&mctx, msg, mlen);
285 sig = FIPS_ecdsa_sign_ctx(key, &mctx);
289 fprintf(stderr, "Error signing message\n");
293 do_bn_print_name(stdout, "Qx", Qx);
294 do_bn_print_name(stdout, "Qy", Qy);
295 do_bn_print_name(stdout, "R", sig->r);
296 do_bn_print_name(stdout, "S", sig->s);
300 FIPS_ecdsa_sig_free(sig);
307 FIPS_md_ctx_cleanup(&mctx);
311 static int SigVer(void)
313 char buf[2048], lbuf[2048];
314 char *keyword, *value;
316 int curve_nid = NID_undef;
318 BIGNUM *Qx = NULL, *Qy = NULL;
320 ECDSA_SIG sg, *sig = &sg;
321 const EVP_MD *digest = NULL;
323 EVP_MD_CTX_init(&mctx);
326 while(fgets(buf, sizeof buf, stdin) != NULL)
331 curve_nid = lookup_curve(buf, lbuf, &digest);
332 if (curve_nid == NID_undef)
335 if (!parse_line(&keyword, &value, lbuf, buf))
337 if (!strcmp(keyword, "Msg"))
339 msg = hex2bin_m(value, &mlen);
342 fprintf(stderr, "Invalid Message\n");
347 if (!strcmp(keyword, "Qx"))
349 if (!do_hex2bn(&Qx, value))
351 fprintf(stderr, "Invalid Qx value\n");
355 if (!strcmp(keyword, "Qy"))
357 if (!do_hex2bn(&Qy, value))
359 fprintf(stderr, "Invalid Qy value\n");
363 if (!strcmp(keyword, "R"))
365 if (!do_hex2bn(&sig->r, value))
367 fprintf(stderr, "Invalid R value\n");
371 if (!strcmp(keyword, "S"))
374 if (!do_hex2bn(&sig->s, value))
376 fprintf(stderr, "Invalid S value\n");
379 key = EC_KEY_new_by_curve_name(curve_nid);
380 rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
384 fprintf(stderr, "Error setting public key\n");
388 FIPS_digestinit(&mctx, digest);
389 FIPS_digestupdate(&mctx, msg, mlen);
391 rv = FIPS_ecdsa_verify_ctx(key, &mctx, sig);
394 printf("Result = %s\n", rv ? "P":"F");
401 int main(int argc, char **argv)
403 const char *cmd = argv[1];
405 fips_set_error_print();
408 fprintf(stderr, "fips_ecdsavs [KeyPair|PKV|SigGen|SigVer]\n");
411 if (!strcmp(cmd, "KeyPair"))
413 else if (!strcmp(cmd, "PKV"))
415 else if (!strcmp(cmd, "SigVer"))
417 else if (!strcmp(cmd, "SigGen"))
421 fprintf(stderr, "Unknown command %s\n", cmd);
426 fprintf(stderr, "Error running %s\n", cmd);