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 */
55 * @brief A 512-bit hashcode. These are the default length for GNUnet, using SHA-512.
57 struct GNUNET_HashCode
59 uint32_t bits[512 / 8 / sizeof (uint32_t)]; /* = 16 */
65 * @brief A 256-bit hashcode. Used under special conditions, like when space
66 * is critical and security is not impacted by it.
68 struct GNUNET_ShortHashCode
70 uint32_t bits[256 / 8 / sizeof (uint32_t)]; /* = 8 */
75 * The identity of the host (wraps the signing key of the peer).
77 struct GNUNET_PeerIdentity;
79 #include "gnunet_common.h"
84 * Maximum length of an ECC signature.
85 * Note: round up to multiple of 8 minus 2 for alignment.
87 #define GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH 126
91 * Desired quality level for random numbers.
94 enum GNUNET_CRYPTO_Quality
97 * No good quality of the operation is needed (i.e.,
98 * random numbers can be pseudo-random).
101 GNUNET_CRYPTO_QUALITY_WEAK,
104 * High-quality operations are desired.
107 GNUNET_CRYPTO_QUALITY_STRONG,
110 * Randomness for IVs etc. is required.
113 GNUNET_CRYPTO_QUALITY_NONCE
118 * @brief length of the sessionkey in bytes (256 BIT sessionkey)
120 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
123 * Length of a hash value
125 #define GNUNET_CRYPTO_HASH_LENGTH (512/8)
128 * How many characters (without 0-terminator) are our ASCII-encoded
129 * public keys (ECDSA/EDDSA/ECDHE).
131 #define GNUNET_CRYPTO_PKEY_ASCII_LENGTH 52
134 * @brief 0-terminated ASCII encoding of a struct GNUNET_HashCode.
136 struct GNUNET_CRYPTO_HashAsciiEncoded
138 unsigned char encoding[104];
142 GNUNET_NETWORK_STRUCT_BEGIN
146 * @brief header of what an ECC signature signs
147 * this must be followed by "size - 8" bytes of
148 * the actual signed data
150 struct GNUNET_CRYPTO_EccSignaturePurpose
153 * How many bytes does this signature sign?
154 * (including this purpose header); in network
157 uint32_t size GNUNET_PACKED;
160 * What does this signature vouch for? This
161 * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
162 * constant (from gnunet_signatures.h). In
163 * network byte order!
165 uint32_t purpose GNUNET_PACKED;
171 * @brief an ECC signature using EdDSA.
172 * See https://gnunet.org/ed25519
174 struct GNUNET_CRYPTO_EddsaSignature
180 unsigned char r[256 / 8];
185 unsigned char s[256 / 8];
192 * @brief an ECC signature using ECDSA
194 struct GNUNET_CRYPTO_EcdsaSignature
200 unsigned char r[256 / 8];
205 unsigned char s[256 / 8];
211 * Public ECC key (always for curve Ed25519) encoded in a format
212 * suitable for network transmission and EdDSA signatures.
214 struct GNUNET_CRYPTO_EddsaPublicKey
217 * Point Q consists of a y-value mod p (256 bits); the x-value is
218 * always positive. The point is stored in Ed25519 standard
221 unsigned char q_y[256 / 8];
227 * Public ECC key (always for Curve25519) encoded in a format suitable
228 * for network transmission and ECDSA signatures.
230 struct GNUNET_CRYPTO_EcdsaPublicKey
233 * Q consists of an x- and a y-value, each mod p (256 bits), given
234 * here in affine coordinates and Ed25519 standard compact format.
236 unsigned char q_y[256 / 8];
242 * The identity of the host (wraps the signing key of the peer).
244 struct GNUNET_PeerIdentity
246 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
251 * Public ECC key (always for Curve25519) encoded in a format suitable
252 * for network transmission and encryption (ECDH),
253 * See http://cr.yp.to/ecdh.html
255 struct GNUNET_CRYPTO_EcdhePublicKey
258 * Q consists of an x- and a y-value, each mod p (256 bits), given
259 * here in affine coordinates and Ed25519 standard compact format.
261 unsigned char q_y[256 / 8];
266 * Private ECC key encoded for transmission. To be used only for ECDH
267 * key exchange (ECDHE to be precise).
269 struct GNUNET_CRYPTO_EcdhePrivateKey
272 * d is a value mod n, where n has at most 256 bits.
274 unsigned char d[256 / 8];
279 * Private ECC key encoded for transmission. To be used only for ECDSA
282 struct GNUNET_CRYPTO_EcdsaPrivateKey
285 * d is a value mod n, where n has at most 256 bits.
287 unsigned char d[256 / 8];
292 * Private ECC key encoded for transmission. To be used only for EdDSA
295 struct GNUNET_CRYPTO_EddsaPrivateKey
298 * d is a value mod n, where n has at most 256 bits.
300 unsigned char d[256 / 8];
306 * @brief type for session keys
308 struct GNUNET_CRYPTO_SymmetricSessionKey
311 * Actual key for AES.
313 unsigned char aes_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
316 * Actual key for TwoFish.
318 unsigned char twofish_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
322 GNUNET_NETWORK_STRUCT_END
325 * @brief IV for sym cipher
327 * NOTE: must be smaller (!) in size than the
328 * `struct GNUNET_HashCode`.
330 struct GNUNET_CRYPTO_SymmetricInitializationVector
332 unsigned char aes_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
334 unsigned char twofish_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
339 * @brief type for (message) authentication keys
341 struct GNUNET_CRYPTO_AuthKey
343 unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
348 * Size of paillier plain texts and public keys.
349 * Private keys and ciphertexts are twice this size.
351 #define GNUNET_CRYPTO_PAILLIER_BITS 2048
355 * Paillier public key.
357 struct GNUNET_CRYPTO_PaillierPublicKey
362 unsigned char n[GNUNET_CRYPTO_PAILLIER_BITS / 8];
367 * Paillier private key.
369 struct GNUNET_CRYPTO_PaillierPrivateKey
372 * Lambda-component of the private key.
374 unsigned char lambda[GNUNET_CRYPTO_PAILLIER_BITS / 8];
376 * Mu-component of the private key.
378 unsigned char mu[GNUNET_CRYPTO_PAILLIER_BITS / 8];
383 * Paillier ciphertext.
385 struct GNUNET_CRYPTO_PaillierCiphertext
388 * Guaranteed minimum number of homomorphic operations with this ciphertext,
389 * in network byte order (NBO).
391 int32_t remaining_ops GNUNET_PACKED;
394 * The bits of the ciphertext.
396 unsigned char bits[GNUNET_CRYPTO_PAILLIER_BITS * 2 / 8];
400 /* **************** Functions and Macros ************* */
404 * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator
407 * @param seed the seed to use
410 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
415 * Calculate the checksum of a buffer in one step.
417 * @param buf buffer to calculate CRC over
418 * @param len number of bytes in @a buf
422 GNUNET_CRYPTO_crc8_n (const void *buf,
427 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
429 * @param sum current sum, initially 0
430 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
431 * @param len number of bytes in @a buf, must be multiple of 2
432 * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish to get actual crc16)
435 GNUNET_CRYPTO_crc16_step (uint32_t sum,
441 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
443 * @param sum cummulative sum
444 * @return crc16 value
447 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
452 * Calculate the checksum of a buffer in one step.
454 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
455 * @param len number of bytes in @a buf, must be multiple of 2
456 * @return crc16 value
459 GNUNET_CRYPTO_crc16_n (const void *buf,
467 * Compute the CRC32 checksum for the first len
468 * bytes of the buffer.
470 * @param buf the data over which we're taking the CRC
471 * @param len the length of the buffer @a buf in bytes
472 * @return the resulting CRC32 checksum
475 GNUNET_CRYPTO_crc32_n (const void *buf,
481 * Fill block with a random values.
483 * @param mode desired quality of the random number
484 * @param buffer the buffer to fill
485 * @param length buffer length
488 GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode,
494 * Produce a random value.
496 * @param mode desired quality of the random number
497 * @param i the upper limit (exclusive) for the random number
498 * @return a random value in the interval [0,@a i) (exclusive).
501 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode,
507 * Random on unsigned 64-bit values.
509 * @param mode desired quality of the random number
510 * @param max value returned will be in range [0,@a max) (exclusive)
511 * @return random 64-bit number
514 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
520 * Get an array with a random permutation of the
522 * @param mode #GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used,
523 * #GNUNET_CRYPTO_QUALITY_WEAK or #GNUNET_CRYPTO_QUALITY_NONCE otherwise
524 * @param n the size of the array
525 * @return the permutation array (allocated from heap)
528 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode,
534 * Create a new random session key.
536 * @param key key to initialize
539 GNUNET_CRYPTO_symmetric_create_session_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key);
544 * Encrypt a block using a symmetric sessionkey.
546 * @param block the block to encrypt
547 * @param size the size of the @a block
548 * @param sessionkey the key used to encrypt
549 * @param iv the initialization vector to use, use INITVALUE
551 * @return the size of the encrypted block, -1 for errors
554 GNUNET_CRYPTO_symmetric_encrypt (const void *block,
556 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
557 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
563 * Decrypt a given block using a symmetric sessionkey.
565 * @param block the data to decrypt, encoded as returned by encrypt
566 * @param size how big is the block?
567 * @param sessionkey the key used to decrypt
568 * @param iv the initialization vector to use
569 * @param result address to store the result at
570 * @return -1 on failure, size of decrypted block on success
573 GNUNET_CRYPTO_symmetric_decrypt (const void *block,
575 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
576 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
582 * @brief Derive an IV
583 * @param iv initialization vector
584 * @param skey session key
585 * @param salt salt for the derivation
586 * @param salt_len size of the @a salt
587 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
590 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
591 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
593 size_t salt_len, ...);
597 * @brief Derive an IV
598 * @param iv initialization vector
599 * @param skey session key
600 * @param salt salt for the derivation
601 * @param salt_len size of the @a salt
602 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
605 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
606 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
614 * Convert hash to ASCII encoding.
615 * @param block the hash code
616 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
617 * safely cast to char*, a '\\0' termination is set).
620 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block,
621 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
626 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
628 * @param enc the encoding
629 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
630 * @param result where to store the hash code
631 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
634 GNUNET_CRYPTO_hash_from_string2 (const char *enc,
636 struct GNUNET_HashCode *result);
641 * Convert ASCII encoding back to `struct GNUNET_HashCode`
643 * @param enc the encoding
644 * @param result where to store the hash code
645 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
647 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
648 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
654 * Compute the distance between 2 hashcodes. The
655 * computation must be fast, not involve @a a[0] or @a a[4] (they're used
656 * elsewhere), and be somewhat consistent. And of course, the result
657 * should be a positive number.
659 * @param a some hash code
660 * @param b some hash code
661 * @return number between 0 and UINT32_MAX
664 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a,
665 const struct GNUNET_HashCode *b);
670 * Compute hash of a given block.
672 * @param block the data to hash
673 * @param size size of the @a block
674 * @param ret pointer to where to write the hashcode
677 GNUNET_CRYPTO_hash (const void *block,
679 struct GNUNET_HashCode *ret);
683 * Context for cummulative hashing.
685 struct GNUNET_HashContext;
689 * Start incremental hashing operation.
691 * @return context for incremental hash computation
693 struct GNUNET_HashContext *
694 GNUNET_CRYPTO_hash_context_start (void);
698 * Add data to be hashed.
700 * @param hc cummulative hash context
701 * @param buf data to add
702 * @param size number of bytes in @a buf
705 GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
711 * Finish the hash computation.
713 * @param hc hash context to use, is freed in the process
714 * @param r_hash where to write the latest / final hash code
717 GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
718 struct GNUNET_HashCode *r_hash);
722 * Abort hashing, do not bother calculating final result.
724 * @param hc hash context to destroy
727 GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc);
731 * Calculate HMAC of a message (RFC 2104)
732 * TODO: Shouldn' this be the standard hmac function and
733 * the above be renamed?
735 * @param key secret key
736 * @param key_len secret key length
737 * @param plaintext input plaintext
738 * @param plaintext_len length of @a plaintext
739 * @param hmac where to store the hmac
742 GNUNET_CRYPTO_hmac_raw (const void *key, size_t key_len,
743 const void *plaintext, size_t plaintext_len,
744 struct GNUNET_HashCode *hmac);
749 * Calculate HMAC of a message (RFC 2104)
751 * @param key secret key
752 * @param plaintext input plaintext
753 * @param plaintext_len length of @a plaintext
754 * @param hmac where to store the hmac
757 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
758 const void *plaintext,
759 size_t plaintext_len,
760 struct GNUNET_HashCode *hmac);
764 * Function called once the hash computation over the
765 * specified file has completed.
768 * @param res resulting hash, NULL on error
771 (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
772 const struct GNUNET_HashCode *res);
776 * Handle to file hashing operation.
778 struct GNUNET_CRYPTO_FileHashContext;
783 * Compute the hash of an entire file.
785 * @param priority scheduling priority to use
786 * @param filename name of file to hash
787 * @param blocksize number of bytes to process in one task
788 * @param callback function to call upon completion
789 * @param callback_cls closure for @a callback
790 * @return NULL on (immediate) errror
792 struct GNUNET_CRYPTO_FileHashContext *
793 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
794 const char *filename,
796 GNUNET_CRYPTO_HashCompletedCallback callback,
801 * Cancel a file hashing operation.
803 * @param fhc operation to cancel (callback must not yet have been invoked)
806 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
811 * Create a random hash code.
813 * @param mode desired quality level
814 * @param result hash code that is randomized
817 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
818 struct GNUNET_HashCode *result);
823 * compute @a result = @a b - @a a
825 * @param a some hash code
826 * @param b some hash code
827 * @param result set to @a b - @a a
830 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
831 const struct GNUNET_HashCode *b,
832 struct GNUNET_HashCode *result);
837 * compute @a result = @a a + @a delta
839 * @param a some hash code
840 * @param delta some hash code
841 * @param result set to @a a + @a delta
844 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a,
845 const struct GNUNET_HashCode *delta,
846 struct GNUNET_HashCode *result);
851 * compute result = a ^ b
853 * @param a some hash code
854 * @param b some hash code
855 * @param result set to @a a ^ @a b
858 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
859 const struct GNUNET_HashCode *b,
860 struct GNUNET_HashCode *result);
865 * Convert a hashcode into a key.
867 * @param hc hash code that serves to generate the key
868 * @param skey set to a valid session key
869 * @param iv set to a valid initialization vector
872 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
873 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
874 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv);
879 * Obtain a bit from a hashcode.
881 * @param code the `struct GNUNET_HashCode` to index bit-wise
882 * @param bit index into the hashcode, [0...159]
883 * @return Bit \a bit from hashcode \a code, -1 for invalid index
886 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code,
892 * Determine how many low order bits match in two
893 * `struct GNUNET_HashCodes`. i.e. - 010011 and 011111 share
894 * the first two lowest order bits, and therefore the
895 * return value is two (NOT XOR distance, nor how many
896 * bits match absolutely!).
898 * @param first the first hashcode
899 * @param second the hashcode to compare first to
900 * @return the number of bits that match
903 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
904 const struct GNUNET_HashCode *second);
909 * Compare function for HashCodes, producing a total ordering
912 * @param h1 some hash code
913 * @param h2 some hash code
914 * @return 1 if @a h1 > @a h2, -1 if @a h1 < @a h2 and 0 if @a h1 == @a h2.
917 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
918 const struct GNUNET_HashCode *h2);
923 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
924 * in the XOR metric (Kademlia).
926 * @param h1 some hash code
927 * @param h2 some hash code
928 * @param target some hash code
929 * @return -1 if @a h1 is closer, 1 if @a h2 is closer and 0 if @a h1== @a h2.
932 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
933 const struct GNUNET_HashCode *h2,
934 const struct GNUNET_HashCode *target);
939 * @brief Derive an authentication key
940 * @param key authentication key
941 * @param rkey root key
943 * @param salt_len size of the salt
944 * @param argp pair of void * & size_t for context chunks, terminated by NULL
947 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
948 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
949 const void *salt, size_t salt_len,
955 * @brief Derive an authentication key
956 * @param key authentication key
957 * @param rkey root key
959 * @param salt_len size of the salt
960 * @param ... pair of void * & size_t for context chunks, terminated by NULL
963 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
964 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
965 const void *salt, size_t salt_len,
972 * @param result buffer for the derived key, allocated by caller
973 * @param out_len desired length of the derived key
974 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
975 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
977 * @param xts_len length of @a xts
978 * @param skm source key material
979 * @param skm_len length of @a skm
980 * @param ... pair of void * & size_t for context chunks, terminated by NULL
981 * @return #GNUNET_YES on success
984 GNUNET_CRYPTO_hkdf (void *result,
998 * @param result buffer for the derived key, allocated by caller
999 * @param out_len desired length of the derived key
1000 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
1001 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
1003 * @param xts_len length of @a xts
1004 * @param skm source key material
1005 * @param skm_len length of @a skm
1006 * @param argp va_list of void * & size_t pairs for context chunks
1007 * @return #GNUNET_YES on success
1010 GNUNET_CRYPTO_hkdf_v (void *result,
1023 * @param result buffer for the derived key, allocated by caller
1024 * @param out_len desired length of the derived key
1026 * @param xts_len length of @a xts
1027 * @param skm source key material
1028 * @param skm_len length of @a skm
1029 * @param argp va_list of void * & size_t pairs for context chunks
1030 * @return #GNUNET_YES on success
1033 GNUNET_CRYPTO_kdf_v (void *result,
1043 * Deterministically generate a pseudo-random number uniformly from the
1044 * integers modulo a libgcrypt mpi.
1046 * @param[out] r MPI value set to the FDH
1047 * @param n MPI to work modulo
1049 * @param xts_len length of @a xts
1050 * @param skm source key material
1051 * @param skm_len length of @a skm
1052 * @param ctx context string
1055 GNUNET_CRYPTO_kdf_mod_mpi (gcry_mpi_t *r,
1057 const void *xts, size_t xts_len,
1058 const void *skm, size_t skm_len,
1065 * @param result buffer for the derived key, allocated by caller
1066 * @param out_len desired length of the derived key
1068 * @param xts_len length of @a xts
1069 * @param skm source key material
1070 * @param skm_len length of @a skm
1071 * @param ... void * & size_t pairs for context chunks
1072 * @return #GNUNET_YES on success
1075 GNUNET_CRYPTO_kdf (void *result,
1086 * Extract the public key for the given private key.
1088 * @param priv the private key
1089 * @param pub where to write the public key
1092 GNUNET_CRYPTO_ecdsa_key_get_public (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1093 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1097 * Extract the public key for the given private key.
1099 * @param priv the private key
1100 * @param pub where to write the public key
1103 GNUNET_CRYPTO_eddsa_key_get_public (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1104 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1110 * Extract the public key for the given private key.
1112 * @param priv the private key
1113 * @param pub where to write the public key
1116 GNUNET_CRYPTO_ecdhe_key_get_public (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1117 struct GNUNET_CRYPTO_EcdhePublicKey *pub);
1121 * Convert a public key to a string.
1123 * @param pub key to convert
1124 * @return string representing @a pub
1127 GNUNET_CRYPTO_ecdsa_public_key_to_string (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1131 * Convert a private key to a string.
1133 * @param priv key to convert
1134 * @return string representing @a pub
1137 GNUNET_CRYPTO_eddsa_private_key_to_string (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv);
1141 * Convert a public key to a string.
1143 * @param pub key to convert
1144 * @return string representing @a pub
1147 GNUNET_CRYPTO_eddsa_public_key_to_string (const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1151 * Convert a string representing a public key to a public key.
1153 * @param enc encoded public key
1154 * @param enclen number of bytes in @a enc (without 0-terminator)
1155 * @param pub where to store the public key
1156 * @return #GNUNET_OK on success
1159 GNUNET_CRYPTO_ecdsa_public_key_from_string (const char *enc,
1161 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1165 * Convert a string representing a private key to a private key.
1167 * @param enc encoded public key
1168 * @param enclen number of bytes in @a enc (without 0-terminator)
1169 * @param priv where to store the private key
1170 * @return #GNUNET_OK on success
1173 GNUNET_CRYPTO_eddsa_private_key_from_string (const char *enc,
1175 struct GNUNET_CRYPTO_EddsaPrivateKey *pub);
1179 * Convert a string representing a public key to a public key.
1181 * @param enc encoded public key
1182 * @param enclen number of bytes in @a enc (without 0-terminator)
1183 * @param pub where to store the public key
1184 * @return #GNUNET_OK on success
1187 GNUNET_CRYPTO_eddsa_public_key_from_string (const char *enc,
1189 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1194 * Create a new private key by reading it from a file. If the
1195 * files does not exist, create a new key and write it to the
1196 * file. Caller must free return value. Note that this function
1197 * can not guarantee that another process might not be trying
1198 * the same operation on the same file at the same time.
1199 * If the contents of the file
1200 * are invalid the old file is deleted and a fresh key is
1203 * @param filename name of file to use to store the key
1204 * @return new private key, NULL on error (for example,
1205 * permission denied); free using #GNUNET_free
1207 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1208 GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename);
1213 * Create a new private key by reading it from a file. If the
1214 * files does not exist, create a new key and write it to the
1215 * file. Caller must free return value. Note that this function
1216 * can not guarantee that another process might not be trying
1217 * the same operation on the same file at the same time.
1218 * If the contents of the file
1219 * are invalid the old file is deleted and a fresh key is
1222 * @param filename name of file to use to store the key
1223 * @return new private key, NULL on error (for example,
1224 * permission denied); free using #GNUNET_free
1226 struct GNUNET_CRYPTO_EddsaPrivateKey *
1227 GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename);
1231 * Forward declaration to simplify #include-structure.
1233 struct GNUNET_CONFIGURATION_Handle;
1238 * Create a new private key by reading our peer's key from
1239 * the file specified in the configuration.
1241 * @param cfg the configuration to use
1242 * @return new private key, NULL on error (for example,
1243 * permission denied); free using #GNUNET_free
1245 struct GNUNET_CRYPTO_EddsaPrivateKey *
1246 GNUNET_CRYPTO_eddsa_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
1251 * Create a new private key. Caller must free return value.
1253 * @return fresh private key; free using #GNUNET_free
1255 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1256 GNUNET_CRYPTO_ecdsa_key_create (void);
1261 * Create a new private key. Caller must free return value.
1263 * @return fresh private key; free using #GNUNET_free
1265 struct GNUNET_CRYPTO_EddsaPrivateKey *
1266 GNUNET_CRYPTO_eddsa_key_create (void);
1271 * Create a new private key. Clear with #GNUNET_CRYPTO_ecdhe_key_clear().
1273 * @param[out] pk set to fresh private key;
1274 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1277 GNUNET_CRYPTO_ecdhe_key_create2 (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1282 * Create a new private key. Caller must free return value.
1284 * @return fresh private key; free using #GNUNET_free
1286 struct GNUNET_CRYPTO_EcdhePrivateKey *
1287 GNUNET_CRYPTO_ecdhe_key_create (void);
1292 * Clear memory that was used to store a private key.
1294 * @param pk location of the key
1297 GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk);
1302 * Clear memory that was used to store a private key.
1304 * @param pk location of the key
1307 GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk);
1312 * Clear memory that was used to store a private key.
1314 * @param pk location of the key
1317 GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1322 * Get the shared private key we use for anonymous users.
1324 * @return "anonymous" private key; do not free
1326 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
1327 GNUNET_CRYPTO_ecdsa_key_get_anonymous (void);
1332 * Setup a hostkey file for a peer given the name of the
1333 * configuration file (!). This function is used so that
1334 * at a later point code can be certain that reading a
1335 * hostkey is fast (for example in time-dependent testcases).
1337 * @param cfg_name name of the configuration file to use
1340 GNUNET_CRYPTO_eddsa_setup_hostkey (const char *cfg_name);
1345 * Retrieve the identity of the host's peer.
1347 * @param cfg configuration to use
1348 * @param dst pointer to where to write the peer identity
1349 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
1350 * could not be retrieved
1353 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
1354 struct GNUNET_PeerIdentity *dst);
1358 * Compare two Peer Identities.
1360 * @param first first peer identity
1361 * @param second second peer identity
1362 * @return bigger than 0 if first > second,
1363 * 0 if they are the same
1364 * smaller than 0 if second > first
1367 GNUNET_CRYPTO_cmp_peer_identity (const struct GNUNET_PeerIdentity *first,
1368 const struct GNUNET_PeerIdentity *second);
1372 * Internal structure used to cache pre-calculated values for DLOG calculation.
1374 struct GNUNET_CRYPTO_EccDlogContext;
1378 * Point on a curve (always for Curve25519) encoded in a format suitable
1379 * for network transmission (ECDH), see http://cr.yp.to/ecdh.html.
1381 struct GNUNET_CRYPTO_EccPoint
1384 * Q consists of an x- and a y-value, each mod p (256 bits), given
1385 * here in affine coordinates and Ed25519 standard compact format.
1387 unsigned char q_y[256 / 8];
1392 * Do pre-calculation for ECC discrete logarithm for small factors.
1394 * @param max maximum value the factor can be
1395 * @param mem memory to use (should be smaller than @a max), must not be zero.
1396 * @return NULL on error
1398 struct GNUNET_CRYPTO_EccDlogContext *
1399 GNUNET_CRYPTO_ecc_dlog_prepare (unsigned int max,
1404 * Calculate ECC discrete logarithm for small factors.
1405 * Opposite of #GNUNET_CRYPTO_ecc_dexp().
1407 * @param dlc precalculated values, determine range of factors
1408 * @param input point on the curve to factor
1409 * @return INT_MAX if dlog failed, otherwise the factor
1412 GNUNET_CRYPTO_ecc_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc,
1413 gcry_mpi_point_t input);
1417 * Multiply the generator g of the elliptic curve by @a val
1418 * to obtain the point on the curve representing @a val.
1419 * Afterwards, point addition will correspond to integer
1420 * addition. #GNUNET_CRYPTO_ecc_dlog() can be used to
1421 * convert a point back to an integer (as long as the
1422 * integer is smaller than the MAX of the @a edc context).
1424 * @param edc calculation context for ECC operations
1425 * @param val value to encode into a point
1426 * @return representation of the value as an ECC point,
1427 * must be freed using #GNUNET_CRYPTO_ecc_free()
1430 GNUNET_CRYPTO_ecc_dexp (struct GNUNET_CRYPTO_EccDlogContext *edc,
1435 * Multiply the generator g of the elliptic curve by @a val
1436 * to obtain the point on the curve representing @a val.
1438 * @param edc calculation context for ECC operations
1439 * @param val (positive) value to encode into a point
1440 * @return representation of the value as an ECC point,
1441 * must be freed using #GNUNET_CRYPTO_ecc_free()
1444 GNUNET_CRYPTO_ecc_dexp_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1449 * Multiply the point @a p on the elliptic curve by @a val.
1451 * @param edc calculation context for ECC operations
1452 * @param p point to multiply
1453 * @param val (positive) value to encode into a point
1454 * @return representation of the value as an ECC point,
1455 * must be freed using #GNUNET_CRYPTO_ecc_free()
1458 GNUNET_CRYPTO_ecc_pmul_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1464 * Convert point value to binary representation.
1466 * @param edc calculation context for ECC operations
1467 * @param point computational point representation
1468 * @param[out] bin binary point representation
1471 GNUNET_CRYPTO_ecc_point_to_bin (struct GNUNET_CRYPTO_EccDlogContext *edc,
1472 gcry_mpi_point_t point,
1473 struct GNUNET_CRYPTO_EccPoint *bin);
1477 * Convert binary representation of a point to computational representation.
1479 * @param edc calculation context for ECC operations
1480 * @param bin binary point representation
1481 * @return computational representation
1484 GNUNET_CRYPTO_ecc_bin_to_point (struct GNUNET_CRYPTO_EccDlogContext *edc,
1485 const struct GNUNET_CRYPTO_EccPoint *bin);
1489 * Add two points on the elliptic curve.
1491 * @param edc calculation context for ECC operations
1492 * @param a some value
1493 * @param b some value
1494 * @return @a a + @a b, must be freed using #GNUNET_CRYPTO_ecc_free()
1497 GNUNET_CRYPTO_ecc_add (struct GNUNET_CRYPTO_EccDlogContext *edc,
1499 gcry_mpi_point_t b);
1503 * Obtain a random point on the curve and its
1504 * additive inverse. Both returned values
1505 * must be freed using #GNUNET_CRYPTO_ecc_free().
1507 * @param edc calculation context for ECC operations
1508 * @param[out] r set to a random point on the curve
1509 * @param[out] r_inv set to the additive inverse of @a r
1512 GNUNET_CRYPTO_ecc_rnd (struct GNUNET_CRYPTO_EccDlogContext *edc,
1513 gcry_mpi_point_t *r,
1514 gcry_mpi_point_t *r_inv);
1518 * Obtain a random scalar for point multiplication on the curve and
1519 * its multiplicative inverse.
1521 * @param edc calculation context for ECC operations
1522 * @param[out] r set to a random scalar on the curve
1523 * @param[out] r_inv set to the multiplicative inverse of @a r
1526 GNUNET_CRYPTO_ecc_rnd_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
1532 * Generate a random value mod n.
1534 * @param edc ECC context
1535 * @return random value mod n.
1538 GNUNET_CRYPTO_ecc_random_mod_n (struct GNUNET_CRYPTO_EccDlogContext *edc);
1542 * Free a point value returned by the API.
1544 * @param p point to free
1547 GNUNET_CRYPTO_ecc_free (gcry_mpi_point_t p);
1551 * Release precalculated values.
1553 * @param dlc dlog context
1556 GNUNET_CRYPTO_ecc_dlog_release (struct GNUNET_CRYPTO_EccDlogContext *dlc);
1561 * Derive key material from a public and a private ECC key.
1563 * @param priv private key to use for the ECDH (x)
1564 * @param pub public key to use for the ECDH (yG)
1565 * @param key_material where to write the key material (xyG)
1566 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1569 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1570 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1571 struct GNUNET_HashCode *key_material);
1576 * Derive key material from a ECDH public key and a private EdDSA key.
1577 * Dual to #GNUNET_CRRYPTO_ecdh_eddsa.
1579 * @param priv private key from EdDSA to use for the ECDH (x)
1580 * @param pub public key to use for the ECDH (yG)
1581 * @param key_material where to write the key material H(h(x)yG)
1582 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1585 GNUNET_CRYPTO_eddsa_ecdh (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1586 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1587 struct GNUNET_HashCode *key_material);
1591 * Derive key material from a ECDH public key and a private ECDSA key.
1592 * Dual to #GNUNET_CRRYPTO_ecdh_ecdsa.
1594 * @param priv private key from ECDSA to use for the ECDH (x)
1595 * @param pub public key to use for the ECDH (yG)
1596 * @param key_material where to write the key material H(h(x)yG)
1597 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1600 GNUNET_CRYPTO_ecdsa_ecdh (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1601 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1602 struct GNUNET_HashCode *key_material);
1607 * Derive key material from a EdDSA public key and a private ECDH key.
1608 * Dual to #GNUNET_CRRYPTO_eddsa_ecdh.
1610 * @param priv private key to use for the ECDH (y)
1611 * @param pub public key from EdDSA to use for the ECDH (X=h(x)G)
1612 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1613 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1616 GNUNET_CRYPTO_ecdh_eddsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1617 const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
1618 struct GNUNET_HashCode *key_material);
1622 * Derive key material from a EcDSA public key and a private ECDH key.
1623 * Dual to #GNUNET_CRRYPTO_ecdsa_ecdh.
1625 * @param priv private key to use for the ECDH (y)
1626 * @param pub public key from ECDSA to use for the ECDH (X=h(x)G)
1627 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1628 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1631 GNUNET_CRYPTO_ecdh_ecdsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1632 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1633 struct GNUNET_HashCode *key_material);
1638 * EdDSA sign a given block.
1640 * @param priv private key to use for the signing
1641 * @param purpose what to sign (size, purpose)
1642 * @param sig where to write the signature
1643 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1646 GNUNET_CRYPTO_eddsa_sign (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1647 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1648 struct GNUNET_CRYPTO_EddsaSignature *sig);
1653 * ECDSA Sign a given block.
1655 * @param priv private key to use for the signing
1656 * @param purpose what to sign (size, purpose)
1657 * @param sig where to write the signature
1658 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1661 GNUNET_CRYPTO_ecdsa_sign (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1662 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1663 struct GNUNET_CRYPTO_EcdsaSignature *sig);
1667 * Verify EdDSA signature.
1669 * @param purpose what is the purpose that the signature should have?
1670 * @param validate block to validate (size, purpose, data)
1671 * @param sig signature that is being validated
1672 * @param pub public key of the signer
1673 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1676 GNUNET_CRYPTO_eddsa_verify (uint32_t purpose,
1677 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1678 const struct GNUNET_CRYPTO_EddsaSignature *sig,
1679 const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1685 * Verify ECDSA signature.
1687 * @param purpose what is the purpose that the signature should have?
1688 * @param validate block to validate (size, purpose, data)
1689 * @param sig signature that is being validated
1690 * @param pub public key of the signer
1691 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1694 GNUNET_CRYPTO_ecdsa_verify (uint32_t purpose,
1695 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1696 const struct GNUNET_CRYPTO_EcdsaSignature *sig,
1697 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1702 * Derive a private key from a given private key and a label.
1703 * Essentially calculates a private key 'h = H(l,P) * d mod n'
1704 * where n is the size of the ECC group and P is the public
1705 * key associated with the private key 'd'.
1707 * @param priv original private key
1708 * @param label label to use for key deriviation
1709 * @param context additional context to use for HKDF of 'h';
1710 * typically the name of the subsystem/application
1711 * @return derived private key
1713 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1714 GNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1716 const char *context);
1721 * Derive a public key from a given public key and a label.
1722 * Essentially calculates a public key 'V = H(l,P) * P'.
1724 * @param pub original public key
1725 * @param label label to use for key deriviation
1726 * @param context additional context to use for HKDF of 'h'.
1727 * typically the name of the subsystem/application
1728 * @param result where to write the derived public key
1731 GNUNET_CRYPTO_ecdsa_public_key_derive (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1733 const char *context,
1734 struct GNUNET_CRYPTO_EcdsaPublicKey *result);
1738 * Output the given MPI value to the given buffer in network
1739 * byte order. The MPI @a val may not be negative.
1741 * @param buf where to output to
1742 * @param size number of bytes in @a buf
1743 * @param val value to write to @a buf
1746 GNUNET_CRYPTO_mpi_print_unsigned (void *buf,
1752 * Convert data buffer into MPI value.
1753 * The buffer is interpreted as network
1754 * byte order, unsigned integer.
1756 * @param result where to store MPI value (allocated)
1757 * @param data raw data (GCRYMPI_FMT_USG)
1758 * @param size number of bytes in @a data
1761 GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result,
1767 * Create a freshly generated paillier public key.
1769 * @param[out] public_key Where to store the public key?
1770 * @param[out] private_key Where to store the private key?
1773 GNUNET_CRYPTO_paillier_create (struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1774 struct GNUNET_CRYPTO_PaillierPrivateKey *private_key);
1778 * Encrypt a plaintext with a paillier public key.
1780 * @param public_key Public key to use.
1781 * @param m Plaintext to encrypt.
1782 * @param desired_ops How many homomorphic ops the caller intends to use
1783 * @param[out] ciphertext Encrytion of @a plaintext with @a public_key.
1784 * @return guaranteed number of supported homomorphic operations >= 1,
1785 * or desired_ops, in case that is lower,
1786 * or -1 if less than one homomorphic operation is possible
1789 GNUNET_CRYPTO_paillier_encrypt (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1792 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext);
1796 * Decrypt a paillier ciphertext with a private key.
1798 * @param private_key Private key to use for decryption.
1799 * @param public_key Public key to use for decryption.
1800 * @param ciphertext Ciphertext to decrypt.
1801 * @param[out] m Decryption of @a ciphertext with @private_key.
1804 GNUNET_CRYPTO_paillier_decrypt (const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key,
1805 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1806 const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
1811 * Compute a ciphertext that represents the sum of the plaintext in @a x1 and @a x2
1813 * Note that this operation can only be done a finite number of times
1814 * before an overflow occurs.
1816 * @param public_key Public key to use for encryption.
1817 * @param c1 Paillier cipher text.
1818 * @param c2 Paillier cipher text.
1819 * @param[out] result Result of the homomorphic operation.
1820 * @return #GNUNET_OK if the result could be computed,
1821 * #GNUNET_SYSERR if no more homomorphic operations are remaining.
1824 GNUNET_CRYPTO_paillier_hom_add (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1825 const struct GNUNET_CRYPTO_PaillierCiphertext *c1,
1826 const struct GNUNET_CRYPTO_PaillierCiphertext *c2,
1827 struct GNUNET_CRYPTO_PaillierCiphertext *result);
1831 * Get the number of remaining supported homomorphic operations.
1833 * @param c Paillier cipher text.
1834 * @return the number of remaining homomorphic operations
1837 GNUNET_CRYPTO_paillier_hom_get_remaining (const struct GNUNET_CRYPTO_PaillierCiphertext *c);
1840 /* ********* Chaum-style RSA-based blind signatures ******************* */
1846 * The private information of an RSA key pair.
1848 struct GNUNET_CRYPTO_RsaPrivateKey;
1851 * The public information of an RSA key pair.
1853 struct GNUNET_CRYPTO_RsaPublicKey;
1856 * Constant-size pre-secret for blinding key generation.
1858 struct GNUNET_CRYPTO_RsaBlindingKeySecret
1861 * Bits used to generate the blinding key. 256 bits
1862 * of entropy is enough.
1864 uint32_t pre_secret[8] GNUNET_PACKED;
1868 * @brief an RSA signature
1870 struct GNUNET_CRYPTO_RsaSignature;
1874 * Create a new private key. Caller must free return value.
1876 * @param len length of the key in bits (i.e. 2048)
1877 * @return fresh private key
1879 struct GNUNET_CRYPTO_RsaPrivateKey *
1880 GNUNET_CRYPTO_rsa_private_key_create (unsigned int len);
1884 * Free memory occupied by the private key.
1886 * @param key pointer to the memory to free
1889 GNUNET_CRYPTO_rsa_private_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *key);
1893 * Encode the private key in a format suitable for
1894 * storing it into a file.
1896 * @param key the private key
1897 * @param[out] buffer set to a buffer with the encoded key
1898 * @return size of memory allocatedin @a buffer
1901 GNUNET_CRYPTO_rsa_private_key_encode (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
1906 * Decode the private key from the data-format back
1907 * to the "normal", internal format.
1909 * @param buf the buffer where the private key data is stored
1910 * @param len the length of the data in @a buf
1911 * @return NULL on error
1913 struct GNUNET_CRYPTO_RsaPrivateKey *
1914 GNUNET_CRYPTO_rsa_private_key_decode (const char *buf,
1919 * Duplicate the given private key
1921 * @param key the private key to duplicate
1922 * @return the duplicate key; NULL upon error
1924 struct GNUNET_CRYPTO_RsaPrivateKey *
1925 GNUNET_CRYPTO_rsa_private_key_dup (const struct GNUNET_CRYPTO_RsaPrivateKey *key);
1929 * Extract the public key of the given private key.
1931 * @param priv the private key
1932 * @retur NULL on error, otherwise the public key
1934 struct GNUNET_CRYPTO_RsaPublicKey *
1935 GNUNET_CRYPTO_rsa_private_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey *priv);
1939 * Compute hash over the public key.
1941 * @param key public key to hash
1942 * @param hc where to store the hash code
1945 GNUNET_CRYPTO_rsa_public_key_hash (const struct GNUNET_CRYPTO_RsaPublicKey *key,
1946 struct GNUNET_HashCode *hc);
1950 * Obtain the length of the RSA key in bits.
1952 * @param key the public key to introspect
1953 * @return length of the key in bits
1956 GNUNET_CRYPTO_rsa_public_key_len (const struct GNUNET_CRYPTO_RsaPublicKey *key);
1960 * Free memory occupied by the public key.
1962 * @param key pointer to the memory to free
1965 GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_RsaPublicKey *key);
1969 * Encode the public key in a format suitable for
1970 * storing it into a file.
1972 * @param key the private key
1973 * @param[out] buffer set to a buffer with the encoded key
1974 * @return size of memory allocated in @a buffer
1977 GNUNET_CRYPTO_rsa_public_key_encode (const struct GNUNET_CRYPTO_RsaPublicKey *key,
1982 * Decode the public key from the data-format back
1983 * to the "normal", internal format.
1985 * @param buf the buffer where the public key data is stored
1986 * @param len the length of the data in @a buf
1987 * @return NULL on error
1989 struct GNUNET_CRYPTO_RsaPublicKey *
1990 GNUNET_CRYPTO_rsa_public_key_decode (const char *buf,
1995 * Duplicate the given public key
1997 * @param key the public key to duplicate
1998 * @return the duplicate key; NULL upon error
2000 struct GNUNET_CRYPTO_RsaPublicKey *
2001 GNUNET_CRYPTO_rsa_public_key_dup (const struct GNUNET_CRYPTO_RsaPublicKey *key);
2005 * Compare the values of two signatures.
2007 * @param s1 one signature
2008 * @param s2 the other signature
2009 * @return 0 if the two are equal
2012 GNUNET_CRYPTO_rsa_signature_cmp (struct GNUNET_CRYPTO_RsaSignature *s1,
2013 struct GNUNET_CRYPTO_RsaSignature *s2);
2016 * Compare the values of two private keys.
2018 * @param p1 one private key
2019 * @param p2 the other private key
2020 * @return 0 if the two are equal
2023 GNUNET_CRYPTO_rsa_private_key_cmp (struct GNUNET_CRYPTO_RsaPrivateKey *p1,
2024 struct GNUNET_CRYPTO_RsaPrivateKey *p2);
2028 * Compare the values of two public keys.
2030 * @param p1 one public key
2031 * @param p2 the other public key
2032 * @return 0 if the two are equal
2035 GNUNET_CRYPTO_rsa_public_key_cmp (struct GNUNET_CRYPTO_RsaPublicKey *p1,
2036 struct GNUNET_CRYPTO_RsaPublicKey *p2);
2040 * Blinds the given message with the given blinding key
2042 * @param hash hash of the message to sign
2043 * @param bkey the blinding key
2044 * @param pkey the public key of the signer
2045 * @param[out] buf set to a buffer with the blinded message to be signed
2046 * @param[out] buf_size number of bytes stored in @a buf
2047 * @return #GNUNET_YES if successful, #GNUNET_NO if RSA key is malicious
2050 GNUNET_CRYPTO_rsa_blind (const struct GNUNET_HashCode *hash,
2051 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
2052 struct GNUNET_CRYPTO_RsaPublicKey *pkey,
2058 * Sign a blinded value, which must be a full domain hash of a message.
2060 * @param key private key to use for the signing
2061 * @param msg the (blinded) message to sign
2062 * @param msg_len number of bytes in @a msg to sign
2063 * @return NULL on error, signature on success
2065 struct GNUNET_CRYPTO_RsaSignature *
2066 GNUNET_CRYPTO_rsa_sign_blinded (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2072 * Create and sign a full domain hash of a message.
2074 * @param key private key to use for the signing
2075 * @param hash the hash of the message to sign
2076 * @return NULL on error, including a malicious RSA key, signature on success
2078 struct GNUNET_CRYPTO_RsaSignature *
2079 GNUNET_CRYPTO_rsa_sign_fdh (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
2080 const struct GNUNET_HashCode *hash);
2084 * Free memory occupied by signature.
2086 * @param sig memory to free
2089 GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_RsaSignature *sig);
2093 * Encode the given signature in a format suitable for storing it into a file.
2095 * @param sig the signature
2096 * @param[out] buffer set to a buffer with the encoded key
2097 * @return size of memory allocated in @a buffer
2100 GNUNET_CRYPTO_rsa_signature_encode (const struct GNUNET_CRYPTO_RsaSignature *sig,
2105 * Decode the signature from the data-format back to the "normal", internal
2108 * @param buf the buffer where the public key data is stored
2109 * @param len the length of the data in @a buf
2110 * @return NULL on error
2112 struct GNUNET_CRYPTO_RsaSignature *
2113 GNUNET_CRYPTO_rsa_signature_decode (const char *buf,
2118 * Duplicate the given rsa signature
2120 * @param sig the signature to duplicate
2121 * @return the duplicate key; NULL upon error
2123 struct GNUNET_CRYPTO_RsaSignature *
2124 GNUNET_CRYPTO_rsa_signature_dup (const struct GNUNET_CRYPTO_RsaSignature *sig);
2128 * Unblind a blind-signed signature. The signature should have been generated
2129 * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with
2130 * #GNUNET_CRYPTO_rsa_blind().
2132 * @param sig the signature made on the blinded signature purpose
2133 * @param bks the blinding key secret used to blind the signature purpose
2134 * @param pkey the public key of the signer
2135 * @return unblinded signature on success, NULL if RSA key is bad or malicious.
2137 struct GNUNET_CRYPTO_RsaSignature *
2138 GNUNET_CRYPTO_rsa_unblind (const struct GNUNET_CRYPTO_RsaSignature *sig,
2139 const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
2140 struct GNUNET_CRYPTO_RsaPublicKey *pkey);
2144 * Verify whether the given hash corresponds to the given signature and the
2145 * signature is valid with respect to the given public key.
2147 * @param hash the message to verify to match the @a sig
2148 * @param sig signature that is being validated
2149 * @param public_key public key of the signer
2150 * @returns #GNUNET_YES if ok, #GNUNET_NO if RSA key is malicious, #GNUNET_SYSERR if signature
2153 GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash,
2154 const struct GNUNET_CRYPTO_RsaSignature *sig,
2155 const struct GNUNET_CRYPTO_RsaPublicKey *public_key);
2158 #if 0 /* keep Emacsens' auto-indent happy */
2166 /* ifndef GNUNET_CRYPTO_LIB_H */
2168 /* end of gnunet_crypto_lib.h */