示例#1
0
static int PKV(FILE *in, FILE *out)
{

    char buf[2048], lbuf[2048];
    char *keyword, *value;
    int curve_nid = NID_undef;
    BIGNUM *Qx = NULL, *Qy = NULL;
    EC_KEY *key = NULL;
    while (fgets(buf, sizeof buf, in) != NULL) {
        fputs(buf, out);
        if (*buf == '[' && buf[2] == '-') {
            curve_nid = elookup_curve(buf, lbuf, NULL);
            if (curve_nid == NID_undef)
                return 0;

        }
        if (!parse_line(&keyword, &value, lbuf, buf))
            continue;
        if (!strcmp(keyword, "Qx")) {
            if (!do_hex2bn(&Qx, value)) {
                fprintf(stderr, "Invalid Qx value\n");
                return 0;
            }
        }
        if (!strcmp(keyword, "Qy")) {
            int rv;
            if (!do_hex2bn(&Qy, value)) {
                fprintf(stderr, "Invalid Qy value\n");
                return 0;
            }
            key = EC_KEY_new_by_curve_name(curve_nid);
            no_err = 1;
            rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
            no_err = 0;
            EC_KEY_free(key);
            fprintf(out, "Result = %s" RESP_EOL, rv ? "P" : "F");
        }

    }
    BN_free(Qx);
    BN_free(Qy);
    return 1;
}
示例#2
0
static void primes()
{
    char buf[10240];
    char lbuf[10240];
    char *keyword, *value;

    while (fgets(buf, sizeof buf, stdin) != NULL) {
        fputs(buf, stdout);
        if (!parse_line(&keyword, &value, lbuf, buf))
            continue;
        if (!strcmp(keyword, "Prime")) {
            BIGNUM *pp;

            pp = BN_new();
            do_hex2bn(&pp, value);
            printf("result= %c\n",
                   BN_is_prime_ex(pp, 20, NULL, NULL) ? 'P' : 'F');
        }
    }
}
static void sigver(FILE *in, FILE *out)
    {
    DSA *dsa=NULL;
    char buf[1024];
    char lbuf[1024];
    unsigned char msg[1024];
    char *keyword, *value;
    int n=0;
    int dsa2, L, N;
    const EVP_MD *md = NULL;
    DSA_SIG sg, *sig = &sg;

    sig->r = NULL;
    sig->s = NULL;

    while(fgets(buf,sizeof buf,in) != NULL)
	{
	if (!parse_line(&keyword, &value, lbuf, buf))
		{
		fputs(buf,out);
		continue;
		}
	fputs(buf,out);
	if(!strcmp(keyword,"[mod"))
	    {
	    if (!parse_mod(value, &dsa2, &L, &N, &md))
		{
		fprintf(stderr, "Mod Parse Error\n");
		exit (1);
		}
	    if (dsa)
		FIPS_dsa_free(dsa);
	    dsa = FIPS_dsa_new();
	    }
	else if(!strcmp(keyword,"P"))
	    do_hex2bn(&dsa->p, value);
	else if(!strcmp(keyword,"Q"))
	    do_hex2bn(&dsa->q, value);
	else if(!strcmp(keyword,"G"))
	    do_hex2bn(&dsa->g, value);
	else if(!strcmp(keyword,"Msg"))
	    n=hex2bin(value,msg);
	else if(!strcmp(keyword,"Y"))
	    do_hex2bn(&dsa->pub_key, value);
	else if(!strcmp(keyword,"R"))
	    sig->r=hex2bn(value);
	else if(!strcmp(keyword,"S"))
	    {
	    int r;
	    sig->s=hex2bn(value);

	    no_err = 1;
	    r = FIPS_dsa_verify(dsa, msg, n, md, sig);
	    no_err = 0;
	    if (sig->s)
		{
		BN_free(sig->s);
		sig->s = NULL;
		}
	    if (sig->r)
		{
		BN_free(sig->r);
		sig->r = NULL;
		}
	
	    fprintf(out, "Result = %c" RESP_EOL RESP_EOL, r == 1 ? 'P' : 'F');
	    }
	}
	if (dsa)
	    FIPS_dsa_free(dsa);
    }
