2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2012 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 2, 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
32 #ifndef GNUNET_CRYPTO_LIB_H
33 #define GNUNET_CRYPTO_LIB_H
38 #if 0 /* keep Emacsens' auto-indent happy */
43 #include "gnunet_common.h"
44 #include "gnunet_scheduler_lib.h"
48 * Maximum length of an ECC signature.
49 * Note: round up to multiple of 8 minus 2 for alignment.
51 #define GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH 126
55 * Desired quality level for cryptographic operations.
57 enum GNUNET_CRYPTO_Quality
60 * No good quality of the operation is needed (i.e.,
61 * random numbers can be pseudo-random).
63 GNUNET_CRYPTO_QUALITY_WEAK,
66 * High-quality operations are desired.
68 GNUNET_CRYPTO_QUALITY_STRONG,
71 * Randomness for IVs etc. is required.
73 GNUNET_CRYPTO_QUALITY_NONCE
78 * @brief length of the sessionkey in bytes (256 BIT sessionkey)
80 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
83 * Length of a hash value
85 #define GNUNET_CRYPTO_HASH_LENGTH (512/8)
88 * @brief 0-terminated ASCII encoding of a struct GNUNET_HashCode.
90 struct GNUNET_CRYPTO_HashAsciiEncoded
92 unsigned char encoding[104];
97 * @brief 0-terminated ASCII encoding of a struct GNUNET_CRYPTO_ShortHashCode.
99 struct GNUNET_CRYPTO_ShortHashAsciiEncoded
101 unsigned char short_encoding[53];
105 GNUNET_NETWORK_STRUCT_BEGIN
109 * @brief header of what an ECC signature signs
110 * this must be followed by "size - 8" bytes of
111 * the actual signed data
113 struct GNUNET_CRYPTO_EccSignaturePurpose
116 * How many bytes does this signature sign?
117 * (including this purpose header); in network
120 uint32_t size GNUNET_PACKED;
123 * What does this signature vouch for? This
124 * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
125 * constant (from gnunet_signatures.h). In
126 * network byte order!
128 uint32_t purpose GNUNET_PACKED;
134 * @brief an ECC signature
136 struct GNUNET_CRYPTO_EccSignature
142 unsigned char r[256 / 8];
147 unsigned char s[256 / 8];
153 * Public ECC key (always for NIST P-521) encoded in a format suitable
154 * for network transmission.
156 struct GNUNET_CRYPTO_EccPublicKey
159 * Q consists of an x- and a y-value, each mod p (256 bits),
160 * given here in affine coordinates.
162 unsigned char q_x[256 / 8];
165 * Q consists of an x- and a y-value, each mod p (256 bits),
166 * given here in affine coordinates.
168 unsigned char q_y[256 / 8];
174 * Private ECC key encoded for transmission.
176 struct GNUNET_CRYPTO_EccPrivateKey
179 * d is a value mod n, where n has at most 256 bits.
181 unsigned char d[256 / 8];
187 * @brief type for session keys
189 struct GNUNET_CRYPTO_AesSessionKey
194 unsigned char key[GNUNET_CRYPTO_AES_KEY_LENGTH];
197 GNUNET_NETWORK_STRUCT_END
200 * @brief IV for sym cipher
202 * NOTE: must be smaller (!) in size than the
203 * struct GNUNET_HashCode.
205 struct GNUNET_CRYPTO_AesInitializationVector
207 unsigned char iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
212 * @brief type for (message) authentication keys
214 struct GNUNET_CRYPTO_AuthKey
216 unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
220 /* **************** Functions and Macros ************* */
223 * Seed a weak random generator. Only GNUNET_CRYPTO_QUALITY_WEAK-mode generator
226 * @param seed the seed to use
229 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
233 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
235 * @param sum current sum, initially 0
236 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
237 * @param len number of bytes in hdr, must be multiple of 2
238 * @return updated crc sum (must be subjected to GNUNET_CRYPTO_crc16_finish to get actual crc16)
241 GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len);
245 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
247 * @param sum cummulative sum
248 * @return crc16 value
251 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
255 * Calculate the checksum of a buffer in one step.
257 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
258 * @param len number of bytes in hdr, must be multiple of 2
259 * @return crc16 value
262 GNUNET_CRYPTO_crc16_n (const void *buf, size_t len);
266 * Compute the CRC32 checksum for the first len
267 * bytes of the buffer.
269 * @param buf the data over which we're taking the CRC
270 * @param len the length of the buffer in bytes
271 * @return the resulting CRC32 checksum
274 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len);
278 * Produce a random value.
280 * @param mode desired quality of the random number
281 * @param i the upper limit (exclusive) for the random number
282 * @return a random value in the interval [0,i) (exclusive).
285 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i);
289 * Random on unsigned 64-bit values.
291 * @param mode desired quality of the random number
292 * @param max value returned will be in range [0,max) (exclusive)
293 * @return random 64-bit number
296 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max);
300 * Get an array with a random permutation of the
302 * @param mode GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used, GNUNET_CRYPTO_QUALITY_WEAK otherwise
303 * @param n the size of the array
304 * @return the permutation array (allocated from heap)
307 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
311 * Create a new Session key.
313 * @param key key to initialize
316 GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key);
320 * Encrypt a block with the public key of another
321 * host that uses the same cyper.
323 * @param block the block to encrypt
324 * @param len the size of the block
325 * @param sessionkey the key used to encrypt
326 * @param iv the initialization vector to use, use INITVALUE
328 * @return the size of the encrypted block, -1 for errors
331 GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
332 const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
333 const struct GNUNET_CRYPTO_AesInitializationVector
338 * Decrypt a given block with the sessionkey.
340 * @param block the data to decrypt, encoded as returned by encrypt
341 * @param size how big is the block?
342 * @param sessionkey the key used to decrypt
343 * @param iv the initialization vector to use
344 * @param result address to store the result at
345 * @return -1 on failure, size of decrypted block on success
348 GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
349 const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
350 const struct GNUNET_CRYPTO_AesInitializationVector
355 * @brief Derive an IV
356 * @param iv initialization vector
357 * @param skey session key
358 * @param salt salt for the derivation
359 * @param salt_len size of the salt
360 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
363 GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
364 const struct GNUNET_CRYPTO_AesSessionKey *skey,
365 const void *salt, size_t salt_len, ...);
369 * @brief Derive an IV
370 * @param iv initialization vector
371 * @param skey session key
372 * @param salt salt for the derivation
373 * @param salt_len size of the salt
374 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
377 GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
378 const struct GNUNET_CRYPTO_AesSessionKey *skey,
379 const void *salt, size_t salt_len, va_list argp);
383 * Convert hash to ASCII encoding.
384 * @param block the hash code
385 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
386 * safely cast to char*, a '\\0' termination is set).
389 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode * block,
390 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
394 * Convert short hash to ASCII encoding.
396 * @param block the hash code
397 * @param result where to store the encoding (struct GNUNET_CRYPTO_ShortHashAsciiEncoded can be
398 * safely cast to char*, a '\\0' termination is set).
401 GNUNET_CRYPTO_short_hash_to_enc (const struct GNUNET_CRYPTO_ShortHashCode * block,
402 struct GNUNET_CRYPTO_ShortHashAsciiEncoded *result);
406 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
408 * @param enc the encoding
409 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
410 * @param result where to store the GNUNET_CRYPTO_hash code
411 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
414 GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
415 struct GNUNET_HashCode * result);
419 * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
421 * @param enc the encoding
422 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
423 * @param result where to store the GNUNET_CRYPTO_hash code
424 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
427 GNUNET_CRYPTO_short_hash_from_string2 (const char *enc, size_t enclen,
428 struct GNUNET_CRYPTO_ShortHashCode * result);
432 * Convert ASCII encoding back to struct GNUNET_HashCode
434 * @param enc the encoding
435 * @param result where to store the hash code
436 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
438 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
439 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
443 * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
445 * @param enc the encoding
446 * @param result where to store the GNUNET_CRYPTO_ShortHash
447 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
449 #define GNUNET_CRYPTO_short_hash_from_string(enc, result) \
450 GNUNET_CRYPTO_short_hash_from_string2 (enc, strlen(enc), result)
454 * Compare function for ShortHashCodes, producing a total ordering
457 * @param h1 some hash code
458 * @param h2 some hash code
459 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
462 GNUNET_CRYPTO_short_hash_cmp (const struct GNUNET_CRYPTO_ShortHashCode * h1,
463 const struct GNUNET_CRYPTO_ShortHashCode * h2);
466 * Compute the distance between 2 hashcodes.
467 * The computation must be fast, not involve
468 * a.a or a.e (they're used elsewhere), and
469 * be somewhat consistent. And of course, the
470 * result should be a positive number.
472 * @param a some hash code
473 * @param b some hash code
474 * @return number between 0 and UINT32_MAX
477 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode * a,
478 const struct GNUNET_HashCode * b);
482 * Compute hash of a given block.
484 * @param block the data to hash
485 * @param size size of the block
486 * @param ret pointer to where to write the hashcode
489 GNUNET_CRYPTO_hash (const void *block, size_t size, struct GNUNET_HashCode * ret);
493 * Compute short (256-bit) hash of a given block.
495 * @param block the data to hash
496 * @param size size of the block
497 * @param ret pointer to where to write the hashcode
500 GNUNET_CRYPTO_short_hash (const void *block, size_t size,
501 struct GNUNET_CRYPTO_ShortHashCode * ret);
505 * Double short (256-bit) hash to create a long hash.
507 * @param sh short hash to double
508 * @param dh where to store the (doubled) long hash (not really a hash)
511 GNUNET_CRYPTO_short_hash_double (const struct GNUNET_CRYPTO_ShortHashCode *sh,
512 struct GNUNET_HashCode *dh);
516 * Truncate doubled short hash back to a short hash.
518 * @param dh doubled short hash to reduce again
519 * @param sh where to store the short hash
520 * @return GNUNET_OK on success, GNUNET_SYSERR if this was not a
524 GNUNET_CRYPTO_short_hash_from_truncation (const struct GNUNET_HashCode *dh,
525 struct GNUNET_CRYPTO_ShortHashCode *sh);
529 * Calculate HMAC of a message (RFC 2104)
531 * @param key secret key
532 * @param plaintext input plaintext
533 * @param plaintext_len length of plaintext
534 * @param hmac where to store the hmac
537 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
538 const void *plaintext, size_t plaintext_len,
539 struct GNUNET_HashCode * hmac);
543 * Function called once the hash computation over the
544 * specified file has completed.
547 * @param res resulting hash, NULL on error
549 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
550 const struct GNUNET_HashCode *
555 * Handle to file hashing operation.
557 struct GNUNET_CRYPTO_FileHashContext;
560 * Compute the hash of an entire file.
562 * @param priority scheduling priority to use
563 * @param filename name of file to hash
564 * @param blocksize number of bytes to process in one task
565 * @param callback function to call upon completion
566 * @param callback_cls closure for callback
567 * @return NULL on (immediate) errror
569 struct GNUNET_CRYPTO_FileHashContext *
570 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
571 const char *filename, size_t blocksize,
572 GNUNET_CRYPTO_HashCompletedCallback callback,
577 * Cancel a file hashing operation.
579 * @param fhc operation to cancel (callback must not yet have been invoked)
582 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
586 * Create a random hash code.
588 * @param mode desired quality level
589 * @param result hash code that is randomized
592 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
593 struct GNUNET_HashCode * result);
597 * compute result(delta) = b - a
599 * @param a some hash code
600 * @param b some hash code
601 * @param result set to b - a
604 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode * a,
605 const struct GNUNET_HashCode * b,
606 struct GNUNET_HashCode * result);
610 * compute result(b) = a + delta
612 * @param a some hash code
613 * @param delta some hash code
614 * @param result set to a + delta
617 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode * a,
618 const struct GNUNET_HashCode * delta,
619 struct GNUNET_HashCode * result);
623 * compute result = a ^ b
625 * @param a some hash code
626 * @param b some hash code
627 * @param result set to a ^ b
630 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode * a, const struct GNUNET_HashCode * b,
631 struct GNUNET_HashCode * result);
635 * Convert a hashcode into a key.
637 * @param hc hash code that serves to generate the key
638 * @param skey set to a valid session key
639 * @param iv set to a valid initialization vector
642 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
643 struct GNUNET_CRYPTO_AesSessionKey *skey,
644 struct GNUNET_CRYPTO_AesInitializationVector
649 * Obtain a bit from a hashcode.
651 * @param code the GNUNET_CRYPTO_hash 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, unsigned int bit);
659 * Determine how many low order bits match in two
660 * struct GNUNET_HashCodes. i.e. - 010011 and 011111 share
661 * the first two lowest order bits, and therefore the
662 * return value is two (NOT XOR distance, nor how many
663 * bits match absolutely!).
665 * @param first the first hashcode
666 * @param second the hashcode to compare first to
668 * @return the number of bits that match
671 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode * first,
672 const struct GNUNET_HashCode * second);
676 * Compare function for HashCodes, producing a total ordering
679 * @param h1 some hash code
680 * @param h2 some hash code
681 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
684 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode * h1, const struct GNUNET_HashCode * h2);
688 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
689 * in the XOR metric (Kademlia).
691 * @param h1 some hash code
692 * @param h2 some hash code
693 * @param target some hash code
694 * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
697 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode * h1,
698 const struct GNUNET_HashCode * h2,
699 const struct GNUNET_HashCode * target);
703 * @brief Derive an authentication key
704 * @param key authentication key
705 * @param rkey root key
707 * @param salt_len size of the salt
708 * @param argp pair of void * & size_t for context chunks, terminated by NULL
711 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
712 const struct GNUNET_CRYPTO_AesSessionKey *rkey,
713 const void *salt, size_t salt_len,
718 * @brief Derive an authentication key
719 * @param key authentication key
720 * @param rkey root key
722 * @param salt_len size of the salt
723 * @param ... pair of void * & size_t for context chunks, terminated by NULL
726 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
727 const struct GNUNET_CRYPTO_AesSessionKey *rkey,
728 const void *salt, size_t salt_len, ...);
732 * @param result buffer for the derived key, allocated by caller
733 * @param out_len desired length of the derived key
734 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
735 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
737 * @param xts_len length of xts
738 * @param skm source key material
739 * @param skm_len length of skm
740 * @param ... pair of void * & size_t for context chunks, terminated by NULL
741 * @return GNUNET_YES on success
744 GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
745 const void *xts, size_t xts_len, const void *skm,
746 size_t skm_len, ...);
751 * @param result buffer for the derived key, allocated by caller
752 * @param out_len desired length of the derived key
753 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
754 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
756 * @param xts_len length of xts
757 * @param skm source key material
758 * @param skm_len length of skm
759 * @param argp va_list of void * & size_t pairs for context chunks
760 * @return GNUNET_YES on success
763 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
764 const void *xts, size_t xts_len, const void *skm,
765 size_t skm_len, va_list argp);
770 * @param result buffer for the derived key, allocated by caller
771 * @param out_len desired length of the derived key
773 * @param xts_len length of xts
774 * @param skm source key material
775 * @param skm_len length of skm
776 * @param argp va_list of void * & size_t pairs for context chunks
777 * @return GNUNET_YES on success
780 GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
781 size_t xts_len, const void *skm, size_t skm_len,
787 * @param result buffer for the derived key, allocated by caller
788 * @param out_len desired length of the derived key
790 * @param xts_len length of xts
791 * @param skm source key material
792 * @param skm_len length of skm
793 * @param ... void * & size_t pairs for context chunks
794 * @return GNUNET_YES on success
797 GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
798 size_t xts_len, const void *skm, size_t skm_len, ...);
802 * Function called upon completion of 'GNUNET_CRYPTO_ecc_key_create_async'.
805 * @param pk NULL on error, otherwise the private key (which must be free'd by the callee)
806 * @param emsg NULL on success, otherwise an error message
808 typedef void (*GNUNET_CRYPTO_EccKeyCallback)(void *cls,
809 struct GNUNET_CRYPTO_EccPrivateKey *pk,
814 * Free memory occupied by ECC key
816 * @param priv pointer to the memory to free
819 GNUNET_CRYPTO_ecc_key_free (struct GNUNET_CRYPTO_EccPrivateKey *priv);
823 * Extract the public key for the given private key.
825 * @param priv the private key
826 * @param pub where to write the public key
829 GNUNET_CRYPTO_ecc_key_get_public (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
830 struct GNUNET_CRYPTO_EccPublicKey *pub);
834 * Convert a public key to a string.
836 * @param pub key to convert
837 * @return string representing 'pub'
840 GNUNET_CRYPTO_ecc_public_key_to_string (const struct GNUNET_CRYPTO_EccPublicKey *pub);
844 * Convert a string representing a public key to a public key.
846 * @param enc encoded public key
847 * @param enclen number of bytes in enc (without 0-terminator)
848 * @param pub where to store the public key
849 * @return GNUNET_OK on success
852 GNUNET_CRYPTO_ecc_public_key_from_string (const char *enc,
854 struct GNUNET_CRYPTO_EccPublicKey *pub);
858 * Create a new private key by reading it from a file. If the
859 * files does not exist, create a new key and write it to the
860 * file. Caller must free return value. Note that this function
861 * can not guarantee that another process might not be trying
862 * the same operation on the same file at the same time.
863 * If the contents of the file
864 * are invalid the old file is deleted and a fresh key is
867 * @param filename name of file to use to store the key
868 * @return new private key, NULL on error (for example,
871 struct GNUNET_CRYPTO_EccPrivateKey *
872 GNUNET_CRYPTO_ecc_key_create_from_file (const char *filename);
876 * Create a new private key by reading our peer's key from
877 * the file specified in the configuration.
879 * @return new private key, NULL on error (for example,
882 struct GNUNET_CRYPTO_EccPrivateKey *
883 GNUNET_CRYPTO_ecc_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
887 * Create a new private key. Caller must free return value.
889 * @return fresh private key
891 struct GNUNET_CRYPTO_EccPrivateKey *
892 GNUNET_CRYPTO_ecc_key_create (void);
896 * Get the shared private key we use for anonymous users.
898 * @return "anonymous" private key
900 const struct GNUNET_CRYPTO_EccPrivateKey *
901 GNUNET_CRYPTO_ecc_key_get_anonymous (void);
905 * Setup a hostkey file for a peer given the name of the
906 * configuration file (!). This function is used so that
907 * at a later point code can be certain that reading a
908 * hostkey is fast (for example in time-dependent testcases).
910 * @param cfg_name name of the configuration file to use
913 GNUNET_CRYPTO_ecc_setup_hostkey (const char *cfg_name);
917 * Retrieve the identity of the host's peer.
919 * @param cfg configuration to use
920 * @param dst pointer to where to write the peer identity
921 * @return GNUNET_OK on success, GNUNET_SYSERR if the identity
922 * could not be retrieved
925 GNUNET_CRYPTO_get_host_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
926 struct GNUNET_PeerIdentity *dst);
930 * Derive key material from a public and a private ECC key.
932 * @param priv private key to use for the ECDH (x)
933 * @param pub public key to use for the ECDY (yG)
934 * @param key_material where to write the key material (xyG)
935 * @return GNUNET_SYSERR on error, GNUNET_OK on success
938 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
939 const struct GNUNET_CRYPTO_EccPublicKey *pub,
940 struct GNUNET_HashCode *key_material);
944 * Sign a given block.
946 * @param priv private key to use for the signing
947 * @param purpose what to sign (size, purpose)
948 * @param sig where to write the signature
949 * @return GNUNET_SYSERR on error, GNUNET_OK on success
952 GNUNET_CRYPTO_ecc_sign (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
953 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
954 struct GNUNET_CRYPTO_EccSignature *sig);
960 * @param purpose what is the purpose that the signature should have?
961 * @param validate block to validate (size, purpose, data)
962 * @param sig signature that is being validated
963 * @param pub public key of the signer
964 * @returns GNUNET_OK if ok, GNUNET_SYSERR if invalid
967 GNUNET_CRYPTO_ecc_verify (uint32_t purpose,
968 const struct GNUNET_CRYPTO_EccSignaturePurpose
970 const struct GNUNET_CRYPTO_EccSignature *sig,
971 const struct GNUNET_CRYPTO_EccPublicKey *pub);
975 * Derive a private key from a given private key and a label.
976 * Essentially calculates a private key 'h = H(l,P) * d mod n'
977 * where n is the size of the ECC group and P is the public
978 * key associated with the private key 'd'.
980 * @param priv original private key
981 * @param label label to use for key deriviation
982 * @return derived private key
984 struct GNUNET_CRYPTO_EccPrivateKey *
985 GNUNET_CRYPTO_ecc_key_derive (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
990 * Derive a public key from a given public key and a label.
991 * Essentially calculates a public key 'V = H(l,P) * P'.
993 * @param pub original public key
994 * @param label label to use for key deriviation
995 * @param result where to write the derived public key
998 GNUNET_CRYPTO_ecc_public_key_derive (const struct GNUNET_CRYPTO_EccPublicKey *pub,
1000 struct GNUNET_CRYPTO_EccPublicKey *result);
1003 #if 0 /* keep Emacsens' auto-indent happy */
1011 /* ifndef GNUNET_CRYPTO_LIB_H */
1013 /* end of gnunet_crypto_lib.h */