/*
+ * Copyright (C) 2012 Tobias Brunner
* Copyright (C) 2008 Martin Willi
* Hochschule fuer Technik Rapperswil
*
*
* @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.
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)
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,
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)
{
{
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,
}
}
-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;
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;
allocate_bytes(this, bytes, &chunk);
memcpy(buffer, chunk.ptr, bytes);
chunk_clear(&chunk);
+ return TRUE;
}
METHOD(rng_t, destroy, void,
};
-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;
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,
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;
}
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,