aboutsummaryrefslogtreecommitdiff
path: root/src/util/gnunet-crypto-tvg.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/gnunet-crypto-tvg.c')
-rw-r--r--src/util/gnunet-crypto-tvg.c262
1 files changed, 197 insertions, 65 deletions
diff --git a/src/util/gnunet-crypto-tvg.c b/src/util/gnunet-crypto-tvg.c
index c3fead62e..b9dbba065 100644
--- a/src/util/gnunet-crypto-tvg.c
+++ b/src/util/gnunet-crypto-tvg.c
@@ -22,11 +22,33 @@
22 * @file util/gnunet-crypto-tgv.c 22 * @file util/gnunet-crypto-tgv.c
23 * @brief Generate test vectors for cryptographic operations. 23 * @brief Generate test vectors for cryptographic operations.
24 * @author Florian Dold 24 * @author Florian Dold
25 *
26 * Test vectors have the following format (TypeScript pseudo code):
27 *
28 * interface TestVectorFile {
29 * encoding: "base32crockford";
30 * producer?: string;
31 * vectors: TestVector[];
32 * }
33 *
34 * enum Operation {
35 * Hash("hash"),
36 * ...
37 * }
38 *
39 * interface TestVector {
40 * operation: Operation;
41 * // Inputs for the operation
42 * [ k: string]: string | number;
43 * };
44 *
45 *
25 */ 46 */
26#include "platform.h" 47#include "platform.h"
27#include "gnunet_util_lib.h" 48#include "gnunet_util_lib.h"
28#include "gnunet_signatures.h" 49#include "gnunet_signatures.h"
29#include "gnunet_testing_lib.h" 50#include "gnunet_testing_lib.h"
51#include <jansson.h>
30#include <gcrypt.h> 52#include <gcrypt.h>
31 53
32GNUNET_NETWORK_STRUCT_BEGIN 54GNUNET_NETWORK_STRUCT_BEGIN
@@ -46,20 +68,68 @@ GNUNET_NETWORK_STRUCT_END
46 68
47 69
48/** 70/**
49 * Print data base32-crockford with a preceding label. 71 * Create a fresh test vector for a given operation label.
72 *
73 * @param vecs array of vectors to append the new vector to
74 * @param vecname label for the operation of the vector
75 * @returns the fresh test vector
76 */
77static json_t *
78vec_for (json_t *vecs, const char *vecname)
79{
80 json_t *t = json_object ();
81
82 json_object_set_new (t,
83 "operation",
84 json_string (vecname));
85 json_array_append_new (vecs, t);
86 return t;
87}
88
89
90/**
91 * Add a base32crockford encoded value
92 * to a test vector.
50 * 93 *
51 * @param label label to print 94 * @param vec test vector to add to
52 * @param data data to print 95 * @param label label for the value
96 * @param data data to add
53 * @param size size of data 97 * @param size size of data
54 */ 98 */
55static void 99static void
56display_data (char *label, void *data, size_t size) 100d2j (json_t *vec,
101 const char *label,
102 const void *data,
103 size_t size)
57{ 104{
58 char *enc = GNUNET_STRINGS_data_to_string_alloc (data, size); 105 char *buf;
59 printf ("%s %s\n", label, enc); 106 json_t *json;
60 GNUNET_free (enc); 107
108 buf = GNUNET_STRINGS_data_to_string_alloc (data, size);
109 json = json_string (buf);
110 GNUNET_free (buf);
111 GNUNET_break (NULL != json);
112
113 json_object_set_new (vec, label, json);
61} 114}
62 115
116/**
117 * Add a number to a test vector.
118 *
119 * @param vec test vector to add to
120 * @param label label for the value
121 * @param data data to add
122 * @param size size of data
123 */
124static void
125uint2j (json_t *vec,
126 const char *label,
127 unsigned int num)
128{
129 json_t *json = json_integer (num);
130
131 json_object_set_new (vec, label, json);
132}
63 133
64/** 134/**
65 * Main function that will be run. 135 * Main function that will be run.
@@ -75,17 +145,31 @@ run (void *cls,
75 const char *cfgfile, 145 const char *cfgfile,
76 const struct GNUNET_CONFIGURATION_Handle *cfg) 146 const struct GNUNET_CONFIGURATION_Handle *cfg)
77{ 147{
148 json_t *vecfile = json_object ();
149 json_t *vecs = json_array ();
150
151 json_object_set_new (vecfile,
152 "encoding",
153 json_string ("base32crockford"));
154 json_object_set_new (vecfile,
155 "producer",
156 json_string ("GNUnet " PACKAGE_VERSION " " VCS_VERSION));
157 json_object_set_new (vecfile,
158 "vectors",
159 vecs);
160
78 { 161 {
162 json_t *vec = vec_for (vecs, "hash");
79 struct GNUNET_HashCode hc; 163 struct GNUNET_HashCode hc;
80 char *str = "Hello, GNUnet"; 164 char *str = "Hello, GNUnet";
81 165
82 GNUNET_CRYPTO_hash (str, strlen (str), &hc); 166 GNUNET_CRYPTO_hash (str, strlen (str), &hc);
83 167
84 printf ("hash code:\n"); 168 d2j (vec, "input", str, strlen (str));
85 display_data (" input", str, strlen (str)); 169 d2j (vec, "output", &hc, sizeof (struct GNUNET_HashCode));
86 display_data (" output", &hc, sizeof (struct GNUNET_HashCode));
87 } 170 }
88 { 171 {
172 json_t *vec = vec_for (vecs, "ecdhe_key_derivation");
89 struct GNUNET_CRYPTO_EcdhePrivateKey priv1; 173 struct GNUNET_CRYPTO_EcdhePrivateKey priv1;
90 struct GNUNET_CRYPTO_EcdhePublicKey pub1; 174 struct GNUNET_CRYPTO_EcdhePublicKey pub1;
91 struct GNUNET_CRYPTO_EcdhePrivateKey priv2; 175 struct GNUNET_CRYPTO_EcdhePrivateKey priv2;
@@ -100,22 +184,26 @@ run (void *cls,
100 &pub1, 184 &pub1,
101 &skm)); 185 &skm));
102 186
103 printf ("ecdhe key:\n"); 187 d2j (vec,
104 display_data (" priv1", 188 "priv1",
105 &priv1, 189 &priv1,
106 sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey)); 190 sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
107 display_data (" pub1", 191 d2j (vec,
108 &pub1, 192 "pub1",
109 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey)); 193 &pub1,
110 display_data (" priv2", 194 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey));
111 &priv2, 195 d2j (vec,
112 sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey)); 196 "priv2",
113 display_data (" skm", 197 &priv2,
114 &skm, 198 sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
115 sizeof (struct GNUNET_HashCode)); 199 d2j (vec,
200 "skm",
201 &skm,
202 sizeof (struct GNUNET_HashCode));
116 } 203 }
117 204
118 { 205 {
206 json_t *vec = vec_for (vecs, "eddsa_key_derivation");
119 struct GNUNET_CRYPTO_EddsaPrivateKey priv; 207 struct GNUNET_CRYPTO_EddsaPrivateKey priv;
120 struct GNUNET_CRYPTO_EddsaPublicKey pub; 208 struct GNUNET_CRYPTO_EddsaPublicKey pub;
121 209
@@ -123,15 +211,17 @@ run (void *cls,
123 GNUNET_CRYPTO_eddsa_key_get_public (&priv, 211 GNUNET_CRYPTO_eddsa_key_get_public (&priv,
124 &pub); 212 &pub);
125 213
126 printf ("eddsa key:\n"); 214 d2j (vec,
127 display_data (" priv", 215 "priv",
128 &priv, 216 &priv,
129 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)); 217 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey));
130 display_data (" pub", 218 d2j (vec,
131 &pub, 219 "pub",
132 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)); 220 &pub,
221 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
133 } 222 }
134 { 223 {
224 json_t *vec = vec_for (vecs, "eddsa_signing");
135 struct GNUNET_CRYPTO_EddsaPrivateKey priv; 225 struct GNUNET_CRYPTO_EddsaPrivateKey priv;
136 struct GNUNET_CRYPTO_EddsaPublicKey pub; 226 struct GNUNET_CRYPTO_EddsaPublicKey pub;
137 struct GNUNET_CRYPTO_EddsaSignature sig; 227 struct GNUNET_CRYPTO_EddsaSignature sig;
@@ -151,22 +241,26 @@ run (void *cls,
151 &sig, 241 &sig,
152 &pub)); 242 &pub));
153 243
154 printf ("eddsa sig:\n"); 244 d2j (vec,
155 display_data (" priv", 245 "priv",
156 &priv, 246 &priv,
157 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)); 247 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey));
158 display_data (" pub", 248 d2j (vec,
159 &pub, 249 "pub",
160 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)); 250 &pub,
161 display_data (" data", 251 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
162 &data, 252 d2j (vec,
163 sizeof (struct TestSignatureDataPS)); 253 "data",
164 display_data (" sig", 254 &data,
165 &sig, 255 sizeof (struct TestSignatureDataPS));
166 sizeof (struct GNUNET_CRYPTO_EddsaSignature)); 256 d2j (vec,
257 "sig",
258 &sig,
259 sizeof (struct GNUNET_CRYPTO_EddsaSignature));
167 } 260 }
168 261
169 { 262 {
263 json_t *vec = vec_for (vecs, "kdf");
170 size_t out_len = 64; 264 size_t out_len = 64;
171 char out[out_len]; 265 char out[out_len];
172 char *ikm = "I'm the secret input key material"; 266 char *ikm = "I'm the secret input key material";
@@ -184,14 +278,28 @@ run (void *cls,
184 strlen (ctx), 278 strlen (ctx),
185 NULL)); 279 NULL));
186 280
187 printf ("kdf:\n"); 281 d2j (vec,
188 display_data (" salt", salt, strlen (salt)); 282 "salt",
189 display_data (" ikm", ikm, strlen (ikm)); 283 salt,
190 display_data (" ctx", ctx, strlen (ctx)); 284 strlen (salt));
191 printf (" out_len %u\n", (unsigned int) out_len); 285 d2j (vec,
192 display_data (" out", out, out_len); 286 "ikm",
287 ikm,
288 strlen (ikm));
289 d2j (vec,
290 "ctx",
291 ctx,
292 strlen (ctx));
293 uint2j (vec,
294 "out_len %u\n",
295 (unsigned int) out_len);
296 d2j (vec,
297 "out",
298 out,
299 out_len);
193 } 300 }
194 { 301 {
302 json_t *vec = vec_for (vecs, "eddsa_ecdh");
195 struct GNUNET_CRYPTO_EcdhePrivateKey priv_ecdhe; 303 struct GNUNET_CRYPTO_EcdhePrivateKey priv_ecdhe;
196 struct GNUNET_CRYPTO_EcdhePublicKey pub_ecdhe; 304 struct GNUNET_CRYPTO_EcdhePublicKey pub_ecdhe;
197 struct GNUNET_CRYPTO_EddsaPrivateKey priv_eddsa; 305 struct GNUNET_CRYPTO_EddsaPrivateKey priv_eddsa;
@@ -204,25 +312,26 @@ run (void *cls,
204 GNUNET_CRYPTO_eddsa_key_get_public (&priv_eddsa, &pub_eddsa); 312 GNUNET_CRYPTO_eddsa_key_get_public (&priv_eddsa, &pub_eddsa);
205 GNUNET_CRYPTO_ecdh_eddsa (&priv_ecdhe, &pub_eddsa, &key_material); 313 GNUNET_CRYPTO_ecdh_eddsa (&priv_ecdhe, &pub_eddsa, &key_material);
206 314
207 printf ("eddsa_ecdh:\n"); 315 d2j (vec, "priv_ecdhe",
208 display_data (" priv_ecdhe",
209 &priv_ecdhe, 316 &priv_ecdhe,
210 sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey)); 317 sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
211 display_data (" pub_ecdhe", 318 d2j (vec, "pub_ecdhe",
212 &pub_ecdhe, 319 &pub_ecdhe,
213 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey)); 320 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey));
214 display_data (" priv_eddsa", 321 d2j (vec, "priv_eddsa",
215 &priv_eddsa, 322 &priv_eddsa,
216 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)); 323 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey));
217 display_data (" pub_eddsa", 324 d2j (vec, "pub_eddsa",
218 &pub_eddsa, 325 &pub_eddsa,
219 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)); 326 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
220 display_data (" key_material", 327 d2j (vec, "key_material",
221 &key_material, 328 &key_material,
222 sizeof (struct GNUNET_HashCode)); 329 sizeof (struct GNUNET_HashCode));
223 } 330 }
224 331
225 { 332 {
333 json_t *vec = vec_for (vecs, "rsa_blind_signing");
334
226 struct GNUNET_CRYPTO_RsaPrivateKey *skey; 335 struct GNUNET_CRYPTO_RsaPrivateKey *skey;
227 struct GNUNET_CRYPTO_RsaPublicKey *pkey; 336 struct GNUNET_CRYPTO_RsaPublicKey *pkey;
228 struct GNUNET_HashCode message_hash; 337 struct GNUNET_HashCode message_hash;
@@ -237,6 +346,7 @@ run (void *cls,
237 size_t blinded_sig_enc_length; 346 size_t blinded_sig_enc_length;
238 void *sig_enc_data; 347 void *sig_enc_data;
239 size_t sig_enc_length; 348 size_t sig_enc_length;
349
240 skey = GNUNET_CRYPTO_rsa_private_key_create (2048); 350 skey = GNUNET_CRYPTO_rsa_private_key_create (2048);
241 pkey = GNUNET_CRYPTO_rsa_private_key_get_public (skey); 351 pkey = GNUNET_CRYPTO_rsa_private_key_get_public (skey);
242 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, 352 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
@@ -263,21 +373,43 @@ run (void *cls,
263 & 373 &
264 blinded_sig_enc_data); 374 blinded_sig_enc_data);
265 sig_enc_length = GNUNET_CRYPTO_rsa_signature_encode (sig, &sig_enc_data); 375 sig_enc_length = GNUNET_CRYPTO_rsa_signature_encode (sig, &sig_enc_data);
266 printf ("blind signing:\n"); 376 d2j (vec,
267 display_data (" message_hash", &message_hash, sizeof (struct 377 "message_hash",
268 GNUNET_HashCode)); 378 &message_hash,
269 display_data (" rsa_public_key", public_enc_data, public_enc_len); 379 sizeof (struct GNUNET_HashCode));
270 display_data (" blinding_key_secret", &bks, sizeof (struct 380 d2j (vec,
271 GNUNET_CRYPTO_RsaBlindingKeySecret)); 381 "rsa_public_key",
272 display_data (" blinded_message", blinded_data, blinded_len); 382 public_enc_data,
273 display_data (" blinded_sig", blinded_sig_enc_data, 383 public_enc_len);
274 blinded_sig_enc_length); 384 d2j (vec,
275 display_data (" sig", sig_enc_data, sig_enc_length); 385 "blinding_key_secret",
386 &bks,
387 sizeof (struct GNUNET_CRYPTO_RsaBlindingKeySecret));
388 d2j (vec,
389 "blinded_message",
390 blinded_data,
391 blinded_len);
392 d2j (vec,
393 "blinded_sig",
394 blinded_sig_enc_data,
395 blinded_sig_enc_length);
396 d2j (vec,
397 "sig",
398 sig_enc_data,
399 sig_enc_length);
276 GNUNET_CRYPTO_rsa_private_key_free (skey); 400 GNUNET_CRYPTO_rsa_private_key_free (skey);
277 GNUNET_CRYPTO_rsa_public_key_free (pkey); 401 GNUNET_CRYPTO_rsa_public_key_free (pkey);
278 GNUNET_CRYPTO_rsa_signature_free (sig); 402 GNUNET_CRYPTO_rsa_signature_free (sig);
279 GNUNET_CRYPTO_rsa_signature_free (blinded_sig); 403 GNUNET_CRYPTO_rsa_signature_free (blinded_sig);
404 GNUNET_free (public_enc_data);
405 GNUNET_free (blinded_data);
406 GNUNET_free (sig_enc_data);
407 GNUNET_free (blinded_sig_enc_data);
280 } 408 }
409
410 json_dumpf (vecfile, stdout, JSON_INDENT (2));
411 json_decref (vecfile);
412 printf ("\n");
281} 413}
282 414
283 415