]> Pileus Git - ~andy/linux/blobdiff - drivers/crypto/caam/caamalg.c
crypto: caam - refactor key_gen, sg
[~andy/linux] / drivers / crypto / caam / caamalg.c
index a4e266f928c23ed99c6b59e6a7aa44be073ad7c6..ea0295d137a744ca77308d34412917be2ea27aba 100644 (file)
@@ -51,6 +51,8 @@
 #include "desc_constr.h"
 #include "jr.h"
 #include "error.h"
+#include "sg_link_tbl.h"
+#include "key_gen.h"
 
 /*
  * crypto alg
@@ -453,121 +455,12 @@ static int aead_setauthsize(struct crypto_aead *authenc,
        return 0;
 }
 
-struct split_key_result {
-       struct completion completion;
-       int err;
-};
-
-static void split_key_done(struct device *dev, u32 *desc, u32 err,
-                          void *context)
+static u32 gen_split_aead_key(struct caam_ctx *ctx, const u8 *key_in,
+                             u32 authkeylen)
 {
-       struct split_key_result *res = context;
-
-#ifdef DEBUG
-       dev_err(dev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
-#endif
-
-       if (err) {
-               char tmp[CAAM_ERROR_STR_MAX];
-
-               dev_err(dev, "%08x: %s\n", err, caam_jr_strstatus(tmp, err));
-       }
-
-       res->err = err;
-
-       complete(&res->completion);
-}
-
-/*
-get a split ipad/opad key
-
-Split key generation-----------------------------------------------
-
-[00] 0xb0810008    jobdesc: stidx=1 share=never len=8
-[01] 0x04000014        key: class2->keyreg len=20
-                       @0xffe01000
-[03] 0x84410014  operation: cls2-op sha1 hmac init dec
-[04] 0x24940000     fifold: class2 msgdata-last2 len=0 imm
-[05] 0xa4000001       jump: class2 local all ->1 [06]
-[06] 0x64260028    fifostr: class2 mdsplit-jdk len=40
-                       @0xffe04000
-*/
-static u32 gen_split_key(struct caam_ctx *ctx, const u8 *key_in, u32 authkeylen)
-{
-       struct device *jrdev = ctx->jrdev;
-       u32 *desc;
-       struct split_key_result result;
-       dma_addr_t dma_addr_in, dma_addr_out;
-       int ret = 0;
-
-       desc = kmalloc(CAAM_CMD_SZ * 6 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA);
-
-       init_job_desc(desc, 0);
-
-       dma_addr_in = dma_map_single(jrdev, (void *)key_in, authkeylen,
-                                    DMA_TO_DEVICE);
-       if (dma_mapping_error(jrdev, dma_addr_in)) {
-               dev_err(jrdev, "unable to map key input memory\n");
-               kfree(desc);
-               return -ENOMEM;
-       }
-       append_key(desc, dma_addr_in, authkeylen, CLASS_2 |
-                      KEY_DEST_CLASS_REG);
-
-       /* Sets MDHA up into an HMAC-INIT */
-       append_operation(desc, ctx->alg_op | OP_ALG_DECRYPT |
-                            OP_ALG_AS_INIT);
-
-       /*
-        * do a FIFO_LOAD of zero, this will trigger the internal key expansion
-          into both pads inside MDHA
-        */
-       append_fifo_load_as_imm(desc, NULL, 0, LDST_CLASS_2_CCB |
-                               FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST2);
-
-       /*
-        * FIFO_STORE with the explicit split-key content store
-        * (0x26 output type)
-        */
-       dma_addr_out = dma_map_single(jrdev, ctx->key, ctx->split_key_pad_len,
-                                     DMA_FROM_DEVICE);
-       if (dma_mapping_error(jrdev, dma_addr_out)) {
-               dev_err(jrdev, "unable to map key output memory\n");
-               kfree(desc);
-               return -ENOMEM;
-       }
-       append_fifo_store(desc, dma_addr_out, ctx->split_key_len,
-                         LDST_CLASS_2_CCB | FIFOST_TYPE_SPLIT_KEK);
-
-#ifdef DEBUG
-       print_hex_dump(KERN_ERR, "ctx.key@"xstr(__LINE__)": ",
-                      DUMP_PREFIX_ADDRESS, 16, 4, key_in, authkeylen, 1);
-       print_hex_dump(KERN_ERR, "jobdesc@"xstr(__LINE__)": ",
-                      DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
-#endif
-
-       result.err = 0;
-       init_completion(&result.completion);
-
-       ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result);
-       if (!ret) {
-               /* in progress */
-               wait_for_completion_interruptible(&result.completion);
-               ret = result.err;
-#ifdef DEBUG
-               print_hex_dump(KERN_ERR, "ctx.key@"xstr(__LINE__)": ",
-                              DUMP_PREFIX_ADDRESS, 16, 4, ctx->key,
-                              ctx->split_key_pad_len, 1);
-#endif
-       }
-
-       dma_unmap_single(jrdev, dma_addr_out, ctx->split_key_pad_len,
-                        DMA_FROM_DEVICE);
-       dma_unmap_single(jrdev, dma_addr_in, authkeylen, DMA_TO_DEVICE);
-
-       kfree(desc);
-
-       return ret;
+       return gen_split_key(ctx->jrdev, ctx->key, ctx->split_key_len,
+                              ctx->split_key_pad_len, key_in, authkeylen,
+                              ctx->alg_op);
 }
 
 static int aead_setkey(struct crypto_aead *aead,
@@ -611,7 +504,7 @@ static int aead_setkey(struct crypto_aead *aead,
                       DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
 #endif
 
-       ret = gen_split_key(ctx, key, authkeylen);
+       ret = gen_split_aead_key(ctx, key, authkeylen);
        if (ret) {
                goto badkey;
        }
@@ -758,14 +651,6 @@ static int ablkcipher_setkey(struct crypto_ablkcipher *ablkcipher,
        return ret;
 }
 
-struct link_tbl_entry {
-       u64 ptr;
-       u32 len;
-       u8 reserved;
-       u8 buf_pool_id;
-       u16 offset;
-};
-
 /*
  * aead_edesc - s/w-extended aead descriptor
  * @assoc_nents: number of segments in associated data (SPI+Seq) scatterlist
@@ -1027,50 +912,6 @@ static void ablkcipher_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
        ablkcipher_request_complete(req, err);
 }
 
-static void sg_to_link_tbl_one(struct link_tbl_entry *link_tbl_ptr,
-                              dma_addr_t dma, u32 len, u32 offset)
-{
-       link_tbl_ptr->ptr = dma;
-       link_tbl_ptr->len = len;
-       link_tbl_ptr->reserved = 0;
-       link_tbl_ptr->buf_pool_id = 0;
-       link_tbl_ptr->offset = offset;
-#ifdef DEBUG
-       print_hex_dump(KERN_ERR, "link_tbl_ptr@"xstr(__LINE__)": ",
-                      DUMP_PREFIX_ADDRESS, 16, 4, link_tbl_ptr,
-                      sizeof(struct link_tbl_entry), 1);
-#endif
-}
-
-/*
- * convert scatterlist to h/w link table format
- * but does not have final bit; instead, returns last entry
- */
-static struct link_tbl_entry *sg_to_link_tbl(struct scatterlist *sg,
-                                            int sg_count, struct link_tbl_entry
-                                            *link_tbl_ptr, u32 offset)
-{
-       while (sg_count) {
-               sg_to_link_tbl_one(link_tbl_ptr, sg_dma_address(sg),
-                                  sg_dma_len(sg), offset);
-               link_tbl_ptr++;
-               sg = sg_next(sg);
-               sg_count--;
-       }
-       return link_tbl_ptr - 1;
-}
-
-/*
- * convert scatterlist to h/w link table format
- * scatterlist must have been previously dma mapped
- */
-static void sg_to_link_tbl_last(struct scatterlist *sg, int sg_count,
-                               struct link_tbl_entry *link_tbl_ptr, u32 offset)
-{
-       link_tbl_ptr = sg_to_link_tbl(sg, sg_count, link_tbl_ptr, offset);
-       link_tbl_ptr->len |= 0x40000000;
-}
-
 /*
  * Fill in aead job descriptor
  */
@@ -1271,28 +1112,6 @@ static void init_ablkcipher_job(u32 *sh_desc, dma_addr_t ptr,
        append_seq_out_ptr(desc, dst_dma, req->nbytes, out_options);
 }
 
-/*
- * derive number of elements in scatterlist
- */
-static int sg_count(struct scatterlist *sg_list, int nbytes)
-{
-       struct scatterlist *sg = sg_list;
-       int sg_nents = 0;
-
-       while (nbytes > 0) {
-               sg_nents++;
-               nbytes -= sg->length;
-               if (!sg_is_last(sg) && (sg + 1)->length == 0)
-                       BUG(); /* Not support chaining */
-               sg = scatterwalk_sg_next(sg);
-       }
-
-       if (likely(sg_nents == 1))
-               return 0;
-
-       return sg_nents;
-}
-
 /*
  * allocate and map the aead extended descriptor
  */