aboutsummaryrefslogtreecommitdiff
path: root/test_crypto.c
diff options
context:
space:
mode:
authorMarkus Teich <markus.teich@stusta.mhn.de>2016-06-13 21:01:14 +0200
committerMarkus Teich <markus.teich@stusta.mhn.de>2016-06-13 21:01:14 +0200
commit557fbe2cc32f3ff6d8c2c0c8aa272f7b55f45618 (patch)
tree1804896d2e6d2ffb14936a6570b96c65e230143f /test_crypto.c
parent745dff3ac02a97d5686270fd142a31ad6e5badb2 (diff)
downloadlibbrandt-557fbe2cc32f3ff6d8c2c0c8aa272f7b55f45618.tar.gz
libbrandt-557fbe2cc32f3ff6d8c2c0c8aa272f7b55f45618.zip
add first ZKP including test case
Diffstat (limited to 'test_crypto.c')
-rw-r--r--test_crypto.c67
1 files changed, 46 insertions, 21 deletions
diff --git a/test_crypto.c b/test_crypto.c
index 67894eb..1bfc321 100644
--- a/test_crypto.c
+++ b/test_crypto.c
@@ -1,16 +1,16 @@
1 1
2#include "brandt.h" 2#include "brandt.h"
3#include "crypto.h" 3#include "crypto.h"
4#include "smc.h"
4#include "test.h" 5#include "test.h"
5 6
7extern gcry_mpi_point_t ec_gen;
8extern gcry_ctx_t ec_ctx;
9
6int 10int
7test_brandt_ec_keypair_create () 11test_brandt_ec_keypair_create ()
8{ 12{
9 gcry_mpi_t skey; 13 gcry_mpi_t skey;
10 gcry_mpi_t x1 = gcry_mpi_new(0);
11 gcry_mpi_t x2 = gcry_mpi_new(0);
12 gcry_mpi_t y1 = gcry_mpi_new(0);
13 gcry_mpi_t y2 = gcry_mpi_new(0);
14 gcry_mpi_point_t pkey1; 14 gcry_mpi_point_t pkey1;
15 gcry_mpi_point_t pkey2 = gcry_mpi_point_new(0); 15 gcry_mpi_point_t pkey2 = gcry_mpi_point_new(0);
16 16
@@ -18,30 +18,58 @@ test_brandt_ec_keypair_create ()
18 check(skey, "no sec key created"); 18 check(skey, "no sec key created");
19 check(pkey1, "no pub key created"); 19 check(pkey1, "no pub key created");
20 check(pkey2, "could not init pkey2"); 20 check(pkey2, "could not init pkey2");
21 check(x1, "could not init x1");
22 check(x2, "could not init x2");
23 check(y1, "could not init y1");
24 check(y2, "could not init y2");
25 21
26 gcry_mpi_ec_mul(pkey2, skey, ec_gen, ec_ctx); 22 gcry_mpi_ec_mul(pkey2, skey, ec_gen, ec_ctx);
27 check(!gcry_mpi_ec_get_affine(x1, y1, pkey1, ec_ctx), "could not get affine coords for pkey1"); 23 check(!brandt_ec_point_cmp(pkey1, pkey2), "pkeys do not match");
28 check(!gcry_mpi_ec_get_affine(x2, y2, pkey2, ec_ctx), "could not get affine coords for pkey1");
29 check(!gcry_mpi_cmp(x1, x2), "x component does not match");
30 check(!gcry_mpi_cmp(y1, y2), "y component does not match");
31 24
32 gcry_mpi_release(skey); 25 gcry_mpi_release(skey);
33 gcry_mpi_release(x1);
34 gcry_mpi_release(x2);
35 gcry_mpi_release(y1);
36 gcry_mpi_release(y2);
37 gcry_mpi_point_release(pkey1); 26 gcry_mpi_point_release(pkey1);
38 gcry_mpi_point_release(pkey2); 27 gcry_mpi_point_release(pkey2);
39} 28}
40 29
41int 30int
31test_smc_zkp_dl ()
32{
33 static int first = 1;
34 gcry_mpi_t c;
35 gcry_mpi_t r;
36 gcry_mpi_t s;
37 gcry_mpi_t x;
38 gcry_mpi_point_t a;
39 gcry_mpi_point_t g;
40 gcry_mpi_point_t v = gcry_mpi_point_new(0);
41
42 check(v, "no pub key initialized");
43 brandt_ec_keypair_create (&g, &s);
44 check(g, "no gen created");
45
46 if (first)
47 {
48 gcry_mpi_ec_mul(g, GCRYMPI_CONST_ONE, ec_gen, ec_ctx);
49 first = 0;
50 }
51
52 brandt_ec_skey_create(&x);
53 check(x, "no sec key created");
54 gcry_mpi_ec_mul(v, x, g, ec_ctx);
55 check(v, "no pub key created");
56
57 smc_zkp_dl(v, g, x, &a, &c, &r);
58 check(!smc_zkp_dl_check(v, g, a, c, r), "zkp was false, should be true");
59
60 gcry_mpi_release(c);
61 gcry_mpi_release(r);
62 gcry_mpi_release(s);
63 gcry_mpi_release(x);
64 gcry_mpi_point_release(a);
65 gcry_mpi_point_release(g);
66 gcry_mpi_point_release(v);
67}
68
69int
42main (int argc, char *argv[]) 70main (int argc, char *argv[])
43{ 71{
44 int repeat = 100; 72 int repeat = 50;
45 int i; 73 int i;
46 74
47 BRANDT_init(); 75 BRANDT_init();
@@ -49,11 +77,8 @@ main (int argc, char *argv[])
49 for (i = 0; i < repeat; i++) 77 for (i = 0; i < repeat; i++)
50 { 78 {
51 run(test_brandt_ec_keypair_create); 79 run(test_brandt_ec_keypair_create);
80 run(test_smc_zkp_dl);
52 } 81 }
53 82
54 if (!ret)
55 {
56 fputs("All tests passed", stderr);
57 }
58 return ret; 83 return ret;
59} 84}