crypto: caam - fix missing dma unmap on error path
[deliverable/linux.git] / drivers / crypto / caam / key_gen.c
CommitLineData
4c1ec1f9
YK
1/*
2 * CAAM/SEC 4.x functions for handling key-generation jobs
3 *
4 * Copyright 2008-2011 Freescale Semiconductor, Inc.
5 *
6 */
7#include "compat.h"
8#include "jr.h"
9#include "error.h"
10#include "desc_constr.h"
11#include "key_gen.h"
12
13void split_key_done(struct device *dev, u32 *desc, u32 err,
14 void *context)
15{
16 struct split_key_result *res = context;
17
18#ifdef DEBUG
19 dev_err(dev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
20#endif
21
fa9659cd
MV
22 if (err)
23 caam_jr_strstatus(dev, err);
4c1ec1f9
YK
24
25 res->err = err;
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*/
66b3e887 44int gen_split_key(struct device *jrdev, u8 *key_out, int split_key_len,
4c1ec1f9
YK
45 int split_key_pad_len, const u8 *key_in, u32 keylen,
46 u32 alg_op)
47{
48 u32 *desc;
49 struct split_key_result result;
50 dma_addr_t dma_addr_in, dma_addr_out;
738459e3 51 int ret = -ENOMEM;
4c1ec1f9
YK
52
53 desc = kmalloc(CAAM_CMD_SZ * 6 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA);
2af8f4a2
KP
54 if (!desc) {
55 dev_err(jrdev, "unable to allocate key input memory\n");
738459e3 56 return ret;
2af8f4a2 57 }
4c1ec1f9 58
4c1ec1f9
YK
59 dma_addr_in = dma_map_single(jrdev, (void *)key_in, keylen,
60 DMA_TO_DEVICE);
61 if (dma_mapping_error(jrdev, dma_addr_in)) {
62 dev_err(jrdev, "unable to map key input memory\n");
738459e3 63 goto out_free;
4c1ec1f9 64 }
738459e3
CS
65
66 dma_addr_out = dma_map_single(jrdev, key_out, split_key_pad_len,
67 DMA_FROM_DEVICE);
68 if (dma_mapping_error(jrdev, dma_addr_out)) {
69 dev_err(jrdev, "unable to map key output memory\n");
70 goto out_unmap_in;
71 }
72
73 init_job_desc(desc, 0);
4c1ec1f9
YK
74 append_key(desc, dma_addr_in, keylen, CLASS_2 | KEY_DEST_CLASS_REG);
75
76 /* Sets MDHA up into an HMAC-INIT */
77 append_operation(desc, alg_op | OP_ALG_DECRYPT | OP_ALG_AS_INIT);
78
79 /*
80 * do a FIFO_LOAD of zero, this will trigger the internal key expansion
81 * into both pads inside MDHA
82 */
83 append_fifo_load_as_imm(desc, NULL, 0, LDST_CLASS_2_CCB |
84 FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST2);
85
86 /*
87 * FIFO_STORE with the explicit split-key content store
88 * (0x26 output type)
89 */
4c1ec1f9
YK
90 append_fifo_store(desc, dma_addr_out, split_key_len,
91 LDST_CLASS_2_CCB | FIFOST_TYPE_SPLIT_KEK);
92
93#ifdef DEBUG
514df281 94 print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ",
4c1ec1f9 95 DUMP_PREFIX_ADDRESS, 16, 4, key_in, keylen, 1);
514df281 96 print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
4c1ec1f9
YK
97 DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
98#endif
99
100 result.err = 0;
101 init_completion(&result.completion);
102
103 ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result);
104 if (!ret) {
105 /* in progress */
106 wait_for_completion_interruptible(&result.completion);
107 ret = result.err;
108#ifdef DEBUG
514df281 109 print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ",
4c1ec1f9
YK
110 DUMP_PREFIX_ADDRESS, 16, 4, key_out,
111 split_key_pad_len, 1);
112#endif
113 }
114
115 dma_unmap_single(jrdev, dma_addr_out, split_key_pad_len,
116 DMA_FROM_DEVICE);
738459e3 117out_unmap_in:
4c1ec1f9 118 dma_unmap_single(jrdev, dma_addr_in, keylen, DMA_TO_DEVICE);
738459e3 119out_free:
4c1ec1f9 120 kfree(desc);
4c1ec1f9
YK
121 return ret;
122}
3b75a2c1 123EXPORT_SYMBOL(gen_split_key);
This page took 0.17916 seconds and 5 git commands to generate.