GNUnet 0.26.2-98-gb402d9955
 
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 <gcrypt.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_create_random (enum GNUNET_CRYPTO_Quality mode, struct GNUNET_HashCode *result)
 Create a random hash code.
 
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
 
void GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode *hc, struct GNUNET_CRYPTO_SymmetricSessionKey *skey, struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
 Convert a hashcode into a key.
 
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 void *key, size_t key_len, const void *plaintext, size_t plaintext_len, struct GNUNET_HashCode *hmac)
 Calculate HMAC of a message (RFC 2104) TODO: Shouldn't this be the standard hmac function and the above be renamed?
 
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 32 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 34 of file crypto_hash.c.

43{
44 BENCHMARK_START (hash);
45 gcry_md_hash_buffer (GCRY_MD_SHA512, ret, block, size);
46 BENCHMARK_END (hash);
47}
48
49
50/* ***************** binary-ASCII encoding *************** */
51
52
53void
56{
57 char *np;
58
59 np = GNUNET_STRINGS_data_to_string ((const unsigned char *) block,
60 sizeof(struct GNUNET_HashCode),
61 (char *) result,
62 sizeof(struct
64 - 1);
65 GNUNET_assert (NULL != np);
66 *np = '\0';
67}
68
69
72 size_t enclen,
73 struct GNUNET_HashCode *result)
74{
76 char *up_ptr;
77
79 if (NULL == up_ptr)
80 return GNUNET_SYSERR;
82 strlen (up_ptr),
83 (unsigned char *) result,
84 sizeof(struct GNUNET_HashCode));
85 GNUNET_free (up_ptr);
86 return ret;
87}
88
89
90unsigned int
92 const struct GNUNET_HashCode *b)
93{
94 unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16;
95 unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16;
96
97 return(x1 * x2);
98}
99
100
101void
103 struct GNUNET_HashCode *result)
104{
106}
107
108
109void
111 const struct GNUNET_HashCode *b,
112 struct GNUNET_HashCode *result)
113{
114 for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
115 i >= 0;
116 i--)
117 result->bits[i] = b->bits[i] - a->bits[i];
118}
119
120
121void
123 const struct GNUNET_HashCode *delta, struct
125{
126 for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
127 i >= 0;
128 i--)
129 result->bits[i] = delta->bits[i] + a->bits[i];
130}
131
132
133void
135 const struct GNUNET_HashCode *b,
136 struct GNUNET_HashCode *result)
137{
138 const unsigned long long *lla = (const unsigned long long *) a;
139 const unsigned long long *llb = (const unsigned long long *) b;
140 unsigned long long *llr = (unsigned long long *) result;
141
142 GNUNET_static_assert (8 == sizeof (unsigned long long));
143 GNUNET_static_assert (0 == sizeof (*a) % sizeof (unsigned long long));
144
145 for (int i = sizeof (*result) / sizeof (*llr) - 1; i>=0; i--)
146 llr[i] = lla[i] ^ llb[i];
147}
148
149
150void
152 const struct GNUNET_HashCode *hc,
155{
158 skey,
159 sizeof(*skey),
160 "Hash key derivation",
161 strlen ("Hash key derivation"),
162 hc, sizeof(*hc)));
165 iv,
166 sizeof(*iv),
167 "Initialization vector derivation",
168 strlen ("Initialization vector derivation"),
169 hc, sizeof(*hc)));
170}
171
172
173unsigned int
175{
176 const unsigned long long *llp = (const unsigned long long *) h;
177 unsigned int ret = 0;
178 unsigned int i;
179
180 GNUNET_static_assert (8 == sizeof (unsigned long long));
181 GNUNET_static_assert (0 == sizeof (*h) % sizeof (unsigned long long));
182 for (i = 0; i<sizeof (*h) / sizeof (*llp); i++)
183 {
184 if (0LLU != llp[i])
185 break;
186 ret += sizeof (*llp) * 8;
187 }
188 if (ret == 8 * sizeof (*h))
189 return ret;
190 ret += __builtin_clzll (GNUNET_ntohll ((uint64_t) llp[i]));
191 return ret;
192}
193
194
195unsigned int
197{
198 const unsigned long long *llp = (const unsigned long long *) h;
199 unsigned int ret = 0;
200 int i;
201
202 GNUNET_static_assert (8 == sizeof (unsigned long long));
203 GNUNET_static_assert (0 == sizeof (*h) % sizeof (unsigned long long));
204 for (i = sizeof (*h) / sizeof (*llp) - 1; i>=0; i--)
205 {
206 if (0LLU != llp[i])
207 break;
208 ret += sizeof (*llp) * 8;
209 }
210 if (ret == 8 * sizeof (*h))
211 return ret;
212 ret += __builtin_ctzll (GNUNET_ntohll ((uint64_t) llp[i]));
213 return ret;
214}
215
216
217int
219 const struct GNUNET_HashCode *h2)
220{
221 unsigned int *i1;
222 unsigned int *i2;
223
224 i1 = (unsigned int *) h1;
225 i2 = (unsigned int *) h2;
226 for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
227 i >= 0;
228 i--)
229 {
230 if (i1[i] > i2[i])
231 return 1;
232 if (i1[i] < i2[i])
233 return -1;
234 }
235 return 0;
236}
237
238
239int
241 const struct GNUNET_HashCode *h2,
242 const struct GNUNET_HashCode *target)
243{
244 const unsigned long long *l1 = (const unsigned long long *) h1;
245 const unsigned long long *l2 = (const unsigned long long *) h2;
246 const unsigned long long *t = (const unsigned long long *) target;
247
248 GNUNET_static_assert (0 == sizeof (*h1) % sizeof (*l1));
249 for (size_t i = 0; i < sizeof(*h1) / sizeof(*l1); i++)
250 {
251 unsigned long long x1 = l1[i] ^ t[i];
252 unsigned long long x2 = l2[i] ^ t[i];
253
254 if (x1 > x2)
255 return 1;
256 if (x1 < x2)
257 return -1;
258 }
259 return 0;
260}
261
262
263void
264GNUNET_CRYPTO_hmac_raw (const void *key, size_t key_len,
265 const void *plaintext, size_t plaintext_len,
266 struct GNUNET_HashCode *hmac)
267{
268 static int once;
269 static gcry_md_hd_t md;
270 const unsigned char *mc;
271
272 if (! once)
273 {
274 once = 1;
275 GNUNET_assert (GPG_ERR_NO_ERROR ==
276 gcry_md_open (&md,
277 GCRY_MD_SHA512,
278 GCRY_MD_FLAG_HMAC));
279 }
280 else
281 {
282 gcry_md_reset (md);
283 }
284 GNUNET_assert (GPG_ERR_NO_ERROR ==
285 gcry_md_setkey (md, key, key_len));
286 gcry_md_write (md, plaintext, plaintext_len);
287 mc = gcry_md_read (md, GCRY_MD_SHA512);
288 GNUNET_assert (NULL != mc);
289 GNUNET_memcpy (hmac->bits, mc, sizeof(hmac->bits));
290}
291
292
293void
295 const void *plaintext, size_t plaintext_len,
296 struct GNUNET_HashCode *hmac)
297{
298 GNUNET_CRYPTO_hmac_raw ((void *) key->key, sizeof(key->key),
299 plaintext, plaintext_len,
300 hmac);
301}
302
303
305{
309 gcry_md_hd_t hd;
310};
311
312
313struct GNUNET_HashContext *
315{
316 struct GNUNET_HashContext *hc;
317
318 BENCHMARK_START (hash_context_start);
319 hc = GNUNET_new (struct GNUNET_HashContext);
320 GNUNET_assert (0 ==
321 gcry_md_open (&hc->hd,
322 GCRY_MD_SHA512,
323 0));
324 BENCHMARK_END (hash_context_start);
325 return hc;
326}
327
328
329void
331 const void *buf,
332 size_t size)
333{
334 BENCHMARK_START (hash_context_read);
335 gcry_md_write (hc->hd, buf, size);
336 BENCHMARK_END (hash_context_read);
337}
338
339
340struct GNUNET_HashContext *
342{
343 struct GNUNET_HashContext *cp;
344
345 cp = GNUNET_new (struct GNUNET_HashContext);
346 GNUNET_assert (0 ==
347 gcry_md_copy (&cp->hd,
348 hc->hd));
349 return cp;
350}
351
352
353void
355 struct GNUNET_HashCode *r_hash)
356{
357 const void *res = gcry_md_read (hc->hd, 0);
358
359 BENCHMARK_START (hash_context_finish);
360
361 GNUNET_assert (NULL != res);
362 if (NULL != r_hash)
363 GNUNET_memcpy (r_hash,
364 res,
365 sizeof(struct GNUNET_HashCode));
367 BENCHMARK_END (hash_context_finish);
368}
369
370
371void
373{
374 gcry_md_close (hc->hd);
375 GNUNET_free (hc);
376}
377
378
379/* 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 char * res
Currently read line or NULL on EOF.
static int once
Option -i.
Definition gnunet-pils.c:39
static struct GNUNET_TESTBED_Controller * mc
Handle to the master controller.
static int result
Global testing status.
static enum @52 mode
Should we do a PUT (mode = 0) or GET (mode = 1);.
void GNUNET_CRYPTO_random_block(enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length)
Fill block with a random values.
GNUNET_CRYPTO_Quality
Desired quality level for random numbers.
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:55
void GNUNET_CRYPTO_hash_create_random(enum GNUNET_CRYPTO_Quality mode, struct GNUNET_HashCode *result)
Create a random hash code.
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_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:72
#define GNUNET_CRYPTO_hkdf_gnunet(result, out_len, xts, xts_len, skm, skm_len,...)
A peculiar HKDF instantiation that tried to mimic Truncated NMAC.
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:92
void GNUNET_CRYPTO_hash_to_aes_key(const struct GNUNET_HashCode *hc, struct GNUNET_CRYPTO_SymmetricSessionKey *skey, struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
Convert a hashcode into a key.
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.
void GNUNET_CRYPTO_hmac_raw(const void *key, size_t key_len, const void *plaintext, size_t plaintext_len, struct GNUNET_HashCode *hmac)
Calculate HMAC of a message (RFC 2104) TODO: Shouldn't this be the standard hmac function and the abo...
#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_YES
@ 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:757
char * GNUNET_STRINGS_utf8_toupper(const char *input)
Convert the utf-8 input string to upper case.
Definition strings.c:500
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:837
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.
gcry_md_hd_t hd
Internal state of the hash function.