Esempio n. 1
0
/**
 * Determine how many low order bits match in two
 * struct GNUNET_HashCodes.  i.e. - 010011 and 011111 share
 * the first two lowest order bits, and therefore the
 * return value is two (NOT XOR distance, nor how many
 * bits match absolutely!).
 *
 * @param first the first hashcode
 * @param second the hashcode to compare first to
 *
 * @return the number of bits that match
 */
unsigned int
GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode * first,
                                  const struct GNUNET_HashCode * second)
{
    unsigned int i;

    for (i = 0; i < sizeof (struct GNUNET_HashCode) * 8; i++)
        if (GNUNET_CRYPTO_hash_get_bit (first, i) !=
                GNUNET_CRYPTO_hash_get_bit (second, i))
            return i;
    return sizeof (struct GNUNET_HashCode) * 8;
}
Esempio n. 2
0
/**
 * Count the leading zeroes in hash.
 *
 * @param hash to count leading zeros in
 * @return the number of leading zero bits.
 */
static unsigned int
count_leading_zeroes (const struct GNUNET_HashCode *hash)
{
  unsigned int hash_count;

  hash_count = 0;
  while (0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count))
    hash_count++;
  return hash_count;
}
Esempio n. 3
0
static int
testArithmetic ()
{
  static struct GNUNET_CRYPTO_AesSessionKey zskey;
  static struct GNUNET_CRYPTO_AesInitializationVector ziv;
  struct GNUNET_HashCode h1;
  struct GNUNET_HashCode h2;
  struct GNUNET_HashCode d;
  struct GNUNET_HashCode s;
  struct GNUNET_CRYPTO_AesSessionKey skey;
  struct GNUNET_CRYPTO_AesInitializationVector iv;

  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h1);
  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h2);
  if (GNUNET_CRYPTO_hash_distance_u32 (&h1, &h2) !=
      GNUNET_CRYPTO_hash_distance_u32 (&h2, &h1))
    return 1;
  GNUNET_CRYPTO_hash_difference (&h1, &h2, &d);
  GNUNET_CRYPTO_hash_sum (&h1, &d, &s);
  if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2))
    return 1;
  GNUNET_CRYPTO_hash_xor (&h1, &h2, &d);
  GNUNET_CRYPTO_hash_xor (&h1, &d, &s);
  if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2))
    return 1;
  if (0 != GNUNET_CRYPTO_hash_xorcmp (&s, &h2, &h1))
    return 1;
  if (-1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h1))
    return 1;
  if (1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h2))
    return 1;
  memset (&d, 0xF0, sizeof (d));
  if (0 != GNUNET_CRYPTO_hash_get_bit (&d, 3))
    return 1;
  if (1 != GNUNET_CRYPTO_hash_get_bit (&d, 6))
    return 1;
  memset (&d, 0, sizeof (d));
  GNUNET_CRYPTO_hash_to_aes_key (&d, &skey, &iv);
  if ((0 != memcmp (&skey, &zskey, sizeof (skey) - sizeof (unsigned int))) ||
      (0 != memcmp (&iv, &ziv, sizeof (iv))))
    return 1;
  return 0;
}