crypto_hash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001-2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/crypto_hash.c
  18. * @brief SHA-512 #GNUNET_CRYPTO_hash() related functions
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_crypto_lib.h"
  23. #include "gnunet_strings_lib.h"
  24. #include "benchmark.h"
  25. #include <gcrypt.h>
  26. #define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-hash", __VA_ARGS__)
  27. #define LOG_STRERROR_FILE(kind, syscall, \
  28. filename) GNUNET_log_from_strerror_file (kind, \
  29. "util-crypto-hash", \
  30. syscall, \
  31. filename)
  32. void
  33. GNUNET_CRYPTO_hash (const void *block,
  34. size_t size,
  35. struct GNUNET_HashCode *ret)
  36. {
  37. BENCHMARK_START (hash);
  38. gcry_md_hash_buffer (GCRY_MD_SHA512, ret, block, size);
  39. BENCHMARK_END (hash);
  40. }
  41. /* ***************** binary-ASCII encoding *************** */
  42. void
  43. GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block,
  44. struct GNUNET_CRYPTO_HashAsciiEncoded *result)
  45. {
  46. char *np;
  47. np = GNUNET_STRINGS_data_to_string ((const unsigned char *) block,
  48. sizeof(struct GNUNET_HashCode),
  49. (char *) result,
  50. sizeof(struct
  51. GNUNET_CRYPTO_HashAsciiEncoded)
  52. - 1);
  53. GNUNET_assert (NULL != np);
  54. *np = '\0';
  55. }
  56. enum GNUNET_GenericReturnValue
  57. GNUNET_CRYPTO_hash_from_string2 (const char *enc,
  58. size_t enclen,
  59. struct GNUNET_HashCode *result)
  60. {
  61. char upper_enc[enclen];
  62. char *up_ptr = upper_enc;
  63. GNUNET_STRINGS_utf8_toupper (enc, up_ptr);
  64. return GNUNET_STRINGS_string_to_data (upper_enc, enclen,
  65. (unsigned char *) result,
  66. sizeof(struct GNUNET_HashCode));
  67. }
  68. unsigned int
  69. GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a,
  70. const struct GNUNET_HashCode *b)
  71. {
  72. unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16;
  73. unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16;
  74. return(x1 * x2);
  75. }
  76. void
  77. GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
  78. struct GNUNET_HashCode *result)
  79. {
  80. for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(uint32_t)) - 1;
  81. i >= 0;
  82. i--)
  83. result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, UINT32_MAX);
  84. }
  85. void
  86. GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
  87. const struct GNUNET_HashCode *b,
  88. struct GNUNET_HashCode *result)
  89. {
  90. for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
  91. i >= 0;
  92. i--)
  93. result->bits[i] = b->bits[i] - a->bits[i];
  94. }
  95. void
  96. GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a,
  97. const struct GNUNET_HashCode *delta, struct
  98. GNUNET_HashCode *result)
  99. {
  100. for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
  101. i >= 0;
  102. i--)
  103. result->bits[i] = delta->bits[i] + a->bits[i];
  104. }
  105. void
  106. GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
  107. const struct GNUNET_HashCode *b,
  108. struct GNUNET_HashCode *result)
  109. {
  110. for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
  111. i >= 0;
  112. i--)
  113. result->bits[i] = a->bits[i] ^ b->bits[i];
  114. }
  115. void
  116. GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode *hc,
  117. struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
  118. struct
  119. GNUNET_CRYPTO_SymmetricInitializationVector *iv)
  120. {
  121. GNUNET_assert (GNUNET_YES ==
  122. GNUNET_CRYPTO_kdf (
  123. skey,
  124. sizeof(struct GNUNET_CRYPTO_SymmetricSessionKey),
  125. "Hash key derivation",
  126. strlen ("Hash key derivation"),
  127. hc, sizeof(struct GNUNET_HashCode),
  128. NULL, 0));
  129. GNUNET_assert (GNUNET_YES ==
  130. GNUNET_CRYPTO_kdf (
  131. iv,
  132. sizeof(struct GNUNET_CRYPTO_SymmetricInitializationVector),
  133. "Initialization vector derivation",
  134. strlen ("Initialization vector derivation"),
  135. hc, sizeof(struct GNUNET_HashCode),
  136. NULL, 0));
  137. }
  138. int
  139. GNUNET_CRYPTO_hash_get_bit_ltr (const struct GNUNET_HashCode *code,
  140. unsigned int bit)
  141. {
  142. GNUNET_assert (bit < 8 * sizeof(struct GNUNET_HashCode));
  143. return (((unsigned char *) code)[bit >> 3] & (128 >> (bit & 7))) > 0;
  144. }
  145. int
  146. GNUNET_CRYPTO_hash_get_bit_rtl (const struct GNUNET_HashCode *code,
  147. unsigned int bit)
  148. {
  149. GNUNET_assert (bit < 8 * sizeof(struct GNUNET_HashCode));
  150. return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0;
  151. }
  152. unsigned int
  153. GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
  154. const struct GNUNET_HashCode *second)
  155. {
  156. for (unsigned int i = 0; i < sizeof(struct GNUNET_HashCode) * 8; i++)
  157. if (GNUNET_CRYPTO_hash_get_bit_rtl (first, i) !=
  158. GNUNET_CRYPTO_hash_get_bit_rtl (second, i))
  159. return i;
  160. return sizeof(struct GNUNET_HashCode) * 8;
  161. }
  162. int
  163. GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
  164. const struct GNUNET_HashCode *h2)
  165. {
  166. unsigned int *i1;
  167. unsigned int *i2;
  168. i1 = (unsigned int *) h1;
  169. i2 = (unsigned int *) h2;
  170. for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
  171. i >= 0;
  172. i--)
  173. {
  174. if (i1[i] > i2[i])
  175. return 1;
  176. if (i1[i] < i2[i])
  177. return -1;
  178. }
  179. return 0;
  180. }
  181. int
  182. GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
  183. const struct GNUNET_HashCode *h2,
  184. const struct GNUNET_HashCode *target)
  185. {
  186. unsigned int d1;
  187. unsigned int d2;
  188. for (ssize_t i = sizeof(struct GNUNET_HashCode) / sizeof(unsigned int) - 1;
  189. i >= 0;
  190. i--)
  191. {
  192. d1 = ((unsigned int *) h1)[i] ^ ((unsigned int *) target)[i];
  193. d2 = ((unsigned int *) h2)[i] ^ ((unsigned int *) target)[i];
  194. if (d1 > d2)
  195. return 1;
  196. else if (d1 < d2)
  197. return -1;
  198. }
  199. return 0;
  200. }
  201. void
  202. GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
  203. const struct
  204. GNUNET_CRYPTO_SymmetricSessionKey *rkey,
  205. const void *salt, size_t salt_len, ...)
  206. {
  207. va_list argp;
  208. va_start (argp, salt_len);
  209. GNUNET_CRYPTO_hmac_derive_key_v (key, rkey, salt, salt_len, argp);
  210. va_end (argp);
  211. }
  212. void
  213. GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
  214. const struct
  215. GNUNET_CRYPTO_SymmetricSessionKey *rkey,
  216. const void *salt, size_t salt_len,
  217. va_list argp)
  218. {
  219. GNUNET_CRYPTO_kdf_v (key->key, sizeof(key->key),
  220. salt, salt_len,
  221. rkey, sizeof(struct GNUNET_CRYPTO_SymmetricSessionKey),
  222. argp);
  223. }
  224. void
  225. GNUNET_CRYPTO_hmac_raw (const void *key, size_t key_len,
  226. const void *plaintext, size_t plaintext_len,
  227. struct GNUNET_HashCode *hmac)
  228. {
  229. static int once;
  230. static gcry_md_hd_t md;
  231. const unsigned char *mc;
  232. if (! once)
  233. {
  234. once = 1;
  235. GNUNET_assert (GPG_ERR_NO_ERROR ==
  236. gcry_md_open (&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC));
  237. }
  238. else
  239. {
  240. gcry_md_reset (md);
  241. }
  242. gcry_md_setkey (md, key, key_len);
  243. gcry_md_write (md, plaintext, plaintext_len);
  244. mc = gcry_md_read (md, GCRY_MD_SHA512);
  245. GNUNET_assert (NULL != mc);
  246. GNUNET_memcpy (hmac->bits, mc, sizeof(hmac->bits));
  247. }
  248. void
  249. GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
  250. const void *plaintext, size_t plaintext_len,
  251. struct GNUNET_HashCode *hmac)
  252. {
  253. GNUNET_CRYPTO_hmac_raw ((void *) key->key, sizeof(key->key),
  254. plaintext, plaintext_len,
  255. hmac);
  256. }
  257. struct GNUNET_HashContext
  258. {
  259. /**
  260. * Internal state of the hash function.
  261. */
  262. gcry_md_hd_t hd;
  263. };
  264. struct GNUNET_HashContext *
  265. GNUNET_CRYPTO_hash_context_start ()
  266. {
  267. struct GNUNET_HashContext *hc;
  268. BENCHMARK_START (hash_context_start);
  269. hc = GNUNET_new (struct GNUNET_HashContext);
  270. GNUNET_assert (0 ==
  271. gcry_md_open (&hc->hd,
  272. GCRY_MD_SHA512,
  273. 0));
  274. BENCHMARK_END (hash_context_start);
  275. return hc;
  276. }
  277. void
  278. GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
  279. const void *buf,
  280. size_t size)
  281. {
  282. BENCHMARK_START (hash_context_read);
  283. gcry_md_write (hc->hd, buf, size);
  284. BENCHMARK_END (hash_context_read);
  285. }
  286. struct GNUNET_HashContext *
  287. GNUNET_CRYPTO_hash_context_copy (const struct GNUNET_HashContext *hc)
  288. {
  289. struct GNUNET_HashContext *cp;
  290. cp = GNUNET_new (struct GNUNET_HashContext);
  291. GNUNET_assert (0 ==
  292. gcry_md_copy (&cp->hd,
  293. hc->hd));
  294. return cp;
  295. }
  296. void
  297. GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
  298. struct GNUNET_HashCode *r_hash)
  299. {
  300. const void *res = gcry_md_read (hc->hd, 0);
  301. BENCHMARK_START (hash_context_finish);
  302. GNUNET_assert (NULL != res);
  303. if (NULL != r_hash)
  304. GNUNET_memcpy (r_hash,
  305. res,
  306. sizeof(struct GNUNET_HashCode));
  307. GNUNET_CRYPTO_hash_context_abort (hc);
  308. BENCHMARK_END (hash_context_finish);
  309. }
  310. void
  311. GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc)
  312. {
  313. gcry_md_close (hc->hd);
  314. GNUNET_free (hc);
  315. }
  316. /* end of crypto_hash.c */