int main(int argc, char **argv)
#endif
    {
    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
	{
	fprintf(stderr,"%s [prime|pqg|pqgver|keypair|keyver|siggen|sigver]\n",argv[0]);
	exit(1);
	}
    fips_algtest_init();
    if(!strcmp(argv[1],"prime"))
	primes(in, out);
    else if(!strcmp(argv[1],"pqg"))
	pqg(in, out);
    else if(!strcmp(argv[1],"pqgver"))
	pqgver(in, out);
    else if(!strcmp(argv[1],"keypair"))
	keypair(in, out);
    else if(!strcmp(argv[1],"keyver"))
	keyver(in, out);
    else if(!strcmp(argv[1],"siggen"))
	siggen(in, out);
    else if(!strcmp(argv[1],"sigver"))
	sigver(in, out);
    else
	{
	fprintf(stderr,"Don't know how to %s.\n",argv[1]);
	exit(1);
	}

    if (argc == 4)
	{
	fclose(in);
	fclose(out);
	}

    return 0;
    }
Exemple #2
0
int main()
	{
	fips_algtest_init();
	run_test(aes_128_mct_key, 16, &aes_128_mct_tv);
	printf("FIPS PRNG test 1 done\n");
	run_test(aes_192_mct_key, 24, &aes_192_mct_tv);
	printf("FIPS PRNG test 2 done\n");
	run_test(aes_256_mct_key, 32, &aes_256_mct_tv);
	printf("FIPS PRNG test 3 done\n");
	return 0;
	}
int main(int argc, char **argv)
# endif
{
    FILE *in = NULL, *out = NULL;
    const char *cmd = argv[1];
    int rv = 0;
    fips_algtest_init();

    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;
    }

    if (!cmd) {
        fprintf(stderr, "fips_ecdsavs [KeyPair|PKV|SigGen|SigVer]\n");
        return 1;
    }
    if (!strcmp(cmd, "KeyPair"))
        rv = KeyPair(in, out);
    else if (!strcmp(cmd, "PKV"))
        rv = PKV(in, out);
    else if (!strcmp(cmd, "SigVer"))
        rv = SigVer(in, out);
    else if (!strcmp(cmd, "SigGen"))
        rv = SigGen(in, out);
    else {
        fprintf(stderr, "Unknown command %s\n", cmd);
        return 1;
    }

    if (argc == 4) {
        fclose(in);
        fclose(out);
    }

    if (rv <= 0) {
        fprintf(stderr, "Error running %s\n", cmd);
        return 1;
    }

    return 0;
}
Exemple #4
0
int main(int argc, char **argv)
	{
	FILE *in = NULL, *out = NULL;
	int mode = 0;		/* 0 => Generate, 1 => Verify */
	int Klen_counts_keys = 0; /* 0 => Klen is size of one key
				     1 => Klen is amount of keys
				  */
	int known_keylen = 0;	/* Only set when Klen_counts_keys = 1 */
	const EVP_CIPHER *cipher = 0;
	int ret = 1;
	fips_algtest_init();

	while (argc > 1 && argv[1][0] == '-')
		{
		switch (argv[1][1])
			{
		case 'a':
			{
			char *p = &argv[1][2];
			if (*p == '\0')
				{
				if (argc <= 2)
					{
					fprintf(stderr, "Option %s needs a value\n", argv[1]);
					goto end;
					}
				argv++;
				argc--;
				p = &argv[1][0];
				}
			if (!strcmp(p, "aes128"))
				cipher = EVP_aes_128_cbc();
			else if (!strcmp(p, "aes192"))
				cipher = EVP_aes_192_cbc();
			else if (!strcmp(p, "aes256"))
				cipher = EVP_aes_256_cbc();
			else if (!strcmp(p, "tdea3") || !strcmp(p, "tdes3"))
				{
				cipher = EVP_des_ede3_cbc();
				Klen_counts_keys = 1;
				known_keylen = 8;
				}
			else
				{
				fprintf(stderr, "Unknown algorithm %s\n", p);
				goto end;
				}
			}
			break;
		case 'g':
			mode = 0;
			break;
		case 'v':
			mode = 1;
			break;
		default:
			fprintf(stderr, "Unknown option %s\n", argv[1]);
			goto end;
			}
		argv++;
		argc--;
		}
	if (argc == 1)
		in = stdin;
	else
		in = fopen(argv[1], "r");

	if (argc < 2)
		out = stdout;
	else
		out = fopen(argv[2], "w");

	if (!in)
		{
		fprintf(stderr, "FATAL input initialization error\n");
		goto end;
		}

	if (!out)
		{
		fprintf(stderr, "FATAL output initialization error\n");
		goto end;
		}

	if (!cmac_test(cipher, out, in, mode,
			Klen_counts_keys, known_keylen))
		{
		fprintf(stderr, "FATAL cmac file processing error\n");
		goto end;
		}
	else
		ret = 0;

	end:

	if (in && (in != stdin))
		fclose(in);
	if (out && (out != stdout))
		fclose(out);

	return ret;

	}
