5f209efea91c8da4ecfc56ba019292eb9a74a240
[oweals/gnunet.git] / src / include / gnunet_crypto_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2012 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_crypto_lib.h
23  * @brief cryptographic primitives for GNUnet
24  *
25  * @author Christian Grothoff
26  * @author Krista Bennett
27  * @author Gerd Knorr <kraxel@bytesex.org>
28  * @author Ioana Patrascu
29  * @author Tzvetan Horozov
30  */
31
32 #ifndef GNUNET_CRYPTO_LIB_H
33 #define GNUNET_CRYPTO_LIB_H
34
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #if 0                           /* keep Emacsens' auto-indent happy */
39 }
40 #endif
41 #endif
42
43 #include "gnunet_common.h"
44 #include "gnunet_scheduler_lib.h"
45
46
47 /**
48  * Maximum length of an ECC signature.
49  * Note: round up to multiple of 8 minus 2 for alignment.
50  */
51 #define GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH 126
52
53
54 /**
55  * Desired quality level for cryptographic operations.
56  */
57 enum GNUNET_CRYPTO_Quality
58 {
59   /**
60    * No good quality of the operation is needed (i.e.,
61    * random numbers can be pseudo-random).
62    */
63   GNUNET_CRYPTO_QUALITY_WEAK,
64
65   /**
66    * High-quality operations are desired.
67    */
68   GNUNET_CRYPTO_QUALITY_STRONG,
69
70   /**
71    * Randomness for IVs etc. is required.
72    */
73   GNUNET_CRYPTO_QUALITY_NONCE
74 };
75
76
77 /**
78  * @brief length of the sessionkey in bytes (256 BIT sessionkey)
79  */
80 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
81
82 /**
83  * Length of a hash value
84  */
85 #define GNUNET_CRYPTO_HASH_LENGTH (512/8)
86
87 /**
88  * @brief 0-terminated ASCII encoding of a struct GNUNET_HashCode.
89  */
90 struct GNUNET_CRYPTO_HashAsciiEncoded
91 {
92   unsigned char encoding[104];
93 };
94
95
96 /**
97  * @brief 0-terminated ASCII encoding of a struct GNUNET_CRYPTO_ShortHashCode.
98  */
99 struct GNUNET_CRYPTO_ShortHashAsciiEncoded
100 {
101   unsigned char short_encoding[53];
102 };
103
104
105 GNUNET_NETWORK_STRUCT_BEGIN
106
107
108 /**
109  * @brief header of what an ECC signature signs
110  *        this must be followed by "size - 8" bytes of
111  *        the actual signed data
112  */
113 struct GNUNET_CRYPTO_EccSignaturePurpose
114 {
115   /**
116    * How many bytes does this signature sign?
117    * (including this purpose header); in network
118    * byte order (!).
119    */
120   uint32_t size GNUNET_PACKED;
121
122   /**
123    * What does this signature vouch for?  This
124    * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
125    * constant (from gnunet_signatures.h).  In
126    * network byte order!
127    */
128   uint32_t purpose GNUNET_PACKED;
129
130 };
131
132
133 /**
134  * @brief an ECC signature
135  */
136 struct GNUNET_CRYPTO_EccSignature
137 {
138
139   /**
140    * R value.
141    */
142   unsigned char r[256 / 8];
143
144   /**
145    * S value.
146    */
147   unsigned char s[256 / 8];
148
149 };
150
151
152 /**
153  * Public ECC key (always for NIST P-521) encoded in a format suitable
154  * for network transmission.
155  */
156 struct GNUNET_CRYPTO_EccPublicKey 
157 {
158   /**
159    * Q consists of an x- and a y-value, each mod p (256 bits),
160    * given here in affine coordinates.
161    */
162   unsigned char q_x[256 / 8];
163
164   /**
165    * Q consists of an x- and a y-value, each mod p (256 bits),
166    * given here in affine coordinates.
167    */
168   unsigned char q_y[256 / 8];
169
170 };
171
172
173 /**
174  * Private ECC key encoded for transmission.
175  */
176 struct GNUNET_CRYPTO_EccPrivateKey
177 {
178   /**
179    * d is a value mod n, where n has at most 256 bits.
180    */
181   unsigned char d[256 / 8];
182
183 };
184
185
186 /**
187  * @brief type for session keys
188  */
189 struct GNUNET_CRYPTO_AesSessionKey
190 {
191   /**
192    * Actual key.
193    */
194   unsigned char key[GNUNET_CRYPTO_AES_KEY_LENGTH];
195
196 };
197 GNUNET_NETWORK_STRUCT_END
198
199 /**
200  * @brief IV for sym cipher
201  *
202  * NOTE: must be smaller (!) in size than the
203  * struct GNUNET_HashCode.
204  */
205 struct GNUNET_CRYPTO_AesInitializationVector
206 {
207   unsigned char iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
208 };
209
210
211 /**
212  * @brief type for (message) authentication keys
213  */
214 struct GNUNET_CRYPTO_AuthKey
215 {
216   unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
217 };
218
219
220 /* **************** Functions and Macros ************* */
221
222 /**
223  * Seed a weak random generator. Only GNUNET_CRYPTO_QUALITY_WEAK-mode generator
224  * can be seeded.
225  *
226  * @param seed the seed to use
227  */
228 void
229 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
230
231
232 /**
233  * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
234  *
235  * @param sum current sum, initially 0
236  * @param buf buffer to calculate CRC over (must be 16-bit aligned)
237  * @param len number of bytes in hdr, must be multiple of 2
238  * @return updated crc sum (must be subjected to GNUNET_CRYPTO_crc16_finish to get actual crc16)
239  */
240 uint32_t
241 GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len);
242
243
244 /**
245  * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
246  *
247  * @param sum cummulative sum
248  * @return crc16 value
249  */
250 uint16_t
251 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
252
253
254 /**
255  * Calculate the checksum of a buffer in one step.
256  *
257  * @param buf buffer to  calculate CRC over (must be 16-bit aligned)
258  * @param len number of bytes in hdr, must be multiple of 2
259  * @return crc16 value
260  */
261 uint16_t
262 GNUNET_CRYPTO_crc16_n (const void *buf, size_t len);
263
264
265 /**
266  * Compute the CRC32 checksum for the first len
267  * bytes of the buffer.
268  *
269  * @param buf the data over which we're taking the CRC
270  * @param len the length of the buffer in bytes
271  * @return the resulting CRC32 checksum
272  */
273 int32_t
274 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len);
275
276
277 /**
278  * Produce a random value.
279  *
280  * @param mode desired quality of the random number
281  * @param i the upper limit (exclusive) for the random number
282  * @return a random value in the interval [0,i) (exclusive).
283  */
284 uint32_t
285 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i);
286
287
288 /**
289  * Random on unsigned 64-bit values.
290  *
291  * @param mode desired quality of the random number
292  * @param max value returned will be in range [0,max) (exclusive)
293  * @return random 64-bit number
294  */
295 uint64_t
296 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max);
297
298
299 /**
300  * Get an array with a random permutation of the
301  * numbers 0...n-1.
302  * @param mode GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used, GNUNET_CRYPTO_QUALITY_WEAK otherwise
303  * @param n the size of the array
304  * @return the permutation array (allocated from heap)
305  */
306 unsigned int *
307 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
308
309
310 /**
311  * Create a new Session key.
312  *
313  * @param key key to initialize
314  */
315 void
316 GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key);
317
318
319 /**
320  * Encrypt a block with the public key of another
321  * host that uses the same cyper.
322  *
323  * @param block the block to encrypt
324  * @param len the size of the block
325  * @param sessionkey the key used to encrypt
326  * @param iv the initialization vector to use, use INITVALUE
327  *        for streams.
328  * @return the size of the encrypted block, -1 for errors
329  */
330 ssize_t
331 GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
332                            const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
333                            const struct GNUNET_CRYPTO_AesInitializationVector
334                            *iv, void *result);
335
336
337 /**
338  * Decrypt a given block with the sessionkey.
339  *
340  * @param block the data to decrypt, encoded as returned by encrypt
341  * @param size how big is the block?
342  * @param sessionkey the key used to decrypt
343  * @param iv the initialization vector to use
344  * @param result address to store the result at
345  * @return -1 on failure, size of decrypted block on success
346  */
347 ssize_t
348 GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
349                            const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
350                            const struct GNUNET_CRYPTO_AesInitializationVector
351                            *iv, void *result);
352
353
354 /**
355  * @brief Derive an IV
356  * @param iv initialization vector
357  * @param skey session key
358  * @param salt salt for the derivation
359  * @param salt_len size of the salt
360  * @param ... pairs of void * & size_t for context chunks, terminated by NULL
361  */
362 void
363 GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
364                              const struct GNUNET_CRYPTO_AesSessionKey *skey,
365                              const void *salt, size_t salt_len, ...);
366
367
368 /**
369  * @brief Derive an IV
370  * @param iv initialization vector
371  * @param skey session key
372  * @param salt salt for the derivation
373  * @param salt_len size of the salt
374  * @param argp pairs of void * & size_t for context chunks, terminated by NULL
375  */
376 void
377 GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
378                                const struct GNUNET_CRYPTO_AesSessionKey *skey,
379                                const void *salt, size_t salt_len, va_list argp);
380
381
382 /**
383  * Convert hash to ASCII encoding.
384  * @param block the hash code
385  * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
386  *  safely cast to char*, a '\\0' termination is set).
387  */
388 void
389 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode * block,
390                            struct GNUNET_CRYPTO_HashAsciiEncoded *result);
391
392
393 /**
394  * Convert short hash to ASCII encoding.
395  *
396  * @param block the hash code
397  * @param result where to store the encoding (struct GNUNET_CRYPTO_ShortHashAsciiEncoded can be
398  *  safely cast to char*, a '\\0' termination is set).
399  */
400 void
401 GNUNET_CRYPTO_short_hash_to_enc (const struct GNUNET_CRYPTO_ShortHashCode * block,
402                                  struct GNUNET_CRYPTO_ShortHashAsciiEncoded *result);
403
404
405 /**
406  * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
407  *
408  * @param enc the encoding
409  * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
410  * @param result where to store the GNUNET_CRYPTO_hash code
411  * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
412  */
413 int
414 GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
415                                  struct GNUNET_HashCode * result);
416
417
418 /**
419  * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
420  *
421  * @param enc the encoding
422  * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
423  * @param result where to store the GNUNET_CRYPTO_hash code
424  * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
425  */
426 int
427 GNUNET_CRYPTO_short_hash_from_string2 (const char *enc, size_t enclen,
428                                        struct GNUNET_CRYPTO_ShortHashCode * result);
429
430
431 /**
432  * Convert ASCII encoding back to struct GNUNET_HashCode
433  *
434  * @param enc the encoding
435  * @param result where to store the hash code
436  * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
437  */
438 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
439   GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
440
441
442 /**
443  * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
444  *
445  * @param enc the encoding
446  * @param result where to store the GNUNET_CRYPTO_ShortHash 
447  * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
448  */
449 #define GNUNET_CRYPTO_short_hash_from_string(enc, result) \
450   GNUNET_CRYPTO_short_hash_from_string2 (enc, strlen(enc), result)
451
452
453 /**
454  * Compare function for ShortHashCodes, producing a total ordering
455  * of all hashcodes.
456  *
457  * @param h1 some hash code
458  * @param h2 some hash code
459  * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
460  */
461 int
462 GNUNET_CRYPTO_short_hash_cmp (const struct GNUNET_CRYPTO_ShortHashCode * h1,
463                               const struct GNUNET_CRYPTO_ShortHashCode * h2);
464
465 /**
466  * Compute the distance between 2 hashcodes.
467  * The computation must be fast, not involve
468  * a.a or a.e (they're used elsewhere), and
469  * be somewhat consistent. And of course, the
470  * result should be a positive number.
471  *
472  * @param a some hash code
473  * @param b some hash code
474  * @return number between 0 and UINT32_MAX
475  */
476 uint32_t
477 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode * a,
478                                  const struct GNUNET_HashCode * b);
479
480
481 /**
482  * Compute hash of a given block.
483  *
484  * @param block the data to hash
485  * @param size size of the block
486  * @param ret pointer to where to write the hashcode
487  */
488 void
489 GNUNET_CRYPTO_hash (const void *block, size_t size, struct GNUNET_HashCode * ret);
490
491
492 /**
493  * Compute short (256-bit) hash of a given block.
494  *
495  * @param block the data to hash
496  * @param size size of the block
497  * @param ret pointer to where to write the hashcode
498  */
499 void
500 GNUNET_CRYPTO_short_hash (const void *block, size_t size, 
501                           struct GNUNET_CRYPTO_ShortHashCode * ret);
502
503
504 /**
505  * Double short (256-bit) hash to create a long hash.
506  *
507  * @param sh short hash to double
508  * @param dh where to store the (doubled) long hash (not really a hash)
509  */
510 void
511 GNUNET_CRYPTO_short_hash_double (const struct GNUNET_CRYPTO_ShortHashCode *sh,
512                                  struct GNUNET_HashCode *dh);
513
514
515 /**
516  * Truncate doubled short hash back to a short hash.
517  *
518  * @param dh doubled short hash to reduce again
519  * @param sh where to store the short hash
520  * @return GNUNET_OK on success, GNUNET_SYSERR if this was not a
521  *         doubled short hash
522  */
523 int
524 GNUNET_CRYPTO_short_hash_from_truncation (const struct GNUNET_HashCode *dh,
525                                           struct GNUNET_CRYPTO_ShortHashCode *sh);
526
527
528 /**
529  * Calculate HMAC of a message (RFC 2104)
530  *
531  * @param key secret key
532  * @param plaintext input plaintext
533  * @param plaintext_len length of plaintext
534  * @param hmac where to store the hmac
535  */
536 void
537 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
538                     const void *plaintext, size_t plaintext_len,
539                     struct GNUNET_HashCode * hmac);
540
541
542 /**
543  * Function called once the hash computation over the
544  * specified file has completed.
545  *
546  * @param cls closure
547  * @param res resulting hash, NULL on error
548  */
549 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
550                                                      const struct GNUNET_HashCode *
551                                                      res);
552
553
554 /**
555  * Handle to file hashing operation.
556  */
557 struct GNUNET_CRYPTO_FileHashContext;
558
559 /**
560  * Compute the hash of an entire file.
561  *
562  * @param priority scheduling priority to use
563  * @param filename name of file to hash
564  * @param blocksize number of bytes to process in one task
565  * @param callback function to call upon completion
566  * @param callback_cls closure for callback
567  * @return NULL on (immediate) errror
568  */
569 struct GNUNET_CRYPTO_FileHashContext *
570 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
571                          const char *filename, size_t blocksize,
572                          GNUNET_CRYPTO_HashCompletedCallback callback,
573                          void *callback_cls);
574
575
576 /**
577  * Cancel a file hashing operation.
578  *
579  * @param fhc operation to cancel (callback must not yet have been invoked)
580  */
581 void
582 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
583
584
585 /**
586  * Create a random hash code.
587  *
588  * @param mode desired quality level
589  * @param result hash code that is randomized
590  */
591 void
592 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
593                                   struct GNUNET_HashCode * result);
594
595
596 /**
597  * compute result(delta) = b - a
598  *
599  * @param a some hash code
600  * @param b some hash code
601  * @param result set to b - a
602  */
603 void
604 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode * a,
605                                const struct GNUNET_HashCode * b,
606                                struct GNUNET_HashCode * result);
607
608
609 /**
610  * compute result(b) = a + delta
611  *
612  * @param a some hash code
613  * @param delta some hash code
614  * @param result set to a + delta
615  */
616 void
617 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode * a,
618                         const struct GNUNET_HashCode * delta,
619                         struct GNUNET_HashCode * result);
620
621
622 /**
623  * compute result = a ^ b
624  *
625  * @param a some hash code
626  * @param b some hash code
627  * @param result set to a ^ b
628  */
629 void
630 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode * a, const struct GNUNET_HashCode * b,
631                         struct GNUNET_HashCode * result);
632
633
634 /**
635  * Convert a hashcode into a key.
636  *
637  * @param hc hash code that serves to generate the key
638  * @param skey set to a valid session key
639  * @param iv set to a valid initialization vector
640  */
641 void
642 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
643                                struct GNUNET_CRYPTO_AesSessionKey *skey,
644                                struct GNUNET_CRYPTO_AesInitializationVector
645                                *iv);
646
647
648 /**
649  * Obtain a bit from a hashcode.
650  *
651  * @param code the GNUNET_CRYPTO_hash to index bit-wise
652  * @param bit index into the hashcode, [0...159]
653  * @return Bit \a bit from hashcode \a code, -1 for invalid index
654  */
655 int
656 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode * code, unsigned int bit);
657
658 /**
659  * Determine how many low order bits match in two
660  * struct GNUNET_HashCodes.  i.e. - 010011 and 011111 share
661  * the first two lowest order bits, and therefore the
662  * return value is two (NOT XOR distance, nor how many
663  * bits match absolutely!).
664  *
665  * @param first the first hashcode
666  * @param second the hashcode to compare first to
667  *
668  * @return the number of bits that match
669  */
670 unsigned int
671 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode * first,
672                                   const struct GNUNET_HashCode * second);
673
674
675 /**
676  * Compare function for HashCodes, producing a total ordering
677  * of all hashcodes.
678  *
679  * @param h1 some hash code
680  * @param h2 some hash code
681  * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
682  */
683 int
684 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode * h1, const struct GNUNET_HashCode * h2);
685
686
687 /**
688  * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
689  * in the XOR metric (Kademlia).
690  *
691  * @param h1 some hash code
692  * @param h2 some hash code
693  * @param target some hash code
694  * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
695  */
696 int
697 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode * h1,
698                            const struct GNUNET_HashCode * h2,
699                            const struct GNUNET_HashCode * target);
700
701
702 /**
703  * @brief Derive an authentication key
704  * @param key authentication key
705  * @param rkey root key
706  * @param salt salt
707  * @param salt_len size of the salt
708  * @param argp pair of void * & size_t for context chunks, terminated by NULL
709  */
710 void
711 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
712                                  const struct GNUNET_CRYPTO_AesSessionKey *rkey,
713                                  const void *salt, size_t salt_len,
714                                  va_list argp);
715
716
717 /**
718  * @brief Derive an authentication key
719  * @param key authentication key
720  * @param rkey root key
721  * @param salt salt
722  * @param salt_len size of the salt
723  * @param ... pair of void * & size_t for context chunks, terminated by NULL
724  */
725 void
726 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
727                                const struct GNUNET_CRYPTO_AesSessionKey *rkey,
728                                const void *salt, size_t salt_len, ...);
729
730 /**
731  * @brief Derive key
732  * @param result buffer for the derived key, allocated by caller
733  * @param out_len desired length of the derived key
734  * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
735  * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
736  * @param xts salt
737  * @param xts_len length of xts
738  * @param skm source key material
739  * @param skm_len length of skm
740  * @param ... pair of void * & size_t for context chunks, terminated by NULL
741  * @return GNUNET_YES on success
742  */
743 int
744 GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
745                     const void *xts, size_t xts_len, const void *skm,
746                     size_t skm_len, ...);
747
748
749 /**
750  * @brief Derive key
751  * @param result buffer for the derived key, allocated by caller
752  * @param out_len desired length of the derived key
753  * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
754  * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
755  * @param xts salt
756  * @param xts_len length of xts
757  * @param skm source key material
758  * @param skm_len length of skm
759  * @param argp va_list of void * & size_t pairs for context chunks
760  * @return GNUNET_YES on success
761  */
762 int
763 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
764                       const void *xts, size_t xts_len, const void *skm,
765                       size_t skm_len, va_list argp);
766
767
768 /**
769  * @brief Derive key
770  * @param result buffer for the derived key, allocated by caller
771  * @param out_len desired length of the derived key
772  * @param xts salt
773  * @param xts_len length of xts
774  * @param skm source key material
775  * @param skm_len length of skm
776  * @param argp va_list of void * & size_t pairs for context chunks
777  * @return GNUNET_YES on success
778  */
779 int
780 GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
781                      size_t xts_len, const void *skm, size_t skm_len,
782                      va_list argp);
783
784
785 /**
786  * @brief Derive key
787  * @param result buffer for the derived key, allocated by caller
788  * @param out_len desired length of the derived key
789  * @param xts salt
790  * @param xts_len length of xts
791  * @param skm source key material
792  * @param skm_len length of skm
793  * @param ... void * & size_t pairs for context chunks
794  * @return GNUNET_YES on success
795  */
796 int
797 GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
798                    size_t xts_len, const void *skm, size_t skm_len, ...);
799
800
801 /**
802  * Function called upon completion of 'GNUNET_CRYPTO_ecc_key_create_async'.
803  *
804  * @param cls closure
805  * @param pk NULL on error, otherwise the private key (which must be free'd by the callee)
806  * @param emsg NULL on success, otherwise an error message
807  */
808 typedef void (*GNUNET_CRYPTO_EccKeyCallback)(void *cls,
809                                              struct GNUNET_CRYPTO_EccPrivateKey *pk,
810                                              const char *emsg);
811
812
813 /**
814  * Free memory occupied by ECC key
815  *
816  * @param priv pointer to the memory to free
817  */
818 void
819 GNUNET_CRYPTO_ecc_key_free (struct GNUNET_CRYPTO_EccPrivateKey *priv);
820
821
822 /**
823  * Extract the public key for the given private key.
824  *
825  * @param priv the private key
826  * @param pub where to write the public key
827  */
828 void
829 GNUNET_CRYPTO_ecc_key_get_public (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
830                                   struct GNUNET_CRYPTO_EccPublicKey *pub);
831
832
833 /**
834  * Convert a public key to a string.
835  *
836  * @param pub key to convert
837  * @return string representing  'pub'
838  */
839 char *
840 GNUNET_CRYPTO_ecc_public_key_to_string (const struct GNUNET_CRYPTO_EccPublicKey *pub);
841
842
843 /**
844  * Convert a string representing a public key to a public key.
845  *
846  * @param enc encoded public key
847  * @param enclen number of bytes in enc (without 0-terminator)
848  * @param pub where to store the public key
849  * @return GNUNET_OK on success
850  */
851 int
852 GNUNET_CRYPTO_ecc_public_key_from_string (const char *enc, 
853                                           size_t enclen,
854                                           struct GNUNET_CRYPTO_EccPublicKey *pub);
855
856
857 /**
858  * Create a new private key by reading it from a file.  If the
859  * files does not exist, create a new key and write it to the
860  * file.  Caller must free return value.  Note that this function
861  * can not guarantee that another process might not be trying
862  * the same operation on the same file at the same time.
863  * If the contents of the file
864  * are invalid the old file is deleted and a fresh key is
865  * created.
866  *
867  * @param filename name of file to use to store the key
868  * @return new private key, NULL on error (for example,
869  *   permission denied)
870  */
871 struct GNUNET_CRYPTO_EccPrivateKey *
872 GNUNET_CRYPTO_ecc_key_create_from_file (const char *filename);
873
874
875 /**
876  * Create a new private key by reading our peer's key from
877  * the file specified in the configuration.
878  *
879  * @return new private key, NULL on error (for example,
880  *   permission denied)
881  */
882 struct GNUNET_CRYPTO_EccPrivateKey *
883 GNUNET_CRYPTO_ecc_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
884
885
886 /**
887  * Create a new private key. Caller must free return value.  Blocking version
888  * (blocks to gather entropy).
889  *
890  * @return fresh private key
891  */
892 struct GNUNET_CRYPTO_EccPrivateKey *
893 GNUNET_CRYPTO_ecc_key_create (void);
894
895
896 /**
897  * Setup a hostkey file for a peer given the name of the
898  * configuration file (!).  This function is used so that
899  * at a later point code can be certain that reading a
900  * hostkey is fast (for example in time-dependent testcases).
901  *
902  * @param cfg_name name of the configuration file to use
903  */
904 void
905 GNUNET_CRYPTO_ecc_setup_hostkey (const char *cfg_name);
906
907
908 /**
909  * Retrieve the identity of the host's peer.
910  *
911  * @param cfg configuration to use
912  * @param dst pointer to where to write the peer identity
913  * @return GNUNET_OK on success, GNUNET_SYSERR if the identity
914  *         could not be retrieved
915  */
916 int
917 GNUNET_CRYPTO_get_host_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
918                                  struct GNUNET_PeerIdentity *dst);
919
920
921 /**
922  * Derive key material from a public and a private ECC key.
923  *
924  * @param key private key to use for the ECDH (x)
925  * @param pub public key to use for the ECDY (yG)
926  * @param key_material where to write the key material (xyG)
927  * @return GNUNET_SYSERR on error, GNUNET_OK on success
928  */
929 int
930 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EccPrivateKey *key,
931                         const struct GNUNET_CRYPTO_EccPublicKey *pub,
932                         struct GNUNET_HashCode *key_material);
933
934
935 /**
936  * Sign a given block.
937  *
938  * @param priv private key to use for the signing
939  * @param purpose what to sign (size, purpose)
940  * @param sig where to write the signature
941  * @return GNUNET_SYSERR on error, GNUNET_OK on success
942  */
943 int
944 GNUNET_CRYPTO_ecc_sign (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
945                         const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
946                         struct GNUNET_CRYPTO_EccSignature *sig);
947
948
949 /**
950  * Verify signature.
951  *
952  * @param purpose what is the purpose that the signature should have?
953  * @param validate block to validate (size, purpose, data)
954  * @param sig signature that is being validated
955  * @param pub public key of the signer
956  * @returns GNUNET_OK if ok, GNUNET_SYSERR if invalid
957  */
958 int
959 GNUNET_CRYPTO_ecc_verify (uint32_t purpose,
960                           const struct GNUNET_CRYPTO_EccSignaturePurpose
961                           *validate,
962                           const struct GNUNET_CRYPTO_EccSignature *sig,
963                           const struct GNUNET_CRYPTO_EccPublicKey *pub);
964
965
966 /**
967  * Derive a private key from a given private key and a label.
968  * Essentially calculates a private key 'h = H(l,P) * d mod n'
969  * where n is the size of the ECC group and P is the public
970  * key associated with the private key 'd'.
971  *
972  * @param priv original private key
973  * @param label label to use for key deriviation
974  * @return derived private key
975  */
976 struct GNUNET_CRYPTO_EccPrivateKey *
977 GNUNET_CRYPTO_ecc_key_derive (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
978                               const char *label);
979
980
981 /**
982  * Derive a public key from a given public key and a label.
983  * Essentially calculates a public key 'V = H(l,P) * P'.
984  *
985  * @param pub original public key
986  * @param label label to use for key deriviation
987  * @param result where to write the derived public key
988  */
989 void
990 GNUNET_CRYPTO_ecc_public_key_derive (const struct GNUNET_CRYPTO_EccPublicKey *pub,
991                                      const char *label,
992                                      struct GNUNET_CRYPTO_EccPublicKey *result);
993
994
995 #if 0                           /* keep Emacsens' auto-indent happy */
996 {
997 #endif
998 #ifdef __cplusplus
999 }
1000 #endif
1001
1002
1003 /* ifndef GNUNET_CRYPTO_LIB_H */
1004 #endif
1005 /* end of gnunet_crypto_lib.h */