|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2024 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 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 | +#include "AppFactoryData.h" |
| 20 | + |
| 21 | +#include <credentials/DeviceAttestationCredsProvider.h> |
| 22 | +#include <platform/CommissionableDataProvider.h> |
| 23 | +#include <platform/DeviceInstanceInfoProvider.h> |
| 24 | + |
| 25 | +#if CONFIG_CHIP_PLAT_LOAD_REAL_FACTORY_DATA |
| 26 | +#include "FactoryDataProvider.h" |
| 27 | +/* |
| 28 | + * Test key used to encrypt factory data before storing it to the flash. |
| 29 | + * The software key should be used only during development stage. |
| 30 | + * For production usage, it is recommended to use the OTP key which needs to be fused in the RT1060 SW_GP2. |
| 31 | + */ |
| 32 | +static const uint8_t aes128TestKey[] |
| 33 | + __attribute__((aligned)) = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }; |
| 34 | +#else |
| 35 | +#include <credentials/examples/DeviceAttestationCredsExample.h> |
| 36 | +#endif |
| 37 | + |
| 38 | +#if CONFIG_CHIP_FACTORY_DATA |
| 39 | +#include <platform/nxp/common/factory_data/FactoryDataProvider.h> |
| 40 | +#else |
| 41 | +#include <platform/nxp/zephyr/DeviceInstanceInfoProviderImpl.h> |
| 42 | +#endif |
| 43 | + |
| 44 | +#if CONFIG_CHIP_FACTORY_DATA && CONFIG_CHIP_ENCRYPTED_FACTORY_DATA |
| 45 | +#ifdef CONFIG_CHIP_ENCRYPTED_FACTORY_DATA_AES128_KEY |
| 46 | + |
| 47 | +#define KEY CONFIG_CHIP_ENCRYPTED_FACTORY_DATA_AES128_KEY |
| 48 | +#define HEXTONIBBLE(c) (*(c) >= 'A' ? (*(c) - 'A') + 10 : (*(c) - '0')) |
| 49 | +#define HEXTOBYTE(c) (HEXTONIBBLE(c) * 16 + HEXTONIBBLE(c + 1)) |
| 50 | +#define AES128_KEY_ARRAY \ |
| 51 | + HEXTOBYTE(KEY + 0), HEXTOBYTE(KEY + 2), HEXTOBYTE(KEY + 4), HEXTOBYTE(KEY + 6), HEXTOBYTE(KEY + 8), HEXTOBYTE(KEY + 10), \ |
| 52 | + HEXTOBYTE(KEY + 12), HEXTOBYTE(KEY + 14), HEXTOBYTE(KEY + 16), HEXTOBYTE(KEY + 18), HEXTOBYTE(KEY + 20), \ |
| 53 | + HEXTOBYTE(KEY + 22), HEXTOBYTE(KEY + 24), HEXTOBYTE(KEY + 26), HEXTOBYTE(KEY + 28), HEXTOBYTE(KEY + 30) |
| 54 | +#else |
| 55 | +#define AES128_KEY_ARRAY 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c |
| 56 | +#endif /* CONFIG_CHIP_ENCRYPTED_FACTORY_DATA_AES128_KEY */ |
| 57 | + |
| 58 | +/* |
| 59 | + * Test key used to encrypt factory data before storing it to the flash. |
| 60 | + */ |
| 61 | +static const uint8_t aes128TestKey[] __attribute__((aligned)) = { AES128_KEY_ARRAY }; |
| 62 | + |
| 63 | +#endif /* CONFIG_CHIP_FACTORY_DATA && CONFIG_CHIP_ENCRYPTED_FACTORY_DATA */ |
| 64 | + |
| 65 | +using namespace chip; |
| 66 | +using namespace ::chip::Credentials; |
| 67 | +using namespace ::chip::DeviceLayer; |
| 68 | + |
| 69 | +/** |
| 70 | + * Allows to register Matter factory data before initializing the Matter stack |
| 71 | + * Load factory data from the flash to the RAM. |
| 72 | + * Needs to be done before starting other Matter modules to avoid concurrent access issues with DCP hardware module. |
| 73 | + * |
| 74 | + * This example demonstrates the usage of the ecb with a software key, to use other encryption mode, |
| 75 | + * or to use hardware keys, check available methodes from the FactoryDataProviderImpl class. |
| 76 | + */ |
| 77 | +CHIP_ERROR NXP::App::AppFactoryData_PreMatterStackInit(void) |
| 78 | +{ |
| 79 | + return CHIP_NO_ERROR; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Allows to register Matter factory data after initializing the Matter stack |
| 84 | + */ |
| 85 | +CHIP_ERROR NXP::App::AppFactoryData_PostMatterStackInit(void) |
| 86 | +{ |
| 87 | +#if CONFIG_CHIP_FACTORY_DATA |
| 88 | +#if CONFIG_CHIP_ENCRYPTED_FACTORY_DATA |
| 89 | + FactoryDataPrvdImpl().SetEncryptionMode(FactoryDataProvider::encrypt_ecb); |
| 90 | + FactoryDataPrvdImpl().SetAes128Key(&aes128TestKey[0]); |
| 91 | +#endif /* CONFIG_CHIP_ENCRYPTED_FACTORY_DATA */ |
| 92 | + ReturnErrorOnFailure(FactoryDataPrvdImpl().Init()); |
| 93 | + SetDeviceInstanceInfoProvider(&FactoryDataPrvd()); |
| 94 | + SetDeviceAttestationCredentialsProvider(&FactoryDataPrvd()); |
| 95 | + SetCommissionableDataProvider(&FactoryDataPrvd()); |
| 96 | +#else |
| 97 | + SetDeviceInstanceInfoProvider(&DeviceInstanceInfoProviderMgrImpl()); |
| 98 | + SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); |
| 99 | +#endif /* CONFIG_CHIP_FACTORY_DATA */ |
| 100 | + return CHIP_NO_ERROR; |
| 101 | +} |
0 commit comments