[PATCH v2 2/3] lib: random: get_random_bytes from HWRNG if present

Oleksij Rempel linux at rempel-privat.de
Fri Mar 17 07:30:43 PDT 2017


Am 17.03.2017 um 13:30 schrieb Sascha Hauer:
> On Thu, Mar 16, 2017 at 06:31:45AM +0100, Oleksij Rempel wrote:
>> From: Steffen Trumtrar <s.trumtrar at pengutronix.de>
>>
>> Instead of generating pseudo random numbers, get random bytes
>> from an optional HW generator, if enabled and registered.
>>
>> Signed-off-by: Steffen Trumtrar <s.trumtrar at pengutronix.de>
>> Signed-off-by: Oleksij Rempel <o.rempel at pengutronix.de>
>> ---
>>  lib/random.c | 19 +++++++++++++++++++
>>  1 file changed, 19 insertions(+)
>>
>> diff --git a/lib/random.c b/lib/random.c
>> index 210fea994..42c0f83b7 100644
>> --- a/lib/random.c
>> +++ b/lib/random.c
>> @@ -1,5 +1,6 @@
>>  #include <common.h>
>>  #include <stdlib.h>
>> +#include <linux/hw_random.h>
>>  
>>  static unsigned int random_seed;
>>  
>> @@ -22,6 +23,24 @@ void get_random_bytes(void *_buf, int len)
>>  {
>>  	char *buf = _buf;
>>  
>> +	if (IS_ENABLED(CONFIG_HWRNG)) {
>> +		struct hwrng *rng;
>> +		int bytes;
>> +
>> +		rng = hwrng_get_first();
>> +		if (!IS_ERR(rng)) {
>> +			while (len) {
>> +				bytes = hwrng_get_data(rng, _buf, len, 1);
>> +				if (!bytes)
>> +					goto sw_fallback;
> 
> hwrng_get_data() looks like it could return a negative error code.

hm.. good point.


-- 
Regards,
Oleksij

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 213 bytes
Desc: OpenPGP digital signature
URL: <http://lists.infradead.org/pipermail/barebox/attachments/20170317/38ad81d3/attachment.sig>


More information about the barebox mailing list