1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2018-2020, Linaro Limited 4 */ 5 6 #ifndef PKCS11_HELPERS_H 7 #define PKCS11_HELPERS_H 8 9 #include <stdint.h> 10 #include <stddef.h> 11 12 #include <token_capabilities.h> 13 14 /* 15 * TEE invocation parameter#0 is an in/out buffer of at least 32bit 16 * to store the TA PKCS#11 compliant return value. 17 */ 18 #define TEE_PARAM0_SIZE_MIN sizeof(uint32_t) 19 20 /* GPD TEE to PKCS11 status conversion */ 21 enum pkcs11_rc tee2pkcs_error(TEE_Result res); 22 23 /* 24 * Return true if and only if attribute ID with companion attribute value 25 * size do match a valid attribute identifier. 26 * 27 * @attribute_id - Target PKCS11 attribute ID 28 * @size - Byte size of the attribute value, 0 if non-constant size 29 */ 30 bool valid_pkcs11_attribute_id(uint32_t attribute_id, uint32_t size); 31 32 /* 33 * Return type attribute byte size if @attribute_id is the ID of a type 34 * attribute or 0 if not. 35 */ 36 size_t pkcs11_attr_is_type(uint32_t attribute_id); 37 38 /* Return true if the object class is related to a type-in-class */ 39 bool pkcs11_class_has_type(uint32_t class_id); 40 41 /* Return true if the object class relates to a key */ 42 bool pkcs11_attr_class_is_key(uint32_t class_id); 43 44 /* Return true if the key type @key_type_id relates to a symmetric key */ 45 bool key_type_is_symm_key(uint32_t key_type_id); 46 47 /* Return true if the key type @key_type_id relates to an asymmetric key */ 48 bool key_type_is_asymm_key(uint32_t key_type_id); 49 50 /* Boolprop flag shift position if @attribute_id is boolean, else -1 */ 51 int pkcs11_attr2boolprop_shift(uint32_t attribute_id); 52 53 /* Return true if attribute is a boolean, false otherwise */ 54 static inline bool pkcs11_attr_is_boolean(enum pkcs11_attr_id id) 55 { 56 return pkcs11_attr2boolprop_shift(id) >= 0; 57 } 58 59 #if CFG_TEE_TA_LOG_LEVEL > 0 60 /* Id-to-string conversions only for trace support */ 61 const char *id2str_ta_cmd(uint32_t id); 62 const char *id2str_rc(uint32_t id); 63 const char *id2str_slot_flag(uint32_t id); 64 const char *id2str_token_flag(uint32_t id); 65 const char *id2str_session_flag(uint32_t id); 66 const char *id2str_session_state(uint32_t id); 67 const char *id2str_attr(uint32_t id); 68 const char *id2str_class(uint32_t id); 69 const char *id2str_type(uint32_t id, uint32_t class); 70 const char *id2str_key_type(uint32_t id); 71 const char *id2str_attr_value(uint32_t id, size_t size, void *value); 72 const char *id2str_proc(uint32_t id); 73 const char *id2str_function(uint32_t id); 74 75 static inline const char *id2str_mechanism(enum pkcs11_mechanism_id id) 76 { 77 return mechanism_string_id(id); 78 } 79 #endif /* CFG_TEE_TA_LOG_LEVEL > 0 */ 80 #endif /*PKCS11_HELPERS_H*/ 81