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