diff options
Diffstat (limited to 'src/util/crypto_hash.c')
-rw-r--r-- | src/util/crypto_hash.c | 413 |
1 files changed, 0 insertions, 413 deletions
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c deleted file mode 100644 index f516f5474..000000000 --- a/src/util/crypto_hash.c +++ /dev/null | |||
@@ -1,413 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2001-2013 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | |||
20 | */ | ||
21 | /** | ||
22 | * @file util/crypto_hash.c | ||
23 | * @brief SHA-512 #GNUNET_CRYPTO_hash() related functions | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_crypto_lib.h" | ||
28 | #include "gnunet_strings_lib.h" | ||
29 | #include "benchmark.h" | ||
30 | #include <gcrypt.h> | ||
31 | |||
32 | #define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-hash", __VA_ARGS__) | ||
33 | |||
34 | #define LOG_STRERROR_FILE(kind, syscall, \ | ||
35 | filename) GNUNET_log_from_strerror_file (kind, \ | ||
36 | "util-crypto-hash", \ | ||
37 | syscall, \ | ||
38 | filename) | ||
39 | |||
40 | void | ||
41 | GNUNET_CRYPTO_hash (const void *block, | ||
42 | size_t size, | ||
43 | struct GNUNET_HashCode *ret) | ||
44 | { | ||
45 | BENCHMARK_START (hash); | ||
46 | gcry_md_hash_buffer (GCRY_MD_SHA512, ret, block, size); | ||
47 | BENCHMARK_END (hash); | ||
48 | } | ||
49 | |||
50 | |||
51 | /* ***************** binary-ASCII encoding *************** */ | ||
52 | |||
53 | |||
54 | void | ||
55 | GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block, | ||
56 | struct GNUNET_CRYPTO_HashAsciiEncoded *result) | ||
57 | { | ||
58 | char *np; | ||
59 | |||
60 | np = GNUNET_STRINGS_data_to_string ((const unsigned char *) block, | ||
61 | sizeof(struct GNUNET_HashCode), | ||
62 | (char *) result, | ||
63 | sizeof(struct | ||
64 | GNUNET_CRYPTO_HashAsciiEncoded) | ||
65 | - 1); | ||
66 | GNUNET_assert (NULL != np); | ||
67 | *np = '\0'; | ||
68 | } | ||
69 | |||
70 | |||
71 | enum GNUNET_GenericReturnValue | ||
72 | GNUNET_CRYPTO_hash_from_string2 (const char *enc, | ||
73 | size_t enclen, | ||
74 | struct GNUNET_HashCode *result) | ||
75 | { | ||
76 | char upper_enc[enclen]; | ||
77 | char *up_ptr = upper_enc; | ||
78 | |||
79 | if (GNUNET_OK != GNUNET_STRINGS_utf8_toupper (enc, up_ptr)) | ||
80 | return GNUNET_SYSERR; | ||
81 | |||
82 | return GNUNET_STRINGS_string_to_data (upper_enc, enclen, | ||
83 | (unsigned char *) result, | ||
84 | sizeof(struct GNUNET_HashCode)); | ||
85 | } | ||
86 | |||
87 | |||
88 | unsigned int | ||
89 | GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a, | ||
90 | const struct GNUNET_HashCode *b) | ||
91 | { | ||
92 | unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16; | ||
93 | unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16; | ||
94 | |||
95 | return(x1 * x2); | ||
96 | } | ||
97 | |||
98 | |||
99 | void | ||
100 | GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode, | ||
101 | struct GNUNET_HashCode *result) | ||
102 | { | ||
103 | for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(uint32_t)) - 1; | ||
104 | i >= 0; | ||
105 | i--) | ||
106 | result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, UINT32_MAX); | ||
107 | } | ||
108 | |||
109 | |||
110 | void | ||
111 | GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a, | ||
112 | const struct GNUNET_HashCode *b, | ||
113 | struct GNUNET_HashCode *result) | ||
114 | { | ||
115 | for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1; | ||
116 | i >= 0; | ||
117 | i--) | ||
118 | result->bits[i] = b->bits[i] - a->bits[i]; | ||
119 | } | ||
120 | |||
121 | |||
122 | void | ||
123 | GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a, | ||
124 | const struct GNUNET_HashCode *delta, struct | ||
125 | GNUNET_HashCode *result) | ||
126 | { | ||
127 | for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1; | ||
128 | i >= 0; | ||
129 | i--) | ||
130 | result->bits[i] = delta->bits[i] + a->bits[i]; | ||
131 | } | ||
132 | |||
133 | |||
134 | void | ||
135 | GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a, | ||
136 | const struct GNUNET_HashCode *b, | ||
137 | struct GNUNET_HashCode *result) | ||
138 | { | ||
139 | const unsigned long long *lla = (const unsigned long long *) a; | ||
140 | const unsigned long long *llb = (const unsigned long long *) b; | ||
141 | unsigned long long *llr = (unsigned long long *) result; | ||
142 | |||
143 | GNUNET_static_assert (8 == sizeof (unsigned long long)); | ||
144 | GNUNET_static_assert (0 == sizeof (*a) % sizeof (unsigned long long)); | ||
145 | for (int i = sizeof (*result) / sizeof (*llr) - 1; i>=0; i--) | ||
146 | llr[i] = lla[i] ^ llb[i]; | ||
147 | } | ||
148 | |||
149 | |||
150 | void | ||
151 | GNUNET_CRYPTO_hash_to_aes_key ( | ||
152 | const struct GNUNET_HashCode *hc, | ||
153 | struct GNUNET_CRYPTO_SymmetricSessionKey *skey, | ||
154 | struct GNUNET_CRYPTO_SymmetricInitializationVector *iv) | ||
155 | { | ||
156 | GNUNET_assert (GNUNET_YES == | ||
157 | GNUNET_CRYPTO_kdf ( | ||
158 | skey, | ||
159 | sizeof(struct GNUNET_CRYPTO_SymmetricSessionKey), | ||
160 | "Hash key derivation", | ||
161 | strlen ("Hash key derivation"), | ||
162 | hc, sizeof(struct GNUNET_HashCode), | ||
163 | NULL, 0)); | ||
164 | GNUNET_assert (GNUNET_YES == | ||
165 | GNUNET_CRYPTO_kdf ( | ||
166 | iv, | ||
167 | sizeof(struct GNUNET_CRYPTO_SymmetricInitializationVector), | ||
168 | "Initialization vector derivation", | ||
169 | strlen ("Initialization vector derivation"), | ||
170 | hc, sizeof(struct GNUNET_HashCode), | ||
171 | NULL, 0)); | ||
172 | } | ||
173 | |||
174 | |||
175 | unsigned int | ||
176 | GNUNET_CRYPTO_hash_count_leading_zeros (const struct GNUNET_HashCode *h) | ||
177 | { | ||
178 | const unsigned long long *llp = (const unsigned long long *) h; | ||
179 | unsigned int ret = 0; | ||
180 | unsigned int i; | ||
181 | |||
182 | GNUNET_static_assert (8 == sizeof (unsigned long long)); | ||
183 | GNUNET_static_assert (0 == sizeof (*h) % sizeof (unsigned long long)); | ||
184 | for (i = 0; i<sizeof (*h) / sizeof (*llp); i++) | ||
185 | { | ||
186 | if (0LLU != llp[i]) | ||
187 | break; | ||
188 | ret += sizeof (*llp) * 8; | ||
189 | } | ||
190 | if (ret == 8 * sizeof (*h)) | ||
191 | return ret; | ||
192 | ret += __builtin_clzll (GNUNET_ntohll ((uint64_t) llp[i])); | ||
193 | return ret; | ||
194 | } | ||
195 | |||
196 | |||
197 | unsigned int | ||
198 | GNUNET_CRYPTO_hash_count_tailing_zeros (const struct GNUNET_HashCode *h) | ||
199 | { | ||
200 | const unsigned long long *llp = (const unsigned long long *) h; | ||
201 | unsigned int ret = 0; | ||
202 | int i; | ||
203 | |||
204 | GNUNET_static_assert (8 == sizeof (unsigned long long)); | ||
205 | GNUNET_static_assert (0 == sizeof (*h) % sizeof (unsigned long long)); | ||
206 | for (i = sizeof (*h) / sizeof (*llp) - 1; i>=0; i--) | ||
207 | { | ||
208 | if (0LLU != llp[i]) | ||
209 | break; | ||
210 | ret += sizeof (*llp) * 8; | ||
211 | } | ||
212 | if (ret == 8 * sizeof (*h)) | ||
213 | return ret; | ||
214 | ret += __builtin_ctzll (GNUNET_ntohll ((uint64_t) llp[i])); | ||
215 | return ret; | ||
216 | } | ||
217 | |||
218 | |||
219 | int | ||
220 | GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1, | ||
221 | const struct GNUNET_HashCode *h2) | ||
222 | { | ||
223 | unsigned int *i1; | ||
224 | unsigned int *i2; | ||
225 | |||
226 | i1 = (unsigned int *) h1; | ||
227 | i2 = (unsigned int *) h2; | ||
228 | for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1; | ||
229 | i >= 0; | ||
230 | i--) | ||
231 | { | ||
232 | if (i1[i] > i2[i]) | ||
233 | return 1; | ||
234 | if (i1[i] < i2[i]) | ||
235 | return -1; | ||
236 | } | ||
237 | return 0; | ||
238 | } | ||
239 | |||
240 | |||
241 | int | ||
242 | GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1, | ||
243 | const struct GNUNET_HashCode *h2, | ||
244 | const struct GNUNET_HashCode *target) | ||
245 | { | ||
246 | const unsigned long long *l1 = (const unsigned long long *) h1; | ||
247 | const unsigned long long *l2 = (const unsigned long long *) h2; | ||
248 | const unsigned long long *t = (const unsigned long long *) target; | ||
249 | |||
250 | GNUNET_static_assert (0 == sizeof (*h1) % sizeof (*l1)); | ||
251 | for (size_t i = 0; i < sizeof(*h1) / sizeof(*l1); i++) | ||
252 | { | ||
253 | unsigned long long x1 = l1[i] ^ t[i]; | ||
254 | unsigned long long x2 = l2[i] ^ t[i]; | ||
255 | |||
256 | if (x1 > x2) | ||
257 | return 1; | ||
258 | if (x1 < x2) | ||
259 | return -1; | ||
260 | } | ||
261 | return 0; | ||
262 | } | ||
263 | |||
264 | |||
265 | void | ||
266 | GNUNET_CRYPTO_hmac_derive_key ( | ||
267 | struct GNUNET_CRYPTO_AuthKey *key, | ||
268 | const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey, | ||
269 | const void *salt, size_t salt_len, | ||
270 | ...) | ||
271 | { | ||
272 | va_list argp; | ||
273 | |||
274 | va_start (argp, | ||
275 | salt_len); | ||
276 | GNUNET_CRYPTO_hmac_derive_key_v (key, | ||
277 | rkey, | ||
278 | salt, salt_len, | ||
279 | argp); | ||
280 | va_end (argp); | ||
281 | } | ||
282 | |||
283 | |||
284 | void | ||
285 | GNUNET_CRYPTO_hmac_derive_key_v ( | ||
286 | struct GNUNET_CRYPTO_AuthKey *key, | ||
287 | const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey, | ||
288 | const void *salt, size_t salt_len, | ||
289 | va_list argp) | ||
290 | { | ||
291 | GNUNET_CRYPTO_kdf_v (key->key, sizeof(key->key), | ||
292 | salt, salt_len, | ||
293 | rkey, sizeof(struct GNUNET_CRYPTO_SymmetricSessionKey), | ||
294 | argp); | ||
295 | } | ||
296 | |||
297 | |||
298 | void | ||
299 | GNUNET_CRYPTO_hmac_raw (const void *key, size_t key_len, | ||
300 | const void *plaintext, size_t plaintext_len, | ||
301 | struct GNUNET_HashCode *hmac) | ||
302 | { | ||
303 | static int once; | ||
304 | static gcry_md_hd_t md; | ||
305 | const unsigned char *mc; | ||
306 | |||
307 | if (! once) | ||
308 | { | ||
309 | once = 1; | ||
310 | GNUNET_assert (GPG_ERR_NO_ERROR == | ||
311 | gcry_md_open (&md, | ||
312 | GCRY_MD_SHA512, | ||
313 | GCRY_MD_FLAG_HMAC)); | ||
314 | } | ||
315 | else | ||
316 | { | ||
317 | gcry_md_reset (md); | ||
318 | } | ||
319 | gcry_md_setkey (md, key, key_len); | ||
320 | gcry_md_write (md, plaintext, plaintext_len); | ||
321 | mc = gcry_md_read (md, GCRY_MD_SHA512); | ||
322 | GNUNET_assert (NULL != mc); | ||
323 | GNUNET_memcpy (hmac->bits, mc, sizeof(hmac->bits)); | ||
324 | } | ||
325 | |||
326 | |||
327 | void | ||
328 | GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key, | ||
329 | const void *plaintext, size_t plaintext_len, | ||
330 | struct GNUNET_HashCode *hmac) | ||
331 | { | ||
332 | GNUNET_CRYPTO_hmac_raw ((void *) key->key, sizeof(key->key), | ||
333 | plaintext, plaintext_len, | ||
334 | hmac); | ||
335 | } | ||
336 | |||
337 | |||
338 | struct GNUNET_HashContext | ||
339 | { | ||
340 | /** | ||
341 | * Internal state of the hash function. | ||
342 | */ | ||
343 | gcry_md_hd_t hd; | ||
344 | }; | ||
345 | |||
346 | |||
347 | struct GNUNET_HashContext * | ||
348 | GNUNET_CRYPTO_hash_context_start () | ||
349 | { | ||
350 | struct GNUNET_HashContext *hc; | ||
351 | |||
352 | BENCHMARK_START (hash_context_start); | ||
353 | hc = GNUNET_new (struct GNUNET_HashContext); | ||
354 | GNUNET_assert (0 == | ||
355 | gcry_md_open (&hc->hd, | ||
356 | GCRY_MD_SHA512, | ||
357 | 0)); | ||
358 | BENCHMARK_END (hash_context_start); | ||
359 | return hc; | ||
360 | } | ||
361 | |||
362 | |||
363 | void | ||
364 | GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc, | ||
365 | const void *buf, | ||
366 | size_t size) | ||
367 | { | ||
368 | BENCHMARK_START (hash_context_read); | ||
369 | gcry_md_write (hc->hd, buf, size); | ||
370 | BENCHMARK_END (hash_context_read); | ||
371 | } | ||
372 | |||
373 | |||
374 | struct GNUNET_HashContext * | ||
375 | GNUNET_CRYPTO_hash_context_copy (const struct GNUNET_HashContext *hc) | ||
376 | { | ||
377 | struct GNUNET_HashContext *cp; | ||
378 | |||
379 | cp = GNUNET_new (struct GNUNET_HashContext); | ||
380 | GNUNET_assert (0 == | ||
381 | gcry_md_copy (&cp->hd, | ||
382 | hc->hd)); | ||
383 | return cp; | ||
384 | } | ||
385 | |||
386 | |||
387 | void | ||
388 | GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc, | ||
389 | struct GNUNET_HashCode *r_hash) | ||
390 | { | ||
391 | const void *res = gcry_md_read (hc->hd, 0); | ||
392 | |||
393 | BENCHMARK_START (hash_context_finish); | ||
394 | |||
395 | GNUNET_assert (NULL != res); | ||
396 | if (NULL != r_hash) | ||
397 | GNUNET_memcpy (r_hash, | ||
398 | res, | ||
399 | sizeof(struct GNUNET_HashCode)); | ||
400 | GNUNET_CRYPTO_hash_context_abort (hc); | ||
401 | BENCHMARK_END (hash_context_finish); | ||
402 | } | ||
403 | |||
404 | |||
405 | void | ||
406 | GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc) | ||
407 | { | ||
408 | gcry_md_close (hc->hd); | ||
409 | GNUNET_free (hc); | ||
410 | } | ||
411 | |||
412 | |||
413 | /* end of crypto_hash.c */ | ||