Exemple #5
0
/*--------------------------------------------------
  Processes either a single file or 
  a set of files whose names are passed in a file.
  A single file is specified as:
    aes_test -f xxx.req
  A set of files is specified as:
    aes_test -d xxxxx.xxx
  The default is: -d req.txt
--------------------------------------------------*/
int main(int argc, char **argv)
    {
    char *rqlist = "req.txt", *rspfile = NULL;
    FILE *fp = NULL;
    char fn[250] = "", rfn[256] = "";
    int f_opt = 0, d_opt = 1;
    fips_algtest_init();

    if (argc > 1)
	{
	if (strcasecmp(argv[1], "-d") == 0)
	    {
	    d_opt = 1;
	    }
	else if (strcasecmp(argv[1], "-f") == 0)
	    {
	    f_opt = 1;
	    d_opt = 0;
	    }
	else
	    {
	    printf("Invalid parameter: %s\n", argv[1]);
	    return 0;
	    }
	if (argc < 3)
	    {
	    printf("Missing parameter\n");
	    return 0;
	    }
	if (d_opt)
	    rqlist = argv[2];
	else
	    {
	    strcpy(fn, argv[2]);
	    rspfile = argv[3];
	    }
	}
    if (d_opt)
	{ /* list of files (directory) */
	if (!(fp = fopen(rqlist, "r")))
	    {
	    printf("Cannot open req list file\n");
	    return -1;
	    }
	while (fgets(fn, sizeof(fn), fp))
	    {
	    strtok(fn, "\r\n");
	    strcpy(rfn, fn);
	    if (VERBOSE)
		printf("Processing: %s\n", rfn);
	    if (proc_file(rfn, rspfile))
		{
		printf(">>> Processing failed for: %s <<<\n", rfn);
		EXIT(1);
		}
	    }
	fclose(fp);
	}
    else /* single file */
	{
	if (VERBOSE)
	    printf("Processing: %s\n", fn);
	if (proc_file(fn, rspfile))
	    {
	    printf(">>> Processing failed for: %s <<<\n", fn);
	    }
	}
    EXIT(0);
    return 0;
    }
