2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file include/gnunet_crypto_lib.h
23 * @brief cryptographic primitives for GNUnet
25 * @author Christian Grothoff
26 * @author Krista Bennett
27 * @author Gerd Knorr <kraxel@bytesex.org>
28 * @author Ioana Patrascu
29 * @author Tzvetan Horozov
31 * @defgroup crypto Cryptographic operations
32 * @defgroup hash Hashing and operations on hashes
35 #ifndef GNUNET_CRYPTO_LIB_H
36 #define GNUNET_CRYPTO_LIB_H
41 #if 0 /* keep Emacsens' auto-indent happy */
47 * @brief A 512-bit hashcode
49 struct GNUNET_HashCode;
52 * The identity of the host (wraps the signing key of the peer).
54 struct GNUNET_PeerIdentity;
56 #include "gnunet_common.h"
57 #include "gnunet_scheduler_lib.h"
61 * @brief A 512-bit hashcode
63 struct GNUNET_HashCode
65 uint32_t bits[512 / 8 / sizeof (uint32_t)]; /* = 16 */
70 * Maximum length of an ECC signature.
71 * Note: round up to multiple of 8 minus 2 for alignment.
73 #define GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH 126
77 * Desired quality level for random numbers.
80 enum GNUNET_CRYPTO_Quality
83 * No good quality of the operation is needed (i.e.,
84 * random numbers can be pseudo-random).
87 GNUNET_CRYPTO_QUALITY_WEAK,
90 * High-quality operations are desired.
93 GNUNET_CRYPTO_QUALITY_STRONG,
96 * Randomness for IVs etc. is required.
99 GNUNET_CRYPTO_QUALITY_NONCE
104 * @brief length of the sessionkey in bytes (256 BIT sessionkey)
106 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
109 * Length of a hash value
111 #define GNUNET_CRYPTO_HASH_LENGTH (512/8)
114 * @brief 0-terminated ASCII encoding of a struct GNUNET_HashCode.
116 struct GNUNET_CRYPTO_HashAsciiEncoded
118 unsigned char encoding[104];
122 GNUNET_NETWORK_STRUCT_BEGIN
126 * @brief header of what an ECC signature signs
127 * this must be followed by "size - 8" bytes of
128 * the actual signed data
130 struct GNUNET_CRYPTO_EccSignaturePurpose
133 * How many bytes does this signature sign?
134 * (including this purpose header); in network
137 uint32_t size GNUNET_PACKED;
140 * What does this signature vouch for? This
141 * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
142 * constant (from gnunet_signatures.h). In
143 * network byte order!
145 uint32_t purpose GNUNET_PACKED;
151 * @brief an ECC signature
153 struct GNUNET_CRYPTO_EccSignature
159 unsigned char r[256 / 8];
164 unsigned char s[256 / 8];
170 * Public ECC key (always for NIST P-521) encoded in a format suitable
171 * for network transmission and signatures (ECDSA/EdDSA).
173 struct GNUNET_CRYPTO_EccPublicSignKey
176 * Q consists of an x- and a y-value, each mod p (256 bits),
177 * given here in affine coordinates.
179 * FIXME: this coordinate will be removed in the future (compressed point!).
181 unsigned char q_x[256 / 8];
184 * Q consists of an x- and a y-value, each mod p (256 bits),
185 * given here in affine coordinates.
187 unsigned char q_y[256 / 8];
194 * The identity of the host (wraps the signing key of the peer).
196 struct GNUNET_PeerIdentity
198 struct GNUNET_CRYPTO_EccPublicSignKey public_key;
203 * Public ECC key (always for NIST P-521) encoded in a format suitable
204 * for network transmission and encryption (ECDH).
206 struct GNUNET_CRYPTO_EccPublicEncryptKey
209 * Q consists of an x- and a y-value, each mod p (256 bits),
210 * given here in affine coordinates.
212 unsigned char q_x[256 / 8];
215 * Q consists of an x- and a y-value, each mod p (256 bits),
216 * given here in affine coordinates.
218 * FIXME: this coordinate will be removed in the future (compressed point!).
220 unsigned char q_y[256 / 8];
226 * Private ECC key encoded for transmission.
228 struct GNUNET_CRYPTO_EccPrivateKey
231 * d is a value mod n, where n has at most 256 bits.
233 unsigned char d[256 / 8];
239 * @brief type for session keys
241 struct GNUNET_CRYPTO_SymmetricSessionKey
244 * Actual key for AES.
246 unsigned char aes_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
249 * Actual key for TwoFish.
251 unsigned char twofish_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
255 GNUNET_NETWORK_STRUCT_END
258 * @brief IV for sym cipher
260 * NOTE: must be smaller (!) in size than the
261 * `struct GNUNET_HashCode`.
263 struct GNUNET_CRYPTO_SymmetricInitializationVector
265 unsigned char aes_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
267 unsigned char twofish_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
272 * @brief type for (message) authentication keys
274 struct GNUNET_CRYPTO_AuthKey
276 unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
280 /* **************** Functions and Macros ************* */
284 * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator
287 * @param seed the seed to use
290 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
294 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
296 * @param sum current sum, initially 0
297 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
298 * @param len number of bytes in @a buf, must be multiple of 2
299 * @return updated crc sum (must be subjected to GNUNET_CRYPTO_crc16_finish to get actual crc16)
302 GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len);
306 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
308 * @param sum cummulative sum
309 * @return crc16 value
312 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
317 * Calculate the checksum of a buffer in one step.
319 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
320 * @param len number of bytes in @a buf, must be multiple of 2
321 * @return crc16 value
324 GNUNET_CRYPTO_crc16_n (const void *buf, size_t len);
329 * Compute the CRC32 checksum for the first len
330 * bytes of the buffer.
332 * @param buf the data over which we're taking the CRC
333 * @param len the length of the buffer @a buf in bytes
334 * @return the resulting CRC32 checksum
337 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len);
342 * Produce a random value.
344 * @param mode desired quality of the random number
345 * @param i the upper limit (exclusive) for the random number
346 * @return a random value in the interval [0,@a i) (exclusive).
349 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i);
354 * Random on unsigned 64-bit values.
356 * @param mode desired quality of the random number
357 * @param max value returned will be in range [0,@a max) (exclusive)
358 * @return random 64-bit number
361 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max);
366 * Get an array with a random permutation of the
368 * @param mode #GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used,
369 * #GNUNET_CRYPTO_QUALITY_WEAK or #GNUNET_CRYPTO_QUALITY_NONCE otherwise
370 * @param n the size of the array
371 * @return the permutation array (allocated from heap)
374 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
379 * Create a new random session key.
381 * @param key key to initialize
384 GNUNET_CRYPTO_symmetric_create_session_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key);
389 * Encrypt a block using a symmetric sessionkey.
391 * @param block the block to encrypt
392 * @param len the size of the block
393 * @param sessionkey the key used to encrypt
394 * @param iv the initialization vector to use, use INITVALUE
396 * @return the size of the encrypted block, -1 for errors
399 GNUNET_CRYPTO_symmetric_encrypt (const void *block, size_t len,
400 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
401 const struct GNUNET_CRYPTO_SymmetricInitializationVector
407 * Decrypt a given block using a symmetric sessionkey.
409 * @param block the data to decrypt, encoded as returned by encrypt
410 * @param size how big is the block?
411 * @param sessionkey the key used to decrypt
412 * @param iv the initialization vector to use
413 * @param result address to store the result at
414 * @return -1 on failure, size of decrypted block on success
417 GNUNET_CRYPTO_symmetric_decrypt (const void *block, size_t size,
418 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
419 const struct GNUNET_CRYPTO_SymmetricInitializationVector
425 * @brief Derive an IV
426 * @param iv initialization vector
427 * @param skey session key
428 * @param salt salt for the derivation
429 * @param salt_len size of the salt
430 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
433 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
434 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
435 const void *salt, size_t salt_len, ...);
439 * @brief Derive an IV
440 * @param iv initialization vector
441 * @param skey session key
442 * @param salt salt for the derivation
443 * @param salt_len size of the salt
444 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
447 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
448 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
449 const void *salt, size_t salt_len, va_list argp);
454 * Convert hash to ASCII encoding.
455 * @param block the hash code
456 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
457 * safely cast to char*, a '\\0' termination is set).
460 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode * block,
461 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
466 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
468 * @param enc the encoding
469 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
470 * @param result where to store the hash code
471 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
474 GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
475 struct GNUNET_HashCode *result);
480 * Convert ASCII encoding back to `struct GNUNET_HashCode`
482 * @param enc the encoding
483 * @param result where to store the hash code
484 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
486 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
487 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
493 * Compute the distance between 2 hashcodes. The
494 * computation must be fast, not involve a[0] or a[4] (they're used
495 * elsewhere), and be somewhat consistent. And of course, the result
496 * should be a positive number.
498 * @param a some hash code
499 * @param b some hash code
500 * @return number between 0 and UINT32_MAX
503 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode * a,
504 const struct GNUNET_HashCode * b);
509 * Compute hash of a given block.
511 * @param block the data to hash
512 * @param size size of the @a block
513 * @param ret pointer to where to write the hashcode
516 GNUNET_CRYPTO_hash (const void *block, size_t size, struct GNUNET_HashCode * ret);
521 * Calculate HMAC of a message (RFC 2104)
523 * @param key secret key
524 * @param plaintext input plaintext
525 * @param plaintext_len length of @a plaintext
526 * @param hmac where to store the hmac
529 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
530 const void *plaintext, size_t plaintext_len,
531 struct GNUNET_HashCode * hmac);
535 * Function called once the hash computation over the
536 * specified file has completed.
539 * @param res resulting hash, NULL on error
541 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
542 const struct GNUNET_HashCode *
547 * Handle to file hashing operation.
549 struct GNUNET_CRYPTO_FileHashContext;
554 * Compute the hash of an entire file.
556 * @param priority scheduling priority to use
557 * @param filename name of file to hash
558 * @param blocksize number of bytes to process in one task
559 * @param callback function to call upon completion
560 * @param callback_cls closure for @a callback
561 * @return NULL on (immediate) errror
563 struct GNUNET_CRYPTO_FileHashContext *
564 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
565 const char *filename, size_t blocksize,
566 GNUNET_CRYPTO_HashCompletedCallback callback,
571 * Cancel a file hashing operation.
573 * @param fhc operation to cancel (callback must not yet have been invoked)
576 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
581 * Create a random hash code.
583 * @param mode desired quality level
584 * @param result hash code that is randomized
587 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
588 struct GNUNET_HashCode *result);
593 * compute result(delta) = b - a
595 * @param a some hash code
596 * @param b some hash code
597 * @param result set to @a b - @a a
600 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode * a,
601 const struct GNUNET_HashCode * b,
602 struct GNUNET_HashCode * result);
607 * compute result(b) = a + delta
609 * @param a some hash code
610 * @param delta some hash code
611 * @param result set to @a a + @a delta
614 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode * a,
615 const struct GNUNET_HashCode * delta,
616 struct GNUNET_HashCode * result);
621 * compute result = a ^ b
623 * @param a some hash code
624 * @param b some hash code
625 * @param result set to @a a ^ @a b
628 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode * a, const struct GNUNET_HashCode * b,
629 struct GNUNET_HashCode * result);
634 * Convert a hashcode into a key.
636 * @param hc hash code that serves to generate the key
637 * @param skey set to a valid session key
638 * @param iv set to a valid initialization vector
641 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
642 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
643 struct GNUNET_CRYPTO_SymmetricInitializationVector
649 * Obtain a bit from a hashcode.
651 * @param code the `struct GNUNET_HashCode` to index bit-wise
652 * @param bit index into the hashcode, [0...159]
653 * @return Bit \a bit from hashcode \a code, -1 for invalid index
656 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code,
662 * Determine how many low order bits match in two
663 * struct GNUNET_HashCodes. i.e. - 010011 and 011111 share
664 * the first two lowest order bits, and therefore the
665 * return value is two (NOT XOR distance, nor how many
666 * bits match absolutely!).
668 * @param first the first hashcode
669 * @param second the hashcode to compare first to
670 * @return the number of bits that match
673 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode * first,
674 const struct GNUNET_HashCode * second);
679 * Compare function for HashCodes, producing a total ordering
682 * @param h1 some hash code
683 * @param h2 some hash code
684 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
687 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode * h1, const struct GNUNET_HashCode * h2);
692 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
693 * in the XOR metric (Kademlia).
695 * @param h1 some hash code
696 * @param h2 some hash code
697 * @param target some hash code
698 * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
701 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode * h1,
702 const struct GNUNET_HashCode * h2,
703 const struct GNUNET_HashCode * target);
708 * @brief Derive an authentication key
709 * @param key authentication key
710 * @param rkey root key
712 * @param salt_len size of the salt
713 * @param argp pair of void * & size_t for context chunks, terminated by NULL
716 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
717 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
718 const void *salt, size_t salt_len,
724 * @brief Derive an authentication key
725 * @param key authentication key
726 * @param rkey root key
728 * @param salt_len size of the salt
729 * @param ... pair of void * & size_t for context chunks, terminated by NULL
732 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
733 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
734 const void *salt, size_t salt_len, ...);
740 * @param result buffer for the derived key, allocated by caller
741 * @param out_len desired length of the derived key
742 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
743 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
745 * @param xts_len length of xts
746 * @param skm source key material
747 * @param skm_len length of skm
748 * @param ... pair of void * & size_t for context chunks, terminated by NULL
749 * @return GNUNET_YES on success
752 GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
753 const void *xts, size_t xts_len, const void *skm,
754 size_t skm_len, ...);
760 * @param result buffer for the derived key, allocated by caller
761 * @param out_len desired length of the derived key
762 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
763 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
765 * @param xts_len length of xts
766 * @param skm source key material
767 * @param skm_len length of skm
768 * @param argp va_list of void * & size_t pairs for context chunks
769 * @return GNUNET_YES on success
772 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
773 const void *xts, size_t xts_len, const void *skm,
774 size_t skm_len, va_list argp);
779 * @param result buffer for the derived key, allocated by caller
780 * @param out_len desired length of the derived key
782 * @param xts_len length of xts
783 * @param skm source key material
784 * @param skm_len length of skm
785 * @param argp va_list of void * & size_t pairs for context chunks
786 * @return GNUNET_YES on success
789 GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
790 size_t xts_len, const void *skm, size_t skm_len,
797 * @param result buffer for the derived key, allocated by caller
798 * @param out_len desired length of the derived key
800 * @param xts_len length of xts
801 * @param skm source key material
802 * @param skm_len length of skm
803 * @param ... void * & size_t pairs for context chunks
804 * @return #GNUNET_YES on success
807 GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
808 size_t xts_len, const void *skm, size_t skm_len, ...);
812 * Function called upon completion of 'GNUNET_CRYPTO_ecc_key_create_async'.
815 * @param pk NULL on error, otherwise the private key (which must be free'd by the callee)
816 * @param emsg NULL on success, otherwise an error message
818 typedef void (*GNUNET_CRYPTO_EccKeyCallback)(void *cls,
819 struct GNUNET_CRYPTO_EccPrivateKey *pk,
825 * Extract the public key for the given private key.
827 * @param priv the private key
828 * @param pub where to write the public key
831 GNUNET_CRYPTO_ecc_key_get_public_for_signature (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
832 struct GNUNET_CRYPTO_EccPublicSignKey *pub);
838 * Extract the public key for the given private key.
840 * @param priv the private key
841 * @param pub where to write the public key
844 GNUNET_CRYPTO_ecc_key_get_public_for_encryption (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
845 struct GNUNET_CRYPTO_EccPublicEncryptKey *pub);
849 * Convert a public key to a string.
851 * @param pub key to convert
852 * @return string representing @a pub
855 GNUNET_CRYPTO_ecc_public_sign_key_to_string (const struct GNUNET_CRYPTO_EccPublicSignKey *pub);
859 * Convert a string representing a public key to a public key.
861 * @param enc encoded public key
862 * @param enclen number of bytes in @a enc (without 0-terminator)
863 * @param pub where to store the public key
864 * @return #GNUNET_OK on success
867 GNUNET_CRYPTO_ecc_public_sign_key_from_string (const char *enc,
869 struct GNUNET_CRYPTO_EccPublicSignKey *pub);
874 * Convert a public key to a string.
876 * @param pub key to convert
877 * @return string representing @a pub
880 GNUNET_CRYPTO_ecc_public_encrypt_key_to_string (const struct GNUNET_CRYPTO_EccPublicEncryptKey *pub);
884 * Convert a string representing a public key to a public key.
886 * @param enc encoded public key
887 * @param enclen number of bytes in @a enc (without 0-terminator)
888 * @param pub where to store the public key
889 * @return #GNUNET_OK on success
892 GNUNET_CRYPTO_ecc_public_encrypt_key_from_string (const char *enc,
894 struct GNUNET_CRYPTO_EccPublicEncryptKey *pub);
899 * Create a new private key by reading it from a file. If the
900 * files does not exist, create a new key and write it to the
901 * file. Caller must free return value. Note that this function
902 * can not guarantee that another process might not be trying
903 * the same operation on the same file at the same time.
904 * If the contents of the file
905 * are invalid the old file is deleted and a fresh key is
908 * @param filename name of file to use to store the key
909 * @return new private key, NULL on error (for example,
910 * permission denied); free using #GNUNET_free
912 struct GNUNET_CRYPTO_EccPrivateKey *
913 GNUNET_CRYPTO_ecc_key_create_from_file (const char *filename);
918 * Create a new private key by reading our peer's key from
919 * the file specified in the configuration.
921 * @param cfg the configuration to use
922 * @return new private key, NULL on error (for example,
923 * permission denied); free using #GNUNET_free
925 struct GNUNET_CRYPTO_EccPrivateKey *
926 GNUNET_CRYPTO_ecc_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
931 * Create a new private key. Caller must free return value.
933 * @return fresh private key; free using #GNUNET_free
935 struct GNUNET_CRYPTO_EccPrivateKey *
936 GNUNET_CRYPTO_ecc_key_create (void);
941 * Clear memory that was used to store a private key.
943 * @param pk location of the key
946 GNUNET_CRYPTO_ecc_key_clear (struct GNUNET_CRYPTO_EccPrivateKey *pk);
951 * Get the shared private key we use for anonymous users.
953 * @return "anonymous" private key; do not free
955 const struct GNUNET_CRYPTO_EccPrivateKey *
956 GNUNET_CRYPTO_ecc_key_get_anonymous (void);
961 * Setup a hostkey file for a peer given the name of the
962 * configuration file (!). This function is used so that
963 * at a later point code can be certain that reading a
964 * hostkey is fast (for example in time-dependent testcases).
966 * @param cfg_name name of the configuration file to use
969 GNUNET_CRYPTO_ecc_setup_hostkey (const char *cfg_name);
974 * Retrieve the identity of the host's peer.
976 * @param cfg configuration to use
977 * @param dst pointer to where to write the peer identity
978 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
979 * could not be retrieved
982 GNUNET_CRYPTO_get_host_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
983 struct GNUNET_PeerIdentity *dst);
988 * Derive key material from a public and a private ECC key.
990 * @param priv private key to use for the ECDH (x)
991 * @param pub public key to use for the ECDY (yG)
992 * @param key_material where to write the key material (xyG)
993 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
996 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
997 const struct GNUNET_CRYPTO_EccPublicEncryptKey *pub,
998 struct GNUNET_HashCode *key_material);
1003 * Sign a given block.
1005 * @param priv private key to use for the signing
1006 * @param purpose what to sign (size, purpose)
1007 * @param sig where to write the signature
1008 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1011 GNUNET_CRYPTO_ecc_sign (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
1012 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1013 struct GNUNET_CRYPTO_EccSignature *sig);
1020 * @param purpose what is the purpose that the signature should have?
1021 * @param validate block to validate (size, purpose, data)
1022 * @param sig signature that is being validated
1023 * @param pub public key of the signer
1024 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1027 GNUNET_CRYPTO_ecc_verify (uint32_t purpose,
1028 const struct GNUNET_CRYPTO_EccSignaturePurpose
1030 const struct GNUNET_CRYPTO_EccSignature *sig,
1031 const struct GNUNET_CRYPTO_EccPublicSignKey *pub);
1036 * Derive a private key from a given private key and a label.
1037 * Essentially calculates a private key 'h = H(l,P) * d mod n'
1038 * where n is the size of the ECC group and P is the public
1039 * key associated with the private key 'd'.
1041 * @param priv original private key
1042 * @param label label to use for key deriviation
1043 * @param context additional context to use for HKDF of 'h';
1044 * typically the name of the subsystem/application
1045 * @return derived private key
1047 struct GNUNET_CRYPTO_EccPrivateKey *
1048 GNUNET_CRYPTO_ecc_key_derive (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
1050 const char *context);
1055 * Derive a public key from a given public key and a label.
1056 * Essentially calculates a public key 'V = H(l,P) * P'.
1058 * @param pub original public key
1059 * @param label label to use for key deriviation
1060 * @param context additional context to use for HKDF of 'h'.
1061 * typically the name of the subsystem/application
1062 * @param result where to write the derived public key
1065 GNUNET_CRYPTO_ecc_public_key_derive (const struct GNUNET_CRYPTO_EccPublicSignKey *pub,
1067 const char *context,
1068 struct GNUNET_CRYPTO_EccPublicSignKey *result);
1071 #if 0 /* keep Emacsens' auto-indent happy */
1079 /* ifndef GNUNET_CRYPTO_LIB_H */
1081 /* end of gnunet_crypto_lib.h */