Skip to content

Commit 07b4bab

Browse files
luoji-nxp“nxf90552”
authored and
“nxf90552”
committed
Support P256KeyPair based on Trusty OS
move the p256keypair operations to Trusty OS to enhance the crypto security. Change-Id: I47ec6b440f91adf3e717ed8915f35b7844731c90 Signed-off-by: Ji Luo <ji.luo@nxp.com> Reviewed-on: http://androidsource.nxp.com/project/21251 Reviewed-by: Elven Wang <elven.wang@nxp.com> Reviewed-on: http://androidsource.nxp.com/project/23005
1 parent c538e1a commit 07b4bab

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

src/crypto/BUILD.gn

+13
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.
@@ -20,6 +21,8 @@ import("${chip_root}/build/chip/buildconfig_header.gni")
2021

2122
import("crypto.gni")
2223

24+
import("${chip_root}/src/lib/trusty.gni")
25+
2326
if (chip_crypto == "") {
2427
if (current_os == "android" || current_os == "freertos" ||
2528
current_os == "zephyr" || current_os == "mbed" || current_os == "webos" ||
@@ -74,6 +77,12 @@ buildconfig_header("crypto_buildconfig") {
7477
"CHIP_CRYPTO_BORINGSSL=${chip_crypto_boringssl}",
7578
"CHIP_CRYPTO_PLATFORM=${chip_crypto_platform}",
7679
]
80+
81+
if (chip_with_trusty_os == 1) {
82+
defines += [ "CHIP_CRYPTO_TRUSTY_OS=1" ]
83+
} else {
84+
defines += [ "CHIP_CRYPTO_TRUSTY_OS=0" ]
85+
}
7786
}
7887

7988
source_set("public_headers") {
@@ -92,6 +101,10 @@ source_set("public_headers") {
92101
"${chip_root}/src/lib/support",
93102
"${nlassert_root}:nlassert",
94103
]
104+
105+
if (chip_with_trusty_os == 1) {
106+
public_deps += [ "${chip_root}/third_party/libtrustymatter" ]
107+
}
95108
}
96109

97110
if (chip_crypto == "openssl") {

src/crypto/CHIPCryptoPAL.h

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
*
33
* Copyright (c) 2020-2023 Project CHIP Authors
4+
* Copyright 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.
@@ -588,6 +589,10 @@ class P256Keypair : public P256KeypairBase
588589
/** Release resources associated with this key pair */
589590
void Clear();
590591

592+
#if CHIP_CRYPTO_TRUSTY_OS
593+
uint64_t p256_handler = 0;
594+
#endif
595+
591596
protected:
592597
P256PublicKey mPublicKey;
593598
mutable P256KeypairContext mKeypair;

src/crypto/CHIPCryptoPALOpenSSL.cpp

+164
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
*
33
* Copyright (c) 2020-2023 Project CHIP Authors
4+
* Copyright 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.
@@ -55,9 +56,18 @@
5556

5657
#include <string.h>
5758

59+
#if CHIP_CRYPTO_TRUSTY_OS
60+
#include <trusty_matter.h>
61+
using namespace matter;
62+
#endif
63+
5864
namespace chip {
5965
namespace Crypto {
6066

67+
#if CHIP_CRYPTO_TRUSTY_OS
68+
TrustyMatter trusty_matter;
69+
#endif
70+
6171
// BoringSSL is designed to implement the same interface as OpenSSL in most
6272
// cases. However, it removes some APIs that can allow very weak configuration.
6373
// (Example: CCM ciphers with low tag lengths.) In order to support Matter,
@@ -744,6 +754,43 @@ static inline const EC_KEY * to_const_EC_KEY(const P256KeypairContext * context)
744754
return *SafePointerCast<const EC_KEY * const *>(context);
745755
}
746756

757+
#if CHIP_CRYPTO_TRUSTY_OS
758+
CHIP_ERROR P256Keypair::ECDSA_sign_msg(const uint8_t * msg, const size_t msg_length, P256ECDSASignature & out_signature) const
759+
{
760+
CHIP_ERROR error = CHIP_NO_ERROR;
761+
size_t sig_size = 0;
762+
int rc = 0;
763+
764+
VerifyOrReturnError((msg != nullptr) && (msg_length > 0), CHIP_ERROR_INVALID_ARGUMENT);
765+
766+
uint8_t digest[kSHA256_Hash_Length];
767+
uint8_t sig[kP256_ECDSA_Signature_Length_Raw];
768+
memset(&digest[0], 0, sizeof(digest));
769+
memset(&sig[0], 0, sizeof(sig));
770+
771+
ReturnErrorOnFailure(Hash_SHA256(msg, msg_length, &digest[0]));
772+
773+
ERR_clear_error();
774+
775+
static_assert(P256ECDSASignature::Capacity() >= kP256_ECDSA_Signature_Length_Raw, "P256ECDSASignature must be large enough");
776+
VerifyOrExit(mInitialized, error = CHIP_ERROR_UNINITIALIZED);
777+
778+
rc = trusty_matter.P256KeypairECSignMsg(p256_handler, digest, kSHA256_Hash_Length, sig, sig_size);
779+
VerifyOrExit(rc == MATTER_ERROR_OK, error = CHIP_ERROR_INTERNAL);
780+
VerifyOrExit(sig_size == kP256_ECDSA_Signature_Length_Raw, error = CHIP_ERROR_INTERNAL);
781+
782+
VerifyOrExit(out_signature.SetLength(kP256_ECDSA_Signature_Length_Raw) == CHIP_NO_ERROR, error = CHIP_ERROR_INTERNAL);
783+
memcpy(out_signature.Bytes() + 0u, sig, sig_size);
784+
785+
exit:
786+
if (error != CHIP_NO_ERROR)
787+
{
788+
_logSSLError();
789+
}
790+
791+
return error;
792+
}
793+
#else
747794
CHIP_ERROR P256Keypair::ECDSA_sign_msg(const uint8_t * msg, const size_t msg_length, P256ECDSASignature & out_signature) const
748795
{
749796
CHIP_ERROR error = CHIP_NO_ERROR;
@@ -799,6 +846,7 @@ CHIP_ERROR P256Keypair::ECDSA_sign_msg(const uint8_t * msg, const size_t msg_len
799846

800847
return error;
801848
}
849+
#endif
802850

803851
CHIP_ERROR P256PublicKey::ECDSA_validate_msg_signature(const uint8_t * msg, const size_t msg_length,
804852
const P256ECDSASignature & signature) const
@@ -968,6 +1016,25 @@ static CHIP_ERROR _create_evp_key_from_binary_p256_key(const P256PublicKey & key
9681016
return error;
9691017
}
9701018

1019+
#if CHIP_CRYPTO_TRUSTY_OS
1020+
CHIP_ERROR P256Keypair::ECDH_derive_secret(const P256PublicKey & remote_public_key, P256ECDHDerivedSecret & out_secret) const
1021+
{
1022+
ERR_clear_error();
1023+
CHIP_ERROR error = CHIP_NO_ERROR;
1024+
int result = -1;
1025+
size_t out_buf_length = 0;
1026+
1027+
result = trusty_matter.P256KeypairECDH_derive_secret(p256_handler, Uint8::to_const_uchar(remote_public_key),
1028+
remote_public_key.Length(), out_secret.Bytes(), out_buf_length);
1029+
VerifyOrExit(result == MATTER_ERROR_OK, error = CHIP_ERROR_INTERNAL);
1030+
VerifyOrExit((out_buf_length > 0), error = CHIP_ERROR_INTERNAL);
1031+
SuccessOrExit(out_secret.SetLength(out_buf_length));
1032+
1033+
exit:
1034+
_logSSLError();
1035+
return error;
1036+
}
1037+
#else
9711038
CHIP_ERROR P256Keypair::ECDH_derive_secret(const P256PublicKey & remote_public_key, P256ECDHDerivedSecret & out_secret) const
9721039
{
9731040
ERR_clear_error();
@@ -1035,6 +1102,7 @@ CHIP_ERROR P256Keypair::ECDH_derive_secret(const P256PublicKey & remote_public_k
10351102
_logSSLError();
10361103
return error;
10371104
}
1105+
#endif
10381106

10391107
void ClearSecretData(uint8_t * buf, size_t len)
10401108
{
@@ -1082,6 +1150,101 @@ static CHIP_ERROR P256PublicKeyFromECKey(EC_KEY * ec_key, P256PublicKey & pubkey
10821150
return error;
10831151
}
10841152

1153+
#if CHIP_CRYPTO_TRUSTY_OS
1154+
CHIP_ERROR P256Keypair::Initialize(ECPKeyTarget key_target)
1155+
{
1156+
ERR_clear_error();
1157+
CHIP_ERROR error = CHIP_NO_ERROR;
1158+
uint8_t public_key[kP256_PublicKey_Length];
1159+
int rc = 0;
1160+
1161+
rc = trusty_matter.P256KeypairInitialize(p256_handler, public_key);
1162+
VerifyOrExit(rc == MATTER_ERROR_OK, error = CHIP_ERROR_INTERNAL);
1163+
1164+
memcpy(Uint8::to_uchar(mPublicKey), public_key, kP256_PublicKey_Length);
1165+
1166+
mInitialized = true;
1167+
1168+
exit:
1169+
_logSSLError();
1170+
return error;
1171+
}
1172+
1173+
CHIP_ERROR P256Keypair::Serialize(P256SerializedKeypair & output) const
1174+
{
1175+
CHIP_ERROR error = CHIP_NO_ERROR;
1176+
uint8_t privkey[kP256_PrivateKey_Length];
1177+
int rc = 0;
1178+
1179+
rc = trusty_matter.P256KeypairSerialize(p256_handler, privkey);
1180+
VerifyOrExit(rc == MATTER_ERROR_OK, error = CHIP_ERROR_INTERNAL);
1181+
1182+
{
1183+
size_t len = output.Length() == 0 ? output.Capacity() : output.Length();
1184+
Encoding::BufferWriter bbuf(output.Bytes(), len);
1185+
bbuf.Put(mPublicKey, mPublicKey.Length());
1186+
bbuf.Put(privkey, sizeof(privkey));
1187+
VerifyOrExit(bbuf.Fit(), error = CHIP_ERROR_NO_MEMORY);
1188+
output.SetLength(bbuf.Needed());
1189+
}
1190+
1191+
exit:
1192+
ClearSecretData(privkey, sizeof(privkey));
1193+
_logSSLError();
1194+
return error;
1195+
}
1196+
1197+
CHIP_ERROR P256Keypair::Deserialize(P256SerializedKeypair & input)
1198+
{
1199+
CHIP_ERROR error = CHIP_NO_ERROR;
1200+
Encoding::BufferWriter bbuf(mPublicKey, mPublicKey.Length());
1201+
int rc = 0;
1202+
1203+
uint8_t *pubkey = input.Bytes();
1204+
uint8_t * privkey = input.Bytes() + mPublicKey.Length();
1205+
1206+
VerifyOrExit(input.Length() == mPublicKey.Length() + kP256_PrivateKey_Length, error = CHIP_ERROR_INVALID_ARGUMENT);
1207+
bbuf.Put(input.ConstBytes(), mPublicKey.Length());
1208+
VerifyOrExit(bbuf.Fit(), error = CHIP_ERROR_NO_MEMORY);
1209+
1210+
rc = trusty_matter.P256KeypairDeserialize(p256_handler, pubkey, mPublicKey.Length(), privkey, kP256_PrivateKey_Length);
1211+
VerifyOrExit(rc == MATTER_ERROR_OK, error = CHIP_ERROR_INTERNAL);
1212+
1213+
mInitialized = true;
1214+
1215+
exit:
1216+
_logSSLError();
1217+
return error;
1218+
}
1219+
1220+
void P256Keypair::Clear()
1221+
{
1222+
mInitialized = false;
1223+
p256_handler = 0;
1224+
}
1225+
1226+
P256Keypair::~P256Keypair()
1227+
{
1228+
trusty_matter.P256KeypairDestory(p256_handler);
1229+
Clear();
1230+
}
1231+
1232+
CHIP_ERROR P256Keypair::NewCertificateSigningRequest(uint8_t * out_csr, size_t & csr_length) const
1233+
{
1234+
ERR_clear_error();
1235+
CHIP_ERROR error = CHIP_NO_ERROR;
1236+
int rc = 0;
1237+
1238+
VerifyOrExit(mInitialized, error = CHIP_ERROR_UNINITIALIZED);
1239+
1240+
rc = trusty_matter.P256KeypairNewCSR(p256_handler, out_csr, csr_length);
1241+
VerifyOrExit(rc == MATTER_ERROR_OK, error = CHIP_ERROR_INTERNAL);
1242+
1243+
exit:
1244+
_logSSLError();
1245+
return error;
1246+
}
1247+
#else
10851248
CHIP_ERROR P256Keypair::Initialize(ECPKeyTarget key_target)
10861249
{
10871250
ERR_clear_error();
@@ -1314,6 +1477,7 @@ CHIP_ERROR P256Keypair::NewCertificateSigningRequest(uint8_t * out_csr, size_t &
13141477
_logSSLError();
13151478
return error;
13161479
}
1480+
#endif
13171481

13181482
CHIP_ERROR VerifyCertificateSigningRequest(const uint8_t * csr, size_t csr_length, P256PublicKey & pubkey)
13191483
{

0 commit comments

Comments
 (0)