forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEBUG IN PROGRESS: PSA symmetric keys.
- Loading branch information
1 parent
931b383
commit 8dc703a
Showing
9 changed files
with
276 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "PSAKeyDerivation.h" | ||
|
||
namespace chip { | ||
namespace Crypto { | ||
|
||
CHIP_ERROR PsaKdf::Init(psa_algorithm_t algorithm, const ByteSpan & secret, const ByteSpan & salt, const ByteSpan & info) | ||
{ | ||
psa_status_t status = PSA_SUCCESS; | ||
psa_key_attributes_t attrs = PSA_KEY_ATTRIBUTES_INIT; | ||
|
||
psa_set_key_type(&attrs, PSA_KEY_TYPE_DERIVE); | ||
psa_set_key_algorithm(&attrs, PSA_ALG_HKDF(PSA_ALG_SHA_256)); | ||
psa_set_key_usage_flags(&attrs, PSA_KEY_USAGE_DERIVE); | ||
|
||
status = psa_import_key(&attrs, secret.data(), secret.size(), &mSecretKeyId); | ||
psa_reset_key_attributes(&attrs); | ||
VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); | ||
|
||
status = psa_key_derivation_setup(&mOperation, algorithm); | ||
VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); | ||
|
||
if (salt.size() > 0) | ||
{ | ||
status = psa_key_derivation_input_bytes(&mOperation, PSA_KEY_DERIVATION_INPUT_SALT, salt.data(), salt.size()); | ||
VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); | ||
} | ||
|
||
status = psa_key_derivation_input_key(&mOperation, PSA_KEY_DERIVATION_INPUT_SECRET, mSecretKeyId); | ||
VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); | ||
|
||
status = psa_key_derivation_input_bytes(&mOperation, PSA_KEY_DERIVATION_INPUT_INFO, info.data(), info.size()); | ||
VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); | ||
|
||
return CHIP_NO_ERROR; | ||
} | ||
|
||
CHIP_ERROR PsaKdf::DeriveBytes(const MutableByteSpan & output) | ||
{ | ||
psa_status_t status = psa_key_derivation_output_bytes(&mOperation, output.data(), output.size()); | ||
VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); | ||
|
||
return CHIP_NO_ERROR; | ||
} | ||
|
||
CHIP_ERROR PsaKdf::DeriveKey(const psa_key_attributes_t & attributes, psa_key_id_t & keyId) | ||
{ | ||
psa_status_t status = psa_key_derivation_output_key(&attributes, &mOperation, &keyId); | ||
VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); | ||
|
||
return CHIP_NO_ERROR; | ||
} | ||
|
||
} // namespace Crypto | ||
} // namespace chip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
|
||
#pragma once | ||
|
||
#include <lib/core/DataModelTypes.h> | ||
#include <lib/core/CHIPError.h> | ||
// #include <lib/core/CHIPVendorIdentifiers.hpp> | ||
// #include <lib/core/Optional.h> | ||
// #include <lib/support/CodeUtils.h> | ||
// #include <lib/support/SafePointerCast.h> | ||
#include <lib/support/Span.h> | ||
#include <psa/crypto.h> | ||
|
||
|
||
namespace chip { | ||
namespace Crypto { | ||
|
||
/** | ||
* @brief Wrapper for PSA key derivation API. | ||
*/ | ||
class PsaKdf | ||
{ | ||
public: | ||
~PsaKdf() | ||
{ | ||
psa_key_derivation_abort(&mOperation); | ||
psa_destroy_key(mSecretKeyId); | ||
} | ||
|
||
/** | ||
* @brief Initializes the key derivation operation. | ||
*/ | ||
CHIP_ERROR Init(psa_algorithm_t algorithm, const ByteSpan & secret, const ByteSpan & salt, const ByteSpan & info); | ||
|
||
/** | ||
* @brief Derives raw key material from the operation. | ||
* | ||
* This method together with @p DeriveKeys can be called multiple times to | ||
* derive several keys. | ||
* | ||
* @param[out] output Span that provides location and length for the derived key material. | ||
* | ||
* @retval CHIP_NO_ERROR On success. | ||
* @retval CHIP_ERROR_INTERNAL On PSA crypto API error. | ||
*/ | ||
CHIP_ERROR DeriveBytes(const MutableByteSpan & output); | ||
|
||
/** | ||
* @brief Derives a key from the operation. | ||
* | ||
* This method together with @p DeriveBytes can be called multiple times to | ||
* derive several keys. | ||
* | ||
* @param[in] attributes Attributes of the derived key. | ||
* @param[out] keyId PSA key ID of the derived key. | ||
* | ||
* @retval CHIP_NO_ERROR On success. | ||
* @retval CHIP_ERROR_INTERNAL On PSA crypto API error. | ||
*/ | ||
CHIP_ERROR DeriveKey(const psa_key_attributes_t & attributes, psa_key_id_t & keyId); | ||
|
||
private: | ||
psa_key_id_t mSecretKeyId = 0; | ||
psa_key_derivation_operation_t mOperation = PSA_KEY_DERIVATION_OPERATION_INIT; | ||
}; | ||
|
||
} // namespace Crypto | ||
} // namespace chip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.