aboutsummaryrefslogtreecommitdiff
path: root/test_crypto.c
blob: 610dc6d6813af43fe61d761824a33f211b9ebbed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* 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.
 */
#include "brandt.h"
#include "crypto.h"
#include "smc.h"
#include "test.h"

extern gcry_mpi_point_t ec_gen;
extern gcry_ctx_t       ec_ctx;

int
test_brandt_ec_keypair_create ()
{
	gcry_mpi_t       skey;
	gcry_mpi_point_t pkey1;
	gcry_mpi_point_t pkey2 = gcry_mpi_point_new (0);

	brandt_ec_keypair_create (&pkey1, &skey);
	check (skey, "no sec key created");
	check (pkey1, "no pub key created");
	check (pkey2, "could not init pkey2");

	gcry_mpi_ec_mul (pkey2, skey, ec_gen, ec_ctx);
	check (!brandt_ec_point_cmp (pkey1, pkey2), "pkeys do not match");

	gcry_mpi_release (skey);
	gcry_mpi_point_release (pkey1);
	gcry_mpi_point_release (pkey2);
}

int
test_smc_zkp_dl ()
{
	static int       first = 1;
	gcry_mpi_t       c;
	gcry_mpi_t       r;
	gcry_mpi_t       s;
	gcry_mpi_t       x;
	gcry_mpi_point_t a;
	gcry_mpi_point_t g;
	gcry_mpi_point_t v = gcry_mpi_point_new (0);

	check (v, "no pub key initialized");
	brandt_ec_keypair_create (&g, &s);
	check (g, "no gen created");

	if (first)
	{
		gcry_mpi_ec_mul (g, GCRYMPI_CONST_ONE, ec_gen, ec_ctx);
		first = 0;
	}

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

int
test_smc_zkp_2dle ()
{
	static int       first = 1;
	gcry_mpi_t       c;
	gcry_mpi_t       r;
	gcry_mpi_t       s;
	gcry_mpi_t       x;
	gcry_mpi_point_t a;
	gcry_mpi_point_t b;
	gcry_mpi_point_t g1;
	gcry_mpi_point_t g2;
	gcry_mpi_point_t v = gcry_mpi_point_new (0);
	gcry_mpi_point_t w = gcry_mpi_point_new (0);

	check (v, "no pub1 key initialized");
	check (w, "no pub2 key initialized");
	brandt_ec_keypair_create (&g1, &s);
	gcry_mpi_release (s);
	brandt_ec_keypair_create (&g2, &s);
	check (g1, "no gen1 created");
	check (g2, "no gen2 created");

	if (first)
	{
		gcry_mpi_ec_mul (g1, GCRYMPI_CONST_ONE, ec_gen, ec_ctx);
		gcry_mpi_ec_mul (g2, GCRYMPI_CONST_ONE, ec_gen, ec_ctx);
		first = 0;
	}

	brandt_ec_skey_create (&x);
	check (x, "no sec key created");
	gcry_mpi_ec_mul (v, x, g1, ec_ctx);
	check (v, "no pub1 key created");
	gcry_mpi_ec_mul (w, x, g2, ec_ctx);
	check (w, "no pub2 key created");

	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 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 (b);
	gcry_mpi_point_release (g1);
	gcry_mpi_point_release (g2);
	gcry_mpi_point_release (v);
	gcry_mpi_point_release (w);
}

int
main (int argc, char *argv[])
{
	int repeat = 50;
	int i;

	BRANDT_init ();

	for (i = 0; i < repeat; i++)
	{
		run (test_brandt_ec_keypair_create);
		run (test_smc_zkp_dl);
		run (test_smc_zkp_2dle);
	}

	return ret;
}