2 This file is part of GNUnet.
3 Copyright (C) 2001-2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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
30 * @author Jeffrey Burdges <burdges@gnunet.org>
32 * @defgroup crypto Crypto library: cryptographic operations
33 * Provides cryptographic primitives.
35 * @see [Documentation](https://gnunet.org/crypto-api)
37 * @defgroup hash Crypto library: hash operations
38 * Provides hashing and operations on hashes.
40 * @see [Documentation](https://gnunet.org/crypto-api)
43 #ifndef GNUNET_CRYPTO_LIB_H
44 #define GNUNET_CRYPTO_LIB_H
49 #if 0 /* keep Emacsens' auto-indent happy */
55 * @brief A 512-bit hashcode. These are the default length for GNUnet, using SHA-512.
57 struct GNUNET_HashCode
59 uint32_t bits[512 / 8 / sizeof (uint32_t)]; /* = 16 */
65 * @brief A 256-bit hashcode. Used under special conditions, like when space
66 * is critical and security is not impacted by it.
68 struct GNUNET_ShortHashCode
70 uint32_t bits[256 / 8 / sizeof (uint32_t)]; /* = 8 */
75 * The identity of the host (wraps the signing key of the peer).
77 struct GNUNET_PeerIdentity;
79 #include "gnunet_common.h"
84 * Maximum length of an ECC signature.
85 * Note: round up to multiple of 8 minus 2 for alignment.
87 #define GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH 126
91 * Desired quality level for random numbers.
94 enum GNUNET_CRYPTO_Quality
97 * No good quality of the operation is needed (i.e.,
98 * random numbers can be pseudo-random).
101 GNUNET_CRYPTO_QUALITY_WEAK,
104 * High-quality operations are desired.
107 GNUNET_CRYPTO_QUALITY_STRONG,
110 * Randomness for IVs etc. is required.
113 GNUNET_CRYPTO_QUALITY_NONCE
118 * @brief length of the sessionkey in bytes (256 BIT sessionkey)
120 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
123 * Length of a hash value
125 #define GNUNET_CRYPTO_HASH_LENGTH (512/8)
128 * How many characters (without 0-terminator) are our ASCII-encoded
129 * public keys (ECDSA/EDDSA/ECDHE).
131 #define GNUNET_CRYPTO_PKEY_ASCII_LENGTH 52
134 * @brief 0-terminated ASCII encoding of a struct GNUNET_HashCode.
136 struct GNUNET_CRYPTO_HashAsciiEncoded
138 unsigned char encoding[104];
142 GNUNET_NETWORK_STRUCT_BEGIN
146 * @brief header of what an ECC signature signs
147 * this must be followed by "size - 8" bytes of
148 * the actual signed data
150 struct GNUNET_CRYPTO_EccSignaturePurpose
153 * How many bytes does this signature sign?
154 * (including this purpose header); in network
157 uint32_t size GNUNET_PACKED;
160 * What does this signature vouch for? This
161 * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
162 * constant (from gnunet_signatures.h). In
163 * network byte order!
165 uint32_t purpose GNUNET_PACKED;
171 * @brief an ECC signature using EdDSA.
172 * See https://gnunet.org/ed25519
174 struct GNUNET_CRYPTO_EddsaSignature
180 unsigned char r[256 / 8];
185 unsigned char s[256 / 8];
192 * @brief an ECC signature using ECDSA
194 struct GNUNET_CRYPTO_EcdsaSignature
200 unsigned char r[256 / 8];
205 unsigned char s[256 / 8];
211 * Public ECC key (always for Curve25519) encoded in a format suitable
212 * for network transmission and EdDSA signatures.
214 struct GNUNET_CRYPTO_EddsaPublicKey
217 * Q consists of an x- and a y-value, each mod p (256 bits), given
218 * here in affine coordinates and Ed25519 standard compact format.
220 unsigned char q_y[256 / 8];
226 * Public ECC key (always for Curve25519) encoded in a format suitable
227 * for network transmission and ECDSA signatures.
229 struct GNUNET_CRYPTO_EcdsaPublicKey
232 * Q consists of an x- and a y-value, each mod p (256 bits), given
233 * here in affine coordinates and Ed25519 standard compact format.
235 unsigned char q_y[256 / 8];
241 * The identity of the host (wraps the signing key of the peer).
243 struct GNUNET_PeerIdentity
245 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
250 * Public ECC key (always for Curve25519) encoded in a format suitable
251 * for network transmission and encryption (ECDH),
252 * See http://cr.yp.to/ecdh.html
254 struct GNUNET_CRYPTO_EcdhePublicKey
257 * Q consists of an x- and a y-value, each mod p (256 bits), given
258 * here in affine coordinates and Ed25519 standard compact format.
260 unsigned char q_y[256 / 8];
265 * Private ECC key encoded for transmission. To be used only for ECDH
266 * key exchange (ECDHE to be precise).
268 struct GNUNET_CRYPTO_EcdhePrivateKey
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 ECDSA
281 struct GNUNET_CRYPTO_EcdsaPrivateKey
284 * d is a value mod n, where n has at most 256 bits.
286 unsigned char d[256 / 8];
291 * Private ECC key encoded for transmission. To be used only for EdDSA
294 struct GNUNET_CRYPTO_EddsaPrivateKey
297 * d is a value mod n, where n has at most 256 bits.
299 unsigned char d[256 / 8];
305 * @brief type for session keys
307 struct GNUNET_CRYPTO_SymmetricSessionKey
310 * Actual key for AES.
312 unsigned char aes_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
315 * Actual key for TwoFish.
317 unsigned char twofish_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
321 GNUNET_NETWORK_STRUCT_END
324 * @brief IV for sym cipher
326 * NOTE: must be smaller (!) in size than the
327 * `struct GNUNET_HashCode`.
329 struct GNUNET_CRYPTO_SymmetricInitializationVector
331 unsigned char aes_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
333 unsigned char twofish_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
338 * @brief type for (message) authentication keys
340 struct GNUNET_CRYPTO_AuthKey
342 unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
347 * Size of paillier plain texts and public keys.
348 * Private keys and ciphertexts are twice this size.
350 #define GNUNET_CRYPTO_PAILLIER_BITS 2048
354 * Paillier public key.
356 struct GNUNET_CRYPTO_PaillierPublicKey
361 unsigned char n[GNUNET_CRYPTO_PAILLIER_BITS / 8];
366 * Paillier private key.
368 struct GNUNET_CRYPTO_PaillierPrivateKey
371 * Lambda-component of the private key.
373 unsigned char lambda[GNUNET_CRYPTO_PAILLIER_BITS / 8];
375 * Mu-component of the private key.
377 unsigned char mu[GNUNET_CRYPTO_PAILLIER_BITS / 8];
382 * Paillier ciphertext.
384 struct GNUNET_CRYPTO_PaillierCiphertext
387 * Guaranteed minimum number of homomorphic operations with this ciphertext,
388 * in network byte order (NBO).
390 int32_t remaining_ops GNUNET_PACKED;
393 * The bits of the ciphertext.
395 unsigned char bits[GNUNET_CRYPTO_PAILLIER_BITS * 2 / 8];
399 /* **************** Functions and Macros ************* */
403 * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator
406 * @param seed the seed to use
409 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
414 * Calculate the checksum of a buffer in one step.
416 * @param buf buffer to calculate CRC over
417 * @param len number of bytes in @a buf
421 GNUNET_CRYPTO_crc8_n (const void *buf,
426 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
428 * @param sum current sum, initially 0
429 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
430 * @param len number of bytes in @a buf, must be multiple of 2
431 * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish to get actual crc16)
434 GNUNET_CRYPTO_crc16_step (uint32_t sum,
440 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
442 * @param sum cummulative sum
443 * @return crc16 value
446 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
451 * Calculate the checksum of a buffer in one step.
453 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
454 * @param len number of bytes in @a buf, must be multiple of 2
455 * @return crc16 value
458 GNUNET_CRYPTO_crc16_n (const void *buf,
466 * Compute the CRC32 checksum for the first len
467 * bytes of the buffer.
469 * @param buf the data over which we're taking the CRC
470 * @param len the length of the buffer @a buf in bytes
471 * @return the resulting CRC32 checksum
474 GNUNET_CRYPTO_crc32_n (const void *buf,
480 * Fill block with a random values.
482 * @param mode desired quality of the random number
483 * @param buffer the buffer to fill
484 * @param length buffer length
487 GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode,
493 * Produce a random value.
495 * @param mode desired quality of the random number
496 * @param i the upper limit (exclusive) for the random number
497 * @return a random value in the interval [0,@a i) (exclusive).
500 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode,
506 * Random on unsigned 64-bit values.
508 * @param mode desired quality of the random number
509 * @param max value returned will be in range [0,@a max) (exclusive)
510 * @return random 64-bit number
513 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
519 * Get an array with a random permutation of the
521 * @param mode #GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used,
522 * #GNUNET_CRYPTO_QUALITY_WEAK or #GNUNET_CRYPTO_QUALITY_NONCE otherwise
523 * @param n the size of the array
524 * @return the permutation array (allocated from heap)
527 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode,
533 * Create a new random session key.
535 * @param key key to initialize
538 GNUNET_CRYPTO_symmetric_create_session_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key);
543 * Encrypt a block using a symmetric sessionkey.
545 * @param block the block to encrypt
546 * @param size the size of the @a block
547 * @param sessionkey the key used to encrypt
548 * @param iv the initialization vector to use, use INITVALUE
550 * @return the size of the encrypted block, -1 for errors
553 GNUNET_CRYPTO_symmetric_encrypt (const void *block,
555 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
556 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
562 * Decrypt a given block using a symmetric sessionkey.
564 * @param block the data to decrypt, encoded as returned by encrypt
565 * @param size how big is the block?
566 * @param sessionkey the key used to decrypt
567 * @param iv the initialization vector to use
568 * @param result address to store the result at
569 * @return -1 on failure, size of decrypted block on success
572 GNUNET_CRYPTO_symmetric_decrypt (const void *block,
574 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
575 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
581 * @brief Derive an IV
582 * @param iv initialization vector
583 * @param skey session key
584 * @param salt salt for the derivation
585 * @param salt_len size of the @a salt
586 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
589 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
590 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
592 size_t salt_len, ...);
596 * @brief Derive an IV
597 * @param iv initialization vector
598 * @param skey session key
599 * @param salt salt for the derivation
600 * @param salt_len size of the @a salt
601 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
604 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
605 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
613 * Convert hash to ASCII encoding.
614 * @param block the hash code
615 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
616 * safely cast to char*, a '\\0' termination is set).
619 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block,
620 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
625 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
627 * @param enc the encoding
628 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
629 * @param result where to store the hash code
630 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
633 GNUNET_CRYPTO_hash_from_string2 (const char *enc,
635 struct GNUNET_HashCode *result);
640 * Convert ASCII encoding back to `struct GNUNET_HashCode`
642 * @param enc the encoding
643 * @param result where to store the hash code
644 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
646 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
647 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
653 * Compute the distance between 2 hashcodes. The
654 * computation must be fast, not involve @a a[0] or @a a[4] (they're used
655 * elsewhere), and be somewhat consistent. And of course, the result
656 * should be a positive number.
658 * @param a some hash code
659 * @param b some hash code
660 * @return number between 0 and UINT32_MAX
663 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a,
664 const struct GNUNET_HashCode *b);
669 * Compute hash of a given block.
671 * @param block the data to hash
672 * @param size size of the @a block
673 * @param ret pointer to where to write the hashcode
676 GNUNET_CRYPTO_hash (const void *block,
678 struct GNUNET_HashCode *ret);
682 * Context for cummulative hashing.
684 struct GNUNET_HashContext;
688 * Start incremental hashing operation.
690 * @return context for incremental hash computation
692 struct GNUNET_HashContext *
693 GNUNET_CRYPTO_hash_context_start (void);
697 * Add data to be hashed.
699 * @param hc cummulative hash context
700 * @param buf data to add
701 * @param size number of bytes in @a buf
704 GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
710 * Finish the hash computation.
712 * @param hc hash context to use, is freed in the process
713 * @param r_hash where to write the latest / final hash code
716 GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
717 struct GNUNET_HashCode *r_hash);
721 * Abort hashing, do not bother calculating final result.
723 * @param hc hash context to destroy
726 GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc);
731 * Calculate HMAC of a message (RFC 2104)
733 * @param key secret key
734 * @param plaintext input plaintext
735 * @param plaintext_len length of @a plaintext
736 * @param hmac where to store the hmac
739 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
740 const void *plaintext,
741 size_t plaintext_len,
742 struct GNUNET_HashCode *hmac);
746 * Function called once the hash computation over the
747 * specified file has completed.
750 * @param res resulting hash, NULL on error
753 (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
754 const struct GNUNET_HashCode *res);
758 * Handle to file hashing operation.
760 struct GNUNET_CRYPTO_FileHashContext;
765 * Compute the hash of an entire file.
767 * @param priority scheduling priority to use
768 * @param filename name of file to hash
769 * @param blocksize number of bytes to process in one task
770 * @param callback function to call upon completion
771 * @param callback_cls closure for @a callback
772 * @return NULL on (immediate) errror
774 struct GNUNET_CRYPTO_FileHashContext *
775 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
776 const char *filename,
778 GNUNET_CRYPTO_HashCompletedCallback callback,
783 * Cancel a file hashing operation.
785 * @param fhc operation to cancel (callback must not yet have been invoked)
788 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
793 * Create a random hash code.
795 * @param mode desired quality level
796 * @param result hash code that is randomized
799 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
800 struct GNUNET_HashCode *result);
805 * compute @a result = @a b - @a a
807 * @param a some hash code
808 * @param b some hash code
809 * @param result set to @a b - @a a
812 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
813 const struct GNUNET_HashCode *b,
814 struct GNUNET_HashCode *result);
819 * compute @a result = @a a + @a delta
821 * @param a some hash code
822 * @param delta some hash code
823 * @param result set to @a a + @a delta
826 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a,
827 const struct GNUNET_HashCode *delta,
828 struct GNUNET_HashCode *result);
833 * compute result = a ^ b
835 * @param a some hash code
836 * @param b some hash code
837 * @param result set to @a a ^ @a b
840 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
841 const struct GNUNET_HashCode *b,
842 struct GNUNET_HashCode *result);
847 * Convert a hashcode into a key.
849 * @param hc hash code that serves to generate the key
850 * @param skey set to a valid session key
851 * @param iv set to a valid initialization vector
854 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
855 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
856 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv);
861 * Obtain a bit from a hashcode.
863 * @param code the `struct GNUNET_HashCode` to index bit-wise
864 * @param bit index into the hashcode, [0...159]
865 * @return Bit \a bit from hashcode \a code, -1 for invalid index
868 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code,
874 * Determine how many low order bits match in two
875 * `struct GNUNET_HashCodes`. i.e. - 010011 and 011111 share
876 * the first two lowest order bits, and therefore the
877 * return value is two (NOT XOR distance, nor how many
878 * bits match absolutely!).
880 * @param first the first hashcode
881 * @param second the hashcode to compare first to
882 * @return the number of bits that match
885 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
886 const struct GNUNET_HashCode *second);
891 * Compare function for HashCodes, producing a total ordering
894 * @param h1 some hash code
895 * @param h2 some hash code
896 * @return 1 if @a h1 > @a h2, -1 if @a h1 < @a h2 and 0 if @a h1 == @a h2.
899 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
900 const struct GNUNET_HashCode *h2);
905 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
906 * in the XOR metric (Kademlia).
908 * @param h1 some hash code
909 * @param h2 some hash code
910 * @param target some hash code
911 * @return -1 if @a h1 is closer, 1 if @a h2 is closer and 0 if @a h1== @a h2.
914 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
915 const struct GNUNET_HashCode *h2,
916 const struct GNUNET_HashCode *target);
921 * @brief Derive an authentication key
922 * @param key authentication key
923 * @param rkey root key
925 * @param salt_len size of the salt
926 * @param argp pair of void * & size_t for context chunks, terminated by NULL
929 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
930 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
931 const void *salt, size_t salt_len,
937 * @brief Derive an authentication key
938 * @param key authentication key
939 * @param rkey root key
941 * @param salt_len size of the salt
942 * @param ... pair of void * & size_t for context chunks, terminated by NULL
945 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
946 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
947 const void *salt, size_t salt_len,
954 * @param result buffer for the derived key, allocated by caller
955 * @param out_len desired length of the derived key
956 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
957 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
959 * @param xts_len length of @a xts
960 * @param skm source key material
961 * @param skm_len length of @a skm
962 * @param ... pair of void * & size_t for context chunks, terminated by NULL
963 * @return #GNUNET_YES on success
966 GNUNET_CRYPTO_hkdf (void *result,
980 * @param result buffer for the derived key, allocated by caller
981 * @param out_len desired length of the derived key
982 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
983 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
985 * @param xts_len length of @a xts
986 * @param skm source key material
987 * @param skm_len length of @a skm
988 * @param argp va_list of void * & size_t pairs for context chunks
989 * @return #GNUNET_YES on success
992 GNUNET_CRYPTO_hkdf_v (void *result,
1005 * @param result buffer for the derived key, allocated by caller
1006 * @param out_len desired length of the derived key
1008 * @param xts_len length of @a xts
1009 * @param skm source key material
1010 * @param skm_len length of @a skm
1011 * @param argp va_list of void * & size_t pairs for context chunks
1012 * @return #GNUNET_YES on success
1015 GNUNET_CRYPTO_kdf_v (void *result,
1025 * Deterministically generate a pseudo-random number uniformly from the
1026 * integers modulo a libgcrypt mpi.
1028 * @param[out] r MPI value set to the FDH
1029 * @param n MPI to work modulo
1031 * @param xts_len length of @a xts
1032 * @param skm source key material
1033 * @param skm_len length of @a skm
1034 * @param ctx context string
1037 GNUNET_CRYPTO_kdf_mod_mpi (gcry_mpi_t *r,
1039 const void *xts, size_t xts_len,
1040 const void *skm, size_t skm_len,
1047 * @param result buffer for the derived key, allocated by caller
1048 * @param out_len desired length of the derived key
1050 * @param xts_len length of @a xts
1051 * @param skm source key material
1052 * @param skm_len length of @a skm
1053 * @param ... void * & size_t pairs for context chunks
1054 * @return #GNUNET_YES on success
1057 GNUNET_CRYPTO_kdf (void *result,
1068 * Extract the public key for the given private key.
1070 * @param priv the private key
1071 * @param pub where to write the public key
1074 GNUNET_CRYPTO_ecdsa_key_get_public (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1075 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1079 * Extract the public key for the given private key.
1081 * @param priv the private key
1082 * @param pub where to write the public key
1085 GNUNET_CRYPTO_eddsa_key_get_public (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1086 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1092 * Extract the public key for the given private key.
1094 * @param priv the private key
1095 * @param pub where to write the public key
1098 GNUNET_CRYPTO_ecdhe_key_get_public (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1099 struct GNUNET_CRYPTO_EcdhePublicKey *pub);
1103 * Convert a public key to a string.
1105 * @param pub key to convert
1106 * @return string representing @a pub
1109 GNUNET_CRYPTO_ecdsa_public_key_to_string (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1113 * Convert a private key to a string.
1115 * @param priv key to convert
1116 * @return string representing @a pub
1119 GNUNET_CRYPTO_eddsa_private_key_to_string (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv);
1123 * Convert a public key to a string.
1125 * @param pub key to convert
1126 * @return string representing @a pub
1129 GNUNET_CRYPTO_eddsa_public_key_to_string (const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1133 * Convert a string representing a public key to a public key.
1135 * @param enc encoded public key
1136 * @param enclen number of bytes in @a enc (without 0-terminator)
1137 * @param pub where to store the public key
1138 * @return #GNUNET_OK on success
1141 GNUNET_CRYPTO_ecdsa_public_key_from_string (const char *enc,
1143 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1147 * Convert a string representing a private key to a private key.
1149 * @param enc encoded public key
1150 * @param enclen number of bytes in @a enc (without 0-terminator)
1151 * @param priv where to store the private key
1152 * @return #GNUNET_OK on success
1155 GNUNET_CRYPTO_eddsa_private_key_from_string (const char *enc,
1157 struct GNUNET_CRYPTO_EddsaPrivateKey *pub);
1161 * Convert a string representing a public key to a public key.
1163 * @param enc encoded public key
1164 * @param enclen number of bytes in @a enc (without 0-terminator)
1165 * @param pub where to store the public key
1166 * @return #GNUNET_OK on success
1169 GNUNET_CRYPTO_eddsa_public_key_from_string (const char *enc,
1171 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1176 * Create a new private key by reading it from a file. If the
1177 * files does not exist, create a new key and write it to the
1178 * file. Caller must free return value. Note that this function
1179 * can not guarantee that another process might not be trying
1180 * the same operation on the same file at the same time.
1181 * If the contents of the file
1182 * are invalid the old file is deleted and a fresh key is
1185 * @param filename name of file to use to store the key
1186 * @return new private key, NULL on error (for example,
1187 * permission denied); free using #GNUNET_free
1189 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1190 GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename);
1195 * Create a new private key by reading it from a file. If the
1196 * files does not exist, create a new key and write it to the
1197 * file. Caller must free return value. Note that this function
1198 * can not guarantee that another process might not be trying
1199 * the same operation on the same file at the same time.
1200 * If the contents of the file
1201 * are invalid the old file is deleted and a fresh key is
1204 * @param filename name of file to use to store the key
1205 * @return new private key, NULL on error (for example,
1206 * permission denied); free using #GNUNET_free
1208 struct GNUNET_CRYPTO_EddsaPrivateKey *
1209 GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename);
1213 * Forward declaration to simplify #include-structure.
1215 struct GNUNET_CONFIGURATION_Handle;
1220 * Create a new private key by reading our peer's key from
1221 * the file specified in the configuration.
1223 * @param cfg the configuration to use
1224 * @return new private key, NULL on error (for example,
1225 * permission denied); free using #GNUNET_free
1227 struct GNUNET_CRYPTO_EddsaPrivateKey *
1228 GNUNET_CRYPTO_eddsa_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
1233 * Create a new private key. Caller must free return value.
1235 * @return fresh private key; free using #GNUNET_free
1237 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1238 GNUNET_CRYPTO_ecdsa_key_create (void);
1243 * Create a new private key. Caller must free return value.
1245 * @return fresh private key; free using #GNUNET_free
1247 struct GNUNET_CRYPTO_EddsaPrivateKey *
1248 GNUNET_CRYPTO_eddsa_key_create (void);
1253 * Create a new private key. Clear with #GNUNET_CRYPTO_ecdhe_key_clear().
1255 * @param[out] pk set to fresh private key;
1256 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1259 GNUNET_CRYPTO_ecdhe_key_create2 (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1264 * Create a new private key. Caller must free return value.
1266 * @return fresh private key; free using #GNUNET_free
1268 struct GNUNET_CRYPTO_EcdhePrivateKey *
1269 GNUNET_CRYPTO_ecdhe_key_create (void);
1274 * Clear memory that was used to store a private key.
1276 * @param pk location of the key
1279 GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk);
1284 * Clear memory that was used to store a private key.
1286 * @param pk location of the key
1289 GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk);
1294 * Clear memory that was used to store a private key.
1296 * @param pk location of the key
1299 GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1304 * Get the shared private key we use for anonymous users.
1306 * @return "anonymous" private key; do not free
1308 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
1309 GNUNET_CRYPTO_ecdsa_key_get_anonymous (void);
1314 * Setup a hostkey file for a peer given the name of the
1315 * configuration file (!). This function is used so that
1316 * at a later point code can be certain that reading a
1317 * hostkey is fast (for example in time-dependent testcases).
1319 * @param cfg_name name of the configuration file to use
1322 GNUNET_CRYPTO_eddsa_setup_hostkey (const char *cfg_name);
1327 * Retrieve the identity of the host's peer.
1329 * @param cfg configuration to use
1330 * @param dst pointer to where to write the peer identity
1331 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
1332 * could not be retrieved
1335 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
1336 struct GNUNET_PeerIdentity *dst);
1340 * Compare two Peer Identities.
1342 * @param first first peer identity
1343 * @param second second peer identity
1344 * @return bigger than 0 if first > second,
1345 * 0 if they are the same
1346 * smaller than 0 if second > first
1349 GNUNET_CRYPTO_cmp_peer_identity (const struct GNUNET_PeerIdentity *first,
1350 const struct GNUNET_PeerIdentity *second);
1354 * Internal structure used to cache pre-calculated values for DLOG calculation.
1356 struct GNUNET_CRYPTO_EccDlogContext;
1360 * Point on a curve (always for Curve25519) encoded in a format suitable
1361 * for network transmission (ECDH), see http://cr.yp.to/ecdh.html.
1363 struct GNUNET_CRYPTO_EccPoint
1366 * Q consists of an x- and a y-value, each mod p (256 bits), given
1367 * here in affine coordinates and Ed25519 standard compact format.
1369 unsigned char q_y[256 / 8];
1374 * Do pre-calculation for ECC discrete logarithm for small factors.
1376 * @param max maximum value the factor can be
1377 * @param mem memory to use (should be smaller than @a max), must not be zero.
1378 * @return NULL on error
1380 struct GNUNET_CRYPTO_EccDlogContext *
1381 GNUNET_CRYPTO_ecc_dlog_prepare (unsigned int max,
1386 * Calculate ECC discrete logarithm for small factors.
1387 * Opposite of #GNUNET_CRYPTO_ecc_dexp().
1389 * @param dlc precalculated values, determine range of factors
1390 * @param input point on the curve to factor
1391 * @return INT_MAX if dlog failed, otherwise the factor
1394 GNUNET_CRYPTO_ecc_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc,
1395 gcry_mpi_point_t input);
1399 * Multiply the generator g of the elliptic curve by @a val
1400 * to obtain the point on the curve representing @a val.
1401 * Afterwards, point addition will correspond to integer
1402 * addition. #GNUNET_CRYPTO_ecc_dlog() can be used to
1403 * convert a point back to an integer (as long as the
1404 * integer is smaller than the MAX of the @a edc context).
1406 * @param edc calculation context for ECC operations
1407 * @param val value to encode into a point
1408 * @return representation of the value as an ECC point,
1409 * must be freed using #GNUNET_CRYPTO_ecc_free()
1412 GNUNET_CRYPTO_ecc_dexp (struct GNUNET_CRYPTO_EccDlogContext *edc,
1417 * Multiply the generator g of the elliptic curve by @a val
1418 * to obtain the point on the curve representing @a val.
1420 * @param edc calculation context for ECC operations
1421 * @param val (positive) value to encode into a point
1422 * @return representation of the value as an ECC point,
1423 * must be freed using #GNUNET_CRYPTO_ecc_free()
1426 GNUNET_CRYPTO_ecc_dexp_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1431 * Multiply the point @a p on the elliptic curve by @a val.
1433 * @param edc calculation context for ECC operations
1434 * @param p point to multiply
1435 * @param val (positive) value to encode into a point
1436 * @return representation of the value as an ECC point,
1437 * must be freed using #GNUNET_CRYPTO_ecc_free()
1440 GNUNET_CRYPTO_ecc_pmul_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1446 * Convert point value to binary representation.
1448 * @param edc calculation context for ECC operations
1449 * @param point computational point representation
1450 * @param[out] bin binary point representation
1453 GNUNET_CRYPTO_ecc_point_to_bin (struct GNUNET_CRYPTO_EccDlogContext *edc,
1454 gcry_mpi_point_t point,
1455 struct GNUNET_CRYPTO_EccPoint *bin);
1459 * Convert binary representation of a point to computational representation.
1461 * @param edc calculation context for ECC operations
1462 * @param bin binary point representation
1463 * @return computational representation
1466 GNUNET_CRYPTO_ecc_bin_to_point (struct GNUNET_CRYPTO_EccDlogContext *edc,
1467 const struct GNUNET_CRYPTO_EccPoint *bin);
1471 * Add two points on the elliptic curve.
1473 * @param edc calculation context for ECC operations
1474 * @param a some value
1475 * @param b some value
1476 * @return @a a + @a b, must be freed using #GNUNET_CRYPTO_ecc_free()
1479 GNUNET_CRYPTO_ecc_add (struct GNUNET_CRYPTO_EccDlogContext *edc,
1481 gcry_mpi_point_t b);
1485 * Obtain a random point on the curve and its
1486 * additive inverse. Both returned values
1487 * must be freed using #GNUNET_CRYPTO_ecc_free().
1489 * @param edc calculation context for ECC operations
1490 * @param[out] r set to a random point on the curve
1491 * @param[out] r_inv set to the additive inverse of @a r
1494 GNUNET_CRYPTO_ecc_rnd (struct GNUNET_CRYPTO_EccDlogContext *edc,
1495 gcry_mpi_point_t *r,
1496 gcry_mpi_point_t *r_inv);
1500 * Obtain a random scalar for point multiplication on the curve and
1501 * its multiplicative inverse.
1503 * @param edc calculation context for ECC operations
1504 * @param[out] r set to a random scalar on the curve
1505 * @param[out] r_inv set to the multiplicative inverse of @a r
1508 GNUNET_CRYPTO_ecc_rnd_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1514 * Generate a random value mod n.
1516 * @param edc ECC context
1517 * @return random value mod n.
1520 GNUNET_CRYPTO_ecc_random_mod_n (struct GNUNET_CRYPTO_EccDlogContext *edc);
1524 * Free a point value returned by the API.
1526 * @param p point to free
1529 GNUNET_CRYPTO_ecc_free (gcry_mpi_point_t p);
1533 * Release precalculated values.
1535 * @param dlc dlog context
1538 GNUNET_CRYPTO_ecc_dlog_release (struct GNUNET_CRYPTO_EccDlogContext *dlc);
1543 * Derive key material from a public and a private ECC key.
1545 * @param priv private key to use for the ECDH (x)
1546 * @param pub public key to use for the ECDH (yG)
1547 * @param key_material where to write the key material (xyG)
1548 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1551 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1552 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1553 struct GNUNET_HashCode *key_material);
1558 * Derive key material from a ECDH public key and a private EdDSA key.
1559 * Dual to #GNUNET_CRRYPTO_ecdh_eddsa.
1561 * @param priv private key from EdDSA to use for the ECDH (x)
1562 * @param pub public key to use for the ECDH (yG)
1563 * @param key_material where to write the key material H(h(x)yG)
1564 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1567 GNUNET_CRYPTO_eddsa_ecdh (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1568 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1569 struct GNUNET_HashCode *key_material);
1573 * Derive key material from a ECDH public key and a private ECDSA key.
1574 * Dual to #GNUNET_CRRYPTO_ecdh_ecdsa.
1576 * @param priv private key from ECDSA to use for the ECDH (x)
1577 * @param pub public key to use for the ECDH (yG)
1578 * @param key_material where to write the key material H(h(x)yG)
1579 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1582 GNUNET_CRYPTO_ecdsa_ecdh (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1583 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1584 struct GNUNET_HashCode *key_material);
1589 * Derive key material from a EdDSA public key and a private ECDH key.
1590 * Dual to #GNUNET_CRRYPTO_eddsa_ecdh.
1592 * @param priv private key to use for the ECDH (y)
1593 * @param pub public key from EdDSA to use for the ECDH (X=h(x)G)
1594 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1595 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1598 GNUNET_CRYPTO_ecdh_eddsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1599 const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
1600 struct GNUNET_HashCode *key_material);
1604 * Derive key material from a EcDSA public key and a private ECDH key.
1605 * Dual to #GNUNET_CRRYPTO_ecdsa_ecdh.
1607 * @param priv private key to use for the ECDH (y)
1608 * @param pub public key from ECDSA to use for the ECDH (X=h(x)G)
1609 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1610 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1613 GNUNET_CRYPTO_ecdh_ecdsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1614 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1615 struct GNUNET_HashCode *key_material);
1620 * EdDSA sign a given block.
1622 * @param priv private key to use for the signing
1623 * @param purpose what to sign (size, purpose)
1624 * @param sig where to write the signature
1625 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1628 GNUNET_CRYPTO_eddsa_sign (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1629 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1630 struct GNUNET_CRYPTO_EddsaSignature *sig);
1635 * ECDSA Sign a given block.
1637 * @param priv private key to use for the signing
1638 * @param purpose what to sign (size, purpose)
1639 * @param sig where to write the signature
1640 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1643 GNUNET_CRYPTO_ecdsa_sign (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1644 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1645 struct GNUNET_CRYPTO_EcdsaSignature *sig);
1649 * Verify EdDSA signature.
1651 * @param purpose what is the purpose that the signature should have?
1652 * @param validate block to validate (size, purpose, data)
1653 * @param sig signature that is being validated
1654 * @param pub public key of the signer
1655 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1658 GNUNET_CRYPTO_eddsa_verify (uint32_t purpose,
1659 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1660 const struct GNUNET_CRYPTO_EddsaSignature *sig,
1661 const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1667 * Verify ECDSA signature.
1669 * @param purpose what is the purpose that the signature should have?
1670 * @param validate block to validate (size, purpose, data)
1671 * @param sig signature that is being validated
1672 * @param pub public key of the signer
1673 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1676 GNUNET_CRYPTO_ecdsa_verify (uint32_t purpose,
1677 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1678 const struct GNUNET_CRYPTO_EcdsaSignature *sig,
1679 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1684 * Derive a private key from a given private key and a label.
1685 * Essentially calculates a private key 'h = H(l,P) * d mod n'
1686 * where n is the size of the ECC group and P is the public
1687 * key associated with the private key 'd'.
1689 * @param priv original private key
1690 * @param label label to use for key deriviation
1691 * @param context additional context to use for HKDF of 'h';
1692 * typically the name of the subsystem/application
1693 * @return derived private key
1695 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1696 GNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1698 const char *context);
1703 * Derive a public key from a given public key and a label.
1704 * Essentially calculates a public key 'V = H(l,P) * P'.
1706 * @param pub original public key
1707 * @param label label to use for key deriviation
1708 * @param context additional context to use for HKDF of 'h'.
1709 * typically the name of the subsystem/application
1710 * @param result where to write the derived public key
1713 GNUNET_CRYPTO_ecdsa_public_key_derive (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1715 const char *context,
1716 struct GNUNET_CRYPTO_EcdsaPublicKey *result);
1720 * Output the given MPI value to the given buffer in network
1721 * byte order. The MPI @a val may not be negative.
1723 * @param buf where to output to
1724 * @param size number of bytes in @a buf
1725 * @param val value to write to @a buf
1728 GNUNET_CRYPTO_mpi_print_unsigned (void *buf,
1734 * Convert data buffer into MPI value.
1735 * The buffer is interpreted as network
1736 * byte order, unsigned integer.
1738 * @param result where to store MPI value (allocated)
1739 * @param data raw data (GCRYMPI_FMT_USG)
1740 * @param size number of bytes in @a data
1743 GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result,
1749 * Create a freshly generated paillier public key.
1751 * @param[out] public_key Where to store the public key?
1752 * @param[out] private_key Where to store the private key?
1755 GNUNET_CRYPTO_paillier_create (struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1756 struct GNUNET_CRYPTO_PaillierPrivateKey *private_key);
1760 * Encrypt a plaintext with a paillier public key.
1762 * @param public_key Public key to use.
1763 * @param m Plaintext to encrypt.
1764 * @param desired_ops How many homomorphic ops the caller intends to use
1765 * @param[out] ciphertext Encrytion of @a plaintext with @a public_key.
1766 * @return guaranteed number of supported homomorphic operations >= 1,
1767 * or desired_ops, in case that is lower,
1768 * or -1 if less than one homomorphic operation is possible
1771 GNUNET_CRYPTO_paillier_encrypt (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1774 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext);
1778 * Decrypt a paillier ciphertext with a private key.
1780 * @param private_key Private key to use for decryption.
1781 * @param public_key Public key to use for decryption.
1782 * @param ciphertext Ciphertext to decrypt.
1783 * @param[out] m Decryption of @a ciphertext with @private_key.
1786 GNUNET_CRYPTO_paillier_decrypt (const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key,
1787 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1788 const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
1793 * Compute a ciphertext that represents the sum of the plaintext in @a x1 and @a x2
1795 * Note that this operation can only be done a finite number of times
1796 * before an overflow occurs.
1798 * @param public_key Public key to use for encryption.
1799 * @param c1 Paillier cipher text.
1800 * @param c2 Paillier cipher text.
1801 * @param[out] result Result of the homomorphic operation.
1802 * @return #GNUNET_OK if the result could be computed,
1803 * #GNUNET_SYSERR if no more homomorphic operations are remaining.
1806 GNUNET_CRYPTO_paillier_hom_add (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1807 const struct GNUNET_CRYPTO_PaillierCiphertext *c1,
1808 const struct GNUNET_CRYPTO_PaillierCiphertext *c2,
1809 struct GNUNET_CRYPTO_PaillierCiphertext *result);
1813 * Get the number of remaining supported homomorphic operations.
1815 * @param c Paillier cipher text.
1816 * @return the number of remaining homomorphic operations
1819 GNUNET_CRYPTO_paillier_hom_get_remaining (const struct GNUNET_CRYPTO_PaillierCiphertext *c);
1822 /* ********* Chaum-style RSA-based blind signatures ******************* */
1828 * The private information of an RSA key pair.
1830 struct GNUNET_CRYPTO_RsaPrivateKey;
1833 * The public information of an RSA key pair.
1835 struct GNUNET_CRYPTO_RsaPublicKey;
1838 * Constant-size pre-secret for blinding key generation.
1840 struct GNUNET_CRYPTO_RsaBlindingKeySecret
1843 * Bits used to generate the blinding key. 256 bits
1844 * of entropy is enough.
1846 uint32_t pre_secret[8] GNUNET_PACKED;
1850 * @brief an RSA signature
1852 struct GNUNET_CRYPTO_RsaSignature;
1856 * Create a new private key. Caller must free return value.
1858 * @param len length of the key in bits (i.e. 2048)
1859 * @return fresh private key
1861 struct GNUNET_CRYPTO_RsaPrivateKey *
1862 GNUNET_CRYPTO_rsa_private_key_create (unsigned int len);
1866 * Free memory occupied by the private key.
1868 * @param key pointer to the memory to free
1871 GNUNET_CRYPTO_rsa_private_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *key);
1875 * Encode the private key in a format suitable for
1876 * storing it into a file.
1878 * @param key the private key
1879 * @param[out] buffer set to a buffer with the encoded key
1880 * @return size of memory allocatedin @a buffer
1883 GNUNET_CRYPTO_rsa_private_key_encode (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
1888 * Decode the private key from the data-format back
1889 * to the "normal", internal format.
1891 * @param buf the buffer where the private key data is stored
1892 * @param len the length of the data in @a buf
1893 * @return NULL on error
1895 struct GNUNET_CRYPTO_RsaPrivateKey *
1896 GNUNET_CRYPTO_rsa_private_key_decode (const char *buf,
1901 * Duplicate the given private key
1903 * @param key the private key to duplicate
1904 * @return the duplicate key; NULL upon error
1906 struct GNUNET_CRYPTO_RsaPrivateKey *
1907 GNUNET_CRYPTO_rsa_private_key_dup (const struct GNUNET_CRYPTO_RsaPrivateKey *key);
1911 * Extract the public key of the given private key.
1913 * @param priv the private key
1914 * @retur NULL on error, otherwise the public key
1916 struct GNUNET_CRYPTO_RsaPublicKey *
1917 GNUNET_CRYPTO_rsa_private_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey *priv);
1921 * Compute hash over the public key.
1923 * @param key public key to hash
1924 * @param hc where to store the hash code
1927 GNUNET_CRYPTO_rsa_public_key_hash (const struct GNUNET_CRYPTO_RsaPublicKey *key,
1928 struct GNUNET_HashCode *hc);
1932 * Obtain the length of the RSA key in bits.
1934 * @param key the public key to introspect
1935 * @return length of the key in bits
1938 GNUNET_CRYPTO_rsa_public_key_len (const struct GNUNET_CRYPTO_RsaPublicKey *key);
1942 * Free memory occupied by the public key.
1944 * @param key pointer to the memory to free
1947 GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_RsaPublicKey *key);
1951 * Encode the public key in a format suitable for
1952 * storing it into a file.
1954 * @param key the private key
1955 * @param[out] buffer set to a buffer with the encoded key
1956 * @return size of memory allocated in @a buffer
1959 GNUNET_CRYPTO_rsa_public_key_encode (const struct GNUNET_CRYPTO_RsaPublicKey *key,
1964 * Decode the public key from the data-format back
1965 * to the "normal", internal format.
1967 * @param buf the buffer where the public key data is stored
1968 * @param len the length of the data in @a buf
1969 * @return NULL on error
1971 struct GNUNET_CRYPTO_RsaPublicKey *
1972 GNUNET_CRYPTO_rsa_public_key_decode (const char *buf,
1977 * Duplicate the given public key
1979 * @param key the public key to duplicate
1980 * @return the duplicate key; NULL upon error
1982 struct GNUNET_CRYPTO_RsaPublicKey *
1983 GNUNET_CRYPTO_rsa_public_key_dup (const struct GNUNET_CRYPTO_RsaPublicKey *key);
1987 * Compare the values of two signatures.
1989 * @param s1 one signature
1990 * @param s2 the other signature
1991 * @return 0 if the two are equal
1994 GNUNET_CRYPTO_rsa_signature_cmp (struct GNUNET_CRYPTO_RsaSignature *s1,
1995 struct GNUNET_CRYPTO_RsaSignature *s2);
1998 * Compare the values of two private keys.
2000 * @param p1 one private key
2001 * @param p2 the other private key
2002 * @return 0 if the two are equal
2005 GNUNET_CRYPTO_rsa_private_key_cmp (struct GNUNET_CRYPTO_RsaPrivateKey *p1,
2006 struct GNUNET_CRYPTO_RsaPrivateKey *p2);
2010 * Compare the values of two public keys.
2012 * @param p1 one public key
2013 * @param p2 the other public key
2014 * @return 0 if the two are equal
2017 GNUNET_CRYPTO_rsa_public_key_cmp (struct GNUNET_CRYPTO_RsaPublicKey *p1,
2018 struct GNUNET_CRYPTO_RsaPublicKey *p2);
2022 * Blinds the given message with the given blinding key
2024 * @param hash hash of the message to sign
2025 * @param bkey the blinding key
2026 * @param pkey the public key of the signer
2027 * @param[out] buf set to a buffer with the blinded message to be signed
2028 * @param[out] buf_size number of bytes stored in @a buf
2029 * @return #GNUNET_YES if successful, #GNUNET_NO if RSA key is malicious
2032 GNUNET_CRYPTO_rsa_blind (const struct GNUNET_HashCode *hash,
2033 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
2034 struct GNUNET_CRYPTO_RsaPublicKey *pkey,
2040 * Sign a blinded value, which must be a full domain hash of a message.
2042 * @param key private key to use for the signing
2043 * @param msg the (blinded) message to sign
2044 * @param msg_len number of bytes in @a msg to sign
2045 * @return NULL on error, signature on success
2047 struct GNUNET_CRYPTO_RsaSignature *
2048 GNUNET_CRYPTO_rsa_sign_blinded (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2054 * Create and sign a full domain hash of a message.
2056 * @param key private key to use for the signing
2057 * @param hash the hash of the message to sign
2058 * @return NULL on error, including a malicious RSA key, signature on success
2060 struct GNUNET_CRYPTO_RsaSignature *
2061 GNUNET_CRYPTO_rsa_sign_fdh (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2062 const struct GNUNET_HashCode *hash);
2066 * Free memory occupied by signature.
2068 * @param sig memory to free
2071 GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_RsaSignature *sig);
2075 * Encode the given signature in a format suitable for storing it into a file.
2077 * @param sig the signature
2078 * @param[out] buffer set to a buffer with the encoded key
2079 * @return size of memory allocated in @a buffer
2082 GNUNET_CRYPTO_rsa_signature_encode (const struct GNUNET_CRYPTO_RsaSignature *sig,
2087 * Decode the signature from the data-format back to the "normal", internal
2090 * @param buf the buffer where the public key data is stored
2091 * @param len the length of the data in @a buf
2092 * @return NULL on error
2094 struct GNUNET_CRYPTO_RsaSignature *
2095 GNUNET_CRYPTO_rsa_signature_decode (const char *buf,
2100 * Duplicate the given rsa signature
2102 * @param sig the signature to duplicate
2103 * @return the duplicate key; NULL upon error
2105 struct GNUNET_CRYPTO_RsaSignature *
2106 GNUNET_CRYPTO_rsa_signature_dup (const struct GNUNET_CRYPTO_RsaSignature *sig);
2110 * Unblind a blind-signed signature. The signature should have been generated
2111 * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with
2112 * #GNUNET_CRYPTO_rsa_blind().
2114 * @param sig the signature made on the blinded signature purpose
2115 * @param bks the blinding key secret used to blind the signature purpose
2116 * @param pkey the public key of the signer
2117 * @return unblinded signature on success, NULL if RSA key is bad or malicious.
2119 struct GNUNET_CRYPTO_RsaSignature *
2120 GNUNET_CRYPTO_rsa_unblind (const struct GNUNET_CRYPTO_RsaSignature *sig,
2121 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
2122 struct GNUNET_CRYPTO_RsaPublicKey *pkey);
2126 * Verify whether the given hash corresponds to the given signature and the
2127 * signature is valid with respect to the given public key.
2129 * @param hash the message to verify to match the @a sig
2130 * @param sig signature that is being validated
2131 * @param public_key public key of the signer
2132 * @returns #GNUNET_YES if ok, #GNUNET_NO if RSA key is malicious, #GNUNET_SYSERR if signature
2135 GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash,
2136 const struct GNUNET_CRYPTO_RsaSignature *sig,
2137 const struct GNUNET_CRYPTO_RsaPublicKey *public_key);
2140 #if 0 /* keep Emacsens' auto-indent happy */
2148 /* ifndef GNUNET_CRYPTO_LIB_H */
2150 /* end of gnunet_crypto_lib.h */