crypto_hash.c | crypto_hash.c | |||
---|---|---|---|---|
skipping to change at line 108 | skipping to change at line 108 | |||
* Current offset. | * Current offset. | |||
*/ | */ | |||
uint64_t offset; | uint64_t offset; | |||
/** | /** | |||
* Current task for hashing. | * Current task for hashing. | |||
*/ | */ | |||
GNUNET_SCHEDULER_TaskIdentifier task; | GNUNET_SCHEDULER_TaskIdentifier task; | |||
/** | /** | |||
* Priority we use. | ||||
*/ | ||||
enum GNUNET_SCHEDULER_Priority priority; | ||||
/** | ||||
* Blocksize. | * Blocksize. | |||
*/ | */ | |||
size_t bsize; | size_t bsize; | |||
}; | }; | |||
/** | /** | |||
* Report result of hash computation to callback | * Report result of hash computation to callback | |||
* and free associated resources. | * and free associated resources. | |||
*/ | */ | |||
skipping to change at line 162 | skipping to change at line 167 | |||
return; | return; | |||
} | } | |||
gcry_md_write (fhc->md, fhc->buffer, delta); | gcry_md_write (fhc->md, fhc->buffer, delta); | |||
fhc->offset += delta; | fhc->offset += delta; | |||
if (fhc->offset == fhc->fsize) | if (fhc->offset == fhc->fsize) | |||
{ | { | |||
res = (GNUNET_HashCode *) gcry_md_read (fhc->md, GCRY_MD_SHA512); | res = (GNUNET_HashCode *) gcry_md_read (fhc->md, GCRY_MD_SHA512); | |||
file_hash_finish (fhc, res); | file_hash_finish (fhc, res); | |||
return; | return; | |||
} | } | |||
fhc->task = GNUNET_SCHEDULER_add_now (&file_hash_task, fhc); | fhc->task = GNUNET_SCHEDULER_add_with_priority (fhc->priority, | |||
&file_hash_task, fhc); | ||||
} | } | |||
/** | /** | |||
* Compute the hash of an entire file. | * Compute the hash of an entire file. | |||
* | * | |||
* @param priority scheduling priority to use | * @param priority scheduling priority to use | |||
* @param filename name of file to hash | * @param filename name of file to hash | |||
* @param blocksize number of bytes to process in one task | * @param blocksize number of bytes to process in one task | |||
* @param callback function to call upon completion | * @param callback function to call upon completion | |||
* @param callback_cls closure for callback | * @param callback_cls closure for callback | |||
skipping to change at line 212 | skipping to change at line 218 | |||
} | } | |||
fhc->fh = | fhc->fh = | |||
GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ, | GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ, | |||
GNUNET_DISK_PERM_NONE); | GNUNET_DISK_PERM_NONE); | |||
if (!fhc->fh) | if (!fhc->fh) | |||
{ | { | |||
GNUNET_free (fhc->filename); | GNUNET_free (fhc->filename); | |||
GNUNET_free (fhc); | GNUNET_free (fhc); | |||
return NULL; | return NULL; | |||
} | } | |||
fhc->priority = priority; | ||||
fhc->task = | fhc->task = | |||
GNUNET_SCHEDULER_add_with_priority (priority, &file_hash_task, fhc); | GNUNET_SCHEDULER_add_with_priority (priority, &file_hash_task, fhc); | |||
return fhc; | return fhc; | |||
} | } | |||
/** | /** | |||
* Cancel a file hashing operation. | * Cancel a file hashing operation. | |||
* | * | |||
* @param fhc operation to cancel (callback must not yet have been invoked) | * @param fhc operation to cancel (callback must not yet have been invoked) | |||
*/ | */ | |||
skipping to change at line 233 | skipping to change at line 240 | |||
GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc) | GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc) | |||
{ | { | |||
GNUNET_SCHEDULER_cancel (fhc->task); | GNUNET_SCHEDULER_cancel (fhc->task); | |||
GNUNET_free (fhc->filename); | GNUNET_free (fhc->filename); | |||
GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh)); | GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh)); | |||
GNUNET_free (fhc); | GNUNET_free (fhc); | |||
} | } | |||
/* ***************** binary-ASCII encoding *************** */ | /* ***************** binary-ASCII encoding *************** */ | |||
/** | ||||
* Get the numeric value corresponding to a character. | ||||
* | ||||
* @param a a character | ||||
* @return corresponding numeric value | ||||
*/ | ||||
static unsigned int | static unsigned int | |||
getValue__ (unsigned char a) | getValue__ (unsigned char a) | |||
{ | { | |||
if ((a >= '0') && (a <= '9')) | if ((a >= '0') && (a <= '9')) | |||
return a - '0'; | return a - '0'; | |||
if ((a >= 'A') && (a <= 'V')) | if ((a >= 'A') && (a <= 'V')) | |||
return (a - 'A' + 10); | return (a - 'A' + 10); | |||
return -1; | return -1; | |||
} | } | |||
skipping to change at line 299 | skipping to change at line 312 | |||
} | } | |||
GNUNET_assert (wpos == sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1 ); | GNUNET_assert (wpos == sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1 ); | |||
GNUNET_assert (vbit == 0); | GNUNET_assert (vbit == 0); | |||
result->encoding[wpos] = '\0'; | result->encoding[wpos] = '\0'; | |||
} | } | |||
/** | /** | |||
* Convert ASCII encoding back to GNUNET_CRYPTO_hash | * Convert ASCII encoding back to GNUNET_CRYPTO_hash | |||
* | * | |||
* @param enc the encoding | * @param enc the encoding | |||
* @param enclen number of characters in 'enc' (without 0-terminator, which can be missing) | ||||
* @param result where to store the GNUNET_CRYPTO_hash code | * @param result where to store the GNUNET_CRYPTO_hash code | |||
* @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong enco ding | * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong enco ding | |||
*/ | */ | |||
int | int | |||
GNUNET_CRYPTO_hash_from_string (const char *enc, GNUNET_HashCode * result) | GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen, | |||
GNUNET_HashCode * result) | ||||
{ | { | |||
unsigned int rpos; | unsigned int rpos; | |||
unsigned int wpos; | unsigned int wpos; | |||
unsigned int bits; | unsigned int bits; | |||
unsigned int vbit; | unsigned int vbit; | |||
int ret; | ||||
if (strlen (enc) != sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1) | if (enclen != sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1) | |||
return GNUNET_SYSERR; | return GNUNET_SYSERR; | |||
vbit = 2; /* padding! */ | vbit = 2; /* padding! */ | |||
wpos = sizeof (GNUNET_HashCode); | wpos = sizeof (GNUNET_HashCode); | |||
rpos = sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1; | rpos = sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1; | |||
bits = getValue__ (enc[--rpos]) >> 3; | bits = (ret = getValue__ (enc[--rpos])) >> 3; | |||
if (-1 == ret) | ||||
return GNUNET_SYSERR; | ||||
while (wpos > 0) | while (wpos > 0) | |||
{ | { | |||
GNUNET_assert (rpos > 0); | GNUNET_assert (rpos > 0); | |||
bits = (getValue__ (enc[--rpos]) << vbit) | bits; | bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits; | |||
if (-1 == ret) | ||||
return GNUNET_SYSERR; | ||||
vbit += 5; | vbit += 5; | |||
if (vbit >= 8) | if (vbit >= 8) | |||
{ | { | |||
((unsigned char *) result)[--wpos] = (unsigned char) bits; | ((unsigned char *) result)[--wpos] = (unsigned char) bits; | |||
bits >>= 8; | bits >>= 8; | |||
vbit -= 8; | vbit -= 8; | |||
} | } | |||
} | } | |||
GNUNET_assert (rpos == 0); | GNUNET_assert (rpos == 0); | |||
GNUNET_assert (vbit == 0); | GNUNET_assert (vbit == 0); | |||
skipping to change at line 355 | skipping to change at line 375 | |||
unsigned int | unsigned int | |||
GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a, | GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a, | |||
const GNUNET_HashCode * b) | const GNUNET_HashCode * b) | |||
{ | { | |||
unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16; | unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16; | |||
unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16; | unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16; | |||
return (x1 * x2); | return (x1 * x2); | |||
} | } | |||
/** | ||||
* Create a random hash code. | ||||
* | ||||
* @param mode desired quality level | ||||
* @param result hash code that is randomized | ||||
*/ | ||||
void | void | |||
GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode, | GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode, | |||
GNUNET_HashCode * result) | GNUNET_HashCode * result) | |||
{ | { | |||
int i; | int i; | |||
for (i = (sizeof (GNUNET_HashCode) / sizeof (uint32_t)) - 1; i >= 0; i--) | for (i = (sizeof (GNUNET_HashCode) / sizeof (uint32_t)) - 1; i >= 0; i--) | |||
result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, UINT32_MAX); | result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, UINT32_MAX); | |||
} | } | |||
/** | ||||
* compute result(delta) = b - a | ||||
* | ||||
* @param a some hash code | ||||
* @param b some hash code | ||||
* @param result set to b - a | ||||
*/ | ||||
void | void | |||
GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a, | GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a, | |||
const GNUNET_HashCode * b, | const GNUNET_HashCode * b, | |||
GNUNET_HashCode * result) | GNUNET_HashCode * result) | |||
{ | { | |||
int i; | int i; | |||
for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--) | for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--) | |||
result->bits[i] = b->bits[i] - a->bits[i]; | result->bits[i] = b->bits[i] - a->bits[i]; | |||
} | } | |||
/** | ||||
* compute result(b) = a + delta | ||||
* | ||||
* @param a some hash code | ||||
* @param delta some hash code | ||||
* @param result set to a + delta | ||||
*/ | ||||
void | void | |||
GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a, | GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a, | |||
const GNUNET_HashCode * delta, GNUNET_HashCode * re sult) | const GNUNET_HashCode * delta, GNUNET_HashCode * re sult) | |||
{ | { | |||
int i; | int i; | |||
for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--) | for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--) | |||
result->bits[i] = delta->bits[i] + a->bits[i]; | result->bits[i] = delta->bits[i] + a->bits[i]; | |||
} | } | |||
/** | ||||
* compute result = a ^ b | ||||
* | ||||
* @param a some hash code | ||||
* @param b some hash code | ||||
* @param result set to a ^ b | ||||
*/ | ||||
void | void | |||
GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b, | GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b, | |||
GNUNET_HashCode * result) | GNUNET_HashCode * result) | |||
{ | { | |||
int i; | int i; | |||
for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--) | for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--) | |||
result->bits[i] = a->bits[i] ^ b->bits[i]; | result->bits[i] = a->bits[i] ^ b->bits[i]; | |||
} | } | |||
/** | /** | |||
* Convert a hashcode into a key. | * Convert a hashcode into a key. | |||
* | ||||
* @param hc hash code that serves to generate the key | ||||
* @param skey set to a valid session key | ||||
* @param iv set to a valid initialization vector | ||||
*/ | */ | |||
void | void | |||
GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc, | GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc, | |||
struct GNUNET_CRYPTO_AesSessionKey *skey, | struct GNUNET_CRYPTO_AesSessionKey *skey, | |||
struct GNUNET_CRYPTO_AesInitializationVector *iv) | struct GNUNET_CRYPTO_AesInitializationVector *iv) | |||
{ | { | |||
GNUNET_assert (sizeof (GNUNET_HashCode) >= | GNUNET_assert (sizeof (GNUNET_HashCode) >= | |||
GNUNET_CRYPTO_AES_KEY_LENGTH + | GNUNET_CRYPTO_AES_KEY_LENGTH + | |||
sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); | sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); | |||
memcpy (skey, hc, GNUNET_CRYPTO_AES_KEY_LENGTH); | memcpy (skey, hc, GNUNET_CRYPTO_AES_KEY_LENGTH); | |||
skipping to change at line 455 | skipping to change at line 506 | |||
for (i = 0; i < sizeof (GNUNET_HashCode) * 8; i++) | for (i = 0; i < sizeof (GNUNET_HashCode) * 8; i++) | |||
if (GNUNET_CRYPTO_hash_get_bit (first, i) != | if (GNUNET_CRYPTO_hash_get_bit (first, i) != | |||
GNUNET_CRYPTO_hash_get_bit (second, i)) | GNUNET_CRYPTO_hash_get_bit (second, i)) | |||
return i; | return i; | |||
return sizeof (GNUNET_HashCode) * 8; | return sizeof (GNUNET_HashCode) * 8; | |||
} | } | |||
/** | /** | |||
* Compare function for HashCodes, producing a total ordering | * Compare function for HashCodes, producing a total ordering | |||
* of all hashcodes. | * of all hashcodes. | |||
* | ||||
* @param h1 some hash code | ||||
* @param h2 some hash code | ||||
* @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2. | * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2. | |||
*/ | */ | |||
int | int | |||
GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, const GNUNET_HashCode * h2) | GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, const GNUNET_HashCode * h2) | |||
{ | { | |||
unsigned int *i1; | unsigned int *i1; | |||
unsigned int *i2; | unsigned int *i2; | |||
int i; | int i; | |||
i1 = (unsigned int *) h1; | i1 = (unsigned int *) h1; | |||
skipping to change at line 479 | skipping to change at line 533 | |||
return 1; | return 1; | |||
if (i1[i] < i2[i]) | if (i1[i] < i2[i]) | |||
return -1; | return -1; | |||
} | } | |||
return 0; | return 0; | |||
} | } | |||
/** | /** | |||
* Find out which of the two GNUNET_CRYPTO_hash codes is closer to target | * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target | |||
* in the XOR metric (Kademlia). | * in the XOR metric (Kademlia). | |||
* | ||||
* @param h1 some hash code | ||||
* @param h2 some hash code | ||||
* @param target some hash code | ||||
* @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2. | * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2. | |||
*/ | */ | |||
int | int | |||
GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1, | GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1, | |||
const GNUNET_HashCode * h2, | const GNUNET_HashCode * h2, | |||
const GNUNET_HashCode * target) | const GNUNET_HashCode * target) | |||
{ | { | |||
int i; | int i; | |||
unsigned int d1; | unsigned int d1; | |||
unsigned int d2; | unsigned int d2; | |||
End of changes. 17 change blocks. | ||||
5 lines changed or deleted | 63 lines changed or added | |||
This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/ |