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