GNUnet 0.28.0-dev.2-27-gc87478450
 
Loading...
Searching...
No Matches
crypto_hash.c File Reference

SHA-512 GNUNET_CRYPTO_hash() related functions. More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "benchmark.h"
Include dependency graph for crypto_hash.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_HashContext
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "util-crypto-hash", __VA_ARGS__)
 
#define LOG_STRERROR_FILE(kind, syscall, filename)
 

Functions

void GNUNET_CRYPTO_hash (const void *block, size_t size, struct GNUNET_HashCode *ret)
 Compute hash of a given block.
 
void GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block, struct GNUNET_CRYPTO_HashAsciiEncoded *result)
 Convert hash to ASCII encoding.
 
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'.
 
unsigned int GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *b)
 Compute the distance between 2 hashcodes.
 
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_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
 
unsigned int GNUNET_CRYPTO_hash_count_leading_zeros (const struct GNUNET_HashCode *h)
 Count the number of leading 0 bits in h.
 
unsigned int GNUNET_CRYPTO_hash_count_tailing_zeros (const struct GNUNET_HashCode *h)
 Count the number of tailing 0 bits in h.
 
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.
 
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).
 
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_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)
 
struct GNUNET_HashContextGNUNET_CRYPTO_hash_context_start ()
 Start incremental hashing operation.
 
void GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc, const void *buf, size_t size)
 Add data to be hashed.
 
struct GNUNET_HashContextGNUNET_CRYPTO_hash_context_copy (const struct GNUNET_HashContext *hc)
 Make a copy of the hash computation.
 
void GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc, struct GNUNET_HashCode *r_hash)
 Finish the hash computation.
 
void GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc)
 Abort hashing, do not bother calculating final result.
 

Detailed Description

SHA-512 GNUNET_CRYPTO_hash() related functions.

Author
Christian Grothoff

Definition in file crypto_hash.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "util-crypto-hash", __VA_ARGS__)

Definition at line 31 of file crypto_hash.c.

◆ LOG_STRERROR_FILE

#define LOG_STRERROR_FILE (   kind,
  syscall,
  filename 
)
Value:
"util-crypto-hash", \
syscall, \
static char * filename
#define GNUNET_log_from_strerror_file(level, component, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...

Definition at line 33 of file crypto_hash.c.

