2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file include/gnunet_crypto_lib.h
23 * @brief cryptographic primitives for GNUnet
25 * @author Christian Grothoff
26 * @author Krista Bennett
27 * @author Gerd Knorr <kraxel@bytesex.org>
28 * @author Ioana Patrascu
29 * @author Tzvetan Horozov
31 * @defgroup crypto Cryptographic operations
32 * @defgroup hash Hashing and operations on hashes
35 #ifndef GNUNET_CRYPTO_LIB_H
36 #define GNUNET_CRYPTO_LIB_H
41 #if 0 /* keep Emacsens' auto-indent happy */
47 * @brief A 512-bit hashcode
49 struct GNUNET_HashCode;
52 * The identity of the host (wraps the signing key of the peer).
54 struct GNUNET_PeerIdentity;
56 #include "gnunet_common.h"
57 #include "gnunet_scheduler_lib.h"
62 * @brief A 512-bit hashcode
64 struct GNUNET_HashCode
66 uint32_t bits[512 / 8 / sizeof (uint32_t)]; /* = 16 */
71 * Maximum length of an ECC signature.
72 * Note: round up to multiple of 8 minus 2 for alignment.
74 #define GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH 126
78 * Desired quality level for random numbers.
81 enum GNUNET_CRYPTO_Quality
84 * No good quality of the operation is needed (i.e.,
85 * random numbers can be pseudo-random).
88 GNUNET_CRYPTO_QUALITY_WEAK,
91 * High-quality operations are desired.
94 GNUNET_CRYPTO_QUALITY_STRONG,
97 * Randomness for IVs etc. is required.
100 GNUNET_CRYPTO_QUALITY_NONCE
105 * @brief length of the sessionkey in bytes (256 BIT sessionkey)
107 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
110 * Length of a hash value
112 #define GNUNET_CRYPTO_HASH_LENGTH (512/8)
115 * How many characters (without 0-terminator) are our ASCII-encoded
116 * public keys (ECDSA/EDDSA/ECDHE).
118 #define GNUNET_CRYPTO_PKEY_ASCII_LENGTH 52
121 * @brief 0-terminated ASCII encoding of a struct GNUNET_HashCode.
123 struct GNUNET_CRYPTO_HashAsciiEncoded
125 unsigned char encoding[104];
129 GNUNET_NETWORK_STRUCT_BEGIN
133 * @brief header of what an ECC signature signs
134 * this must be followed by "size - 8" bytes of
135 * the actual signed data
137 struct GNUNET_CRYPTO_EccSignaturePurpose
140 * How many bytes does this signature sign?
141 * (including this purpose header); in network
144 uint32_t size GNUNET_PACKED;
147 * What does this signature vouch for? This
148 * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
149 * constant (from gnunet_signatures.h). In
150 * network byte order!
152 uint32_t purpose GNUNET_PACKED;
158 * @brief an ECC signature using EdDSA.
159 * See https://gnunet.org/ed25519
161 struct GNUNET_CRYPTO_EddsaSignature
167 unsigned char r[256 / 8];
172 unsigned char s[256 / 8];
179 * @brief an ECC signature using ECDSA
181 struct GNUNET_CRYPTO_EcdsaSignature
187 unsigned char r[256 / 8];
192 unsigned char s[256 / 8];
198 * Public ECC key (always for Curve25519) encoded in a format suitable
199 * for network transmission and EdDSA signatures.
201 struct GNUNET_CRYPTO_EddsaPublicKey
204 * Q consists of an x- and a y-value, each mod p (256 bits), given
205 * here in affine coordinates and Ed25519 standard compact format.
207 unsigned char q_y[256 / 8];
213 * Public ECC key (always for Curve25519) encoded in a format suitable
214 * for network transmission and ECDSA signatures.
216 struct GNUNET_CRYPTO_EcdsaPublicKey
219 * Q consists of an x- and a y-value, each mod p (256 bits), given
220 * here in affine coordinates and Ed25519 standard compact format.
222 unsigned char q_y[256 / 8];
228 * The identity of the host (wraps the signing key of the peer).
230 struct GNUNET_PeerIdentity
232 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
237 * Public ECC key (always for Curve25519) encoded in a format suitable
238 * for network transmission and encryption (ECDH),
239 * See http://cr.yp.to/ecdh.html
241 struct GNUNET_CRYPTO_EcdhePublicKey
244 * Q consists of an x- and a y-value, each mod p (256 bits), given
245 * here in affine coordinates and Ed25519 standard compact format.
247 unsigned char q_y[256 / 8];
252 * Private ECC key encoded for transmission. To be used only for ECDH
253 * key exchange (ECDHE to be precise).
255 struct GNUNET_CRYPTO_EcdhePrivateKey
258 * d is a value mod n, where n has at most 256 bits.
260 unsigned char d[256 / 8];
265 * Private ECC key encoded for transmission. To be used only for ECDSA
268 struct GNUNET_CRYPTO_EcdsaPrivateKey
271 * d is a value mod n, where n has at most 256 bits.
273 unsigned char d[256 / 8];
278 * Private ECC key encoded for transmission. To be used only for EdDSA
281 struct GNUNET_CRYPTO_EddsaPrivateKey
284 * d is a value mod n, where n has at most 256 bits.
286 unsigned char d[256 / 8];
292 * @brief type for session keys
294 struct GNUNET_CRYPTO_SymmetricSessionKey
297 * Actual key for AES.
299 unsigned char aes_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
302 * Actual key for TwoFish.
304 unsigned char twofish_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
308 GNUNET_NETWORK_STRUCT_END
311 * @brief IV for sym cipher
313 * NOTE: must be smaller (!) in size than the
314 * `struct GNUNET_HashCode`.
316 struct GNUNET_CRYPTO_SymmetricInitializationVector
318 unsigned char aes_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
320 unsigned char twofish_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
325 * @brief type for (message) authentication keys
327 struct GNUNET_CRYPTO_AuthKey
329 unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
334 * Size of paillier plain texts and public keys.
335 * Private keys and ciphertexts are twice this size.
337 #define GNUNET_CRYPTO_PAILLIER_BITS 2048
341 * Paillier public key.
343 struct GNUNET_CRYPTO_PaillierPublicKey
348 unsigned char n[GNUNET_CRYPTO_PAILLIER_BITS / 8];
353 * Paillier public key.
355 struct GNUNET_CRYPTO_PaillierPrivateKey
358 * Lambda-component of the private key.
360 unsigned char lambda[GNUNET_CRYPTO_PAILLIER_BITS / 8];
362 * Mu-component of the private key.
364 unsigned char mu[GNUNET_CRYPTO_PAILLIER_BITS / 8];
369 * Paillier ciphertext.
371 struct GNUNET_CRYPTO_PaillierCiphertext
374 * guaranteed minimum number of homomorphic operations with this ciphertext
376 int32_t remaining_ops GNUNET_PACKED;
379 * The bits of the ciphertext.
381 unsigned char bits[GNUNET_CRYPTO_PAILLIER_BITS * 2 / 8];
385 /* **************** Functions and Macros ************* */
389 * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator
392 * @param seed the seed to use
395 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
399 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
401 * @param sum current sum, initially 0
402 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
403 * @param len number of bytes in @a buf, must be multiple of 2
404 * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish to get actual crc16)
407 GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len);
411 * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
413 * @param sum cummulative sum
414 * @return crc16 value
417 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
422 * Calculate the checksum of a buffer in one step.
424 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
425 * @param len number of bytes in @a buf, must be multiple of 2
426 * @return crc16 value
429 GNUNET_CRYPTO_crc16_n (const void *buf, size_t len);
434 * Compute the CRC32 checksum for the first len
435 * bytes of the buffer.
437 * @param buf the data over which we're taking the CRC
438 * @param len the length of the buffer @a buf in bytes
439 * @return the resulting CRC32 checksum
442 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len);
447 * Fill block with a random values.
449 * @param mode desired quality of the random number
450 * @param buffer the buffer to fill
451 * @param length buffer length
454 GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length);
458 * Produce a random value.
460 * @param mode desired quality of the random number
461 * @param i the upper limit (exclusive) for the random number
462 * @return a random value in the interval [0,@a i) (exclusive).
465 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i);
470 * Random on unsigned 64-bit values.
472 * @param mode desired quality of the random number
473 * @param max value returned will be in range [0,@a max) (exclusive)
474 * @return random 64-bit number
477 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max);
482 * Get an array with a random permutation of the
484 * @param mode #GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used,
485 * #GNUNET_CRYPTO_QUALITY_WEAK or #GNUNET_CRYPTO_QUALITY_NONCE otherwise
486 * @param n the size of the array
487 * @return the permutation array (allocated from heap)
490 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
495 * Create a new random session key.
497 * @param key key to initialize
500 GNUNET_CRYPTO_symmetric_create_session_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key);
505 * Encrypt a block using a symmetric sessionkey.
507 * @param block the block to encrypt
508 * @param size the size of the @a block
509 * @param sessionkey the key used to encrypt
510 * @param iv the initialization vector to use, use INITVALUE
512 * @return the size of the encrypted block, -1 for errors
515 GNUNET_CRYPTO_symmetric_encrypt (const void *block, size_t size,
516 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
517 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
523 * Decrypt a given block using a symmetric sessionkey.
525 * @param block the data to decrypt, encoded as returned by encrypt
526 * @param size how big is the block?
527 * @param sessionkey the key used to decrypt
528 * @param iv the initialization vector to use
529 * @param result address to store the result at
530 * @return -1 on failure, size of decrypted block on success
533 GNUNET_CRYPTO_symmetric_decrypt (const void *block, size_t size,
534 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
535 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
541 * @brief Derive an IV
542 * @param iv initialization vector
543 * @param skey session key
544 * @param salt salt for the derivation
545 * @param salt_len size of the @a salt
546 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
549 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
550 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
552 size_t salt_len, ...);
556 * @brief Derive an IV
557 * @param iv initialization vector
558 * @param skey session key
559 * @param salt salt for the derivation
560 * @param salt_len size of the @a salt
561 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
564 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
565 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
573 * Convert hash to ASCII encoding.
574 * @param block the hash code
575 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
576 * safely cast to char*, a '\\0' termination is set).
579 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode * block,
580 struct GNUNET_CRYPTO_HashAsciiEncoded *result);
585 * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
587 * @param enc the encoding
588 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
589 * @param result where to store the hash code
590 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
593 GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
594 struct GNUNET_HashCode *result);
599 * Convert ASCII encoding back to `struct GNUNET_HashCode`
601 * @param enc the encoding
602 * @param result where to store the hash code
603 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
605 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
606 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
612 * Compute the distance between 2 hashcodes. The
613 * computation must be fast, not involve @a a[0] or @a a[4] (they're used
614 * elsewhere), and be somewhat consistent. And of course, the result
615 * 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);
628 * Compute hash of a given block.
630 * @param block the data to hash
631 * @param size size of the @a block
632 * @param ret pointer to where to write the hashcode
635 GNUNET_CRYPTO_hash (const void *block, size_t size, struct GNUNET_HashCode * ret);
640 * Calculate HMAC of a message (RFC 2104)
642 * @param key secret key
643 * @param plaintext input plaintext
644 * @param plaintext_len length of @a plaintext
645 * @param hmac where to store the hmac
648 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
649 const void *plaintext, size_t plaintext_len,
650 struct GNUNET_HashCode * hmac);
654 * Function called once the hash computation over the
655 * specified file has completed.
658 * @param res resulting hash, NULL on error
660 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
661 const struct GNUNET_HashCode *res);
665 * Handle to file hashing operation.
667 struct GNUNET_CRYPTO_FileHashContext;
672 * Compute the hash of an entire file.
674 * @param priority scheduling priority to use
675 * @param filename name of file to hash
676 * @param blocksize number of bytes to process in one task
677 * @param callback function to call upon completion
678 * @param callback_cls closure for @a callback
679 * @return NULL on (immediate) errror
681 struct GNUNET_CRYPTO_FileHashContext *
682 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
683 const char *filename, size_t blocksize,
684 GNUNET_CRYPTO_HashCompletedCallback callback,
689 * Cancel a file hashing operation.
691 * @param fhc operation to cancel (callback must not yet have been invoked)
694 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
699 * Create a random hash code.
701 * @param mode desired quality level
702 * @param result hash code that is randomized
705 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
706 struct GNUNET_HashCode *result);
711 * compute @a result = @a b - @a a
713 * @param a some hash code
714 * @param b some hash code
715 * @param result set to @a b - @a a
718 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
719 const struct GNUNET_HashCode *b,
720 struct GNUNET_HashCode *result);
725 * compute @a result = @a a + @a delta
727 * @param a some hash code
728 * @param delta some hash code
729 * @param result set to @a a + @a delta
732 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a,
733 const struct GNUNET_HashCode *delta,
734 struct GNUNET_HashCode *result);
739 * compute result = a ^ b
741 * @param a some hash code
742 * @param b some hash code
743 * @param result set to @a a ^ @a b
746 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
747 const struct GNUNET_HashCode *b,
748 struct GNUNET_HashCode *result);
753 * Convert a hashcode into a key.
755 * @param hc hash code that serves to generate the key
756 * @param skey set to a valid session key
757 * @param iv set to a valid initialization vector
760 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
761 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
762 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv);
767 * Obtain a bit from a hashcode.
769 * @param code the `struct GNUNET_HashCode` to index bit-wise
770 * @param bit index into the hashcode, [0...159]
771 * @return Bit \a bit from hashcode \a code, -1 for invalid index
774 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code,
780 * Determine how many low order bits match in two
781 * `struct GNUNET_HashCodes`. i.e. - 010011 and 011111 share
782 * the first two lowest order bits, and therefore the
783 * return value is two (NOT XOR distance, nor how many
784 * bits match absolutely!).
786 * @param first the first hashcode
787 * @param second the hashcode to compare first to
788 * @return the number of bits that match
791 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
792 const struct GNUNET_HashCode *second);
797 * Compare function for HashCodes, producing a total ordering
800 * @param h1 some hash code
801 * @param h2 some hash code
802 * @return 1 if @a h1 > @a h2, -1 if @a h1 < @a h2 and 0 if @a h1 == @a h2.
805 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
806 const struct GNUNET_HashCode *h2);
811 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
812 * in the XOR metric (Kademlia).
814 * @param h1 some hash code
815 * @param h2 some hash code
816 * @param target some hash code
817 * @return -1 if @a h1 is closer, 1 if @a h2 is closer and 0 if @a h1== @a h2.
820 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
821 const struct GNUNET_HashCode *h2,
822 const struct GNUNET_HashCode *target);
827 * @brief Derive an authentication key
828 * @param key authentication key
829 * @param rkey root key
831 * @param salt_len size of the salt
832 * @param argp pair of void * & size_t for context chunks, terminated by NULL
835 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
836 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
837 const void *salt, size_t salt_len,
843 * @brief Derive an authentication key
844 * @param key authentication key
845 * @param rkey root key
847 * @param salt_len size of the salt
848 * @param ... pair of void * & size_t for context chunks, terminated by NULL
851 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
852 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
853 const void *salt, size_t salt_len, ...);
859 * @param result buffer for the derived key, allocated by caller
860 * @param out_len desired length of the derived key
861 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
862 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
864 * @param xts_len length of @a xts
865 * @param skm source key material
866 * @param skm_len length of @a skm
867 * @param ... pair of void * & size_t for context chunks, terminated by NULL
868 * @return #GNUNET_YES on success
871 GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
872 const void *xts, size_t xts_len, const void *skm,
873 size_t skm_len, ...);
879 * @param result buffer for the derived key, allocated by caller
880 * @param out_len desired length of the derived key
881 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
882 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
884 * @param xts_len length of @a xts
885 * @param skm source key material
886 * @param skm_len length of @a skm
887 * @param argp va_list of void * & size_t pairs for context chunks
888 * @return #GNUNET_YES on success
891 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
892 const void *xts, size_t xts_len, const void *skm,
893 size_t skm_len, va_list argp);
898 * @param result buffer for the derived key, allocated by caller
899 * @param out_len desired length of the derived key
901 * @param xts_len length of @a xts
902 * @param skm source key material
903 * @param skm_len length of @a skm
904 * @param argp va_list of void * & size_t pairs for context chunks
905 * @return #GNUNET_YES on success
908 GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
909 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 @a xts
920 * @param skm source key material
921 * @param skm_len length of @a 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, ...);
932 * Extract the public key for the given private key.
934 * @param priv the private key
935 * @param pub where to write the public key
938 GNUNET_CRYPTO_ecdsa_key_get_public (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
939 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
943 * Extract the public key for the given private key.
945 * @param priv the private key
946 * @param pub where to write the public key
949 GNUNET_CRYPTO_eddsa_key_get_public (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
950 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
955 * Extract the public key for the given private key.
957 * @param priv the private key
958 * @param pub where to write the public key
961 GNUNET_CRYPTO_ecdhe_key_get_public (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
962 struct GNUNET_CRYPTO_EcdhePublicKey *pub);
966 * Convert a public key to a string.
968 * @param pub key to convert
969 * @return string representing @a pub
972 GNUNET_CRYPTO_ecdsa_public_key_to_string (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
976 * Convert a public key to a string.
978 * @param pub key to convert
979 * @return string representing @a pub
982 GNUNET_CRYPTO_eddsa_public_key_to_string (const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
986 * Convert a string representing a public key to a public key.
988 * @param enc encoded public key
989 * @param enclen number of bytes in @a enc (without 0-terminator)
990 * @param pub where to store the public key
991 * @return #GNUNET_OK on success
994 GNUNET_CRYPTO_ecdsa_public_key_from_string (const char *enc,
996 struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1000 * Convert a string representing a public key to a public key.
1002 * @param enc encoded public key
1003 * @param enclen number of bytes in @a enc (without 0-terminator)
1004 * @param pub where to store the public key
1005 * @return #GNUNET_OK on success
1008 GNUNET_CRYPTO_eddsa_public_key_from_string (const char *enc,
1010 struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1015 * Create a new private key by reading it from a file. If the
1016 * files does not exist, create a new key and write it to the
1017 * file. Caller must free return value. Note that this function
1018 * can not guarantee that another process might not be trying
1019 * the same operation on the same file at the same time.
1020 * If the contents of the file
1021 * are invalid the old file is deleted and a fresh key is
1024 * @param filename name of file to use to store the key
1025 * @return new private key, NULL on error (for example,
1026 * permission denied); free using #GNUNET_free
1028 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1029 GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename);
1034 * Create a new private key by reading it from a file. If the
1035 * files does not exist, create a new key and write it to the
1036 * file. Caller must free return value. Note that this function
1037 * can not guarantee that another process might not be trying
1038 * the same operation on the same file at the same time.
1039 * If the contents of the file
1040 * are invalid the old file is deleted and a fresh key is
1043 * @param filename name of file to use to store the key
1044 * @return new private key, NULL on error (for example,
1045 * permission denied); free using #GNUNET_free
1047 struct GNUNET_CRYPTO_EddsaPrivateKey *
1048 GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename);
1050 struct GNUNET_CONFIGURATION_Handle;
1055 * Create a new private key by reading our peer's key from
1056 * the file specified in the configuration.
1058 * @param cfg the configuration to use
1059 * @return new private key, NULL on error (for example,
1060 * permission denied); free using #GNUNET_free
1062 struct GNUNET_CRYPTO_EddsaPrivateKey *
1063 GNUNET_CRYPTO_eddsa_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
1068 * Create a new private key. Caller must free return value.
1070 * @return fresh private key; free using #GNUNET_free
1072 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1073 GNUNET_CRYPTO_ecdsa_key_create (void);
1078 * Create a new private key. Caller must free return value.
1080 * @return fresh private key; free using #GNUNET_free
1082 struct GNUNET_CRYPTO_EddsaPrivateKey *
1083 GNUNET_CRYPTO_eddsa_key_create (void);
1088 * Create a new private key. Caller must free return value.
1090 * @return fresh private key; free using #GNUNET_free
1092 struct GNUNET_CRYPTO_EcdhePrivateKey *
1093 GNUNET_CRYPTO_ecdhe_key_create (void);
1098 * Clear memory that was used to store a private key.
1100 * @param pk location of the key
1103 GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk);
1108 * Clear memory that was used to store a private key.
1110 * @param pk location of the key
1113 GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk);
1117 * Clear memory that was used to store a private key.
1119 * @param pk location of the key
1122 GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
1127 * Get the shared private key we use for anonymous users.
1129 * @return "anonymous" private key; do not free
1131 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
1132 GNUNET_CRYPTO_ecdsa_key_get_anonymous (void);
1137 * Setup a hostkey file for a peer given the name of the
1138 * configuration file (!). This function is used so that
1139 * at a later point code can be certain that reading a
1140 * hostkey is fast (for example in time-dependent testcases).
1142 * @param cfg_name name of the configuration file to use
1145 GNUNET_CRYPTO_eddsa_setup_hostkey (const char *cfg_name);
1150 * Retrieve the identity of the host's peer.
1152 * @param cfg configuration to use
1153 * @param dst pointer to where to write the peer identity
1154 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
1155 * could not be retrieved
1158 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
1159 struct GNUNET_PeerIdentity *dst);
1162 * Compare two Peer Identities.
1164 * @param first first peer identity
1165 * @param second second peer identity
1166 * @return bigger than 0 if first > second,
1167 * 0 if they are the same
1168 * smaller than 0 if second > first
1171 GNUNET_CRYPTO_cmp_peer_identity (const struct GNUNET_PeerIdentity *first,
1172 const struct GNUNET_PeerIdentity *second);
1177 * Derive key material from a public and a private ECC key.
1179 * @param priv private key to use for the ECDH (x)
1180 * @param pub public key to use for the ECDH (yG)
1181 * @param key_material where to write the key material (xyG)
1182 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1185 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1186 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1187 struct GNUNET_HashCode *key_material);
1192 * EdDSA sign a given block.
1194 * @param priv private key to use for the signing
1195 * @param purpose what to sign (size, purpose)
1196 * @param sig where to write the signature
1197 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1200 GNUNET_CRYPTO_eddsa_sign (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1201 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1202 struct GNUNET_CRYPTO_EddsaSignature *sig);
1207 * ECDSA Sign a given block.
1209 * @param priv private key to use for the signing
1210 * @param purpose what to sign (size, purpose)
1211 * @param sig where to write the signature
1212 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1215 GNUNET_CRYPTO_ecdsa_sign (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1216 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1217 struct GNUNET_CRYPTO_EcdsaSignature *sig);
1221 * Verify EdDSA signature.
1223 * @param purpose what is the purpose that the signature should have?
1224 * @param validate block to validate (size, purpose, data)
1225 * @param sig signature that is being validated
1226 * @param pub public key of the signer
1227 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1230 GNUNET_CRYPTO_eddsa_verify (uint32_t purpose,
1231 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1232 const struct GNUNET_CRYPTO_EddsaSignature *sig,
1233 const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
1239 * Verify ECDSA signature.
1241 * @param purpose what is the purpose that the signature should have?
1242 * @param validate block to validate (size, purpose, data)
1243 * @param sig signature that is being validated
1244 * @param pub public key of the signer
1245 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1248 GNUNET_CRYPTO_ecdsa_verify (uint32_t purpose,
1249 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1250 const struct GNUNET_CRYPTO_EcdsaSignature *sig,
1251 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
1256 * Derive a private key from a given private key and a label.
1257 * Essentially calculates a private key 'h = H(l,P) * d mod n'
1258 * where n is the size of the ECC group and P is the public
1259 * key associated with the private key 'd'.
1261 * @param priv original private key
1262 * @param label label to use for key deriviation
1263 * @param context additional context to use for HKDF of 'h';
1264 * typically the name of the subsystem/application
1265 * @return derived private key
1267 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1268 GNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1270 const char *context);
1275 * Derive a public key from a given public key and a label.
1276 * Essentially calculates a public key 'V = H(l,P) * P'.
1278 * @param pub original public key
1279 * @param label label to use for key deriviation
1280 * @param context additional context to use for HKDF of 'h'.
1281 * typically the name of the subsystem/application
1282 * @param result where to write the derived public key
1285 GNUNET_CRYPTO_ecdsa_public_key_derive (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1287 const char *context,
1288 struct GNUNET_CRYPTO_EcdsaPublicKey *result);
1292 * Output the given MPI value to the given buffer in network
1293 * byte order. The MPI @a val may not be negative.
1295 * @param buf where to output to
1296 * @param size number of bytes in @a buf
1297 * @param val value to write to @a buf
1300 GNUNET_CRYPTO_mpi_print_unsigned (void *buf,
1306 * Convert data buffer into MPI value.
1307 * The buffer is interpreted as network
1308 * byte order, unsigned integer.
1310 * @param result where to store MPI value (allocated)
1311 * @param data raw data (GCRYMPI_FMT_USG)
1312 * @param size number of bytes in @a data
1315 GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result,
1321 * Create a freshly generated paillier public key.
1323 * @param[out] public_key Where to store the public key?
1324 * @param[out] private_key Where to store the private key?
1327 GNUNET_CRYPTO_paillier_create (struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1328 struct GNUNET_CRYPTO_PaillierPrivateKey *private_key);
1332 * Encrypt a plaintext with a paillier public key.
1334 * @param public_key Public key to use.
1335 * @param m Plaintext to encrypt.
1336 * @param desired_ops How many homomorphic ops the caller intends to use
1337 * @param[out] ciphertext Encrytion of @a plaintext with @a public_key.
1338 * @return guaranteed number of supported homomorphic operations >= 1,
1339 * or desired_ops, in case that is lower,
1340 * or -1 if less than one homomorphic operation is possible
1343 GNUNET_CRYPTO_paillier_encrypt (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1346 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext);
1350 * Decrypt a paillier ciphertext with a private key.
1352 * @param private_key Private key to use for decryption.
1353 * @param public_key Public key to use for decryption.
1354 * @param ciphertext Ciphertext to decrypt.
1355 * @param[out] m Decryption of @a ciphertext with @private_key.
1358 GNUNET_CRYPTO_paillier_decrypt (const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key,
1359 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1360 const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
1365 * Compute a ciphertext that represents the sum of the plaintext in @a x1 and @a x2
1367 * Note that this operation can only be done a finite number of times
1368 * before an overflow occurs.
1370 * @param public_key Public key to use for encryption.
1371 * @param c1 Paillier cipher text.
1372 * @param c2 Paillier cipher text.
1373 * @param[out] result Result of the homomorphic operation.
1374 * @return #GNUNET_OK if the result could be computed,
1375 * #GNUNET_SYSERR if no more homomorphic operations are remaining.
1378 GNUNET_CRYPTO_paillier_hom_add (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
1379 const struct GNUNET_CRYPTO_PaillierCiphertext *c1,
1380 const struct GNUNET_CRYPTO_PaillierCiphertext *c2,
1381 struct GNUNET_CRYPTO_PaillierCiphertext *result);
1385 * Get the number of remaining supported homomorphic operations.
1387 * @param c Paillier cipher text.
1388 * @return the number of remaining homomorphic operations
1391 GNUNET_CRYPTO_paillier_hom_get_remaining (const struct GNUNET_CRYPTO_PaillierCiphertext *c);
1393 #if 0 /* keep Emacsens' auto-indent happy */
1401 /* ifndef GNUNET_CRYPTO_LIB_H */
1403 /* end of gnunet_crypto_lib.h */