test_crypto_ecdh_ecdsa.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2002-2015 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/test_crypto_ecdh_ecdsa.c
  18. * @brief testcase for ECC DH key exchange with ECDSA private keys.
  19. * @author Christian Grothoff
  20. * @author Bart Polot
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include <gcrypt.h>
  25. static int
  26. test_ecdh ()
  27. {
  28. struct GNUNET_CRYPTO_EcdsaPrivateKey priv_dsa;
  29. struct GNUNET_CRYPTO_EcdhePrivateKey priv_ecdh;
  30. struct GNUNET_CRYPTO_EcdsaPublicKey id1;
  31. struct GNUNET_CRYPTO_EcdhePublicKey id2;
  32. struct GNUNET_HashCode dh[2];
  33. /* Generate keys */
  34. GNUNET_CRYPTO_ecdsa_key_create (&priv_dsa);
  35. GNUNET_CRYPTO_ecdsa_key_get_public (&priv_dsa,
  36. &id1);
  37. for (unsigned int j = 0; j < 4; j++)
  38. {
  39. fprintf (stderr, ",");
  40. GNUNET_CRYPTO_ecdhe_key_create (&priv_ecdh);
  41. /* Extract public keys */
  42. GNUNET_CRYPTO_ecdhe_key_get_public (&priv_ecdh,
  43. &id2);
  44. /* Do ECDH */
  45. GNUNET_assert (GNUNET_OK ==
  46. GNUNET_CRYPTO_ecdsa_ecdh (&priv_dsa,
  47. &id2,
  48. &dh[0]));
  49. GNUNET_assert (GNUNET_OK ==
  50. GNUNET_CRYPTO_ecdh_ecdsa (&priv_ecdh,
  51. &id1,
  52. &dh[1]));
  53. /* Check that both DH results are equal. */
  54. GNUNET_assert (0 ==
  55. GNUNET_memcmp (&dh[0],
  56. &dh[1]));
  57. }
  58. return 0;
  59. }
  60. int
  61. main (int argc, char *argv[])
  62. {
  63. if (! gcry_check_version ("1.6.0"))
  64. {
  65. fprintf (stderr,
  66. "libgcrypt has not the expected version (version %s is required).\n",
  67. "1.6.0");
  68. return 0;
  69. }
  70. if (getenv ("GNUNET_GCRYPT_DEBUG"))
  71. gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
  72. GNUNET_log_setup ("test-crypto-ecdh-ecdsa", "WARNING", NULL);
  73. for (unsigned int i = 0; i < 4; i++)
  74. {
  75. fprintf (stderr,
  76. ".");
  77. if (0 != test_ecdh ())
  78. return 1;
  79. }
  80. return 0;
  81. }
  82. /* end of test_crypto_ecdh_ecdsa.c */