2 This file is part of GNUnet.
3 Copyright (C) 2001-2013 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file include/gnunet_crypto_lib.h
23 * @brief cryptographic primitives for GNUnet
25 * @author Christian Grothoff
26 * @author Krista Bennett
27 * @author Gerd Knorr <kraxel@bytesex.org>
28 * @author Ioana Patrascu
29 * @author Tzvetan Horozov
31 * @defgroup crypto Crypto library: cryptographic operations
32 * Provides cryptographic primitives.
34 * @see [Documentation](https://gnunet.org/crypto-api)
36 * @defgroup hash Crypto library: hash operations
37 * Provides hashing and operations on hashes.
39 * @see [Documentation](https://gnunet.org/crypto-api)
42 #ifndef GNUNET_CRYPTO_LIB_H
43 #define GNUNET_CRYPTO_LIB_H
48 #if 0 /* keep Emacsens' auto-indent happy */
54 * @brief A 512-bit hashcode
56 struct GNUNET_HashCode;
59 * The identity of the host (wraps the signing key of the peer).
61 struct GNUNET_PeerIdentity;
63 #include "gnunet_common.h"
68 * @brief A 512-bit hashcode
70 struct GNUNET_HashCode
72 uint32_t bits[512 / 8 / sizeof (uint32_t)]; /* = 16 */
77 * Maximum length of an ECC signature.
78 * Note: round up to multiple of 8 minus 2 for alignment.
80 #define GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH 126
84 * Desired quality level for random numbers.
87 enum GNUNET_CRYPTO_Quality
90 * No good quality of the operation is needed (i.e.,
91 * random numbers can be pseudo-random).
94 GNUNET_CRYPTO_QUALITY_WEAK,
97 * High-quality operations are desired.
100 GNUNET_CRYPTO_QUALITY_STRONG,
103 * Randomness for IVs etc. is required.
106 GNUNET_CRYPTO_QUALITY_NONCE
111 * @brief length of the sessionkey in bytes (256 BIT sessionkey)
113 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
116 * Length of a hash value
118 #define GNUNET_CRYPTO_HASH_LENGTH (512/8)
121 * How many characters (without 0-terminator) are our ASCII-encoded
122 * public keys (ECDSA/EDDSA/ECDHE).
124 #define GNUNET_CRYPTO_PKEY_ASCII_LENGTH 52
127 * @brief 0-terminated ASCII encoding of a struct GNUNET_HashCode.
129 struct GNUNET_CRYPTO_HashAsciiEncoded
131 unsigned char encoding[104];
135 GNUNET_NETWORK_STRUCT_BEGIN
139 * @brief header of what an ECC signature signs
140 * this must be followed by "size - 8" bytes of
141 * the actual signed data
143 struct GNUNET_CRYPTO_EccSignaturePurpose
146 * How many bytes does this signature sign?
147 * (including this purpose header); in network
150 uint32_t size GNUNET_PACKED;
153 * What does this signature vouch for? This
154 * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
155 * constant (from gnunet_signatures.h). In
156 * network byte order!
158 uint32_t purpose GNUNET_PACKED;
164 * @brief an ECC signature using EdDSA.
165 * See https://gnunet.org/ed25519
167 struct GNUNET_CRYPTO_EddsaSignature
173 unsigned char r[256 / 8];
178 unsigned char s[256 / 8];
185 * @brief an ECC signature using ECDSA
187 struct GNUNET_CRYPTO_EcdsaSignature
193 unsigned char r[256 / 8];
198 unsigned char s[256 / 8];
204 * Public ECC key (always for Curve25519) encoded in a format suitable
205 * for network transmission and EdDSA signatures.
207 struct GNUNET_CRYPTO_EddsaPublicKey
210 * Q consists of an x- and a y-value, each mod p (256 bits), given
211 * here in affine coordinates and Ed25519 standard compact format.
213 unsigned char q_y[256 / 8];
219 * Public ECC key (always for Curve25519) encoded in a format suitable
220 * for network transmission and ECDSA signatures.
222 struct GNUNET_CRYPTO_EcdsaPublicKey
225 * Q consists of an x- and a y-value, each mod p (256 bits), given
226 * here in affine coordinates and Ed25519 standard compact format.
228 unsigned char q_y[256 / 8];
234 * The identity of the host (wraps the signing key of the peer).
236 struct GNUNET_PeerIdentity
238 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
243 * Public ECC key (always for Curve25519) encoded in a format suitable
244 * for network transmission and encryption (ECDH),
245 * See http://cr.yp.to/ecdh.html
247 struct GNUNET_CRYPTO_EcdhePublicKey
250 * Q consists of an x- and a y-value, each mod p (256 bits), given
251 * here in affine coordinates and Ed25519 standard compact format.
253 unsigned char q_y[256 / 8];
258 * Private ECC key encoded for transmission. To be used only for ECDH
259 * key exchange (ECDHE to be precise).
261 struct GNUNET_CRYPTO_EcdhePrivateKey
264 * d is a value mod n, where n has at most 256 bits.
266 unsigned char d[256 / 8];
271 * Private ECC key encoded for transmission. To be used only for ECDSA
274 struct GNUNET_CRYPTO_EcdsaPrivateKey
277 * d is a value mod n, where n has at most 256 bits.
279 unsigned char d[256 / 8];
284 * Private ECC key encoded for transmission. To be used only for EdDSA
287 struct GNUNET_CRYPTO_EddsaPrivateKey
290 * d is a value mod n, where n has at most 256 bits.
292 unsigned char d[256 / 8];
298 * @brief type for session keys
300 struct GNUNET_CRYPTO_SymmetricSessionKey
303 * Actual key for AES.
305 unsigned char aes_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
308 * Actual key for TwoFish.
310 unsigned char twofish_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
314 GNUNET_NETWORK_STRUCT_END
317 * @brief IV for sym cipher
319 * NOTE: must be smaller (!) in size than the
320 * `struct GNUNET_HashCode`.
322 struct GNUNET_CRYPTO_SymmetricInitializationVector
324 unsigned char aes_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
326 unsigned char twofish_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
331 * @brief type for (message) authentication keys
333 struct GNUNET_CRYPTO_AuthKey
335 unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
340 * Size of paillier plain texts and public keys.
341 * Private keys and ciphertexts are twice this size.
343 #define GNUNET_CRYPTO_PAILLIER_BITS 2048
347 * Paillier public key.
349 struct GNUNET_CRYPTO_PaillierPublicKey
354 unsigned char n[GNUNET_CRYPTO_PAILLIER_BITS / 8];
359 * Paillier public key.
361 struct GNUNET_CRYPTO_PaillierPrivateKey
364 * Lambda-component of the private key.
366 unsigned char lambda[GNUNET_CRYPTO_PAILLIER_BITS / 8];
368 * Mu-component of the private key.
370 unsigned char mu[GNUNET_CRYPTO_PAILLIER_BITS / 8];
375 * Paillier ciphertext.
377 struct GNUNET_CRYPTO_PaillierCiphertext
380 * Guaranteed minimum number of homomorphic operations with this ciphertext,
381 * in network byte order (NBO).
383 int32_t remaining_ops GNUNET_PACKED;
386 * The bits of the ciphertext.
388 unsigned char bits[GNUNET_CRYPTO_PAILLIER_BITS * 2 / 8];
392 /* **************** Functions and Macros ************* */
396 * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator
399 * @param seed the seed to use
402 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
407 * Calculate the checksum of a buffer in one step.
409 * @param buf buffer to calculate CRC over
410 * @param len number of bytes in @a buf
414 GNUNET_CRYPTO_crc8_n (const void *buf,
419 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
421 * @param sum current sum, initially 0
422 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
423 * @param len number of bytes in @a buf, must be multiple of 2
424 * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish to get actual crc16)
427 GNUNET_CRYPTO_crc16_step (uint32_t sum,
433 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
435 * @param sum cummulative sum
436 * @return crc16 value
439 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
444 * Calculate the checksum of a buffer in one step.
446 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
447 * @param len number of bytes in @a buf, must be multiple of 2
448 * @return crc16 value
451 GNUNET_CRYPTO_crc16_n (const void *buf,
459 * Compute the CRC32 checksum for the first len
460 * bytes of the buffer.
462 * @param buf the data over which we're taking the CRC
463 * @param len the length of the buffer @a buf in bytes
464 * @return the resulting CRC32 checksum
467 GNUNET_CRYPTO_crc32_n (const void *buf,
473 * Fill block with a random values.
475 * @param mode desired quality of the random number
476 * @param buffer the buffer to fill
477 * @param length buffer length
480 GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode,
486 * Produce a random value.
488 * @param mode desired quality of the random number
489 * @param i the upper limit (exclusive) for the random number
490 * @return a random value in the interval [0,@a i) (exclusive).
493 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode,
499 * Random on unsigned 64-bit values.
501 * @param mode desired quality of the random number
502 * @param max value returned will be in range [0,@a max) (exclusive)
503 * @return random 64-bit number
506 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
512 * Get an array with a random permutation of the
514 * @param mode #GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used,
515 * #GNUNET_CRYPTO_QUALITY_WEAK or #GNUNET_CRYPTO_QUALITY_NONCE otherwise
516 * @param n the size of the array
517 * @return the permutation array (allocated from heap)
520 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode,
526 * Create a new random session key.
528 * @param key key to initialize
531 GNUNET_CRYPTO_symmetric_create_session_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key);
536 * Encrypt a block using a symmetric sessionkey.
538 * @param block the block to encrypt
539 * @param size the size of the @a block
540 * @param sessionkey the key used to encrypt
541 * @param iv the initialization vector to use, use INITVALUE
543 * @return the size of the encrypted block, -1 for errors
546 GNUNET_CRYPTO_symmetric_encrypt (const void *block,
548 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
549 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
555 * Decrypt a given block using a symmetric sessionkey.
557 * @param block the data to decrypt, encoded as returned by encrypt
558 * @param size how big is the block?
559 * @param sessionkey the key used to decrypt
560 * @param iv the initialization vector to use
561 * @param result address to store the result at
562 * @return -1 on failure, size of decrypted block on success
565 GNUNET_CRYPTO_symmetric_decrypt (const void *block,
567 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
568 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
574 * @brief Derive an IV
575 * @param iv initialization vector
576 * @param skey session key
577 * @param salt salt for the derivation
578 * @param salt_len size of the @a salt
579 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
582 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
583 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
585 size_t salt_len, ...);
589 * @brief Derive an IV
590 * @param iv initialization vector
591 * @param skey session key
592 * @param salt salt for the derivation
593 * @param salt_len size of the @a salt
594 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
597 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
598 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
606 * Convert hash to ASCII encoding.
607 * @param block the hash code
608 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
609 * safely cast to char*, a '\\0' termination is set).
612 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block,
613 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
618 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
620 * @param enc the encoding
621 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
622 * @param result where to store the hash code
623 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
626 GNUNET_CRYPTO_hash_from_string2 (const char *enc,
628 struct GNUNET_HashCode *result);
633 * Convert ASCII encoding back to `struct GNUNET_HashCode`
635 * @param enc the encoding
636 * @param result where to store the hash code
637 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
639 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
640 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
646 * Compute the distance between 2 hashcodes. The
647 * computation must be fast, not involve @a a[0] or @a a[4] (they're used
648 * elsewhere), and be somewhat consistent. And of course, the result
649 * should be a positive number.
651 * @param a some hash code
652 * @param b some hash code
653 * @return number between 0 and UINT32_MAX
656 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a,
657 const struct GNUNET_HashCode *b);
662 * Compute hash of a given block.
664 * @param block the data to hash
665 * @param size size of the @a block
666 * @param ret pointer to where to write the hashcode
669 GNUNET_CRYPTO_hash (const void *block,
671 struct GNUNET_HashCode *ret);
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);
724 * Calculate HMAC of a message (RFC 2104)
726 * @param key secret key
727 * @param plaintext input plaintext
728 * @param plaintext_len length of @a plaintext
729 * @param hmac where to store the hmac
732 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
733 const void *plaintext,
734 size_t plaintext_len,
735 struct GNUNET_HashCode *hmac);
739 * Function called once the hash computation over the
740 * specified file has completed.
743 * @param res resulting hash, NULL on error
746 (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
747 const struct GNUNET_HashCode *res);
751 * Handle to file hashing operation.
753 struct GNUNET_CRYPTO_FileHashContext;
758 * Compute the hash of an entire file.
760 * @param priority scheduling priority to use
761 * @param filename name of file to hash
762 * @param blocksize number of bytes to process in one task
763 * @param callback function to call upon completion
764 * @param callback_cls closure for @a callback
765 * @return NULL on (immediate) errror
767 struct GNUNET_CRYPTO_FileHashContext *
768 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
769 const char *filename,
771 GNUNET_CRYPTO_HashCompletedCallback callback,
776 * Cancel a file hashing operation.
778 * @param fhc operation to cancel (callback must not yet have been invoked)
781 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
786 * Create a random hash code.
788 * @param mode desired quality level
789 * @param result hash code that is randomized
792 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
793 struct GNUNET_HashCode *result);
798 * compute @a result = @a b - @a a
800 * @param a some hash code
801 * @param b some hash code
802 * @param result set to @a b - @a a
805 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
806 const struct GNUNET_HashCode *b,
807 struct GNUNET_HashCode *result);
812 * compute @a result = @a a + @a delta
814 * @param a some hash code
815 * @param delta some hash code
816 * @param result set to @a a + @a delta
819 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a,
820 const struct GNUNET_HashCode *delta,
821 struct GNUNET_HashCode *result);
826 * compute result = a ^ b
828 * @param a some hash code
829 * @param b some hash code
830 * @param result set to @a a ^ @a b
833 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
834 const struct GNUNET_HashCode *b,
835 struct GNUNET_HashCode *result);
840 * Convert a hashcode into a key.
842 * @param hc hash code that serves to generate the key
843 * @param skey set to a valid session key
844 * @param iv set to a valid initialization vector
847 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
848 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
849 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv);
854 * Obtain a bit from a hashcode.
856 * @param code the `struct GNUNET_HashCode` to index bit-wise
857 * @param bit index into the hashcode, [0...159]
858 * @return Bit \a bit from hashcode \a code, -1 for invalid index
861 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code,
867 * Determine how many low order bits match in two
868 * `struct GNUNET_HashCodes`. i.e. - 010011 and 011111 share
869 * the first two lowest order bits, and therefore the
870 * return value is two (NOT XOR distance, nor how many
871 * bits match absolutely!).
873 * @param first the first hashcode
874 * @param second the hashcode to compare first to
875 * @return the number of bits that match
878 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
879 const struct GNUNET_HashCode *second);
884 * Compare function for HashCodes, producing a total ordering
887 * @param h1 some hash code
888 * @param h2 some hash code
889 * @return 1 if @a h1 > @a h2, -1 if @a h1 < @a h2 and 0 if @a h1 == @a h2.
892 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
893 const struct GNUNET_HashCode *h2);
898 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
899 * in the XOR metric (Kademlia).
901 * @param h1 some hash code
902 * @param h2 some hash code
903 * @param target some hash code
904 * @return -1 if @a h1 is closer, 1 if @a h2 is closer and 0 if @a h1== @a h2.
907 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
908 const struct GNUNET_HashCode *h2,
909 const struct GNUNET_HashCode *target);
914 * @brief Derive an authentication key
915 * @param key authentication key
916 * @param rkey root key
918 * @param salt_len size of the salt
919 * @param argp pair of void * & size_t for context chunks, terminated by NULL
922 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
923 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
924 const void *salt, size_t salt_len,
930 * @brief Derive an authentication key
931 * @param key authentication key
932 * @param rkey root key
934 * @param salt_len size of the salt
935 * @param ... pair of void * & size_t for context chunks, terminated by NULL
938 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
939 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
940 const void *salt, size_t salt_len,
947 * @param result buffer for the derived key, allocated by caller
948 * @param out_len desired length of the derived key
949 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
950 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
952 * @param xts_len length of @a xts
953 * @param skm source key material
954 * @param skm_len length of @a skm
955 * @param ... pair of void * & size_t for context chunks, terminated by NULL
956 * @return #GNUNET_YES on success
959 GNUNET_CRYPTO_hkdf (void *result,
973 * @param result buffer for the derived key, allocated by caller
974 * @param out_len desired length of the derived key
975 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
976 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
978 * @param xts_len length of @a xts
979 * @param skm source key material
980 * @param skm_len length of @a skm
981 * @param argp va_list of void * & size_t pairs for context chunks
982 * @return #GNUNET_YES on success
985 GNUNET_CRYPTO_hkdf_v (void *result,
998 * @param result buffer for the derived key, allocated by caller
999 * @param out_len desired length of the derived key
1001 * @param xts_len length of @a xts
1002 * @param skm source key material
1003 * @param skm_len length of @a skm
1004 * @param argp va_list of void * & size_t pairs for context chunks
1005 * @return #GNUNET_YES on success
1008 GNUNET_CRYPTO_kdf_v (void *result,
1020 * @param result buffer for the derived key, allocated by caller
1021 * @param out_len desired length of the derived key
1023 * @param xts_len length of @a xts
1024 * @param skm source key material
1025 * @param skm_len length of @a skm
1026 * @param ... void * & size_t pairs for context chunks
1027 * @return #GNUNET_YES on success
1030 GNUNET_CRYPTO_kdf (void *result,
1041 * Extract the public key for the given private key.
1043 * @param priv the private key
1044 * @param pub where to write the public key
1047 GNUNET_CRYPTO_ecdsa_key_get_public (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1048 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1052 * Extract the public key for the given private key.
1054 * @param priv the private key
1055 * @param pub where to write the public key
1058 GNUNET_CRYPTO_eddsa_key_get_public (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1059 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1065 * Extract the public key for the given private key.
1067 * @param priv the private key
1068 * @param pub where to write the public key
1071 GNUNET_CRYPTO_ecdhe_key_get_public (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1072 struct GNUNET_CRYPTO_EcdhePublicKey *pub);
1076 * Convert a public key to a string.
1078 * @param pub key to convert
1079 * @return string representing @a pub
1082 GNUNET_CRYPTO_ecdsa_public_key_to_string (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1086 * Convert a public key to a string.
1088 * @param pub key to convert
1089 * @return string representing @a pub
1092 GNUNET_CRYPTO_eddsa_public_key_to_string (const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1096 * Convert a string representing a public key to a public key.
1098 * @param enc encoded public key
1099 * @param enclen number of bytes in @a enc (without 0-terminator)
1100 * @param pub where to store the public key
1101 * @return #GNUNET_OK on success
1104 GNUNET_CRYPTO_ecdsa_public_key_from_string (const char *enc,
1106 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1110 * Convert a string representing a private key to a private key.
1112 * @param enc encoded public key
1113 * @param enclen number of bytes in @a enc (without 0-terminator)
1114 * @param priv where to store the private key
1115 * @return #GNUNET_OK on success
1118 GNUNET_CRYPTO_eddsa_private_key_from_string (const char *enc,
1120 struct GNUNET_CRYPTO_EddsaPrivateKey *pub);
1124 * Convert a string representing a public key to a public key.
1126 * @param enc encoded public key
1127 * @param enclen number of bytes in @a enc (without 0-terminator)
1128 * @param pub where to store the public key
1129 * @return #GNUNET_OK on success
1132 GNUNET_CRYPTO_eddsa_public_key_from_string (const char *enc,
1134 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1139 * Create a new private key by reading it from a file. If the
1140 * files does not exist, create a new key and write it to the
1141 * file. Caller must free return value. Note that this function
1142 * can not guarantee that another process might not be trying
1143 * the same operation on the same file at the same time.
1144 * If the contents of the file
1145 * are invalid the old file is deleted and a fresh key is
1148 * @param filename name of file to use to store the key
1149 * @return new private key, NULL on error (for example,
1150 * permission denied); free using #GNUNET_free
1152 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1153 GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename);
1158 * Create a new private key by reading it from a file. If the
1159 * files does not exist, create a new key and write it to the
1160 * file. Caller must free return value. Note that this function
1161 * can not guarantee that another process might not be trying
1162 * the same operation on the same file at the same time.
1163 * If the contents of the file
1164 * are invalid the old file is deleted and a fresh key is
1167 * @param filename name of file to use to store the key
1168 * @return new private key, NULL on error (for example,
1169 * permission denied); free using #GNUNET_free
1171 struct GNUNET_CRYPTO_EddsaPrivateKey *
1172 GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename);
1176 * Forward declaration to simplify #include-structure.
1178 struct GNUNET_CONFIGURATION_Handle;
1183 * Create a new private key by reading our peer's key from
1184 * the file specified in the configuration.
1186 * @param cfg the configuration to use
1187 * @return new private key, NULL on error (for example,
1188 * permission denied); free using #GNUNET_free
1190 struct GNUNET_CRYPTO_EddsaPrivateKey *
1191 GNUNET_CRYPTO_eddsa_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
1196 * Create a new private key. Caller must free return value.
1198 * @return fresh private key; free using #GNUNET_free
1200 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1201 GNUNET_CRYPTO_ecdsa_key_create (void);
1206 * Create a new private key. Caller must free return value.
1208 * @return fresh private key; free using #GNUNET_free
1210 struct GNUNET_CRYPTO_EddsaPrivateKey *
1211 GNUNET_CRYPTO_eddsa_key_create (void);
1216 * Create a new private key. Caller must free return value.
1218 * @return fresh private key; free using #GNUNET_free
1220 struct GNUNET_CRYPTO_EcdhePrivateKey *
1221 GNUNET_CRYPTO_ecdhe_key_create (void);
1226 * Clear memory that was used to store a private key.
1228 * @param pk location of the key
1231 GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk);
1236 * Clear memory that was used to store a private key.
1238 * @param pk location of the key
1241 GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk);
1246 * Clear memory that was used to store a private key.
1248 * @param pk location of the key
1251 GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1256 * Get the shared private key we use for anonymous users.
1258 * @return "anonymous" private key; do not free
1260 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
1261 GNUNET_CRYPTO_ecdsa_key_get_anonymous (void);
1266 * Setup a hostkey file for a peer given the name of the
1267 * configuration file (!). This function is used so that
1268 * at a later point code can be certain that reading a
1269 * hostkey is fast (for example in time-dependent testcases).
1271 * @param cfg_name name of the configuration file to use
1274 GNUNET_CRYPTO_eddsa_setup_hostkey (const char *cfg_name);
1279 * Retrieve the identity of the host's peer.
1281 * @param cfg configuration to use
1282 * @param dst pointer to where to write the peer identity
1283 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
1284 * could not be retrieved
1287 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
1288 struct GNUNET_PeerIdentity *dst);
1292 * Compare two Peer Identities.
1294 * @param first first peer identity
1295 * @param second second peer identity
1296 * @return bigger than 0 if first > second,
1297 * 0 if they are the same
1298 * smaller than 0 if second > first
1301 GNUNET_CRYPTO_cmp_peer_identity (const struct GNUNET_PeerIdentity *first,
1302 const struct GNUNET_PeerIdentity *second);
1306 * Internal structure used to cache pre-calculated values for DLOG calculation.
1308 struct GNUNET_CRYPTO_EccDlogContext;
1312 * Point on a curve (always for Curve25519) encoded in a format suitable
1313 * for network transmission (ECDH), see http://cr.yp.to/ecdh.html.
1315 struct GNUNET_CRYPTO_EccPoint
1318 * Q consists of an x- and a y-value, each mod p (256 bits), given
1319 * here in affine coordinates and Ed25519 standard compact format.
1321 unsigned char q_y[256 / 8];
1326 * Do pre-calculation for ECC discrete logarithm for small factors.
1328 * @param max maximum value the factor can be
1329 * @param mem memory to use (should be smaller than @a max), must not be zero.
1330 * @return @a max if dlog failed, otherwise the factor
1332 struct GNUNET_CRYPTO_EccDlogContext *
1333 GNUNET_CRYPTO_ecc_dlog_prepare (unsigned int max,
1338 * Calculate ECC discrete logarithm for small factors.
1339 * Opposite of #GNUNET_CRYPTO_ecc_dexp().
1341 * @param dlc precalculated values, determine range of factors
1342 * @param input point on the curve to factor
1343 * @return `dlc->max` if dlog failed, otherwise the factor
1346 GNUNET_CRYPTO_ecc_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc,
1347 gcry_mpi_point_t input);
1351 * Multiply the generator g of the elliptic curve by @a val
1352 * to obtain the point on the curve representing @a val.
1353 * Afterwards, point addition will correspond to integer
1354 * addition. #GNUNET_CRYPTO_ecc_dlog() can be used to
1355 * convert a point back to an integer (as long as the
1356 * integer is smaller than the MAX of the @a edc context).
1358 * @param edc calculation context for ECC operations
1359 * @param val value to encode into a point
1360 * @return representation of the value as an ECC point,
1361 * must be freed using #GNUNET_CRYPTO_ecc_free()
1364 GNUNET_CRYPTO_ecc_dexp (struct GNUNET_CRYPTO_EccDlogContext *edc,
1369 * Multiply the generator g of the elliptic curve by @a val
1370 * to obtain the point on the curve representing @a val.
1372 * @param edc calculation context for ECC operations
1373 * @param val (positive) value to encode into a point
1374 * @return representation of the value as an ECC point,
1375 * must be freed using #GNUNET_CRYPTO_ecc_free()
1378 GNUNET_CRYPTO_ecc_dexp_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1383 * Multiply the point @a p on the elliptic curve by @a val.
1385 * @param edc calculation context for ECC operations
1386 * @param p point to multiply
1387 * @param val (positive) value to encode into a point
1388 * @return representation of the value as an ECC point,
1389 * must be freed using #GNUNET_CRYPTO_ecc_free()
1392 GNUNET_CRYPTO_ecc_pmul_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1398 * Convert point value to binary representation.
1400 * @param edc calculation context for ECC operations
1401 * @param point computational point representation
1402 * @param[out] bin binary point representation
1405 GNUNET_CRYPTO_ecc_point_to_bin (struct GNUNET_CRYPTO_EccDlogContext *edc,
1406 gcry_mpi_point_t point,
1407 struct GNUNET_CRYPTO_EccPoint *bin);
1411 * Convert binary representation of a point to computational representation.
1413 * @param edc calculation context for ECC operations
1414 * @param bin binary point representation
1415 * @return computational representation
1418 GNUNET_CRYPTO_ecc_bin_to_point (struct GNUNET_CRYPTO_EccDlogContext *edc,
1419 const struct GNUNET_CRYPTO_EccPoint *bin);
1423 * Add two points on the elliptic curve.
1425 * @param edc calculation context for ECC operations
1426 * @param a some value
1427 * @param b some value
1428 * @return @a a + @a b, must be freed using #GNUNET_CRYPTO_ecc_free()
1431 GNUNET_CRYPTO_ecc_add (struct GNUNET_CRYPTO_EccDlogContext *edc,
1433 gcry_mpi_point_t b);
1437 * Obtain a random point on the curve and its
1438 * additive inverse. Both returned values
1439 * must be freed using #GNUNET_CRYPTO_ecc_free().
1441 * @param edc calculation context for ECC operations
1442 * @param[out] r set to a random point on the curve
1443 * @param[out] r_inv set to the additive inverse of @a r
1446 GNUNET_CRYPTO_ecc_rnd (struct GNUNET_CRYPTO_EccDlogContext *edc,
1447 gcry_mpi_point_t *r,
1448 gcry_mpi_point_t *r_inv);
1452 * Obtain a random scalar for point multiplication on the curve and
1453 * its multiplicative inverse.
1455 * @param edc calculation context for ECC operations
1456 * @param[out] r set to a random scalar on the curve
1457 * @param[out] r_inv set to the multiplicative inverse of @a r
1460 GNUNET_CRYPTO_ecc_rnd_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1466 * Generate a random value mod n.
1468 * @param edc ECC context
1469 * @return random value mod n.
1472 GNUNET_CRYPTO_ecc_random_mod_n (struct GNUNET_CRYPTO_EccDlogContext *edc);
1476 * Free a point value returned by the API.
1478 * @param p point to free
1481 GNUNET_CRYPTO_ecc_free (gcry_mpi_point_t p);
1485 * Release precalculated values.
1487 * @param dlc dlog context
1490 GNUNET_CRYPTO_ecc_dlog_release (struct GNUNET_CRYPTO_EccDlogContext *dlc);
1495 * Derive key material from a public and a private ECC key.
1497 * @param priv private key to use for the ECDH (x)
1498 * @param pub public key to use for the ECDH (yG)
1499 * @param key_material where to write the key material (xyG)
1500 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1503 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1504 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1505 struct GNUNET_HashCode *key_material);
1510 * Derive key material from a ECDH public key and a private EdDSA key.
1511 * Dual to #GNUNET_CRRYPTO_ecdh_eddsa.
1513 * @param priv private key from EdDSA to use for the ECDH (x)
1514 * @param pub public key to use for the ECDH (yG)
1515 * @param key_material where to write the key material H(h(x)yG)
1516 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1519 GNUNET_CRYPTO_eddsa_ecdh (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1520 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1521 struct GNUNET_HashCode *key_material);
1525 * Derive key material from a ECDH public key and a private ECDSA key.
1526 * Dual to #GNUNET_CRRYPTO_ecdh_ecdsa.
1528 * @param priv private key from ECDSA to use for the ECDH (x)
1529 * @param pub public key to use for the ECDH (yG)
1530 * @param key_material where to write the key material H(h(x)yG)
1531 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1534 GNUNET_CRYPTO_ecdsa_ecdh (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1535 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1536 struct GNUNET_HashCode *key_material);
1541 * Derive key material from a EdDSA public key and a private ECDH key.
1542 * Dual to #GNUNET_CRRYPTO_eddsa_ecdh.
1544 * @param priv private key to use for the ECDH (y)
1545 * @param pub public key from EdDSA to use for the ECDH (X=h(x)G)
1546 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1547 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1550 GNUNET_CRYPTO_ecdh_eddsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1551 const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
1552 struct GNUNET_HashCode *key_material);
1556 * Derive key material from a EcDSA public key and a private ECDH key.
1557 * Dual to #GNUNET_CRRYPTO_ecdsa_ecdh.
1559 * @param priv private key to use for the ECDH (y)
1560 * @param pub public key from ECDSA to use for the ECDH (X=h(x)G)
1561 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1562 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1565 GNUNET_CRYPTO_ecdh_ecdsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1566 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1567 struct GNUNET_HashCode *key_material);
1572 * EdDSA sign a given block.
1574 * @param priv private key to use for the signing
1575 * @param purpose what to sign (size, purpose)
1576 * @param sig where to write the signature
1577 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1580 GNUNET_CRYPTO_eddsa_sign (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1581 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1582 struct GNUNET_CRYPTO_EddsaSignature *sig);
1587 * ECDSA Sign a given block.
1589 * @param priv private key to use for the signing
1590 * @param purpose what to sign (size, purpose)
1591 * @param sig where to write the signature
1592 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1595 GNUNET_CRYPTO_ecdsa_sign (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1596 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1597 struct GNUNET_CRYPTO_EcdsaSignature *sig);
1601 * Verify EdDSA signature.
1603 * @param purpose what is the purpose that the signature should have?
1604 * @param validate block to validate (size, purpose, data)
1605 * @param sig signature that is being validated
1606 * @param pub public key of the signer
1607 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1610 GNUNET_CRYPTO_eddsa_verify (uint32_t purpose,
1611 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1612 const struct GNUNET_CRYPTO_EddsaSignature *sig,
1613 const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1619 * Verify ECDSA signature.
1621 * @param purpose what is the purpose that the signature should have?
1622 * @param validate block to validate (size, purpose, data)
1623 * @param sig signature that is being validated
1624 * @param pub public key of the signer
1625 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1628 GNUNET_CRYPTO_ecdsa_verify (uint32_t purpose,
1629 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1630 const struct GNUNET_CRYPTO_EcdsaSignature *sig,
1631 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1636 * Derive a private key from a given private key and a label.
1637 * Essentially calculates a private key 'h = H(l,P) * d mod n'
1638 * where n is the size of the ECC group and P is the public
1639 * key associated with the private key 'd'.
1641 * @param priv original private key
1642 * @param label label to use for key deriviation
1643 * @param context additional context to use for HKDF of 'h';
1644 * typically the name of the subsystem/application
1645 * @return derived private key
1647 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1648 GNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1650 const char *context);
1655 * Derive a public key from a given public key and a label.
1656 * Essentially calculates a public key 'V = H(l,P) * P'.
1658 * @param pub original public key
1659 * @param label label to use for key deriviation
1660 * @param context additional context to use for HKDF of 'h'.
1661 * typically the name of the subsystem/application
1662 * @param result where to write the derived public key
1665 GNUNET_CRYPTO_ecdsa_public_key_derive (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1667 const char *context,
1668 struct GNUNET_CRYPTO_EcdsaPublicKey *result);
1672 * Output the given MPI value to the given buffer in network
1673 * byte order. The MPI @a val may not be negative.
1675 * @param buf where to output to
1676 * @param size number of bytes in @a buf
1677 * @param val value to write to @a buf
1680 GNUNET_CRYPTO_mpi_print_unsigned (void *buf,
1686 * Convert data buffer into MPI value.
1687 * The buffer is interpreted as network
1688 * byte order, unsigned integer.
1690 * @param result where to store MPI value (allocated)
1691 * @param data raw data (GCRYMPI_FMT_USG)
1692 * @param size number of bytes in @a data
1695 GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result,
1701 * Create a freshly generated paillier public key.
1703 * @param[out] public_key Where to store the public key?
1704 * @param[out] private_key Where to store the private key?
1707 GNUNET_CRYPTO_paillier_create (struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1708 struct GNUNET_CRYPTO_PaillierPrivateKey *private_key);
1712 * Encrypt a plaintext with a paillier public key.
1714 * @param public_key Public key to use.
1715 * @param m Plaintext to encrypt.
1716 * @param desired_ops How many homomorphic ops the caller intends to use
1717 * @param[out] ciphertext Encrytion of @a plaintext with @a public_key.
1718 * @return guaranteed number of supported homomorphic operations >= 1,
1719 * or desired_ops, in case that is lower,
1720 * or -1 if less than one homomorphic operation is possible
1723 GNUNET_CRYPTO_paillier_encrypt (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1726 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext);
1730 * Decrypt a paillier ciphertext with a private key.
1732 * @param private_key Private key to use for decryption.
1733 * @param public_key Public key to use for decryption.
1734 * @param ciphertext Ciphertext to decrypt.
1735 * @param[out] m Decryption of @a ciphertext with @private_key.
1738 GNUNET_CRYPTO_paillier_decrypt (const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key,
1739 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1740 const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
1745 * Compute a ciphertext that represents the sum of the plaintext in @a x1 and @a x2
1747 * Note that this operation can only be done a finite number of times
1748 * before an overflow occurs.
1750 * @param public_key Public key to use for encryption.
1751 * @param c1 Paillier cipher text.
1752 * @param c2 Paillier cipher text.
1753 * @param[out] result Result of the homomorphic operation.
1754 * @return #GNUNET_OK if the result could be computed,
1755 * #GNUNET_SYSERR if no more homomorphic operations are remaining.
1758 GNUNET_CRYPTO_paillier_hom_add (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1759 const struct GNUNET_CRYPTO_PaillierCiphertext *c1,
1760 const struct GNUNET_CRYPTO_PaillierCiphertext *c2,
1761 struct GNUNET_CRYPTO_PaillierCiphertext *result);
1765 * Get the number of remaining supported homomorphic operations.
1767 * @param c Paillier cipher text.
1768 * @return the number of remaining homomorphic operations
1771 GNUNET_CRYPTO_paillier_hom_get_remaining (const struct GNUNET_CRYPTO_PaillierCiphertext *c);
1774 /* ********* Chaum-style RSA-based blind signatures ******************* */
1780 * The private information of an RSA key pair.
1782 struct GNUNET_CRYPTO_RsaPrivateKey;
1785 * The public information of an RSA key pair.
1787 struct GNUNET_CRYPTO_RsaPublicKey;
1790 * Key used to blind a message
1792 struct GNUNET_CRYPTO_rsa_BlindingKey;
1795 * @brief an RSA signature
1797 struct GNUNET_CRYPTO_RsaSignature;
1801 * Create a new private key. Caller must free return value.
1803 * @param len length of the key in bits (i.e. 2048)
1804 * @return fresh private key
1806 struct GNUNET_CRYPTO_RsaPrivateKey *
1807 GNUNET_CRYPTO_rsa_private_key_create (unsigned int len);
1811 * Free memory occupied by the private key.
1813 * @param key pointer to the memory to free
1816 GNUNET_CRYPTO_rsa_private_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *key);
1820 * Encode the private key in a format suitable for
1821 * storing it into a file.
1823 * @param key the private key
1824 * @param[out] buffer set to a buffer with the encoded key
1825 * @return size of memory allocatedin @a buffer
1828 GNUNET_CRYPTO_rsa_private_key_encode (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
1833 * Decode the private key from the data-format back
1834 * to the "normal", internal format.
1836 * @param buf the buffer where the private key data is stored
1837 * @param len the length of the data in @a buf
1838 * @return NULL on error
1840 struct GNUNET_CRYPTO_RsaPrivateKey *
1841 GNUNET_CRYPTO_rsa_private_key_decode (const char *buf,
1846 * Duplicate the given private key
1848 * @param key the private key to duplicate
1849 * @return the duplicate key; NULL upon error
1851 struct GNUNET_CRYPTO_RsaPrivateKey *
1852 GNUNET_CRYPTO_rsa_private_key_dup (const struct GNUNET_CRYPTO_RsaPrivateKey *key);
1856 * Extract the public key of the given private key.
1858 * @param priv the private key
1859 * @retur NULL on error, otherwise the public key
1861 struct GNUNET_CRYPTO_RsaPublicKey *
1862 GNUNET_CRYPTO_rsa_private_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey *priv);
1866 * Compute hash over the public key.
1868 * @param key public key to hash
1869 * @param hc where to store the hash code
1872 GNUNET_CRYPTO_rsa_public_key_hash (const struct GNUNET_CRYPTO_RsaPublicKey *key,
1873 struct GNUNET_HashCode *hc);
1877 * Obtain the length of the RSA key in bits.
1879 * @param key the public key to introspect
1880 * @return length of the key in bits
1883 GNUNET_CRYPTO_rsa_public_key_len (const struct GNUNET_CRYPTO_RsaPublicKey *key);
1887 * Free memory occupied by the public key.
1889 * @param key pointer to the memory to free
1892 GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_RsaPublicKey *key);
1896 * Encode the public key in a format suitable for
1897 * storing it into a file.
1899 * @param key the private key
1900 * @param[out] buffer set to a buffer with the encoded key
1901 * @return size of memory allocated in @a buffer
1904 GNUNET_CRYPTO_rsa_public_key_encode (const struct GNUNET_CRYPTO_RsaPublicKey *key,
1909 * Decode the public key from the data-format back
1910 * to the "normal", internal format.
1912 * @param buf the buffer where the public key data is stored
1913 * @param len the length of the data in @a buf
1914 * @return NULL on error
1916 struct GNUNET_CRYPTO_RsaPublicKey *
1917 GNUNET_CRYPTO_rsa_public_key_decode (const char *buf,
1922 * Duplicate the given public key
1924 * @param key the public key to duplicate
1925 * @return the duplicate key; NULL upon error
1927 struct GNUNET_CRYPTO_RsaPublicKey *
1928 GNUNET_CRYPTO_rsa_public_key_dup (const struct GNUNET_CRYPTO_RsaPublicKey *key);
1932 * Create a blinding key
1934 * @param len length of the key in bits (i.e. 2048)
1935 * @return the newly created blinding key
1937 struct GNUNET_CRYPTO_rsa_BlindingKey *
1938 GNUNET_CRYPTO_rsa_blinding_key_create (unsigned int len);
1942 * Compare the values of two blinding keys.
1945 * @param b2 the other key
1946 * @return 0 if the two are equal
1949 GNUNET_CRYPTO_rsa_blinding_key_cmp (struct GNUNET_CRYPTO_rsa_BlindingKey *b1,
1950 struct GNUNET_CRYPTO_rsa_BlindingKey *b2);
1954 * Compare the values of two signatures.
1956 * @param s1 one signature
1957 * @param s2 the other signature
1958 * @return 0 if the two are equal
1961 GNUNET_CRYPTO_rsa_signature_cmp (struct GNUNET_CRYPTO_RsaSignature *s1,
1962 struct GNUNET_CRYPTO_RsaSignature *s2);
1965 * Compare the values of two private keys.
1967 * @param p1 one private key
1968 * @param p2 the other private key
1969 * @return 0 if the two are equal
1972 GNUNET_CRYPTO_rsa_private_key_cmp (struct GNUNET_CRYPTO_RsaPrivateKey *p1,
1973 struct GNUNET_CRYPTO_RsaPrivateKey *p2);
1977 * Compare the values of two public keys.
1979 * @param p1 one public key
1980 * @param p2 the other public key
1981 * @return 0 if the two are equal
1984 GNUNET_CRYPTO_rsa_public_key_cmp (struct GNUNET_CRYPTO_RsaPublicKey *p1,
1985 struct GNUNET_CRYPTO_RsaPublicKey *p2);
1989 * Destroy a blinding key
1991 * @param bkey the blinding key to destroy
1994 GNUNET_CRYPTO_rsa_blinding_key_free (struct GNUNET_CRYPTO_rsa_BlindingKey *bkey);
1998 * Encode the blinding key in a format suitable for
1999 * storing it into a file.
2001 * @param bkey the blinding key
2002 * @param[out] buffer set to a buffer with the encoded key
2003 * @return size of memory allocated in @a buffer
2006 GNUNET_CRYPTO_rsa_blinding_key_encode (const struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
2011 * Decode the blinding key from the data-format back
2012 * to the "normal", internal format.
2014 * @param buf the buffer where the public key data is stored
2015 * @param len the length of the data in @a buf
2016 * @return NULL on error
2018 struct GNUNET_CRYPTO_rsa_BlindingKey *
2019 GNUNET_CRYPTO_rsa_blinding_key_decode (const char *buf,
2024 * Blinds the given message with the given blinding key
2026 * @param hash hash of the message to sign
2027 * @param bkey the blinding key
2028 * @param pkey the public key of the signer
2029 * @param[out] buffer set to a buffer with the blinded message to be signed
2030 * @return number of bytes stored in @a buffer
2033 GNUNET_CRYPTO_rsa_blind (const struct GNUNET_HashCode *hash,
2034 struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
2035 struct GNUNET_CRYPTO_RsaPublicKey *pkey,
2040 * Sign the given message.
2042 * @param key private key to use for the signing
2043 * @param msg the (blinded) message to sign
2044 * @param msg_len number of bytes in @a msg to sign
2045 * @return NULL on error, signature on success
2047 struct GNUNET_CRYPTO_RsaSignature *
2048 GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2054 * Free memory occupied by signature.
2056 * @param sig memory to free
2059 GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_RsaSignature *sig);
2063 * Encode the given signature in a format suitable for storing it into a file.
2065 * @param sig the signature
2066 * @param[out] buffer set to a buffer with the encoded key
2067 * @return size of memory allocated in @a buffer
2070 GNUNET_CRYPTO_rsa_signature_encode (const struct GNUNET_CRYPTO_RsaSignature *sig,
2075 * Decode the signature from the data-format back to the "normal", internal
2078 * @param buf the buffer where the public key data is stored
2079 * @param len the length of the data in @a buf
2080 * @return NULL on error
2082 struct GNUNET_CRYPTO_RsaSignature *
2083 GNUNET_CRYPTO_rsa_signature_decode (const char *buf,
2088 * Duplicate the given rsa signature
2090 * @param sig the signature to duplicate
2091 * @return the duplicate key; NULL upon error
2093 struct GNUNET_CRYPTO_RsaSignature *
2094 GNUNET_CRYPTO_rsa_signature_dup (const struct GNUNET_CRYPTO_RsaSignature *sig);
2098 * Unblind a blind-signed signature. The signature should have been generated
2099 * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with
2100 * #GNUNET_CRYPTO_rsa_blind().
2102 * @param sig the signature made on the blinded signature purpose
2103 * @param bkey the blinding key used to blind the signature purpose
2104 * @param pkey the public key of the signer
2105 * @return unblinded signature on success, NULL on error
2107 struct GNUNET_CRYPTO_RsaSignature *
2108 GNUNET_CRYPTO_rsa_unblind (struct GNUNET_CRYPTO_RsaSignature *sig,
2109 struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
2110 struct GNUNET_CRYPTO_RsaPublicKey *pkey);
2114 * Verify whether the given hash corresponds to the given signature and the
2115 * signature is valid with respect to the given public key.
2117 * @param hash the message to verify to match the @a sig
2118 * @param sig signature that is being validated
2119 * @param public_key public key of the signer
2120 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
2123 GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash,
2124 const struct GNUNET_CRYPTO_RsaSignature *sig,
2125 const struct GNUNET_CRYPTO_RsaPublicKey *public_key);
2128 #if 0 /* keep Emacsens' auto-indent happy */
2136 /* ifndef GNUNET_CRYPTO_LIB_H */
2138 /* end of gnunet_crypto_lib.h */