3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2020 Nest Labs, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * Provides implementations for the Chip entropy sourcing functions
22 * on the NXP K32W platforms.
24 /* this file behaves like a config.h, comes first */
25 #include <platform/internal/CHIPDeviceLayerInternal.h>
27 #include <support/crypto/CHIPRNG.h>
29 #include <mbedtls/entropy_poll.h>
30 #include <openthread/platform/entropy.h>
32 using namespace ::chip;
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
39 namespace DeviceLayer {
43 * Retrieve entropy from the underlying RNG source.
45 * This function is called by the CHIP DRBG to acquire entropy.
47 int GetEntropy_K32W(uint8_t * buf, size_t count)
51 VerifyOrDie(count <= UINT16_MAX);
53 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD
54 if (ThreadStackManagerImpl::IsInitialized())
56 ThreadStackMgr().LockThreadStack();
58 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
60 otError otErr = otPlatEntropyGet(buf, (uint16_t) count);
61 if (otErr != OT_ERROR_NONE)
63 res = CHIP_ERROR_DRBG_ENTROPY_SOURCE_FAILED;
66 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD
67 if (ThreadStackManagerImpl::IsInitialized())
69 ThreadStackMgr().UnlockThreadStack();
71 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
76 CHIP_ERROR InitEntropy()
80 // Initialize the CHIP DRBG.
81 err = Platform::Security::InitSecureRandomDataSource(GetEntropy_K32W, 64, NULL, 0);
84 // Seed the standard rand() pseudo-random generator with data from the secure random source.
87 err = Platform::Security::GetSecureRandomData((uint8_t *) &seed, sizeof(seed));
93 if (err != CHIP_NO_ERROR)
95 ChipLogError(Crypto, "InitEntropy() failed: 0x%08" PRIX32, err);
100 } // namespace Internal
101 } // namespace DeviceLayer