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, const void *buf, size_t len);
412 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
414 * @param sum cummulative sum
415 * @return crc16 value
418 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
423 * Calculate the checksum of a buffer in one step.
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
430 GNUNET_CRYPTO_crc16_n (const void *buf, size_t len);
435 * Compute the CRC32 checksum for the first len
436 * bytes of the buffer.
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
443 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len);
448 * Fill block with a random values.
450 * @param mode desired quality of the random number
451 * @param buffer the buffer to fill
452 * @param length buffer length
455 GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length);
459 * Produce a random value.
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).
466 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i);
471 * Random on unsigned 64-bit values.
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
478 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max);
483 * Get an array with a random permutation of the
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)
491 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
496 * Create a new random session key.
498 * @param key key to initialize
501 GNUNET_CRYPTO_symmetric_create_session_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key);
506 * Encrypt a block using a symmetric sessionkey.
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
513 * @return the size of the encrypted block, -1 for errors
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,
524 * Decrypt a given block using a symmetric sessionkey.
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
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,
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
550 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
551 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
553 size_t salt_len, ...);
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
565 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
566 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
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).
580 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode * block,
581 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
586 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
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
594 GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
595 struct GNUNET_HashCode *result);
600 * Convert ASCII encoding back to `struct GNUNET_HashCode`
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
606 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
607 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
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.
618 * @param a some hash code
619 * @param b some hash code
620 * @return number between 0 and UINT32_MAX
623 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a,
624 const struct GNUNET_HashCode *b);
629 * Compute hash of a given block.
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
636 GNUNET_CRYPTO_hash (const void *block,
638 struct GNUNET_HashCode *ret);
642 * Context for cummulative hashing.
644 struct GNUNET_HashContext;
648 * Start incremental hashing operation.
650 * @return context for incremental hash computation
652 struct GNUNET_HashContext *
653 GNUNET_CRYPTO_hash_context_start (void);
657 * Add data to be hashed.
659 * @param hc cummulative hash context
660 * @param buf data to add
661 * @param size number of bytes in @a buf
664 GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
670 * Finish the hash computation.
672 * @param hc hash context to use, is freed in the process
673 * @param r_hash where to write the latest / final hash code
676 GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
677 struct GNUNET_HashCode *r_hash);
681 * Abort hashing, do not bother calculating final result.
683 * @param hc hash context to destroy
686 GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc);
691 * Calculate HMAC of a message (RFC 2104)
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
699 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
700 const void *plaintext, size_t plaintext_len,
701 struct GNUNET_HashCode * hmac);
705 * Function called once the hash computation over the
706 * specified file has completed.
709 * @param res resulting hash, NULL on error
711 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
712 const struct GNUNET_HashCode *res);
716 * Handle to file hashing operation.
718 struct GNUNET_CRYPTO_FileHashContext;
723 * Compute the hash of an entire file.
725 * @param priority scheduling priority to use
726 * @param filename name of file to hash
727 * @param blocksize number of bytes to process in one task
728 * @param callback function to call upon completion
729 * @param callback_cls closure for @a callback
730 * @return NULL on (immediate) errror
732 struct GNUNET_CRYPTO_FileHashContext *
733 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
734 const char *filename, size_t blocksize,
735 GNUNET_CRYPTO_HashCompletedCallback callback,
740 * Cancel a file hashing operation.
742 * @param fhc operation to cancel (callback must not yet have been invoked)
745 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
750 * Create a random hash code.
752 * @param mode desired quality level
753 * @param result hash code that is randomized
756 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
757 struct GNUNET_HashCode *result);
762 * compute @a result = @a b - @a a
764 * @param a some hash code
765 * @param b some hash code
766 * @param result set to @a b - @a a
769 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
770 const struct GNUNET_HashCode *b,
771 struct GNUNET_HashCode *result);
776 * compute @a result = @a a + @a delta
778 * @param a some hash code
779 * @param delta some hash code
780 * @param result set to @a a + @a delta
783 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a,
784 const struct GNUNET_HashCode *delta,
785 struct GNUNET_HashCode *result);
790 * compute result = a ^ b
792 * @param a some hash code
793 * @param b some hash code
794 * @param result set to @a a ^ @a b
797 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
798 const struct GNUNET_HashCode *b,
799 struct GNUNET_HashCode *result);
804 * Convert a hashcode into a key.
806 * @param hc hash code that serves to generate the key
807 * @param skey set to a valid session key
808 * @param iv set to a valid initialization vector
811 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
812 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
813 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv);
818 * Obtain a bit from a hashcode.
820 * @param code the `struct GNUNET_HashCode` to index bit-wise
821 * @param bit index into the hashcode, [0...159]
822 * @return Bit \a bit from hashcode \a code, -1 for invalid index
825 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code,
831 * Determine how many low order bits match in two
832 * `struct GNUNET_HashCodes`. i.e. - 010011 and 011111 share
833 * the first two lowest order bits, and therefore the
834 * return value is two (NOT XOR distance, nor how many
835 * bits match absolutely!).
837 * @param first the first hashcode
838 * @param second the hashcode to compare first to
839 * @return the number of bits that match
842 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
843 const struct GNUNET_HashCode *second);
848 * Compare function for HashCodes, producing a total ordering
851 * @param h1 some hash code
852 * @param h2 some hash code
853 * @return 1 if @a h1 > @a h2, -1 if @a h1 < @a h2 and 0 if @a h1 == @a h2.
856 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
857 const struct GNUNET_HashCode *h2);
862 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
863 * in the XOR metric (Kademlia).
865 * @param h1 some hash code
866 * @param h2 some hash code
867 * @param target some hash code
868 * @return -1 if @a h1 is closer, 1 if @a h2 is closer and 0 if @a h1== @a h2.
871 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
872 const struct GNUNET_HashCode *h2,
873 const struct GNUNET_HashCode *target);
878 * @brief Derive an authentication key
879 * @param key authentication key
880 * @param rkey root key
882 * @param salt_len size of the salt
883 * @param argp pair of void * & size_t for context chunks, terminated by NULL
886 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
887 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
888 const void *salt, size_t salt_len,
894 * @brief Derive an authentication key
895 * @param key authentication key
896 * @param rkey root key
898 * @param salt_len size of the salt
899 * @param ... pair of void * & size_t for context chunks, terminated by NULL
902 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
903 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
904 const void *salt, size_t salt_len, ...);
910 * @param result buffer for the derived key, allocated by caller
911 * @param out_len desired length of the derived key
912 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
913 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
915 * @param xts_len length of @a xts
916 * @param skm source key material
917 * @param skm_len length of @a skm
918 * @param ... pair of void * & size_t for context chunks, terminated by NULL
919 * @return #GNUNET_YES on success
922 GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
923 const void *xts, size_t xts_len, const void *skm,
924 size_t skm_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 argp va_list of void * & size_t pairs for context chunks
939 * @return #GNUNET_YES on success
942 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
943 const void *xts, size_t xts_len, const void *skm,
944 size_t skm_len, va_list argp);
949 * @param result buffer for the derived key, allocated by caller
950 * @param out_len desired length of the derived key
952 * @param xts_len length of @a xts
953 * @param skm source key material
954 * @param skm_len length of @a skm
955 * @param argp va_list of void * & size_t pairs for context chunks
956 * @return #GNUNET_YES on success
959 GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
960 size_t xts_len, const void *skm, size_t skm_len,
967 * @param result buffer for the derived key, allocated by caller
968 * @param out_len desired length of the derived key
970 * @param xts_len length of @a xts
971 * @param skm source key material
972 * @param skm_len length of @a skm
973 * @param ... void * & size_t pairs for context chunks
974 * @return #GNUNET_YES on success
977 GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
978 size_t xts_len, const void *skm, size_t skm_len, ...);
983 * Extract the public key for the given private key.
985 * @param priv the private key
986 * @param pub where to write the public key
989 GNUNET_CRYPTO_ecdsa_key_get_public (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
990 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
994 * Extract the public key for the given private key.
996 * @param priv the private key
997 * @param pub where to write the public key
1000 GNUNET_CRYPTO_eddsa_key_get_public (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1001 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1006 * Extract the public key for the given private key.
1008 * @param priv the private key
1009 * @param pub where to write the public key
1012 GNUNET_CRYPTO_ecdhe_key_get_public (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1013 struct GNUNET_CRYPTO_EcdhePublicKey *pub);
1017 * Convert a public key to a string.
1019 * @param pub key to convert
1020 * @return string representing @a pub
1023 GNUNET_CRYPTO_ecdsa_public_key_to_string (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1027 * Convert a public key to a string.
1029 * @param pub key to convert
1030 * @return string representing @a pub
1033 GNUNET_CRYPTO_eddsa_public_key_to_string (const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1037 * Convert a string representing a public key to a public key.
1039 * @param enc encoded public key
1040 * @param enclen number of bytes in @a enc (without 0-terminator)
1041 * @param pub where to store the public key
1042 * @return #GNUNET_OK on success
1045 GNUNET_CRYPTO_ecdsa_public_key_from_string (const char *enc,
1047 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1051 * Convert a string representing a private key to a private key.
1053 * @param enc encoded public key
1054 * @param enclen number of bytes in @a enc (without 0-terminator)
1055 * @param priv where to store the private key
1056 * @return #GNUNET_OK on success
1059 GNUNET_CRYPTO_eddsa_private_key_from_string (const char *enc,
1061 struct GNUNET_CRYPTO_EddsaPrivateKey *pub);
1065 * Convert a string representing a public key to a public key.
1067 * @param enc encoded public key
1068 * @param enclen number of bytes in @a enc (without 0-terminator)
1069 * @param pub where to store the public key
1070 * @return #GNUNET_OK on success
1073 GNUNET_CRYPTO_eddsa_public_key_from_string (const char *enc,
1075 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1080 * Create a new private key by reading it from a file. If the
1081 * files does not exist, create a new key and write it to the
1082 * file. Caller must free return value. Note that this function
1083 * can not guarantee that another process might not be trying
1084 * the same operation on the same file at the same time.
1085 * If the contents of the file
1086 * are invalid the old file is deleted and a fresh key is
1089 * @param filename name of file to use to store the key
1090 * @return new private key, NULL on error (for example,
1091 * permission denied); free using #GNUNET_free
1093 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1094 GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename);
1099 * Create a new private key by reading it from a file. If the
1100 * files does not exist, create a new key and write it to the
1101 * file. Caller must free return value. Note that this function
1102 * can not guarantee that another process might not be trying
1103 * the same operation on the same file at the same time.
1104 * If the contents of the file
1105 * are invalid the old file is deleted and a fresh key is
1108 * @param filename name of file to use to store the key
1109 * @return new private key, NULL on error (for example,
1110 * permission denied); free using #GNUNET_free
1112 struct GNUNET_CRYPTO_EddsaPrivateKey *
1113 GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename);
1115 struct GNUNET_CONFIGURATION_Handle;
1120 * Create a new private key by reading our peer's key from
1121 * the file specified in the configuration.
1123 * @param cfg the configuration to use
1124 * @return new private key, NULL on error (for example,
1125 * permission denied); free using #GNUNET_free
1127 struct GNUNET_CRYPTO_EddsaPrivateKey *
1128 GNUNET_CRYPTO_eddsa_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
1133 * Create a new private key. Caller must free return value.
1135 * @return fresh private key; free using #GNUNET_free
1137 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1138 GNUNET_CRYPTO_ecdsa_key_create (void);
1143 * Create a new private key. Caller must free return value.
1145 * @return fresh private key; free using #GNUNET_free
1147 struct GNUNET_CRYPTO_EddsaPrivateKey *
1148 GNUNET_CRYPTO_eddsa_key_create (void);
1153 * Create a new private key. Caller must free return value.
1155 * @return fresh private key; free using #GNUNET_free
1157 struct GNUNET_CRYPTO_EcdhePrivateKey *
1158 GNUNET_CRYPTO_ecdhe_key_create (void);
1163 * Clear memory that was used to store a private key.
1165 * @param pk location of the key
1168 GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk);
1173 * Clear memory that was used to store a private key.
1175 * @param pk location of the key
1178 GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk);
1182 * Clear memory that was used to store a private key.
1184 * @param pk location of the key
1187 GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1192 * Get the shared private key we use for anonymous users.
1194 * @return "anonymous" private key; do not free
1196 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
1197 GNUNET_CRYPTO_ecdsa_key_get_anonymous (void);
1202 * Setup a hostkey file for a peer given the name of the
1203 * configuration file (!). This function is used so that
1204 * at a later point code can be certain that reading a
1205 * hostkey is fast (for example in time-dependent testcases).
1207 * @param cfg_name name of the configuration file to use
1210 GNUNET_CRYPTO_eddsa_setup_hostkey (const char *cfg_name);
1215 * Retrieve the identity of the host's peer.
1217 * @param cfg configuration to use
1218 * @param dst pointer to where to write the peer identity
1219 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
1220 * could not be retrieved
1223 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
1224 struct GNUNET_PeerIdentity *dst);
1228 * Compare two Peer Identities.
1230 * @param first first peer identity
1231 * @param second second peer identity
1232 * @return bigger than 0 if first > second,
1233 * 0 if they are the same
1234 * smaller than 0 if second > first
1237 GNUNET_CRYPTO_cmp_peer_identity (const struct GNUNET_PeerIdentity *first,
1238 const struct GNUNET_PeerIdentity *second);
1243 * Derive key material from a public and a private ECC key.
1245 * @param priv private key to use for the ECDH (x)
1246 * @param pub public key to use for the ECDH (yG)
1247 * @param key_material where to write the key material (xyG)
1248 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1251 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1252 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1253 struct GNUNET_HashCode *key_material);
1258 * EdDSA sign a given block.
1260 * @param priv private key to use for the signing
1261 * @param purpose what to sign (size, purpose)
1262 * @param sig where to write the signature
1263 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1266 GNUNET_CRYPTO_eddsa_sign (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1267 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1268 struct GNUNET_CRYPTO_EddsaSignature *sig);
1273 * ECDSA Sign a given block.
1275 * @param priv private key to use for the signing
1276 * @param purpose what to sign (size, purpose)
1277 * @param sig where to write the signature
1278 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1281 GNUNET_CRYPTO_ecdsa_sign (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1282 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1283 struct GNUNET_CRYPTO_EcdsaSignature *sig);
1287 * Verify EdDSA signature.
1289 * @param purpose what is the purpose that the signature should have?
1290 * @param validate block to validate (size, purpose, data)
1291 * @param sig signature that is being validated
1292 * @param pub public key of the signer
1293 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1296 GNUNET_CRYPTO_eddsa_verify (uint32_t purpose,
1297 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1298 const struct GNUNET_CRYPTO_EddsaSignature *sig,
1299 const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1305 * Verify ECDSA signature.
1307 * @param purpose what is the purpose that the signature should have?
1308 * @param validate block to validate (size, purpose, data)
1309 * @param sig signature that is being validated
1310 * @param pub public key of the signer
1311 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1314 GNUNET_CRYPTO_ecdsa_verify (uint32_t purpose,
1315 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1316 const struct GNUNET_CRYPTO_EcdsaSignature *sig,
1317 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1322 * Derive a private key from a given private key and a label.
1323 * Essentially calculates a private key 'h = H(l,P) * d mod n'
1324 * where n is the size of the ECC group and P is the public
1325 * key associated with the private key 'd'.
1327 * @param priv original private key
1328 * @param label label to use for key deriviation
1329 * @param context additional context to use for HKDF of 'h';
1330 * typically the name of the subsystem/application
1331 * @return derived private key
1333 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1334 GNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1336 const char *context);
1341 * Derive a public key from a given public key and a label.
1342 * Essentially calculates a public key 'V = H(l,P) * P'.
1344 * @param pub original public key
1345 * @param label label to use for key deriviation
1346 * @param context additional context to use for HKDF of 'h'.
1347 * typically the name of the subsystem/application
1348 * @param result where to write the derived public key
1351 GNUNET_CRYPTO_ecdsa_public_key_derive (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1353 const char *context,
1354 struct GNUNET_CRYPTO_EcdsaPublicKey *result);
1358 * Output the given MPI value to the given buffer in network
1359 * byte order. The MPI @a val may not be negative.
1361 * @param buf where to output to
1362 * @param size number of bytes in @a buf
1363 * @param val value to write to @a buf
1366 GNUNET_CRYPTO_mpi_print_unsigned (void *buf,
1372 * Convert data buffer into MPI value.
1373 * The buffer is interpreted as network
1374 * byte order, unsigned integer.
1376 * @param result where to store MPI value (allocated)
1377 * @param data raw data (GCRYMPI_FMT_USG)
1378 * @param size number of bytes in @a data
1381 GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result,
1387 * Create a freshly generated paillier public key.
1389 * @param[out] public_key Where to store the public key?
1390 * @param[out] private_key Where to store the private key?
1393 GNUNET_CRYPTO_paillier_create (struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1394 struct GNUNET_CRYPTO_PaillierPrivateKey *private_key);
1398 * Encrypt a plaintext with a paillier public key.
1400 * @param public_key Public key to use.
1401 * @param m Plaintext to encrypt.
1402 * @param desired_ops How many homomorphic ops the caller intends to use
1403 * @param[out] ciphertext Encrytion of @a plaintext with @a public_key.
1404 * @return guaranteed number of supported homomorphic operations >= 1,
1405 * or desired_ops, in case that is lower,
1406 * or -1 if less than one homomorphic operation is possible
1409 GNUNET_CRYPTO_paillier_encrypt (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1412 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext);
1416 * Decrypt a paillier ciphertext with a private key.
1418 * @param private_key Private key to use for decryption.
1419 * @param public_key Public key to use for decryption.
1420 * @param ciphertext Ciphertext to decrypt.
1421 * @param[out] m Decryption of @a ciphertext with @private_key.
1424 GNUNET_CRYPTO_paillier_decrypt (const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key,
1425 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1426 const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
1431 * Compute a ciphertext that represents the sum of the plaintext in @a x1 and @a x2
1433 * Note that this operation can only be done a finite number of times
1434 * before an overflow occurs.
1436 * @param public_key Public key to use for encryption.
1437 * @param c1 Paillier cipher text.
1438 * @param c2 Paillier cipher text.
1439 * @param[out] result Result of the homomorphic operation.
1440 * @return #GNUNET_OK if the result could be computed,
1441 * #GNUNET_SYSERR if no more homomorphic operations are remaining.
1444 GNUNET_CRYPTO_paillier_hom_add (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1445 const struct GNUNET_CRYPTO_PaillierCiphertext *c1,
1446 const struct GNUNET_CRYPTO_PaillierCiphertext *c2,
1447 struct GNUNET_CRYPTO_PaillierCiphertext *result);
1451 * Get the number of remaining supported homomorphic operations.
1453 * @param c Paillier cipher text.
1454 * @return the number of remaining homomorphic operations
1457 GNUNET_CRYPTO_paillier_hom_get_remaining (const struct GNUNET_CRYPTO_PaillierCiphertext *c);
1460 /* ********* Chaum-style RSA-based blind signatures ******************* */
1466 * The private information of an RSA key pair.
1468 struct GNUNET_CRYPTO_rsa_PrivateKey;
1471 * The public information of an RSA key pair.
1473 struct GNUNET_CRYPTO_rsa_PublicKey;
1476 * Key used to blind a message
1478 struct GNUNET_CRYPTO_rsa_BlindingKey;
1481 * @brief an RSA signature
1483 struct GNUNET_CRYPTO_rsa_Signature;
1487 * Create a new private key. Caller must free return value.
1489 * @param len length of the key in bits (i.e. 2048)
1490 * @return fresh private key
1492 struct GNUNET_CRYPTO_rsa_PrivateKey *
1493 GNUNET_CRYPTO_rsa_private_key_create (unsigned int len);
1497 * Free memory occupied by the private key.
1499 * @param key pointer to the memory to free
1502 GNUNET_CRYPTO_rsa_private_key_free (struct GNUNET_CRYPTO_rsa_PrivateKey *key);
1506 * Encode the private key in a format suitable for
1507 * storing it into a file.
1509 * @param key the private key
1510 * @param[out] buffer set to a buffer with the encoded key
1511 * @return size of memory allocatedin @a buffer
1514 GNUNET_CRYPTO_rsa_private_key_encode (const struct GNUNET_CRYPTO_rsa_PrivateKey *key,
1519 * Decode the private key from the data-format back
1520 * to the "normal", internal format.
1522 * @param buf the buffer where the private key data is stored
1523 * @param len the length of the data in @a buf
1524 * @return NULL on error
1526 struct GNUNET_CRYPTO_rsa_PrivateKey *
1527 GNUNET_CRYPTO_rsa_private_key_decode (const char *buf,
1532 * Extract the public key of the given private key.
1534 * @param priv the private key
1535 * @retur NULL on error, otherwise the public key
1537 struct GNUNET_CRYPTO_rsa_PublicKey *
1538 GNUNET_CRYPTO_rsa_private_key_get_public (const struct GNUNET_CRYPTO_rsa_PrivateKey *priv);
1542 * Free memory occupied by the public key.
1544 * @param key pointer to the memory to free
1547 GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_rsa_PublicKey *key);
1551 * Encode the public key in a format suitable for
1552 * storing it into a file.
1554 * @param key the private key
1555 * @param[out] buffer set to a buffer with the encoded key
1556 * @return size of memory allocated in @a buffer
1559 GNUNET_CRYPTO_rsa_public_key_encode (const struct GNUNET_CRYPTO_rsa_PublicKey *key,
1564 * Decode the public key from the data-format back
1565 * to the "normal", internal format.
1567 * @param buf the buffer where the public key data is stored
1568 * @param len the length of the data in @a buf
1569 * @return NULL on error
1571 struct GNUNET_CRYPTO_rsa_PublicKey *
1572 GNUNET_CRYPTO_rsa_public_key_decode (const char *buf,
1577 * Create a blinding key
1579 * @param len length of the key in bits (i.e. 2048)
1580 * @return the newly created blinding key
1582 struct GNUNET_CRYPTO_rsa_BlindingKey *
1583 GNUNET_CRYPTO_rsa_blinding_key_create (unsigned int len);
1587 * Destroy a blinding key
1589 * @param bkey the blinding key to destroy
1592 GNUNET_CRYPTO_rsa_blinding_key_free (struct GNUNET_CRYPTO_rsa_BlindingKey *bkey);
1596 * Encode the blinding key in a format suitable for
1597 * storing it into a file.
1599 * @param bkey the blinding key
1600 * @param[out] buffer set to a buffer with the encoded key
1601 * @return size of memory allocated in @a buffer
1604 GNUNET_CRYPTO_rsa_blinding_key_encode (const struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
1609 * Decode the blinding key from the data-format back
1610 * to the "normal", internal format.
1612 * @param buf the buffer where the public key data is stored
1613 * @param len the length of the data in @a buf
1614 * @return NULL on error
1616 struct GNUNET_CRYPTO_rsa_BlindingKey *
1617 GNUNET_CRYPTO_rsa_blinding_key_decode (const char *buf,
1622 * Blinds the given message with the given blinding key
1624 * @param hash hash of the message to sign
1625 * @param bkey the blinding key
1626 * @param pkey the public key of the signer
1627 * @param[out] buffer set to a buffer with the blinded message to be signed
1628 * @return number of bytes stored in @a buffer
1631 GNUNET_CRYPTO_rsa_blind (const struct GNUNET_HashCode *hash,
1632 struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
1633 struct GNUNET_CRYPTO_rsa_PublicKey *pkey,
1638 * Sign the given message.
1640 * @param key private key to use for the signing
1641 * @param msg the (blinded) message to sign
1642 * @param msg_len number of bytes in @a msg to sign
1643 * @return NULL on error, signature on success
1645 struct GNUNET_CRYPTO_rsa_Signature *
1646 GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_rsa_PrivateKey *key,
1652 * Free memory occupied by signature.
1654 * @param sig memory to freee
1657 GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_rsa_Signature *sig);
1661 * Encode the given signature in a format suitable for storing it into a file.
1663 * @param sig the signature
1664 * @param[out] buffer set to a buffer with the encoded key
1665 * @return size of memory allocated in @a buffer
1668 GNUNET_CRYPTO_rsa_signature_encode (const struct GNUNET_CRYPTO_rsa_Signature *sig,
1673 * Decode the signature from the data-format back to the "normal", internal
1676 * @param buf the buffer where the public key data is stored
1677 * @param len the length of the data in @a buf
1678 * @return NULL on error
1680 struct GNUNET_CRYPTO_rsa_Signature *
1681 GNUNET_CRYPTO_rsa_signature_decode (const char *buf,
1686 * Unblind a blind-signed signature. The signature should have been generated
1687 * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with
1688 * #GNUNET_CRYPTO_rsa_blind().
1690 * @param sig the signature made on the blinded signature purpose
1691 * @param bkey the blinding key used to blind the signature purpose
1692 * @param pkey the public key of the signer
1693 * @return unblinded signature on success, NULL on error
1695 struct GNUNET_CRYPTO_rsa_Signature *
1696 GNUNET_CRYPTO_rsa_unblind (struct GNUNET_CRYPTO_rsa_Signature *sig,
1697 struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
1698 struct GNUNET_CRYPTO_rsa_PublicKey *pkey);
1702 * Verify whether the given hash corresponds to the given signature and the
1703 * signature is valid with respect to the given public key.
1705 * @param hash the message to verify to match the @a sig
1706 * @param sig signature that is being validated
1707 * @param public_key public key of the signer
1708 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1711 GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash,
1712 const struct GNUNET_CRYPTO_rsa_Signature *sig,
1713 const struct GNUNET_CRYPTO_rsa_PublicKey *public_key);
1716 #if 0 /* keep Emacsens' auto-indent happy */
1724 /* ifndef GNUNET_CRYPTO_LIB_H */
1726 /* end of gnunet_crypto_lib.h */