Пример #1
0
static void keyver(FILE *in, FILE *out)
    {
    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;
    int dsa2, L, N;
    int paramcheck = 0;

    ctx = BN_CTX_new();
    Y2 = BN_new();

    while(fgets(buf,sizeof buf,in) != NULL)
	{
	if (!parse_line(&keyword, &value, lbuf, buf))
		{
		fputs(buf,out);
		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;
	    if (!parse_mod(value, &dsa2, &L, &N, NULL))
		{
		fprintf(stderr, "Mod Parse Error\n");
		exit (1);
		}
	    }
	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);
		}
	    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);
	    if (!paramcheck)
		{
		if (dss_paramcheck(L, N, p, q, g, ctx))
			paramcheck = 1;
		else
			paramcheck = -1;
		}
	    if (paramcheck != 1)
	   	fprintf(out, "Result = F\n");
	    else
		{
		if (!BN_mod_exp(Y2, g, X, p, ctx) || BN_cmp(Y2, Y))
	    		fprintf(out, "Result = F\n");
	        else
	    		fprintf(out, "Result = P\n");
		}
	    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);
    }
Пример #2
0
static void keyver()
{
    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;
    int nmod = 0, paramcheck = 0;

    ctx = BN_CTX_new();
    Y2 = BN_new();

    while (fgets(buf, sizeof buf, stdin) != NULL) {
        if (!parse_line(&keyword, &value, lbuf, buf)) {
            fputs(buf, stdout);
            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;
            nmod = atoi(value);
        } 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);
            }
            pbn("P", p);
            pbn("Q", q);
            pbn("G", g);
            pbn("X", X);
            pbn("Y", Y);
            if (!paramcheck) {
                if (dss_paramcheck(nmod, p, q, g, ctx))
                    paramcheck = 1;
                else
                    paramcheck = -1;
            }
            if (paramcheck != 1)
                printf("Result = F\n");
            else {
                if (!BN_mod_exp(Y2, g, X, p, ctx) || BN_cmp(Y2, Y))
                    printf("Result = F\n");
                else
                    printf("Result = P\n");
            }
            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);
}