source: rtems/c/src/lib/libbsp/shared/getentropy-cpucounter.c @ ca4895c

5
Last change on this file since ca4895c was ca4895c, checked in by Christian Mauderer <Christian.Mauderer@…>, on 11/13/17 at 08:21:29

getentropy: Add cpu counter based implementation.

Update #3239.

  • Property mode set to 100644
File size: 1.2 KB
Line 
1/*
2 * Copyright (c) 2017 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15/*
16 * ATTENTION: THIS IS A VERY LIMITED ENTROPY SOURCE.
17 *
18 * This implementation uses a time-based value for it's entropy. The only thing
19 * that makes it random are interrupts from external sources. Don't use it if
20 * you need for example a strong crypto.
21 */
22
23#include <unistd.h>
24#include <string.h>
25#include <rtems/sysinit.h>
26#include <rtems/counter.h>
27
28int getentropy(void *ptr, size_t n)
29{
30        uint8_t *dest = ptr;
31
32        while (n > 0) {
33                rtems_counter_ticks ticks;
34
35                ticks = rtems_counter_read();
36
37                if (n >= sizeof(ticks)) {
38                        memcpy(dest, &ticks, sizeof(ticks));
39                        n -= sizeof(ticks);
40                        dest += sizeof(ticks);
41                } else {
42                        /*
43                         * Fill the remaining bytes with only the least
44                         * significant byte of the time. That is the byte with
45                         * the most changes.
46                         */
47                        *dest = ticks & 0xFF;
48                        --n;
49                        ++dest;
50                }
51        }
52
53        return 0;
54}
Note: See TracBrowser for help on using the repository browser.