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];
327 GNUNET_NETWORK_STRUCT_END
330 * @brief IV for sym cipher
332 * NOTE: must be smaller (!) in size than the
333 * struct GNUNET_HashCode.
335 struct GNUNET_CRYPTO_AesInitializationVector
337 unsigned char iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
342 * @brief type for (message) authentication keys
344 struct GNUNET_CRYPTO_AuthKey
346 unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
350 /* **************** Functions and Macros ************* */
353 * Seed a weak random generator. Only GNUNET_CRYPTO_QUALITY_WEAK-mode generator
356 * @param seed the seed to use
359 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
363 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
365 * @param sum current sum, initially 0
366 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
367 * @param len number of bytes in hdr, must be multiple of 2
368 * @return updated crc sum (must be subjected to GNUNET_CRYPTO_crc16_finish to get actual crc16)
371 GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len);
375 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
377 * @param sum cummulative sum
378 * @return crc16 value
381 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
385 * Calculate the checksum of a buffer in one step.
387 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
388 * @param len number of bytes in hdr, must be multiple of 2
389 * @return crc16 value
392 GNUNET_CRYPTO_crc16_n (const void *buf, size_t len);
396 * Compute the CRC32 checksum for the first len
397 * bytes of the buffer.
399 * @param buf the data over which we're taking the CRC
400 * @param len the length of the buffer in bytes
401 * @return the resulting CRC32 checksum
404 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len);
408 * Produce a random value.
410 * @param mode desired quality of the random number
411 * @param i the upper limit (exclusive) for the random number
412 * @return a random value in the interval [0,i) (exclusive).
415 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i);
419 * Random on unsigned 64-bit values.
421 * @param mode desired quality of the random number
422 * @param max value returned will be in range [0,max) (exclusive)
423 * @return random 64-bit number
426 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max);
430 * Get an array with a random permutation of the
432 * @param mode GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used, GNUNET_CRYPTO_QUALITY_WEAK otherwise
433 * @param n the size of the array
434 * @return the permutation array (allocated from heap)
437 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
441 * Create a new Session key.
443 * @param key key to initialize
446 GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key);
450 * Encrypt a block with the public key of another
451 * host that uses the same cyper.
453 * @param block the block to encrypt
454 * @param len the size of the block
455 * @param sessionkey the key used to encrypt
456 * @param iv the initialization vector to use, use INITVALUE
458 * @return the size of the encrypted block, -1 for errors
461 GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
462 const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
463 const struct GNUNET_CRYPTO_AesInitializationVector
468 * Decrypt a given block with the sessionkey.
470 * @param block the data to decrypt, encoded as returned by encrypt
471 * @param size how big is the block?
472 * @param sessionkey the key used to decrypt
473 * @param iv the initialization vector to use
474 * @param result address to store the result at
475 * @return -1 on failure, size of decrypted block on success
478 GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
479 const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
480 const struct GNUNET_CRYPTO_AesInitializationVector
485 * @brief Derive an IV
486 * @param iv initialization vector
487 * @param skey session key
488 * @param salt salt for the derivation
489 * @param salt_len size of the salt
490 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
493 GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
494 const struct GNUNET_CRYPTO_AesSessionKey *skey,
495 const void *salt, size_t salt_len, ...);
499 * @brief Derive an IV
500 * @param iv initialization vector
501 * @param skey session key
502 * @param salt salt for the derivation
503 * @param salt_len size of the salt
504 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
507 GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
508 const struct GNUNET_CRYPTO_AesSessionKey *skey,
509 const void *salt, size_t salt_len, va_list argp);
513 * Convert hash to ASCII encoding.
514 * @param block the hash code
515 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
516 * safely cast to char*, a '\\0' termination is set).
519 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode * block,
520 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
524 * Convert short hash to ASCII encoding.
526 * @param block the hash code
527 * @param result where to store the encoding (struct GNUNET_CRYPTO_ShortHashAsciiEncoded can be
528 * safely cast to char*, a '\\0' termination is set).
531 GNUNET_CRYPTO_short_hash_to_enc (const struct GNUNET_CRYPTO_ShortHashCode * block,
532 struct GNUNET_CRYPTO_ShortHashAsciiEncoded *result);
536 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
538 * @param enc the encoding
539 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
540 * @param result where to store the GNUNET_CRYPTO_hash code
541 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
544 GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
545 struct GNUNET_HashCode * result);
549 * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
551 * @param enc the encoding
552 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
553 * @param result where to store the GNUNET_CRYPTO_hash code
554 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
557 GNUNET_CRYPTO_short_hash_from_string2 (const char *enc, size_t enclen,
558 struct GNUNET_CRYPTO_ShortHashCode * result);
562 * Convert ASCII encoding back to struct GNUNET_HashCode
564 * @param enc the encoding
565 * @param result where to store the hash code
566 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
568 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
569 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
573 * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
575 * @param enc the encoding
576 * @param result where to store the GNUNET_CRYPTO_ShortHash
577 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
579 #define GNUNET_CRYPTO_short_hash_from_string(enc, result) \
580 GNUNET_CRYPTO_short_hash_from_string2 (enc, strlen(enc), result)
584 * Compare function for ShortHashCodes, producing a total ordering
587 * @param h1 some hash code
588 * @param h2 some hash code
589 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
592 GNUNET_CRYPTO_short_hash_cmp (const struct GNUNET_CRYPTO_ShortHashCode * h1,
593 const struct GNUNET_CRYPTO_ShortHashCode * h2);
596 * Compute the distance between 2 hashcodes.
597 * The computation must be fast, not involve
598 * a.a or a.e (they're used elsewhere), and
599 * be somewhat consistent. And of course, the
600 * result should be a positive number.
602 * @param a some hash code
603 * @param b some hash code
604 * @return number between 0 and UINT32_MAX
607 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode * a,
608 const struct GNUNET_HashCode * b);
612 * Compute hash of a given block.
614 * @param block the data to hash
615 * @param size size of the block
616 * @param ret pointer to where to write the hashcode
619 GNUNET_CRYPTO_hash (const void *block, size_t size, struct GNUNET_HashCode * ret);
623 * Compute short (256-bit) hash of a given block.
625 * @param block the data to hash
626 * @param size size of the block
627 * @param ret pointer to where to write the hashcode
630 GNUNET_CRYPTO_short_hash (const void *block, size_t size,
631 struct GNUNET_CRYPTO_ShortHashCode * ret);
635 * Double short (256-bit) hash to create a long hash.
637 * @param sh short hash to double
638 * @param dh where to store the (doubled) long hash (not really a hash)
641 GNUNET_CRYPTO_short_hash_double (const struct GNUNET_CRYPTO_ShortHashCode *sh,
642 struct GNUNET_HashCode *dh);
646 * Truncate doubled short hash back to a short hash.
648 * @param dh doubled short hash to reduce again
649 * @param sh where to store the short hash
650 * @return GNUNET_OK on success, GNUNET_SYSERR if this was not a
654 GNUNET_CRYPTO_short_hash_from_truncation (const struct GNUNET_HashCode *dh,
655 struct GNUNET_CRYPTO_ShortHashCode *sh);
659 * Calculate HMAC of a message (RFC 2104)
661 * @param key secret key
662 * @param plaintext input plaintext
663 * @param plaintext_len length of plaintext
664 * @param hmac where to store the hmac
667 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
668 const void *plaintext, size_t plaintext_len,
669 struct GNUNET_HashCode * hmac);
673 * Function called once the hash computation over the
674 * specified file has completed.
677 * @param res resulting hash, NULL on error
679 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
680 const struct GNUNET_HashCode *
685 * Handle to file hashing operation.
687 struct GNUNET_CRYPTO_FileHashContext;
690 * Compute the hash of an entire file.
692 * @param priority scheduling priority to use
693 * @param filename name of file to hash
694 * @param blocksize number of bytes to process in one task
695 * @param callback function to call upon completion
696 * @param callback_cls closure for callback
697 * @return NULL on (immediate) errror
699 struct GNUNET_CRYPTO_FileHashContext *
700 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
701 const char *filename, size_t blocksize,
702 GNUNET_CRYPTO_HashCompletedCallback callback,
707 * Cancel a file hashing operation.
709 * @param fhc operation to cancel (callback must not yet have been invoked)
712 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
716 * Create a random hash code.
718 * @param mode desired quality level
719 * @param result hash code that is randomized
722 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
723 struct GNUNET_HashCode * result);
727 * compute result(delta) = b - a
729 * @param a some hash code
730 * @param b some hash code
731 * @param result set to b - a
734 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode * a,
735 const struct GNUNET_HashCode * b,
736 struct GNUNET_HashCode * result);
740 * compute result(b) = a + delta
742 * @param a some hash code
743 * @param delta some hash code
744 * @param result set to a + delta
747 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode * a,
748 const struct GNUNET_HashCode * delta,
749 struct GNUNET_HashCode * result);
753 * compute result = a ^ b
755 * @param a some hash code
756 * @param b some hash code
757 * @param result set to a ^ b
760 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode * a, const struct GNUNET_HashCode * b,
761 struct GNUNET_HashCode * result);
765 * Convert a hashcode into a key.
767 * @param hc hash code that serves to generate the key
768 * @param skey set to a valid session key
769 * @param iv set to a valid initialization vector
772 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
773 struct GNUNET_CRYPTO_AesSessionKey *skey,
774 struct GNUNET_CRYPTO_AesInitializationVector
779 * Obtain a bit from a hashcode.
781 * @param code the GNUNET_CRYPTO_hash to index bit-wise
782 * @param bit index into the hashcode, [0...159]
783 * @return Bit \a bit from hashcode \a code, -1 for invalid index
786 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode * code, unsigned int bit);
789 * Determine how many low order bits match in two
790 * struct GNUNET_HashCodes. i.e. - 010011 and 011111 share
791 * the first two lowest order bits, and therefore the
792 * return value is two (NOT XOR distance, nor how many
793 * bits match absolutely!).
795 * @param first the first hashcode
796 * @param second the hashcode to compare first to
798 * @return the number of bits that match
801 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode * first,
802 const struct GNUNET_HashCode * second);
806 * Compare function for HashCodes, producing a total ordering
809 * @param h1 some hash code
810 * @param h2 some hash code
811 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
814 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode * h1, const struct GNUNET_HashCode * h2);
818 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
819 * in the XOR metric (Kademlia).
821 * @param h1 some hash code
822 * @param h2 some hash code
823 * @param target some hash code
824 * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
827 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode * h1,
828 const struct GNUNET_HashCode * h2,
829 const struct GNUNET_HashCode * target);
833 * @brief Derive an authentication key
834 * @param key authentication key
835 * @param rkey root key
837 * @param salt_len size of the salt
838 * @param argp pair of void * & size_t for context chunks, terminated by NULL
841 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
842 const struct GNUNET_CRYPTO_AesSessionKey *rkey,
843 const void *salt, size_t salt_len,
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 ... pair of void * & size_t for context chunks, terminated by NULL
856 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
857 const struct GNUNET_CRYPTO_AesSessionKey *rkey,
858 const void *salt, size_t salt_len, ...);
862 * @param result buffer for the derived key, allocated by caller
863 * @param out_len desired length of the derived key
864 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
865 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
867 * @param xts_len length of xts
868 * @param skm source key material
869 * @param skm_len length of skm
870 * @return GNUNET_YES on success
873 GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
874 const void *xts, size_t xts_len, const void *skm,
875 size_t skm_len, ...);
880 * @param result buffer for the derived key, allocated by caller
881 * @param out_len desired length of the derived key
882 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
883 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
885 * @param xts_len length of xts
886 * @param skm source key material
887 * @param skm_len length of skm
888 * @param argp va_list of void * & size_t pairs for context chunks
889 * @return GNUNET_YES on success
892 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
893 const void *xts, size_t xts_len, const void *skm,
894 size_t skm_len, va_list argp);
899 * @param result buffer for the derived key, allocated by caller
900 * @param out_len desired length of the derived key
902 * @param xts_len length of xts
903 * @param skm source key material
904 * @param skm_len length of skm
905 * @param argp va_list of void * & size_t pairs for context chunks
906 * @return GNUNET_YES on success
909 GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
910 size_t xts_len, const void *skm, size_t skm_len,
916 * @param result buffer for the derived key, allocated by caller
917 * @param out_len desired length of the derived key
919 * @param xts_len length of xts
920 * @param skm source key material
921 * @param skm_len length of skm
922 * @param ... void * & size_t pairs for context chunks
923 * @return GNUNET_YES on success
926 GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
927 size_t xts_len, const void *skm, size_t skm_len, ...);
931 * Convert a public key to a string.
933 * @param pub key to convert
934 * @return string representing 'pub'
937 GNUNET_CRYPTO_rsa_public_key_to_string (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pub);
941 * Convert a string representing a public key to a public key.
943 * @param enc encoded public key
944 * @param enclen number of bytes in enc (without 0-terminator)
945 * @param pub where to store the public key
946 * @return GNUNET_OK on success
949 GNUNET_CRYPTO_rsa_public_key_from_string (const char *enc,
951 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pub);
955 * Encode the private key in a format suitable for
956 * storing it into a file.
957 * @return encoding of the private key
959 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *
960 GNUNET_CRYPTO_rsa_encode_key (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey);
964 * Decode the private key from the data-format back
965 * to the "normal", internal format.
967 * @param buf the buffer where the private key data is stored
968 * @param len the length of the data in 'buffer'
969 * @return NULL on error
971 struct GNUNET_CRYPTO_RsaPrivateKey *
972 GNUNET_CRYPTO_rsa_decode_key (const char *buf, uint16_t len);
976 * Create a new private key by reading it from a file. If the
977 * files does not exist, create a new key and write it to the
978 * file. Caller must free return value. Note that this function
979 * can not guarantee that another process might not be trying
980 * the same operation on the same file at the same time.
981 * If the contents of the file
982 * are invalid the old file is deleted and a fresh key is
985 * @param filename name of file to use for storage
986 * @return new private key, NULL on error (for example,
988 * @deprecated use 'GNUNET_CRYPTO_rsa_key_create_start' instead
990 struct GNUNET_CRYPTO_RsaPrivateKey *
991 GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename);
995 * Handle to cancel private key generation.
997 struct GNUNET_CRYPTO_RsaKeyGenerationContext;
1001 * Function called upon completion of 'GNUNET_CRYPTO_rsa_key_create_async'.
1003 * @param cls closure
1004 * @param pk NULL on error, otherwise the private key (which must be free'd by the callee)
1005 * @param emsg NULL on success, otherwise an error message
1007 typedef void (*GNUNET_CRYPTO_RsaKeyCallback)(void *cls,
1008 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
1013 * Create a new private key by reading it from a file. If the files
1014 * does not exist, create a new key and write it to the file. If the
1015 * contents of the file are invalid the old file is deleted and a
1016 * fresh key is created.
1018 * @param filename name of file to use for storage
1019 * @param cont function to call when done (or on errors)
1020 * @param cont_cls closure for 'cont'
1021 * @return handle to abort operation, NULL on fatal errors (cont will not be called if NULL is returned)
1023 struct GNUNET_CRYPTO_RsaKeyGenerationContext *
1024 GNUNET_CRYPTO_rsa_key_create_start (const char *filename,
1025 GNUNET_CRYPTO_RsaKeyCallback cont,
1030 * Abort RSA key generation.
1032 * @param gc key generation context to abort
1035 GNUNET_CRYPTO_rsa_key_create_stop (struct GNUNET_CRYPTO_RsaKeyGenerationContext *gc);
1039 * Setup a hostkey file for a peer given the name of the
1040 * configuration file (!). This function is used so that
1041 * at a later point code can be certain that reading a
1042 * hostkey is fast (for example in time-dependent testcases).
1044 * @param cfg_name name of the configuration file to use
1047 GNUNET_CRYPTO_rsa_setup_hostkey (const char *cfg_name);
1051 * Deterministically (!) create a private key using only the
1052 * given HashCode as input to the PRNG.
1054 * @param hc "random" input to PRNG
1055 * @return some private key purely dependent on input
1057 struct GNUNET_CRYPTO_RsaPrivateKey *
1058 GNUNET_CRYPTO_rsa_key_create_from_hash (const struct GNUNET_HashCode *hc);
1062 * Free memory occupied by the private key.
1064 * @param key pointer to the memory to free
1067 GNUNET_CRYPTO_rsa_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *key);
1071 * Extract the public key of the host.
1073 * @param priv the private key
1074 * @param pub where to write the public key
1077 GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey
1079 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
1084 * Encrypt a block with the public key of another host that uses the
1087 * @param block the block to encrypt
1088 * @param size the size of block
1089 * @param publicKey the encoded public key used to encrypt
1090 * @param target where to store the encrypted block
1091 * @return GNUNET_SYSERR on error, GNUNET_OK if ok
1094 GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size,
1095 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
1097 struct GNUNET_CRYPTO_RsaEncryptedData *target);
1101 * Decrypt a given block with the hostkey.
1103 * @param key the key to use
1104 * @param block the data to decrypt, encoded as returned by encrypt, not consumed
1105 * @param result pointer to a location where the result can be stored
1106 * @param max how many bytes of a result are expected? Must be exact.
1107 * @return the size of the decrypted block (that is, size) or -1 on error
1110 GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
1111 const struct GNUNET_CRYPTO_RsaEncryptedData *block,
1112 void *result, size_t max);
1116 * Sign a given block.
1118 * @param key private key to use for the signing
1119 * @param purpose what to sign (size, purpose)
1120 * @param sig where to write the signature
1121 * @return GNUNET_SYSERR on error, GNUNET_OK on success
1124 GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
1125 const struct GNUNET_CRYPTO_RsaSignaturePurpose *purpose,
1126 struct GNUNET_CRYPTO_RsaSignature *sig);
1130 * Verify signature. Note that the caller MUST have already
1131 * checked that "validate->size" bytes are actually available.
1133 * @param purpose what is the purpose that validate should have?
1134 * @param validate block to validate (size, purpose, data)
1135 * @param sig signature that is being validated
1136 * @param publicKey public key of the signer
1137 * @return GNUNET_OK if ok, GNUNET_SYSERR if invalid
1140 GNUNET_CRYPTO_rsa_verify (uint32_t purpose,
1141 const struct GNUNET_CRYPTO_RsaSignaturePurpose
1143 const struct GNUNET_CRYPTO_RsaSignature *sig,
1144 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
1150 * Function called upon completion of 'GNUNET_CRYPTO_ecc_key_create_async'.
1152 * @param cls closure
1153 * @param pk NULL on error, otherwise the private key (which must be free'd by the callee)
1154 * @param emsg NULL on success, otherwise an error message
1156 typedef void (*GNUNET_CRYPTO_EccKeyCallback)(void *cls,
1157 struct GNUNET_CRYPTO_EccPrivateKey *pk,
1162 * Free memory occupied by ECC key
1164 * @param privatekey pointer to the memory to free
1167 GNUNET_CRYPTO_ecc_key_free (struct GNUNET_CRYPTO_EccPrivateKey *privatekey);
1171 * Extract the public key for the given private key.
1173 * @param priv the private key
1174 * @param pub where to write the public key
1177 GNUNET_CRYPTO_ecc_key_get_public (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
1178 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pub);
1181 * Convert a public key to a string.
1183 * @param pub key to convert
1184 * @return string representing 'pub'
1187 GNUNET_CRYPTO_ecc_public_key_to_string (const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pub);
1191 * Convert a string representing a public key to a public key.
1193 * @param enc encoded public key
1194 * @param enclen number of bytes in enc (without 0-terminator)
1195 * @param pub where to store the public key
1196 * @return GNUNET_OK on success
1199 GNUNET_CRYPTO_ecc_public_key_from_string (const char *enc,
1201 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pub);
1205 * Encode the private key in a format suitable for
1206 * storing it into a file.
1208 * @param key key to encode
1209 * @return encoding of the private key.
1210 * The first 4 bytes give the size of the array, as usual.
1212 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *
1213 GNUNET_CRYPTO_ecc_encode_key (const struct GNUNET_CRYPTO_EccPrivateKey *key);
1217 * Decode the private key from the file-format back
1218 * to the "normal", internal format.
1220 * @param buf the buffer where the private key data is stored
1221 * @param len the length of the data in 'buffer'
1222 * @return NULL on error
1224 struct GNUNET_CRYPTO_EccPrivateKey *
1225 GNUNET_CRYPTO_ecc_decode_key (const char *buf,
1230 * Create a new private key by reading it from a file. If the
1231 * files does not exist, create a new key and write it to the
1232 * file. Caller must free return value. Note that this function
1233 * can not guarantee that another process might not be trying
1234 * the same operation on the same file at the same time.
1235 * If the contents of the file
1236 * are invalid the old file is deleted and a fresh key is
1239 * @return new private key, NULL on error (for example,
1240 * permission denied)
1242 struct GNUNET_CRYPTO_EccPrivateKey *
1243 GNUNET_CRYPTO_ecc_key_create_from_file (const char *filename);
1247 * Handle to cancel private key generation and state for the
1248 * key generation operation.
1250 struct GNUNET_CRYPTO_EccKeyGenerationContext;
1253 * Create a new private key. Caller must free return value. Blocking version
1254 * (blocks to gather entropy).
1256 * @return fresh private key
1258 struct GNUNET_CRYPTO_EccPrivateKey *
1259 GNUNET_CRYPTO_ecc_key_create (void);
1263 * Create a new private key by reading it from a file. If the files
1264 * does not exist, create a new key and write it to the file. If the
1265 * contents of the file are invalid the old file is deleted and a
1266 * fresh key is created.
1268 * @param filename name of file to use for storage
1269 * @param cont function to call when done (or on errors)
1270 * @param cont_cls closure for 'cont'
1271 * @return handle to abort operation, NULL on fatal errors (cont will not be called if NULL is returned)
1273 struct GNUNET_CRYPTO_EccKeyGenerationContext *
1274 GNUNET_CRYPTO_ecc_key_create_start (const char *filename,
1275 GNUNET_CRYPTO_EccKeyCallback cont,
1280 * Abort ECC key generation.
1282 * @param gc key generation context to abort
1285 GNUNET_CRYPTO_ecc_key_create_stop (struct GNUNET_CRYPTO_EccKeyGenerationContext *gc);
1288 * Setup a hostkey file for a peer given the name of the
1289 * configuration file (!). This function is used so that
1290 * at a later point code can be certain that reading a
1291 * hostkey is fast (for example in time-dependent testcases).
1293 * @param cfg_name name of the configuration file to use
1296 GNUNET_CRYPTO_ecc_setup_hostkey (const char *cfg_name);
1300 * Derive key material from a public and a private ECC key.
1302 * @param key private key to use for the ECDH (x)
1303 * @param pub public key to use for the ECDY (yG)
1304 * @param key_material where to write the key material (xyG)
1305 * @return GNUNET_SYSERR on error, GNUNET_OK on success
1308 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EccPrivateKey *key,
1309 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pub,
1310 struct GNUNET_HashCode *key_material);
1314 * Sign a given block.
1316 * @param key private key to use for the signing
1317 * @param purpose what to sign (size, purpose)
1318 * @param sig where to write the signature
1319 * @return GNUNET_SYSERR on error, GNUNET_OK on success
1322 GNUNET_CRYPTO_ecc_sign (const struct GNUNET_CRYPTO_EccPrivateKey *key,
1323 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1324 struct GNUNET_CRYPTO_EccSignature *sig);
1330 * @param purpose what is the purpose that the signature should have?
1331 * @param validate block to validate (size, purpose, data)
1332 * @param sig signature that is being validated
1333 * @param publicKey public key of the signer
1334 * @returns GNUNET_OK if ok, GNUNET_SYSERR if invalid
1337 GNUNET_CRYPTO_ecc_verify (uint32_t purpose,
1338 const struct GNUNET_CRYPTO_EccSignaturePurpose
1340 const struct GNUNET_CRYPTO_EccSignature *sig,
1341 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
1345 #if 0 /* keep Emacsens' auto-indent happy */
1353 /* ifndef GNUNET_CRYPTO_LIB_H */
1355 /* end of gnunet_crypto_lib.h */