Skip to content

Commit 5cffa52

Browse files
TE-N-JiLuo“nxf90552”
authored and
“nxf90552”
committed
Add device attestation based on ELE
Support device attestation based on EdgeLock Enclave(ELE). Attestation certifications and keys should be provisioned into device in advance. It's currently only enabled for i.MX 93 platform. Change-Id: I39c79efa17a99266113e2bf28204d0c23b81af1d Signed-off-by: Ji Luo <[email protected]> Reviewed-on: http://androidsource.nxp.com/project/23345 Reviewed-by: Elven Wang <[email protected]>
1 parent 3e99dc7 commit 5cffa52

7 files changed

+273
-10
lines changed

examples/platform/linux/AppMain.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
*
33
* Copyright (c) 2021-2022 Project CHIP Authors
4+
* Copyright 2023, 2025 NXP
45
* All rights reserved.
56
*
67
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -127,8 +128,11 @@
127128
#endif // CHIP_DEVICE_LAYER_TARGET_LINUX
128129

129130
#if CHIP_ATTESTATION_TRUSTY_OS
130-
#include "DeviceAttestationCreds.h"
131+
#include "DeviceAttestationCredsTrusty.h"
131132
using namespace chip::Credentials::Trusty;
133+
#elif CHIP_ATTESTATION_ELE
134+
#include "DeviceAttestationCredsEle.h"
135+
using namespace chip::Credentials::ele;
132136
#endif
133137

134138
#if CHIP_OP_KEYSTORE_TRUSTY_OS
@@ -722,6 +726,8 @@ void ChipLinuxAppMainLoop(AppMainLoopImplementation * impl)
722726
// Initialize device attestation config
723727
#if CHIP_ATTESTATION_TRUSTY_OS
724728
SetDeviceAttestationCredentialsProvider(&TrustyDACProvider::GetTrustyDACProvider());
729+
#elif CHIP_ATTESTATION_ELE
730+
SetDeviceAttestationCredentialsProvider(&EleDACProvider::GetEleDACProvider());
725731
#else
726732
SetDeviceAttestationCredentialsProvider(LinuxDeviceOptions::GetInstance().dacProvider);
727733
#endif

