2011-01-27 19:10:56 +00:00
|
|
|
|
|
|
|
#define OPENSSL_FIPSAPI
|
2011-01-26 00:56:19 +00:00
|
|
|
#include <openssl/opensslconf.h>
|
|
|
|
|
|
|
|
#ifndef OPENSSL_FIPS
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
printf("No FIPS DSA support\n");
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/dsa.h>
|
|
|
|
#include <openssl/fips.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "fips_utl.h"
|
|
|
|
|
2011-01-31 19:44:09 +00:00
|
|
|
static int parse_mod(char *line, int *pdsa2, int *pL, int *pN,
|
|
|
|
const EVP_MD **pmd)
|
|
|
|
{
|
|
|
|
char lbuf[10240];
|
|
|
|
char *keyword, *value;
|
|
|
|
|
|
|
|
char *p;
|
|
|
|
p = strchr(line, ',');
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
*pL = atoi(line);
|
|
|
|
*pdsa2 = 0;
|
|
|
|
*pN = 160;
|
2011-02-01 19:15:12 +00:00
|
|
|
if (pmd)
|
|
|
|
*pmd = EVP_sha1();
|
2011-01-31 19:44:09 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
*pdsa2 = 1;
|
|
|
|
*p = 0;
|
2011-08-08 14:47:51 +00:00
|
|
|
if (!parse_line2(&keyword, &value, lbuf, line, 0))
|
2011-01-31 19:44:09 +00:00
|
|
|
return 0;
|
|
|
|
if (strcmp(keyword, "L"))
|
|
|
|
return 0;
|
|
|
|
*pL = atoi(value);
|
|
|
|
strcpy(line, p + 1);
|
2011-02-01 18:53:48 +00:00
|
|
|
if (pmd)
|
|
|
|
p = strchr(line, ',');
|
|
|
|
else
|
|
|
|
p = strchr(line, ']');
|
2011-01-31 19:44:09 +00:00
|
|
|
if (!p)
|
|
|
|
return 0;
|
|
|
|
*p = 0;
|
2011-08-08 14:47:51 +00:00
|
|
|
if (!parse_line2(&keyword, &value, lbuf, line, 0))
|
2011-01-31 19:44:09 +00:00
|
|
|
return 0;
|
|
|
|
if (strcmp(keyword, "N"))
|
|
|
|
return 0;
|
|
|
|
*pN = atoi(value);
|
2011-02-01 18:53:48 +00:00
|
|
|
if (!pmd)
|
|
|
|
return 1;
|
2011-01-31 19:44:09 +00:00
|
|
|
strcpy(line, p + 1);
|
|
|
|
p = strchr(line, ']');
|
|
|
|
if (!p)
|
|
|
|
return 0;
|
|
|
|
*p = 0;
|
|
|
|
p = line;
|
|
|
|
while(isspace(*p))
|
|
|
|
p++;
|
|
|
|
if (!strcmp(p, "SHA-1"))
|
|
|
|
*pmd = EVP_sha1();
|
|
|
|
else if (!strcmp(p, "SHA-224"))
|
|
|
|
*pmd = EVP_sha224();
|
|
|
|
else if (!strcmp(p, "SHA-256"))
|
|
|
|
*pmd = EVP_sha256();
|
|
|
|
else if (!strcmp(p, "SHA-384"))
|
|
|
|
*pmd = EVP_sha384();
|
|
|
|
else if (!strcmp(p, "SHA-512"))
|
|
|
|
*pmd = EVP_sha512();
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
static void primes(FILE *in, FILE *out)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
char buf[10240];
|
|
|
|
char lbuf[10240];
|
|
|
|
char *keyword, *value;
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
while(fgets(buf,sizeof buf,in) != NULL)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
2011-03-08 13:27:29 +00:00
|
|
|
fputs(buf,out);
|
2011-01-26 00:56:19 +00:00
|
|
|
if (!parse_line(&keyword, &value, lbuf, buf))
|
|
|
|
continue;
|
|
|
|
if(!strcmp(keyword,"Prime"))
|
|
|
|
{
|
|
|
|
BIGNUM *pp;
|
|
|
|
|
|
|
|
pp=BN_new();
|
|
|
|
do_hex2bn(&pp,value);
|
2011-10-01 20:42:52 +00:00
|
|
|
fprintf(out, "result= %c" RESP_EOL,
|
2011-01-26 00:56:19 +00:00
|
|
|
BN_is_prime_ex(pp,20,NULL,NULL) ? 'P' : 'F');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
|
|
|
|
const EVP_MD *evpmd, const unsigned char *seed_in, size_t seed_len,
|
|
|
|
unsigned char *seed_out,
|
|
|
|
int *counter_ret, unsigned long *h_ret, BN_GENCB *cb);
|
2011-08-26 14:51:49 +00:00
|
|
|
int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
|
2011-01-31 19:44:09 +00:00
|
|
|
const EVP_MD *evpmd, const unsigned char *seed_in, size_t seed_len,
|
2011-08-26 14:51:49 +00:00
|
|
|
int idx, unsigned char *seed_out,
|
2011-01-31 19:44:09 +00:00
|
|
|
int *counter_ret, unsigned long *h_ret, BN_GENCB *cb);
|
2011-01-26 00:56:19 +00:00
|
|
|
|
2011-10-12 17:03:15 +00:00
|
|
|
int dsa_paramgen_check_g(DSA *dsa);
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
static void pqg(FILE *in, FILE *out)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
char lbuf[1024];
|
|
|
|
char *keyword, *value;
|
2011-02-01 17:54:23 +00:00
|
|
|
int dsa2, L, N;
|
|
|
|
const EVP_MD *md = NULL;
|
2011-08-27 12:30:47 +00:00
|
|
|
BIGNUM *p = NULL, *q = NULL;
|
|
|
|
enum pqtype { PQG_NONE, PQG_PQ, PQG_G, PQG_GCANON}
|
|
|
|
pqg_type = PQG_NONE;
|
|
|
|
int seedlen=-1, idxlen, idx = -1;
|
|
|
|
unsigned char seed[1024], idtmp[1024];
|
2011-01-26 00:56:19 +00:00
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
while(fgets(buf,sizeof buf,in) != NULL)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
2011-08-27 12:30:47 +00:00
|
|
|
if (buf[0] == '[')
|
|
|
|
{
|
|
|
|
if (strstr(buf, "Probable"))
|
|
|
|
pqg_type = PQG_PQ;
|
|
|
|
else if (strstr(buf, "Unverifiable"))
|
|
|
|
pqg_type = PQG_G;
|
|
|
|
else if (strstr(buf, "Canonical"))
|
|
|
|
pqg_type = PQG_GCANON;
|
|
|
|
}
|
2011-01-26 00:56:19 +00:00
|
|
|
if (!parse_line(&keyword, &value, lbuf, buf))
|
|
|
|
{
|
2011-03-08 13:27:29 +00:00
|
|
|
fputs(buf,out);
|
2011-01-26 00:56:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-09-30 11:58:59 +00:00
|
|
|
if (strcmp(keyword, "Num"))
|
2011-09-28 22:35:30 +00:00
|
|
|
fputs(buf,out);
|
2011-01-26 00:56:19 +00:00
|
|
|
if(!strcmp(keyword,"[mod"))
|
2011-01-31 19:44:09 +00:00
|
|
|
{
|
2011-02-01 17:54:23 +00:00
|
|
|
if (!parse_mod(value, &dsa2, &L, &N, &md))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Mod Parse Error\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
2011-01-31 19:44:09 +00:00
|
|
|
}
|
2011-08-27 12:30:47 +00:00
|
|
|
else if(!strcmp(keyword,"N")
|
|
|
|
|| (!strcmp(keyword, "Num") && pqg_type == PQG_PQ))
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
int n=atoi(value);
|
|
|
|
|
|
|
|
while(n--)
|
|
|
|
{
|
|
|
|
DSA *dsa;
|
|
|
|
int counter;
|
|
|
|
unsigned long h;
|
|
|
|
dsa = FIPS_dsa_new();
|
|
|
|
|
2011-02-01 17:54:23 +00:00
|
|
|
if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, md,
|
|
|
|
NULL, 0, seed,
|
|
|
|
&counter, &h, NULL))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Parameter Generation error\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, md,
|
2011-08-26 14:51:49 +00:00
|
|
|
NULL, 0, -1, seed,
|
2011-02-01 17:54:23 +00:00
|
|
|
&counter, &h, NULL) <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Parameter Generation error\n");
|
2011-01-26 00:56:19 +00:00
|
|
|
exit(1);
|
2011-02-01 17:54:23 +00:00
|
|
|
}
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
do_bn_print_name(out, "P",dsa->p);
|
|
|
|
do_bn_print_name(out, "Q",dsa->q);
|
2011-08-27 12:30:47 +00:00
|
|
|
if (!dsa2)
|
|
|
|
do_bn_print_name(out, "G",dsa->g);
|
|
|
|
OutputValue(dsa2 ? "domain_parameter_seed" : "Seed",
|
|
|
|
seed, M_EVP_MD_size(md), out, 0);
|
|
|
|
if (!dsa2)
|
|
|
|
{
|
2011-10-01 20:42:52 +00:00
|
|
|
fprintf(out, "c = %d" RESP_EOL, counter);
|
|
|
|
fprintf(out, "H = %lx" RESP_EOL RESP_EOL,h);
|
2011-08-27 12:30:47 +00:00
|
|
|
}
|
|
|
|
else
|
2011-09-28 22:35:30 +00:00
|
|
|
{
|
2011-10-01 20:42:52 +00:00
|
|
|
fprintf(out, "counter = %d" RESP_EOL RESP_EOL, counter);
|
2011-09-28 22:35:30 +00:00
|
|
|
}
|
2011-11-02 19:16:43 +00:00
|
|
|
FIPS_dsa_free(dsa);
|
2011-01-26 00:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-27 12:30:47 +00:00
|
|
|
else if(!strcmp(keyword,"P"))
|
|
|
|
p=hex2bn(value);
|
|
|
|
else if(!strcmp(keyword,"Q"))
|
|
|
|
q=hex2bn(value);
|
|
|
|
else if(!strcmp(keyword,"domain_parameter_seed"))
|
|
|
|
seedlen = hex2bin(value, seed);
|
2011-09-25 22:04:43 +00:00
|
|
|
else if(!strcmp(keyword,"firstseed"))
|
|
|
|
seedlen = hex2bin(value, seed);
|
|
|
|
else if(!strcmp(keyword,"pseed"))
|
|
|
|
seedlen += hex2bin(value, seed + seedlen);
|
|
|
|
else if(!strcmp(keyword,"qseed"))
|
|
|
|
seedlen += hex2bin(value, seed + seedlen);
|
2011-08-27 12:30:47 +00:00
|
|
|
else if(!strcmp(keyword,"index"))
|
|
|
|
{
|
|
|
|
idxlen = hex2bin(value, idtmp);
|
|
|
|
if (idxlen != 1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Index value error\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
idx = idtmp[0];
|
|
|
|
}
|
|
|
|
if ((idx >= 0 && pqg_type == PQG_GCANON) || (q && pqg_type == PQG_G))
|
|
|
|
{
|
|
|
|
DSA *dsa;
|
|
|
|
dsa = FIPS_dsa_new();
|
|
|
|
dsa->p = p;
|
|
|
|
dsa->q = q;
|
|
|
|
p = q = NULL;
|
|
|
|
if (dsa_builtin_paramgen2(dsa, L, N, md,
|
|
|
|
seed, seedlen, idx, NULL,
|
|
|
|
NULL, NULL, NULL) <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Parameter Generation error\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
do_bn_print_name(out, "G",dsa->g);
|
|
|
|
FIPS_dsa_free(dsa);
|
|
|
|
idx = -1;
|
|
|
|
}
|
2011-01-26 00:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
static void pqgver(FILE *in, FILE *out)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
char lbuf[1024];
|
|
|
|
char *keyword, *value;
|
|
|
|
BIGNUM *p = NULL, *q = NULL, *g = NULL;
|
2011-02-23 16:06:33 +00:00
|
|
|
int counter=-1, counter2;
|
2011-07-25 21:58:11 +00:00
|
|
|
unsigned long h=0, h2;
|
2011-01-26 00:56:19 +00:00
|
|
|
DSA *dsa=NULL;
|
2011-02-02 17:48:03 +00:00
|
|
|
int dsa2, L, N, part_test = 0;
|
2011-01-31 19:44:09 +00:00
|
|
|
const EVP_MD *md = NULL;
|
2011-08-26 14:51:49 +00:00
|
|
|
int seedlen=-1, idxlen, idx = -1;
|
|
|
|
unsigned char seed[1024], idtmp[1024];
|
2011-01-26 00:56:19 +00:00
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
while(fgets(buf,sizeof buf,in) != NULL)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
if (!parse_line(&keyword, &value, lbuf, buf))
|
|
|
|
{
|
2011-02-02 17:48:03 +00:00
|
|
|
if (p && q)
|
|
|
|
{
|
|
|
|
part_test = 1;
|
|
|
|
goto partial;
|
|
|
|
}
|
2011-03-08 13:27:29 +00:00
|
|
|
fputs(buf,out);
|
2011-01-26 00:56:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-03-08 13:27:29 +00:00
|
|
|
fputs(buf, out);
|
2011-01-26 00:56:19 +00:00
|
|
|
if(!strcmp(keyword,"[mod"))
|
2011-01-31 19:44:09 +00:00
|
|
|
{
|
|
|
|
if (!parse_mod(value, &dsa2, &L, &N, &md))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Mod Parse Error\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
2011-01-26 00:56:19 +00:00
|
|
|
else if(!strcmp(keyword,"P"))
|
|
|
|
p=hex2bn(value);
|
|
|
|
else if(!strcmp(keyword,"Q"))
|
|
|
|
q=hex2bn(value);
|
|
|
|
else if(!strcmp(keyword,"G"))
|
|
|
|
g=hex2bn(value);
|
2011-10-12 15:32:57 +00:00
|
|
|
else if(!strcmp(keyword,"firstseed"))
|
|
|
|
seedlen = hex2bin(value, seed);
|
|
|
|
else if(!strcmp(keyword,"pseed"))
|
|
|
|
seedlen += hex2bin(value, seed + seedlen);
|
|
|
|
else if(!strcmp(keyword,"qseed"))
|
|
|
|
seedlen += hex2bin(value, seed + seedlen);
|
2011-08-26 14:51:49 +00:00
|
|
|
else if(!strcmp(keyword,"Seed")
|
|
|
|
|| !strcmp(keyword,"domain_parameter_seed"))
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
2011-01-31 19:44:09 +00:00
|
|
|
seedlen = hex2bin(value, seed);
|
|
|
|
if (!dsa2 && seedlen != 20)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Seed parse length error\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
2011-08-26 14:51:49 +00:00
|
|
|
if (idx > 0)
|
|
|
|
part_test = 1;
|
|
|
|
}
|
|
|
|
else if(!strcmp(keyword,"index"))
|
|
|
|
{
|
|
|
|
idxlen = hex2bin(value, idtmp);
|
|
|
|
if (idxlen != 1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Index value error\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
idx = idtmp[0];
|
2011-01-26 00:56:19 +00:00
|
|
|
}
|
|
|
|
else if(!strcmp(keyword,"c"))
|
2011-08-26 14:51:49 +00:00
|
|
|
counter = atoi(buf+4);
|
2011-02-02 17:48:03 +00:00
|
|
|
partial:
|
2011-10-12 17:03:15 +00:00
|
|
|
if (part_test && idx < 0 && h == 0 && g)
|
|
|
|
{
|
|
|
|
dsa = FIPS_dsa_new();
|
|
|
|
dsa->p = BN_dup(p);
|
|
|
|
dsa->q = BN_dup(q);
|
|
|
|
dsa->g = BN_dup(g);
|
|
|
|
if (dsa_paramgen_check_g(dsa))
|
|
|
|
fprintf(out, "Result = P" RESP_EOL);
|
|
|
|
else
|
|
|
|
fprintf(out, "Result = F" RESP_EOL);
|
|
|
|
BN_free(p);
|
|
|
|
BN_free(q);
|
|
|
|
BN_free(g);
|
|
|
|
p = NULL;
|
|
|
|
q = NULL;
|
|
|
|
g = NULL;
|
|
|
|
FIPS_dsa_free(dsa);
|
|
|
|
dsa = NULL;
|
|
|
|
part_test = 0;
|
|
|
|
}
|
|
|
|
else if(!strcmp(keyword,"H") || part_test)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
2011-02-02 17:48:03 +00:00
|
|
|
if (!part_test)
|
|
|
|
h = atoi(value);
|
|
|
|
if (!p || !q || (!g && !part_test))
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Parse Error\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
dsa = FIPS_dsa_new();
|
2011-08-26 14:51:49 +00:00
|
|
|
if (idx >= 0)
|
|
|
|
{
|
|
|
|
dsa->p = BN_dup(p);
|
|
|
|
dsa->q = BN_dup(q);
|
|
|
|
}
|
2011-09-21 18:42:12 +00:00
|
|
|
no_err = 1;
|
2011-01-31 19:44:09 +00:00
|
|
|
if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, md,
|
|
|
|
seed, seedlen, NULL,
|
|
|
|
&counter2, &h2, NULL))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Parameter Generation error\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, md,
|
2011-08-26 14:51:49 +00:00
|
|
|
seed, seedlen, idx, NULL,
|
2011-01-31 19:44:09 +00:00
|
|
|
&counter2, &h2, NULL) < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Parameter Generation error\n");
|
2011-01-26 00:56:19 +00:00
|
|
|
exit(1);
|
2011-01-31 19:44:09 +00:00
|
|
|
}
|
2011-09-21 18:42:12 +00:00
|
|
|
no_err = 0;
|
2011-08-26 14:51:49 +00:00
|
|
|
if (idx >= 0)
|
|
|
|
{
|
|
|
|
if (BN_cmp(dsa->g, g))
|
2011-10-01 20:42:52 +00:00
|
|
|
fprintf(out, "Result = F" RESP_EOL);
|
2011-08-26 14:51:49 +00:00
|
|
|
else
|
2011-10-01 20:42:52 +00:00
|
|
|
fprintf(out, "Result = P" RESP_EOL);
|
2011-08-26 14:51:49 +00:00
|
|
|
}
|
|
|
|
else if (BN_cmp(dsa->p, p) || BN_cmp(dsa->q, q) ||
|
2011-02-02 17:48:03 +00:00
|
|
|
(!part_test &&
|
|
|
|
((BN_cmp(dsa->g, g) || (counter != counter2) || (h != h2)))))
|
2011-10-01 20:42:52 +00:00
|
|
|
fprintf(out, "Result = F" RESP_EOL);
|
2011-01-26 00:56:19 +00:00
|
|
|
else
|
2011-10-01 20:42:52 +00:00
|
|
|
fprintf(out, "Result = P" RESP_EOL);
|
2011-01-26 00:56:19 +00:00
|
|
|
BN_free(p);
|
|
|
|
BN_free(q);
|
|
|
|
BN_free(g);
|
|
|
|
p = NULL;
|
|
|
|
q = NULL;
|
|
|
|
g = NULL;
|
|
|
|
FIPS_dsa_free(dsa);
|
|
|
|
dsa = NULL;
|
2011-02-02 17:48:03 +00:00
|
|
|
if (part_test)
|
|
|
|
{
|
2011-08-26 14:51:49 +00:00
|
|
|
if (idx == -1)
|
|
|
|
fputs(buf,out);
|
2011-02-02 17:48:03 +00:00
|
|
|
part_test = 0;
|
|
|
|
}
|
2011-08-26 14:51:49 +00:00
|
|
|
idx = -1;
|
2011-01-26 00:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keypair verification routine. NB: this isn't part of the standard FIPS140-2
|
|
|
|
* algorithm tests. It is an additional test to perform sanity checks on the
|
|
|
|
* output of the KeyPair test.
|
|
|
|
*/
|
|
|
|
|
2011-02-01 18:53:48 +00:00
|
|
|
static int dss_paramcheck(int L, int N, BIGNUM *p, BIGNUM *q, BIGNUM *g,
|
2011-01-26 00:56:19 +00:00
|
|
|
BN_CTX *ctx)
|
|
|
|
{
|
|
|
|
BIGNUM *rem = NULL;
|
2011-02-01 18:53:48 +00:00
|
|
|
if (BN_num_bits(p) != L)
|
2011-01-26 00:56:19 +00:00
|
|
|
return 0;
|
2011-02-01 18:53:48 +00:00
|
|
|
if (BN_num_bits(q) != N)
|
2011-01-26 00:56:19 +00:00
|
|
|
return 0;
|
|
|
|
if (BN_is_prime_ex(p, BN_prime_checks, ctx, NULL) != 1)
|
|
|
|
return 0;
|
|
|
|
if (BN_is_prime_ex(q, BN_prime_checks, ctx, NULL) != 1)
|
|
|
|
return 0;
|
|
|
|
rem = BN_new();
|
|
|
|
if (!BN_mod(rem, p, q, ctx) || !BN_is_one(rem)
|
|
|
|
|| (BN_cmp(g, BN_value_one()) <= 0)
|
|
|
|
|| !BN_mod_exp(rem, g, q, p, ctx) || !BN_is_one(rem))
|
|
|
|
{
|
|
|
|
BN_free(rem);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Todo: check g */
|
|
|
|
BN_free(rem);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
static void keyver(FILE *in, FILE *out)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
char lbuf[1024];
|
|
|
|
char *keyword, *value;
|
|
|
|
BIGNUM *p = NULL, *q = NULL, *g = NULL, *X = NULL, *Y = NULL;
|
|
|
|
BIGNUM *Y2;
|
|
|
|
BN_CTX *ctx = NULL;
|
2011-02-01 18:53:48 +00:00
|
|
|
int dsa2, L, N;
|
|
|
|
int paramcheck = 0;
|
2011-01-26 00:56:19 +00:00
|
|
|
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
Y2 = BN_new();
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
while(fgets(buf,sizeof buf,in) != NULL)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
if (!parse_line(&keyword, &value, lbuf, buf))
|
|
|
|
{
|
2011-03-08 13:27:29 +00:00
|
|
|
fputs(buf,out);
|
2011-01-26 00:56:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(!strcmp(keyword,"[mod"))
|
|
|
|
{
|
|
|
|
if (p)
|
|
|
|
BN_free(p);
|
|
|
|
p = NULL;
|
|
|
|
if (q)
|
|
|
|
BN_free(q);
|
|
|
|
q = NULL;
|
|
|
|
if (g)
|
|
|
|
BN_free(g);
|
|
|
|
g = NULL;
|
|
|
|
paramcheck = 0;
|
2011-02-01 18:53:48 +00:00
|
|
|
if (!parse_mod(value, &dsa2, &L, &N, NULL))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Mod Parse Error\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
2011-01-26 00:56:19 +00:00
|
|
|
}
|
|
|
|
else if(!strcmp(keyword,"P"))
|
|
|
|
p=hex2bn(value);
|
|
|
|
else if(!strcmp(keyword,"Q"))
|
|
|
|
q=hex2bn(value);
|
|
|
|
else if(!strcmp(keyword,"G"))
|
|
|
|
g=hex2bn(value);
|
|
|
|
else if(!strcmp(keyword,"X"))
|
|
|
|
X=hex2bn(value);
|
|
|
|
else if(!strcmp(keyword,"Y"))
|
|
|
|
{
|
|
|
|
Y=hex2bn(value);
|
|
|
|
if (!p || !q || !g || !X || !Y)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Parse Error\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
2011-03-08 13:27:29 +00:00
|
|
|
do_bn_print_name(out, "P",p);
|
|
|
|
do_bn_print_name(out, "Q",q);
|
|
|
|
do_bn_print_name(out, "G",g);
|
|
|
|
do_bn_print_name(out, "X",X);
|
|
|
|
do_bn_print_name(out, "Y",Y);
|
2011-01-26 00:56:19 +00:00
|
|
|
if (!paramcheck)
|
|
|
|
{
|
2011-02-01 18:53:48 +00:00
|
|
|
if (dss_paramcheck(L, N, p, q, g, ctx))
|
2011-01-26 00:56:19 +00:00
|
|
|
paramcheck = 1;
|
|
|
|
else
|
|
|
|
paramcheck = -1;
|
|
|
|
}
|
|
|
|
if (paramcheck != 1)
|
2011-10-01 20:42:52 +00:00
|
|
|
fprintf(out, "Result = F" RESP_EOL);
|
2011-01-26 00:56:19 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!BN_mod_exp(Y2, g, X, p, ctx) || BN_cmp(Y2, Y))
|
2011-10-01 20:42:52 +00:00
|
|
|
fprintf(out, "Result = F" RESP_EOL);
|
2011-01-26 00:56:19 +00:00
|
|
|
else
|
2011-10-01 20:42:52 +00:00
|
|
|
fprintf(out, "Result = P" RESP_EOL);
|
2011-01-26 00:56:19 +00:00
|
|
|
}
|
|
|
|
BN_free(X);
|
|
|
|
BN_free(Y);
|
|
|
|
X = NULL;
|
|
|
|
Y = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p)
|
|
|
|
BN_free(p);
|
|
|
|
if (q)
|
|
|
|
BN_free(q);
|
|
|
|
if (g)
|
|
|
|
BN_free(g);
|
|
|
|
if (Y2)
|
|
|
|
BN_free(Y2);
|
2011-11-02 19:16:43 +00:00
|
|
|
if (ctx)
|
|
|
|
BN_CTX_free(ctx);
|
2011-01-26 00:56:19 +00:00
|
|
|
}
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
static void keypair(FILE *in, FILE *out)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
char lbuf[1024];
|
|
|
|
char *keyword, *value;
|
2011-02-01 18:53:48 +00:00
|
|
|
int dsa2, L, N;
|
2011-01-26 00:56:19 +00:00
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
while(fgets(buf,sizeof buf,in) != NULL)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
if (!parse_line(&keyword, &value, lbuf, buf))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(!strcmp(keyword,"[mod"))
|
2011-02-01 18:53:48 +00:00
|
|
|
{
|
|
|
|
if (!parse_mod(value, &dsa2, &L, &N, NULL))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Mod Parse Error\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
2011-03-08 13:27:29 +00:00
|
|
|
fputs(buf,out);
|
2011-02-01 18:53:48 +00:00
|
|
|
}
|
2011-01-26 00:56:19 +00:00
|
|
|
else if(!strcmp(keyword,"N"))
|
|
|
|
{
|
|
|
|
DSA *dsa;
|
|
|
|
int n=atoi(value);
|
|
|
|
|
|
|
|
dsa = FIPS_dsa_new();
|
2011-02-01 18:53:48 +00:00
|
|
|
if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, NULL, NULL, 0,
|
|
|
|
NULL, NULL, NULL, NULL))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Parameter Generation error\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-08-26 14:51:49 +00:00
|
|
|
if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, NULL, NULL, 0, -1,
|
2011-02-01 18:53:48 +00:00
|
|
|
NULL, NULL, NULL, NULL) <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Parameter Generation error\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-03-08 13:27:29 +00:00
|
|
|
do_bn_print_name(out, "P",dsa->p);
|
|
|
|
do_bn_print_name(out, "Q",dsa->q);
|
|
|
|
do_bn_print_name(out, "G",dsa->g);
|
2011-10-01 20:42:52 +00:00
|
|
|
fputs(RESP_EOL, out);
|
2011-01-26 00:56:19 +00:00
|
|
|
|
|
|
|
while(n--)
|
|
|
|
{
|
|
|
|
if (!DSA_generate_key(dsa))
|
|
|
|
exit(1);
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
do_bn_print_name(out, "X",dsa->priv_key);
|
|
|
|
do_bn_print_name(out, "Y",dsa->pub_key);
|
2011-10-01 20:42:52 +00:00
|
|
|
fputs(RESP_EOL, out);
|
2011-01-26 00:56:19 +00:00
|
|
|
}
|
2011-11-02 19:16:43 +00:00
|
|
|
if (dsa)
|
|
|
|
FIPS_dsa_free(dsa);
|
2011-01-26 00:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
static void siggen(FILE *in, FILE *out)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
char lbuf[1024];
|
|
|
|
char *keyword, *value;
|
2011-02-01 17:54:23 +00:00
|
|
|
int dsa2, L, N;
|
|
|
|
const EVP_MD *md = NULL;
|
2011-01-26 00:56:19 +00:00
|
|
|
DSA *dsa=NULL;
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
while(fgets(buf,sizeof buf,in) != NULL)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
if (!parse_line(&keyword, &value, lbuf, buf))
|
|
|
|
{
|
2011-03-08 13:27:29 +00:00
|
|
|
fputs(buf,out);
|
2011-01-26 00:56:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-03-08 13:27:29 +00:00
|
|
|
fputs(buf,out);
|
2011-01-26 00:56:19 +00:00
|
|
|
if(!strcmp(keyword,"[mod"))
|
|
|
|
{
|
2011-02-01 17:54:23 +00:00
|
|
|
if (!parse_mod(value, &dsa2, &L, &N, &md))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Mod Parse Error\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
2011-01-26 00:56:19 +00:00
|
|
|
if (dsa)
|
|
|
|
FIPS_dsa_free(dsa);
|
|
|
|
dsa = FIPS_dsa_new();
|
2011-02-01 17:54:23 +00:00
|
|
|
if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, md, NULL, 0,
|
|
|
|
NULL, NULL, NULL, NULL))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Parameter Generation error\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-08-26 14:51:49 +00:00
|
|
|
if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, md, NULL, 0, -1,
|
2011-02-01 17:54:23 +00:00
|
|
|
NULL, NULL, NULL, NULL) <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Parameter Generation error\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-03-08 13:27:29 +00:00
|
|
|
do_bn_print_name(out, "P",dsa->p);
|
|
|
|
do_bn_print_name(out, "Q",dsa->q);
|
|
|
|
do_bn_print_name(out, "G",dsa->g);
|
2011-10-01 20:42:52 +00:00
|
|
|
fputs(RESP_EOL, out);
|
2011-01-26 00:56:19 +00:00
|
|
|
}
|
|
|
|
else if(!strcmp(keyword,"Msg"))
|
|
|
|
{
|
|
|
|
unsigned char msg[1024];
|
|
|
|
int n;
|
|
|
|
EVP_MD_CTX mctx;
|
|
|
|
DSA_SIG *sig;
|
2011-02-12 18:25:18 +00:00
|
|
|
FIPS_md_ctx_init(&mctx);
|
2011-01-26 00:56:19 +00:00
|
|
|
|
|
|
|
n=hex2bin(value,msg);
|
|
|
|
|
|
|
|
if (!DSA_generate_key(dsa))
|
|
|
|
exit(1);
|
2011-03-08 13:27:29 +00:00
|
|
|
do_bn_print_name(out, "Y",dsa->pub_key);
|
2011-01-26 00:56:19 +00:00
|
|
|
|
2011-02-12 18:25:18 +00:00
|
|
|
FIPS_digestinit(&mctx, md);
|
|
|
|
FIPS_digestupdate(&mctx, msg, n);
|
2011-01-26 00:56:19 +00:00
|
|
|
sig = FIPS_dsa_sign_ctx(dsa, &mctx);
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
do_bn_print_name(out, "R",sig->r);
|
|
|
|
do_bn_print_name(out, "S",sig->s);
|
2011-10-01 20:42:52 +00:00
|
|
|
fputs(RESP_EOL, out);
|
2011-02-13 18:45:41 +00:00
|
|
|
FIPS_dsa_sig_free(sig);
|
2011-02-12 18:25:18 +00:00
|
|
|
FIPS_md_ctx_cleanup(&mctx);
|
2011-01-26 00:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-02 19:16:43 +00:00
|
|
|
if (dsa)
|
|
|
|
FIPS_dsa_free(dsa);
|
2011-01-26 00:56:19 +00:00
|
|
|
}
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
static void sigver(FILE *in, FILE *out)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
DSA *dsa=NULL;
|
|
|
|
char buf[1024];
|
|
|
|
char lbuf[1024];
|
|
|
|
unsigned char msg[1024];
|
|
|
|
char *keyword, *value;
|
2011-02-01 12:52:01 +00:00
|
|
|
int n=0;
|
|
|
|
int dsa2, L, N;
|
|
|
|
const EVP_MD *md = NULL;
|
2011-01-26 00:56:19 +00:00
|
|
|
DSA_SIG sg, *sig = &sg;
|
|
|
|
|
|
|
|
sig->r = NULL;
|
|
|
|
sig->s = NULL;
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
while(fgets(buf,sizeof buf,in) != NULL)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
if (!parse_line(&keyword, &value, lbuf, buf))
|
|
|
|
{
|
2011-03-08 13:27:29 +00:00
|
|
|
fputs(buf,out);
|
2011-01-26 00:56:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-03-08 13:27:29 +00:00
|
|
|
fputs(buf,out);
|
2011-01-26 00:56:19 +00:00
|
|
|
if(!strcmp(keyword,"[mod"))
|
|
|
|
{
|
2011-02-01 12:52:01 +00:00
|
|
|
if (!parse_mod(value, &dsa2, &L, &N, &md))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Mod Parse Error\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
if (dsa)
|
2011-01-26 00:56:19 +00:00
|
|
|
FIPS_dsa_free(dsa);
|
2011-02-01 12:52:01 +00:00
|
|
|
dsa = FIPS_dsa_new();
|
2011-01-26 00:56:19 +00:00
|
|
|
}
|
|
|
|
else if(!strcmp(keyword,"P"))
|
2011-11-02 19:16:43 +00:00
|
|
|
do_hex2bn(&dsa->p, value);
|
2011-01-26 00:56:19 +00:00
|
|
|
else if(!strcmp(keyword,"Q"))
|
2011-11-02 19:16:43 +00:00
|
|
|
do_hex2bn(&dsa->q, value);
|
2011-01-26 00:56:19 +00:00
|
|
|
else if(!strcmp(keyword,"G"))
|
2011-11-02 19:16:43 +00:00
|
|
|
do_hex2bn(&dsa->g, value);
|
2011-01-26 00:56:19 +00:00
|
|
|
else if(!strcmp(keyword,"Msg"))
|
|
|
|
n=hex2bin(value,msg);
|
|
|
|
else if(!strcmp(keyword,"Y"))
|
2011-11-02 19:16:43 +00:00
|
|
|
do_hex2bn(&dsa->pub_key, value);
|
2011-01-26 00:56:19 +00:00
|
|
|
else if(!strcmp(keyword,"R"))
|
|
|
|
sig->r=hex2bn(value);
|
|
|
|
else if(!strcmp(keyword,"S"))
|
|
|
|
{
|
|
|
|
EVP_MD_CTX mctx;
|
|
|
|
int r;
|
2011-02-12 18:25:18 +00:00
|
|
|
FIPS_md_ctx_init(&mctx);
|
2011-01-26 00:56:19 +00:00
|
|
|
sig->s=hex2bn(value);
|
2011-02-01 17:54:23 +00:00
|
|
|
|
2011-02-12 18:25:18 +00:00
|
|
|
FIPS_digestinit(&mctx, md);
|
|
|
|
FIPS_digestupdate(&mctx, msg, n);
|
2011-01-26 00:56:19 +00:00
|
|
|
no_err = 1;
|
|
|
|
r = FIPS_dsa_verify_ctx(dsa, &mctx, sig);
|
|
|
|
no_err = 0;
|
2011-02-12 18:25:18 +00:00
|
|
|
FIPS_md_ctx_cleanup(&mctx);
|
2011-11-02 19:16:43 +00:00
|
|
|
if (sig->s)
|
|
|
|
{
|
|
|
|
BN_free(sig->s);
|
|
|
|
sig->s = NULL;
|
|
|
|
}
|
|
|
|
if (sig->r)
|
|
|
|
{
|
|
|
|
BN_free(sig->r);
|
|
|
|
sig->r = NULL;
|
|
|
|
}
|
2011-01-26 00:56:19 +00:00
|
|
|
|
2011-10-01 20:42:52 +00:00
|
|
|
fprintf(out, "Result = %c" RESP_EOL RESP_EOL, r == 1 ? 'P' : 'F');
|
2011-01-26 00:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-02 19:16:43 +00:00
|
|
|
if (dsa)
|
|
|
|
FIPS_dsa_free(dsa);
|
2011-01-26 00:56:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-01 13:45:30 +00:00
|
|
|
#ifdef FIPS_ALGVS
|
|
|
|
int fips_dssvs_main(int argc, char **argv)
|
|
|
|
#else
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
#endif
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
2011-03-08 13:27:29 +00:00
|
|
|
FILE *in, *out;
|
|
|
|
if (argc == 4)
|
|
|
|
{
|
|
|
|
in = fopen(argv[2], "r");
|
|
|
|
if (!in)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error opening input file\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
out = fopen(argv[3], "w");
|
|
|
|
if (!out)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error opening output file\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (argc == 2)
|
|
|
|
{
|
|
|
|
in = stdin;
|
|
|
|
out = stdout;
|
|
|
|
}
|
|
|
|
else
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
2011-02-01 18:53:48 +00:00
|
|
|
fprintf(stderr,"%s [prime|pqg|pqgver|keypair|keyver|siggen|sigver]\n",argv[0]);
|
2011-01-26 00:56:19 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2011-03-25 16:36:46 +00:00
|
|
|
fips_algtest_init();
|
2011-01-26 00:56:19 +00:00
|
|
|
if(!strcmp(argv[1],"prime"))
|
2011-03-08 13:27:29 +00:00
|
|
|
primes(in, out);
|
2011-01-26 00:56:19 +00:00
|
|
|
else if(!strcmp(argv[1],"pqg"))
|
2011-03-08 13:27:29 +00:00
|
|
|
pqg(in, out);
|
2011-01-26 00:56:19 +00:00
|
|
|
else if(!strcmp(argv[1],"pqgver"))
|
2011-03-08 13:27:29 +00:00
|
|
|
pqgver(in, out);
|
2011-01-26 00:56:19 +00:00
|
|
|
else if(!strcmp(argv[1],"keypair"))
|
2011-03-08 13:27:29 +00:00
|
|
|
keypair(in, out);
|
2011-01-26 00:56:19 +00:00
|
|
|
else if(!strcmp(argv[1],"keyver"))
|
2011-03-08 13:27:29 +00:00
|
|
|
keyver(in, out);
|
2011-01-26 00:56:19 +00:00
|
|
|
else if(!strcmp(argv[1],"siggen"))
|
2011-03-08 13:27:29 +00:00
|
|
|
siggen(in, out);
|
2011-01-26 00:56:19 +00:00
|
|
|
else if(!strcmp(argv[1],"sigver"))
|
2011-03-08 13:27:29 +00:00
|
|
|
sigver(in, out);
|
2011-01-26 00:56:19 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Don't know how to %s.\n",argv[1]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
if (argc == 4)
|
|
|
|
{
|
|
|
|
fclose(in);
|
|
|
|
fclose(out);
|
|
|
|
}
|
|
|
|
|
2011-01-26 00:56:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|