Mistake on this page? Email us
key_config_manager.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // Copyright 2016-2017 ARM Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 // ----------------------------------------------------------------------------
16 
17 #ifndef __KEYS_CONFIG_MANAGER_H__
18 #define __KEYS_CONFIG_MANAGER_H__
19 
20 #include <stdlib.h>
21 #include <stdbool.h>
22 #include <inttypes.h>
23 #include "kcm_status.h"
24 #include "kcm_defs.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
35  /* === Initialization and Finalization === */
36 
44  kcm_status_e kcm_init(void);
45 
54 
55  /* === Key, certificate, and configuration data storage === */
56 
80  kcm_status_e kcm_item_store(const uint8_t *kcm_item_name,
81  size_t kcm_item_name_len,
82  kcm_item_type_e kcm_item_type,
83  bool kcm_item_is_factory,
84  const uint8_t *kcm_item_data,
85  size_t kcm_item_data_size,
86  const kcm_security_desc_s kcm_item_info);
87 
88  /* === Key, certificate, and configuration data retrieval === */
89 
104  kcm_status_e kcm_item_get_data_size(const uint8_t *kcm_item_name,
105  size_t kcm_item_name_len,
106  kcm_item_type_e kcm_item_type,
107  size_t *kcm_item_data_size_out);
108 
125  kcm_status_e kcm_item_get_data(const uint8_t *kcm_item_name,
126  size_t kcm_item_name_len,
127  kcm_item_type_e kcm_item_type,
128  uint8_t *kcm_item_data_out,
129  size_t kcm_item_data_max_size,
130  size_t *kcm_item_data_act_size_out);
131 
149  kcm_status_e kcm_item_get_size_and_data(const uint8_t * kcm_item_name,
150  size_t kcm_item_name_len,
151  kcm_item_type_e kcm_item_type,
152  uint8_t ** kcm_item_data_out,
153  size_t * kcm_item_data_size_out);
154 
155 #ifdef MBED_CONF_MBED_CLOUD_CLIENT_PSA_SUPPORT
156 
157  /* === Key and Configuration Manager with Platform Secure Architecture (PSA) support uses PSA key IDs from 0x1 up to 0x2800 === */
158 
159 
177  kcm_status_e kcm_item_get_handle(const uint8_t *kcm_item_name,
178  size_t kcm_item_name_len,
179  kcm_item_type_e kcm_item_type,
180  kcm_key_handle_t *key_handle_out);
181 
191  kcm_status_e kcm_item_close_handle(kcm_key_handle_t *key_handle);
192 
193 #ifdef MBED_CONF_MBED_CLOUD_CLIENT_SECURE_ELEMENT_SUPPORT
194 
198 #define KCM_ITEM_EXTRA_INFO_INIT {KCM_LOCATION_PSA, KCM_LOCATION_PSA}
199  static inline kcm_item_extra_info_s kcm_item_extra_info_init(void)
200  {
201  const kcm_item_extra_info_s extra_info = KCM_ITEM_EXTRA_INFO_INIT;
202  return (extra_info);
203  }
204 
222  kcm_status_e kcm_item_get_location(const uint8_t *item_name,
223  size_t item_name_len,
224  kcm_item_type_e kcm_item_type,
225  kcm_item_location_e *item_location_out);
226 
227 
240  kcm_status_e kcm_se_private_key_get_slot(const uint8_t *prv_key_name,
241  size_t prv_key_name_len,
242  uint64_t *se_prv_key_slot);
243 
244 
245 #endif // #ifdef MBED_CONF_MBED_CLOUD_CLIENT_SECURE_ELEMENT_SUPPORT
246 #endif // #ifdef MBED_CONF_MBED_CLOUD_CLIENT_PSA_SUPPORT
247 
248  /* === Key, certificate, and configuration delete === */
249 
260  kcm_status_e kcm_item_delete(const uint8_t *kcm_item_name,
261  size_t kcm_item_name_len,
262  kcm_item_type_e kcm_item_type);
263 
264  /* === Certificate chain APIs === */
265 
279  kcm_status_e kcm_cert_chain_create(kcm_cert_chain_handle *kcm_chain_handle,
280  const uint8_t *kcm_chain_name,
281  size_t kcm_chain_name_len,
282  size_t kcm_chain_len,
283  bool kcm_chain_is_factory);
284 
306  kcm_status_e kcm_cert_chain_open(kcm_cert_chain_handle *kcm_chain_handle,
307  const uint8_t *kcm_chain_name,
308  size_t kcm_chain_name_len,
309  size_t *kcm_chain_len_out);
310 
327  kcm_status_e kcm_cert_chain_add_next(kcm_cert_chain_handle kcm_chain_handle,
328  const uint8_t *kcm_cert_data,
329  size_t kcm_cert_data_size);
330 
341  kcm_status_e kcm_cert_chain_delete(const uint8_t *kcm_chain_name,
342  size_t kcm_chain_name_len);
343 
357  kcm_status_e kcm_cert_chain_get_next_size(kcm_cert_chain_handle kcm_chain_handle,
358  size_t *kcm_cert_data_size);
359 
375  kcm_status_e kcm_cert_chain_get_next_data(kcm_cert_chain_handle kcm_chain_handle,
376  uint8_t *kcm_cert_data,
377  size_t kcm_max_cert_data_size,
378  size_t *kcm_actual_cert_data_size);
379 
380 
393  kcm_status_e kcm_cert_chain_close(kcm_cert_chain_handle kcm_chain_handle);
394 
395 
396  /* === Factory Reset === */
397 
406 
407 
432  const uint8_t *private_key_name,
433  size_t private_key_name_len,
434  const uint8_t *public_key_name,
435  size_t public_key_name_len,
436  bool kcm_item_is_factory,
437  const kcm_security_desc_s kcm_item_info);
438 
439 
454  kcm_status_e kcm_csr_generate(const uint8_t *private_key_name,
455  size_t private_key_name_len,
456  const kcm_csr_params_s *csr_params,
457  uint8_t *csr_buff_out,
458  size_t csr_buff_max_size,
459  size_t *csr_buff_act_size);
460 
461 
489  const uint8_t *private_key_name,
490  size_t private_key_name_len,
491  const uint8_t *public_key_name,
492  size_t public_key_name_len,
493  bool kcm_item_is_factory,
494  const kcm_csr_params_s *csr_params,
495  uint8_t *csr_buff_out,
496  size_t csr_buff_max_size,
497  size_t *csr_buff_act_size_out,
498  const kcm_security_desc_s kcm_item_info);
499 
516  kcm_status_e kcm_certificate_verify_with_private_key(const uint8_t *kcm_cert_data,
517  size_t kcm_cert_data_size,
518  const uint8_t *kcm_priv_key_name,
519  size_t kcm_priv_key_name_len);
520 
521 
545  const uint8_t *private_key_name,
546  size_t private_key_name_len,
547  const uint8_t *hash_digest,
548  size_t hash_digest_size,
549  uint8_t *signature_data_out,
550  size_t signature_data_max_size,
551  size_t *signature_data_act_size_out);
552 
553 
577  const uint8_t *public_key_name,
578  size_t public_key_name_len,
579  const uint8_t *hash_digest,
580  size_t hash_digest_size,
581  const uint8_t *signature,
582  size_t signature_size);
583 
597  kcm_status_e kcm_generate_random(uint8_t *buffer, size_t buffer_size);
598 
599  /* Computes a shared secret using the elliptic curve Diffie-Hellman algorithm.
600  *
601  * A few limitations to consider:
602  * (1) If a secure element exists, this function enables use of a single key only - ALG_ECDSA(ALG_SHA_256).
603  * (2) If PSA and secure element do not exist, this function enables use of multiple keys, except LPC55S69_NS and CY8CKIT_062_WIFI_BT_PSA targets.
604  *
605  * @param[in] private_key_name The private key name to fetch from storage.
606  * @param[in] private_key_name_len The length of the private key name.
607  * @param[in] peer_public_key The public key from a peer in DER format.
608  * @param[in] peer_public_key_size The length of the public key from a peer.
609  * @param[out] shared_secret A pointer to the output shared secret buffer.
610  * @param[in] shared_secret_max_size The size of the shared secret buffer. Must be at least ::KCM_EC_SECP256R1_SHARED_SECRET_SIZE bytes.
611  * @param[out] shared_secret_act_size_out The actual size of the shared secret buffer.
612  *
613  * @returns
614  * KCM_STATUS_SUCCESS on success.
615  * KCM_STATUS_INVALID_PARAMETER if one of the parameters is illegal.
616  * One of the `::kcm_status_e` errors otherwise.
617  */
618  kcm_status_e kcm_ecdh_key_agreement(
619  const uint8_t *private_key_name,
620  size_t private_key_name_len,
621  const uint8_t *peer_public_key,
622  size_t peer_public_key_size,
623  uint8_t *shared_secret,
624  size_t shared_secret_max_size,
625  size_t *shared_secret_act_size_out);
626 
627 #ifdef __cplusplus
628 }
629 #endif
630 
631 #endif //__KEYS_CONFIG_MANAGER_H__
kcm_crypto_key_scheme_e
Definition: kcm_defs.h:74
kcm_status_e kcm_cert_chain_close(kcm_cert_chain_handle kcm_chain_handle)
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)
Keys and configuration manager (KCM) status/error codes. This list may grow as needed.
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_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_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)
uintptr_t kcm_key_handle_t
Definition: kcm_defs.h:88
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_finalize(void)
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)
void * kcm_security_desc_s
Definition: kcm_defs.h:94
Definition: kcm_defs.h:104
kcm_status_e kcm_generate_random(uint8_t *buffer, size_t buffer_size)
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_cert_chain_get_next_size(kcm_cert_chain_handle kcm_chain_handle, size_t *kcm_cert_data_size)
kcm_item_type_e
Definition: kcm_defs.h:34
kcm_status_e kcm_factory_reset(void)
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)
Keys and configuration manager (KCM) definitions.
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_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
Definition: kcm_status.h:30
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_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_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_init(void)
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_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_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)