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