1 #define OPENSSL_FIPSAPI
2 #include <openssl/opensslconf.h>
7 int main(int argc, char **argv)
9 printf("No FIPS DSA 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(const char *curve_name)
28 strncpy(cname, curve_name, 5);
30 if (!strcmp(cname, "B-163"))
32 if (!strcmp(cname, "B-233"))
34 if (!strcmp(cname, "B-283"))
36 if (!strcmp(cname, "B-409"))
38 if (!strcmp(cname, "B-571"))
40 if (!strcmp(cname, "K-163"))
42 if (!strcmp(cname, "K-233"))
44 if (!strcmp(cname, "K-283"))
46 if (!strcmp(cname, "K-409"))
48 if (!strcmp(cname, "K-571"))
50 if (!strcmp(cname, "P-192"))
51 return NID_X9_62_prime192v1;
52 if (!strcmp(cname, "P-224"))
54 if (!strcmp(cname, "P-256"))
55 return NID_X9_62_prime256v1;
56 if (!strcmp(cname, "P-384"))
58 if (!strcmp(cname, "P-521"))
61 fprintf(stderr, "Unknown Curve name %s\n", cname);
68 char buf[1024], lbuf[1024];
69 char *keyword, *value;
70 int curve_nid = NID_undef;
71 BIGNUM *Qx = NULL, *Qy = NULL;
73 while(fgets(buf, sizeof buf, stdin) != NULL)
78 curve_nid = lookup_curve(buf + 1);
79 if (curve_nid == NID_undef)
83 if (!parse_line(&keyword, &value, lbuf, buf))
85 if (!strcmp(keyword, "Qx"))
87 if (!do_hex2bn(&Qx, value))
89 fprintf(stderr, "Invalid Qx value\n");
93 if (!strcmp(keyword, "Qy"))
96 if (!do_hex2bn(&Qy, value))
98 fprintf(stderr, "Invalid Qy value\n");
101 key = EC_KEY_new_by_curve_name(curve_nid);
102 rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
103 printf("Result = %s\n", rv ? "P":"F");
110 static int SigVer(void)
112 char buf[1024], lbuf[1024];
113 char *keyword, *value;
115 int curve_nid = NID_undef;
117 BIGNUM *Qx = NULL, *Qy = NULL;
119 ECDSA_SIG sg, *sig = &sg;
120 const EVP_MD *digest = EVP_sha1();
122 EVP_MD_CTX_init(&mctx);
125 while(fgets(buf, sizeof buf, stdin) != NULL)
130 curve_nid = lookup_curve(buf + 1);
131 if (curve_nid == NID_undef)
134 if (!parse_line(&keyword, &value, lbuf, buf))
136 if (!strcmp(keyword, "Msg"))
138 msg = hex2bin_m(value, &mlen);
141 fprintf(stderr, "Invalid Message\n");
146 if (!strcmp(keyword, "Qx"))
148 if (!do_hex2bn(&Qx, value))
150 fprintf(stderr, "Invalid Qx value\n");
154 if (!strcmp(keyword, "Qy"))
156 if (!do_hex2bn(&Qy, value))
158 fprintf(stderr, "Invalid Qy value\n");
162 if (!strcmp(keyword, "R"))
164 if (!do_hex2bn(&sig->r, value))
166 fprintf(stderr, "Invalid R value\n");
170 if (!strcmp(keyword, "S"))
173 if (!do_hex2bn(&sig->s, value))
175 fprintf(stderr, "Invalid S value\n");
178 key = EC_KEY_new_by_curve_name(curve_nid);
179 rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
183 fprintf(stderr, "Error setting public key\n");
187 FIPS_digestinit(&mctx, digest);
188 FIPS_digestupdate(&mctx, msg, mlen);
190 rv = FIPS_ecdsa_verify_ctx(key, &mctx, sig);
193 printf("Result = %s\n", rv ? "P":"F");
200 int main(int argc, char **argv)
202 const char *cmd = argv[1];
203 fips_set_error_print();
206 fprintf(stderr, "fips_ecdsavs [PKV|SigVer]\n");
209 if (!strcmp(cmd, "PKV"))
214 if (!strcmp(cmd, "SigVer"))
221 fprintf(stderr, "Error running %s\n", cmd);