Software APIs
prng.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
3 // All rights reserved.
4 // SPDX-License-Identifier: BSD-3-Clause
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions
8 // are met:
9 //
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //
13 // 2. Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 //
17 // 3. The names of its contributors may not be used to endorse or promote
18 // products derived from this software without specific prior written
19 // permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25 // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 #include <stddef.h>
34 #include <stdint.h>
35 
36 /**
37  * Mersenne Twister PRNG modified from:
38  * http://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/MT2002/CODES/mt19937ar.c
39  *
40  * The code below is identical to the code at the link above except for:
41  * - Unused functions were removed,
42  * - `static` specifier was added to all remaining functions,
43  * - Data types were replaced with compatible types for consistency,
44  * - `clang-format` was run, and
45  * - `genrand_int32()` was specified as `noinline` to improve capture
46  * performance.
47  */
48 
49 /* Period parameters */
50 #define N 624
51 #define M 397
52 #define MATRIX_A 0x9908b0dfUL /* constant vector a */
53 #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
54 #define LOWER_MASK 0x7fffffffUL /* least significant r bits */
55 
56 static uint32_t mt[N]; /* the array for the state vector */
57 static uint32_t mti = N + 1; /* mti==N+1 means mt[N] is not initialized */
58 
59 /* initializes mt[N] with a seed */
60 static void init_genrand(uint32_t s) {
61  mt[0] = s & 0xffffffffUL;
62  for (mti = 1; mti < N; mti++) {
63  mt[mti] = (1812433253UL * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
64  /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
65  /* In the previous versions, MSBs of the seed affect */
66  /* only MSBs of the array mt[]. */
67  /* 2002/01/09 modified by Makoto Matsumoto */
68  mt[mti] &= 0xffffffffUL;
69  /* for >32 bit machines */
70  }
71 }
72 
73 /* initialize by an array with array-length */
74 /* init_key is the array for initializing keys */
75 /* key_length is its length */
76 /* slight change for C++, 2004/2/26 */
77 static void init_by_array(uint32_t init_key[], uint32_t key_length) {
78  uint32_t i, j, k;
79  init_genrand(19650218UL);
80  i = 1;
81  j = 0;
82  k = (N > key_length ? N : key_length);
83  for (; k; k--) {
84  mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1664525UL)) +
85  init_key[j] + j; /* non linear */
86  mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
87  i++;
88  j++;
89  if (i >= N) {
90  mt[0] = mt[N - 1];
91  i = 1;
92  }
93  if (j >= key_length)
94  j = 0;
95  }
96  for (k = N - 1; k; k--) {
97  mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1566083941UL)) -
98  i; /* non linear */
99  mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
100  i++;
101  if (i >= N) {
102  mt[0] = mt[N - 1];
103  i = 1;
104  }
105  }
106 
107  mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
108 }
109 
110 /* generates a random number on [0,0xffffffff]-interval */
111 /* Note: Specified as noinline to improve capture performance. */
112 __attribute__((noinline)) static uint32_t genrand_int32(void) {
113  uint32_t y;
114  static uint32_t mag01[2] = {0x0UL, MATRIX_A};
115  /* mag01[x] = x * MATRIX_A for x=0,1 */
116 
117  if (mti >= N) { /* generate N words at one time */
118  int32_t kk;
119 
120  if (mti == N + 1) /* if init_genrand() has not been called, */
121  init_genrand(5489UL); /* a default initial seed is used */
122 
123  for (kk = 0; kk < N - M; kk++) {
124  y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
125  mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1UL];
126  }
127  for (; kk < N - 1; kk++) {
128  y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
129  mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
130  }
131  y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
132  mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1UL];
133 
134  mti = 0;
135  }
136 
137  y = mt[mti++];
138 
139  /* Tempering */
140  y ^= (y >> 11);
141  y ^= (y << 7) & 0x9d2c5680UL;
142  y ^= (y << 15) & 0xefc60000UL;
143  y ^= (y >> 18);
144 
145  return y;
146 }
147 
148 /**
149  * End of Mersenne Twister PRNG modified from:
150  * http://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/MT2002/CODES/mt19937ar.c
151  */
152 
153 /**
154  * TODO(alphan): Using MT for now as a proof of concept to minimize host-side
155  * changes. We should probably replace this with a PRNG from xoshiro* family
156  * for PRNGs, e.g. xoshiro128++, for better performance and less overhead.
157  */
158 
159 void prng_seed(uint32_t seed) { init_by_array(&seed, 1); }
160 
161 uint32_t prng_rand_uint32(void) { return genrand_int32(); }
162 
163 uint8_t prng_rand_byte(void) {
164  uint32_t rand = 0;
165  do {
166  /**
167  * Note: We shift right by 23 bits instead of 24 to match the behavior of
168  * `random.randint(0, 255)` in python. This, however, is obviously less
169  * efficient since it introduces a random delay for each byte.
170  *
171  * TODO(alphan): Use `random.randbytes()` on the host side instead of
172  * `ktp.next()` and update this function to match.
173  */
174  rand = genrand_int32() >> 23;
175  } while (rand > 255);
176  return (uint8_t)rand;
177 }
178 
179 void prng_rand_bytes(uint8_t *buffer, size_t buffer_len) {
180  for (size_t i = 0; i < buffer_len; ++i) {
181  buffer[i] = prng_rand_byte();
182  }
183 }