if (i > 0)
GNUNET_CRYPTO_hash (&hc, sizeof (GNUNET_HashCode), &tmp);
- for (j = sizeof(GNUNET_HashCode) / sizeof(unsigned int); j > 0; j--)
+ fprintf (stderr, "H: %s\n", GNUNET_h2s (&tmp));
+
+ for (j=0;j<sizeof(GNUNET_HashCode) / sizeof(uint32_t); j++)
{
-#if HAVE_GCRY_MPI_LSHIFT
- gcry_mpi_lshift (n, n, sizeof(unsigned int));
+#if HAVE_GCRY_MPI_LSHIFT
+ gcry_mpi_lshift (n, n, sizeof(uint32_t)*8);
#else
- gcry_mpi_mul_ui(n, n, pow (2, sizeof(unsigned int)));
+ gcry_mpi_mul_ui(n, n, 1 << (sizeof(uint32_t)*4));
+ gcry_mpi_mul_ui(n, n, 1 << (sizeof(uint32_t)*4));
#endif
- gcry_mpi_add_ui(n, n, ((unsigned int *) &tmp)[j]);
+ gcry_mpi_add_ui(n, n, ntohl (((uint32_t *) &tmp)[j]));
}
hc = tmp;
}
return rc;
}
+/**
+ * If target != size, move target bytes to the
+ * end of the size-sized buffer and zero out the
+ * first target-size bytes.
+ */
+static void
+adjust (unsigned char *buf, size_t size, size_t target)
+{
+ if (size < target)
+ {
+ memmove (&buf[target - size], buf, size);
+ memset (buf, 0, target - size);
+ }
+}
+
+
static void
gen_prime (gcry_mpi_t *ptest, unsigned int nbits, GNUNET_HashCode * hc)
{
static int no_of_small_prime_numbers = DIM (small_prime_numbers) - 1;
gcry_mpi_t prime, pminus1, val_2, val_3, result;
- int i;
- unsigned x, step;
- unsigned int *mods;
+ unsigned int i;
+ unsigned int step;
+ unsigned int mods[no_of_small_prime_numbers];
gcry_mpi_t tmp;
gcry_mpi_t sp;
GNUNET_assert (nbits >= 16);
- mods = GNUNET_malloc (no_of_small_prime_numbers * sizeof (*mods));
/* Make nbits fit into mpz_t implementation. */
val_2 = gcry_mpi_set_ui (NULL, 2);
val_3 = gcry_mpi_set_ui (NULL, 3);
- prime = gcry_mpi_new(0);
+ prime = gcry_mpi_snew(0);
result = gcry_mpi_new(0);
pminus1 = gcry_mpi_new(0);
*ptest = gcry_mpi_new(0);
+ tmp = gcry_mpi_new (0);
+ sp = gcry_mpi_new (0);
while (1)
{
/* generate a random number */
gcry_mpi_set_bit (prime, 0);
/* Calculate all remainders. */
- tmp = gcry_mpi_new (0);
- sp = gcry_mpi_new (0);
- for (i = 0; (x = small_prime_numbers[i]); i++)
+ for (i = 0; i < no_of_small_prime_numbers; i++)
{
size_t written;
- gcry_mpi_set_ui(sp, x);
- gcry_mpi_div (NULL, tmp, prime, sp, -1 /* TODO CG: is this correct? */);
- gcry_mpi_print (GCRYMPI_FMT_USG, (unsigned char *) &mods[i], sizeof(*mods), &written, tmp);
- }
- gcry_mpi_release (sp);
- gcry_mpi_release (tmp);
+ gcry_mpi_set_ui(sp, small_prime_numbers[i]);
+ gcry_mpi_div (NULL, tmp, prime, sp, -1);
+ mods[i] = 0;
+ written = sizeof (unsigned int);
+ GNUNET_assert (0 ==
+ gcry_mpi_print (GCRYMPI_FMT_USG,
+ (unsigned char*) &mods[i], written, &written,
+ tmp));
+ adjust ( (unsigned char*) &mods[i], written, sizeof (unsigned int));
+ mods[i] = ntohl (mods[i]);
+ }
/* Now try some primes starting with prime. */
for (step = 0; step < 20000; step += 2)
{
/* Check against all the small primes we have in mods. */
- for (i = 0; (x = small_prime_numbers[i]); i++)
+ for (i = 0; i < no_of_small_prime_numbers; i++)
{
+ uint16_t x = small_prime_numbers[i];
while (mods[i] + step >= x)
mods[i] -= x;
if (!(mods[i] + step))
break;
}
- if (x)
+ if (i < no_of_small_prime_numbers)
continue; /* Found a multiple of an already known prime. */
gcry_mpi_add_ui (*ptest, prime, step);
if ((!gcry_mpi_cmp_ui (result, 1)) && (is_prime (*ptest, 5, hc)))
{
/* Got it. */
+ gcry_mpi_release (sp);
+ gcry_mpi_release (tmp);
gcry_mpi_release (val_2);
gcry_mpi_release (val_3);
gcry_mpi_release (result);
gcry_mpi_release (pminus1);
gcry_mpi_release (prime);
- GNUNET_free (mods);
return;
}
}
gcry_mpi_sub_ui (t2, sk->q, 1);
gcry_mpi_mul (phi, t1, t2);
gcry_mpi_gcd (g, t1, t2);
- gcry_mpi_div (f, NULL, phi, g, -1 /* TODO CG: is this correct? */);
-
+ gcry_mpi_div (f, NULL, phi, g, 0);
while (0 == gcry_mpi_gcd (t1, sk->e, phi))
{ /* (while gcd is not 1) */
gcry_mpi_add_ui (sk->e, sk->e, 2);
}
-typedef struct
+struct KBlockKeyCacheLine
{
GNUNET_HashCode hc;
struct KskRsaPrivateKeyBinaryEncoded *pke;
-} KBlockKeyCacheLine;
+};
-static KBlockKeyCacheLine **cache;
+static struct KBlockKeyCacheLine **cache;
static unsigned int cacheSize;
GNUNET_CRYPTO_rsa_key_create_from_hash (const GNUNET_HashCode * hc)
{
struct GNUNET_CRYPTO_RsaPrivateKey *ret;
- KBlockKeyCacheLine *line;
- int i;
+ struct KBlockKeyCacheLine *line;
+ unsigned int i;
for (i = 0; i < cacheSize; i++)
{
}
}
- line = GNUNET_malloc (sizeof (KBlockKeyCacheLine));
+ line = GNUNET_malloc (sizeof (struct KBlockKeyCacheLine));
line->hc = *hc;
line->pke = makeKblockKeyInternal (hc);
GNUNET_array_grow (cache, cacheSize, cacheSize + 1);
void __attribute__ ((destructor)) GNUNET_CRYPTO_ksk_fini ()
{
- int i;
+ unsigned int i;
for (i = 0; i < cacheSize; i++)
{