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
49 #if 0 /* keep Emacsens' auto-indent happy */
56 * The identity of the host (wraps the signing key of the peer).
58 struct GNUNET_PeerIdentity;
60 #include "gnunet_common.h"
65 * Maximum length of an ECC signature.
66 * Note: round up to multiple of 8 minus 2 for alignment.
68 #define GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH 126
72 * Desired quality level for random numbers.
75 enum GNUNET_CRYPTO_Quality
78 * No good quality of the operation is needed (i.e.,
79 * random numbers can be pseudo-random).
82 GNUNET_CRYPTO_QUALITY_WEAK,
85 * High-quality operations are desired.
88 GNUNET_CRYPTO_QUALITY_STRONG,
91 * Randomness for IVs etc. is required.
94 GNUNET_CRYPTO_QUALITY_NONCE
99 * @brief length of the sessionkey in bytes (256 BIT sessionkey)
101 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
104 * Length of a hash value
106 #define GNUNET_CRYPTO_HASH_LENGTH (512/8)
109 * How many characters (without 0-terminator) are our ASCII-encoded
110 * public keys (ECDSA/EDDSA/ECDHE).
112 #define GNUNET_CRYPTO_PKEY_ASCII_LENGTH 52
115 * @brief 0-terminated ASCII encoding of a struct GNUNET_HashCode.
117 struct GNUNET_CRYPTO_HashAsciiEncoded
119 unsigned char encoding[104];
123 GNUNET_NETWORK_STRUCT_BEGIN
127 * @brief header of what an ECC signature signs
128 * this must be followed by "size - 8" bytes of
129 * the actual signed data
131 struct GNUNET_CRYPTO_EccSignaturePurpose
134 * How many bytes does this signature sign?
135 * (including this purpose header); in network
138 uint32_t size GNUNET_PACKED;
141 * What does this signature vouch for? This
142 * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
143 * constant (from gnunet_signatures.h). In
144 * network byte order!
146 uint32_t purpose GNUNET_PACKED;
152 * @brief an ECC signature using EdDSA.
153 * See https://gnunet.org/ed25519
155 struct GNUNET_CRYPTO_EddsaSignature
161 unsigned char r[256 / 8];
166 unsigned char s[256 / 8];
173 * @brief an ECC signature using ECDSA
175 struct GNUNET_CRYPTO_EcdsaSignature
181 unsigned char r[256 / 8];
186 unsigned char s[256 / 8];
192 * Public ECC key (always for curve Ed25519) encoded in a format
193 * suitable for network transmission and EdDSA signatures.
195 struct GNUNET_CRYPTO_EddsaPublicKey
198 * Point Q consists of a y-value mod p (256 bits); the x-value is
199 * always positive. The point is stored in Ed25519 standard
202 unsigned char q_y[256 / 8];
208 * Public ECC key (always for Curve25519) encoded in a format suitable
209 * for network transmission and ECDSA signatures.
211 struct GNUNET_CRYPTO_EcdsaPublicKey
214 * Q consists of an x- and a y-value, each mod p (256 bits), given
215 * here in affine coordinates and Ed25519 standard compact format.
217 unsigned char q_y[256 / 8];
223 * The identity of the host (wraps the signing key of the peer).
225 struct GNUNET_PeerIdentity
227 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
232 * Public ECC key (always for Curve25519) encoded in a format suitable
233 * for network transmission and encryption (ECDH),
234 * See http://cr.yp.to/ecdh.html
236 struct GNUNET_CRYPTO_EcdhePublicKey
239 * Q consists of an x- and a y-value, each mod p (256 bits), given
240 * here in affine coordinates and Ed25519 standard compact format.
242 unsigned char q_y[256 / 8];
247 * Private ECC key encoded for transmission. To be used only for ECDH
248 * key exchange (ECDHE to be precise).
250 struct GNUNET_CRYPTO_EcdhePrivateKey
253 * d is a value mod n, where n has at most 256 bits.
255 unsigned char d[256 / 8];
260 * Private ECC key encoded for transmission. To be used only for ECDSA
263 struct GNUNET_CRYPTO_EcdsaPrivateKey
266 * d is a value mod n, where n has at most 256 bits.
268 unsigned char d[256 / 8];
273 * Private ECC key encoded for transmission. To be used only for EdDSA
276 struct GNUNET_CRYPTO_EddsaPrivateKey
279 * d is a value mod n, where n has at most 256 bits.
281 unsigned char d[256 / 8];
287 * @brief type for session keys
289 struct GNUNET_CRYPTO_SymmetricSessionKey
292 * Actual key for AES.
294 unsigned char aes_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
297 * Actual key for TwoFish.
299 unsigned char twofish_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
303 GNUNET_NETWORK_STRUCT_END
306 * @brief IV for sym cipher
308 * NOTE: must be smaller (!) in size than the
309 * `struct GNUNET_HashCode`.
311 struct GNUNET_CRYPTO_SymmetricInitializationVector
313 unsigned char aes_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
315 unsigned char twofish_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
320 * @brief type for (message) authentication keys
322 struct GNUNET_CRYPTO_AuthKey
324 unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
329 * Size of paillier plain texts and public keys.
330 * Private keys and ciphertexts are twice this size.
332 #define GNUNET_CRYPTO_PAILLIER_BITS 2048
336 * Paillier public key.
338 struct GNUNET_CRYPTO_PaillierPublicKey
343 unsigned char n[GNUNET_CRYPTO_PAILLIER_BITS / 8];
348 * Paillier private key.
350 struct GNUNET_CRYPTO_PaillierPrivateKey
353 * Lambda-component of the private key.
355 unsigned char lambda[GNUNET_CRYPTO_PAILLIER_BITS / 8];
357 * Mu-component of the private key.
359 unsigned char mu[GNUNET_CRYPTO_PAILLIER_BITS / 8];
364 * Paillier ciphertext.
366 struct GNUNET_CRYPTO_PaillierCiphertext
369 * Guaranteed minimum number of homomorphic operations with this ciphertext,
370 * in network byte order (NBO).
372 int32_t remaining_ops GNUNET_PACKED;
375 * The bits of the ciphertext.
377 unsigned char bits[GNUNET_CRYPTO_PAILLIER_BITS * 2 / 8];
381 /* **************** Functions and Macros ************* */
385 * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator
388 * @param seed the seed to use
391 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
396 * Calculate the checksum of a buffer in one step.
398 * @param buf buffer to calculate CRC over
399 * @param len number of bytes in @a buf
403 GNUNET_CRYPTO_crc8_n (const void *buf,
408 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
410 * @param sum current sum, initially 0
411 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
412 * @param len number of bytes in @a buf, must be multiple of 2
413 * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish to get actual crc16)
416 GNUNET_CRYPTO_crc16_step (uint32_t sum,
422 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
424 * @param sum cummulative sum
425 * @return crc16 value
428 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
433 * Calculate the checksum of a buffer in one step.
435 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
436 * @param len number of bytes in @a buf, must be multiple of 2
437 * @return crc16 value
440 GNUNET_CRYPTO_crc16_n (const void *buf,
448 * Compute the CRC32 checksum for the first len
449 * bytes of the buffer.
451 * @param buf the data over which we're taking the CRC
452 * @param len the length of the buffer @a buf in bytes
453 * @return the resulting CRC32 checksum
456 GNUNET_CRYPTO_crc32_n (const void *buf,
462 * Fill block with a random values.
464 * @param mode desired quality of the random number
465 * @param buffer the buffer to fill
466 * @param length buffer length
469 GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode,
475 * Produce a random value.
477 * @param mode desired quality of the random number
478 * @param i the upper limit (exclusive) for the random number
479 * @return a random value in the interval [0,@a i) (exclusive).
482 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode,
488 * Random on unsigned 64-bit values.
490 * @param mode desired quality of the random number
491 * @param max value returned will be in range [0,@a max) (exclusive)
492 * @return random 64-bit number
495 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
501 * Get an array with a random permutation of the
503 * @param mode #GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used,
504 * #GNUNET_CRYPTO_QUALITY_WEAK or #GNUNET_CRYPTO_QUALITY_NONCE otherwise
505 * @param n the size of the array
506 * @return the permutation array (allocated from heap)
509 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode,
515 * Create a new random session key.
517 * @param key key to initialize
520 GNUNET_CRYPTO_symmetric_create_session_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key);
525 * Encrypt a block using a symmetric sessionkey.
527 * @param block the block to encrypt
528 * @param size the size of the @a block
529 * @param sessionkey the key used to encrypt
530 * @param iv the initialization vector to use, use INITVALUE
532 * @return the size of the encrypted block, -1 for errors
535 GNUNET_CRYPTO_symmetric_encrypt (const void *block,
537 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
538 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
544 * Decrypt a given block using a symmetric sessionkey.
546 * @param block the data to decrypt, encoded as returned by encrypt
547 * @param size how big is the block?
548 * @param sessionkey the key used to decrypt
549 * @param iv the initialization vector to use
550 * @param result address to store the result at
551 * @return -1 on failure, size of decrypted block on success
554 GNUNET_CRYPTO_symmetric_decrypt (const void *block,
556 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
557 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
563 * @brief Derive an IV
564 * @param iv initialization vector
565 * @param skey session key
566 * @param salt salt for the derivation
567 * @param salt_len size of the @a salt
568 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
571 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
572 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
574 size_t salt_len, ...);
578 * @brief Derive an IV
579 * @param iv initialization vector
580 * @param skey session key
581 * @param salt salt for the derivation
582 * @param salt_len size of the @a salt
583 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
586 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
587 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
595 * Convert hash to ASCII encoding.
596 * @param block the hash code
597 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
598 * safely cast to char*, a '\\0' termination is set).
601 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block,
602 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
607 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
609 * @param enc the encoding
610 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
611 * @param result where to store the hash code
612 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
615 GNUNET_CRYPTO_hash_from_string2 (const char *enc,
617 struct GNUNET_HashCode *result);
622 * Convert ASCII encoding back to `struct GNUNET_HashCode`
624 * @param enc the encoding
625 * @param result where to store the hash code
626 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
628 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
629 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
635 * Compute the distance between 2 hashcodes. The
636 * computation must be fast, not involve @a a[0] or @a a[4] (they're used
637 * elsewhere), and be somewhat consistent. And of course, the result
638 * should be a positive number.
640 * @param a some hash code
641 * @param b some hash code
642 * @return number between 0 and UINT32_MAX
645 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a,
646 const struct GNUNET_HashCode *b);
651 * Compute hash of a given block.
653 * @param block the data to hash
654 * @param size size of the @a block
655 * @param ret pointer to where to write the hashcode
658 GNUNET_CRYPTO_hash (const void *block,
660 struct GNUNET_HashCode *ret);
664 * Context for cummulative hashing.
666 struct GNUNET_HashContext;
670 * Start incremental hashing operation.
672 * @return context for incremental hash computation
674 struct GNUNET_HashContext *
675 GNUNET_CRYPTO_hash_context_start (void);
679 * Add data to be hashed.
681 * @param hc cummulative hash context
682 * @param buf data to add
683 * @param size number of bytes in @a buf
686 GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
692 * Finish the hash computation.
694 * @param hc hash context to use, is freed in the process
695 * @param r_hash where to write the latest / final hash code
698 GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
699 struct GNUNET_HashCode *r_hash);
703 * Abort hashing, do not bother calculating final result.
705 * @param hc hash context to destroy
708 GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc);
712 * Calculate HMAC of a message (RFC 2104)
713 * TODO: Shouldn' this be the standard hmac function and
714 * the above be renamed?
716 * @param key secret key
717 * @param key_len secret key length
718 * @param plaintext input plaintext
719 * @param plaintext_len length of @a plaintext
720 * @param hmac where to store the hmac
723 GNUNET_CRYPTO_hmac_raw (const void *key, size_t key_len,
724 const void *plaintext, size_t plaintext_len,
725 struct GNUNET_HashCode *hmac);
730 * Calculate HMAC of a message (RFC 2104)
732 * @param key secret key
733 * @param plaintext input plaintext
734 * @param plaintext_len length of @a plaintext
735 * @param hmac where to store the hmac
738 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
739 const void *plaintext,
740 size_t plaintext_len,
741 struct GNUNET_HashCode *hmac);
745 * Function called once the hash computation over the
746 * specified file has completed.
749 * @param res resulting hash, NULL on error
752 (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
753 const struct GNUNET_HashCode *res);
757 * Handle to file hashing operation.
759 struct GNUNET_CRYPTO_FileHashContext;
764 * Compute the hash of an entire file.
766 * @param priority scheduling priority to use
767 * @param filename name of file to hash
768 * @param blocksize number of bytes to process in one task
769 * @param callback function to call upon completion
770 * @param callback_cls closure for @a callback
771 * @return NULL on (immediate) errror
773 struct GNUNET_CRYPTO_FileHashContext *
774 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
775 const char *filename,
777 GNUNET_CRYPTO_HashCompletedCallback callback,
782 * Cancel a file hashing operation.
784 * @param fhc operation to cancel (callback must not yet have been invoked)
787 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
792 * Create a random hash code.
794 * @param mode desired quality level
795 * @param result hash code that is randomized
798 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
799 struct GNUNET_HashCode *result);
804 * compute @a result = @a b - @a a
806 * @param a some hash code
807 * @param b some hash code
808 * @param result set to @a b - @a a
811 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
812 const struct GNUNET_HashCode *b,
813 struct GNUNET_HashCode *result);
818 * compute @a result = @a a + @a delta
820 * @param a some hash code
821 * @param delta some hash code
822 * @param result set to @a a + @a delta
825 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a,
826 const struct GNUNET_HashCode *delta,
827 struct GNUNET_HashCode *result);
832 * compute result = a ^ b
834 * @param a some hash code
835 * @param b some hash code
836 * @param result set to @a a ^ @a b
839 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
840 const struct GNUNET_HashCode *b,
841 struct GNUNET_HashCode *result);
846 * Convert a hashcode into a key.
848 * @param hc hash code that serves to generate the key
849 * @param skey set to a valid session key
850 * @param iv set to a valid initialization vector
853 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
854 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
855 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv);
860 * Obtain a bit from a hashcode.
862 * @param code the `struct GNUNET_HashCode` to index bit-wise
863 * @param bit index into the hashcode, [0...159]
864 * @return Bit \a bit from hashcode \a code, -1 for invalid index
867 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code,
873 * Determine how many low order bits match in two
874 * `struct GNUNET_HashCodes`. i.e. - 010011 and 011111 share
875 * the first two lowest order bits, and therefore the
876 * return value is two (NOT XOR distance, nor how many
877 * bits match absolutely!).
879 * @param first the first hashcode
880 * @param second the hashcode to compare first to
881 * @return the number of bits that match
884 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
885 const struct GNUNET_HashCode *second);
890 * Compare function for HashCodes, producing a total ordering
893 * @param h1 some hash code
894 * @param h2 some hash code
895 * @return 1 if @a h1 > @a h2, -1 if @a h1 < @a h2 and 0 if @a h1 == @a h2.
898 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
899 const struct GNUNET_HashCode *h2);
904 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
905 * in the XOR metric (Kademlia).
907 * @param h1 some hash code
908 * @param h2 some hash code
909 * @param target some hash code
910 * @return -1 if @a h1 is closer, 1 if @a h2 is closer and 0 if @a h1== @a h2.
913 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
914 const struct GNUNET_HashCode *h2,
915 const struct GNUNET_HashCode *target);
920 * @brief Derive an authentication key
921 * @param key authentication key
922 * @param rkey root key
924 * @param salt_len size of the salt
925 * @param argp pair of void * & size_t for context chunks, terminated by NULL
928 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
929 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
930 const void *salt, size_t salt_len,
936 * @brief Derive an authentication key
937 * @param key authentication key
938 * @param rkey root key
940 * @param salt_len size of the salt
941 * @param ... pair of void * & size_t for context chunks, terminated by NULL
944 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
945 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
946 const void *salt, size_t salt_len,
953 * @param result buffer for the derived key, allocated by caller
954 * @param out_len desired length of the derived key
955 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
956 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
958 * @param xts_len length of @a xts
959 * @param skm source key material
960 * @param skm_len length of @a skm
961 * @param ... pair of void * & size_t for context chunks, terminated by NULL
962 * @return #GNUNET_YES on success
965 GNUNET_CRYPTO_hkdf (void *result,
979 * @param result buffer for the derived key, allocated by caller
980 * @param out_len desired length of the derived key
981 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
982 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
984 * @param xts_len length of @a xts
985 * @param skm source key material
986 * @param skm_len length of @a skm
987 * @param argp va_list of void * & size_t pairs for context chunks
988 * @return #GNUNET_YES on success
991 GNUNET_CRYPTO_hkdf_v (void *result,
1004 * @param result buffer for the derived key, allocated by caller
1005 * @param out_len desired length of the derived key
1007 * @param xts_len length of @a xts
1008 * @param skm source key material
1009 * @param skm_len length of @a skm
1010 * @param argp va_list of void * & size_t pairs for context chunks
1011 * @return #GNUNET_YES on success
1014 GNUNET_CRYPTO_kdf_v (void *result,
1024 * Deterministically generate a pseudo-random number uniformly from the
1025 * integers modulo a libgcrypt mpi.
1027 * @param[out] r MPI value set to the FDH
1028 * @param n MPI to work modulo
1030 * @param xts_len length of @a xts
1031 * @param skm source key material
1032 * @param skm_len length of @a skm
1033 * @param ctx context string
1036 GNUNET_CRYPTO_kdf_mod_mpi (gcry_mpi_t *r,
1038 const void *xts, size_t xts_len,
1039 const void *skm, size_t skm_len,
1046 * @param result buffer for the derived key, allocated by caller
1047 * @param out_len desired length of the derived key
1049 * @param xts_len length of @a xts
1050 * @param skm source key material
1051 * @param skm_len length of @a skm
1052 * @param ... void * & size_t pairs for context chunks
1053 * @return #GNUNET_YES on success
1056 GNUNET_CRYPTO_kdf (void *result,
1067 * Extract the public key for the given private key.
1069 * @param priv the private key
1070 * @param pub where to write the public key
1073 GNUNET_CRYPTO_ecdsa_key_get_public (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1074 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1078 * Extract the public key for the given private key.
1080 * @param priv the private key
1081 * @param pub where to write the public key
1084 GNUNET_CRYPTO_eddsa_key_get_public (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1085 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1091 * Extract the public key for the given private key.
1093 * @param priv the private key
1094 * @param pub where to write the public key
1097 GNUNET_CRYPTO_ecdhe_key_get_public (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1098 struct GNUNET_CRYPTO_EcdhePublicKey *pub);
1102 * Convert a public key to a string.
1104 * @param pub key to convert
1105 * @return string representing @a pub
1108 GNUNET_CRYPTO_ecdsa_public_key_to_string (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1112 * Convert a private key to a string.
1114 * @param priv key to convert
1115 * @return string representing @a pub
1118 GNUNET_CRYPTO_eddsa_private_key_to_string (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv);
1122 * Convert a public key to a string.
1124 * @param pub key to convert
1125 * @return string representing @a pub
1128 GNUNET_CRYPTO_eddsa_public_key_to_string (const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1132 * Convert a string representing a public key to a public key.
1134 * @param enc encoded public key
1135 * @param enclen number of bytes in @a enc (without 0-terminator)
1136 * @param pub where to store the public key
1137 * @return #GNUNET_OK on success
1140 GNUNET_CRYPTO_ecdsa_public_key_from_string (const char *enc,
1142 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1146 * Convert a string representing a private key to a private key.
1148 * @param enc encoded public key
1149 * @param enclen number of bytes in @a enc (without 0-terminator)
1150 * @param priv where to store the private key
1151 * @return #GNUNET_OK on success
1154 GNUNET_CRYPTO_eddsa_private_key_from_string (const char *enc,
1156 struct GNUNET_CRYPTO_EddsaPrivateKey *pub);
1160 * Convert a string representing a public key to a public key.
1162 * @param enc encoded public key
1163 * @param enclen number of bytes in @a enc (without 0-terminator)
1164 * @param pub where to store the public key
1165 * @return #GNUNET_OK on success
1168 GNUNET_CRYPTO_eddsa_public_key_from_string (const char *enc,
1170 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1175 * Create a new private key by reading it from a file. If the
1176 * files does not exist, create a new key and write it to the
1177 * file. Caller must free return value. Note that this function
1178 * can not guarantee that another process might not be trying
1179 * the same operation on the same file at the same time.
1180 * If the contents of the file
1181 * are invalid the old file is deleted and a fresh key is
1184 * @param filename name of file to use to store the key
1185 * @return new private key, NULL on error (for example,
1186 * permission denied); free using #GNUNET_free
1188 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1189 GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename);
1194 * Create a new private key by reading it from a file. If the
1195 * files does not exist, create a new key and write it to the
1196 * file. Caller must free return value. Note that this function
1197 * can not guarantee that another process might not be trying
1198 * the same operation on the same file at the same time.
1199 * If the contents of the file
1200 * are invalid the old file is deleted and a fresh key is
1203 * @param filename name of file to use to store the key
1204 * @return new private key, NULL on error (for example,
1205 * permission denied); free using #GNUNET_free
1207 struct GNUNET_CRYPTO_EddsaPrivateKey *
1208 GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename);
1212 * Forward declaration to simplify #include-structure.
1214 struct GNUNET_CONFIGURATION_Handle;
1219 * Create a new private key by reading our peer's key from
1220 * the file specified in the configuration.
1222 * @param cfg the configuration to use
1223 * @return new private key, NULL on error (for example,
1224 * permission denied); free using #GNUNET_free
1226 struct GNUNET_CRYPTO_EddsaPrivateKey *
1227 GNUNET_CRYPTO_eddsa_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
1232 * Create a new private key. Caller must free return value.
1234 * @return fresh private key; free using #GNUNET_free
1236 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1237 GNUNET_CRYPTO_ecdsa_key_create (void);
1242 * Create a new private key. Caller must free return value.
1244 * @return fresh private key; free using #GNUNET_free
1246 struct GNUNET_CRYPTO_EddsaPrivateKey *
1247 GNUNET_CRYPTO_eddsa_key_create (void);
1252 * Create a new private key. Clear with #GNUNET_CRYPTO_ecdhe_key_clear().
1254 * @param[out] pk set to fresh private key;
1255 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1258 GNUNET_CRYPTO_ecdhe_key_create2 (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1263 * Create a new private key. Caller must free return value.
1265 * @return fresh private key; free using #GNUNET_free
1267 struct GNUNET_CRYPTO_EcdhePrivateKey *
1268 GNUNET_CRYPTO_ecdhe_key_create (void);
1273 * Clear memory that was used to store a private key.
1275 * @param pk location of the key
1278 GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk);
1283 * Clear memory that was used to store a private key.
1285 * @param pk location of the key
1288 GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk);
1293 * Clear memory that was used to store a private key.
1295 * @param pk location of the key
1298 GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1303 * Get the shared private key we use for anonymous users.
1305 * @return "anonymous" private key; do not free
1307 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
1308 GNUNET_CRYPTO_ecdsa_key_get_anonymous (void);
1313 * Setup a hostkey file for a peer given the name of the
1314 * configuration file (!). This function is used so that
1315 * at a later point code can be certain that reading a
1316 * hostkey is fast (for example in time-dependent testcases).
1318 * @param cfg_name name of the configuration file to use
1321 GNUNET_CRYPTO_eddsa_setup_hostkey (const char *cfg_name);
1326 * Retrieve the identity of the host's peer.
1328 * @param cfg configuration to use
1329 * @param dst pointer to where to write the peer identity
1330 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
1331 * could not be retrieved
1334 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
1335 struct GNUNET_PeerIdentity *dst);
1339 * Internal structure used to cache pre-calculated values for DLOG calculation.
1341 struct GNUNET_CRYPTO_EccDlogContext;
1345 * Point on a curve (always for Curve25519) encoded in a format suitable
1346 * for network transmission (ECDH), see http://cr.yp.to/ecdh.html.
1348 struct GNUNET_CRYPTO_EccPoint
1351 * Q consists of an x- and a y-value, each mod p (256 bits), given
1352 * here in affine coordinates and Ed25519 standard compact format.
1354 unsigned char q_y[256 / 8];
1359 * Do pre-calculation for ECC discrete logarithm for small factors.
1361 * @param max maximum value the factor can be
1362 * @param mem memory to use (should be smaller than @a max), must not be zero.
1363 * @return NULL on error
1365 struct GNUNET_CRYPTO_EccDlogContext *
1366 GNUNET_CRYPTO_ecc_dlog_prepare (unsigned int max,
1371 * Calculate ECC discrete logarithm for small factors.
1372 * Opposite of #GNUNET_CRYPTO_ecc_dexp().
1374 * @param dlc precalculated values, determine range of factors
1375 * @param input point on the curve to factor
1376 * @return INT_MAX if dlog failed, otherwise the factor
1379 GNUNET_CRYPTO_ecc_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc,
1380 gcry_mpi_point_t input);
1384 * Multiply the generator g of the elliptic curve by @a val
1385 * to obtain the point on the curve representing @a val.
1386 * Afterwards, point addition will correspond to integer
1387 * addition. #GNUNET_CRYPTO_ecc_dlog() can be used to
1388 * convert a point back to an integer (as long as the
1389 * integer is smaller than the MAX of the @a edc context).
1391 * @param edc calculation context for ECC operations
1392 * @param val value to encode into a point
1393 * @return representation of the value as an ECC point,
1394 * must be freed using #GNUNET_CRYPTO_ecc_free()
1397 GNUNET_CRYPTO_ecc_dexp (struct GNUNET_CRYPTO_EccDlogContext *edc,
1402 * Multiply the generator g of the elliptic curve by @a val
1403 * to obtain the point on the curve representing @a val.
1405 * @param edc calculation context for ECC operations
1406 * @param val (positive) value to encode into a point
1407 * @return representation of the value as an ECC point,
1408 * must be freed using #GNUNET_CRYPTO_ecc_free()
1411 GNUNET_CRYPTO_ecc_dexp_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1416 * Multiply the point @a p on the elliptic curve by @a val.
1418 * @param edc calculation context for ECC operations
1419 * @param p point to multiply
1420 * @param val (positive) value to encode into a point
1421 * @return representation of the value as an ECC point,
1422 * must be freed using #GNUNET_CRYPTO_ecc_free()
1425 GNUNET_CRYPTO_ecc_pmul_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1431 * Convert point value to binary representation.
1433 * @param edc calculation context for ECC operations
1434 * @param point computational point representation
1435 * @param[out] bin binary point representation
1438 GNUNET_CRYPTO_ecc_point_to_bin (struct GNUNET_CRYPTO_EccDlogContext *edc,
1439 gcry_mpi_point_t point,
1440 struct GNUNET_CRYPTO_EccPoint *bin);
1444 * Convert binary representation of a point to computational representation.
1446 * @param edc calculation context for ECC operations
1447 * @param bin binary point representation
1448 * @return computational representation
1451 GNUNET_CRYPTO_ecc_bin_to_point (struct GNUNET_CRYPTO_EccDlogContext *edc,
1452 const struct GNUNET_CRYPTO_EccPoint *bin);
1456 * Add two points on the elliptic curve.
1458 * @param edc calculation context for ECC operations
1459 * @param a some value
1460 * @param b some value
1461 * @return @a a + @a b, must be freed using #GNUNET_CRYPTO_ecc_free()
1464 GNUNET_CRYPTO_ecc_add (struct GNUNET_CRYPTO_EccDlogContext *edc,
1466 gcry_mpi_point_t b);
1470 * Obtain a random point on the curve and its
1471 * additive inverse. Both returned values
1472 * must be freed using #GNUNET_CRYPTO_ecc_free().
1474 * @param edc calculation context for ECC operations
1475 * @param[out] r set to a random point on the curve
1476 * @param[out] r_inv set to the additive inverse of @a r
1479 GNUNET_CRYPTO_ecc_rnd (struct GNUNET_CRYPTO_EccDlogContext *edc,
1480 gcry_mpi_point_t *r,
1481 gcry_mpi_point_t *r_inv);
1485 * Obtain a random scalar for point multiplication on the curve and
1486 * its multiplicative inverse.
1488 * @param edc calculation context for ECC operations
1489 * @param[out] r set to a random scalar on the curve
1490 * @param[out] r_inv set to the multiplicative inverse of @a r
1493 GNUNET_CRYPTO_ecc_rnd_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1499 * Generate a random value mod n.
1501 * @param edc ECC context
1502 * @return random value mod n.
1505 GNUNET_CRYPTO_ecc_random_mod_n (struct GNUNET_CRYPTO_EccDlogContext *edc);
1509 * Free a point value returned by the API.
1511 * @param p point to free
1514 GNUNET_CRYPTO_ecc_free (gcry_mpi_point_t p);
1518 * Release precalculated values.
1520 * @param dlc dlog context
1523 GNUNET_CRYPTO_ecc_dlog_release (struct GNUNET_CRYPTO_EccDlogContext *dlc);
1528 * Derive key material from a public and a private ECC key.
1530 * @param priv private key to use for the ECDH (x)
1531 * @param pub public key to use for the ECDH (yG)
1532 * @param key_material where to write the key material (xyG)
1533 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1536 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1537 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1538 struct GNUNET_HashCode *key_material);
1543 * Derive key material from a ECDH public key and a private EdDSA key.
1544 * Dual to #GNUNET_CRRYPTO_ecdh_eddsa.
1546 * @param priv private key from EdDSA to use for the ECDH (x)
1547 * @param pub public key to use for the ECDH (yG)
1548 * @param key_material where to write the key material H(h(x)yG)
1549 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1552 GNUNET_CRYPTO_eddsa_ecdh (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1553 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1554 struct GNUNET_HashCode *key_material);
1558 * Derive key material from a ECDH public key and a private ECDSA key.
1559 * Dual to #GNUNET_CRRYPTO_ecdh_ecdsa.
1561 * @param priv private key from ECDSA to use for the ECDH (x)
1562 * @param pub public key to use for the ECDH (yG)
1563 * @param key_material where to write the key material H(h(x)yG)
1564 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1567 GNUNET_CRYPTO_ecdsa_ecdh (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1568 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1569 struct GNUNET_HashCode *key_material);
1574 * Derive key material from a EdDSA public key and a private ECDH key.
1575 * Dual to #GNUNET_CRRYPTO_eddsa_ecdh.
1577 * @param priv private key to use for the ECDH (y)
1578 * @param pub public key from EdDSA to use for the ECDH (X=h(x)G)
1579 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1580 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1583 GNUNET_CRYPTO_ecdh_eddsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1584 const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
1585 struct GNUNET_HashCode *key_material);
1589 * Derive key material from a EcDSA public key and a private ECDH key.
1590 * Dual to #GNUNET_CRRYPTO_ecdsa_ecdh.
1592 * @param priv private key to use for the ECDH (y)
1593 * @param pub public key from ECDSA to use for the ECDH (X=h(x)G)
1594 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1595 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1598 GNUNET_CRYPTO_ecdh_ecdsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1599 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1600 struct GNUNET_HashCode *key_material);
1605 * EdDSA sign a given block.
1607 * @param priv private key to use for the signing
1608 * @param purpose what to sign (size, purpose)
1609 * @param sig where to write the signature
1610 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1613 GNUNET_CRYPTO_eddsa_sign (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1614 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1615 struct GNUNET_CRYPTO_EddsaSignature *sig);
1620 * ECDSA Sign a given block.
1622 * @param priv private key to use for the signing
1623 * @param purpose what to sign (size, purpose)
1624 * @param sig where to write the signature
1625 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1628 GNUNET_CRYPTO_ecdsa_sign (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1629 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1630 struct GNUNET_CRYPTO_EcdsaSignature *sig);
1634 * Verify EdDSA signature.
1636 * @param purpose what is the purpose that the signature should have?
1637 * @param validate block to validate (size, purpose, data)
1638 * @param sig signature that is being validated
1639 * @param pub public key of the signer
1640 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1643 GNUNET_CRYPTO_eddsa_verify (uint32_t purpose,
1644 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1645 const struct GNUNET_CRYPTO_EddsaSignature *sig,
1646 const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1652 * Verify ECDSA signature.
1654 * @param purpose what is the purpose that the signature should have?
1655 * @param validate block to validate (size, purpose, data)
1656 * @param sig signature that is being validated
1657 * @param pub public key of the signer
1658 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1661 GNUNET_CRYPTO_ecdsa_verify (uint32_t purpose,
1662 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1663 const struct GNUNET_CRYPTO_EcdsaSignature *sig,
1664 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1669 * Derive a private key from a given private key and a label.
1670 * Essentially calculates a private key 'h = H(l,P) * d mod n'
1671 * where n is the size of the ECC group and P is the public
1672 * key associated with the private key 'd'.
1674 * @param priv original private key
1675 * @param label label to use for key deriviation
1676 * @param context additional context to use for HKDF of 'h';
1677 * typically the name of the subsystem/application
1678 * @return derived private key
1680 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1681 GNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1683 const char *context);
1688 * Derive a public key from a given public key and a label.
1689 * Essentially calculates a public key 'V = H(l,P) * P'.
1691 * @param pub original public key
1692 * @param label label to use for key deriviation
1693 * @param context additional context to use for HKDF of 'h'.
1694 * typically the name of the subsystem/application
1695 * @param result where to write the derived public key
1698 GNUNET_CRYPTO_ecdsa_public_key_derive (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1700 const char *context,
1701 struct GNUNET_CRYPTO_EcdsaPublicKey *result);
1705 * Output the given MPI value to the given buffer in network
1706 * byte order. The MPI @a val may not be negative.
1708 * @param buf where to output to
1709 * @param size number of bytes in @a buf
1710 * @param val value to write to @a buf
1713 GNUNET_CRYPTO_mpi_print_unsigned (void *buf,
1719 * Convert data buffer into MPI value.
1720 * The buffer is interpreted as network
1721 * byte order, unsigned integer.
1723 * @param result where to store MPI value (allocated)
1724 * @param data raw data (GCRYMPI_FMT_USG)
1725 * @param size number of bytes in @a data
1728 GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result,
1734 * Create a freshly generated paillier public key.
1736 * @param[out] public_key Where to store the public key?
1737 * @param[out] private_key Where to store the private key?
1740 GNUNET_CRYPTO_paillier_create (struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1741 struct GNUNET_CRYPTO_PaillierPrivateKey *private_key);
1745 * Encrypt a plaintext with a paillier public key.
1747 * @param public_key Public key to use.
1748 * @param m Plaintext to encrypt.
1749 * @param desired_ops How many homomorphic ops the caller intends to use
1750 * @param[out] ciphertext Encrytion of @a plaintext with @a public_key.
1751 * @return guaranteed number of supported homomorphic operations >= 1,
1752 * or desired_ops, in case that is lower,
1753 * or -1 if less than one homomorphic operation is possible
1756 GNUNET_CRYPTO_paillier_encrypt (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1759 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext);
1763 * Decrypt a paillier ciphertext with a private key.
1765 * @param private_key Private key to use for decryption.
1766 * @param public_key Public key to use for decryption.
1767 * @param ciphertext Ciphertext to decrypt.
1768 * @param[out] m Decryption of @a ciphertext with @private_key.
1771 GNUNET_CRYPTO_paillier_decrypt (const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key,
1772 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1773 const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
1778 * Compute a ciphertext that represents the sum of the plaintext in @a x1 and @a x2
1780 * Note that this operation can only be done a finite number of times
1781 * before an overflow occurs.
1783 * @param public_key Public key to use for encryption.
1784 * @param c1 Paillier cipher text.
1785 * @param c2 Paillier cipher text.
1786 * @param[out] result Result of the homomorphic operation.
1787 * @return #GNUNET_OK if the result could be computed,
1788 * #GNUNET_SYSERR if no more homomorphic operations are remaining.
1791 GNUNET_CRYPTO_paillier_hom_add (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1792 const struct GNUNET_CRYPTO_PaillierCiphertext *c1,
1793 const struct GNUNET_CRYPTO_PaillierCiphertext *c2,
1794 struct GNUNET_CRYPTO_PaillierCiphertext *result);
1798 * Get the number of remaining supported homomorphic operations.
1800 * @param c Paillier cipher text.
1801 * @return the number of remaining homomorphic operations
1804 GNUNET_CRYPTO_paillier_hom_get_remaining (const struct GNUNET_CRYPTO_PaillierCiphertext *c);
1807 /* ********* Chaum-style RSA-based blind signatures ******************* */
1813 * The private information of an RSA key pair.
1815 struct GNUNET_CRYPTO_RsaPrivateKey;
1818 * The public information of an RSA key pair.
1820 struct GNUNET_CRYPTO_RsaPublicKey;
1823 * Constant-size pre-secret for blinding key generation.
1825 struct GNUNET_CRYPTO_RsaBlindingKeySecret
1828 * Bits used to generate the blinding key. 256 bits
1829 * of entropy is enough.
1831 uint32_t pre_secret[8] GNUNET_PACKED;
1835 * @brief an RSA signature
1837 struct GNUNET_CRYPTO_RsaSignature;
1841 * Create a new private key. Caller must free return value.
1843 * @param len length of the key in bits (i.e. 2048)
1844 * @return fresh private key
1846 struct GNUNET_CRYPTO_RsaPrivateKey *
1847 GNUNET_CRYPTO_rsa_private_key_create (unsigned int len);
1851 * Free memory occupied by the private key.
1853 * @param key pointer to the memory to free
1856 GNUNET_CRYPTO_rsa_private_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *key);
1860 * Encode the private key in a format suitable for
1861 * storing it into a file.
1863 * @param key the private key
1864 * @param[out] buffer set to a buffer with the encoded key
1865 * @return size of memory allocatedin @a buffer
1868 GNUNET_CRYPTO_rsa_private_key_encode (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
1873 * Decode the private key from the data-format back
1874 * to the "normal", internal format.
1876 * @param buf the buffer where the private key data is stored
1877 * @param len the length of the data in @a buf
1878 * @return NULL on error
1880 struct GNUNET_CRYPTO_RsaPrivateKey *
1881 GNUNET_CRYPTO_rsa_private_key_decode (const char *buf,
1886 * Duplicate the given private key
1888 * @param key the private key to duplicate
1889 * @return the duplicate key; NULL upon error
1891 struct GNUNET_CRYPTO_RsaPrivateKey *
1892 GNUNET_CRYPTO_rsa_private_key_dup (const struct GNUNET_CRYPTO_RsaPrivateKey *key);
1896 * Extract the public key of the given private key.
1898 * @param priv the private key
1899 * @retur NULL on error, otherwise the public key
1901 struct GNUNET_CRYPTO_RsaPublicKey *
1902 GNUNET_CRYPTO_rsa_private_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey *priv);
1906 * Compute hash over the public key.
1908 * @param key public key to hash
1909 * @param hc where to store the hash code
1912 GNUNET_CRYPTO_rsa_public_key_hash (const struct GNUNET_CRYPTO_RsaPublicKey *key,
1913 struct GNUNET_HashCode *hc);
1917 * Obtain the length of the RSA key in bits.
1919 * @param key the public key to introspect
1920 * @return length of the key in bits
1923 GNUNET_CRYPTO_rsa_public_key_len (const struct GNUNET_CRYPTO_RsaPublicKey *key);
1927 * Free memory occupied by the public key.
1929 * @param key pointer to the memory to free
1932 GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_RsaPublicKey *key);
1936 * Encode the public key in a format suitable for
1937 * storing it into a file.
1939 * @param key the private key
1940 * @param[out] buffer set to a buffer with the encoded key
1941 * @return size of memory allocated in @a buffer
1944 GNUNET_CRYPTO_rsa_public_key_encode (const struct GNUNET_CRYPTO_RsaPublicKey *key,
1949 * Decode the public key from the data-format back
1950 * to the "normal", internal format.
1952 * @param buf the buffer where the public key data is stored
1953 * @param len the length of the data in @a buf
1954 * @return NULL on error
1956 struct GNUNET_CRYPTO_RsaPublicKey *
1957 GNUNET_CRYPTO_rsa_public_key_decode (const char *buf,
1962 * Duplicate the given public key
1964 * @param key the public key to duplicate
1965 * @return the duplicate key; NULL upon error
1967 struct GNUNET_CRYPTO_RsaPublicKey *
1968 GNUNET_CRYPTO_rsa_public_key_dup (const struct GNUNET_CRYPTO_RsaPublicKey *key);
1972 * Compare the values of two signatures.
1974 * @param s1 one signature
1975 * @param s2 the other signature
1976 * @return 0 if the two are equal
1979 GNUNET_CRYPTO_rsa_signature_cmp (struct GNUNET_CRYPTO_RsaSignature *s1,
1980 struct GNUNET_CRYPTO_RsaSignature *s2);
1983 * Compare the values of two private keys.
1985 * @param p1 one private key
1986 * @param p2 the other private key
1987 * @return 0 if the two are equal
1990 GNUNET_CRYPTO_rsa_private_key_cmp (struct GNUNET_CRYPTO_RsaPrivateKey *p1,
1991 struct GNUNET_CRYPTO_RsaPrivateKey *p2);
1995 * Compare the values of two public keys.
1997 * @param p1 one public key
1998 * @param p2 the other public key
1999 * @return 0 if the two are equal
2002 GNUNET_CRYPTO_rsa_public_key_cmp (struct GNUNET_CRYPTO_RsaPublicKey *p1,
2003 struct GNUNET_CRYPTO_RsaPublicKey *p2);
2007 * Blinds the given message with the given blinding key
2009 * @param hash hash of the message to sign
2010 * @param bkey the blinding key
2011 * @param pkey the public key of the signer
2012 * @param[out] buf set to a buffer with the blinded message to be signed
2013 * @param[out] buf_size number of bytes stored in @a buf
2014 * @return #GNUNET_YES if successful, #GNUNET_NO if RSA key is malicious
2017 GNUNET_CRYPTO_rsa_blind (const struct GNUNET_HashCode *hash,
2018 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
2019 struct GNUNET_CRYPTO_RsaPublicKey *pkey,
2025 * Sign a blinded value, which must be a full domain hash of a message.
2027 * @param key private key to use for the signing
2028 * @param msg the (blinded) message to sign
2029 * @param msg_len number of bytes in @a msg to sign
2030 * @return NULL on error, signature on success
2032 struct GNUNET_CRYPTO_RsaSignature *
2033 GNUNET_CRYPTO_rsa_sign_blinded (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2039 * Create and sign a full domain hash of a message.
2041 * @param key private key to use for the signing
2042 * @param hash the hash of the message to sign
2043 * @return NULL on error, including a malicious RSA key, signature on success
2045 struct GNUNET_CRYPTO_RsaSignature *
2046 GNUNET_CRYPTO_rsa_sign_fdh (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2047 const struct GNUNET_HashCode *hash);
2051 * Free memory occupied by signature.
2053 * @param sig memory to free
2056 GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_RsaSignature *sig);
2060 * Encode the given signature in a format suitable for storing it into a file.
2062 * @param sig the signature
2063 * @param[out] buffer set to a buffer with the encoded key
2064 * @return size of memory allocated in @a buffer
2067 GNUNET_CRYPTO_rsa_signature_encode (const struct GNUNET_CRYPTO_RsaSignature *sig,
2072 * Decode the signature from the data-format back to the "normal", internal
2075 * @param buf the buffer where the public key data is stored
2076 * @param len the length of the data in @a buf
2077 * @return NULL on error
2079 struct GNUNET_CRYPTO_RsaSignature *
2080 GNUNET_CRYPTO_rsa_signature_decode (const char *buf,
2085 * Duplicate the given rsa signature
2087 * @param sig the signature to duplicate
2088 * @return the duplicate key; NULL upon error
2090 struct GNUNET_CRYPTO_RsaSignature *
2091 GNUNET_CRYPTO_rsa_signature_dup (const struct GNUNET_CRYPTO_RsaSignature *sig);
2095 * Unblind a blind-signed signature. The signature should have been generated
2096 * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with
2097 * #GNUNET_CRYPTO_rsa_blind().
2099 * @param sig the signature made on the blinded signature purpose
2100 * @param bks the blinding key secret used to blind the signature purpose
2101 * @param pkey the public key of the signer
2102 * @return unblinded signature on success, NULL if RSA key is bad or malicious.
2104 struct GNUNET_CRYPTO_RsaSignature *
2105 GNUNET_CRYPTO_rsa_unblind (const struct GNUNET_CRYPTO_RsaSignature *sig,
2106 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
2107 struct GNUNET_CRYPTO_RsaPublicKey *pkey);
2111 * Verify whether the given hash corresponds to the given signature and the
2112 * signature is valid with respect to the given public key.
2114 * @param hash the message to verify to match the @a sig
2115 * @param sig signature that is being validated
2116 * @param public_key public key of the signer
2117 * @returns #GNUNET_YES if ok, #GNUNET_NO if RSA key is malicious, #GNUNET_SYSERR if signature
2120 GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash,
2121 const struct GNUNET_CRYPTO_RsaSignature *sig,
2122 const struct GNUNET_CRYPTO_RsaPublicKey *public_key);
2125 #if 0 /* keep Emacsens' auto-indent happy */
2133 /* ifndef GNUNET_CRYPTO_LIB_H */
2135 /* end of gnunet_crypto_lib.h */