|
1 | 1 | /*
|
2 | 2 | *
|
3 | 3 | * Copyright (c) 2020-2023 Project CHIP Authors
|
| 4 | + * Copyright 2025 NXP |
4 | 5 | *
|
5 | 6 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | 7 | * you may not use this file except in compliance with the License.
|
|
55 | 56 |
|
56 | 57 | #include <string.h>
|
57 | 58 |
|
| 59 | +#if CHIP_CRYPTO_TRUSTY_OS |
| 60 | +#include <trusty_matter.h> |
| 61 | +using namespace matter; |
| 62 | +#endif |
| 63 | + |
58 | 64 | namespace chip {
|
59 | 65 | namespace Crypto {
|
60 | 66 |
|
| 67 | +#if CHIP_CRYPTO_TRUSTY_OS |
| 68 | +TrustyMatter trusty_matter; |
| 69 | +#endif |
| 70 | + |
61 | 71 | // BoringSSL is designed to implement the same interface as OpenSSL in most
|
62 | 72 | // cases. However, it removes some APIs that can allow very weak configuration.
|
63 | 73 | // (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)
|
744 | 754 | return *SafePointerCast<const EC_KEY * const *>(context);
|
745 | 755 | }
|
746 | 756 |
|
| 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 |
747 | 794 | CHIP_ERROR P256Keypair::ECDSA_sign_msg(const uint8_t * msg, const size_t msg_length, P256ECDSASignature & out_signature) const
|
748 | 795 | {
|
749 | 796 | 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
|
799 | 846 |
|
800 | 847 | return error;
|
801 | 848 | }
|
| 849 | +#endif |
802 | 850 |
|
803 | 851 | CHIP_ERROR P256PublicKey::ECDSA_validate_msg_signature(const uint8_t * msg, const size_t msg_length,
|
804 | 852 | const P256ECDSASignature & signature) const
|
@@ -968,6 +1016,25 @@ static CHIP_ERROR _create_evp_key_from_binary_p256_key(const P256PublicKey & key
|
968 | 1016 | return error;
|
969 | 1017 | }
|
970 | 1018 |
|
| 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 |
971 | 1038 | CHIP_ERROR P256Keypair::ECDH_derive_secret(const P256PublicKey & remote_public_key, P256ECDHDerivedSecret & out_secret) const
|
972 | 1039 | {
|
973 | 1040 | ERR_clear_error();
|
@@ -1035,6 +1102,7 @@ CHIP_ERROR P256Keypair::ECDH_derive_secret(const P256PublicKey & remote_public_k
|
1035 | 1102 | _logSSLError();
|
1036 | 1103 | return error;
|
1037 | 1104 | }
|
| 1105 | +#endif |
1038 | 1106 |
|
1039 | 1107 | void ClearSecretData(uint8_t * buf, size_t len)
|
1040 | 1108 | {
|
@@ -1082,6 +1150,101 @@ static CHIP_ERROR P256PublicKeyFromECKey(EC_KEY * ec_key, P256PublicKey & pubkey
|
1082 | 1150 | return error;
|
1083 | 1151 | }
|
1084 | 1152 |
|
| 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 |
1085 | 1248 | CHIP_ERROR P256Keypair::Initialize(ECPKeyTarget key_target)
|
1086 | 1249 | {
|
1087 | 1250 | ERR_clear_error();
|
@@ -1314,6 +1477,7 @@ CHIP_ERROR P256Keypair::NewCertificateSigningRequest(uint8_t * out_csr, size_t &
|
1314 | 1477 | _logSSLError();
|
1315 | 1478 | return error;
|
1316 | 1479 | }
|
| 1480 | +#endif |
1317 | 1481 |
|
1318 | 1482 | CHIP_ERROR VerifyCertificateSigningRequest(const uint8_t * csr, size_t csr_length, P256PublicKey & pubkey)
|
1319 | 1483 | {
|
|
0 commit comments