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"
47 * Desired quality level for cryptographic operations.
49 enum GNUNET_CRYPTO_Quality
52 * No good quality of the operation is needed (i.e.,
53 * random numbers can be pseudo-random).
55 GNUNET_CRYPTO_QUALITY_WEAK,
58 * High-quality operations are desired.
60 GNUNET_CRYPTO_QUALITY_STRONG,
63 * Randomness for IVs etc. is required.
65 GNUNET_CRYPTO_QUALITY_NONCE
70 * @brief length of the sessionkey in bytes (256 BIT sessionkey)
72 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
75 * @brief Length of RSA encrypted data (2048 bit)
77 * We currently do not handle encryption of data
78 * that can not be done in a single call to the
79 * RSA methods (read: large chunks of data).
80 * We should never need that, as we can use
81 * the GNUNET_CRYPTO_hash for larger pieces of data for signing,
82 * and for encryption, we only need to encode sessionkeys!
84 #define GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH 256
87 * Length of an RSA KEY (n,e,len), 2048 bit (=256 octests) key n, 2 byte e
89 #define GNUNET_CRYPTO_RSA_KEY_LENGTH 258
92 * Length of a hash value
94 #define GNUNET_CRYPTO_HASH_LENGTH (512/8)
97 * Maximum length of an ECC signature.
98 * Note: round up to multiple of 8 minus 2 for alignment.
100 #define GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH 126
103 * Maximum length of the public key (q-point, Q = dP) when encoded.
105 #define GNUNET_CRYPTO_ECC_MAX_PUBLIC_KEY_LENGTH 76
109 * The private information of an RSA key pair.
111 struct GNUNET_CRYPTO_RsaPrivateKey;
114 * The private information of an ECC private key.
116 struct GNUNET_CRYPTO_EccPrivateKey;
119 GNUNET_NETWORK_STRUCT_BEGIN
122 * GNUnet mandates a certain format for the encoding
123 * of private RSA key information that is provided
124 * by the RSA implementations. This format is used
125 * to serialize a private RSA key (typically when
126 * writing it to disk).
128 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded
131 * Total size of the structure, in bytes, in big-endian!
133 uint16_t len GNUNET_PACKED;
134 uint16_t sizen GNUNET_PACKED; /* in big-endian! */
135 uint16_t sizee GNUNET_PACKED; /* in big-endian! */
136 uint16_t sized GNUNET_PACKED; /* in big-endian! */
137 uint16_t sizep GNUNET_PACKED; /* in big-endian! */
138 uint16_t sizeq GNUNET_PACKED; /* in big-endian! */
139 uint16_t sizedmp1 GNUNET_PACKED; /* in big-endian! */
140 uint16_t sizedmq1 GNUNET_PACKED; /* in big-endian! */
141 /* followed by the actual values */
143 GNUNET_NETWORK_STRUCT_END
147 * @brief 0-terminated ASCII encoding of a struct GNUNET_HashCode.
149 struct GNUNET_CRYPTO_HashAsciiEncoded
151 unsigned char encoding[104];
156 * @brief 0-terminated ASCII encoding of a 'struct GNUNET_ShortHashCode'.
158 struct GNUNET_CRYPTO_ShortHashAsciiEncoded
160 unsigned char short_encoding[53];
166 * @brief an RSA signature
168 struct GNUNET_CRYPTO_RsaSignature
170 unsigned char sig[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH];
174 GNUNET_NETWORK_STRUCT_BEGIN
177 * @brief header of what an RSA signature signs
178 * this must be followed by "size - 8" bytes of
179 * the actual signed data
181 struct GNUNET_CRYPTO_RsaSignaturePurpose
184 * How many bytes does this signature sign?
185 * (including this purpose header); in network
188 uint32_t size GNUNET_PACKED;
191 * What does this signature vouch for? This
192 * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
193 * constant (from gnunet_signatures.h). In
194 * network byte order!
196 uint32_t purpose GNUNET_PACKED;
202 * @brief A public key.
204 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
207 * In big-endian, must be GNUNET_CRYPTO_RSA_KEY_LENGTH+4
209 uint16_t len GNUNET_PACKED;
212 * Size of n in key; in big-endian!
214 uint16_t sizen GNUNET_PACKED;
217 * The key itself, contains n followed by e.
219 unsigned char key[GNUNET_CRYPTO_RSA_KEY_LENGTH];
222 * Padding (must be 0)
224 uint16_t padding GNUNET_PACKED;
229 * RSA Encrypted data.
231 struct GNUNET_CRYPTO_RsaEncryptedData
233 unsigned char encoding[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH];
238 * @brief header of what an ECC signature signs
239 * this must be followed by "size - 8" bytes of
240 * the actual signed data
242 struct GNUNET_CRYPTO_EccSignaturePurpose
245 * How many bytes does this signature sign?
246 * (including this purpose header); in network
249 uint32_t size GNUNET_PACKED;
252 * What does this signature vouch for? This
253 * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
254 * constant (from gnunet_signatures.h). In
255 * network byte order!
257 uint32_t purpose GNUNET_PACKED;
263 * @brief an ECC signature
265 struct GNUNET_CRYPTO_EccSignature
268 * Overall size of the signature data.
270 uint16_t size GNUNET_PACKED;
273 * S-expression, padded with zeros.
275 char sexpr[GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH];
280 * Public ECC key (always for NIST P-521) encoded in a format suitable
281 * for network transmission as created using 'gcry_sexp_sprint'.
283 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
286 * Size of the encoding, in network byte order.
288 uint16_t size GNUNET_PACKED;
291 * Actual length of the q-point binary encoding.
293 uint16_t len GNUNET_PACKED;
296 * 0-padded q-point in binary encoding (GCRYPT_MPI_FMT_USG).
298 unsigned char key[GNUNET_CRYPTO_ECC_MAX_PUBLIC_KEY_LENGTH];
303 * Private ECC key encoded for transmission (with length prefix).
305 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded
308 * Overall size of the private key in network byte order.
312 /* followd by S-expression, opaque to applications */
314 /* FIXME: consider defining padding to make this a fixed-size struct */
320 * @brief type for session keys
322 struct GNUNET_CRYPTO_AesSessionKey
327 unsigned char key[GNUNET_CRYPTO_AES_KEY_LENGTH];
330 GNUNET_NETWORK_STRUCT_END
333 * @brief IV for sym cipher
335 * NOTE: must be smaller (!) in size than the
336 * struct GNUNET_HashCode.
338 struct GNUNET_CRYPTO_AesInitializationVector
340 unsigned char iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
345 * @brief type for (message) authentication keys
347 struct GNUNET_CRYPTO_AuthKey
349 unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
353 /* **************** Functions and Macros ************* */
356 * Seed a weak random generator. Only GNUNET_CRYPTO_QUALITY_WEAK-mode generator
359 * @param seed the seed to use
362 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
366 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
368 * @param sum current sum, initially 0
369 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
370 * @param len number of bytes in hdr, must be multiple of 2
371 * @return updated crc sum (must be subjected to GNUNET_CRYPTO_crc16_finish to get actual crc16)
374 GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len);
378 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
380 * @param sum cummulative sum
381 * @return crc16 value
384 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
388 * Calculate the checksum of a buffer in one step.
390 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
391 * @param len number of bytes in hdr, must be multiple of 2
392 * @return crc16 value
395 GNUNET_CRYPTO_crc16_n (const void *buf, size_t len);
399 * Compute the CRC32 checksum for the first len
400 * bytes of the buffer.
402 * @param buf the data over which we're taking the CRC
403 * @param len the length of the buffer in bytes
404 * @return the resulting CRC32 checksum
407 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len);
411 * Produce a random value.
413 * @param mode desired quality of the random number
414 * @param i the upper limit (exclusive) for the random number
415 * @return a random value in the interval [0,i) (exclusive).
418 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i);
422 * Random on unsigned 64-bit values.
424 * @param mode desired quality of the random number
425 * @param max value returned will be in range [0,max) (exclusive)
426 * @return random 64-bit number
429 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max);
433 * Get an array with a random permutation of the
435 * @param mode GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used, GNUNET_CRYPTO_QUALITY_WEAK otherwise
436 * @param n the size of the array
437 * @return the permutation array (allocated from heap)
440 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
444 * Create a new Session key.
446 * @param key key to initialize
449 GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key);
453 * Encrypt a block with the public key of another
454 * host that uses the same cyper.
456 * @param block the block to encrypt
457 * @param len the size of the block
458 * @param sessionkey the key used to encrypt
459 * @param iv the initialization vector to use, use INITVALUE
461 * @return the size of the encrypted block, -1 for errors
464 GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
465 const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
466 const struct GNUNET_CRYPTO_AesInitializationVector
471 * Decrypt a given block with the sessionkey.
473 * @param block the data to decrypt, encoded as returned by encrypt
474 * @param size how big is the block?
475 * @param sessionkey the key used to decrypt
476 * @param iv the initialization vector to use
477 * @param result address to store the result at
478 * @return -1 on failure, size of decrypted block on success
481 GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
482 const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
483 const struct GNUNET_CRYPTO_AesInitializationVector
488 * @brief Derive an IV
489 * @param iv initialization vector
490 * @param skey session key
491 * @param salt salt for the derivation
492 * @param salt_len size of the salt
493 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
496 GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
497 const struct GNUNET_CRYPTO_AesSessionKey *skey,
498 const void *salt, size_t salt_len, ...);
502 * @brief Derive an IV
503 * @param iv initialization vector
504 * @param skey session key
505 * @param salt salt for the derivation
506 * @param salt_len size of the salt
507 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
510 GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
511 const struct GNUNET_CRYPTO_AesSessionKey *skey,
512 const void *salt, size_t salt_len, va_list argp);
516 * Convert hash to ASCII encoding.
517 * @param block the hash code
518 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
519 * safely cast to char*, a '\\0' termination is set).
522 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode * block,
523 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
527 * Convert short hash to ASCII encoding.
529 * @param block the hash code
530 * @param result where to store the encoding (struct GNUNET_CRYPTO_ShortHashAsciiEncoded can be
531 * safely cast to char*, a '\\0' termination is set).
534 GNUNET_CRYPTO_short_hash_to_enc (const struct GNUNET_CRYPTO_ShortHashCode * block,
535 struct GNUNET_CRYPTO_ShortHashAsciiEncoded *result);
539 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
541 * @param enc the encoding
542 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
543 * @param result where to store the GNUNET_CRYPTO_hash code
544 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
547 GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
548 struct GNUNET_HashCode * result);
552 * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
554 * @param enc the encoding
555 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
556 * @param result where to store the GNUNET_CRYPTO_hash code
557 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
560 GNUNET_CRYPTO_short_hash_from_string2 (const char *enc, size_t enclen,
561 struct GNUNET_CRYPTO_ShortHashCode * result);
565 * Convert ASCII encoding back to struct GNUNET_HashCode
567 * @param enc the encoding
568 * @param result where to store the hash code
569 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
571 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
572 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
576 * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
578 * @param enc the encoding
579 * @param result where to store the GNUNET_CRYPTO_ShortHash
580 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
582 #define GNUNET_CRYPTO_short_hash_from_string(enc, result) \
583 GNUNET_CRYPTO_short_hash_from_string2 (enc, strlen(enc), result)
587 * Compare function for ShortHashCodes, producing a total ordering
590 * @param h1 some hash code
591 * @param h2 some hash code
592 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
595 GNUNET_CRYPTO_short_hash_cmp (const struct GNUNET_CRYPTO_ShortHashCode * h1,
596 const struct GNUNET_CRYPTO_ShortHashCode * h2);
599 * Compute the distance between 2 hashcodes.
600 * The computation must be fast, not involve
601 * a.a or a.e (they're used elsewhere), and
602 * be somewhat consistent. And of course, the
603 * result should be a positive number.
605 * @param a some hash code
606 * @param b some hash code
607 * @return number between 0 and UINT32_MAX
610 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode * a,
611 const struct GNUNET_HashCode * b);
615 * Compute hash of a given block.
617 * @param block the data to hash
618 * @param size size of the block
619 * @param ret pointer to where to write the hashcode
622 GNUNET_CRYPTO_hash (const void *block, size_t size, struct GNUNET_HashCode * ret);
626 * Compute short (256-bit) hash of a given block.
628 * @param block the data to hash
629 * @param size size of the block
630 * @param ret pointer to where to write the hashcode
633 GNUNET_CRYPTO_short_hash (const void *block, size_t size,
634 struct GNUNET_CRYPTO_ShortHashCode * ret);
638 * Double short (256-bit) hash to create a long hash.
640 * @param sh short hash to double
641 * @param dh where to store the (doubled) long hash (not really a hash)
644 GNUNET_CRYPTO_short_hash_double (const struct GNUNET_CRYPTO_ShortHashCode *sh,
645 struct GNUNET_HashCode *dh);
649 * Truncate doubled short hash back to a short hash.
651 * @param dh doubled short hash to reduce again
652 * @param sh where to store the short hash
653 * @return GNUNET_OK on success, GNUNET_SYSERR if this was not a
657 GNUNET_CRYPTO_short_hash_from_truncation (const struct GNUNET_HashCode *dh,
658 struct GNUNET_CRYPTO_ShortHashCode *sh);
662 * Calculate HMAC of a message (RFC 2104)
664 * @param key secret key
665 * @param plaintext input plaintext
666 * @param plaintext_len length of plaintext
667 * @param hmac where to store the hmac
670 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
671 const void *plaintext, size_t plaintext_len,
672 struct GNUNET_HashCode * hmac);
676 * Function called once the hash computation over the
677 * specified file has completed.
680 * @param res resulting hash, NULL on error
682 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
683 const struct GNUNET_HashCode *
688 * Handle to file hashing operation.
690 struct GNUNET_CRYPTO_FileHashContext;
693 * Compute the hash of an entire file.
695 * @param priority scheduling priority to use
696 * @param filename name of file to hash
697 * @param blocksize number of bytes to process in one task
698 * @param callback function to call upon completion
699 * @param callback_cls closure for callback
700 * @return NULL on (immediate) errror
702 struct GNUNET_CRYPTO_FileHashContext *
703 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
704 const char *filename, size_t blocksize,
705 GNUNET_CRYPTO_HashCompletedCallback callback,
710 * Cancel a file hashing operation.
712 * @param fhc operation to cancel (callback must not yet have been invoked)
715 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
719 * Create a random hash code.
721 * @param mode desired quality level
722 * @param result hash code that is randomized
725 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
726 struct GNUNET_HashCode * result);
730 * compute result(delta) = b - a
732 * @param a some hash code
733 * @param b some hash code
734 * @param result set to b - a
737 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode * a,
738 const struct GNUNET_HashCode * b,
739 struct GNUNET_HashCode * result);
743 * compute result(b) = a + delta
745 * @param a some hash code
746 * @param delta some hash code
747 * @param result set to a + delta
750 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode * a,
751 const struct GNUNET_HashCode * delta,
752 struct GNUNET_HashCode * result);
756 * compute result = a ^ b
758 * @param a some hash code
759 * @param b some hash code
760 * @param result set to a ^ b
763 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode * a, const struct GNUNET_HashCode * b,
764 struct GNUNET_HashCode * result);
768 * Convert a hashcode into a key.
770 * @param hc hash code that serves to generate the key
771 * @param skey set to a valid session key
772 * @param iv set to a valid initialization vector
775 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
776 struct GNUNET_CRYPTO_AesSessionKey *skey,
777 struct GNUNET_CRYPTO_AesInitializationVector
782 * Obtain a bit from a hashcode.
784 * @param code the GNUNET_CRYPTO_hash to index bit-wise
785 * @param bit index into the hashcode, [0...159]
786 * @return Bit \a bit from hashcode \a code, -1 for invalid index
789 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode * code, unsigned int bit);
792 * Determine how many low order bits match in two
793 * struct GNUNET_HashCodes. i.e. - 010011 and 011111 share
794 * the first two lowest order bits, and therefore the
795 * return value is two (NOT XOR distance, nor how many
796 * bits match absolutely!).
798 * @param first the first hashcode
799 * @param second the hashcode to compare first to
801 * @return the number of bits that match
804 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode * first,
805 const struct GNUNET_HashCode * second);
809 * Compare function for HashCodes, producing a total ordering
812 * @param h1 some hash code
813 * @param h2 some hash code
814 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
817 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode * h1, const struct GNUNET_HashCode * h2);
821 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
822 * in the XOR metric (Kademlia).
824 * @param h1 some hash code
825 * @param h2 some hash code
826 * @param target some hash code
827 * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
830 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode * h1,
831 const struct GNUNET_HashCode * h2,
832 const struct GNUNET_HashCode * target);
836 * @brief Derive an authentication key
837 * @param key authentication key
838 * @param rkey root key
840 * @param salt_len size of the salt
841 * @param argp pair of void * & size_t for context chunks, terminated by NULL
844 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
845 const struct GNUNET_CRYPTO_AesSessionKey *rkey,
846 const void *salt, size_t salt_len,
851 * @brief Derive an authentication key
852 * @param key authentication key
853 * @param rkey root key
855 * @param salt_len size of the salt
856 * @param ... pair of void * & size_t for context chunks, terminated by NULL
859 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
860 const struct GNUNET_CRYPTO_AesSessionKey *rkey,
861 const void *salt, size_t salt_len, ...);
865 * @param result buffer for the derived key, allocated by caller
866 * @param out_len desired length of the derived key
867 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
868 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
870 * @param xts_len length of xts
871 * @param skm source key material
872 * @param skm_len length of skm
873 * @param ... pair of void * & size_t for context chunks, terminated by NULL
874 * @return GNUNET_YES on success
877 GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
878 const void *xts, size_t xts_len, const void *skm,
879 size_t skm_len, ...);
884 * @param result buffer for the derived key, allocated by caller
885 * @param out_len desired length of the derived key
886 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
887 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
889 * @param xts_len length of xts
890 * @param skm source key material
891 * @param skm_len length of skm
892 * @param argp va_list of void * & size_t pairs for context chunks
893 * @return GNUNET_YES on success
896 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
897 const void *xts, size_t xts_len, const void *skm,
898 size_t skm_len, va_list argp);
903 * @param result buffer for the derived key, allocated by caller
904 * @param out_len desired length of the derived key
906 * @param xts_len length of xts
907 * @param skm source key material
908 * @param skm_len length of skm
909 * @param argp va_list of void * & size_t pairs for context chunks
910 * @return GNUNET_YES on success
913 GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
914 size_t xts_len, const void *skm, size_t skm_len,
920 * @param result buffer for the derived key, allocated by caller
921 * @param out_len desired length of the derived key
923 * @param xts_len length of xts
924 * @param skm source key material
925 * @param skm_len length of skm
926 * @param ... void * & size_t pairs for context chunks
927 * @return GNUNET_YES on success
930 GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
931 size_t xts_len, const void *skm, size_t skm_len, ...);
935 * Convert a public key to a string.
937 * @param pub key to convert
938 * @return string representing 'pub'
941 GNUNET_CRYPTO_rsa_public_key_to_string (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pub);
945 * Convert a string representing a public key to a public key.
947 * @param enc encoded public key
948 * @param enclen number of bytes in enc (without 0-terminator)
949 * @param pub where to store the public key
950 * @return GNUNET_OK on success
953 GNUNET_CRYPTO_rsa_public_key_from_string (const char *enc,
955 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pub);
959 * Encode the private key in a format suitable for
960 * storing it into a file.
961 * @return encoding of the private key
963 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *
964 GNUNET_CRYPTO_rsa_encode_key (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey);
968 * Decode the private key from the data-format back
969 * to the "normal", internal format.
971 * @param buf the buffer where the private key data is stored
972 * @param len the length of the data in 'buffer'
973 * @return NULL on error
975 struct GNUNET_CRYPTO_RsaPrivateKey *
976 GNUNET_CRYPTO_rsa_decode_key (const char *buf, uint16_t len);
980 * Create a new private key by reading it from a file. If the
981 * files does not exist, create a new key and write it to the
982 * file. Caller must free return value. Note that this function
983 * can not guarantee that another process might not be trying
984 * the same operation on the same file at the same time.
985 * If the contents of the file
986 * are invalid the old file is deleted and a fresh key is
989 * @param filename name of file to use for storage
990 * @return new private key, NULL on error (for example,
992 * @deprecated use 'GNUNET_CRYPTO_rsa_key_create_start' instead
994 struct GNUNET_CRYPTO_RsaPrivateKey *
995 GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename);
999 * Open existing private key file and read it. If the
1000 * file does not exist, or the contents of the file are
1001 * invalid, the function fails
1002 * Caller must free returned value.
1004 * @return a private key, NULL on error (for example,
1005 * permission denied) or when file does not exist or contains invalid
1008 struct GNUNET_CRYPTO_RsaPrivateKey *
1009 GNUNET_CRYPTO_rsa_key_create_from_existing_file (const char *filename);
1013 * Handle to cancel private key generation.
1015 struct GNUNET_CRYPTO_RsaKeyGenerationContext;
1019 * Function called upon completion of 'GNUNET_CRYPTO_rsa_key_create_async'.
1021 * @param cls closure
1022 * @param pk NULL on error, otherwise the private key (which must be free'd by the callee)
1023 * @param emsg NULL on success, otherwise an error message
1025 typedef void (*GNUNET_CRYPTO_RsaKeyCallback)(void *cls,
1026 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
1031 * Create a new private key by reading it from a file. If the files
1032 * does not exist, create a new key and write it to the file. If the
1033 * contents of the file are invalid the old file is deleted and a
1034 * fresh key is created.
1036 * @param filename name of file to use for storage
1037 * @param cont function to call when done (or on errors)
1038 * @param cont_cls closure for 'cont'
1039 * @return handle to abort operation, NULL on fatal errors (cont will not be called if NULL is returned)
1041 struct GNUNET_CRYPTO_RsaKeyGenerationContext *
1042 GNUNET_CRYPTO_rsa_key_create_start (const char *filename,
1043 GNUNET_CRYPTO_RsaKeyCallback cont,
1048 * Abort RSA key generation.
1050 * @param gc key generation context to abort
1053 GNUNET_CRYPTO_rsa_key_create_stop (struct GNUNET_CRYPTO_RsaKeyGenerationContext *gc);
1057 * Setup a hostkey file for a peer given the name of the
1058 * configuration file (!). This function is used so that
1059 * at a later point code can be certain that reading a
1060 * hostkey is fast (for example in time-dependent testcases).
1062 * @param cfg_name name of the configuration file to use
1065 GNUNET_CRYPTO_rsa_setup_hostkey (const char *cfg_name);
1069 * Deterministically (!) create a private key using only the
1070 * given HashCode as input to the PRNG.
1072 * @param hc "random" input to PRNG
1073 * @return some private key purely dependent on input
1075 struct GNUNET_CRYPTO_RsaPrivateKey *
1076 GNUNET_CRYPTO_rsa_key_create_from_hash (const struct GNUNET_HashCode *hc);
1080 * Free memory occupied by the private key.
1082 * @param key pointer to the memory to free
1085 GNUNET_CRYPTO_rsa_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *key);
1089 * Extract the public key of the host.
1091 * @param priv the private key
1092 * @param pub where to write the public key
1095 GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey
1097 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
1102 * Get hash of the public key that corresponds to a private key.
1104 * @param key RSA private key
1105 * @param id buffer for hash of the public key
1108 GNUNET_CRYPTO_rsa_get_public_key_hash (struct GNUNET_CRYPTO_RsaPrivateKey *key,
1109 struct GNUNET_HashCode *id);
1113 * Encrypt a block with the public key of another host that uses the
1116 * @param block the block to encrypt
1117 * @param size the size of block
1118 * @param publicKey the encoded public key used to encrypt
1119 * @param target where to store the encrypted block
1120 * @return GNUNET_SYSERR on error, GNUNET_OK if ok
1123 GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size,
1124 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
1126 struct GNUNET_CRYPTO_RsaEncryptedData *target);
1130 * Decrypt a given block with the hostkey.
1132 * @param key the key to use
1133 * @param block the data to decrypt, encoded as returned by encrypt, not consumed
1134 * @param result pointer to a location where the result can be stored
1135 * @param max how many bytes of a result are expected? Must be exact.
1136 * @return the size of the decrypted block (that is, size) or -1 on error
1139 GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
1140 const struct GNUNET_CRYPTO_RsaEncryptedData *block,
1141 void *result, size_t max);
1145 * Sign a given block.
1147 * @param key private key to use for the signing
1148 * @param purpose what to sign (size, purpose)
1149 * @param sig where to write the signature
1150 * @return GNUNET_SYSERR on error, GNUNET_OK on success
1153 GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
1154 const struct GNUNET_CRYPTO_RsaSignaturePurpose *purpose,
1155 struct GNUNET_CRYPTO_RsaSignature *sig);
1159 * Verify signature. Note that the caller MUST have already
1160 * checked that "validate->size" bytes are actually available.
1162 * @param purpose what is the purpose that validate should have?
1163 * @param validate block to validate (size, purpose, data)
1164 * @param sig signature that is being validated
1165 * @param publicKey public key of the signer
1166 * @return GNUNET_OK if ok, GNUNET_SYSERR if invalid
1169 GNUNET_CRYPTO_rsa_verify (uint32_t purpose,
1170 const struct GNUNET_CRYPTO_RsaSignaturePurpose
1172 const struct GNUNET_CRYPTO_RsaSignature *sig,
1173 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
1179 * Function called upon completion of 'GNUNET_CRYPTO_ecc_key_create_async'.
1181 * @param cls closure
1182 * @param pk NULL on error, otherwise the private key (which must be free'd by the callee)
1183 * @param emsg NULL on success, otherwise an error message
1185 typedef void (*GNUNET_CRYPTO_EccKeyCallback)(void *cls,
1186 struct GNUNET_CRYPTO_EccPrivateKey *pk,
1191 * Free memory occupied by ECC key
1193 * @param privatekey pointer to the memory to free
1196 GNUNET_CRYPTO_ecc_key_free (struct GNUNET_CRYPTO_EccPrivateKey *privatekey);
1200 * Extract the public key for the given private key.
1202 * @param priv the private key
1203 * @param pub where to write the public key
1206 GNUNET_CRYPTO_ecc_key_get_public (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
1207 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pub);
1210 * Convert a public key to a string.
1212 * @param pub key to convert
1213 * @return string representing 'pub'
1216 GNUNET_CRYPTO_ecc_public_key_to_string (const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pub);
1220 * Convert a string representing a public key to a public key.
1222 * @param enc encoded public key
1223 * @param enclen number of bytes in enc (without 0-terminator)
1224 * @param pub where to store the public key
1225 * @return GNUNET_OK on success
1228 GNUNET_CRYPTO_ecc_public_key_from_string (const char *enc,
1230 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pub);
1234 * Encode the private key in a format suitable for
1235 * storing it into a file.
1237 * @param key key to encode
1238 * @return encoding of the private key.
1239 * The first 4 bytes give the size of the array, as usual.
1241 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *
1242 GNUNET_CRYPTO_ecc_encode_key (const struct GNUNET_CRYPTO_EccPrivateKey *key);
1246 * Decode the private key from the file-format back
1247 * to the "normal", internal format.
1249 * @param buf the buffer where the private key data is stored
1250 * @param len the length of the data in 'buffer'
1251 * @param validate GNUNET_YES to validate that the key is well-formed,
1252 * GNUNET_NO if the key comes from a totally trusted source
1253 * and validation is considered too expensive
1254 * @return NULL on error
1256 struct GNUNET_CRYPTO_EccPrivateKey *
1257 GNUNET_CRYPTO_ecc_decode_key (const char *buf,
1263 * Create a new private key by reading it from a file. If the
1264 * files does not exist, create a new key and write it to the
1265 * file. Caller must free return value. Note that this function
1266 * can not guarantee that another process might not be trying
1267 * the same operation on the same file at the same time.
1268 * If the contents of the file
1269 * are invalid the old file is deleted and a fresh key is
1272 * @return new private key, NULL on error (for example,
1273 * permission denied)
1275 struct GNUNET_CRYPTO_EccPrivateKey *
1276 GNUNET_CRYPTO_ecc_key_create_from_file (const char *filename);
1280 * Create a new private key by reading our peer's key from
1281 * the file specified in the configuration.
1283 * @return new private key, NULL on error (for example,
1284 * permission denied)
1286 struct GNUNET_CRYPTO_EccPrivateKey *
1287 GNUNET_CRYPTO_ecc_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
1291 * Create a new private key. Caller must free return value. Blocking version
1292 * (blocks to gather entropy).
1294 * @return fresh private key
1296 struct GNUNET_CRYPTO_EccPrivateKey *
1297 GNUNET_CRYPTO_ecc_key_create (void);
1301 * Setup a hostkey file for a peer given the name of the
1302 * configuration file (!). This function is used so that
1303 * at a later point code can be certain that reading a
1304 * hostkey is fast (for example in time-dependent testcases).
1306 * @param cfg_name name of the configuration file to use
1309 GNUNET_CRYPTO_ecc_setup_hostkey (const char *cfg_name);
1313 * Retrieve the identity of the host's peer.
1315 * @param cfg configuration to use
1316 * @param dst pointer to where to write the peer identity
1317 * @return GNUNET_OK on success, GNUNET_SYSERR if the identity
1318 * could not be retrieved
1321 GNUNET_CRYPTO_get_host_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
1322 struct GNUNET_PeerIdentity *dst);
1326 * Derive key material from a public and a private ECC key.
1328 * @param key private key to use for the ECDH (x)
1329 * @param pub public key to use for the ECDY (yG)
1330 * @param key_material where to write the key material (xyG)
1331 * @return GNUNET_SYSERR on error, GNUNET_OK on success
1334 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EccPrivateKey *key,
1335 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pub,
1336 struct GNUNET_HashCode *key_material);
1340 * Sign a given block.
1342 * @param key private key to use for the signing
1343 * @param purpose what to sign (size, purpose)
1344 * @param sig where to write the signature
1345 * @return GNUNET_SYSERR on error, GNUNET_OK on success
1348 GNUNET_CRYPTO_ecc_sign (const struct GNUNET_CRYPTO_EccPrivateKey *key,
1349 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1350 struct GNUNET_CRYPTO_EccSignature *sig);
1356 * @param purpose what is the purpose that the signature should have?
1357 * @param validate block to validate (size, purpose, data)
1358 * @param sig signature that is being validated
1359 * @param publicKey public key of the signer
1360 * @returns GNUNET_OK if ok, GNUNET_SYSERR if invalid
1363 GNUNET_CRYPTO_ecc_verify (uint32_t purpose,
1364 const struct GNUNET_CRYPTO_EccSignaturePurpose
1366 const struct GNUNET_CRYPTO_EccSignature *sig,
1367 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
1371 #if 0 /* keep Emacsens' auto-indent happy */
1379 /* ifndef GNUNET_CRYPTO_LIB_H */
1381 /* end of gnunet_crypto_lib.h */