1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 Pengutronix, Steffen Trumtrar <kernel@pengutronix.de>
4 * Copyright (C) 2021 Pengutronix, Ahmad Fatoum <kernel@pengutronix.de>
7 #define pr_fmt(fmt) "caam blob_gen: " fmt
9 #include <linux/bitfield.h>
10 #include <linux/device.h>
11 #include <soc/fsl/caam-blob.h>
14 #include "desc_constr.h"
21 #define CAAM_BLOB_DESC_BYTES_MAX \
22 /* Command to initialize & stating length of descriptor */ \
24 /* Command to append the key-modifier + key-modifier data */ \
25 CAAM_CMD_SZ + CAAM_BLOB_KEYMOD_LENGTH + \
26 /* Command to include input key + pointer to the input key */ \
27 CAAM_CMD_SZ + CAAM_PTR_SZ_MAX + \
28 /* Command to include output key + pointer to the output key */ \
29 CAAM_CMD_SZ + CAAM_PTR_SZ_MAX + \
30 /* Command describing the operation to perform */ \
33 struct caam_blob_priv
{
37 struct caam_blob_job_result
{
39 struct completion completion
;
42 static void caam_blob_job_done(struct device
*dev
, u32
*desc
, u32 err
, void *context
)
44 struct caam_blob_job_result
*res
= context
;
47 dev_dbg(dev
, "%s %d: err 0x%x\n", __func__
, __LINE__
, err
);
50 ecode
= caam_jr_strstatus(dev
, err
);
55 * Upon completion, desc points to a buffer containing a CAAM job
56 * descriptor which encapsulates data into an externally-storable
59 complete(&res
->completion
);
62 int caam_process_blob(struct caam_blob_priv
*priv
,
63 struct caam_blob_info
*info
, bool encap
)
65 const struct caam_drv_private
*ctrlpriv
;
66 struct caam_blob_job_result testres
;
67 struct device
*jrdev
= &priv
->jrdev
;
68 dma_addr_t dma_in
, dma_out
;
69 int op
= OP_PCLID_BLOB
;
75 if (info
->key_mod_len
> CAAM_BLOB_KEYMOD_LENGTH
)
79 op
|= OP_TYPE_ENCAP_PROTOCOL
;
80 output_len
= info
->input_len
+ CAAM_BLOB_OVERHEAD
;
82 op
|= OP_TYPE_DECAP_PROTOCOL
;
83 output_len
= info
->input_len
- CAAM_BLOB_OVERHEAD
;
86 desc
= kzalloc(CAAM_BLOB_DESC_BYTES_MAX
, GFP_KERNEL
);
90 dma_in
= dma_map_single(jrdev
, info
->input
, info
->input_len
,
92 if (dma_mapping_error(jrdev
, dma_in
)) {
93 dev_err(jrdev
, "unable to map input DMA buffer\n");
98 dma_out
= dma_map_single(jrdev
, info
->output
, output_len
,
100 if (dma_mapping_error(jrdev
, dma_out
)) {
101 dev_err(jrdev
, "unable to map output DMA buffer\n");
106 ctrlpriv
= dev_get_drvdata(jrdev
->parent
);
107 moo
= FIELD_GET(CSTA_MOO
, rd_reg32(&ctrlpriv
->ctrl
->perfmon
.status
));
108 if (moo
!= CSTA_MOO_SECURE
&& moo
!= CSTA_MOO_TRUSTED
)
110 "using insecure test key, enable HAB to use unique device key!\n");
113 * A data blob is encrypted using a blob key (BK); a random number.
114 * The BK is used as an AES-CCM key. The initial block (B0) and the
115 * initial counter (Ctr0) are generated automatically and stored in
116 * Class 1 Context DWords 0+1+2+3. The random BK is stored in the
117 * Class 1 Key Register. Operation Mode is set to AES-CCM.
120 init_job_desc(desc
, 0);
121 append_key_as_imm(desc
, info
->key_mod
, info
->key_mod_len
,
122 info
->key_mod_len
, CLASS_2
| KEY_DEST_CLASS_REG
);
123 append_seq_in_ptr_intlen(desc
, dma_in
, info
->input_len
, 0);
124 append_seq_out_ptr_intlen(desc
, dma_out
, output_len
, 0);
125 append_operation(desc
, op
);
127 print_hex_dump_debug("data@"__stringify(__LINE__
)": ",
128 DUMP_PREFIX_ADDRESS
, 16, 1, info
->input
,
129 info
->input_len
, false);
130 print_hex_dump_debug("jobdesc@"__stringify(__LINE__
)": ",
131 DUMP_PREFIX_ADDRESS
, 16, 1, desc
,
132 desc_bytes(desc
), false);
135 init_completion(&testres
.completion
);
137 ret
= caam_jr_enqueue(jrdev
, desc
, caam_blob_job_done
, &testres
);
138 if (ret
== -EINPROGRESS
) {
139 wait_for_completion(&testres
.completion
);
141 print_hex_dump_debug("output@"__stringify(__LINE__
)": ",
142 DUMP_PREFIX_ADDRESS
, 16, 1, info
->output
,
147 info
->output_len
= output_len
;
149 dma_unmap_single(jrdev
, dma_out
, output_len
, DMA_FROM_DEVICE
);
151 dma_unmap_single(jrdev
, dma_in
, info
->input_len
, DMA_TO_DEVICE
);
157 EXPORT_SYMBOL(caam_process_blob
);
159 struct caam_blob_priv
*caam_blob_gen_init(void)
161 struct caam_drv_private
*ctrlpriv
;
162 struct device
*jrdev
;
165 * caam_blob_gen_init() may expectedly fail with -ENODEV, e.g. when
166 * CAAM driver didn't probe or when SoC lacks BLOB support. An
167 * error would be harsh in this case, so we stick to info level.
170 jrdev
= caam_jr_alloc();
172 pr_info("job ring requested, but none currently available\n");
173 return ERR_PTR(-ENODEV
);
176 ctrlpriv
= dev_get_drvdata(jrdev
->parent
);
177 if (!ctrlpriv
->blob_present
) {
178 dev_info(jrdev
, "no hardware blob generation support\n");
180 return ERR_PTR(-ENODEV
);
183 return container_of(jrdev
, struct caam_blob_priv
, jrdev
);
185 EXPORT_SYMBOL(caam_blob_gen_init
);
187 void caam_blob_gen_exit(struct caam_blob_priv
*priv
)
189 caam_jr_free(&priv
->jrdev
);
191 EXPORT_SYMBOL(caam_blob_gen_exit
);