libbrandt/test_crypto.c

198 lines
5.5 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.
*/
/* For testing static functions and variables we include the whole source */
#include "crypto.c"
2016-06-12 20:52:22 +02:00
#include "brandt.h"
#include "crypto.h"
#include "test.h"
int
test_smc_2d_array ()
{
gcry_mpi_point_t **array;
uint16_t size1 = 3;
uint16_t size2 = 7;
uint16_t i, j;
array = smc_init2 (size1, size2);
check (array, "memory allocation failed");
for (i = 0; i < size1; i++)
for (j = 0; j < size2; j++)
check (array[i][j], "point has not been initialized");
smc_free2 (array, size1, size2);
}
2016-06-12 20:52:22 +02:00
2016-06-13 21:01:14 +02:00
int
test_smc_zkp_dl ()
{
gcry_mpi_t c = gcry_mpi_new (0);
gcry_mpi_t r = gcry_mpi_new (0);
gcry_mpi_t x = gcry_mpi_new (0);
gcry_mpi_point_t a = gcry_mpi_point_new (0);
gcry_mpi_point_t g = gcry_mpi_point_new (0);
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
ec_keypair_create (g, c);
2016-06-13 21:01:14 +02:00
if (0 == tests_run)
2016-06-13 21:01:14 +02:00
{
/**TODO: there has to be a better way to copy a point */
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
}
ec_keypair_create_base (v, x, g);
smc_zkp_dl (v, g, x, a, c, r);
check (!smc_zkp_dl_check (v, g, a, c, r), "zkp dl wrong");
2016-06-13 21:09:41 +02:00
check (gcry_mpi_ec_curve_point (a, ec_ctx), "not on curve");
check (gcry_mpi_ec_curve_point (g, ec_ctx), "not on curve");
check (gcry_mpi_ec_curve_point (v, ec_ctx), "not on curve");
2016-06-13 21:09:41 +02:00
gcry_mpi_release (c);
gcry_mpi_release (r);
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
}
int
test_smc_zkp_2dle ()
{
gcry_mpi_t c = gcry_mpi_new (0);
gcry_mpi_t r = gcry_mpi_new (0);
gcry_mpi_t x = gcry_mpi_new (0);
gcry_mpi_point_t a = gcry_mpi_point_new (0);
gcry_mpi_point_t b = gcry_mpi_point_new (0);
gcry_mpi_point_t g1 = gcry_mpi_point_new (0);
gcry_mpi_point_t g2 = gcry_mpi_point_new (0);
gcry_mpi_point_t v = gcry_mpi_point_new (0);
gcry_mpi_point_t w = gcry_mpi_point_new (0);
ec_keypair_create (g1, c);
ec_keypair_create (g2, c);
if (0 == tests_run)
{
/**TODO: there has to be a better way to copy a point */
gcry_mpi_ec_mul (g1, GCRYMPI_CONST_ONE, ec_gen, ec_ctx);
gcry_mpi_ec_mul (g2, GCRYMPI_CONST_ONE, ec_gen, ec_ctx);
}
ec_keypair_create_base (v, x, g1);
gcry_mpi_ec_mul (w, x, g2, ec_ctx);
smc_zkp_2dle (v, w, g1, g2, x, a, b, c, r);
check (!smc_zkp_2dle_check (v, w, g1, g2, a, b, c, r), "zkp 2dle wrong");
check (gcry_mpi_ec_curve_point (a, ec_ctx), "not on curve");
check (gcry_mpi_ec_curve_point (b, ec_ctx), "not on curve");
check (gcry_mpi_ec_curve_point (g1, ec_ctx), "not on curve");
check (gcry_mpi_ec_curve_point (g2, ec_ctx), "not on curve");
check (gcry_mpi_ec_curve_point (v, ec_ctx), "not on curve");
check (gcry_mpi_ec_curve_point (w, ec_ctx), "not on curve");
gcry_mpi_release (c);
gcry_mpi_release (r);
gcry_mpi_release (x);
gcry_mpi_point_release (a);
gcry_mpi_point_release (b);
gcry_mpi_point_release (g1);
gcry_mpi_point_release (g2);
gcry_mpi_point_release (v);
gcry_mpi_point_release (w);
}
int
test_smc_zkp_0og ()
{
gcry_mpi_t c = gcry_mpi_new (0);
gcry_mpi_t d1 = gcry_mpi_new (0);
gcry_mpi_t d2 = gcry_mpi_new (0);
gcry_mpi_t r1 = gcry_mpi_new (0);
gcry_mpi_t r2 = gcry_mpi_new (0);
gcry_mpi_point_t y = gcry_mpi_point_new (0);
gcry_mpi_point_t alpha = gcry_mpi_point_new (0);
gcry_mpi_point_t beta = gcry_mpi_point_new (0);
gcry_mpi_point_t a1 = gcry_mpi_point_new (0);
gcry_mpi_point_t a2 = gcry_mpi_point_new (0);
gcry_mpi_point_t b1 = gcry_mpi_point_new (0);
gcry_mpi_point_t b2 = gcry_mpi_point_new (0);
ec_keypair_create (y, c);
smc_zkp_0og (alpha, (tests_run % 2 ? ec_zero : ec_gen), y, beta, a1, a2, b1,
b2, c, d1, d2, r1, r2);
check (!smc_zkp_0og_check (alpha, y, beta, a1, a2, b1, b2, c, d1, d2, r1,
r2), "zkp 0og is wrong");
check (gcry_mpi_ec_curve_point (y, ec_ctx), "not on curve");
check (gcry_mpi_ec_curve_point (alpha, ec_ctx), "not on curve");
check (gcry_mpi_ec_curve_point (beta, ec_ctx), "not on curve");
check (gcry_mpi_ec_curve_point (a1, ec_ctx), "not on curve");
check (gcry_mpi_ec_curve_point (a2, ec_ctx), "not on curve");
check (gcry_mpi_ec_curve_point (b1, ec_ctx), "not on curve");
check (gcry_mpi_ec_curve_point (b2, ec_ctx), "not on curve");
gcry_mpi_release (c);
gcry_mpi_release (d1);
gcry_mpi_release (d2);
gcry_mpi_release (r1);
gcry_mpi_release (r2);
gcry_mpi_point_release (y);
gcry_mpi_point_release (alpha);
gcry_mpi_point_release (beta);
gcry_mpi_point_release (a1);
gcry_mpi_point_release (a2);
gcry_mpi_point_release (b1);
gcry_mpi_point_release (b2);
}
2016-06-12 20:52:22 +02:00
int
main (int argc, char *argv[])
{
int repeat = 8;
2016-06-12 20:52:22 +02:00
2016-06-13 21:09:41 +02:00
BRANDT_init ();
2016-06-12 20:52:22 +02:00
/* tests that need to run only once */
run (test_smc_2d_array);
for (tests_run = 0; tests_run < repeat; tests_run++)
2016-06-12 20:52:22 +02:00
{
2016-06-13 21:09:41 +02:00
run (test_smc_zkp_dl);
2016-06-15 01:58:42 +02:00
run (test_smc_zkp_2dle);
run (test_smc_zkp_0og);
2016-06-12 20:52:22 +02:00
}
return ret;
}