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