示例#4
0
static int SigVer(FILE *in, FILE *out)
{
    char buf[2048], lbuf[2048];
    char *keyword, *value;
    unsigned char *msg = NULL;
    int curve_nid = NID_undef;
    long mlen;
    BIGNUM *Qx = NULL, *Qy = NULL;
    EC_KEY *key = NULL;
    ECDSA_SIG sg, *sig = &sg;
    const EVP_MD *digest = NULL;
    sig->r = NULL;
    sig->s = NULL;
    while (fgets(buf, sizeof buf, in) != NULL) {
        fputs(buf, out);
        if (*buf == '[') {
            curve_nid = elookup_curve(buf, lbuf, &digest);
            if (curve_nid == NID_undef)
                return 0;
        }
        if (!parse_line(&keyword, &value, lbuf, buf))
            continue;
        if (!strcmp(keyword, "Msg")) {
            msg = hex2bin_m(value, &mlen);
            if (!msg) {
                fprintf(stderr, "Invalid Message\n");
                return 0;
            }
        }

        if (!strcmp(keyword, "Qx")) {
            if (!do_hex2bn(&Qx, value)) {
                fprintf(stderr, "Invalid Qx value\n");
                return 0;
            }
        }
        if (!strcmp(keyword, "Qy")) {
            if (!do_hex2bn(&Qy, value)) {
                fprintf(stderr, "Invalid Qy value\n");
                return 0;
            }
        }
        if (!strcmp(keyword, "R")) {
            if (!do_hex2bn(&sig->r, value)) {
                fprintf(stderr, "Invalid R value\n");
                return 0;
            }
        }
        if (!strcmp(keyword, "S")) {
            int rv;
            if (!do_hex2bn(&sig->s, value)) {
                fprintf(stderr, "Invalid S value\n");
                return 0;
            }
            key = EC_KEY_new_by_curve_name(curve_nid);
            rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);

            if (rv != 1) {
                fprintf(stderr, "Error setting public key\n");
                return 0;
            }

            no_err = 1;
            rv = FIPS_ecdsa_verify(key, msg, mlen, digest, sig);
            EC_KEY_free(key);
            if (msg)
                OPENSSL_free(msg);
            no_err = 0;

            fprintf(out, "Result = %s" RESP_EOL, rv ? "P" : "F");
        }

    }
    if (sig->r)
        BN_free(sig->r);
    if (sig->s)
        BN_free(sig->s);
    if (Qx)
        BN_free(Qx);
    if (Qy)
        BN_free(Qy);
    return 1;
}
示例#5
0
int rsa_test(FILE *out, FILE *in)
	{
	char *linebuf, *olinebuf, *p, *q;
	char *keyword, *value;
	RSA *rsa = NULL;
	BIGNUM *Xp1 = NULL, *Xp2 = NULL, *Xp = NULL;
	BIGNUM *Xq1 = NULL, *Xq2 = NULL, *Xq = NULL;
	BIGNUM *e = NULL;
	int ret = 0;
	int lnum = 0;

	olinebuf = OPENSSL_malloc(RSA_TEST_MAXLINELEN);
	linebuf = OPENSSL_malloc(RSA_TEST_MAXLINELEN);

	if (!linebuf || !olinebuf)
		goto error;

	while (fgets(olinebuf, RSA_TEST_MAXLINELEN, in))
		{
		lnum++;
		strcpy(linebuf, olinebuf);
		keyword = linebuf;
		/* Skip leading space */
		while (isspace((unsigned char)*keyword))
			keyword++;

		/* Look for = sign */
		p = strchr(linebuf, '=');

		/* If no = or starts with [ (for [foo = bar] line) just copy */
		if (!p || *keyword=='[')
			{
			if (fputs(olinebuf, out) < 0)
				goto error;
			continue;
			}

		q = p - 1;

		/* Remove trailing space */
		while (isspace((unsigned char)*q))
			*q-- = 0;

		*p = 0;
		value = p + 1;

		/* Remove leading space from value */
		while (isspace((unsigned char)*value))
			value++;

		/* Remove trailing space from value */
		p = value + strlen(value) - 1;

		while (*p == '\n' || isspace((unsigned char)*p))
			*p-- = 0;

		if (!strcmp(keyword, "xp1"))
			{
			if (Xp1 || !do_hex2bn(&Xp1,value))
				goto parse_error;
			}
		else if (!strcmp(keyword, "xp2"))
			{
			if (Xp2 || !do_hex2bn(&Xp2,value))
				goto parse_error;
			}
		else if (!strcmp(keyword, "Xp"))
			{
			if (Xp || !do_hex2bn(&Xp,value))
				goto parse_error;
			}
		else if (!strcmp(keyword, "xq1"))
			{
			if (Xq1 || !do_hex2bn(&Xq1,value))
				goto parse_error;
			}
		else if (!strcmp(keyword, "xq2"))
			{
			if (Xq2 || !do_hex2bn(&Xq2,value))
				goto parse_error;
			}
		else if (!strcmp(keyword, "Xq"))
			{
			if (Xq || !do_hex2bn(&Xq,value))
				goto parse_error;
			}
		else if (!strcmp(keyword, "e"))
			{
			if (e || !do_hex2bn(&e,value))
				goto parse_error;
			}
		else if (!strcmp(keyword, "p1"))
			continue;
		else if (!strcmp(keyword, "p2"))
			continue;
		else if (!strcmp(keyword, "p"))
			continue;
		else if (!strcmp(keyword, "q1"))
			continue;
		else if (!strcmp(keyword, "q2"))
			continue;
		else if (!strcmp(keyword, "q"))
			continue;
		else if (!strcmp(keyword, "n"))
			continue;
		else if (!strcmp(keyword, "d"))
			continue;
		else
			goto parse_error;

		fputs(olinebuf, out);

		if (e && Xp1 && Xp2 && Xp)
			{
			rsa = FIPS_rsa_new();
			if (!rsa)
				goto error;
			if (!rsa_printkey1(out, rsa, Xp1, Xp2, Xp, e))
				goto error;
			BN_free(Xp1);
			Xp1 = NULL;
			BN_free(Xp2);
			Xp2 = NULL;
			BN_free(Xp);
			Xp = NULL;
			BN_free(e);
			e = NULL;
			}

		if (rsa && Xq1 && Xq2 && Xq)
			{
			if (!rsa_printkey2(out, rsa, Xq1, Xq2, Xq))
				goto error;
			BN_free(Xq1);
			Xq1 = NULL;
			BN_free(Xq2);
			Xq2 = NULL;
			BN_free(Xq);
			Xq = NULL;
			FIPS_rsa_free(rsa);
			rsa = NULL;
			}
		}

	ret = 1;

	error:

	if (olinebuf)
		OPENSSL_free(olinebuf);
	if (linebuf)
		OPENSSL_free(linebuf);

	if (Xp1)
		BN_free(Xp1);
	if (Xp2)
		BN_free(Xp2);
	if (Xp)
		BN_free(Xp);
	if (Xq1)
		BN_free(Xq1);
	if (Xq1)
		BN_free(Xq1);
	if (Xq2)
		BN_free(Xq2);
	if (Xq)
		BN_free(Xq);
	if (e)
		BN_free(e);
	if (rsa)
		FIPS_rsa_free(rsa);

	return ret;

	parse_error:

	fprintf(stderr, "FATAL parse error processing line %d\n", lnum);

	goto error;

	}