xref: /optee_os/ta/pkcs11/src/pkcs11_helpers.h (revision 145ae446b961c32d566596c90d6a9e167c6591e0)
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 <pkcs11_ta.h>
10 #include <stdint.h>
11 #include <stddef.h>
12 #include <tee_internal_api.h>
13 
14 #include <pkcs11_attributes.h>
15 #include <token_capabilities.h>
16 
17 struct pkcs11_object;
18 
19 /*
20  * TEE invocation parameter#0 is an in/out buffer of at least 32bit
21  * to store the TA PKCS#11 compliant return value.
22  */
23 #define TEE_PARAM0_SIZE_MIN		sizeof(uint32_t)
24 
25 /* GPD TEE to PKCS11 status conversion */
26 enum pkcs11_rc tee2pkcs_error(TEE_Result res);
27 
28 /*
29  * Return true if and only if attribute ID with companion attribute value
30  * size do match a valid attribute identifier.
31  *
32  * @attribute_id - Target PKCS11 attribute ID
33  * @size - Byte size of the attribute value, 0 if non-constant size
34  */
35 bool valid_pkcs11_attribute_id(uint32_t attribute_id, uint32_t size);
36 
37 /*
38  * Return type attribute byte size if @attribute_id is the ID of a type
39  * attribute or 0 if not.
40  */
41 size_t pkcs11_attr_is_type(uint32_t attribute_id);
42 
43 /* Return true if the object class is related to a type-in-class */
44 bool pkcs11_class_has_type(uint32_t class_id);
45 
46 /* Return true if the object class relates to a key */
47 bool pkcs11_attr_class_is_key(uint32_t class_id);
48 
49 /* Return true if the key type @key_type_id relates to a symmetric key */
50 bool key_type_is_symm_key(uint32_t key_type_id);
51 
52 /* Return true if the key type @key_type_id relates to an asymmetric key */
53 bool key_type_is_asymm_key(uint32_t key_type_id);
54 
55 /* Boolprop flag shift position if @attribute_id is boolean, else -1 */
56 int pkcs11_attr2boolprop_shift(uint32_t attribute_id);
57 
58 /* Convert PKCS11 TA function ID into a TEE crypto operation mode */
59 void pkcs2tee_mode(uint32_t *tee_id, enum processing_func function);
60 
61 /* Load TEE operation attributes from a PKCS11 object, return false on error */
62 bool pkcs2tee_load_attr(TEE_Attribute *tee_ref, uint32_t tee_id,
63 			struct pkcs11_object *obj,
64 			enum pkcs11_attr_id pkcs11_id);
65 
66 /* Hash and load TEE operation attributes from a PKCS11 object */
67 enum pkcs11_rc pkcs2tee_load_hashed_attr(TEE_Attribute *tee_ref,
68 					 uint32_t tee_id,
69 					 struct pkcs11_object *obj,
70 					 enum pkcs11_attr_id pkcs11_id,
71 					 uint32_t tee_algo, void *hash_ptr,
72 					 uint32_t *hash_size);
73 
74 /* Return true if attribute is a boolean, false otherwise */
75 static inline bool pkcs11_attr_is_boolean(enum pkcs11_attr_id id)
76 {
77 	return pkcs11_attr2boolprop_shift(id) >= 0;
78 }
79 
80 #if CFG_TEE_TA_LOG_LEVEL > 0
81 /* Id-to-string conversions only for trace support */
82 const char *id2str_ta_cmd(uint32_t id);
83 const char *id2str_rc(uint32_t id);
84 const char *id2str_slot_flag(uint32_t id);
85 const char *id2str_token_flag(uint32_t id);
86 const char *id2str_session_flag(uint32_t id);
87 const char *id2str_session_state(uint32_t id);
88 const char *id2str_attr(uint32_t id);
89 const char *id2str_class(uint32_t id);
90 const char *id2str_type(uint32_t id, uint32_t class);
91 const char *id2str_key_type(uint32_t id);
92 const char *id2str_attr_value(uint32_t id, size_t size, void *value);
93 const char *id2str_proc(uint32_t id);
94 const char *id2str_function(uint32_t id);
95 
96 static inline const char *id2str_mechanism(enum pkcs11_mechanism_id id)
97 {
98 	return mechanism_string_id(id);
99 }
100 #endif /* CFG_TEE_TA_LOG_LEVEL > 0 */
101 #endif /*PKCS11_HELPERS_H*/
102