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 190
103 * Maximum length of the public key (q-point, Q = dP) when encoded.
105 #define GNUNET_CRYPTO_ECC_MAX_PUBLIC_KEY_LENGTH 140
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.
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.
291 * Actual length of the q-point binary encoding.
296 * 0-padded q-point in binary encoding (GCRYPT_MPI_FMT_USG).
298 unsigned char key[GNUNET_CRYPTO_ECC_MAX_PUBLIC_KEY_LENGTH];
302 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded
305 * Overall size of the private key.
309 /* followd by S-expression, opaque to applications */
311 /* FIXME: consider defining padding to make this a fixed-size struct */
317 * @brief type for session keys
319 struct GNUNET_CRYPTO_AesSessionKey
324 unsigned char key[GNUNET_CRYPTO_AES_KEY_LENGTH];
329 uint32_t crc32 GNUNET_PACKED;
331 GNUNET_NETWORK_STRUCT_END
334 * @brief IV for sym cipher
336 * NOTE: must be smaller (!) in size than the
337 * struct GNUNET_HashCode.
339 struct GNUNET_CRYPTO_AesInitializationVector
341 unsigned char iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
346 * @brief type for (message) authentication keys
348 struct GNUNET_CRYPTO_AuthKey
350 unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
354 /* **************** Functions and Macros ************* */
357 * Seed a weak random generator. Only GNUNET_CRYPTO_QUALITY_WEAK-mode generator
360 * @param seed the seed to use
363 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
367 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
369 * @param sum current sum, initially 0
370 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
371 * @param len number of bytes in hdr, must be multiple of 2
372 * @return updated crc sum (must be subjected to GNUNET_CRYPTO_crc16_finish to get actual crc16)
375 GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len);
379 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
381 * @param sum cummulative sum
382 * @return crc16 value
385 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
389 * Calculate the checksum of a buffer in one step.
391 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
392 * @param len number of bytes in hdr, must be multiple of 2
393 * @return crc16 value
396 GNUNET_CRYPTO_crc16_n (const void *buf, size_t len);
400 * Compute the CRC32 checksum for the first len
401 * bytes of the buffer.
403 * @param buf the data over which we're taking the CRC
404 * @param len the length of the buffer in bytes
405 * @return the resulting CRC32 checksum
408 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len);
412 * Produce a random value.
414 * @param mode desired quality of the random number
415 * @param i the upper limit (exclusive) for the random number
416 * @return a random value in the interval [0,i) (exclusive).
419 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i);
423 * Random on unsigned 64-bit values.
425 * @param mode desired quality of the random number
426 * @param max value returned will be in range [0,max) (exclusive)
427 * @return random 64-bit number
430 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max);
434 * Get an array with a random permutation of the
436 * @param mode GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used, GNUNET_CRYPTO_QUALITY_WEAK otherwise
437 * @param n the size of the array
438 * @return the permutation array (allocated from heap)
441 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
445 * Create a new Session key.
447 * @param key key to initialize
450 GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key);
454 * Check that a new session key is well-formed.
456 * @param key key to check
457 * @return GNUNET_OK if the key is valid
460 GNUNET_CRYPTO_aes_check_session_key (const struct GNUNET_CRYPTO_AesSessionKey
465 * Encrypt a block with the public key of another
466 * host that uses the same cyper.
468 * @param block the block to encrypt
469 * @param len the size of the block
470 * @param sessionkey the key used to encrypt
471 * @param iv the initialization vector to use, use INITVALUE
473 * @return the size of the encrypted block, -1 for errors
476 GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
477 const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
478 const struct GNUNET_CRYPTO_AesInitializationVector
483 * Decrypt a given block with the sessionkey.
485 * @param block the data to decrypt, encoded as returned by encrypt
486 * @param size how big is the block?
487 * @param sessionkey the key used to decrypt
488 * @param iv the initialization vector to use
489 * @param result address to store the result at
490 * @return -1 on failure, size of decrypted block on success
493 GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
494 const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
495 const struct GNUNET_CRYPTO_AesInitializationVector
500 * @brief Derive an IV
501 * @param iv initialization vector
502 * @param skey session key
503 * @param salt salt for the derivation
504 * @param salt_len size of the salt
505 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
508 GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
509 const struct GNUNET_CRYPTO_AesSessionKey *skey,
510 const void *salt, size_t salt_len, ...);
514 * @brief Derive an IV
515 * @param iv initialization vector
516 * @param skey session key
517 * @param salt salt for the derivation
518 * @param salt_len size of the salt
519 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
522 GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
523 const struct GNUNET_CRYPTO_AesSessionKey *skey,
524 const void *salt, size_t salt_len, va_list argp);
528 * Convert hash to ASCII encoding.
529 * @param block the hash code
530 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
531 * safely cast to char*, a '\\0' termination is set).
534 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode * block,
535 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
539 * Convert short hash to ASCII encoding.
541 * @param block the hash code
542 * @param result where to store the encoding (struct GNUNET_CRYPTO_ShortHashAsciiEncoded can be
543 * safely cast to char*, a '\\0' termination is set).
546 GNUNET_CRYPTO_short_hash_to_enc (const struct GNUNET_CRYPTO_ShortHashCode * block,
547 struct GNUNET_CRYPTO_ShortHashAsciiEncoded *result);
551 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
553 * @param enc the encoding
554 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
555 * @param result where to store the GNUNET_CRYPTO_hash code
556 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
559 GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
560 struct GNUNET_HashCode * result);
564 * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
566 * @param enc the encoding
567 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
568 * @param result where to store the GNUNET_CRYPTO_hash code
569 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
572 GNUNET_CRYPTO_short_hash_from_string2 (const char *enc, size_t enclen,
573 struct GNUNET_CRYPTO_ShortHashCode * result);
577 * Convert ASCII encoding back to struct GNUNET_HashCode
579 * @param enc the encoding
580 * @param result where to store the hash code
581 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
583 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
584 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
588 * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
590 * @param enc the encoding
591 * @param result where to store the GNUNET_CRYPTO_ShortHash
592 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
594 #define GNUNET_CRYPTO_short_hash_from_string(enc, result) \
595 GNUNET_CRYPTO_short_hash_from_string2 (enc, strlen(enc), result)
599 * Compare function for ShortHashCodes, producing a total ordering
602 * @param h1 some hash code
603 * @param h2 some hash code
604 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
607 GNUNET_CRYPTO_short_hash_cmp (const struct GNUNET_CRYPTO_ShortHashCode * h1,
608 const struct GNUNET_CRYPTO_ShortHashCode * h2);
611 * Compute the distance between 2 hashcodes.
612 * The computation must be fast, not involve
613 * a.a or a.e (they're used elsewhere), and
614 * be somewhat consistent. And of course, the
615 * result should be a positive number.
617 * @param a some hash code
618 * @param b some hash code
619 * @return number between 0 and UINT32_MAX
622 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode * a,
623 const struct GNUNET_HashCode * b);
627 * Compute hash of a given block.
629 * @param block the data to hash
630 * @param size size of the block
631 * @param ret pointer to where to write the hashcode
634 GNUNET_CRYPTO_hash (const void *block, size_t size, struct GNUNET_HashCode * ret);
638 * Compute short (256-bit) hash of a given block.
640 * @param block the data to hash
641 * @param size size of the block
642 * @param ret pointer to where to write the hashcode
645 GNUNET_CRYPTO_short_hash (const void *block, size_t size,
646 struct GNUNET_CRYPTO_ShortHashCode * ret);
650 * Double short (256-bit) hash to create a long hash.
652 * @param sh short hash to double
653 * @param dh where to store the (doubled) long hash (not really a hash)
656 GNUNET_CRYPTO_short_hash_double (const struct GNUNET_CRYPTO_ShortHashCode *sh,
657 struct GNUNET_HashCode *dh);
661 * Truncate doubled short hash back to a short hash.
663 * @param dh doubled short hash to reduce again
664 * @param sh where to store the short hash
665 * @return GNUNET_OK on success, GNUNET_SYSERR if this was not a
669 GNUNET_CRYPTO_short_hash_from_truncation (const struct GNUNET_HashCode *dh,
670 struct GNUNET_CRYPTO_ShortHashCode *sh);
674 * Calculate HMAC of a message (RFC 2104)
676 * @param key secret key
677 * @param plaintext input plaintext
678 * @param plaintext_len length of plaintext
679 * @param hmac where to store the hmac
682 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
683 const void *plaintext, size_t plaintext_len,
684 struct GNUNET_HashCode * hmac);
688 * Function called once the hash computation over the
689 * specified file has completed.
692 * @param res resulting hash, NULL on error
694 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
695 const struct GNUNET_HashCode *
700 * Handle to file hashing operation.
702 struct GNUNET_CRYPTO_FileHashContext;
705 * Compute the hash of an entire file.
707 * @param priority scheduling priority to use
708 * @param filename name of file to hash
709 * @param blocksize number of bytes to process in one task
710 * @param callback function to call upon completion
711 * @param callback_cls closure for callback
712 * @return NULL on (immediate) errror
714 struct GNUNET_CRYPTO_FileHashContext *
715 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
716 const char *filename, size_t blocksize,
717 GNUNET_CRYPTO_HashCompletedCallback callback,
722 * Cancel a file hashing operation.
724 * @param fhc operation to cancel (callback must not yet have been invoked)
727 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
731 * Create a random hash code.
733 * @param mode desired quality level
734 * @param result hash code that is randomized
737 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
738 struct GNUNET_HashCode * result);
742 * compute result(delta) = b - a
744 * @param a some hash code
745 * @param b some hash code
746 * @param result set to b - a
749 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode * a,
750 const struct GNUNET_HashCode * b,
751 struct GNUNET_HashCode * result);
755 * compute result(b) = a + delta
757 * @param a some hash code
758 * @param delta some hash code
759 * @param result set to a + delta
762 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode * a,
763 const struct GNUNET_HashCode * delta,
764 struct GNUNET_HashCode * result);
768 * compute result = a ^ b
770 * @param a some hash code
771 * @param b some hash code
772 * @param result set to a ^ b
775 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode * a, const struct GNUNET_HashCode * b,
776 struct GNUNET_HashCode * result);
780 * Convert a hashcode into a key.
782 * @param hc hash code that serves to generate the key
783 * @param skey set to a valid session key
784 * @param iv set to a valid initialization vector
787 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
788 struct GNUNET_CRYPTO_AesSessionKey *skey,
789 struct GNUNET_CRYPTO_AesInitializationVector
794 * Obtain a bit from a hashcode.
796 * @param code the GNUNET_CRYPTO_hash to index bit-wise
797 * @param bit index into the hashcode, [0...159]
798 * @return Bit \a bit from hashcode \a code, -1 for invalid index
801 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode * code, unsigned int bit);
804 * Determine how many low order bits match in two
805 * struct GNUNET_HashCodes. i.e. - 010011 and 011111 share
806 * the first two lowest order bits, and therefore the
807 * return value is two (NOT XOR distance, nor how many
808 * bits match absolutely!).
810 * @param first the first hashcode
811 * @param second the hashcode to compare first to
813 * @return the number of bits that match
816 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode * first,
817 const struct GNUNET_HashCode * second);
821 * Compare function for HashCodes, producing a total ordering
824 * @param h1 some hash code
825 * @param h2 some hash code
826 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
829 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode * h1, const struct GNUNET_HashCode * h2);
833 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
834 * in the XOR metric (Kademlia).
836 * @param h1 some hash code
837 * @param h2 some hash code
838 * @param target some hash code
839 * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
842 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode * h1,
843 const struct GNUNET_HashCode * h2,
844 const struct GNUNET_HashCode * target);
848 * @brief Derive an authentication key
849 * @param key authentication key
850 * @param rkey root key
852 * @param salt_len size of the salt
853 * @param argp pair of void * & size_t for context chunks, terminated by NULL
856 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
857 const struct GNUNET_CRYPTO_AesSessionKey *rkey,
858 const void *salt, size_t salt_len,
863 * @brief Derive an authentication key
864 * @param key authentication key
865 * @param rkey root key
867 * @param salt_len size of the salt
868 * @param ... pair of void * & size_t for context chunks, terminated by NULL
871 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
872 const struct GNUNET_CRYPTO_AesSessionKey *rkey,
873 const void *salt, size_t salt_len, ...);
877 * @param result buffer for the derived key, allocated by caller
878 * @param out_len desired length of the derived key
879 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
880 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
882 * @param xts_len length of xts
883 * @param skm source key material
884 * @param skm_len length of skm
885 * @return GNUNET_YES on success
888 GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
889 const void *xts, size_t xts_len, const void *skm,
890 size_t skm_len, ...);
895 * @param result buffer for the derived key, allocated by caller
896 * @param out_len desired length of the derived key
897 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
898 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
900 * @param xts_len length of xts
901 * @param skm source key material
902 * @param skm_len length of skm
903 * @param argp va_list of void * & size_t pairs for context chunks
904 * @return GNUNET_YES on success
907 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
908 const void *xts, size_t xts_len, const void *skm,
909 size_t skm_len, va_list argp);
914 * @param result buffer for the derived key, allocated by caller
915 * @param out_len desired length of the derived key
917 * @param xts_len length of xts
918 * @param skm source key material
919 * @param skm_len length of skm
920 * @param argp va_list of void * & size_t pairs for context chunks
921 * @return GNUNET_YES on success
924 GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
925 size_t xts_len, const void *skm, size_t skm_len,
931 * @param result buffer for the derived key, allocated by caller
932 * @param out_len desired length of the derived key
934 * @param xts_len length of xts
935 * @param skm source key material
936 * @param skm_len length of skm
937 * @param ... void * & size_t pairs for context chunks
938 * @return GNUNET_YES on success
941 GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
942 size_t xts_len, const void *skm, size_t skm_len, ...);
946 * Convert a public key to a string.
948 * @param pub key to convert
949 * @return string representing 'pub'
952 GNUNET_CRYPTO_rsa_public_key_to_string (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pub);
956 * Convert a string representing a public key to a public key.
958 * @param enc encoded public key
959 * @param enclen number of bytes in enc (without 0-terminator)
960 * @param pub where to store the public key
961 * @return GNUNET_OK on success
964 GNUNET_CRYPTO_rsa_public_key_from_string (const char *enc,
966 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pub);
970 * Encode the private key in a format suitable for
971 * storing it into a file.
972 * @return encoding of the private key
974 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *
975 GNUNET_CRYPTO_rsa_encode_key (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey);
979 * Decode the private key from the data-format back
980 * to the "normal", internal format.
982 * @param buf the buffer where the private key data is stored
983 * @param len the length of the data in 'buffer'
984 * @return NULL on error
986 struct GNUNET_CRYPTO_RsaPrivateKey *
987 GNUNET_CRYPTO_rsa_decode_key (const char *buf, uint16_t len);
991 * Create a new private key by reading it from a file. If the
992 * files does not exist, create a new key and write it to the
993 * file. Caller must free return value. Note that this function
994 * can not guarantee that another process might not be trying
995 * the same operation on the same file at the same time.
996 * If the contents of the file
997 * are invalid the old file is deleted and a fresh key is
1000 * @param filename name of file to use for storage
1001 * @return new private key, NULL on error (for example,
1002 * permission denied)
1003 * @deprecated use 'GNUNET_CRYPTO_rsa_key_create_start' instead
1005 struct GNUNET_CRYPTO_RsaPrivateKey *
1006 GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename);
1010 * Handle to cancel private key generation.
1012 struct GNUNET_CRYPTO_RsaKeyGenerationContext;
1016 * Function called upon completion of 'GNUNET_CRYPTO_rsa_key_create_async'.
1018 * @param cls closure
1019 * @param pk NULL on error, otherwise the private key (which must be free'd by the callee)
1020 * @param emsg NULL on success, otherwise an error message
1022 typedef void (*GNUNET_CRYPTO_RsaKeyCallback)(void *cls,
1023 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
1028 * Create a new private key by reading it from a file. If the files
1029 * does not exist, create a new key and write it to the file. If the
1030 * contents of the file are invalid the old file is deleted and a
1031 * fresh key is created.
1033 * @param filename name of file to use for storage
1034 * @param cont function to call when done (or on errors)
1035 * @param cont_cls closure for 'cont'
1036 * @return handle to abort operation, NULL on fatal errors (cont will not be called if NULL is returned)
1038 struct GNUNET_CRYPTO_RsaKeyGenerationContext *
1039 GNUNET_CRYPTO_rsa_key_create_start (const char *filename,
1040 GNUNET_CRYPTO_RsaKeyCallback cont,
1045 * Abort RSA key generation.
1047 * @param gc key generation context to abort
1050 GNUNET_CRYPTO_rsa_key_create_stop (struct GNUNET_CRYPTO_RsaKeyGenerationContext *gc);
1054 * Setup a hostkey file for a peer given the name of the
1055 * configuration file (!). This function is used so that
1056 * at a later point code can be certain that reading a
1057 * hostkey is fast (for example in time-dependent testcases).
1059 * @param cfg_name name of the configuration file to use
1062 GNUNET_CRYPTO_rsa_setup_hostkey (const char *cfg_name);
1066 * Deterministically (!) create a private key using only the
1067 * given HashCode as input to the PRNG.
1069 * @param hc "random" input to PRNG
1070 * @return some private key purely dependent on input
1072 struct GNUNET_CRYPTO_RsaPrivateKey *
1073 GNUNET_CRYPTO_rsa_key_create_from_hash (const struct GNUNET_HashCode *hc);
1077 * Free memory occupied by the private key.
1079 * @param key pointer to the memory to free
1082 GNUNET_CRYPTO_rsa_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *key);
1086 * Extract the public key of the host.
1088 * @param priv the private key
1089 * @param pub where to write the public key
1092 GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey
1094 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
1099 * Encrypt a block with the public key of another host that uses the
1102 * @param block the block to encrypt
1103 * @param size the size of block
1104 * @param publicKey the encoded public key used to encrypt
1105 * @param target where to store the encrypted block
1106 * @return GNUNET_SYSERR on error, GNUNET_OK if ok
1109 GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size,
1110 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
1112 struct GNUNET_CRYPTO_RsaEncryptedData *target);
1116 * Decrypt a given block with the hostkey.
1118 * @param key the key to use
1119 * @param block the data to decrypt, encoded as returned by encrypt, not consumed
1120 * @param result pointer to a location where the result can be stored
1121 * @param max how many bytes of a result are expected? Must be exact.
1122 * @return the size of the decrypted block (that is, size) or -1 on error
1125 GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
1126 const struct GNUNET_CRYPTO_RsaEncryptedData *block,
1127 void *result, size_t max);
1131 * Sign a given block.
1133 * @param key private key to use for the signing
1134 * @param purpose what to sign (size, purpose)
1135 * @param sig where to write the signature
1136 * @return GNUNET_SYSERR on error, GNUNET_OK on success
1139 GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
1140 const struct GNUNET_CRYPTO_RsaSignaturePurpose *purpose,
1141 struct GNUNET_CRYPTO_RsaSignature *sig);
1145 * Verify signature. Note that the caller MUST have already
1146 * checked that "validate->size" bytes are actually available.
1148 * @param purpose what is the purpose that validate should have?
1149 * @param validate block to validate (size, purpose, data)
1150 * @param sig signature that is being validated
1151 * @param publicKey public key of the signer
1152 * @return GNUNET_OK if ok, GNUNET_SYSERR if invalid
1155 GNUNET_CRYPTO_rsa_verify (uint32_t purpose,
1156 const struct GNUNET_CRYPTO_RsaSignaturePurpose
1158 const struct GNUNET_CRYPTO_RsaSignature *sig,
1159 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
1165 * Function called upon completion of 'GNUNET_CRYPTO_ecc_key_create_async'.
1167 * @param cls closure
1168 * @param pk NULL on error, otherwise the private key (which must be free'd by the callee)
1169 * @param emsg NULL on success, otherwise an error message
1171 typedef void (*GNUNET_CRYPTO_EccKeyCallback)(void *cls,
1172 struct GNUNET_CRYPTO_EccPrivateKey *pk,
1177 * Free memory occupied by ECC key
1179 * @param privatekey pointer to the memory to free
1182 GNUNET_CRYPTO_ecc_key_free (struct GNUNET_CRYPTO_EccPrivateKey *privatekey);
1186 * Extract the public key for the given private key.
1188 * @param priv the private key
1189 * @param pub where to write the public key
1192 GNUNET_CRYPTO_ecc_key_get_public (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
1193 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pub);
1196 * Convert a public key to a string.
1198 * @param pub key to convert
1199 * @return string representing 'pub'
1202 GNUNET_CRYPTO_ecc_public_key_to_string (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pub);
1206 * Convert a string representing a public key to a public key.
1208 * @param enc encoded public key
1209 * @param enclen number of bytes in enc (without 0-terminator)
1210 * @param pub where to store the public key
1211 * @return GNUNET_OK on success
1214 GNUNET_CRYPTO_ecc_public_key_from_string (const char *enc,
1216 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pub);
1220 * Encode the private key in a format suitable for
1221 * storing it into a file.
1223 * @param key key to encode
1224 * @return encoding of the private key.
1225 * The first 4 bytes give the size of the array, as usual.
1227 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *
1228 GNUNET_CRYPTO_ecc_encode_key (const struct GNUNET_CRYPTO_EccPrivateKey *key);
1232 * Decode the private key from the file-format back
1233 * to the "normal", internal format.
1235 * @param buf the buffer where the private key data is stored
1236 * @param len the length of the data in 'buffer'
1237 * @return NULL on error
1239 struct GNUNET_CRYPTO_EccPrivateKey *
1240 GNUNET_CRYPTO_ecc_decode_key (const char *buf,
1245 * Create a new private key by reading it from a file. If the
1246 * files does not exist, create a new key and write it to the
1247 * file. Caller must free return value. Note that this function
1248 * can not guarantee that another process might not be trying
1249 * the same operation on the same file at the same time.
1250 * If the contents of the file
1251 * are invalid the old file is deleted and a fresh key is
1254 * @return new private key, NULL on error (for example,
1255 * permission denied)
1257 struct GNUNET_CRYPTO_EccPrivateKey *
1258 GNUNET_CRYPTO_ecc_key_create_from_file (const char *filename);
1262 * Handle to cancel private key generation and state for the
1263 * key generation operation.
1265 struct GNUNET_CRYPTO_EccKeyGenerationContext;
1269 * Create a new private key by reading it from a file. If the files
1270 * does not exist, create a new key and write it to the file. If the
1271 * contents of the file are invalid the old file is deleted and a
1272 * fresh key is created.
1274 * @param filename name of file to use for storage
1275 * @param cont function to call when done (or on errors)
1276 * @param cont_cls closure for 'cont'
1277 * @return handle to abort operation, NULL on fatal errors (cont will not be called if NULL is returned)
1279 struct GNUNET_CRYPTO_EccKeyGenerationContext *
1280 GNUNET_CRYPTO_ecc_key_create_start (const char *filename,
1281 GNUNET_CRYPTO_EccKeyCallback cont,
1286 * Abort ECC key generation.
1288 * @param gc key generation context to abort
1291 GNUNET_CRYPTO_ecc_key_create_stop (struct GNUNET_CRYPTO_EccKeyGenerationContext *gc);
1294 * Setup a hostkey file for a peer given the name of the
1295 * configuration file (!). This function is used so that
1296 * at a later point code can be certain that reading a
1297 * hostkey is fast (for example in time-dependent testcases).
1299 * @param cfg_name name of the configuration file to use
1302 GNUNET_CRYPTO_ecc_setup_hostkey (const char *cfg_name);
1306 * Sign a given block.
1308 * @param key private key to use for the signing
1309 * @param purpose what to sign (size, purpose)
1310 * @param sig where to write the signature
1311 * @return GNUNET_SYSERR on error, GNUNET_OK on success
1314 GNUNET_CRYPTO_ecc_sign (const struct GNUNET_CRYPTO_EccPrivateKey *key,
1315 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1316 struct GNUNET_CRYPTO_EccSignature *sig);
1322 * @param purpose what is the purpose that the signature should have?
1323 * @param validate block to validate (size, purpose, data)
1324 * @param sig signature that is being validated
1325 * @param publicKey public key of the signer
1326 * @returns GNUNET_OK if ok, GNUNET_SYSERR if invalid
1329 GNUNET_CRYPTO_ecc_verify (uint32_t purpose,
1330 const struct GNUNET_CRYPTO_EccSignaturePurpose
1332 const struct GNUNET_CRYPTO_EccSignature *sig,
1333 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
1338 * This function should only be called in testcases
1339 * where strong entropy gathering is not desired
1340 * (for example, for hostkey generation).
1343 GNUNET_CRYPTO_random_disable_entropy_gathering (void);
1347 * Check if we are using weak random number generation.
1349 * @return GNUNET_YES if weak number generation is on
1350 * (thus will return YES if 'GNUNET_CRYPTO_random_disable_entropy_gathering'
1351 * was called previously).
1354 GNUNET_CRYPTO_random_is_weak (void);
1357 #if 0 /* keep Emacsens' auto-indent happy */
1365 /* ifndef GNUNET_CRYPTO_LIB_H */
1367 /* end of gnunet_crypto_lib.h */