OPTIGA Trust M  1.1.0
C++ library for Optiga Trust M Chip Security Controller
optiga_crypt.h
Go to the documentation of this file.
1 
38 #ifndef _OPTIGA_CRYPT_H_
39 #define _OPTIGA_CRYPT_H_
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 #include "optiga_cmd.h"
46 
48 typedef union optiga_crypt_params
49 {
67 
70 {
80  uint16_t instance_state;
81 #ifdef OPTIGA_COMMS_SHIELDED_CONNECTION
82  uint8_t protection_level;
86 #endif //OPTIGA_COMMS_SHIELDED_CONNECTION
87 
88 };
89 
92 
93 #ifdef OPTIGA_COMMS_SHIELDED_CONNECTION
94 
121  uint8_t configuration_type,
122  uint8_t value);
123 #endif
124 
152 LIBRARY_EXPORTS optiga_crypt_t * optiga_crypt_create(uint8_t optiga_instance_id,
154  void * caller_context);
155 
178 
179 #ifdef OPTIGA_CRYPT_RANDOM_ENABLED
180 
216  optiga_rng_type_t rng_type,
217  uint8_t * random_data,
218  uint16_t random_data_length);
219 #endif //OPTIGA_CRYPT_RANDOM_ENABLED
220 
221 #ifdef OPTIGA_CRYPT_HASH_ENABLED
222 
256  optiga_hash_context_t * hash_ctx);
257 
258 
294  optiga_hash_context_t * hash_ctx,
295  uint8_t source_of_data_to_hash,
296  const void * data_to_hash);
297 
332  optiga_hash_context_t * hash_ctx,
333  uint8_t * hash_output);
334 #endif //OPTIGA_CRYPT_HASH_ENABLED
335 
336 
337 #ifdef OPTIGA_CRYPT_ECC_GENERATE_KEYPAIR_ENABLED
338 
380  optiga_ecc_curve_t curve_id,
381  uint8_t key_usage,
382  bool_t export_private_key,
383  void * private_key,
384  uint8_t * public_key,
385  uint16_t * public_key_length);
386 #endif //OPTIGA_CRYPT_ECC_GENERATE_KEYPAIR_ENABLED
387 
388 #ifdef OPTIGA_CRYPT_ECDSA_SIGN_ENABLED
389 
425  const uint8_t * digest,
426  uint8_t digest_length,
427  optiga_key_id_t private_key,
428  uint8_t * signature,
429  uint16_t * signature_length);
430 #endif //OPTIGA_CRYPT_ECDSA_SIGN_ENABLED
431 
432 #ifdef OPTIGA_CRYPT_ECDSA_VERIFY_ENABLED
433 
471  const uint8_t * digest,
472  uint8_t digest_length,
473  const uint8_t * signature,
474  uint16_t signature_length,
475  uint8_t public_key_source_type,
476  const void * public_key);
477 #endif //OPTIGA_CRYPT_ECDSA_VERIFY_ENABLED
478 
479 #ifdef OPTIGA_CRYPT_ECDH_ENABLED
480 
521  optiga_key_id_t private_key,
522  public_key_from_host_t * public_key,
523  bool_t export_to_host,
524  uint8_t * shared_secret);
525 #endif //OPTIGA_CRYPT_ECDH_ENABLED
526 
527 #ifdef OPTIGA_CRYPT_TLS_PRF_SHA256_ENABLED
528 
574  uint16_t secret,
575  const uint8_t * label,
576  uint16_t label_length,
577  const uint8_t * seed,
578  uint16_t seed_length,
579  uint16_t derived_key_length,
580  bool_t export_to_host,
581  uint8_t * derived_key);
582 #endif //OPTIGA_CRYPT_TLS_PRF_SHA256_ENABLED
583 
584 #ifdef OPTIGA_CRYPT_RSA_GENERATE_KEYPAIR_ENABLED
585 
629  optiga_rsa_key_type_t key_type,
630  uint8_t key_usage,
631  bool_t export_private_key,
632  void * private_key,
633  uint8_t * public_key,
634  uint16_t * public_key_length);
635 #endif //OPTIGA_CRYPT_RSA_GENERATE_KEYPAIR_ENABLED
636 
637 
638 #ifdef OPTIGA_CRYPT_RSA_SIGN_ENABLED
639 
675  optiga_rsa_signature_scheme_t signature_scheme,
676  const uint8_t * digest,
677  uint8_t digest_length,
678  optiga_key_id_t private_key,
679  uint8_t * signature,
680  uint16_t * signature_length,
681  uint16_t salt_length);
682 #endif //OPTIGA_CRYPT_RSA_SIGN_ENABLED
683 
684 #ifdef OPTIGA_CRYPT_RSA_VERIFY_ENABLED
685 
725  optiga_rsa_signature_scheme_t signature_scheme,
726  const uint8_t * digest,
727  uint8_t digest_length,
728  const uint8_t * signature,
729  uint16_t signature_length,
730  uint8_t public_key_source_type,
731  const void * public_key,
732  uint16_t salt_length);
733 #endif //OPTIGA_CRYPT_RSA_VERIFY_ENABLED
734 
735 #ifdef OPTIGA_CRYPT_RSA_PRE_MASTER_SECRET_ENABLED
736 
779  const uint8_t * optional_data,
780  uint16_t optional_data_length,
781  uint16_t pre_master_secret_length);
782 #endif //OPTIGA_CRYPT_RSA_PRE_MASTER_SECRET_ENABLED
783 
784 #ifdef OPTIGA_CRYPT_RSA_ENCRYPT_ENABLED
785 
830  optiga_rsa_encryption_scheme_t encryption_scheme,
831  const uint8_t * message,
832  uint16_t message_length,
833  const uint8_t * label,
834  uint16_t label_length,
835  uint8_t public_key_source_type,
836  const void * public_key,
837  uint8_t * encrypted_message,
838  uint16_t * encrypted_message_length);
839 
884  optiga_rsa_encryption_scheme_t encryption_scheme,
885  const uint8_t * label,
886  uint16_t label_length,
887  uint8_t public_key_source_type,
888  const void * public_key,
889  uint8_t * encrypted_message,
890  uint16_t * encrypted_message_length);
891 
892 #endif //OPTIGA_CRYPT_RSA_ENCRYPT_ENABLED
893 
894 #ifdef OPTIGA_CRYPT_RSA_DECRYPT_ENABLED
895 
936  optiga_rsa_encryption_scheme_t encryption_scheme,
937  const uint8_t * encrypted_message,
938  uint16_t encrypted_message_length,
939  const uint8_t * label,
940  uint16_t label_length,
941  optiga_key_id_t private_key,
942  uint8_t * message,
943  uint16_t * message_length);
944 
982  optiga_rsa_encryption_scheme_t encryption_scheme,
983  const uint8_t * encrypted_message,
984  uint16_t encrypted_message_length,
985  const uint8_t * label,
986  uint16_t label_length,
987  optiga_key_id_t private_key);
988 
989 #endif //OPTIGA_CRYPT_RSA_DECRYPT_ENABLED
990 
1016 #ifdef OPTIGA_COMMS_SHIELDED_CONNECTION
1017 #define OPTIGA_CRYPT_SET_COMMS_PROTECTION_LEVEL(p_instance, protection_level) \
1018 { \
1019  optiga_crypt_set_comms_params(p_instance, \
1020  OPTIGA_COMMS_PROTECTION_LEVEL, \
1021  protection_level);\
1022 }
1023 #else
1024 #define OPTIGA_CRYPT_SET_COMMS_PROTECTION_LEVEL(p_instance, protection_level) {}
1025 #endif
1026 
1044 #ifdef OPTIGA_COMMS_SHIELDED_CONNECTION
1045 #define OPTIGA_CRYPT_SET_COMMS_PROTOCOL_VERSION(p_instance, version) \
1046 { \
1047  optiga_crypt_set_comms_params(p_instance, \
1048  OPTIGA_COMMS_PROTOCOL_VERSION, \
1049  version);\
1050 }
1051 #else
1052 #define OPTIGA_CRYPT_SET_COMMS_PROTOCOL_VERSION(p_instance, version) {}
1053 #endif
1054 
1055 #if defined (OPTIGA_LIB_ENABLE_LOGGING) && defined (OPTIGA_LIB_ENABLE_CRYPT_LOGGING)
1056 
1070 #define OPTIGA_CRYPT_LOG_MESSAGE(msg) \
1071 {\
1072  optiga_lib_print_message(msg,OPTIGA_CRYPT_SERVICE,OPTIGA_CRYPT_SERVICE_COLOR);\
1073 }
1074 
1090 #define OPTIGA_CRYPT_LOG_HEX_DATA(array,array_len) \
1091 {\
1092  optiga_lib_print_array_hex_format(array,array_len,OPTIGA_UNPROTECTED_DATA_COLOR);\
1093 }
1094 
1109 #define OPTIGA_CRYPT_LOG_STATUS(return_value) \
1110 { \
1111  if (OPTIGA_LIB_SUCCESS != return_value) \
1112  { \
1113  optiga_lib_print_status(OPTIGA_CRYPT_SERVICE,OPTIGA_ERROR_COLOR,return_value); \
1114  } \
1115  else\
1116  { \
1117  optiga_lib_print_status(OPTIGA_CRYPT_SERVICE,OPTIGA_CRYPT_SERVICE_COLOR,return_value); \
1118  } \
1119 }
1120 #else
1121 
1122 #define OPTIGA_CRYPT_LOG_MESSAGE(msg) {}
1123 #define OPTIGA_CRYPT_LOG_HEX_DATA(array, array_len) {}
1124 #define OPTIGA_CRYPT_LOG_STATUS(return_value) {}
1125 
1126 #endif
1127 
1128 #ifdef __cplusplus
1129 }
1130 #endif
1131 
1132 #endif /*_OPTIGA_CRYPT_H_*/
1133 
Specifies the data structure of the Public Key details (key, size and type)
Definition: optiga_lib_common.h:236
optiga_encrypt_asym_params_t optiga_encrypt_asym_params
asymmetric encryption params
Definition: optiga_crypt.h:59
void optiga_crypt_set_comms_params(optiga_crypt_t *me, uint8_t configuration_type, uint8_t value)
Sets/updates the OPTIGA Comms Shielded connection configuration in the respective (optiga_util) insta...
Definition: optiga_crypt.c:375
LIBRARY_EXPORTS optiga_lib_status_t optiga_crypt_ecdh(optiga_crypt_t *me, optiga_key_id_t private_key, public_key_from_host_t *public_key, bool_t export_to_host, uint8_t *shared_secret)
Calculates the shared secret using ECDH algorithm.
Definition: optiga_crypt.c:718
optiga_cmd_t * my_cmd
Command module instance.
Definition: optiga_crypt.h:74
optiga_calc_sign_params_t optiga_calc_sign_params
calc sign params
Definition: optiga_crypt.h:55
optiga_calc_ssec_params_t optiga_calc_ssec_params
calc ssec params
Definition: optiga_crypt.h:63
enum optiga_rsa_signature_scheme optiga_rsa_signature_scheme_t
Specifies the RSA signature schemes type in OPTIGA.
optiga_crypt_params_t params
Details/references (pointers) to the Application Inputs.
Definition: optiga_crypt.h:72
callback_handler_t handler
Callback handler.
Definition: optiga_crypt.h:78
LIBRARY_EXPORTS optiga_lib_status_t optiga_crypt_random(optiga_crypt_t *me, optiga_rng_type_t rng_type, uint8_t *random_data, uint16_t random_data_length)
Generates a random number.
Definition: optiga_crypt.c:466
LIBRARY_EXPORTS optiga_lib_status_t optiga_crypt_tls_prf_sha256(optiga_crypt_t *me, uint16_t secret, const uint8_t *label, uint16_t label_length, const uint8_t *seed, uint16_t seed_length, uint16_t derived_key_length, bool_t export_to_host, uint8_t *derived_key)
Derives a key.
Definition: optiga_crypt.c:773
uint8_t protection_level
To provide the encryption and decryption need for command and response.
Definition: optiga_crypt.h:83
optiga_verify_sign_params_t optiga_verify_sign_params
verify sign params
Definition: optiga_crypt.h:57
LIBRARY_EXPORTS optiga_lib_status_t optiga_crypt_rsa_generate_pre_master_secret(optiga_crypt_t *me, const uint8_t *optional_data, uint16_t optional_data_length, uint16_t pre_master_secret_length)
Generates a pre-master secret.
Definition: optiga_crypt.c:1066
optiga_gen_keypair_params_t optiga_gen_keypair_params
get key pair params
Definition: optiga_crypt.h:53
enum optiga_rsa_key_type optiga_rsa_key_type_t
Specifies the RSA key type in OPTIGA.
LIBRARY_EXPORTS optiga_lib_status_t optiga_crypt_rsa_decrypt_and_store(optiga_crypt_t *me, optiga_rsa_encryption_scheme_t encryption_scheme, const uint8_t *encrypted_message, uint16_t encrypted_message_length, const uint8_t *label, uint16_t label_length, optiga_key_id_t private_key)
Decrypts input data using OPTIGA private key and stores it in a OPTIGA session.
Definition: optiga_crypt.c:1028
Specifies the structure for derivation of key using pseudo random function.
Definition: optiga_lib_common.h:413
optiga_calc_hash_params_t optiga_calc_hash_params
calc hash params
Definition: optiga_crypt.h:61
LIBRARY_EXPORTS optiga_lib_status_t optiga_crypt_destroy(optiga_crypt_t *me)
Destroys an instance of optiga_crypt_t.
Definition: optiga_crypt.c:440
uint8_t bool_t
Typedef for a boolean.
Definition: optiga_lib_types.h:91
enum optiga_ecc_curve optiga_ecc_curve_t
Specifies the key curve type in OPTIGA.
LIBRARY_EXPORTS optiga_lib_status_t optiga_crypt_hash_finalize(optiga_crypt_t *me, optiga_hash_context_t *hash_ctx, uint8_t *hash_output)
Finalizes and exports the hash output.
Definition: optiga_crypt.c:603
LIBRARY_EXPORTS optiga_lib_status_t optiga_crypt_rsa_decrypt_and_export(optiga_crypt_t *me, optiga_rsa_encryption_scheme_t encryption_scheme, const uint8_t *encrypted_message, uint16_t encrypted_message_length, const uint8_t *label, uint16_t label_length, optiga_key_id_t private_key, uint8_t *message, uint16_t *message_length)
Decrypts input data using OPTIGA private key and export it to the host.
Definition: optiga_crypt.c:986
LIBRARY_EXPORTS optiga_lib_status_t optiga_crypt_ecdsa_verify(optiga_crypt_t *me, const uint8_t *digest, uint8_t digest_length, const uint8_t *signature, uint16_t signature_length, uint8_t public_key_source_type, const void *public_key)
Verifies the signature over the given digest.
Definition: optiga_crypt.c:695
LIBRARY_EXPORTS optiga_lib_status_t optiga_crypt_rsa_generate_keypair(optiga_crypt_t *me, optiga_rsa_key_type_t key_type, uint8_t key_usage, bool_t export_private_key, void *private_key, uint8_t *public_key, uint16_t *public_key_length)
Generates an key pair based on RSA key type.
Definition: optiga_crypt.c:841
uint8_t protocol_version
To provide the presentation layer protocol version to be used.
Definition: optiga_crypt.h:85
uint16_t instance_state
To provide the busy/free status of the crypt instance.
Definition: optiga_crypt.h:80
Specifies the data structure for generate key pair.
Definition: optiga_lib_common.h:339
OPTIGA crypt instance structure.
Definition: optiga_crypt.h:69
Specifies the data structure for ecdh secret generation.
Definition: optiga_lib_common.h:398
Specifies the data structure for ECDSA signature verification.
Definition: optiga_lib_common.h:377
LIBRARY_EXPORTS optiga_lib_status_t optiga_crypt_rsa_sign(optiga_crypt_t *me, optiga_rsa_signature_scheme_t signature_scheme, const uint8_t *digest, uint8_t digest_length, optiga_key_id_t private_key, uint8_t *signature, uint16_t *signature_length, uint16_t salt_length)
Generates a RSA signature for the given digest based on the input signature scheme.
Definition: optiga_crypt.c:862
LIBRARY_EXPORTS optiga_lib_status_t optiga_crypt_hash_update(optiga_crypt_t *me, optiga_hash_context_t *hash_ctx, uint8_t source_of_data_to_hash, const void *data_to_hash)
Updates a hash context with the input data.
Definition: optiga_crypt.c:542
enum optiga_key_id optiga_key_id_t
Specifies the key location in OPTIGA.
optiga_get_random_params_t optiga_get_random_params
get random params
Definition: optiga_crypt.h:51
LIBRARY_EXPORTS optiga_lib_status_t optiga_crypt_ecdsa_sign(optiga_crypt_t *me, const uint8_t *digest, uint8_t digest_length, optiga_key_id_t private_key, uint8_t *signature, uint16_t *signature_length)
Generates a signature for the given digest.
Definition: optiga_crypt.c:675
enum optiga_rng_type optiga_rng_type_t
Specifies the random generation types.
Command context.
Definition: optiga_cmd.c:331
Specifies the structure for asymmetric encryption and decryption.
Definition: optiga_lib_common.h:434
LIBRARY_EXPORTS optiga_lib_status_t optiga_crypt_rsa_verify(optiga_crypt_t *me, optiga_rsa_signature_scheme_t signature_scheme, const uint8_t *digest, uint8_t digest_length, const uint8_t *signature, uint16_t signature_length, uint8_t public_key_source_type, const void *public_key, uint16_t salt_length)
Verifies the RSA signature over the given digest.
Definition: optiga_crypt.c:884
LIBRARY_EXPORTS optiga_lib_status_t optiga_crypt_hash_start(optiga_crypt_t *me, optiga_hash_context_t *hash_ctx)
Initializes a hash context.
Definition: optiga_crypt.c:495
Specifies the data structure of calculate hash.
Definition: optiga_lib_common.h:297
Specifies the data structure for ECDSA signature.
Definition: optiga_lib_common.h:360
void * caller_context
Caller context.
Definition: optiga_crypt.h:76
optiga_derive_key_params_t optiga_derive_key_params
derive key params
Definition: optiga_crypt.h:65
This file defines APIs, types and data structures used in the Command (cmd) module implementation.
LIBRARY_EXPORTS optiga_lib_status_t optiga_crypt_rsa_encrypt_session(optiga_crypt_t *me, optiga_rsa_encryption_scheme_t encryption_scheme, const uint8_t *label, uint16_t label_length, uint8_t public_key_source_type, const void *public_key, uint8_t *encrypted_message, uint16_t *encrypted_message_length)
Encrypts session data using RSA public key.
Definition: optiga_crypt.c:946
Specifies the structure to the Hash context details managed by OPTIGA.
Definition: optiga_lib_common.h:199
LIBRARY_EXPORTS optiga_lib_status_t optiga_crypt_ecc_generate_keypair(optiga_crypt_t *me, optiga_ecc_curve_t curve_id, uint8_t key_usage, bool_t export_private_key, void *private_key, uint8_t *public_key, uint16_t *public_key_length)
Generates an key pair based on ECC curves.
Definition: optiga_crypt.c:655
uint16_t optiga_lib_status_t
typedef for OPTIGA host library status
Definition: optiga_lib_types.h:97
LIBRARY_EXPORTS optiga_crypt_t * optiga_crypt_create(uint8_t optiga_instance_id, callback_handler_t handler, void *caller_context)
Create an instance of optiga_crypt_t.
Definition: optiga_crypt.c:399
enum optiga_rsa_encryption_scheme optiga_rsa_encryption_scheme_t
Specifies the RSA encryption schemes.
LIBRARY_EXPORTS optiga_lib_status_t optiga_crypt_rsa_encrypt_message(optiga_crypt_t *me, optiga_rsa_encryption_scheme_t encryption_scheme, const uint8_t *message, uint16_t message_length, const uint8_t *label, uint16_t label_length, uint8_t public_key_source_type, const void *public_key, uint8_t *encrypted_message, uint16_t *encrypted_message_length)
Encrypts message using RSA public key.
Definition: optiga_crypt.c:908
void(* callback_handler_t)(void *callback_ctx, optiga_lib_status_t event)
typedef for event callback handler
Definition: optiga_lib_types.h:106
union for OPTIGA crypt parameters
Definition: optiga_crypt.h:48
Specifies the data structure of random generation.
Definition: optiga_lib_common.h:323
union optiga_crypt_params optiga_crypt_params_t
union for OPTIGA crypt parameters