GNUnet debian-0.24.3-29-g453fda2cf
 
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_derive_key (struct GNUNET_CRYPTO_AuthKey *key, const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey, const void *salt, size_t salt_len,...)
 Derive an authentication key.
 
void GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key, const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey, const void *salt, size_t salt_len, va_list argp)
 Derive an authentication key.
 
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{
75 char upper_enc[enclen + 1];
76 char *up_ptr = upper_enc;
77
79 return GNUNET_SYSERR;
80
81 return GNUNET_STRINGS_string_to_data (upper_enc, enclen,
82 (unsigned char *) result,
83 sizeof(struct GNUNET_HashCode));
84}
85
86
87unsigned int
89 const struct GNUNET_HashCode *b)
90{
91 unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16;
92 unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16;
93
94 return(x1 * x2);
95}
96
97
98void
100 struct GNUNET_HashCode *result)
101{
103}
104
105
106void
108 const struct GNUNET_HashCode *b,
109 struct GNUNET_HashCode *result)
110{
111 for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
112 i >= 0;
113 i--)
114 result->bits[i] = b->bits[i] - a->bits[i];
115}
116
117
118void
120 const struct GNUNET_HashCode *delta, struct
122{
123 for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
124 i >= 0;
125 i--)
126 result->bits[i] = delta->bits[i] + a->bits[i];
127}
128
129
130void
132 const struct GNUNET_HashCode *b,
133 struct GNUNET_HashCode *result)
134{
135 const unsigned long long *lla = (const unsigned long long *) a;
136 const unsigned long long *llb = (const unsigned long long *) b;
137 unsigned long long *llr = (unsigned long long *) result;
138
139 GNUNET_static_assert (8 == sizeof (unsigned long long));
140 GNUNET_static_assert (0 == sizeof (*a) % sizeof (unsigned long long));
141
142 for (int i = sizeof (*result) / sizeof (*llr) - 1; i>=0; i--)
143 llr[i] = lla[i] ^ llb[i];
144}
145
146
147void
149 const struct GNUNET_HashCode *hc,
152{
155 skey,
156 sizeof(*skey),
157 "Hash key derivation",
158 strlen ("Hash key derivation"),
159 hc, sizeof(*hc),
160 NULL, 0));
163 iv,
164 sizeof(*iv),
165 "Initialization vector derivation",
166 strlen ("Initialization vector derivation"),
167 hc, sizeof(*hc),
168 NULL, 0));
169}
170
171
172unsigned int
174{
175 const unsigned long long *llp = (const unsigned long long *) h;
176 unsigned int ret = 0;
177 unsigned int i;
178
179 GNUNET_static_assert (8 == sizeof (unsigned long long));
180 GNUNET_static_assert (0 == sizeof (*h) % sizeof (unsigned long long));
181 for (i = 0; i<sizeof (*h) / sizeof (*llp); i++)
182 {
183 if (0LLU != llp[i])
184 break;
185 ret += sizeof (*llp) * 8;
186 }
187 if (ret == 8 * sizeof (*h))
188 return ret;
189 ret += __builtin_clzll (GNUNET_ntohll ((uint64_t) llp[i]));
190 return ret;
191}
192
193
194unsigned int
196{
197 const unsigned long long *llp = (const unsigned long long *) h;
198 unsigned int ret = 0;
199 int i;
200
201 GNUNET_static_assert (8 == sizeof (unsigned long long));
202 GNUNET_static_assert (0 == sizeof (*h) % sizeof (unsigned long long));
203 for (i = sizeof (*h) / sizeof (*llp) - 1; i>=0; i--)
204 {
205 if (0LLU != llp[i])
206 break;
207 ret += sizeof (*llp) * 8;
208 }
209 if (ret == 8 * sizeof (*h))
210 return ret;
211 ret += __builtin_ctzll (GNUNET_ntohll ((uint64_t) llp[i]));
212 return ret;
213}
214
215
216int
218 const struct GNUNET_HashCode *h2)
219{
220 unsigned int *i1;
221 unsigned int *i2;
222
223 i1 = (unsigned int *) h1;
224 i2 = (unsigned int *) h2;
225 for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
226 i >= 0;
227 i--)
228 {
229 if (i1[i] > i2[i])
230 return 1;
231 if (i1[i] < i2[i])
232 return -1;
233 }
234 return 0;
235}
236
237
238int
240 const struct GNUNET_HashCode *h2,
241 const struct GNUNET_HashCode *target)
242{
243 const unsigned long long *l1 = (const unsigned long long *) h1;
244 const unsigned long long *l2 = (const unsigned long long *) h2;
245 const unsigned long long *t = (const unsigned long long *) target;
246
247 GNUNET_static_assert (0 == sizeof (*h1) % sizeof (*l1));
248 for (size_t i = 0; i < sizeof(*h1) / sizeof(*l1); i++)
249 {
250 unsigned long long x1 = l1[i] ^ t[i];
251 unsigned long long x2 = l2[i] ^ t[i];
252
253 if (x1 > x2)
254 return 1;
255 if (x1 < x2)
256 return -1;
257 }
258 return 0;
259}
260
261
262void
265 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
266 const void *salt, size_t salt_len,
267 ...)
268{
269 va_list argp;
270
271 va_start (argp,
272 salt_len);
274 rkey,
275 salt, salt_len,
276 argp);
277 va_end (argp);
278}
279
280
281void
284 const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
285 const void *salt, size_t salt_len,
286 va_list argp)
287{
288 GNUNET_CRYPTO_kdf_v (key->key, sizeof(key->key),
289 salt, salt_len,
290 rkey, sizeof(struct GNUNET_CRYPTO_SymmetricSessionKey),
291 argp);
292}
293
294
295void
296GNUNET_CRYPTO_hmac_raw (const void *key, size_t key_len,
297 const void *plaintext, size_t plaintext_len,
298 struct GNUNET_HashCode *hmac)
299{
300 static int once;
301 static gcry_md_hd_t md;
302 const unsigned char *mc;
303
304 if (! once)
305 {
306 once = 1;
307 GNUNET_assert (GPG_ERR_NO_ERROR ==
308 gcry_md_open (&md,
309 GCRY_MD_SHA512,
310 GCRY_MD_FLAG_HMAC));
311 }
312 else
313 {
314 gcry_md_reset (md);
315 }
316 GNUNET_assert (GPG_ERR_NO_ERROR ==
317 gcry_md_setkey (md, key, key_len));
318 gcry_md_write (md, plaintext, plaintext_len);
319 mc = gcry_md_read (md, GCRY_MD_SHA512);
320 GNUNET_assert (NULL != mc);
321 GNUNET_memcpy (hmac->bits, mc, sizeof(hmac->bits));
322}
323
324
325void
327 const void *plaintext, size_t plaintext_len,
328 struct GNUNET_HashCode *hmac)
329{
330 GNUNET_CRYPTO_hmac_raw ((void *) key->key, sizeof(key->key),
331 plaintext, plaintext_len,
332 hmac);
333}
334
335
337{
341 gcry_md_hd_t hd;
342};
343
344
345struct GNUNET_HashContext *
347{
348 struct GNUNET_HashContext *hc;
349
350 BENCHMARK_START (hash_context_start);
351 hc = GNUNET_new (struct GNUNET_HashContext);
352 GNUNET_assert (0 ==
353 gcry_md_open (&hc->hd,
354 GCRY_MD_SHA512,
355 0));
356 BENCHMARK_END (hash_context_start);
357 return hc;
358}
359
360
361void
363 const void *buf,
364 size_t size)
365{
366 BENCHMARK_START (hash_context_read);
367 gcry_md_write (hc->hd, buf, size);
368 BENCHMARK_END (hash_context_read);
369}
370
371
372struct GNUNET_HashContext *
374{
375 struct GNUNET_HashContext *cp;
376
377 cp = GNUNET_new (struct GNUNET_HashContext);
378 GNUNET_assert (0 ==
379 gcry_md_copy (&cp->hd,
380 hc->hd));
381 return cp;
382}
383
384
385void
387 struct GNUNET_HashCode *r_hash)
388{
389 const void *res = gcry_md_read (hc->hd, 0);
390
391 BENCHMARK_START (hash_context_finish);
392
393 GNUNET_assert (NULL != res);
394 if (NULL != r_hash)
395 GNUNET_memcpy (r_hash,
396 res,
397 sizeof(struct GNUNET_HashCode));
399 BENCHMARK_END (hash_context_finish);
400}
401
402
403void
405{
406 gcry_md_close (hc->hd);
407 GNUNET_free (hc);
408}
409
410
411/* 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 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 struct GNUNET_CRYPTO_PowSalt salt
Salt for PoW calculations.
static struct GNUNET_SCHEDULER_Task * t
Main task.
static enum @44 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).
void GNUNET_CRYPTO_hmac_derive_key_v(struct GNUNET_CRYPTO_AuthKey *key, const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey, const void *salt, size_t salt_len, va_list argp)
Derive an authentication key.
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
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:89
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_kdf(void *result, size_t out_len, const void *xts, size_t xts_len, const void *skm, size_t skm_len,...)
Derive key.
Definition crypto_kdf.c:62
void GNUNET_CRYPTO_hmac_derive_key(struct GNUNET_CRYPTO_AuthKey *key, const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey, const void *salt, size_t salt_len,...)
Derive an authentication key.
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.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_kdf_v(void *result, size_t out_len, const void *xts, size_t xts_len, const void *skm, size_t skm_len, va_list argp)
Derive key.
Definition crypto_kdf.c:38
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_OK
@ 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:752
enum GNUNET_GenericReturnValue GNUNET_STRINGS_utf8_toupper(const char *input, char *output)
Convert the utf-8 input string to upper case.
Definition strings.c:497
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:832
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.