libbrandt/test_crypto.c

104 lines
2.4 KiB
C
Raw Normal View History

2016-06-13 21:09:41 +02:00
/* This file is part of libbrandt.
* Copyright (C) 2016 GNUnet e.V.
*
* libbrandt is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* libbrandt is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* libbrandt. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file test_crypto.c
* @brief testing crypto and smc functions.
*/
2016-06-12 20:52:22 +02:00
#include "brandt.h"
#include "crypto.h"
2016-06-13 21:01:14 +02:00
#include "smc.h"
2016-06-12 20:52:22 +02:00
#include "test.h"
2016-06-13 21:01:14 +02:00
extern gcry_mpi_point_t ec_gen;
2016-06-13 21:09:41 +02:00
extern gcry_ctx_t ec_ctx;
2016-06-13 21:01:14 +02:00
2016-06-12 20:52:22 +02:00
int
test_brandt_ec_keypair_create ()
{
2016-06-13 21:09:41 +02:00
gcry_mpi_t skey;
2016-06-12 20:52:22 +02:00
gcry_mpi_point_t pkey1;
2016-06-13 21:09:41 +02:00
gcry_mpi_point_t pkey2 = gcry_mpi_point_new (0);
2016-06-12 20:52:22 +02:00
brandt_ec_keypair_create (&pkey1, &skey);
2016-06-13 21:09:41 +02:00
check (skey, "no sec key created");
check (pkey1, "no pub key created");
check (pkey2, "could not init pkey2");
2016-06-12 20:52:22 +02:00
2016-06-13 21:09:41 +02:00
gcry_mpi_ec_mul (pkey2, skey, ec_gen, ec_ctx);
check (!brandt_ec_point_cmp (pkey1, pkey2), "pkeys do not match");
2016-06-12 20:52:22 +02:00
2016-06-13 21:09:41 +02:00
gcry_mpi_release (skey);
gcry_mpi_point_release (pkey1);
gcry_mpi_point_release (pkey2);
2016-06-12 20:52:22 +02:00
}
2016-06-13 21:01:14 +02:00
int
test_smc_zkp_dl ()
{
2016-06-13 21:09:41 +02:00
static int first = 1;
gcry_mpi_t c;
gcry_mpi_t r;
gcry_mpi_t s;
gcry_mpi_t x;
2016-06-13 21:01:14 +02:00
gcry_mpi_point_t a;
gcry_mpi_point_t g;
2016-06-13 21:09:41 +02:00
gcry_mpi_point_t v = gcry_mpi_point_new (0);
2016-06-13 21:01:14 +02:00
2016-06-13 21:09:41 +02:00
check (v, "no pub key initialized");
2016-06-13 21:01:14 +02:00
brandt_ec_keypair_create (&g, &s);
2016-06-13 21:09:41 +02:00
check (g, "no gen created");
2016-06-13 21:01:14 +02:00
if (first)
{
2016-06-13 21:09:41 +02:00
gcry_mpi_ec_mul (g, GCRYMPI_CONST_ONE, ec_gen, ec_ctx);
2016-06-13 21:01:14 +02:00
first = 0;
}
2016-06-13 21:09:41 +02:00
brandt_ec_skey_create (&x);
check (x, "no sec key created");
gcry_mpi_ec_mul (v, x, g, ec_ctx);
check (v, "no pub key created");
smc_zkp_dl (v, g, x, &a, &c, &r);
check (!smc_zkp_dl_check (v, g, a, c, r), "zkp was false, should be true");
gcry_mpi_release (c);
gcry_mpi_release (r);
gcry_mpi_release (s);
gcry_mpi_release (x);
gcry_mpi_point_release (a);
gcry_mpi_point_release (g);
gcry_mpi_point_release (v);
2016-06-13 21:01:14 +02:00
}
2016-06-12 20:52:22 +02:00
int
main (int argc, char *argv[])
{
2016-06-13 21:01:14 +02:00
int repeat = 50;
2016-06-12 20:52:22 +02:00
int i;
2016-06-13 21:09:41 +02:00
BRANDT_init ();
2016-06-12 20:52:22 +02:00
for (i = 0; i < repeat; i++)
{
2016-06-13 21:09:41 +02:00
run (test_brandt_ec_keypair_create);
run (test_smc_zkp_dl);
2016-06-12 20:52:22 +02:00
}
return ret;
}