3d2c946b0fc9db156e9757e65f06e174458315fb
[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 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  * Desired quality level for cryptographic operations.
48  */
49 enum GNUNET_CRYPTO_Quality
50 {
51   /**
52    * No good quality of the operation is needed (i.e.,
53    * random numbers can be pseudo-random).
54    */
55   GNUNET_CRYPTO_QUALITY_WEAK,
56
57   /**
58    * High-quality operations are desired.
59    */
60   GNUNET_CRYPTO_QUALITY_STRONG,
61
62   /**
63    * Randomness for IVs etc. is required.
64    */
65   GNUNET_CRYPTO_QUALITY_NONCE
66 };
67
68
69 /**
70  * @brief length of the sessionkey in bytes (256 BIT sessionkey)
71  */
72 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
73
74
75 /**
76  * @brief Length of RSA encrypted data (2048 bit)
77  *
78  * We currently do not handle encryption of data
79  * that can not be done in a single call to the
80  * RSA methods (read: large chunks of data).
81  * We should never need that, as we can use
82  * the GNUNET_CRYPTO_hash for larger pieces of data for signing,
83  * and for encryption, we only need to encode sessionkeys!
84  */
85 #define GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH 256
86
87
88 /**
89  * Length of an RSA KEY (d,e,len), 2048 bit (=256 octests) key d, 2 byte e
90  */
91 #define GNUNET_CRYPTO_RSA_KEY_LENGTH 258
92
93
94 /**
95  * Length of a hash value
96  */
97 #define GNUNET_CRYPTO_HASH_LENGTH 512/8
98
99 /**
100  * The private information of an RSA key pair.
101  */
102 struct GNUNET_CRYPTO_RsaPrivateKey;
103
104 GNUNET_NETWORK_STRUCT_BEGIN
105
106 /**
107  * GNUnet mandates a certain format for the encoding
108  * of private RSA key information that is provided
109  * by the RSA implementations.  This format is used
110  * to serialize a private RSA key (typically when
111  * writing it to disk).
112  */
113 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded
114 {
115   /**
116    * Total size of the structure, in bytes, in big-endian!
117    */
118   uint16_t len GNUNET_PACKED;
119   uint16_t sizen GNUNET_PACKED; /*  in big-endian! */
120   uint16_t sizee GNUNET_PACKED; /*  in big-endian! */
121   uint16_t sized GNUNET_PACKED; /*  in big-endian! */
122   uint16_t sizep GNUNET_PACKED; /*  in big-endian! */
123   uint16_t sizeq GNUNET_PACKED; /*  in big-endian! */
124   uint16_t sizedmp1 GNUNET_PACKED;      /*  in big-endian! */
125   uint16_t sizedmq1 GNUNET_PACKED;      /*  in big-endian! */
126   /* followed by the actual values */
127 };
128 GNUNET_NETWORK_STRUCT_END
129
130
131 /**
132  * @brief 0-terminated ASCII encoding of a GNUNET_HashCode.
133  */
134 struct GNUNET_CRYPTO_HashAsciiEncoded
135 {
136   unsigned char encoding[104];
137 };
138
139
140
141 /**
142  * @brief an RSA signature
143  */
144 struct GNUNET_CRYPTO_RsaSignature
145 {
146   unsigned char sig[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH];
147 };
148
149
150 GNUNET_NETWORK_STRUCT_BEGIN
151
152 /**
153  * @brief header of what an RSA signature signs
154  *        this must be followed by "size - 8" bytes of
155  *        the actual signed data
156  */
157 struct GNUNET_CRYPTO_RsaSignaturePurpose
158 {
159   /**
160    * How many bytes does this signature sign?
161    * (including this purpose header); in network
162    * byte order (!).
163    */
164   uint32_t size GNUNET_PACKED;
165
166   /**
167    * What does this signature vouch for?  This
168    * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
169    * constant (from gnunet_signatures.h).  In
170    * network byte order!
171    */
172   uint32_t purpose GNUNET_PACKED;
173
174 };
175
176
177 /**
178  * @brief A public key.
179  */
180 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
181 {
182   /**
183    * In big-endian, must be GNUNET_CRYPTO_RSA_KEY_LENGTH+4
184    */
185   uint16_t len GNUNET_PACKED;
186
187   /**
188    * Size of n in key; in big-endian!
189    */
190   uint16_t sizen GNUNET_PACKED;
191
192   /**
193    * The key itself, contains n followed by e.
194    */
195   unsigned char key[GNUNET_CRYPTO_RSA_KEY_LENGTH];
196
197   /**
198    * Padding (must be 0)
199    */
200   uint16_t padding GNUNET_PACKED;
201 };
202
203
204 /**
205  * RSA Encrypted data.
206  */
207 struct GNUNET_CRYPTO_RsaEncryptedData
208 {
209   unsigned char encoding[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH];
210 };
211
212
213 /**
214  * @brief type for session keys
215  */
216 struct GNUNET_CRYPTO_AesSessionKey
217 {
218   /**
219    * Actual key.
220    */
221   unsigned char key[GNUNET_CRYPTO_AES_KEY_LENGTH];
222
223   /**
224    * checksum!
225    */
226   uint32_t crc32 GNUNET_PACKED;
227 };
228 GNUNET_NETWORK_STRUCT_END
229
230 /**
231  * @brief IV for sym cipher
232  *
233  * NOTE: must be smaller (!) in size than the
234  * GNUNET_HashCode.
235  */
236 struct GNUNET_CRYPTO_AesInitializationVector
237 {
238   unsigned char 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  * Seed a weak random generator. Only GNUNET_CRYPTO_QUALITY_WEAK-mode generator
255  * can be seeded.
256  *
257  * @param seed the seed to use
258  */
259 void
260 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
261
262
263 /**
264  * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
265  *
266  * @param sum current sum, initially 0
267  * @param buf buffer to calculate CRC over (must be 16-bit aligned)
268  * @param len number of bytes in hdr, must be multiple of 2
269  * @return updated crc sum (must be subjected to GNUNET_CRYPTO_crc16_finish to get actual crc16)
270  */
271 uint32_t
272 GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len);
273
274
275 /**
276  * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
277  *
278  * @param sum cummulative sum
279  * @return crc16 value
280  */
281 uint16_t
282 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
283
284
285 /**
286  * Calculate the checksum of a buffer in one step.
287  *
288  * @param buf buffer to  calculate CRC over (must be 16-bit aligned)
289  * @param len number of bytes in hdr, must be multiple of 2
290  * @return crc16 value
291  */
292 uint16_t
293 GNUNET_CRYPTO_crc16_n (const void *buf, size_t len);
294
295
296 /**
297  * Compute the CRC32 checksum for the first len
298  * bytes of the buffer.
299  *
300  * @param buf the data over which we're taking the CRC
301  * @param len the length of the buffer in bytes
302  * @return the resulting CRC32 checksum
303  */
304 int32_t
305 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len);
306
307
308 /**
309  * Produce a random value.
310  *
311  * @param mode desired quality of the random number
312  * @param i the upper limit (exclusive) for the random number
313  * @return a random value in the interval [0,i) (exclusive).
314  */
315 uint32_t
316 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i);
317
318
319 /**
320  * Random on unsigned 64-bit values.
321  *
322  * @param mode desired quality of the random number
323  * @param max value returned will be in range [0,max) (exclusive)
324  * @return random 64-bit number
325  */
326 uint64_t
327 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max);
328
329
330 /**
331  * Get an array with a random permutation of the
332  * numbers 0...n-1.
333  * @param mode GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used, GNUNET_CRYPTO_QUALITY_WEAK otherwise
334  * @param n the size of the array
335  * @return the permutation array (allocated from heap)
336  */
337 unsigned int *
338 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
339
340
341 /**
342  * Create a new Session key.
343  *
344  * @param key key to initialize
345  */
346 void
347 GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key);
348
349 /**
350  * Check that a new session key is well-formed.
351  *
352  * @param key key to check
353  * @return GNUNET_OK if the key is valid
354  */
355 int
356 GNUNET_CRYPTO_aes_check_session_key (const struct GNUNET_CRYPTO_AesSessionKey
357                                      *key);
358
359
360 /**
361  * Encrypt a block with the public key of another
362  * host that uses the same cyper.
363  *
364  * @param block the block to encrypt
365  * @param len the size of the block
366  * @param sessionkey the key used to encrypt
367  * @param iv the initialization vector to use, use INITVALUE
368  *        for streams.
369  * @return the size of the encrypted block, -1 for errors
370  */
371 ssize_t
372 GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
373                            const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
374                            const struct GNUNET_CRYPTO_AesInitializationVector
375                            *iv, void *result);
376
377
378 /**
379  * Decrypt a given block with the sessionkey.
380  *
381  * @param block the data to decrypt, encoded as returned by encrypt
382  * @param size how big is the block?
383  * @param sessionkey the key used to decrypt
384  * @param iv the initialization vector to use
385  * @param result address to store the result at
386  * @return -1 on failure, size of decrypted block on success
387  */
388 ssize_t
389 GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
390                            const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
391                            const struct GNUNET_CRYPTO_AesInitializationVector
392                            *iv, void *result);
393
394
395 /**
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_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
405                              const struct GNUNET_CRYPTO_AesSessionKey *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_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
419                                const struct GNUNET_CRYPTO_AesSessionKey *skey,
420                                const void *salt, size_t salt_len, va_list argp);
421
422
423 /**
424  * Convert hash to ASCII encoding.
425  * @param block the hash code
426  * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
427  *  safely cast to char*, a '\\0' termination is set).
428  */
429 void
430 GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block,
431                            struct GNUNET_CRYPTO_HashAsciiEncoded *result);
432
433
434 /**
435  * Convert ASCII encoding back to GNUNET_CRYPTO_hash
436  *
437  * @param enc the encoding
438  * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
439  * @param result where to store the GNUNET_CRYPTO_hash code
440  * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
441  */
442 int
443 GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
444                                  GNUNET_HashCode * result);
445
446
447 /**
448  * Convert ASCII encoding back to GNUNET_CRYPTO_hash
449  * @param enc the encoding
450  * @param result where to store the GNUNET_CRYPTO_hash code
451  * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
452  */
453 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
454   GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
455
456
457 /**
458  * Compute the distance between 2 hashcodes.
459  * The computation must be fast, not involve
460  * a.a or a.e (they're used elsewhere), and
461  * be somewhat consistent. And of course, the
462  * result should be a positive number.
463  *
464  * @param a some hash code
465  * @param b some hash code
466  * @return number between 0 and UINT32_MAX
467  */
468 uint32_t
469 GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a,
470                                  const GNUNET_HashCode * b);
471
472
473 /**
474  * Compute hash of a given block.
475  *
476  * @param block the data to hash
477  * @param size size of the block
478  * @param ret pointer to where to write the hashcode
479  */
480 void
481 GNUNET_CRYPTO_hash (const void *block, size_t size, GNUNET_HashCode * ret);
482
483
484 /**
485  * Calculate HMAC of a message (RFC 2104)
486  *
487  * @param key secret key
488  * @param plaintext input plaintext
489  * @param plaintext_len length of plaintext
490  * @param hmac where to store the hmac
491  */
492 void
493 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
494                     const void *plaintext, size_t plaintext_len,
495                     GNUNET_HashCode * hmac);
496
497
498 /**
499  * Function called once the hash computation over the
500  * specified file has completed.
501  *
502  * @param cls closure
503  * @param res resulting hash, NULL on error
504  */
505 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
506                                                      const GNUNET_HashCode *
507                                                      res);
508
509
510 /**
511  * Handle to file hashing operation.
512  */
513 struct GNUNET_CRYPTO_FileHashContext;
514
515 /**
516  * Compute the hash of an entire file.
517  *
518  * @param priority scheduling priority to use
519  * @param filename name of file to hash
520  * @param blocksize number of bytes to process in one task
521  * @param callback function to call upon completion
522  * @param callback_cls closure for callback
523  * @return NULL on (immediate) errror
524  */
525 struct GNUNET_CRYPTO_FileHashContext *
526 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
527                          const char *filename, size_t blocksize,
528                          GNUNET_CRYPTO_HashCompletedCallback callback,
529                          void *callback_cls);
530
531
532 /**
533  * Cancel a file hashing operation.
534  *
535  * @param fhc operation to cancel (callback must not yet have been invoked)
536  */
537 void
538 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
539
540
541 /**
542  * Create a random hash code.
543  *
544  * @param mode desired quality level
545  * @param result hash code that is randomized
546  */
547 void
548 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
549                                   GNUNET_HashCode * result);
550
551
552 /**
553  * compute result(delta) = b - a
554  *
555  * @param a some hash code
556  * @param b some hash code
557  * @param result set to b - a
558  */
559 void
560 GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a,
561                                const GNUNET_HashCode * b,
562                                GNUNET_HashCode * result);
563
564
565 /**
566  * compute result(b) = a + delta
567  *
568  * @param a some hash code
569  * @param delta some hash code
570  * @param result set to a + delta
571  */
572 void
573 GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a,
574                         const GNUNET_HashCode * delta,
575                         GNUNET_HashCode * result);
576
577
578 /**
579  * compute result = a ^ b
580  *
581  * @param a some hash code
582  * @param b some hash code
583  * @param result set to a ^ b
584  */
585 void
586 GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b,
587                         GNUNET_HashCode * result);
588
589
590 /**
591  * Convert a hashcode into a key.
592  *
593  * @param hc hash code that serves to generate the key
594  * @param skey set to a valid session key
595  * @param iv set to a valid initialization vector
596  */
597 void
598 GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc,
599                                struct GNUNET_CRYPTO_AesSessionKey *skey,
600                                struct GNUNET_CRYPTO_AesInitializationVector
601                                *iv);
602
603
604 /**
605  * Obtain a bit from a hashcode.
606  *
607  * @param code the GNUNET_CRYPTO_hash to index bit-wise
608  * @param bit index into the hashcode, [0...159]
609  * @return Bit \a bit from hashcode \a code, -1 for invalid index
610  */
611 int
612 GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code, unsigned int bit);
613
614 /**
615  * Determine how many low order bits match in two
616  * GNUNET_HashCodes.  i.e. - 010011 and 011111 share
617  * the first two lowest order bits, and therefore the
618  * return value is two (NOT XOR distance, nor how many
619  * bits match absolutely!).
620  *
621  * @param first the first hashcode
622  * @param second the hashcode to compare first to
623  *
624  * @return the number of bits that match
625  */
626 unsigned int
627 GNUNET_CRYPTO_hash_matching_bits (const GNUNET_HashCode * first,
628                                   const GNUNET_HashCode * second);
629
630
631 /**
632  * Compare function for HashCodes, producing a total ordering
633  * of all hashcodes.
634  *
635  * @param h1 some hash code
636  * @param h2 some hash code
637  * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
638  */
639 int
640 GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, const GNUNET_HashCode * h2);
641
642
643 /**
644  * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
645  * in the XOR metric (Kademlia).
646  *
647  * @param h1 some hash code
648  * @param h2 some hash code
649  * @param target some hash code
650  * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
651  */
652 int
653 GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1,
654                            const GNUNET_HashCode * h2,
655                            const GNUNET_HashCode * target);
656
657
658 /**
659  * @brief Derive an authentication key
660  * @param key authentication key
661  * @param rkey root key
662  * @param salt salt
663  * @param salt_len size of the salt
664  * @param argp pair of void * & size_t for context chunks, terminated by NULL
665  */
666 void
667 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
668                                  const struct GNUNET_CRYPTO_AesSessionKey *rkey,
669                                  const void *salt, size_t salt_len,
670                                  va_list argp);
671
672
673 /**
674  * @brief Derive an authentication key
675  * @param key authentication key
676  * @param rkey root key
677  * @param salt salt
678  * @param salt_len size of the salt
679  * @param ... pair of void * & size_t for context chunks, terminated by NULL
680  */
681 void
682 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
683                                const struct GNUNET_CRYPTO_AesSessionKey *rkey,
684                                const void *salt, size_t salt_len, ...);
685
686 /**
687  * @brief Derive key
688  * @param result buffer for the derived key, allocated by caller
689  * @param out_len desired length of the derived key
690  * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
691  * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
692  * @param xts salt
693  * @param xts_len length of xts
694  * @param skm source key material
695  * @param skm_len length of skm
696  * @return GNUNET_YES on success
697  */
698 int
699 GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
700                     const void *xts, size_t xts_len, const void *skm,
701                     size_t skm_len, ...);
702
703
704 /**
705  * @brief Derive key
706  * @param result buffer for the derived key, allocated by caller
707  * @param out_len desired length of the derived key
708  * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
709  * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
710  * @param xts salt
711  * @param xts_len length of xts
712  * @param skm source key material
713  * @param skm_len length of skm
714  * @param argp va_list of void * & size_t pairs for context chunks
715  * @return GNUNET_YES on success
716  */
717 int
718 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
719                       const void *xts, size_t xts_len, const void *skm,
720                       size_t skm_len, va_list argp);
721
722
723 /**
724  * @brief Derive key
725  * @param result buffer for the derived key, allocated by caller
726  * @param out_len desired length of the derived key
727  * @param xts salt
728  * @param xts_len length of xts
729  * @param skm source key material
730  * @param skm_len length of skm
731  * @param argp va_list of void * & size_t pairs for context chunks
732  * @return GNUNET_YES on success
733  */
734 int
735 GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
736                      size_t xts_len, const void *skm, size_t skm_len,
737                      va_list argp);
738
739
740 /**
741  * @brief Derive key
742  * @param result buffer for the derived key, allocated by caller
743  * @param out_len desired length of the derived key
744  * @param xts salt
745  * @param xts_len length of xts
746  * @param skm source key material
747  * @param skm_len length of skm
748  * @param ... void * & size_t pairs for context chunks
749  * @return GNUNET_YES on success
750  */
751 int
752 GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
753                    size_t xts_len, const void *skm, size_t skm_len, ...);
754
755
756 /**
757  * Create a new private key. Caller must free return value.
758  *
759  * @return fresh private key
760  */
761 struct GNUNET_CRYPTO_RsaPrivateKey *
762 GNUNET_CRYPTO_rsa_key_create (void);
763
764 /**
765  * Encode the private key in a format suitable for
766  * storing it into a file.
767  * @returns encoding of the private key.
768  *    The first 4 bytes give the size of the array, as usual.
769  */
770 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *
771 GNUNET_CRYPTO_rsa_encode_key (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey);
772
773 /**
774  * Decode the private key from the data-format back
775  * to the "normal", internal format.
776  *
777  * @param buf the buffer where the private key data is stored
778  * @param len the length of the data in 'buffer'
779  */
780 struct GNUNET_CRYPTO_RsaPrivateKey *
781 GNUNET_CRYPTO_rsa_decode_key (const char *buf, uint16_t len);
782
783 /**
784  * Create a new private key by reading it from a file.  If the
785  * files does not exist, create a new key and write it to the
786  * file.  Caller must free return value. Note that this function
787  * can not guarantee that another process might not be trying
788  * the same operation on the same file at the same time.
789  * If the contents of the file
790  * are invalid the old file is deleted and a fresh key is
791  * created.
792  *
793  * @param filename name of file to use for storage
794  * @return new private key, NULL on error (for example,
795  *   permission denied)
796  */
797 struct GNUNET_CRYPTO_RsaPrivateKey *
798 GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename);
799
800
801 /**
802  * Setup a hostkey file for a peer given the name of the
803  * configuration file (!).  This function is used so that
804  * at a later point code can be certain that reading a
805  * hostkey is fast (for example in time-dependent testcases).
806  *
807  * @param cfg_name name of the configuration file to use
808  */
809 void
810 GNUNET_CRYPTO_setup_hostkey (const char *cfg_name);
811
812
813 /**
814  * Deterministically (!) create a private key using only the
815  * given HashCode as input to the PRNG.
816  *
817  * @param hc "random" input to PRNG
818  * @return some private key purely dependent on input
819  */
820 struct GNUNET_CRYPTO_RsaPrivateKey *
821 GNUNET_CRYPTO_rsa_key_create_from_hash (const GNUNET_HashCode * hc);
822
823
824 /**
825  * Free memory occupied by the private key.
826  * @param hostkey pointer to the memory to free
827  */
828 void
829 GNUNET_CRYPTO_rsa_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey);
830
831
832 /**
833  * Extract the public key of the host.
834  *
835  * @param priv the private key
836  * @param pub where to write the public key
837  */
838 void
839 GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey
840                                   *priv,
841                                   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
842                                   *pub);
843
844
845 /**
846  * Encrypt a block with the public key of another host that uses the
847  * same cyper.
848  *
849  * @param block the block to encrypt
850  * @param size the size of block
851  * @param publicKey the encoded public key used to encrypt
852  * @param target where to store the encrypted block
853  * @return GNUNET_SYSERR on error, GNUNET_OK if ok
854  */
855 int
856 GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size,
857                            const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
858                            *publicKey,
859                            struct GNUNET_CRYPTO_RsaEncryptedData *target);
860
861
862 /**
863  * Decrypt a given block with the hostkey.
864  *
865  * @param key the key to use
866  * @param block the data to decrypt, encoded as returned by encrypt, not consumed
867  * @param result pointer to a location where the result can be stored
868  * @param max how many bytes of a result are expected? Must be exact.
869  * @return the size of the decrypted block (that is, size) or -1 on error
870  */
871 ssize_t
872 GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
873                            const struct GNUNET_CRYPTO_RsaEncryptedData *block,
874                            void *result, size_t max);
875
876
877 /**
878  * Sign a given block.
879  *
880  * @param key private key to use for the signing
881  * @param purpose what to sign (size, purpose)
882  * @param sig where to write the signature
883  * @return GNUNET_SYSERR on error, GNUNET_OK on success
884  */
885 int
886 GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
887                         const struct GNUNET_CRYPTO_RsaSignaturePurpose *purpose,
888                         struct GNUNET_CRYPTO_RsaSignature *sig);
889
890
891 /**
892  * Verify signature.  Note that the caller MUST have already
893  * checked that "validate->size" bytes are actually available.
894  *
895  * @param purpose what is the purpose that validate should have?
896  * @param validate block to validate (size, purpose, data)
897  * @param sig signature that is being validated
898  * @param publicKey public key of the signer
899  * @return GNUNET_OK if ok, GNUNET_SYSERR if invalid
900  */
901 int
902 GNUNET_CRYPTO_rsa_verify (uint32_t purpose,
903                           const struct GNUNET_CRYPTO_RsaSignaturePurpose
904                           *validate,
905                           const struct GNUNET_CRYPTO_RsaSignature *sig,
906                           const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
907                           *publicKey);
908
909
910
911 /**
912  * This function should only be called in testcases
913  * where strong entropy gathering is not desired
914  * (for example, for hostkey generation).
915  */
916 void
917 GNUNET_CRYPTO_random_disable_entropy_gathering (void);
918
919 #if 0                           /* keep Emacsens' auto-indent happy */
920 {
921 #endif
922 #ifdef __cplusplus
923 }
924 #endif
925
926
927 /* ifndef GNUNET_CRYPTO_LIB_H */
928 #endif
929 /* end of gnunet_crypto_lib.h */