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 */
46 #include "gnunet_common.h"
47 #include "gnunet_scheduler_lib.h"
51 * Maximum length of an ECC signature.
52 * Note: round up to multiple of 8 minus 2 for alignment.
54 #define GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH 126
58 * Desired quality level for cryptographic operations.
60 enum GNUNET_CRYPTO_Quality
63 * No good quality of the operation is needed (i.e.,
64 * random numbers can be pseudo-random).
66 GNUNET_CRYPTO_QUALITY_WEAK,
69 * High-quality operations are desired.
71 GNUNET_CRYPTO_QUALITY_STRONG,
74 * Randomness for IVs etc. is required.
76 GNUNET_CRYPTO_QUALITY_NONCE
81 * @brief length of the sessionkey in bytes (256 BIT sessionkey)
83 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
86 * Length of a hash value
88 #define GNUNET_CRYPTO_HASH_LENGTH (512/8)
91 * @brief 0-terminated ASCII encoding of a struct GNUNET_HashCode.
93 struct GNUNET_CRYPTO_HashAsciiEncoded
95 unsigned char encoding[104];
100 * @brief 0-terminated ASCII encoding of a struct GNUNET_CRYPTO_ShortHashCode.
102 struct GNUNET_CRYPTO_ShortHashAsciiEncoded
104 unsigned char short_encoding[53];
108 GNUNET_NETWORK_STRUCT_BEGIN
112 * @brief header of what an ECC signature signs
113 * this must be followed by "size - 8" bytes of
114 * the actual signed data
116 struct GNUNET_CRYPTO_EccSignaturePurpose
119 * How many bytes does this signature sign?
120 * (including this purpose header); in network
123 uint32_t size GNUNET_PACKED;
126 * What does this signature vouch for? This
127 * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
128 * constant (from gnunet_signatures.h). In
129 * network byte order!
131 uint32_t purpose GNUNET_PACKED;
137 * @brief an ECC signature
139 struct GNUNET_CRYPTO_EccSignature
145 unsigned char r[256 / 8];
150 unsigned char s[256 / 8];
156 * Public ECC key (always for NIST P-521) encoded in a format suitable
157 * for network transmission.
159 struct GNUNET_CRYPTO_EccPublicKey
162 * Q consists of an x- and a y-value, each mod p (256 bits),
163 * given here in affine coordinates.
165 unsigned char q_x[256 / 8];
168 * Q consists of an x- and a y-value, each mod p (256 bits),
169 * given here in affine coordinates.
171 unsigned char q_y[256 / 8];
177 * Private ECC key encoded for transmission.
179 struct GNUNET_CRYPTO_EccPrivateKey
182 * d is a value mod n, where n has at most 256 bits.
184 unsigned char d[256 / 8];
190 * @brief type for session keys
192 struct GNUNET_CRYPTO_AesSessionKey
197 unsigned char key[GNUNET_CRYPTO_AES_KEY_LENGTH];
200 GNUNET_NETWORK_STRUCT_END
203 * @brief IV for sym cipher
205 * NOTE: must be smaller (!) in size than the
206 * struct GNUNET_HashCode.
208 struct GNUNET_CRYPTO_AesInitializationVector
210 unsigned char iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
215 * @brief type for (message) authentication keys
217 struct GNUNET_CRYPTO_AuthKey
219 unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
223 /* **************** Functions and Macros ************* */
227 * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator
230 * @param seed the seed to use
233 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
237 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
239 * @param sum current sum, initially 0
240 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
241 * @param len number of bytes in @a buf, must be multiple of 2
242 * @return updated crc sum (must be subjected to GNUNET_CRYPTO_crc16_finish to get actual crc16)
245 GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len);
249 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
251 * @param sum cummulative sum
252 * @return crc16 value
255 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
260 * Calculate the checksum of a buffer in one step.
262 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
263 * @param len number of bytes in @a buf, must be multiple of 2
264 * @return crc16 value
267 GNUNET_CRYPTO_crc16_n (const void *buf, size_t len);
272 * Compute the CRC32 checksum for the first len
273 * bytes of the buffer.
275 * @param buf the data over which we're taking the CRC
276 * @param len the length of the buffer @a buf in bytes
277 * @return the resulting CRC32 checksum
280 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len);
285 * Produce a random value.
287 * @param mode desired quality of the random number
288 * @param i the upper limit (exclusive) for the random number
289 * @return a random value in the interval [0,@a i) (exclusive).
292 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i);
297 * Random on unsigned 64-bit values.
299 * @param mode desired quality of the random number
300 * @param max value returned will be in range [0,@a max) (exclusive)
301 * @return random 64-bit number
304 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max);
309 * Get an array with a random permutation of the
311 * @param mode #GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used,
312 * #GNUNET_CRYPTO_QUALITY_WEAK or #GNUNET_CRYPTO_QUALITY_NONCE otherwise
313 * @param n the size of the array
314 * @return the permutation array (allocated from heap)
317 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
322 * Create a new random session key.
324 * @param key key to initialize
327 GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key);
332 * Encrypt a block using a symmetric sessionkey.
334 * @param block the block to encrypt
335 * @param len the size of the block
336 * @param sessionkey the key used to encrypt
337 * @param iv the initialization vector to use, use INITVALUE
339 * @return the size of the encrypted block, -1 for errors
342 GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
343 const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
344 const struct GNUNET_CRYPTO_AesInitializationVector
350 * Decrypt a given block using a symmetric sessionkey.
352 * @param block the data to decrypt, encoded as returned by encrypt
353 * @param size how big is the block?
354 * @param sessionkey the key used to decrypt
355 * @param iv the initialization vector to use
356 * @param result address to store the result at
357 * @return -1 on failure, size of decrypted block on success
360 GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
361 const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
362 const struct GNUNET_CRYPTO_AesInitializationVector
368 * @brief Derive an IV
369 * @param iv initialization vector
370 * @param skey session key
371 * @param salt salt for the derivation
372 * @param salt_len size of the salt
373 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
376 GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
377 const struct GNUNET_CRYPTO_AesSessionKey *skey,
378 const void *salt, size_t salt_len, ...);
382 * @brief Derive an IV
383 * @param iv initialization vector
384 * @param skey session key
385 * @param salt salt for the derivation
386 * @param salt_len size of the salt
387 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
390 GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
391 const struct GNUNET_CRYPTO_AesSessionKey *skey,
392 const void *salt, size_t salt_len, va_list argp);
397 * Convert hash to ASCII encoding.
398 * @param block the hash code
399 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
400 * safely cast to char*, a '\\0' termination is set).
403 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode * block,
404 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
409 * Convert short hash to ASCII encoding.
411 * @param block the hash code
412 * @param result where to store the encoding (struct GNUNET_CRYPTO_ShortHashAsciiEncoded can be
413 * safely cast to char*, a '\\0' termination is set).
416 GNUNET_CRYPTO_short_hash_to_enc (const struct GNUNET_CRYPTO_ShortHashCode * block,
417 struct GNUNET_CRYPTO_ShortHashAsciiEncoded *result);
422 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
424 * @param enc the encoding
425 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
426 * @param result where to store the hash code
427 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
430 GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
431 struct GNUNET_HashCode *result);
436 * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
438 * @param enc the encoding
439 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
440 * @param result where to store the hash code
441 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
444 GNUNET_CRYPTO_short_hash_from_string2 (const char *enc, size_t enclen,
445 struct GNUNET_CRYPTO_ShortHashCode * result);
450 * Convert ASCII encoding back to struct GNUNET_HashCode
452 * @param enc the encoding
453 * @param result where to store the hash code
454 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
456 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
457 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
462 * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
464 * @param enc the encoding
465 * @param result where to store the GNUNET_CRYPTO_ShortHash
466 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
468 #define GNUNET_CRYPTO_short_hash_from_string(enc, result) \
469 GNUNET_CRYPTO_short_hash_from_string2 (enc, strlen(enc), result)
474 * Compare function for ShortHashCodes, producing a total ordering
477 * @param h1 some hash code
478 * @param h2 some hash code
479 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
482 GNUNET_CRYPTO_short_hash_cmp (const struct GNUNET_CRYPTO_ShortHashCode * h1,
483 const struct GNUNET_CRYPTO_ShortHashCode * h2);
487 * Compute the distance between 2 hashcodes.
488 * The computation must be fast, not involve
489 * a.a or a.e (they're used elsewhere), and
490 * be somewhat consistent. And of course, the
491 * result should be a positive number.
493 * @param a some hash code
494 * @param b some hash code
495 * @return number between 0 and UINT32_MAX
498 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode * a,
499 const struct GNUNET_HashCode * b);
504 * Compute hash of a given block.
506 * @param block the data to hash
507 * @param size size of the @a block
508 * @param ret pointer to where to write the hashcode
511 GNUNET_CRYPTO_hash (const void *block, size_t size, struct GNUNET_HashCode * ret);
516 * Compute short (256-bit) hash of a given block.
518 * @param block the data to hash
519 * @param size size of the @a block
520 * @param ret pointer to where to write the hashcode
523 GNUNET_CRYPTO_short_hash (const void *block, size_t size,
524 struct GNUNET_CRYPTO_ShortHashCode * ret);
529 * Double short (256-bit) hash to create a long hash.
531 * @param sh short hash to double
532 * @param dh where to store the (doubled) long hash (not really a hash)
535 GNUNET_CRYPTO_short_hash_double (const struct GNUNET_CRYPTO_ShortHashCode *sh,
536 struct GNUNET_HashCode *dh);
541 * Truncate doubled short hash back to a short hash.
543 * @param dh doubled short hash to reduce again
544 * @param sh where to store the short hash
545 * @return GNUNET_OK on success, GNUNET_SYSERR if this was not a
549 GNUNET_CRYPTO_short_hash_from_truncation (const struct GNUNET_HashCode *dh,
550 struct GNUNET_CRYPTO_ShortHashCode *sh);
555 * Calculate HMAC of a message (RFC 2104)
557 * @param key secret key
558 * @param plaintext input plaintext
559 * @param plaintext_len length of @a plaintext
560 * @param hmac where to store the hmac
563 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
564 const void *plaintext, size_t plaintext_len,
565 struct GNUNET_HashCode * hmac);
569 * Function called once the hash computation over the
570 * specified file has completed.
573 * @param res resulting hash, NULL on error
575 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
576 const struct GNUNET_HashCode *
581 * Handle to file hashing operation.
583 struct GNUNET_CRYPTO_FileHashContext;
588 * Compute the hash of an entire file.
590 * @param priority scheduling priority to use
591 * @param filename name of file to hash
592 * @param blocksize number of bytes to process in one task
593 * @param callback function to call upon completion
594 * @param callback_cls closure for callback
595 * @return NULL on (immediate) errror
597 struct GNUNET_CRYPTO_FileHashContext *
598 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
599 const char *filename, size_t blocksize,
600 GNUNET_CRYPTO_HashCompletedCallback callback,
605 * Cancel a file hashing operation.
607 * @param fhc operation to cancel (callback must not yet have been invoked)
610 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
615 * Create a random hash code.
617 * @param mode desired quality level
618 * @param result hash code that is randomized
621 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
622 struct GNUNET_HashCode * result);
627 * compute result(delta) = b - a
629 * @param a some hash code
630 * @param b some hash code
631 * @param result set to @a b - @a a
634 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode * a,
635 const struct GNUNET_HashCode * b,
636 struct GNUNET_HashCode * result);
641 * compute result(b) = a + delta
643 * @param a some hash code
644 * @param delta some hash code
645 * @param result set to @a a + @a delta
648 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode * a,
649 const struct GNUNET_HashCode * delta,
650 struct GNUNET_HashCode * result);
655 * compute result = a ^ b
657 * @param a some hash code
658 * @param b some hash code
659 * @param result set to @a a ^ @a b
662 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode * a, const struct GNUNET_HashCode * b,
663 struct GNUNET_HashCode * result);
668 * Convert a hashcode into a key.
670 * @param hc hash code that serves to generate the key
671 * @param skey set to a valid session key
672 * @param iv set to a valid initialization vector
675 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
676 struct GNUNET_CRYPTO_AesSessionKey *skey,
677 struct GNUNET_CRYPTO_AesInitializationVector
683 * Obtain a bit from a hashcode.
685 * @param code the GNUNET_CRYPTO_hash to index bit-wise
686 * @param bit index into the hashcode, [0...159]
687 * @return Bit \a bit from hashcode \a code, -1 for invalid index
690 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode * code, unsigned int bit);
695 * Determine how many low order bits match in two
696 * struct GNUNET_HashCodes. i.e. - 010011 and 011111 share
697 * the first two lowest order bits, and therefore the
698 * return value is two (NOT XOR distance, nor how many
699 * bits match absolutely!).
701 * @param first the first hashcode
702 * @param second the hashcode to compare first to
703 * @return the number of bits that match
706 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode * first,
707 const struct GNUNET_HashCode * second);
712 * Compare function for HashCodes, producing a total ordering
715 * @param h1 some hash code
716 * @param h2 some hash code
717 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
720 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode * h1, const struct GNUNET_HashCode * h2);
725 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
726 * in the XOR metric (Kademlia).
728 * @param h1 some hash code
729 * @param h2 some hash code
730 * @param target some hash code
731 * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
734 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode * h1,
735 const struct GNUNET_HashCode * h2,
736 const struct GNUNET_HashCode * target);
741 * @brief Derive an authentication key
742 * @param key authentication key
743 * @param rkey root key
745 * @param salt_len size of the salt
746 * @param argp pair of void * & size_t for context chunks, terminated by NULL
749 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
750 const struct GNUNET_CRYPTO_AesSessionKey *rkey,
751 const void *salt, size_t salt_len,
757 * @brief Derive an authentication key
758 * @param key authentication key
759 * @param rkey root key
761 * @param salt_len size of the salt
762 * @param ... pair of void * & size_t for context chunks, terminated by NULL
765 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
766 const struct GNUNET_CRYPTO_AesSessionKey *rkey,
767 const void *salt, size_t salt_len, ...);
773 * @param result buffer for the derived key, allocated by caller
774 * @param out_len desired length of the derived key
775 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
776 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
778 * @param xts_len length of xts
779 * @param skm source key material
780 * @param skm_len length of skm
781 * @param ... pair of void * & size_t for context chunks, terminated by NULL
782 * @return GNUNET_YES on success
785 GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
786 const void *xts, size_t xts_len, const void *skm,
787 size_t skm_len, ...);
793 * @param result buffer for the derived key, allocated by caller
794 * @param out_len desired length of the derived key
795 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
796 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
798 * @param xts_len length of xts
799 * @param skm source key material
800 * @param skm_len length of skm
801 * @param argp va_list of void * & size_t pairs for context chunks
802 * @return GNUNET_YES on success
805 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
806 const void *xts, size_t xts_len, const void *skm,
807 size_t skm_len, va_list argp);
812 * @param result buffer for the derived key, allocated by caller
813 * @param out_len desired length of the derived key
815 * @param xts_len length of xts
816 * @param skm source key material
817 * @param skm_len length of skm
818 * @param argp va_list of void * & size_t pairs for context chunks
819 * @return GNUNET_YES on success
822 GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
823 size_t xts_len, const void *skm, size_t skm_len,
830 * @param result buffer for the derived key, allocated by caller
831 * @param out_len desired length of the derived key
833 * @param xts_len length of xts
834 * @param skm source key material
835 * @param skm_len length of skm
836 * @param ... void * & size_t pairs for context chunks
837 * @return GNUNET_YES on success
840 GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
841 size_t xts_len, const void *skm, size_t skm_len, ...);
845 * Function called upon completion of 'GNUNET_CRYPTO_ecc_key_create_async'.
848 * @param pk NULL on error, otherwise the private key (which must be free'd by the callee)
849 * @param emsg NULL on success, otherwise an error message
851 typedef void (*GNUNET_CRYPTO_EccKeyCallback)(void *cls,
852 struct GNUNET_CRYPTO_EccPrivateKey *pk,
858 * Free memory occupied by ECC key
860 * @param priv pointer to the memory to free
863 GNUNET_CRYPTO_ecc_key_free (struct GNUNET_CRYPTO_EccPrivateKey *priv);
868 * Extract the public key for the given private key.
870 * @param priv the private key
871 * @param pub where to write the public key
874 GNUNET_CRYPTO_ecc_key_get_public (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
875 struct GNUNET_CRYPTO_EccPublicKey *pub);
879 * Convert a public key to a string.
881 * @param pub key to convert
882 * @return string representing 'pub'
885 GNUNET_CRYPTO_ecc_public_key_to_string (const struct GNUNET_CRYPTO_EccPublicKey *pub);
889 * Convert a string representing a public key to a public key.
891 * @param enc encoded public key
892 * @param enclen number of bytes in enc (without 0-terminator)
893 * @param pub where to store the public key
894 * @return GNUNET_OK on success
897 GNUNET_CRYPTO_ecc_public_key_from_string (const char *enc,
899 struct GNUNET_CRYPTO_EccPublicKey *pub);
904 * Create a new private key by reading it from a file. If the
905 * files does not exist, create a new key and write it to the
906 * file. Caller must free return value. Note that this function
907 * can not guarantee that another process might not be trying
908 * the same operation on the same file at the same time.
909 * If the contents of the file
910 * are invalid the old file is deleted and a fresh key is
913 * @param filename name of file to use to store the key
914 * @return new private key, NULL on error (for example,
917 struct GNUNET_CRYPTO_EccPrivateKey *
918 GNUNET_CRYPTO_ecc_key_create_from_file (const char *filename);
923 * Create a new private key by reading our peer's key from
924 * the file specified in the configuration.
926 * @return new private key, NULL on error (for example,
929 struct GNUNET_CRYPTO_EccPrivateKey *
930 GNUNET_CRYPTO_ecc_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
935 * Create a new private key. Caller must free return value.
937 * @return fresh private key
939 struct GNUNET_CRYPTO_EccPrivateKey *
940 GNUNET_CRYPTO_ecc_key_create (void);
945 * Get the shared private key we use for anonymous users.
947 * @return "anonymous" private key
949 const struct GNUNET_CRYPTO_EccPrivateKey *
950 GNUNET_CRYPTO_ecc_key_get_anonymous (void);
955 * Setup a hostkey file for a peer given the name of the
956 * configuration file (!). This function is used so that
957 * at a later point code can be certain that reading a
958 * hostkey is fast (for example in time-dependent testcases).
960 * @param cfg_name name of the configuration file to use
963 GNUNET_CRYPTO_ecc_setup_hostkey (const char *cfg_name);
968 * Retrieve the identity of the host's peer.
970 * @param cfg configuration to use
971 * @param dst pointer to where to write the peer identity
972 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
973 * could not be retrieved
976 GNUNET_CRYPTO_get_host_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
977 struct GNUNET_PeerIdentity *dst);
982 * Derive key material from a public and a private ECC key.
984 * @param priv private key to use for the ECDH (x)
985 * @param pub public key to use for the ECDY (yG)
986 * @param key_material where to write the key material (xyG)
987 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
990 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
991 const struct GNUNET_CRYPTO_EccPublicKey *pub,
992 struct GNUNET_HashCode *key_material);
997 * Sign a given block.
999 * @param priv private key to use for the signing
1000 * @param purpose what to sign (size, purpose)
1001 * @param sig where to write the signature
1002 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1005 GNUNET_CRYPTO_ecc_sign (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
1006 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1007 struct GNUNET_CRYPTO_EccSignature *sig);
1014 * @param purpose what is the purpose that the signature should have?
1015 * @param validate block to validate (size, purpose, data)
1016 * @param sig signature that is being validated
1017 * @param pub public key of the signer
1018 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1021 GNUNET_CRYPTO_ecc_verify (uint32_t purpose,
1022 const struct GNUNET_CRYPTO_EccSignaturePurpose
1024 const struct GNUNET_CRYPTO_EccSignature *sig,
1025 const struct GNUNET_CRYPTO_EccPublicKey *pub);
1030 * Derive a private key from a given private key and a label.
1031 * Essentially calculates a private key 'h = H(l,P) * d mod n'
1032 * where n is the size of the ECC group and P is the public
1033 * key associated with the private key 'd'.
1035 * @param priv original private key
1036 * @param label label to use for key deriviation
1037 * @param context additional context to use for HKDF of 'h';
1038 * typically the name of the subsystem/application
1039 * @return derived private key
1041 struct GNUNET_CRYPTO_EccPrivateKey *
1042 GNUNET_CRYPTO_ecc_key_derive (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
1044 const char *context);
1049 * Derive a public key from a given public key and a label.
1050 * Essentially calculates a public key 'V = H(l,P) * P'.
1052 * @param pub original public key
1053 * @param label label to use for key deriviation
1054 * @param context additional context to use for HKDF of 'h'.
1055 * typically the name of the subsystem/application
1056 * @param result where to write the derived public key
1059 GNUNET_CRYPTO_ecc_public_key_derive (const struct GNUNET_CRYPTO_EccPublicKey *pub,
1061 const char *context,
1062 struct GNUNET_CRYPTO_EccPublicKey *result);
1065 #if 0 /* keep Emacsens' auto-indent happy */
1073 /* ifndef GNUNET_CRYPTO_LIB_H */
1075 /* end of gnunet_crypto_lib.h */