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 using EdDSA.
152 * See https://gnunet.org/ed25519
154 struct GNUNET_CRYPTO_EddsaSignature
160 unsigned char r[256 / 8];
165 unsigned char s[256 / 8];
172 * @brief an ECC signature using ECDSA
174 struct GNUNET_CRYPTO_EcdsaSignature
180 unsigned char r[256 / 8];
185 unsigned char s[256 / 8];
191 * Public ECC key (always for Curve25519) encoded in a format suitable
192 * for network transmission and EdDSA signatures.
194 struct GNUNET_CRYPTO_EddsaPublicKey
197 * Q consists of an x- and a y-value, each mod p (256 bits), given
198 * here in affine coordinates and Ed25519 standard compact format.
200 unsigned char q_y[256 / 8];
206 * Public ECC key (always for Curve25519) encoded in a format suitable
207 * for network transmission and ECDSA signatures.
209 struct GNUNET_CRYPTO_EcdsaPublicKey
212 * Q consists of an x- and a y-value, each mod p (256 bits), given
213 * here in affine coordinates and Ed25519 standard compact format.
215 unsigned char q_y[256 / 8];
221 * The identity of the host (wraps the signing key of the peer).
223 struct GNUNET_PeerIdentity
225 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
230 * Public ECC key (always for Curve25519) encoded in a format suitable
231 * for network transmission and encryption (ECDH),
232 * See http://cr.yp.to/ecdh.html
234 struct GNUNET_CRYPTO_EcdhePublicKey
237 * Q consists of an x- and a y-value, each mod p (256 bits), given
238 * here in affine coordinates and Ed25519 standard compact format.
240 unsigned char q_y[256 / 8];
245 * Private ECC key encoded for transmission. To be used only for ECDH
246 * key exchange (ECDHE to be precise).
248 struct GNUNET_CRYPTO_EcdhePrivateKey
251 * d is a value mod n, where n has at most 256 bits.
253 unsigned char d[256 / 8];
258 * Private ECC key encoded for transmission. To be used only for ECDSA
261 struct GNUNET_CRYPTO_EcdsaPrivateKey
264 * d is a value mod n, where n has at most 256 bits.
266 unsigned char d[256 / 8];
271 * Private ECC key encoded for transmission. To be used only for EdDSA
274 struct GNUNET_CRYPTO_EddsaPrivateKey
277 * d is a value mod n, where n has at most 256 bits.
279 unsigned char d[256 / 8];
285 * @brief type for session keys
287 struct GNUNET_CRYPTO_SymmetricSessionKey
290 * Actual key for AES.
292 unsigned char aes_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
295 * Actual key for TwoFish.
297 unsigned char twofish_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
301 GNUNET_NETWORK_STRUCT_END
304 * @brief IV for sym cipher
306 * NOTE: must be smaller (!) in size than the
307 * `struct GNUNET_HashCode`.
309 struct GNUNET_CRYPTO_SymmetricInitializationVector
311 unsigned char aes_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
313 unsigned char twofish_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
318 * @brief type for (message) authentication keys
320 struct GNUNET_CRYPTO_AuthKey
322 unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
326 /* **************** Functions and Macros ************* */
330 * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator
333 * @param seed the seed to use
336 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
340 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
342 * @param sum current sum, initially 0
343 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
344 * @param len number of bytes in @a buf, must be multiple of 2
345 * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish to get actual crc16)
348 GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len);
352 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
354 * @param sum cummulative sum
355 * @return crc16 value
358 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
363 * Calculate the checksum of a buffer in one step.
365 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
366 * @param len number of bytes in @a buf, must be multiple of 2
367 * @return crc16 value
370 GNUNET_CRYPTO_crc16_n (const void *buf, size_t len);
375 * Compute the CRC32 checksum for the first len
376 * bytes of the buffer.
378 * @param buf the data over which we're taking the CRC
379 * @param len the length of the buffer @a buf in bytes
380 * @return the resulting CRC32 checksum
383 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len);
388 * Fill block with a random values.
390 * @param mode desired quality of the random number
391 * @param buffer the buffer to fill
392 * @param length buffer length
395 GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length);
399 * Produce a random value.
401 * @param mode desired quality of the random number
402 * @param i the upper limit (exclusive) for the random number
403 * @return a random value in the interval [0,@a i) (exclusive).
406 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i);
411 * Random on unsigned 64-bit values.
413 * @param mode desired quality of the random number
414 * @param max value returned will be in range [0,@a max) (exclusive)
415 * @return random 64-bit number
418 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max);
423 * Get an array with a random permutation of the
425 * @param mode #GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used,
426 * #GNUNET_CRYPTO_QUALITY_WEAK or #GNUNET_CRYPTO_QUALITY_NONCE otherwise
427 * @param n the size of the array
428 * @return the permutation array (allocated from heap)
431 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
436 * Create a new random session key.
438 * @param key key to initialize
441 GNUNET_CRYPTO_symmetric_create_session_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key);
446 * Encrypt a block using a symmetric sessionkey.
448 * @param block the block to encrypt
449 * @param len the size of the block
450 * @param sessionkey the key used to encrypt
451 * @param iv the initialization vector to use, use INITVALUE
453 * @return the size of the encrypted block, -1 for errors
456 GNUNET_CRYPTO_symmetric_encrypt (const void *block, size_t len,
457 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
458 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
464 * Decrypt a given block using a symmetric sessionkey.
466 * @param block the data to decrypt, encoded as returned by encrypt
467 * @param size how big is the block?
468 * @param sessionkey the key used to decrypt
469 * @param iv the initialization vector to use
470 * @param result address to store the result at
471 * @return -1 on failure, size of decrypted block on success
474 GNUNET_CRYPTO_symmetric_decrypt (const void *block, size_t size,
475 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
476 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
482 * @brief Derive an IV
483 * @param iv initialization vector
484 * @param skey session key
485 * @param salt salt for the derivation
486 * @param salt_len size of the @a salt
487 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
490 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
491 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
493 size_t salt_len, ...);
497 * @brief Derive an IV
498 * @param iv initialization vector
499 * @param skey session key
500 * @param salt salt for the derivation
501 * @param salt_len size of the @a salt
502 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
505 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
506 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
514 * Convert hash to ASCII encoding.
515 * @param block the hash code
516 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
517 * safely cast to char*, a '\\0' termination is set).
520 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode * block,
521 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
526 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
528 * @param enc the encoding
529 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
530 * @param result where to store the hash code
531 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
534 GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
535 struct GNUNET_HashCode *result);
540 * Convert ASCII encoding back to `struct GNUNET_HashCode`
542 * @param enc the encoding
543 * @param result where to store the hash code
544 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
546 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
547 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
553 * Compute the distance between 2 hashcodes. The
554 * computation must be fast, not involve @a a[0] or @a a[4] (they're used
555 * elsewhere), and be somewhat consistent. And of course, the result
556 * should be a positive number.
558 * @param a some hash code
559 * @param b some hash code
560 * @return number between 0 and UINT32_MAX
563 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a,
564 const struct GNUNET_HashCode *b);
569 * Compute hash of a given block.
571 * @param block the data to hash
572 * @param size size of the @a block
573 * @param ret pointer to where to write the hashcode
576 GNUNET_CRYPTO_hash (const void *block, size_t size, struct GNUNET_HashCode * ret);
581 * Calculate HMAC of a message (RFC 2104)
583 * @param key secret key
584 * @param plaintext input plaintext
585 * @param plaintext_len length of @a plaintext
586 * @param hmac where to store the hmac
589 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
590 const void *plaintext, size_t plaintext_len,
591 struct GNUNET_HashCode * hmac);
595 * Function called once the hash computation over the
596 * specified file has completed.
599 * @param res resulting hash, NULL on error
601 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
602 const struct GNUNET_HashCode *res);
606 * Handle to file hashing operation.
608 struct GNUNET_CRYPTO_FileHashContext;
613 * Compute the hash of an entire file.
615 * @param priority scheduling priority to use
616 * @param filename name of file to hash
617 * @param blocksize number of bytes to process in one task
618 * @param callback function to call upon completion
619 * @param callback_cls closure for @a callback
620 * @return NULL on (immediate) errror
622 struct GNUNET_CRYPTO_FileHashContext *
623 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
624 const char *filename, size_t blocksize,
625 GNUNET_CRYPTO_HashCompletedCallback callback,
630 * Cancel a file hashing operation.
632 * @param fhc operation to cancel (callback must not yet have been invoked)
635 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
640 * Create a random hash code.
642 * @param mode desired quality level
643 * @param result hash code that is randomized
646 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
647 struct GNUNET_HashCode *result);
652 * compute @a result = @a b - @a a
654 * @param a some hash code
655 * @param b some hash code
656 * @param result set to @a b - @a a
659 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
660 const struct GNUNET_HashCode *b,
661 struct GNUNET_HashCode *result);
666 * compute @a result = @a a + @a delta
668 * @param a some hash code
669 * @param delta some hash code
670 * @param result set to @a a + @a delta
673 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a,
674 const struct GNUNET_HashCode *delta,
675 struct GNUNET_HashCode *result);
680 * compute result = a ^ b
682 * @param a some hash code
683 * @param b some hash code
684 * @param result set to @a a ^ @a b
687 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode * a, const struct GNUNET_HashCode * b,
688 struct GNUNET_HashCode * result);
693 * Convert a hashcode into a key.
695 * @param hc hash code that serves to generate the key
696 * @param skey set to a valid session key
697 * @param iv set to a valid initialization vector
700 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
701 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
702 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv);
707 * Obtain a bit from a hashcode.
709 * @param code the `struct GNUNET_HashCode` to index bit-wise
710 * @param bit index into the hashcode, [0...159]
711 * @return Bit \a bit from hashcode \a code, -1 for invalid index
714 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code,
720 * Determine how many low order bits match in two
721 * `struct GNUNET_HashCodes`. i.e. - 010011 and 011111 share
722 * the first two lowest order bits, and therefore the
723 * return value is two (NOT XOR distance, nor how many
724 * bits match absolutely!).
726 * @param first the first hashcode
727 * @param second the hashcode to compare first to
728 * @return the number of bits that match
731 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
732 const struct GNUNET_HashCode *second);
737 * Compare function for HashCodes, producing a total ordering
740 * @param h1 some hash code
741 * @param h2 some hash code
742 * @return 1 if @a h1 > @a h2, -1 if @a h1 < @a h2 and 0 if @a h1 == @a h2.
745 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
746 const struct GNUNET_HashCode *h2);
751 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
752 * in the XOR metric (Kademlia).
754 * @param h1 some hash code
755 * @param h2 some hash code
756 * @param target some hash code
757 * @return -1 if @a h1 is closer, 1 if @a h2 is closer and 0 if @a h1== @a h2.
760 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
761 const struct GNUNET_HashCode *h2,
762 const struct GNUNET_HashCode *target);
767 * @brief Derive an authentication key
768 * @param key authentication key
769 * @param rkey root key
771 * @param salt_len size of the salt
772 * @param argp pair of void * & size_t for context chunks, terminated by NULL
775 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
776 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
777 const void *salt, size_t salt_len,
783 * @brief Derive an authentication key
784 * @param key authentication key
785 * @param rkey root key
787 * @param salt_len size of the salt
788 * @param ... pair of void * & size_t for context chunks, terminated by NULL
791 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
792 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
793 const void *salt, size_t salt_len, ...);
799 * @param result buffer for the derived key, allocated by caller
800 * @param out_len desired length of the derived key
801 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
802 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
804 * @param xts_len length of @a xts
805 * @param skm source key material
806 * @param skm_len length of @a skm
807 * @param ... pair of void * & size_t for context chunks, terminated by NULL
808 * @return #GNUNET_YES on success
811 GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
812 const void *xts, size_t xts_len, const void *skm,
813 size_t skm_len, ...);
819 * @param result buffer for the derived key, allocated by caller
820 * @param out_len desired length of the derived key
821 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
822 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
824 * @param xts_len length of @a xts
825 * @param skm source key material
826 * @param skm_len length of @a skm
827 * @param argp va_list of void * & size_t pairs for context chunks
828 * @return #GNUNET_YES on success
831 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
832 const void *xts, size_t xts_len, const void *skm,
833 size_t skm_len, va_list argp);
838 * @param result buffer for the derived key, allocated by caller
839 * @param out_len desired length of the derived key
841 * @param xts_len length of @a xts
842 * @param skm source key material
843 * @param skm_len length of @a skm
844 * @param argp va_list of void * & size_t pairs for context chunks
845 * @return #GNUNET_YES on success
848 GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
849 size_t xts_len, const void *skm, size_t skm_len,
856 * @param result buffer for the derived key, allocated by caller
857 * @param out_len desired length of the derived key
859 * @param xts_len length of @a xts
860 * @param skm source key material
861 * @param skm_len length of @a skm
862 * @param ... void * & size_t pairs for context chunks
863 * @return #GNUNET_YES on success
866 GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
867 size_t xts_len, const void *skm, size_t skm_len, ...);
872 * Extract the public key for the given private key.
874 * @param priv the private key
875 * @param pub where to write the public key
878 GNUNET_CRYPTO_ecdsa_key_get_public (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
879 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
883 * Extract the public key for the given private key.
885 * @param priv the private key
886 * @param pub where to write the public key
889 GNUNET_CRYPTO_eddsa_key_get_public (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
890 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
895 * Extract the public key for the given private key.
897 * @param priv the private key
898 * @param pub where to write the public key
901 GNUNET_CRYPTO_ecdhe_key_get_public (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
902 struct GNUNET_CRYPTO_EcdhePublicKey *pub);
906 * Convert a public key to a string.
908 * @param pub key to convert
909 * @return string representing @a pub
912 GNUNET_CRYPTO_ecdsa_public_key_to_string (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
916 * Convert a public key to a string.
918 * @param pub key to convert
919 * @return string representing @a pub
922 GNUNET_CRYPTO_eddsa_public_key_to_string (const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
926 * Convert a string representing a public key to a public key.
928 * @param enc encoded public key
929 * @param enclen number of bytes in @a enc (without 0-terminator)
930 * @param pub where to store the public key
931 * @return #GNUNET_OK on success
934 GNUNET_CRYPTO_ecdsa_public_key_from_string (const char *enc,
936 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
940 * Convert a string representing a public key to a public key.
942 * @param enc encoded public key
943 * @param enclen number of bytes in @a enc (without 0-terminator)
944 * @param pub where to store the public key
945 * @return #GNUNET_OK on success
948 GNUNET_CRYPTO_eddsa_public_key_from_string (const char *enc,
950 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
955 * Create a new private key by reading it from a file. If the
956 * files does not exist, create a new key and write it to the
957 * file. Caller must free return value. Note that this function
958 * can not guarantee that another process might not be trying
959 * the same operation on the same file at the same time.
960 * If the contents of the file
961 * are invalid the old file is deleted and a fresh key is
964 * @param filename name of file to use to store the key
965 * @return new private key, NULL on error (for example,
966 * permission denied); free using #GNUNET_free
968 struct GNUNET_CRYPTO_EcdsaPrivateKey *
969 GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename);
974 * Create a new private key by reading it from a file. If the
975 * files does not exist, create a new key and write it to the
976 * file. Caller must free return value. Note that this function
977 * can not guarantee that another process might not be trying
978 * the same operation on the same file at the same time.
979 * If the contents of the file
980 * are invalid the old file is deleted and a fresh key is
983 * @param filename name of file to use to store the key
984 * @return new private key, NULL on error (for example,
985 * permission denied); free using #GNUNET_free
987 struct GNUNET_CRYPTO_EddsaPrivateKey *
988 GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename);
993 * Create a new private key by reading our peer's key from
994 * the file specified in the configuration.
996 * @param cfg the configuration to use
997 * @return new private key, NULL on error (for example,
998 * permission denied); free using #GNUNET_free
1000 struct GNUNET_CRYPTO_EddsaPrivateKey *
1001 GNUNET_CRYPTO_eddsa_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
1006 * Create a new private key. Caller must free return value.
1008 * @return fresh private key; free using #GNUNET_free
1010 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1011 GNUNET_CRYPTO_ecdsa_key_create (void);
1016 * Create a new private key. Caller must free return value.
1018 * @return fresh private key; free using #GNUNET_free
1020 struct GNUNET_CRYPTO_EddsaPrivateKey *
1021 GNUNET_CRYPTO_eddsa_key_create (void);
1026 * Create a new private key. Caller must free return value.
1028 * @return fresh private key; free using #GNUNET_free
1030 struct GNUNET_CRYPTO_EcdhePrivateKey *
1031 GNUNET_CRYPTO_ecdhe_key_create (void);
1036 * Clear memory that was used to store a private key.
1038 * @param pk location of the key
1041 GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk);
1046 * Clear memory that was used to store a private key.
1048 * @param pk location of the key
1051 GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk);
1055 * Clear memory that was used to store a private key.
1057 * @param pk location of the key
1060 GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1065 * Get the shared private key we use for anonymous users.
1067 * @return "anonymous" private key; do not free
1069 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
1070 GNUNET_CRYPTO_ecdsa_key_get_anonymous (void);
1075 * Setup a hostkey file for a peer given the name of the
1076 * configuration file (!). This function is used so that
1077 * at a later point code can be certain that reading a
1078 * hostkey is fast (for example in time-dependent testcases).
1080 * @param cfg_name name of the configuration file to use
1083 GNUNET_CRYPTO_eddsa_setup_hostkey (const char *cfg_name);
1088 * Retrieve the identity of the host's peer.
1090 * @param cfg configuration to use
1091 * @param dst pointer to where to write the peer identity
1092 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
1093 * could not be retrieved
1096 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
1097 struct GNUNET_PeerIdentity *dst);
1102 * Derive key material from a public and a private ECC key.
1104 * @param priv private key to use for the ECDH (x)
1105 * @param pub public key to use for the ECDH (yG)
1106 * @param key_material where to write the key material (xyG)
1107 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1110 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1111 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1112 struct GNUNET_HashCode *key_material);
1117 * EdDSA sign a given block.
1119 * @param priv private key to use for the signing
1120 * @param purpose what to sign (size, purpose)
1121 * @param sig where to write the signature
1122 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1125 GNUNET_CRYPTO_eddsa_sign (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1126 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1127 struct GNUNET_CRYPTO_EddsaSignature *sig);
1132 * ECDSA Sign a given block.
1134 * @param priv private key to use for the signing
1135 * @param purpose what to sign (size, purpose)
1136 * @param sig where to write the signature
1137 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1140 GNUNET_CRYPTO_ecdsa_sign (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1141 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1142 struct GNUNET_CRYPTO_EcdsaSignature *sig);
1146 * Verify EdDSA signature.
1148 * @param purpose what is the purpose that the signature should have?
1149 * @param validate block to validate (size, purpose, data)
1150 * @param sig signature that is being validated
1151 * @param pub public key of the signer
1152 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1155 GNUNET_CRYPTO_eddsa_verify (uint32_t purpose,
1156 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1157 const struct GNUNET_CRYPTO_EddsaSignature *sig,
1158 const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1164 * Verify ECDSA signature.
1166 * @param purpose what is the purpose that the signature should have?
1167 * @param validate block to validate (size, purpose, data)
1168 * @param sig signature that is being validated
1169 * @param pub public key of the signer
1170 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1173 GNUNET_CRYPTO_ecdsa_verify (uint32_t purpose,
1174 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1175 const struct GNUNET_CRYPTO_EcdsaSignature *sig,
1176 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1181 * Derive a private key from a given private key and a label.
1182 * Essentially calculates a private key 'h = H(l,P) * d mod n'
1183 * where n is the size of the ECC group and P is the public
1184 * key associated with the private key 'd'.
1186 * @param priv original private key
1187 * @param label label to use for key deriviation
1188 * @param context additional context to use for HKDF of 'h';
1189 * typically the name of the subsystem/application
1190 * @return derived private key
1192 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1193 GNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1195 const char *context);
1200 * Derive a public key from a given public key and a label.
1201 * Essentially calculates a public key 'V = H(l,P) * P'.
1203 * @param pub original public key
1204 * @param label label to use for key deriviation
1205 * @param context additional context to use for HKDF of 'h'.
1206 * typically the name of the subsystem/application
1207 * @param result where to write the derived public key
1210 GNUNET_CRYPTO_ecdsa_public_key_derive (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1212 const char *context,
1213 struct GNUNET_CRYPTO_EcdsaPublicKey *result);
1216 #if 0 /* keep Emacsens' auto-indent happy */
1224 /* ifndef GNUNET_CRYPTO_LIB_H */
1226 /* end of gnunet_crypto_lib.h */