GNUnet  0.11.x
gnunet-crypto-tvg.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2020 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  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_signatures.h"
29 #include "gnunet_testing_lib.h"
30 #include <gcrypt.h>
31 
33 
40 {
42  uint32_t testval;
43 };
44 
46 
47 
55 static void
56 display_data (char *label, void *data, size_t size)
57 {
58  char *enc = GNUNET_STRINGS_data_to_string_alloc (data, size);
59  printf ("%s %s\n", label, enc);
60  GNUNET_free (enc);
61 }
62 
63 
72 static void
73 run (void *cls,
74  char *const *args,
75  const char *cfgfile,
76  const struct GNUNET_CONFIGURATION_Handle *cfg)
77 {
78  {
79  struct GNUNET_HashCode hc;
80  char *str = "Hello, GNUnet";
81 
82  GNUNET_CRYPTO_hash (str, strlen (str), &hc);
83 
84  printf ("hash code:\n");
85  display_data (" input", str, strlen (str));
86  display_data (" output", &hc, sizeof (struct GNUNET_HashCode));
87  }
88  {
89  struct GNUNET_CRYPTO_EcdhePrivateKey *priv1;
90  struct GNUNET_CRYPTO_EcdhePublicKey pub1;
91  struct GNUNET_CRYPTO_EcdhePrivateKey *priv2;
92  struct GNUNET_HashCode skm;
96  GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecc_ecdh (priv2, &pub1, &skm));
97 
98  printf ("ecdhe key:\n");
99  display_data (" priv1", priv1, sizeof (struct
101  display_data (" pub1", &pub1, sizeof (struct
103  display_data (" priv2", priv2, sizeof (struct
105  display_data (" skm", &skm, sizeof (struct GNUNET_HashCode));
106  GNUNET_free (priv1);
107  GNUNET_free (priv2);
108  }
109 
110  {
111  struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
112  struct GNUNET_CRYPTO_EddsaPublicKey pub;
115 
116  printf ("eddsa key:\n");
117  display_data (" priv", priv, sizeof (struct
119  display_data (" pub", &pub, sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
120  GNUNET_free (priv);
121  }
122  {
123  struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
124  struct GNUNET_CRYPTO_EddsaPublicKey pub;
125  struct GNUNET_CRYPTO_EddsaSignature sig;
126  struct TestSignatureDataPS data = { 0 };
129  data.purpose.size = htonl (sizeof (struct TestSignatureDataPS));
132  &sig));
134  &data.purpose,
135  &sig,
136  &pub));
137 
138  printf ("eddsa sig:\n");
139  display_data (" priv", priv, sizeof (struct
141  display_data (" pub", &pub, sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
142  display_data (" data", &data, sizeof (struct TestSignatureDataPS));
143  display_data (" sig", &sig, sizeof (struct GNUNET_CRYPTO_EddsaSignature));
144  GNUNET_free (priv);
145  }
146 
147  {
148  size_t out_len = 64;
149  char out[out_len];
150  char *ikm = "I'm the secret input key material";
151  char *salt = "I'm very salty";
152  char *ctx = "I'm a context chunk, also known as 'info' in the RFC";
153 
155  out_len,
156  salt,
157  strlen (salt),
158  ikm,
159  strlen (ikm),
160  ctx,
161  strlen (ctx),
162  NULL));
163 
164  printf ("kdf:\n");
165  display_data (" salt", salt, strlen (salt));
166  display_data (" ikm", ikm, strlen (ikm));
167  display_data (" ctx", ctx, strlen (ctx));
168  printf (" out_len %u\n", (unsigned int) out_len);
169  display_data (" out", out, out_len);
170  }
171  {
172  struct GNUNET_CRYPTO_EcdhePrivateKey *priv_ecdhe;
173  struct GNUNET_CRYPTO_EcdhePublicKey pub_ecdhe;
174  struct GNUNET_CRYPTO_EddsaPrivateKey *priv_eddsa;
175  struct GNUNET_CRYPTO_EddsaPublicKey pub_eddsa;
176  struct GNUNET_HashCode key_material;
177  priv_ecdhe = GNUNET_CRYPTO_ecdhe_key_create ();
178  GNUNET_CRYPTO_ecdhe_key_get_public (priv_ecdhe, &pub_ecdhe);
179  priv_eddsa = GNUNET_CRYPTO_eddsa_key_create ();
180  GNUNET_CRYPTO_eddsa_key_get_public (priv_eddsa, &pub_eddsa);
181  GNUNET_CRYPTO_ecdh_eddsa (priv_ecdhe, &pub_eddsa, &key_material);
182 
183  printf ("eddsa_ecdh:\n");
184  display_data (" priv_ecdhe", priv_ecdhe, sizeof (struct
186  display_data (" pub_ecdhe", &pub_ecdhe, sizeof (struct
188  display_data (" priv_eddsa", priv_eddsa, sizeof (struct
190  display_data (" pub_eddsa", &pub_eddsa, sizeof (struct
192  display_data (" key_material", &key_material, sizeof (struct
193  GNUNET_HashCode));
194  }
195 
196  {
197  struct GNUNET_CRYPTO_RsaPrivateKey *skey;
199  struct GNUNET_HashCode message_hash;
201  struct GNUNET_CRYPTO_RsaSignature *blinded_sig;
202  struct GNUNET_CRYPTO_RsaSignature *sig;
203  char *blinded_data;
204  size_t blinded_len;
205  char *public_enc_data;
206  size_t public_enc_len;
207  char *blinded_sig_enc_data;
208  size_t blinded_sig_enc_length;
209  char *sig_enc_data;
210  size_t sig_enc_length;
214  sizeof (struct GNUNET_HashCode));
217  GNUNET_assert (GNUNET_YES == GNUNET_CRYPTO_rsa_blind (&message_hash, &bks,
218  pkey, &blinded_data,
219  &blinded_len));
220  blinded_sig = GNUNET_CRYPTO_rsa_sign_blinded (skey, blinded_data,
221  blinded_len);
222  sig = GNUNET_CRYPTO_rsa_unblind (blinded_sig, &bks, pkey);
223  GNUNET_assert (GNUNET_YES == GNUNET_CRYPTO_rsa_verify (&message_hash, sig,
224  pkey));
225  public_enc_len = GNUNET_CRYPTO_rsa_public_key_encode (pkey,
226  &public_enc_data);
227  blinded_sig_enc_length = GNUNET_CRYPTO_rsa_signature_encode (blinded_sig,
228  &
229  blinded_sig_enc_data);
230  sig_enc_length = GNUNET_CRYPTO_rsa_signature_encode (sig, &sig_enc_data);
231  printf ("blind signing:\n");
232  display_data (" message_hash", &message_hash, sizeof (struct
233  GNUNET_HashCode));
234  display_data (" rsa_public_key", public_enc_data, public_enc_len);
235  display_data (" blinding_key_secret", &bks, sizeof (struct
237  display_data (" blinded_message", blinded_data, blinded_len);
238  display_data (" blinded_sig", blinded_sig_enc_data,
239  blinded_sig_enc_length);
240  display_data (" sig", sig_enc_data, sig_enc_length);
244  GNUNET_CRYPTO_rsa_signature_free (blinded_sig);
245  }
246 }
247 
248 
256 int
257 main (int argc,
258  char *const *argv)
259 {
260  const struct GNUNET_GETOPT_CommandLineOption options[] = {
262  };
263 
265  GNUNET_log_setup ("gnunet-crypto-tvg",
266  "INFO",
267  NULL));
268  if (GNUNET_OK !=
269  GNUNET_PROGRAM_run (argc, argv,
270  "gnunet-crypto-tvg",
271  "Generate test vectors for cryptographic operations",
272  options,
273  &run, NULL))
274  return 1;
275  return 0;
276 }
277 
278 /* end of gnunet-crypto-tvg.c */
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static char * pkey
Public key of the zone to look in, in ASCII.
an RSA signature
Definition: crypto_rsa.c:63
void GNUNET_CRYPTO_rsa_signature_free(struct GNUNET_CRYPTO_RsaSignature *sig)
Free memory occupied by signature.
Definition: crypto_rsa.c:1058
size_t GNUNET_CRYPTO_rsa_public_key_encode(const struct GNUNET_CRYPTO_RsaPublicKey *key, char **buffer)
Encode the public key in a format suitable for storing it into a file.
Definition: crypto_rsa.c:346
size_t GNUNET_CRYPTO_rsa_signature_encode(const struct GNUNET_CRYPTO_RsaSignature *sig, char **buffer)
Encode the given signature in a format suitable for storing it into a file.
Definition: crypto_rsa.c:1073
static GNUNET_NETWORK_STRUCT_END void display_data(char *label, void *data, size_t size)
Print data base32-crockford with a preceding label.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
int GNUNET_CRYPTO_eddsa_sign(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, struct GNUNET_CRYPTO_EddsaSignature *sig)
EdDSA sign a given block.
Definition: crypto_ecc.c:745
int GNUNET_CRYPTO_ecc_ecdh(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *pub, struct GNUNET_HashCode *key_material)
Derive key material from a public and a private ECC key.
Definition: crypto_ecc.c:875
struct GNUNET_CRYPTO_EcdhePrivateKey * GNUNET_CRYPTO_ecdhe_key_create(void)
Create a new private key.
Definition: crypto_ecc.c:499
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
Definition of a command line option.
struct GNUNET_CRYPTO_RsaPrivateKey * GNUNET_CRYPTO_rsa_private_key_create(unsigned int len)
Create a new private key.
Definition: crypto_rsa.c:149
The public information of an RSA key pair.
Definition: crypto_rsa.c:51
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
#define GNUNET_NETWORK_STRUCT_BEGIN
Define as empty, GNUNET_PACKED should suffice, but this won&#39;t work on W32.
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
int GNUNET_CRYPTO_rsa_verify(const struct GNUNET_HashCode *hash, const struct GNUNET_CRYPTO_RsaSignature *sig, const struct GNUNET_CRYPTO_RsaPublicKey *public_key)
Verify whether the given hash corresponds to the given signature and the signature is valid with resp...
Definition: crypto_rsa.c:1282
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
struct GNUNET_CRYPTO_EddsaPrivateKey * GNUNET_CRYPTO_eddsa_key_create(void)
Create a new private key.
Definition: crypto_ecc.c:594
int GNUNET_CRYPTO_eddsa_verify(uint32_t purpose, const struct GNUNET_CRYPTO_EccSignaturePurpose *validate, const struct GNUNET_CRYPTO_EddsaSignature *sig, const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Verify EdDSA signature.
Definition: crypto_ecc.c:844
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:48
#define GNUNET_SIGNATURE_PURPOSE_TEST
Test signature, not valid for anything other than writing a test.
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!)...
A 512-bit hashcode.
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run.
int GNUNET_CRYPTO_ecdh_eddsa(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, const struct GNUNET_CRYPTO_EddsaPublicKey *pub, struct GNUNET_HashCode *key_material)
Derive key material from a EdDSA public key and a private ECDH key.
Definition: crypto_ecc.c:1101
struct GNUNET_CRYPTO_RsaSignature * GNUNET_CRYPTO_rsa_unblind(const struct GNUNET_CRYPTO_RsaSignature *sig, const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks, struct GNUNET_CRYPTO_RsaPublicKey *pkey)
Unblind a blind-signed signature.
Definition: crypto_rsa.c:1192
Private ECC key encoded for transmission.
int main(int argc, char *const *argv)
The main function of the test vector generation tool.
static unsigned int size
Size of the "table".
Definition: peer.c:67
void GNUNET_CRYPTO_ecdhe_key_get_public(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, struct GNUNET_CRYPTO_EcdhePublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:206
an ECC signature using EdDSA.
void GNUNET_CRYPTO_eddsa_key_get_public(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:189
int GNUNET_CRYPTO_rsa_blind(const struct GNUNET_HashCode *hash, const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks, struct GNUNET_CRYPTO_RsaPublicKey *pkey, char **buf, size_t *buf_size)
Blinds the given message with the given blinding key.
Definition: crypto_rsa.c:841
#define GNUNET_NETWORK_STRUCT_END
Define as empty, GNUNET_PACKED should suffice, but this won&#39;t work on W32;.
Private ECC key encoded for transmission.
configuration data
Definition: configuration.c:84
static OpusEncoder * enc
OPUS encoder.
struct GNUNET_CRYPTO_RsaSignature * GNUNET_CRYPTO_rsa_sign_blinded(const struct GNUNET_CRYPTO_RsaPrivateKey *key, const void *msg, size_t msg_len)
Sign a blinded value, which must be a full domain hash of a message.
Definition: crypto_rsa.c:1002
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and encr...
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
The private information of an RSA key pair.
Definition: crypto_rsa.c:39
void GNUNET_CRYPTO_random_block(enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length)
Fill block with a random values.
int GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:367
struct GNUNET_CRYPTO_RsaPublicKey * GNUNET_CRYPTO_rsa_private_key_get_public(const struct GNUNET_CRYPTO_RsaPrivateKey *priv)
Extract the public key of the given private key.
Definition: crypto_rsa.c:265
Constant-size pre-secret for blinding key generation.
void GNUNET_CRYPTO_rsa_public_key_free(struct GNUNET_CRYPTO_RsaPublicKey *key)
Free memory occupied by the public key.
Definition: crypto_rsa.c:305
#define GNUNET_YES
Definition: gnunet_common.h:85
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:920
int GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
Sample signature struct.
uint32_t data
The data value.
int GNUNET_CRYPTO_kdf(void *result, size_t out_len, const void *xts, size_t xts_len, const void *skm, size_t skm_len,...)
Derive key.
Definition: crypto_kdf.c:89
void GNUNET_CRYPTO_rsa_private_key_free(struct GNUNET_CRYPTO_RsaPrivateKey *key)
Free memory occupied by the private key.
Definition: crypto_rsa.c:183
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
#define GNUNET_free(ptr)
Wrapper around free.
Public ECC key (always for curve Ed25519) encoded in a format suitable for network transmission and E...