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