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