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
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 Add functions to tag an object with metadata(configuration).
13 - init_tag_object_header
20 The API expects an object to be a space a memory
21 with an address and a size.
23 The implementation of the tag is currently exposed
24 but users shouldn't access it directly, they should
25 use the functions provided.
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)
32 -make tag functionality depend on JR
33 -change commit headline prefix
35 Signed-off-by: Horia Geantă <horia.geanta@nxp.com>
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
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.
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
55 + Selecting this will register algorithms supporting tagged
58 + Tagged key are keys that contains metadata indicating what
59 + they are and how to handle them.
61 config CRYPTO_DEV_FSL_CAAM_RNG_TEST
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
72 caam-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI) += qi.o
73 ifneq ($(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI),)
75 +++ b/drivers/crypto/caam/tag_object.c
77 +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
79 + * Copyright 2018-2019 NXP
82 +#include <linux/export.h>
83 +#include <linux/string.h>
84 +#include <linux/errno.h>
86 +#include "tag_object.h"
90 + * Magic number to clearly identify the structure is for us
96 +#define TAG_OBJECT_MAGIC 0x5461674f
99 + * struct tagged_object - Structure representing a tagged object
100 + * @tag : The configuration of the data
101 + * @object : The object
103 +struct tagged_object {
104 + struct tag_object_conf tag;
109 + * is_bk_type() - Determines if black key type.
112 + * Return: True if black key type, False otherwise.
114 +static bool is_bk_type(enum tag_type type)
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);
123 + * is_bk_conf() - Determines if black key conf.
124 + * @tag_obj_conf : The tag object conf
126 + * Return: True if black key conf, False otherwise.
128 +bool is_bk_conf(const struct tag_object_conf *tag_obj_conf)
130 + return is_bk_type(tag_obj_conf->header.type);
132 +EXPORT_SYMBOL(is_bk_conf);
135 + * get_bk_conf() - Gets the block conf.
136 + * @tag_obj_conf : The tag object conf
138 + * Return: The block conf.
140 +const struct blackey_conf *get_bk_conf(const struct tag_object_conf *tag_obj_conf)
142 + return &tag_obj_conf->conf.bk_conf;
146 + * get_tag_object_overhead() - Gets the tag object overhead.
148 + * Return: The tag object overhead.
150 +size_t get_tag_object_overhead(void)
152 + return TAG_OVERHEAD;
154 +EXPORT_SYMBOL(get_tag_object_overhead);
157 + * is_valid_type() - Determines if valid type.
160 + * Return: True if valid type, False otherwise.
162 +bool is_valid_type(enum tag_type type)
164 + return (type > TAG_TYPE_NOT_SUPPORTED) && (type < NB_TAG_TYPE);
166 +EXPORT_SYMBOL(is_valid_type);
169 + * is_valid_header() - Determines if valid header.
170 + * @header : The header
172 + * Return: True if valid tag object conf, False otherwise.
174 +static bool is_valid_header(const struct conf_header *header)
176 + bool valid = header->_magic_number == TAG_OBJECT_MAGIC;
178 + valid = valid && is_valid_type(header->type);
184 + * is_valid_tag_object_conf() - Determines if valid tag object conf.
185 + * @tag_obj_conf : The tag object conf
187 + * Return: True if valid header, False otherwise.
189 +bool is_valid_tag_object_conf(const struct tag_object_conf *tag_obj_conf)
193 + valid = is_valid_header(&tag_obj_conf->header);
197 +EXPORT_SYMBOL(is_valid_tag_object_conf);
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
205 + * Return: 0 if success, else error code
207 +int get_tag_object_conf(void *buffer, size_t size,
208 + struct tag_object_conf **tag_obj_conf)
211 + struct tagged_object *tago = (struct tagged_object *)buffer;
212 + size_t conf_size = get_tag_object_overhead();
214 + /* Check we can retrieve the conf */
215 + if (size < conf_size)
218 + is_valid = is_valid_tag_object_conf(&tago->tag);
220 + *tag_obj_conf = &tago->tag;
222 + return (is_valid) ? 0 : -EINVAL;
224 +EXPORT_SYMBOL(get_tag_object_conf);
227 + * init_tag_object_header() - Initialize the tag object header
228 + * @conf_header : The configuration header
231 + * It initialize the header structure
233 +void init_tag_object_header(struct conf_header *conf_header,
234 + enum tag_type type)
236 + conf_header->_magic_number = TAG_OBJECT_MAGIC;
237 + conf_header->type = type;
239 +EXPORT_SYMBOL(init_tag_object_header);
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
248 + * Return: 0 if success, else error code
250 +int set_tag_object_conf(const struct tag_object_conf *tag_obj_conf,
251 + void *buffer, size_t obj_size, u32 *to_size)
253 + struct tagged_object *tago = buffer;
254 + size_t conf_size = get_tag_object_overhead();
255 + size_t req_size = obj_size + conf_size;
257 + /* Check we can set the conf */
258 + if (*to_size < req_size) {
259 + *to_size = req_size;
263 + /* Move the object */
264 + memmove(&tago->object, buffer, obj_size);
267 + memcpy(&tago->tag, tag_obj_conf, conf_size);
269 + *to_size = req_size;
273 +EXPORT_SYMBOL(set_tag_object_conf);
276 + * init_blackey_conf() - Initialize the black key configuration
277 + * @blackey_conf : The blackey conf
278 + * @len : The length
280 + * @tk : The trusted key
282 + * It initialize the black key configuration structure
284 +void init_blackey_conf(struct blackey_conf *blackey_conf,
285 + size_t len, bool ccm, bool tk)
287 + blackey_conf->real_len = len;
288 + blackey_conf->load = KEY_ENC
289 + | ((ccm) ? KEY_EKT : 0)
290 + | ((tk) ? KEY_TK : 0);
292 +EXPORT_SYMBOL(init_blackey_conf);
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
300 + * It retrieve the black key configuration
302 +void get_blackey_conf(const struct blackey_conf *blackey_conf,
303 + u32 *real_len, u32 *load_param)
305 + *real_len = blackey_conf->real_len;
306 + *load_param = blackey_conf->load;
308 +EXPORT_SYMBOL(get_blackey_conf);
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
317 + * Return: 0 if success, else error code
319 +int get_tagged_data(void *tagged_object, size_t tagged_object_size,
320 + void **data, u32 *data_size)
322 + struct tagged_object *tago =
323 + (struct tagged_object *)tagged_object;
324 + size_t conf_size = get_tag_object_overhead();
326 + /* Check we can retrieve the object */
327 + if (tagged_object_size < conf_size)
330 + /* Retrieve the object */
331 + *data = &tago->object;
332 + *data_size = tagged_object_size - conf_size;
336 +EXPORT_SYMBOL(get_tagged_data);
338 +++ b/drivers/crypto/caam/tag_object.h
340 +/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
342 + * Copyright 2018-2019 NXP
345 +#ifndef _TAG_OBJECT_H_
346 +#define _TAG_OBJECT_H_
348 +#include <linux/types.h>
350 +#define TAG_MIN_SIZE (2 * sizeof(struct conf_header))
351 +#define TAG_OVERHEAD sizeof(struct tag_object_conf)
354 + * enum tag_type - Type of data represented by the tag
357 + /** @TAG_TYPE_NOT_SUPPORTED: The type is not supported */
358 + TAG_TYPE_NOT_SUPPORTED = 0,
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,
364 + * @TAG_TYPE_BLACK_KEY_ECB_TRUSTED: ECB Black key created by trusted
367 + TAG_TYPE_BLACK_KEY_ECB_TRUSTED,
368 + /** @TAG_TYPE_BLACK_KEY_CCM: Black key encrypted with CCM */
369 + TAG_TYPE_BLACK_KEY_CCM,
371 + * @TAG_TYPE_BLACK_KEY_CCM_TRUSTED: CCM Black key created by trusted
374 + TAG_TYPE_BLACK_KEY_CCM_TRUSTED,
376 + /** @NB_TAG_TYPE: Number of type of tag */
381 + * struct conf_header - Common struture holding the type of data and the magic
383 + * @_magic_number : A magic number to identify the structure
384 + * @type : The type of data contained
386 +struct conf_header {
392 + * struct blackey_conf - Configuration for a black key
393 + * @load : Load parameter for CAAM
394 + * @real_len : Length of the key before encryption
396 +struct blackey_conf {
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
408 +struct tag_object_conf {
409 + struct conf_header header;
411 + struct blackey_conf bk_conf;
415 +bool is_bk_conf(const struct tag_object_conf *tag_obj_conf);
417 +bool is_valid_tag_object_conf(const struct tag_object_conf *tag_obj_conf);
419 +void init_tag_object_header(struct conf_header *conf_header,
420 + enum tag_type type);
422 +int get_tag_object_conf(void *buffer, size_t buffer_size,
423 + struct tag_object_conf **tag_obj_conf);
425 +int set_tag_object_conf(const struct tag_object_conf *tag_obj_conf,
426 + void *buffer, size_t obj_size, u32 *to_size);
428 +size_t get_tag_object_overhead(void);
430 +void get_blackey_conf(const struct blackey_conf *blackey_conf,
431 + u32 *real_len, u32 *load_param);
433 +void init_blackey_conf(struct blackey_conf *blackey_conf,
434 + size_t len, bool ccm, bool tk);
436 +int get_tagged_data(void *buffer, size_t buffer_size,
437 + void **data, u32 *data_size);
439 +#endif /* _TAG_OBJECT_H_ */