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)
76 * @brief Length of RSA encrypted data (2048 bit)
78 * We currently do not handle encryption of data
79 * that can not be done in a single call to the
80 * RSA methods (read: large chunks of data).
81 * We should never need that, as we can use
82 * the GNUNET_CRYPTO_hash for larger pieces of data for signing,
83 * and for encryption, we only need to encode sessionkeys!
85 #define GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH 256
89 * Length of an RSA KEY (n,e,len), 2048 bit (=256 octests) key n, 2 byte e
91 #define GNUNET_CRYPTO_RSA_KEY_LENGTH 258
95 * Length of a hash value
97 #define GNUNET_CRYPTO_HASH_LENGTH 512/8
100 * The private information of an RSA key pair.
102 struct GNUNET_CRYPTO_RsaPrivateKey;
104 GNUNET_NETWORK_STRUCT_BEGIN
107 * GNUnet mandates a certain format for the encoding
108 * of private RSA key information that is provided
109 * by the RSA implementations. This format is used
110 * to serialize a private RSA key (typically when
111 * writing it to disk).
113 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded
116 * Total size of the structure, in bytes, in big-endian!
118 uint16_t len GNUNET_PACKED;
119 uint16_t sizen GNUNET_PACKED; /* in big-endian! */
120 uint16_t sizee GNUNET_PACKED; /* in big-endian! */
121 uint16_t sized GNUNET_PACKED; /* in big-endian! */
122 uint16_t sizep GNUNET_PACKED; /* in big-endian! */
123 uint16_t sizeq GNUNET_PACKED; /* in big-endian! */
124 uint16_t sizedmp1 GNUNET_PACKED; /* in big-endian! */
125 uint16_t sizedmq1 GNUNET_PACKED; /* in big-endian! */
126 /* followed by the actual values */
128 GNUNET_NETWORK_STRUCT_END
132 * @brief 0-terminated ASCII encoding of a GNUNET_HashCode.
134 struct GNUNET_CRYPTO_HashAsciiEncoded
136 unsigned char encoding[104];
143 * @brief 256-bit hashcode
145 struct GNUNET_CRYPTO_ShortHashCode
147 uint32_t bits[256 / 8 / sizeof (uint32_t)]; /* = 8 */
152 * @brief 0-terminated ASCII encoding of a 'struct GNUNET_ShortHashCode'.
154 struct GNUNET_CRYPTO_ShortHashAsciiEncoded
156 unsigned char short_encoding[53];
162 * @brief an RSA signature
164 struct GNUNET_CRYPTO_RsaSignature
166 unsigned char sig[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH];
170 GNUNET_NETWORK_STRUCT_BEGIN
173 * @brief header of what an RSA signature signs
174 * this must be followed by "size - 8" bytes of
175 * the actual signed data
177 struct GNUNET_CRYPTO_RsaSignaturePurpose
180 * How many bytes does this signature sign?
181 * (including this purpose header); in network
184 uint32_t size GNUNET_PACKED;
187 * What does this signature vouch for? This
188 * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
189 * constant (from gnunet_signatures.h). In
190 * network byte order!
192 uint32_t purpose GNUNET_PACKED;
198 * @brief A public key.
200 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
203 * In big-endian, must be GNUNET_CRYPTO_RSA_KEY_LENGTH+4
205 uint16_t len GNUNET_PACKED;
208 * Size of n in key; in big-endian!
210 uint16_t sizen GNUNET_PACKED;
213 * The key itself, contains n followed by e.
215 unsigned char key[GNUNET_CRYPTO_RSA_KEY_LENGTH];
218 * Padding (must be 0)
220 uint16_t padding GNUNET_PACKED;
225 * RSA Encrypted data.
227 struct GNUNET_CRYPTO_RsaEncryptedData
229 unsigned char encoding[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH];
234 * @brief type for session keys
236 struct GNUNET_CRYPTO_AesSessionKey
241 unsigned char key[GNUNET_CRYPTO_AES_KEY_LENGTH];
246 uint32_t crc32 GNUNET_PACKED;
248 GNUNET_NETWORK_STRUCT_END
251 * @brief IV for sym cipher
253 * NOTE: must be smaller (!) in size than the
256 struct GNUNET_CRYPTO_AesInitializationVector
258 unsigned char iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
263 * @brief type for (message) authentication keys
265 struct GNUNET_CRYPTO_AuthKey
267 unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
271 /* **************** Functions and Macros ************* */
274 * Seed a weak random generator. Only GNUNET_CRYPTO_QUALITY_WEAK-mode generator
277 * @param seed the seed to use
280 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
284 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
286 * @param sum current sum, initially 0
287 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
288 * @param len number of bytes in hdr, must be multiple of 2
289 * @return updated crc sum (must be subjected to GNUNET_CRYPTO_crc16_finish to get actual crc16)
292 GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len);
296 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
298 * @param sum cummulative sum
299 * @return crc16 value
302 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
306 * Calculate the checksum of a buffer in one step.
308 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
309 * @param len number of bytes in hdr, must be multiple of 2
310 * @return crc16 value
313 GNUNET_CRYPTO_crc16_n (const void *buf, size_t len);
317 * Compute the CRC32 checksum for the first len
318 * bytes of the buffer.
320 * @param buf the data over which we're taking the CRC
321 * @param len the length of the buffer in bytes
322 * @return the resulting CRC32 checksum
325 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len);
329 * Produce a random value.
331 * @param mode desired quality of the random number
332 * @param i the upper limit (exclusive) for the random number
333 * @return a random value in the interval [0,i) (exclusive).
336 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i);
340 * Random on unsigned 64-bit values.
342 * @param mode desired quality of the random number
343 * @param max value returned will be in range [0,max) (exclusive)
344 * @return random 64-bit number
347 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max);
351 * Get an array with a random permutation of the
353 * @param mode GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used, GNUNET_CRYPTO_QUALITY_WEAK otherwise
354 * @param n the size of the array
355 * @return the permutation array (allocated from heap)
358 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
362 * Create a new Session key.
364 * @param key key to initialize
367 GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key);
371 * Check that a new session key is well-formed.
373 * @param key key to check
374 * @return GNUNET_OK if the key is valid
377 GNUNET_CRYPTO_aes_check_session_key (const struct GNUNET_CRYPTO_AesSessionKey
382 * Encrypt a block with the public key of another
383 * host that uses the same cyper.
385 * @param block the block to encrypt
386 * @param len the size of the block
387 * @param sessionkey the key used to encrypt
388 * @param iv the initialization vector to use, use INITVALUE
390 * @return the size of the encrypted block, -1 for errors
393 GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
394 const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
395 const struct GNUNET_CRYPTO_AesInitializationVector
400 * Decrypt a given block with the sessionkey.
402 * @param block the data to decrypt, encoded as returned by encrypt
403 * @param size how big is the block?
404 * @param sessionkey the key used to decrypt
405 * @param iv the initialization vector to use
406 * @param result address to store the result at
407 * @return -1 on failure, size of decrypted block on success
410 GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
411 const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
412 const struct GNUNET_CRYPTO_AesInitializationVector
417 * @brief Derive an IV
418 * @param iv initialization vector
419 * @param skey session key
420 * @param salt salt for the derivation
421 * @param salt_len size of the salt
422 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
425 GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
426 const struct GNUNET_CRYPTO_AesSessionKey *skey,
427 const void *salt, size_t salt_len, ...);
431 * @brief Derive an IV
432 * @param iv initialization vector
433 * @param skey session key
434 * @param salt salt for the derivation
435 * @param salt_len size of the salt
436 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
439 GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
440 const struct GNUNET_CRYPTO_AesSessionKey *skey,
441 const void *salt, size_t salt_len, va_list argp);
445 * Convert hash to ASCII encoding.
446 * @param block the hash code
447 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
448 * safely cast to char*, a '\\0' termination is set).
451 GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block,
452 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
456 * Convert short hash to ASCII encoding.
458 * @param block the hash code
459 * @param result where to store the encoding (struct GNUNET_CRYPTO_ShortHashAsciiEncoded can be
460 * safely cast to char*, a '\\0' termination is set).
463 GNUNET_CRYPTO_short_hash_to_enc (const struct GNUNET_CRYPTO_ShortHashCode * block,
464 struct GNUNET_CRYPTO_ShortHashAsciiEncoded *result);
468 * Convert ASCII encoding back to a 'GNUNET_HashCode'
470 * @param enc the encoding
471 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
472 * @param result where to store the GNUNET_CRYPTO_hash code
473 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
476 GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
477 GNUNET_HashCode * result);
481 * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
483 * @param enc the encoding
484 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
485 * @param result where to store the GNUNET_CRYPTO_hash code
486 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
489 GNUNET_CRYPTO_short_hash_from_string2 (const char *enc, size_t enclen,
490 struct GNUNET_CRYPTO_ShortHashCode * result);
494 * Convert ASCII encoding back to GNUNET_HashCode
496 * @param enc the encoding
497 * @param result where to store the hash code
498 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
500 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
501 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
505 * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
507 * @param enc the encoding
508 * @param result where to store the GNUNET_CRYPTO_ShortHash
509 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
511 #define GNUNET_CRYPTO_short_hash_from_string(enc, result) \
512 GNUNET_CRYPTO_short_hash_from_string2 (enc, strlen(enc), result)
516 * Compute the distance between 2 hashcodes.
517 * The computation must be fast, not involve
518 * a.a or a.e (they're used elsewhere), and
519 * be somewhat consistent. And of course, the
520 * result should be a positive number.
522 * @param a some hash code
523 * @param b some hash code
524 * @return number between 0 and UINT32_MAX
527 GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a,
528 const GNUNET_HashCode * b);
532 * Compute hash of a given block.
534 * @param block the data to hash
535 * @param size size of the block
536 * @param ret pointer to where to write the hashcode
539 GNUNET_CRYPTO_hash (const void *block, size_t size, GNUNET_HashCode * ret);
543 * Compute short (256-bit) hash of a given block.
545 * @param block the data to hash
546 * @param size size of the block
547 * @param ret pointer to where to write the hashcode
550 GNUNET_CRYPTO_short_hash (const void *block, size_t size,
551 struct GNUNET_CRYPTO_ShortHashCode * ret);
555 * Double short (256-bit) hash to create a long hash.
557 * @param sh short hash to double
558 * @param dh where to store the (doubled) long hash (not really a hash)
561 GNUNET_CRYPTO_short_hash_double (const struct GNUNET_CRYPTO_ShortHashCode *sh,
562 struct GNUNET_HashCode *dh);
566 * Truncate doubled short hash back to a short hash.
568 * @param dh doubled short hash to reduce again
569 * @param sh where to store the short hash
570 * @return GNUNET_OK on success, GNUNET_SYSERR if this was not a
574 GNUNET_CRYPTO_short_hash_from_truncation (const struct GNUNET_HashCode *dh,
575 struct GNUNET_CRYPTO_ShortHashCode *sh);
579 * Calculate HMAC of a message (RFC 2104)
581 * @param key secret key
582 * @param plaintext input plaintext
583 * @param plaintext_len length of plaintext
584 * @param hmac where to store the hmac
587 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
588 const void *plaintext, size_t plaintext_len,
589 GNUNET_HashCode * hmac);
593 * Function called once the hash computation over the
594 * specified file has completed.
597 * @param res resulting hash, NULL on error
599 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
600 const GNUNET_HashCode *
605 * Handle to file hashing operation.
607 struct GNUNET_CRYPTO_FileHashContext;
610 * Compute the hash of an entire file.
612 * @param priority scheduling priority to use
613 * @param filename name of file to hash
614 * @param blocksize number of bytes to process in one task
615 * @param callback function to call upon completion
616 * @param callback_cls closure for callback
617 * @return NULL on (immediate) errror
619 struct GNUNET_CRYPTO_FileHashContext *
620 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
621 const char *filename, size_t blocksize,
622 GNUNET_CRYPTO_HashCompletedCallback callback,
627 * Cancel a file hashing operation.
629 * @param fhc operation to cancel (callback must not yet have been invoked)
632 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
636 * Create a random hash code.
638 * @param mode desired quality level
639 * @param result hash code that is randomized
642 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
643 GNUNET_HashCode * result);
647 * compute result(delta) = b - a
649 * @param a some hash code
650 * @param b some hash code
651 * @param result set to b - a
654 GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a,
655 const GNUNET_HashCode * b,
656 GNUNET_HashCode * result);
660 * compute result(b) = a + delta
662 * @param a some hash code
663 * @param delta some hash code
664 * @param result set to a + delta
667 GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a,
668 const GNUNET_HashCode * delta,
669 GNUNET_HashCode * result);
673 * compute result = a ^ b
675 * @param a some hash code
676 * @param b some hash code
677 * @param result set to a ^ b
680 GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b,
681 GNUNET_HashCode * result);
685 * Convert a hashcode into a key.
687 * @param hc hash code that serves to generate the key
688 * @param skey set to a valid session key
689 * @param iv set to a valid initialization vector
692 GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc,
693 struct GNUNET_CRYPTO_AesSessionKey *skey,
694 struct GNUNET_CRYPTO_AesInitializationVector
699 * Obtain a bit from a hashcode.
701 * @param code the GNUNET_CRYPTO_hash to index bit-wise
702 * @param bit index into the hashcode, [0...159]
703 * @return Bit \a bit from hashcode \a code, -1 for invalid index
706 GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code, unsigned int bit);
709 * Determine how many low order bits match in two
710 * GNUNET_HashCodes. i.e. - 010011 and 011111 share
711 * the first two lowest order bits, and therefore the
712 * return value is two (NOT XOR distance, nor how many
713 * bits match absolutely!).
715 * @param first the first hashcode
716 * @param second the hashcode to compare first to
718 * @return the number of bits that match
721 GNUNET_CRYPTO_hash_matching_bits (const GNUNET_HashCode * first,
722 const GNUNET_HashCode * second);
726 * Compare function for HashCodes, producing a total ordering
729 * @param h1 some hash code
730 * @param h2 some hash code
731 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
734 GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, const GNUNET_HashCode * h2);
738 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
739 * in the XOR metric (Kademlia).
741 * @param h1 some hash code
742 * @param h2 some hash code
743 * @param target some hash code
744 * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
747 GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1,
748 const GNUNET_HashCode * h2,
749 const GNUNET_HashCode * target);
753 * @brief Derive an authentication key
754 * @param key authentication key
755 * @param rkey root key
757 * @param salt_len size of the salt
758 * @param argp pair of void * & size_t for context chunks, terminated by NULL
761 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
762 const struct GNUNET_CRYPTO_AesSessionKey *rkey,
763 const void *salt, size_t salt_len,
768 * @brief Derive an authentication key
769 * @param key authentication key
770 * @param rkey root key
772 * @param salt_len size of the salt
773 * @param ... pair of void * & size_t for context chunks, terminated by NULL
776 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
777 const struct GNUNET_CRYPTO_AesSessionKey *rkey,
778 const void *salt, size_t salt_len, ...);
782 * @param result buffer for the derived key, allocated by caller
783 * @param out_len desired length of the derived key
784 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
785 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
787 * @param xts_len length of xts
788 * @param skm source key material
789 * @param skm_len length of skm
790 * @return GNUNET_YES on success
793 GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
794 const void *xts, size_t xts_len, const void *skm,
795 size_t skm_len, ...);
800 * @param result buffer for the derived key, allocated by caller
801 * @param out_len desired length of the derived key
802 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
803 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
805 * @param xts_len length of xts
806 * @param skm source key material
807 * @param skm_len length of skm
808 * @param argp va_list of void * & size_t pairs for context chunks
809 * @return GNUNET_YES on success
812 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
813 const void *xts, size_t xts_len, const void *skm,
814 size_t skm_len, va_list argp);
819 * @param result buffer for the derived key, allocated by caller
820 * @param out_len desired length of the derived key
822 * @param xts_len length of xts
823 * @param skm source key material
824 * @param skm_len length of skm
825 * @param argp va_list of void * & size_t pairs for context chunks
826 * @return GNUNET_YES on success
829 GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
830 size_t xts_len, const void *skm, size_t skm_len,
836 * @param result buffer for the derived key, allocated by caller
837 * @param out_len desired length of the derived key
839 * @param xts_len length of xts
840 * @param skm source key material
841 * @param skm_len length of skm
842 * @param ... void * & size_t pairs for context chunks
843 * @return GNUNET_YES on success
846 GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
847 size_t xts_len, const void *skm, size_t skm_len, ...);
851 * Create a new private key. Caller must free return value.
853 * @return fresh private key
855 struct GNUNET_CRYPTO_RsaPrivateKey *
856 GNUNET_CRYPTO_rsa_key_create (void);
860 * Convert a public key to a string.
862 * @param pub key to convert
863 * @return string representing 'pub'
866 GNUNET_CRYPTO_rsa_public_key_to_string (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pub);
870 * Convert a string representing a public key to a public key.
872 * @param enc encoded public key
873 * @param enclen number of bytes in enc (without 0-terminator)
874 * @param pub where to store the public key
875 * @return GNUNET_OK on success
878 GNUNET_CRYPTO_rsa_public_key_from_string (const char *enc,
880 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pub);
884 * Encode the private key in a format suitable for
885 * storing it into a file.
886 * @returns encoding of the private key.
887 * The first 4 bytes give the size of the array, as usual.
889 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *
890 GNUNET_CRYPTO_rsa_encode_key (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey);
893 * Decode the private key from the data-format back
894 * to the "normal", internal format.
896 * @param buf the buffer where the private key data is stored
897 * @param len the length of the data in 'buffer'
899 struct GNUNET_CRYPTO_RsaPrivateKey *
900 GNUNET_CRYPTO_rsa_decode_key (const char *buf, uint16_t len);
903 * Create a new private key by reading it from a file. If the
904 * files does not exist, create a new key and write it to the
905 * file. Caller must free return value. Note that this function
906 * can not guarantee that another process might not be trying
907 * the same operation on the same file at the same time.
908 * If the contents of the file
909 * are invalid the old file is deleted and a fresh key is
912 * @param filename name of file to use for storage
913 * @return new private key, NULL on error (for example,
916 struct GNUNET_CRYPTO_RsaPrivateKey *
917 GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename);
921 * Setup a hostkey file for a peer given the name of the
922 * configuration file (!). This function is used so that
923 * at a later point code can be certain that reading a
924 * hostkey is fast (for example in time-dependent testcases).
926 * @param cfg_name name of the configuration file to use
929 GNUNET_CRYPTO_setup_hostkey (const char *cfg_name);
933 * Deterministically (!) create a private key using only the
934 * given HashCode as input to the PRNG.
936 * @param hc "random" input to PRNG
937 * @return some private key purely dependent on input
939 struct GNUNET_CRYPTO_RsaPrivateKey *
940 GNUNET_CRYPTO_rsa_key_create_from_hash (const GNUNET_HashCode * hc);
944 * Free memory occupied by the private key.
945 * @param hostkey pointer to the memory to free
948 GNUNET_CRYPTO_rsa_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey);
952 * Extract the public key of the host.
954 * @param priv the private key
955 * @param pub where to write the public key
958 GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey
960 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
965 * Encrypt a block with the public key of another host that uses the
968 * @param block the block to encrypt
969 * @param size the size of block
970 * @param publicKey the encoded public key used to encrypt
971 * @param target where to store the encrypted block
972 * @return GNUNET_SYSERR on error, GNUNET_OK if ok
975 GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size,
976 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
978 struct GNUNET_CRYPTO_RsaEncryptedData *target);
982 * Decrypt a given block with the hostkey.
984 * @param key the key to use
985 * @param block the data to decrypt, encoded as returned by encrypt, not consumed
986 * @param result pointer to a location where the result can be stored
987 * @param max how many bytes of a result are expected? Must be exact.
988 * @return the size of the decrypted block (that is, size) or -1 on error
991 GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
992 const struct GNUNET_CRYPTO_RsaEncryptedData *block,
993 void *result, size_t max);
997 * Sign a given block.
999 * @param key private key to use for the signing
1000 * @param purpose what to sign (size, purpose)
1001 * @param sig where to write the signature
1002 * @return GNUNET_SYSERR on error, GNUNET_OK on success
1005 GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
1006 const struct GNUNET_CRYPTO_RsaSignaturePurpose *purpose,
1007 struct GNUNET_CRYPTO_RsaSignature *sig);
1011 * Verify signature. Note that the caller MUST have already
1012 * checked that "validate->size" bytes are actually available.
1014 * @param purpose what is the purpose that validate should have?
1015 * @param validate block to validate (size, purpose, data)
1016 * @param sig signature that is being validated
1017 * @param publicKey public key of the signer
1018 * @return GNUNET_OK if ok, GNUNET_SYSERR if invalid
1021 GNUNET_CRYPTO_rsa_verify (uint32_t purpose,
1022 const struct GNUNET_CRYPTO_RsaSignaturePurpose
1024 const struct GNUNET_CRYPTO_RsaSignature *sig,
1025 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
1031 * This function should only be called in testcases
1032 * where strong entropy gathering is not desired
1033 * (for example, for hostkey generation).
1036 GNUNET_CRYPTO_random_disable_entropy_gathering (void);
1039 #if 0 /* keep Emacsens' auto-indent happy */
1047 /* ifndef GNUNET_CRYPTO_LIB_H */
1049 /* end of gnunet_crypto_lib.h */