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