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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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
48 #if 0 /* keep Emacsens' auto-indent happy */
55 * The identity of the host (wraps the signing key of the peer).
57 struct GNUNET_PeerIdentity;
59 #include "gnunet_common.h"
64 * Maximum length of an ECC signature.
65 * Note: round up to multiple of 8 minus 2 for alignment.
67 #define GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH 126
71 * Desired quality level for random numbers.
74 enum GNUNET_CRYPTO_Quality
77 * No good quality of the operation is needed (i.e.,
78 * random numbers can be pseudo-random).
81 GNUNET_CRYPTO_QUALITY_WEAK,
84 * High-quality operations are desired.
87 GNUNET_CRYPTO_QUALITY_STRONG,
90 * Randomness for IVs etc. is required.
93 GNUNET_CRYPTO_QUALITY_NONCE
98 * @brief length of the sessionkey in bytes (256 BIT sessionkey)
100 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256 / 8)
103 * Length of a hash value
105 #define GNUNET_CRYPTO_HASH_LENGTH (512 / 8)
108 * How many characters (without 0-terminator) are our ASCII-encoded
109 * public keys (ECDSA/EDDSA/ECDHE).
111 #define GNUNET_CRYPTO_PKEY_ASCII_LENGTH 52
114 * @brief 0-terminated ASCII encoding of a struct GNUNET_HashCode.
116 struct GNUNET_CRYPTO_HashAsciiEncoded
118 unsigned char encoding[104];
122 GNUNET_NETWORK_STRUCT_BEGIN
126 * @brief header of what an ECC signature signs
127 * this must be followed by "size - 8" bytes of
128 * the actual signed data
130 struct GNUNET_CRYPTO_EccSignaturePurpose
133 * How many bytes does this signature sign?
134 * (including this purpose header); in network
137 uint32_t size GNUNET_PACKED;
140 * What does this signature vouch for? This
141 * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
142 * constant (from gnunet_signatures.h). In
143 * network byte order!
145 uint32_t purpose GNUNET_PACKED;
150 * @brief an ECC signature using EdDSA.
151 * See https://gnunet.org/ed25519
153 struct GNUNET_CRYPTO_EddsaSignature
159 unsigned char r[256 / 8];
164 unsigned char s[256 / 8];
169 * @brief an ECC signature using ECDSA
171 struct GNUNET_CRYPTO_EcdsaSignature
177 unsigned char r[256 / 8];
182 unsigned char s[256 / 8];
187 * Public ECC key (always for curve Ed25519) encoded in a format
188 * suitable for network transmission and EdDSA signatures.
190 struct GNUNET_CRYPTO_EddsaPublicKey
193 * Point Q consists of a y-value mod p (256 bits); the x-value is
194 * always positive. The point is stored in Ed25519 standard
197 unsigned char q_y[256 / 8];
202 * Public ECC key (always for Curve25519) encoded in a format suitable
203 * for network transmission and ECDSA signatures.
205 struct GNUNET_CRYPTO_EcdsaPublicKey
208 * Q consists of an x- and a y-value, each mod p (256 bits), given
209 * here in affine coordinates and Ed25519 standard compact format.
211 unsigned char q_y[256 / 8];
216 * The identity of the host (wraps the signing key of the peer).
218 struct GNUNET_PeerIdentity
220 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
225 * Public ECC key (always for Curve25519) encoded in a format suitable
226 * for network transmission and encryption (ECDH),
227 * See http://cr.yp.to/ecdh.html
229 struct GNUNET_CRYPTO_EcdhePublicKey
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];
240 * Private ECC key encoded for transmission. To be used only for ECDH
241 * key exchange (ECDHE to be precise).
243 struct GNUNET_CRYPTO_EcdhePrivateKey
246 * d is a value mod n, where n has at most 256 bits.
248 unsigned char d[256 / 8];
252 * Private ECC key encoded for transmission. To be used only for ECDSA
255 struct GNUNET_CRYPTO_EcdsaPrivateKey
258 * d is a value mod n, where n has at most 256 bits.
260 unsigned char d[256 / 8];
264 * Private ECC key encoded for transmission. To be used only for EdDSA
267 struct GNUNET_CRYPTO_EddsaPrivateKey
270 * d is a value mod n, where n has at most 256 bits.
272 unsigned char d[256 / 8];
277 * @brief type for session keys
279 struct GNUNET_CRYPTO_SymmetricSessionKey
282 * Actual key for AES.
284 unsigned char aes_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
287 * Actual key for TwoFish.
289 unsigned char twofish_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
292 GNUNET_NETWORK_STRUCT_END
295 * @brief IV for sym cipher
297 * NOTE: must be smaller (!) in size than the
298 * `struct GNUNET_HashCode`.
300 struct GNUNET_CRYPTO_SymmetricInitializationVector
302 unsigned char aes_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
304 unsigned char twofish_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
309 * @brief type for (message) authentication keys
311 struct GNUNET_CRYPTO_AuthKey
313 unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
318 * Size of paillier plain texts and public keys.
319 * Private keys and ciphertexts are twice this size.
321 #define GNUNET_CRYPTO_PAILLIER_BITS 2048
325 * Paillier public key.
327 struct GNUNET_CRYPTO_PaillierPublicKey
332 unsigned char n[GNUNET_CRYPTO_PAILLIER_BITS / 8];
337 * Paillier private key.
339 struct GNUNET_CRYPTO_PaillierPrivateKey
342 * Lambda-component of the private key.
344 unsigned char lambda[GNUNET_CRYPTO_PAILLIER_BITS / 8];
346 * Mu-component of the private key.
348 unsigned char mu[GNUNET_CRYPTO_PAILLIER_BITS / 8];
353 * Paillier ciphertext.
355 struct GNUNET_CRYPTO_PaillierCiphertext
358 * Guaranteed minimum number of homomorphic operations with this ciphertext,
359 * in network byte order (NBO).
361 int32_t remaining_ops GNUNET_PACKED;
364 * The bits of the ciphertext.
366 unsigned char bits[GNUNET_CRYPTO_PAILLIER_BITS * 2 / 8];
370 /* **************** Functions and Macros ************* */
374 * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator
377 * @param seed the seed to use
380 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
385 * Calculate the checksum of a buffer in one step.
387 * @param buf buffer to calculate CRC over
388 * @param len number of bytes in @a buf
392 GNUNET_CRYPTO_crc8_n (const void *buf, size_t len);
396 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
398 * @param sum current sum, initially 0
399 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
400 * @param len number of bytes in @a buf, must be multiple of 2
401 * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish to get actual crc16)
404 GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len);
408 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
410 * @param sum cummulative sum
411 * @return crc16 value
414 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
419 * Calculate the checksum of a buffer in one step.
421 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
422 * @param len number of bytes in @a buf, must be multiple of 2
423 * @return crc16 value
426 GNUNET_CRYPTO_crc16_n (const void *buf, size_t len);
431 * Compute the CRC32 checksum for the first len
432 * bytes of the buffer.
434 * @param buf the data over which we're taking the CRC
435 * @param len the length of the buffer @a buf in bytes
436 * @return the resulting CRC32 checksum
439 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len);
443 * Zero out @a buffer, securely against compiler optimizations.
444 * Used to delete key material.
446 * @param buffer the buffer to zap
447 * @param length buffer length
450 GNUNET_CRYPTO_zero_keys (void *buffer, size_t length);
455 * Fill block with a random values.
457 * @param mode desired quality of the random number
458 * @param buffer the buffer to fill
459 * @param length buffer length
462 GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode,
468 * Produce a random value.
470 * @param mode desired quality of the random number
471 * @param i the upper limit (exclusive) for the random number
472 * @return a random value in the interval [0,@a i) (exclusive).
475 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i);
480 * Random on unsigned 64-bit values.
482 * @param mode desired quality of the random number
483 * @param max value returned will be in range [0,@a max) (exclusive)
484 * @return random 64-bit number
487 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max);
492 * Get an array with a random permutation of the
494 * @param mode #GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used,
495 * #GNUNET_CRYPTO_QUALITY_WEAK or #GNUNET_CRYPTO_QUALITY_NONCE otherwise
496 * @param n the size of the array
497 * @return the permutation array (allocated from heap)
500 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
505 * Create a new random session key.
507 * @param key key to initialize
510 GNUNET_CRYPTO_symmetric_create_session_key (
511 struct GNUNET_CRYPTO_SymmetricSessionKey *key);
516 * Encrypt a block using a symmetric sessionkey.
518 * @param block the block to encrypt
519 * @param size the size of the @a block
520 * @param sessionkey the key used to encrypt
521 * @param iv the initialization vector to use, use INITVALUE
523 * @return the size of the encrypted block, -1 for errors
526 GNUNET_CRYPTO_symmetric_encrypt (
529 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
530 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
536 * Decrypt a given block using a symmetric sessionkey.
538 * @param block the data to decrypt, encoded as returned by encrypt
539 * @param size how big is the block?
540 * @param sessionkey the key used to decrypt
541 * @param iv the initialization vector to use
542 * @param result address to store the result at
543 * @return -1 on failure, size of decrypted block on success
546 GNUNET_CRYPTO_symmetric_decrypt (
549 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
550 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
556 * @brief Derive an IV
557 * @param iv initialization vector
558 * @param skey session key
559 * @param salt salt for the derivation
560 * @param salt_len size of the @a salt
561 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
564 GNUNET_CRYPTO_symmetric_derive_iv (
565 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
566 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
573 * @brief Derive an IV
574 * @param iv initialization vector
575 * @param skey session key
576 * @param salt salt for the derivation
577 * @param salt_len size of the @a salt
578 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
581 GNUNET_CRYPTO_symmetric_derive_iv_v (
582 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
583 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
591 * Convert hash to ASCII encoding.
592 * @param block the hash code
593 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
594 * safely cast to char*, a '\\0' termination is set).
597 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block,
598 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
603 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
605 * @param enc the encoding
606 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
607 * @param result where to store the hash code
608 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
611 GNUNET_CRYPTO_hash_from_string2 (const char *enc,
613 struct GNUNET_HashCode *result);
618 * Convert ASCII encoding back to `struct GNUNET_HashCode`
620 * @param enc the encoding
621 * @param result where to store the hash code
622 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
624 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
625 GNUNET_CRYPTO_hash_from_string2 (enc, strlen (enc), result)
631 * Compute the distance between 2 hashcodes. The
632 * computation must be fast, not involve @a a[0] or @a a[4] (they're used
633 * elsewhere), and be somewhat consistent. And of course, the result
634 * should be a positive number.
636 * @param a some hash code
637 * @param b some hash code
638 * @return number between 0 and UINT32_MAX
641 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a,
642 const struct GNUNET_HashCode *b);
647 * Compute hash of a given block.
649 * @param block the data to hash
650 * @param size size of the @a block
651 * @param ret pointer to where to write the hashcode
654 GNUNET_CRYPTO_hash (const void *block,
656 struct GNUNET_HashCode *ret);
660 * Context for cummulative hashing.
662 struct GNUNET_HashContext;
666 * Start incremental hashing operation.
668 * @return context for incremental hash computation
670 struct GNUNET_HashContext *
671 GNUNET_CRYPTO_hash_context_start (void);
675 * Add data to be hashed.
677 * @param hc cummulative hash context
678 * @param buf data to add
679 * @param size number of bytes in @a buf
682 GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
688 * Finish the hash computation.
690 * @param hc hash context to use, is freed in the process
691 * @param r_hash where to write the latest / final hash code
694 GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
695 struct GNUNET_HashCode *r_hash);
699 * Abort hashing, do not bother calculating final result.
701 * @param hc hash context to destroy
704 GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc);
708 * Calculate HMAC of a message (RFC 2104)
709 * TODO: Shouldn' this be the standard hmac function and
710 * the above be renamed?
712 * @param key secret key
713 * @param key_len secret key length
714 * @param plaintext input plaintext
715 * @param plaintext_len length of @a plaintext
716 * @param hmac where to store the hmac
719 GNUNET_CRYPTO_hmac_raw (const void *key,
721 const void *plaintext,
722 size_t plaintext_len,
723 struct GNUNET_HashCode *hmac);
728 * Calculate HMAC of a message (RFC 2104)
730 * @param key secret key
731 * @param plaintext input plaintext
732 * @param plaintext_len length of @a plaintext
733 * @param hmac where to store the hmac
736 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
737 const void *plaintext,
738 size_t plaintext_len,
739 struct GNUNET_HashCode *hmac);
743 * Function called once the hash computation over the
744 * specified file has completed.
747 * @param res resulting hash, NULL on error
749 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (
751 const struct GNUNET_HashCode *res);
755 * Handle to file hashing operation.
757 struct GNUNET_CRYPTO_FileHashContext;
762 * Compute the hash of an entire file.
764 * @param priority scheduling priority to use
765 * @param filename name of file to hash
766 * @param blocksize number of bytes to process in one task
767 * @param callback function to call upon completion
768 * @param callback_cls closure for @a callback
769 * @return NULL on (immediate) errror
771 struct GNUNET_CRYPTO_FileHashContext *
772 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
773 const char *filename,
775 GNUNET_CRYPTO_HashCompletedCallback callback,
780 * Cancel a file hashing operation.
782 * @param fhc operation to cancel (callback must not yet have been invoked)
785 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
790 * Create a random hash code.
792 * @param mode desired quality level
793 * @param result hash code that is randomized
796 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
797 struct GNUNET_HashCode *result);
802 * compute @a result = @a b - @a a
804 * @param a some hash code
805 * @param b some hash code
806 * @param result set to @a b - @a a
809 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
810 const struct GNUNET_HashCode *b,
811 struct GNUNET_HashCode *result);
816 * compute @a result = @a a + @a delta
818 * @param a some hash code
819 * @param delta some hash code
820 * @param result set to @a a + @a delta
823 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a,
824 const struct GNUNET_HashCode *delta,
825 struct GNUNET_HashCode *result);
830 * compute result = a ^ b
832 * @param a some hash code
833 * @param b some hash code
834 * @param result set to @a a ^ @a b
837 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
838 const struct GNUNET_HashCode *b,
839 struct GNUNET_HashCode *result);
844 * Convert a hashcode into a key.
846 * @param hc hash code that serves to generate the key
847 * @param skey set to a valid session key
848 * @param iv set to a valid initialization vector
851 GNUNET_CRYPTO_hash_to_aes_key (
852 const struct GNUNET_HashCode *hc,
853 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
854 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv);
859 * Obtain a bit from a hashcode.
861 * @param code the `struct GNUNET_HashCode` to index bit-wise
862 * @param bit index into the hashcode, [0...159]
863 * @return Bit \a bit from hashcode \a code, -1 for invalid index
866 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code,
872 * Determine how many low order bits match in two
873 * `struct GNUNET_HashCodes`. i.e. - 010011 and 011111 share
874 * the first two lowest order bits, and therefore the
875 * return value is two (NOT XOR distance, nor how many
876 * bits match absolutely!).
878 * @param first the first hashcode
879 * @param second the hashcode to compare first to
880 * @return the number of bits that match
883 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
884 const struct GNUNET_HashCode *second);
889 * Compare function for HashCodes, producing a total ordering
892 * @param h1 some hash code
893 * @param h2 some hash code
894 * @return 1 if @a h1 > @a h2, -1 if @a h1 < @a h2 and 0 if @a h1 == @a h2.
897 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
898 const struct GNUNET_HashCode *h2);
903 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
904 * in the XOR metric (Kademlia).
906 * @param h1 some hash code
907 * @param h2 some hash code
908 * @param target some hash code
909 * @return -1 if @a h1 is closer, 1 if @a h2 is closer and 0 if @a h1== @a h2.
912 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
913 const struct GNUNET_HashCode *h2,
914 const struct GNUNET_HashCode *target);
919 * @brief Derive an authentication key
920 * @param key authentication key
921 * @param rkey root key
923 * @param salt_len size of the salt
924 * @param argp pair of void * & size_t for context chunks, terminated by NULL
927 GNUNET_CRYPTO_hmac_derive_key_v (
928 struct GNUNET_CRYPTO_AuthKey *key,
929 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
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 (
946 struct GNUNET_CRYPTO_AuthKey *key,
947 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
956 * @param result buffer for the derived key, allocated by caller
957 * @param out_len desired length of the derived key
958 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
959 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
961 * @param xts_len length of @a xts
962 * @param skm source key material
963 * @param skm_len length of @a skm
964 * @param ... pair of void * & size_t for context chunks, terminated by NULL
965 * @return #GNUNET_YES on success
968 GNUNET_CRYPTO_hkdf (void *result,
982 * @param result buffer for the derived key, allocated by caller
983 * @param out_len desired length of the derived key
984 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
985 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
987 * @param xts_len length of @a xts
988 * @param skm source key material
989 * @param skm_len length of @a skm
990 * @param argp va_list of void * & size_t pairs for context chunks
991 * @return #GNUNET_YES on success
994 GNUNET_CRYPTO_hkdf_v (void *result,
1007 * @param result buffer for the derived key, allocated by caller
1008 * @param out_len desired length of the derived key
1010 * @param xts_len length of @a xts
1011 * @param skm source key material
1012 * @param skm_len length of @a skm
1013 * @param argp va_list of void * & size_t pairs for context chunks
1014 * @return #GNUNET_YES on success
1017 GNUNET_CRYPTO_kdf_v (void *result,
1027 * Deterministically generate a pseudo-random number uniformly from the
1028 * integers modulo a libgcrypt mpi.
1030 * @param[out] r MPI value set to the FDH
1031 * @param n MPI to work modulo
1033 * @param xts_len length of @a xts
1034 * @param skm source key material
1035 * @param skm_len length of @a skm
1036 * @param ctx context string
1039 GNUNET_CRYPTO_kdf_mod_mpi (gcry_mpi_t *r,
1051 * @param result buffer for the derived key, allocated by caller
1052 * @param out_len desired length of the derived key
1054 * @param xts_len length of @a xts
1055 * @param skm source key material
1056 * @param skm_len length of @a skm
1057 * @param ... void * & size_t pairs for context chunks
1058 * @return #GNUNET_YES on success
1061 GNUNET_CRYPTO_kdf (void *result,
1072 * Extract the public key for the given private key.
1074 * @param priv the private key
1075 * @param pub where to write the public key
1078 GNUNET_CRYPTO_ecdsa_key_get_public (
1079 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 (
1091 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1092 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 (
1104 const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1105 struct GNUNET_CRYPTO_EcdhePublicKey *pub);
1109 * Convert a public key to a string.
1111 * @param pub key to convert
1112 * @return string representing @a pub
1115 GNUNET_CRYPTO_ecdsa_public_key_to_string (
1116 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1119 * Convert a private key to a string.
1121 * @param priv key to convert
1122 * @return string representing @a priv
1125 GNUNET_CRYPTO_ecdsa_private_key_to_string (
1126 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv);
1130 * Convert a private key to a string.
1132 * @param priv key to convert
1133 * @return string representing @a pub
1136 GNUNET_CRYPTO_eddsa_private_key_to_string (
1137 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv);
1141 * Convert a public key to a string.
1143 * @param pub key to convert
1144 * @return string representing @a pub
1147 GNUNET_CRYPTO_eddsa_public_key_to_string (
1148 const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1152 * Convert a string representing a public key to a public key.
1154 * @param enc encoded public key
1155 * @param enclen number of bytes in @a enc (without 0-terminator)
1156 * @param pub where to store the public key
1157 * @return #GNUNET_OK on success
1160 GNUNET_CRYPTO_ecdsa_public_key_from_string (
1163 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1167 * Convert a string representing a private key to a private key.
1169 * @param enc encoded public key
1170 * @param enclen number of bytes in @a enc (without 0-terminator)
1171 * @param priv where to store the private key
1172 * @return #GNUNET_OK on success
1175 GNUNET_CRYPTO_eddsa_private_key_from_string (
1178 struct GNUNET_CRYPTO_EddsaPrivateKey *pub);
1182 * Convert a string representing a public key to a public key.
1184 * @param enc encoded public key
1185 * @param enclen number of bytes in @a enc (without 0-terminator)
1186 * @param pub where to store the public key
1187 * @return #GNUNET_OK on success
1190 GNUNET_CRYPTO_eddsa_public_key_from_string (
1193 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1198 * Create a new private key by reading it from a file. If the
1199 * files does not exist, create a new key and write it to the
1200 * file. Caller must free return value. Note that this function
1201 * can not guarantee that another process might not be trying
1202 * the same operation on the same file at the same time.
1203 * If the contents of the file
1204 * are invalid the old file is deleted and a fresh key is
1207 * @param filename name of file to use to store the key
1208 * @return new private key, NULL on error (for example,
1209 * permission denied); free using #GNUNET_free
1211 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1212 GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename);
1217 * Create a new private key by reading it from a file. If the
1218 * files does not exist, create a new key and write it to the
1219 * file. Caller must free return value. Note that this function
1220 * can not guarantee that another process might not be trying
1221 * the same operation on the same file at the same time.
1222 * If the contents of the file
1223 * are invalid the old file is deleted and a fresh key is
1226 * @param filename name of file to use to store the key
1227 * @return new private key, NULL on error (for example,
1228 * permission denied); free using #GNUNET_free
1230 struct GNUNET_CRYPTO_EddsaPrivateKey *
1231 GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename);
1235 * Forward declaration to simplify #include-structure.
1237 struct GNUNET_CONFIGURATION_Handle;
1242 * Create a new private key by reading our peer's key from
1243 * the file specified in the configuration.
1245 * @param cfg the configuration to use
1246 * @return new private key, NULL on error (for example,
1247 * permission denied); free using #GNUNET_free
1249 struct GNUNET_CRYPTO_EddsaPrivateKey *
1250 GNUNET_CRYPTO_eddsa_key_create_from_configuration (
1251 const struct GNUNET_CONFIGURATION_Handle *cfg);
1256 * Create a new private key. Caller must free return value.
1258 * @return fresh private key; free using #GNUNET_free
1260 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1261 GNUNET_CRYPTO_ecdsa_key_create (void);
1266 * Create a new private key. Caller must free return value.
1268 * @return fresh private key; free using #GNUNET_free
1270 struct GNUNET_CRYPTO_EddsaPrivateKey *
1271 GNUNET_CRYPTO_eddsa_key_create (void);
1276 * Create a new private key. Clear with #GNUNET_CRYPTO_ecdhe_key_clear().
1278 * @param[out] pk set to fresh private key;
1279 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1282 GNUNET_CRYPTO_ecdhe_key_create2 (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1287 * Create a new private key. Caller must free return value.
1289 * @return fresh private key; free using #GNUNET_free
1291 struct GNUNET_CRYPTO_EcdhePrivateKey *
1292 GNUNET_CRYPTO_ecdhe_key_create (void);
1297 * Clear memory that was used to store a private key.
1299 * @param pk location of the key
1302 GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk);
1307 * Clear memory that was used to store a private key.
1309 * @param pk location of the key
1312 GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk);
1317 * Clear memory that was used to store a private key.
1319 * @param pk location of the key
1322 GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1327 * Get the shared private key we use for anonymous users.
1329 * @return "anonymous" private key; do not free
1331 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
1332 GNUNET_CRYPTO_ecdsa_key_get_anonymous (void);
1337 * Setup a hostkey file for a peer given the name of the
1338 * configuration file (!). This function is used so that
1339 * at a later point code can be certain that reading a
1340 * hostkey is fast (for example in time-dependent testcases).
1342 * @param cfg_name name of the configuration file to use
1345 GNUNET_CRYPTO_eddsa_setup_hostkey (const char *cfg_name);
1350 * Retrieve the identity of the host's peer.
1352 * @param cfg configuration to use
1353 * @param dst pointer to where to write the peer identity
1354 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
1355 * could not be retrieved
1358 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
1359 struct GNUNET_PeerIdentity *dst);
1363 * Internal structure used to cache pre-calculated values for DLOG calculation.
1365 struct GNUNET_CRYPTO_EccDlogContext;
1369 * Point on a curve (always for Curve25519) encoded in a format suitable
1370 * for network transmission (ECDH), see http://cr.yp.to/ecdh.html.
1372 struct GNUNET_CRYPTO_EccPoint
1375 * Q consists of an x- and a y-value, each mod p (256 bits), given
1376 * here in affine coordinates and Ed25519 standard compact format.
1378 unsigned char q_y[256 / 8];
1383 * Do pre-calculation for ECC discrete logarithm for small factors.
1385 * @param max maximum value the factor can be
1386 * @param mem memory to use (should be smaller than @a max), must not be zero.
1387 * @return NULL on error
1389 struct GNUNET_CRYPTO_EccDlogContext *
1390 GNUNET_CRYPTO_ecc_dlog_prepare (unsigned int max, unsigned int mem);
1394 * Calculate ECC discrete logarithm for small factors.
1395 * Opposite of #GNUNET_CRYPTO_ecc_dexp().
1397 * @param dlc precalculated values, determine range of factors
1398 * @param input point on the curve to factor
1399 * @return INT_MAX if dlog failed, otherwise the factor
1402 GNUNET_CRYPTO_ecc_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc,
1403 gcry_mpi_point_t input);
1407 * Multiply the generator g of the elliptic curve by @a val
1408 * to obtain the point on the curve representing @a val.
1409 * Afterwards, point addition will correspond to integer
1410 * addition. #GNUNET_CRYPTO_ecc_dlog() can be used to
1411 * convert a point back to an integer (as long as the
1412 * integer is smaller than the MAX of the @a edc context).
1414 * @param edc calculation context for ECC operations
1415 * @param val value to encode into a point
1416 * @return representation of the value as an ECC point,
1417 * must be freed using #GNUNET_CRYPTO_ecc_free()
1420 GNUNET_CRYPTO_ecc_dexp (struct GNUNET_CRYPTO_EccDlogContext *edc, int val);
1424 * Multiply the generator g of the elliptic curve by @a val
1425 * to obtain the point on the curve representing @a val.
1427 * @param edc calculation context for ECC operations
1428 * @param val (positive) value to encode into a point
1429 * @return representation of the value as an ECC point,
1430 * must be freed using #GNUNET_CRYPTO_ecc_free()
1433 GNUNET_CRYPTO_ecc_dexp_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1438 * Multiply the point @a p on the elliptic curve by @a val.
1440 * @param edc calculation context for ECC operations
1441 * @param p point to multiply
1442 * @param val (positive) value to encode into a point
1443 * @return representation of the value as an ECC point,
1444 * must be freed using #GNUNET_CRYPTO_ecc_free()
1447 GNUNET_CRYPTO_ecc_pmul_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1453 * Convert point value to binary representation.
1455 * @param edc calculation context for ECC operations
1456 * @param point computational point representation
1457 * @param[out] bin binary point representation
1460 GNUNET_CRYPTO_ecc_point_to_bin (struct GNUNET_CRYPTO_EccDlogContext *edc,
1461 gcry_mpi_point_t point,
1462 struct GNUNET_CRYPTO_EccPoint *bin);
1466 * Convert binary representation of a point to computational representation.
1468 * @param edc calculation context for ECC operations
1469 * @param bin binary point representation
1470 * @return computational representation
1473 GNUNET_CRYPTO_ecc_bin_to_point (struct GNUNET_CRYPTO_EccDlogContext *edc,
1474 const struct GNUNET_CRYPTO_EccPoint *bin);
1478 * Add two points on the elliptic curve.
1480 * @param edc calculation context for ECC operations
1481 * @param a some value
1482 * @param b some value
1483 * @return @a a + @a b, must be freed using #GNUNET_CRYPTO_ecc_free()
1486 GNUNET_CRYPTO_ecc_add (struct GNUNET_CRYPTO_EccDlogContext *edc,
1488 gcry_mpi_point_t b);
1492 * Obtain a random point on the curve and its
1493 * additive inverse. Both returned values
1494 * must be freed using #GNUNET_CRYPTO_ecc_free().
1496 * @param edc calculation context for ECC operations
1497 * @param[out] r set to a random point on the curve
1498 * @param[out] r_inv set to the additive inverse of @a r
1501 GNUNET_CRYPTO_ecc_rnd (struct GNUNET_CRYPTO_EccDlogContext *edc,
1502 gcry_mpi_point_t *r,
1503 gcry_mpi_point_t *r_inv);
1507 * Obtain a random scalar for point multiplication on the curve and
1508 * its multiplicative inverse.
1510 * @param edc calculation context for ECC operations
1511 * @param[out] r set to a random scalar on the curve
1512 * @param[out] r_inv set to the multiplicative inverse of @a r
1515 GNUNET_CRYPTO_ecc_rnd_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1521 * Generate a random value mod n.
1523 * @param edc ECC context
1524 * @return random value mod n.
1527 GNUNET_CRYPTO_ecc_random_mod_n (struct GNUNET_CRYPTO_EccDlogContext *edc);
1531 * Free a point value returned by the API.
1533 * @param p point to free
1536 GNUNET_CRYPTO_ecc_free (gcry_mpi_point_t p);
1540 * Release precalculated values.
1542 * @param dlc dlog context
1545 GNUNET_CRYPTO_ecc_dlog_release (struct GNUNET_CRYPTO_EccDlogContext *dlc);
1550 * Derive key material from a public and a private ECC key.
1552 * @param priv private key to use for the ECDH (x)
1553 * @param pub public key to use for the ECDH (yG)
1554 * @param key_material where to write the key material (xyG)
1555 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1558 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1559 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1560 struct GNUNET_HashCode *key_material);
1565 * Derive key material from a ECDH public key and a private EdDSA key.
1566 * Dual to #GNUNET_CRRYPTO_ecdh_eddsa.
1568 * @param priv private key from EdDSA to use for the ECDH (x)
1569 * @param pub public key to use for the ECDH (yG)
1570 * @param key_material where to write the key material H(h(x)yG)
1571 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1574 GNUNET_CRYPTO_eddsa_ecdh (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1575 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1576 struct GNUNET_HashCode *key_material);
1580 * Derive key material from a ECDH public key and a private ECDSA key.
1581 * Dual to #GNUNET_CRRYPTO_ecdh_ecdsa.
1583 * @param priv private key from ECDSA to use for the ECDH (x)
1584 * @param pub public key to use for the ECDH (yG)
1585 * @param key_material where to write the key material H(h(x)yG)
1586 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1589 GNUNET_CRYPTO_ecdsa_ecdh (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1590 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1591 struct GNUNET_HashCode *key_material);
1596 * Derive key material from a EdDSA public key and a private ECDH key.
1597 * Dual to #GNUNET_CRRYPTO_eddsa_ecdh.
1599 * @param priv private key to use for the ECDH (y)
1600 * @param pub public key from EdDSA to use for the ECDH (X=h(x)G)
1601 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1602 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1605 GNUNET_CRYPTO_ecdh_eddsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1606 const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
1607 struct GNUNET_HashCode *key_material);
1611 * Derive key material from a EcDSA public key and a private ECDH key.
1612 * Dual to #GNUNET_CRRYPTO_ecdsa_ecdh.
1614 * @param priv private key to use for the ECDH (y)
1615 * @param pub public key from ECDSA to use for the ECDH (X=h(x)G)
1616 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1617 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1620 GNUNET_CRYPTO_ecdh_ecdsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1621 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1622 struct GNUNET_HashCode *key_material);
1627 * EdDSA sign a given block.
1629 * @param priv private key to use for the signing
1630 * @param purpose what to sign (size, purpose)
1631 * @param sig where to write the signature
1632 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1635 GNUNET_CRYPTO_eddsa_sign (
1636 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1637 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1638 struct GNUNET_CRYPTO_EddsaSignature *sig);
1643 * ECDSA Sign a given block.
1645 * @param priv private key to use for the signing
1646 * @param purpose what to sign (size, purpose)
1647 * @param sig where to write the signature
1648 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1651 GNUNET_CRYPTO_ecdsa_sign (
1652 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1653 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1654 struct GNUNET_CRYPTO_EcdsaSignature *sig);
1658 * Verify EdDSA signature.
1660 * @param purpose what is the purpose that the signature should have?
1661 * @param validate block to validate (size, purpose, data)
1662 * @param sig signature that is being validated
1663 * @param pub public key of the signer
1664 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1667 GNUNET_CRYPTO_eddsa_verify (
1669 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1670 const struct GNUNET_CRYPTO_EddsaSignature *sig,
1671 const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1676 * Verify ECDSA signature.
1678 * @param purpose what is the purpose that the signature should have?
1679 * @param validate block to validate (size, purpose, data)
1680 * @param sig signature that is being validated
1681 * @param pub public key of the signer
1682 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1685 GNUNET_CRYPTO_ecdsa_verify (
1687 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1688 const struct GNUNET_CRYPTO_EcdsaSignature *sig,
1689 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1694 * Derive a private key from a given private key and a label.
1695 * Essentially calculates a private key 'h = H(l,P) * d mod n'
1696 * where n is the size of the ECC group and P is the public
1697 * key associated with the private key 'd'.
1699 * @param priv original private key
1700 * @param label label to use for key deriviation
1701 * @param context additional context to use for HKDF of 'h';
1702 * typically the name of the subsystem/application
1703 * @return derived private key
1705 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1706 GNUNET_CRYPTO_ecdsa_private_key_derive (
1707 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1709 const char *context);
1714 * Derive a public key from a given public key and a label.
1715 * Essentially calculates a public key 'V = H(l,P) * P'.
1717 * @param pub original public key
1718 * @param label label to use for key deriviation
1719 * @param context additional context to use for HKDF of 'h'.
1720 * typically the name of the subsystem/application
1721 * @param result where to write the derived public key
1724 GNUNET_CRYPTO_ecdsa_public_key_derive (
1725 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1727 const char *context,
1728 struct GNUNET_CRYPTO_EcdsaPublicKey *result);
1732 * Output the given MPI value to the given buffer in network
1733 * byte order. The MPI @a val may not be negative.
1735 * @param buf where to output to
1736 * @param size number of bytes in @a buf
1737 * @param val value to write to @a buf
1740 GNUNET_CRYPTO_mpi_print_unsigned (void *buf, size_t size, gcry_mpi_t val);
1744 * Convert data buffer into MPI value.
1745 * The buffer is interpreted as network
1746 * byte order, unsigned integer.
1748 * @param result where to store MPI value (allocated)
1749 * @param data raw data (GCRYMPI_FMT_USG)
1750 * @param size number of bytes in @a data
1753 GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result,
1759 * Create a freshly generated paillier public key.
1761 * @param[out] public_key Where to store the public key?
1762 * @param[out] private_key Where to store the private key?
1765 GNUNET_CRYPTO_paillier_create (
1766 struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1767 struct GNUNET_CRYPTO_PaillierPrivateKey *private_key);
1771 * Encrypt a plaintext with a paillier public key.
1773 * @param public_key Public key to use.
1774 * @param m Plaintext to encrypt.
1775 * @param desired_ops How many homomorphic ops the caller intends to use
1776 * @param[out] ciphertext Encrytion of @a plaintext with @a public_key.
1777 * @return guaranteed number of supported homomorphic operations >= 1,
1778 * or desired_ops, in case that is lower,
1779 * or -1 if less than one homomorphic operation is possible
1782 GNUNET_CRYPTO_paillier_encrypt (
1783 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1786 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext);
1790 * Decrypt a paillier ciphertext with a private key.
1792 * @param private_key Private key to use for decryption.
1793 * @param public_key Public key to use for decryption.
1794 * @param ciphertext Ciphertext to decrypt.
1795 * @param[out] m Decryption of @a ciphertext with @private_key.
1798 GNUNET_CRYPTO_paillier_decrypt (
1799 const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key,
1800 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1801 const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
1806 * Compute a ciphertext that represents the sum of the plaintext in @a x1 and @a x2
1808 * Note that this operation can only be done a finite number of times
1809 * before an overflow occurs.
1811 * @param public_key Public key to use for encryption.
1812 * @param c1 Paillier cipher text.
1813 * @param c2 Paillier cipher text.
1814 * @param[out] result Result of the homomorphic operation.
1815 * @return #GNUNET_OK if the result could be computed,
1816 * #GNUNET_SYSERR if no more homomorphic operations are remaining.
1819 GNUNET_CRYPTO_paillier_hom_add (
1820 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1821 const struct GNUNET_CRYPTO_PaillierCiphertext *c1,
1822 const struct GNUNET_CRYPTO_PaillierCiphertext *c2,
1823 struct GNUNET_CRYPTO_PaillierCiphertext *result);
1827 * Get the number of remaining supported homomorphic operations.
1829 * @param c Paillier cipher text.
1830 * @return the number of remaining homomorphic operations
1833 GNUNET_CRYPTO_paillier_hom_get_remaining (
1834 const struct GNUNET_CRYPTO_PaillierCiphertext *c);
1837 /* ********* Chaum-style RSA-based blind signatures ******************* */
1841 * The private information of an RSA key pair.
1843 struct GNUNET_CRYPTO_RsaPrivateKey;
1846 * The public information of an RSA key pair.
1848 struct GNUNET_CRYPTO_RsaPublicKey;
1851 * Constant-size pre-secret for blinding key generation.
1853 struct GNUNET_CRYPTO_RsaBlindingKeySecret
1856 * Bits used to generate the blinding key. 256 bits
1857 * of entropy is enough.
1859 uint32_t pre_secret[8] GNUNET_PACKED;
1863 * @brief an RSA signature
1865 struct GNUNET_CRYPTO_RsaSignature;
1869 * Create a new private key. Caller must free return value.
1871 * @param len length of the key in bits (i.e. 2048)
1872 * @return fresh private key
1874 struct GNUNET_CRYPTO_RsaPrivateKey *
1875 GNUNET_CRYPTO_rsa_private_key_create (unsigned int len);
1879 * Free memory occupied by the private key.
1881 * @param key pointer to the memory to free
1884 GNUNET_CRYPTO_rsa_private_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *key);
1888 * Encode the private key in a format suitable for
1889 * storing it into a file.
1891 * @param key the private key
1892 * @param[out] buffer set to a buffer with the encoded key
1893 * @return size of memory allocatedin @a buffer
1896 GNUNET_CRYPTO_rsa_private_key_encode (
1897 const struct GNUNET_CRYPTO_RsaPrivateKey *key,
1902 * Decode the private key from the data-format back
1903 * to the "normal", internal format.
1905 * @param buf the buffer where the private key data is stored
1906 * @param len the length of the data in @a buf
1907 * @return NULL on error
1909 struct GNUNET_CRYPTO_RsaPrivateKey *
1910 GNUNET_CRYPTO_rsa_private_key_decode (const char *buf, size_t len);
1914 * Duplicate the given private key
1916 * @param key the private key to duplicate
1917 * @return the duplicate key; NULL upon error
1919 struct GNUNET_CRYPTO_RsaPrivateKey *
1920 GNUNET_CRYPTO_rsa_private_key_dup (
1921 const struct GNUNET_CRYPTO_RsaPrivateKey *key);
1925 * Extract the public key of the given private key.
1927 * @param priv the private key
1928 * @retur NULL on error, otherwise the public key
1930 struct GNUNET_CRYPTO_RsaPublicKey *
1931 GNUNET_CRYPTO_rsa_private_key_get_public (
1932 const struct GNUNET_CRYPTO_RsaPrivateKey *priv);
1936 * Compute hash over the public key.
1938 * @param key public key to hash
1939 * @param hc where to store the hash code
1942 GNUNET_CRYPTO_rsa_public_key_hash (const struct GNUNET_CRYPTO_RsaPublicKey *key,
1943 struct GNUNET_HashCode *hc);
1947 * Obtain the length of the RSA key in bits.
1949 * @param key the public key to introspect
1950 * @return length of the key in bits
1953 GNUNET_CRYPTO_rsa_public_key_len (const struct GNUNET_CRYPTO_RsaPublicKey *key);
1957 * Free memory occupied by the public key.
1959 * @param key pointer to the memory to free
1962 GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_RsaPublicKey *key);
1966 * Encode the public key in a format suitable for
1967 * storing it into a file.
1969 * @param key the private key
1970 * @param[out] buffer set to a buffer with the encoded key
1971 * @return size of memory allocated in @a buffer
1974 GNUNET_CRYPTO_rsa_public_key_encode (
1975 const struct GNUNET_CRYPTO_RsaPublicKey *key,
1980 * Decode the public key from the data-format back
1981 * to the "normal", internal format.
1983 * @param buf the buffer where the public key data is stored
1984 * @param len the length of the data in @a buf
1985 * @return NULL on error
1987 struct GNUNET_CRYPTO_RsaPublicKey *
1988 GNUNET_CRYPTO_rsa_public_key_decode (const char *buf, size_t len);
1992 * Duplicate the given public key
1994 * @param key the public key to duplicate
1995 * @return the duplicate key; NULL upon error
1997 struct GNUNET_CRYPTO_RsaPublicKey *
1998 GNUNET_CRYPTO_rsa_public_key_dup (const struct GNUNET_CRYPTO_RsaPublicKey *key);
2002 * Compare the values of two signatures.
2004 * @param s1 one signature
2005 * @param s2 the other signature
2006 * @return 0 if the two are equal
2009 GNUNET_CRYPTO_rsa_signature_cmp (struct GNUNET_CRYPTO_RsaSignature *s1,
2010 struct GNUNET_CRYPTO_RsaSignature *s2);
2013 * Compare the values of two private keys.
2015 * @param p1 one private key
2016 * @param p2 the other private key
2017 * @return 0 if the two are equal
2020 GNUNET_CRYPTO_rsa_private_key_cmp (struct GNUNET_CRYPTO_RsaPrivateKey *p1,
2021 struct GNUNET_CRYPTO_RsaPrivateKey *p2);
2025 * Compare the values of two public keys.
2027 * @param p1 one public key
2028 * @param p2 the other public key
2029 * @return 0 if the two are equal
2032 GNUNET_CRYPTO_rsa_public_key_cmp (struct GNUNET_CRYPTO_RsaPublicKey *p1,
2033 struct GNUNET_CRYPTO_RsaPublicKey *p2);
2037 * Blinds the given message with the given blinding key
2039 * @param hash hash of the message to sign
2040 * @param bkey the blinding key
2041 * @param pkey the public key of the signer
2042 * @param[out] buf set to a buffer with the blinded message to be signed
2043 * @param[out] buf_size number of bytes stored in @a buf
2044 * @return #GNUNET_YES if successful, #GNUNET_NO if RSA key is malicious
2047 GNUNET_CRYPTO_rsa_blind (const struct GNUNET_HashCode *hash,
2048 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
2049 struct GNUNET_CRYPTO_RsaPublicKey *pkey,
2055 * Sign a blinded value, which must be a full domain hash of a message.
2057 * @param key private key to use for the signing
2058 * @param msg the (blinded) message to sign
2059 * @param msg_len number of bytes in @a msg to sign
2060 * @return NULL on error, signature on success
2062 struct GNUNET_CRYPTO_RsaSignature *
2063 GNUNET_CRYPTO_rsa_sign_blinded (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2069 * Create and sign a full domain hash of a message.
2071 * @param key private key to use for the signing
2072 * @param hash the hash of the message to sign
2073 * @return NULL on error, including a malicious RSA key, signature on success
2075 struct GNUNET_CRYPTO_RsaSignature *
2076 GNUNET_CRYPTO_rsa_sign_fdh (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2077 const struct GNUNET_HashCode *hash);
2081 * Free memory occupied by signature.
2083 * @param sig memory to free
2086 GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_RsaSignature *sig);
2090 * Encode the given signature in a format suitable for storing it into a file.
2092 * @param sig the signature
2093 * @param[out] buffer set to a buffer with the encoded key
2094 * @return size of memory allocated in @a buffer
2097 GNUNET_CRYPTO_rsa_signature_encode (
2098 const struct GNUNET_CRYPTO_RsaSignature *sig,
2103 * Decode the signature from the data-format back to the "normal", internal
2106 * @param buf the buffer where the public key data is stored
2107 * @param len the length of the data in @a buf
2108 * @return NULL on error
2110 struct GNUNET_CRYPTO_RsaSignature *
2111 GNUNET_CRYPTO_rsa_signature_decode (const char *buf, size_t len);
2115 * Duplicate the given rsa signature
2117 * @param sig the signature to duplicate
2118 * @return the duplicate key; NULL upon error
2120 struct GNUNET_CRYPTO_RsaSignature *
2121 GNUNET_CRYPTO_rsa_signature_dup (const struct GNUNET_CRYPTO_RsaSignature *sig);
2125 * Unblind a blind-signed signature. The signature should have been generated
2126 * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with
2127 * #GNUNET_CRYPTO_rsa_blind().
2129 * @param sig the signature made on the blinded signature purpose
2130 * @param bks the blinding key secret used to blind the signature purpose
2131 * @param pkey the public key of the signer
2132 * @return unblinded signature on success, NULL if RSA key is bad or malicious.
2134 struct GNUNET_CRYPTO_RsaSignature *
2135 GNUNET_CRYPTO_rsa_unblind (const struct GNUNET_CRYPTO_RsaSignature *sig,
2136 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
2137 struct GNUNET_CRYPTO_RsaPublicKey *pkey);
2141 * Verify whether the given hash corresponds to the given signature and the
2142 * signature is valid with respect to the given public key.
2144 * @param hash the message to verify to match the @a sig
2145 * @param sig signature that is being validated
2146 * @param public_key public key of the signer
2147 * @returns #GNUNET_YES if ok, #GNUNET_NO if RSA key is malicious, #GNUNET_SYSERR if signature
2150 GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash,
2151 const struct GNUNET_CRYPTO_RsaSignature *sig,
2152 const struct GNUNET_CRYPTO_RsaPublicKey *public_key);
2155 #if 0 /* keep Emacsens' auto-indent happy */
2163 /* ifndef GNUNET_CRYPTO_LIB_H */
2165 /* end of gnunet_crypto_lib.h */