diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-01-07 20:15:45 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-01-07 20:15:45 +0000 |
commit | b89c033494de27537c6b936e2effdea975d75075 (patch) | |
tree | 144ad73d3b38372d26a99b4105992b3719f5bd45 /src/util/crypto_hash.c | |
parent | 705966df17b43ca8f1481834a80d19cd7cad874b (diff) | |
download | gnunet-b89c033494de27537c6b936e2effdea975d75075.tar.gz gnunet-b89c033494de27537c6b936e2effdea975d75075.zip |
-add missing comments, expand error checking
Diffstat (limited to 'src/util/crypto_hash.c')
-rw-r--r-- | src/util/crypto_hash.c | 60 |
1 files changed, 58 insertions, 2 deletions
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c index 9faa213ae..0350df545 100644 --- a/src/util/crypto_hash.c +++ b/src/util/crypto_hash.c | |||
@@ -244,6 +244,12 @@ GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc) | |||
244 | 244 | ||
245 | /* ***************** binary-ASCII encoding *************** */ | 245 | /* ***************** binary-ASCII encoding *************** */ |
246 | 246 | ||
247 | /** | ||
248 | * Get the numeric value corresponding to a character. | ||
249 | * | ||
250 | * @param a a character | ||
251 | * @return corresponding numeric value | ||
252 | */ | ||
247 | static unsigned int | 253 | static unsigned int |
248 | getValue__ (unsigned char a) | 254 | getValue__ (unsigned char a) |
249 | { | 255 | { |
@@ -254,6 +260,7 @@ getValue__ (unsigned char a) | |||
254 | return -1; | 260 | return -1; |
255 | } | 261 | } |
256 | 262 | ||
263 | |||
257 | /** | 264 | /** |
258 | * Convert GNUNET_CRYPTO_hash to ASCII encoding. The ASCII encoding is rather | 265 | * Convert GNUNET_CRYPTO_hash to ASCII encoding. The ASCII encoding is rather |
259 | * GNUnet specific. It was chosen such that it only uses characters | 266 | * GNUnet specific. It was chosen such that it only uses characters |
@@ -306,6 +313,7 @@ GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block, | |||
306 | result->encoding[wpos] = '\0'; | 313 | result->encoding[wpos] = '\0'; |
307 | } | 314 | } |
308 | 315 | ||
316 | |||
309 | /** | 317 | /** |
310 | * Convert ASCII encoding back to GNUNET_CRYPTO_hash | 318 | * Convert ASCII encoding back to GNUNET_CRYPTO_hash |
311 | * | 319 | * |
@@ -320,6 +328,7 @@ GNUNET_CRYPTO_hash_from_string (const char *enc, GNUNET_HashCode * result) | |||
320 | unsigned int wpos; | 328 | unsigned int wpos; |
321 | unsigned int bits; | 329 | unsigned int bits; |
322 | unsigned int vbit; | 330 | unsigned int vbit; |
331 | int ret; | ||
323 | 332 | ||
324 | if (strlen (enc) != sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1) | 333 | if (strlen (enc) != sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1) |
325 | return GNUNET_SYSERR; | 334 | return GNUNET_SYSERR; |
@@ -327,11 +336,15 @@ GNUNET_CRYPTO_hash_from_string (const char *enc, GNUNET_HashCode * result) | |||
327 | vbit = 2; /* padding! */ | 336 | vbit = 2; /* padding! */ |
328 | wpos = sizeof (GNUNET_HashCode); | 337 | wpos = sizeof (GNUNET_HashCode); |
329 | rpos = sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1; | 338 | rpos = sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1; |
330 | bits = getValue__ (enc[--rpos]) >> 3; | 339 | bits = (ret = getValue__ (enc[--rpos])) >> 3; |
340 | if (-1 == ret) | ||
341 | return GNUNET_SYSERR; | ||
331 | while (wpos > 0) | 342 | while (wpos > 0) |
332 | { | 343 | { |
333 | GNUNET_assert (rpos > 0); | 344 | GNUNET_assert (rpos > 0); |
334 | bits = (getValue__ (enc[--rpos]) << vbit) | bits; | 345 | bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits; |
346 | if (-1 == ret) | ||
347 | return GNUNET_SYSERR; | ||
335 | vbit += 5; | 348 | vbit += 5; |
336 | if (vbit >= 8) | 349 | if (vbit >= 8) |
337 | { | 350 | { |
@@ -345,6 +358,7 @@ GNUNET_CRYPTO_hash_from_string (const char *enc, GNUNET_HashCode * result) | |||
345 | return GNUNET_OK; | 358 | return GNUNET_OK; |
346 | } | 359 | } |
347 | 360 | ||
361 | |||
348 | /** | 362 | /** |
349 | * Compute the distance between 2 hashcodes. The computation must be | 363 | * Compute the distance between 2 hashcodes. The computation must be |
350 | * fast, not involve bits[0] or bits[4] (they're used elsewhere), and be | 364 | * fast, not involve bits[0] or bits[4] (they're used elsewhere), and be |
@@ -366,6 +380,13 @@ GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a, | |||
366 | return (x1 * x2); | 380 | return (x1 * x2); |
367 | } | 381 | } |
368 | 382 | ||
383 | |||
384 | /** | ||
385 | * Create a random hash code. | ||
386 | * | ||
387 | * @param mode desired quality level | ||
388 | * @param result hash code that is randomized | ||
389 | */ | ||
369 | void | 390 | void |
370 | GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode, | 391 | GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode, |
371 | GNUNET_HashCode * result) | 392 | GNUNET_HashCode * result) |
@@ -376,6 +397,14 @@ GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode, | |||
376 | result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, UINT32_MAX); | 397 | result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, UINT32_MAX); |
377 | } | 398 | } |
378 | 399 | ||
400 | |||
401 | /** | ||
402 | * compute result(delta) = b - a | ||
403 | * | ||
404 | * @param a some hash code | ||
405 | * @param b some hash code | ||
406 | * @param result set to b - a | ||
407 | */ | ||
379 | void | 408 | void |
380 | GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a, | 409 | GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a, |
381 | const GNUNET_HashCode * b, | 410 | const GNUNET_HashCode * b, |
@@ -387,6 +416,14 @@ GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a, | |||
387 | result->bits[i] = b->bits[i] - a->bits[i]; | 416 | result->bits[i] = b->bits[i] - a->bits[i]; |
388 | } | 417 | } |
389 | 418 | ||
419 | |||
420 | /** | ||
421 | * compute result(b) = a + delta | ||
422 | * | ||
423 | * @param a some hash code | ||
424 | * @param delta some hash code | ||
425 | * @param result set to a + delta | ||
426 | */ | ||
390 | void | 427 | void |
391 | GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a, | 428 | GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a, |
392 | const GNUNET_HashCode * delta, GNUNET_HashCode * result) | 429 | const GNUNET_HashCode * delta, GNUNET_HashCode * result) |
@@ -398,6 +435,13 @@ GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a, | |||
398 | } | 435 | } |
399 | 436 | ||
400 | 437 | ||
438 | /** | ||
439 | * compute result = a ^ b | ||
440 | * | ||
441 | * @param a some hash code | ||
442 | * @param b some hash code | ||
443 | * @param result set to a ^ b | ||
444 | */ | ||
401 | void | 445 | void |
402 | GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b, | 446 | GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b, |
403 | GNUNET_HashCode * result) | 447 | GNUNET_HashCode * result) |
@@ -411,6 +455,10 @@ GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b, | |||
411 | 455 | ||
412 | /** | 456 | /** |
413 | * Convert a hashcode into a key. | 457 | * Convert a hashcode into a key. |
458 | * | ||
459 | * @param hc hash code that serves to generate the key | ||
460 | * @param skey set to a valid session key | ||
461 | * @param iv set to a valid initialization vector | ||
414 | */ | 462 | */ |
415 | void | 463 | void |
416 | GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc, | 464 | GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc, |
@@ -441,6 +489,7 @@ GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code, unsigned int bit) | |||
441 | return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0; | 489 | return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0; |
442 | } | 490 | } |
443 | 491 | ||
492 | |||
444 | /** | 493 | /** |
445 | * Determine how many low order bits match in two | 494 | * Determine how many low order bits match in two |
446 | * GNUNET_HashCodes. i.e. - 010011 and 011111 share | 495 | * GNUNET_HashCodes. i.e. - 010011 and 011111 share |
@@ -470,6 +519,9 @@ GNUNET_CRYPTO_hash_matching_bits (const GNUNET_HashCode * first, | |||
470 | /** | 519 | /** |
471 | * Compare function for HashCodes, producing a total ordering | 520 | * Compare function for HashCodes, producing a total ordering |
472 | * of all hashcodes. | 521 | * of all hashcodes. |
522 | * | ||
523 | * @param h1 some hash code | ||
524 | * @param h2 some hash code | ||
473 | * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2. | 525 | * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2. |
474 | */ | 526 | */ |
475 | int | 527 | int |
@@ -495,6 +547,10 @@ GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, const GNUNET_HashCode * h2) | |||
495 | /** | 547 | /** |
496 | * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target | 548 | * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target |
497 | * in the XOR metric (Kademlia). | 549 | * in the XOR metric (Kademlia). |
550 | * | ||
551 | * @param h1 some hash code | ||
552 | * @param h2 some hash code | ||
553 | * @param target some hash code | ||
498 | * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2. | 554 | * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2. |
499 | */ | 555 | */ |
500 | int | 556 | int |