2 This file is part of GNUnet.
3 Copyright (C) 2002-2013 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file util/test_crypto_eddsa.c
23 * @brief testcase for ECC public key crypto
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_signatures.h"
33 #define KEYFILE "/tmp/test-gnunet-crypto-eddsa.key"
35 #define PERF GNUNET_YES
38 static struct GNUNET_CRYPTO_EddsaPrivateKey *key;
44 struct GNUNET_CRYPTO_EddsaSignature sig;
45 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
46 struct GNUNET_CRYPTO_EddsaPublicKey pkey;
48 struct GNUNET_TIME_Absolute start;
51 fprintf(stderr, "%s", "W");
52 GNUNET_CRYPTO_eddsa_key_get_public(key, &pkey);
53 start = GNUNET_TIME_absolute_get();
54 purp.size = htonl(sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
55 purp.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_TEST);
57 for (i = 0; i < ITER; i++)
59 fprintf(stderr, "%s", "."); fflush(stderr);
60 if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign(key, &purp, &sig))
62 fprintf(stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
67 GNUNET_CRYPTO_eddsa_verify(GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
70 printf("GNUNET_CRYPTO_eddsa_verify failed!\n");
75 GNUNET_CRYPTO_eddsa_verify(GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
78 printf("GNUNET_CRYPTO_eddsa_verify failed to fail!\n");
83 printf("%d EdDSA sign/verify operations %s\n", ITER,
84 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), GNUNET_YES));
93 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
94 struct GNUNET_CRYPTO_EddsaSignature sig;
95 struct GNUNET_CRYPTO_EddsaPublicKey pkey;
97 struct GNUNET_TIME_Absolute start;
100 purp.size = htonl(sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
101 purp.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_TEST);
102 fprintf(stderr, "%s", "W");
103 GNUNET_CRYPTO_eddsa_key_get_public(key, &pkey);
104 start = GNUNET_TIME_absolute_get();
105 for (i = 0; i < ITER; i++)
107 fprintf(stderr, "%s", "."); fflush(stderr);
108 if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign(key, &purp, &sig))
110 fprintf(stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
115 printf("%d EdDSA sign operations %s\n", ITER,
116 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start),
126 struct GNUNET_CRYPTO_EddsaPublicKey p1;
127 struct GNUNET_CRYPTO_EddsaPublicKey p2;
129 key = GNUNET_CRYPTO_eddsa_key_create_from_file(KEYFILE);
130 GNUNET_assert(NULL != key);
131 GNUNET_CRYPTO_eddsa_key_get_public(key, &p1);
133 key = GNUNET_CRYPTO_eddsa_key_create_from_file(KEYFILE);
134 GNUNET_assert(NULL != key);
135 GNUNET_CRYPTO_eddsa_key_get_public(key, &p2);
136 GNUNET_assert(0 == memcmp(&p1, &p2, sizeof(p1)));
138 GNUNET_assert(0 == unlink(KEYFILE));
139 key = GNUNET_CRYPTO_eddsa_key_create_from_file(KEYFILE);
140 GNUNET_assert(NULL != key);
141 GNUNET_CRYPTO_eddsa_key_get_public(key, &p2);
142 GNUNET_assert(0 != memcmp(&p1, &p2, sizeof(p1)));
151 struct GNUNET_TIME_Absolute start;
152 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
155 fprintf(stderr, "%s", "W");
156 start = GNUNET_TIME_absolute_get();
157 for (i = 0; i < 10; i++)
159 fprintf(stderr, "."); fflush(stderr);
160 pk = GNUNET_CRYPTO_eddsa_key_create();
164 fprintf(stderr, ".");
166 printf("10 EdDSA keys created in %s\n",
167 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), GNUNET_YES));
172 main(int argc, char *argv[])
174 int failure_count = 0;
176 if (!gcry_check_version("1.6.0"))
179 _("libgcrypt has not the expected version (version %s is required).\n"),
183 if (getenv("GNUNET_GCRYPT_DEBUG"))
184 gcry_control(GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
185 GNUNET_log_setup("test-crypto-eddsa", "WARNING", NULL);
186 key = GNUNET_CRYPTO_eddsa_key_create();
188 if (GNUNET_OK != testSignPerformance())
191 if (GNUNET_OK != testSignVerify())
194 if (GNUNET_OK != testCreateFromFile())
196 GNUNET_assert(0 == unlink(KEYFILE));
199 if (0 != failure_count)
202 "\n\n%d TESTS FAILED!\n\n",
209 /* end of test_crypto_eddsa.c */