|
16 | 16 | */
|
17 | 17 |
|
18 | 18 | #include "FactoryDataProvider.h"
|
19 |
| - |
20 | 19 | #include "FactoryDataDecoder.h"
|
21 | 20 | #include <crypto/CHIPCryptoPAL.h>
|
22 | 21 | #include <lib/core/CHIPError.h>
|
@@ -254,25 +253,43 @@ CHIP_ERROR FactoryDataProvider::SignWithDeviceAttestationKey(const ByteSpan & me
|
254 | 253 |
|
255 | 254 | if (kReadFromFlash)
|
256 | 255 | {
|
| 256 | +#if CONFIG_ENABLE_AMEBA_CRYPTO |
| 257 | + ReturnErrorCodeIf(!mFactoryData.dac.dac_cert.value, CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND); |
| 258 | + // Extract public key from DAC cert. |
| 259 | + ByteSpan dacCertSpan{ reinterpret_cast<uint8_t *>(mFactoryData.dac.dac_cert.value), mFactoryData.dac.dac_cert.len }; |
| 260 | + chip::Crypto::P256PublicKey dacPublicKey; |
| 261 | + |
| 262 | + ReturnErrorOnFailure(chip::Crypto::ExtractPubkeyFromX509Cert(dacCertSpan, dacPublicKey)); |
| 263 | + |
| 264 | + CHIP_ERROR err = CHIP_NO_ERROR; |
| 265 | + FactoryDataDecoder decoder = FactoryDataDecoder::GetInstance(); |
| 266 | + err = decoder.GetSign(dacPublicKey.Bytes(), dacPublicKey.Length(), messageToSign.data(), messageToSign.size(), |
| 267 | + signature.Bytes()); |
| 268 | +#else |
257 | 269 | ReturnErrorCodeIf(!mFactoryData.dac.dac_cert.value, CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
|
258 | 270 | ReturnErrorCodeIf(!mFactoryData.dac.dac_key.value, CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
|
259 | 271 | // Extract public key from DAC cert.
|
260 | 272 | ByteSpan dacCertSpan{ reinterpret_cast<uint8_t *>(mFactoryData.dac.dac_cert.value), mFactoryData.dac.dac_cert.len };
|
261 | 273 | chip::Crypto::P256PublicKey dacPublicKey;
|
262 | 274 |
|
263 | 275 | ReturnErrorOnFailure(chip::Crypto::ExtractPubkeyFromX509Cert(dacCertSpan, dacPublicKey));
|
| 276 | + |
264 | 277 | ReturnErrorOnFailure(
|
265 | 278 | LoadKeypairFromRaw(ByteSpan(reinterpret_cast<uint8_t *>(mFactoryData.dac.dac_key.value), mFactoryData.dac.dac_key.len),
|
266 | 279 | ByteSpan(dacPublicKey.Bytes(), dacPublicKey.Length()), keypair));
|
| 280 | +#endif |
267 | 281 | }
|
268 | 282 | else
|
269 | 283 | {
|
270 | 284 | ReturnErrorOnFailure(LoadKeypairFromRaw(ByteSpan(kDacPrivateKey), ByteSpan(kDacPublicKey), keypair));
|
271 | 285 | }
|
272 |
| - |
| 286 | +#if CONFIG_ENABLE_AMEBA_CRYPTO |
| 287 | + VerifyOrReturnError(signature.SetLength(chip::Crypto::kP256_ECDSA_Signature_Length_Raw) == CHIP_NO_ERROR, CHIP_ERROR_INTERNAL); |
| 288 | + return CopySpanToMutableSpan(ByteSpan{ signature.ConstBytes(), signature.Length() }, outSignBuffer); |
| 289 | +#else |
273 | 290 | ReturnErrorOnFailure(keypair.ECDSA_sign_msg(messageToSign.data(), messageToSign.size(), signature));
|
274 |
| - |
275 | 291 | return CopySpanToMutableSpan(ByteSpan{ signature.ConstBytes(), signature.Length() }, outSignBuffer);
|
| 292 | +#endif |
276 | 293 | }
|
277 | 294 |
|
278 | 295 | CHIP_ERROR FactoryDataProvider::GetSetupDiscriminator(uint16_t & setupDiscriminator)
|
|
0 commit comments