Merge remote-tracking branch 'origin/master' into identity_abe
[oweals/gnunet.git] / src / include / gnunet_crypto_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001-2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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  * @author Jeffrey Burdges <burdges@gnunet.org>
31  *
32  * @defgroup crypto  Crypto library: cryptographic operations
33  * Provides cryptographic primitives.
34  *
35  * @see [Documentation](https://gnunet.org/crypto-api)
36  *
37  * @defgroup hash  Crypto library: hash operations
38  * Provides hashing and operations on hashes.
39  *
40  * @see [Documentation](https://gnunet.org/crypto-api)
41  */
42
43 #ifndef GNUNET_CRYPTO_LIB_H
44 #define GNUNET_CRYPTO_LIB_H
45
46 #ifdef __cplusplus
47 extern "C"
48 {
49 #if 0                           /* keep Emacsens' auto-indent happy */
50 }
51 #endif
52 #endif
53
54 /**
55  * @brief A 512-bit hashcode.  These are the default length for GNUnet, using SHA-512.
56  */
57 struct GNUNET_HashCode
58 {
59   uint32_t bits[512 / 8 / sizeof (uint32_t)];   /* = 16 */
60 };
61
62
63
64 /**
65  * @brief A 256-bit hashcode.  Used under special conditions, like when space
66  * is critical and security is not impacted by it.
67  */
68 struct GNUNET_ShortHashCode
69 {
70   uint32_t bits[256 / 8 / sizeof (uint32_t)];   /* = 8 */
71 };
72
73
74 /**
75  * The identity of the host (wraps the signing key of the peer).
76  */
77 struct GNUNET_PeerIdentity;
78
79 #include "gnunet_common.h"
80 #include <gcrypt.h>
81
82
83 /**
84  * Maximum length of an ECC signature.
85  * Note: round up to multiple of 8 minus 2 for alignment.
86  */
87 #define GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH 126
88
89
90 /**
91  * Desired quality level for random numbers.
92  * @ingroup crypto
93  */
94 enum GNUNET_CRYPTO_Quality
95 {
96   /**
97    * No good quality of the operation is needed (i.e.,
98    * random numbers can be pseudo-random).
99    * @ingroup crypto
100    */
101   GNUNET_CRYPTO_QUALITY_WEAK,
102
103   /**
104    * High-quality operations are desired.
105    * @ingroup crypto
106    */
107   GNUNET_CRYPTO_QUALITY_STRONG,
108
109   /**
110    * Randomness for IVs etc. is required.
111    * @ingroup crypto
112    */
113   GNUNET_CRYPTO_QUALITY_NONCE
114 };
115
116
117 /**
118  * @brief length of the sessionkey in bytes (256 BIT sessionkey)
119  */
120 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
121
122 /**
123  * Length of a hash value
124  */
125 #define GNUNET_CRYPTO_HASH_LENGTH (512/8)
126
127 /**
128  * How many characters (without 0-terminator) are our ASCII-encoded
129  * public keys (ECDSA/EDDSA/ECDHE).
130  */
131 #define GNUNET_CRYPTO_PKEY_ASCII_LENGTH 52
132
133 /**
134  * @brief 0-terminated ASCII encoding of a struct GNUNET_HashCode.
135  */
136 struct GNUNET_CRYPTO_HashAsciiEncoded
137 {
138   unsigned char encoding[104];
139 };
140
141
142 GNUNET_NETWORK_STRUCT_BEGIN
143
144
145 /**
146  * @brief header of what an ECC signature signs
147  *        this must be followed by "size - 8" bytes of
148  *        the actual signed data
149  */
150 struct GNUNET_CRYPTO_EccSignaturePurpose
151 {
152   /**
153    * How many bytes does this signature sign?
154    * (including this purpose header); in network
155    * byte order (!).
156    */
157   uint32_t size GNUNET_PACKED;
158
159   /**
160    * What does this signature vouch for?  This
161    * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
162    * constant (from gnunet_signatures.h).  In
163    * network byte order!
164    */
165   uint32_t purpose GNUNET_PACKED;
166
167 };
168
169
170 /**
171  * @brief an ECC signature using EdDSA.
172  * See https://gnunet.org/ed25519
173  */
174 struct GNUNET_CRYPTO_EddsaSignature
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 /**
192  * @brief an ECC signature using ECDSA
193  */
194 struct GNUNET_CRYPTO_EcdsaSignature
195 {
196
197   /**
198    * R value.
199    */
200   unsigned char r[256 / 8];
201
202   /**
203    * S value.
204    */
205   unsigned char s[256 / 8];
206
207 };
208
209
210 /**
211  * Public ECC key (always for Curve25519) encoded in a format suitable
212  * for network transmission and EdDSA signatures.
213  */
214 struct GNUNET_CRYPTO_EddsaPublicKey
215 {
216   /**
217    * Q consists of an x- and a y-value, each mod p (256 bits), given
218    * here in affine coordinates and Ed25519 standard compact format.
219    */
220   unsigned char q_y[256 / 8];
221
222 };
223
224
225 /**
226  * Public ECC key (always for Curve25519) encoded in a format suitable
227  * for network transmission and ECDSA signatures.
228  */
229 struct GNUNET_CRYPTO_EcdsaPublicKey
230 {
231   /**
232    * Q consists of an x- and a y-value, each mod p (256 bits), given
233    * here in affine coordinates and Ed25519 standard compact format.
234    */
235   unsigned char q_y[256 / 8];
236
237 };
238
239
240 /**
241  * The identity of the host (wraps the signing key of the peer).
242  */
243 struct GNUNET_PeerIdentity
244 {
245   struct GNUNET_CRYPTO_EddsaPublicKey public_key;
246 };
247
248
249 /**
250  * Public ECC key (always for Curve25519) encoded in a format suitable
251  * for network transmission and encryption (ECDH),
252  * See http://cr.yp.to/ecdh.html
253  */
254 struct GNUNET_CRYPTO_EcdhePublicKey
255 {
256   /**
257    * Q consists of an x- and a y-value, each mod p (256 bits), given
258    * here in affine coordinates and Ed25519 standard compact format.
259    */
260   unsigned char q_y[256 / 8];
261 };
262
263
264 /**
265  * Private ECC key encoded for transmission.  To be used only for ECDH
266  * key exchange (ECDHE to be precise).
267  */
268 struct GNUNET_CRYPTO_EcdhePrivateKey
269 {
270   /**
271    * d is a value mod n, where n has at most 256 bits.
272    */
273   unsigned char d[256 / 8];
274
275 };
276
277 /**
278  * Private ECC key encoded for transmission.  To be used only for ECDSA
279  * signatures.
280  */
281 struct GNUNET_CRYPTO_EcdsaPrivateKey
282 {
283   /**
284    * d is a value mod n, where n has at most 256 bits.
285    */
286   unsigned char d[256 / 8];
287
288 };
289
290 /**
291  * Private ECC key encoded for transmission.  To be used only for EdDSA
292  * signatures.
293  */
294 struct GNUNET_CRYPTO_EddsaPrivateKey
295 {
296   /**
297    * d is a value mod n, where n has at most 256 bits.
298    */
299   unsigned char d[256 / 8];
300
301 };
302
303
304 /**
305  * @brief type for session keys
306  */
307 struct GNUNET_CRYPTO_SymmetricSessionKey
308 {
309   /**
310    * Actual key for AES.
311    */
312   unsigned char aes_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
313
314   /**
315    * Actual key for TwoFish.
316    */
317   unsigned char twofish_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
318
319 };
320
321 GNUNET_NETWORK_STRUCT_END
322
323 /**
324  * @brief IV for sym cipher
325  *
326  * NOTE: must be smaller (!) in size than the
327  * `struct GNUNET_HashCode`.
328  */
329 struct GNUNET_CRYPTO_SymmetricInitializationVector
330 {
331   unsigned char aes_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
332
333   unsigned char twofish_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
334 };
335
336
337 /**
338  * @brief type for (message) authentication keys
339  */
340 struct GNUNET_CRYPTO_AuthKey
341 {
342   unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
343 };
344
345
346 /**
347  * Size of paillier plain texts and public keys.
348  * Private keys and ciphertexts are twice this size.
349  */
350 #define GNUNET_CRYPTO_PAILLIER_BITS 2048
351
352
353 /**
354  * Paillier public key.
355  */
356 struct GNUNET_CRYPTO_PaillierPublicKey
357 {
358   /**
359    * N value.
360    */
361   unsigned char n[GNUNET_CRYPTO_PAILLIER_BITS / 8];
362 };
363
364
365 /**
366  * Paillier public key.
367  */
368 struct GNUNET_CRYPTO_PaillierPrivateKey
369 {
370   /**
371    * Lambda-component of the private key.
372    */
373   unsigned char lambda[GNUNET_CRYPTO_PAILLIER_BITS / 8];
374   /**
375    * Mu-component of the private key.
376    */
377   unsigned char mu[GNUNET_CRYPTO_PAILLIER_BITS / 8];
378 };
379
380
381 /**
382  * Paillier ciphertext.
383  */
384 struct GNUNET_CRYPTO_PaillierCiphertext
385 {
386   /**
387    * Guaranteed minimum number of homomorphic operations with this ciphertext,
388    * in network byte order (NBO).
389    */
390   int32_t remaining_ops GNUNET_PACKED;
391
392   /**
393    * The bits of the ciphertext.
394    */
395   unsigned char bits[GNUNET_CRYPTO_PAILLIER_BITS * 2 / 8];
396 };
397
398 /**
399  * @brief type for ABE master keys
400  */
401 struct GNUNET_CRYPTO_AbeMasterKey;
402
403
404 /* **************** Functions and Macros ************* */
405
406 /**
407  * @ingroup crypto
408  * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator
409  * can be seeded.
410  *
411  * @param seed the seed to use
412  */
413 void
414 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
415
416
417 /**
418  * @ingroup hash
419  * Calculate the checksum of a buffer in one step.
420  *
421  * @param buf buffer to calculate CRC over
422  * @param len number of bytes in @a buf
423  * @return crc8 value
424  */
425 uint8_t
426 GNUNET_CRYPTO_crc8_n (const void *buf,
427                       size_t len);
428
429
430 /**
431  * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
432  *
433  * @param sum current sum, initially 0
434  * @param buf buffer to calculate CRC over (must be 16-bit aligned)
435  * @param len number of bytes in @a buf, must be multiple of 2
436  * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish to get actual crc16)
437  */
438 uint32_t
439 GNUNET_CRYPTO_crc16_step (uint32_t sum,
440                           const void *buf,
441                           size_t len);
442
443
444 /**
445  * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
446  *
447  * @param sum cummulative sum
448  * @return crc16 value
449  */
450 uint16_t
451 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
452
453
454 /**
455  * @ingroup hash
456  * Calculate the checksum of a buffer in one step.
457  *
458  * @param buf buffer to calculate CRC over (must be 16-bit aligned)
459  * @param len number of bytes in @a buf, must be multiple of 2
460  * @return crc16 value
461  */
462 uint16_t
463 GNUNET_CRYPTO_crc16_n (const void *buf,
464                        size_t len);
465
466
467
468
469 /**
470  * @ingroup hash
471  * Compute the CRC32 checksum for the first len
472  * bytes of the buffer.
473  *
474  * @param buf the data over which we're taking the CRC
475  * @param len the length of the buffer @a buf in bytes
476  * @return the resulting CRC32 checksum
477  */
478 int32_t
479 GNUNET_CRYPTO_crc32_n (const void *buf,
480                        size_t len);
481
482
483 /**
484  * @ingroup crypto
485  * Fill block with a random values.
486  *
487  * @param mode desired quality of the random number
488  * @param buffer the buffer to fill
489  * @param length buffer length
490  */
491 void
492 GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode,
493                             void *buffer,
494                             size_t length);
495
496 /**
497  * @ingroup crypto
498  * Produce a random value.
499  *
500  * @param mode desired quality of the random number
501  * @param i the upper limit (exclusive) for the random number
502  * @return a random value in the interval [0,@a i) (exclusive).
503  */
504 uint32_t
505 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode,
506                           uint32_t i);
507
508
509 /**
510  * @ingroup crypto
511  * Random on unsigned 64-bit values.
512  *
513  * @param mode desired quality of the random number
514  * @param max value returned will be in range [0,@a max) (exclusive)
515  * @return random 64-bit number
516  */
517 uint64_t
518 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
519                           uint64_t max);
520
521
522 /**
523  * @ingroup crypto
524  * Get an array with a random permutation of the
525  * numbers 0...n-1.
526  * @param mode #GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used,
527  *             #GNUNET_CRYPTO_QUALITY_WEAK or #GNUNET_CRYPTO_QUALITY_NONCE otherwise
528  * @param n the size of the array
529  * @return the permutation array (allocated from heap)
530  */
531 unsigned int *
532 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode,
533                               unsigned int n);
534
535
536 /**
537  * @ingroup crypto
538  * Create a new random session key.
539  *
540  * @param key key to initialize
541  */
542 void
543 GNUNET_CRYPTO_symmetric_create_session_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key);
544
545
546 /**
547  * @ingroup crypto
548  * Encrypt a block using a symmetric sessionkey.
549  *
550  * @param block the block to encrypt
551  * @param size the size of the @a block
552  * @param sessionkey the key used to encrypt
553  * @param iv the initialization vector to use, use INITVALUE
554  *        for streams.
555  * @return the size of the encrypted block, -1 for errors
556  */
557 ssize_t
558 GNUNET_CRYPTO_symmetric_encrypt (const void *block,
559                                  size_t size,
560                                  const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
561                                  const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
562                                  void *result);
563
564
565 /**
566  * @ingroup crypto
567  * Decrypt a given block using a symmetric sessionkey.
568  *
569  * @param block the data to decrypt, encoded as returned by encrypt
570  * @param size how big is the block?
571  * @param sessionkey the key used to decrypt
572  * @param iv the initialization vector to use
573  * @param result address to store the result at
574  * @return -1 on failure, size of decrypted block on success
575  */
576 ssize_t
577 GNUNET_CRYPTO_symmetric_decrypt (const void *block,
578                                  size_t size,
579                                  const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
580                                  const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
581                                  void *result);
582
583
584 /**
585  * @ingroup crypto
586  * @brief Derive an IV
587  * @param iv initialization vector
588  * @param skey session key
589  * @param salt salt for the derivation
590  * @param salt_len size of the @a salt
591  * @param ... pairs of void * & size_t for context chunks, terminated by NULL
592  */
593 void
594 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
595                                    const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
596                                    const void *salt,
597                                    size_t salt_len, ...);
598
599
600 /**
601  * @brief Derive an IV
602  * @param iv initialization vector
603  * @param skey session key
604  * @param salt salt for the derivation
605  * @param salt_len size of the @a salt
606  * @param argp pairs of void * & size_t for context chunks, terminated by NULL
607  */
608 void
609 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
610                                      const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
611                                      const void *salt,
612                                      size_t salt_len,
613                                      va_list argp);
614
615
616 /**
617  * @ingroup hash
618  * Convert hash to ASCII encoding.
619  * @param block the hash code
620  * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
621  *  safely cast to char*, a '\\0' termination is set).
622  */
623 void
624 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block,
625                            struct GNUNET_CRYPTO_HashAsciiEncoded *result);
626
627
628 /**
629  * @ingroup hash
630  * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
631  *
632  * @param enc the encoding
633  * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
634  * @param result where to store the hash code
635  * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
636  */
637 int
638 GNUNET_CRYPTO_hash_from_string2 (const char *enc,
639                                  size_t enclen,
640                                  struct GNUNET_HashCode *result);
641
642
643 /**
644  * @ingroup hash
645  * Convert ASCII encoding back to `struct GNUNET_HashCode`
646  *
647  * @param enc the encoding
648  * @param result where to store the hash code
649  * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
650  */
651 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
652   GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
653
654
655 /**
656  * @ingroup hash
657  *
658  * Compute the distance between 2 hashcodes.  The
659  * computation must be fast, not involve @a a[0] or @a a[4] (they're used
660  * elsewhere), and be somewhat consistent. And of course, the result
661  * should be a positive number.
662  *
663  * @param a some hash code
664  * @param b some hash code
665  * @return number between 0 and UINT32_MAX
666  */
667 uint32_t
668 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a,
669                                  const struct GNUNET_HashCode *b);
670
671
672 /**
673  * @ingroup hash
674  * Compute hash of a given block.
675  *
676  * @param block the data to hash
677  * @param size size of the @a block
678  * @param ret pointer to where to write the hashcode
679  */
680 void
681 GNUNET_CRYPTO_hash (const void *block,
682                     size_t size,
683                     struct GNUNET_HashCode *ret);
684
685
686 /**
687  * Context for cummulative hashing.
688  */
689 struct GNUNET_HashContext;
690
691
692 /**
693  * Start incremental hashing operation.
694  *
695  * @return context for incremental hash computation
696  */
697 struct GNUNET_HashContext *
698 GNUNET_CRYPTO_hash_context_start (void);
699
700
701 /**
702  * Add data to be hashed.
703  *
704  * @param hc cummulative hash context
705  * @param buf data to add
706  * @param size number of bytes in @a buf
707  */
708 void
709 GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
710                                  const void *buf,
711                                  size_t size);
712
713
714 /**
715  * Finish the hash computation.
716  *
717  * @param hc hash context to use, is freed in the process
718  * @param r_hash where to write the latest / final hash code
719  */
720 void
721 GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
722                                    struct GNUNET_HashCode *r_hash);
723
724
725 /**
726  * Abort hashing, do not bother calculating final result.
727  *
728  * @param hc hash context to destroy
729  */
730 void
731 GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc);
732
733
734 /**
735  * @ingroup hash
736  * Calculate HMAC of a message (RFC 2104)
737  *
738  * @param key secret key
739  * @param plaintext input plaintext
740  * @param plaintext_len length of @a plaintext
741  * @param hmac where to store the hmac
742  */
743 void
744 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
745                     const void *plaintext,
746                     size_t plaintext_len,
747                     struct GNUNET_HashCode *hmac);
748
749
750 /**
751  * Function called once the hash computation over the
752  * specified file has completed.
753  *
754  * @param cls closure
755  * @param res resulting hash, NULL on error
756  */
757 typedef void
758 (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
759                                         const struct GNUNET_HashCode *res);
760
761
762 /**
763  * Handle to file hashing operation.
764  */
765 struct GNUNET_CRYPTO_FileHashContext;
766
767
768 /**
769  * @ingroup hash
770  * Compute the hash of an entire file.
771  *
772  * @param priority scheduling priority to use
773  * @param filename name of file to hash
774  * @param blocksize number of bytes to process in one task
775  * @param callback function to call upon completion
776  * @param callback_cls closure for @a callback
777  * @return NULL on (immediate) errror
778  */
779 struct GNUNET_CRYPTO_FileHashContext *
780 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
781                          const char *filename,
782                          size_t blocksize,
783                          GNUNET_CRYPTO_HashCompletedCallback callback,
784                          void *callback_cls);
785
786
787 /**
788  * Cancel a file hashing operation.
789  *
790  * @param fhc operation to cancel (callback must not yet have been invoked)
791  */
792 void
793 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
794
795
796 /**
797  * @ingroup hash
798  * Create a random hash code.
799  *
800  * @param mode desired quality level
801  * @param result hash code that is randomized
802  */
803 void
804 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
805                                   struct GNUNET_HashCode *result);
806
807
808 /**
809  * @ingroup hash
810  * compute @a result = @a b - @a a
811  *
812  * @param a some hash code
813  * @param b some hash code
814  * @param result set to @a b - @a a
815  */
816 void
817 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
818                                const struct GNUNET_HashCode *b,
819                                struct GNUNET_HashCode *result);
820
821
822 /**
823  * @ingroup hash
824  * compute @a result = @a a + @a delta
825  *
826  * @param a some hash code
827  * @param delta some hash code
828  * @param result set to @a a + @a delta
829  */
830 void
831 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a,
832                         const struct GNUNET_HashCode *delta,
833                         struct GNUNET_HashCode *result);
834
835
836 /**
837  * @ingroup hash
838  * compute result = a ^ b
839  *
840  * @param a some hash code
841  * @param b some hash code
842  * @param result set to @a a ^ @a b
843  */
844 void
845 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
846                         const struct GNUNET_HashCode *b,
847                         struct GNUNET_HashCode *result);
848
849
850 /**
851  * @ingroup hash
852  * Convert a hashcode into a key.
853  *
854  * @param hc hash code that serves to generate the key
855  * @param skey set to a valid session key
856  * @param iv set to a valid initialization vector
857  */
858 void
859 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
860                                struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
861                                struct GNUNET_CRYPTO_SymmetricInitializationVector *iv);
862
863
864 /**
865  * @ingroup hash
866  * Obtain a bit from a hashcode.
867  *
868  * @param code the `struct GNUNET_HashCode` to index bit-wise
869  * @param bit index into the hashcode, [0...159]
870  * @return Bit \a bit from hashcode \a code, -1 for invalid index
871  */
872 int
873 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code,
874                             unsigned int bit);
875
876
877 /**
878  * @ingroup hash
879  * Determine how many low order bits match in two
880  * `struct GNUNET_HashCodes`.  i.e. - 010011 and 011111 share
881  * the first two lowest order bits, and therefore the
882  * return value is two (NOT XOR distance, nor how many
883  * bits match absolutely!).
884  *
885  * @param first the first hashcode
886  * @param second the hashcode to compare first to
887  * @return the number of bits that match
888  */
889 unsigned int
890 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
891                                   const struct GNUNET_HashCode *second);
892
893
894 /**
895  * @ingroup hash
896  * Compare function for HashCodes, producing a total ordering
897  * of all hashcodes.
898  *
899  * @param h1 some hash code
900  * @param h2 some hash code
901  * @return 1 if @a h1 > @a h2, -1 if @a h1 < @a h2 and 0 if @a h1 == @a h2.
902  */
903 int
904 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
905                         const struct GNUNET_HashCode *h2);
906
907
908 /**
909  * @ingroup hash
910  * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
911  * in the XOR metric (Kademlia).
912  *
913  * @param h1 some hash code
914  * @param h2 some hash code
915  * @param target some hash code
916  * @return -1 if @a h1 is closer, 1 if @a h2 is closer and 0 if @a h1== @a h2.
917  */
918 int
919 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
920                            const struct GNUNET_HashCode *h2,
921                            const struct GNUNET_HashCode *target);
922
923
924 /**
925  * @ingroup hash
926  * @brief Derive an authentication key
927  * @param key authentication key
928  * @param rkey root key
929  * @param salt salt
930  * @param salt_len size of the salt
931  * @param argp pair of void * & size_t for context chunks, terminated by NULL
932  */
933 void
934 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
935                                  const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
936                                  const void *salt, size_t salt_len,
937                                  va_list argp);
938
939
940 /**
941  * @ingroup hash
942  * @brief Derive an authentication key
943  * @param key authentication key
944  * @param rkey root key
945  * @param salt salt
946  * @param salt_len size of the salt
947  * @param ... pair of void * & size_t for context chunks, terminated by NULL
948  */
949 void
950 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
951                                const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
952                                const void *salt, size_t salt_len,
953                                ...);
954
955
956 /**
957  * @ingroup hash
958  * @brief Derive key
959  * @param result buffer for the derived key, allocated by caller
960  * @param out_len desired length of the derived key
961  * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
962  * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
963  * @param xts salt
964  * @param xts_len length of @a xts
965  * @param skm source key material
966  * @param skm_len length of @a skm
967  * @param ... pair of void * & size_t for context chunks, terminated by NULL
968  * @return #GNUNET_YES on success
969  */
970 int
971 GNUNET_CRYPTO_hkdf (void *result,
972                     size_t out_len,
973                     int xtr_algo,
974                     int prf_algo,
975                     const void *xts,
976                     size_t xts_len,
977                     const void *skm,
978                     size_t skm_len,
979                     ...);
980
981
982 /**
983  * @ingroup hash
984  * @brief Derive key
985  * @param result buffer for the derived key, allocated by caller
986  * @param out_len desired length of the derived key
987  * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
988  * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
989  * @param xts salt
990  * @param xts_len length of @a xts
991  * @param skm source key material
992  * @param skm_len length of @a skm
993  * @param argp va_list of void * & size_t pairs for context chunks
994  * @return #GNUNET_YES on success
995  */
996 int
997 GNUNET_CRYPTO_hkdf_v (void *result,
998                       size_t out_len,
999                       int xtr_algo,
1000                       int prf_algo,
1001                       const void *xts,
1002                       size_t xts_len,
1003                       const void *skm,
1004                       size_t skm_len,
1005                       va_list argp);
1006
1007
1008 /**
1009  * @brief Derive key
1010  * @param result buffer for the derived key, allocated by caller
1011  * @param out_len desired length of the derived key
1012  * @param xts salt
1013  * @param xts_len length of @a xts
1014  * @param skm source key material
1015  * @param skm_len length of @a skm
1016  * @param argp va_list of void * & size_t pairs for context chunks
1017  * @return #GNUNET_YES on success
1018  */
1019 int
1020 GNUNET_CRYPTO_kdf_v (void *result,
1021                      size_t out_len,
1022                      const void *xts,
1023                      size_t xts_len,
1024                      const void *skm,
1025                      size_t skm_len,
1026                      va_list argp);
1027
1028
1029 /**
1030  * Deterministically generate a pseudo-random number uniformly from the
1031  * integers modulo a libgcrypt mpi.
1032  *
1033  * @param[out] r MPI value set to the FDH
1034  * @param n MPI to work modulo
1035  * @param xts salt
1036  * @param xts_len length of @a xts
1037  * @param skm source key material
1038  * @param skm_len length of @a skm
1039  * @param ctx context string
1040  */
1041 void
1042 GNUNET_CRYPTO_kdf_mod_mpi (gcry_mpi_t *r,
1043                            gcry_mpi_t n,
1044                            const void *xts,  size_t xts_len,
1045                            const void *skm,  size_t skm_len,
1046                            const char *ctx);
1047
1048
1049 /**
1050  * @ingroup hash
1051  * @brief Derive key
1052  * @param result buffer for the derived key, allocated by caller
1053  * @param out_len desired length of the derived key
1054  * @param xts salt
1055  * @param xts_len length of @a xts
1056  * @param skm source key material
1057  * @param skm_len length of @a skm
1058  * @param ... void * & size_t pairs for context chunks
1059  * @return #GNUNET_YES on success
1060  */
1061 int
1062 GNUNET_CRYPTO_kdf (void *result,
1063                    size_t out_len,
1064                    const void *xts,
1065                    size_t xts_len,
1066                    const void *skm,
1067                    size_t skm_len,
1068                    ...);
1069
1070
1071 /**
1072  * @ingroup crypto
1073  * Extract the public key for the given private key.
1074  *
1075  * @param priv the private key
1076  * @param pub where to write the public key
1077  */
1078 void
1079 GNUNET_CRYPTO_ecdsa_key_get_public (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1080                                     struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1081
1082 /**
1083  * @ingroup crypto
1084  * Extract the public key for the given private key.
1085  *
1086  * @param priv the private key
1087  * @param pub where to write the public key
1088  */
1089 void
1090 GNUNET_CRYPTO_eddsa_key_get_public (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1091                                     struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1092
1093
1094
1095 /**
1096  * @ingroup crypto
1097  * Extract the public key for the given private key.
1098  *
1099  * @param priv the private key
1100  * @param pub where to write the public key
1101  */
1102 void
1103 GNUNET_CRYPTO_ecdhe_key_get_public (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1104                                     struct GNUNET_CRYPTO_EcdhePublicKey *pub);
1105
1106
1107 /**
1108  * Convert a public key to a string.
1109  *
1110  * @param pub key to convert
1111  * @return string representing @a pub
1112  */
1113 char *
1114 GNUNET_CRYPTO_ecdsa_public_key_to_string (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1115
1116
1117 /**
1118  * Convert a private key to a string.
1119  *
1120  * @param priv key to convert
1121  * @return string representing @a pub
1122  */
1123 char *
1124 GNUNET_CRYPTO_eddsa_private_key_to_string (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv);
1125
1126
1127 /**
1128  * Convert a public key to a string.
1129  *
1130  * @param pub key to convert
1131  * @return string representing @a pub
1132  */
1133 char *
1134 GNUNET_CRYPTO_eddsa_public_key_to_string (const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1135
1136
1137 /**
1138  * Convert a string representing a public key to a public key.
1139  *
1140  * @param enc encoded public key
1141  * @param enclen number of bytes in @a enc (without 0-terminator)
1142  * @param pub where to store the public key
1143  * @return #GNUNET_OK on success
1144  */
1145 int
1146 GNUNET_CRYPTO_ecdsa_public_key_from_string (const char *enc,
1147                                             size_t enclen,
1148                                             struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1149
1150
1151 /**
1152  * Convert a string representing a private key to a private key.
1153  *
1154  * @param enc encoded public key
1155  * @param enclen number of bytes in @a enc (without 0-terminator)
1156  * @param priv where to store the private key
1157  * @return #GNUNET_OK on success
1158  */
1159 int
1160 GNUNET_CRYPTO_eddsa_private_key_from_string (const char *enc,
1161                                              size_t enclen,
1162                                              struct GNUNET_CRYPTO_EddsaPrivateKey *pub);
1163
1164
1165 /**
1166  * Convert a string representing a public key to a public key.
1167  *
1168  * @param enc encoded public key
1169  * @param enclen number of bytes in @a enc (without 0-terminator)
1170  * @param pub where to store the public key
1171  * @return #GNUNET_OK on success
1172  */
1173 int
1174 GNUNET_CRYPTO_eddsa_public_key_from_string (const char *enc,
1175                                             size_t enclen,
1176                                             struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1177
1178
1179 /**
1180  * @ingroup crypto
1181  * Create a new private key by reading it from a file.  If the
1182  * files does not exist, create a new key and write it to the
1183  * file.  Caller must free return value.  Note that this function
1184  * can not guarantee that another process might not be trying
1185  * the same operation on the same file at the same time.
1186  * If the contents of the file
1187  * are invalid the old file is deleted and a fresh key is
1188  * created.
1189  *
1190  * @param filename name of file to use to store the key
1191  * @return new private key, NULL on error (for example,
1192  *   permission denied); free using #GNUNET_free
1193  */
1194 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1195 GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename);
1196
1197
1198 /**
1199  * @ingroup crypto
1200  * Create a new private key by reading it from a file.  If the
1201  * files does not exist, create a new key and write it to the
1202  * file.  Caller must free return value.  Note that this function
1203  * can not guarantee that another process might not be trying
1204  * the same operation on the same file at the same time.
1205  * If the contents of the file
1206  * are invalid the old file is deleted and a fresh key is
1207  * created.
1208  *
1209  * @param filename name of file to use to store the key
1210  * @return new private key, NULL on error (for example,
1211  *   permission denied); free using #GNUNET_free
1212  */
1213 struct GNUNET_CRYPTO_EddsaPrivateKey *
1214 GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename);
1215
1216
1217 /**
1218  * Forward declaration to simplify #include-structure.
1219  */
1220 struct GNUNET_CONFIGURATION_Handle;
1221
1222
1223 /**
1224  * @ingroup crypto
1225  * Create a new private key by reading our peer's key from
1226  * the file specified in the configuration.
1227  *
1228  * @param cfg the configuration to use
1229  * @return new private key, NULL on error (for example,
1230  *   permission denied); free using #GNUNET_free
1231  */
1232 struct GNUNET_CRYPTO_EddsaPrivateKey *
1233 GNUNET_CRYPTO_eddsa_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
1234
1235
1236 /**
1237  * @ingroup crypto
1238  * Create a new private key. Caller must free return value.
1239  *
1240  * @return fresh private key; free using #GNUNET_free
1241  */
1242 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1243 GNUNET_CRYPTO_ecdsa_key_create (void);
1244
1245
1246 /**
1247  * @ingroup crypto
1248  * Create a new private key. Caller must free return value.
1249  *
1250  * @return fresh private key; free using #GNUNET_free
1251  */
1252 struct GNUNET_CRYPTO_EddsaPrivateKey *
1253 GNUNET_CRYPTO_eddsa_key_create (void);
1254
1255
1256 /**
1257  * @ingroup crypto
1258  * Create a new private key.  Clear with #GNUNET_CRYPTO_ecdhe_key_clear().
1259  *
1260  * @param[out] pk set to fresh private key;
1261  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1262  */
1263 int
1264 GNUNET_CRYPTO_ecdhe_key_create2 (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1265
1266
1267 /**
1268  * @ingroup crypto
1269  * Create a new private key. Caller must free return value.
1270  *
1271  * @return fresh private key; free using #GNUNET_free
1272  */
1273 struct GNUNET_CRYPTO_EcdhePrivateKey *
1274 GNUNET_CRYPTO_ecdhe_key_create (void);
1275
1276
1277 /**
1278  * @ingroup crypto
1279  * Clear memory that was used to store a private key.
1280  *
1281  * @param pk location of the key
1282  */
1283 void
1284 GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk);
1285
1286
1287 /**
1288  * @ingroup crypto
1289  * Clear memory that was used to store a private key.
1290  *
1291  * @param pk location of the key
1292  */
1293 void
1294 GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk);
1295
1296
1297 /**
1298  * @ingroup crypto
1299  * Clear memory that was used to store a private key.
1300  *
1301  * @param pk location of the key
1302  */
1303 void
1304 GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1305
1306
1307 /**
1308  * @ingroup crypto
1309  * Get the shared private key we use for anonymous users.
1310  *
1311  * @return "anonymous" private key; do not free
1312  */
1313 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
1314 GNUNET_CRYPTO_ecdsa_key_get_anonymous (void);
1315
1316
1317 /**
1318  * @ingroup crypto
1319  * Setup a hostkey file for a peer given the name of the
1320  * configuration file (!).  This function is used so that
1321  * at a later point code can be certain that reading a
1322  * hostkey is fast (for example in time-dependent testcases).
1323 *
1324  * @param cfg_name name of the configuration file to use
1325  */
1326 void
1327 GNUNET_CRYPTO_eddsa_setup_hostkey (const char *cfg_name);
1328
1329
1330 /**
1331  * @ingroup crypto
1332  * Retrieve the identity of the host's peer.
1333  *
1334  * @param cfg configuration to use
1335  * @param dst pointer to where to write the peer identity
1336  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
1337  *         could not be retrieved
1338  */
1339 int
1340 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
1341                                  struct GNUNET_PeerIdentity *dst);
1342
1343
1344 /**
1345  * Compare two Peer Identities.
1346  *
1347  * @param first first peer identity
1348  * @param second second peer identity
1349  * @return bigger than 0 if first > second,
1350  *         0 if they are the same
1351  *         smaller than 0 if second > first
1352  */
1353 int
1354 GNUNET_CRYPTO_cmp_peer_identity (const struct GNUNET_PeerIdentity *first,
1355                                  const struct GNUNET_PeerIdentity *second);
1356
1357
1358 /**
1359  * Internal structure used to cache pre-calculated values for DLOG calculation.
1360  */
1361 struct GNUNET_CRYPTO_EccDlogContext;
1362
1363
1364 /**
1365  * Point on a curve (always for Curve25519) encoded in a format suitable
1366  * for network transmission (ECDH), see http://cr.yp.to/ecdh.html.
1367  */
1368 struct GNUNET_CRYPTO_EccPoint
1369 {
1370   /**
1371    * Q consists of an x- and a y-value, each mod p (256 bits), given
1372    * here in affine coordinates and Ed25519 standard compact format.
1373    */
1374   unsigned char q_y[256 / 8];
1375 };
1376
1377
1378 /**
1379  * Do pre-calculation for ECC discrete logarithm for small factors.
1380  *
1381  * @param max maximum value the factor can be
1382  * @param mem memory to use (should be smaller than @a max), must not be zero.
1383  * @return NULL on error
1384  */
1385 struct GNUNET_CRYPTO_EccDlogContext *
1386 GNUNET_CRYPTO_ecc_dlog_prepare (unsigned int max,
1387                                 unsigned int mem);
1388
1389
1390 /**
1391  * Calculate ECC discrete logarithm for small factors.
1392  * Opposite of #GNUNET_CRYPTO_ecc_dexp().
1393  *
1394  * @param dlc precalculated values, determine range of factors
1395  * @param input point on the curve to factor
1396  * @return INT_MAX if dlog failed, otherwise the factor
1397  */
1398 int
1399 GNUNET_CRYPTO_ecc_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc,
1400                         gcry_mpi_point_t input);
1401
1402
1403 /**
1404  * Multiply the generator g of the elliptic curve by @a val
1405  * to obtain the point on the curve representing @a val.
1406  * Afterwards, point addition will correspond to integer
1407  * addition.  #GNUNET_CRYPTO_ecc_dlog() can be used to
1408  * convert a point back to an integer (as long as the
1409  * integer is smaller than the MAX of the @a edc context).
1410  *
1411  * @param edc calculation context for ECC operations
1412  * @param val value to encode into a point
1413  * @return representation of the value as an ECC point,
1414  *         must be freed using #GNUNET_CRYPTO_ecc_free()
1415  */
1416 gcry_mpi_point_t
1417 GNUNET_CRYPTO_ecc_dexp (struct GNUNET_CRYPTO_EccDlogContext *edc,
1418                         int val);
1419
1420
1421 /**
1422  * Multiply the generator g of the elliptic curve by @a val
1423  * to obtain the point on the curve representing @a val.
1424  *
1425  * @param edc calculation context for ECC operations
1426  * @param val (positive) value to encode into a point
1427  * @return representation of the value as an ECC point,
1428  *         must be freed using #GNUNET_CRYPTO_ecc_free()
1429  */
1430 gcry_mpi_point_t
1431 GNUNET_CRYPTO_ecc_dexp_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1432                             gcry_mpi_t val);
1433
1434
1435 /**
1436  * Multiply the point @a p on the elliptic curve by @a val.
1437  *
1438  * @param edc calculation context for ECC operations
1439  * @param p point to multiply
1440  * @param val (positive) value to encode into a point
1441  * @return representation of the value as an ECC point,
1442  *         must be freed using #GNUNET_CRYPTO_ecc_free()
1443  */
1444 gcry_mpi_point_t
1445 GNUNET_CRYPTO_ecc_pmul_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1446                             gcry_mpi_point_t p,
1447                             gcry_mpi_t val);
1448
1449
1450 /**
1451  * Convert point value to binary representation.
1452  *
1453  * @param edc calculation context for ECC operations
1454  * @param point computational point representation
1455  * @param[out] bin binary point representation
1456  */
1457 void
1458 GNUNET_CRYPTO_ecc_point_to_bin (struct GNUNET_CRYPTO_EccDlogContext *edc,
1459                                 gcry_mpi_point_t point,
1460                                 struct GNUNET_CRYPTO_EccPoint *bin);
1461
1462
1463 /**
1464  * Convert binary representation of a point to computational representation.
1465  *
1466  * @param edc calculation context for ECC operations
1467  * @param bin binary point representation
1468  * @return computational representation
1469  */
1470 gcry_mpi_point_t
1471 GNUNET_CRYPTO_ecc_bin_to_point (struct GNUNET_CRYPTO_EccDlogContext *edc,
1472                                 const struct GNUNET_CRYPTO_EccPoint *bin);
1473
1474
1475 /**
1476  * Add two points on the elliptic curve.
1477  *
1478  * @param edc calculation context for ECC operations
1479  * @param a some value
1480  * @param b some value
1481  * @return @a a + @a b, must be freed using #GNUNET_CRYPTO_ecc_free()
1482  */
1483 gcry_mpi_point_t
1484 GNUNET_CRYPTO_ecc_add (struct GNUNET_CRYPTO_EccDlogContext *edc,
1485                        gcry_mpi_point_t a,
1486                        gcry_mpi_point_t b);
1487
1488
1489 /**
1490  * Obtain a random point on the curve and its
1491  * additive inverse. Both returned values
1492  * must be freed using #GNUNET_CRYPTO_ecc_free().
1493  *
1494  * @param edc calculation context for ECC operations
1495  * @param[out] r set to a random point on the curve
1496  * @param[out] r_inv set to the additive inverse of @a r
1497  */
1498 void
1499 GNUNET_CRYPTO_ecc_rnd (struct GNUNET_CRYPTO_EccDlogContext *edc,
1500                        gcry_mpi_point_t *r,
1501                        gcry_mpi_point_t *r_inv);
1502
1503
1504 /**
1505  * Obtain a random scalar for point multiplication on the curve and
1506  * its multiplicative inverse.
1507  *
1508  * @param edc calculation context for ECC operations
1509  * @param[out] r set to a random scalar on the curve
1510  * @param[out] r_inv set to the multiplicative inverse of @a r
1511  */
1512 void
1513 GNUNET_CRYPTO_ecc_rnd_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1514                            gcry_mpi_t *r,
1515                            gcry_mpi_t *r_inv);
1516
1517
1518 /**
1519  * Generate a random value mod n.
1520  *
1521  * @param edc ECC context
1522  * @return random value mod n.
1523  */
1524 gcry_mpi_t
1525 GNUNET_CRYPTO_ecc_random_mod_n (struct GNUNET_CRYPTO_EccDlogContext *edc);
1526
1527
1528 /**
1529  * Free a point value returned by the API.
1530  *
1531  * @param p point to free
1532  */
1533 void
1534 GNUNET_CRYPTO_ecc_free (gcry_mpi_point_t p);
1535
1536
1537 /**
1538  * Release precalculated values.
1539  *
1540  * @param dlc dlog context
1541  */
1542 void
1543 GNUNET_CRYPTO_ecc_dlog_release (struct GNUNET_CRYPTO_EccDlogContext *dlc);
1544
1545
1546 /**
1547  * @ingroup crypto
1548  * Derive key material from a public and a private ECC key.
1549  *
1550  * @param priv private key to use for the ECDH (x)
1551  * @param pub public key to use for the ECDH (yG)
1552  * @param key_material where to write the key material (xyG)
1553  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1554  */
1555 int
1556 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1557                         const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1558                         struct GNUNET_HashCode *key_material);
1559
1560
1561 /**
1562  * @ingroup crypto
1563  * Derive key material from a ECDH public key and a private EdDSA key.
1564  * Dual to #GNUNET_CRRYPTO_ecdh_eddsa.
1565  *
1566  * @param priv private key from EdDSA to use for the ECDH (x)
1567  * @param pub public key to use for the ECDH (yG)
1568  * @param key_material where to write the key material H(h(x)yG)
1569  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1570  */
1571 int
1572 GNUNET_CRYPTO_eddsa_ecdh (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1573                           const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1574                           struct GNUNET_HashCode *key_material);
1575
1576 /**
1577  * @ingroup crypto
1578  * Derive key material from a ECDH public key and a private ECDSA key.
1579  * Dual to #GNUNET_CRRYPTO_ecdh_ecdsa.
1580  *
1581  * @param priv private key from ECDSA to use for the ECDH (x)
1582  * @param pub public key to use for the ECDH (yG)
1583  * @param key_material where to write the key material H(h(x)yG)
1584  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1585  */
1586 int
1587 GNUNET_CRYPTO_ecdsa_ecdh (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1588                           const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1589                           struct GNUNET_HashCode *key_material);
1590
1591
1592 /**
1593  * @ingroup crypto
1594  * Derive key material from a EdDSA public key and a private ECDH key.
1595  * Dual to #GNUNET_CRRYPTO_eddsa_ecdh.
1596  *
1597  * @param priv private key to use for the ECDH (y)
1598  * @param pub public key from EdDSA to use for the ECDH (X=h(x)G)
1599  * @param key_material where to write the key material H(yX)=H(h(x)yG)
1600  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1601  */
1602 int
1603 GNUNET_CRYPTO_ecdh_eddsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1604                           const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
1605                           struct GNUNET_HashCode *key_material);
1606
1607 /**
1608  * @ingroup crypto
1609  * Derive key material from a EcDSA public key and a private ECDH key.
1610  * Dual to #GNUNET_CRRYPTO_ecdsa_ecdh.
1611  *
1612  * @param priv private key to use for the ECDH (y)
1613  * @param pub public key from ECDSA to use for the ECDH (X=h(x)G)
1614  * @param key_material where to write the key material H(yX)=H(h(x)yG)
1615  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1616  */
1617 int
1618 GNUNET_CRYPTO_ecdh_ecdsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1619                           const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1620                           struct GNUNET_HashCode *key_material);
1621
1622
1623 /**
1624  * @ingroup crypto
1625  * EdDSA sign a given block.
1626  *
1627  * @param priv private key to use for the signing
1628  * @param purpose what to sign (size, purpose)
1629  * @param sig where to write the signature
1630  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1631  */
1632 int
1633 GNUNET_CRYPTO_eddsa_sign (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1634                           const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1635                           struct GNUNET_CRYPTO_EddsaSignature *sig);
1636
1637
1638 /**
1639  * @ingroup crypto
1640  * ECDSA Sign a given block.
1641  *
1642  * @param priv private key to use for the signing
1643  * @param purpose what to sign (size, purpose)
1644  * @param sig where to write the signature
1645  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1646  */
1647 int
1648 GNUNET_CRYPTO_ecdsa_sign (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1649                           const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1650                           struct GNUNET_CRYPTO_EcdsaSignature *sig);
1651
1652 /**
1653  * @ingroup crypto
1654  * Verify EdDSA signature.
1655  *
1656  * @param purpose what is the purpose that the signature should have?
1657  * @param validate block to validate (size, purpose, data)
1658  * @param sig signature that is being validated
1659  * @param pub public key of the signer
1660  * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1661  */
1662 int
1663 GNUNET_CRYPTO_eddsa_verify (uint32_t purpose,
1664                             const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1665                             const struct GNUNET_CRYPTO_EddsaSignature *sig,
1666                             const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1667
1668
1669
1670 /**
1671  * @ingroup crypto
1672  * Verify ECDSA signature.
1673  *
1674  * @param purpose what is the purpose that the signature should have?
1675  * @param validate block to validate (size, purpose, data)
1676  * @param sig signature that is being validated
1677  * @param pub public key of the signer
1678  * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1679  */
1680 int
1681 GNUNET_CRYPTO_ecdsa_verify (uint32_t purpose,
1682                             const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1683                             const struct GNUNET_CRYPTO_EcdsaSignature *sig,
1684                             const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1685
1686
1687 /**
1688  * @ingroup crypto
1689  * Derive a private key from a given private key and a label.
1690  * Essentially calculates a private key 'h = H(l,P) * d mod n'
1691  * where n is the size of the ECC group and P is the public
1692  * key associated with the private key 'd'.
1693  *
1694  * @param priv original private key
1695  * @param label label to use for key deriviation
1696  * @param context additional context to use for HKDF of 'h';
1697  *        typically the name of the subsystem/application
1698  * @return derived private key
1699  */
1700 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1701 GNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1702                                         const char *label,
1703                                         const char *context);
1704
1705
1706 /**
1707  * @ingroup crypto
1708  * Derive a public key from a given public key and a label.
1709  * Essentially calculates a public key 'V = H(l,P) * P'.
1710  *
1711  * @param pub original public key
1712  * @param label label to use for key deriviation
1713  * @param context additional context to use for HKDF of 'h'.
1714  *        typically the name of the subsystem/application
1715  * @param result where to write the derived public key
1716  */
1717 void
1718 GNUNET_CRYPTO_ecdsa_public_key_derive (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1719                                        const char *label,
1720                                        const char *context,
1721                                        struct GNUNET_CRYPTO_EcdsaPublicKey *result);
1722
1723
1724 /**
1725  * Output the given MPI value to the given buffer in network
1726  * byte order.  The MPI @a val may not be negative.
1727  *
1728  * @param buf where to output to
1729  * @param size number of bytes in @a buf
1730  * @param val value to write to @a buf
1731  */
1732 void
1733 GNUNET_CRYPTO_mpi_print_unsigned (void *buf,
1734                                   size_t size,
1735                                   gcry_mpi_t val);
1736
1737
1738 /**
1739  * Convert data buffer into MPI value.
1740  * The buffer is interpreted as network
1741  * byte order, unsigned integer.
1742  *
1743  * @param result where to store MPI value (allocated)
1744  * @param data raw data (GCRYMPI_FMT_USG)
1745  * @param size number of bytes in @a data
1746  */
1747 void
1748 GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result,
1749                                  const void *data,
1750                                  size_t size);
1751
1752
1753 /**
1754  * Create a freshly generated paillier public key.
1755  *
1756  * @param[out] public_key Where to store the public key?
1757  * @param[out] private_key Where to store the private key?
1758  */
1759 void
1760 GNUNET_CRYPTO_paillier_create (struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1761                                struct GNUNET_CRYPTO_PaillierPrivateKey *private_key);
1762
1763
1764 /**
1765  * Encrypt a plaintext with a paillier public key.
1766  *
1767  * @param public_key Public key to use.
1768  * @param m Plaintext to encrypt.
1769  * @param desired_ops How many homomorphic ops the caller intends to use
1770  * @param[out] ciphertext Encrytion of @a plaintext with @a public_key.
1771  * @return guaranteed number of supported homomorphic operations >= 1,
1772  *         or desired_ops, in case that is lower,
1773  *         or -1 if less than one homomorphic operation is possible
1774  */
1775 int
1776 GNUNET_CRYPTO_paillier_encrypt (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1777                                 const gcry_mpi_t m,
1778                                 int desired_ops,
1779                                 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext);
1780
1781
1782 /**
1783  * Decrypt a paillier ciphertext with a private key.
1784  *
1785  * @param private_key Private key to use for decryption.
1786  * @param public_key Public key to use for decryption.
1787  * @param ciphertext Ciphertext to decrypt.
1788  * @param[out] m Decryption of @a ciphertext with @private_key.
1789  */
1790 void
1791 GNUNET_CRYPTO_paillier_decrypt (const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key,
1792                                 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1793                                 const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
1794                                 gcry_mpi_t m);
1795
1796
1797 /**
1798  * Compute a ciphertext that represents the sum of the plaintext in @a x1 and @a x2
1799  *
1800  * Note that this operation can only be done a finite number of times
1801  * before an overflow occurs.
1802  *
1803  * @param public_key Public key to use for encryption.
1804  * @param c1 Paillier cipher text.
1805  * @param c2 Paillier cipher text.
1806  * @param[out] result Result of the homomorphic operation.
1807  * @return #GNUNET_OK if the result could be computed,
1808  *         #GNUNET_SYSERR if no more homomorphic operations are remaining.
1809  */
1810 int
1811 GNUNET_CRYPTO_paillier_hom_add (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1812                                 const struct GNUNET_CRYPTO_PaillierCiphertext *c1,
1813                                 const struct GNUNET_CRYPTO_PaillierCiphertext *c2,
1814                                 struct GNUNET_CRYPTO_PaillierCiphertext *result);
1815
1816
1817 /**
1818  * Get the number of remaining supported homomorphic operations.
1819  *
1820  * @param c Paillier cipher text.
1821  * @return the number of remaining homomorphic operations
1822  */
1823 int
1824 GNUNET_CRYPTO_paillier_hom_get_remaining (const struct GNUNET_CRYPTO_PaillierCiphertext *c);
1825
1826
1827 /* ********* Chaum-style RSA-based blind signatures ******************* */
1828
1829
1830
1831
1832 /**
1833  * The private information of an RSA key pair.
1834  */
1835 struct GNUNET_CRYPTO_RsaPrivateKey;
1836
1837 /**
1838  * The public information of an RSA key pair.
1839  */
1840 struct GNUNET_CRYPTO_RsaPublicKey;
1841
1842 /**
1843  * Constant-size pre-secret for blinding key generation.
1844  */
1845 struct GNUNET_CRYPTO_RsaBlindingKeySecret
1846 {
1847   /**
1848    * Bits used to generate the blinding key.  256 bits
1849    * of entropy is enough.
1850    */
1851   uint32_t pre_secret[8] GNUNET_PACKED;
1852 };
1853
1854 /**
1855  * @brief an RSA signature
1856  */
1857 struct GNUNET_CRYPTO_RsaSignature;
1858
1859
1860 /**
1861  * Create a new private key. Caller must free return value.
1862  *
1863  * @param len length of the key in bits (i.e. 2048)
1864  * @return fresh private key
1865  */
1866 struct GNUNET_CRYPTO_RsaPrivateKey *
1867 GNUNET_CRYPTO_rsa_private_key_create (unsigned int len);
1868
1869
1870 /**
1871  * Free memory occupied by the private key.
1872  *
1873  * @param key pointer to the memory to free
1874  */
1875 void
1876 GNUNET_CRYPTO_rsa_private_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *key);
1877
1878
1879 /**
1880  * Encode the private key in a format suitable for
1881  * storing it into a file.
1882  *
1883  * @param key the private key
1884  * @param[out] buffer set to a buffer with the encoded key
1885  * @return size of memory allocatedin @a buffer
1886  */
1887 size_t
1888 GNUNET_CRYPTO_rsa_private_key_encode (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
1889                                       char **buffer);
1890
1891
1892 /**
1893  * Decode the private key from the data-format back
1894  * to the "normal", internal format.
1895  *
1896  * @param buf the buffer where the private key data is stored
1897  * @param len the length of the data in @a buf
1898  * @return NULL on error
1899  */
1900 struct GNUNET_CRYPTO_RsaPrivateKey *
1901 GNUNET_CRYPTO_rsa_private_key_decode (const char *buf,
1902                                       size_t len);
1903
1904
1905 /**
1906  * Duplicate the given private key
1907  *
1908  * @param key the private key to duplicate
1909  * @return the duplicate key; NULL upon error
1910  */
1911 struct GNUNET_CRYPTO_RsaPrivateKey *
1912 GNUNET_CRYPTO_rsa_private_key_dup (const struct GNUNET_CRYPTO_RsaPrivateKey *key);
1913
1914
1915 /**
1916  * Extract the public key of the given private key.
1917  *
1918  * @param priv the private key
1919  * @retur NULL on error, otherwise the public key
1920  */
1921 struct GNUNET_CRYPTO_RsaPublicKey *
1922 GNUNET_CRYPTO_rsa_private_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey *priv);
1923
1924
1925 /**
1926  * Compute hash over the public key.
1927  *
1928  * @param key public key to hash
1929  * @param hc where to store the hash code
1930  */
1931 void
1932 GNUNET_CRYPTO_rsa_public_key_hash (const struct GNUNET_CRYPTO_RsaPublicKey *key,
1933                                    struct GNUNET_HashCode *hc);
1934
1935
1936 /**
1937  * Obtain the length of the RSA key in bits.
1938  *
1939  * @param key the public key to introspect
1940  * @return length of the key in bits
1941  */
1942 unsigned int
1943 GNUNET_CRYPTO_rsa_public_key_len (const struct GNUNET_CRYPTO_RsaPublicKey *key);
1944
1945
1946 /**
1947  * Free memory occupied by the public key.
1948  *
1949  * @param key pointer to the memory to free
1950  */
1951 void
1952 GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_RsaPublicKey *key);
1953
1954
1955 /**
1956  * Encode the public key in a format suitable for
1957  * storing it into a file.
1958  *
1959  * @param key the private key
1960  * @param[out] buffer set to a buffer with the encoded key
1961  * @return size of memory allocated in @a buffer
1962  */
1963 size_t
1964 GNUNET_CRYPTO_rsa_public_key_encode (const struct GNUNET_CRYPTO_RsaPublicKey *key,
1965                                      char **buffer);
1966
1967
1968 /**
1969  * Decode the public key from the data-format back
1970  * to the "normal", internal format.
1971  *
1972  * @param buf the buffer where the public key data is stored
1973  * @param len the length of the data in @a buf
1974  * @return NULL on error
1975  */
1976 struct GNUNET_CRYPTO_RsaPublicKey *
1977 GNUNET_CRYPTO_rsa_public_key_decode (const char *buf,
1978                                      size_t len);
1979
1980
1981 /**
1982  * Duplicate the given public key
1983  *
1984  * @param key the public key to duplicate
1985  * @return the duplicate key; NULL upon error
1986  */
1987 struct GNUNET_CRYPTO_RsaPublicKey *
1988 GNUNET_CRYPTO_rsa_public_key_dup (const struct GNUNET_CRYPTO_RsaPublicKey *key);
1989
1990
1991 /**
1992  * Compare the values of two signatures.
1993  *
1994  * @param s1 one signature
1995  * @param s2 the other signature
1996  * @return 0 if the two are equal
1997  */
1998 int
1999 GNUNET_CRYPTO_rsa_signature_cmp (struct GNUNET_CRYPTO_RsaSignature *s1,
2000                                  struct GNUNET_CRYPTO_RsaSignature *s2);
2001
2002 /**
2003  * Compare the values of two private keys.
2004  *
2005  * @param p1 one private key
2006  * @param p2 the other private key
2007  * @return 0 if the two are equal
2008  */
2009 int
2010 GNUNET_CRYPTO_rsa_private_key_cmp (struct GNUNET_CRYPTO_RsaPrivateKey *p1,
2011                                   struct GNUNET_CRYPTO_RsaPrivateKey *p2);
2012
2013
2014 /**
2015  * Compare the values of two public keys.
2016  *
2017  * @param p1 one public key
2018  * @param p2 the other public key
2019  * @return 0 if the two are equal
2020  */
2021 int
2022 GNUNET_CRYPTO_rsa_public_key_cmp (struct GNUNET_CRYPTO_RsaPublicKey *p1,
2023                                   struct GNUNET_CRYPTO_RsaPublicKey *p2);
2024
2025
2026 /**
2027  * Blinds the given message with the given blinding key
2028  *
2029  * @param hash hash of the message to sign
2030  * @param bkey the blinding key
2031  * @param pkey the public key of the signer
2032  * @param[out] buf set to a buffer with the blinded message to be signed
2033  * @param[out] buf_size number of bytes stored in @a buf
2034  * @return #GNUNET_YES if successful, #GNUNET_NO if RSA key is malicious
2035  */
2036 int
2037 GNUNET_CRYPTO_rsa_blind (const struct GNUNET_HashCode *hash,
2038                          const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
2039                          struct GNUNET_CRYPTO_RsaPublicKey *pkey,
2040                          char **buf,
2041                          size_t *buf_size);
2042
2043
2044 /**
2045  * Sign a blinded value, which must be a full domain hash of a message.
2046  *
2047  * @param key private key to use for the signing
2048  * @param msg the (blinded) message to sign
2049  * @param msg_len number of bytes in @a msg to sign
2050  * @return NULL on error, signature on success
2051  */
2052 struct GNUNET_CRYPTO_RsaSignature *
2053 GNUNET_CRYPTO_rsa_sign_blinded (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2054                                 const void *msg,
2055                                 size_t msg_len);
2056
2057
2058 /**
2059  * Create and sign a full domain hash of a message.
2060  *
2061  * @param key private key to use for the signing
2062  * @param hash the hash of the message to sign
2063  * @return NULL on error, including a malicious RSA key, signature on success
2064  */
2065 struct GNUNET_CRYPTO_RsaSignature *
2066 GNUNET_CRYPTO_rsa_sign_fdh (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2067                             const struct GNUNET_HashCode *hash);
2068
2069
2070 /**
2071  * Free memory occupied by signature.
2072  *
2073  * @param sig memory to free
2074  */
2075 void
2076 GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_RsaSignature *sig);
2077
2078
2079 /**
2080  * Encode the given signature in a format suitable for storing it into a file.
2081  *
2082  * @param sig the signature
2083  * @param[out] buffer set to a buffer with the encoded key
2084  * @return size of memory allocated in @a buffer
2085  */
2086 size_t
2087 GNUNET_CRYPTO_rsa_signature_encode (const struct GNUNET_CRYPTO_RsaSignature *sig,
2088                                     char **buffer);
2089
2090
2091 /**
2092  * Decode the signature from the data-format back to the "normal", internal
2093  * format.
2094  *
2095  * @param buf the buffer where the public key data is stored
2096  * @param len the length of the data in @a buf
2097  * @return NULL on error
2098  */
2099 struct GNUNET_CRYPTO_RsaSignature *
2100 GNUNET_CRYPTO_rsa_signature_decode (const char *buf,
2101                                     size_t len);
2102
2103
2104 /**
2105  * Duplicate the given rsa signature
2106  *
2107  * @param sig the signature to duplicate
2108  * @return the duplicate key; NULL upon error
2109  */
2110 struct GNUNET_CRYPTO_RsaSignature *
2111 GNUNET_CRYPTO_rsa_signature_dup (const struct GNUNET_CRYPTO_RsaSignature *sig);
2112
2113
2114 /**
2115  * Unblind a blind-signed signature.  The signature should have been generated
2116  * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with
2117  * #GNUNET_CRYPTO_rsa_blind().
2118  *
2119  * @param sig the signature made on the blinded signature purpose
2120  * @param bks the blinding key secret used to blind the signature purpose
2121  * @param pkey the public key of the signer
2122  * @return unblinded signature on success, NULL if RSA key is bad or malicious.
2123  */
2124 struct GNUNET_CRYPTO_RsaSignature *
2125 GNUNET_CRYPTO_rsa_unblind (const struct GNUNET_CRYPTO_RsaSignature *sig,
2126                            const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
2127                            struct GNUNET_CRYPTO_RsaPublicKey *pkey);
2128
2129
2130 /**
2131  * Verify whether the given hash corresponds to the given signature and the
2132  * signature is valid with respect to the given public key.
2133  *
2134  * @param hash the message to verify to match the @a sig
2135  * @param sig signature that is being validated
2136  * @param public_key public key of the signer
2137  * @returns #GNUNET_YES if ok, #GNUNET_NO if RSA key is malicious, #GNUNET_SYSERR if signature
2138  */
2139 int
2140 GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash,
2141                           const struct GNUNET_CRYPTO_RsaSignature *sig,
2142                           const struct GNUNET_CRYPTO_RsaPublicKey *public_key);
2143
2144
2145 /**
2146  * @ingroup crypto
2147  * Create a new CP-ABE master key. Caller must free return value.
2148  *
2149  * @return fresh private key; free using #GNUNET_free
2150  */
2151 struct GNUNET_CRYPTO_AbeMasterKey *
2152 GNUNET_CRYPTO_cpabe_create_master_key (void);
2153 void
2154 GNUNET_CRYPTO_cpabe_delete_master_key (struct GNUNET_CRYPTO_AbeMasterKey *key);
2155
2156 /**
2157  * @ingroup crypto
2158  * Create a new CP-ABE key. Caller must free return value.
2159  *
2160  * @return fresh private key; free using #GNUNET_free
2161  */
2162 struct GNUNET_CRYPTO_AbeKey *
2163 GNUNET_CRYPTO_cpabe_create_key (struct GNUNET_CRYPTO_AbeMasterKey *msk,
2164                                 char **attrs);
2165 void
2166 GNUNET_CRYPTO_cpabe_delete_key (struct GNUNET_CRYPTO_AbeKey *key,
2167                                 int delete_pub);
2168
2169
2170 /**
2171  * @ingroup crypto
2172  * Encrypt a block using  sessionkey.
2173  *
2174  * @param block the block to encrypt
2175  * @param size the size of the @a block
2176  * @param sessionkey the key used to encrypt
2177  * @param iv the initialization vector to use, use INITVALUE
2178  *        for streams.
2179  * @return the size of the encrypted block, -1 for errors
2180  */
2181 ssize_t
2182 GNUNET_CRYPTO_cpabe_encrypt (const void *block,
2183                              size_t size,
2184                              const char *policy,
2185                              const struct GNUNET_CRYPTO_AbeMasterKey *key,
2186                              void **result);
2187
2188 /**
2189  * @ingroup crypto
2190  * Encrypt a block using  sessionkey.
2191  *
2192  * @param block the block to encrypt
2193  * @param size the size of the @a block
2194  * @param sessionkey the key used to encrypt
2195  * @param iv the initialization vector to use, use INITVALUE
2196  *        for streams.
2197  * @return the size of the encrypted block, -1 for errors
2198  */
2199 ssize_t
2200 GNUNET_CRYPTO_cpabe_decrypt (const void *block,
2201                              size_t size,
2202                              const struct GNUNET_CRYPTO_AbeKey *key,
2203                              void **result);
2204
2205 ssize_t
2206 GNUNET_CRYPTO_cpabe_serialize_key (const struct GNUNET_CRYPTO_AbeKey *key,
2207                                    void **result);
2208
2209 struct GNUNET_CRYPTO_AbeKey*
2210 GNUNET_CRYPTO_cpabe_deserialize_key (const void *data,
2211                                      size_t len);
2212
2213 ssize_t
2214 GNUNET_CRYPTO_cpabe_serialize_master_key (const struct GNUNET_CRYPTO_AbeMasterKey *key,
2215                                           void **result);
2216
2217 struct GNUNET_CRYPTO_AbeMasterKey*
2218 GNUNET_CRYPTO_cpabe_deserialize_master_key (const void *data,
2219                                             size_t len);
2220
2221
2222 #if 0                           /* keep Emacsens' auto-indent happy */
2223 {
2224 #endif
2225 #ifdef __cplusplus
2226 }
2227 #endif
2228
2229
2230 /* ifndef GNUNET_CRYPTO_LIB_H */
2231 #endif
2232 /* end of gnunet_crypto_lib.h */