@@ -78,7 +78,6 @@ extern CHIP_ERROR ECDSA_validate_msg_signature_H(const P256PublicKey * public_ke
78
78
extern CHIP_ERROR ECDSA_validate_hash_signature_H (const P256PublicKey * public_key, const uint8_t * hash, const size_t hash_length,
79
79
const P256ECDSASignature & signature);
80
80
81
- #if (ENABLE_TRUSTM_GENERATE_EC_KEY || ENABLE_TRUSTM_ECDSA_VERIFY)
82
81
static CHIP_ERROR get_trustm_keyid_from_keypair (const P256KeypairContext mKeypair , uint32_t * key_id)
83
82
{
84
83
if (0 != memcmp (&mKeypair .mBytes [0 ], trustm_magic_no, sizeof (trustm_magic_no)))
@@ -87,36 +86,24 @@ static CHIP_ERROR get_trustm_keyid_from_keypair(const P256KeypairContext mKeypai
87
86
}
88
87
89
88
*key_id += (mKeypair .mBytes [CRYPTO_KEYPAIR_KEYID_OFFSET]) | (mKeypair .mBytes [CRYPTO_KEYPAIR_KEYID_OFFSET + 1 ] << 8 );
90
-
91
89
return CHIP_NO_ERROR;
92
90
}
93
- #endif // #if (ENABLE_TRUSTM_GENERATE_EC_KEY || ENABLE_TRUSTM_ECDSA_VERIFY)
94
91
95
92
P256Keypair::~P256Keypair ()
96
93
{
97
- // Add method to get the keyid
98
94
if (CHIP_NO_ERROR != get_trustm_keyid_from_keypair (mKeypair , &keyid))
99
95
{
100
96
Clear ();
101
97
}
102
98
else
103
99
{
104
- // Delete the key in SE
105
100
}
106
101
}
107
102
108
103
CHIP_ERROR P256Keypair::Initialize (ECPKeyTarget key_target)
109
104
{
110
105
CHIP_ERROR error = CHIP_ERROR_INTERNAL;
111
106
112
- #if !ENABLE_TRUSTM_GENERATE_EC_KEY
113
- if (CHIP_NO_ERROR == Initialize_H (this , &mPublicKey , &mKeypair ))
114
- {
115
- mInitialized = true ;
116
- }
117
- error = CHIP_NO_ERROR;
118
- return error;
119
- #else
120
107
uint8_t pubkey[128 ] = {
121
108
0 ,
122
109
};
@@ -136,11 +123,20 @@ CHIP_ERROR P256Keypair::Initialize(ECPKeyTarget key_target)
136
123
}
137
124
else
138
125
{
126
+ #if !ENABLE_TRUSTM_NOC_KEYGEN
127
+ if (CHIP_NO_ERROR == Initialize_H (this , &mPublicKey , &mKeypair ))
128
+ {
129
+ mInitialized = true ;
130
+ }
131
+ error = CHIP_NO_ERROR;
132
+ return error;
133
+ #else
139
134
// Add the logic to use different keyid
140
135
keyid = TRUSTM_NODE_OID_KEY_START;
141
136
// Trust M ECC 256 Key Gen
142
137
ChipLogDetail (Crypto, " Generating NIST256 key in TrustM !" );
143
138
key_usage = (optiga_key_usage_t ) (OPTIGA_KEY_USAGE_SIGN | OPTIGA_KEY_USAGE_AUTHENTICATION);
139
+ #endif // ! ENABLE_TRUSTM_NOC_KEYGEN
144
140
}
145
141
// Trust M init
146
142
trustm_Open ();
@@ -167,14 +163,13 @@ CHIP_ERROR P256Keypair::Initialize(ECPKeyTarget key_target)
167
163
trustm_close ();
168
164
}
169
165
return error;
170
- #endif
171
166
}
172
167
173
168
CHIP_ERROR P256Keypair::ECDSA_sign_msg (const uint8_t * msg, size_t msg_length, P256ECDSASignature & out_signature) const
174
169
{
175
- # if !ENABLE_TRUSTM_GENERATE_EC_KEY
176
- return ECDSA_sign_msg_H (& mKeypair , msg, msg_length, out_signature );
177
- # else
170
+ VerifyOrReturnError ( mInitialized , CHIP_ERROR_UNINITIALIZED);
171
+ uint16_t keyid = ( mKeypair . mBytes [CRYPTO_KEYPAIR_KEYID_OFFSET]) | ( mKeypair . mBytes [CRYPTO_KEYPAIR_KEYID_OFFSET + 1 ] << 8 );
172
+
178
173
CHIP_ERROR error = CHIP_ERROR_INTERNAL;
179
174
optiga_lib_status_t return_status = OPTIGA_LIB_BUSY;
180
175
@@ -188,20 +183,31 @@ CHIP_ERROR P256Keypair::ECDSA_sign_msg(const uint8_t * msg, size_t msg_length, P
188
183
189
184
VerifyOrReturnError (msg != nullptr , CHIP_ERROR_INVALID_ARGUMENT);
190
185
VerifyOrReturnError (msg_length > 0 , CHIP_ERROR_INVALID_ARGUMENT);
191
- ChipLogDetail (Crypto, " TrustM: ECDSA_sign_msg" );
192
186
// Trust M Init
193
187
trustm_Open ();
194
188
// Hash to get the digest
195
189
Hash_SHA256 (msg, msg_length, &digest[0 ]);
196
- uint16_t keyid = (mKeypair .mBytes [CRYPTO_KEYPAIR_KEYID_OFFSET]) | (mKeypair .mBytes [CRYPTO_KEYPAIR_KEYID_OFFSET + 1 ] << 8 );
197
- // Api call to calculate the signature
198
- if (keyid == OPTIGA_KEY_ID_E0F2)
190
+
191
+ if (keyid == OPTIGA_KEY_ID_E0F0)
199
192
{
200
- return_status = trustm_ecdsa_sign (OPTIGA_KEY_ID_E0F2, digest, digest_length, signature_trustm, &signature_trustm_len);
193
+ ChipLogDetail (Crypto, " TrustM: ECDSA_sign_msg" );
194
+
195
+ // Api call to calculate the signature
196
+ return_status = trustm_ecdsa_sign (OPTIGA_KEY_ID_E0F0, digest, digest_length, signature_trustm, &signature_trustm_len);
201
197
}
202
198
else
203
199
{
204
- return_status = trustm_ecdsa_sign (OPTIGA_KEY_ID_E0F0, digest, digest_length, signature_trustm, &signature_trustm_len);
200
+ #if !ENABLE_TRUSTM_NOC_KEYGEN
201
+ // Use the mbedtls based method
202
+ printf (" ECDSA sing msg mbedtls\n " );
203
+ return ECDSA_sign_msg_H (&mKeypair , msg, msg_length, out_signature);
204
+ #else
205
+ if (keyid == OPTIGA_KEY_ID_E0F2)
206
+ {
207
+ ChipLogDetail (Crypto, " TrustM: ECDSA_sign_msg" );
208
+ return_status = trustm_ecdsa_sign (OPTIGA_KEY_ID_E0F2, digest, digest_length, signature_trustm, &signature_trustm_len);
209
+ }
210
+ #endif // ! ENABLE_TRUSTM_NOC_KEYGEN
205
211
}
206
212
207
213
VerifyOrExit (return_status == OPTIGA_LIB_SUCCESS, error = CHIP_ERROR_INTERNAL);
@@ -220,14 +226,10 @@ CHIP_ERROR P256Keypair::ECDSA_sign_msg(const uint8_t * msg, size_t msg_length, P
220
226
trustm_close ();
221
227
}
222
228
return error;
223
- #endif
224
229
}
225
230
226
231
CHIP_ERROR P256Keypair::ECDH_derive_secret (const P256PublicKey & remote_public_key, P256ECDHDerivedSecret & out_secret) const
227
232
{
228
- #if !ENABLE_TRUSTM_GENERATE_EC_KEY
229
- return ECDH_derive_secret_H (&mKeypair , remote_public_key, out_secret);
230
- #else
231
233
CHIP_ERROR error = CHIP_ERROR_INTERNAL;
232
234
optiga_lib_status_t return_status = OPTIGA_LIB_BUSY;
233
235
size_t secret_length = (out_secret.Length () == 0 ) ? out_secret.Capacity () : out_secret.Length ();
@@ -262,7 +264,6 @@ CHIP_ERROR P256Keypair::ECDH_derive_secret(const P256PublicKey & remote_public_k
262
264
trustm_close ();
263
265
}
264
266
return error;
265
- #endif
266
267
}
267
268
268
269
CHIP_ERROR P256PublicKey::ECDSA_validate_hash_signature (const uint8_t * hash, size_t hash_length,
@@ -313,6 +314,12 @@ CHIP_ERROR P256Keypair::Serialize(P256SerializedKeypair & output) const
313
314
0 ,
314
315
};
315
316
317
+ if (0 != memcmp (&mKeypair .mBytes [0 ], trustm_magic_no, sizeof (trustm_magic_no)))
318
+ {
319
+ VerifyOrReturnError (mInitialized , CHIP_ERROR_UNINITIALIZED);
320
+ return Serialize_H (mKeypair , mPublicKey , output);
321
+ }
322
+
316
323
/* Set the public key */
317
324
P256PublicKey & public_key = const_cast <P256PublicKey &>(Pubkey ());
318
325
bbuf.Put (Uint8::to_uchar (public_key), public_key.Length ());
@@ -358,16 +365,11 @@ CHIP_ERROR P256Keypair::Deserialize(P256SerializedKeypair & input)
358
365
}
359
366
else
360
367
{
361
- #if !ENABLE_TRUSTM_KEY_IMPORT
362
368
if (CHIP_NO_ERROR == (error = Deserialize_H (this , &mPublicKey , &mKeypair , input)))
363
369
{
364
370
mInitialized = true ;
365
371
}
366
372
return error;
367
- #else
368
- // Add in code for Trust M
369
- return CHIP_NO_ERROR;
370
- #endif
371
373
}
372
374
}
373
375
@@ -430,9 +432,6 @@ static void add_tlv(uint8_t * buf, size_t buf_index, uint8_t tag, size_t len, ui
430
432
431
433
CHIP_ERROR P256Keypair::NewCertificateSigningRequest (uint8_t * csr, size_t & csr_length) const
432
434
{
433
- #if !ENABLE_TRUSTM_GENERATE_EC_KEY
434
- return NewCertificateSigningRequest_H (&mKeypair , csr, csr_length);
435
- #else
436
435
CHIP_ERROR error = CHIP_ERROR_INTERNAL;
437
436
optiga_lib_status_t return_status = OPTIGA_LIB_BUSY;
438
437
@@ -585,8 +584,6 @@ CHIP_ERROR P256Keypair::NewCertificateSigningRequest(uint8_t * csr, size_t & csr
585
584
trustm_close ();
586
585
}
587
586
return error;
588
-
589
- #endif
590
587
}
591
588
592
589
} // namespace Crypto
0 commit comments