- refactor kx sending, unify under send_kx
[oweals/gnunet.git] / src / util / test_crypto_ecdh_ecdsa.c
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
32 static int
33 test_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   GNUNET_CRYPTO_ecdsa_private_to_ecdhe (priv1,
44                                         &priv2);
45   /* Extract public keys */
46   GNUNET_CRYPTO_ecdsa_key_get_public (priv1, &pub1);
47   GNUNET_CRYPTO_ecdhe_key_get_public (&priv2, &pub2);
48
49   GNUNET_CRYPTO_ecdsa_public_to_ecdhe (&pub1, &pub1c);
50   if (0 == memcmp (&pub1c,
51                    &pub2,
52                    sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
53   {
54     GNUNET_free (priv1);
55     return 0;
56   }
57   GNUNET_free (priv1);
58   return 1;
59 }
60
61
62 static int
63 test_ecdh()
64 {
65   struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_dsa1;
66   struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_dsa2;
67   struct GNUNET_CRYPTO_EcdsaPublicKey id1;
68   struct GNUNET_CRYPTO_EcdsaPublicKey id2;
69   struct GNUNET_CRYPTO_EcdhePublicKey id1c;
70   struct GNUNET_CRYPTO_EcdhePublicKey id2c;
71
72   struct GNUNET_CRYPTO_EcdhePrivateKey priv1;
73   struct GNUNET_CRYPTO_EcdhePrivateKey priv2;
74   struct GNUNET_CRYPTO_EcdhePublicKey pub2;
75   struct GNUNET_HashCode dh[3];
76
77   /* Generate, cast keys */
78   priv_dsa1 = GNUNET_CRYPTO_ecdsa_key_create ();
79   priv_dsa2 = GNUNET_CRYPTO_ecdsa_key_create ();
80   GNUNET_CRYPTO_ecdsa_private_to_ecdhe (priv_dsa1,
81                                         &priv1);
82
83   GNUNET_CRYPTO_ecdsa_private_to_ecdhe (priv_dsa2,
84                                         &priv2);
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   GNUNET_free (priv_dsa1);
107   GNUNET_free (priv_dsa2);
108   return 0;
109 }
110
111
112 int
113 main (int argc, char *argv[])
114 {
115   if (! gcry_check_version ("1.6.0"))
116   {
117     FPRINTF (stderr,
118              _("libgcrypt has not the expected version (version %s is required).\n"),
119              "1.6.0");
120     return 0;
121   }
122   if (getenv ("GNUNET_GCRYPT_DEBUG"))
123     gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
124   GNUNET_log_setup ("test-crypto-ecdh-ecdsa", "WARNING", NULL);
125   if (0 != test_pk())
126     return 1;
127   if (0 != test_ecdh())
128     return 1;
129   return 0;
130 }
131
132
133 /* end of test_crypto_ecdh_ecdsa.c */