2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 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
65 * @brief length of the sessionkey in bytes (256 BIT sessionkey)
67 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
71 * @brief Length of RSA encrypted data (2048 bit)
73 * We currently do not handle encryption of data
74 * that can not be done in a single call to the
75 * RSA methods (read: large chunks of data).
76 * We should never need that, as we can use
77 * the GNUNET_CRYPTO_hash for larger pieces of data for signing,
78 * and for encryption, we only need to encode sessionkeys!
80 #define GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH 256
84 * Length of an RSA KEY (d,e,len), 2048 bit (=256 octests) key d, 2 byte e
86 #define GNUNET_CRYPTO_RSA_KEY_LENGTH 258
90 * The private information of an RSA key pair.
92 struct GNUNET_CRYPTO_RsaPrivateKey;
96 * @brief 0-terminated ASCII encoding of a GNUNET_HashCode.
98 struct GNUNET_CRYPTO_HashAsciiEncoded
100 unsigned char encoding[104];
106 * @brief an RSA signature
108 struct GNUNET_CRYPTO_RsaSignature
110 unsigned char sig[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH];
115 * @brief header of what an RSA signature signs
116 * this must be followed by "size - 8" bytes of
117 * the actual signed data
119 struct GNUNET_CRYPTO_RsaSignaturePurpose
122 * How many bytes does this signature sign?
123 * (including this purpose header); in network
126 uint32_t size GNUNET_PACKED;
129 * What does this signature vouch for? This
130 * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
131 * constant (from gnunet_signatures.h). In
132 * network byte order!
134 uint32_t purpose GNUNET_PACKED;
140 * @brief A public key.
142 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
145 * In big-endian, must be GNUNET_CRYPTO_RSA_KEY_LENGTH+4
147 uint16_t len GNUNET_PACKED;
150 * Size of n in key; in big-endian!
152 uint16_t sizen GNUNET_PACKED;
155 * The key itself, contains n followed by e.
157 unsigned char key[GNUNET_CRYPTO_RSA_KEY_LENGTH];
160 * Padding (must be 0)
162 uint16_t padding GNUNET_PACKED;
167 * RSA Encrypted data.
169 struct GNUNET_CRYPTO_RsaEncryptedData
171 unsigned char encoding[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH];
176 * @brief type for session keys
178 struct GNUNET_CRYPTO_AesSessionKey
183 unsigned char key[GNUNET_CRYPTO_AES_KEY_LENGTH];
188 uint32_t crc32 GNUNET_PACKED;
193 * @brief IV for sym cipher
195 * NOTE: must be smaller (!) in size than the
198 struct GNUNET_CRYPTO_AesInitializationVector
200 unsigned char iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
204 /* **************** Functions and Macros ************* */
208 * Compute the CRC32 checksum for the first len
209 * bytes of the buffer.
211 * @param buf the data over which we're taking the CRC
212 * @param len the length of the buffer in bytes
213 * @return the resulting CRC32 checksum
215 int32_t GNUNET_CRYPTO_crc32_n (const void *buf,
220 * Produce a random value.
222 * @param mode desired quality of the random number
223 * @param i the upper limit (exclusive) for the random number
224 * @return a random value in the interval [0,i) (exclusive).
226 uint32_t GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode,
231 * Random on unsigned 64-bit values.
233 * @param mode desired quality of the random number
234 * @param max value returned will be in range [0,max) (exclusive)
235 * @return random 64-bit number
237 uint64_t GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
242 * Get an array with a random permutation of the
244 * @param mode GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used, GNUNET_CRYPTO_QUALITY_WEAK otherwise
245 * @param n the size of the array
246 * @return the permutation array (allocated from heap)
248 unsigned int *GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode,
253 * Create a new Session key.
255 * @param key key to initialize
257 void GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey
262 * Check that a new session key is well-formed.
264 * @param key key to check
265 * @return GNUNET_OK if the key is valid
267 int GNUNET_CRYPTO_aes_check_session_key (const struct
268 GNUNET_CRYPTO_AesSessionKey *key);
272 * Encrypt a block with the public key of another
273 * host that uses the same cyper.
275 * @param block the block to encrypt
276 * @param len the size of the block
277 * @param sessionkey the key used to encrypt
278 * @param iv the initialization vector to use, use INITVALUE
280 * @return the size of the encrypted block, -1 for errors
282 ssize_t GNUNET_CRYPTO_aes_encrypt (const void *block,
284 const struct GNUNET_CRYPTO_AesSessionKey
287 GNUNET_CRYPTO_AesInitializationVector *iv,
292 * Decrypt a given block with the sessionkey.
294 * @param block the data to decrypt, encoded as returned by encrypt
295 * @param size how big is the block?
296 * @param sessionkey the key used to decrypt
297 * @param iv the initialization vector to use
298 * @param result address to store the result at
299 * @return -1 on failure, size of decrypted block on success
301 ssize_t GNUNET_CRYPTO_aes_decrypt (const void *block,
303 const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
304 const struct GNUNET_CRYPTO_AesInitializationVector *iv,
309 * Convert hash to ASCII encoding.
310 * @param block the hash code
311 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
312 * safely cast to char*, a '\\0' termination is set).
314 void GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block,
315 struct GNUNET_CRYPTO_HashAsciiEncoded
320 * Convert ASCII encoding back to GNUNET_CRYPTO_hash
321 * @param enc the encoding
322 * @param result where to store the GNUNET_CRYPTO_hash code
323 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
325 int GNUNET_CRYPTO_hash_from_string (const char *enc,
326 GNUNET_HashCode * result);
330 * Compute the distance between 2 hashcodes.
331 * The computation must be fast, not involve
332 * a.a or a.e (they're used elsewhere), and
333 * be somewhat consistent. And of course, the
334 * result should be a positive number.
336 * @param a some hash code
337 * @param b some hash code
338 * @return number between 0 and UINT32_MAX
340 uint32_t GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a,
341 const GNUNET_HashCode * b);
345 * Compute hash of a given block.
347 * @param block the data to hash
348 * @param size size of the block
349 * @param ret pointer to where to write the hashcode
351 void GNUNET_CRYPTO_hash (const void *block,
353 GNUNET_HashCode * ret);
357 * Calculate HMAC of a message (RFC 2104)
359 * @param key secret key
360 * @param plaintext input plaintext
361 * @param plaintext_len length of plaintext
362 * @param hmac where to store the hmac
365 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AesSessionKey *key,
366 const void *plaintext,
367 size_t plaintext_len,
368 GNUNET_HashCode *hmac);
372 * Function called once the hash computation over the
373 * specified file has completed.
376 * @param res resulting hash, NULL on error
378 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
379 const GNUNET_HashCode *
384 * Handle to file hashing operation.
386 struct GNUNET_CRYPTO_FileHashContext;
389 * Compute the hash of an entire file.
391 * @param sched scheduler to use
392 * @param priority scheduling priority to use
393 * @param filename name of file to hash
394 * @param blocksize number of bytes to process in one task
395 * @param callback function to call upon completion
396 * @param callback_cls closure for callback
397 * @return NULL on (immediate) errror
399 struct GNUNET_CRYPTO_FileHashContext *
400 GNUNET_CRYPTO_hash_file (struct GNUNET_SCHEDULER_Handle *sched,
401 enum GNUNET_SCHEDULER_Priority priority,
402 const char *filename,
404 GNUNET_CRYPTO_HashCompletedCallback callback,
409 * Cancel a file hashing operation.
411 * @param fhc operation to cancel (callback must not yet have been invoked)
414 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
418 * Create a random hash code.
420 * @param mode desired quality level
421 * @param result hash code that is randomized
423 void GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
424 GNUNET_HashCode * result);
428 * compute result(delta) = b - a
430 * @param a some hash code
431 * @param b some hash code
432 * @param result set to b - a
434 void GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a,
435 const GNUNET_HashCode * b,
436 GNUNET_HashCode * result);
440 * compute result(b) = a + delta
442 * @param a some hash code
443 * @param delta some hash code
444 * @param result set to a + delta
446 void GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a,
447 const GNUNET_HashCode * delta,
448 GNUNET_HashCode * result);
452 * compute result = a ^ b
454 * @param a some hash code
455 * @param b some hash code
456 * @param result set to a ^ b
458 void GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a,
459 const GNUNET_HashCode * b,
460 GNUNET_HashCode * result);
464 * Convert a hashcode into a key.
466 * @param hc hash code that serves to generate the key
467 * @param skey set to a valid session key
468 * @param iv set to a valid initialization vector
470 void GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc,
471 struct GNUNET_CRYPTO_AesSessionKey *skey,
473 GNUNET_CRYPTO_AesInitializationVector
478 * Obtain a bit from a hashcode.
480 * @param code the GNUNET_CRYPTO_hash to index bit-wise
481 * @param bit index into the hashcode, [0...159]
482 * @return Bit \a bit from hashcode \a code, -1 for invalid index
484 int GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code,
489 * Compare function for HashCodes, producing a total ordering
492 * @param h1 some hash code
493 * @param h2 some hash code
494 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
496 int GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1,
497 const GNUNET_HashCode * h2);
501 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
502 * in the XOR metric (Kademlia).
504 * @param h1 some hash code
505 * @param h2 some hash code
506 * @param target some hash code
507 * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
509 int GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1,
510 const GNUNET_HashCode * h2,
511 const GNUNET_HashCode * target);
516 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
517 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
519 * @param xts_len length of xts
520 * @param skm source key material
521 * @param skm_len length of skm
522 * @param ctx context info
523 * @param ctx_len length of ctx
524 * @param out_len desired length of the derived key
525 * @param result buffer for the derived key, allocated by caller
526 * @return GNUNET_YES on success
529 GNUNET_CRYPTO_hkdf (int xtr_algo, int prf_algo, const void *xts,
530 const size_t xts_len, const void *skm, const size_t skm_len,
531 const void *ctx, const size_t ctx_len, const unsigned long long out_len,
536 * Create a new private key. Caller must free return value.
538 * @return fresh private key
540 struct GNUNET_CRYPTO_RsaPrivateKey *GNUNET_CRYPTO_rsa_key_create (void);
544 * Create a new private key by reading it from a file. If the
545 * files does not exist, create a new key and write it to the
546 * file. Caller must free return value. Note that this function
547 * can not guarantee that another process might not be trying
548 * the same operation on the same file at the same time.
549 * If the contents of the file
550 * are invalid the old file is deleted and a fresh key is
553 * @param filename name of file to use for storage
554 * @return new private key, NULL on error (for example,
557 struct GNUNET_CRYPTO_RsaPrivateKey
558 *GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename);
562 * Deterministically (!) create a private key using only the
563 * given HashCode as input to the PRNG.
565 * @param hc "random" input to PRNG
566 * @return some private key purely dependent on input
568 struct GNUNET_CRYPTO_RsaPrivateKey
569 *GNUNET_CRYPTO_rsa_key_create_from_hash (const GNUNET_HashCode * hc);
573 * Free memory occupied by the private key.
574 * @param hostkey pointer to the memory to free
576 void GNUNET_CRYPTO_rsa_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey);
580 * Extract the public key of the host.
582 * @param priv the private key
583 * @param pub where to write the public key
585 void GNUNET_CRYPTO_rsa_key_get_public (const struct
586 GNUNET_CRYPTO_RsaPrivateKey *priv,
588 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
593 * Encrypt a block with the public key of another host that uses the
596 * @param block the block to encrypt
597 * @param size the size of block
598 * @param publicKey the encoded public key used to encrypt
599 * @param target where to store the encrypted block
600 * @return GNUNET_SYSERR on error, GNUNET_OK if ok
602 int GNUNET_CRYPTO_rsa_encrypt (const void *block,
605 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
607 struct GNUNET_CRYPTO_RsaEncryptedData *target);
611 * Decrypt a given block with the hostkey.
613 * @param key the key to use
614 * @param block the data to decrypt, encoded as returned by encrypt, not consumed
615 * @param result pointer to a location where the result can be stored
616 * @param max how many bytes of a result are expected? Must be exact.
617 * @return the size of the decrypted block (that is, size) or -1 on error
619 ssize_t GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
620 const struct GNUNET_CRYPTO_RsaEncryptedData
627 * Sign a given block.
629 * @param key private key to use for the signing
630 * @param purpose what to sign (size, purpose)
631 * @param sig where to write the signature
632 * @return GNUNET_SYSERR on error, GNUNET_OK on success
634 int GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
635 const struct GNUNET_CRYPTO_RsaSignaturePurpose
637 struct GNUNET_CRYPTO_RsaSignature *sig);
641 * Verify signature. Note that the caller MUST have already
642 * checked that "validate->size" bytes are actually available.
644 * @param purpose what is the purpose that validate should have?
645 * @param validate block to validate (size, purpose, data)
646 * @param sig signature that is being validated
647 * @param publicKey public key of the signer
648 * @return GNUNET_OK if ok, GNUNET_SYSERR if invalid
650 int GNUNET_CRYPTO_rsa_verify (uint32_t purpose,
651 const struct GNUNET_CRYPTO_RsaSignaturePurpose
653 const struct GNUNET_CRYPTO_RsaSignature *sig,
655 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
661 * This function should only be called in testcases
662 * where strong entropy gathering is not desired
663 * (for example, for hostkey generation).
665 void GNUNET_CRYPTO_random_disable_entropy_gathering (void);
667 #if 0 /* keep Emacsens' auto-indent happy */
675 /* ifndef GNUNET_CRYPTO_LIB_H */
677 /* end of gnunet_crypto_lib.h */