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
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 @a buf, 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 @a buf, 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 @buf 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,@a 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,@a 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,
303 * #GNUNET_CRYPTO_QUALITY_WEAK or #GNUNET_CRYPTO_QUALITY_NONCE otherwise
304 * @param n the size of the array
305 * @return the permutation array (allocated from heap)
308 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
312 * Create a new Session key.
314 * @param key key to initialize
317 GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key);
321 * Encrypt a block with the public key of another
322 * host that uses the same cyper.
324 * @param block the block to encrypt
325 * @param len the size of the block
326 * @param sessionkey the key used to encrypt
327 * @param iv the initialization vector to use, use INITVALUE
329 * @return the size of the encrypted block, -1 for errors
332 GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
333 const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
334 const struct GNUNET_CRYPTO_AesInitializationVector
339 * Decrypt a given block with the sessionkey.
341 * @param block the data to decrypt, encoded as returned by encrypt
342 * @param size how big is the block?
343 * @param sessionkey the key used to decrypt
344 * @param iv the initialization vector to use
345 * @param result address to store the result at
346 * @return -1 on failure, size of decrypted block on success
349 GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
350 const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
351 const struct GNUNET_CRYPTO_AesInitializationVector
356 * @brief Derive an IV
357 * @param iv initialization vector
358 * @param skey session key
359 * @param salt salt for the derivation
360 * @param salt_len size of the salt
361 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
364 GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
365 const struct GNUNET_CRYPTO_AesSessionKey *skey,
366 const void *salt, size_t salt_len, ...);
370 * @brief Derive an IV
371 * @param iv initialization vector
372 * @param skey session key
373 * @param salt salt for the derivation
374 * @param salt_len size of the salt
375 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
378 GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
379 const struct GNUNET_CRYPTO_AesSessionKey *skey,
380 const void *salt, size_t salt_len, va_list argp);
384 * Convert hash to ASCII encoding.
385 * @param block the hash code
386 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
387 * safely cast to char*, a '\\0' termination is set).
390 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode * block,
391 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
395 * Convert short hash to ASCII encoding.
397 * @param block the hash code
398 * @param result where to store the encoding (struct GNUNET_CRYPTO_ShortHashAsciiEncoded can be
399 * safely cast to char*, a '\\0' termination is set).
402 GNUNET_CRYPTO_short_hash_to_enc (const struct GNUNET_CRYPTO_ShortHashCode * block,
403 struct GNUNET_CRYPTO_ShortHashAsciiEncoded *result);
407 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
409 * @param enc the encoding
410 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
411 * @param result where to store the hash code
412 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
415 GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
416 struct GNUNET_HashCode *result);
420 * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
422 * @param enc the encoding
423 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
424 * @param result where to store the hash code
425 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
428 GNUNET_CRYPTO_short_hash_from_string2 (const char *enc, size_t enclen,
429 struct GNUNET_CRYPTO_ShortHashCode * result);
433 * Convert ASCII encoding back to struct GNUNET_HashCode
435 * @param enc the encoding
436 * @param result where to store the hash code
437 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
439 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
440 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
444 * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
446 * @param enc the encoding
447 * @param result where to store the GNUNET_CRYPTO_ShortHash
448 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
450 #define GNUNET_CRYPTO_short_hash_from_string(enc, result) \
451 GNUNET_CRYPTO_short_hash_from_string2 (enc, strlen(enc), result)
455 * Compare function for ShortHashCodes, producing a total ordering
458 * @param h1 some hash code
459 * @param h2 some hash code
460 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
463 GNUNET_CRYPTO_short_hash_cmp (const struct GNUNET_CRYPTO_ShortHashCode * h1,
464 const struct GNUNET_CRYPTO_ShortHashCode * h2);
467 * Compute the distance between 2 hashcodes.
468 * The computation must be fast, not involve
469 * a.a or a.e (they're used elsewhere), and
470 * be somewhat consistent. And of course, the
471 * result should be a positive number.
473 * @param a some hash code
474 * @param b some hash code
475 * @return number between 0 and UINT32_MAX
478 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode * a,
479 const struct GNUNET_HashCode * b);
483 * Compute hash of a given block.
485 * @param block the data to hash
486 * @param size size of the @a block
487 * @param ret pointer to where to write the hashcode
490 GNUNET_CRYPTO_hash (const void *block, size_t size, struct GNUNET_HashCode * ret);
494 * Compute short (256-bit) hash of a given block.
496 * @param block the data to hash
497 * @param size size of the @a block
498 * @param ret pointer to where to write the hashcode
501 GNUNET_CRYPTO_short_hash (const void *block, size_t size,
502 struct GNUNET_CRYPTO_ShortHashCode * ret);
506 * Double short (256-bit) hash to create a long hash.
508 * @param sh short hash to double
509 * @param dh where to store the (doubled) long hash (not really a hash)
512 GNUNET_CRYPTO_short_hash_double (const struct GNUNET_CRYPTO_ShortHashCode *sh,
513 struct GNUNET_HashCode *dh);
517 * Truncate doubled short hash back to a short hash.
519 * @param dh doubled short hash to reduce again
520 * @param sh where to store the short hash
521 * @return GNUNET_OK on success, GNUNET_SYSERR if this was not a
525 GNUNET_CRYPTO_short_hash_from_truncation (const struct GNUNET_HashCode *dh,
526 struct GNUNET_CRYPTO_ShortHashCode *sh);
530 * Calculate HMAC of a message (RFC 2104)
532 * @param key secret key
533 * @param plaintext input plaintext
534 * @param plaintext_len length of @a plaintext
535 * @param hmac where to store the hmac
538 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
539 const void *plaintext, size_t plaintext_len,
540 struct GNUNET_HashCode * hmac);
544 * Function called once the hash computation over the
545 * specified file has completed.
548 * @param res resulting hash, NULL on error
550 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
551 const struct GNUNET_HashCode *
556 * Handle to file hashing operation.
558 struct GNUNET_CRYPTO_FileHashContext;
561 * Compute the hash of an entire file.
563 * @param priority scheduling priority to use
564 * @param filename name of file to hash
565 * @param blocksize number of bytes to process in one task
566 * @param callback function to call upon completion
567 * @param callback_cls closure for callback
568 * @return NULL on (immediate) errror
570 struct GNUNET_CRYPTO_FileHashContext *
571 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
572 const char *filename, size_t blocksize,
573 GNUNET_CRYPTO_HashCompletedCallback callback,
578 * Cancel a file hashing operation.
580 * @param fhc operation to cancel (callback must not yet have been invoked)
583 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
587 * Create a random hash code.
589 * @param mode desired quality level
590 * @param result hash code that is randomized
593 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
594 struct GNUNET_HashCode * result);
598 * compute result(delta) = b - a
600 * @param a some hash code
601 * @param b some hash code
602 * @param result set to @a b - @a a
605 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode * a,
606 const struct GNUNET_HashCode * b,
607 struct GNUNET_HashCode * result);
611 * compute result(b) = a + delta
613 * @param a some hash code
614 * @param delta some hash code
615 * @param result set to @a a + @a delta
618 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode * a,
619 const struct GNUNET_HashCode * delta,
620 struct GNUNET_HashCode * result);
624 * compute result = a ^ b
626 * @param a some hash code
627 * @param b some hash code
628 * @param result set to @a a ^ @a b
631 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode * a, const struct GNUNET_HashCode * b,
632 struct GNUNET_HashCode * result);
636 * Convert a hashcode into a key.
638 * @param hc hash code that serves to generate the key
639 * @param skey set to a valid session key
640 * @param iv set to a valid initialization vector
643 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
644 struct GNUNET_CRYPTO_AesSessionKey *skey,
645 struct GNUNET_CRYPTO_AesInitializationVector
650 * Obtain a bit from a hashcode.
652 * @param code the GNUNET_CRYPTO_hash to index bit-wise
653 * @param bit index into the hashcode, [0...159]
654 * @return Bit \a bit from hashcode \a code, -1 for invalid index
657 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode * code, unsigned int bit);
661 * Determine how many low order bits match in two
662 * struct GNUNET_HashCodes. i.e. - 010011 and 011111 share
663 * the first two lowest order bits, and therefore the
664 * return value is two (NOT XOR distance, nor how many
665 * bits match absolutely!).
667 * @param first the first hashcode
668 * @param second the hashcode to compare first to
669 * @return the number of bits that match
672 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode * first,
673 const struct GNUNET_HashCode * second);
677 * Compare function for HashCodes, producing a total ordering
680 * @param h1 some hash code
681 * @param h2 some hash code
682 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
685 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode * h1, const struct GNUNET_HashCode * h2);
689 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
690 * in the XOR metric (Kademlia).
692 * @param h1 some hash code
693 * @param h2 some hash code
694 * @param target some hash code
695 * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
698 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode * h1,
699 const struct GNUNET_HashCode * h2,
700 const struct GNUNET_HashCode * target);
704 * @brief Derive an authentication key
705 * @param key authentication key
706 * @param rkey root key
708 * @param salt_len size of the salt
709 * @param argp pair of void * & size_t for context chunks, terminated by NULL
712 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
713 const struct GNUNET_CRYPTO_AesSessionKey *rkey,
714 const void *salt, size_t salt_len,
719 * @brief Derive an authentication key
720 * @param key authentication key
721 * @param rkey root key
723 * @param salt_len size of the salt
724 * @param ... pair of void * & size_t for context chunks, terminated by NULL
727 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
728 const struct GNUNET_CRYPTO_AesSessionKey *rkey,
729 const void *salt, size_t salt_len, ...);
734 * @param result buffer for the derived key, allocated by caller
735 * @param out_len desired length of the derived key
736 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
737 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
739 * @param xts_len length of xts
740 * @param skm source key material
741 * @param skm_len length of skm
742 * @param ... pair of void * & size_t for context chunks, terminated by NULL
743 * @return GNUNET_YES on success
746 GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
747 const void *xts, size_t xts_len, const void *skm,
748 size_t skm_len, ...);
753 * @param result buffer for the derived key, allocated by caller
754 * @param out_len desired length of the derived key
755 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
756 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
758 * @param xts_len length of xts
759 * @param skm source key material
760 * @param skm_len length of skm
761 * @param argp va_list of void * & size_t pairs for context chunks
762 * @return GNUNET_YES on success
765 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
766 const void *xts, size_t xts_len, const void *skm,
767 size_t skm_len, va_list argp);
772 * @param result buffer for the derived key, allocated by caller
773 * @param out_len desired length of the derived key
775 * @param xts_len length of xts
776 * @param skm source key material
777 * @param skm_len length of skm
778 * @param argp va_list of void * & size_t pairs for context chunks
779 * @return GNUNET_YES on success
782 GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
783 size_t xts_len, const void *skm, size_t skm_len,
789 * @param result buffer for the derived key, allocated by caller
790 * @param out_len desired length of the derived key
792 * @param xts_len length of xts
793 * @param skm source key material
794 * @param skm_len length of skm
795 * @param ... void * & size_t pairs for context chunks
796 * @return GNUNET_YES on success
799 GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
800 size_t xts_len, const void *skm, size_t skm_len, ...);
804 * Function called upon completion of 'GNUNET_CRYPTO_ecc_key_create_async'.
807 * @param pk NULL on error, otherwise the private key (which must be free'd by the callee)
808 * @param emsg NULL on success, otherwise an error message
810 typedef void (*GNUNET_CRYPTO_EccKeyCallback)(void *cls,
811 struct GNUNET_CRYPTO_EccPrivateKey *pk,
816 * Free memory occupied by ECC key
818 * @param priv pointer to the memory to free
821 GNUNET_CRYPTO_ecc_key_free (struct GNUNET_CRYPTO_EccPrivateKey *priv);
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 (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
832 struct GNUNET_CRYPTO_EccPublicKey *pub);
836 * Convert a public key to a string.
838 * @param pub key to convert
839 * @return string representing 'pub'
842 GNUNET_CRYPTO_ecc_public_key_to_string (const struct GNUNET_CRYPTO_EccPublicKey *pub);
846 * Convert a string representing a public key to a public key.
848 * @param enc encoded public key
849 * @param enclen number of bytes in enc (without 0-terminator)
850 * @param pub where to store the public key
851 * @return GNUNET_OK on success
854 GNUNET_CRYPTO_ecc_public_key_from_string (const char *enc,
856 struct GNUNET_CRYPTO_EccPublicKey *pub);
860 * Create a new private key by reading it from a file. If the
861 * files does not exist, create a new key and write it to the
862 * file. Caller must free return value. Note that this function
863 * can not guarantee that another process might not be trying
864 * the same operation on the same file at the same time.
865 * If the contents of the file
866 * are invalid the old file is deleted and a fresh key is
869 * @param filename name of file to use to store the key
870 * @return new private key, NULL on error (for example,
873 struct GNUNET_CRYPTO_EccPrivateKey *
874 GNUNET_CRYPTO_ecc_key_create_from_file (const char *filename);
878 * Create a new private key by reading our peer's key from
879 * the file specified in the configuration.
881 * @return new private key, NULL on error (for example,
884 struct GNUNET_CRYPTO_EccPrivateKey *
885 GNUNET_CRYPTO_ecc_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
889 * Create a new private key. Caller must free return value.
891 * @return fresh private key
893 struct GNUNET_CRYPTO_EccPrivateKey *
894 GNUNET_CRYPTO_ecc_key_create (void);
898 * Get the shared private key we use for anonymous users.
900 * @return "anonymous" private key
902 const struct GNUNET_CRYPTO_EccPrivateKey *
903 GNUNET_CRYPTO_ecc_key_get_anonymous (void);
907 * Setup a hostkey file for a peer given the name of the
908 * configuration file (!). This function is used so that
909 * at a later point code can be certain that reading a
910 * hostkey is fast (for example in time-dependent testcases).
912 * @param cfg_name name of the configuration file to use
915 GNUNET_CRYPTO_ecc_setup_hostkey (const char *cfg_name);
919 * Retrieve the identity of the host's peer.
921 * @param cfg configuration to use
922 * @param dst pointer to where to write the peer identity
923 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
924 * could not be retrieved
927 GNUNET_CRYPTO_get_host_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
928 struct GNUNET_PeerIdentity *dst);
932 * Derive key material from a public and a private ECC key.
934 * @param priv private key to use for the ECDH (x)
935 * @param pub public key to use for the ECDY (yG)
936 * @param key_material where to write the key material (xyG)
937 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
940 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
941 const struct GNUNET_CRYPTO_EccPublicKey *pub,
942 struct GNUNET_HashCode *key_material);
946 * Sign a given block.
948 * @param priv private key to use for the signing
949 * @param purpose what to sign (size, purpose)
950 * @param sig where to write the signature
951 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
954 GNUNET_CRYPTO_ecc_sign (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
955 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
956 struct GNUNET_CRYPTO_EccSignature *sig);
962 * @param purpose what is the purpose that the signature should have?
963 * @param validate block to validate (size, purpose, data)
964 * @param sig signature that is being validated
965 * @param pub public key of the signer
966 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
969 GNUNET_CRYPTO_ecc_verify (uint32_t purpose,
970 const struct GNUNET_CRYPTO_EccSignaturePurpose
972 const struct GNUNET_CRYPTO_EccSignature *sig,
973 const struct GNUNET_CRYPTO_EccPublicKey *pub);
977 * Derive a private key from a given private key and a label.
978 * Essentially calculates a private key 'h = H(l,P) * d mod n'
979 * where n is the size of the ECC group and P is the public
980 * key associated with the private key 'd'.
982 * @param priv original private key
983 * @param label label to use for key deriviation
984 * @param context additional context to use for HKDF of 'h';
985 * typically the name of the subsystem/application
986 * @return derived private key
988 struct GNUNET_CRYPTO_EccPrivateKey *
989 GNUNET_CRYPTO_ecc_key_derive (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
991 const char *context);
995 * Derive a public key from a given public key and a label.
996 * Essentially calculates a public key 'V = H(l,P) * P'.
998 * @param pub original public key
999 * @param label label to use for key deriviation
1000 * @param context additional context to use for HKDF of 'h'.
1001 * typically the name of the subsystem/application
1002 * @param result where to write the derived public key
1005 GNUNET_CRYPTO_ecc_public_key_derive (const struct GNUNET_CRYPTO_EccPublicKey *pub,
1007 const char *context,
1008 struct GNUNET_CRYPTO_EccPublicKey *result);
1011 #if 0 /* keep Emacsens' auto-indent happy */
1019 /* ifndef GNUNET_CRYPTO_LIB_H */
1021 /* end of gnunet_crypto_lib.h */