source: rtems/bsps/shared/dev/getentropy/getentropy-cpucounter.c @ 9bf813c5

Last change on this file since 9bf813c5 was 9bf813c5, checked in by Joel Sherrill <joel@…>, on 03/01/22 at 21:38:43

bsps/shared/: Scripted embedded brains header file clean up

Updates #4625.

  • Property mode set to 100644
File size: 1.3 KB
Line 
1/*
2 * Copyright (c) 2017 embedded brains GmbH.  All rights reserved.
3 *
4 * The license and distribution terms for this file may be
5 * found in the file LICENSE in this distribution or at
6 * http://www.rtems.org/license/LICENSE.
7 */
8
9/*
10 * ATTENTION: THIS IS A VERY LIMITED ENTROPY SOURCE.
11 *
12 * This implementation uses a time-based value for it's entropy. The only thing
13 * that makes it random are interrupts from external sources. Don't use it if
14 * you need for example a strong crypto.
15 */
16
17#include <sys/param.h>
18#include <unistd.h>
19#include <string.h>
20#include <rtems/bsd.h>
21#include <rtems/counter.h>
22#include <rtems/sysinit.h>
23
24static uint32_t state;
25
26int getentropy(void *ptr, size_t n)
27{
28  uint8_t *dest = ptr;
29
30  state ^= rtems_counter_read();
31  state *= 25169206;
32  state += 1679610226;
33
34  while (n > 0) {
35    size_t m;
36
37    m = MIN(n, sizeof(state));
38    memcpy(dest, &state, m);
39    n -= m;
40    dest += m;
41    state *= 85236167;
42    state += 30557471;
43  }
44
45  return 0;
46}
47
48static void getentropy_init(void)
49{
50  struct bintime bt;
51
52  rtems_bsd_bintime(&bt);
53  state = (uint32_t) bt.frac;
54  state ^= (uint32_t) (bt.frac >> 32);
55  state ^= (uint32_t) bt.sec;
56  state ^= (uint32_t) (bt.sec >> 32);
57}
58
59RTEMS_SYSINIT_ITEM(
60  getentropy_init,
61  RTEMS_SYSINIT_DEVICE_DRIVERS,
62  RTEMS_SYSINIT_ORDER_LAST_BUT_5
63);
Note: See TracBrowser for help on using the repository browser.