42{
43 BENCHMARK_START (hash);
44 crypto_hash_sha512 ((unsigned char*) ret,
45 block,
46 size);
47 BENCHMARK_END (hash);
48}
49
50
51/* ***************** binary-ASCII encoding *************** */
52
53
54void
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
65 - 1);
66 GNUNET_assert (NULL != np);
67 *np = '\0';
68}
69
70
73 size_t enclen,
74 struct GNUNET_HashCode *result)
75{
77 char *up_ptr;
78
80 if (NULL == up_ptr)
81 return GNUNET_SYSERR;
83 strlen (up_ptr),
84 (unsigned char *) result,
85 sizeof(struct GNUNET_HashCode));
86 GNUNET_free (up_ptr);
87 return ret;
88}
89
90
91unsigned int
93 const struct GNUNET_HashCode *b)
94{
95 unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16;
96 unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16;
97
98 return(x1 * x2);
99}
100
101
102void
104 const struct GNUNET_HashCode *b,
105 struct GNUNET_HashCode *result)
106{
107 for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
108 i >= 0;
109 i--)
110 result->bits[i] = b->bits[i] - a->bits[i];
111}
112
113
114void
116 const struct GNUNET_HashCode *delta, struct
118{
119 for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
120 i >= 0;
121 i--)
122 result->bits[i] = delta->bits[i] + a->bits[i];
123}
124
125
126void
128 const struct GNUNET_HashCode *b,
129 struct GNUNET_HashCode *result)
130{
131 const unsigned long long *lla = (const unsigned long long *) a;
132 const unsigned long long *llb = (const unsigned long long *) b;
133 unsigned long long *llr = (unsigned long long *) result;
134
135 GNUNET_static_assert (8 == sizeof (unsigned long long));
136 GNUNET_static_assert (0 == sizeof (*a) % sizeof (unsigned long long));
137
138 for (int i = sizeof (*result) / sizeof (*llr) - 1; i>=0; i--)
139 llr[i] = lla[i] ^ llb[i];
140}
141
142
143unsigned int
145{
146 const unsigned long long *llp = (const unsigned long long *) h;
147 unsigned int ret = 0;
148 unsigned int i;
149
150 GNUNET_static_assert (8 == sizeof (unsigned long long));
151 GNUNET_static_assert (0 == sizeof (*h) % sizeof (unsigned long long));
152 for (i = 0; i<sizeof (*h) / sizeof (*llp); i++)
153 {
154 if (0LLU != llp[i])
155 break;
156 ret += sizeof (*llp) * 8;
157 }
158 if (ret == 8 * sizeof (*h))
159 return ret;
160 ret += __builtin_clzll (GNUNET_ntohll ((uint64_t) llp[i]));
161 return ret;
162}
163
164
165unsigned int
167{
168 const unsigned long long *llp = (const unsigned long long *) h;
169 unsigned int ret = 0;
170 int i;
171
172 GNUNET_static_assert (8 == sizeof (unsigned long long));
173 GNUNET_static_assert (0 == sizeof (*h) % sizeof (unsigned long long));
174 for (i = sizeof (*h) / sizeof (*llp) - 1; i>=0; i--)
175 {
176 if (0LLU != llp[i])
177 break;
178 ret += sizeof (*llp) * 8;
179 }
180 if (ret == 8 * sizeof (*h))
181 return ret;
182 ret += __builtin_ctzll (GNUNET_ntohll ((uint64_t) llp[i]));
183 return ret;
184}
185
186
187int
189 const struct GNUNET_HashCode *h2)
190{
191 unsigned int *i1;
192 unsigned int *i2;
193
194 i1 = (unsigned int *) h1;
195 i2 = (unsigned int *) h2;
196 for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
197 i >= 0;
198 i--)
199 {
200 if (i1[i] > i2[i])
201 return 1;
202 if (i1[i] < i2[i])
203 return -1;
204 }
205 return 0;
206}
207
208
209int
211 const struct GNUNET_HashCode *h2,
212 const struct GNUNET_HashCode *target)
213{
214 const unsigned long long *l1 = (const unsigned long long *) h1;
215 const unsigned long long *l2 = (const unsigned long long *) h2;
216 const unsigned long long *t = (const unsigned long long *) target;
217
218 GNUNET_static_assert (0 == sizeof (*h1) % sizeof (*l1));
219 for (size_t i = 0; i < sizeof(*h1) / sizeof(*l1); i++)
220 {
221 unsigned long long x1 = l1[i] ^ t[i];
222 unsigned long long x2 = l2[i] ^ t[i];
223
224 if (x1 > x2)
225 return 1;
226 if (x1 < x2)
227 return -1;
228 }
229 return 0;
230}
231
232
233void
234GNUNET_CRYPTO_hmac_raw (const unsigned char *key,
235 unsigned int keylen,
236 const void *plaintext,
237 size_t plaintext_len,
238 struct GNUNET_HashCode *hmac)
239{
240 crypto_auth_hmacsha512_state state;
241 crypto_auth_hmacsha512_init (&state,
242 key,
243 keylen);
244 crypto_auth_hmacsha512_update (&state,
245 plaintext,
246 plaintext_len);
247 crypto_auth_hmacsha512_final (&state,
248 (unsigned char*) hmac);
249}
250
251
252void
254 const void *plaintext, size_t plaintext_len,
255 struct GNUNET_HashCode *hmac)
256{
257 GNUNET_CRYPTO_hmac_raw ((unsigned char*) key,
258 sizeof *key,
259 plaintext,
260 plaintext_len,
261 hmac);
262}
263
264
266{
270 crypto_hash_sha512_state hd;
271};
272
273
274struct GNUNET_HashContext *
276{
277 struct GNUNET_HashContext *hc;
278
279 BENCHMARK_START (hash_context_start);
280 hc = GNUNET_new (struct GNUNET_HashContext);
281 crypto_hash_sha512_init (&hc->hd);
282 BENCHMARK_END (hash_context_start);
283 return hc;
284}
285
286
287void
289 const void *buf,
290 size_t size)
291{
292 BENCHMARK_START (hash_context_read);
293 crypto_hash_sha512_update (&hc->hd,
294 buf,
295 size);
296 BENCHMARK_END (hash_context_read);
297}
298
299
300struct GNUNET_HashContext *
302{
303 struct GNUNET_HashContext *cp;
304
305 cp = GNUNET_new (struct GNUNET_HashContext);
306 GNUNET_memcpy (cp, hc, sizeof *hc);
307 return cp;
308}
309
310
311void
313 struct GNUNET_HashCode *r_hash)
314{
315 BENCHMARK_START (hash_context_finish);
316
317 crypto_hash_sha512_final (&hc->hd,
318 (unsigned char*) r_hash);
320 BENCHMARK_END (hash_context_finish);
321}
322
323
324void
326{
327 GNUNET_free (hc);
328}
329
330
331/* end of crypto_hash.c */
#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_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.