work-around for crypto bug (to be documented more)
[oweals/gnunet.git] / src / util / crypto_bug.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2018 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file util/crypto_bug.c
23  * @brief work around unidentified public key cryptography bug
24  * @author Christian Grothoff
25  */
26
27 /**
28  * Enable work-around.  Will cause code to call #check_eddsa_key() to
29  * see if we have a bad key, and if so, create a new one.
30  */
31 #define CRYPTO_BUG 1
32
33
34 #if CRYPTO_BUG
35 /**
36  * Check if ECDH works with @a priv_dsa and this version
37  * of libgcrypt.
38  *
39  * @param priv_dsa key to check
40  * @return #GNUNET_OK if key passes
41  */
42 static int
43 check_eddsa_key (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv_dsa)
44 {
45   struct GNUNET_CRYPTO_EcdhePrivateKey *priv_ecdh;
46   struct GNUNET_CRYPTO_EddsaPublicKey id1;
47   struct GNUNET_CRYPTO_EcdhePublicKey id2;
48   struct GNUNET_HashCode dh[2];
49
50   GNUNET_CRYPTO_eddsa_key_get_public (priv_dsa,
51                                       &id1);
52   for (unsigned int j=0;j<4;j++)
53   {
54     priv_ecdh = GNUNET_CRYPTO_ecdhe_key_create ();
55     /* Extract public keys */
56     GNUNET_CRYPTO_ecdhe_key_get_public (priv_ecdh,
57                                         &id2);
58     /* Do ECDH */
59     GNUNET_assert (GNUNET_OK ==
60                    GNUNET_CRYPTO_eddsa_ecdh (priv_dsa,
61                                              &id2,
62                                              &dh[0]));
63     GNUNET_assert (GNUNET_OK ==
64                    GNUNET_CRYPTO_ecdh_eddsa (priv_ecdh,
65                                              &id1,
66                                              &dh[1]));
67     /* Check that both DH results are equal. */
68     if (0 != memcmp (&dh[0],
69                      &dh[1],
70                      sizeof (struct GNUNET_HashCode)))
71     {
72       GNUNET_break (0); /* bad EdDSA key! */
73       return GNUNET_SYSERR;
74     }
75     GNUNET_free (priv_ecdh);
76   }
77   return GNUNET_OK;
78 }
79 #endif