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