1 /*
2 * Copyright (c) 2019, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /**
30 * @file
31 * This file implements an entropy source based on /dev/urandom or pseudo-random generator.
32 *
33 */
34
35 #include "platform-simulation.h"
36
37 #include <assert.h>
38 #include <stdio.h>
39
40 #include <openthread/platform/entropy.h>
41
42 #include "utils/code_utils.h"
43
44 #ifndef __SANITIZE_ADDRESS__
45 #define __SANITIZE_ADDRESS__ 0
46 #endif
47
48 #if __SANITIZE_ADDRESS__ != 0
49
50 static uint32_t sState = 1;
51
52 #endif // __SANITIZE_ADDRESS__
53
platformRandomInit(void)54 void platformRandomInit(void)
55 {
56 #if __SANITIZE_ADDRESS__ != 0
57
58 // Multiplying gNodeId assures that no two nodes gets the same seed within an hour.
59 sState = (uint32_t)time(NULL) + (3600 * gNodeId);
60
61 #endif // __SANITIZE_ADDRESS__
62 }
63
64 #if __SANITIZE_ADDRESS__ != 0
65
randomUint32Get(void)66 static uint32_t randomUint32Get(void)
67 {
68 uint32_t mlcg, p, q;
69 uint64_t tmpstate;
70
71 tmpstate = (uint64_t)33614 * (uint64_t)sState;
72 q = tmpstate & 0xffffffff;
73 q = q >> 1;
74 p = tmpstate >> 32;
75 mlcg = p + q;
76
77 if (mlcg & 0x80000000)
78 {
79 mlcg &= 0x7fffffff;
80 mlcg++;
81 }
82
83 sState = mlcg;
84
85 return mlcg;
86 }
87
88 #endif // __SANITIZE_ADDRESS__
89
otPlatEntropyGet(uint8_t * aOutput,uint16_t aOutputLength)90 otError otPlatEntropyGet(uint8_t *aOutput, uint16_t aOutputLength)
91 {
92 otError error = OT_ERROR_NONE;
93
94 #if __SANITIZE_ADDRESS__ == 0
95
96 FILE *file = NULL;
97 size_t readLength;
98
99 otEXPECT_ACTION(aOutput && aOutputLength, error = OT_ERROR_INVALID_ARGS);
100
101 file = fopen("/dev/urandom", "rb");
102 otEXPECT_ACTION(file != NULL, error = OT_ERROR_FAILED);
103
104 readLength = fread(aOutput, 1, aOutputLength, file);
105 otEXPECT_ACTION(readLength == aOutputLength, error = OT_ERROR_FAILED);
106
107 exit:
108
109 if (file != NULL)
110 {
111 fclose(file);
112 }
113
114 #else // __SANITIZE_ADDRESS__
115
116 /*
117 * THE IMPLEMENTATION BELOW IS NOT COMPLIANT WITH THE THREAD SPECIFICATION.
118 *
119 * Address Sanitizer triggers test failures when reading random
120 * values from /dev/urandom. The pseudo-random number generator
121 * implementation below is only used to enable continuous
122 * integration checks with Address Sanitizer enabled.
123 */
124 otEXPECT_ACTION(aOutput && aOutputLength, error = OT_ERROR_INVALID_ARGS);
125
126 for (uint16_t length = 0; length < aOutputLength; length++)
127 {
128 aOutput[length] = (uint8_t)randomUint32Get();
129 }
130
131 exit:
132
133 #endif // __SANITIZE_ADDRESS__
134
135 return error;
136 }
137