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