1efa29d0ae1b655664c66d7f216c14bc8a31c729
[oweals/gnunet.git] / src / include / gnunet_crypto_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2001-2013 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 3, 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  * @defgroup crypto Cryptographic operations
32  * @defgroup hash Hashing and operations on hashes
33  */
34
35 #ifndef GNUNET_CRYPTO_LIB_H
36 #define GNUNET_CRYPTO_LIB_H
37
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #if 0                           /* keep Emacsens' auto-indent happy */
42 }
43 #endif
44 #endif
45
46 #include "gnunet_common.h"
47 #include "gnunet_scheduler_lib.h"
48
49
50 /**
51  * Maximum length of an ECC signature.
52  * Note: round up to multiple of 8 minus 2 for alignment.
53  */
54 #define GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH 126
55
56
57 /**
58  * Desired quality level for random numbers.
59  * @ingroup crypto
60  */
61 enum GNUNET_CRYPTO_Quality
62 {
63   /**
64    * No good quality of the operation is needed (i.e.,
65    * random numbers can be pseudo-random).
66    * @ingroup crypto
67    */
68   GNUNET_CRYPTO_QUALITY_WEAK,
69
70   /**
71    * High-quality operations are desired.
72    * @ingroup crypto
73    */
74   GNUNET_CRYPTO_QUALITY_STRONG,
75
76   /**
77    * Randomness for IVs etc. is required.
78    * @ingroup crypto
79    */
80   GNUNET_CRYPTO_QUALITY_NONCE
81 };
82
83
84 /**
85  * @brief length of the sessionkey in bytes (256 BIT sessionkey)
86  */
87 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
88
89 /**
90  * Length of a hash value
91  */
92 #define GNUNET_CRYPTO_HASH_LENGTH (512/8)
93
94 /**
95  * @brief 0-terminated ASCII encoding of a struct GNUNET_HashCode.
96  */
97 struct GNUNET_CRYPTO_HashAsciiEncoded
98 {
99   unsigned char encoding[104];
100 };
101
102
103 GNUNET_NETWORK_STRUCT_BEGIN
104
105
106 /**
107  * @brief header of what an ECC signature signs
108  *        this must be followed by "size - 8" bytes of
109  *        the actual signed data
110  */
111 struct GNUNET_CRYPTO_EccSignaturePurpose
112 {
113   /**
114    * How many bytes does this signature sign?
115    * (including this purpose header); in network
116    * byte order (!).
117    */
118   uint32_t size GNUNET_PACKED;
119
120   /**
121    * What does this signature vouch for?  This
122    * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
123    * constant (from gnunet_signatures.h).  In
124    * network byte order!
125    */
126   uint32_t purpose GNUNET_PACKED;
127
128 };
129
130
131 /**
132  * @brief an ECC signature
133  */
134 struct GNUNET_CRYPTO_EccSignature
135 {
136
137   /**
138    * R value.
139    */
140   unsigned char r[256 / 8];
141
142   /**
143    * S value.
144    */
145   unsigned char s[256 / 8];
146
147 };
148
149
150 /**
151  * Public ECC key (always for NIST P-521) encoded in a format suitable
152  * for network transmission and signatures (ECDSA/EdDSA).
153  */
154 struct GNUNET_CRYPTO_EccPublicSignKey
155 {
156   /**
157    * Q consists of an x- and a y-value, each mod p (256 bits),
158    * given here in affine coordinates.
159    */
160   unsigned char q_y[256 / 8];
161
162 };
163
164
165 /**
166  * Public ECC key (always for NIST P-521) encoded in a format suitable
167  * for network transmission and encryption (ECDH).
168  */
169 struct GNUNET_CRYPTO_EccPublicEncryptKey 
170 {
171   /**
172    * Q consists of an x- and a y-value, each mod p (256 bits),
173    * given here in affine coordinates.
174    */
175   unsigned char q_x[256 / 8];
176
177 };
178
179
180 /**
181  * Private ECC key encoded for transmission.
182  */
183 struct GNUNET_CRYPTO_EccPrivateKey
184 {
185   /**
186    * d is a value mod n, where n has at most 256 bits.
187    */
188   unsigned char d[256 / 8];
189
190 };
191
192
193 /**
194  * @brief type for session keys
195  */
196 struct GNUNET_CRYPTO_AesSessionKey
197 {
198   /**
199    * Actual key.
200    */
201   unsigned char key[GNUNET_CRYPTO_AES_KEY_LENGTH];
202
203 };
204 GNUNET_NETWORK_STRUCT_END
205
206 /**
207  * @brief IV for sym cipher
208  *
209  * NOTE: must be smaller (!) in size than the
210  * struct GNUNET_HashCode.
211  */
212 struct GNUNET_CRYPTO_AesInitializationVector
213 {
214   unsigned char iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
215 };
216
217
218 /**
219  * @brief type for (message) authentication keys
220  */
221 struct GNUNET_CRYPTO_AuthKey
222 {
223   unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
224 };
225
226
227 /* **************** Functions and Macros ************* */
228
229 /**
230  * @ingroup crypto
231  * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator
232  * can be seeded.
233  *
234  * @param seed the seed to use
235  */
236 void
237 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
238
239
240 /**
241  * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
242  *
243  * @param sum current sum, initially 0
244  * @param buf buffer to calculate CRC over (must be 16-bit aligned)
245  * @param len number of bytes in @a buf, must be multiple of 2
246  * @return updated crc sum (must be subjected to GNUNET_CRYPTO_crc16_finish to get actual crc16)
247  */
248 uint32_t
249 GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len);
250
251
252 /**
253  * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
254  *
255  * @param sum cummulative sum
256  * @return crc16 value
257  */
258 uint16_t
259 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
260
261
262 /**
263  * @ingroup hash
264  * Calculate the checksum of a buffer in one step.
265  *
266  * @param buf buffer to calculate CRC over (must be 16-bit aligned)
267  * @param len number of bytes in @a buf, must be multiple of 2
268  * @return crc16 value
269  */
270 uint16_t
271 GNUNET_CRYPTO_crc16_n (const void *buf, size_t len);
272
273
274 /**
275  * @ingroup hash
276  * Compute the CRC32 checksum for the first len
277  * bytes of the buffer.
278  *
279  * @param buf the data over which we're taking the CRC
280  * @param len the length of the buffer @a buf in bytes
281  * @return the resulting CRC32 checksum
282  */
283 int32_t
284 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len);
285
286
287 /**
288  * @ingroup crypto
289  * Produce a random value.
290  *
291  * @param mode desired quality of the random number
292  * @param i the upper limit (exclusive) for the random number
293  * @return a random value in the interval [0,@a i) (exclusive).
294  */
295 uint32_t
296 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i);
297
298
299 /**
300  * @ingroup crypto
301  * Random on unsigned 64-bit values.
302  *
303  * @param mode desired quality of the random number
304  * @param max value returned will be in range [0,@a max) (exclusive)
305  * @return random 64-bit number
306  */
307 uint64_t
308 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max);
309
310
311 /**
312  * @ingroup crypto
313  * Get an array with a random permutation of the
314  * numbers 0...n-1.
315  * @param mode #GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used,
316  *             #GNUNET_CRYPTO_QUALITY_WEAK or #GNUNET_CRYPTO_QUALITY_NONCE otherwise
317  * @param n the size of the array
318  * @return the permutation array (allocated from heap)
319  */
320 unsigned int *
321 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
322
323
324 /**
325  * @ingroup crypto
326  * Create a new random session key.
327  *
328  * @param key key to initialize
329  */
330 void
331 GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key);
332
333
334 /**
335  * @ingroup crypto
336  * Encrypt a block using a symmetric sessionkey.
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  * @ingroup crypto
354  * Decrypt a given block using a symmetric sessionkey.
355  *
356  * @param block the data to decrypt, encoded as returned by encrypt
357  * @param size how big is the block?
358  * @param sessionkey the key used to decrypt
359  * @param iv the initialization vector to use
360  * @param result address to store the result at
361  * @return -1 on failure, size of decrypted block on success
362  */
363 ssize_t
364 GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
365                            const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
366                            const struct GNUNET_CRYPTO_AesInitializationVector
367                            *iv, void *result);
368
369
370 /**
371  * @ingroup crypto
372  * @brief Derive an IV
373  * @param iv initialization vector
374  * @param skey session key
375  * @param salt salt for the derivation
376  * @param salt_len size of the salt
377  * @param ... pairs of void * & size_t for context chunks, terminated by NULL
378  */
379 void
380 GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
381                              const struct GNUNET_CRYPTO_AesSessionKey *skey,
382                              const void *salt, size_t salt_len, ...);
383
384
385 /**
386  * @brief Derive an IV
387  * @param iv initialization vector
388  * @param skey session key
389  * @param salt salt for the derivation
390  * @param salt_len size of the salt
391  * @param argp pairs of void * & size_t for context chunks, terminated by NULL
392  */
393 void
394 GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
395                                const struct GNUNET_CRYPTO_AesSessionKey *skey,
396                                const void *salt, size_t salt_len, va_list argp);
397
398
399 /**
400  * @ingroup hash
401  * Convert hash to ASCII encoding.
402  * @param block the hash code
403  * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
404  *  safely cast to char*, a '\\0' termination is set).
405  */
406 void
407 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode * block,
408                            struct GNUNET_CRYPTO_HashAsciiEncoded *result);
409
410
411 /**
412  * @ingroup hash
413  * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
414  *
415  * @param enc the encoding
416  * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
417  * @param result where to store the hash code
418  * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
419  */
420 int
421 GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
422                                  struct GNUNET_HashCode *result);
423
424
425 /**
426  * @ingroup hash
427  * Convert ASCII encoding back to `struct GNUNET_HashCode`
428  *
429  * @param enc the encoding
430  * @param result where to store the hash code
431  * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
432  */
433 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
434   GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
435
436
437 /**
438  * @ingroup hash
439  *
440  * Compute the distance between 2 hashcodes.  The
441  * computation must be fast, not involve a[0] or a[4] (they're used
442  * elsewhere), and be somewhat consistent. And of course, the result
443  * should be a positive number.
444  *
445  * @param a some hash code
446  * @param b some hash code
447  * @return number between 0 and UINT32_MAX
448  */
449 uint32_t
450 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode * a,
451                                  const struct GNUNET_HashCode * b);
452
453
454 /**
455  * @ingroup hash
456  * Compute hash of a given block.
457  *
458  * @param block the data to hash
459  * @param size size of the @a block
460  * @param ret pointer to where to write the hashcode
461  */
462 void
463 GNUNET_CRYPTO_hash (const void *block, size_t size, struct GNUNET_HashCode * ret);
464
465
466 /**
467  * @ingroup hash
468  * Calculate HMAC of a message (RFC 2104)
469  *
470  * @param key secret key
471  * @param plaintext input plaintext
472  * @param plaintext_len length of @a plaintext
473  * @param hmac where to store the hmac
474  */
475 void
476 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
477                     const void *plaintext, size_t plaintext_len,
478                     struct GNUNET_HashCode * hmac);
479
480
481 /**
482  * Function called once the hash computation over the
483  * specified file has completed.
484  *
485  * @param cls closure
486  * @param res resulting hash, NULL on error
487  */
488 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
489                                                      const struct GNUNET_HashCode *
490                                                      res);
491
492
493 /**
494  * Handle to file hashing operation.
495  */
496 struct GNUNET_CRYPTO_FileHashContext;
497
498
499 /**
500  * @ingroup hash
501  * Compute the hash of an entire file.
502  *
503  * @param priority scheduling priority to use
504  * @param filename name of file to hash
505  * @param blocksize number of bytes to process in one task
506  * @param callback function to call upon completion
507  * @param callback_cls closure for @a callback
508  * @return NULL on (immediate) errror
509  */
510 struct GNUNET_CRYPTO_FileHashContext *
511 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
512                          const char *filename, size_t blocksize,
513                          GNUNET_CRYPTO_HashCompletedCallback callback,
514                          void *callback_cls);
515
516
517 /**
518  * Cancel a file hashing operation.
519  *
520  * @param fhc operation to cancel (callback must not yet have been invoked)
521  */
522 void
523 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
524
525
526 /**
527  * @ingroup hash
528  * Create a random hash code.
529  *
530  * @param mode desired quality level
531  * @param result hash code that is randomized
532  */
533 void
534 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
535                                   struct GNUNET_HashCode * result);
536
537
538 /**
539  * @ingroup hash
540  * compute result(delta) = b - a
541  *
542  * @param a some hash code
543  * @param b some hash code
544  * @param result set to @a b - @a a
545  */
546 void
547 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode * a,
548                                const struct GNUNET_HashCode * b,
549                                struct GNUNET_HashCode * result);
550
551
552 /**
553  * @ingroup hash
554  * compute result(b) = a + delta
555  *
556  * @param a some hash code
557  * @param delta some hash code
558  * @param result set to @a a + @a delta
559  */
560 void
561 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode * a,
562                         const struct GNUNET_HashCode * delta,
563                         struct GNUNET_HashCode * result);
564
565
566 /**
567  * @ingroup hash
568  * compute result = a ^ b
569  *
570  * @param a some hash code
571  * @param b some hash code
572  * @param result set to @a a ^ @a b
573  */
574 void
575 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode * a, const struct GNUNET_HashCode * b,
576                         struct GNUNET_HashCode * result);
577
578
579 /**
580  * @ingroup hash
581  * Convert a hashcode into a key.
582  *
583  * @param hc hash code that serves to generate the key
584  * @param skey set to a valid session key
585  * @param iv set to a valid initialization vector
586  */
587 void
588 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
589                                struct GNUNET_CRYPTO_AesSessionKey *skey,
590                                struct GNUNET_CRYPTO_AesInitializationVector
591                                *iv);
592
593
594 /**
595  * @ingroup hash
596  * Obtain a bit from a hashcode.
597  *
598  * @param code the `struct GNUNET_HashCode` to index bit-wise
599  * @param bit index into the hashcode, [0...159]
600  * @return Bit \a bit from hashcode \a code, -1 for invalid index
601  */
602 int
603 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code, 
604                             unsigned int bit);
605
606
607 /**
608  * @ingroup hash
609  * Determine how many low order bits match in two
610  * struct GNUNET_HashCodes.  i.e. - 010011 and 011111 share
611  * the first two lowest order bits, and therefore the
612  * return value is two (NOT XOR distance, nor how many
613  * bits match absolutely!).
614  *
615  * @param first the first hashcode
616  * @param second the hashcode to compare first to
617  * @return the number of bits that match
618  */
619 unsigned int
620 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode * first,
621                                   const struct GNUNET_HashCode * second);
622
623
624 /**
625  * @ingroup hash
626  * Compare function for HashCodes, producing a total ordering
627  * of all hashcodes.
628  *
629  * @param h1 some hash code
630  * @param h2 some hash code
631  * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
632  */
633 int
634 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode * h1, const struct GNUNET_HashCode * h2);
635
636
637 /**
638  * @ingroup hash
639  * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
640  * in the XOR metric (Kademlia).
641  *
642  * @param h1 some hash code
643  * @param h2 some hash code
644  * @param target some hash code
645  * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
646  */
647 int
648 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode * h1,
649                            const struct GNUNET_HashCode * h2,
650                            const struct GNUNET_HashCode * target);
651
652
653 /**
654  * @ingroup hash
655  * @brief Derive an authentication key
656  * @param key authentication key
657  * @param rkey root key
658  * @param salt salt
659  * @param salt_len size of the salt
660  * @param argp pair of void * & size_t for context chunks, terminated by NULL
661  */
662 void
663 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
664                                  const struct GNUNET_CRYPTO_AesSessionKey *rkey,
665                                  const void *salt, size_t salt_len,
666                                  va_list argp);
667
668
669 /**
670  * @ingroup hash
671  * @brief Derive an authentication key
672  * @param key authentication key
673  * @param rkey root key
674  * @param salt salt
675  * @param salt_len size of the salt
676  * @param ... pair of void * & size_t for context chunks, terminated by NULL
677  */
678 void
679 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
680                                const struct GNUNET_CRYPTO_AesSessionKey *rkey,
681                                const void *salt, size_t salt_len, ...);
682
683
684 /**
685  * @ingroup hash
686  * @brief Derive key
687  * @param result buffer for the derived key, allocated by caller
688  * @param out_len desired length of the derived key
689  * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
690  * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
691  * @param xts salt
692  * @param xts_len length of xts
693  * @param skm source key material
694  * @param skm_len length of skm
695  * @param ... pair of void * & size_t for context chunks, terminated by NULL
696  * @return GNUNET_YES on success
697  */
698 int
699 GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
700                     const void *xts, size_t xts_len, const void *skm,
701                     size_t skm_len, ...);
702
703
704 /**
705  * @ingroup hash
706  * @brief Derive key
707  * @param result buffer for the derived key, allocated by caller
708  * @param out_len desired length of the derived key
709  * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
710  * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
711  * @param xts salt
712  * @param xts_len length of xts
713  * @param skm source key material
714  * @param skm_len length of skm
715  * @param argp va_list of void * & size_t pairs for context chunks
716  * @return GNUNET_YES on success
717  */
718 int
719 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
720                       const void *xts, size_t xts_len, const void *skm,
721                       size_t skm_len, va_list argp);
722
723
724 /**
725  * @brief Derive key
726  * @param result buffer for the derived key, allocated by caller
727  * @param out_len desired length of the derived key
728  * @param xts salt
729  * @param xts_len length of xts
730  * @param skm source key material
731  * @param skm_len length of skm
732  * @param argp va_list of void * & size_t pairs for context chunks
733  * @return GNUNET_YES on success
734  */
735 int
736 GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
737                      size_t xts_len, const void *skm, size_t skm_len,
738                      va_list argp);
739
740
741 /**
742  * @ingroup hash
743  * @brief Derive key
744  * @param result buffer for the derived key, allocated by caller
745  * @param out_len desired length of the derived key
746  * @param xts salt
747  * @param xts_len length of xts
748  * @param skm source key material
749  * @param skm_len length of skm
750  * @param ... void * & size_t pairs for context chunks
751  * @return #GNUNET_YES on success
752  */
753 int
754 GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
755                    size_t xts_len, const void *skm, size_t skm_len, ...);
756
757
758 /**
759  * Function called upon completion of 'GNUNET_CRYPTO_ecc_key_create_async'.
760  *
761  * @param cls closure
762  * @param pk NULL on error, otherwise the private key (which must be free'd by the callee)
763  * @param emsg NULL on success, otherwise an error message
764  */
765 typedef void (*GNUNET_CRYPTO_EccKeyCallback)(void *cls,
766                                              struct GNUNET_CRYPTO_EccPrivateKey *pk,
767                                              const char *emsg);
768
769
770 /**
771  * @ingroup crypto
772  * Extract the public key for the given private key.
773  *
774  * @param priv the private key
775  * @param pub where to write the public key
776  */
777 void
778 GNUNET_CRYPTO_ecc_key_get_public_for_signature (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
779                                                 struct GNUNET_CRYPTO_EccPublicSignKey *pub);
780
781
782
783 /**
784  * @ingroup crypto
785  * Extract the public key for the given private key.
786  *
787  * @param priv the private key
788  * @param pub where to write the public key
789  */
790 void
791 GNUNET_CRYPTO_ecc_key_get_public_for_encryption (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
792                                                  struct GNUNET_CRYPTO_EccPublicEncryptKey *pub);
793
794
795 /**
796  * Convert a public key to a string.
797  *
798  * @param pub key to convert
799  * @return string representing @a pub
800  */
801 char *
802 GNUNET_CRYPTO_ecc_public_sign_key_to_string (const struct GNUNET_CRYPTO_EccPublicSignKey *pub);
803
804
805 /**
806  * Convert a string representing a public key to a public key.
807  *
808  * @param enc encoded public key
809  * @param enclen number of bytes in @a enc (without 0-terminator)
810  * @param pub where to store the public key
811  * @return #GNUNET_OK on success
812  */
813 int
814 GNUNET_CRYPTO_ecc_public_sign_key_from_string (const char *enc, 
815                                                size_t enclen,
816                                                struct GNUNET_CRYPTO_EccPublicSignKey *pub);
817
818
819
820 /**
821  * Convert a public key to a string.
822  *
823  * @param pub key to convert
824  * @return string representing @a pub
825  */
826 char *
827 GNUNET_CRYPTO_ecc_public_encrypt_key_to_string (const struct GNUNET_CRYPTO_EccPublicEncryptKey *pub);
828
829
830 /**
831  * Convert a string representing a public key to a public key.
832  *
833  * @param enc encoded public key
834  * @param enclen number of bytes in @a enc (without 0-terminator)
835  * @param pub where to store the public key
836  * @return #GNUNET_OK on success
837  */
838 int
839 GNUNET_CRYPTO_ecc_public_encrypt_key_from_string (const char *enc, 
840                                                   size_t enclen,
841                                                   struct GNUNET_CRYPTO_EccPublicEncryptKey *pub);
842
843
844 /**
845  * @ingroup crypto
846  * Create a new private key by reading it from a file.  If the
847  * files does not exist, create a new key and write it to the
848  * file.  Caller must free return value.  Note that this function
849  * can not guarantee that another process might not be trying
850  * the same operation on the same file at the same time.
851  * If the contents of the file
852  * are invalid the old file is deleted and a fresh key is
853  * created.
854  *
855  * @param filename name of file to use to store the key
856  * @return new private key, NULL on error (for example,
857  *   permission denied); free using #GNUNET_free
858  */
859 struct GNUNET_CRYPTO_EccPrivateKey *
860 GNUNET_CRYPTO_ecc_key_create_from_file (const char *filename);
861
862
863 /**
864  * @ingroup crypto
865  * Create a new private key by reading our peer's key from
866  * the file specified in the configuration.
867  *
868  * @param cfg the configuration to use
869  * @return new private key, NULL on error (for example,
870  *   permission denied); free using #GNUNET_free
871  */
872 struct GNUNET_CRYPTO_EccPrivateKey *
873 GNUNET_CRYPTO_ecc_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
874
875
876 /**
877  * @ingroup crypto
878  * Create a new private key. Caller must free return value.
879  *
880  * @return fresh private key; free using #GNUNET_free
881  */
882 struct GNUNET_CRYPTO_EccPrivateKey *
883 GNUNET_CRYPTO_ecc_key_create (void);
884
885
886 /**
887  * @ingroup crypto
888  * Clear memory that was used to store a private key. 
889  *
890  * @param pk location of the key
891  */
892 void
893 GNUNET_CRYPTO_ecc_key_clear (struct GNUNET_CRYPTO_EccPrivateKey *pk);
894
895
896 /**
897  * @ingroup crypto
898  * Get the shared private key we use for anonymous users.
899  *
900  * @return "anonymous" private key; do not free
901  */
902 const struct GNUNET_CRYPTO_EccPrivateKey *
903 GNUNET_CRYPTO_ecc_key_get_anonymous (void);
904
905
906 /**
907  * @ingroup crypto
908  * Setup a hostkey file for a peer given the name of the
909  * configuration file (!).  This function is used so that
910  * at a later point code can be certain that reading a
911  * hostkey is fast (for example in time-dependent testcases).
912  *
913  * @param cfg_name name of the configuration file to use
914  */
915 void
916 GNUNET_CRYPTO_ecc_setup_hostkey (const char *cfg_name);
917
918
919 /**
920  * @ingroup crypto
921  * Retrieve the identity of the host's peer.
922  *
923  * @param cfg configuration to use
924  * @param dst pointer to where to write the peer identity
925  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
926  *         could not be retrieved
927  */
928 int
929 GNUNET_CRYPTO_get_host_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
930                                  struct GNUNET_PeerIdentity *dst);
931
932
933 /**
934  * @ingroup crypto
935  * Derive key material from a public and a private ECC key.
936  *
937  * @param priv private key to use for the ECDH (x)
938  * @param pub public key to use for the ECDY (yG)
939  * @param key_material where to write the key material (xyG)
940  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
941  */
942 int
943 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
944                         const struct GNUNET_CRYPTO_EccPublicEncryptKey *pub,
945                         struct GNUNET_HashCode *key_material);
946
947
948 /**
949  * @ingroup crypto
950  * Sign a given block.
951  *
952  * @param priv private key to use for the signing
953  * @param purpose what to sign (size, purpose)
954  * @param sig where to write the signature
955  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
956  */
957 int
958 GNUNET_CRYPTO_ecc_sign (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
959                         const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
960                         struct GNUNET_CRYPTO_EccSignature *sig);
961
962
963 /**
964  * @ingroup crypto
965  * Verify signature.
966  *
967  * @param purpose what is the purpose that the signature should have?
968  * @param validate block to validate (size, purpose, data)
969  * @param sig signature that is being validated
970  * @param pub public key of the signer
971  * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
972  */
973 int
974 GNUNET_CRYPTO_ecc_verify (uint32_t purpose,
975                           const struct GNUNET_CRYPTO_EccSignaturePurpose
976                           *validate,
977                           const struct GNUNET_CRYPTO_EccSignature *sig,
978                           const struct GNUNET_CRYPTO_EccPublicSignKey *pub);
979
980
981 /**
982  * @ingroup crypto
983  * Derive a private key from a given private key and a label.
984  * Essentially calculates a private key 'h = H(l,P) * d mod n'
985  * where n is the size of the ECC group and P is the public
986  * key associated with the private key 'd'.
987  *
988  * @param priv original private key
989  * @param label label to use for key deriviation
990  * @param context additional context to use for HKDF of 'h';
991  *        typically the name of the subsystem/application
992  * @return derived private key
993  */
994 struct GNUNET_CRYPTO_EccPrivateKey *
995 GNUNET_CRYPTO_ecc_key_derive (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
996                               const char *label,
997                               const char *context);
998
999
1000 /**
1001  * @ingroup crypto
1002  * Derive a public key from a given public key and a label.
1003  * Essentially calculates a public key 'V = H(l,P) * P'.
1004  *
1005  * @param pub original public key
1006  * @param label label to use for key deriviation
1007  * @param context additional context to use for HKDF of 'h'.
1008  *        typically the name of the subsystem/application
1009  * @param result where to write the derived public key
1010  */
1011 void
1012 GNUNET_CRYPTO_ecc_public_key_derive (const struct GNUNET_CRYPTO_EccPublicSignKey *pub,
1013                                      const char *label,
1014                                      const char *context,
1015                                      struct GNUNET_CRYPTO_EccPublicSignKey *result);
1016
1017
1018 #if 0                           /* keep Emacsens' auto-indent happy */
1019 {
1020 #endif
1021 #ifdef __cplusplus
1022 }
1023 #endif
1024
1025
1026 /* ifndef GNUNET_CRYPTO_LIB_H */
1027 #endif
1028 /* end of gnunet_crypto_lib.h */