1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3 * Copyright 2023 NXP
4 */
5 #ifndef __CAAM_KEY_H__
6 #define __CAAM_KEY_H__
7
8 #include <caam_types.h>
9 #include <crypto/crypto.h>
10 #include <types_ext.h>
11
12 /*
13 * CAAM Key types
14 */
15 enum caam_key_type {
16 CAAM_KEY_PLAIN_TEXT = 0, /* Plain text key or red key */
17 CAAM_KEY_BLACK_ECB, /* Black key AES-ECB encrypted */
18 CAAM_KEY_BLACK_CCM, /* Black key AES-CCM encrypted */
19 CAAM_KEY_MAX_VALUE, /* Max value - not valid */
20 };
21
22 /*
23 * CAAM key structure
24 */
25 struct caamkey {
26 struct caambuf buf; /* Key buffer */
27 enum caam_key_type key_type; /* CAAM Key type */
28 size_t sec_size; /* Security key size */
29 bool is_blob; /* Shows if the key is in blob format */
30 };
31
32 /*
33 * Returns the default key type for CAAM key generation.
34 * The CAAM can only generate one key type.
35 */
caam_key_default_key_gen_type(void)36 static inline enum caam_key_type caam_key_default_key_gen_type(void)
37 {
38 return CAAM_KEY_BLACK_CCM;
39 }
40
41 /*
42 * Print CAAM Key structure
43 *
44 * @trace Additional log string
45 * @key Key to print
46 */
47 void caam_key_dump(const char *trace, const struct caamkey *key);
48
49 /*
50 * Allocate CAAM key buffer based on the CAAM key type, key security size, and
51 * whether it is in a blob format or not.
52 *
53 * @key CAAM key to allocate
54 */
55 enum caam_status caam_key_alloc(struct caamkey *key);
56
57 /*
58 * Free the CAAM key buffer
59 *
60 * @key CAAM key to free
61 */
62 void caam_key_free(struct caamkey *key);
63
64 /*
65 * Perform a cache operation on CAAM key buffer.
66 *
67 * @op Cache operation type
68 * @key CAAM key buffer to operate
69 */
70 void caam_key_cache_op(enum utee_cache_operation op, const struct caamkey *key);
71
72 /*
73 * Encapsulate or decapsulate the given CAAM key
74 *
75 * @in_key CAAM Key to encapsulate or decapsulate
76 * @out_key CAAM Key operation result. The out_key is allocated by the function.
77 */
78 enum caam_status caam_key_operation_blob(const struct caamkey *in_key,
79 struct caamkey *out_key);
80
81 /*
82 * Deserialize CAAM key structure from binary buffer
83 *
84 * @data Buffer input
85 * @size Buffer input size
86 * @key CAAM key structure to populate
87 * @sec_size Security key size to deserialize, optional. If not needed,
88 * set it to 0.
89 */
90 enum caam_status caam_key_deserialize_from_bin(uint8_t *data, size_t size,
91 struct caamkey *key,
92 size_t sec_size);
93
94 /*
95 * Serialize CAAM key structure to binary buffer
96 *
97 * @data Buffer output
98 * @size Buffer output size
99 * @key CAAM key structure to serialize
100 */
101 enum caam_status caam_key_serialize_to_bin(uint8_t *data, size_t size,
102 const struct caamkey *key);
103
104 /*
105 * Deserialize CAAM key structure from bignum
106 *
107 * @inkey Bignum input
108 * @outkey CAAM key structure to populate
109 * @size_sec Security key size to deserialize, optional. If not needed,
110 * set it to zero.
111 */
112 enum caam_status caam_key_deserialize_from_bn(const struct bignum *inkey,
113 struct caamkey *outkey,
114 size_t size_sec);
115
116 /*
117 * Serialize CAAM key structure to bignum
118 *
119 * @outkey Bignum output
120 * @inkey CAAM key structure to serialize
121 */
122 enum caam_status caam_key_serialize_to_bn(struct bignum *outkey,
123 const struct caamkey *inkey);
124
125 /*
126 * Return the key buffer size needed given the CAAM key type, key security size,
127 * and whether it is in a blob format or not
128 *
129 * @key CAAM key structure input
130 */
131 size_t caam_key_get_alloc_size(const struct caamkey *key);
132
133 /*
134 * Return the buffer size needed to serialize the given CAAM key structure
135 *
136 * @key CAAM Key structure to serialize
137 * @size returned buffer size
138 */
139 enum caam_status caam_key_serialized_size(const struct caamkey *key,
140 size_t *size);
141
142 /*
143 * Encapsulate a plain text key to CAAM black key.
144 *
145 * @key CAAM key to encapsulate
146 * @key_type CAAM key encapsulation type
147 */
148 enum caam_status caam_key_black_encapsulation(struct caamkey *key,
149 enum caam_key_type key_type);
150
151 /*
152 * CAAM Key initialization
153 */
154 enum caam_status caam_key_init(void);
155 #endif /* __CAAM_KEY_H__ */
156