Exemple #6
0
int main(int argc,char **argv)
	{
	FILE *in, *out;
	DRBG_CTX *dctx = NULL;
	TEST_ENT t;
	int r, nid = 0;
	int pr = 0;
	char buf[2048], lbuf[2048];
	unsigned char randout[2048];
	char *keyword = NULL, *value = NULL;

	unsigned char *ent = NULL, *nonce = NULL, *pers = NULL, *adin = NULL;
	long entlen, noncelen, perslen, adinlen;
	int df = 0;

	int randoutlen = 0;

	int gen = 0;

	fips_algtest_init();

	if (argc == 3)
		{
		in = fopen(argv[1], "r");
		if (!in)
			{
			fprintf(stderr, "Error opening input file\n");
			exit(1);
			}
		out = fopen(argv[2], "w");
		if (!out)
			{
			fprintf(stderr, "Error opening output file\n");
			exit(1);
			}
		}
	else if (argc == 1)
		{
		in = stdin;
		out = stdout;
		}
	else
		{
		fprintf(stderr,"%s (infile outfile)\n",argv[0]);
		exit(1);
		}

	while (fgets(buf, sizeof(buf), in) != NULL)
		{
		fputs(buf, out);
		if (strlen(buf) > 4 && !strncmp(buf, "[SHA-", 5))
			{
			nid = parse_md(buf);
			if (nid == NID_undef)
				exit(1);
			}
		if (strlen(buf) > 12 && !strncmp(buf, "[AES-", 5))
			{
			nid = parse_aes(buf, &df);
			if (nid == NID_undef)
				exit(1);
			}
		if (!parse_line(&keyword, &value, lbuf, buf))
			continue;

		if (!strcmp(keyword, "[PredictionResistance"))
			{
			if (!strcmp(value, "True]"))
				pr = 1;
			else if (!strcmp(value, "False]"))
				pr = 0;
			else
				exit(1);
			}

		if (!strcmp(keyword, "EntropyInput"))
			{
			ent = hex2bin_m(value, &entlen);
			t.ent = ent;
			t.entlen = entlen;
			}

		if (!strcmp(keyword, "Nonce"))
			{
			nonce = hex2bin_m(value, &noncelen);
			t.nonce = nonce;
			t.noncelen = noncelen;
			}

		if (!strcmp(keyword, "PersonalizationString"))
			{
			pers = hex2bin_m(value, &perslen);
			dctx = FIPS_drbg_new(nid, df | DRBG_FLAG_TEST);
			if (!dctx)
				exit (1);
			FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0,
							test_nonce, 0);
			FIPS_drbg_set_app_data(dctx, &t);
			randoutlen = (int)FIPS_drbg_get_blocklength(dctx);
			r = FIPS_drbg_instantiate(dctx, pers, perslen);
			if (!r)
				{
				fprintf(stderr, "Error instantiating DRBG\n");
				exit(1);
				}
			OPENSSL_free(pers);
			OPENSSL_free(ent);
			OPENSSL_free(nonce);
			ent = nonce = pers = NULL;
			gen = 0;
			}

		if (!strcmp(keyword, "AdditionalInput"))
			{
			adin = hex2bin_m(value, &adinlen);
			if (pr)
				continue;
			r = FIPS_drbg_generate(dctx, randout, randoutlen, 0, 0,
								adin, adinlen);
			if (!r)
				{
				fprintf(stderr, "Error generating DRBG bits\n");
				exit(1);
				}
			if (!r)
				exit(1);
			OPENSSL_free(adin);
			adin = NULL;
			gen++;
			}

		if (pr)
			{
			if (!strcmp(keyword, "EntropyInputPR"))
				{
				ent = hex2bin_m(value, &entlen);
				t.ent = ent;
				t.entlen = entlen;
				r = FIPS_drbg_generate(dctx,
							randout, randoutlen,
							0, 1, adin, adinlen);
				if (!r)
					{
					fprintf(stderr,
						"Error generating DRBG bits\n");
					exit(1);
					}
				OPENSSL_free(adin);
				OPENSSL_free(ent);
				adin = ent = NULL;
				gen++;
				}
			}
		if (!strcmp(keyword, "EntropyInputReseed"))
			{
			ent = hex2bin_m(value, &entlen);
			t.ent = ent;
			t.entlen = entlen;
			}
		if (!strcmp(keyword, "AdditionalInputReseed"))
			{
			adin = hex2bin_m(value, &adinlen);
			FIPS_drbg_reseed(dctx, adin, adinlen);
			OPENSSL_free(ent);
			OPENSSL_free(adin);
			ent = adin = NULL;
			}
		if (gen == 2)
			{
			OutputValue("ReturnedBits", randout, randoutlen,
									out, 0);
			FIPS_drbg_free(dctx);
			dctx = NULL;
			gen = 0;
			}

		}
	return 0;
	}
