Commit 3f8cfb28 authored by Niels Möller's avatar Niels Möller
Browse files

Moved randomness functions to a separate file.

Rev: src/crypto.c:1.3
Rev: src/crypto.h:1.4
parent 66b1559a
......@@ -106,7 +106,7 @@ static struct hash_instance *make_sha_instance(struct hash_algorithm *ignored)
}
struct hash_algorithm sha_algorithm =
{ 64, 20, make_sha_instance };
{ SHA_DATASIZE, SHA_DIGESTSIZE, make_sha_instance };
/* HMAC */
......@@ -216,59 +216,3 @@ struct mac_algorithm *make_hmac_algorithm(struct hash_algorithm *h)
return &self->super;
}
/* Random */
struct poor_random
{
struct randomness super;
struct hash_instance *hash;
UINT32 pos;
UINT8 buffer[1];
};
static void do_poor_random(struct randomness **r, UINT32 length, UINT8 *dst)
{
struct poor_random *self = (struct poor_random *) *r;
while(length)
{
UINT32 available = self->hash->hash_size - self->pos;
UINT32 to_copy;
if (!available)
{
time_t now = time(NULL); /* To avoid cycles */
HASH_UPDATE(self->hash, sizeof(now), (UINT8 *) &now);
HASH_UPDATE(self->hash, self->hash->hash_size,
self->buffer);
HASH_DIGEST(self->hash, self->buffer);
available = self->hash->hash_size;
self->pos = 0;
}
to_copy = MIN(available, length);
memcpy(dst, self->buffer + self->pos, to_copy);
length -= to_copy;
dst += to_copy;
self->pos += to_copy;
}
}
struct randomness *make_poor_random(struct hash_algorithm *hash,
struct lsh_string *init)
{
struct poor_random *self
= xalloc(sizeof(struct poor_random) - 1 + hash->hash_size);
time_t now = time(NULL); /* To avoid cycles */
self->super.random = do_poor_random;
self->hash = MAKE_HASH(hash);
HASH_UPDATE(self->hash, sizeof(now), (UINT8 *) &now);
HASH_UPDATE(self->hash, init->length, init->data);
HASH_DIGEST(self->hash, self->buffer);
self->pos = 0;
return &self->super;
}
......@@ -17,4 +17,21 @@ struct mac_algorithm *make_hmac_algorithm(struct hash_algorithm *h);
struct randomness *make_poor_random(struct hash_algorithm *hash,
struct lsh_string *init);
/* Combining block cryptos */
/* Example: To create a tripple DES cbc encryptor:
*
* struct crypto_instance des3_cbc
* = make_cbc(make_crypto_cascade(3, MAKE_ENCRYPT(&des_algoritm, k1),
* MAKE_DECRYPT(&des_algoritm, k2),
* MAKE_ENCRYPT(&des_algoritm, k3)),
* CRYPTO_ENCRYPT, iv);
*/
struct crypto_instance *
make_cbc(struct crypto_instance *c, int mode, UINT32 iv);
struct crypto_instance *
make_crypto_cascade(unsigned n, ...);
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment