Skip to content

Commit

Permalink
Set key_id to context for LoadKeypairFromRaw
Browse files Browse the repository at this point in the history
Signed-off-by: ATmobica <[email protected]>
  • Loading branch information
ATmobica authored and adbridge committed Jun 14, 2023
1 parent a168a8d commit 6bd1b2d
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions src/crypto/CHIPCryptoPALPSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,18 +712,22 @@ CHIP_ERROR P256Keypair::Initialize(ECPKeyTarget key_target)

CHIP_ERROR P256Keypair::LoadKeypairFromRaw(ByteSpan private_key, ByteSpan public_key)
{
CHIP_ERROR error = CHIP_NO_ERROR;
psa_key_attributes_t attributes = configure_ecc_key_pair_attributes();
psa_key_id_t key_id;
psa_status_t status;
PsaP256KeypairContext & context = ToPsaContext(mKeypair);

status = psa_import_key(&attributes, private_key.data(), kP256_PrivateKey_Length, &key_id);
VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL);
status = psa_import_key(&attributes, private_key.data(), kP256_PrivateKey_Length, &context.key_id);
VerifyOrExit(status == PSA_SUCCESS, error = CHIP_ERROR_INTERNAL);

memcpy(mPublicKey.Bytes(), public_key.data(), kP256_PublicKey_Length);
memcpy(mKeypair.mBytes, &key_id, sizeof(psa_key_id_t));
memcpy(mKeypair.mBytes, &context.key_id, sizeof(psa_key_id_t));
mInitialized = true;

return CHIP_NO_ERROR;
exit:
logPsaError(status);

return error;
}

CHIP_ERROR P256Keypair::LoadKeypairFromRaw(const uint8_t * private_key, const size_t private_key_size, const uint8_t * public_key,
Expand All @@ -745,36 +749,45 @@ CHIP_ERROR P256Keypair::LoadKeypairFromRaw(const uint8_t * private_key, const si

CHIP_ERROR P256Keypair::LoadKeypairFromRaw(const ByteSpan & key_pair)
{
CHIP_ERROR error = CHIP_NO_ERROR;
psa_key_attributes_t attributes = configure_ecc_key_pair_attributes();
psa_key_id_t key_id;
psa_status_t status;
PsaP256KeypairContext & context = ToPsaContext(mKeypair);

status = psa_import_key(&attributes, key_pair.data() + kP256_PublicKey_Length, kP256_PrivateKey_Length, &key_id);
VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL);
status = psa_import_key(&attributes, key_pair.data() + kP256_PublicKey_Length, kP256_PrivateKey_Length, &context.key_id);
VerifyOrExit(status == PSA_SUCCESS, error = CHIP_ERROR_INTERNAL);

memcpy(mPublicKey.Bytes(), key_pair.data(), kP256_PublicKey_Length);
memcpy(mKeypair.mBytes, &key_id, sizeof(psa_key_id_t));
memcpy(mKeypair.mBytes, &context.key_id, sizeof(psa_key_id_t));
mInitialized = true;

return CHIP_NO_ERROR;
exit:
logPsaError(status);

return error;
}

CHIP_ERROR P256Keypair::LoadKeypairFromRaw(const uint8_t * key_data, size_t key_data_size)
{
CHIP_ERROR error = CHIP_NO_ERROR;
psa_key_attributes_t attributes = configure_ecc_key_pair_attributes();
psa_key_id_t key_id;
psa_status_t status;
PsaP256KeypairContext & context = ToPsaContext(mKeypair);

VerifyOrReturnError(key_data_size == kP256_PublicKey_Length + kP256_PrivateKey_Length, CHIP_ERROR_INTERNAL);
VerifyOrExit(key_data_size == kP256_PublicKey_Length + kP256_PrivateKey_Length, error = CHIP_ERROR_INTERNAL);

status = psa_import_key(&attributes, key_data + kP256_PublicKey_Length, kP256_PrivateKey_Length, &key_id);
VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL);
status = psa_import_key(&attributes, key_data + kP256_PublicKey_Length, kP256_PrivateKey_Length, &context.key_id);
VerifyOrExit(status == PSA_SUCCESS, error = CHIP_ERROR_INTERNAL);

memcpy(mPublicKey.Bytes(), key_data, kP256_PublicKey_Length);
memcpy(mKeypair.mBytes, &key_id, sizeof(psa_key_id_t));
memcpy(mKeypair.mBytes, &context.key_id, sizeof(psa_key_id_t));
mInitialized = true;

return CHIP_NO_ERROR;
exit:
logPsaError(status);

return error;
}

CHIP_ERROR P256Keypair::Serialize(P256SerializedKeypair & output) const
Expand Down

0 comments on commit 6bd1b2d

Please sign in to comment.