cb2477c2e59b7a691a190c9dd0ec287a485b446a
[openwrt/staging/aparcar.git] /
1 From b2fb5441c4613465e0c8f9203cdec4f8083d2fdd Mon Sep 17 00:00:00 2001
2 From: Franck LENORMAND <franck.lenormand@nxp.com>
3 Date: Fri, 5 Oct 2018 16:08:25 +0200
4 Subject: [PATCH] MLK-19801-1 crypto: caam - add tag functionality
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Add functions to tag an object with metadata(configuration).
10
11 It is possible to:
12 - create metadata:
13 - init_tag_object_header
14 - init_blackey_conf
15 - set_tag_object_conf
16 - retrieve metadata:
17 - get_tag_object_conf
18 - get_blackey_conf
19
20 The API expects an object to be a space a memory
21 with an address and a size.
22
23 The implementation of the tag is currently exposed
24 but users shouldn't access it directly, they should
25 use the functions provided.
26
27 Signed-off-by: Franck LENORMAND <franck.lenormand@nxp.com>
28 (cherry picked from commit ebbb132da8e7f9de7f3d375eff8d87f684feb1eb)
29 Signed-off-by: Vipul Kumar <vipul_kumar@mentor.com>
30 (cherry picked from commit 8b6f6b4474be33ee271dfe2cce79f9f6335733aa)
31
32 -make tag functionality depend on JR
33 -change commit headline prefix
34
35 Signed-off-by: Horia Geantă <horia.geanta@nxp.com>
36 ---
37 drivers/crypto/caam/Kconfig | 10 ++
38 drivers/crypto/caam/Makefile | 1 +
39 drivers/crypto/caam/tag_object.c | 260 +++++++++++++++++++++++++++++++++++++++
40 drivers/crypto/caam/tag_object.h | 100 +++++++++++++++
41 4 files changed, 371 insertions(+)
42 create mode 100644 drivers/crypto/caam/tag_object.c
43 create mode 100644 drivers/crypto/caam/tag_object.h
44
45 --- a/drivers/crypto/caam/Kconfig
46 +++ b/drivers/crypto/caam/Kconfig
47 @@ -147,6 +147,16 @@ config CRYPTO_DEV_FSL_CAAM_RNG_API
48 Selecting this will register the SEC4 hardware rng to
49 the hw_random API for suppying the kernel entropy pool.
50
51 +config CRYPTO_DEV_FSL_CAAM_TK_API
52 + bool "Register tagged key cryptography implementations with Crypto API"
53 + depends on CRYPTO_DEV_FSL_CAAM_CRYPTO_API
54 + help
55 + Selecting this will register algorithms supporting tagged
56 + key.
57 +
58 + Tagged key are keys that contains metadata indicating what
59 + they are and how to handle them.
60 +
61 config CRYPTO_DEV_FSL_CAAM_RNG_TEST
62 bool "Test caam rng"
63 depends on CRYPTO_DEV_FSL_CAAM_RNG_API
64 --- a/drivers/crypto/caam/Makefile
65 +++ b/drivers/crypto/caam/Makefile
66 @@ -24,6 +24,7 @@ caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_PKC
67 caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_SM) += sm_store.o
68 caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_SM_TEST) += sm_test.o
69 caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_SECVIO) += secvio.o
70 +caam-jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_TK_API) += tag_object.o
71
72 caam-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI) += qi.o
73 ifneq ($(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI),)
74 --- /dev/null
75 +++ b/drivers/crypto/caam/tag_object.c
76 @@ -0,0 +1,260 @@
77 +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
78 +/*
79 + * Copyright 2018-2019 NXP
80 + */
81 +
82 +#include <linux/export.h>
83 +#include <linux/string.h>
84 +#include <linux/errno.h>
85 +
86 +#include "tag_object.h"
87 +#include "desc.h"
88 +
89 +/*
90 + * Magic number to clearly identify the structure is for us
91 + * 0x54 = 'T'
92 + * 0x61 = 'a'
93 + * 0x67 = 'g'
94 + * 0x4f = 'O'
95 + */
96 +#define TAG_OBJECT_MAGIC 0x5461674f
97 +
98 +/**
99 + * struct tagged_object - Structure representing a tagged object
100 + * @tag : The configuration of the data
101 + * @object : The object
102 + */
103 +struct tagged_object {
104 + struct tag_object_conf tag;
105 + char object;
106 +};
107 +
108 +/**
109 + * is_bk_type() - Determines if black key type.
110 + * @type: The type
111 + *
112 + * Return: True if black key type, False otherwise.
113 + */
114 +static bool is_bk_type(enum tag_type type)
115 +{
116 + return (type == TAG_TYPE_BLACK_KEY_ECB) ||
117 + (type == TAG_TYPE_BLACK_KEY_ECB_TRUSTED) ||
118 + (type == TAG_TYPE_BLACK_KEY_CCM) ||
119 + (type == TAG_TYPE_BLACK_KEY_CCM_TRUSTED);
120 +}
121 +
122 +/**
123 + * is_bk_conf() - Determines if black key conf.
124 + * @tag_obj_conf : The tag object conf
125 + *
126 + * Return: True if black key conf, False otherwise.
127 + */
128 +bool is_bk_conf(const struct tag_object_conf *tag_obj_conf)
129 +{
130 + return is_bk_type(tag_obj_conf->header.type);
131 +}
132 +EXPORT_SYMBOL(is_bk_conf);
133 +
134 +/**
135 + * get_bk_conf() - Gets the block conf.
136 + * @tag_obj_conf : The tag object conf
137 + *
138 + * Return: The block conf.
139 + */
140 +const struct blackey_conf *get_bk_conf(const struct tag_object_conf *tag_obj_conf)
141 +{
142 + return &tag_obj_conf->conf.bk_conf;
143 +}
144 +
145 +/**
146 + * get_tag_object_overhead() - Gets the tag object overhead.
147 + *
148 + * Return: The tag object overhead.
149 + */
150 +size_t get_tag_object_overhead(void)
151 +{
152 + return TAG_OVERHEAD;
153 +}
154 +EXPORT_SYMBOL(get_tag_object_overhead);
155 +
156 +/**
157 + * is_valid_type() - Determines if valid type.
158 + * @type : The type
159 + *
160 + * Return: True if valid type, False otherwise.
161 + */
162 +bool is_valid_type(enum tag_type type)
163 +{
164 + return (type > TAG_TYPE_NOT_SUPPORTED) && (type < NB_TAG_TYPE);
165 +}
166 +EXPORT_SYMBOL(is_valid_type);
167 +
168 +/**
169 + * is_valid_header() - Determines if valid header.
170 + * @header : The header
171 + *
172 + * Return: True if valid tag object conf, False otherwise.
173 + */
174 +static bool is_valid_header(const struct conf_header *header)
175 +{
176 + bool valid = header->_magic_number == TAG_OBJECT_MAGIC;
177 +
178 + valid = valid && is_valid_type(header->type);
179 +
180 + return valid;
181 +}
182 +
183 +/**
184 + * is_valid_tag_object_conf() - Determines if valid tag object conf.
185 + * @tag_obj_conf : The tag object conf
186 + *
187 + * Return: True if valid header, False otherwise.
188 + */
189 +bool is_valid_tag_object_conf(const struct tag_object_conf *tag_obj_conf)
190 +{
191 + bool valid = true;
192 +
193 + valid = is_valid_header(&tag_obj_conf->header);
194 +
195 + return valid;
196 +}
197 +EXPORT_SYMBOL(is_valid_tag_object_conf);
198 +
199 +/**
200 + * get_tag_object_conf() - Gets a pointer on the tag object conf.
201 + * @tag_obj_conf : The tag object conf
202 + * @buffer : The buffer
203 + * @size : The size
204 + *
205 + * Return: 0 if success, else error code
206 + */
207 +int get_tag_object_conf(void *buffer, size_t size,
208 + struct tag_object_conf **tag_obj_conf)
209 +{
210 + bool is_valid;
211 + struct tagged_object *tago = (struct tagged_object *)buffer;
212 + size_t conf_size = get_tag_object_overhead();
213 +
214 + /* Check we can retrieve the conf */
215 + if (size < conf_size)
216 + return -EINVAL;
217 +
218 + is_valid = is_valid_tag_object_conf(&tago->tag);
219 +
220 + *tag_obj_conf = &tago->tag;
221 +
222 + return (is_valid) ? 0 : -EINVAL;
223 +}
224 +EXPORT_SYMBOL(get_tag_object_conf);
225 +
226 +/**
227 + * init_tag_object_header() - Initialize the tag object header
228 + * @conf_header : The configuration header
229 + * @type : The type
230 + *
231 + * It initialize the header structure
232 + */
233 +void init_tag_object_header(struct conf_header *conf_header,
234 + enum tag_type type)
235 +{
236 + conf_header->_magic_number = TAG_OBJECT_MAGIC;
237 + conf_header->type = type;
238 +}
239 +EXPORT_SYMBOL(init_tag_object_header);
240 +
241 +/**
242 + * set_tag_object_conf() - Sets the tag object conf.
243 + * @tag_obj_conf : The tag object conf
244 + * @buffer : The buffer
245 + * @obj_size : The object size
246 + * @to_size : The tagged object size
247 + *
248 + * Return: 0 if success, else error code
249 + */
250 +int set_tag_object_conf(const struct tag_object_conf *tag_obj_conf,
251 + void *buffer, size_t obj_size, u32 *to_size)
252 +{
253 + struct tagged_object *tago = buffer;
254 + size_t conf_size = get_tag_object_overhead();
255 + size_t req_size = obj_size + conf_size;
256 +
257 + /* Check we can set the conf */
258 + if (*to_size < req_size) {
259 + *to_size = req_size;
260 + return -EINVAL;
261 + }
262 +
263 + /* Move the object */
264 + memmove(&tago->object, buffer, obj_size);
265 +
266 + /* Copy the tag */
267 + memcpy(&tago->tag, tag_obj_conf, conf_size);
268 +
269 + *to_size = req_size;
270 +
271 + return 0;
272 +}
273 +EXPORT_SYMBOL(set_tag_object_conf);
274 +
275 +/**
276 + * init_blackey_conf() - Initialize the black key configuration
277 + * @blackey_conf : The blackey conf
278 + * @len : The length
279 + * @ccm : The ccm
280 + * @tk : The trusted key
281 + *
282 + * It initialize the black key configuration structure
283 + */
284 +void init_blackey_conf(struct blackey_conf *blackey_conf,
285 + size_t len, bool ccm, bool tk)
286 +{
287 + blackey_conf->real_len = len;
288 + blackey_conf->load = KEY_ENC
289 + | ((ccm) ? KEY_EKT : 0)
290 + | ((tk) ? KEY_TK : 0);
291 +}
292 +EXPORT_SYMBOL(init_blackey_conf);
293 +
294 +/**
295 + * get_blackey_conf() - Get the black key configuration
296 + * @blackey_conf : The blackey conf
297 + * @real_len : The real length
298 + * @load_param : The load parameter
299 + *
300 + * It retrieve the black key configuration
301 + */
302 +void get_blackey_conf(const struct blackey_conf *blackey_conf,
303 + u32 *real_len, u32 *load_param)
304 +{
305 + *real_len = blackey_conf->real_len;
306 + *load_param = blackey_conf->load;
307 +}
308 +EXPORT_SYMBOL(get_blackey_conf);
309 +
310 +/**
311 + * get_tagged_data() - Get a pointer on the data and the size
312 + * @tagged_object : Pointer on the tagged object
313 + * @tagged_object_size : tagged object size in bytes
314 + * @data : Pointer on the data
315 + * @data_size : data size in bytes
316 + *
317 + * Return: 0 if success, else error code
318 + */
319 +int get_tagged_data(void *tagged_object, size_t tagged_object_size,
320 + void **data, u32 *data_size)
321 +{
322 + struct tagged_object *tago =
323 + (struct tagged_object *)tagged_object;
324 + size_t conf_size = get_tag_object_overhead();
325 +
326 + /* Check we can retrieve the object */
327 + if (tagged_object_size < conf_size)
328 + return -EINVAL;
329 +
330 + /* Retrieve the object */
331 + *data = &tago->object;
332 + *data_size = tagged_object_size - conf_size;
333 +
334 + return 0;
335 +}
336 +EXPORT_SYMBOL(get_tagged_data);
337 --- /dev/null
338 +++ b/drivers/crypto/caam/tag_object.h
339 @@ -0,0 +1,100 @@
340 +/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
341 +/*
342 + * Copyright 2018-2019 NXP
343 + */
344 +
345 +#ifndef _TAG_OBJECT_H_
346 +#define _TAG_OBJECT_H_
347 +
348 +#include <linux/types.h>
349 +
350 +#define TAG_MIN_SIZE (2 * sizeof(struct conf_header))
351 +#define TAG_OVERHEAD sizeof(struct tag_object_conf)
352 +
353 +/**
354 + * enum tag_type - Type of data represented by the tag
355 + */
356 +enum tag_type {
357 + /** @TAG_TYPE_NOT_SUPPORTED: The type is not supported */
358 + TAG_TYPE_NOT_SUPPORTED = 0,
359 +
360 + /* Type that passes is_tag_type_valid() */
361 + /** @TAG_TYPE_BLACK_KEY_ECB: Black key encrypted with ECB */
362 + TAG_TYPE_BLACK_KEY_ECB,
363 + /**
364 + * @TAG_TYPE_BLACK_KEY_ECB_TRUSTED: ECB Black key created by trusted
365 + * descriptor
366 + */
367 + TAG_TYPE_BLACK_KEY_ECB_TRUSTED,
368 + /** @TAG_TYPE_BLACK_KEY_CCM: Black key encrypted with CCM */
369 + TAG_TYPE_BLACK_KEY_CCM,
370 + /**
371 + * @TAG_TYPE_BLACK_KEY_CCM_TRUSTED: CCM Black key created by trusted
372 + * descriptor
373 + */
374 + TAG_TYPE_BLACK_KEY_CCM_TRUSTED,
375 +
376 + /** @NB_TAG_TYPE: Number of type of tag */
377 + NB_TAG_TYPE,
378 +};
379 +
380 +/**
381 + * struct conf_header - Common struture holding the type of data and the magic
382 + * number
383 + * @_magic_number : A magic number to identify the structure
384 + * @type : The type of data contained
385 + */
386 +struct conf_header {
387 + u32 _magic_number;
388 + u32 type;
389 +};
390 +
391 +/**
392 + * struct blackey_conf - Configuration for a black key
393 + * @load : Load parameter for CAAM
394 + * @real_len : Length of the key before encryption
395 + */
396 +struct blackey_conf {
397 + u32 load;
398 + u32 real_len;
399 +};
400 +
401 +/**
402 + * struct tag_object_conf - Common structure which is the tag applied to data
403 + * @header : Part of the data initialized with common function
404 + * :c:func:`init_tag_object_header`
405 + * @conf : Configuration data about the object tagged, initialized with
406 + * specific function
407 + */
408 +struct tag_object_conf {
409 + struct conf_header header;
410 + union {
411 + struct blackey_conf bk_conf;
412 + } conf;
413 +};
414 +
415 +bool is_bk_conf(const struct tag_object_conf *tag_obj_conf);
416 +
417 +bool is_valid_tag_object_conf(const struct tag_object_conf *tag_obj_conf);
418 +
419 +void init_tag_object_header(struct conf_header *conf_header,
420 + enum tag_type type);
421 +
422 +int get_tag_object_conf(void *buffer, size_t buffer_size,
423 + struct tag_object_conf **tag_obj_conf);
424 +
425 +int set_tag_object_conf(const struct tag_object_conf *tag_obj_conf,
426 + void *buffer, size_t obj_size, u32 *to_size);
427 +
428 +size_t get_tag_object_overhead(void);
429 +
430 +void get_blackey_conf(const struct blackey_conf *blackey_conf,
431 + u32 *real_len, u32 *load_param);
432 +
433 +void init_blackey_conf(struct blackey_conf *blackey_conf,
434 + size_t len, bool ccm, bool tk);
435 +
436 +int get_tagged_data(void *buffer, size_t buffer_size,
437 + void **data, u32 *data_size);
438 +
439 +#endif /* _TAG_OBJECT_H_ */