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 cr.yp.to/papers.html#ed25519
153 struct GNUNET_CRYPTO_EddsaSignature
158 unsigned char r[256 / 8];
163 unsigned char s[256 / 8];
168 * @brief an ECC signature using ECDSA
170 struct GNUNET_CRYPTO_EcdsaSignature
175 unsigned char r[256 / 8];
180 unsigned char s[256 / 8];
185 * Public ECC key (always for curve Ed25519) encoded in a format
186 * suitable for network transmission and EdDSA signatures. Refer
187 * to section 5.1.3 of rfc8032, for a thorough explanation of how
188 * this value maps to the x- and y-coordinates.
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 * Calculate the 'proof-of-work' hash (an expensive hash).
662 * @param salt salt to use in pow calculation
663 * @param buf data to hash
664 * @param buf_len number of bytes in @a buf
665 * @param result where to write the resulting hash
668 GNUNET_CRYPTO_pow_hash (const char *salt,
671 struct GNUNET_HashCode *result);
675 * Context for cummulative hashing.
677 struct GNUNET_HashContext;
681 * Start incremental hashing operation.
683 * @return context for incremental hash computation
685 struct GNUNET_HashContext *
686 GNUNET_CRYPTO_hash_context_start (void);
690 * Add data to be hashed.
692 * @param hc cummulative hash context
693 * @param buf data to add
694 * @param size number of bytes in @a buf
697 GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
703 * Finish the hash computation.
705 * @param hc hash context to use, is freed in the process
706 * @param r_hash where to write the latest / final hash code
709 GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
710 struct GNUNET_HashCode *r_hash);
714 * Abort hashing, do not bother calculating final result.
716 * @param hc hash context to destroy
719 GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc);
723 * Calculate HMAC of a message (RFC 2104)
724 * TODO: Shouldn' this be the standard hmac function and
725 * the above be renamed?
727 * @param key secret key
728 * @param key_len secret key length
729 * @param plaintext input plaintext
730 * @param plaintext_len length of @a plaintext
731 * @param hmac where to store the hmac
734 GNUNET_CRYPTO_hmac_raw (const void *key,
736 const void *plaintext,
737 size_t plaintext_len,
738 struct GNUNET_HashCode *hmac);
743 * Calculate HMAC of a message (RFC 2104)
745 * @param key secret key
746 * @param plaintext input plaintext
747 * @param plaintext_len length of @a plaintext
748 * @param hmac where to store the hmac
751 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
752 const void *plaintext,
753 size_t plaintext_len,
754 struct GNUNET_HashCode *hmac);
758 * Function called once the hash computation over the
759 * specified file has completed.
762 * @param res resulting hash, NULL on error
764 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (
766 const struct GNUNET_HashCode *res);
770 * Handle to file hashing operation.
772 struct GNUNET_CRYPTO_FileHashContext;
777 * Compute the hash of an entire file.
779 * @param priority scheduling priority to use
780 * @param filename name of file to hash
781 * @param blocksize number of bytes to process in one task
782 * @param callback function to call upon completion
783 * @param callback_cls closure for @a callback
784 * @return NULL on (immediate) errror
786 struct GNUNET_CRYPTO_FileHashContext *
787 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
788 const char *filename,
790 GNUNET_CRYPTO_HashCompletedCallback callback,
795 * Cancel a file hashing operation.
797 * @param fhc operation to cancel (callback must not yet have been invoked)
800 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
805 * Create a random hash code.
807 * @param mode desired quality level
808 * @param result hash code that is randomized
811 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
812 struct GNUNET_HashCode *result);
817 * compute @a result = @a b - @a a
819 * @param a some hash code
820 * @param b some hash code
821 * @param result set to @a b - @a a
824 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
825 const struct GNUNET_HashCode *b,
826 struct GNUNET_HashCode *result);
831 * compute @a result = @a a + @a delta
833 * @param a some hash code
834 * @param delta some hash code
835 * @param result set to @a a + @a delta
838 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a,
839 const struct GNUNET_HashCode *delta,
840 struct GNUNET_HashCode *result);
845 * compute result = a ^ b
847 * @param a some hash code
848 * @param b some hash code
849 * @param result set to @a a ^ @a b
852 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
853 const struct GNUNET_HashCode *b,
854 struct GNUNET_HashCode *result);
859 * Convert a hashcode into a key.
861 * @param hc hash code that serves to generate the key
862 * @param skey set to a valid session key
863 * @param iv set to a valid initialization vector
866 GNUNET_CRYPTO_hash_to_aes_key (
867 const struct GNUNET_HashCode *hc,
868 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
869 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv);
874 * Obtain a bit from a hashcode.
876 * @param code the `struct GNUNET_HashCode` to index bit-wise
877 * @param bit index into the hashcode, [0...159]
878 * @return Bit \a bit from hashcode \a code, -1 for invalid index
881 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code,
887 * Determine how many low order bits match in two
888 * `struct GNUNET_HashCodes`. i.e. - 010011 and 011111 share
889 * the first two lowest order bits, and therefore the
890 * return value is two (NOT XOR distance, nor how many
891 * bits match absolutely!).
893 * @param first the first hashcode
894 * @param second the hashcode to compare first to
895 * @return the number of bits that match
898 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
899 const struct GNUNET_HashCode *second);
904 * Compare function for HashCodes, producing a total ordering
907 * @param h1 some hash code
908 * @param h2 some hash code
909 * @return 1 if @a h1 > @a h2, -1 if @a h1 < @a h2 and 0 if @a h1 == @a h2.
912 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
913 const struct GNUNET_HashCode *h2);
918 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
919 * in the XOR metric (Kademlia).
921 * @param h1 some hash code
922 * @param h2 some hash code
923 * @param target some hash code
924 * @return -1 if @a h1 is closer, 1 if @a h2 is closer and 0 if @a h1== @a h2.
927 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
928 const struct GNUNET_HashCode *h2,
929 const struct GNUNET_HashCode *target);
934 * @brief Derive an authentication key
935 * @param key authentication key
936 * @param rkey root key
938 * @param salt_len size of the salt
939 * @param argp pair of void * & size_t for context chunks, terminated by NULL
942 GNUNET_CRYPTO_hmac_derive_key_v (
943 struct GNUNET_CRYPTO_AuthKey *key,
944 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
952 * @brief Derive an authentication key
953 * @param key authentication key
954 * @param rkey root key
956 * @param salt_len size of the salt
957 * @param ... pair of void * & size_t for context chunks, terminated by NULL
960 GNUNET_CRYPTO_hmac_derive_key (
961 struct GNUNET_CRYPTO_AuthKey *key,
962 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
971 * @param result buffer for the derived key, allocated by caller
972 * @param out_len desired length of the derived key
973 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
974 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
976 * @param xts_len length of @a xts
977 * @param skm source key material
978 * @param skm_len length of @a skm
979 * @param ... pair of void * & size_t for context chunks, terminated by NULL
980 * @return #GNUNET_YES on success
983 GNUNET_CRYPTO_hkdf (void *result,
997 * @param result buffer for the derived key, allocated by caller
998 * @param out_len desired length of the derived key
999 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
1000 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
1002 * @param xts_len length of @a xts
1003 * @param skm source key material
1004 * @param skm_len length of @a skm
1005 * @param argp va_list of void * & size_t pairs for context chunks
1006 * @return #GNUNET_YES on success
1009 GNUNET_CRYPTO_hkdf_v (void *result,
1022 * @param result buffer for the derived key, allocated by caller
1023 * @param out_len desired length of the derived key
1025 * @param xts_len length of @a xts
1026 * @param skm source key material
1027 * @param skm_len length of @a skm
1028 * @param argp va_list of void * & size_t pairs for context chunks
1029 * @return #GNUNET_YES on success
1032 GNUNET_CRYPTO_kdf_v (void *result,
1042 * Deterministically generate a pseudo-random number uniformly from the
1043 * integers modulo a libgcrypt mpi.
1045 * @param[out] r MPI value set to the FDH
1046 * @param n MPI to work modulo
1048 * @param xts_len length of @a xts
1049 * @param skm source key material
1050 * @param skm_len length of @a skm
1051 * @param ctx context string
1054 GNUNET_CRYPTO_kdf_mod_mpi (gcry_mpi_t *r,
1066 * @param result buffer for the derived key, allocated by caller
1067 * @param out_len desired length of the derived key
1069 * @param xts_len length of @a xts
1070 * @param skm source key material
1071 * @param skm_len length of @a skm
1072 * @param ... void * & size_t pairs for context chunks
1073 * @return #GNUNET_YES on success
1076 GNUNET_CRYPTO_kdf (void *result,
1087 * Extract the public key for the given private key.
1089 * @param priv the private key
1090 * @param pub where to write the public key
1093 GNUNET_CRYPTO_ecdsa_key_get_public (
1094 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1095 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1099 * Extract the public key for the given private key.
1101 * @param priv the private key
1102 * @param pub where to write the public key
1105 GNUNET_CRYPTO_eddsa_key_get_public (
1106 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1107 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1112 * Extract the public key for the given private key.
1114 * @param priv the private key
1115 * @param pub where to write the public key
1118 GNUNET_CRYPTO_ecdhe_key_get_public (
1119 const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1120 struct GNUNET_CRYPTO_EcdhePublicKey *pub);
1124 * Convert a public key to a string.
1126 * @param pub key to convert
1127 * @return string representing @a pub
1130 GNUNET_CRYPTO_ecdsa_public_key_to_string (
1131 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1134 * Convert a private key to a string.
1136 * @param priv key to convert
1137 * @return string representing @a priv
1140 GNUNET_CRYPTO_ecdsa_private_key_to_string (
1141 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv);
1145 * Convert a private key to a string.
1147 * @param priv key to convert
1148 * @return string representing @a pub
1151 GNUNET_CRYPTO_eddsa_private_key_to_string (
1152 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv);
1156 * Convert a public key to a string.
1158 * @param pub key to convert
1159 * @return string representing @a pub
1162 GNUNET_CRYPTO_eddsa_public_key_to_string (
1163 const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1167 * Convert a string representing a public key to a public key.
1169 * @param enc encoded public key
1170 * @param enclen number of bytes in @a enc (without 0-terminator)
1171 * @param pub where to store the public key
1172 * @return #GNUNET_OK on success
1175 GNUNET_CRYPTO_ecdsa_public_key_from_string (
1178 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1182 * Convert a string representing a private key to a private key.
1184 * @param enc encoded public key
1185 * @param enclen number of bytes in @a enc (without 0-terminator)
1186 * @param priv where to store the private key
1187 * @return #GNUNET_OK on success
1190 GNUNET_CRYPTO_eddsa_private_key_from_string (
1193 struct GNUNET_CRYPTO_EddsaPrivateKey *pub);
1197 * Convert a string representing a public key to a public key.
1199 * @param enc encoded public key
1200 * @param enclen number of bytes in @a enc (without 0-terminator)
1201 * @param pub where to store the public key
1202 * @return #GNUNET_OK on success
1205 GNUNET_CRYPTO_eddsa_public_key_from_string (
1208 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1213 * Create a new private key by reading it from a file. If the
1214 * files does not exist, create a new key and write it to the
1215 * file. Caller must free return value. Note that this function
1216 * can not guarantee that another process might not be trying
1217 * the same operation on the same file at the same time.
1218 * If the contents of the file
1219 * are invalid the old file is deleted and a fresh key is
1222 * @param filename name of file to use to store the key
1223 * @return new private key, NULL on error (for example,
1224 * permission denied); free using #GNUNET_free
1226 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1227 GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename);
1232 * Create a new private key by reading it from a file. If the
1233 * files does not exist, create a new key and write it to the
1234 * file. Caller must free return value. Note that this function
1235 * can not guarantee that another process might not be trying
1236 * the same operation on the same file at the same time.
1237 * If the contents of the file
1238 * are invalid the old file is deleted and a fresh key is
1241 * @param filename name of file to use to store the key
1242 * @return new private key, NULL on error (for example,
1243 * permission denied); free using #GNUNET_free
1245 struct GNUNET_CRYPTO_EddsaPrivateKey *
1246 GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename);
1250 * Forward declaration to simplify #include-structure.
1252 struct GNUNET_CONFIGURATION_Handle;
1257 * Create a new private key by reading our peer's key from
1258 * the file specified in the configuration.
1260 * @param cfg the configuration to use
1261 * @return new private key, NULL on error (for example,
1262 * permission denied); free using #GNUNET_free
1264 struct GNUNET_CRYPTO_EddsaPrivateKey *
1265 GNUNET_CRYPTO_eddsa_key_create_from_configuration (
1266 const struct GNUNET_CONFIGURATION_Handle *cfg);
1271 * Create a new private key. Caller must free return value.
1273 * @return fresh private key; free using #GNUNET_free
1275 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1276 GNUNET_CRYPTO_ecdsa_key_create (void);
1281 * Create a new private key. Caller must free return value.
1283 * @return fresh private key; free using #GNUNET_free
1285 struct GNUNET_CRYPTO_EddsaPrivateKey *
1286 GNUNET_CRYPTO_eddsa_key_create (void);
1291 * Create a new private key. Clear with #GNUNET_CRYPTO_ecdhe_key_clear().
1293 * @param[out] pk set to fresh private key;
1294 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1297 GNUNET_CRYPTO_ecdhe_key_create2 (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1302 * Create a new private key. Caller must free return value.
1304 * @return fresh private key; free using #GNUNET_free
1306 struct GNUNET_CRYPTO_EcdhePrivateKey *
1307 GNUNET_CRYPTO_ecdhe_key_create (void);
1312 * Clear memory that was used to store a private key.
1314 * @param pk location of the key
1317 GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk);
1322 * Clear memory that was used to store a private key.
1324 * @param pk location of the key
1327 GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk);
1332 * Clear memory that was used to store a private key.
1334 * @param pk location of the key
1337 GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1342 * Get the shared private key we use for anonymous users.
1344 * @return "anonymous" private key; do not free
1346 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
1347 GNUNET_CRYPTO_ecdsa_key_get_anonymous (void);
1352 * Setup a hostkey file for a peer given the name of the
1353 * configuration file (!). This function is used so that
1354 * at a later point code can be certain that reading a
1355 * hostkey is fast (for example in time-dependent testcases).
1357 * @param cfg_name name of the configuration file to use
1360 GNUNET_CRYPTO_eddsa_setup_hostkey (const char *cfg_name);
1365 * Retrieve the identity of the host's peer.
1367 * @param cfg configuration to use
1368 * @param dst pointer to where to write the peer identity
1369 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
1370 * could not be retrieved
1373 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
1374 struct GNUNET_PeerIdentity *dst);
1378 * Internal structure used to cache pre-calculated values for DLOG calculation.
1380 struct GNUNET_CRYPTO_EccDlogContext;
1384 * Point on a curve (always for Curve25519) encoded in a format suitable
1385 * for network transmission (ECDH), see http://cr.yp.to/ecdh.html.
1387 struct GNUNET_CRYPTO_EccPoint
1390 * Q consists of an x- and a y-value, each mod p (256 bits), given
1391 * here in affine coordinates and Ed25519 standard compact format.
1393 unsigned char q_y[256 / 8];
1398 * Do pre-calculation for ECC discrete logarithm for small factors.
1400 * @param max maximum value the factor can be
1401 * @param mem memory to use (should be smaller than @a max), must not be zero.
1402 * @return NULL on error
1404 struct GNUNET_CRYPTO_EccDlogContext *
1405 GNUNET_CRYPTO_ecc_dlog_prepare (unsigned int max, unsigned int mem);
1409 * Calculate ECC discrete logarithm for small factors.
1410 * Opposite of #GNUNET_CRYPTO_ecc_dexp().
1412 * @param dlc precalculated values, determine range of factors
1413 * @param input point on the curve to factor
1414 * @return INT_MAX if dlog failed, otherwise the factor
1417 GNUNET_CRYPTO_ecc_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc,
1418 gcry_mpi_point_t input);
1422 * Multiply the generator g of the elliptic curve by @a val
1423 * to obtain the point on the curve representing @a val.
1424 * Afterwards, point addition will correspond to integer
1425 * addition. #GNUNET_CRYPTO_ecc_dlog() can be used to
1426 * convert a point back to an integer (as long as the
1427 * integer is smaller than the MAX of the @a edc context).
1429 * @param edc calculation context for ECC operations
1430 * @param val value to encode into a point
1431 * @return representation of the value as an ECC point,
1432 * must be freed using #GNUNET_CRYPTO_ecc_free()
1435 GNUNET_CRYPTO_ecc_dexp (struct GNUNET_CRYPTO_EccDlogContext *edc, int val);
1439 * Multiply the generator g of the elliptic curve by @a val
1440 * to obtain the point on the curve representing @a val.
1442 * @param edc calculation context for ECC operations
1443 * @param val (positive) value to encode into a point
1444 * @return representation of the value as an ECC point,
1445 * must be freed using #GNUNET_CRYPTO_ecc_free()
1448 GNUNET_CRYPTO_ecc_dexp_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1453 * Multiply the point @a p on the elliptic curve by @a val.
1455 * @param edc calculation context for ECC operations
1456 * @param p point to multiply
1457 * @param val (positive) value to encode into a point
1458 * @return representation of the value as an ECC point,
1459 * must be freed using #GNUNET_CRYPTO_ecc_free()
1462 GNUNET_CRYPTO_ecc_pmul_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1468 * Convert point value to binary representation.
1470 * @param edc calculation context for ECC operations
1471 * @param point computational point representation
1472 * @param[out] bin binary point representation
1475 GNUNET_CRYPTO_ecc_point_to_bin (struct GNUNET_CRYPTO_EccDlogContext *edc,
1476 gcry_mpi_point_t point,
1477 struct GNUNET_CRYPTO_EccPoint *bin);
1481 * Convert binary representation of a point to computational representation.
1483 * @param edc calculation context for ECC operations
1484 * @param bin binary point representation
1485 * @return computational representation
1488 GNUNET_CRYPTO_ecc_bin_to_point (struct GNUNET_CRYPTO_EccDlogContext *edc,
1489 const struct GNUNET_CRYPTO_EccPoint *bin);
1493 * Add two points on the elliptic curve.
1495 * @param edc calculation context for ECC operations
1496 * @param a some value
1497 * @param b some value
1498 * @return @a a + @a b, must be freed using #GNUNET_CRYPTO_ecc_free()
1501 GNUNET_CRYPTO_ecc_add (struct GNUNET_CRYPTO_EccDlogContext *edc,
1503 gcry_mpi_point_t b);
1507 * Obtain a random point on the curve and its
1508 * additive inverse. Both returned values
1509 * must be freed using #GNUNET_CRYPTO_ecc_free().
1511 * @param edc calculation context for ECC operations
1512 * @param[out] r set to a random point on the curve
1513 * @param[out] r_inv set to the additive inverse of @a r
1516 GNUNET_CRYPTO_ecc_rnd (struct GNUNET_CRYPTO_EccDlogContext *edc,
1517 gcry_mpi_point_t *r,
1518 gcry_mpi_point_t *r_inv);
1522 * Obtain a random scalar for point multiplication on the curve and
1523 * its multiplicative inverse.
1525 * @param edc calculation context for ECC operations
1526 * @param[out] r set to a random scalar on the curve
1527 * @param[out] r_inv set to the multiplicative inverse of @a r
1530 GNUNET_CRYPTO_ecc_rnd_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1536 * Generate a random value mod n.
1538 * @param edc ECC context
1539 * @return random value mod n.
1542 GNUNET_CRYPTO_ecc_random_mod_n (struct GNUNET_CRYPTO_EccDlogContext *edc);
1546 * Free a point value returned by the API.
1548 * @param p point to free
1551 GNUNET_CRYPTO_ecc_free (gcry_mpi_point_t p);
1555 * Release precalculated values.
1557 * @param dlc dlog context
1560 GNUNET_CRYPTO_ecc_dlog_release (struct GNUNET_CRYPTO_EccDlogContext *dlc);
1565 * Derive key material from a public and a private ECC key.
1567 * @param priv private key to use for the ECDH (x)
1568 * @param pub public key to use for the ECDH (yG)
1569 * @param key_material where to write the key material (xyG)
1570 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1573 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1574 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1575 struct GNUNET_HashCode *key_material);
1580 * Derive key material from a ECDH public key and a private EdDSA key.
1581 * Dual to #GNUNET_CRRYPTO_ecdh_eddsa.
1583 * @param priv private key from EdDSA 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_eddsa_ecdh (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1590 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1591 struct GNUNET_HashCode *key_material);
1595 * Derive key material from a ECDH public key and a private ECDSA key.
1596 * Dual to #GNUNET_CRRYPTO_ecdh_ecdsa.
1598 * @param priv private key from ECDSA to use for the ECDH (x)
1599 * @param pub public key to use for the ECDH (yG)
1600 * @param key_material where to write the key material H(h(x)yG)
1601 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1604 GNUNET_CRYPTO_ecdsa_ecdh (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1605 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1606 struct GNUNET_HashCode *key_material);
1611 * Derive key material from a EdDSA public key and a private ECDH key.
1612 * Dual to #GNUNET_CRRYPTO_eddsa_ecdh.
1614 * @param priv private key to use for the ECDH (y)
1615 * @param pub public key from EdDSA 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_eddsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1621 const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
1622 struct GNUNET_HashCode *key_material);
1626 * Derive key material from a EcDSA public key and a private ECDH key.
1627 * Dual to #GNUNET_CRRYPTO_ecdsa_ecdh.
1629 * @param priv private key to use for the ECDH (y)
1630 * @param pub public key from ECDSA to use for the ECDH (X=h(x)G)
1631 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1632 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1635 GNUNET_CRYPTO_ecdh_ecdsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1636 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1637 struct GNUNET_HashCode *key_material);
1642 * EdDSA sign a given block.
1644 * @param priv private key to use for the signing
1645 * @param purpose what to sign (size, purpose)
1646 * @param sig where to write the signature
1647 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1650 GNUNET_CRYPTO_eddsa_sign (
1651 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1652 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1653 struct GNUNET_CRYPTO_EddsaSignature *sig);
1658 * ECDSA Sign a given block.
1660 * @param priv private key to use for the signing
1661 * @param purpose what to sign (size, purpose)
1662 * @param sig where to write the signature
1663 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1666 GNUNET_CRYPTO_ecdsa_sign (
1667 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1668 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1669 struct GNUNET_CRYPTO_EcdsaSignature *sig);
1673 * Verify EdDSA signature.
1675 * @param purpose what is the purpose that the signature should have?
1676 * @param validate block to validate (size, purpose, data)
1677 * @param sig signature that is being validated
1678 * @param pub public key of the signer
1679 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1682 GNUNET_CRYPTO_eddsa_verify (
1684 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1685 const struct GNUNET_CRYPTO_EddsaSignature *sig,
1686 const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1691 * Verify ECDSA signature.
1693 * @param purpose what is the purpose that the signature should have?
1694 * @param validate block to validate (size, purpose, data)
1695 * @param sig signature that is being validated
1696 * @param pub public key of the signer
1697 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1700 GNUNET_CRYPTO_ecdsa_verify (
1702 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1703 const struct GNUNET_CRYPTO_EcdsaSignature *sig,
1704 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1709 * Derive a private key from a given private key and a label.
1710 * Essentially calculates a private key 'h = H(l,P) * d mod n'
1711 * where n is the size of the ECC group and P is the public
1712 * key associated with the private key 'd'.
1714 * @param priv original private key
1715 * @param label label to use for key deriviation
1716 * @param context additional context to use for HKDF of 'h';
1717 * typically the name of the subsystem/application
1718 * @return derived private key
1720 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1721 GNUNET_CRYPTO_ecdsa_private_key_derive (
1722 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1724 const char *context);
1729 * Derive a public key from a given public key and a label.
1730 * Essentially calculates a public key 'V = H(l,P) * P'.
1732 * @param pub original public key
1733 * @param label label to use for key deriviation
1734 * @param context additional context to use for HKDF of 'h'.
1735 * typically the name of the subsystem/application
1736 * @param result where to write the derived public key
1739 GNUNET_CRYPTO_ecdsa_public_key_derive (
1740 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1742 const char *context,
1743 struct GNUNET_CRYPTO_EcdsaPublicKey *result);
1747 * Output the given MPI value to the given buffer in network
1748 * byte order. The MPI @a val may not be negative.
1750 * @param buf where to output to
1751 * @param size number of bytes in @a buf
1752 * @param val value to write to @a buf
1755 GNUNET_CRYPTO_mpi_print_unsigned (void *buf, size_t size, gcry_mpi_t val);
1759 * Convert data buffer into MPI value.
1760 * The buffer is interpreted as network
1761 * byte order, unsigned integer.
1763 * @param result where to store MPI value (allocated)
1764 * @param data raw data (GCRYMPI_FMT_USG)
1765 * @param size number of bytes in @a data
1768 GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result,
1774 * Create a freshly generated paillier public key.
1776 * @param[out] public_key Where to store the public key?
1777 * @param[out] private_key Where to store the private key?
1780 GNUNET_CRYPTO_paillier_create (
1781 struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1782 struct GNUNET_CRYPTO_PaillierPrivateKey *private_key);
1786 * Encrypt a plaintext with a paillier public key.
1788 * @param public_key Public key to use.
1789 * @param m Plaintext to encrypt.
1790 * @param desired_ops How many homomorphic ops the caller intends to use
1791 * @param[out] ciphertext Encrytion of @a plaintext with @a public_key.
1792 * @return guaranteed number of supported homomorphic operations >= 1,
1793 * or desired_ops, in case that is lower,
1794 * or -1 if less than one homomorphic operation is possible
1797 GNUNET_CRYPTO_paillier_encrypt (
1798 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1801 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext);
1805 * Decrypt a paillier ciphertext with a private key.
1807 * @param private_key Private key to use for decryption.
1808 * @param public_key Public key to use for decryption.
1809 * @param ciphertext Ciphertext to decrypt.
1810 * @param[out] m Decryption of @a ciphertext with @private_key.
1813 GNUNET_CRYPTO_paillier_decrypt (
1814 const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key,
1815 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1816 const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
1821 * Compute a ciphertext that represents the sum of the plaintext in @a x1 and @a x2
1823 * Note that this operation can only be done a finite number of times
1824 * before an overflow occurs.
1826 * @param public_key Public key to use for encryption.
1827 * @param c1 Paillier cipher text.
1828 * @param c2 Paillier cipher text.
1829 * @param[out] result Result of the homomorphic operation.
1830 * @return #GNUNET_OK if the result could be computed,
1831 * #GNUNET_SYSERR if no more homomorphic operations are remaining.
1834 GNUNET_CRYPTO_paillier_hom_add (
1835 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1836 const struct GNUNET_CRYPTO_PaillierCiphertext *c1,
1837 const struct GNUNET_CRYPTO_PaillierCiphertext *c2,
1838 struct GNUNET_CRYPTO_PaillierCiphertext *result);
1842 * Get the number of remaining supported homomorphic operations.
1844 * @param c Paillier cipher text.
1845 * @return the number of remaining homomorphic operations
1848 GNUNET_CRYPTO_paillier_hom_get_remaining (
1849 const struct GNUNET_CRYPTO_PaillierCiphertext *c);
1852 /* ********* Chaum-style RSA-based blind signatures ******************* */
1856 * The private information of an RSA key pair.
1858 struct GNUNET_CRYPTO_RsaPrivateKey;
1861 * The public information of an RSA key pair.
1863 struct GNUNET_CRYPTO_RsaPublicKey;
1866 * Constant-size pre-secret for blinding key generation.
1868 struct GNUNET_CRYPTO_RsaBlindingKeySecret
1871 * Bits used to generate the blinding key. 256 bits
1872 * of entropy is enough.
1874 uint32_t pre_secret[8] GNUNET_PACKED;
1878 * @brief an RSA signature
1880 struct GNUNET_CRYPTO_RsaSignature;
1884 * Create a new private key. Caller must free return value.
1886 * @param len length of the key in bits (i.e. 2048)
1887 * @return fresh private key
1889 struct GNUNET_CRYPTO_RsaPrivateKey *
1890 GNUNET_CRYPTO_rsa_private_key_create (unsigned int len);
1894 * Free memory occupied by the private key.
1896 * @param key pointer to the memory to free
1899 GNUNET_CRYPTO_rsa_private_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *key);
1903 * Encode the private key in a format suitable for
1904 * storing it into a file.
1906 * @param key the private key
1907 * @param[out] buffer set to a buffer with the encoded key
1908 * @return size of memory allocatedin @a buffer
1911 GNUNET_CRYPTO_rsa_private_key_encode (
1912 const struct GNUNET_CRYPTO_RsaPrivateKey *key,
1917 * Decode the private key from the data-format back
1918 * to the "normal", internal format.
1920 * @param buf the buffer where the private key data is stored
1921 * @param buf_size the size of the data in @a buf
1922 * @return NULL on error
1924 struct GNUNET_CRYPTO_RsaPrivateKey *
1925 GNUNET_CRYPTO_rsa_private_key_decode (const void *buf,
1930 * Duplicate the given private key
1932 * @param key the private key to duplicate
1933 * @return the duplicate key; NULL upon error
1935 struct GNUNET_CRYPTO_RsaPrivateKey *
1936 GNUNET_CRYPTO_rsa_private_key_dup (
1937 const struct GNUNET_CRYPTO_RsaPrivateKey *key);
1941 * Extract the public key of the given private key.
1943 * @param priv the private key
1944 * @retur NULL on error, otherwise the public key
1946 struct GNUNET_CRYPTO_RsaPublicKey *
1947 GNUNET_CRYPTO_rsa_private_key_get_public (
1948 const struct GNUNET_CRYPTO_RsaPrivateKey *priv);
1952 * Compute hash over the public key.
1954 * @param key public key to hash
1955 * @param hc where to store the hash code
1958 GNUNET_CRYPTO_rsa_public_key_hash (const struct GNUNET_CRYPTO_RsaPublicKey *key,
1959 struct GNUNET_HashCode *hc);
1963 * Obtain the length of the RSA key in bits.
1965 * @param key the public key to introspect
1966 * @return length of the key in bits
1969 GNUNET_CRYPTO_rsa_public_key_len (const struct GNUNET_CRYPTO_RsaPublicKey *key);
1973 * Free memory occupied by the public key.
1975 * @param key pointer to the memory to free
1978 GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_RsaPublicKey *key);
1982 * Encode the public key in a format suitable for
1983 * storing it into a file.
1985 * @param key the private key
1986 * @param[out] buffer set to a buffer with the encoded key
1987 * @return size of memory allocated in @a buffer
1990 GNUNET_CRYPTO_rsa_public_key_encode (
1991 const struct GNUNET_CRYPTO_RsaPublicKey *key,
1996 * Decode the public key from the data-format back
1997 * to the "normal", internal format.
1999 * @param buf the buffer where the public key data is stored
2000 * @param len the length of the data in @a buf
2001 * @return NULL on error
2003 struct GNUNET_CRYPTO_RsaPublicKey *
2004 GNUNET_CRYPTO_rsa_public_key_decode (const char *buf, size_t len);
2008 * Duplicate the given public key
2010 * @param key the public key to duplicate
2011 * @return the duplicate key; NULL upon error
2013 struct GNUNET_CRYPTO_RsaPublicKey *
2014 GNUNET_CRYPTO_rsa_public_key_dup (const struct GNUNET_CRYPTO_RsaPublicKey *key);
2018 * Compare the values of two signatures.
2020 * @param s1 one signature
2021 * @param s2 the other signature
2022 * @return 0 if the two are equal
2025 GNUNET_CRYPTO_rsa_signature_cmp (struct GNUNET_CRYPTO_RsaSignature *s1,
2026 struct GNUNET_CRYPTO_RsaSignature *s2);
2029 * Compare the values of two private keys.
2031 * @param p1 one private key
2032 * @param p2 the other private key
2033 * @return 0 if the two are equal
2036 GNUNET_CRYPTO_rsa_private_key_cmp (struct GNUNET_CRYPTO_RsaPrivateKey *p1,
2037 struct GNUNET_CRYPTO_RsaPrivateKey *p2);
2041 * Compare the values of two public keys.
2043 * @param p1 one public key
2044 * @param p2 the other public key
2045 * @return 0 if the two are equal
2048 GNUNET_CRYPTO_rsa_public_key_cmp (struct GNUNET_CRYPTO_RsaPublicKey *p1,
2049 struct GNUNET_CRYPTO_RsaPublicKey *p2);
2053 * Blinds the given message with the given blinding key
2055 * @param hash hash of the message to sign
2056 * @param bkey the blinding key
2057 * @param pkey the public key of the signer
2058 * @param[out] buf set to a buffer with the blinded message to be signed
2059 * @param[out] buf_size number of bytes stored in @a buf
2060 * @return #GNUNET_YES if successful, #GNUNET_NO if RSA key is malicious
2063 GNUNET_CRYPTO_rsa_blind (const struct GNUNET_HashCode *hash,
2064 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
2065 struct GNUNET_CRYPTO_RsaPublicKey *pkey,
2071 * Sign a blinded value, which must be a full domain hash of a message.
2073 * @param key private key to use for the signing
2074 * @param msg the (blinded) message to sign
2075 * @param msg_len number of bytes in @a msg to sign
2076 * @return NULL on error, signature on success
2078 struct GNUNET_CRYPTO_RsaSignature *
2079 GNUNET_CRYPTO_rsa_sign_blinded (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2085 * Create and sign a full domain hash of a message.
2087 * @param key private key to use for the signing
2088 * @param hash the hash of the message to sign
2089 * @return NULL on error, including a malicious RSA key, signature on success
2091 struct GNUNET_CRYPTO_RsaSignature *
2092 GNUNET_CRYPTO_rsa_sign_fdh (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2093 const struct GNUNET_HashCode *hash);
2097 * Free memory occupied by signature.
2099 * @param sig memory to free
2102 GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_RsaSignature *sig);
2106 * Encode the given signature in a format suitable for storing it into a file.
2108 * @param sig the signature
2109 * @param[out] buffer set to a buffer with the encoded key
2110 * @return size of memory allocated in @a buffer
2113 GNUNET_CRYPTO_rsa_signature_encode (
2114 const struct GNUNET_CRYPTO_RsaSignature *sig,
2119 * Decode the signature from the data-format back to the "normal", internal
2122 * @param buf the buffer where the public key data is stored
2123 * @param buf_size the number of bytes of the data in @a buf
2124 * @return NULL on error
2126 struct GNUNET_CRYPTO_RsaSignature *
2127 GNUNET_CRYPTO_rsa_signature_decode (const void *buf,
2132 * Duplicate the given rsa signature
2134 * @param sig the signature to duplicate
2135 * @return the duplicate key; NULL upon error
2137 struct GNUNET_CRYPTO_RsaSignature *
2138 GNUNET_CRYPTO_rsa_signature_dup (const struct GNUNET_CRYPTO_RsaSignature *sig);
2142 * Unblind a blind-signed signature. The signature should have been generated
2143 * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with
2144 * #GNUNET_CRYPTO_rsa_blind().
2146 * @param sig the signature made on the blinded signature purpose
2147 * @param bks the blinding key secret used to blind the signature purpose
2148 * @param pkey the public key of the signer
2149 * @return unblinded signature on success, NULL if RSA key is bad or malicious.
2151 struct GNUNET_CRYPTO_RsaSignature *
2152 GNUNET_CRYPTO_rsa_unblind (const struct GNUNET_CRYPTO_RsaSignature *sig,
2153 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
2154 struct GNUNET_CRYPTO_RsaPublicKey *pkey);
2158 * Verify whether the given hash corresponds to the given signature and the
2159 * signature is valid with respect to the given public key.
2161 * @param hash the message to verify to match the @a sig
2162 * @param sig signature that is being validated
2163 * @param public_key public key of the signer
2164 * @returns #GNUNET_YES if ok, #GNUNET_NO if RSA key is malicious, #GNUNET_SYSERR if signature
2167 GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash,
2168 const struct GNUNET_CRYPTO_RsaSignature *sig,
2169 const struct GNUNET_CRYPTO_RsaPublicKey *public_key);
2172 #if 0 /* keep Emacsens' auto-indent happy */
2180 /* ifndef GNUNET_CRYPTO_LIB_H */
2182 /* end of gnunet_crypto_lib.h */