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 #include "openthread-posix-config.h"
35 #include "platform-posix.h"
36
37 #include <assert.h>
38 #include <stdio.h>
39
40 #include <openthread/error.h>
41 #include <openthread/platform/entropy.h>
42
43 #include "common/code_utils.hpp"
44
45 #ifndef __SANITIZE_ADDRESS__
46 #define __SANITIZE_ADDRESS__ 0
47 #endif
48
49 #if __SANITIZE_ADDRESS__ != 0
50
51 static uint32_t sState = 1;
52
53 #endif // __SANITIZE_ADDRESS__
54
platformRandomInit(void)55 void platformRandomInit(void)
56 {
57 #if __SANITIZE_ADDRESS__ != 0
58
59 // Multiplying gNodeId assures that no two nodes gets the same seed within an hour.
60 sState = (uint32_t)time(nullptr) + (3600 * gNodeId);
61
62 #endif // __SANITIZE_ADDRESS__
63 }
64
65 #if __SANITIZE_ADDRESS__ != 0
66
randomUint32Get(void)67 uint32_t randomUint32Get(void)
68 {
69 uint32_t mlcg, p, q;
70 uint64_t tmpstate;
71
72 tmpstate = (uint64_t)33614 * (uint64_t)sState;
73 q = tmpstate & 0xffffffff;
74 q = q >> 1;
75 p = tmpstate >> 32;
76 mlcg = p + q;
77
78 if (mlcg & 0x80000000)
79 {
80 mlcg &= 0x7fffffff;
81 mlcg++;
82 }
83
84 sState = mlcg;
85
86 return mlcg;
87 }
88
89 #endif // __SANITIZE_ADDRESS__
90
otPlatEntropyGet(uint8_t * aOutput,uint16_t aOutputLength)91 otError otPlatEntropyGet(uint8_t *aOutput, uint16_t aOutputLength)
92 {
93 otError error = OT_ERROR_NONE;
94
95 #if __SANITIZE_ADDRESS__ == 0
96
97 FILE *file = nullptr;
98 size_t readLength;
99
100 VerifyOrExit(aOutput && aOutputLength, error = OT_ERROR_INVALID_ARGS);
101
102 file = fopen("/dev/urandom", "rb");
103 VerifyOrExit(file != nullptr, error = OT_ERROR_FAILED);
104
105 readLength = fread(aOutput, 1, aOutputLength, file);
106 VerifyOrExit(readLength == aOutputLength, error = OT_ERROR_FAILED);
107
108 exit:
109
110 if (file != nullptr)
111 {
112 fclose(file);
113 }
114
115 #else // __SANITIZE_ADDRESS__
116
117 /*
118 * THE IMPLEMENTATION BELOW IS NOT COMPLIANT WITH THE THREAD SPECIFICATION.
119 *
120 * Address Sanitizer triggers test failures when reading random
121 * values from /dev/urandom. The pseudo-random number generator
122 * implementation below is only used to enable continuous
123 * integration checks with Address Sanitizer enabled.
124 */
125 VerifyOrExit(aOutput && aOutputLength, error = OT_ERROR_INVALID_ARGS);
126
127 for (uint16_t length = 0; length < aOutputLength; length++)
128 {
129 aOutput[length] = (uint8_t)randomUint32Get();
130 }
131
132 exit:
133
134 #endif // __SANITIZE_ADDRESS__
135
136 return error;
137 }
138