examples/platform/linux/BUILD.gn

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Copyright (c) 2020 Project CHIP Authors
2+
# Copyright 2025 NXP
23
#
34
# Licensed under the Apache License, Version 2.0 (the "License");
45
# you may not use this file except in compliance with the License.
@@ -100,15 +101,17 @@ source_set("app-main") {
100101

101102
if (chip_with_trusty_os == 1) {
102103
sources += [
103-
"DeviceAttestationCreds.cpp",
104-
"DeviceAttestationCreds.h",
104+
"DeviceAttestationCredsTrusty.cpp",
105+
"DeviceAttestationCredsTrusty.h",
105106
"PersistentStorageOperationalKeystoreTrusty.cpp",
106107
"PersistentStorageOperationalKeystoreTrusty.h",
107108
]
108109
}
109110

110111
if (chip_with_imx_ele == 1) {
111112
sources += [
113+
"DeviceAttestationCredsEle.cpp",
114+
"DeviceAttestationCredsEle.h",
112115
"PersistentStorageOperationalKeystoreEle.cpp",
113116
"PersistentStorageOperationalKeystoreEle.h",
114117
]
@@ -162,8 +165,10 @@ source_set("app-main") {
162165

163166
if (chip_with_imx_ele == 1) {
164167
defines += [ "CHIP_OP_KEYSTORE_ELE=1" ]
168+
defines += [ "CHIP_ATTESTATION_ELE=1" ]
165169
} else {
166170
defines += [ "CHIP_OP_KEYSTORE_ELE=0" ]
171+
defines += [ "CHIP_ATTESTATION_ELE=0" ]
167172
}
168173

169174
if (chip_enable_pw_rpc) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/*
2+
*
3+
* Copyright (c) 2021-2022 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
* Copyright 2023 NXP
18+
*/
19+
#include "DeviceAttestationCredsEle.h"
20+
21+
#include <crypto/CHIPCryptoPAL.h>
22+
#include <lib/core/CHIPError.h>
23+
#include <lib/support/Span.h>
24+
25+
#define KEY_STORE_ID (0xBBBB)
26+
#define AUTHEN_NONCE (0x2222)
27+
#define PAI_DATA_ID 0x1111
28+
#define DAC_DATA_ID 0x1112
29+
#define CD_DATA_ID 0x1113
30+
#define DAC_PRIVATE_KEY_ID 0x1114
31+
32+
namespace chip {
33+
namespace Credentials {
34+
namespace ele {
35+
36+
EleDACProvider::EleDACProvider()
37+
{
38+
hsm_err_t err;
39+
40+
// open the session
41+
open_session_args_t open_session_args = {0};
42+
open_session_args.mu_type = HSM1;
43+
err = hsm_open_session(&open_session_args, &hsm_session_hdl);
44+
if (err != HSM_NO_ERROR) {
45+
ChipLogDetail(Crypto, "ELE device attestation session open failed. ret: 0x%x\n", err);
46+
return;
47+
} else {
48+
ChipLogDetail(Crypto, "ELE device attestation session open successfully.\n");
49+
}
50+
51+
// open the keystore
52+
open_svc_key_store_args_t open_svc_key_store_args = {0};
53+
open_svc_key_store_args.key_store_identifier = KEY_STORE_ID;
54+
open_svc_key_store_args.authentication_nonce = AUTHEN_NONCE;
55+
// try to create a new keystore, if it already exist, open it
56+
open_svc_key_store_args.flags = (HSM_SVC_KEY_STORE_FLAGS_CREATE | HSM_SVC_KEY_STORE_FLAGS_STRICT_OPERATION);
57+
err = hsm_open_key_store_service(hsm_session_hdl, &open_svc_key_store_args, &key_store_hdl);
58+
if (err == HSM_KEY_STORE_CONFLICT) {
59+
ChipLogDetail(Crypto, "device attestation keystore already existed, open it...\n");
60+
open_svc_key_store_args.flags = HSM_SVC_KEY_STORE_FLAGS_LOAD;
61+
err = hsm_open_key_store_service(hsm_session_hdl, &open_svc_key_store_args, &key_store_hdl);
62+
if (err != HSM_NO_ERROR) {
63+
ChipLogDetail(Crypto, "device attestation keystore open failed. ret:0x%x\n", err);
64+
return;
65+
} else
66+
ChipLogDetail(Crypto, "device attestation keystore open successfully.\n");
67+
} else {
68+
ChipLogDetail(Crypto, "device attestation keystore created successfully.\n");
69+
}
70+
}
71+
72+
EleDACProvider::~EleDACProvider()
73+
{
74+
hsm_err_t err;
75+
76+
ChipLogDetail(Crypto, "close all ELE device attestation services.\n");
77+
78+
err = hsm_close_key_store_service(key_store_hdl);
79+
key_store_hdl = 0;
80+
ChipLogDetail(Crypto, "close device attestation key store service returns:0x%x\n", err);
81+
82+
err = hsm_close_session(hsm_session_hdl);
83+
hsm_session_hdl = 0;
84+
ChipLogDetail(Crypto, "close ELE device attestation session returns:0x%x\n", err);
85+
}
86+
87+
CHIP_ERROR EleDACProvider::GetDeviceAttestationCert(MutableByteSpan & out_dac_buffer)
88+
{
89+
op_data_storage_args_t data_storage_args;
90+
hsm_err_t err;
91+
92+
data_storage_args.svc_flags = 0;
93+
data_storage_args.data = out_dac_buffer.data();
94+
data_storage_args.data_size = out_dac_buffer.size();
95+
data_storage_args.data_id = DAC_DATA_ID;
96+
data_storage_args.flags |= HSM_OP_DATA_STORAGE_FLAGS_RETRIEVE;
97+
err = hsm_data_ops(key_store_hdl, &data_storage_args);
98+
if (err) {
99+
ChipLogDetail(Crypto, "ELE get DAC failed. ret: 0x%x\n", err);
100+
return CHIP_ERROR_CERT_LOAD_FAILED;
101+
}
102+
103+
out_dac_buffer.reduce_size(data_storage_args.exp_output_size);
104+
return CHIP_NO_ERROR;
105+
}
106+
107+
CHIP_ERROR EleDACProvider::GetProductAttestationIntermediateCert(MutableByteSpan & out_pai_buffer)
108+
{
109+
op_data_storage_args_t data_storage_args;
110+
hsm_err_t err;
111+
112+
data_storage_args.svc_flags = 0;
113+
data_storage_args.data = out_pai_buffer.data();
114+
data_storage_args.data_size = out_pai_buffer.size();
115+
data_storage_args.data_id = PAI_DATA_ID;
116+
data_storage_args.flags |= HSM_OP_DATA_STORAGE_FLAGS_RETRIEVE;
117+
err = hsm_data_ops(key_store_hdl, &data_storage_args);
118+
if (err) {
119+
ChipLogDetail(Crypto, "ELE get PAI failed. ret: 0x%x\n", err);
120+
return CHIP_ERROR_CERT_LOAD_FAILED;
121+
}
122+
123+
out_pai_buffer.reduce_size(data_storage_args.exp_output_size);
124+
return CHIP_NO_ERROR;
125+
}
126+
127+
CHIP_ERROR EleDACProvider::GetCertificationDeclaration(MutableByteSpan & out_cd_buffer)
128+
{
129+
op_data_storage_args_t data_storage_args;
130+
hsm_err_t err;
131+
132+
data_storage_args.svc_flags = 0;
133+
data_storage_args.data = out_cd_buffer.data();
134+
data_storage_args.data_size = out_cd_buffer.size();
135+
data_storage_args.data_id = CD_DATA_ID;
136+
data_storage_args.flags |= HSM_OP_DATA_STORAGE_FLAGS_RETRIEVE;
137+
err = hsm_data_ops(key_store_hdl, &data_storage_args);
138+
if (err) {
139+
ChipLogDetail(Crypto, "ELE get CD failed. ret: 0x%x\n", err);
140+
return CHIP_ERROR_CERT_LOAD_FAILED;
141+
}
142+
143+
out_cd_buffer.reduce_size(data_storage_args.exp_output_size);
144+
return CHIP_NO_ERROR;
145+
}
146+
147+
CHIP_ERROR EleDACProvider::GetFirmwareInformation(MutableByteSpan & out_firmware_info_buffer)
148+
{
149+
// TODO: We need a real example FirmwareInformation to be populated.
150+
out_firmware_info_buffer.reduce_size(0);
151+
152+
return CHIP_NO_ERROR;
153+
}
154+
155+
CHIP_ERROR EleDACProvider::SignWithDeviceAttestationKey(const ByteSpan & message_to_sign,
156+
MutableByteSpan & out_signature_buffer)
157+
{
158+
open_svc_sign_gen_args_t open_sig_gen_args;
159+
op_generate_sign_args_t sig_gen_args;
160+
uint8_t signature[64];
161+
hsm_hdl_t sig_gen_hdl;
162+
size_t out_size = 0;
163+
hsm_err_t hsmret;
164+
165+
VerifyOrReturnError(IsSpanUsable(out_signature_buffer), CHIP_ERROR_INVALID_ARGUMENT);
166+
VerifyOrReturnError(IsSpanUsable(message_to_sign), CHIP_ERROR_INVALID_ARGUMENT);
167+
VerifyOrReturnError(out_signature_buffer.size() >= sizeof(signature), CHIP_ERROR_BUFFER_TOO_SMALL);
168+
169+
// open signature generation service
170+
memset(&open_sig_gen_args, 0, sizeof(open_sig_gen_args));
171+
hsmret = hsm_open_signature_generation_service(key_store_hdl, &open_sig_gen_args, &sig_gen_hdl);
172+
if (hsmret != HSM_NO_ERROR) {
173+
ChipLogDetail(Crypto, "open signature generation service failed. ret:0x%x\n", hsmret);
174+
return CHIP_ERROR_HSM;
175+
}
176+
177+
// generate signature
178+
memset(&sig_gen_args, 0, sizeof(sig_gen_args));
179+
sig_gen_args.key_identifier = DAC_PRIVATE_KEY_ID;
180+
sig_gen_args.scheme_id = HSM_SIGNATURE_SCHEME_ECDSA_SHA256;
181+
sig_gen_args.message = (uint8_t *)(message_to_sign.data());
182+
sig_gen_args.signature = signature;
183+
sig_gen_args.message_size = message_to_sign.size();
184+
sig_gen_args.signature_size = sizeof(signature);
185+
sig_gen_args.flags = HSM_OP_GENERATE_SIGN_FLAGS_INPUT_MESSAGE;
186+
hsmret = hsm_generate_signature(sig_gen_hdl, &sig_gen_args);
187+
hsm_close_signature_generation_service(sig_gen_hdl);
188+
if (hsmret != HSM_NO_ERROR) {
189+
ChipLogDetail(Crypto, "generate signature failed. ret:0x%x\n", hsmret);
190+
return CHIP_ERROR_HSM;
191+
}
192+
193+
memcpy(out_signature_buffer.data(), signature, sizeof(signature));
194+
out_signature_buffer.reduce_size(sizeof(signature));
195+
return CHIP_NO_ERROR;
196+
}
197+
198+
EleDACProvider & EleDACProvider::GetEleDACProvider()
199+
{
200+
static EleDACProvider ele_dac_provider;
201+
202+
return ele_dac_provider;
203+
}
204+
205+
} // namespace Trusty
206+
} // namespace Credentials
207+
} // namespace chip
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2023 NXP
3+
*
4+
* Copyright (c) 2021 Project CHIP Authors
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+
#pragma once
19+
20+
#include <credentials/DeviceAttestationCredsProvider.h>
21+
#include "hsm_api.h"
22+
23+
namespace chip {
24+
namespace Credentials {
25+
namespace ele {
26+
27+
class EleDACProvider : public DeviceAttestationCredentialsProvider
28+
{
29+
public:
30+
EleDACProvider();
31+
~EleDACProvider();
32+
static EleDACProvider & GetEleDACProvider();
33+
34+
CHIP_ERROR GetCertificationDeclaration(MutableByteSpan & out_cd_buffer) override;
35+
CHIP_ERROR GetFirmwareInformation(MutableByteSpan & out_firmware_info_buffer) override;
36+
CHIP_ERROR GetDeviceAttestationCert(MutableByteSpan & out_dac_buffer) override;
37+
CHIP_ERROR GetProductAttestationIntermediateCert(MutableByteSpan & out_pai_buffer) override;
38+
CHIP_ERROR SignWithDeviceAttestationKey(const ByteSpan & message_to_sign, MutableByteSpan & out_signature_buffer) override;
39+
40+
private:
41+
hsm_hdl_t hsm_session_hdl = 0;
42+
hsm_hdl_t key_store_hdl = 0;
43+
};
44+
45+
} // namespace ele
46+
} // namespace Credentials
47+
} // namespace chip

examples/platform/linux/DeviceAttestationCreds.cpp examples/platform/linux/DeviceAttestationCredsTrusty.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
*
33
* Copyright (c) 2021-2022 Project CHIP Authors
4+
* Copyright 2023, 2025 NXP
45
*
56
* Licensed under the Apache License, Version 2.0 (the "License");
67
* you may not use this file except in compliance with the License.
@@ -13,10 +14,8 @@
1314
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1415
* See the License for the specific language governing permissions and
1516
* limitations under the License.
16-
*
17-
* Copyright 2023 NXP
1817
*/
19-
#include "DeviceAttestationCreds.h"
18+
#include "DeviceAttestationCredsTrusty.h"
2019

2120
#include <crypto/CHIPCryptoPAL.h>
2221
#include <lib/core/CHIPError.h>

examples/platform/linux/PersistentStorageOperationalKeystoreEle.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2022 Project CHIP Authors
3+
* Copyright 2023, 2025 NXP
34
* All rights reserved.
45
*
56
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,8 +14,6 @@
1314
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1415
* See the License for the specific language governing permissions and
1516
* limitations under the License.
16-
*
17-
* Copyright 2023 NXP
1817
*/
1918

2019
#include <crypto/OperationalKeystore.h>
@@ -73,11 +72,11 @@ PersistentStorageOperationalKeystoreEle::PersistentStorageOperationalKeystoreEle
7372
open_svc_key_store_args.key_store_identifier = KEY_STORE_ID;
7473
open_svc_key_store_args.authentication_nonce = AUTHEN_NONCE;
7574
// try to create a new keystore, if it already exist, open it
76-
open_svc_key_store_args.flags = 1;
75+
open_svc_key_store_args.flags = (HSM_SVC_KEY_STORE_FLAGS_CREATE | HSM_SVC_KEY_STORE_FLAGS_STRICT_OPERATION);
7776
err = hsm_open_key_store_service(hsm_session_hdl, &open_svc_key_store_args, &key_store_hdl);
7877
if (err == HSM_KEY_STORE_CONFLICT) {
7978
ChipLogDetail(Crypto, "keystore already existed, open it...\n");
80-
open_svc_key_store_args.flags = 0;
79+
open_svc_key_store_args.flags = HSM_SVC_KEY_STORE_FLAGS_LOAD;
8180
err = hsm_open_key_store_service(hsm_session_hdl, &open_svc_key_store_args, &key_store_hdl);
8281
if (err != HSM_NO_ERROR) {
8382
ChipLogDetail(Crypto, "keystore open failed. ret:0x%x\n", err);

0 commit comments

Comments
 (0)