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