RNGs' get_bytes and allocate_bytes return boolean

This commit is contained in:
Tobias Brunner
2012-07-16 14:53:34 +02:00
committed by Martin Willi
parent 605985d122
commit 39e807728e
6 changed files with 39 additions and 21 deletions
+7 -2
View File
@@ -1,4 +1,5 @@
/*
* Copyright (C) 2012 Tobias Brunner
* Copyright (C) 2008 Martin Willi
* Hochschule fuer Technik Rapperswil
*
@@ -53,16 +54,20 @@ struct rng_t {
*
* @param len number of bytes to get
* @param buffer pointer where the generated bytes will be written
* @return TRUE if bytes successfully written
*/
void (*get_bytes) (rng_t *this, size_t len, u_int8_t *buffer);
__attribute__((warn_unused_result))
bool (*get_bytes) (rng_t *this, size_t len, u_int8_t *buffer);
/**
* Generates random bytes and allocate space for them.
*
* @param len number of bytes to get
* @param chunk chunk which will hold generated bytes
* @return TRUE if allocation succeeded
*/
void (*allocate_bytes) (rng_t *this, size_t len, chunk_t *chunk);
__attribute__((warn_unused_result))
bool (*allocate_bytes) (rng_t *this, size_t len, chunk_t *chunk);
/**
* Destroys a rng object.
@@ -35,7 +35,7 @@ struct private_gcrypt_rng_t {
rng_quality_t quality;
};
METHOD(rng_t, get_bytes, void,
METHOD(rng_t, get_bytes, bool,
private_gcrypt_rng_t *this, size_t bytes, u_int8_t *buffer)
{
switch (this->quality)
@@ -50,13 +50,15 @@ METHOD(rng_t, get_bytes, void,
gcry_randomize(buffer, bytes, GCRY_VERY_STRONG_RANDOM);
break;
}
return TRUE;
}
METHOD(rng_t, allocate_bytes, void,
METHOD(rng_t, allocate_bytes, bool,
private_gcrypt_rng_t *this, size_t bytes, chunk_t *chunk)
{
*chunk = chunk_alloc(bytes);
get_bytes(this, chunk->len, chunk->ptr);
return TRUE;
}
METHOD(rng_t, destroy, void,
@@ -44,10 +44,10 @@ struct private_openssl_rng_t {
rng_quality_t quality;
};
METHOD(rng_t, get_bytes, void,
METHOD(rng_t, get_bytes, bool,
private_openssl_rng_t *this, size_t bytes, u_int8_t *buffer)
{
u_int32_t ret=0;
u_int32_t ret;
if (this->quality == RNG_STRONG)
{
@@ -57,18 +57,19 @@ METHOD(rng_t, get_bytes, void,
{
ret = RAND_pseudo_bytes((char*)buffer, bytes);
}
if (ret == 0)
{
DBG1(DBG_LIB, "getting randomness from openssl failed.");
}
return ret != 0;
}
METHOD(rng_t, allocate_bytes, void,
METHOD(rng_t, allocate_bytes, bool,
private_openssl_rng_t *this, size_t bytes, chunk_t *chunk)
{
*chunk = chunk_alloc(bytes);
get_bytes(this, chunk->len, chunk->ptr);
if (!get_bytes(this, chunk->len, chunk->ptr))
{
chunk_free(chunk);
return FALSE;
}
return TRUE;
}
METHOD(rng_t, destroy, void,
@@ -69,7 +69,7 @@ static void rng(char *buf, int len, int quality)
}
}
METHOD(rng_t, allocate_bytes, void,
METHOD(rng_t, allocate_bytes, bool,
private_padlock_rng_t *this, size_t bytes, chunk_t *chunk)
{
chunk->len = bytes;
@@ -77,9 +77,10 @@ METHOD(rng_t, allocate_bytes, void,
chunk->ptr = malloc(bytes + 7);
rng(chunk->ptr, chunk->len, this->quality);
return TRUE;
}
METHOD(rng_t, get_bytes, void,
METHOD(rng_t, get_bytes, bool,
private_padlock_rng_t *this, size_t bytes, u_int8_t *buffer)
{
chunk_t chunk;
@@ -88,6 +89,7 @@ METHOD(rng_t, get_bytes, void,
allocate_bytes(this, bytes, &chunk);
memcpy(buffer, chunk.ptr, bytes);
chunk_clear(&chunk);
return TRUE;
}
METHOD(rng_t, destroy, void,
+10 -4
View File
@@ -43,7 +43,7 @@ struct private_pkcs11_rng_t {
};
METHOD(rng_t, get_bytes, void,
METHOD(rng_t, get_bytes, bool,
private_pkcs11_rng_t *this, size_t bytes, u_int8_t *buffer)
{
CK_RV rv;
@@ -51,15 +51,21 @@ METHOD(rng_t, get_bytes, void,
if (rv != CKR_OK)
{
DBG1(DBG_CFG, "C_GenerateRandom() failed: %N", ck_rv_names, rv);
abort();
return FALSE;
}
return TRUE;
}
METHOD(rng_t, allocate_bytes, void,
METHOD(rng_t, allocate_bytes, bool,
private_pkcs11_rng_t *this, size_t bytes, chunk_t *chunk)
{
*chunk = chunk_alloc(bytes);
get_bytes(this, chunk->len, chunk->ptr);
if (!get_bytes(this, chunk->len, chunk->ptr))
{
chunk_clear(chunk);
return FALSE;
}
return TRUE;
}
METHOD(rng_t, destroy, void,
@@ -40,7 +40,7 @@ struct private_random_rng_t {
int fd;
};
METHOD(rng_t, get_bytes, void,
METHOD(rng_t, get_bytes, bool,
private_random_rng_t *this, size_t bytes, u_int8_t *buffer)
{
size_t done;
@@ -59,13 +59,15 @@ METHOD(rng_t, get_bytes, void,
}
done += got;
}
return TRUE;
}
METHOD(rng_t, allocate_bytes, void,
METHOD(rng_t, allocate_bytes, bool,
private_random_rng_t *this, size_t bytes, chunk_t *chunk)
{
*chunk = chunk_alloc(bytes);
get_bytes(this, chunk->len, chunk->ptr);
return TRUE;
}
METHOD(rng_t, destroy, void,