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