aboutsummaryrefslogtreecommitdiff
path: root/test_crypto.c
diff options
context:
space:
mode:
authorMarkus Teich <markus.teich@stusta.mhn.de>2016-06-16 00:09:29 +0200
committerMarkus Teich <markus.teich@stusta.mhn.de>2016-06-17 10:29:49 +0200
commit761dd37e1f905719df2cd8f4420e4b46da80bffb (patch)
treeb93ec567a19e2407274fc3350ea51186aac8f091 /test_crypto.c
parent6f3fb463176c04c9a258fce820ec66724a4d13f4 (diff)
downloadlibbrandt-761dd37e1f905719df2cd8f4420e4b46da80bffb.tar.gz
libbrandt-761dd37e1f905719df2cd8f4420e4b46da80bffb.zip
refactor smc and ec crypto functions and ad 0og zkp
Diffstat (limited to 'test_crypto.c')
-rw-r--r--test_crypto.c160
1 files changed, 90 insertions, 70 deletions
diff --git a/test_crypto.c b/test_crypto.c
index 610dc6d..5cc2ed1 100644
--- a/test_crypto.c
+++ b/test_crypto.c
@@ -23,111 +23,85 @@
23#include "smc.h" 23#include "smc.h"
24#include "test.h" 24#include "test.h"
25 25
26extern gcry_mpi_point_t ec_gen;
27extern gcry_ctx_t ec_ctx; 26extern gcry_ctx_t ec_ctx;
28 27extern gcry_mpi_point_t ec_gen;
29int 28extern gcry_mpi_point_t ec_zero;
30test_brandt_ec_keypair_create () 29extern gcry_mpi_t ec_n;
31{
32 gcry_mpi_t skey;
33 gcry_mpi_point_t pkey1;
34 gcry_mpi_point_t pkey2 = gcry_mpi_point_new (0);
35
36 brandt_ec_keypair_create (&pkey1, &skey);
37 check (skey, "no sec key created");
38 check (pkey1, "no pub key created");
39 check (pkey2, "could not init pkey2");
40
41 gcry_mpi_ec_mul (pkey2, skey, ec_gen, ec_ctx);
42 check (!brandt_ec_point_cmp (pkey1, pkey2), "pkeys do not match");
43
44 gcry_mpi_release (skey);
45 gcry_mpi_point_release (pkey1);
46 gcry_mpi_point_release (pkey2);
47}
48 30
49int 31int
50test_smc_zkp_dl () 32test_smc_zkp_dl ()
51{ 33{
52 static int first = 1; 34 gcry_mpi_t c = gcry_mpi_new (0);
53 gcry_mpi_t c; 35 gcry_mpi_t r = gcry_mpi_new (0);
54 gcry_mpi_t r; 36 gcry_mpi_t x = gcry_mpi_new (0);
55 gcry_mpi_t s; 37 gcry_mpi_point_t a = gcry_mpi_point_new (0);
56 gcry_mpi_t x; 38 gcry_mpi_point_t g = gcry_mpi_point_new (0);
57 gcry_mpi_point_t a;
58 gcry_mpi_point_t g;
59 gcry_mpi_point_t v = gcry_mpi_point_new (0); 39 gcry_mpi_point_t v = gcry_mpi_point_new (0);
60 40
61 check (v, "no pub key initialized"); 41 brandt_ec_keypair_create (g, c);
62 brandt_ec_keypair_create (&g, &s);
63 check (g, "no gen created");
64 42
65 if (first) 43 if (0 == tests_run)
66 { 44 {
45 /**TODO: there has to be a better way to copy a point */
67 gcry_mpi_ec_mul (g, GCRYMPI_CONST_ONE, ec_gen, ec_ctx); 46 gcry_mpi_ec_mul (g, GCRYMPI_CONST_ONE, ec_gen, ec_ctx);
68 first = 0;
69 } 47 }
70 48
71 brandt_ec_skey_create (&x); 49 brandt_ec_keypair_create_base (v, x, g);
72 check (x, "no sec key created"); 50
73 gcry_mpi_ec_mul (v, x, g, ec_ctx); 51 smc_zkp_dl (v, g, x, a, c, r);
74 check (v, "no pub key created"); 52 check (!smc_zkp_dl_check (v, g, a, c, r), "zkp dl wrong");
75 53
76 smc_zkp_dl (v, g, x, &a, &c, &r); 54 check (gcry_mpi_ec_curve_point (a, ec_ctx), "not on curve");
77 check (!smc_zkp_dl_check (v, g, a, c, r), "zkp was false, should be true"); 55 check (gcry_mpi_ec_curve_point (g, ec_ctx), "not on curve");
56 check (gcry_mpi_ec_curve_point (v, ec_ctx), "not on curve");
78 57
79 gcry_mpi_release (c); 58 gcry_mpi_release (c);
80 gcry_mpi_release (r); 59 gcry_mpi_release (r);
81 gcry_mpi_release (s);
82 gcry_mpi_release (x); 60 gcry_mpi_release (x);
83 gcry_mpi_point_release (a); 61 gcry_mpi_point_release (a);
84 gcry_mpi_point_release (g); 62 gcry_mpi_point_release (g);
85 gcry_mpi_point_release (v); 63 gcry_mpi_point_release (v);
86} 64}
87 65
66
88int 67int
89test_smc_zkp_2dle () 68test_smc_zkp_2dle ()
90{ 69{
91 static int first = 1; 70 gcry_mpi_t c = gcry_mpi_new (0);
92 gcry_mpi_t c; 71 gcry_mpi_t r = gcry_mpi_new (0);
93 gcry_mpi_t r; 72 gcry_mpi_t x = gcry_mpi_new (0);
94 gcry_mpi_t s; 73 gcry_mpi_point_t a = gcry_mpi_point_new (0);
95 gcry_mpi_t x; 74 gcry_mpi_point_t b = gcry_mpi_point_new (0);
96 gcry_mpi_point_t a; 75 gcry_mpi_point_t g1 = gcry_mpi_point_new (0);
97 gcry_mpi_point_t b; 76 gcry_mpi_point_t g2 = gcry_mpi_point_new (0);
98 gcry_mpi_point_t g1;
99 gcry_mpi_point_t g2;
100 gcry_mpi_point_t v = gcry_mpi_point_new (0); 77 gcry_mpi_point_t v = gcry_mpi_point_new (0);
101 gcry_mpi_point_t w = gcry_mpi_point_new (0); 78 gcry_mpi_point_t w = gcry_mpi_point_new (0);
102 79
103 check (v, "no pub1 key initialized"); 80 brandt_ec_keypair_create (g1, c);
104 check (w, "no pub2 key initialized"); 81 brandt_ec_keypair_create (g2, c);
105 brandt_ec_keypair_create (&g1, &s);
106 gcry_mpi_release (s);
107 brandt_ec_keypair_create (&g2, &s);
108 check (g1, "no gen1 created");
109 check (g2, "no gen2 created");
110 82
111 if (first) 83 if (0 == tests_run)
112 { 84 {
85 /**TODO: there has to be a better way to copy a point */
113 gcry_mpi_ec_mul (g1, GCRYMPI_CONST_ONE, ec_gen, ec_ctx); 86 gcry_mpi_ec_mul (g1, GCRYMPI_CONST_ONE, ec_gen, ec_ctx);
114 gcry_mpi_ec_mul (g2, GCRYMPI_CONST_ONE, ec_gen, ec_ctx); 87 gcry_mpi_ec_mul (g2, GCRYMPI_CONST_ONE, ec_gen, ec_ctx);
115 first = 0;
116 } 88 }
117 89
118 brandt_ec_skey_create (&x); 90 brandt_ec_keypair_create_base (v, x, g1);
119 check (x, "no sec key created");
120 gcry_mpi_ec_mul (v, x, g1, ec_ctx);
121 check (v, "no pub1 key created");
122 gcry_mpi_ec_mul (w, x, g2, ec_ctx); 91 gcry_mpi_ec_mul (w, x, g2, ec_ctx);
123 check (w, "no pub2 key created");
124 92
125 smc_zkp_2dle (v, w, g1, g2, x, &a, &b, &c, &r); 93 smc_zkp_2dle (v, w, g1, g2, x, a, b, c, r);
126 check (!smc_zkp_2dle_check (v, w, g1, g2, a, b, c, r), "zkp was false, should be true"); 94 check (!smc_zkp_2dle_check (v, w, g1, g2, a, b, c, r), "zkp 2dle wrong");
95
96 check (gcry_mpi_ec_curve_point (a, ec_ctx), "not on curve");
97 check (gcry_mpi_ec_curve_point (b, ec_ctx), "not on curve");
98 check (gcry_mpi_ec_curve_point (g1, ec_ctx), "not on curve");
99 check (gcry_mpi_ec_curve_point (g2, ec_ctx), "not on curve");
100 check (gcry_mpi_ec_curve_point (v, ec_ctx), "not on curve");
101 check (gcry_mpi_ec_curve_point (w, ec_ctx), "not on curve");
127 102
128 gcry_mpi_release (c); 103 gcry_mpi_release (c);
129 gcry_mpi_release (r); 104 gcry_mpi_release (r);
130 gcry_mpi_release (s);
131 gcry_mpi_release (x); 105 gcry_mpi_release (x);
132 gcry_mpi_point_release (a); 106 gcry_mpi_point_release (a);
133 gcry_mpi_point_release (b); 107 gcry_mpi_point_release (b);
@@ -137,19 +111,65 @@ test_smc_zkp_2dle ()
137 gcry_mpi_point_release (w); 111 gcry_mpi_point_release (w);
138} 112}
139 113
114
115int
116test_smc_zkp_0og ()
117{
118 gcry_mpi_t c = gcry_mpi_new (0);
119 gcry_mpi_t d1 = gcry_mpi_new (0);
120 gcry_mpi_t d2 = gcry_mpi_new (0);
121 gcry_mpi_t r1 = gcry_mpi_new (0);
122 gcry_mpi_t r2 = gcry_mpi_new (0);
123 gcry_mpi_point_t y = gcry_mpi_point_new (0);
124 gcry_mpi_point_t alpha = gcry_mpi_point_new (0);
125 gcry_mpi_point_t beta = gcry_mpi_point_new (0);
126 gcry_mpi_point_t a1 = gcry_mpi_point_new (0);
127 gcry_mpi_point_t a2 = gcry_mpi_point_new (0);
128 gcry_mpi_point_t b1 = gcry_mpi_point_new (0);
129 gcry_mpi_point_t b2 = gcry_mpi_point_new (0);
130
131 brandt_ec_keypair_create (y, c);
132
133 smc_zkp_0og (alpha, (tests_run % 2 ? ec_zero : ec_gen), y, beta, a1, a2, b1,
134 b2, c, d1, d2, r1, r2);
135 check (!smc_zkp_0og_check (alpha, y, beta, a1, a2, b1, b2, c, d1, d2, r1,
136 r2), "zkp 0og is wrong");
137
138 check (gcry_mpi_ec_curve_point (y, ec_ctx), "not on curve");
139 check (gcry_mpi_ec_curve_point (alpha, ec_ctx), "not on curve");
140 check (gcry_mpi_ec_curve_point (beta, ec_ctx), "not on curve");
141 check (gcry_mpi_ec_curve_point (a1, ec_ctx), "not on curve");
142 check (gcry_mpi_ec_curve_point (a2, ec_ctx), "not on curve");
143 check (gcry_mpi_ec_curve_point (b1, ec_ctx), "not on curve");
144 check (gcry_mpi_ec_curve_point (b2, ec_ctx), "not on curve");
145
146 gcry_mpi_release (c);
147 gcry_mpi_release (d1);
148 gcry_mpi_release (d2);
149 gcry_mpi_release (r1);
150 gcry_mpi_release (r2);
151 gcry_mpi_point_release (y);
152 gcry_mpi_point_release (alpha);
153 gcry_mpi_point_release (beta);
154 gcry_mpi_point_release (a1);
155 gcry_mpi_point_release (a2);
156 gcry_mpi_point_release (b1);
157 gcry_mpi_point_release (b2);
158}
159
160
140int 161int
141main (int argc, char *argv[]) 162main (int argc, char *argv[])
142{ 163{
143 int repeat = 50; 164 int repeat = 32;
144 int i;
145 165
146 BRANDT_init (); 166 BRANDT_init ();
147 167
148 for (i = 0; i < repeat; i++) 168 for (tests_run = 0; tests_run < repeat; tests_run++)
149 { 169 {
150 run (test_brandt_ec_keypair_create);
151 run (test_smc_zkp_dl); 170 run (test_smc_zkp_dl);
152 run (test_smc_zkp_2dle); 171 run (test_smc_zkp_2dle);
172 run (test_smc_zkp_0og);
153 } 173 }
154 174
155 return ret; 175 return ret;