^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * CAAM/SEC 4.x functions for handling key-generation jobs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2008-2011 Freescale Semiconductor, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "compat.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "jr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "error.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "desc_constr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "key_gen.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) void split_key_done(struct device *dev, u32 *desc, u32 err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct split_key_result *res = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) int ecode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) dev_dbg(dev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) ecode = caam_jr_strstatus(dev, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) res->err = ecode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) complete(&res->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) EXPORT_SYMBOL(split_key_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) get a split ipad/opad key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) Split key generation-----------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) [00] 0xb0810008 jobdesc: stidx=1 share=never len=8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) [01] 0x04000014 key: class2->keyreg len=20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) @0xffe01000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) [03] 0x84410014 operation: cls2-op sha1 hmac init dec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) [04] 0x24940000 fifold: class2 msgdata-last2 len=0 imm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) [05] 0xa4000001 jump: class2 local all ->1 [06]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) [06] 0x64260028 fifostr: class2 mdsplit-jdk len=40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) @0xffe04000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int gen_split_key(struct device *jrdev, u8 *key_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct alginfo * const adata, const u8 *key_in, u32 keylen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int max_keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) u32 *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct split_key_result result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) dma_addr_t dma_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned int local_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) adata->keylen = split_key_len(adata->algtype & OP_ALG_ALGSEL_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) adata->keylen_pad = split_key_pad_len(adata->algtype &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) OP_ALG_ALGSEL_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) local_max = max(keylen, adata->keylen_pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) dev_dbg(jrdev, "split keylen %d split keylen padded %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) adata->keylen, adata->keylen_pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) print_hex_dump_debug("ctx.key@" __stringify(__LINE__)": ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) DUMP_PREFIX_ADDRESS, 16, 4, key_in, keylen, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (local_max > max_keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) desc = kmalloc(CAAM_CMD_SZ * 6 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (!desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) dev_err(jrdev, "unable to allocate key input memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) memcpy(key_out, key_in, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) dma_addr = dma_map_single(jrdev, key_out, local_max, DMA_BIDIRECTIONAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (dma_mapping_error(jrdev, dma_addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) dev_err(jrdev, "unable to map key memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) init_job_desc(desc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) append_key(desc, dma_addr, keylen, CLASS_2 | KEY_DEST_CLASS_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /* Sets MDHA up into an HMAC-INIT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) append_operation(desc, (adata->algtype & OP_ALG_ALGSEL_MASK) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) OP_ALG_AAI_HMAC | OP_TYPE_CLASS2_ALG | OP_ALG_DECRYPT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) OP_ALG_AS_INIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * do a FIFO_LOAD of zero, this will trigger the internal key expansion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * into both pads inside MDHA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) append_fifo_load_as_imm(desc, NULL, 0, LDST_CLASS_2_CCB |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * FIFO_STORE with the explicit split-key content store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * (0x26 output type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) append_fifo_store(desc, dma_addr, adata->keylen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) LDST_CLASS_2_CCB | FIFOST_TYPE_SPLIT_KEK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) result.err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) init_completion(&result.completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (ret == -EINPROGRESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /* in progress */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) wait_for_completion(&result.completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) ret = result.err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) print_hex_dump_debug("ctx.key@"__stringify(__LINE__)": ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) DUMP_PREFIX_ADDRESS, 16, 4, key_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) adata->keylen_pad, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) dma_unmap_single(jrdev, dma_addr, local_max, DMA_BIDIRECTIONAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) kfree(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) EXPORT_SYMBOL(gen_split_key);