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"
60 * We need this enum here, but not the entire scheduler API.
61 * For the full definition, see gnunet_scheduler_lib.h.
63 enum GNUNET_SCHEDULER_Priority;
66 * @brief A 512-bit hashcode
68 struct GNUNET_HashCode
70 uint32_t bits[512 / 8 / sizeof (uint32_t)]; /* = 16 */
75 * Maximum length of an ECC signature.
76 * Note: round up to multiple of 8 minus 2 for alignment.
78 #define GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH 126
82 * Desired quality level for random numbers.
85 enum GNUNET_CRYPTO_Quality
88 * No good quality of the operation is needed (i.e.,
89 * random numbers can be pseudo-random).
92 GNUNET_CRYPTO_QUALITY_WEAK,
95 * High-quality operations are desired.
98 GNUNET_CRYPTO_QUALITY_STRONG,
101 * Randomness for IVs etc. is required.
104 GNUNET_CRYPTO_QUALITY_NONCE
109 * @brief length of the sessionkey in bytes (256 BIT sessionkey)
111 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
114 * Length of a hash value
116 #define GNUNET_CRYPTO_HASH_LENGTH (512/8)
119 * How many characters (without 0-terminator) are our ASCII-encoded
120 * public keys (ECDSA/EDDSA/ECDHE).
122 #define GNUNET_CRYPTO_PKEY_ASCII_LENGTH 52
125 * @brief 0-terminated ASCII encoding of a struct GNUNET_HashCode.
127 struct GNUNET_CRYPTO_HashAsciiEncoded
129 unsigned char encoding[104];
133 GNUNET_NETWORK_STRUCT_BEGIN
137 * @brief header of what an ECC signature signs
138 * this must be followed by "size - 8" bytes of
139 * the actual signed data
141 struct GNUNET_CRYPTO_EccSignaturePurpose
144 * How many bytes does this signature sign?
145 * (including this purpose header); in network
148 uint32_t size GNUNET_PACKED;
151 * What does this signature vouch for? This
152 * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
153 * constant (from gnunet_signatures.h). In
154 * network byte order!
156 uint32_t purpose GNUNET_PACKED;
162 * @brief an ECC signature using EdDSA.
163 * See https://gnunet.org/ed25519
165 struct GNUNET_CRYPTO_EddsaSignature
171 unsigned char r[256 / 8];
176 unsigned char s[256 / 8];
183 * @brief an ECC signature using ECDSA
185 struct GNUNET_CRYPTO_EcdsaSignature
191 unsigned char r[256 / 8];
196 unsigned char s[256 / 8];
202 * Public ECC key (always for Curve25519) encoded in a format suitable
203 * for network transmission and EdDSA signatures.
205 struct GNUNET_CRYPTO_EddsaPublicKey
208 * Q consists of an x- and a y-value, each mod p (256 bits), given
209 * here in affine coordinates and Ed25519 standard compact format.
211 unsigned char q_y[256 / 8];
217 * Public ECC key (always for Curve25519) encoded in a format suitable
218 * for network transmission and ECDSA signatures.
220 struct GNUNET_CRYPTO_EcdsaPublicKey
223 * Q consists of an x- and a y-value, each mod p (256 bits), given
224 * here in affine coordinates and Ed25519 standard compact format.
226 unsigned char q_y[256 / 8];
232 * The identity of the host (wraps the signing key of the peer).
234 struct GNUNET_PeerIdentity
236 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
241 * Public ECC key (always for Curve25519) encoded in a format suitable
242 * for network transmission and encryption (ECDH),
243 * See http://cr.yp.to/ecdh.html
245 struct GNUNET_CRYPTO_EcdhePublicKey
248 * Q consists of an x- and a y-value, each mod p (256 bits), given
249 * here in affine coordinates and Ed25519 standard compact format.
251 unsigned char q_y[256 / 8];
256 * Private ECC key encoded for transmission. To be used only for ECDH
257 * key exchange (ECDHE to be precise).
259 struct GNUNET_CRYPTO_EcdhePrivateKey
262 * d is a value mod n, where n has at most 256 bits.
264 unsigned char d[256 / 8];
269 * Private ECC key encoded for transmission. To be used only for ECDSA
272 struct GNUNET_CRYPTO_EcdsaPrivateKey
275 * d is a value mod n, where n has at most 256 bits.
277 unsigned char d[256 / 8];
282 * Private ECC key encoded for transmission. To be used only for EdDSA
285 struct GNUNET_CRYPTO_EddsaPrivateKey
288 * d is a value mod n, where n has at most 256 bits.
290 unsigned char d[256 / 8];
296 * @brief type for session keys
298 struct GNUNET_CRYPTO_SymmetricSessionKey
301 * Actual key for AES.
303 unsigned char aes_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
306 * Actual key for TwoFish.
308 unsigned char twofish_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
312 GNUNET_NETWORK_STRUCT_END
315 * @brief IV for sym cipher
317 * NOTE: must be smaller (!) in size than the
318 * `struct GNUNET_HashCode`.
320 struct GNUNET_CRYPTO_SymmetricInitializationVector
322 unsigned char aes_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
324 unsigned char twofish_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
329 * @brief type for (message) authentication keys
331 struct GNUNET_CRYPTO_AuthKey
333 unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
338 * Size of paillier plain texts and public keys.
339 * Private keys and ciphertexts are twice this size.
341 #define GNUNET_CRYPTO_PAILLIER_BITS 2048
345 * Paillier public key.
347 struct GNUNET_CRYPTO_PaillierPublicKey
352 unsigned char n[GNUNET_CRYPTO_PAILLIER_BITS / 8];
357 * Paillier public key.
359 struct GNUNET_CRYPTO_PaillierPrivateKey
362 * Lambda-component of the private key.
364 unsigned char lambda[GNUNET_CRYPTO_PAILLIER_BITS / 8];
366 * Mu-component of the private key.
368 unsigned char mu[GNUNET_CRYPTO_PAILLIER_BITS / 8];
373 * Paillier ciphertext.
375 struct GNUNET_CRYPTO_PaillierCiphertext
378 * Guaranteed minimum number of homomorphic operations with this ciphertext,
379 * in network byte order (NBO).
381 int32_t remaining_ops GNUNET_PACKED;
384 * The bits of the ciphertext.
386 unsigned char bits[GNUNET_CRYPTO_PAILLIER_BITS * 2 / 8];
390 /* **************** Functions and Macros ************* */
394 * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator
397 * @param seed the seed to use
400 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
404 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
406 * @param sum current sum, initially 0
407 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
408 * @param len number of bytes in @a buf, must be multiple of 2
409 * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish to get actual crc16)
412 GNUNET_CRYPTO_crc16_step (uint32_t sum,
418 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
420 * @param sum cummulative sum
421 * @return crc16 value
424 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
429 * Calculate the checksum of a buffer in one step.
431 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
432 * @param len number of bytes in @a buf, must be multiple of 2
433 * @return crc16 value
436 GNUNET_CRYPTO_crc16_n (const void *buf,
442 * Compute the CRC32 checksum for the first len
443 * bytes of the buffer.
445 * @param buf the data over which we're taking the CRC
446 * @param len the length of the buffer @a buf in bytes
447 * @return the resulting CRC32 checksum
450 GNUNET_CRYPTO_crc32_n (const void *buf,
456 * Fill block with a random values.
458 * @param mode desired quality of the random number
459 * @param buffer the buffer to fill
460 * @param length buffer length
463 GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode,
469 * Produce a random value.
471 * @param mode desired quality of the random number
472 * @param i the upper limit (exclusive) for the random number
473 * @return a random value in the interval [0,@a i) (exclusive).
476 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode,
482 * Random on unsigned 64-bit values.
484 * @param mode desired quality of the random number
485 * @param max value returned will be in range [0,@a max) (exclusive)
486 * @return random 64-bit number
489 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
495 * Get an array with a random permutation of the
497 * @param mode #GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used,
498 * #GNUNET_CRYPTO_QUALITY_WEAK or #GNUNET_CRYPTO_QUALITY_NONCE otherwise
499 * @param n the size of the array
500 * @return the permutation array (allocated from heap)
503 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode,
509 * Create a new random session key.
511 * @param key key to initialize
514 GNUNET_CRYPTO_symmetric_create_session_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key);
519 * Encrypt a block using a symmetric sessionkey.
521 * @param block the block to encrypt
522 * @param size the size of the @a block
523 * @param sessionkey the key used to encrypt
524 * @param iv the initialization vector to use, use INITVALUE
526 * @return the size of the encrypted block, -1 for errors
529 GNUNET_CRYPTO_symmetric_encrypt (const void *block,
531 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
532 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
538 * Decrypt a given block using a symmetric sessionkey.
540 * @param block the data to decrypt, encoded as returned by encrypt
541 * @param size how big is the block?
542 * @param sessionkey the key used to decrypt
543 * @param iv the initialization vector to use
544 * @param result address to store the result at
545 * @return -1 on failure, size of decrypted block on success
548 GNUNET_CRYPTO_symmetric_decrypt (const void *block,
550 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
551 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
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 ... pairs of void * & size_t for context chunks, terminated by NULL
565 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
566 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
568 size_t salt_len, ...);
572 * @brief Derive an IV
573 * @param iv initialization vector
574 * @param skey session key
575 * @param salt salt for the derivation
576 * @param salt_len size of the @a salt
577 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
580 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
581 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
589 * Convert hash to ASCII encoding.
590 * @param block the hash code
591 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
592 * safely cast to char*, a '\\0' termination is set).
595 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block,
596 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
601 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
603 * @param enc the encoding
604 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
605 * @param result where to store the hash code
606 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
609 GNUNET_CRYPTO_hash_from_string2 (const char *enc,
611 struct GNUNET_HashCode *result);
616 * Convert ASCII encoding back to `struct GNUNET_HashCode`
618 * @param enc the encoding
619 * @param result where to store the hash code
620 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
622 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
623 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
629 * Compute the distance between 2 hashcodes. The
630 * computation must be fast, not involve @a a[0] or @a a[4] (they're used
631 * elsewhere), and be somewhat consistent. And of course, the result
632 * should be a positive number.
634 * @param a some hash code
635 * @param b some hash code
636 * @return number between 0 and UINT32_MAX
639 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a,
640 const struct GNUNET_HashCode *b);
645 * Compute hash of a given block.
647 * @param block the data to hash
648 * @param size size of the @a block
649 * @param ret pointer to where to write the hashcode
652 GNUNET_CRYPTO_hash (const void *block,
654 struct GNUNET_HashCode *ret);
658 * Context for cummulative hashing.
660 struct GNUNET_HashContext;
664 * Start incremental hashing operation.
666 * @return context for incremental hash computation
668 struct GNUNET_HashContext *
669 GNUNET_CRYPTO_hash_context_start (void);
673 * Add data to be hashed.
675 * @param hc cummulative hash context
676 * @param buf data to add
677 * @param size number of bytes in @a buf
680 GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
686 * Finish the hash computation.
688 * @param hc hash context to use, is freed in the process
689 * @param r_hash where to write the latest / final hash code
692 GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
693 struct GNUNET_HashCode *r_hash);
697 * Abort hashing, do not bother calculating final result.
699 * @param hc hash context to destroy
702 GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc);
707 * Calculate HMAC of a message (RFC 2104)
709 * @param key secret key
710 * @param plaintext input plaintext
711 * @param plaintext_len length of @a plaintext
712 * @param hmac where to store the hmac
715 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
716 const void *plaintext,
717 size_t plaintext_len,
718 struct GNUNET_HashCode *hmac);
722 * Function called once the hash computation over the
723 * specified file has completed.
726 * @param res resulting hash, NULL on error
729 (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
730 const struct GNUNET_HashCode *res);
734 * Handle to file hashing operation.
736 struct GNUNET_CRYPTO_FileHashContext;
741 * Compute the hash of an entire file.
743 * @param priority scheduling priority to use
744 * @param filename name of file to hash
745 * @param blocksize number of bytes to process in one task
746 * @param callback function to call upon completion
747 * @param callback_cls closure for @a callback
748 * @return NULL on (immediate) errror
750 struct GNUNET_CRYPTO_FileHashContext *
751 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
752 const char *filename,
754 GNUNET_CRYPTO_HashCompletedCallback callback,
759 * Cancel a file hashing operation.
761 * @param fhc operation to cancel (callback must not yet have been invoked)
764 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
769 * Create a random hash code.
771 * @param mode desired quality level
772 * @param result hash code that is randomized
775 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
776 struct GNUNET_HashCode *result);
781 * compute @a result = @a b - @a a
783 * @param a some hash code
784 * @param b some hash code
785 * @param result set to @a b - @a a
788 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
789 const struct GNUNET_HashCode *b,
790 struct GNUNET_HashCode *result);
795 * compute @a result = @a a + @a delta
797 * @param a some hash code
798 * @param delta some hash code
799 * @param result set to @a a + @a delta
802 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a,
803 const struct GNUNET_HashCode *delta,
804 struct GNUNET_HashCode *result);
809 * compute result = a ^ b
811 * @param a some hash code
812 * @param b some hash code
813 * @param result set to @a a ^ @a b
816 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
817 const struct GNUNET_HashCode *b,
818 struct GNUNET_HashCode *result);
823 * Convert a hashcode into a key.
825 * @param hc hash code that serves to generate the key
826 * @param skey set to a valid session key
827 * @param iv set to a valid initialization vector
830 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
831 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
832 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv);
837 * Obtain a bit from a hashcode.
839 * @param code the `struct GNUNET_HashCode` to index bit-wise
840 * @param bit index into the hashcode, [0...159]
841 * @return Bit \a bit from hashcode \a code, -1 for invalid index
844 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code,
850 * Determine how many low order bits match in two
851 * `struct GNUNET_HashCodes`. i.e. - 010011 and 011111 share
852 * the first two lowest order bits, and therefore the
853 * return value is two (NOT XOR distance, nor how many
854 * bits match absolutely!).
856 * @param first the first hashcode
857 * @param second the hashcode to compare first to
858 * @return the number of bits that match
861 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
862 const struct GNUNET_HashCode *second);
867 * Compare function for HashCodes, producing a total ordering
870 * @param h1 some hash code
871 * @param h2 some hash code
872 * @return 1 if @a h1 > @a h2, -1 if @a h1 < @a h2 and 0 if @a h1 == @a h2.
875 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
876 const struct GNUNET_HashCode *h2);
881 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
882 * in the XOR metric (Kademlia).
884 * @param h1 some hash code
885 * @param h2 some hash code
886 * @param target some hash code
887 * @return -1 if @a h1 is closer, 1 if @a h2 is closer and 0 if @a h1== @a h2.
890 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
891 const struct GNUNET_HashCode *h2,
892 const struct GNUNET_HashCode *target);
897 * @brief Derive an authentication key
898 * @param key authentication key
899 * @param rkey root key
901 * @param salt_len size of the salt
902 * @param argp pair of void * & size_t for context chunks, terminated by NULL
905 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
906 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
907 const void *salt, size_t salt_len,
913 * @brief Derive an authentication key
914 * @param key authentication key
915 * @param rkey root key
917 * @param salt_len size of the salt
918 * @param ... pair of void * & size_t for context chunks, terminated by NULL
921 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
922 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
923 const void *salt, size_t salt_len,
930 * @param result buffer for the derived key, allocated by caller
931 * @param out_len desired length of the derived key
932 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
933 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
935 * @param xts_len length of @a xts
936 * @param skm source key material
937 * @param skm_len length of @a skm
938 * @param ... pair of void * & size_t for context chunks, terminated by NULL
939 * @return #GNUNET_YES on success
942 GNUNET_CRYPTO_hkdf (void *result,
956 * @param result buffer for the derived key, allocated by caller
957 * @param out_len desired length of the derived key
958 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
959 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
961 * @param xts_len length of @a xts
962 * @param skm source key material
963 * @param skm_len length of @a skm
964 * @param argp va_list of void * & size_t pairs for context chunks
965 * @return #GNUNET_YES on success
968 GNUNET_CRYPTO_hkdf_v (void *result,
981 * @param result buffer for the derived key, allocated by caller
982 * @param out_len desired length of the derived key
984 * @param xts_len length of @a xts
985 * @param skm source key material
986 * @param skm_len length of @a skm
987 * @param argp va_list of void * & size_t pairs for context chunks
988 * @return #GNUNET_YES on success
991 GNUNET_CRYPTO_kdf_v (void *result,
1003 * @param result buffer for the derived key, allocated by caller
1004 * @param out_len desired length of the derived key
1006 * @param xts_len length of @a xts
1007 * @param skm source key material
1008 * @param skm_len length of @a skm
1009 * @param ... void * & size_t pairs for context chunks
1010 * @return #GNUNET_YES on success
1013 GNUNET_CRYPTO_kdf (void *result,
1024 * Extract the public key for the given private key.
1026 * @param priv the private key
1027 * @param pub where to write the public key
1030 GNUNET_CRYPTO_ecdsa_key_get_public (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1031 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1035 * Extract the public key for the given private key.
1037 * @param priv the private key
1038 * @param pub where to write the public key
1041 GNUNET_CRYPTO_eddsa_key_get_public (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1042 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1048 * Extract the public key for the given private key.
1050 * @param priv the private key
1051 * @param pub where to write the public key
1054 GNUNET_CRYPTO_ecdhe_key_get_public (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1055 struct GNUNET_CRYPTO_EcdhePublicKey *pub);
1059 * Convert a public key to a string.
1061 * @param pub key to convert
1062 * @return string representing @a pub
1065 GNUNET_CRYPTO_ecdsa_public_key_to_string (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1069 * Convert a public key to a string.
1071 * @param pub key to convert
1072 * @return string representing @a pub
1075 GNUNET_CRYPTO_eddsa_public_key_to_string (const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1079 * Convert a string representing a public key to a public key.
1081 * @param enc encoded public key
1082 * @param enclen number of bytes in @a enc (without 0-terminator)
1083 * @param pub where to store the public key
1084 * @return #GNUNET_OK on success
1087 GNUNET_CRYPTO_ecdsa_public_key_from_string (const char *enc,
1089 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1093 * Convert a string representing a private key to a private key.
1095 * @param enc encoded public key
1096 * @param enclen number of bytes in @a enc (without 0-terminator)
1097 * @param priv where to store the private key
1098 * @return #GNUNET_OK on success
1101 GNUNET_CRYPTO_eddsa_private_key_from_string (const char *enc,
1103 struct GNUNET_CRYPTO_EddsaPrivateKey *pub);
1107 * Convert a string representing a public key to a public key.
1109 * @param enc encoded public key
1110 * @param enclen number of bytes in @a enc (without 0-terminator)
1111 * @param pub where to store the public key
1112 * @return #GNUNET_OK on success
1115 GNUNET_CRYPTO_eddsa_public_key_from_string (const char *enc,
1117 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1122 * Create a new private key by reading it from a file. If the
1123 * files does not exist, create a new key and write it to the
1124 * file. Caller must free return value. Note that this function
1125 * can not guarantee that another process might not be trying
1126 * the same operation on the same file at the same time.
1127 * If the contents of the file
1128 * are invalid the old file is deleted and a fresh key is
1131 * @param filename name of file to use to store the key
1132 * @return new private key, NULL on error (for example,
1133 * permission denied); free using #GNUNET_free
1135 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1136 GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename);
1141 * Create a new private key by reading it from a file. If the
1142 * files does not exist, create a new key and write it to the
1143 * file. Caller must free return value. Note that this function
1144 * can not guarantee that another process might not be trying
1145 * the same operation on the same file at the same time.
1146 * If the contents of the file
1147 * are invalid the old file is deleted and a fresh key is
1150 * @param filename name of file to use to store the key
1151 * @return new private key, NULL on error (for example,
1152 * permission denied); free using #GNUNET_free
1154 struct GNUNET_CRYPTO_EddsaPrivateKey *
1155 GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename);
1159 * Forward declaration to simplify #include-structure.
1161 struct GNUNET_CONFIGURATION_Handle;
1166 * Create a new private key by reading our peer's key from
1167 * the file specified in the configuration.
1169 * @param cfg the configuration to use
1170 * @return new private key, NULL on error (for example,
1171 * permission denied); free using #GNUNET_free
1173 struct GNUNET_CRYPTO_EddsaPrivateKey *
1174 GNUNET_CRYPTO_eddsa_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
1179 * Create a new private key. Caller must free return value.
1181 * @return fresh private key; free using #GNUNET_free
1183 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1184 GNUNET_CRYPTO_ecdsa_key_create (void);
1189 * Create a new private key. Caller must free return value.
1191 * @return fresh private key; free using #GNUNET_free
1193 struct GNUNET_CRYPTO_EddsaPrivateKey *
1194 GNUNET_CRYPTO_eddsa_key_create (void);
1199 * Create a new private key. Caller must free return value.
1201 * @return fresh private key; free using #GNUNET_free
1203 struct GNUNET_CRYPTO_EcdhePrivateKey *
1204 GNUNET_CRYPTO_ecdhe_key_create (void);
1209 * Clear memory that was used to store a private key.
1211 * @param pk location of the key
1214 GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk);
1219 * Clear memory that was used to store a private key.
1221 * @param pk location of the key
1224 GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk);
1229 * Clear memory that was used to store a private key.
1231 * @param pk location of the key
1234 GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1239 * Get the shared private key we use for anonymous users.
1241 * @return "anonymous" private key; do not free
1243 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
1244 GNUNET_CRYPTO_ecdsa_key_get_anonymous (void);
1249 * Setup a hostkey file for a peer given the name of the
1250 * configuration file (!). This function is used so that
1251 * at a later point code can be certain that reading a
1252 * hostkey is fast (for example in time-dependent testcases).
1254 * @param cfg_name name of the configuration file to use
1257 GNUNET_CRYPTO_eddsa_setup_hostkey (const char *cfg_name);
1262 * Retrieve the identity of the host's peer.
1264 * @param cfg configuration to use
1265 * @param dst pointer to where to write the peer identity
1266 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
1267 * could not be retrieved
1270 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
1271 struct GNUNET_PeerIdentity *dst);
1275 * Compare two Peer Identities.
1277 * @param first first peer identity
1278 * @param second second peer identity
1279 * @return bigger than 0 if first > second,
1280 * 0 if they are the same
1281 * smaller than 0 if second > first
1284 GNUNET_CRYPTO_cmp_peer_identity (const struct GNUNET_PeerIdentity *first,
1285 const struct GNUNET_PeerIdentity *second);
1290 * Derive key material from a public and a private ECC key.
1292 * @param priv private key to use for the ECDH (x)
1293 * @param pub public key to use for the ECDH (yG)
1294 * @param key_material where to write the key material (xyG)
1295 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1298 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1299 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1300 struct GNUNET_HashCode *key_material);
1305 * Derive key material from a ECDH public key and a private EdDSA key.
1306 * Dual to #GNUNET_CRRYPTO_ecdh_eddsa.
1308 * @param priv private key from EdDSA to use for the ECDH (x)
1309 * @param pub public key to use for the ECDH (yG)
1310 * @param key_material where to write the key material H(h(x)yG)
1311 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1314 GNUNET_CRYPTO_eddsa_ecdh (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1315 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1316 struct GNUNET_HashCode *key_material);
1321 * Derive key material from a EdDSA public key and a private ECDH key.
1322 * Dual to #GNUNET_CRRYPTO_eddsa_ecdh.
1324 * @param priv private key to use for the ECDH (y)
1325 * @param pub public key from EdDSA to use for the ECDH (X=h(x)G)
1326 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1327 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1330 GNUNET_CRYPTO_ecdh_eddsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1331 const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
1332 struct GNUNET_HashCode *key_material);
1337 * EdDSA sign a given block.
1339 * @param priv private key to use for the signing
1340 * @param purpose what to sign (size, purpose)
1341 * @param sig where to write the signature
1342 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1345 GNUNET_CRYPTO_eddsa_sign (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1346 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1347 struct GNUNET_CRYPTO_EddsaSignature *sig);
1352 * ECDSA Sign a given block.
1354 * @param priv private key to use for the signing
1355 * @param purpose what to sign (size, purpose)
1356 * @param sig where to write the signature
1357 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1360 GNUNET_CRYPTO_ecdsa_sign (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1361 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1362 struct GNUNET_CRYPTO_EcdsaSignature *sig);
1366 * Verify EdDSA signature.
1368 * @param purpose what is the purpose that the signature should have?
1369 * @param validate block to validate (size, purpose, data)
1370 * @param sig signature that is being validated
1371 * @param pub public key of the signer
1372 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1375 GNUNET_CRYPTO_eddsa_verify (uint32_t purpose,
1376 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1377 const struct GNUNET_CRYPTO_EddsaSignature *sig,
1378 const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1384 * Verify ECDSA signature.
1386 * @param purpose what is the purpose that the signature should have?
1387 * @param validate block to validate (size, purpose, data)
1388 * @param sig signature that is being validated
1389 * @param pub public key of the signer
1390 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1393 GNUNET_CRYPTO_ecdsa_verify (uint32_t purpose,
1394 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1395 const struct GNUNET_CRYPTO_EcdsaSignature *sig,
1396 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1401 * Derive a private key from a given private key and a label.
1402 * Essentially calculates a private key 'h = H(l,P) * d mod n'
1403 * where n is the size of the ECC group and P is the public
1404 * key associated with the private key 'd'.
1406 * @param priv original private key
1407 * @param label label to use for key deriviation
1408 * @param context additional context to use for HKDF of 'h';
1409 * typically the name of the subsystem/application
1410 * @return derived private key
1412 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1413 GNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1415 const char *context);
1420 * Derive a public key from a given public key and a label.
1421 * Essentially calculates a public key 'V = H(l,P) * P'.
1423 * @param pub original public key
1424 * @param label label to use for key deriviation
1425 * @param context additional context to use for HKDF of 'h'.
1426 * typically the name of the subsystem/application
1427 * @param result where to write the derived public key
1430 GNUNET_CRYPTO_ecdsa_public_key_derive (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1432 const char *context,
1433 struct GNUNET_CRYPTO_EcdsaPublicKey *result);
1437 * Output the given MPI value to the given buffer in network
1438 * byte order. The MPI @a val may not be negative.
1440 * @param buf where to output to
1441 * @param size number of bytes in @a buf
1442 * @param val value to write to @a buf
1445 GNUNET_CRYPTO_mpi_print_unsigned (void *buf,
1451 * Convert data buffer into MPI value.
1452 * The buffer is interpreted as network
1453 * byte order, unsigned integer.
1455 * @param result where to store MPI value (allocated)
1456 * @param data raw data (GCRYMPI_FMT_USG)
1457 * @param size number of bytes in @a data
1460 GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result,
1466 * Create a freshly generated paillier public key.
1468 * @param[out] public_key Where to store the public key?
1469 * @param[out] private_key Where to store the private key?
1472 GNUNET_CRYPTO_paillier_create (struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1473 struct GNUNET_CRYPTO_PaillierPrivateKey *private_key);
1477 * Encrypt a plaintext with a paillier public key.
1479 * @param public_key Public key to use.
1480 * @param m Plaintext to encrypt.
1481 * @param desired_ops How many homomorphic ops the caller intends to use
1482 * @param[out] ciphertext Encrytion of @a plaintext with @a public_key.
1483 * @return guaranteed number of supported homomorphic operations >= 1,
1484 * or desired_ops, in case that is lower,
1485 * or -1 if less than one homomorphic operation is possible
1488 GNUNET_CRYPTO_paillier_encrypt (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1491 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext);
1495 * Decrypt a paillier ciphertext with a private key.
1497 * @param private_key Private key to use for decryption.
1498 * @param public_key Public key to use for decryption.
1499 * @param ciphertext Ciphertext to decrypt.
1500 * @param[out] m Decryption of @a ciphertext with @private_key.
1503 GNUNET_CRYPTO_paillier_decrypt (const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key,
1504 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1505 const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
1510 * Compute a ciphertext that represents the sum of the plaintext in @a x1 and @a x2
1512 * Note that this operation can only be done a finite number of times
1513 * before an overflow occurs.
1515 * @param public_key Public key to use for encryption.
1516 * @param c1 Paillier cipher text.
1517 * @param c2 Paillier cipher text.
1518 * @param[out] result Result of the homomorphic operation.
1519 * @return #GNUNET_OK if the result could be computed,
1520 * #GNUNET_SYSERR if no more homomorphic operations are remaining.
1523 GNUNET_CRYPTO_paillier_hom_add (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1524 const struct GNUNET_CRYPTO_PaillierCiphertext *c1,
1525 const struct GNUNET_CRYPTO_PaillierCiphertext *c2,
1526 struct GNUNET_CRYPTO_PaillierCiphertext *result);
1530 * Get the number of remaining supported homomorphic operations.
1532 * @param c Paillier cipher text.
1533 * @return the number of remaining homomorphic operations
1536 GNUNET_CRYPTO_paillier_hom_get_remaining (const struct GNUNET_CRYPTO_PaillierCiphertext *c);
1539 /* ********* Chaum-style RSA-based blind signatures ******************* */
1545 * The private information of an RSA key pair.
1547 struct GNUNET_CRYPTO_rsa_PrivateKey;
1550 * The public information of an RSA key pair.
1552 struct GNUNET_CRYPTO_rsa_PublicKey;
1555 * Key used to blind a message
1557 struct GNUNET_CRYPTO_rsa_BlindingKey;
1560 * @brief an RSA signature
1562 struct GNUNET_CRYPTO_rsa_Signature;
1566 * Create a new private key. Caller must free return value.
1568 * @param len length of the key in bits (i.e. 2048)
1569 * @return fresh private key
1571 struct GNUNET_CRYPTO_rsa_PrivateKey *
1572 GNUNET_CRYPTO_rsa_private_key_create (unsigned int len);
1576 * Free memory occupied by the private key.
1578 * @param key pointer to the memory to free
1581 GNUNET_CRYPTO_rsa_private_key_free (struct GNUNET_CRYPTO_rsa_PrivateKey *key);
1585 * Encode the private key in a format suitable for
1586 * storing it into a file.
1588 * @param key the private key
1589 * @param[out] buffer set to a buffer with the encoded key
1590 * @return size of memory allocatedin @a buffer
1593 GNUNET_CRYPTO_rsa_private_key_encode (const struct GNUNET_CRYPTO_rsa_PrivateKey *key,
1598 * Decode the private key from the data-format back
1599 * to the "normal", internal format.
1601 * @param buf the buffer where the private key data is stored
1602 * @param len the length of the data in @a buf
1603 * @return NULL on error
1605 struct GNUNET_CRYPTO_rsa_PrivateKey *
1606 GNUNET_CRYPTO_rsa_private_key_decode (const char *buf,
1611 * Extract the public key of the given private key.
1613 * @param priv the private key
1614 * @retur NULL on error, otherwise the public key
1616 struct GNUNET_CRYPTO_rsa_PublicKey *
1617 GNUNET_CRYPTO_rsa_private_key_get_public (const struct GNUNET_CRYPTO_rsa_PrivateKey *priv);
1621 * Compute hash over the public key.
1623 * @param key public key to hash
1624 * @param hc where to store the hash code
1627 GNUNET_CRYPTO_rsa_public_key_hash (const struct GNUNET_CRYPTO_rsa_PublicKey *key,
1628 struct GNUNET_HashCode *hc);
1632 * Free memory occupied by the public key.
1634 * @param key pointer to the memory to free
1637 GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_rsa_PublicKey *key);
1641 * Encode the public key in a format suitable for
1642 * storing it into a file.
1644 * @param key the private key
1645 * @param[out] buffer set to a buffer with the encoded key
1646 * @return size of memory allocated in @a buffer
1649 GNUNET_CRYPTO_rsa_public_key_encode (const struct GNUNET_CRYPTO_rsa_PublicKey *key,
1654 * Decode the public key from the data-format back
1655 * to the "normal", internal format.
1657 * @param buf the buffer where the public key data is stored
1658 * @param len the length of the data in @a buf
1659 * @return NULL on error
1661 struct GNUNET_CRYPTO_rsa_PublicKey *
1662 GNUNET_CRYPTO_rsa_public_key_decode (const char *buf,
1667 * Duplicate the given public key
1669 * @param key the public key to duplicate
1670 * @return the duplicate key; NULL upon error
1672 struct GNUNET_CRYPTO_rsa_PublicKey *
1673 GNUNET_CRYPTO_rsa_public_key_dup (const struct GNUNET_CRYPTO_rsa_PublicKey *key);
1677 * Create a blinding key
1679 * @param len length of the key in bits (i.e. 2048)
1680 * @return the newly created blinding key
1682 struct GNUNET_CRYPTO_rsa_BlindingKey *
1683 GNUNET_CRYPTO_rsa_blinding_key_create (unsigned int len);
1687 * Compare the values of two blinding keys.
1690 * @param b2 the other key
1691 * @return 0 if the two are equal
1694 GNUNET_CRYPTO_rsa_blinding_key_cmp (struct GNUNET_CRYPTO_rsa_BlindingKey *b1,
1695 struct GNUNET_CRYPTO_rsa_BlindingKey *b2);
1699 * Compare the values of two signatures.
1701 * @param s1 one signature
1702 * @param s2 the other signature
1703 * @return 0 if the two are equal
1706 GNUNET_CRYPTO_rsa_signature_cmp (struct GNUNET_CRYPTO_rsa_Signature *s1,
1707 struct GNUNET_CRYPTO_rsa_Signature *s2);
1711 * Compare the values of two public keys.
1713 * @param p1 one public key
1714 * @param p2 the other public key
1715 * @return 0 if the two are equal
1718 GNUNET_CRYPTO_rsa_public_key_cmp (struct GNUNET_CRYPTO_rsa_PublicKey *p1,
1719 struct GNUNET_CRYPTO_rsa_PublicKey *p2);
1723 * Destroy a blinding key
1725 * @param bkey the blinding key to destroy
1728 GNUNET_CRYPTO_rsa_blinding_key_free (struct GNUNET_CRYPTO_rsa_BlindingKey *bkey);
1732 * Encode the blinding key in a format suitable for
1733 * storing it into a file.
1735 * @param bkey the blinding key
1736 * @param[out] buffer set to a buffer with the encoded key
1737 * @return size of memory allocated in @a buffer
1740 GNUNET_CRYPTO_rsa_blinding_key_encode (const struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
1745 * Decode the blinding key from the data-format back
1746 * to the "normal", internal format.
1748 * @param buf the buffer where the public key data is stored
1749 * @param len the length of the data in @a buf
1750 * @return NULL on error
1752 struct GNUNET_CRYPTO_rsa_BlindingKey *
1753 GNUNET_CRYPTO_rsa_blinding_key_decode (const char *buf,
1758 * Blinds the given message with the given blinding key
1760 * @param hash hash of the message to sign
1761 * @param bkey the blinding key
1762 * @param pkey the public key of the signer
1763 * @param[out] buffer set to a buffer with the blinded message to be signed
1764 * @return number of bytes stored in @a buffer
1767 GNUNET_CRYPTO_rsa_blind (const struct GNUNET_HashCode *hash,
1768 struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
1769 struct GNUNET_CRYPTO_rsa_PublicKey *pkey,
1774 * Sign the given message.
1776 * @param key private key to use for the signing
1777 * @param msg the (blinded) message to sign
1778 * @param msg_len number of bytes in @a msg to sign
1779 * @return NULL on error, signature on success
1781 struct GNUNET_CRYPTO_rsa_Signature *
1782 GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_rsa_PrivateKey *key,
1788 * Free memory occupied by signature.
1790 * @param sig memory to free
1793 GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_rsa_Signature *sig);
1797 * Encode the given signature in a format suitable for storing it into a file.
1799 * @param sig the signature
1800 * @param[out] buffer set to a buffer with the encoded key
1801 * @return size of memory allocated in @a buffer
1804 GNUNET_CRYPTO_rsa_signature_encode (const struct GNUNET_CRYPTO_rsa_Signature *sig,
1809 * Decode the signature from the data-format back to the "normal", internal
1812 * @param buf the buffer where the public key data is stored
1813 * @param len the length of the data in @a buf
1814 * @return NULL on error
1816 struct GNUNET_CRYPTO_rsa_Signature *
1817 GNUNET_CRYPTO_rsa_signature_decode (const char *buf,
1822 * Unblind a blind-signed signature. The signature should have been generated
1823 * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with
1824 * #GNUNET_CRYPTO_rsa_blind().
1826 * @param sig the signature made on the blinded signature purpose
1827 * @param bkey the blinding key used to blind the signature purpose
1828 * @param pkey the public key of the signer
1829 * @return unblinded signature on success, NULL on error
1831 struct GNUNET_CRYPTO_rsa_Signature *
1832 GNUNET_CRYPTO_rsa_unblind (struct GNUNET_CRYPTO_rsa_Signature *sig,
1833 struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
1834 struct GNUNET_CRYPTO_rsa_PublicKey *pkey);
1838 * Verify whether the given hash corresponds to the given signature and the
1839 * signature is valid with respect to the given public key.
1841 * @param hash the message to verify to match the @a sig
1842 * @param sig signature that is being validated
1843 * @param public_key public key of the signer
1844 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1847 GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash,
1848 const struct GNUNET_CRYPTO_rsa_Signature *sig,
1849 const struct GNUNET_CRYPTO_rsa_PublicKey *public_key);
1852 #if 0 /* keep Emacsens' auto-indent happy */
1860 /* ifndef GNUNET_CRYPTO_LIB_H */
1862 /* end of gnunet_crypto_lib.h */