int main(int argc,char **argv)
#endif
	{
	FILE *in, *out;
	DRBG_CTX *dctx = NULL;
	TEST_ENT t;
	int r, nid = 0;
	int pr = 0;
	char buf[2048], lbuf[2048];
	unsigned char randout[2048];
	char *keyword = NULL, *value = NULL;

	unsigned char *ent = NULL, *nonce = NULL, *pers = NULL, *adin = NULL;
	long entlen, noncelen, perslen, adinlen;
	int df = 0;

	enum dtype { DRBG_NONE, DRBG_CTR, DRBG_HASH, DRBG_HMAC, DRBG_DUAL_EC }
		drbg_type = DRBG_NONE;

	int randoutlen = 0;

	int gen = 0;

	fips_algtest_init();

	if (argc == 3)
		{
		in = fopen(argv[1], "r");
		if (!in)
			{
			fprintf(stderr, "Error opening input file\n");
			exit(1);
			}
		out = fopen(argv[2], "w");
		if (!out)
			{
			fprintf(stderr, "Error opening output file\n");
			exit(1);
			}
		}
	else if (argc == 1)
		{
		in = stdin;
		out = stdout;
		}
	else
		{
		fprintf(stderr,"%s (infile outfile)\n",argv[0]);
		exit(1);
		}

	while (fgets(buf, sizeof(buf), in) != NULL)
		{
		fputs(buf, out);
		if (drbg_type == DRBG_NONE)
			{
			if (strstr(buf, "CTR_DRBG"))
				drbg_type = DRBG_CTR;
			else if (strstr(buf, "Hash_DRBG"))
				drbg_type = DRBG_HASH;
			else if (strstr(buf, "HMAC_DRBG"))
				drbg_type = DRBG_HMAC;
			else if (strstr(buf, "Dual_EC_DRBG"))
				drbg_type = DRBG_DUAL_EC;
			else
				continue;
			}
		if (strlen(buf) > 4 && !strncmp(buf, "[SHA-", 5))
			{
			nid = dparse_md(buf);
			if (nid == NID_undef)
				exit(1);
			if (drbg_type == DRBG_HMAC)
				{
				switch (nid)
					{
					case NID_sha1:
					nid = NID_hmacWithSHA1;
					break;

					case NID_sha224:
					nid = NID_hmacWithSHA224;
					break;

					case NID_sha256:
					nid = NID_hmacWithSHA256;
					break;

					case NID_sha384:
					nid = NID_hmacWithSHA384;
					break;

					case NID_sha512:
					nid = NID_hmacWithSHA512;
					break;

					default:
					exit(1);
					}
				}
			}
		if (strlen(buf) > 12 && !strncmp(buf, "[AES-", 5))
			{
			nid = parse_aes(buf, &df);
			if (nid == NID_undef)
				exit(1);
			}
		if (strlen(buf) > 12 && !strncmp(buf, "[P-", 3))
			{
			nid = parse_ec(buf);
			if (nid == NID_undef)
				exit(1);
			}
		if (!parse_line(&keyword, &value, lbuf, buf))
			continue;

		if (!strcmp(keyword, "[PredictionResistance"))
			{
			if (!strcmp(value, "True]"))
				pr = 1;
			else if (!strcmp(value, "False]"))
				pr = 0;
			else
				exit(1);
			}

		if (!strcmp(keyword, "EntropyInput"))
			{
			ent = hex2bin_m(value, &entlen);
			t.ent = ent;
			t.entlen = entlen;
			}

		if (!strcmp(keyword, "Nonce"))
			{
			nonce = hex2bin_m(value, &noncelen);
			t.nonce = nonce;
			t.noncelen = noncelen;
			}

		if (!strcmp(keyword, "PersonalizationString"))
			{
			pers = hex2bin_m(value, &perslen);
			if (nid == 0)
				{
				fprintf(stderr, "DRBG type not recognised!\n");
				exit (1);
				}
			dctx = FIPS_drbg_new(nid, df | DRBG_FLAG_TEST);
			if (!dctx)
				exit (1);
			FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0,
							test_nonce, 0);
			FIPS_drbg_set_app_data(dctx, &t);
			randoutlen = (int)FIPS_drbg_get_blocklength(dctx);
			r = FIPS_drbg_instantiate(dctx, pers, perslen);
			if (!r)
				{
				fprintf(stderr, "Error instantiating DRBG\n");
				exit(1);
				}
			OPENSSL_free(pers);
			OPENSSL_free(ent);
			OPENSSL_free(nonce);
			ent = nonce = pers = NULL;
			gen = 0;
			}

		if (!strcmp(keyword, "AdditionalInput"))
			{
			adin = hex2bin_m(value, &adinlen);
			if (pr)
				continue;
			r = FIPS_drbg_generate(dctx, randout, randoutlen, 0,
								adin, adinlen);
			if (!r)
				{
				fprintf(stderr, "Error generating DRBG bits\n");
				exit(1);
				}
			if (!r)
				exit(1);
			OPENSSL_free(adin);
			adin = NULL;
			gen++;
			}

		if (pr)
			{
			if (!strcmp(keyword, "EntropyInputPR"))
				{
				ent = hex2bin_m(value, &entlen);
				t.ent = ent;
				t.entlen = entlen;
				r = FIPS_drbg_generate(dctx,
							randout, randoutlen,
							1, adin, adinlen);
				if (!r)
					{
					fprintf(stderr,
						"Error generating DRBG bits\n");
					exit(1);
					}
				OPENSSL_free(adin);
				OPENSSL_free(ent);
				adin = ent = NULL;
				gen++;
				}
			}
		if (!strcmp(keyword, "EntropyInputReseed"))
			{
			ent = hex2bin_m(value, &entlen);
			t.ent = ent;
			t.entlen = entlen;
			}
		if (!strcmp(keyword, "AdditionalInputReseed"))
			{
			adin = hex2bin_m(value, &adinlen);
			FIPS_drbg_reseed(dctx, adin, adinlen);
			OPENSSL_free(ent);
			OPENSSL_free(adin);
			ent = adin = NULL;
			}
		if (gen == 2)
			{
			OutputValue("ReturnedBits", randout, randoutlen,
									out, 0);
			FIPS_drbg_free(dctx);
			dctx = NULL;
			gen = 0;
			}

		}
	return 0;
	}
