Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / K32W / Entropy.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2020 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 NXP K32W 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 #include <openthread/platform/entropy.h>
31
32 using namespace ::chip;
33
34 #if !CHIP_CONFIG_RNG_IMPLEMENTATION_CHIPDRBG
35 #error "CHIP DRBG implementation must be enabled on K32W platforms"
36 #endif // !CHIP_CONFIG_RNG_IMPLEMENTATION_CHIPDRBG
37
38 namespace chip {
39 namespace DeviceLayer {
40 namespace Internal {
41
42 /**
43  * Retrieve entropy from the underlying RNG source.
44  *
45  * This function is called by the CHIP DRBG to acquire entropy.
46  */
47 int GetEntropy_K32W(uint8_t * buf, size_t count)
48 {
49     int res = 0;
50
51     VerifyOrDie(count <= UINT16_MAX);
52
53 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD
54     if (ThreadStackManagerImpl::IsInitialized())
55     {
56         ThreadStackMgr().LockThreadStack();
57     }
58 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
59
60     otError otErr = otPlatEntropyGet(buf, (uint16_t) count);
61     if (otErr != OT_ERROR_NONE)
62     {
63         res = CHIP_ERROR_DRBG_ENTROPY_SOURCE_FAILED;
64     }
65
66 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD
67     if (ThreadStackManagerImpl::IsInitialized())
68     {
69         ThreadStackMgr().UnlockThreadStack();
70     }
71 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
72
73     return res;
74 }
75
76 CHIP_ERROR InitEntropy()
77 {
78     CHIP_ERROR err;
79
80     // Initialize the CHIP DRBG.
81     err = Platform::Security::InitSecureRandomDataSource(GetEntropy_K32W, 64, NULL, 0);
82     SuccessOrExit(err);
83
84     // Seed the standard rand() pseudo-random generator with data from the secure random source.
85     {
86         unsigned int seed;
87         err = Platform::Security::GetSecureRandomData((uint8_t *) &seed, sizeof(seed));
88         SuccessOrExit(err);
89         srand(seed);
90     }
91
92 exit:
93     if (err != CHIP_NO_ERROR)
94     {
95         ChipLogError(Crypto, "InitEntropy() failed: 0x%08" PRIX32, err);
96     }
97     return err;
98 }
99
100 } // namespace Internal
101 } // namespace DeviceLayer
102 } // namespace chip