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