Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / EFR32 / Entropy.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2019 Nest Labs, Inc.
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18
19 /**
20  *    @file
21  *          Provides implementations for the Chip entropy sourcing functions
22  *          on the Silcon Labs EFR32 platforms.
23  */
24 /* this file behaves like a config.h, comes first */
25 #include <platform/internal/CHIPDeviceLayerInternal.h>
26
27 #include <support/crypto/CHIPRNG.h>
28
29 #include <mbedtls/entropy_poll.h>
30
31 #include <em_device.h>
32
33 #if defined(_SILICON_LABS_32B_SERIES_1)
34 #include <openthread/platform/entropy.h>
35 #elif defined(_SILICON_LABS_32B_SERIES_2)
36 extern "C" int mbedtls_hardware_poll(void * data, unsigned char * output, size_t len, size_t * olen);
37 #else // !defined(_SILICON_LABS_32B_SERIES_1) && !defined(_SILICON_LABS_32B_SERIES_2)
38 #error "Unsupported EFR32 series"
39 #endif
40
41 using namespace ::chip;
42
43 #if !CHIP_CONFIG_RNG_IMPLEMENTATION_CHIPDRBG
44 #error "CHIP DRBG implementation must be enabled on EFR32 platforms"
45 #endif // !CHIP_CONFIG_RNG_IMPLEMENTATION_CHIPDRBG
46
47 namespace chip {
48 namespace DeviceLayer {
49 namespace Internal {
50
51 /**
52  * Retrieve entropy from the underlying RNG source.
53  *
54  * This function is called by the CHIP DRBG to acquire entropy.
55  */
56 int GetEntropy_EFR32(uint8_t * buf, size_t count)
57 {
58     int res = 0;
59
60     VerifyOrDie(count <= UINT16_MAX);
61
62 #if defined(_SILICON_LABS_32B_SERIES_1)
63 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD
64     if (ThreadStackManagerImpl::IsInitialized())
65     {
66         ThreadStackMgr().LockThreadStack();
67     }
68 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
69
70 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD
71     if (ThreadStackManagerImpl::IsInitialized())
72     {
73         ThreadStackMgr().LockThreadStack();
74     }
75 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
76
77     otError otErr = otPlatEntropyGet(buf, (uint16_t) count);
78     if (otErr != OT_ERROR_NONE)
79     {
80         res = CHIP_ERROR_DRBG_ENTROPY_SOURCE_FAILED;
81     }
82
83 #elif defined(_SILICON_LABS_32B_SERIES_2)
84     size_t entropy_len = 0;
85     size_t olen        = 0;
86
87     while (entropy_len < count)
88     {
89         res = mbedtls_hardware_poll(NULL, buf + entropy_len, count - entropy_len, &olen);
90         if (res != 0)
91         {
92             res = CHIP_ERROR_DRBG_ENTROPY_SOURCE_FAILED;
93             break;
94         }
95
96         entropy_len += olen;
97     }
98 #else // !defined(_SILICON_LABS_32B_SERIES_1) && !defined(_SILICON_LABS_32B_SERIES_2)
99 #error "Unsupported EFR32 series"
100 #endif
101
102     return res;
103 }
104
105 CHIP_ERROR InitEntropy()
106 {
107     CHIP_ERROR err;
108
109     // Initialize the CHIP DRBG.
110     err = Platform::Security::InitSecureRandomDataSource(GetEntropy_EFR32, 64, NULL, 0);
111     SuccessOrExit(err);
112
113     // Seed the standard rand() pseudo-random generator with data from the secure random source.
114     {
115         unsigned int seed;
116         err = Platform::Security::GetSecureRandomData((uint8_t *) &seed, sizeof(seed));
117         SuccessOrExit(err);
118         srand(seed);
119     }
120
121 exit:
122     if (err != CHIP_NO_ERROR)
123     {
124         ChipLogError(Crypto, "InitEntropy() failed: 0x%08" PRIX32, err);
125     }
126     return err;
127 }
128
129 } // namespace Internal
130 } // namespace DeviceLayer
131 } // namespace chip