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/>.
20 * @file include/gnunet_crypto_lib.h
21 * @brief cryptographic primitives for GNUnet
23 * @author Christian Grothoff
24 * @author Krista Bennett
25 * @author Gerd Knorr <kraxel@bytesex.org>
26 * @author Ioana Patrascu
27 * @author Tzvetan Horozov
28 * @author Jeffrey Burdges <burdges@gnunet.org>
30 * @defgroup crypto Crypto library: cryptographic operations
31 * Provides cryptographic primitives.
33 * @see [Documentation](https://gnunet.org/crypto-api)
35 * @defgroup hash Crypto library: hash operations
36 * Provides hashing and operations on hashes.
38 * @see [Documentation](https://gnunet.org/crypto-api)
41 #ifndef GNUNET_CRYPTO_LIB_H
42 #define GNUNET_CRYPTO_LIB_H
47 #if 0 /* keep Emacsens' auto-indent happy */
53 * @brief A 512-bit hashcode. These are the default length for GNUnet, using SHA-512.
55 struct GNUNET_HashCode
57 uint32_t bits[512 / 8 / sizeof (uint32_t)]; /* = 16 */
63 * @brief A 256-bit hashcode. Used under special conditions, like when space
64 * is critical and security is not impacted by it.
66 struct GNUNET_ShortHashCode
68 uint32_t bits[256 / 8 / sizeof (uint32_t)]; /* = 8 */
73 * The identity of the host (wraps the signing key of the peer).
75 struct GNUNET_PeerIdentity;
77 #include "gnunet_common.h"
82 * Maximum length of an ECC signature.
83 * Note: round up to multiple of 8 minus 2 for alignment.
85 #define GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH 126
89 * Desired quality level for random numbers.
92 enum GNUNET_CRYPTO_Quality
95 * No good quality of the operation is needed (i.e.,
96 * random numbers can be pseudo-random).
99 GNUNET_CRYPTO_QUALITY_WEAK,
102 * High-quality operations are desired.
105 GNUNET_CRYPTO_QUALITY_STRONG,
108 * Randomness for IVs etc. is required.
111 GNUNET_CRYPTO_QUALITY_NONCE
116 * @brief length of the sessionkey in bytes (256 BIT sessionkey)
118 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
121 * Length of a hash value
123 #define GNUNET_CRYPTO_HASH_LENGTH (512/8)
126 * How many characters (without 0-terminator) are our ASCII-encoded
127 * public keys (ECDSA/EDDSA/ECDHE).
129 #define GNUNET_CRYPTO_PKEY_ASCII_LENGTH 52
132 * @brief 0-terminated ASCII encoding of a struct GNUNET_HashCode.
134 struct GNUNET_CRYPTO_HashAsciiEncoded
136 unsigned char encoding[104];
140 GNUNET_NETWORK_STRUCT_BEGIN
144 * @brief header of what an ECC signature signs
145 * this must be followed by "size - 8" bytes of
146 * the actual signed data
148 struct GNUNET_CRYPTO_EccSignaturePurpose
151 * How many bytes does this signature sign?
152 * (including this purpose header); in network
155 uint32_t size GNUNET_PACKED;
158 * What does this signature vouch for? This
159 * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
160 * constant (from gnunet_signatures.h). In
161 * network byte order!
163 uint32_t purpose GNUNET_PACKED;
169 * @brief an ECC signature using EdDSA.
170 * See https://gnunet.org/ed25519
172 struct GNUNET_CRYPTO_EddsaSignature
178 unsigned char r[256 / 8];
183 unsigned char s[256 / 8];
190 * @brief an ECC signature using ECDSA
192 struct GNUNET_CRYPTO_EcdsaSignature
198 unsigned char r[256 / 8];
203 unsigned char s[256 / 8];
209 * Public ECC key (always for curve Ed25519) encoded in a format
210 * suitable for network transmission and EdDSA signatures.
212 struct GNUNET_CRYPTO_EddsaPublicKey
215 * Point Q consists of a y-value mod p (256 bits); the x-value is
216 * always positive. The point is stored in Ed25519 standard
219 unsigned char q_y[256 / 8];
225 * Public ECC key (always for Curve25519) encoded in a format suitable
226 * for network transmission and ECDSA signatures.
228 struct GNUNET_CRYPTO_EcdsaPublicKey
231 * Q consists of an x- and a y-value, each mod p (256 bits), given
232 * here in affine coordinates and Ed25519 standard compact format.
234 unsigned char q_y[256 / 8];
240 * The identity of the host (wraps the signing key of the peer).
242 struct GNUNET_PeerIdentity
244 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
249 * Public ECC key (always for Curve25519) encoded in a format suitable
250 * for network transmission and encryption (ECDH),
251 * See http://cr.yp.to/ecdh.html
253 struct GNUNET_CRYPTO_EcdhePublicKey
256 * Q consists of an x- and a y-value, each mod p (256 bits), given
257 * here in affine coordinates and Ed25519 standard compact format.
259 unsigned char q_y[256 / 8];
264 * Private ECC key encoded for transmission. To be used only for ECDH
265 * key exchange (ECDHE to be precise).
267 struct GNUNET_CRYPTO_EcdhePrivateKey
270 * d is a value mod n, where n has at most 256 bits.
272 unsigned char d[256 / 8];
277 * Private ECC key encoded for transmission. To be used only for ECDSA
280 struct GNUNET_CRYPTO_EcdsaPrivateKey
283 * d is a value mod n, where n has at most 256 bits.
285 unsigned char d[256 / 8];
290 * Private ECC key encoded for transmission. To be used only for EdDSA
293 struct GNUNET_CRYPTO_EddsaPrivateKey
296 * d is a value mod n, where n has at most 256 bits.
298 unsigned char d[256 / 8];
304 * @brief type for session keys
306 struct GNUNET_CRYPTO_SymmetricSessionKey
309 * Actual key for AES.
311 unsigned char aes_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
314 * Actual key for TwoFish.
316 unsigned char twofish_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
320 GNUNET_NETWORK_STRUCT_END
323 * @brief IV for sym cipher
325 * NOTE: must be smaller (!) in size than the
326 * `struct GNUNET_HashCode`.
328 struct GNUNET_CRYPTO_SymmetricInitializationVector
330 unsigned char aes_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
332 unsigned char twofish_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
337 * @brief type for (message) authentication keys
339 struct GNUNET_CRYPTO_AuthKey
341 unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
346 * Size of paillier plain texts and public keys.
347 * Private keys and ciphertexts are twice this size.
349 #define GNUNET_CRYPTO_PAILLIER_BITS 2048
353 * Paillier public key.
355 struct GNUNET_CRYPTO_PaillierPublicKey
360 unsigned char n[GNUNET_CRYPTO_PAILLIER_BITS / 8];
365 * Paillier private key.
367 struct GNUNET_CRYPTO_PaillierPrivateKey
370 * Lambda-component of the private key.
372 unsigned char lambda[GNUNET_CRYPTO_PAILLIER_BITS / 8];
374 * Mu-component of the private key.
376 unsigned char mu[GNUNET_CRYPTO_PAILLIER_BITS / 8];
381 * Paillier ciphertext.
383 struct GNUNET_CRYPTO_PaillierCiphertext
386 * Guaranteed minimum number of homomorphic operations with this ciphertext,
387 * in network byte order (NBO).
389 int32_t remaining_ops GNUNET_PACKED;
392 * The bits of the ciphertext.
394 unsigned char bits[GNUNET_CRYPTO_PAILLIER_BITS * 2 / 8];
398 /* **************** Functions and Macros ************* */
402 * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator
405 * @param seed the seed to use
408 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
413 * Calculate the checksum of a buffer in one step.
415 * @param buf buffer to calculate CRC over
416 * @param len number of bytes in @a buf
420 GNUNET_CRYPTO_crc8_n (const void *buf,
425 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
427 * @param sum current sum, initially 0
428 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
429 * @param len number of bytes in @a buf, must be multiple of 2
430 * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish to get actual crc16)
433 GNUNET_CRYPTO_crc16_step (uint32_t sum,
439 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
441 * @param sum cummulative sum
442 * @return crc16 value
445 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
450 * Calculate the checksum of a buffer in one step.
452 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
453 * @param len number of bytes in @a buf, must be multiple of 2
454 * @return crc16 value
457 GNUNET_CRYPTO_crc16_n (const void *buf,
465 * Compute the CRC32 checksum for the first len
466 * bytes of the buffer.
468 * @param buf the data over which we're taking the CRC
469 * @param len the length of the buffer @a buf in bytes
470 * @return the resulting CRC32 checksum
473 GNUNET_CRYPTO_crc32_n (const void *buf,
479 * Fill block with a random values.
481 * @param mode desired quality of the random number
482 * @param buffer the buffer to fill
483 * @param length buffer length
486 GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode,
492 * Produce a random value.
494 * @param mode desired quality of the random number
495 * @param i the upper limit (exclusive) for the random number
496 * @return a random value in the interval [0,@a i) (exclusive).
499 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode,
505 * Random on unsigned 64-bit values.
507 * @param mode desired quality of the random number
508 * @param max value returned will be in range [0,@a max) (exclusive)
509 * @return random 64-bit number
512 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
518 * Get an array with a random permutation of the
520 * @param mode #GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used,
521 * #GNUNET_CRYPTO_QUALITY_WEAK or #GNUNET_CRYPTO_QUALITY_NONCE otherwise
522 * @param n the size of the array
523 * @return the permutation array (allocated from heap)
526 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode,
532 * Create a new random session key.
534 * @param key key to initialize
537 GNUNET_CRYPTO_symmetric_create_session_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key);
542 * Encrypt a block using a symmetric sessionkey.
544 * @param block the block to encrypt
545 * @param size the size of the @a block
546 * @param sessionkey the key used to encrypt
547 * @param iv the initialization vector to use, use INITVALUE
549 * @return the size of the encrypted block, -1 for errors
552 GNUNET_CRYPTO_symmetric_encrypt (const void *block,
554 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
555 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
561 * Decrypt a given block using a symmetric sessionkey.
563 * @param block the data to decrypt, encoded as returned by encrypt
564 * @param size how big is the block?
565 * @param sessionkey the key used to decrypt
566 * @param iv the initialization vector to use
567 * @param result address to store the result at
568 * @return -1 on failure, size of decrypted block on success
571 GNUNET_CRYPTO_symmetric_decrypt (const void *block,
573 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
574 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
580 * @brief Derive an IV
581 * @param iv initialization vector
582 * @param skey session key
583 * @param salt salt for the derivation
584 * @param salt_len size of the @a salt
585 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
588 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
589 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
591 size_t salt_len, ...);
595 * @brief Derive an IV
596 * @param iv initialization vector
597 * @param skey session key
598 * @param salt salt for the derivation
599 * @param salt_len size of the @a salt
600 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
603 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
604 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
612 * Convert hash to ASCII encoding.
613 * @param block the hash code
614 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
615 * safely cast to char*, a '\\0' termination is set).
618 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block,
619 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
624 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
626 * @param enc the encoding
627 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
628 * @param result where to store the hash code
629 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
632 GNUNET_CRYPTO_hash_from_string2 (const char *enc,
634 struct GNUNET_HashCode *result);
639 * Convert ASCII encoding back to `struct GNUNET_HashCode`
641 * @param enc the encoding
642 * @param result where to store the hash code
643 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
645 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
646 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
652 * Compute the distance between 2 hashcodes. The
653 * computation must be fast, not involve @a a[0] or @a a[4] (they're used
654 * elsewhere), and be somewhat consistent. And of course, the result
655 * should be a positive number.
657 * @param a some hash code
658 * @param b some hash code
659 * @return number between 0 and UINT32_MAX
662 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a,
663 const struct GNUNET_HashCode *b);
668 * Compute hash of a given block.
670 * @param block the data to hash
671 * @param size size of the @a block
672 * @param ret pointer to where to write the hashcode
675 GNUNET_CRYPTO_hash (const void *block,
677 struct GNUNET_HashCode *ret);
681 * Context for cummulative hashing.
683 struct GNUNET_HashContext;
687 * Start incremental hashing operation.
689 * @return context for incremental hash computation
691 struct GNUNET_HashContext *
692 GNUNET_CRYPTO_hash_context_start (void);
696 * Add data to be hashed.
698 * @param hc cummulative hash context
699 * @param buf data to add
700 * @param size number of bytes in @a buf
703 GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
709 * Finish the hash computation.
711 * @param hc hash context to use, is freed in the process
712 * @param r_hash where to write the latest / final hash code
715 GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
716 struct GNUNET_HashCode *r_hash);
720 * Abort hashing, do not bother calculating final result.
722 * @param hc hash context to destroy
725 GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc);
729 * Calculate HMAC of a message (RFC 2104)
730 * TODO: Shouldn' this be the standard hmac function and
731 * the above be renamed?
733 * @param key secret key
734 * @param key_len secret key length
735 * @param plaintext input plaintext
736 * @param plaintext_len length of @a plaintext
737 * @param hmac where to store the hmac
740 GNUNET_CRYPTO_hmac_raw (const void *key, size_t key_len,
741 const void *plaintext, size_t plaintext_len,
742 struct GNUNET_HashCode *hmac);
747 * Calculate HMAC of a message (RFC 2104)
749 * @param key secret key
750 * @param plaintext input plaintext
751 * @param plaintext_len length of @a plaintext
752 * @param hmac where to store the hmac
755 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
756 const void *plaintext,
757 size_t plaintext_len,
758 struct GNUNET_HashCode *hmac);
762 * Function called once the hash computation over the
763 * specified file has completed.
766 * @param res resulting hash, NULL on error
769 (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
770 const struct GNUNET_HashCode *res);
774 * Handle to file hashing operation.
776 struct GNUNET_CRYPTO_FileHashContext;
781 * Compute the hash of an entire file.
783 * @param priority scheduling priority to use
784 * @param filename name of file to hash
785 * @param blocksize number of bytes to process in one task
786 * @param callback function to call upon completion
787 * @param callback_cls closure for @a callback
788 * @return NULL on (immediate) errror
790 struct GNUNET_CRYPTO_FileHashContext *
791 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
792 const char *filename,
794 GNUNET_CRYPTO_HashCompletedCallback callback,
799 * Cancel a file hashing operation.
801 * @param fhc operation to cancel (callback must not yet have been invoked)
804 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
809 * Create a random hash code.
811 * @param mode desired quality level
812 * @param result hash code that is randomized
815 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
816 struct GNUNET_HashCode *result);
821 * compute @a result = @a b - @a a
823 * @param a some hash code
824 * @param b some hash code
825 * @param result set to @a b - @a a
828 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
829 const struct GNUNET_HashCode *b,
830 struct GNUNET_HashCode *result);
835 * compute @a result = @a a + @a delta
837 * @param a some hash code
838 * @param delta some hash code
839 * @param result set to @a a + @a delta
842 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a,
843 const struct GNUNET_HashCode *delta,
844 struct GNUNET_HashCode *result);
849 * compute result = a ^ b
851 * @param a some hash code
852 * @param b some hash code
853 * @param result set to @a a ^ @a b
856 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
857 const struct GNUNET_HashCode *b,
858 struct GNUNET_HashCode *result);
863 * Convert a hashcode into a key.
865 * @param hc hash code that serves to generate the key
866 * @param skey set to a valid session key
867 * @param iv set to a valid initialization vector
870 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
871 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
872 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv);
877 * Obtain a bit from a hashcode.
879 * @param code the `struct GNUNET_HashCode` to index bit-wise
880 * @param bit index into the hashcode, [0...159]
881 * @return Bit \a bit from hashcode \a code, -1 for invalid index
884 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code,
890 * Determine how many low order bits match in two
891 * `struct GNUNET_HashCodes`. i.e. - 010011 and 011111 share
892 * the first two lowest order bits, and therefore the
893 * return value is two (NOT XOR distance, nor how many
894 * bits match absolutely!).
896 * @param first the first hashcode
897 * @param second the hashcode to compare first to
898 * @return the number of bits that match
901 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
902 const struct GNUNET_HashCode *second);
907 * Compare function for HashCodes, producing a total ordering
910 * @param h1 some hash code
911 * @param h2 some hash code
912 * @return 1 if @a h1 > @a h2, -1 if @a h1 < @a h2 and 0 if @a h1 == @a h2.
915 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
916 const struct GNUNET_HashCode *h2);
921 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
922 * in the XOR metric (Kademlia).
924 * @param h1 some hash code
925 * @param h2 some hash code
926 * @param target some hash code
927 * @return -1 if @a h1 is closer, 1 if @a h2 is closer and 0 if @a h1== @a h2.
930 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
931 const struct GNUNET_HashCode *h2,
932 const struct GNUNET_HashCode *target);
937 * @brief Derive an authentication key
938 * @param key authentication key
939 * @param rkey root key
941 * @param salt_len size of the salt
942 * @param argp pair of void * & size_t for context chunks, terminated by NULL
945 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
946 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
947 const void *salt, size_t salt_len,
953 * @brief Derive an authentication key
954 * @param key authentication key
955 * @param rkey root key
957 * @param salt_len size of the salt
958 * @param ... pair of void * & size_t for context chunks, terminated by NULL
961 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
962 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
963 const void *salt, size_t salt_len,
970 * @param result buffer for the derived key, allocated by caller
971 * @param out_len desired length of the derived key
972 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
973 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
975 * @param xts_len length of @a xts
976 * @param skm source key material
977 * @param skm_len length of @a skm
978 * @param ... pair of void * & size_t for context chunks, terminated by NULL
979 * @return #GNUNET_YES on success
982 GNUNET_CRYPTO_hkdf (void *result,
996 * @param result buffer for the derived key, allocated by caller
997 * @param out_len desired length of the derived key
998 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
999 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
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_hkdf_v (void *result,
1021 * @param result buffer for the derived key, allocated by caller
1022 * @param out_len desired length of the derived key
1024 * @param xts_len length of @a xts
1025 * @param skm source key material
1026 * @param skm_len length of @a skm
1027 * @param argp va_list of void * & size_t pairs for context chunks
1028 * @return #GNUNET_YES on success
1031 GNUNET_CRYPTO_kdf_v (void *result,
1041 * Deterministically generate a pseudo-random number uniformly from the
1042 * integers modulo a libgcrypt mpi.
1044 * @param[out] r MPI value set to the FDH
1045 * @param n MPI to work modulo
1047 * @param xts_len length of @a xts
1048 * @param skm source key material
1049 * @param skm_len length of @a skm
1050 * @param ctx context string
1053 GNUNET_CRYPTO_kdf_mod_mpi (gcry_mpi_t *r,
1055 const void *xts, size_t xts_len,
1056 const void *skm, size_t skm_len,
1063 * @param result buffer for the derived key, allocated by caller
1064 * @param out_len desired length of the derived key
1066 * @param xts_len length of @a xts
1067 * @param skm source key material
1068 * @param skm_len length of @a skm
1069 * @param ... void * & size_t pairs for context chunks
1070 * @return #GNUNET_YES on success
1073 GNUNET_CRYPTO_kdf (void *result,
1084 * Extract the public key for the given private key.
1086 * @param priv the private key
1087 * @param pub where to write the public key
1090 GNUNET_CRYPTO_ecdsa_key_get_public (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1091 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1095 * Extract the public key for the given private key.
1097 * @param priv the private key
1098 * @param pub where to write the public key
1101 GNUNET_CRYPTO_eddsa_key_get_public (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1102 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1108 * Extract the public key for the given private key.
1110 * @param priv the private key
1111 * @param pub where to write the public key
1114 GNUNET_CRYPTO_ecdhe_key_get_public (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1115 struct GNUNET_CRYPTO_EcdhePublicKey *pub);
1119 * Convert a public key to a string.
1121 * @param pub key to convert
1122 * @return string representing @a pub
1125 GNUNET_CRYPTO_ecdsa_public_key_to_string (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1129 * Convert a private key to a string.
1131 * @param priv key to convert
1132 * @return string representing @a pub
1135 GNUNET_CRYPTO_eddsa_private_key_to_string (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv);
1139 * Convert a public key to a string.
1141 * @param pub key to convert
1142 * @return string representing @a pub
1145 GNUNET_CRYPTO_eddsa_public_key_to_string (const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1149 * Convert a string representing a public key to a public key.
1151 * @param enc encoded public key
1152 * @param enclen number of bytes in @a enc (without 0-terminator)
1153 * @param pub where to store the public key
1154 * @return #GNUNET_OK on success
1157 GNUNET_CRYPTO_ecdsa_public_key_from_string (const char *enc,
1159 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1163 * Convert a string representing a private key to a private key.
1165 * @param enc encoded public key
1166 * @param enclen number of bytes in @a enc (without 0-terminator)
1167 * @param priv where to store the private key
1168 * @return #GNUNET_OK on success
1171 GNUNET_CRYPTO_eddsa_private_key_from_string (const char *enc,
1173 struct GNUNET_CRYPTO_EddsaPrivateKey *pub);
1177 * Convert a string representing a public key to a public key.
1179 * @param enc encoded public key
1180 * @param enclen number of bytes in @a enc (without 0-terminator)
1181 * @param pub where to store the public key
1182 * @return #GNUNET_OK on success
1185 GNUNET_CRYPTO_eddsa_public_key_from_string (const char *enc,
1187 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1192 * Create a new private key by reading it from a file. If the
1193 * files does not exist, create a new key and write it to the
1194 * file. Caller must free return value. Note that this function
1195 * can not guarantee that another process might not be trying
1196 * the same operation on the same file at the same time.
1197 * If the contents of the file
1198 * are invalid the old file is deleted and a fresh key is
1201 * @param filename name of file to use to store the key
1202 * @return new private key, NULL on error (for example,
1203 * permission denied); free using #GNUNET_free
1205 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1206 GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename);
1211 * Create a new private key by reading it from a file. If the
1212 * files does not exist, create a new key and write it to the
1213 * file. Caller must free return value. Note that this function
1214 * can not guarantee that another process might not be trying
1215 * the same operation on the same file at the same time.
1216 * If the contents of the file
1217 * are invalid the old file is deleted and a fresh key is
1220 * @param filename name of file to use to store the key
1221 * @return new private key, NULL on error (for example,
1222 * permission denied); free using #GNUNET_free
1224 struct GNUNET_CRYPTO_EddsaPrivateKey *
1225 GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename);
1229 * Forward declaration to simplify #include-structure.
1231 struct GNUNET_CONFIGURATION_Handle;
1236 * Create a new private key by reading our peer's key from
1237 * the file specified in the configuration.
1239 * @param cfg the configuration to use
1240 * @return new private key, NULL on error (for example,
1241 * permission denied); free using #GNUNET_free
1243 struct GNUNET_CRYPTO_EddsaPrivateKey *
1244 GNUNET_CRYPTO_eddsa_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
1249 * Create a new private key. Caller must free return value.
1251 * @return fresh private key; free using #GNUNET_free
1253 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1254 GNUNET_CRYPTO_ecdsa_key_create (void);
1259 * Create a new private key. Caller must free return value.
1261 * @return fresh private key; free using #GNUNET_free
1263 struct GNUNET_CRYPTO_EddsaPrivateKey *
1264 GNUNET_CRYPTO_eddsa_key_create (void);
1269 * Create a new private key. Clear with #GNUNET_CRYPTO_ecdhe_key_clear().
1271 * @param[out] pk set to fresh private key;
1272 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1275 GNUNET_CRYPTO_ecdhe_key_create2 (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1280 * Create a new private key. Caller must free return value.
1282 * @return fresh private key; free using #GNUNET_free
1284 struct GNUNET_CRYPTO_EcdhePrivateKey *
1285 GNUNET_CRYPTO_ecdhe_key_create (void);
1290 * Clear memory that was used to store a private key.
1292 * @param pk location of the key
1295 GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk);
1300 * Clear memory that was used to store a private key.
1302 * @param pk location of the key
1305 GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk);
1310 * Clear memory that was used to store a private key.
1312 * @param pk location of the key
1315 GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1320 * Get the shared private key we use for anonymous users.
1322 * @return "anonymous" private key; do not free
1324 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
1325 GNUNET_CRYPTO_ecdsa_key_get_anonymous (void);
1330 * Setup a hostkey file for a peer given the name of the
1331 * configuration file (!). This function is used so that
1332 * at a later point code can be certain that reading a
1333 * hostkey is fast (for example in time-dependent testcases).
1335 * @param cfg_name name of the configuration file to use
1338 GNUNET_CRYPTO_eddsa_setup_hostkey (const char *cfg_name);
1343 * Retrieve the identity of the host's peer.
1345 * @param cfg configuration to use
1346 * @param dst pointer to where to write the peer identity
1347 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
1348 * could not be retrieved
1351 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
1352 struct GNUNET_PeerIdentity *dst);
1356 * Compare two Peer Identities.
1358 * @param first first peer identity
1359 * @param second second peer identity
1360 * @return bigger than 0 if first > second,
1361 * 0 if they are the same
1362 * smaller than 0 if second > first
1365 GNUNET_CRYPTO_cmp_peer_identity (const struct GNUNET_PeerIdentity *first,
1366 const struct GNUNET_PeerIdentity *second);
1370 * Internal structure used to cache pre-calculated values for DLOG calculation.
1372 struct GNUNET_CRYPTO_EccDlogContext;
1376 * Point on a curve (always for Curve25519) encoded in a format suitable
1377 * for network transmission (ECDH), see http://cr.yp.to/ecdh.html.
1379 struct GNUNET_CRYPTO_EccPoint
1382 * Q consists of an x- and a y-value, each mod p (256 bits), given
1383 * here in affine coordinates and Ed25519 standard compact format.
1385 unsigned char q_y[256 / 8];
1390 * Do pre-calculation for ECC discrete logarithm for small factors.
1392 * @param max maximum value the factor can be
1393 * @param mem memory to use (should be smaller than @a max), must not be zero.
1394 * @return NULL on error
1396 struct GNUNET_CRYPTO_EccDlogContext *
1397 GNUNET_CRYPTO_ecc_dlog_prepare (unsigned int max,
1402 * Calculate ECC discrete logarithm for small factors.
1403 * Opposite of #GNUNET_CRYPTO_ecc_dexp().
1405 * @param dlc precalculated values, determine range of factors
1406 * @param input point on the curve to factor
1407 * @return INT_MAX if dlog failed, otherwise the factor
1410 GNUNET_CRYPTO_ecc_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc,
1411 gcry_mpi_point_t input);
1415 * Multiply the generator g of the elliptic curve by @a val
1416 * to obtain the point on the curve representing @a val.
1417 * Afterwards, point addition will correspond to integer
1418 * addition. #GNUNET_CRYPTO_ecc_dlog() can be used to
1419 * convert a point back to an integer (as long as the
1420 * integer is smaller than the MAX of the @a edc context).
1422 * @param edc calculation context for ECC operations
1423 * @param val value to encode into a point
1424 * @return representation of the value as an ECC point,
1425 * must be freed using #GNUNET_CRYPTO_ecc_free()
1428 GNUNET_CRYPTO_ecc_dexp (struct GNUNET_CRYPTO_EccDlogContext *edc,
1433 * Multiply the generator g of the elliptic curve by @a val
1434 * to obtain the point on the curve representing @a val.
1436 * @param edc calculation context for ECC operations
1437 * @param val (positive) value to encode into a point
1438 * @return representation of the value as an ECC point,
1439 * must be freed using #GNUNET_CRYPTO_ecc_free()
1442 GNUNET_CRYPTO_ecc_dexp_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1447 * Multiply the point @a p on the elliptic curve by @a val.
1449 * @param edc calculation context for ECC operations
1450 * @param p point to multiply
1451 * @param val (positive) value to encode into a point
1452 * @return representation of the value as an ECC point,
1453 * must be freed using #GNUNET_CRYPTO_ecc_free()
1456 GNUNET_CRYPTO_ecc_pmul_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1462 * Convert point value to binary representation.
1464 * @param edc calculation context for ECC operations
1465 * @param point computational point representation
1466 * @param[out] bin binary point representation
1469 GNUNET_CRYPTO_ecc_point_to_bin (struct GNUNET_CRYPTO_EccDlogContext *edc,
1470 gcry_mpi_point_t point,
1471 struct GNUNET_CRYPTO_EccPoint *bin);
1475 * Convert binary representation of a point to computational representation.
1477 * @param edc calculation context for ECC operations
1478 * @param bin binary point representation
1479 * @return computational representation
1482 GNUNET_CRYPTO_ecc_bin_to_point (struct GNUNET_CRYPTO_EccDlogContext *edc,
1483 const struct GNUNET_CRYPTO_EccPoint *bin);
1487 * Add two points on the elliptic curve.
1489 * @param edc calculation context for ECC operations
1490 * @param a some value
1491 * @param b some value
1492 * @return @a a + @a b, must be freed using #GNUNET_CRYPTO_ecc_free()
1495 GNUNET_CRYPTO_ecc_add (struct GNUNET_CRYPTO_EccDlogContext *edc,
1497 gcry_mpi_point_t b);
1501 * Obtain a random point on the curve and its
1502 * additive inverse. Both returned values
1503 * must be freed using #GNUNET_CRYPTO_ecc_free().
1505 * @param edc calculation context for ECC operations
1506 * @param[out] r set to a random point on the curve
1507 * @param[out] r_inv set to the additive inverse of @a r
1510 GNUNET_CRYPTO_ecc_rnd (struct GNUNET_CRYPTO_EccDlogContext *edc,
1511 gcry_mpi_point_t *r,
1512 gcry_mpi_point_t *r_inv);
1516 * Obtain a random scalar for point multiplication on the curve and
1517 * its multiplicative inverse.
1519 * @param edc calculation context for ECC operations
1520 * @param[out] r set to a random scalar on the curve
1521 * @param[out] r_inv set to the multiplicative inverse of @a r
1524 GNUNET_CRYPTO_ecc_rnd_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1530 * Generate a random value mod n.
1532 * @param edc ECC context
1533 * @return random value mod n.
1536 GNUNET_CRYPTO_ecc_random_mod_n (struct GNUNET_CRYPTO_EccDlogContext *edc);
1540 * Free a point value returned by the API.
1542 * @param p point to free
1545 GNUNET_CRYPTO_ecc_free (gcry_mpi_point_t p);
1549 * Release precalculated values.
1551 * @param dlc dlog context
1554 GNUNET_CRYPTO_ecc_dlog_release (struct GNUNET_CRYPTO_EccDlogContext *dlc);
1559 * Derive key material from a public and a private ECC key.
1561 * @param priv private key 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 (xyG)
1564 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1567 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1568 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1569 struct GNUNET_HashCode *key_material);
1574 * Derive key material from a ECDH public key and a private EdDSA key.
1575 * Dual to #GNUNET_CRRYPTO_ecdh_eddsa.
1577 * @param priv private key from EdDSA to use for the ECDH (x)
1578 * @param pub public key to use for the ECDH (yG)
1579 * @param key_material where to write the key material H(h(x)yG)
1580 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1583 GNUNET_CRYPTO_eddsa_ecdh (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1584 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1585 struct GNUNET_HashCode *key_material);
1589 * Derive key material from a ECDH public key and a private ECDSA key.
1590 * Dual to #GNUNET_CRRYPTO_ecdh_ecdsa.
1592 * @param priv private key from ECDSA to use for the ECDH (x)
1593 * @param pub public key to use for the ECDH (yG)
1594 * @param key_material where to write the key material H(h(x)yG)
1595 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1598 GNUNET_CRYPTO_ecdsa_ecdh (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1599 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1600 struct GNUNET_HashCode *key_material);
1605 * Derive key material from a EdDSA public key and a private ECDH key.
1606 * Dual to #GNUNET_CRRYPTO_eddsa_ecdh.
1608 * @param priv private key to use for the ECDH (y)
1609 * @param pub public key from EdDSA to use for the ECDH (X=h(x)G)
1610 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1611 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1614 GNUNET_CRYPTO_ecdh_eddsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1615 const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
1616 struct GNUNET_HashCode *key_material);
1620 * Derive key material from a EcDSA public key and a private ECDH key.
1621 * Dual to #GNUNET_CRRYPTO_ecdsa_ecdh.
1623 * @param priv private key to use for the ECDH (y)
1624 * @param pub public key from ECDSA to use for the ECDH (X=h(x)G)
1625 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1626 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1629 GNUNET_CRYPTO_ecdh_ecdsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1630 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1631 struct GNUNET_HashCode *key_material);
1636 * EdDSA sign a given block.
1638 * @param priv private key to use for the signing
1639 * @param purpose what to sign (size, purpose)
1640 * @param sig where to write the signature
1641 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1644 GNUNET_CRYPTO_eddsa_sign (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1645 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1646 struct GNUNET_CRYPTO_EddsaSignature *sig);
1651 * ECDSA Sign a given block.
1653 * @param priv private key to use for the signing
1654 * @param purpose what to sign (size, purpose)
1655 * @param sig where to write the signature
1656 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1659 GNUNET_CRYPTO_ecdsa_sign (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1660 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1661 struct GNUNET_CRYPTO_EcdsaSignature *sig);
1665 * Verify EdDSA signature.
1667 * @param purpose what is the purpose that the signature should have?
1668 * @param validate block to validate (size, purpose, data)
1669 * @param sig signature that is being validated
1670 * @param pub public key of the signer
1671 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1674 GNUNET_CRYPTO_eddsa_verify (uint32_t purpose,
1675 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1676 const struct GNUNET_CRYPTO_EddsaSignature *sig,
1677 const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1683 * Verify ECDSA signature.
1685 * @param purpose what is the purpose that the signature should have?
1686 * @param validate block to validate (size, purpose, data)
1687 * @param sig signature that is being validated
1688 * @param pub public key of the signer
1689 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1692 GNUNET_CRYPTO_ecdsa_verify (uint32_t purpose,
1693 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1694 const struct GNUNET_CRYPTO_EcdsaSignature *sig,
1695 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1700 * Derive a private key from a given private key and a label.
1701 * Essentially calculates a private key 'h = H(l,P) * d mod n'
1702 * where n is the size of the ECC group and P is the public
1703 * key associated with the private key 'd'.
1705 * @param priv original private key
1706 * @param label label to use for key deriviation
1707 * @param context additional context to use for HKDF of 'h';
1708 * typically the name of the subsystem/application
1709 * @return derived private key
1711 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1712 GNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1714 const char *context);
1719 * Derive a public key from a given public key and a label.
1720 * Essentially calculates a public key 'V = H(l,P) * P'.
1722 * @param pub original public key
1723 * @param label label to use for key deriviation
1724 * @param context additional context to use for HKDF of 'h'.
1725 * typically the name of the subsystem/application
1726 * @param result where to write the derived public key
1729 GNUNET_CRYPTO_ecdsa_public_key_derive (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1731 const char *context,
1732 struct GNUNET_CRYPTO_EcdsaPublicKey *result);
1736 * Output the given MPI value to the given buffer in network
1737 * byte order. The MPI @a val may not be negative.
1739 * @param buf where to output to
1740 * @param size number of bytes in @a buf
1741 * @param val value to write to @a buf
1744 GNUNET_CRYPTO_mpi_print_unsigned (void *buf,
1750 * Convert data buffer into MPI value.
1751 * The buffer is interpreted as network
1752 * byte order, unsigned integer.
1754 * @param result where to store MPI value (allocated)
1755 * @param data raw data (GCRYMPI_FMT_USG)
1756 * @param size number of bytes in @a data
1759 GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result,
1765 * Create a freshly generated paillier public key.
1767 * @param[out] public_key Where to store the public key?
1768 * @param[out] private_key Where to store the private key?
1771 GNUNET_CRYPTO_paillier_create (struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1772 struct GNUNET_CRYPTO_PaillierPrivateKey *private_key);
1776 * Encrypt a plaintext with a paillier public key.
1778 * @param public_key Public key to use.
1779 * @param m Plaintext to encrypt.
1780 * @param desired_ops How many homomorphic ops the caller intends to use
1781 * @param[out] ciphertext Encrytion of @a plaintext with @a public_key.
1782 * @return guaranteed number of supported homomorphic operations >= 1,
1783 * or desired_ops, in case that is lower,
1784 * or -1 if less than one homomorphic operation is possible
1787 GNUNET_CRYPTO_paillier_encrypt (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1790 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext);
1794 * Decrypt a paillier ciphertext with a private key.
1796 * @param private_key Private key to use for decryption.
1797 * @param public_key Public key to use for decryption.
1798 * @param ciphertext Ciphertext to decrypt.
1799 * @param[out] m Decryption of @a ciphertext with @private_key.
1802 GNUNET_CRYPTO_paillier_decrypt (const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key,
1803 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1804 const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
1809 * Compute a ciphertext that represents the sum of the plaintext in @a x1 and @a x2
1811 * Note that this operation can only be done a finite number of times
1812 * before an overflow occurs.
1814 * @param public_key Public key to use for encryption.
1815 * @param c1 Paillier cipher text.
1816 * @param c2 Paillier cipher text.
1817 * @param[out] result Result of the homomorphic operation.
1818 * @return #GNUNET_OK if the result could be computed,
1819 * #GNUNET_SYSERR if no more homomorphic operations are remaining.
1822 GNUNET_CRYPTO_paillier_hom_add (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1823 const struct GNUNET_CRYPTO_PaillierCiphertext *c1,
1824 const struct GNUNET_CRYPTO_PaillierCiphertext *c2,
1825 struct GNUNET_CRYPTO_PaillierCiphertext *result);
1829 * Get the number of remaining supported homomorphic operations.
1831 * @param c Paillier cipher text.
1832 * @return the number of remaining homomorphic operations
1835 GNUNET_CRYPTO_paillier_hom_get_remaining (const struct GNUNET_CRYPTO_PaillierCiphertext *c);
1838 /* ********* Chaum-style RSA-based blind signatures ******************* */
1844 * The private information of an RSA key pair.
1846 struct GNUNET_CRYPTO_RsaPrivateKey;
1849 * The public information of an RSA key pair.
1851 struct GNUNET_CRYPTO_RsaPublicKey;
1854 * Constant-size pre-secret for blinding key generation.
1856 struct GNUNET_CRYPTO_RsaBlindingKeySecret
1859 * Bits used to generate the blinding key. 256 bits
1860 * of entropy is enough.
1862 uint32_t pre_secret[8] GNUNET_PACKED;
1866 * @brief an RSA signature
1868 struct GNUNET_CRYPTO_RsaSignature;
1872 * Create a new private key. Caller must free return value.
1874 * @param len length of the key in bits (i.e. 2048)
1875 * @return fresh private key
1877 struct GNUNET_CRYPTO_RsaPrivateKey *
1878 GNUNET_CRYPTO_rsa_private_key_create (unsigned int len);
1882 * Free memory occupied by the private key.
1884 * @param key pointer to the memory to free
1887 GNUNET_CRYPTO_rsa_private_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *key);
1891 * Encode the private key in a format suitable for
1892 * storing it into a file.
1894 * @param key the private key
1895 * @param[out] buffer set to a buffer with the encoded key
1896 * @return size of memory allocatedin @a buffer
1899 GNUNET_CRYPTO_rsa_private_key_encode (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
1904 * Decode the private key from the data-format back
1905 * to the "normal", internal format.
1907 * @param buf the buffer where the private key data is stored
1908 * @param len the length of the data in @a buf
1909 * @return NULL on error
1911 struct GNUNET_CRYPTO_RsaPrivateKey *
1912 GNUNET_CRYPTO_rsa_private_key_decode (const char *buf,
1917 * Duplicate the given private key
1919 * @param key the private key to duplicate
1920 * @return the duplicate key; NULL upon error
1922 struct GNUNET_CRYPTO_RsaPrivateKey *
1923 GNUNET_CRYPTO_rsa_private_key_dup (const struct GNUNET_CRYPTO_RsaPrivateKey *key);
1927 * Extract the public key of the given private key.
1929 * @param priv the private key
1930 * @retur NULL on error, otherwise the public key
1932 struct GNUNET_CRYPTO_RsaPublicKey *
1933 GNUNET_CRYPTO_rsa_private_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey *priv);
1937 * Compute hash over the public key.
1939 * @param key public key to hash
1940 * @param hc where to store the hash code
1943 GNUNET_CRYPTO_rsa_public_key_hash (const struct GNUNET_CRYPTO_RsaPublicKey *key,
1944 struct GNUNET_HashCode *hc);
1948 * Obtain the length of the RSA key in bits.
1950 * @param key the public key to introspect
1951 * @return length of the key in bits
1954 GNUNET_CRYPTO_rsa_public_key_len (const struct GNUNET_CRYPTO_RsaPublicKey *key);
1958 * Free memory occupied by the public key.
1960 * @param key pointer to the memory to free
1963 GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_RsaPublicKey *key);
1967 * Encode the public key in a format suitable for
1968 * storing it into a file.
1970 * @param key the private key
1971 * @param[out] buffer set to a buffer with the encoded key
1972 * @return size of memory allocated in @a buffer
1975 GNUNET_CRYPTO_rsa_public_key_encode (const struct GNUNET_CRYPTO_RsaPublicKey *key,
1980 * Decode the public key from the data-format back
1981 * to the "normal", internal format.
1983 * @param buf the buffer where the public key data is stored
1984 * @param len the length of the data in @a buf
1985 * @return NULL on error
1987 struct GNUNET_CRYPTO_RsaPublicKey *
1988 GNUNET_CRYPTO_rsa_public_key_decode (const char *buf,
1993 * Duplicate the given public key
1995 * @param key the public key to duplicate
1996 * @return the duplicate key; NULL upon error
1998 struct GNUNET_CRYPTO_RsaPublicKey *
1999 GNUNET_CRYPTO_rsa_public_key_dup (const struct GNUNET_CRYPTO_RsaPublicKey *key);
2003 * Compare the values of two signatures.
2005 * @param s1 one signature
2006 * @param s2 the other signature
2007 * @return 0 if the two are equal
2010 GNUNET_CRYPTO_rsa_signature_cmp (struct GNUNET_CRYPTO_RsaSignature *s1,
2011 struct GNUNET_CRYPTO_RsaSignature *s2);
2014 * Compare the values of two private keys.
2016 * @param p1 one private key
2017 * @param p2 the other private key
2018 * @return 0 if the two are equal
2021 GNUNET_CRYPTO_rsa_private_key_cmp (struct GNUNET_CRYPTO_RsaPrivateKey *p1,
2022 struct GNUNET_CRYPTO_RsaPrivateKey *p2);
2026 * Compare the values of two public keys.
2028 * @param p1 one public key
2029 * @param p2 the other public key
2030 * @return 0 if the two are equal
2033 GNUNET_CRYPTO_rsa_public_key_cmp (struct GNUNET_CRYPTO_RsaPublicKey *p1,
2034 struct GNUNET_CRYPTO_RsaPublicKey *p2);
2038 * Blinds the given message with the given blinding key
2040 * @param hash hash of the message to sign
2041 * @param bkey the blinding key
2042 * @param pkey the public key of the signer
2043 * @param[out] buf set to a buffer with the blinded message to be signed
2044 * @param[out] buf_size number of bytes stored in @a buf
2045 * @return #GNUNET_YES if successful, #GNUNET_NO if RSA key is malicious
2048 GNUNET_CRYPTO_rsa_blind (const struct GNUNET_HashCode *hash,
2049 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
2050 struct GNUNET_CRYPTO_RsaPublicKey *pkey,
2056 * Sign a blinded value, which must be a full domain hash of a message.
2058 * @param key private key to use for the signing
2059 * @param msg the (blinded) message to sign
2060 * @param msg_len number of bytes in @a msg to sign
2061 * @return NULL on error, signature on success
2063 struct GNUNET_CRYPTO_RsaSignature *
2064 GNUNET_CRYPTO_rsa_sign_blinded (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2070 * Create and sign a full domain hash of a message.
2072 * @param key private key to use for the signing
2073 * @param hash the hash of the message to sign
2074 * @return NULL on error, including a malicious RSA key, signature on success
2076 struct GNUNET_CRYPTO_RsaSignature *
2077 GNUNET_CRYPTO_rsa_sign_fdh (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2078 const struct GNUNET_HashCode *hash);
2082 * Free memory occupied by signature.
2084 * @param sig memory to free
2087 GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_RsaSignature *sig);
2091 * Encode the given signature in a format suitable for storing it into a file.
2093 * @param sig the signature
2094 * @param[out] buffer set to a buffer with the encoded key
2095 * @return size of memory allocated in @a buffer
2098 GNUNET_CRYPTO_rsa_signature_encode (const struct GNUNET_CRYPTO_RsaSignature *sig,
2103 * Decode the signature from the data-format back to the "normal", internal
2106 * @param buf the buffer where the public key data is stored
2107 * @param len the length of the data in @a buf
2108 * @return NULL on error
2110 struct GNUNET_CRYPTO_RsaSignature *
2111 GNUNET_CRYPTO_rsa_signature_decode (const char *buf,
2116 * Duplicate the given rsa signature
2118 * @param sig the signature to duplicate
2119 * @return the duplicate key; NULL upon error
2121 struct GNUNET_CRYPTO_RsaSignature *
2122 GNUNET_CRYPTO_rsa_signature_dup (const struct GNUNET_CRYPTO_RsaSignature *sig);
2126 * Unblind a blind-signed signature. The signature should have been generated
2127 * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with
2128 * #GNUNET_CRYPTO_rsa_blind().
2130 * @param sig the signature made on the blinded signature purpose
2131 * @param bks the blinding key secret used to blind the signature purpose
2132 * @param pkey the public key of the signer
2133 * @return unblinded signature on success, NULL if RSA key is bad or malicious.
2135 struct GNUNET_CRYPTO_RsaSignature *
2136 GNUNET_CRYPTO_rsa_unblind (const struct GNUNET_CRYPTO_RsaSignature *sig,
2137 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
2138 struct GNUNET_CRYPTO_RsaPublicKey *pkey);
2142 * Verify whether the given hash corresponds to the given signature and the
2143 * signature is valid with respect to the given public key.
2145 * @param hash the message to verify to match the @a sig
2146 * @param sig signature that is being validated
2147 * @param public_key public key of the signer
2148 * @returns #GNUNET_YES if ok, #GNUNET_NO if RSA key is malicious, #GNUNET_SYSERR if signature
2151 GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash,
2152 const struct GNUNET_CRYPTO_RsaSignature *sig,
2153 const struct GNUNET_CRYPTO_RsaPublicKey *public_key);
2156 #if 0 /* keep Emacsens' auto-indent happy */
2164 /* ifndef GNUNET_CRYPTO_LIB_H */
2166 /* end of gnunet_crypto_lib.h */