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,
461 * Zero out @a buffer, securely against compiler optimizations.
462 * Used to delete key material.
464 * @param buffer the buffer to zap
465 * @param length buffer length
468 GNUNET_CRYPTO_zero_keys (void *buffer,
474 * Fill block with a random values.
476 * @param mode desired quality of the random number
477 * @param buffer the buffer to fill
478 * @param length buffer length
481 GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode,
487 * Produce a random value.
489 * @param mode desired quality of the random number
490 * @param i the upper limit (exclusive) for the random number
491 * @return a random value in the interval [0,@a i) (exclusive).
494 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode,
500 * Random on unsigned 64-bit values.
502 * @param mode desired quality of the random number
503 * @param max value returned will be in range [0,@a max) (exclusive)
504 * @return random 64-bit number
507 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
513 * Get an array with a random permutation of the
515 * @param mode #GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used,
516 * #GNUNET_CRYPTO_QUALITY_WEAK or #GNUNET_CRYPTO_QUALITY_NONCE otherwise
517 * @param n the size of the array
518 * @return the permutation array (allocated from heap)
521 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode,
527 * Create a new random session key.
529 * @param key key to initialize
532 GNUNET_CRYPTO_symmetric_create_session_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key);
537 * Encrypt a block using a symmetric sessionkey.
539 * @param block the block to encrypt
540 * @param size the size of the @a block
541 * @param sessionkey the key used to encrypt
542 * @param iv the initialization vector to use, use INITVALUE
544 * @return the size of the encrypted block, -1 for errors
547 GNUNET_CRYPTO_symmetric_encrypt (const void *block,
549 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
550 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
556 * Decrypt a given block using a symmetric sessionkey.
558 * @param block the data to decrypt, encoded as returned by encrypt
559 * @param size how big is the block?
560 * @param sessionkey the key used to decrypt
561 * @param iv the initialization vector to use
562 * @param result address to store the result at
563 * @return -1 on failure, size of decrypted block on success
566 GNUNET_CRYPTO_symmetric_decrypt (const void *block,
568 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
569 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
575 * @brief Derive an IV
576 * @param iv initialization vector
577 * @param skey session key
578 * @param salt salt for the derivation
579 * @param salt_len size of the @a salt
580 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
583 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
584 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
586 size_t salt_len, ...);
590 * @brief Derive an IV
591 * @param iv initialization vector
592 * @param skey session key
593 * @param salt salt for the derivation
594 * @param salt_len size of the @a salt
595 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
598 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
599 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
607 * Convert hash to ASCII encoding.
608 * @param block the hash code
609 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
610 * safely cast to char*, a '\\0' termination is set).
613 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block,
614 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
619 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
621 * @param enc the encoding
622 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
623 * @param result where to store the hash code
624 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
627 GNUNET_CRYPTO_hash_from_string2 (const char *enc,
629 struct GNUNET_HashCode *result);
634 * Convert ASCII encoding back to `struct GNUNET_HashCode`
636 * @param enc the encoding
637 * @param result where to store the hash code
638 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
640 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
641 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
647 * Compute the distance between 2 hashcodes. The
648 * computation must be fast, not involve @a a[0] or @a a[4] (they're used
649 * elsewhere), and be somewhat consistent. And of course, the result
650 * should be a positive number.
652 * @param a some hash code
653 * @param b some hash code
654 * @return number between 0 and UINT32_MAX
657 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a,
658 const struct GNUNET_HashCode *b);
663 * Compute hash of a given block.
665 * @param block the data to hash
666 * @param size size of the @a block
667 * @param ret pointer to where to write the hashcode
670 GNUNET_CRYPTO_hash (const void *block,
672 struct GNUNET_HashCode *ret);
676 * Context for cummulative hashing.
678 struct GNUNET_HashContext;
682 * Start incremental hashing operation.
684 * @return context for incremental hash computation
686 struct GNUNET_HashContext *
687 GNUNET_CRYPTO_hash_context_start (void);
691 * Add data to be hashed.
693 * @param hc cummulative hash context
694 * @param buf data to add
695 * @param size number of bytes in @a buf
698 GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
704 * Finish the hash computation.
706 * @param hc hash context to use, is freed in the process
707 * @param r_hash where to write the latest / final hash code
710 GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
711 struct GNUNET_HashCode *r_hash);
715 * Abort hashing, do not bother calculating final result.
717 * @param hc hash context to destroy
720 GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc);
724 * Calculate HMAC of a message (RFC 2104)
725 * TODO: Shouldn' this be the standard hmac function and
726 * the above be renamed?
728 * @param key secret key
729 * @param key_len secret key length
730 * @param plaintext input plaintext
731 * @param plaintext_len length of @a plaintext
732 * @param hmac where to store the hmac
735 GNUNET_CRYPTO_hmac_raw (const void *key, size_t key_len,
736 const void *plaintext, size_t plaintext_len,
737 struct GNUNET_HashCode *hmac);
742 * Calculate HMAC of a message (RFC 2104)
744 * @param key secret key
745 * @param plaintext input plaintext
746 * @param plaintext_len length of @a plaintext
747 * @param hmac where to store the hmac
750 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
751 const void *plaintext,
752 size_t plaintext_len,
753 struct GNUNET_HashCode *hmac);
757 * Function called once the hash computation over the
758 * specified file has completed.
761 * @param res resulting hash, NULL on error
764 (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
765 const struct GNUNET_HashCode *res);
769 * Handle to file hashing operation.
771 struct GNUNET_CRYPTO_FileHashContext;
776 * Compute the hash of an entire file.
778 * @param priority scheduling priority to use
779 * @param filename name of file to hash
780 * @param blocksize number of bytes to process in one task
781 * @param callback function to call upon completion
782 * @param callback_cls closure for @a callback
783 * @return NULL on (immediate) errror
785 struct GNUNET_CRYPTO_FileHashContext *
786 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
787 const char *filename,
789 GNUNET_CRYPTO_HashCompletedCallback callback,
794 * Cancel a file hashing operation.
796 * @param fhc operation to cancel (callback must not yet have been invoked)
799 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
804 * Create a random hash code.
806 * @param mode desired quality level
807 * @param result hash code that is randomized
810 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
811 struct GNUNET_HashCode *result);
816 * compute @a result = @a b - @a a
818 * @param a some hash code
819 * @param b some hash code
820 * @param result set to @a b - @a a
823 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
824 const struct GNUNET_HashCode *b,
825 struct GNUNET_HashCode *result);
830 * compute @a result = @a a + @a delta
832 * @param a some hash code
833 * @param delta some hash code
834 * @param result set to @a a + @a delta
837 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a,
838 const struct GNUNET_HashCode *delta,
839 struct GNUNET_HashCode *result);
844 * compute result = a ^ b
846 * @param a some hash code
847 * @param b some hash code
848 * @param result set to @a a ^ @a b
851 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
852 const struct GNUNET_HashCode *b,
853 struct GNUNET_HashCode *result);
858 * Convert a hashcode into a key.
860 * @param hc hash code that serves to generate the key
861 * @param skey set to a valid session key
862 * @param iv set to a valid initialization vector
865 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
866 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
867 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv);
872 * Obtain a bit from a hashcode.
874 * @param code the `struct GNUNET_HashCode` to index bit-wise
875 * @param bit index into the hashcode, [0...159]
876 * @return Bit \a bit from hashcode \a code, -1 for invalid index
879 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code,
885 * Determine how many low order bits match in two
886 * `struct GNUNET_HashCodes`. i.e. - 010011 and 011111 share
887 * the first two lowest order bits, and therefore the
888 * return value is two (NOT XOR distance, nor how many
889 * bits match absolutely!).
891 * @param first the first hashcode
892 * @param second the hashcode to compare first to
893 * @return the number of bits that match
896 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
897 const struct GNUNET_HashCode *second);
902 * Compare function for HashCodes, producing a total ordering
905 * @param h1 some hash code
906 * @param h2 some hash code
907 * @return 1 if @a h1 > @a h2, -1 if @a h1 < @a h2 and 0 if @a h1 == @a h2.
910 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
911 const struct GNUNET_HashCode *h2);
916 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
917 * in the XOR metric (Kademlia).
919 * @param h1 some hash code
920 * @param h2 some hash code
921 * @param target some hash code
922 * @return -1 if @a h1 is closer, 1 if @a h2 is closer and 0 if @a h1== @a h2.
925 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
926 const struct GNUNET_HashCode *h2,
927 const struct GNUNET_HashCode *target);
932 * @brief Derive an authentication key
933 * @param key authentication key
934 * @param rkey root key
936 * @param salt_len size of the salt
937 * @param argp pair of void * & size_t for context chunks, terminated by NULL
940 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
941 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
942 const void *salt, size_t salt_len,
948 * @brief Derive an authentication key
949 * @param key authentication key
950 * @param rkey root key
952 * @param salt_len size of the salt
953 * @param ... pair of void * & size_t for context chunks, terminated by NULL
956 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
957 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
958 const void *salt, size_t salt_len,
965 * @param result buffer for the derived key, allocated by caller
966 * @param out_len desired length of the derived key
967 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
968 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
970 * @param xts_len length of @a xts
971 * @param skm source key material
972 * @param skm_len length of @a skm
973 * @param ... pair of void * & size_t for context chunks, terminated by NULL
974 * @return #GNUNET_YES on success
977 GNUNET_CRYPTO_hkdf (void *result,
991 * @param result buffer for the derived key, allocated by caller
992 * @param out_len desired length of the derived key
993 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
994 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
996 * @param xts_len length of @a xts
997 * @param skm source key material
998 * @param skm_len length of @a skm
999 * @param argp va_list of void * & size_t pairs for context chunks
1000 * @return #GNUNET_YES on success
1003 GNUNET_CRYPTO_hkdf_v (void *result,
1016 * @param result buffer for the derived key, allocated by caller
1017 * @param out_len desired length of the derived key
1019 * @param xts_len length of @a xts
1020 * @param skm source key material
1021 * @param skm_len length of @a skm
1022 * @param argp va_list of void * & size_t pairs for context chunks
1023 * @return #GNUNET_YES on success
1026 GNUNET_CRYPTO_kdf_v (void *result,
1036 * Deterministically generate a pseudo-random number uniformly from the
1037 * integers modulo a libgcrypt mpi.
1039 * @param[out] r MPI value set to the FDH
1040 * @param n MPI to work modulo
1042 * @param xts_len length of @a xts
1043 * @param skm source key material
1044 * @param skm_len length of @a skm
1045 * @param ctx context string
1048 GNUNET_CRYPTO_kdf_mod_mpi (gcry_mpi_t *r,
1050 const void *xts, size_t xts_len,
1051 const void *skm, size_t skm_len,
1058 * @param result buffer for the derived key, allocated by caller
1059 * @param out_len desired length of the derived key
1061 * @param xts_len length of @a xts
1062 * @param skm source key material
1063 * @param skm_len length of @a skm
1064 * @param ... void * & size_t pairs for context chunks
1065 * @return #GNUNET_YES on success
1068 GNUNET_CRYPTO_kdf (void *result,
1079 * Extract the public key for the given private key.
1081 * @param priv the private key
1082 * @param pub where to write the public key
1085 GNUNET_CRYPTO_ecdsa_key_get_public (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1086 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1090 * Extract the public key for the given private key.
1092 * @param priv the private key
1093 * @param pub where to write the public key
1096 GNUNET_CRYPTO_eddsa_key_get_public (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1097 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1103 * Extract the public key for the given private key.
1105 * @param priv the private key
1106 * @param pub where to write the public key
1109 GNUNET_CRYPTO_ecdhe_key_get_public (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1110 struct GNUNET_CRYPTO_EcdhePublicKey *pub);
1114 * Convert a public key to a string.
1116 * @param pub key to convert
1117 * @return string representing @a pub
1120 GNUNET_CRYPTO_ecdsa_public_key_to_string (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1124 * Convert a private key to a string.
1126 * @param priv key to convert
1127 * @return string representing @a pub
1130 GNUNET_CRYPTO_eddsa_private_key_to_string (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv);
1134 * Convert a public key to a string.
1136 * @param pub key to convert
1137 * @return string representing @a pub
1140 GNUNET_CRYPTO_eddsa_public_key_to_string (const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1144 * Convert a string representing a public key to a public key.
1146 * @param enc encoded public key
1147 * @param enclen number of bytes in @a enc (without 0-terminator)
1148 * @param pub where to store the public key
1149 * @return #GNUNET_OK on success
1152 GNUNET_CRYPTO_ecdsa_public_key_from_string (const char *enc,
1154 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1158 * Convert a string representing a private key to a private key.
1160 * @param enc encoded public key
1161 * @param enclen number of bytes in @a enc (without 0-terminator)
1162 * @param priv where to store the private key
1163 * @return #GNUNET_OK on success
1166 GNUNET_CRYPTO_eddsa_private_key_from_string (const char *enc,
1168 struct GNUNET_CRYPTO_EddsaPrivateKey *pub);
1172 * Convert a string representing a public key to a public key.
1174 * @param enc encoded public key
1175 * @param enclen number of bytes in @a enc (without 0-terminator)
1176 * @param pub where to store the public key
1177 * @return #GNUNET_OK on success
1180 GNUNET_CRYPTO_eddsa_public_key_from_string (const char *enc,
1182 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1187 * Create a new private key by reading it from a file. If the
1188 * files does not exist, create a new key and write it to the
1189 * file. Caller must free return value. Note that this function
1190 * can not guarantee that another process might not be trying
1191 * the same operation on the same file at the same time.
1192 * If the contents of the file
1193 * are invalid the old file is deleted and a fresh key is
1196 * @param filename name of file to use to store the key
1197 * @return new private key, NULL on error (for example,
1198 * permission denied); free using #GNUNET_free
1200 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1201 GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename);
1206 * Create a new private key by reading it from a file. If the
1207 * files does not exist, create a new key and write it to the
1208 * file. Caller must free return value. Note that this function
1209 * can not guarantee that another process might not be trying
1210 * the same operation on the same file at the same time.
1211 * If the contents of the file
1212 * are invalid the old file is deleted and a fresh key is
1215 * @param filename name of file to use to store the key
1216 * @return new private key, NULL on error (for example,
1217 * permission denied); free using #GNUNET_free
1219 struct GNUNET_CRYPTO_EddsaPrivateKey *
1220 GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename);
1224 * Forward declaration to simplify #include-structure.
1226 struct GNUNET_CONFIGURATION_Handle;
1231 * Create a new private key by reading our peer's key from
1232 * the file specified in the configuration.
1234 * @param cfg the configuration to use
1235 * @return new private key, NULL on error (for example,
1236 * permission denied); free using #GNUNET_free
1238 struct GNUNET_CRYPTO_EddsaPrivateKey *
1239 GNUNET_CRYPTO_eddsa_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
1244 * Create a new private key. Caller must free return value.
1246 * @return fresh private key; free using #GNUNET_free
1248 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1249 GNUNET_CRYPTO_ecdsa_key_create (void);
1254 * Create a new private key. Caller must free return value.
1256 * @return fresh private key; free using #GNUNET_free
1258 struct GNUNET_CRYPTO_EddsaPrivateKey *
1259 GNUNET_CRYPTO_eddsa_key_create (void);
1264 * Create a new private key. Clear with #GNUNET_CRYPTO_ecdhe_key_clear().
1266 * @param[out] pk set to fresh private key;
1267 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1270 GNUNET_CRYPTO_ecdhe_key_create2 (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1275 * Create a new private key. Caller must free return value.
1277 * @return fresh private key; free using #GNUNET_free
1279 struct GNUNET_CRYPTO_EcdhePrivateKey *
1280 GNUNET_CRYPTO_ecdhe_key_create (void);
1285 * Clear memory that was used to store a private key.
1287 * @param pk location of the key
1290 GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk);
1295 * Clear memory that was used to store a private key.
1297 * @param pk location of the key
1300 GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk);
1305 * Clear memory that was used to store a private key.
1307 * @param pk location of the key
1310 GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1315 * Get the shared private key we use for anonymous users.
1317 * @return "anonymous" private key; do not free
1319 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
1320 GNUNET_CRYPTO_ecdsa_key_get_anonymous (void);
1325 * Setup a hostkey file for a peer given the name of the
1326 * configuration file (!). This function is used so that
1327 * at a later point code can be certain that reading a
1328 * hostkey is fast (for example in time-dependent testcases).
1330 * @param cfg_name name of the configuration file to use
1333 GNUNET_CRYPTO_eddsa_setup_hostkey (const char *cfg_name);
1338 * Retrieve the identity of the host's peer.
1340 * @param cfg configuration to use
1341 * @param dst pointer to where to write the peer identity
1342 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
1343 * could not be retrieved
1346 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
1347 struct GNUNET_PeerIdentity *dst);
1351 * Internal structure used to cache pre-calculated values for DLOG calculation.
1353 struct GNUNET_CRYPTO_EccDlogContext;
1357 * Point on a curve (always for Curve25519) encoded in a format suitable
1358 * for network transmission (ECDH), see http://cr.yp.to/ecdh.html.
1360 struct GNUNET_CRYPTO_EccPoint
1363 * Q consists of an x- and a y-value, each mod p (256 bits), given
1364 * here in affine coordinates and Ed25519 standard compact format.
1366 unsigned char q_y[256 / 8];
1371 * Do pre-calculation for ECC discrete logarithm for small factors.
1373 * @param max maximum value the factor can be
1374 * @param mem memory to use (should be smaller than @a max), must not be zero.
1375 * @return NULL on error
1377 struct GNUNET_CRYPTO_EccDlogContext *
1378 GNUNET_CRYPTO_ecc_dlog_prepare (unsigned int max,
1383 * Calculate ECC discrete logarithm for small factors.
1384 * Opposite of #GNUNET_CRYPTO_ecc_dexp().
1386 * @param dlc precalculated values, determine range of factors
1387 * @param input point on the curve to factor
1388 * @return INT_MAX if dlog failed, otherwise the factor
1391 GNUNET_CRYPTO_ecc_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc,
1392 gcry_mpi_point_t input);
1396 * Multiply the generator g of the elliptic curve by @a val
1397 * to obtain the point on the curve representing @a val.
1398 * Afterwards, point addition will correspond to integer
1399 * addition. #GNUNET_CRYPTO_ecc_dlog() can be used to
1400 * convert a point back to an integer (as long as the
1401 * integer is smaller than the MAX of the @a edc context).
1403 * @param edc calculation context for ECC operations
1404 * @param val value to encode into a point
1405 * @return representation of the value as an ECC point,
1406 * must be freed using #GNUNET_CRYPTO_ecc_free()
1409 GNUNET_CRYPTO_ecc_dexp (struct GNUNET_CRYPTO_EccDlogContext *edc,
1414 * Multiply the generator g of the elliptic curve by @a val
1415 * to obtain the point on the curve representing @a val.
1417 * @param edc calculation context for ECC operations
1418 * @param val (positive) value to encode into a point
1419 * @return representation of the value as an ECC point,
1420 * must be freed using #GNUNET_CRYPTO_ecc_free()
1423 GNUNET_CRYPTO_ecc_dexp_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1428 * Multiply the point @a p on the elliptic curve by @a val.
1430 * @param edc calculation context for ECC operations
1431 * @param p point to multiply
1432 * @param val (positive) value to encode into a point
1433 * @return representation of the value as an ECC point,
1434 * must be freed using #GNUNET_CRYPTO_ecc_free()
1437 GNUNET_CRYPTO_ecc_pmul_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1443 * Convert point value to binary representation.
1445 * @param edc calculation context for ECC operations
1446 * @param point computational point representation
1447 * @param[out] bin binary point representation
1450 GNUNET_CRYPTO_ecc_point_to_bin (struct GNUNET_CRYPTO_EccDlogContext *edc,
1451 gcry_mpi_point_t point,
1452 struct GNUNET_CRYPTO_EccPoint *bin);
1456 * Convert binary representation of a point to computational representation.
1458 * @param edc calculation context for ECC operations
1459 * @param bin binary point representation
1460 * @return computational representation
1463 GNUNET_CRYPTO_ecc_bin_to_point (struct GNUNET_CRYPTO_EccDlogContext *edc,
1464 const struct GNUNET_CRYPTO_EccPoint *bin);
1468 * Add two points on the elliptic curve.
1470 * @param edc calculation context for ECC operations
1471 * @param a some value
1472 * @param b some value
1473 * @return @a a + @a b, must be freed using #GNUNET_CRYPTO_ecc_free()
1476 GNUNET_CRYPTO_ecc_add (struct GNUNET_CRYPTO_EccDlogContext *edc,
1478 gcry_mpi_point_t b);
1482 * Obtain a random point on the curve and its
1483 * additive inverse. Both returned values
1484 * must be freed using #GNUNET_CRYPTO_ecc_free().
1486 * @param edc calculation context for ECC operations
1487 * @param[out] r set to a random point on the curve
1488 * @param[out] r_inv set to the additive inverse of @a r
1491 GNUNET_CRYPTO_ecc_rnd (struct GNUNET_CRYPTO_EccDlogContext *edc,
1492 gcry_mpi_point_t *r,
1493 gcry_mpi_point_t *r_inv);
1497 * Obtain a random scalar for point multiplication on the curve and
1498 * its multiplicative inverse.
1500 * @param edc calculation context for ECC operations
1501 * @param[out] r set to a random scalar on the curve
1502 * @param[out] r_inv set to the multiplicative inverse of @a r
1505 GNUNET_CRYPTO_ecc_rnd_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1511 * Generate a random value mod n.
1513 * @param edc ECC context
1514 * @return random value mod n.
1517 GNUNET_CRYPTO_ecc_random_mod_n (struct GNUNET_CRYPTO_EccDlogContext *edc);
1521 * Free a point value returned by the API.
1523 * @param p point to free
1526 GNUNET_CRYPTO_ecc_free (gcry_mpi_point_t p);
1530 * Release precalculated values.
1532 * @param dlc dlog context
1535 GNUNET_CRYPTO_ecc_dlog_release (struct GNUNET_CRYPTO_EccDlogContext *dlc);
1540 * Derive key material from a public and a private ECC key.
1542 * @param priv private key to use for the ECDH (x)
1543 * @param pub public key to use for the ECDH (yG)
1544 * @param key_material where to write the key material (xyG)
1545 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1548 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1549 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1550 struct GNUNET_HashCode *key_material);
1555 * Derive key material from a ECDH public key and a private EdDSA key.
1556 * Dual to #GNUNET_CRRYPTO_ecdh_eddsa.
1558 * @param priv private key from EdDSA to use for the ECDH (x)
1559 * @param pub public key to use for the ECDH (yG)
1560 * @param key_material where to write the key material H(h(x)yG)
1561 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1564 GNUNET_CRYPTO_eddsa_ecdh (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1565 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1566 struct GNUNET_HashCode *key_material);
1570 * Derive key material from a ECDH public key and a private ECDSA key.
1571 * Dual to #GNUNET_CRRYPTO_ecdh_ecdsa.
1573 * @param priv private key from ECDSA to use for the ECDH (x)
1574 * @param pub public key to use for the ECDH (yG)
1575 * @param key_material where to write the key material H(h(x)yG)
1576 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1579 GNUNET_CRYPTO_ecdsa_ecdh (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1580 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1581 struct GNUNET_HashCode *key_material);
1586 * Derive key material from a EdDSA public key and a private ECDH key.
1587 * Dual to #GNUNET_CRRYPTO_eddsa_ecdh.
1589 * @param priv private key to use for the ECDH (y)
1590 * @param pub public key from EdDSA to use for the ECDH (X=h(x)G)
1591 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1592 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1595 GNUNET_CRYPTO_ecdh_eddsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1596 const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
1597 struct GNUNET_HashCode *key_material);
1601 * Derive key material from a EcDSA public key and a private ECDH key.
1602 * Dual to #GNUNET_CRRYPTO_ecdsa_ecdh.
1604 * @param priv private key to use for the ECDH (y)
1605 * @param pub public key from ECDSA to use for the ECDH (X=h(x)G)
1606 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1607 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1610 GNUNET_CRYPTO_ecdh_ecdsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1611 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1612 struct GNUNET_HashCode *key_material);
1617 * EdDSA sign a given block.
1619 * @param priv private key to use for the signing
1620 * @param purpose what to sign (size, purpose)
1621 * @param sig where to write the signature
1622 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1625 GNUNET_CRYPTO_eddsa_sign (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1626 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1627 struct GNUNET_CRYPTO_EddsaSignature *sig);
1632 * ECDSA Sign a given block.
1634 * @param priv private key to use for the signing
1635 * @param purpose what to sign (size, purpose)
1636 * @param sig where to write the signature
1637 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1640 GNUNET_CRYPTO_ecdsa_sign (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1641 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1642 struct GNUNET_CRYPTO_EcdsaSignature *sig);
1646 * Verify EdDSA signature.
1648 * @param purpose what is the purpose that the signature should have?
1649 * @param validate block to validate (size, purpose, data)
1650 * @param sig signature that is being validated
1651 * @param pub public key of the signer
1652 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1655 GNUNET_CRYPTO_eddsa_verify (uint32_t purpose,
1656 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1657 const struct GNUNET_CRYPTO_EddsaSignature *sig,
1658 const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1664 * Verify ECDSA signature.
1666 * @param purpose what is the purpose that the signature should have?
1667 * @param validate block to validate (size, purpose, data)
1668 * @param sig signature that is being validated
1669 * @param pub public key of the signer
1670 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1673 GNUNET_CRYPTO_ecdsa_verify (uint32_t purpose,
1674 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1675 const struct GNUNET_CRYPTO_EcdsaSignature *sig,
1676 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1681 * Derive a private key from a given private key and a label.
1682 * Essentially calculates a private key 'h = H(l,P) * d mod n'
1683 * where n is the size of the ECC group and P is the public
1684 * key associated with the private key 'd'.
1686 * @param priv original private key
1687 * @param label label to use for key deriviation
1688 * @param context additional context to use for HKDF of 'h';
1689 * typically the name of the subsystem/application
1690 * @return derived private key
1692 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1693 GNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1695 const char *context);
1700 * Derive a public key from a given public key and a label.
1701 * Essentially calculates a public key 'V = H(l,P) * P'.
1703 * @param pub original public key
1704 * @param label label to use for key deriviation
1705 * @param context additional context to use for HKDF of 'h'.
1706 * typically the name of the subsystem/application
1707 * @param result where to write the derived public key
1710 GNUNET_CRYPTO_ecdsa_public_key_derive (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1712 const char *context,
1713 struct GNUNET_CRYPTO_EcdsaPublicKey *result);
1717 * Output the given MPI value to the given buffer in network
1718 * byte order. The MPI @a val may not be negative.
1720 * @param buf where to output to
1721 * @param size number of bytes in @a buf
1722 * @param val value to write to @a buf
1725 GNUNET_CRYPTO_mpi_print_unsigned (void *buf,
1731 * Convert data buffer into MPI value.
1732 * The buffer is interpreted as network
1733 * byte order, unsigned integer.
1735 * @param result where to store MPI value (allocated)
1736 * @param data raw data (GCRYMPI_FMT_USG)
1737 * @param size number of bytes in @a data
1740 GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result,
1746 * Create a freshly generated paillier public key.
1748 * @param[out] public_key Where to store the public key?
1749 * @param[out] private_key Where to store the private key?
1752 GNUNET_CRYPTO_paillier_create (struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1753 struct GNUNET_CRYPTO_PaillierPrivateKey *private_key);
1757 * Encrypt a plaintext with a paillier public key.
1759 * @param public_key Public key to use.
1760 * @param m Plaintext to encrypt.
1761 * @param desired_ops How many homomorphic ops the caller intends to use
1762 * @param[out] ciphertext Encrytion of @a plaintext with @a public_key.
1763 * @return guaranteed number of supported homomorphic operations >= 1,
1764 * or desired_ops, in case that is lower,
1765 * or -1 if less than one homomorphic operation is possible
1768 GNUNET_CRYPTO_paillier_encrypt (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1771 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext);
1775 * Decrypt a paillier ciphertext with a private key.
1777 * @param private_key Private key to use for decryption.
1778 * @param public_key Public key to use for decryption.
1779 * @param ciphertext Ciphertext to decrypt.
1780 * @param[out] m Decryption of @a ciphertext with @private_key.
1783 GNUNET_CRYPTO_paillier_decrypt (const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key,
1784 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1785 const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
1790 * Compute a ciphertext that represents the sum of the plaintext in @a x1 and @a x2
1792 * Note that this operation can only be done a finite number of times
1793 * before an overflow occurs.
1795 * @param public_key Public key to use for encryption.
1796 * @param c1 Paillier cipher text.
1797 * @param c2 Paillier cipher text.
1798 * @param[out] result Result of the homomorphic operation.
1799 * @return #GNUNET_OK if the result could be computed,
1800 * #GNUNET_SYSERR if no more homomorphic operations are remaining.
1803 GNUNET_CRYPTO_paillier_hom_add (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1804 const struct GNUNET_CRYPTO_PaillierCiphertext *c1,
1805 const struct GNUNET_CRYPTO_PaillierCiphertext *c2,
1806 struct GNUNET_CRYPTO_PaillierCiphertext *result);
1810 * Get the number of remaining supported homomorphic operations.
1812 * @param c Paillier cipher text.
1813 * @return the number of remaining homomorphic operations
1816 GNUNET_CRYPTO_paillier_hom_get_remaining (const struct GNUNET_CRYPTO_PaillierCiphertext *c);
1819 /* ********* Chaum-style RSA-based blind signatures ******************* */
1825 * The private information of an RSA key pair.
1827 struct GNUNET_CRYPTO_RsaPrivateKey;
1830 * The public information of an RSA key pair.
1832 struct GNUNET_CRYPTO_RsaPublicKey;
1835 * Constant-size pre-secret for blinding key generation.
1837 struct GNUNET_CRYPTO_RsaBlindingKeySecret
1840 * Bits used to generate the blinding key. 256 bits
1841 * of entropy is enough.
1843 uint32_t pre_secret[8] GNUNET_PACKED;
1847 * @brief an RSA signature
1849 struct GNUNET_CRYPTO_RsaSignature;
1853 * Create a new private key. Caller must free return value.
1855 * @param len length of the key in bits (i.e. 2048)
1856 * @return fresh private key
1858 struct GNUNET_CRYPTO_RsaPrivateKey *
1859 GNUNET_CRYPTO_rsa_private_key_create (unsigned int len);
1863 * Free memory occupied by the private key.
1865 * @param key pointer to the memory to free
1868 GNUNET_CRYPTO_rsa_private_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *key);
1872 * Encode the private key in a format suitable for
1873 * storing it into a file.
1875 * @param key the private key
1876 * @param[out] buffer set to a buffer with the encoded key
1877 * @return size of memory allocatedin @a buffer
1880 GNUNET_CRYPTO_rsa_private_key_encode (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
1885 * Decode the private key from the data-format back
1886 * to the "normal", internal format.
1888 * @param buf the buffer where the private key data is stored
1889 * @param len the length of the data in @a buf
1890 * @return NULL on error
1892 struct GNUNET_CRYPTO_RsaPrivateKey *
1893 GNUNET_CRYPTO_rsa_private_key_decode (const char *buf,
1898 * Duplicate the given private key
1900 * @param key the private key to duplicate
1901 * @return the duplicate key; NULL upon error
1903 struct GNUNET_CRYPTO_RsaPrivateKey *
1904 GNUNET_CRYPTO_rsa_private_key_dup (const struct GNUNET_CRYPTO_RsaPrivateKey *key);
1908 * Extract the public key of the given private key.
1910 * @param priv the private key
1911 * @retur NULL on error, otherwise the public key
1913 struct GNUNET_CRYPTO_RsaPublicKey *
1914 GNUNET_CRYPTO_rsa_private_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey *priv);
1918 * Compute hash over the public key.
1920 * @param key public key to hash
1921 * @param hc where to store the hash code
1924 GNUNET_CRYPTO_rsa_public_key_hash (const struct GNUNET_CRYPTO_RsaPublicKey *key,
1925 struct GNUNET_HashCode *hc);
1929 * Obtain the length of the RSA key in bits.
1931 * @param key the public key to introspect
1932 * @return length of the key in bits
1935 GNUNET_CRYPTO_rsa_public_key_len (const struct GNUNET_CRYPTO_RsaPublicKey *key);
1939 * Free memory occupied by the public key.
1941 * @param key pointer to the memory to free
1944 GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_RsaPublicKey *key);
1948 * Encode the public key in a format suitable for
1949 * storing it into a file.
1951 * @param key the private key
1952 * @param[out] buffer set to a buffer with the encoded key
1953 * @return size of memory allocated in @a buffer
1956 GNUNET_CRYPTO_rsa_public_key_encode (const struct GNUNET_CRYPTO_RsaPublicKey *key,
1961 * Decode the public key from the data-format back
1962 * to the "normal", internal format.
1964 * @param buf the buffer where the public key data is stored
1965 * @param len the length of the data in @a buf
1966 * @return NULL on error
1968 struct GNUNET_CRYPTO_RsaPublicKey *
1969 GNUNET_CRYPTO_rsa_public_key_decode (const char *buf,
1974 * Duplicate the given public key
1976 * @param key the public key to duplicate
1977 * @return the duplicate key; NULL upon error
1979 struct GNUNET_CRYPTO_RsaPublicKey *
1980 GNUNET_CRYPTO_rsa_public_key_dup (const struct GNUNET_CRYPTO_RsaPublicKey *key);
1984 * Compare the values of two signatures.
1986 * @param s1 one signature
1987 * @param s2 the other signature
1988 * @return 0 if the two are equal
1991 GNUNET_CRYPTO_rsa_signature_cmp (struct GNUNET_CRYPTO_RsaSignature *s1,
1992 struct GNUNET_CRYPTO_RsaSignature *s2);
1995 * Compare the values of two private keys.
1997 * @param p1 one private key
1998 * @param p2 the other private key
1999 * @return 0 if the two are equal
2002 GNUNET_CRYPTO_rsa_private_key_cmp (struct GNUNET_CRYPTO_RsaPrivateKey *p1,
2003 struct GNUNET_CRYPTO_RsaPrivateKey *p2);
2007 * Compare the values of two public keys.
2009 * @param p1 one public key
2010 * @param p2 the other public key
2011 * @return 0 if the two are equal
2014 GNUNET_CRYPTO_rsa_public_key_cmp (struct GNUNET_CRYPTO_RsaPublicKey *p1,
2015 struct GNUNET_CRYPTO_RsaPublicKey *p2);
2019 * Blinds the given message with the given blinding key
2021 * @param hash hash of the message to sign
2022 * @param bkey the blinding key
2023 * @param pkey the public key of the signer
2024 * @param[out] buf set to a buffer with the blinded message to be signed
2025 * @param[out] buf_size number of bytes stored in @a buf
2026 * @return #GNUNET_YES if successful, #GNUNET_NO if RSA key is malicious
2029 GNUNET_CRYPTO_rsa_blind (const struct GNUNET_HashCode *hash,
2030 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
2031 struct GNUNET_CRYPTO_RsaPublicKey *pkey,
2037 * Sign a blinded value, which must be a full domain hash of a message.
2039 * @param key private key to use for the signing
2040 * @param msg the (blinded) message to sign
2041 * @param msg_len number of bytes in @a msg to sign
2042 * @return NULL on error, signature on success
2044 struct GNUNET_CRYPTO_RsaSignature *
2045 GNUNET_CRYPTO_rsa_sign_blinded (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2051 * Create and sign a full domain hash of a message.
2053 * @param key private key to use for the signing
2054 * @param hash the hash of the message to sign
2055 * @return NULL on error, including a malicious RSA key, signature on success
2057 struct GNUNET_CRYPTO_RsaSignature *
2058 GNUNET_CRYPTO_rsa_sign_fdh (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2059 const struct GNUNET_HashCode *hash);
2063 * Free memory occupied by signature.
2065 * @param sig memory to free
2068 GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_RsaSignature *sig);
2072 * Encode the given signature in a format suitable for storing it into a file.
2074 * @param sig the signature
2075 * @param[out] buffer set to a buffer with the encoded key
2076 * @return size of memory allocated in @a buffer
2079 GNUNET_CRYPTO_rsa_signature_encode (const struct GNUNET_CRYPTO_RsaSignature *sig,
2084 * Decode the signature from the data-format back to the "normal", internal
2087 * @param buf the buffer where the public key data is stored
2088 * @param len the length of the data in @a buf
2089 * @return NULL on error
2091 struct GNUNET_CRYPTO_RsaSignature *
2092 GNUNET_CRYPTO_rsa_signature_decode (const char *buf,
2097 * Duplicate the given rsa signature
2099 * @param sig the signature to duplicate
2100 * @return the duplicate key; NULL upon error
2102 struct GNUNET_CRYPTO_RsaSignature *
2103 GNUNET_CRYPTO_rsa_signature_dup (const struct GNUNET_CRYPTO_RsaSignature *sig);
2107 * Unblind a blind-signed signature. The signature should have been generated
2108 * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with
2109 * #GNUNET_CRYPTO_rsa_blind().
2111 * @param sig the signature made on the blinded signature purpose
2112 * @param bks the blinding key secret used to blind the signature purpose
2113 * @param pkey the public key of the signer
2114 * @return unblinded signature on success, NULL if RSA key is bad or malicious.
2116 struct GNUNET_CRYPTO_RsaSignature *
2117 GNUNET_CRYPTO_rsa_unblind (const struct GNUNET_CRYPTO_RsaSignature *sig,
2118 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
2119 struct GNUNET_CRYPTO_RsaPublicKey *pkey);
2123 * Verify whether the given hash corresponds to the given signature and the
2124 * signature is valid with respect to the given public key.
2126 * @param hash the message to verify to match the @a sig
2127 * @param sig signature that is being validated
2128 * @param public_key public key of the signer
2129 * @returns #GNUNET_YES if ok, #GNUNET_NO if RSA key is malicious, #GNUNET_SYSERR if signature
2132 GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash,
2133 const struct GNUNET_CRYPTO_RsaSignature *sig,
2134 const struct GNUNET_CRYPTO_RsaPublicKey *public_key);
2137 #if 0 /* keep Emacsens' auto-indent happy */
2145 /* ifndef GNUNET_CRYPTO_LIB_H */
2147 /* end of gnunet_crypto_lib.h */