GNUnet 0.28.0-dev.3-7-g31e20e2e6
 
Loading...
Searching...
No Matches
crypto_hash.c
Go to the documentation of this file.
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 */
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "benchmark.h"
30
31#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-hash", __VA_ARGS__)
32
33#define LOG_STRERROR_FILE(kind, syscall, \
34 filename) GNUNET_log_from_strerror_file (kind, \
35 "util-crypto-hash", \
36 syscall, \
37 filename)
38
39void
40GNUNET_CRYPTO_hash (const void *block,
41 size_t size,
42 struct GNUNET_HashCode *ret)
43{
44 BENCHMARK_START (hash);
45 crypto_hash_sha512 ((unsigned char*) ret,
46 block,
47 size);
48 BENCHMARK_END (hash);
49}
50
51
52/* ***************** binary-ASCII encoding *************** */
53
54
55void
58{
59 char *np;
60
61 np = GNUNET_STRINGS_data_to_string ((const unsigned char *) block,
62 sizeof(struct GNUNET_HashCode),
63 (char *) result,
64 sizeof(struct
66 - 1);
67 GNUNET_assert (NULL != np);
68 *np = '\0';
69}
70
71
74 size_t enclen,
75 struct GNUNET_HashCode *result)
76{
78 char *up_ptr;
79
81 if (NULL == up_ptr)
82 return GNUNET_SYSERR;
84 strlen (up_ptr),
85 (unsigned char *) result,
86 sizeof(struct GNUNET_HashCode));
87 GNUNET_free (up_ptr);
88 return ret;
89}
90
91
92unsigned int
94 const struct GNUNET_HashCode *b)
95{
96 unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16;
97 unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16;
98
99 return(x1 * x2);
100}
101
102
103void
105 const struct GNUNET_HashCode *b,
106 struct GNUNET_HashCode *result)
107{
108 for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
109 i >= 0;
110 i--)
111 result->bits[i] = b->bits[i] - a->bits[i];
112}
113
114
115void
117 const struct GNUNET_HashCode *delta, struct
119{
120 for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
121 i >= 0;
122 i--)
123 result->bits[i] = delta->bits[i] + a->bits[i];
124}
125
126
127void
129 const struct GNUNET_HashCode *b,
130 struct GNUNET_HashCode *result)
131{
132 const unsigned long long *lla = (const unsigned long long *) a;
133 const unsigned long long *llb = (const unsigned long long *) b;
134 unsigned long long *llr = (unsigned long long *) result;
135
136 GNUNET_static_assert (8 == sizeof (unsigned long long));
137 GNUNET_static_assert (0 == sizeof (*a) % sizeof (unsigned long long));
138
139 for (int i = sizeof (*result) / sizeof (*llr) - 1; i>=0; i--)
140 llr[i] = lla[i] ^ llb[i];
141}
142
143
144unsigned int
146{
147 const unsigned long long *llp = (const unsigned long long *) h;
148 unsigned int ret = 0;
149 unsigned int i;
150
151 GNUNET_static_assert (8 == sizeof (unsigned long long));
152 GNUNET_static_assert (0 == sizeof (*h) % sizeof (unsigned long long));
153 for (i = 0; i<sizeof (*h) / sizeof (*llp); i++)
154 {
155 if (0LLU != llp[i])
156 break;
157 ret += sizeof (*llp) * 8;
158 }
159 if (ret == 8 * sizeof (*h))
160 return ret;
161 ret += __builtin_clzll (GNUNET_ntohll ((uint64_t) llp[i]));
162 return ret;
163}
164
165
166unsigned int
168{
169 const unsigned long long *llp = (const unsigned long long *) h;
170 unsigned int ret = 0;
171 int i;
172
173 GNUNET_static_assert (8 == sizeof (unsigned long long));
174 GNUNET_static_assert (0 == sizeof (*h) % sizeof (unsigned long long));
175 for (i = sizeof (*h) / sizeof (*llp) - 1; i>=0; i--)
176 {
177 if (0LLU != llp[i])
178 break;
179 ret += sizeof (*llp) * 8;
180 }
181 if (ret == 8 * sizeof (*h))
182 return ret;
183 ret += __builtin_ctzll (GNUNET_ntohll ((uint64_t) llp[i]));
184 return ret;
185}
186
187
188int
190 const struct GNUNET_HashCode *h2)
191{
192 unsigned int *i1;
193 unsigned int *i2;
194
195 i1 = (unsigned int *) h1;
196 i2 = (unsigned int *) h2;
197 for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
198 i >= 0;
199 i--)
200 {
201 if (i1[i] > i2[i])
202 return 1;
203 if (i1[i] < i2[i])
204 return -1;
205 }
206 return 0;
207}
208
209
210int
212 const struct GNUNET_HashCode *h2,
213 const struct GNUNET_HashCode *target)
214{
215 const unsigned long long *l1 = (const unsigned long long *) h1;
216 const unsigned long long *l2 = (const unsigned long long *) h2;
217 const unsigned long long *t = (const unsigned long long *) target;
218
219 GNUNET_static_assert (0 == sizeof (*h1) % sizeof (*l1));
220 for (size_t i = 0; i < sizeof(*h1) / sizeof(*l1); i++)
221 {
222 unsigned long long x1 = l1[i] ^ t[i];
223 unsigned long long x2 = l2[i] ^ t[i];
224
225 if (x1 > x2)
226 return 1;
227 if (x1 < x2)
228 return -1;
229 }
230 return 0;
231}
232
233
234void
235GNUNET_CRYPTO_hmac_raw (const unsigned char *key,
236 unsigned int keylen,
237 const void *plaintext,
238 size_t plaintext_len,
239 struct GNUNET_HashCode *hmac)
240{
241 crypto_auth_hmacsha512_state state;
242 crypto_auth_hmacsha512_init (&state,
243 key,
244 keylen);
245 crypto_auth_hmacsha512_update (&state,
246 plaintext,
247 plaintext_len);
248 crypto_auth_hmacsha512_final (&state,
249 (unsigned char*) hmac);
250}
251
252
253void
255 const void *plaintext, size_t plaintext_len,
256 struct GNUNET_HashCode *hmac)
257{
258 GNUNET_CRYPTO_hmac_raw ((unsigned char*) key,
259 sizeof *key,
260 plaintext,
261 plaintext_len,
262 hmac);
263}
264
265
267{
271 crypto_hash_sha512_state hd;
272};
273
274
275struct GNUNET_HashContext *
277{
278 struct GNUNET_HashContext *hc;
279
280 BENCHMARK_START (hash_context_start);
281 hc = GNUNET_new (struct GNUNET_HashContext);
282 crypto_hash_sha512_init (&hc->hd);
283 BENCHMARK_END (hash_context_start);
284 return hc;
285}
286
287
288void
290 const void *buf,
291 size_t size)
292{
293 BENCHMARK_START (hash_context_read);
294 crypto_hash_sha512_update (&hc->hd,
295 buf,
296 size);
297 BENCHMARK_END (hash_context_read);
298}
299
300
301struct GNUNET_HashContext *
303{
304 struct GNUNET_HashContext *cp;
305
306 cp = GNUNET_new (struct GNUNET_HashContext);
307 GNUNET_memcpy (cp, hc, sizeof *hc);
308 return cp;
309}
310
311
312void
314 struct GNUNET_HashCode *r_hash)
315{
316 BENCHMARK_START (hash_context_finish);
317
318 crypto_hash_sha512_final (&hc->hd,
319 (unsigned char*) r_hash);
321 BENCHMARK_END (hash_context_finish);
322}
323
324
325void
330
331
332/* end of crypto_hash.c */
benchmarking for various operations
#define BENCHMARK_START(opname)
Definition benchmark.h:57
#define BENCHMARK_END(opname)
Definition benchmark.h:58
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition gnunet-arm.c:98
static int ret
Final status code.
Definition gnunet-arm.c:93
struct GNUNET_HashCode key
The key used in the DHT.
static OpusEncoder * enc
OPUS encoder.
static struct GNUNET_SCHEDULER_Task * t
Main task.
static int state
The current state of the parser.
static int result
Global testing status.
void GNUNET_CRYPTO_hash_difference(const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *b, struct GNUNET_HashCode *result)
compute result = b - a
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition crypto_hash.c:40
void GNUNET_CRYPTO_hash_to_enc(const struct GNUNET_HashCode *block, struct GNUNET_CRYPTO_HashAsciiEncoded *result)
Convert hash to ASCII encoding.
Definition crypto_hash.c:56
void GNUNET_CRYPTO_hmac(const struct GNUNET_CRYPTO_AuthKey *key, const void *plaintext, size_t plaintext_len, struct GNUNET_HashCode *hmac)
Calculate HMAC of a message (RFC 2104)
void GNUNET_CRYPTO_hmac_raw(const unsigned char *key, unsigned int keylen, const void *plaintext, size_t plaintext_len, struct GNUNET_HashCode *hmac)
Calculate HMAC of a message (RFC 2104) with arbitrary key length.
void GNUNET_CRYPTO_hash_sum(const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *delta, struct GNUNET_HashCode *result)
compute result = a + delta
void GNUNET_CRYPTO_hash_xor(const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *b, struct GNUNET_HashCode *result)
compute result = a ^ b
int GNUNET_CRYPTO_hash_xorcmp(const struct GNUNET_HashCode *h1, const struct GNUNET_HashCode *h2, const struct GNUNET_HashCode *target)
Find out which of the two GNUNET_CRYPTO_hash codes is closer to target in the XOR metric (Kademlia).
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hash_from_string2(const char *enc, size_t enclen, struct GNUNET_HashCode *result)
Convert ASCII encoding back to a 'struct GNUNET_HashCode'.
Definition crypto_hash.c:73
int GNUNET_CRYPTO_hash_cmp(const struct GNUNET_HashCode *h1, const struct GNUNET_HashCode *h2)
Compare function for HashCodes, producing a total ordering of all hashcodes.
unsigned int GNUNET_CRYPTO_hash_distance_u32(const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *b)
Compute the distance between 2 hashcodes.
Definition crypto_hash.c:93
unsigned int GNUNET_CRYPTO_hash_count_leading_zeros(const struct GNUNET_HashCode *h)
Count the number of leading 0 bits in h.
void GNUNET_CRYPTO_hash_context_read(struct GNUNET_HashContext *hc, const void *buf, size_t size)
Add data to be hashed.
struct GNUNET_HashContext * GNUNET_CRYPTO_hash_context_copy(const struct GNUNET_HashContext *hc)
Make a copy of the hash computation.
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
void GNUNET_CRYPTO_hash_context_abort(struct GNUNET_HashContext *hc)
Abort hashing, do not bother calculating final result.
void GNUNET_CRYPTO_hash_context_finish(struct GNUNET_HashContext *hc, struct GNUNET_HashCode *r_hash)
Finish the hash computation.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
unsigned int GNUNET_CRYPTO_hash_count_tailing_zeros(const struct GNUNET_HashCode *h)
Count the number of tailing 0 bits in h.
GNUNET_GenericReturnValue
Named constants for return values.
struct GNUNET_HashContext * GNUNET_CRYPTO_hash_context_start()
Start incremental hashing operation.
#define GNUNET_static_assert(cond)
Assertion to be checked (if supported by C compiler) at compile time, otherwise checked at runtime an...
uint32_t bits[512/8/sizeof(uint32_t)]
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
char * GNUNET_STRINGS_data_to_string(const void *data, size_t size, char *out, size_t out_size)
Convert binary data to ASCII encoding using CrockfordBase32.
Definition strings.c:763
char * GNUNET_STRINGS_utf8_toupper(const char *input)
Convert the utf-8 input string to upper case.
Definition strings.c:506
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data(const char *enc, size_t enclen, void *out, size_t out_size)
Convert CrockfordBase32 encoding back to data.
Definition strings.c:843
static unsigned int size
Size of the "table".
Definition peer.c:68
static struct GNUNET_TIME_Relative delta
Definition speedup.c:36
type for (message) authentication keys
0-terminated ASCII encoding of a struct GNUNET_HashCode.
A 512-bit hashcode.
crypto_hash_sha512_state hd
Internal state of the hash function.