#define OPENSSL_FIPSAPI
#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"
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;
if (pmd)
*pmd = EVP_sha1();
return 1;
}
*pdsa2 = 1;
*p = 0;
if (!parse_line(&keyword, &value, lbuf, line))
return 0;
if (strcmp(keyword, "L"))
return 0;
*pL = atoi(value);
strcpy(line, p + 1);
if (pmd)
p = strchr(line, ',');
else
p = strchr(line, ']');
if (!p)
return 0;
*p = 0;
if (!parse_line(&keyword, &value, lbuf, line))
return 0;
if (strcmp(keyword, "N"))
return 0;
*pN = atoi(value);
if (!pmd)
return 1;
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;
}
static void primes(FILE *in, FILE *out)
{
char buf[10240];
char lbuf[10240];
char *keyword, *value;
while(fgets(buf,sizeof buf,in) != NULL)
{
fputs(buf,out);
if (!parse_line(&keyword, &value, lbuf, buf))
continue;
if(!strcmp(keyword,"Prime"))
{
BIGNUM *pp;
pp=BN_new();
do_hex2bn(&pp,value);
fprintf(out, "result= %c\n",
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);
int dsa_builtin_paramgen2(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);
static void pqg(FILE *in, FILE *out)
{
char buf[1024];
char lbuf[1024];
char *keyword, *value;
int dsa2, L, N;
const EVP_MD *md = NULL;
while(fgets(buf,sizeof buf,in) != NULL)
{
if (!parse_line(&keyword, &value, lbuf, buf))
{
fputs(buf,out);
continue;
}
if(!strcmp(keyword,"[mod"))
{
fputs(buf,out);
if (!parse_mod(value, &dsa2, &L, &N, &md))
{
fprintf(stderr, "Mod Parse Error\n");
exit (1);
}
}
else if(!strcmp(keyword,"N"))
{
int n=atoi(value);
while(n--)
{
unsigned char seed[EVP_MAX_MD_SIZE];
DSA *dsa;
int counter;
unsigned long h;
dsa = FIPS_dsa_new();
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,
NULL, 0, seed,
&counter, &h, NULL) <= 0)
{
fprintf(stderr, "Parameter Generation error\n");
exit(1);
}
do_bn_print_name(out, "P",dsa->p);