added ATS addresstype information to unix
[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
105 /**
106  * @brief 0-terminated ASCII encoding of a GNUNET_HashCode.
107  */
108 struct GNUNET_CRYPTO_HashAsciiEncoded
109 {
110   unsigned char encoding[104];
111 };
112
113
114
115 /**
116  * @brief an RSA signature
117  */
118 struct GNUNET_CRYPTO_RsaSignature
119 {
120   unsigned char sig[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH];
121 };
122
123
124 /**
125  * @brief header of what an RSA signature signs
126  *        this must be followed by "size - 8" bytes of
127  *        the actual signed data
128  */
129 struct GNUNET_CRYPTO_RsaSignaturePurpose
130 {
131   /**
132    * How many bytes does this signature sign?
133    * (including this purpose header); in network
134    * byte order (!).
135    */
136   uint32_t size GNUNET_PACKED;
137
138   /**
139    * What does this signature vouch for?  This
140    * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
141    * constant (from gnunet_signatures.h).  In
142    * network byte order!
143    */
144   uint32_t purpose GNUNET_PACKED;
145
146 };
147
148
149 /**
150  * @brief A public key.
151  */
152 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
153 {
154   /**
155    * In big-endian, must be GNUNET_CRYPTO_RSA_KEY_LENGTH+4
156    */
157   uint16_t len GNUNET_PACKED;
158
159   /**
160    * Size of n in key; in big-endian!
161    */
162   uint16_t sizen GNUNET_PACKED;
163
164   /**
165    * The key itself, contains n followed by e.
166    */
167   unsigned char key[GNUNET_CRYPTO_RSA_KEY_LENGTH];
168
169   /**
170    * Padding (must be 0)
171    */
172   uint16_t padding GNUNET_PACKED;
173 };
174
175
176 /**
177  * RSA Encrypted data.
178  */
179 struct GNUNET_CRYPTO_RsaEncryptedData
180 {
181   unsigned char encoding[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH];
182 };
183
184
185 /**
186  * @brief type for session keys
187  */
188 struct GNUNET_CRYPTO_AesSessionKey
189 {
190   /**
191    * Actual key.
192    */
193   unsigned char key[GNUNET_CRYPTO_AES_KEY_LENGTH];
194
195   /**
196    * checksum!
197    */
198   uint32_t crc32 GNUNET_PACKED;
199 };
200
201
202 /**
203  * @brief IV for sym cipher
204  *
205  * NOTE: must be smaller (!) in size than the
206  * 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  * Seed a weak random generator. Only GNUNET_CRYPTO_QUALITY_WEAK-mode generator
227  * can be seeded.
228  *
229  * @param seed the seed to use
230  */
231 void
232 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
233
234 /**
235  * Compute the CRC32 checksum for the first len
236  * bytes of the buffer.
237  *
238  * @param buf the data over which we're taking the CRC
239  * @param len the length of the buffer in bytes
240  * @return the resulting CRC32 checksum
241  */
242 int32_t
243 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len);
244
245
246 /**
247  * Produce a random value.
248  *
249  * @param mode desired quality of the random number
250  * @param i the upper limit (exclusive) for the random number
251  * @return a random value in the interval [0,i) (exclusive).
252  */
253 uint32_t
254 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i);
255
256
257 /**
258  * Random on unsigned 64-bit values.
259  *
260  * @param mode desired quality of the random number
261  * @param max value returned will be in range [0,max) (exclusive)
262  * @return random 64-bit number
263  */
264 uint64_t
265 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max);
266
267
268 /**
269  * Get an array with a random permutation of the
270  * numbers 0...n-1.
271  * @param mode GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used, GNUNET_CRYPTO_QUALITY_WEAK otherwise
272  * @param n the size of the array
273  * @return the permutation array (allocated from heap)
274  */
275 unsigned int *
276 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
277
278
279 /**
280  * Create a new Session key.
281  *
282  * @param key key to initialize
283  */
284 void
285 GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key);
286
287 /**
288  * Check that a new session key is well-formed.
289  *
290  * @param key key to check
291  * @return GNUNET_OK if the key is valid
292  */
293 int
294 GNUNET_CRYPTO_aes_check_session_key (const struct GNUNET_CRYPTO_AesSessionKey
295                                      *key);
296
297
298 /**
299  * Encrypt a block with the public key of another
300  * host that uses the same cyper.
301  *
302  * @param block the block to encrypt
303  * @param len the size of the block
304  * @param sessionkey the key used to encrypt
305  * @param iv the initialization vector to use, use INITVALUE
306  *        for streams.
307  * @return the size of the encrypted block, -1 for errors
308  */
309 ssize_t
310 GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
311                            const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
312                            const struct GNUNET_CRYPTO_AesInitializationVector
313                            *iv, void *result);
314
315
316 /**
317  * Decrypt a given block with the sessionkey.
318  *
319  * @param block the data to decrypt, encoded as returned by encrypt
320  * @param size how big is the block?
321  * @param sessionkey the key used to decrypt
322  * @param iv the initialization vector to use
323  * @param result address to store the result at
324  * @return -1 on failure, size of decrypted block on success
325  */
326 ssize_t
327 GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
328                            const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
329                            const struct GNUNET_CRYPTO_AesInitializationVector
330                            *iv, void *result);
331
332
333 /**
334  * @brief Derive an IV
335  * @param iv initialization vector
336  * @param skey session key
337  * @param salt salt for the derivation
338  * @param salt_len size of the salt
339  * @param ... pairs of void * & size_t for context chunks, terminated by NULL
340  */
341 void
342 GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
343                              const struct GNUNET_CRYPTO_AesSessionKey *skey,
344                              const void *salt, size_t salt_len, ...);
345
346
347 /**
348  * @brief Derive an IV
349  * @param iv initialization vector
350  * @param skey session key
351  * @param salt salt for the derivation
352  * @param salt_len size of the salt
353  * @param argp pairs of void * & size_t for context chunks, terminated by NULL
354  */
355 void
356 GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
357                                const struct GNUNET_CRYPTO_AesSessionKey *skey,
358                                const void *salt, size_t salt_len, va_list argp);
359
360
361 /**
362  * Convert hash to ASCII encoding.
363  * @param block the hash code
364  * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
365  *  safely cast to char*, a '\\0' termination is set).
366  */
367 void
368 GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block,
369                            struct GNUNET_CRYPTO_HashAsciiEncoded *result);
370
371
372 /**
373  * Convert ASCII encoding back to GNUNET_CRYPTO_hash
374  * @param enc the encoding
375  * @param result where to store the GNUNET_CRYPTO_hash code
376  * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
377  */
378 int
379 GNUNET_CRYPTO_hash_from_string (const char *enc, GNUNET_HashCode * result);
380
381
382 /**
383  * Compute the distance between 2 hashcodes.
384  * The computation must be fast, not involve
385  * a.a or a.e (they're used elsewhere), and
386  * be somewhat consistent. And of course, the
387  * result should be a positive number.
388  *
389  * @param a some hash code
390  * @param b some hash code
391  * @return number between 0 and UINT32_MAX
392  */
393 uint32_t
394 GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a,
395                                  const GNUNET_HashCode * b);
396
397
398 /**
399  * Compute hash of a given block.
400  *
401  * @param block the data to hash
402  * @param size size of the block
403  * @param ret pointer to where to write the hashcode
404  */
405 void
406 GNUNET_CRYPTO_hash (const void *block, size_t size, GNUNET_HashCode * ret);
407
408
409 /**
410  * Calculate HMAC of a message (RFC 2104)
411  *
412  * @param key secret key
413  * @param plaintext input plaintext
414  * @param plaintext_len length of plaintext
415  * @param hmac where to store the hmac
416  */
417 void
418 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
419                     const void *plaintext, size_t plaintext_len,
420                     GNUNET_HashCode * hmac);
421
422
423 /**
424  * Function called once the hash computation over the
425  * specified file has completed.
426  *
427  * @param cls closure
428  * @param res resulting hash, NULL on error
429  */
430 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
431                                                      const GNUNET_HashCode *
432                                                      res);
433
434
435 /**
436  * Handle to file hashing operation.
437  */
438 struct GNUNET_CRYPTO_FileHashContext;
439
440 /**
441  * Compute the hash of an entire file.
442  *
443  * @param priority scheduling priority to use
444  * @param filename name of file to hash
445  * @param blocksize number of bytes to process in one task
446  * @param callback function to call upon completion
447  * @param callback_cls closure for callback
448  * @return NULL on (immediate) errror
449  */
450 struct GNUNET_CRYPTO_FileHashContext *
451 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
452                          const char *filename, size_t blocksize,
453                          GNUNET_CRYPTO_HashCompletedCallback callback,
454                          void *callback_cls);
455
456
457 /**
458  * Cancel a file hashing operation.
459  *
460  * @param fhc operation to cancel (callback must not yet have been invoked)
461  */
462 void
463 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
464
465
466 /**
467  * Create a random hash code.
468  *
469  * @param mode desired quality level
470  * @param result hash code that is randomized
471  */
472 void
473 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
474                                   GNUNET_HashCode * result);
475
476
477 /**
478  * compute result(delta) = b - a
479  *
480  * @param a some hash code
481  * @param b some hash code
482  * @param result set to b - a
483  */
484 void
485 GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a,
486                                const GNUNET_HashCode * b,
487                                GNUNET_HashCode * result);
488
489
490 /**
491  * compute result(b) = a + delta
492  *
493  * @param a some hash code
494  * @param delta some hash code
495  * @param result set to a + delta
496  */
497 void
498 GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a,
499                         const GNUNET_HashCode * delta,
500                         GNUNET_HashCode * result);
501
502
503 /**
504  * compute result = a ^ b
505  *
506  * @param a some hash code
507  * @param b some hash code
508  * @param result set to a ^ b
509  */
510 void
511 GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b,
512                         GNUNET_HashCode * result);
513
514
515 /**
516  * Convert a hashcode into a key.
517  *
518  * @param hc hash code that serves to generate the key
519  * @param skey set to a valid session key
520  * @param iv set to a valid initialization vector
521  */
522 void
523 GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc,
524                                struct GNUNET_CRYPTO_AesSessionKey *skey,
525                                struct GNUNET_CRYPTO_AesInitializationVector
526                                *iv);
527
528
529 /**
530  * Obtain a bit from a hashcode.
531  *
532  * @param code the GNUNET_CRYPTO_hash to index bit-wise
533  * @param bit index into the hashcode, [0...159]
534  * @return Bit \a bit from hashcode \a code, -1 for invalid index
535  */
536 int
537 GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code, unsigned int bit);
538
539 /**
540  * Determine how many low order bits match in two
541  * GNUNET_HashCodes.  i.e. - 010011 and 011111 share
542  * the first two lowest order bits, and therefore the
543  * return value is two (NOT XOR distance, nor how many
544  * bits match absolutely!).
545  *
546  * @param first the first hashcode
547  * @param second the hashcode to compare first to
548  *
549  * @return the number of bits that match
550  */
551 unsigned int
552 GNUNET_CRYPTO_hash_matching_bits (const GNUNET_HashCode * first,
553                                   const GNUNET_HashCode * second);
554
555
556 /**
557  * Compare function for HashCodes, producing a total ordering
558  * of all hashcodes.
559  *
560  * @param h1 some hash code
561  * @param h2 some hash code
562  * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
563  */
564 int
565 GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, const GNUNET_HashCode * h2);
566
567
568 /**
569  * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
570  * in the XOR metric (Kademlia).
571  *
572  * @param h1 some hash code
573  * @param h2 some hash code
574  * @param target some hash code
575  * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
576  */
577 int
578 GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1,
579                            const GNUNET_HashCode * h2,
580                            const GNUNET_HashCode * target);
581
582
583 /**
584  * @brief Derive an authentication key
585  * @param key authentication key
586  * @param rkey root key
587  * @param salt salt
588  * @param salt_len size of the salt
589  * @param argp pair of void * & size_t for context chunks, terminated by NULL
590  */
591 void
592 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
593                                  const struct GNUNET_CRYPTO_AesSessionKey *rkey,
594                                  const void *salt, size_t salt_len,
595                                  va_list argp);
596
597
598 /**
599  * @brief Derive an authentication key
600  * @param key authentication key
601  * @param rkey root key
602  * @param salt salt
603  * @param salt_len size of the salt
604  * @param ... pair of void * & size_t for context chunks, terminated by NULL
605  */
606 void
607 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
608                                const struct GNUNET_CRYPTO_AesSessionKey *rkey,
609                                const void *salt, size_t salt_len, ...);
610
611 /**
612  * @brief Derive key
613  * @param result buffer for the derived key, allocated by caller
614  * @param out_len desired length of the derived key
615  * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
616  * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
617  * @param xts salt
618  * @param xts_len length of xts
619  * @param skm source key material
620  * @param skm_len length of skm
621  * @return GNUNET_YES on success
622  */
623 int
624 GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
625                     const void *xts, size_t xts_len, const void *skm,
626                     size_t skm_len, ...);
627
628
629 /**
630  * @brief Derive key
631  * @param result buffer for the derived key, allocated by caller
632  * @param out_len desired length of the derived key
633  * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
634  * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
635  * @param xts salt
636  * @param xts_len length of xts
637  * @param skm source key material
638  * @param skm_len length of skm
639  * @param argp va_list of void * & size_t pairs for context chunks
640  * @return GNUNET_YES on success
641  */
642 int
643 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
644                       const void *xts, size_t xts_len, const void *skm,
645                       size_t skm_len, va_list argp);
646
647
648 /**
649  * @brief Derive key
650  * @param result buffer for the derived key, allocated by caller
651  * @param out_len desired length of the derived key
652  * @param xts salt
653  * @param xts_len length of xts
654  * @param skm source key material
655  * @param skm_len length of skm
656  * @param argp va_list of void * & size_t pairs for context chunks
657  * @return GNUNET_YES on success
658  */
659 int
660 GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
661                      size_t xts_len, const void *skm, size_t skm_len,
662                      va_list argp);
663
664
665 /**
666  * @brief Derive key
667  * @param result buffer for the derived key, allocated by caller
668  * @param out_len desired length of the derived key
669  * @param xts salt
670  * @param xts_len length of xts
671  * @param skm source key material
672  * @param skm_len length of skm
673  * @param ... void * & size_t pairs for context chunks
674  * @return GNUNET_YES on success
675  */
676 int
677 GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
678                    size_t xts_len, const void *skm, size_t skm_len, ...);
679
680
681 /**
682  * Create a new private key. Caller must free return value.
683  *
684  * @return fresh private key
685  */
686 struct GNUNET_CRYPTO_RsaPrivateKey *
687 GNUNET_CRYPTO_rsa_key_create (void);
688
689 /**
690  * Decode the private key from the data-format back
691  * to the "normal", internal format.
692  *
693  * @param buf the buffer where the private key data is stored
694  * @param len the length of the data in 'buffer'
695  */
696 struct GNUNET_CRYPTO_RsaPrivateKey *
697 GNUNET_CRYPTO_rsa_decode_key (const char *buf, uint16_t len);
698
699 /**
700  * Create a new private key by reading it from a file.  If the
701  * files does not exist, create a new key and write it to the
702  * file.  Caller must free return value. Note that this function
703  * can not guarantee that another process might not be trying
704  * the same operation on the same file at the same time.
705  * If the contents of the file
706  * are invalid the old file is deleted and a fresh key is
707  * created.
708  *
709  * @param filename name of file to use for storage
710  * @return new private key, NULL on error (for example,
711  *   permission denied)
712  */
713 struct GNUNET_CRYPTO_RsaPrivateKey *
714 GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename);
715
716
717 /**
718  * Deterministically (!) create a private key using only the
719  * given HashCode as input to the PRNG.
720  *
721  * @param hc "random" input to PRNG
722  * @return some private key purely dependent on input
723  */
724 struct GNUNET_CRYPTO_RsaPrivateKey *
725 GNUNET_CRYPTO_rsa_key_create_from_hash (const GNUNET_HashCode * hc);
726
727
728 /**
729  * Free memory occupied by the private key.
730  * @param hostkey pointer to the memory to free
731  */
732 void
733 GNUNET_CRYPTO_rsa_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey);
734
735
736 /**
737  * Extract the public key of the host.
738  *
739  * @param priv the private key
740  * @param pub where to write the public key
741  */
742 void
743 GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey
744                                   *priv,
745                                   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
746                                   *pub);
747
748
749 /**
750  * Encrypt a block with the public key of another host that uses the
751  * same cyper.
752  *
753  * @param block the block to encrypt
754  * @param size the size of block
755  * @param publicKey the encoded public key used to encrypt
756  * @param target where to store the encrypted block
757  * @return GNUNET_SYSERR on error, GNUNET_OK if ok
758  */
759 int
760 GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size,
761                            const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
762                            *publicKey,
763                            struct GNUNET_CRYPTO_RsaEncryptedData *target);
764
765
766 /**
767  * Decrypt a given block with the hostkey.
768  *
769  * @param key the key to use
770  * @param block the data to decrypt, encoded as returned by encrypt, not consumed
771  * @param result pointer to a location where the result can be stored
772  * @param max how many bytes of a result are expected? Must be exact.
773  * @return the size of the decrypted block (that is, size) or -1 on error
774  */
775 ssize_t
776 GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
777                            const struct GNUNET_CRYPTO_RsaEncryptedData *block,
778                            void *result, size_t max);
779
780
781 /**
782  * Sign a given block.
783  *
784  * @param key private key to use for the signing
785  * @param purpose what to sign (size, purpose)
786  * @param sig where to write the signature
787  * @return GNUNET_SYSERR on error, GNUNET_OK on success
788  */
789 int
790 GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
791                         const struct GNUNET_CRYPTO_RsaSignaturePurpose *purpose,
792                         struct GNUNET_CRYPTO_RsaSignature *sig);
793
794
795 /**
796  * Verify signature.  Note that the caller MUST have already
797  * checked that "validate->size" bytes are actually available.
798  *
799  * @param purpose what is the purpose that validate should have?
800  * @param validate block to validate (size, purpose, data)
801  * @param sig signature that is being validated
802  * @param publicKey public key of the signer
803  * @return GNUNET_OK if ok, GNUNET_SYSERR if invalid
804  */
805 int
806 GNUNET_CRYPTO_rsa_verify (uint32_t purpose,
807                           const struct GNUNET_CRYPTO_RsaSignaturePurpose
808                           *validate,
809                           const struct GNUNET_CRYPTO_RsaSignature *sig,
810                           const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
811                           *publicKey);
812
813
814
815 /**
816  * This function should only be called in testcases
817  * where strong entropy gathering is not desired
818  * (for example, for hostkey generation).
819  */
820 void
821 GNUNET_CRYPTO_random_disable_entropy_gathering (void);
822
823 #if 0                           /* keep Emacsens' auto-indent happy */
824 {
825 #endif
826 #ifdef __cplusplus
827 }
828 #endif
829
830
831 /* ifndef GNUNET_CRYPTO_LIB_H */
832 #endif
833 /* end of gnunet_crypto_lib.h */