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
57 struct GNUNET_HashCode;
60 * The identity of the host (wraps the signing key of the peer).
62 struct GNUNET_PeerIdentity;
64 #include "gnunet_common.h"
69 * @brief A 512-bit hashcode. These are the default length for GNUnet, using SHA-512.
71 struct GNUNET_HashCode
73 uint32_t bits[512 / 8 / sizeof (uint32_t)]; /* = 16 */
79 * @brief A 256-bit hashcode. Used under special conditions, like when space
80 * is critical and security is not impacted by it.
82 struct GNUNET_ShortHashCode
84 uint32_t bits[256 / 8 / sizeof (uint32_t)]; /* = 8 */
89 * Maximum length of an ECC signature.
90 * Note: round up to multiple of 8 minus 2 for alignment.
92 #define GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH 126
96 * Desired quality level for random numbers.
99 enum GNUNET_CRYPTO_Quality
102 * No good quality of the operation is needed (i.e.,
103 * random numbers can be pseudo-random).
106 GNUNET_CRYPTO_QUALITY_WEAK,
109 * High-quality operations are desired.
112 GNUNET_CRYPTO_QUALITY_STRONG,
115 * Randomness for IVs etc. is required.
118 GNUNET_CRYPTO_QUALITY_NONCE
123 * @brief length of the sessionkey in bytes (256 BIT sessionkey)
125 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
128 * Length of a hash value
130 #define GNUNET_CRYPTO_HASH_LENGTH (512/8)
133 * How many characters (without 0-terminator) are our ASCII-encoded
134 * public keys (ECDSA/EDDSA/ECDHE).
136 #define GNUNET_CRYPTO_PKEY_ASCII_LENGTH 52
139 * @brief 0-terminated ASCII encoding of a struct GNUNET_HashCode.
141 struct GNUNET_CRYPTO_HashAsciiEncoded
143 unsigned char encoding[104];
147 GNUNET_NETWORK_STRUCT_BEGIN
151 * @brief header of what an ECC signature signs
152 * this must be followed by "size - 8" bytes of
153 * the actual signed data
155 struct GNUNET_CRYPTO_EccSignaturePurpose
158 * How many bytes does this signature sign?
159 * (including this purpose header); in network
162 uint32_t size GNUNET_PACKED;
165 * What does this signature vouch for? This
166 * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
167 * constant (from gnunet_signatures.h). In
168 * network byte order!
170 uint32_t purpose GNUNET_PACKED;
176 * @brief an ECC signature using EdDSA.
177 * See https://gnunet.org/ed25519
179 struct GNUNET_CRYPTO_EddsaSignature
185 unsigned char r[256 / 8];
190 unsigned char s[256 / 8];
197 * @brief an ECC signature using ECDSA
199 struct GNUNET_CRYPTO_EcdsaSignature
205 unsigned char r[256 / 8];
210 unsigned char s[256 / 8];
216 * Public ECC key (always for Curve25519) encoded in a format suitable
217 * for network transmission and EdDSA signatures.
219 struct GNUNET_CRYPTO_EddsaPublicKey
222 * Q consists of an x- and a y-value, each mod p (256 bits), given
223 * here in affine coordinates and Ed25519 standard compact format.
225 unsigned char q_y[256 / 8];
231 * Public ECC key (always for Curve25519) encoded in a format suitable
232 * for network transmission and ECDSA signatures.
234 struct GNUNET_CRYPTO_EcdsaPublicKey
237 * Q consists of an x- and a y-value, each mod p (256 bits), given
238 * here in affine coordinates and Ed25519 standard compact format.
240 unsigned char q_y[256 / 8];
246 * The identity of the host (wraps the signing key of the peer).
248 struct GNUNET_PeerIdentity
250 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
255 * Public ECC key (always for Curve25519) encoded in a format suitable
256 * for network transmission and encryption (ECDH),
257 * See http://cr.yp.to/ecdh.html
259 struct GNUNET_CRYPTO_EcdhePublicKey
262 * Q consists of an x- and a y-value, each mod p (256 bits), given
263 * here in affine coordinates and Ed25519 standard compact format.
265 unsigned char q_y[256 / 8];
270 * Private ECC key encoded for transmission. To be used only for ECDH
271 * key exchange (ECDHE to be precise).
273 struct GNUNET_CRYPTO_EcdhePrivateKey
276 * d is a value mod n, where n has at most 256 bits.
278 unsigned char d[256 / 8];
283 * Private ECC key encoded for transmission. To be used only for ECDSA
286 struct GNUNET_CRYPTO_EcdsaPrivateKey
289 * d is a value mod n, where n has at most 256 bits.
291 unsigned char d[256 / 8];
296 * Private ECC key encoded for transmission. To be used only for EdDSA
299 struct GNUNET_CRYPTO_EddsaPrivateKey
302 * d is a value mod n, where n has at most 256 bits.
304 unsigned char d[256 / 8];
310 * @brief type for session keys
312 struct GNUNET_CRYPTO_SymmetricSessionKey
315 * Actual key for AES.
317 unsigned char aes_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
320 * Actual key for TwoFish.
322 unsigned char twofish_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
326 GNUNET_NETWORK_STRUCT_END
329 * @brief IV for sym cipher
331 * NOTE: must be smaller (!) in size than the
332 * `struct GNUNET_HashCode`.
334 struct GNUNET_CRYPTO_SymmetricInitializationVector
336 unsigned char aes_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
338 unsigned char twofish_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
343 * @brief type for (message) authentication keys
345 struct GNUNET_CRYPTO_AuthKey
347 unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
352 * Size of paillier plain texts and public keys.
353 * Private keys and ciphertexts are twice this size.
355 #define GNUNET_CRYPTO_PAILLIER_BITS 2048
359 * Paillier public key.
361 struct GNUNET_CRYPTO_PaillierPublicKey
366 unsigned char n[GNUNET_CRYPTO_PAILLIER_BITS / 8];
371 * Paillier public key.
373 struct GNUNET_CRYPTO_PaillierPrivateKey
376 * Lambda-component of the private key.
378 unsigned char lambda[GNUNET_CRYPTO_PAILLIER_BITS / 8];
380 * Mu-component of the private key.
382 unsigned char mu[GNUNET_CRYPTO_PAILLIER_BITS / 8];
387 * Paillier ciphertext.
389 struct GNUNET_CRYPTO_PaillierCiphertext
392 * Guaranteed minimum number of homomorphic operations with this ciphertext,
393 * in network byte order (NBO).
395 int32_t remaining_ops GNUNET_PACKED;
398 * The bits of the ciphertext.
400 unsigned char bits[GNUNET_CRYPTO_PAILLIER_BITS * 2 / 8];
404 /* **************** Functions and Macros ************* */
408 * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator
411 * @param seed the seed to use
414 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
419 * Calculate the checksum of a buffer in one step.
421 * @param buf buffer to calculate CRC over
422 * @param len number of bytes in @a buf
426 GNUNET_CRYPTO_crc8_n (const void *buf,
431 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
433 * @param sum current sum, initially 0
434 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
435 * @param len number of bytes in @a buf, must be multiple of 2
436 * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish to get actual crc16)
439 GNUNET_CRYPTO_crc16_step (uint32_t sum,
445 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
447 * @param sum cummulative sum
448 * @return crc16 value
451 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
456 * Calculate the checksum of a buffer in one step.
458 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
459 * @param len number of bytes in @a buf, must be multiple of 2
460 * @return crc16 value
463 GNUNET_CRYPTO_crc16_n (const void *buf,
471 * Compute the CRC32 checksum for the first len
472 * bytes of the buffer.
474 * @param buf the data over which we're taking the CRC
475 * @param len the length of the buffer @a buf in bytes
476 * @return the resulting CRC32 checksum
479 GNUNET_CRYPTO_crc32_n (const void *buf,
485 * Fill block with a random values.
487 * @param mode desired quality of the random number
488 * @param buffer the buffer to fill
489 * @param length buffer length
492 GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode,
498 * Produce a random value.
500 * @param mode desired quality of the random number
501 * @param i the upper limit (exclusive) for the random number
502 * @return a random value in the interval [0,@a i) (exclusive).
505 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode,
511 * Random on unsigned 64-bit values.
513 * @param mode desired quality of the random number
514 * @param max value returned will be in range [0,@a max) (exclusive)
515 * @return random 64-bit number
518 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
524 * Get an array with a random permutation of the
526 * @param mode #GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used,
527 * #GNUNET_CRYPTO_QUALITY_WEAK or #GNUNET_CRYPTO_QUALITY_NONCE otherwise
528 * @param n the size of the array
529 * @return the permutation array (allocated from heap)
532 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode,
538 * Create a new random session key.
540 * @param key key to initialize
543 GNUNET_CRYPTO_symmetric_create_session_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key);
548 * Encrypt a block using a symmetric sessionkey.
550 * @param block the block to encrypt
551 * @param size the size of the @a block
552 * @param sessionkey the key used to encrypt
553 * @param iv the initialization vector to use, use INITVALUE
555 * @return the size of the encrypted block, -1 for errors
558 GNUNET_CRYPTO_symmetric_encrypt (const void *block,
560 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
561 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
567 * Decrypt a given block using a symmetric sessionkey.
569 * @param block the data to decrypt, encoded as returned by encrypt
570 * @param size how big is the block?
571 * @param sessionkey the key used to decrypt
572 * @param iv the initialization vector to use
573 * @param result address to store the result at
574 * @return -1 on failure, size of decrypted block on success
577 GNUNET_CRYPTO_symmetric_decrypt (const void *block,
579 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
580 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
586 * @brief Derive an IV
587 * @param iv initialization vector
588 * @param skey session key
589 * @param salt salt for the derivation
590 * @param salt_len size of the @a salt
591 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
594 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
595 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
597 size_t salt_len, ...);
601 * @brief Derive an IV
602 * @param iv initialization vector
603 * @param skey session key
604 * @param salt salt for the derivation
605 * @param salt_len size of the @a salt
606 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
609 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
610 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
618 * Convert hash to ASCII encoding.
619 * @param block the hash code
620 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
621 * safely cast to char*, a '\\0' termination is set).
624 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block,
625 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
630 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
632 * @param enc the encoding
633 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
634 * @param result where to store the hash code
635 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
638 GNUNET_CRYPTO_hash_from_string2 (const char *enc,
640 struct GNUNET_HashCode *result);
645 * Convert ASCII encoding back to `struct GNUNET_HashCode`
647 * @param enc the encoding
648 * @param result where to store the hash code
649 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
651 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
652 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
658 * Compute the distance between 2 hashcodes. The
659 * computation must be fast, not involve @a a[0] or @a a[4] (they're used
660 * elsewhere), and be somewhat consistent. And of course, the result
661 * should be a positive number.
663 * @param a some hash code
664 * @param b some hash code
665 * @return number between 0 and UINT32_MAX
668 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a,
669 const struct GNUNET_HashCode *b);
674 * Compute hash of a given block.
676 * @param block the data to hash
677 * @param size size of the @a block
678 * @param ret pointer to where to write the hashcode
681 GNUNET_CRYPTO_hash (const void *block,
683 struct GNUNET_HashCode *ret);
687 * Context for cummulative hashing.
689 struct GNUNET_HashContext;
693 * Start incremental hashing operation.
695 * @return context for incremental hash computation
697 struct GNUNET_HashContext *
698 GNUNET_CRYPTO_hash_context_start (void);
702 * Add data to be hashed.
704 * @param hc cummulative hash context
705 * @param buf data to add
706 * @param size number of bytes in @a buf
709 GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
715 * Finish the hash computation.
717 * @param hc hash context to use, is freed in the process
718 * @param r_hash where to write the latest / final hash code
721 GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
722 struct GNUNET_HashCode *r_hash);
726 * Abort hashing, do not bother calculating final result.
728 * @param hc hash context to destroy
731 GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc);
736 * Calculate HMAC of a message (RFC 2104)
738 * @param key secret key
739 * @param plaintext input plaintext
740 * @param plaintext_len length of @a plaintext
741 * @param hmac where to store the hmac
744 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
745 const void *plaintext,
746 size_t plaintext_len,
747 struct GNUNET_HashCode *hmac);
751 * Function called once the hash computation over the
752 * specified file has completed.
755 * @param res resulting hash, NULL on error
758 (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
759 const struct GNUNET_HashCode *res);
763 * Handle to file hashing operation.
765 struct GNUNET_CRYPTO_FileHashContext;
770 * Compute the hash of an entire file.
772 * @param priority scheduling priority to use
773 * @param filename name of file to hash
774 * @param blocksize number of bytes to process in one task
775 * @param callback function to call upon completion
776 * @param callback_cls closure for @a callback
777 * @return NULL on (immediate) errror
779 struct GNUNET_CRYPTO_FileHashContext *
780 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
781 const char *filename,
783 GNUNET_CRYPTO_HashCompletedCallback callback,
788 * Cancel a file hashing operation.
790 * @param fhc operation to cancel (callback must not yet have been invoked)
793 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
798 * Create a random hash code.
800 * @param mode desired quality level
801 * @param result hash code that is randomized
804 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
805 struct GNUNET_HashCode *result);
810 * compute @a result = @a b - @a a
812 * @param a some hash code
813 * @param b some hash code
814 * @param result set to @a b - @a a
817 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
818 const struct GNUNET_HashCode *b,
819 struct GNUNET_HashCode *result);
824 * compute @a result = @a a + @a delta
826 * @param a some hash code
827 * @param delta some hash code
828 * @param result set to @a a + @a delta
831 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a,
832 const struct GNUNET_HashCode *delta,
833 struct GNUNET_HashCode *result);
838 * compute result = a ^ b
840 * @param a some hash code
841 * @param b some hash code
842 * @param result set to @a a ^ @a b
845 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
846 const struct GNUNET_HashCode *b,
847 struct GNUNET_HashCode *result);
852 * Convert a hashcode into a key.
854 * @param hc hash code that serves to generate the key
855 * @param skey set to a valid session key
856 * @param iv set to a valid initialization vector
859 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
860 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
861 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv);
866 * Obtain a bit from a hashcode.
868 * @param code the `struct GNUNET_HashCode` to index bit-wise
869 * @param bit index into the hashcode, [0...159]
870 * @return Bit \a bit from hashcode \a code, -1 for invalid index
873 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code,
879 * Determine how many low order bits match in two
880 * `struct GNUNET_HashCodes`. i.e. - 010011 and 011111 share
881 * the first two lowest order bits, and therefore the
882 * return value is two (NOT XOR distance, nor how many
883 * bits match absolutely!).
885 * @param first the first hashcode
886 * @param second the hashcode to compare first to
887 * @return the number of bits that match
890 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
891 const struct GNUNET_HashCode *second);
896 * Compare function for HashCodes, producing a total ordering
899 * @param h1 some hash code
900 * @param h2 some hash code
901 * @return 1 if @a h1 > @a h2, -1 if @a h1 < @a h2 and 0 if @a h1 == @a h2.
904 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
905 const struct GNUNET_HashCode *h2);
910 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
911 * in the XOR metric (Kademlia).
913 * @param h1 some hash code
914 * @param h2 some hash code
915 * @param target some hash code
916 * @return -1 if @a h1 is closer, 1 if @a h2 is closer and 0 if @a h1== @a h2.
919 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
920 const struct GNUNET_HashCode *h2,
921 const struct GNUNET_HashCode *target);
926 * @brief Derive an authentication key
927 * @param key authentication key
928 * @param rkey root key
930 * @param salt_len size of the salt
931 * @param argp pair of void * & size_t for context chunks, terminated by NULL
934 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
935 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
936 const void *salt, size_t salt_len,
942 * @brief Derive an authentication key
943 * @param key authentication key
944 * @param rkey root key
946 * @param salt_len size of the salt
947 * @param ... pair of void * & size_t for context chunks, terminated by NULL
950 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
951 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
952 const void *salt, size_t salt_len,
959 * @param result buffer for the derived key, allocated by caller
960 * @param out_len desired length of the derived key
961 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
962 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
964 * @param xts_len length of @a xts
965 * @param skm source key material
966 * @param skm_len length of @a skm
967 * @param ... pair of void * & size_t for context chunks, terminated by NULL
968 * @return #GNUNET_YES on success
971 GNUNET_CRYPTO_hkdf (void *result,
985 * @param result buffer for the derived key, allocated by caller
986 * @param out_len desired length of the derived key
987 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
988 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
990 * @param xts_len length of @a xts
991 * @param skm source key material
992 * @param skm_len length of @a skm
993 * @param argp va_list of void * & size_t pairs for context chunks
994 * @return #GNUNET_YES on success
997 GNUNET_CRYPTO_hkdf_v (void *result,
1010 * @param result buffer for the derived key, allocated by caller
1011 * @param out_len desired length of the derived key
1013 * @param xts_len length of @a xts
1014 * @param skm source key material
1015 * @param skm_len length of @a skm
1016 * @param argp va_list of void * & size_t pairs for context chunks
1017 * @return #GNUNET_YES on success
1020 GNUNET_CRYPTO_kdf_v (void *result,
1030 * Deterministically generate a pseudo-random number uniformly from the
1031 * integers modulo a libgcrypt mpi.
1033 * @param[out] r MPI value set to the FDH
1034 * @param n MPI to work modulo
1036 * @param xts_len length of @a xts
1037 * @param skm source key material
1038 * @param skm_len length of @a skm
1039 * @param ctx context string
1042 GNUNET_CRYPTO_kdf_mod_mpi (gcry_mpi_t *r,
1044 const void *xts, size_t xts_len,
1045 const void *skm, size_t skm_len,
1052 * @param result buffer for the derived key, allocated by caller
1053 * @param out_len desired length of the derived key
1055 * @param xts_len length of @a xts
1056 * @param skm source key material
1057 * @param skm_len length of @a skm
1058 * @param ... void * & size_t pairs for context chunks
1059 * @return #GNUNET_YES on success
1062 GNUNET_CRYPTO_kdf (void *result,
1073 * Extract the public key for the given private key.
1075 * @param priv the private key
1076 * @param pub where to write the public key
1079 GNUNET_CRYPTO_ecdsa_key_get_public (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1080 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1084 * Extract the public key for the given private key.
1086 * @param priv the private key
1087 * @param pub where to write the public key
1090 GNUNET_CRYPTO_eddsa_key_get_public (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1091 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1097 * Extract the public key for the given private key.
1099 * @param priv the private key
1100 * @param pub where to write the public key
1103 GNUNET_CRYPTO_ecdhe_key_get_public (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1104 struct GNUNET_CRYPTO_EcdhePublicKey *pub);
1108 * Convert a public key to a string.
1110 * @param pub key to convert
1111 * @return string representing @a pub
1114 GNUNET_CRYPTO_ecdsa_public_key_to_string (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1118 * Convert a public key to a string.
1120 * @param pub key to convert
1121 * @return string representing @a pub
1124 GNUNET_CRYPTO_eddsa_public_key_to_string (const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1128 * Convert a string representing a public key to a public key.
1130 * @param enc encoded public key
1131 * @param enclen number of bytes in @a enc (without 0-terminator)
1132 * @param pub where to store the public key
1133 * @return #GNUNET_OK on success
1136 GNUNET_CRYPTO_ecdsa_public_key_from_string (const char *enc,
1138 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1142 * Convert a string representing a private key to a private key.
1144 * @param enc encoded public key
1145 * @param enclen number of bytes in @a enc (without 0-terminator)
1146 * @param priv where to store the private key
1147 * @return #GNUNET_OK on success
1150 GNUNET_CRYPTO_eddsa_private_key_from_string (const char *enc,
1152 struct GNUNET_CRYPTO_EddsaPrivateKey *pub);
1156 * Convert a string representing a public key to a public key.
1158 * @param enc encoded public key
1159 * @param enclen number of bytes in @a enc (without 0-terminator)
1160 * @param pub where to store the public key
1161 * @return #GNUNET_OK on success
1164 GNUNET_CRYPTO_eddsa_public_key_from_string (const char *enc,
1166 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1171 * Create a new private key by reading it from a file. If the
1172 * files does not exist, create a new key and write it to the
1173 * file. Caller must free return value. Note that this function
1174 * can not guarantee that another process might not be trying
1175 * the same operation on the same file at the same time.
1176 * If the contents of the file
1177 * are invalid the old file is deleted and a fresh key is
1180 * @param filename name of file to use to store the key
1181 * @return new private key, NULL on error (for example,
1182 * permission denied); free using #GNUNET_free
1184 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1185 GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename);
1190 * Create a new private key by reading it from a file. If the
1191 * files does not exist, create a new key and write it to the
1192 * file. Caller must free return value. Note that this function
1193 * can not guarantee that another process might not be trying
1194 * the same operation on the same file at the same time.
1195 * If the contents of the file
1196 * are invalid the old file is deleted and a fresh key is
1199 * @param filename name of file to use to store the key
1200 * @return new private key, NULL on error (for example,
1201 * permission denied); free using #GNUNET_free
1203 struct GNUNET_CRYPTO_EddsaPrivateKey *
1204 GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename);
1208 * Forward declaration to simplify #include-structure.
1210 struct GNUNET_CONFIGURATION_Handle;
1215 * Create a new private key by reading our peer's key from
1216 * the file specified in the configuration.
1218 * @param cfg the configuration to use
1219 * @return new private key, NULL on error (for example,
1220 * permission denied); free using #GNUNET_free
1222 struct GNUNET_CRYPTO_EddsaPrivateKey *
1223 GNUNET_CRYPTO_eddsa_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
1228 * Create a new private key. Caller must free return value.
1230 * @return fresh private key; free using #GNUNET_free
1232 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1233 GNUNET_CRYPTO_ecdsa_key_create (void);
1238 * Create a new private key. Caller must free return value.
1240 * @return fresh private key; free using #GNUNET_free
1242 struct GNUNET_CRYPTO_EddsaPrivateKey *
1243 GNUNET_CRYPTO_eddsa_key_create (void);
1248 * Create a new private key. Caller must free return value.
1250 * @return fresh private key; free using #GNUNET_free
1252 struct GNUNET_CRYPTO_EcdhePrivateKey *
1253 GNUNET_CRYPTO_ecdhe_key_create (void);
1258 * Clear memory that was used to store a private key.
1260 * @param pk location of the key
1263 GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk);
1268 * Clear memory that was used to store a private key.
1270 * @param pk location of the key
1273 GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk);
1278 * Clear memory that was used to store a private key.
1280 * @param pk location of the key
1283 GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1288 * Get the shared private key we use for anonymous users.
1290 * @return "anonymous" private key; do not free
1292 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
1293 GNUNET_CRYPTO_ecdsa_key_get_anonymous (void);
1298 * Setup a hostkey file for a peer given the name of the
1299 * configuration file (!). This function is used so that
1300 * at a later point code can be certain that reading a
1301 * hostkey is fast (for example in time-dependent testcases).
1303 * @param cfg_name name of the configuration file to use
1306 GNUNET_CRYPTO_eddsa_setup_hostkey (const char *cfg_name);
1311 * Retrieve the identity of the host's peer.
1313 * @param cfg configuration to use
1314 * @param dst pointer to where to write the peer identity
1315 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
1316 * could not be retrieved
1319 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
1320 struct GNUNET_PeerIdentity *dst);
1324 * Compare two Peer Identities.
1326 * @param first first peer identity
1327 * @param second second peer identity
1328 * @return bigger than 0 if first > second,
1329 * 0 if they are the same
1330 * smaller than 0 if second > first
1333 GNUNET_CRYPTO_cmp_peer_identity (const struct GNUNET_PeerIdentity *first,
1334 const struct GNUNET_PeerIdentity *second);
1338 * Internal structure used to cache pre-calculated values for DLOG calculation.
1340 struct GNUNET_CRYPTO_EccDlogContext;
1344 * Point on a curve (always for Curve25519) encoded in a format suitable
1345 * for network transmission (ECDH), see http://cr.yp.to/ecdh.html.
1347 struct GNUNET_CRYPTO_EccPoint
1350 * Q consists of an x- and a y-value, each mod p (256 bits), given
1351 * here in affine coordinates and Ed25519 standard compact format.
1353 unsigned char q_y[256 / 8];
1358 * Do pre-calculation for ECC discrete logarithm for small factors.
1360 * @param max maximum value the factor can be
1361 * @param mem memory to use (should be smaller than @a max), must not be zero.
1362 * @return NULL on error
1364 struct GNUNET_CRYPTO_EccDlogContext *
1365 GNUNET_CRYPTO_ecc_dlog_prepare (unsigned int max,
1370 * Calculate ECC discrete logarithm for small factors.
1371 * Opposite of #GNUNET_CRYPTO_ecc_dexp().
1373 * @param dlc precalculated values, determine range of factors
1374 * @param input point on the curve to factor
1375 * @return INT_MAX if dlog failed, otherwise the factor
1378 GNUNET_CRYPTO_ecc_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc,
1379 gcry_mpi_point_t input);
1383 * Multiply the generator g of the elliptic curve by @a val
1384 * to obtain the point on the curve representing @a val.
1385 * Afterwards, point addition will correspond to integer
1386 * addition. #GNUNET_CRYPTO_ecc_dlog() can be used to
1387 * convert a point back to an integer (as long as the
1388 * integer is smaller than the MAX of the @a edc context).
1390 * @param edc calculation context for ECC operations
1391 * @param val value to encode into a point
1392 * @return representation of the value as an ECC point,
1393 * must be freed using #GNUNET_CRYPTO_ecc_free()
1396 GNUNET_CRYPTO_ecc_dexp (struct GNUNET_CRYPTO_EccDlogContext *edc,
1401 * Multiply the generator g of the elliptic curve by @a val
1402 * to obtain the point on the curve representing @a val.
1404 * @param edc calculation context for ECC operations
1405 * @param val (positive) value to encode into a point
1406 * @return representation of the value as an ECC point,
1407 * must be freed using #GNUNET_CRYPTO_ecc_free()
1410 GNUNET_CRYPTO_ecc_dexp_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1415 * Multiply the point @a p on the elliptic curve by @a val.
1417 * @param edc calculation context for ECC operations
1418 * @param p point to multiply
1419 * @param val (positive) value to encode into a point
1420 * @return representation of the value as an ECC point,
1421 * must be freed using #GNUNET_CRYPTO_ecc_free()
1424 GNUNET_CRYPTO_ecc_pmul_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1430 * Convert point value to binary representation.
1432 * @param edc calculation context for ECC operations
1433 * @param point computational point representation
1434 * @param[out] bin binary point representation
1437 GNUNET_CRYPTO_ecc_point_to_bin (struct GNUNET_CRYPTO_EccDlogContext *edc,
1438 gcry_mpi_point_t point,
1439 struct GNUNET_CRYPTO_EccPoint *bin);
1443 * Convert binary representation of a point to computational representation.
1445 * @param edc calculation context for ECC operations
1446 * @param bin binary point representation
1447 * @return computational representation
1450 GNUNET_CRYPTO_ecc_bin_to_point (struct GNUNET_CRYPTO_EccDlogContext *edc,
1451 const struct GNUNET_CRYPTO_EccPoint *bin);
1455 * Add two points on the elliptic curve.
1457 * @param edc calculation context for ECC operations
1458 * @param a some value
1459 * @param b some value
1460 * @return @a a + @a b, must be freed using #GNUNET_CRYPTO_ecc_free()
1463 GNUNET_CRYPTO_ecc_add (struct GNUNET_CRYPTO_EccDlogContext *edc,
1465 gcry_mpi_point_t b);
1469 * Obtain a random point on the curve and its
1470 * additive inverse. Both returned values
1471 * must be freed using #GNUNET_CRYPTO_ecc_free().
1473 * @param edc calculation context for ECC operations
1474 * @param[out] r set to a random point on the curve
1475 * @param[out] r_inv set to the additive inverse of @a r
1478 GNUNET_CRYPTO_ecc_rnd (struct GNUNET_CRYPTO_EccDlogContext *edc,
1479 gcry_mpi_point_t *r,
1480 gcry_mpi_point_t *r_inv);
1484 * Obtain a random scalar for point multiplication on the curve and
1485 * its multiplicative inverse.
1487 * @param edc calculation context for ECC operations
1488 * @param[out] r set to a random scalar on the curve
1489 * @param[out] r_inv set to the multiplicative inverse of @a r
1492 GNUNET_CRYPTO_ecc_rnd_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1498 * Generate a random value mod n.
1500 * @param edc ECC context
1501 * @return random value mod n.
1504 GNUNET_CRYPTO_ecc_random_mod_n (struct GNUNET_CRYPTO_EccDlogContext *edc);
1508 * Free a point value returned by the API.
1510 * @param p point to free
1513 GNUNET_CRYPTO_ecc_free (gcry_mpi_point_t p);
1517 * Release precalculated values.
1519 * @param dlc dlog context
1522 GNUNET_CRYPTO_ecc_dlog_release (struct GNUNET_CRYPTO_EccDlogContext *dlc);
1527 * Derive key material from a public and a private ECC key.
1529 * @param priv private key to use for the ECDH (x)
1530 * @param pub public key to use for the ECDH (yG)
1531 * @param key_material where to write the key material (xyG)
1532 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1535 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1536 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1537 struct GNUNET_HashCode *key_material);
1542 * Derive key material from a ECDH public key and a private EdDSA key.
1543 * Dual to #GNUNET_CRRYPTO_ecdh_eddsa.
1545 * @param priv private key from EdDSA 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 H(h(x)yG)
1548 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1551 GNUNET_CRYPTO_eddsa_ecdh (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1552 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1553 struct GNUNET_HashCode *key_material);
1557 * Derive key material from a ECDH public key and a private ECDSA key.
1558 * Dual to #GNUNET_CRRYPTO_ecdh_ecdsa.
1560 * @param priv private key from ECDSA to use for the ECDH (x)
1561 * @param pub public key to use for the ECDH (yG)
1562 * @param key_material where to write the key material H(h(x)yG)
1563 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1566 GNUNET_CRYPTO_ecdsa_ecdh (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1567 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1568 struct GNUNET_HashCode *key_material);
1573 * Derive key material from a EdDSA public key and a private ECDH key.
1574 * Dual to #GNUNET_CRRYPTO_eddsa_ecdh.
1576 * @param priv private key to use for the ECDH (y)
1577 * @param pub public key from EdDSA to use for the ECDH (X=h(x)G)
1578 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1579 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1582 GNUNET_CRYPTO_ecdh_eddsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1583 const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
1584 struct GNUNET_HashCode *key_material);
1588 * Derive key material from a EcDSA public key and a private ECDH key.
1589 * Dual to #GNUNET_CRRYPTO_ecdsa_ecdh.
1591 * @param priv private key to use for the ECDH (y)
1592 * @param pub public key from ECDSA to use for the ECDH (X=h(x)G)
1593 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1594 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1597 GNUNET_CRYPTO_ecdh_ecdsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1598 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1599 struct GNUNET_HashCode *key_material);
1604 * EdDSA sign a given block.
1606 * @param priv private key to use for the signing
1607 * @param purpose what to sign (size, purpose)
1608 * @param sig where to write the signature
1609 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1612 GNUNET_CRYPTO_eddsa_sign (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1613 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1614 struct GNUNET_CRYPTO_EddsaSignature *sig);
1619 * ECDSA Sign a given block.
1621 * @param priv private key to use for the signing
1622 * @param purpose what to sign (size, purpose)
1623 * @param sig where to write the signature
1624 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1627 GNUNET_CRYPTO_ecdsa_sign (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1628 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1629 struct GNUNET_CRYPTO_EcdsaSignature *sig);
1633 * Verify EdDSA signature.
1635 * @param purpose what is the purpose that the signature should have?
1636 * @param validate block to validate (size, purpose, data)
1637 * @param sig signature that is being validated
1638 * @param pub public key of the signer
1639 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1642 GNUNET_CRYPTO_eddsa_verify (uint32_t purpose,
1643 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1644 const struct GNUNET_CRYPTO_EddsaSignature *sig,
1645 const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1651 * Verify ECDSA signature.
1653 * @param purpose what is the purpose that the signature should have?
1654 * @param validate block to validate (size, purpose, data)
1655 * @param sig signature that is being validated
1656 * @param pub public key of the signer
1657 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1660 GNUNET_CRYPTO_ecdsa_verify (uint32_t purpose,
1661 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1662 const struct GNUNET_CRYPTO_EcdsaSignature *sig,
1663 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1668 * Derive a private key from a given private key and a label.
1669 * Essentially calculates a private key 'h = H(l,P) * d mod n'
1670 * where n is the size of the ECC group and P is the public
1671 * key associated with the private key 'd'.
1673 * @param priv original private key
1674 * @param label label to use for key deriviation
1675 * @param context additional context to use for HKDF of 'h';
1676 * typically the name of the subsystem/application
1677 * @return derived private key
1679 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1680 GNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1682 const char *context);
1687 * Derive a public key from a given public key and a label.
1688 * Essentially calculates a public key 'V = H(l,P) * P'.
1690 * @param pub original public key
1691 * @param label label to use for key deriviation
1692 * @param context additional context to use for HKDF of 'h'.
1693 * typically the name of the subsystem/application
1694 * @param result where to write the derived public key
1697 GNUNET_CRYPTO_ecdsa_public_key_derive (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1699 const char *context,
1700 struct GNUNET_CRYPTO_EcdsaPublicKey *result);
1704 * Output the given MPI value to the given buffer in network
1705 * byte order. The MPI @a val may not be negative.
1707 * @param buf where to output to
1708 * @param size number of bytes in @a buf
1709 * @param val value to write to @a buf
1712 GNUNET_CRYPTO_mpi_print_unsigned (void *buf,
1718 * Convert data buffer into MPI value.
1719 * The buffer is interpreted as network
1720 * byte order, unsigned integer.
1722 * @param result where to store MPI value (allocated)
1723 * @param data raw data (GCRYMPI_FMT_USG)
1724 * @param size number of bytes in @a data
1727 GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result,
1733 * Create a freshly generated paillier public key.
1735 * @param[out] public_key Where to store the public key?
1736 * @param[out] private_key Where to store the private key?
1739 GNUNET_CRYPTO_paillier_create (struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1740 struct GNUNET_CRYPTO_PaillierPrivateKey *private_key);
1744 * Encrypt a plaintext with a paillier public key.
1746 * @param public_key Public key to use.
1747 * @param m Plaintext to encrypt.
1748 * @param desired_ops How many homomorphic ops the caller intends to use
1749 * @param[out] ciphertext Encrytion of @a plaintext with @a public_key.
1750 * @return guaranteed number of supported homomorphic operations >= 1,
1751 * or desired_ops, in case that is lower,
1752 * or -1 if less than one homomorphic operation is possible
1755 GNUNET_CRYPTO_paillier_encrypt (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1758 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext);
1762 * Decrypt a paillier ciphertext with a private key.
1764 * @param private_key Private key to use for decryption.
1765 * @param public_key Public key to use for decryption.
1766 * @param ciphertext Ciphertext to decrypt.
1767 * @param[out] m Decryption of @a ciphertext with @private_key.
1770 GNUNET_CRYPTO_paillier_decrypt (const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key,
1771 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1772 const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
1777 * Compute a ciphertext that represents the sum of the plaintext in @a x1 and @a x2
1779 * Note that this operation can only be done a finite number of times
1780 * before an overflow occurs.
1782 * @param public_key Public key to use for encryption.
1783 * @param c1 Paillier cipher text.
1784 * @param c2 Paillier cipher text.
1785 * @param[out] result Result of the homomorphic operation.
1786 * @return #GNUNET_OK if the result could be computed,
1787 * #GNUNET_SYSERR if no more homomorphic operations are remaining.
1790 GNUNET_CRYPTO_paillier_hom_add (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1791 const struct GNUNET_CRYPTO_PaillierCiphertext *c1,
1792 const struct GNUNET_CRYPTO_PaillierCiphertext *c2,
1793 struct GNUNET_CRYPTO_PaillierCiphertext *result);
1797 * Get the number of remaining supported homomorphic operations.
1799 * @param c Paillier cipher text.
1800 * @return the number of remaining homomorphic operations
1803 GNUNET_CRYPTO_paillier_hom_get_remaining (const struct GNUNET_CRYPTO_PaillierCiphertext *c);
1806 /* ********* Chaum-style RSA-based blind signatures ******************* */
1812 * The private information of an RSA key pair.
1814 struct GNUNET_CRYPTO_RsaPrivateKey;
1817 * The public information of an RSA key pair.
1819 struct GNUNET_CRYPTO_RsaPublicKey;
1822 * Constant-size pre-secret for blinding key generation.
1824 struct GNUNET_CRYPTO_RsaBlindingKeySecret
1827 * Bits used to generate the blinding key. 256 bits
1828 * of entropy is enough.
1830 uint32_t pre_secret[8] GNUNET_PACKED;
1834 * @brief an RSA signature
1836 struct GNUNET_CRYPTO_RsaSignature;
1840 * Create a new private key. Caller must free return value.
1842 * @param len length of the key in bits (i.e. 2048)
1843 * @return fresh private key
1845 struct GNUNET_CRYPTO_RsaPrivateKey *
1846 GNUNET_CRYPTO_rsa_private_key_create (unsigned int len);
1850 * Free memory occupied by the private key.
1852 * @param key pointer to the memory to free
1855 GNUNET_CRYPTO_rsa_private_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *key);
1859 * Encode the private key in a format suitable for
1860 * storing it into a file.
1862 * @param key the private key
1863 * @param[out] buffer set to a buffer with the encoded key
1864 * @return size of memory allocatedin @a buffer
1867 GNUNET_CRYPTO_rsa_private_key_encode (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
1872 * Decode the private key from the data-format back
1873 * to the "normal", internal format.
1875 * @param buf the buffer where the private key data is stored
1876 * @param len the length of the data in @a buf
1877 * @return NULL on error
1879 struct GNUNET_CRYPTO_RsaPrivateKey *
1880 GNUNET_CRYPTO_rsa_private_key_decode (const char *buf,
1885 * Duplicate the given private key
1887 * @param key the private key to duplicate
1888 * @return the duplicate key; NULL upon error
1890 struct GNUNET_CRYPTO_RsaPrivateKey *
1891 GNUNET_CRYPTO_rsa_private_key_dup (const struct GNUNET_CRYPTO_RsaPrivateKey *key);
1895 * Extract the public key of the given private key.
1897 * @param priv the private key
1898 * @retur NULL on error, otherwise the public key
1900 struct GNUNET_CRYPTO_RsaPublicKey *
1901 GNUNET_CRYPTO_rsa_private_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey *priv);
1905 * Compute hash over the public key.
1907 * @param key public key to hash
1908 * @param hc where to store the hash code
1911 GNUNET_CRYPTO_rsa_public_key_hash (const struct GNUNET_CRYPTO_RsaPublicKey *key,
1912 struct GNUNET_HashCode *hc);
1916 * Obtain the length of the RSA key in bits.
1918 * @param key the public key to introspect
1919 * @return length of the key in bits
1922 GNUNET_CRYPTO_rsa_public_key_len (const struct GNUNET_CRYPTO_RsaPublicKey *key);
1926 * Free memory occupied by the public key.
1928 * @param key pointer to the memory to free
1931 GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_RsaPublicKey *key);
1935 * Encode the public key in a format suitable for
1936 * storing it into a file.
1938 * @param key the private key
1939 * @param[out] buffer set to a buffer with the encoded key
1940 * @return size of memory allocated in @a buffer
1943 GNUNET_CRYPTO_rsa_public_key_encode (const struct GNUNET_CRYPTO_RsaPublicKey *key,
1948 * Decode the public key from the data-format back
1949 * to the "normal", internal format.
1951 * @param buf the buffer where the public key data is stored
1952 * @param len the length of the data in @a buf
1953 * @return NULL on error
1955 struct GNUNET_CRYPTO_RsaPublicKey *
1956 GNUNET_CRYPTO_rsa_public_key_decode (const char *buf,
1961 * Duplicate the given public key
1963 * @param key the public key to duplicate
1964 * @return the duplicate key; NULL upon error
1966 struct GNUNET_CRYPTO_RsaPublicKey *
1967 GNUNET_CRYPTO_rsa_public_key_dup (const struct GNUNET_CRYPTO_RsaPublicKey *key);
1971 * Compare the values of two signatures.
1973 * @param s1 one signature
1974 * @param s2 the other signature
1975 * @return 0 if the two are equal
1978 GNUNET_CRYPTO_rsa_signature_cmp (struct GNUNET_CRYPTO_RsaSignature *s1,
1979 struct GNUNET_CRYPTO_RsaSignature *s2);
1982 * Compare the values of two private keys.
1984 * @param p1 one private key
1985 * @param p2 the other private key
1986 * @return 0 if the two are equal
1989 GNUNET_CRYPTO_rsa_private_key_cmp (struct GNUNET_CRYPTO_RsaPrivateKey *p1,
1990 struct GNUNET_CRYPTO_RsaPrivateKey *p2);
1994 * Compare the values of two public keys.
1996 * @param p1 one public key
1997 * @param p2 the other public key
1998 * @return 0 if the two are equal
2001 GNUNET_CRYPTO_rsa_public_key_cmp (struct GNUNET_CRYPTO_RsaPublicKey *p1,
2002 struct GNUNET_CRYPTO_RsaPublicKey *p2);
2006 * Blinds the given message with the given blinding key
2008 * @param hash hash of the message to sign
2009 * @param bkey the blinding key
2010 * @param pkey the public key of the signer
2011 * @param[out] buf set to a buffer with the blinded message to be signed
2012 * @param[out] buf_size number of bytes stored in @a buf
2013 * @return GNUNET_YES if successful, GNUNET_NO if RSA key is malicious
2016 GNUNET_CRYPTO_rsa_blind (const struct GNUNET_HashCode *hash,
2017 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
2018 struct GNUNET_CRYPTO_RsaPublicKey *pkey,
2019 char **buf, size_t *buf_size);
2023 * Sign a blinded value, which must be a full domain hash of a message.
2025 * @param key private key to use for the signing
2026 * @param msg the (blinded) message to sign
2027 * @param msg_len number of bytes in @a msg to sign
2028 * @return NULL on error, signature on success
2030 struct GNUNET_CRYPTO_RsaSignature *
2031 GNUNET_CRYPTO_rsa_sign_blinded (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2032 const void *msg, size_t msg_len);
2036 * Create and sign a full domain hash of a message.
2038 * @param key private key to use for the signing
2039 * @param hash the hash of the message to sign
2040 * @return NULL on error, including a malicious RSA key, signature on success
2042 struct GNUNET_CRYPTO_RsaSignature *
2043 GNUNET_CRYPTO_rsa_sign_fdh (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2044 const struct GNUNET_HashCode *hash);
2048 * Free memory occupied by signature.
2050 * @param sig memory to free
2053 GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_RsaSignature *sig);
2057 * Encode the given signature in a format suitable for storing it into a file.
2059 * @param sig the signature
2060 * @param[out] buffer set to a buffer with the encoded key
2061 * @return size of memory allocated in @a buffer
2064 GNUNET_CRYPTO_rsa_signature_encode (const struct GNUNET_CRYPTO_RsaSignature *sig,
2069 * Decode the signature from the data-format back to the "normal", internal
2072 * @param buf the buffer where the public key data is stored
2073 * @param len the length of the data in @a buf
2074 * @return NULL on error
2076 struct GNUNET_CRYPTO_RsaSignature *
2077 GNUNET_CRYPTO_rsa_signature_decode (const char *buf,
2082 * Duplicate the given rsa signature
2084 * @param sig the signature to duplicate
2085 * @return the duplicate key; NULL upon error
2087 struct GNUNET_CRYPTO_RsaSignature *
2088 GNUNET_CRYPTO_rsa_signature_dup (const struct GNUNET_CRYPTO_RsaSignature *sig);
2092 * Unblind a blind-signed signature. The signature should have been generated
2093 * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with
2094 * #GNUNET_CRYPTO_rsa_blind().
2096 * @param sig the signature made on the blinded signature purpose
2097 * @param bks the blinding key secret used to blind the signature purpose
2098 * @param pkey the public key of the signer
2099 * @return unblinded signature on success, NULL if RSA key is bad or malicious.
2101 struct GNUNET_CRYPTO_RsaSignature *
2102 GNUNET_CRYPTO_rsa_unblind (struct GNUNET_CRYPTO_RsaSignature *sig,
2103 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
2104 struct GNUNET_CRYPTO_RsaPublicKey *pkey);
2108 * Verify whether the given hash corresponds to the given signature and the
2109 * signature is valid with respect to the given public key.
2111 * @param hash the message to verify to match the @a sig
2112 * @param sig signature that is being validated
2113 * @param public_key public key of the signer
2114 * @returns #GNUNET_YES if ok, #GNUNET_NO if RSA key is malicious, #GNUNET_SYSERR if signature
2117 GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash,
2118 const struct GNUNET_CRYPTO_RsaSignature *sig,
2119 const struct GNUNET_CRYPTO_RsaPublicKey *public_key);
2122 #if 0 /* keep Emacsens' auto-indent happy */
2130 /* ifndef GNUNET_CRYPTO_LIB_H */
2132 /* end of gnunet_crypto_lib.h */