[PATCH v6 3/8] lib: random: add get_crypto_bytes interface and use HWRNG if posssible

Sascha Hauer s.hauer at pengutronix.de
Wed Mar 22 00:33:37 PDT 2017


On Tue, Mar 21, 2017 at 03:00:20PM +0100, Oleksij Rempel wrote:
> For crypto applications we need to use some thing else as PRNG.
> So provide get_crypto_bytes() and use HWRNG as main source.
> PRNG is allowed as fallback if user decided to configure it so.
> 
>  static unsigned int random_seed;
>  
> @@ -18,6 +19,11 @@ void srand(unsigned int seed)
>  	random_seed = seed;
>  }
>  
> +/**
> + * get_random_bytes - get pseudo random numbers.
> + * This interface can be good enough to generate MAC address
> + * or use for NAND test.
> + */
>  void get_random_bytes(void *_buf, int len)
>  {
>  	char *buf = _buf;
> @@ -25,3 +31,53 @@ void get_random_bytes(void *_buf, int len)
>  	while (len--)
>  		*buf++ = rand() % 256;
>  }
> +
> +/**
> + * get_crypto_bytes - get random numbers suitable for cryptographic needs.
> + */
> +static int _get_crypto_bytes(void *_buf, int len)
> +{
> +	struct hwrng *rng;
> +	u8 *buf = _buf;

This extra u8 buffer pointer seems unnecessary. Why not use the input
void * throughout the function?

> +
> +	if (!IS_ENABLED(CONFIG_HWRNG))
> +		return -ENOENT;
> +
> +	rng = hwrng_get_first();

Instead of having to test for CONFIG_HWRNG in the users
hwrng_get_first() could be a static inline function when CONFIG_HWRNG is
disabled.

Sascha


-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



More information about the barebox mailing list