[PATCH 2/4] getentropy: Add cpu counter based implementation.

Gedare Bloom gedare at rtems.org
Fri Nov 10 15:55:33 UTC 2017


> diff --git a/c/src/lib/libbsp/shared/getentropy-cpucounter-based.c b/c/src/lib/libbsp/shared/getentropy-cpucounter-based.c
> new file mode 100644
> index 0000000000..137fdfbb6c
> --- /dev/null
> +++ b/c/src/lib/libbsp/shared/getentropy-cpucounter-based.c
> @@ -0,0 +1,46 @@
> +/*
> + * Copyright (c) 2017 embedded brains GmbH.  All rights reserved.
> + *
> + *  embedded brains GmbH
> + *  Dornierstr. 4
> + *  82178 Puchheim
> + *  Germany
> + *  <rtems at embedded-brains.de>
> + *
> + * The license and distribution terms for this file may be
> + * found in the file LICENSE in this distribution or at
> + * http://www.rtems.org/license/LICENSE.
> + */
> +
> +#include <unistd.h>
> +#include <string.h>
> +#include <rtems/sysinit.h>
> +#include <rtems/counter.h>
> +
> +int getentropy(void *ptr, size_t n)
> +{
> +       uint8_t *dest = ptr;
> +
> +       while (n > 0) {
> +               rtems_counter_ticks ticks;
> +
> +               ticks = rtems_counter_read();
> +
> +               if (n >= sizeof(ticks)) {
> +                       memcpy(dest, &ticks, sizeof(ticks));
> +                       n -= sizeof(ticks);
> +                       dest += sizeof(ticks);
> +               } else {
> +                       /*
> +                        * Fill the remaining bytes with only the least
> +                        * significant byte of the time. That is the byte with
> +                        * the most changes.
> +                        */
> +                       *dest = ticks & 0xFF;
> +                       --n;
> +                       ++dest;
> +               }
> +       }
> +
> +       return 0;
> +}

I don't believe this is a good source of entropy--the passage of time
is not random. Should this be documented as such?


More information about the devel mailing list