2 This file is part of GNUnet.
3 Copyright (C) 2001-2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file include/gnunet_crypto_lib.h
23 * @brief cryptographic primitives for GNUnet
25 * @author Christian Grothoff
26 * @author Krista Bennett
27 * @author Gerd Knorr <kraxel@bytesex.org>
28 * @author Ioana Patrascu
29 * @author Tzvetan Horozov
31 * @defgroup crypto Cryptographic operations
32 * @defgroup hash Hashing and operations on hashes
35 #ifndef GNUNET_CRYPTO_LIB_H
36 #define GNUNET_CRYPTO_LIB_H
41 #if 0 /* keep Emacsens' auto-indent happy */
47 * @brief A 512-bit hashcode
49 struct GNUNET_HashCode;
52 * The identity of the host (wraps the signing key of the peer).
54 struct GNUNET_PeerIdentity;
56 #include "gnunet_common.h"
57 #include "gnunet_scheduler_lib.h"
62 * @brief A 512-bit hashcode
64 struct GNUNET_HashCode
66 uint32_t bits[512 / 8 / sizeof (uint32_t)]; /* = 16 */
71 * Maximum length of an ECC signature.
72 * Note: round up to multiple of 8 minus 2 for alignment.
74 #define GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH 126
78 * Desired quality level for random numbers.
81 enum GNUNET_CRYPTO_Quality
84 * No good quality of the operation is needed (i.e.,
85 * random numbers can be pseudo-random).
88 GNUNET_CRYPTO_QUALITY_WEAK,
91 * High-quality operations are desired.
94 GNUNET_CRYPTO_QUALITY_STRONG,
97 * Randomness for IVs etc. is required.
100 GNUNET_CRYPTO_QUALITY_NONCE
105 * @brief length of the sessionkey in bytes (256 BIT sessionkey)
107 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
110 * Length of a hash value
112 #define GNUNET_CRYPTO_HASH_LENGTH (512/8)
115 * How many characters (without 0-terminator) are our ASCII-encoded
116 * public keys (ECDSA/EDDSA/ECDHE).
118 #define GNUNET_CRYPTO_PKEY_ASCII_LENGTH 52
121 * @brief 0-terminated ASCII encoding of a struct GNUNET_HashCode.
123 struct GNUNET_CRYPTO_HashAsciiEncoded
125 unsigned char encoding[104];
129 GNUNET_NETWORK_STRUCT_BEGIN
133 * @brief header of what an ECC signature signs
134 * this must be followed by "size - 8" bytes of
135 * the actual signed data
137 struct GNUNET_CRYPTO_EccSignaturePurpose
140 * How many bytes does this signature sign?
141 * (including this purpose header); in network
144 uint32_t size GNUNET_PACKED;
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!
152 uint32_t purpose GNUNET_PACKED;
158 * @brief an ECC signature using EdDSA.
159 * See https://gnunet.org/ed25519
161 struct GNUNET_CRYPTO_EddsaSignature
167 unsigned char r[256 / 8];
172 unsigned char s[256 / 8];
179 * @brief an ECC signature using ECDSA
181 struct GNUNET_CRYPTO_EcdsaSignature
187 unsigned char r[256 / 8];
192 unsigned char s[256 / 8];
198 * Public ECC key (always for Curve25519) encoded in a format suitable
199 * for network transmission and EdDSA signatures.
201 struct GNUNET_CRYPTO_EddsaPublicKey
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.
207 unsigned char q_y[256 / 8];
213 * Public ECC key (always for Curve25519) encoded in a format suitable
214 * for network transmission and ECDSA signatures.
216 struct GNUNET_CRYPTO_EcdsaPublicKey
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.
222 unsigned char q_y[256 / 8];
228 * The identity of the host (wraps the signing key of the peer).
230 struct GNUNET_PeerIdentity
232 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
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
241 struct GNUNET_CRYPTO_EcdhePublicKey
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.
247 unsigned char q_y[256 / 8];
252 * Private ECC key encoded for transmission. To be used only for ECDH
253 * key exchange (ECDHE to be precise).
255 struct GNUNET_CRYPTO_EcdhePrivateKey
258 * d is a value mod n, where n has at most 256 bits.
260 unsigned char d[256 / 8];
265 * Private ECC key encoded for transmission. To be used only for ECDSA
268 struct GNUNET_CRYPTO_EcdsaPrivateKey
271 * d is a value mod n, where n has at most 256 bits.
273 unsigned char d[256 / 8];
278 * Private ECC key encoded for transmission. To be used only for EdDSA
281 struct GNUNET_CRYPTO_EddsaPrivateKey
284 * d is a value mod n, where n has at most 256 bits.
286 unsigned char d[256 / 8];
292 * @brief type for session keys
294 struct GNUNET_CRYPTO_SymmetricSessionKey
297 * Actual key for AES.
299 unsigned char aes_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
302 * Actual key for TwoFish.
304 unsigned char twofish_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
308 GNUNET_NETWORK_STRUCT_END
311 * @brief IV for sym cipher
313 * NOTE: must be smaller (!) in size than the
314 * `struct GNUNET_HashCode`.
316 struct GNUNET_CRYPTO_SymmetricInitializationVector
318 unsigned char aes_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
320 unsigned char twofish_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
325 * @brief type for (message) authentication keys
327 struct GNUNET_CRYPTO_AuthKey
329 unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
334 * Size of paillier plain texts and public keys.
335 * Private keys and ciphertexts are twice this size.
337 #define GNUNET_CRYPTO_PAILLIER_BITS 2048
341 * Paillier public key.
343 struct GNUNET_CRYPTO_PaillierPublicKey
348 unsigned char n[GNUNET_CRYPTO_PAILLIER_BITS / 8];
353 * Paillier public key.
355 struct GNUNET_CRYPTO_PaillierPrivateKey
358 * Lambda-component of the private key.
360 unsigned char lambda[GNUNET_CRYPTO_PAILLIER_BITS / 8];
362 * Mu-component of the private key.
364 unsigned char mu[GNUNET_CRYPTO_PAILLIER_BITS / 8];
369 * Paillier ciphertext.
371 struct GNUNET_CRYPTO_PaillierCiphertext
374 * Guaranteed minimum number of homomorphic operations with this ciphertext,
375 * in network byte order (NBO).
377 int32_t remaining_ops GNUNET_PACKED;
380 * The bits of the ciphertext.
382 unsigned char bits[GNUNET_CRYPTO_PAILLIER_BITS * 2 / 8];
386 /* **************** Functions and Macros ************* */
390 * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator
393 * @param seed the seed to use
396 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
400 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
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)
408 GNUNET_CRYPTO_crc16_step (uint32_t sum,
414 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
416 * @param sum cummulative sum
417 * @return crc16 value
420 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
425 * Calculate the checksum of a buffer in one step.
427 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
428 * @param len number of bytes in @a buf, must be multiple of 2
429 * @return crc16 value
432 GNUNET_CRYPTO_crc16_n (const void *buf,
438 * Compute the CRC32 checksum for the first len
439 * bytes of the buffer.
441 * @param buf the data over which we're taking the CRC
442 * @param len the length of the buffer @a buf in bytes
443 * @return the resulting CRC32 checksum
446 GNUNET_CRYPTO_crc32_n (const void *buf,
452 * Fill block with a random values.
454 * @param mode desired quality of the random number
455 * @param buffer the buffer to fill
456 * @param length buffer length
459 GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode,
465 * Produce a random value.
467 * @param mode desired quality of the random number
468 * @param i the upper limit (exclusive) for the random number
469 * @return a random value in the interval [0,@a i) (exclusive).
472 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode,
478 * Random on unsigned 64-bit values.
480 * @param mode desired quality of the random number
481 * @param max value returned will be in range [0,@a max) (exclusive)
482 * @return random 64-bit number
485 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
491 * Get an array with a random permutation of the
493 * @param mode #GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used,
494 * #GNUNET_CRYPTO_QUALITY_WEAK or #GNUNET_CRYPTO_QUALITY_NONCE otherwise
495 * @param n the size of the array
496 * @return the permutation array (allocated from heap)
499 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode,
505 * Create a new random session key.
507 * @param key key to initialize
510 GNUNET_CRYPTO_symmetric_create_session_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key);
515 * Encrypt a block using a symmetric sessionkey.
517 * @param block the block to encrypt
518 * @param size the size of the @a block
519 * @param sessionkey the key used to encrypt
520 * @param iv the initialization vector to use, use INITVALUE
522 * @return the size of the encrypted block, -1 for errors
525 GNUNET_CRYPTO_symmetric_encrypt (const void *block,
527 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
528 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
534 * Decrypt a given block using a symmetric sessionkey.
536 * @param block the data to decrypt, encoded as returned by encrypt
537 * @param size how big is the block?
538 * @param sessionkey the key used to decrypt
539 * @param iv the initialization vector to use
540 * @param result address to store the result at
541 * @return -1 on failure, size of decrypted block on success
544 GNUNET_CRYPTO_symmetric_decrypt (const void *block,
546 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
547 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
553 * @brief Derive an IV
554 * @param iv initialization vector
555 * @param skey session key
556 * @param salt salt for the derivation
557 * @param salt_len size of the @a salt
558 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
561 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
562 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
564 size_t salt_len, ...);
568 * @brief Derive an IV
569 * @param iv initialization vector
570 * @param skey session key
571 * @param salt salt for the derivation
572 * @param salt_len size of the @a salt
573 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
576 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
577 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
585 * Convert hash to ASCII encoding.
586 * @param block the hash code
587 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
588 * safely cast to char*, a '\\0' termination is set).
591 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block,
592 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
597 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
599 * @param enc the encoding
600 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
601 * @param result where to store the hash code
602 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
605 GNUNET_CRYPTO_hash_from_string2 (const char *enc,
607 struct GNUNET_HashCode *result);
612 * Convert ASCII encoding back to `struct GNUNET_HashCode`
614 * @param enc the encoding
615 * @param result where to store the hash code
616 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
618 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
619 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
625 * Compute the distance between 2 hashcodes. The
626 * computation must be fast, not involve @a a[0] or @a a[4] (they're used
627 * elsewhere), and be somewhat consistent. And of course, the result
628 * should be a positive number.
630 * @param a some hash code
631 * @param b some hash code
632 * @return number between 0 and UINT32_MAX
635 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a,
636 const struct GNUNET_HashCode *b);
641 * Compute hash of a given block.
643 * @param block the data to hash
644 * @param size size of the @a block
645 * @param ret pointer to where to write the hashcode
648 GNUNET_CRYPTO_hash (const void *block,
650 struct GNUNET_HashCode *ret);
654 * Context for cummulative hashing.
656 struct GNUNET_HashContext;
660 * Start incremental hashing operation.
662 * @return context for incremental hash computation
664 struct GNUNET_HashContext *
665 GNUNET_CRYPTO_hash_context_start (void);
669 * Add data to be hashed.
671 * @param hc cummulative hash context
672 * @param buf data to add
673 * @param size number of bytes in @a buf
676 GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
682 * Finish the hash computation.
684 * @param hc hash context to use, is freed in the process
685 * @param r_hash where to write the latest / final hash code
688 GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
689 struct GNUNET_HashCode *r_hash);
693 * Abort hashing, do not bother calculating final result.
695 * @param hc hash context to destroy
698 GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc);
703 * Calculate HMAC of a message (RFC 2104)
705 * @param key secret key
706 * @param plaintext input plaintext
707 * @param plaintext_len length of @a plaintext
708 * @param hmac where to store the hmac
711 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
712 const void *plaintext,
713 size_t plaintext_len,
714 struct GNUNET_HashCode *hmac);
718 * Function called once the hash computation over the
719 * specified file has completed.
722 * @param res resulting hash, NULL on error
725 (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
726 const struct GNUNET_HashCode *res);
730 * Handle to file hashing operation.
732 struct GNUNET_CRYPTO_FileHashContext;
737 * Compute the hash of an entire file.
739 * @param priority scheduling priority to use
740 * @param filename name of file to hash
741 * @param blocksize number of bytes to process in one task
742 * @param callback function to call upon completion
743 * @param callback_cls closure for @a callback
744 * @return NULL on (immediate) errror
746 struct GNUNET_CRYPTO_FileHashContext *
747 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
748 const char *filename,
750 GNUNET_CRYPTO_HashCompletedCallback callback,
755 * Cancel a file hashing operation.
757 * @param fhc operation to cancel (callback must not yet have been invoked)
760 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
765 * Create a random hash code.
767 * @param mode desired quality level
768 * @param result hash code that is randomized
771 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
772 struct GNUNET_HashCode *result);
777 * compute @a result = @a b - @a a
779 * @param a some hash code
780 * @param b some hash code
781 * @param result set to @a b - @a a
784 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
785 const struct GNUNET_HashCode *b,
786 struct GNUNET_HashCode *result);
791 * compute @a result = @a a + @a delta
793 * @param a some hash code
794 * @param delta some hash code
795 * @param result set to @a a + @a delta
798 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a,
799 const struct GNUNET_HashCode *delta,
800 struct GNUNET_HashCode *result);
805 * compute result = a ^ b
807 * @param a some hash code
808 * @param b some hash code
809 * @param result set to @a a ^ @a b
812 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
813 const struct GNUNET_HashCode *b,
814 struct GNUNET_HashCode *result);
819 * Convert a hashcode into a key.
821 * @param hc hash code that serves to generate the key
822 * @param skey set to a valid session key
823 * @param iv set to a valid initialization vector
826 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
827 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
828 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv);
833 * Obtain a bit from a hashcode.
835 * @param code the `struct GNUNET_HashCode` to index bit-wise
836 * @param bit index into the hashcode, [0...159]
837 * @return Bit \a bit from hashcode \a code, -1 for invalid index
840 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code,
846 * Determine how many low order bits match in two
847 * `struct GNUNET_HashCodes`. i.e. - 010011 and 011111 share
848 * the first two lowest order bits, and therefore the
849 * return value is two (NOT XOR distance, nor how many
850 * bits match absolutely!).
852 * @param first the first hashcode
853 * @param second the hashcode to compare first to
854 * @return the number of bits that match
857 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
858 const struct GNUNET_HashCode *second);
863 * Compare function for HashCodes, producing a total ordering
866 * @param h1 some hash code
867 * @param h2 some hash code
868 * @return 1 if @a h1 > @a h2, -1 if @a h1 < @a h2 and 0 if @a h1 == @a h2.
871 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
872 const struct GNUNET_HashCode *h2);
877 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
878 * in the XOR metric (Kademlia).
880 * @param h1 some hash code
881 * @param h2 some hash code
882 * @param target some hash code
883 * @return -1 if @a h1 is closer, 1 if @a h2 is closer and 0 if @a h1== @a h2.
886 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
887 const struct GNUNET_HashCode *h2,
888 const struct GNUNET_HashCode *target);
893 * @brief Derive an authentication key
894 * @param key authentication key
895 * @param rkey root key
897 * @param salt_len size of the salt
898 * @param argp pair of void * & size_t for context chunks, terminated by NULL
901 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
902 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
903 const void *salt, size_t salt_len,
909 * @brief Derive an authentication key
910 * @param key authentication key
911 * @param rkey root key
913 * @param salt_len size of the salt
914 * @param ... pair of void * & size_t for context chunks, terminated by NULL
917 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
918 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
919 const void *salt, size_t salt_len,
926 * @param result buffer for the derived key, allocated by caller
927 * @param out_len desired length of the derived key
928 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
929 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
931 * @param xts_len length of @a xts
932 * @param skm source key material
933 * @param skm_len length of @a skm
934 * @param ... pair of void * & size_t for context chunks, terminated by NULL
935 * @return #GNUNET_YES on success
938 GNUNET_CRYPTO_hkdf (void *result,
952 * @param result buffer for the derived key, allocated by caller
953 * @param out_len desired length of the derived key
954 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
955 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
957 * @param xts_len length of @a xts
958 * @param skm source key material
959 * @param skm_len length of @a skm
960 * @param argp va_list of void * & size_t pairs for context chunks
961 * @return #GNUNET_YES on success
964 GNUNET_CRYPTO_hkdf_v (void *result,
977 * @param result buffer for the derived key, allocated by caller
978 * @param out_len desired length of the derived key
980 * @param xts_len length of @a xts
981 * @param skm source key material
982 * @param skm_len length of @a skm
983 * @param argp va_list of void * & size_t pairs for context chunks
984 * @return #GNUNET_YES on success
987 GNUNET_CRYPTO_kdf_v (void *result,
999 * @param result buffer for the derived key, allocated by caller
1000 * @param out_len desired length of the derived key
1002 * @param xts_len length of @a xts
1003 * @param skm source key material
1004 * @param skm_len length of @a skm
1005 * @param ... void * & size_t pairs for context chunks
1006 * @return #GNUNET_YES on success
1009 GNUNET_CRYPTO_kdf (void *result,
1020 * Extract the public key for the given private key.
1022 * @param priv the private key
1023 * @param pub where to write the public key
1026 GNUNET_CRYPTO_ecdsa_key_get_public (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1027 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1031 * Extract the public key for the given private key.
1033 * @param priv the private key
1034 * @param pub where to write the public key
1037 GNUNET_CRYPTO_eddsa_key_get_public (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1038 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1044 * Convert ECDSA public key to ECDHE public key.
1045 * Please be very careful when using this function, as mixing
1046 * cryptographic primitives is not always healthy.
1048 * @param ecdsa ecdsa public key
1049 * @param ecdhe[OUT] ecdhe public key
1052 GNUNET_CRYPTO_ecdsa_public_to_ecdhe (const struct GNUNET_CRYPTO_EcdsaPublicKey *ecdsa,
1053 struct GNUNET_CRYPTO_EcdhePublicKey *ecdhe);
1058 * Extract the public key for the given private key.
1060 * @param priv the private key
1061 * @param pub where to write the public key
1064 GNUNET_CRYPTO_ecdhe_key_get_public (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1065 struct GNUNET_CRYPTO_EcdhePublicKey *pub);
1069 * Convert a public key to a string.
1071 * @param pub key to convert
1072 * @return string representing @a pub
1075 GNUNET_CRYPTO_ecdsa_public_key_to_string (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1079 * Convert a public key to a string.
1081 * @param pub key to convert
1082 * @return string representing @a pub
1085 GNUNET_CRYPTO_eddsa_public_key_to_string (const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1089 * Convert a string representing a public key to a public key.
1091 * @param enc encoded public key
1092 * @param enclen number of bytes in @a enc (without 0-terminator)
1093 * @param pub where to store the public key
1094 * @return #GNUNET_OK on success
1097 GNUNET_CRYPTO_ecdsa_public_key_from_string (const char *enc,
1099 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1103 * Convert a string representing a private key to a private key.
1105 * @param enc encoded public key
1106 * @param enclen number of bytes in @a enc (without 0-terminator)
1107 * @param priv where to store the private key
1108 * @return #GNUNET_OK on success
1111 GNUNET_CRYPTO_eddsa_private_key_from_string (const char *enc,
1113 struct GNUNET_CRYPTO_EddsaPrivateKey *pub);
1117 * Convert a string representing a public key to a public key.
1119 * @param enc encoded public key
1120 * @param enclen number of bytes in @a enc (without 0-terminator)
1121 * @param pub where to store the public key
1122 * @return #GNUNET_OK on success
1125 GNUNET_CRYPTO_eddsa_public_key_from_string (const char *enc,
1127 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1132 * Create a new private key by reading it from a file. If the
1133 * files does not exist, create a new key and write it to the
1134 * file. Caller must free return value. Note that this function
1135 * can not guarantee that another process might not be trying
1136 * the same operation on the same file at the same time.
1137 * If the contents of the file
1138 * are invalid the old file is deleted and a fresh key is
1141 * @param filename name of file to use to store the key
1142 * @return new private key, NULL on error (for example,
1143 * permission denied); free using #GNUNET_free
1145 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1146 GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename);
1151 * Create a new private key by reading it from a file. If the
1152 * files does not exist, create a new key and write it to the
1153 * file. Caller must free return value. Note that this function
1154 * can not guarantee that another process might not be trying
1155 * the same operation on the same file at the same time.
1156 * If the contents of the file
1157 * are invalid the old file is deleted and a fresh key is
1160 * @param filename name of file to use to store the key
1161 * @return new private key, NULL on error (for example,
1162 * permission denied); free using #GNUNET_free
1164 struct GNUNET_CRYPTO_EddsaPrivateKey *
1165 GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename);
1167 struct GNUNET_CONFIGURATION_Handle;
1172 * Create a new private key by reading our peer's key from
1173 * the file specified in the configuration.
1175 * @param cfg the configuration to use
1176 * @return new private key, NULL on error (for example,
1177 * permission denied); free using #GNUNET_free
1179 struct GNUNET_CRYPTO_EddsaPrivateKey *
1180 GNUNET_CRYPTO_eddsa_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
1185 * Create a new private key. Caller must free return value.
1187 * @return fresh private key; free using #GNUNET_free
1189 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1190 GNUNET_CRYPTO_ecdsa_key_create (void);
1195 * Create a new private key. Caller must free return value.
1197 * @return fresh private key; free using #GNUNET_free
1199 struct GNUNET_CRYPTO_EddsaPrivateKey *
1200 GNUNET_CRYPTO_eddsa_key_create (void);
1205 * Create a new private key. Caller must free return value.
1207 * @return fresh private key; free using #GNUNET_free
1209 struct GNUNET_CRYPTO_EcdhePrivateKey *
1210 GNUNET_CRYPTO_ecdhe_key_create (void);
1215 * Clear memory that was used to store a private key.
1217 * @param pk location of the key
1220 GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk);
1225 * Clear memory that was used to store a private key.
1227 * @param pk location of the key
1230 GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk);
1234 * Clear memory that was used to store a private key.
1236 * @param pk location of the key
1239 GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1244 * Get the shared private key we use for anonymous users.
1246 * @return "anonymous" private key; do not free
1248 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
1249 GNUNET_CRYPTO_ecdsa_key_get_anonymous (void);
1254 * Setup a hostkey file for a peer given the name of the
1255 * configuration file (!). This function is used so that
1256 * at a later point code can be certain that reading a
1257 * hostkey is fast (for example in time-dependent testcases).
1259 * @param cfg_name name of the configuration file to use
1262 GNUNET_CRYPTO_eddsa_setup_hostkey (const char *cfg_name);
1267 * Retrieve the identity of the host's peer.
1269 * @param cfg configuration to use
1270 * @param dst pointer to where to write the peer identity
1271 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
1272 * could not be retrieved
1275 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
1276 struct GNUNET_PeerIdentity *dst);
1280 * Compare two Peer Identities.
1282 * @param first first peer identity
1283 * @param second second peer identity
1284 * @return bigger than 0 if first > second,
1285 * 0 if they are the same
1286 * smaller than 0 if second > first
1289 GNUNET_CRYPTO_cmp_peer_identity (const struct GNUNET_PeerIdentity *first,
1290 const struct GNUNET_PeerIdentity *second);
1295 * Derive key material from a public and a private ECC key.
1297 * @param priv private key to use for the ECDH (x)
1298 * @param pub public key to use for the ECDH (yG)
1299 * @param key_material where to write the key material (xyG)
1300 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1303 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1304 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1305 struct GNUNET_HashCode *key_material);
1310 * EdDSA sign a given block.
1312 * @param priv private key to use for the signing
1313 * @param purpose what to sign (size, purpose)
1314 * @param sig where to write the signature
1315 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1318 GNUNET_CRYPTO_eddsa_sign (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1319 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1320 struct GNUNET_CRYPTO_EddsaSignature *sig);
1325 * ECDSA Sign a given block.
1327 * @param priv private key to use for the signing
1328 * @param purpose what to sign (size, purpose)
1329 * @param sig where to write the signature
1330 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1333 GNUNET_CRYPTO_ecdsa_sign (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1334 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1335 struct GNUNET_CRYPTO_EcdsaSignature *sig);
1339 * Verify EdDSA signature.
1341 * @param purpose what is the purpose that the signature should have?
1342 * @param validate block to validate (size, purpose, data)
1343 * @param sig signature that is being validated
1344 * @param pub public key of the signer
1345 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1348 GNUNET_CRYPTO_eddsa_verify (uint32_t purpose,
1349 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1350 const struct GNUNET_CRYPTO_EddsaSignature *sig,
1351 const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1357 * Verify ECDSA signature.
1359 * @param purpose what is the purpose that the signature should have?
1360 * @param validate block to validate (size, purpose, data)
1361 * @param sig signature that is being validated
1362 * @param pub public key of the signer
1363 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1366 GNUNET_CRYPTO_ecdsa_verify (uint32_t purpose,
1367 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1368 const struct GNUNET_CRYPTO_EcdsaSignature *sig,
1369 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1374 * Derive a private key from a given private key and a label.
1375 * Essentially calculates a private key 'h = H(l,P) * d mod n'
1376 * where n is the size of the ECC group and P is the public
1377 * key associated with the private key 'd'.
1379 * @param priv original private key
1380 * @param label label to use for key deriviation
1381 * @param context additional context to use for HKDF of 'h';
1382 * typically the name of the subsystem/application
1383 * @return derived private key
1385 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1386 GNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1388 const char *context);
1393 * Derive a public key from a given public key and a label.
1394 * Essentially calculates a public key 'V = H(l,P) * P'.
1396 * @param pub original public key
1397 * @param label label to use for key deriviation
1398 * @param context additional context to use for HKDF of 'h'.
1399 * typically the name of the subsystem/application
1400 * @param result where to write the derived public key
1403 GNUNET_CRYPTO_ecdsa_public_key_derive (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1405 const char *context,
1406 struct GNUNET_CRYPTO_EcdsaPublicKey *result);
1410 * Output the given MPI value to the given buffer in network
1411 * byte order. The MPI @a val may not be negative.
1413 * @param buf where to output to
1414 * @param size number of bytes in @a buf
1415 * @param val value to write to @a buf
1418 GNUNET_CRYPTO_mpi_print_unsigned (void *buf,
1424 * Convert data buffer into MPI value.
1425 * The buffer is interpreted as network
1426 * byte order, unsigned integer.
1428 * @param result where to store MPI value (allocated)
1429 * @param data raw data (GCRYMPI_FMT_USG)
1430 * @param size number of bytes in @a data
1433 GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result,
1439 * Create a freshly generated paillier public key.
1441 * @param[out] public_key Where to store the public key?
1442 * @param[out] private_key Where to store the private key?
1445 GNUNET_CRYPTO_paillier_create (struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1446 struct GNUNET_CRYPTO_PaillierPrivateKey *private_key);
1450 * Encrypt a plaintext with a paillier public key.
1452 * @param public_key Public key to use.
1453 * @param m Plaintext to encrypt.
1454 * @param desired_ops How many homomorphic ops the caller intends to use
1455 * @param[out] ciphertext Encrytion of @a plaintext with @a public_key.
1456 * @return guaranteed number of supported homomorphic operations >= 1,
1457 * or desired_ops, in case that is lower,
1458 * or -1 if less than one homomorphic operation is possible
1461 GNUNET_CRYPTO_paillier_encrypt (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1464 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext);
1468 * Decrypt a paillier ciphertext with a private key.
1470 * @param private_key Private key to use for decryption.
1471 * @param public_key Public key to use for decryption.
1472 * @param ciphertext Ciphertext to decrypt.
1473 * @param[out] m Decryption of @a ciphertext with @private_key.
1476 GNUNET_CRYPTO_paillier_decrypt (const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key,
1477 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1478 const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
1483 * Compute a ciphertext that represents the sum of the plaintext in @a x1 and @a x2
1485 * Note that this operation can only be done a finite number of times
1486 * before an overflow occurs.
1488 * @param public_key Public key to use for encryption.
1489 * @param c1 Paillier cipher text.
1490 * @param c2 Paillier cipher text.
1491 * @param[out] result Result of the homomorphic operation.
1492 * @return #GNUNET_OK if the result could be computed,
1493 * #GNUNET_SYSERR if no more homomorphic operations are remaining.
1496 GNUNET_CRYPTO_paillier_hom_add (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1497 const struct GNUNET_CRYPTO_PaillierCiphertext *c1,
1498 const struct GNUNET_CRYPTO_PaillierCiphertext *c2,
1499 struct GNUNET_CRYPTO_PaillierCiphertext *result);
1503 * Get the number of remaining supported homomorphic operations.
1505 * @param c Paillier cipher text.
1506 * @return the number of remaining homomorphic operations
1509 GNUNET_CRYPTO_paillier_hom_get_remaining (const struct GNUNET_CRYPTO_PaillierCiphertext *c);
1512 /* ********* Chaum-style RSA-based blind signatures ******************* */
1518 * The private information of an RSA key pair.
1520 struct GNUNET_CRYPTO_rsa_PrivateKey;
1523 * The public information of an RSA key pair.
1525 struct GNUNET_CRYPTO_rsa_PublicKey;
1528 * Key used to blind a message
1530 struct GNUNET_CRYPTO_rsa_BlindingKey;
1533 * @brief an RSA signature
1535 struct GNUNET_CRYPTO_rsa_Signature;
1539 * Create a new private key. Caller must free return value.
1541 * @param len length of the key in bits (i.e. 2048)
1542 * @return fresh private key
1544 struct GNUNET_CRYPTO_rsa_PrivateKey *
1545 GNUNET_CRYPTO_rsa_private_key_create (unsigned int len);
1549 * Free memory occupied by the private key.
1551 * @param key pointer to the memory to free
1554 GNUNET_CRYPTO_rsa_private_key_free (struct GNUNET_CRYPTO_rsa_PrivateKey *key);
1558 * Encode the private key in a format suitable for
1559 * storing it into a file.
1561 * @param key the private key
1562 * @param[out] buffer set to a buffer with the encoded key
1563 * @return size of memory allocatedin @a buffer
1566 GNUNET_CRYPTO_rsa_private_key_encode (const struct GNUNET_CRYPTO_rsa_PrivateKey *key,
1571 * Decode the private key from the data-format back
1572 * to the "normal", internal format.
1574 * @param buf the buffer where the private key data is stored
1575 * @param len the length of the data in @a buf
1576 * @return NULL on error
1578 struct GNUNET_CRYPTO_rsa_PrivateKey *
1579 GNUNET_CRYPTO_rsa_private_key_decode (const char *buf,
1584 * Extract the public key of the given private key.
1586 * @param priv the private key
1587 * @retur NULL on error, otherwise the public key
1589 struct GNUNET_CRYPTO_rsa_PublicKey *
1590 GNUNET_CRYPTO_rsa_private_key_get_public (const struct GNUNET_CRYPTO_rsa_PrivateKey *priv);
1594 * Compute hash over the public key.
1596 * @param key public key to hash
1597 * @param hc where to store the hash code
1600 GNUNET_CRYPTO_rsa_public_key_hash (const struct GNUNET_CRYPTO_rsa_PublicKey *key,
1601 struct GNUNET_HashCode *hc);
1605 * Free memory occupied by the public key.
1607 * @param key pointer to the memory to free
1610 GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_rsa_PublicKey *key);
1614 * Encode the public key in a format suitable for
1615 * storing it into a file.
1617 * @param key the private key
1618 * @param[out] buffer set to a buffer with the encoded key
1619 * @return size of memory allocated in @a buffer
1622 GNUNET_CRYPTO_rsa_public_key_encode (const struct GNUNET_CRYPTO_rsa_PublicKey *key,
1627 * Decode the public key from the data-format back
1628 * to the "normal", internal format.
1630 * @param buf the buffer where the public key data is stored
1631 * @param len the length of the data in @a buf
1632 * @return NULL on error
1634 struct GNUNET_CRYPTO_rsa_PublicKey *
1635 GNUNET_CRYPTO_rsa_public_key_decode (const char *buf,
1640 * Create a blinding key
1642 * @param len length of the key in bits (i.e. 2048)
1643 * @return the newly created blinding key
1645 struct GNUNET_CRYPTO_rsa_BlindingKey *
1646 GNUNET_CRYPTO_rsa_blinding_key_create (unsigned int len);
1650 * Destroy a blinding key
1652 * @param bkey the blinding key to destroy
1655 GNUNET_CRYPTO_rsa_blinding_key_free (struct GNUNET_CRYPTO_rsa_BlindingKey *bkey);
1659 * Encode the blinding key in a format suitable for
1660 * storing it into a file.
1662 * @param bkey the blinding key
1663 * @param[out] buffer set to a buffer with the encoded key
1664 * @return size of memory allocated in @a buffer
1667 GNUNET_CRYPTO_rsa_blinding_key_encode (const struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
1672 * Decode the blinding key from the data-format back
1673 * to the "normal", internal format.
1675 * @param buf the buffer where the public key data is stored
1676 * @param len the length of the data in @a buf
1677 * @return NULL on error
1679 struct GNUNET_CRYPTO_rsa_BlindingKey *
1680 GNUNET_CRYPTO_rsa_blinding_key_decode (const char *buf,
1685 * Blinds the given message with the given blinding key
1687 * @param hash hash of the message to sign
1688 * @param bkey the blinding key
1689 * @param pkey the public key of the signer
1690 * @param[out] buffer set to a buffer with the blinded message to be signed
1691 * @return number of bytes stored in @a buffer
1694 GNUNET_CRYPTO_rsa_blind (const struct GNUNET_HashCode *hash,
1695 struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
1696 struct GNUNET_CRYPTO_rsa_PublicKey *pkey,
1701 * Sign the given message.
1703 * @param key private key to use for the signing
1704 * @param msg the (blinded) message to sign
1705 * @param msg_len number of bytes in @a msg to sign
1706 * @return NULL on error, signature on success
1708 struct GNUNET_CRYPTO_rsa_Signature *
1709 GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_rsa_PrivateKey *key,
1715 * Free memory occupied by signature.
1717 * @param sig memory to freee
1720 GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_rsa_Signature *sig);
1724 * Encode the given signature in a format suitable for storing it into a file.
1726 * @param sig the signature
1727 * @param[out] buffer set to a buffer with the encoded key
1728 * @return size of memory allocated in @a buffer
1731 GNUNET_CRYPTO_rsa_signature_encode (const struct GNUNET_CRYPTO_rsa_Signature *sig,
1736 * Decode the signature from the data-format back to the "normal", internal
1739 * @param buf the buffer where the public key data is stored
1740 * @param len the length of the data in @a buf
1741 * @return NULL on error
1743 struct GNUNET_CRYPTO_rsa_Signature *
1744 GNUNET_CRYPTO_rsa_signature_decode (const char *buf,
1749 * Unblind a blind-signed signature. The signature should have been generated
1750 * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with
1751 * #GNUNET_CRYPTO_rsa_blind().
1753 * @param sig the signature made on the blinded signature purpose
1754 * @param bkey the blinding key used to blind the signature purpose
1755 * @param pkey the public key of the signer
1756 * @return unblinded signature on success, NULL on error
1758 struct GNUNET_CRYPTO_rsa_Signature *
1759 GNUNET_CRYPTO_rsa_unblind (struct GNUNET_CRYPTO_rsa_Signature *sig,
1760 struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
1761 struct GNUNET_CRYPTO_rsa_PublicKey *pkey);
1765 * Verify whether the given hash corresponds to the given signature and the
1766 * signature is valid with respect to the given public key.
1768 * @param hash the message to verify to match the @a sig
1769 * @param sig signature that is being validated
1770 * @param public_key public key of the signer
1771 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1774 GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash,
1775 const struct GNUNET_CRYPTO_rsa_Signature *sig,
1776 const struct GNUNET_CRYPTO_rsa_PublicKey *public_key);
1779 #if 0 /* keep Emacsens' auto-indent happy */
1787 /* ifndef GNUNET_CRYPTO_LIB_H */
1789 /* end of gnunet_crypto_lib.h */