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