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 enum GNUNET_CRYPTO_Quality
49 GNUNET_CRYPTO_QUALITY_WEAK,
50 GNUNET_CRYPTO_QUALITY_STRONG
55 * @brief length of the sessionkey in bytes (256 BIT sessionkey)
57 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256/8)
61 * @brief Length of RSA encrypted data (2048 bit)
63 * We currently do not handle encryption of data
64 * that can not be done in a single call to the
65 * RSA methods (read: large chunks of data).
66 * We should never need that, as we can use
67 * the GNUNET_CRYPTO_hash for larger pieces of data for signing,
68 * and for encryption, we only need to encode sessionkeys!
70 #define GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH 256
74 * Length of an RSA KEY (d,e,len), 2048 bit (=256 octests) key d, 2 byte e
76 #define GNUNET_CRYPTO_RSA_KEY_LENGTH 258
80 * The private information of an RSA key pair.
82 struct GNUNET_CRYPTO_RsaPrivateKey;
86 * @brief 0-terminated ASCII encoding of a GNUNET_HashCode.
88 struct GNUNET_CRYPTO_HashAsciiEncoded
90 unsigned char encoding[104];
96 * @brief an RSA signature
98 struct GNUNET_CRYPTO_RsaSignature
100 unsigned char sig[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH];
105 * @brief header of what an RSA signature signs
106 * this must be followed by "size - 8" bytes of
107 * the actual signed data
109 struct GNUNET_CRYPTO_RsaSignaturePurpose
112 * How many bytes does this signature sign?
113 * (including this purpose header); in network
116 uint32_t size GNUNET_PACKED;
119 * What does this signature vouch for? This
120 * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
121 * constant (from gnunet_signatures.h). In
122 * network byte order!
124 uint32_t purpose GNUNET_PACKED;
130 * @brief A public key.
132 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
135 * In big-endian, must be GNUNET_CRYPTO_RSA_KEY_LENGTH+4
137 uint16_t len GNUNET_PACKED;
140 * Size of n in key; in big-endian!
142 uint16_t sizen GNUNET_PACKED;
145 * The key itself, contains n followed by e.
147 unsigned char key[GNUNET_CRYPTO_RSA_KEY_LENGTH];
150 * Padding (must be 0)
152 uint16_t padding GNUNET_PACKED;
157 * RSA Encrypted data.
159 struct GNUNET_CRYPTO_RsaEncryptedData
161 unsigned char encoding[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH];
166 * @brief type for session keys
168 struct GNUNET_CRYPTO_AesSessionKey
173 unsigned char key[GNUNET_CRYPTO_AES_KEY_LENGTH];
178 uint32_t crc32 GNUNET_PACKED;
183 * @brief IV for sym cipher
185 * NOTE: must be smaller (!) in size than the
188 struct GNUNET_CRYPTO_AesInitializationVector
190 unsigned char iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
194 /* **************** Functions and Macros ************* */
198 * Compute the CRC32 checksum for the first len
199 * bytes of the buffer.
201 * @param buf the data over which we're taking the CRC
202 * @param len the length of the buffer in bytes
203 * @return the resulting CRC32 checksum
205 int32_t GNUNET_CRYPTO_crc32_n (const void *buf,
210 * Produce a random value.
212 * @param mode desired quality of the random number
213 * @param i the upper limit (exclusive) for the random number
214 * @return a random value in the interval [0,i) (exclusive).
216 uint32_t GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode,
221 * Random on unsigned 64-bit values. We break them down into signed
222 * 32-bit values and reassemble the 64-bit random value bit-wise.
224 * @param mode desired quality of the random number
225 * @param max value returned will be in range [0,max) (exclusive)
226 * @return random 64-bit number
228 uint64_t GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
233 * Get an array with a random permutation of the
235 * @param mode GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used, GNUNET_CRYPTO_QUALITY_WEAK otherwise
236 * @param n the size of the array
237 * @return the permutation array (allocated from heap)
239 unsigned int *GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode,
244 * Create a new Session key.
246 void GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey
251 * Check that a new session key is well-formed.
253 * @return GNUNET_OK if the key is valid
255 int GNUNET_CRYPTO_aes_check_session_key (const struct
256 GNUNET_CRYPTO_AesSessionKey *key);
260 * Encrypt a block with the public key of another
261 * host that uses the same cyper.
263 * @param block the block to encrypt
264 * @param len the size of the block
265 * @param sessionkey the key used to encrypt
266 * @param iv the initialization vector to use, use INITVALUE
268 * @returns the size of the encrypted block, -1 for errors
270 ssize_t GNUNET_CRYPTO_aes_encrypt (const void *block,
272 const struct GNUNET_CRYPTO_AesSessionKey
275 GNUNET_CRYPTO_AesInitializationVector *iv,
280 * Decrypt a given block with the sessionkey.
282 * @param block the data to decrypt, encoded as returned by encrypt
283 * @param size how big is the block?
284 * @param sessionkey the key used to decrypt
285 * @param iv the initialization vector to use
286 * @param result address to store the result at
287 * @return -1 on failure, size of decrypted block on success
289 ssize_t GNUNET_CRYPTO_aes_decrypt (const void *block,
291 const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
292 const struct GNUNET_CRYPTO_AesInitializationVector *iv,
297 * Convert GNUNET_CRYPTO_hash to ASCII encoding.
298 * @param block the GNUNET_CRYPTO_hash code
299 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
300 * safely cast to char*, a '\0' termination is set).
302 void GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block,
303 struct GNUNET_CRYPTO_HashAsciiEncoded
308 * Convert ASCII encoding back to GNUNET_CRYPTO_hash
309 * @param enc the encoding
310 * @param result where to store the GNUNET_CRYPTO_hash code
311 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
313 int GNUNET_CRYPTO_hash_from_string (const char *enc,
314 GNUNET_HashCode * result);
318 * Compute the distance between 2 hashcodes.
319 * The computation must be fast, not involve
320 * a.a or a.e (they're used elsewhere), and
321 * be somewhat consistent. And of course, the
322 * result should be a positive number.
323 * @return number between 0 and 65536
325 uint32_t GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a,
326 const GNUNET_HashCode * b);
330 * Compute hash of a given block.
332 * @param block the data to hash
333 * @param size size of the block
334 * @param ret pointer to where to write the hashcode
336 void GNUNET_CRYPTO_hash (const void *block,
338 GNUNET_HashCode * ret);
342 * Function called once the hash computation over the
343 * specified file has completed.
346 * @param res resulting hash, NULL on error
348 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
349 const GNUNET_HashCode *
354 * Compute the hash of an entire file.
356 * @param sched scheduler to use
357 * @param priority scheduling priority to use
358 * @param run_on_shutdown should we complete even on shutdown?
359 * @param filename name of file to hash
360 * @param blocksize number of bytes to process in one task
361 * @param callback function to call upon completion
362 * @param callback_cls closure for callback
364 void GNUNET_CRYPTO_hash_file (struct GNUNET_SCHEDULER_Handle *sched,
365 enum GNUNET_SCHEDULER_Priority priority,
367 const char *filename,
369 GNUNET_CRYPTO_HashCompletedCallback callback,
374 * Create a random hash code.
376 void GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
377 GNUNET_HashCode * result);
381 * compute result(delta) = b - a
383 void GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a,
384 const GNUNET_HashCode * b,
385 GNUNET_HashCode * result);
389 * compute result(b) = a + delta
391 void GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a,
392 const GNUNET_HashCode * delta,
393 GNUNET_HashCode * result);
397 * compute result = a ^ b
399 void GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a,
400 const GNUNET_HashCode * b,
401 GNUNET_HashCode * result);
405 * Convert a hashcode into a key.
407 void GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc,
408 struct GNUNET_CRYPTO_AesSessionKey *skey,
410 GNUNET_CRYPTO_AesInitializationVector
415 * Obtain a bit from a hashcode.
416 * @param code the GNUNET_CRYPTO_hash to index bit-wise
417 * @param bit index into the hashcode, [0...159]
418 * @return Bit \a bit from hashcode \a code, -1 for invalid index
420 int GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code,
425 * Compare function for HashCodes, producing a total ordering
427 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
429 int GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1,
430 const GNUNET_HashCode * h2);
434 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
435 * in the XOR metric (Kademlia).
436 * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
438 int GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1,
439 const GNUNET_HashCode * h2,
440 const GNUNET_HashCode * target);
444 * Create a new private key. Caller must free return value.
446 struct GNUNET_CRYPTO_RsaPrivateKey *GNUNET_CRYPTO_rsa_key_create (void);
450 * Create a new private key by reading it from a file. If the
451 * files does not exist, create a new key and write it to the
452 * file. Caller must free return value. Note that this function
453 * can not guarantee that another process might not be trying
454 * the same operation on the same file at the same time. The
455 * caller must somehow know that the file either already exists
456 * with a valid key OR be sure that no other process is calling
457 * this function at the same time. If the contents of the file
458 * are invalid the old file is deleted and a fresh key is
461 * @return new private key, NULL on error (for example,
464 struct GNUNET_CRYPTO_RsaPrivateKey
465 *GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename);
469 * Deterministically (!) create a private key using only the
470 * given HashCode as input to the PRNG.
472 struct GNUNET_CRYPTO_RsaPrivateKey
473 *GNUNET_CRYPTO_rsa_key_create_from_hash (const GNUNET_HashCode * input);
477 * Free memory occupied by the private key.
478 * @param hostkey pointer to the memory to free
480 void GNUNET_CRYPTO_rsa_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey);
484 * Extract the public key of the host.
485 * @param result where to write the result.
487 void GNUNET_CRYPTO_rsa_key_get_public (const struct
488 GNUNET_CRYPTO_RsaPrivateKey *hostkey,
490 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
495 * Encrypt a block with the public key of another host that uses the
498 * @param block the block to encrypt
499 * @param size the size of block
500 * @param publicKey the encoded public key used to encrypt
501 * @param target where to store the encrypted block
502 * @returns GNUNET_SYSERR on error, GNUNET_OK if ok
504 int GNUNET_CRYPTO_rsa_encrypt (const void *block,
507 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
509 struct GNUNET_CRYPTO_RsaEncryptedData *target);
513 * Decrypt a given block with the hostkey.
515 * @param key the key to use
516 * @param block the data to decrypt, encoded as returned by encrypt, not consumed
517 * @param result pointer to a location where the result can be stored
518 * @param size how many bytes of a result are expected? Must be exact.
519 * @returns the size of the decrypted block (that is, size) or -1 on error
521 ssize_t GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
522 const struct GNUNET_CRYPTO_RsaEncryptedData
529 * Sign a given block.
531 * @param key private key to use for the signing
532 * @param purpose what to sign (size, purpose)
533 * @param result where to write the signature
534 * @return GNUNET_SYSERR on error, GNUNET_OK on success
536 int GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
537 const struct GNUNET_CRYPTO_RsaSignaturePurpose
539 struct GNUNET_CRYPTO_RsaSignature *result);
543 * Verify signature. Note that the caller MUST have already
544 * checked that "validate->size" bytes are actually available.
546 * @param purpose what is the purpose that validate should have?
547 * @param validate block to validate (size, purpose, data)
548 * @param sig signature that is being validated
549 * @param publicKey public key of the signer
550 * @returns GNUNET_OK if ok, GNUNET_SYSERR if invalid
552 int GNUNET_CRYPTO_rsa_verify (uint32_t purpose,
553 const struct GNUNET_CRYPTO_RsaSignaturePurpose
555 const struct GNUNET_CRYPTO_RsaSignature *sig,
557 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
563 * This function should only be called in testcases
564 * where strong entropy gathering is not desired
565 * (for example, for hostkey generation).
567 void GNUNET_CRYPTO_random_disable_entropy_gathering (void);
569 #if 0 /* keep Emacsens' auto-indent happy */
577 /* ifndef GNUNET_CRYPTO_LIB_H */
579 /* end of gnunet_crypto_lib.h */