Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2015 Pengutronix, Steffen Trumtrar <kernel@pengutronix.de>
0004  * Copyright (C) 2021 Pengutronix, Ahmad Fatoum <kernel@pengutronix.de>
0005  */
0006 
0007 #define pr_fmt(fmt) "caam blob_gen: " fmt
0008 
0009 #include <linux/device.h>
0010 #include <soc/fsl/caam-blob.h>
0011 
0012 #include "compat.h"
0013 #include "desc_constr.h"
0014 #include "desc.h"
0015 #include "error.h"
0016 #include "intern.h"
0017 #include "jr.h"
0018 #include "regs.h"
0019 
0020 #define CAAM_BLOB_DESC_BYTES_MAX                    \
0021     /* Command to initialize & stating length of descriptor */  \
0022     (CAAM_CMD_SZ +                          \
0023     /* Command to append the key-modifier + key-modifier data */    \
0024      CAAM_CMD_SZ + CAAM_BLOB_KEYMOD_LENGTH +            \
0025     /* Command to include input key + pointer to the input key */   \
0026      CAAM_CMD_SZ + CAAM_PTR_SZ_MAX +                \
0027     /* Command to include output key + pointer to the output key */ \
0028      CAAM_CMD_SZ + CAAM_PTR_SZ_MAX +                \
0029     /* Command describing the operation to perform */       \
0030      CAAM_CMD_SZ)
0031 
0032 struct caam_blob_priv {
0033     struct device jrdev;
0034 };
0035 
0036 struct caam_blob_job_result {
0037     int err;
0038     struct completion completion;
0039 };
0040 
0041 static void caam_blob_job_done(struct device *dev, u32 *desc, u32 err, void *context)
0042 {
0043     struct caam_blob_job_result *res = context;
0044     int ecode = 0;
0045 
0046     dev_dbg(dev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
0047 
0048     if (err)
0049         ecode = caam_jr_strstatus(dev, err);
0050 
0051     res->err = ecode;
0052 
0053     /*
0054      * Upon completion, desc points to a buffer containing a CAAM job
0055      * descriptor which encapsulates data into an externally-storable
0056      * blob.
0057      */
0058     complete(&res->completion);
0059 }
0060 
0061 int caam_process_blob(struct caam_blob_priv *priv,
0062               struct caam_blob_info *info, bool encap)
0063 {
0064     struct caam_blob_job_result testres;
0065     struct device *jrdev = &priv->jrdev;
0066     dma_addr_t dma_in, dma_out;
0067     int op = OP_PCLID_BLOB;
0068     size_t output_len;
0069     u32 *desc;
0070     int ret;
0071 
0072     if (info->key_mod_len > CAAM_BLOB_KEYMOD_LENGTH)
0073         return -EINVAL;
0074 
0075     if (encap) {
0076         op |= OP_TYPE_ENCAP_PROTOCOL;
0077         output_len = info->input_len + CAAM_BLOB_OVERHEAD;
0078     } else {
0079         op |= OP_TYPE_DECAP_PROTOCOL;
0080         output_len = info->input_len - CAAM_BLOB_OVERHEAD;
0081     }
0082 
0083     desc = kzalloc(CAAM_BLOB_DESC_BYTES_MAX, GFP_KERNEL | GFP_DMA);
0084     if (!desc)
0085         return -ENOMEM;
0086 
0087     dma_in = dma_map_single(jrdev, info->input, info->input_len,
0088                 DMA_TO_DEVICE);
0089     if (dma_mapping_error(jrdev, dma_in)) {
0090         dev_err(jrdev, "unable to map input DMA buffer\n");
0091         ret = -ENOMEM;
0092         goto out_free;
0093     }
0094 
0095     dma_out = dma_map_single(jrdev, info->output, output_len,
0096                  DMA_FROM_DEVICE);
0097     if (dma_mapping_error(jrdev, dma_out)) {
0098         dev_err(jrdev, "unable to map output DMA buffer\n");
0099         ret = -ENOMEM;
0100         goto out_unmap_in;
0101     }
0102 
0103     /*
0104      * A data blob is encrypted using a blob key (BK); a random number.
0105      * The BK is used as an AES-CCM key. The initial block (B0) and the
0106      * initial counter (Ctr0) are generated automatically and stored in
0107      * Class 1 Context DWords 0+1+2+3. The random BK is stored in the
0108      * Class 1 Key Register. Operation Mode is set to AES-CCM.
0109      */
0110 
0111     init_job_desc(desc, 0);
0112     append_key_as_imm(desc, info->key_mod, info->key_mod_len,
0113               info->key_mod_len, CLASS_2 | KEY_DEST_CLASS_REG);
0114     append_seq_in_ptr_intlen(desc, dma_in, info->input_len, 0);
0115     append_seq_out_ptr_intlen(desc, dma_out, output_len, 0);
0116     append_operation(desc, op);
0117 
0118     print_hex_dump_debug("data@"__stringify(__LINE__)": ",
0119                  DUMP_PREFIX_ADDRESS, 16, 1, info->input,
0120                  info->input_len, false);
0121     print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
0122                  DUMP_PREFIX_ADDRESS, 16, 1, desc,
0123                  desc_bytes(desc), false);
0124 
0125     testres.err = 0;
0126     init_completion(&testres.completion);
0127 
0128     ret = caam_jr_enqueue(jrdev, desc, caam_blob_job_done, &testres);
0129     if (ret == -EINPROGRESS) {
0130         wait_for_completion(&testres.completion);
0131         ret = testres.err;
0132         print_hex_dump_debug("output@"__stringify(__LINE__)": ",
0133                      DUMP_PREFIX_ADDRESS, 16, 1, info->output,
0134                      output_len, false);
0135     }
0136 
0137     if (ret == 0)
0138         info->output_len = output_len;
0139 
0140     dma_unmap_single(jrdev, dma_out, output_len, DMA_FROM_DEVICE);
0141 out_unmap_in:
0142     dma_unmap_single(jrdev, dma_in, info->input_len, DMA_TO_DEVICE);
0143 out_free:
0144     kfree(desc);
0145 
0146     return ret;
0147 }
0148 EXPORT_SYMBOL(caam_process_blob);
0149 
0150 struct caam_blob_priv *caam_blob_gen_init(void)
0151 {
0152     struct caam_drv_private *ctrlpriv;
0153     struct device *jrdev;
0154 
0155     /*
0156      * caam_blob_gen_init() may expectedly fail with -ENODEV, e.g. when
0157      * CAAM driver didn't probe or when SoC lacks BLOB support. An
0158      * error would be harsh in this case, so we stick to info level.
0159      */
0160 
0161     jrdev = caam_jr_alloc();
0162     if (IS_ERR(jrdev)) {
0163         pr_info("job ring requested, but none currently available\n");
0164         return ERR_PTR(-ENODEV);
0165     }
0166 
0167     ctrlpriv = dev_get_drvdata(jrdev->parent);
0168     if (!ctrlpriv->blob_present) {
0169         dev_info(jrdev, "no hardware blob generation support\n");
0170         caam_jr_free(jrdev);
0171         return ERR_PTR(-ENODEV);
0172     }
0173 
0174     return container_of(jrdev, struct caam_blob_priv, jrdev);
0175 }
0176 EXPORT_SYMBOL(caam_blob_gen_init);
0177 
0178 void caam_blob_gen_exit(struct caam_blob_priv *priv)
0179 {
0180     caam_jr_free(&priv->jrdev);
0181 }
0182 EXPORT_SYMBOL(caam_blob_gen_exit);