aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_crypto_ecdh_ecdsa.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2015-03-19 13:12:47 +0000
committerChristian Grothoff <christian@grothoff.org>2015-03-19 13:12:47 +0000
commit56af9c4ba537faf4c0a4a3acf8447a49673eec64 (patch)
tree1a98bea2666fb86bef94d361199c3d01fde93db9 /src/util/test_crypto_ecdh_ecdsa.c
parentd86130ceeb0e9f7ef7adf192b8f44bf94398b466 (diff)
downloadgnunet-56af9c4ba537faf4c0a4a3acf8447a49673eec64.tar.gz
gnunet-56af9c4ba537faf4c0a4a3acf8447a49673eec64.zip
-get test to work, but with ecdsa instead of eddsa
Diffstat (limited to 'src/util/test_crypto_ecdh_ecdsa.c')
-rw-r--r--src/util/test_crypto_ecdh_ecdsa.c141
1 files changed, 141 insertions, 0 deletions
diff --git a/src/util/test_crypto_ecdh_ecdsa.c b/src/util/test_crypto_ecdh_ecdsa.c
new file mode 100644
index 000000000..8eb2055ae
--- /dev/null
+++ b/src/util/test_crypto_ecdh_ecdsa.c
@@ -0,0 +1,141 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2002-2015 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19
20*/
21/**
22 * @file util/test_crypto_ecdh_ecdsa.c
23 * @brief testcase for ECC DH key exchange with EdDSA private keys.
24 * @author Christian Grothoff
25 * @author Bart Polot
26 */
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include <gcrypt.h>
30
31
32static int
33test_pk()
34{
35 struct GNUNET_CRYPTO_EcdsaPrivateKey *priv1;
36 struct GNUNET_CRYPTO_EcdhePrivateKey priv2;
37 struct GNUNET_CRYPTO_EcdsaPublicKey pub1;
38 struct GNUNET_CRYPTO_EcdhePublicKey pub2;
39 struct GNUNET_CRYPTO_EcdhePublicKey pub1c;
40
41 /* Generate, cast keys */
42 priv1 = GNUNET_CRYPTO_ecdsa_key_create ();
43 memcpy (&priv2,
44 priv1,
45 sizeof (priv2));
46
47 /* Extract public keys */
48 GNUNET_CRYPTO_ecdsa_key_get_public (priv1, &pub1);
49 GNUNET_CRYPTO_ecdhe_key_get_public (&priv2, &pub2);
50
51 GNUNET_CRYPTO_ecdsa_public_to_ecdhe (&pub1, &pub1c);
52 if (0 == memcmp (&pub1c,
53 &pub2,
54 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
55 {
56 GNUNET_free (priv1);
57 return 0;
58 }
59 GNUNET_free (priv1);
60 return 1;
61}
62
63
64static int
65test_ecdh()
66{
67 struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_dsa1;
68 struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_dsa2;
69 struct GNUNET_CRYPTO_EcdsaPublicKey id1;
70 struct GNUNET_CRYPTO_EcdsaPublicKey id2;
71 struct GNUNET_CRYPTO_EcdhePublicKey id1c;
72 struct GNUNET_CRYPTO_EcdhePublicKey id2c;
73
74 struct GNUNET_CRYPTO_EcdhePrivateKey *priv1;
75 struct GNUNET_CRYPTO_EcdhePrivateKey *priv2;
76 struct GNUNET_CRYPTO_EcdhePublicKey pub2;
77 struct GNUNET_HashCode dh[3];
78
79 /* Generate, cast keys */
80 priv_dsa1 = GNUNET_CRYPTO_ecdsa_key_create ();
81 priv_dsa2 = GNUNET_CRYPTO_ecdsa_key_create ();
82 priv1 = (struct GNUNET_CRYPTO_EcdhePrivateKey *) priv_dsa1;
83 priv2 = (struct GNUNET_CRYPTO_EcdhePrivateKey *) priv_dsa2;
84
85 /* Extract public keys */
86 GNUNET_CRYPTO_ecdsa_key_get_public (priv_dsa1, &id1);
87 GNUNET_CRYPTO_ecdsa_key_get_public (priv_dsa2, &id2);
88 GNUNET_CRYPTO_ecdhe_key_get_public (priv2, &pub2);
89
90 /* Do ECDH */
91 GNUNET_CRYPTO_ecdsa_public_to_ecdhe (&id2,
92 &id2c);
93 GNUNET_CRYPTO_ecdsa_public_to_ecdhe (&id1,
94 &id1c);
95 GNUNET_CRYPTO_ecc_ecdh (priv1,
96 &id2c,
97 &dh[0]);
98 GNUNET_CRYPTO_ecc_ecdh (priv2,
99 &id1c,
100 &dh[1]);
101 GNUNET_CRYPTO_ecc_ecdh (priv1, &pub2, &dh[2]);
102
103 /* Check that both DH results are equal. */
104 GNUNET_assert (0 == memcmp (&dh[0], &dh[1],
105 sizeof (struct GNUNET_HashCode)));
106
107 /* FIXME: Maybe it should be the same as with ECDHE. */
108 // GNUNET_assert (0 == memcmp (&dh[1], &dh[2],
109 // sizeof (struct GNUNET_HashCode)));
110 // GNUNET_assert (0 == memcmp (&id1, &pub1,
111 // sizeof (struct GNUNET_CRYPTO_EcdhePublicKey)));
112
113 /* Free */
114 GNUNET_free (priv1);
115 GNUNET_free (priv2);
116 return 0;
117}
118
119
120int
121main (int argc, char *argv[])
122{
123 if (! gcry_check_version ("1.6.0"))
124 {
125 FPRINTF (stderr,
126 _("libgcrypt has not the expected version (version %s is required).\n"),
127 "1.6.0");
128 return 0;
129 }
130 if (getenv ("GNUNET_GCRYPT_DEBUG"))
131 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
132 GNUNET_log_setup ("test-crypto-ecdh-ecdsa", "WARNING", NULL);
133 if (0 != test_pk())
134 return 1;
135 if (0 != test_ecdh())
136 return 1;
137 return 0;
138}
139
140
141/* end of test_crypto_ecdh_ecdsa.c */