b7b1634e411aa479f5cc55bb4eeea68360b3be50
[oweals/gnunet.git] / src / util / test_crypto_ecdh_eddsa.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2002-2015 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 */
19 /**
20  * @file util/test_crypto_ecdh_eddsa.c
21  * @brief testcase for ECC DH key exchange with EdDSA private keys.
22  * @author Christian Grothoff
23  * @author Bart Polot
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include <gcrypt.h>
28
29
30 static int
31 test_ecdh()
32 {
33   struct GNUNET_CRYPTO_EddsaPrivateKey *priv_dsa;
34   struct GNUNET_CRYPTO_EcdhePrivateKey *priv_ecdh;
35   struct GNUNET_CRYPTO_EddsaPublicKey id1;
36   struct GNUNET_CRYPTO_EcdhePublicKey id2;
37   struct GNUNET_HashCode dh[2];
38
39   /* Generate keys */
40   priv_dsa = GNUNET_CRYPTO_eddsa_key_create ();
41   GNUNET_CRYPTO_eddsa_key_get_public (priv_dsa,
42                                       &id1);
43   for (unsigned int j=0;j<4;j++)
44   {
45     fprintf (stderr, ",");
46     priv_ecdh = GNUNET_CRYPTO_ecdhe_key_create ();
47     /* Extract public keys */
48     GNUNET_CRYPTO_ecdhe_key_get_public (priv_ecdh,
49                                         &id2);
50     /* Do ECDH */
51     GNUNET_assert (GNUNET_OK ==
52                    GNUNET_CRYPTO_eddsa_ecdh (priv_dsa,
53                                              &id2,
54                                              &dh[0]));
55     GNUNET_assert (GNUNET_OK ==
56                    GNUNET_CRYPTO_ecdh_eddsa (priv_ecdh,
57                                              &id1,
58                                              &dh[1]));
59     /* Check that both DH results are equal. */
60     GNUNET_assert (0 == memcmp (&dh[0],
61                                 &dh[1],
62                                 sizeof (struct GNUNET_HashCode)));
63     GNUNET_free (priv_ecdh);
64   }
65   GNUNET_free (priv_dsa);
66   return 0;
67 }
68
69
70 int
71 main (int argc, char *argv[])
72 {
73   if (! gcry_check_version ("1.6.0"))
74   {
75     FPRINTF (stderr,
76              _("libgcrypt has not the expected version (version %s is required).\n"),
77              "1.6.0");
78     return 0;
79   }
80   if (getenv ("GNUNET_GCRYPT_DEBUG"))
81     gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
82   GNUNET_log_setup ("test-crypto-ecdh-eddsa", "WARNING", NULL);
83   for (unsigned int i=0;i<4;i++)
84   {
85     fprintf (stderr,
86              ".");
87     if (0 != test_ecdh())
88       return 1;
89   }
90   return 0;
91 }
92
93
94 /* end of test_crypto_ecdh_eddsa.c */