Blame view

RIOT/cpu/native/periph/hwrng.c 3.77 KB
fb11e647   vrobic   reseau statique a...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  /*
   * Copyright (C) 2014 Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>
   *               2016 Freie Universität Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     native_cpu
   * @defgroup    native_rng
   * @{
   *
   * @file
   * @brief       HWRNG interface implementation
   *
   * @author      Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>
   */
  
  #include <string.h>
  #include <stdlib.h>
  #include <err.h>
  #include <sys/stat.h>
  #include <fcntl.h>
  
  #include "cpu_conf.h"
  #include "native_internal.h"
  
  #include "periph/hwrng.h"
  
  #define ENABLE_DEBUG (0)
  #include "debug.h"
  
  static int initialized = 0;
  static int dev_random = -1;
  
  /**********************************************************************
   * internal API declaration
   **********************************************************************/
  
  /**
   * seed host random module with @ref _native_rng_seed
   */
  void _native_rng_init_det(void);
  void _native_rng_init_hq(void);
  unsigned _native_rng_read_det(uint8_t *buf, unsigned num);
  unsigned _native_rng_read_hq(uint8_t *buf, unsigned num);
  
  /**********************************************************************
   * public API implementation
   **********************************************************************/
  
  void hwrng_init(void)
  {
      DEBUG("hwrng_init: initializing\n");
      switch (_native_rng_mode) {
          case 0:
              _native_rng_init_hq();
              break;
          case 1:
              _native_rng_init_det();
              break;
          default:
              err(EXIT_FAILURE, "hwrng_init: _native_rng_mode is in invalid state %i\n",
                     _native_rng_mode);
              break;
      }
  
      initialized = 1;
  }
  
  void hwrng_read(uint8_t *buf, unsigned int num)
  {
      if (!initialized) {
          warnx("hwrng_read: random device not initialized, failing\n");
          return;
      }
  
      DEBUG("hwrng_read: writing %u bytes\n", num);
      switch (_native_rng_mode) {
          case 0:
              _native_rng_read_hq(buf, num);
              break;
          case 1:
              _native_rng_read_det(buf, num);
              break;
          default:
              err(EXIT_FAILURE, "hwrng_read: _native_rng_mode is in invalid state %i\n",
                     _native_rng_mode);
              break;
      }
  }
  
  /**********************************************************************
   * internal API implementation
   **********************************************************************/
  
  void _native_rng_init_det(void)
  {
      DEBUG("_native_rng_init_det\n");
      _native_syscall_enter();
      real_srandom(_native_rng_seed);
      _native_syscall_leave();
  }
  
  void _native_rng_init_hq(void)
  {
      DEBUG("_native_rng_init_hq\n");
      _native_syscall_enter();
      dev_random = real_open("/dev/urandom", O_RDONLY | O_CLOEXEC);
      if (dev_random == -1) {
          dev_random = real_open("/dev/random", O_RDONLY | O_CLOEXEC);
          if (dev_random == -1) {
              err(EXIT_FAILURE, "_native_rng_init_hq: open(/dev/urandom|/dev/random)");
          }
      }
      _native_syscall_leave();
  }
  
  unsigned _native_rng_read_det(uint8_t *buf, unsigned num)
  {
      DEBUG("_native_rng_read_det\n");
      for (unsigned i = 0; i < num; i++) {
          _native_syscall_enter();
          buf[i] = (uint8_t)real_random();
          _native_syscall_leave();
      }
  
      return num;
  }
  
  unsigned _native_rng_read_hq(uint8_t *buf, unsigned num)
  {
      DEBUG("_native_rng_read_hq\n");
      unsigned offset = 0;
  
      while (num > 0) {
          _native_syscall_enter();
          int r = real_read(dev_random, (buf + offset), num);
          _native_syscall_leave();
  
          if (r == -1) {
              err(EXIT_FAILURE, "_native_rng_read_hq: read");
          }
  
          num -= r;
          offset += r;
      }
  
      return offset;
  }
  
  /**
   * @}
   */