2 This file is part of GNUnet.
3 Copyright (C) 2001-2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file util/crypto_hash.c
23 * @brief SHA-512 #GNUNET_CRYPTO_hash() related functions
24 * @author Christian Grothoff
27 #include "gnunet_crypto_lib.h"
28 #include "gnunet_strings_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-hash", __VA_ARGS__)
33 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-crypto-hash", syscall, filename)
36 * Hash block of given size.
38 * @param block the data to #GNUNET_CRYPTO_hash, length is given as a second argument
39 * @param size the length of the data to #GNUNET_CRYPTO_hash in @a block
40 * @param ret pointer to where to write the hashcode
43 GNUNET_CRYPTO_hash (const void *block,
45 struct GNUNET_HashCode *ret)
47 gcry_md_hash_buffer (GCRY_MD_SHA512, ret, block, size);
51 /* ***************** binary-ASCII encoding *************** */
55 * Convert GNUNET_CRYPTO_hash to ASCII encoding. The ASCII encoding is rather
56 * GNUnet specific. It was chosen such that it only uses characters
57 * in [0-9A-V], can be produced without complex arithmetics and uses a
58 * small number of characters. The GNUnet encoding uses 103
59 * characters plus a null terminator.
61 * @param block the hash code
62 * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
63 * safely cast to char*, a '\\0' termination is set).
66 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block,
67 struct GNUNET_CRYPTO_HashAsciiEncoded *result)
71 np = GNUNET_STRINGS_data_to_string ((const unsigned char *) block,
72 sizeof (struct GNUNET_HashCode),
74 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1);
75 GNUNET_assert (NULL != np);
81 * Convert ASCII encoding back to hash code.
83 * @param enc the encoding
84 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
85 * @param result where to store the hash code
86 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
89 GNUNET_CRYPTO_hash_from_string2 (const char *enc,
91 struct GNUNET_HashCode *result)
93 char upper_enc[enclen];
94 char *up_ptr = upper_enc;
96 GNUNET_STRINGS_utf8_toupper (enc, up_ptr);
98 return GNUNET_STRINGS_string_to_data (upper_enc, enclen,
99 (unsigned char*) result,
100 sizeof (struct GNUNET_HashCode));
107 * Compute the distance between 2 hashcodes. The computation must be
108 * fast, not involve bits[0] or bits[4] (they're used elsewhere), and be
109 * somewhat consistent. And of course, the result should be a positive
112 * @param a some hash code
113 * @param b some hash code
114 * @return a positive number which is a measure for
115 * hashcode proximity.
118 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a,
119 const struct GNUNET_HashCode *b)
121 unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16;
122 unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16;
129 * Create a random hash code.
131 * @param mode desired quality level
132 * @param result hash code that is randomized
135 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
136 struct GNUNET_HashCode *result)
140 for (i = (sizeof (struct GNUNET_HashCode) / sizeof (uint32_t)) - 1; i >= 0; i--)
141 result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, UINT32_MAX);
146 * compute result(delta) = b - a
148 * @param a some hash code
149 * @param b some hash code
150 * @param result set to b - a
153 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
154 const struct GNUNET_HashCode *b,
155 struct GNUNET_HashCode *result)
159 for (i = (sizeof (struct GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
160 result->bits[i] = b->bits[i] - a->bits[i];
165 * compute result(b) = a + delta
167 * @param a some hash code
168 * @param delta some hash code
169 * @param result set to a + delta
172 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode * a,
173 const struct GNUNET_HashCode * delta, struct GNUNET_HashCode * result)
177 for (i = (sizeof (struct GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
178 result->bits[i] = delta->bits[i] + a->bits[i];
183 * compute result = a ^ b
185 * @param a some hash code
186 * @param b some hash code
187 * @param result set to a ^ b
190 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
191 const struct GNUNET_HashCode *b,
192 struct GNUNET_HashCode *result)
196 for (i = (sizeof (struct GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
197 result->bits[i] = a->bits[i] ^ b->bits[i];
202 * Convert a hashcode into a key.
204 * @param hc hash code that serves to generate the key
205 * @param skey set to a valid session key
206 * @param iv set to a valid initialization vector
209 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode *hc,
210 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
211 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
213 GNUNET_assert (GNUNET_YES ==
214 GNUNET_CRYPTO_kdf (skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
215 "Hash key derivation", strlen ("Hash key derivation"),
216 hc, sizeof (struct GNUNET_HashCode),
218 GNUNET_assert (GNUNET_YES ==
219 GNUNET_CRYPTO_kdf (iv, sizeof (struct GNUNET_CRYPTO_SymmetricInitializationVector),
220 "Initialization vector derivation", strlen ("Initialization vector derivation"),
221 hc, sizeof (struct GNUNET_HashCode),
227 * Obtain a bit from a hashcode.
228 * @param code the GNUNET_CRYPTO_hash to index bit-wise
229 * @param bit index into the hashcode, [0...511]
230 * @return Bit \a bit from hashcode \a code, -1 for invalid index
233 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode * code, unsigned int bit)
235 GNUNET_assert (bit < 8 * sizeof (struct GNUNET_HashCode));
236 return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0;
241 * Determine how many low order bits match in two
242 * `struct GNUNET_HashCode`s. i.e. - 010011 and 011111 share
243 * the first two lowest order bits, and therefore the
244 * return value is two (NOT XOR distance, nor how many
245 * bits match absolutely!).
247 * @param first the first hashcode
248 * @param second the hashcode to compare first to
250 * @return the number of bits that match
253 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode * first,
254 const struct GNUNET_HashCode * second)
258 for (i = 0; i < sizeof (struct GNUNET_HashCode) * 8; i++)
259 if (GNUNET_CRYPTO_hash_get_bit (first, i) !=
260 GNUNET_CRYPTO_hash_get_bit (second, i))
262 return sizeof (struct GNUNET_HashCode) * 8;
267 * Compare function for HashCodes, producing a total ordering
270 * @param h1 some hash code
271 * @param h2 some hash code
272 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
275 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
276 const struct GNUNET_HashCode *h2)
282 i1 = (unsigned int *) h1;
283 i2 = (unsigned int *) h2;
284 for (i = (sizeof (struct GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
296 * Find out which of the two `struct GNUNET_HashCode`s is closer to target
297 * in the XOR metric (Kademlia).
299 * @param h1 some hash code
300 * @param h2 some hash code
301 * @param target some hash code
302 * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
305 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
306 const struct GNUNET_HashCode *h2,
307 const struct GNUNET_HashCode *target)
313 for (i = sizeof (struct GNUNET_HashCode) / sizeof (unsigned int) - 1; i >= 0; i--)
315 d1 = ((unsigned int *) h1)[i] ^ ((unsigned int *) target)[i];
316 d2 = ((unsigned int *) h2)[i] ^ ((unsigned int *) target)[i];
327 * @brief Derive an authentication key
328 * @param key authentication key
329 * @param rkey root key
331 * @param salt_len size of the @a salt
332 * @param ... pair of void * & size_t for context chunks, terminated by NULL
335 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
336 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
337 const void *salt, size_t salt_len, ...)
341 va_start (argp, salt_len);
342 GNUNET_CRYPTO_hmac_derive_key_v (key, rkey, salt, salt_len, argp);
348 * @brief Derive an authentication key
349 * @param key authentication key
350 * @param rkey root key
352 * @param salt_len size of the @a salt
353 * @param argp pair of void * & size_t for context chunks, terminated by NULL
356 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
357 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
358 const void *salt, size_t salt_len,
361 GNUNET_CRYPTO_kdf_v (key->key, sizeof (key->key),
363 rkey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
369 * Calculate HMAC of a message (RFC 2104)
371 * @param key secret key
372 * @param plaintext input plaintext
373 * @param plaintext_len length of @a plaintext
374 * @param hmac where to store the hmac
377 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
378 const void *plaintext, size_t plaintext_len,
379 struct GNUNET_HashCode *hmac)
382 static gcry_md_hd_t md;
383 const unsigned char *mc;
388 GNUNET_assert (GPG_ERR_NO_ERROR ==
389 gcry_md_open (&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC));
395 gcry_md_setkey (md, key->key, sizeof (key->key));
396 gcry_md_write (md, plaintext, plaintext_len);
397 mc = gcry_md_read (md, GCRY_MD_SHA512);
398 GNUNET_assert (NULL != mc);
399 GNUNET_memcpy (hmac->bits, mc, sizeof (hmac->bits));
404 * Context for cummulative hashing.
406 struct GNUNET_HashContext
409 * Internal state of the hash function.
416 * Start incremental hashing operation.
418 * @return context for incremental hash computation
420 struct GNUNET_HashContext *
421 GNUNET_CRYPTO_hash_context_start ()
423 struct GNUNET_HashContext *hc;
425 hc = GNUNET_new (struct GNUNET_HashContext);
427 gcry_md_open (&hc->hd,
435 * Add data to be hashed.
437 * @param hc cummulative hash context
438 * @param buf data to add
439 * @param size number of bytes in @a buf
442 GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
446 gcry_md_write (hc->hd, buf, size);
451 * Finish the hash computation.
453 * @param hc hash context to use
454 * @param r_hash where to write the latest / final hash code
457 GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
458 struct GNUNET_HashCode *r_hash)
460 const void *res = gcry_md_read (hc->hd, 0);
462 GNUNET_assert (NULL != res);
464 GNUNET_memcpy (r_hash,
466 sizeof (struct GNUNET_HashCode));
467 GNUNET_CRYPTO_hash_context_abort (hc);
472 * Abort hashing, do not bother calculating final result.
474 * @param hc hash context to destroy
477 GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc)
479 gcry_md_close (hc->hd);
484 /* end of crypto_hash.c */