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