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