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 int GNUNET_CRYPTO_crc32_n (const void *buf, unsigned int len);
209 * Produce a random value.
211 * @param i the upper limit (exclusive) for the random number
212 * @return a random value in the interval [0,i[.
214 unsigned int GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality,
219 * Random on unsigned 64-bit values. We break them down into signed
220 * 32-bit values and reassemble the 64-bit random value bit-wise.
222 unsigned long long GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
223 unsigned long long u);
227 * Get an array with a random permutation of the
229 * @param mode GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used, GNUNET_CRYPTO_QUALITY_WEAK otherwise
230 * @param n the size of the array
231 * @return the permutation array (allocated from heap)
233 unsigned int *GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode,
238 * Create a new Session key.
240 void GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey
245 * Check that a new session key is well-formed.
247 * @return GNUNET_OK if the key is valid
249 int GNUNET_CRYPTO_aes_check_session_key (const struct
250 GNUNET_CRYPTO_AesSessionKey *key);
254 * Encrypt a block with the public key of another
255 * host that uses the same cyper.
257 * @param block the block to encrypt
258 * @param len the size of the block
259 * @param sessionkey the key used to encrypt
260 * @param iv the initialization vector to use, use INITVALUE
262 * @returns the size of the encrypted block, -1 for errors
264 int GNUNET_CRYPTO_aes_encrypt (const void *block,
266 const struct GNUNET_CRYPTO_AesSessionKey
269 GNUNET_CRYPTO_AesInitializationVector *iv,
274 * Decrypt a given block with the sessionkey.
276 * @param block the data to decrypt, encoded as returned by encrypt
277 * @param size how big is the block?
278 * @param sessionkey the key used to decrypt
279 * @param iv the initialization vector to use
280 * @param result address to store the result at
281 * @return -1 on failure, size of decrypted block on success
283 int GNUNET_CRYPTO_aes_decrypt (const void *block, uint16_t size,
284 const struct GNUNET_CRYPTO_AesSessionKey *sessionkey,
285 const struct GNUNET_CRYPTO_AesInitializationVector *iv,
290 * Convert GNUNET_CRYPTO_hash to ASCII encoding.
291 * @param block the GNUNET_CRYPTO_hash code
292 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
293 * safely cast to char*, a '\0' termination is set).
295 void GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block,
296 struct GNUNET_CRYPTO_HashAsciiEncoded
301 * Convert ASCII encoding back to GNUNET_CRYPTO_hash
302 * @param enc the encoding
303 * @param result where to store the GNUNET_CRYPTO_hash code
304 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
306 int GNUNET_CRYPTO_hash_from_string (const char *enc,
307 GNUNET_HashCode * result);
311 * Compute the distance between 2 hashcodes.
312 * The computation must be fast, not involve
313 * a.a or a.e (they're used elsewhere), and
314 * be somewhat consistent. And of course, the
315 * result should be a positive number.
316 * @return number between 0 and 65536
318 unsigned int GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a,
319 const GNUNET_HashCode * b);
323 * Hash block of given size.
324 * @param block the data to GNUNET_CRYPTO_hash, length is given as a second argument
325 * @param ret pointer to where to write the hashcode
327 void GNUNET_CRYPTO_hash (const void *block, unsigned int size,
328 GNUNET_HashCode * ret);
332 * Function called once the hash computation over the
333 * specified file has completed.
336 * @param res resulting hash, NULL on error
338 typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
339 const GNUNET_HashCode *
344 * Compute the hash of an entire file.
346 * @param sched scheduler to use
347 * @param priority scheduling priority to use
348 * @param run_on_shutdown should we complete even on shutdown?
349 * @param filename name of file to hash
350 * @param blocksize number of bytes to process in one task
351 * @param callback function to call upon completion
352 * @param callback_cls closure for callback
354 void GNUNET_CRYPTO_hash_file (struct GNUNET_SCHEDULER_Handle *sched,
355 enum GNUNET_SCHEDULER_Priority priority,
357 const char *filename,
359 GNUNET_CRYPTO_HashCompletedCallback callback,
364 * Create a random hash code.
366 void GNUNET_CRYPTO_hash_create_random (GNUNET_HashCode * result);
370 * compute result(delta) = b - a
372 void GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a,
373 const GNUNET_HashCode * b,
374 GNUNET_HashCode * result);
378 * compute result(b) = a + delta
380 void GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a,
381 const GNUNET_HashCode * delta,
382 GNUNET_HashCode * result);
386 * compute result = a ^ b
388 void GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a,
389 const GNUNET_HashCode * b,
390 GNUNET_HashCode * result);
394 * Convert a hashcode into a key.
396 void GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc,
397 struct GNUNET_CRYPTO_AesSessionKey *skey,
399 GNUNET_CRYPTO_AesInitializationVector
404 * Obtain a bit from a hashcode.
405 * @param code the GNUNET_CRYPTO_hash to index bit-wise
406 * @param bit index into the hashcode, [0...159]
407 * @return Bit \a bit from hashcode \a code, -1 for invalid index
409 int GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code,
414 * Compare function for HashCodes, producing a total ordering
416 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
418 int GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1,
419 const GNUNET_HashCode * h2);
423 * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
424 * in the XOR metric (Kademlia).
425 * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
427 int GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1,
428 const GNUNET_HashCode * h2,
429 const GNUNET_HashCode * target);
433 * Create a new private key. Caller must free return value.
435 struct GNUNET_CRYPTO_RsaPrivateKey *GNUNET_CRYPTO_rsa_key_create (void);
439 * Create a new private key by reading it from a file. If the
440 * files does not exist, create a new key and write it to the
441 * file. Caller must free return value. Note that this function
442 * can not guarantee that another process might not be trying
443 * the same operation on the same file at the same time. The
444 * caller must somehow know that the file either already exists
445 * with a valid key OR be sure that no other process is calling
446 * this function at the same time. If the contents of the file
447 * are invalid the old file is deleted and a fresh key is
450 * @return new private key, NULL on error (for example,
453 struct GNUNET_CRYPTO_RsaPrivateKey
454 *GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename);
458 * Deterministically (!) create a private key using only the
459 * given HashCode as input to the PRNG.
461 struct GNUNET_CRYPTO_RsaPrivateKey
462 *GNUNET_CRYPTO_rsa_key_create_from_hash (const GNUNET_HashCode * input);
466 * Free memory occupied by the private key.
467 * @param hostkey pointer to the memory to free
469 void GNUNET_CRYPTO_rsa_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey);
473 * Extract the public key of the host.
474 * @param result where to write the result.
476 void GNUNET_CRYPTO_rsa_key_get_public (const struct
477 GNUNET_CRYPTO_RsaPrivateKey *hostkey,
479 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
484 * Encrypt a block with the public key of another host that uses the
487 * @param block the block to encrypt
488 * @param size the size of block
489 * @param publicKey the encoded public key used to encrypt
490 * @param target where to store the encrypted block
491 * @returns GNUNET_SYSERR on error, GNUNET_OK if ok
493 int GNUNET_CRYPTO_rsa_encrypt (const void *block,
496 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
498 struct GNUNET_CRYPTO_RsaEncryptedData *target);
502 * Decrypt a given block with the hostkey.
504 * @param key the key to use
505 * @param block the data to decrypt, encoded as returned by encrypt, not consumed
506 * @param result pointer to a location where the result can be stored
507 * @param size how many bytes of a result are expected? Must be exact.
508 * @returns the size of the decrypted block (that is, size) or -1 on error
510 int GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
511 const struct GNUNET_CRYPTO_RsaEncryptedData
512 *block, void *result, uint16_t size);
516 * Sign a given block.
518 * @param key private key to use for the signing
519 * @param purpose what to sign (size, purpose)
520 * @param result where to write the signature
521 * @return GNUNET_SYSERR on error, GNUNET_OK on success
523 int GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
524 const struct GNUNET_CRYPTO_RsaSignaturePurpose
526 struct GNUNET_CRYPTO_RsaSignature *result);
530 * Verify signature. Note that the caller MUST have already
531 * checked that "validate->size" bytes are actually available.
533 * @param purpose what is the purpose that validate should have?
534 * @param validate block to validate (size, purpose, data)
535 * @param sig signature that is being validated
536 * @param publicKey public key of the signer
537 * @returns GNUNET_OK if ok, GNUNET_SYSERR if invalid
539 int GNUNET_CRYPTO_rsa_verify (uint32_t purpose,
540 const struct GNUNET_CRYPTO_RsaSignaturePurpose
542 const struct GNUNET_CRYPTO_RsaSignature *sig,
544 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
550 * This function should only be called in testcases
551 * where strong entropy gathering is not desired
552 * (for example, for hostkey generation).
554 void GNUNET_CRYPTO_random_disable_entropy_gathering (void);
556 #if 0 /* keep Emacsens' auto-indent happy */
564 /* ifndef GNUNET_CRYPTO_LIB_H */
566 /* end of gnunet_crypto_lib.h */