Key and Configuration Manager (KCM) APIs. More...
#include <stdlib.h>
#include <stdbool.h>
#include <inttypes.h>
#include "kcm_status.h"
#include "kcm_defs.h"
Go to the source code of this file.
Functions | |
kcm_status_e | kcm_init (void) |
kcm_status_e | kcm_finalize (void) |
kcm_status_e | kcm_item_store (const uint8_t *kcm_item_name, size_t kcm_item_name_len, kcm_item_type_e kcm_item_type, bool kcm_item_is_factory, const uint8_t *kcm_item_data, size_t kcm_item_data_size, const kcm_security_desc_s kcm_item_info) |
kcm_status_e | kcm_item_get_data_size (const uint8_t *kcm_item_name, size_t kcm_item_name_len, kcm_item_type_e kcm_item_type, size_t *kcm_item_data_size_out) |
kcm_status_e | kcm_item_get_data (const uint8_t *kcm_item_name, size_t kcm_item_name_len, kcm_item_type_e kcm_item_type, uint8_t *kcm_item_data_out, size_t kcm_item_data_max_size, size_t *kcm_item_data_act_size_out) |
kcm_status_e | kcm_item_get_size_and_data (const uint8_t *kcm_item_name, size_t kcm_item_name_len, kcm_item_type_e kcm_item_type, uint8_t **kcm_item_data_out, size_t *kcm_item_data_size_out) |
kcm_status_e | kcm_item_delete (const uint8_t *kcm_item_name, size_t kcm_item_name_len, kcm_item_type_e kcm_item_type) |
kcm_status_e | kcm_cert_chain_create (kcm_cert_chain_handle *kcm_chain_handle, const uint8_t *kcm_chain_name, size_t kcm_chain_name_len, size_t kcm_chain_len, bool kcm_chain_is_factory) |
kcm_status_e | kcm_cert_chain_open (kcm_cert_chain_handle *kcm_chain_handle, const uint8_t *kcm_chain_name, size_t kcm_chain_name_len, size_t *kcm_chain_len_out) |
kcm_status_e | kcm_cert_chain_add_next (kcm_cert_chain_handle kcm_chain_handle, const uint8_t *kcm_cert_data, size_t kcm_cert_data_size) |
kcm_status_e | kcm_cert_chain_delete (const uint8_t *kcm_chain_name, size_t kcm_chain_name_len) |
kcm_status_e | kcm_cert_chain_get_next_size (kcm_cert_chain_handle kcm_chain_handle, size_t *kcm_cert_data_size) |
kcm_status_e | kcm_cert_chain_get_next_data (kcm_cert_chain_handle kcm_chain_handle, uint8_t *kcm_cert_data, size_t kcm_max_cert_data_size, size_t *kcm_actual_cert_data_size) |
kcm_status_e | kcm_cert_chain_close (kcm_cert_chain_handle kcm_chain_handle) |
kcm_status_e | kcm_factory_reset (void) |
kcm_status_e | kcm_key_pair_generate_and_store (const kcm_crypto_key_scheme_e key_scheme, const uint8_t *private_key_name, size_t private_key_name_len, const uint8_t *public_key_name, size_t public_key_name_len, bool kcm_item_is_factory, const kcm_security_desc_s kcm_item_info) |
kcm_status_e | kcm_csr_generate (const uint8_t *private_key_name, size_t private_key_name_len, const kcm_csr_params_s *csr_params, uint8_t *csr_buff_out, size_t csr_buff_max_size, size_t *csr_buff_act_size) |
kcm_status_e | kcm_generate_keys_and_csr (kcm_crypto_key_scheme_e key_scheme, const uint8_t *private_key_name, size_t private_key_name_len, const uint8_t *public_key_name, size_t public_key_name_len, bool kcm_item_is_factory, const kcm_csr_params_s *csr_params, uint8_t *csr_buff_out, size_t csr_buff_max_size, size_t *csr_buff_act_size_out, const kcm_security_desc_s kcm_item_info) |
kcm_status_e | kcm_certificate_verify_with_private_key (const uint8_t *kcm_cert_data, size_t kcm_cert_data_size, const uint8_t *kcm_priv_key_name, size_t kcm_priv_key_name_len) |
kcm_status_e | kcm_asymmetric_sign (const uint8_t *private_key_name, size_t private_key_name_len, const uint8_t *hash_digest, size_t hash_digest_size, uint8_t *signature_data_out, size_t signature_data_max_size, size_t *signature_data_act_size_out) |
kcm_status_e | kcm_asymmetric_verify (const uint8_t *public_key_name, size_t public_key_name_len, const uint8_t *hash_digest, size_t hash_digest_size, const uint8_t *signature, size_t signature_size) |
kcm_status_e | kcm_generate_random (uint8_t *buffer, size_t buffer_size) |
kcm_status_e | kcm_ecdh_key_agreement (const uint8_t *private_key_name, size_t private_key_name_len, const uint8_t *peer_public_key, size_t peer_public_key_size, uint8_t *shared_secret, size_t shared_secret_max_size, size_t *shared_secret_act_size_out) |
Key and Configuration Manager (KCM) APIs.
kcm_status_e kcm_asymmetric_sign | ( | const uint8_t * | private_key_name, |
size_t | private_key_name_len, | ||
const uint8_t * | hash_digest, | ||
size_t | hash_digest_size, | ||
uint8_t * | signature_data_out, | ||
size_t | signature_data_max_size, | ||
size_t * | signature_data_act_size_out | ||
) |
Calculates asymmetric signature on hash digest using associated private key.
The function retrieves a key data/handle according to the private key unique name, calls an asymmetric EC SECP256R1 sign function, and returns the calculated signature.
[in] | private_key_name | The private key name to fetch from storage. |
[in] | private_key_name_len | The length of the private key name. |
[in] | hash_digest | A pointer to a SHA256 hash digest buffer. |
[in] | hash_digest_size | The size of the hash digest buffer. Must be exactly KCM_SHA256_SIZE bytes. |
[out] | signature_data_out | A pointer to the output buffer for the calculated signature in raw format. |
[in] | signature_data_max_size | The size of the signature buffer. Must be at least KCM_EC_SECP256R1_SIGNATURE_RAW_SIZE bytes. |
[out] | signature_data_act_size_out | The actual size of the output signature buffer. |
kcm_status_e
errors otherwise. kcm_status_e kcm_asymmetric_verify | ( | const uint8_t * | public_key_name, |
size_t | public_key_name_len, | ||
const uint8_t * | hash_digest, | ||
size_t | hash_digest_size, | ||
const uint8_t * | signature, | ||
size_t | signature_size | ||
) |
Verifies the signature of a previously hashed message using the associated public key.
The function retrieves a key data/handle according to the public key unique name, calls an asymmetric EC SECP256R1 verify function, and returns the result.
[in] | public_key_name | The public key name to fetch from storage. |
[in] | public_key_name_len | The length of the public key name. |
[in] | hash_digest | A pointer to a SHA256 hash digest buffer. |
[in] | hash_digest_size | The size of the hash digest buffer. Must be exactly KCM_SHA256_SIZE bytes. |
[in] | signature | The signature buffer in raw format. |
[in] | signature_size | The size of the signature buffer. Must be at most KCM_EC_SECP256R1_SIGNATURE_RAW_SIZE bytes. |
kcm_status_e
errors otherwise. kcm_status_e kcm_cert_chain_add_next | ( | kcm_cert_chain_handle | kcm_chain_handle, |
const uint8_t * | kcm_cert_data, | ||
size_t | kcm_cert_data_size | ||
) |
Adds the next chain of certificates to storage.
It also validates the previous certificate (unless it is the first certificate) with the public key from kcm_cert_data
. The certificates must be added in order - starting with the leaf, followed by the certificate that signs it, and so on - all the way to the root of the chain.
[in] | kcm_chain_handle | The certificate chain handle. |
[in] | kcm_cert_data | A pointer to the certificate data in DER format. |
[in] | kcm_cert_data_size | The size of the certificate data buffer. |
kcm_status_e
errors otherwise. kcm_status_e kcm_cert_chain_close | ( | kcm_cert_chain_handle | kcm_chain_handle | ) |
Releases the context and frees allocated resources. When the operation type is creation, if the total number of added or stored certificates is not equal to the number of certificates in the chain, the API returns an error.
[in] | kcm_chain_handle | The certificate chain handle. |
kcm_status_e
errors. kcm_status_e kcm_cert_chain_create | ( | kcm_cert_chain_handle * | kcm_chain_handle, |
const uint8_t * | kcm_chain_name, | ||
size_t | kcm_chain_name_len, | ||
size_t | kcm_chain_len, | ||
bool | kcm_chain_is_factory | ||
) |
Initializes the chain context for the write chain operation. This API must be called before the kcm_cert_chain_add_next
API.
[out] | kcm_chain_handle | A pointer to the certificate chain handle. |
[in] | kcm_chain_name | Certificate chain name. |
[in] | kcm_chain_name_len | Certificate chain name length. |
[in] | kcm_chain_len | The number of certificates in the chain. |
[in] | kcm_chain_is_factory | True if the KCM chain is a factory item; otherwise, false. |
kcm_status_e
errors otherwise. kcm_status_e kcm_cert_chain_delete | ( | const uint8_t * | kcm_chain_name, |
size_t | kcm_chain_name_len | ||
) |
Deletes all certificates of the chain from storage. For an invalid chain, the API deletes all reachable certificates and returns a relevant error.
[in] | kcm_chain_name | Certificate chain name. |
[in] | kcm_chain_name_len | Certificate chain name length. |
kcm_status_e
errors otherwise. kcm_status_e kcm_cert_chain_get_next_data | ( | kcm_cert_chain_handle | kcm_chain_handle, |
uint8_t * | kcm_cert_data, | ||
size_t | kcm_max_cert_data_size, | ||
size_t * | kcm_actual_cert_data_size | ||
) |
Returns the data of the next certificate in the chain. To get the exact size of the next certificate, use kcm_cert_chain_get_next_size
. In the end of the get data operation, the chain context points to the next certificate of the current chain.
[in] | kcm_chain_handle | The certificate chain handle. |
[out] | kcm_cert_data | A pointer to the certificate data in DER format. |
[in] | kcm_max_cert_data_size | The maximum size of the certificate data buffer. |
[out] | kcm_actual_cert_data_size | The actual size of the certificate data. |
kcm_status_e
errors. kcm_status_e kcm_cert_chain_get_next_size | ( | kcm_cert_chain_handle | kcm_chain_handle, |
size_t * | kcm_cert_data_size | ||
) |
Returns the size of the next certificate in the chain. This API must be called before kcm_cert_chain_get_next_data
. This operation does not increase the chain's context iterator.
[in] | kcm_chain_handle | The certificate chain handle. |
[out] | kcm_cert_data_size | The pointer size of the next certificate. |
kcm_status_e
errors. kcm_status_e kcm_cert_chain_open | ( | kcm_cert_chain_handle * | kcm_chain_handle, |
const uint8_t * | kcm_chain_name, | ||
size_t | kcm_chain_name_len, | ||
size_t * | kcm_chain_len_out | ||
) |
Initializes the chain context for the read chain operation. This API must be called before the kcm_cert_chain_get_next_size
and kcm_cert_chain_get_next_data
APIs.
[out] | kcm_chain_handle | A pointer to the certificate chain handle. |
[in] | kcm_chain_name | Certificate chain name. |
[in] | kcm_chain_name_len | Certificate chain name length. |
[out] | kcm_chain_len | The length of the certificate chain. |
kcm_cert_chain_get_next_size
or kcm_cert_chain_get_next_data
APIs, the called API then returns a KCM_STATUS_ITEM_NOT_FOUND error. One of the kcm_status_e
errors otherwise. kcm_status_e kcm_certificate_verify_with_private_key | ( | const uint8_t * | kcm_cert_data, |
size_t | kcm_cert_data_size, | ||
const uint8_t * | kcm_priv_key_name, | ||
size_t | kcm_priv_key_name_len | ||
) |
Verifies the device-generated certificate against the given private key name from storage. This function can be called when the certificate creation is initiated by the device using the kcm_generate_keys_and_csr
or kcm_csr_generate
functions. In this case, the function checks the correlation between the certificate's public key and the given private key generated by the device and saved in device storage.
[in] | kcm_cert_data | The DER certificate data buffer. |
[in] | kcm_cert_data_size | The size of the DER certificate data buffer in bytes. |
[in] | kcm_priv_key_name | The private key name of the certificate. The function assumes that the key was generated by the device and saved in the storage. |
[in] | kcm_priv_key_name_len | The length of the private key name of the certificate. |
kcm_status_e
errors. kcm_status_e kcm_csr_generate | ( | const uint8_t * | private_key_name, |
size_t | private_key_name_len, | ||
const kcm_csr_params_s * | csr_params, | ||
uint8_t * | csr_buff_out, | ||
size_t | csr_buff_max_size, | ||
size_t * | csr_buff_act_size | ||
) |
Generates a general CSR from the given private key.
@param[in] private_key_name The private key name to fetch from storage. @param[in] private_key_name_len The length of the private key name. @param[in] csr_params CSR parameters. @param[out] csr_buff_out A pointer to the generated CSR buffer to fill. @param[in] csr_buff_max_size The size of the supplied CSR buffer. @param[out] csr_buff_act_size The actual size of the filled CSR buffer. @returns ::KCM_STATUS_SUCCESS in the event of success. Otherwise, one of the `::kcm_status_e` errors.
kcm_status_e kcm_factory_reset | ( | void | ) |
Resets the KCM secure storage to factory state.
kcm_status_e
errors. kcm_status_e kcm_finalize | ( | void | ) |
Finalizes the KCM module. Finalizes and frees file storage resources.
kcm_status_e
errors otherwise. kcm_status_e kcm_generate_keys_and_csr | ( | kcm_crypto_key_scheme_e | key_scheme, |
const uint8_t * | private_key_name, | ||
size_t | private_key_name_len, | ||
const uint8_t * | public_key_name, | ||
size_t | public_key_name_len, | ||
bool | kcm_item_is_factory, | ||
const kcm_csr_params_s * | csr_params, | ||
uint8_t * | csr_buff_out, | ||
size_t | csr_buff_max_size, | ||
size_t * | csr_buff_act_size_out, | ||
const kcm_security_desc_s | kcm_item_info | ||
) |
Generates a private and public key and CSR from the generated keys.
@param[in] key_scheme The cryptographic scheme. @param[in] private_key_name The private key name to generate. @param[in] private_key_name_len The length of the private key name. @param[in] public_key_name The public key name for which a key pair is generated. This parameter is optional. If not provided, the key is generated, but not stored. @param public_key_name_len The length of the public key name. Must be 0, if `::public_key_name` is not provided. @param[in] kcm_item_is_factory True if the KCM item is a factory item; otherwise, it is false. @param[in] csr_params CSR parameters. @param[out] csr_buff_out A pointer to the generated CSR buffer to fill. @param[in] csr_buff_max_size The size of the supplied CSR buffer. @param[out] csr_buff_act_size The actual size of the filled CSR buffer. @param[in] kcm_item_info Additional item data. if Non-PSA: this parameter must be set to NULL if PSA: (1) if NULL: the private/public keys will be generated and stored in the default key resident set in pre-build. (2) if `kcm_item_extra_info_s`: the private/public keys will be generated and stored in the selected resident defined in `::kcm_item_extra_info_s`. @returns ::KCM_STATUS_SUCCESS in case of success. Otherwise, one of the `::kcm_status_e` errors.
kcm_status_e kcm_generate_random | ( | uint8_t * | buffer, |
size_t | buffer_size | ||
) |
Generates a random number into a given buffer of a given size in bytes.
The function returns an error if entropy is expected and the function is called before entropy was injected.
[out] | buffer | A pointer to a buffer that holds the generated number. |
[in] | buffer_size | The size of the buffer and the size of the required random number to generate. |
kcm_status_e
errors otherwise. kcm_status_e kcm_init | ( | void | ) |
Initiates the KCM module. Allocates and initializes file storage resources.
kcm_status_e
errors otherwise. kcm_status_e kcm_item_delete | ( | const uint8_t * | kcm_item_name, |
size_t | kcm_item_name_len, | ||
kcm_item_type_e | kcm_item_type | ||
) |
Deletes a KCM item from a secure storage.
[in] | kcm_item_name | KCM item name. |
[in] | kcm_item_name_len | KCM item name length. |
[in] | kcm_item_type | KCM item type as defined in kcm_item_type_e . |
kcm_status_e kcm_item_get_data | ( | const uint8_t * | kcm_item_name, |
size_t | kcm_item_name_len, | ||
kcm_item_type_e | kcm_item_type, | ||
uint8_t * | kcm_item_data_out, | ||
size_t | kcm_item_data_max_size, | ||
size_t * | kcm_item_data_act_size_out | ||
) |
Retrieves KCM item data from secure storage. In PSA mode (MBED_CONF_MBED_CLOUD_CLIENT_PSA_SUPPORT is on), KCM_PRIVATE_KEY_ITEM type is not supported.
[in] | kcm_item_name | KCM item name. |
[in] | kcm_item_name_len | KCM item name length. |
[in] | kcm_item_type | KCM item type as defined in kcm_item_type_e . |
[out] | kcm_item_data_out | KCM item data output buffer. Can be NULL if kcm_item_data_size is 0. |
[in] | kcm_item_data_max_size | The maximum size of the KCM item data output buffer in bytes. |
[out] | kcm_item_data_act_size_out | Actual KCM item data output buffer size in bytes. |
kcm_status_e
errors otherwise. kcm_status_e kcm_item_get_data_size | ( | const uint8_t * | kcm_item_name, |
size_t | kcm_item_name_len, | ||
kcm_item_type_e | kcm_item_type, | ||
size_t * | kcm_item_data_size_out | ||
) |
Retrieves the KCM item data size from secure storage. In PSA mode (MBED_CONF_MBED_CLOUD_CLIENT_PSA_SUPPORT is on), KCM_PRIVATE_KEY_ITEM type is not supported.
[in] | kcm_item_name | KCM item name. |
[in] | kcm_item_name_len | KCM item name length. |
[in] | kcm_item_type | KCM item type as defined in kcm_item_type_e . |
[out] | kcm_item_data_size_out | KCM item data size in bytes. |
kcm_status_e
errors otherwise. kcm_status_e kcm_item_get_size_and_data | ( | const uint8_t * | kcm_item_name, |
size_t | kcm_item_name_len, | ||
kcm_item_type_e | kcm_item_type, | ||
uint8_t ** | kcm_item_data_out, | ||
size_t * | kcm_item_data_size_out | ||
) |
Retrieves KCM item data and its size from secure storage. The buffer for the data is allocated internally and the caller is responsible to free it. If kcm_status_e` error returned, no need to free the buffer In PSA mode (MBED_CONF_MBED_CLOUD_CLIENT_PSA_SUPPORT is on), KCM_PRIVATE_KEY_ITEM type is not supported.
[in] | kcm_item_name | KCM item name. |
[in] | kcm_item_name_len | KCM item name length. |
[in] | kcm_item_type | KCM item type as defined in kcm_item_type_e . |
[out] | kcm_item_data_out | KCM item data output buffer. The buffer allocated internally. |
[out] | kcm_item_data_size_out | KCM item data output buffer size in bytes. |
kcm_status_e
errors otherwise. kcm_status_e kcm_item_store | ( | const uint8_t * | kcm_item_name, |
size_t | kcm_item_name_len, | ||
kcm_item_type_e | kcm_item_type, | ||
bool | kcm_item_is_factory, | ||
const uint8_t * | kcm_item_data, | ||
size_t | kcm_item_data_size, | ||
const kcm_security_desc_s | kcm_item_info | ||
) |
Stores a KCM item in secure storage.
When MBED_CONF_MBED_CLOUD_CLIENT_PSA_SUPPORT
is on, the `KCM_PRIVATE_KEY_ITEM and KCM_PUBLIC_KEY_ITEM
types are stored in PSA storage.
Item name restrictions (the kcm_item_name argument): kcm_item_name must only include the following characters: a
-z
, A
-Z
, 0
-9
, _
, -
, .
.
[in] | kcm_item_name | KCM item name. See comment above. |
[in] | kcm_item_name_len | KCM item name length. kcm_item_name_len must be at most KCM_MAX_FILENAME_SIZE bytes. |
[in] | kcm_item_type | KCM item type as defined in kcm_item_type_e . |
[in] | kcm_item_is_factory | True if the KCM item is a factory item; otherwise, false. |
[in] | kcm_item_data | KCM item data buffer. Can be NULL if kcm_item_data_size is 0. |
[in] | kcm_item_data_size | KCM item data buffer size in bytes. Can be 0 if you want to store an empty file. |
[in] | kcm_item_info | Security descriptor, caller must set this to NULL. |
kcm_status_e
errors otherwise. kcm_status_e kcm_key_pair_generate_and_store | ( | const kcm_crypto_key_scheme_e | key_scheme, |
const uint8_t * | private_key_name, | ||
size_t | private_key_name_len, | ||
const uint8_t * | public_key_name, | ||
size_t | public_key_name_len, | ||
bool | kcm_item_is_factory, | ||
const kcm_security_desc_s | kcm_item_info | ||
) |
Generates a key pair that complies with the given cryptographic scheme in DER format. Saves the private and public key, if provided.
@param[in] key_scheme The cryptographic scheme. @param[in] private_key_name The private key name for which a key pair is generated. @param[in] private_key_name_len The length of the private key name. @param[in] public_key_name The public key name for which a key pair is generated. This parameter is optional. If not provided, the key is generated, but not stored. @param[in] public_key_name_len The length of the public key name. Must be 0, if `::public_key_name` is not provided. @param[in] kcm_item_is_factory True if the KCM item is a factory item; otherwise, it is false. @param[in] kcm_item_info Additional item data. if Non-PSA: this parameter must be set to NULL if PSA: (1) if NULL: the private/public keys will be generated and stored in the default key resident set in pre-build time. (2) if `kcm_item_extra_info_s`: the private/public keys will be generated and stored in the selected resident defined in `::kcm_item_extra_info_s`. @returns ::KCM_STATUS_SUCCESS in the event of success. Otherwise, one of the `::kcm_status_e` errors.