Exemple #8
0
int main(int argc, char **argv)
	{
	DSA *dsa=NULL;
	DSA_SIG *sig = NULL;
	int counter,ret=0,i,j;
	unsigned char buf[256];
	unsigned long h;
	BN_GENCB cb;
	BN_GENCB_set(&cb, dsa_cb, stderr);

    	fips_algtest_init();

	fprintf(stderr,"test generation of DSA parameters\n");

	dsa = FIPS_dsa_new();
	DSA_generate_parameters_ex(dsa, 1024,seed,20,&counter,&h,&cb);

	fprintf(stderr,"seed\n");
	for (i=0; i<20; i+=4)
		{
		fprintf(stderr,"%02X%02X%02X%02X ",
			seed[i],seed[i+1],seed[i+2],seed[i+3]);
		}
	fprintf(stderr,"\ncounter=%d h=%ld\n",counter,h);

	if (dsa == NULL) goto end;
	if (counter != 16) 
		{
		fprintf(stderr,"counter should be 105\n");
		goto end;
		}
	if (h != 2)
		{
		fprintf(stderr,"h should be 2\n");
		goto end;
		}

	i=BN_bn2bin(dsa->q,buf);
	j=sizeof(out_q);
	if ((i != j) || (memcmp(buf,out_q,i) != 0))
		{
		fprintf(stderr,"q value is wrong\n");
		goto end;
		}

	i=BN_bn2bin(dsa->p,buf);
	j=sizeof(out_p);
	if ((i != j) || (memcmp(buf,out_p,i) != 0))
		{
		fprintf(stderr,"p value is wrong\n");
		goto end;
		}

	i=BN_bn2bin(dsa->g,buf);
	j=sizeof(out_g);
	if ((i != j) || (memcmp(buf,out_g,i) != 0))
		{
		fprintf(stderr,"g value is wrong\n");
		goto end;
		}
	DSA_generate_key(dsa);

	sig = FIPS_dsa_sign(dsa, str1, 20, EVP_sha1());
	if (!sig)
		goto end;

	if (FIPS_dsa_verify(dsa, str1, 20, EVP_sha1(), sig) != 1)
		goto end;

	ret = 1;

end:
	if (sig)
		FIPS_dsa_sig_free(sig);
	if (dsa != NULL) FIPS_dsa_free(dsa);
#if 0
	CRYPTO_mem_leaks(bio_err);
#endif
	EXIT(!ret);
	return(!ret);
	}