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.
23 * @file util/crypto_hash.c
24 * @brief SHA-512 GNUNET_CRYPTO_hash related functions
25 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
37 * Hash block of given size.
39 * @param block the data to #GNUNET_CRYPTO_hash, length is given as a second argument
40 * @param size the length of the data to #GNUNET_CRYPTO_hash in @a block
41 * @param ret pointer to where to write the hashcode
44 GNUNET_CRYPTO_hash (const void *block,
46 struct GNUNET_HashCode *ret)
48 gcry_md_hash_buffer (GCRY_MD_SHA512, ret, block, size);
53 * Context used when hashing a file.
55 struct GNUNET_CRYPTO_FileHashContext
59 * Function to call upon completion.
61 GNUNET_CRYPTO_HashCompletedCallback callback;
64 * Closure for callback.
71 unsigned char *buffer;
74 * Name of the file we are hashing.
81 struct GNUNET_DISK_FileHandle *fh;
99 * Current task for hashing.
101 GNUNET_SCHEDULER_TaskIdentifier task;
106 enum GNUNET_SCHEDULER_Priority priority;
117 * Report result of hash computation to callback
118 * and free associated resources.
121 file_hash_finish (struct GNUNET_CRYPTO_FileHashContext *fhc,
122 const struct GNUNET_HashCode * res)
124 fhc->callback (fhc->callback_cls, res);
125 GNUNET_free (fhc->filename);
126 if (!GNUNET_DISK_handle_invalid (fhc->fh))
127 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh));
128 gcry_md_close (fhc->md);
129 GNUNET_free (fhc); /* also frees fhc->buffer */
140 file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
142 struct GNUNET_CRYPTO_FileHashContext *fhc = cls;
143 struct GNUNET_HashCode *res;
146 fhc->task = GNUNET_SCHEDULER_NO_TASK;
147 GNUNET_assert (fhc->offset <= fhc->fsize);
149 if (fhc->fsize - fhc->offset < delta)
150 delta = fhc->fsize - fhc->offset;
151 if (delta != GNUNET_DISK_file_read (fhc->fh, fhc->buffer, delta))
153 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", fhc->filename);
154 file_hash_finish (fhc, NULL);
157 gcry_md_write (fhc->md, fhc->buffer, delta);
158 fhc->offset += delta;
159 if (fhc->offset == fhc->fsize)
161 res = (struct GNUNET_HashCode *) gcry_md_read (fhc->md, GCRY_MD_SHA512);
162 file_hash_finish (fhc, res);
165 fhc->task = GNUNET_SCHEDULER_add_with_priority (fhc->priority,
166 &file_hash_task, fhc);
171 * Compute the hash of an entire file.
173 * @param priority scheduling priority to use
174 * @param filename name of file to hash
175 * @param blocksize number of bytes to process in one task
176 * @param callback function to call upon completion
177 * @param callback_cls closure for callback
178 * @return NULL on (immediate) errror
180 struct GNUNET_CRYPTO_FileHashContext *
181 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
182 const char *filename, size_t blocksize,
183 GNUNET_CRYPTO_HashCompletedCallback callback,
186 struct GNUNET_CRYPTO_FileHashContext *fhc;
188 GNUNET_assert (blocksize > 0);
190 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_FileHashContext) + blocksize);
191 fhc->callback = callback;
192 fhc->callback_cls = callback_cls;
193 fhc->buffer = (unsigned char *) &fhc[1];
194 fhc->filename = GNUNET_strdup (filename);
195 if (GPG_ERR_NO_ERROR != gcry_md_open (&fhc->md, GCRY_MD_SHA512, 0))
201 fhc->bsize = blocksize;
202 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fhc->fsize, GNUNET_NO, GNUNET_YES))
204 GNUNET_free (fhc->filename);
209 GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
210 GNUNET_DISK_PERM_NONE);
213 GNUNET_free (fhc->filename);
217 fhc->priority = priority;
219 GNUNET_SCHEDULER_add_with_priority (priority, &file_hash_task, fhc);
225 * Cancel a file hashing operation.
227 * @param fhc operation to cancel (callback must not yet have been invoked)
230 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc)
232 GNUNET_SCHEDULER_cancel (fhc->task);
233 GNUNET_free (fhc->filename);
234 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh));
239 /* ***************** binary-ASCII encoding *************** */
243 * Convert GNUNET_CRYPTO_hash to ASCII encoding. The ASCII encoding is rather
244 * GNUnet specific. It was chosen such that it only uses characters
245 * in [0-9A-V], can be produced without complex arithmetics and uses a
246 * small number of characters. The GNUnet encoding uses 103
247 * characters plus a null terminator.
249 * @param block the hash code
250 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
251 * safely cast to char*, a '\\0' termination is set).
254 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block,
255 struct GNUNET_CRYPTO_HashAsciiEncoded *result)
259 np = GNUNET_STRINGS_data_to_string ((const unsigned char *) block,
260 sizeof (struct GNUNET_HashCode),
262 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1);
263 GNUNET_assert (NULL != np);
269 * Convert ASCII encoding back to hash code.
271 * @param enc the encoding
272 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
273 * @param result where to store the hash code
274 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
277 GNUNET_CRYPTO_hash_from_string2 (const char *enc,
279 struct GNUNET_HashCode *result)
281 char upper_enc[enclen];
282 char *up_ptr = upper_enc;
284 GNUNET_STRINGS_utf8_toupper (enc, up_ptr);
286 return GNUNET_STRINGS_string_to_data (upper_enc, enclen,
287 (unsigned char*) result,
288 sizeof (struct GNUNET_HashCode));
295 * Compute the distance between 2 hashcodes. The computation must be
296 * fast, not involve bits[0] or bits[4] (they're used elsewhere), and be
297 * somewhat consistent. And of course, the result should be a positive
300 * @param a some hash code
301 * @param b some hash code
302 * @return a positive number which is a measure for
303 * hashcode proximity.
306 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a,
307 const struct GNUNET_HashCode *b)
309 unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16;
310 unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16;
317 * Create a random hash code.
319 * @param mode desired quality level
320 * @param result hash code that is randomized
323 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
324 struct GNUNET_HashCode *result)
328 for (i = (sizeof (struct GNUNET_HashCode) / sizeof (uint32_t)) - 1; i >= 0; i--)
329 result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, UINT32_MAX);
334 * compute result(delta) = b - a
336 * @param a some hash code
337 * @param b some hash code
338 * @param result set to b - a
341 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
342 const struct GNUNET_HashCode *b,
343 struct GNUNET_HashCode *result)
347 for (i = (sizeof (struct GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
348 result->bits[i] = b->bits[i] - a->bits[i];
353 * compute result(b) = a + delta
355 * @param a some hash code
356 * @param delta some hash code
357 * @param result set to a + delta
360 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode * a,
361 const struct GNUNET_HashCode * delta, struct GNUNET_HashCode * result)
365 for (i = (sizeof (struct GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
366 result->bits[i] = delta->bits[i] + a->bits[i];
371 * compute result = a ^ b
373 * @param a some hash code
374 * @param b some hash code
375 * @param result set to a ^ b
378 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode * a, const struct GNUNET_HashCode * b,
379 struct GNUNET_HashCode * result)
383 for (i = (sizeof (struct GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
384 result->bits[i] = a->bits[i] ^ b->bits[i];
389 * Convert a hashcode into a key.
391 * @param hc hash code that serves to generate the key
392 * @param skey set to a valid session key
393 * @param iv set to a valid initialization vector
396 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode *hc,
397 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
398 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
400 GNUNET_assert (GNUNET_YES ==
401 GNUNET_CRYPTO_kdf (skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
402 "Hash key derivation", strlen ("Hash key derivation"),
403 hc, sizeof (struct GNUNET_HashCode),
405 GNUNET_assert (GNUNET_YES ==
406 GNUNET_CRYPTO_kdf (iv, sizeof (struct GNUNET_CRYPTO_SymmetricInitializationVector),
407 "Initialization vector derivation", strlen ("Initialization vector derivation"),
408 hc, sizeof (struct GNUNET_HashCode),
414 * Obtain a bit from a hashcode.
415 * @param code the GNUNET_CRYPTO_hash to index bit-wise
416 * @param bit index into the hashcode, [0...511]
417 * @return Bit \a bit from hashcode \a code, -1 for invalid index
420 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode * code, unsigned int bit)
422 GNUNET_assert (bit < 8 * sizeof (struct GNUNET_HashCode));
423 return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0;
428 * Determine how many low order bits match in two
429 * `struct GNUNET_HashCode`s. i.e. - 010011 and 011111 share
430 * the first two lowest order bits, and therefore the
431 * return value is two (NOT XOR distance, nor how many
432 * bits match absolutely!).
434 * @param first the first hashcode
435 * @param second the hashcode to compare first to
437 * @return the number of bits that match
440 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode * first,
441 const struct GNUNET_HashCode * second)
445 for (i = 0; i < sizeof (struct GNUNET_HashCode) * 8; i++)
446 if (GNUNET_CRYPTO_hash_get_bit (first, i) !=
447 GNUNET_CRYPTO_hash_get_bit (second, i))
449 return sizeof (struct GNUNET_HashCode) * 8;
454 * Compare function for HashCodes, producing a total ordering
457 * @param h1 some hash code
458 * @param h2 some hash code
459 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
462 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
463 const struct GNUNET_HashCode *h2)
469 i1 = (unsigned int *) h1;
470 i2 = (unsigned int *) h2;
471 for (i = (sizeof (struct GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
483 * Find out which of the two `struct GNUNET_HashCode`s is closer to target
484 * in the XOR metric (Kademlia).
486 * @param h1 some hash code
487 * @param h2 some hash code
488 * @param target some hash code
489 * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
492 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
493 const struct GNUNET_HashCode *h2,
494 const struct GNUNET_HashCode *target)
500 for (i = sizeof (struct GNUNET_HashCode) / sizeof (unsigned int) - 1; i >= 0; i--)
502 d1 = ((unsigned int *) h1)[i] ^ ((unsigned int *) target)[i];
503 d2 = ((unsigned int *) h2)[i] ^ ((unsigned int *) target)[i];
514 * @brief Derive an authentication key
515 * @param key authentication key
516 * @param rkey root key
518 * @param salt_len size of the @a salt
519 * @param ... pair of void * & size_t for context chunks, terminated by NULL
522 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
523 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
524 const void *salt, size_t salt_len, ...)
528 va_start (argp, salt_len);
529 GNUNET_CRYPTO_hmac_derive_key_v (key, rkey, salt, salt_len, argp);
535 * @brief Derive an authentication key
536 * @param key authentication key
537 * @param rkey root key
539 * @param salt_len size of the @a salt
540 * @param argp pair of void * & size_t for context chunks, terminated by NULL
543 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
544 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
545 const void *salt, size_t salt_len,
548 GNUNET_CRYPTO_kdf_v (key->key, sizeof (key->key),
550 rkey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
556 * Calculate HMAC of a message (RFC 2104)
558 * @param key secret key
559 * @param plaintext input plaintext
560 * @param plaintext_len length of @a plaintext
561 * @param hmac where to store the hmac
564 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
565 const void *plaintext, size_t plaintext_len,
566 struct GNUNET_HashCode *hmac)
569 static gcry_md_hd_t md;
570 const unsigned char *mc;
575 GNUNET_assert (GPG_ERR_NO_ERROR ==
576 gcry_md_open (&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC));
582 gcry_md_setkey (md, key->key, sizeof (key->key));
583 gcry_md_write (md, plaintext, plaintext_len);
584 mc = gcry_md_read (md, GCRY_MD_SHA512);
585 GNUNET_assert (NULL != mc);
586 memcpy (hmac->bits, mc, sizeof (hmac->bits));
590 /* end of crypto_hash.c */