diff options
author | Tudor Ambarus <tudor-dan.ambarus@nxp.com> | 2016-07-04 06:12:08 -0400 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2016-07-05 11:05:24 -0400 |
commit | 8c419778ab57e497b5de1352aa39dbe2efb3ed54 (patch) | |
tree | b9977075029d6d46cc1ae2e743e7df9bde0bd1c3 /drivers/crypto | |
parent | 57763f5ec7488d5864e4d6ec9d4197b8f52214bd (diff) |
crypto: caam - add support for RSA algorithm
Add RSA support to caam driver.
Initial author is Yashpal Dutta <yashpal.dutta@freescale.com>.
Signed-off-by: Tudor Ambarus <tudor-dan.ambarus@nxp.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'drivers/crypto')
-rw-r--r-- | drivers/crypto/caam/Kconfig | 12 | ||||
-rw-r--r-- | drivers/crypto/caam/Makefile | 2 | ||||
-rw-r--r-- | drivers/crypto/caam/caampkc.c | 607 | ||||
-rw-r--r-- | drivers/crypto/caam/caampkc.h | 70 | ||||
-rw-r--r-- | drivers/crypto/caam/compat.h | 3 | ||||
-rw-r--r-- | drivers/crypto/caam/desc.h | 2 | ||||
-rw-r--r-- | drivers/crypto/caam/desc_constr.h | 7 | ||||
-rw-r--r-- | drivers/crypto/caam/pdb.h | 51 | ||||
-rw-r--r-- | drivers/crypto/caam/pkc_desc.c | 36 |
9 files changed, 789 insertions, 1 deletions
diff --git a/drivers/crypto/caam/Kconfig b/drivers/crypto/caam/Kconfig index ff54c42e6e51..64bf3024b680 100644 --- a/drivers/crypto/caam/Kconfig +++ b/drivers/crypto/caam/Kconfig | |||
@@ -99,6 +99,18 @@ config CRYPTO_DEV_FSL_CAAM_AHASH_API | |||
99 | To compile this as a module, choose M here: the module | 99 | To compile this as a module, choose M here: the module |
100 | will be called caamhash. | 100 | will be called caamhash. |
101 | 101 | ||
102 | config CRYPTO_DEV_FSL_CAAM_PKC_API | ||
103 | tristate "Register public key cryptography implementations with Crypto API" | ||
104 | depends on CRYPTO_DEV_FSL_CAAM && CRYPTO_DEV_FSL_CAAM_JR | ||
105 | default y | ||
106 | select CRYPTO_RSA | ||
107 | help | ||
108 | Selecting this will allow SEC Public key support for RSA. | ||
109 | Supported cryptographic primitives: encryption, decryption, | ||
110 | signature and verification. | ||
111 | To compile this as a module, choose M here: the module | ||
112 | will be called caam_pkc. | ||
113 | |||
102 | config CRYPTO_DEV_FSL_CAAM_RNG_API | 114 | config CRYPTO_DEV_FSL_CAAM_RNG_API |
103 | tristate "Register caam device for hwrng API" | 115 | tristate "Register caam device for hwrng API" |
104 | depends on CRYPTO_DEV_FSL_CAAM && CRYPTO_DEV_FSL_CAAM_JR | 116 | depends on CRYPTO_DEV_FSL_CAAM && CRYPTO_DEV_FSL_CAAM_JR |
diff --git a/drivers/crypto/caam/Makefile b/drivers/crypto/caam/Makefile index 3904700ef110..08bf5515ae8a 100644 --- a/drivers/crypto/caam/Makefile +++ b/drivers/crypto/caam/Makefile | |||
@@ -10,6 +10,8 @@ obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_JR) += caam_jr.o | |||
10 | obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API) += caamalg.o | 10 | obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API) += caamalg.o |
11 | obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_AHASH_API) += caamhash.o | 11 | obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_AHASH_API) += caamhash.o |
12 | obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API) += caamrng.o | 12 | obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API) += caamrng.o |
13 | obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_PKC_API) += caam_pkc.o | ||
13 | 14 | ||
14 | caam-objs := ctrl.o | 15 | caam-objs := ctrl.o |
15 | caam_jr-objs := jr.o key_gen.o error.o | 16 | caam_jr-objs := jr.o key_gen.o error.o |
17 | caam_pkc-y := caampkc.o pkc_desc.o | ||
diff --git a/drivers/crypto/caam/caampkc.c b/drivers/crypto/caam/caampkc.c new file mode 100644 index 000000000000..851015e652b8 --- /dev/null +++ b/drivers/crypto/caam/caampkc.c | |||
@@ -0,0 +1,607 @@ | |||
1 | /* | ||
2 | * caam - Freescale FSL CAAM support for Public Key Cryptography | ||
3 | * | ||
4 | * Copyright 2016 Freescale Semiconductor, Inc. | ||
5 | * | ||
6 | * There is no Shared Descriptor for PKC so that the Job Descriptor must carry | ||
7 | * all the desired key parameters, input and output pointers. | ||
8 | */ | ||
9 | #include "compat.h" | ||
10 | #include "regs.h" | ||
11 | #include "intern.h" | ||
12 | #include "jr.h" | ||
13 | #include "error.h" | ||
14 | #include "desc_constr.h" | ||
15 | #include "sg_sw_sec4.h" | ||
16 | #include "caampkc.h" | ||
17 | |||
18 | #define DESC_RSA_PUB_LEN (2 * CAAM_CMD_SZ + sizeof(struct rsa_pub_pdb)) | ||
19 | #define DESC_RSA_PRIV_F1_LEN (2 * CAAM_CMD_SZ + \ | ||
20 | sizeof(struct rsa_priv_f1_pdb)) | ||
21 | |||
22 | static void rsa_io_unmap(struct device *dev, struct rsa_edesc *edesc, | ||
23 | struct akcipher_request *req) | ||
24 | { | ||
25 | dma_unmap_sg(dev, req->dst, edesc->dst_nents, DMA_FROM_DEVICE); | ||
26 | dma_unmap_sg(dev, req->src, edesc->src_nents, DMA_TO_DEVICE); | ||
27 | |||
28 | if (edesc->sec4_sg_bytes) | ||
29 | dma_unmap_single(dev, edesc->sec4_sg_dma, edesc->sec4_sg_bytes, | ||
30 | DMA_TO_DEVICE); | ||
31 | } | ||
32 | |||
33 | static void rsa_pub_unmap(struct device *dev, struct rsa_edesc *edesc, | ||
34 | struct akcipher_request *req) | ||
35 | { | ||
36 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); | ||
37 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); | ||
38 | struct caam_rsa_key *key = &ctx->key; | ||
39 | struct rsa_pub_pdb *pdb = &edesc->pdb.pub; | ||
40 | |||
41 | dma_unmap_single(dev, pdb->n_dma, key->n_sz, DMA_TO_DEVICE); | ||
42 | dma_unmap_single(dev, pdb->e_dma, key->e_sz, DMA_TO_DEVICE); | ||
43 | } | ||
44 | |||
45 | static void rsa_priv_f1_unmap(struct device *dev, struct rsa_edesc *edesc, | ||
46 | struct akcipher_request *req) | ||
47 | { | ||
48 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); | ||
49 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); | ||
50 | struct caam_rsa_key *key = &ctx->key; | ||
51 | struct rsa_priv_f1_pdb *pdb = &edesc->pdb.priv_f1; | ||
52 | |||
53 | dma_unmap_single(dev, pdb->n_dma, key->n_sz, DMA_TO_DEVICE); | ||
54 | dma_unmap_single(dev, pdb->d_dma, key->d_sz, DMA_TO_DEVICE); | ||
55 | } | ||
56 | |||
57 | /* RSA Job Completion handler */ | ||
58 | static void rsa_pub_done(struct device *dev, u32 *desc, u32 err, void *context) | ||
59 | { | ||
60 | struct akcipher_request *req = context; | ||
61 | struct rsa_edesc *edesc; | ||
62 | |||
63 | if (err) | ||
64 | caam_jr_strstatus(dev, err); | ||
65 | |||
66 | edesc = container_of(desc, struct rsa_edesc, hw_desc[0]); | ||
67 | |||
68 | rsa_pub_unmap(dev, edesc, req); | ||
69 | rsa_io_unmap(dev, edesc, req); | ||
70 | kfree(edesc); | ||
71 | |||
72 | akcipher_request_complete(req, err); | ||
73 | } | ||
74 | |||
75 | static void rsa_priv_f1_done(struct device *dev, u32 *desc, u32 err, | ||
76 | void *context) | ||
77 | { | ||
78 | struct akcipher_request *req = context; | ||
79 | struct rsa_edesc *edesc; | ||
80 | |||
81 | if (err) | ||
82 | caam_jr_strstatus(dev, err); | ||
83 | |||
84 | edesc = container_of(desc, struct rsa_edesc, hw_desc[0]); | ||
85 | |||
86 | rsa_priv_f1_unmap(dev, edesc, req); | ||
87 | rsa_io_unmap(dev, edesc, req); | ||
88 | kfree(edesc); | ||
89 | |||
90 | akcipher_request_complete(req, err); | ||
91 | } | ||
92 | |||
93 | static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req, | ||
94 | size_t desclen) | ||
95 | { | ||
96 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); | ||
97 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); | ||
98 | struct device *dev = ctx->dev; | ||
99 | struct rsa_edesc *edesc; | ||
100 | gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG | | ||
101 | CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC; | ||
102 | int sgc; | ||
103 | int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes; | ||
104 | int src_nents, dst_nents; | ||
105 | |||
106 | src_nents = sg_nents_for_len(req->src, req->src_len); | ||
107 | dst_nents = sg_nents_for_len(req->dst, req->dst_len); | ||
108 | |||
109 | if (src_nents > 1) | ||
110 | sec4_sg_len = src_nents; | ||
111 | if (dst_nents > 1) | ||
112 | sec4_sg_len += dst_nents; | ||
113 | |||
114 | sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry); | ||
115 | |||
116 | /* allocate space for base edesc, hw desc commands and link tables */ | ||
117 | edesc = kzalloc(sizeof(*edesc) + desclen + sec4_sg_bytes, | ||
118 | GFP_DMA | flags); | ||
119 | if (!edesc) | ||
120 | return ERR_PTR(-ENOMEM); | ||
121 | |||
122 | sgc = dma_map_sg(dev, req->src, src_nents, DMA_TO_DEVICE); | ||
123 | if (unlikely(!sgc)) { | ||
124 | dev_err(dev, "unable to map source\n"); | ||
125 | goto src_fail; | ||
126 | } | ||
127 | |||
128 | sgc = dma_map_sg(dev, req->dst, dst_nents, DMA_FROM_DEVICE); | ||
129 | if (unlikely(!sgc)) { | ||
130 | dev_err(dev, "unable to map destination\n"); | ||
131 | goto dst_fail; | ||
132 | } | ||
133 | |||
134 | edesc->sec4_sg = (void *)edesc + sizeof(*edesc) + desclen; | ||
135 | |||
136 | sec4_sg_index = 0; | ||
137 | if (src_nents > 1) { | ||
138 | sg_to_sec4_sg_last(req->src, src_nents, edesc->sec4_sg, 0); | ||
139 | sec4_sg_index += src_nents; | ||
140 | } | ||
141 | if (dst_nents > 1) | ||
142 | sg_to_sec4_sg_last(req->dst, dst_nents, | ||
143 | edesc->sec4_sg + sec4_sg_index, 0); | ||
144 | |||
145 | /* Save nents for later use in Job Descriptor */ | ||
146 | edesc->src_nents = src_nents; | ||
147 | edesc->dst_nents = dst_nents; | ||
148 | |||
149 | if (!sec4_sg_bytes) | ||
150 | return edesc; | ||
151 | |||
152 | edesc->sec4_sg_dma = dma_map_single(dev, edesc->sec4_sg, | ||
153 | sec4_sg_bytes, DMA_TO_DEVICE); | ||
154 | if (dma_mapping_error(dev, edesc->sec4_sg_dma)) { | ||
155 | dev_err(dev, "unable to map S/G table\n"); | ||
156 | goto sec4_sg_fail; | ||
157 | } | ||
158 | |||
159 | edesc->sec4_sg_bytes = sec4_sg_bytes; | ||
160 | |||
161 | return edesc; | ||
162 | |||
163 | sec4_sg_fail: | ||
164 | dma_unmap_sg(dev, req->dst, dst_nents, DMA_FROM_DEVICE); | ||
165 | dst_fail: | ||
166 | dma_unmap_sg(dev, req->src, src_nents, DMA_TO_DEVICE); | ||
167 | src_fail: | ||
168 | kfree(edesc); | ||
169 | return ERR_PTR(-ENOMEM); | ||
170 | } | ||
171 | |||
172 | static int set_rsa_pub_pdb(struct akcipher_request *req, | ||
173 | struct rsa_edesc *edesc) | ||
174 | { | ||
175 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); | ||
176 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); | ||
177 | struct caam_rsa_key *key = &ctx->key; | ||
178 | struct device *dev = ctx->dev; | ||
179 | struct rsa_pub_pdb *pdb = &edesc->pdb.pub; | ||
180 | int sec4_sg_index = 0; | ||
181 | |||
182 | pdb->n_dma = dma_map_single(dev, key->n, key->n_sz, DMA_TO_DEVICE); | ||
183 | if (dma_mapping_error(dev, pdb->n_dma)) { | ||
184 | dev_err(dev, "Unable to map RSA modulus memory\n"); | ||
185 | return -ENOMEM; | ||
186 | } | ||
187 | |||
188 | pdb->e_dma = dma_map_single(dev, key->e, key->e_sz, DMA_TO_DEVICE); | ||
189 | if (dma_mapping_error(dev, pdb->e_dma)) { | ||
190 | dev_err(dev, "Unable to map RSA public exponent memory\n"); | ||
191 | dma_unmap_single(dev, pdb->n_dma, key->n_sz, DMA_TO_DEVICE); | ||
192 | return -ENOMEM; | ||
193 | } | ||
194 | |||
195 | if (edesc->src_nents > 1) { | ||
196 | pdb->sgf |= RSA_PDB_SGF_F; | ||
197 | pdb->f_dma = edesc->sec4_sg_dma; | ||
198 | sec4_sg_index += edesc->src_nents; | ||
199 | } else { | ||
200 | pdb->f_dma = sg_dma_address(req->src); | ||
201 | } | ||
202 | |||
203 | if (edesc->dst_nents > 1) { | ||
204 | pdb->sgf |= RSA_PDB_SGF_G; | ||
205 | pdb->g_dma = edesc->sec4_sg_dma + | ||
206 | sec4_sg_index * sizeof(struct sec4_sg_entry); | ||
207 | } else { | ||
208 | pdb->g_dma = sg_dma_address(req->dst); | ||
209 | } | ||
210 | |||
211 | pdb->sgf |= (key->e_sz << RSA_PDB_E_SHIFT) | key->n_sz; | ||
212 | pdb->f_len = req->src_len; | ||
213 | |||
214 | return 0; | ||
215 | } | ||
216 | |||
217 | static int set_rsa_priv_f1_pdb(struct akcipher_request *req, | ||
218 | struct rsa_edesc *edesc) | ||
219 | { | ||
220 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); | ||
221 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); | ||
222 | struct caam_rsa_key *key = &ctx->key; | ||
223 | struct device *dev = ctx->dev; | ||
224 | struct rsa_priv_f1_pdb *pdb = &edesc->pdb.priv_f1; | ||
225 | int sec4_sg_index = 0; | ||
226 | |||
227 | pdb->n_dma = dma_map_single(dev, key->n, key->n_sz, DMA_TO_DEVICE); | ||
228 | if (dma_mapping_error(dev, pdb->n_dma)) { | ||
229 | dev_err(dev, "Unable to map modulus memory\n"); | ||
230 | return -ENOMEM; | ||
231 | } | ||
232 | |||
233 | pdb->d_dma = dma_map_single(dev, key->d, key->d_sz, DMA_TO_DEVICE); | ||
234 | if (dma_mapping_error(dev, pdb->d_dma)) { | ||
235 | dev_err(dev, "Unable to map RSA private exponent memory\n"); | ||
236 | dma_unmap_single(dev, pdb->n_dma, key->n_sz, DMA_TO_DEVICE); | ||
237 | return -ENOMEM; | ||
238 | } | ||
239 | |||
240 | if (edesc->src_nents > 1) { | ||
241 | pdb->sgf |= RSA_PRIV_PDB_SGF_G; | ||
242 | pdb->g_dma = edesc->sec4_sg_dma; | ||
243 | sec4_sg_index += edesc->src_nents; | ||
244 | } else { | ||
245 | pdb->g_dma = sg_dma_address(req->src); | ||
246 | } | ||
247 | |||
248 | if (edesc->dst_nents > 1) { | ||
249 | pdb->sgf |= RSA_PRIV_PDB_SGF_F; | ||
250 | pdb->f_dma = edesc->sec4_sg_dma + | ||
251 | sec4_sg_index * sizeof(struct sec4_sg_entry); | ||
252 | } else { | ||
253 | pdb->f_dma = sg_dma_address(req->dst); | ||
254 | } | ||
255 | |||
256 | pdb->sgf |= (key->d_sz << RSA_PDB_D_SHIFT) | key->n_sz; | ||
257 | |||
258 | return 0; | ||
259 | } | ||
260 | |||
261 | static int caam_rsa_enc(struct akcipher_request *req) | ||
262 | { | ||
263 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); | ||
264 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); | ||
265 | struct caam_rsa_key *key = &ctx->key; | ||
266 | struct device *jrdev = ctx->dev; | ||
267 | struct rsa_edesc *edesc; | ||
268 | int ret; | ||
269 | |||
270 | if (unlikely(!key->n || !key->e)) | ||
271 | return -EINVAL; | ||
272 | |||
273 | if (req->dst_len < key->n_sz) { | ||
274 | req->dst_len = key->n_sz; | ||
275 | dev_err(jrdev, "Output buffer length less than parameter n\n"); | ||
276 | return -EOVERFLOW; | ||
277 | } | ||
278 | |||
279 | /* Allocate extended descriptor */ | ||
280 | edesc = rsa_edesc_alloc(req, DESC_RSA_PUB_LEN); | ||
281 | if (IS_ERR(edesc)) | ||
282 | return PTR_ERR(edesc); | ||
283 | |||
284 | /* Set RSA Encrypt Protocol Data Block */ | ||
285 | ret = set_rsa_pub_pdb(req, edesc); | ||
286 | if (ret) | ||
287 | goto init_fail; | ||
288 | |||
289 | /* Initialize Job Descriptor */ | ||
290 | init_rsa_pub_desc(edesc->hw_desc, &edesc->pdb.pub); | ||
291 | |||
292 | ret = caam_jr_enqueue(jrdev, edesc->hw_desc, rsa_pub_done, req); | ||
293 | if (!ret) | ||
294 | return -EINPROGRESS; | ||
295 | |||
296 | rsa_pub_unmap(jrdev, edesc, req); | ||
297 | |||
298 | init_fail: | ||
299 | rsa_io_unmap(jrdev, edesc, req); | ||
300 | kfree(edesc); | ||
301 | return ret; | ||
302 | } | ||
303 | |||
304 | static int caam_rsa_dec(struct akcipher_request *req) | ||
305 | { | ||
306 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); | ||
307 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); | ||
308 | struct caam_rsa_key *key = &ctx->key; | ||
309 | struct device *jrdev = ctx->dev; | ||
310 | struct rsa_edesc *edesc; | ||
311 | int ret; | ||
312 | |||
313 | if (unlikely(!key->n || !key->d)) | ||
314 | return -EINVAL; | ||
315 | |||
316 | if (req->dst_len < key->n_sz) { | ||
317 | req->dst_len = key->n_sz; | ||
318 | dev_err(jrdev, "Output buffer length less than parameter n\n"); | ||
319 | return -EOVERFLOW; | ||
320 | } | ||
321 | |||
322 | /* Allocate extended descriptor */ | ||
323 | edesc = rsa_edesc_alloc(req, DESC_RSA_PRIV_F1_LEN); | ||
324 | if (IS_ERR(edesc)) | ||
325 | return PTR_ERR(edesc); | ||
326 | |||
327 | /* Set RSA Decrypt Protocol Data Block - Private Key Form #1 */ | ||
328 | ret = set_rsa_priv_f1_pdb(req, edesc); | ||
329 | if (ret) | ||
330 | goto init_fail; | ||
331 | |||
332 | /* Initialize Job Descriptor */ | ||
333 | init_rsa_priv_f1_desc(edesc->hw_desc, &edesc->pdb.priv_f1); | ||
334 | |||
335 | ret = caam_jr_enqueue(jrdev, edesc->hw_desc, rsa_priv_f1_done, req); | ||
336 | if (!ret) | ||
337 | return -EINPROGRESS; | ||
338 | |||
339 | rsa_priv_f1_unmap(jrdev, edesc, req); | ||
340 | |||
341 | init_fail: | ||
342 | rsa_io_unmap(jrdev, edesc, req); | ||
343 | kfree(edesc); | ||
344 | return ret; | ||
345 | } | ||
346 | |||
347 | static void caam_rsa_free_key(struct caam_rsa_key *key) | ||
348 | { | ||
349 | kzfree(key->d); | ||
350 | kfree(key->e); | ||
351 | kfree(key->n); | ||
352 | key->d = NULL; | ||
353 | key->e = NULL; | ||
354 | key->n = NULL; | ||
355 | key->d_sz = 0; | ||
356 | key->e_sz = 0; | ||
357 | key->n_sz = 0; | ||
358 | } | ||
359 | |||
360 | /** | ||
361 | * caam_read_raw_data - Read a raw byte stream as a positive integer. | ||
362 | * The function skips buffer's leading zeros, copies the remained data | ||
363 | * to a buffer allocated in the GFP_DMA | GFP_KERNEL zone and returns | ||
364 | * the address of the new buffer. | ||
365 | * | ||
366 | * @buf : The data to read | ||
367 | * @nbytes: The amount of data to read | ||
368 | */ | ||
369 | static inline u8 *caam_read_raw_data(const u8 *buf, size_t *nbytes) | ||
370 | { | ||
371 | u8 *val; | ||
372 | |||
373 | while (!*buf && *nbytes) { | ||
374 | buf++; | ||
375 | (*nbytes)--; | ||
376 | } | ||
377 | |||
378 | val = kzalloc(*nbytes, GFP_DMA | GFP_KERNEL); | ||
379 | if (!val) | ||
380 | return NULL; | ||
381 | |||
382 | memcpy(val, buf, *nbytes); | ||
383 | |||
384 | return val; | ||
385 | } | ||
386 | |||
387 | static int caam_rsa_check_key_length(unsigned int len) | ||
388 | { | ||
389 | if (len > 4096) | ||
390 | return -EINVAL; | ||
391 | return 0; | ||
392 | } | ||
393 | |||
394 | static int caam_rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key, | ||
395 | unsigned int keylen) | ||
396 | { | ||
397 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); | ||
398 | struct rsa_key raw_key = {0}; | ||
399 | struct caam_rsa_key *rsa_key = &ctx->key; | ||
400 | int ret; | ||
401 | |||
402 | /* Free the old RSA key if any */ | ||
403 | caam_rsa_free_key(rsa_key); | ||
404 | |||
405 | ret = rsa_parse_pub_key(&raw_key, key, keylen); | ||
406 | if (ret) | ||
407 | return ret; | ||
408 | |||
409 | /* Copy key in DMA zone */ | ||
410 | rsa_key->e = kzalloc(raw_key.e_sz, GFP_DMA | GFP_KERNEL); | ||
411 | if (!rsa_key->e) | ||
412 | goto err; | ||
413 | |||
414 | /* | ||
415 | * Skip leading zeros and copy the positive integer to a buffer | ||
416 | * allocated in the GFP_DMA | GFP_KERNEL zone. The decryption descriptor | ||
417 | * expects a positive integer for the RSA modulus and uses its length as | ||
418 | * decryption output length. | ||
419 | */ | ||
420 | rsa_key->n = caam_read_raw_data(raw_key.n, &raw_key.n_sz); | ||
421 | if (!rsa_key->n) | ||
422 | goto err; | ||
423 | |||
424 | if (caam_rsa_check_key_length(raw_key.n_sz << 3)) { | ||
425 | caam_rsa_free_key(rsa_key); | ||
426 | return -EINVAL; | ||
427 | } | ||
428 | |||
429 | rsa_key->e_sz = raw_key.e_sz; | ||
430 | rsa_key->n_sz = raw_key.n_sz; | ||
431 | |||
432 | memcpy(rsa_key->e, raw_key.e, raw_key.e_sz); | ||
433 | |||
434 | return 0; | ||
435 | err: | ||
436 | caam_rsa_free_key(rsa_key); | ||
437 | return -ENOMEM; | ||
438 | } | ||
439 | |||
440 | static int caam_rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key, | ||
441 | unsigned int keylen) | ||
442 | { | ||
443 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); | ||
444 | struct rsa_key raw_key = {0}; | ||
445 | struct caam_rsa_key *rsa_key = &ctx->key; | ||
446 | int ret; | ||
447 | |||
448 | /* Free the old RSA key if any */ | ||
449 | caam_rsa_free_key(rsa_key); | ||
450 | |||
451 | ret = rsa_parse_priv_key(&raw_key, key, keylen); | ||
452 | if (ret) | ||
453 | return ret; | ||
454 | |||
455 | /* Copy key in DMA zone */ | ||
456 | rsa_key->d = kzalloc(raw_key.d_sz, GFP_DMA | GFP_KERNEL); | ||
457 | if (!rsa_key->d) | ||
458 | goto err; | ||
459 | |||
460 | rsa_key->e = kzalloc(raw_key.e_sz, GFP_DMA | GFP_KERNEL); | ||
461 | if (!rsa_key->e) | ||
462 | goto err; | ||
463 | |||
464 | /* | ||
465 | * Skip leading zeros and copy the positive integer to a buffer | ||
466 | * allocated in the GFP_DMA | GFP_KERNEL zone. The decryption descriptor | ||
467 | * expects a positive integer for the RSA modulus and uses its length as | ||
468 | * decryption output length. | ||
469 | */ | ||
470 | rsa_key->n = caam_read_raw_data(raw_key.n, &raw_key.n_sz); | ||
471 | if (!rsa_key->n) | ||
472 | goto err; | ||
473 | |||
474 | if (caam_rsa_check_key_length(raw_key.n_sz << 3)) { | ||
475 | caam_rsa_free_key(rsa_key); | ||
476 | return -EINVAL; | ||
477 | } | ||
478 | |||
479 | rsa_key->d_sz = raw_key.d_sz; | ||
480 | rsa_key->e_sz = raw_key.e_sz; | ||
481 | rsa_key->n_sz = raw_key.n_sz; | ||
482 | |||
483 | memcpy(rsa_key->d, raw_key.d, raw_key.d_sz); | ||
484 | memcpy(rsa_key->e, raw_key.e, raw_key.e_sz); | ||
485 | |||
486 | return 0; | ||
487 | |||
488 | err: | ||
489 | caam_rsa_free_key(rsa_key); | ||
490 | return -ENOMEM; | ||
491 | } | ||
492 | |||
493 | static int caam_rsa_max_size(struct crypto_akcipher *tfm) | ||
494 | { | ||
495 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); | ||
496 | struct caam_rsa_key *key = &ctx->key; | ||
497 | |||
498 | return (key->n) ? key->n_sz : -EINVAL; | ||
499 | } | ||
500 | |||
501 | /* Per session pkc's driver context creation function */ | ||
502 | static int caam_rsa_init_tfm(struct crypto_akcipher *tfm) | ||
503 | { | ||
504 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); | ||
505 | |||
506 | ctx->dev = caam_jr_alloc(); | ||
507 | |||
508 | if (IS_ERR(ctx->dev)) { | ||
509 | dev_err(ctx->dev, "Job Ring Device allocation for transform failed\n"); | ||
510 | return PTR_ERR(ctx->dev); | ||
511 | } | ||
512 | |||
513 | return 0; | ||
514 | } | ||
515 | |||
516 | /* Per session pkc's driver context cleanup function */ | ||
517 | static void caam_rsa_exit_tfm(struct crypto_akcipher *tfm) | ||
518 | { | ||
519 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); | ||
520 | struct caam_rsa_key *key = &ctx->key; | ||
521 | |||
522 | caam_rsa_free_key(key); | ||
523 | caam_jr_free(ctx->dev); | ||
524 | } | ||
525 | |||
526 | static struct akcipher_alg caam_rsa = { | ||
527 | .encrypt = caam_rsa_enc, | ||
528 | .decrypt = caam_rsa_dec, | ||
529 | .sign = caam_rsa_dec, | ||
530 | .verify = caam_rsa_enc, | ||
531 | .set_pub_key = caam_rsa_set_pub_key, | ||
532 | .set_priv_key = caam_rsa_set_priv_key, | ||
533 | .max_size = caam_rsa_max_size, | ||
534 | .init = caam_rsa_init_tfm, | ||
535 | .exit = caam_rsa_exit_tfm, | ||
536 | .base = { | ||
537 | .cra_name = "rsa", | ||
538 | .cra_driver_name = "rsa-caam", | ||
539 | .cra_priority = 3000, | ||
540 | .cra_module = THIS_MODULE, | ||
541 | .cra_ctxsize = sizeof(struct caam_rsa_ctx), | ||
542 | }, | ||
543 | }; | ||
544 | |||
545 | /* Public Key Cryptography module initialization handler */ | ||
546 | static int __init caam_pkc_init(void) | ||
547 | { | ||
548 | struct device_node *dev_node; | ||
549 | struct platform_device *pdev; | ||
550 | struct device *ctrldev; | ||
551 | struct caam_drv_private *priv; | ||
552 | u32 cha_inst, pk_inst; | ||
553 | int err; | ||
554 | |||
555 | dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0"); | ||
556 | if (!dev_node) { | ||
557 | dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0"); | ||
558 | if (!dev_node) | ||
559 | return -ENODEV; | ||
560 | } | ||
561 | |||
562 | pdev = of_find_device_by_node(dev_node); | ||
563 | if (!pdev) { | ||
564 | of_node_put(dev_node); | ||
565 | return -ENODEV; | ||
566 | } | ||
567 | |||
568 | ctrldev = &pdev->dev; | ||
569 | priv = dev_get_drvdata(ctrldev); | ||
570 | of_node_put(dev_node); | ||
571 | |||
572 | /* | ||
573 | * If priv is NULL, it's probably because the caam driver wasn't | ||
574 | * properly initialized (e.g. RNG4 init failed). Thus, bail out here. | ||
575 | */ | ||
576 | if (!priv) | ||
577 | return -ENODEV; | ||
578 | |||
579 | /* Determine public key hardware accelerator presence. */ | ||
580 | cha_inst = rd_reg32(&priv->ctrl->perfmon.cha_num_ls); | ||
581 | pk_inst = (cha_inst & CHA_ID_LS_PK_MASK) >> CHA_ID_LS_PK_SHIFT; | ||
582 | |||
583 | /* Do not register algorithms if PKHA is not present. */ | ||
584 | if (!pk_inst) | ||
585 | return -ENODEV; | ||
586 | |||
587 | err = crypto_register_akcipher(&caam_rsa); | ||
588 | if (err) | ||
589 | dev_warn(ctrldev, "%s alg registration failed\n", | ||
590 | caam_rsa.base.cra_driver_name); | ||
591 | else | ||
592 | dev_info(ctrldev, "caam pkc algorithms registered in /proc/crypto\n"); | ||
593 | |||
594 | return err; | ||
595 | } | ||
596 | |||
597 | static void __exit caam_pkc_exit(void) | ||
598 | { | ||
599 | crypto_unregister_akcipher(&caam_rsa); | ||
600 | } | ||
601 | |||
602 | module_init(caam_pkc_init); | ||
603 | module_exit(caam_pkc_exit); | ||
604 | |||
605 | MODULE_LICENSE("Dual BSD/GPL"); | ||
606 | MODULE_DESCRIPTION("FSL CAAM support for PKC functions of crypto API"); | ||
607 | MODULE_AUTHOR("Freescale Semiconductor"); | ||
diff --git a/drivers/crypto/caam/caampkc.h b/drivers/crypto/caam/caampkc.h new file mode 100644 index 000000000000..f595d159b112 --- /dev/null +++ b/drivers/crypto/caam/caampkc.h | |||
@@ -0,0 +1,70 @@ | |||
1 | /* | ||
2 | * caam - Freescale FSL CAAM support for Public Key Cryptography descriptors | ||
3 | * | ||
4 | * Copyright 2016 Freescale Semiconductor, Inc. | ||
5 | * | ||
6 | * There is no Shared Descriptor for PKC so that the Job Descriptor must carry | ||
7 | * all the desired key parameters, input and output pointers. | ||
8 | */ | ||
9 | |||
10 | #ifndef _PKC_DESC_H_ | ||
11 | #define _PKC_DESC_H_ | ||
12 | #include "compat.h" | ||
13 | #include "pdb.h" | ||
14 | |||
15 | /** | ||
16 | * caam_rsa_key - CAAM RSA key structure. Keys are allocated in DMA zone. | ||
17 | * @n : RSA modulus raw byte stream | ||
18 | * @e : RSA public exponent raw byte stream | ||
19 | * @d : RSA private exponent raw byte stream | ||
20 | * @n_sz : length in bytes of RSA modulus n | ||
21 | * @e_sz : length in bytes of RSA public exponent | ||
22 | * @d_sz : length in bytes of RSA private exponent | ||
23 | */ | ||
24 | struct caam_rsa_key { | ||
25 | u8 *n; | ||
26 | u8 *e; | ||
27 | u8 *d; | ||
28 | size_t n_sz; | ||
29 | size_t e_sz; | ||
30 | size_t d_sz; | ||
31 | }; | ||
32 | |||
33 | /** | ||
34 | * caam_rsa_ctx - per session context. | ||
35 | * @key : RSA key in DMA zone | ||
36 | * @dev : device structure | ||
37 | */ | ||
38 | struct caam_rsa_ctx { | ||
39 | struct caam_rsa_key key; | ||
40 | struct device *dev; | ||
41 | }; | ||
42 | |||
43 | /** | ||
44 | * rsa_edesc - s/w-extended rsa descriptor | ||
45 | * @src_nents : number of segments in input scatterlist | ||
46 | * @dst_nents : number of segments in output scatterlist | ||
47 | * @sec4_sg_bytes : length of h/w link table | ||
48 | * @sec4_sg_dma : dma address of h/w link table | ||
49 | * @sec4_sg : pointer to h/w link table | ||
50 | * @pdb : specific RSA Protocol Data Block (PDB) | ||
51 | * @hw_desc : descriptor followed by link tables if any | ||
52 | */ | ||
53 | struct rsa_edesc { | ||
54 | int src_nents; | ||
55 | int dst_nents; | ||
56 | int sec4_sg_bytes; | ||
57 | dma_addr_t sec4_sg_dma; | ||
58 | struct sec4_sg_entry *sec4_sg; | ||
59 | union { | ||
60 | struct rsa_pub_pdb pub; | ||
61 | struct rsa_priv_f1_pdb priv_f1; | ||
62 | } pdb; | ||
63 | u32 hw_desc[]; | ||
64 | }; | ||
65 | |||
66 | /* Descriptor construction primitives. */ | ||
67 | void init_rsa_pub_desc(u32 *desc, struct rsa_pub_pdb *pdb); | ||
68 | void init_rsa_priv_f1_desc(u32 *desc, struct rsa_priv_f1_pdb *pdb); | ||
69 | |||
70 | #endif | ||
diff --git a/drivers/crypto/caam/compat.h b/drivers/crypto/caam/compat.h index b6955ecdfb3f..7149cd2492e0 100644 --- a/drivers/crypto/caam/compat.h +++ b/drivers/crypto/caam/compat.h | |||
@@ -35,8 +35,11 @@ | |||
35 | #include <crypto/md5.h> | 35 | #include <crypto/md5.h> |
36 | #include <crypto/internal/aead.h> | 36 | #include <crypto/internal/aead.h> |
37 | #include <crypto/authenc.h> | 37 | #include <crypto/authenc.h> |
38 | #include <crypto/akcipher.h> | ||
38 | #include <crypto/scatterwalk.h> | 39 | #include <crypto/scatterwalk.h> |
39 | #include <crypto/internal/skcipher.h> | 40 | #include <crypto/internal/skcipher.h> |
40 | #include <crypto/internal/hash.h> | 41 | #include <crypto/internal/hash.h> |
42 | #include <crypto/internal/rsa.h> | ||
43 | #include <crypto/internal/akcipher.h> | ||
41 | 44 | ||
42 | #endif /* !defined(CAAM_COMPAT_H) */ | 45 | #endif /* !defined(CAAM_COMPAT_H) */ |
diff --git a/drivers/crypto/caam/desc.h b/drivers/crypto/caam/desc.h index d8d5584b600b..26427c11ad87 100644 --- a/drivers/crypto/caam/desc.h +++ b/drivers/crypto/caam/desc.h | |||
@@ -453,6 +453,8 @@ struct sec4_sg_entry { | |||
453 | #define OP_PCLID_PUBLICKEYPAIR (0x14 << OP_PCLID_SHIFT) | 453 | #define OP_PCLID_PUBLICKEYPAIR (0x14 << OP_PCLID_SHIFT) |
454 | #define OP_PCLID_DSASIGN (0x15 << OP_PCLID_SHIFT) | 454 | #define OP_PCLID_DSASIGN (0x15 << OP_PCLID_SHIFT) |
455 | #define OP_PCLID_DSAVERIFY (0x16 << OP_PCLID_SHIFT) | 455 | #define OP_PCLID_DSAVERIFY (0x16 << OP_PCLID_SHIFT) |
456 | #define OP_PCLID_RSAENC_PUBKEY (0x18 << OP_PCLID_SHIFT) | ||
457 | #define OP_PCLID_RSADEC_PRVKEY (0x19 << OP_PCLID_SHIFT) | ||
456 | 458 | ||
457 | /* Assuming OP_TYPE = OP_TYPE_DECAP_PROTOCOL/ENCAP_PROTOCOL */ | 459 | /* Assuming OP_TYPE = OP_TYPE_DECAP_PROTOCOL/ENCAP_PROTOCOL */ |
458 | #define OP_PCLID_IPSEC (0x01 << OP_PCLID_SHIFT) | 460 | #define OP_PCLID_IPSEC (0x01 << OP_PCLID_SHIFT) |
diff --git a/drivers/crypto/caam/desc_constr.h b/drivers/crypto/caam/desc_constr.h index ae3aef6e9fee..d3869b95e7b1 100644 --- a/drivers/crypto/caam/desc_constr.h +++ b/drivers/crypto/caam/desc_constr.h | |||
@@ -77,6 +77,13 @@ static inline void init_job_desc(u32 *desc, u32 options) | |||
77 | init_desc(desc, CMD_DESC_HDR | options); | 77 | init_desc(desc, CMD_DESC_HDR | options); |
78 | } | 78 | } |
79 | 79 | ||
80 | static inline void init_job_desc_pdb(u32 *desc, u32 options, size_t pdb_bytes) | ||
81 | { | ||
82 | u32 pdb_len = (pdb_bytes + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ; | ||
83 | |||
84 | init_job_desc(desc, (((pdb_len + 1) << HDR_START_IDX_SHIFT)) | options); | ||
85 | } | ||
86 | |||
80 | static inline void append_ptr(u32 *desc, dma_addr_t ptr) | 87 | static inline void append_ptr(u32 *desc, dma_addr_t ptr) |
81 | { | 88 | { |
82 | dma_addr_t *offset = (dma_addr_t *)desc_end(desc); | 89 | dma_addr_t *offset = (dma_addr_t *)desc_end(desc); |
diff --git a/drivers/crypto/caam/pdb.h b/drivers/crypto/caam/pdb.h index d383573d3dd4..aaa00dd1c601 100644 --- a/drivers/crypto/caam/pdb.h +++ b/drivers/crypto/caam/pdb.h | |||
@@ -1,12 +1,13 @@ | |||
1 | /* | 1 | /* |
2 | * CAAM Protocol Data Block (PDB) definition header file | 2 | * CAAM Protocol Data Block (PDB) definition header file |
3 | * | 3 | * |
4 | * Copyright 2008-2012 Freescale Semiconductor, Inc. | 4 | * Copyright 2008-2016 Freescale Semiconductor, Inc. |
5 | * | 5 | * |
6 | */ | 6 | */ |
7 | 7 | ||
8 | #ifndef CAAM_PDB_H | 8 | #ifndef CAAM_PDB_H |
9 | #define CAAM_PDB_H | 9 | #define CAAM_PDB_H |
10 | #include "compat.h" | ||
10 | 11 | ||
11 | /* | 12 | /* |
12 | * PDB- IPSec ESP Header Modification Options | 13 | * PDB- IPSec ESP Header Modification Options |
@@ -476,4 +477,52 @@ struct dsa_verify_pdb { | |||
476 | u8 *ab; /* only used if ECC processing */ | 477 | u8 *ab; /* only used if ECC processing */ |
477 | }; | 478 | }; |
478 | 479 | ||
480 | /* RSA Protocol Data Block */ | ||
481 | #define RSA_PDB_SGF_SHIFT 28 | ||
482 | #define RSA_PDB_E_SHIFT 12 | ||
483 | #define RSA_PDB_E_MASK (0xFFF << RSA_PDB_E_SHIFT) | ||
484 | #define RSA_PDB_D_SHIFT 12 | ||
485 | #define RSA_PDB_D_MASK (0xFFF << RSA_PDB_D_SHIFT) | ||
486 | |||
487 | #define RSA_PDB_SGF_F (0x8 << RSA_PDB_SGF_SHIFT) | ||
488 | #define RSA_PDB_SGF_G (0x4 << RSA_PDB_SGF_SHIFT) | ||
489 | #define RSA_PRIV_PDB_SGF_F (0x4 << RSA_PDB_SGF_SHIFT) | ||
490 | #define RSA_PRIV_PDB_SGF_G (0x8 << RSA_PDB_SGF_SHIFT) | ||
491 | |||
492 | #define RSA_PRIV_KEY_FRM_1 0 | ||
493 | |||
494 | /** | ||
495 | * RSA Encrypt Protocol Data Block | ||
496 | * @sgf: scatter-gather field | ||
497 | * @f_dma: dma address of input data | ||
498 | * @g_dma: dma address of encrypted output data | ||
499 | * @n_dma: dma address of RSA modulus | ||
500 | * @e_dma: dma address of RSA public exponent | ||
501 | * @f_len: length in octets of the input data | ||
502 | */ | ||
503 | struct rsa_pub_pdb { | ||
504 | u32 sgf; | ||
505 | dma_addr_t f_dma; | ||
506 | dma_addr_t g_dma; | ||
507 | dma_addr_t n_dma; | ||
508 | dma_addr_t e_dma; | ||
509 | u32 f_len; | ||
510 | } __packed; | ||
511 | |||
512 | /** | ||
513 | * RSA Decrypt PDB - Private Key Form #1 | ||
514 | * @sgf: scatter-gather field | ||
515 | * @g_dma: dma address of encrypted input data | ||
516 | * @f_dma: dma address of output data | ||
517 | * @n_dma: dma address of RSA modulus | ||
518 | * @d_dma: dma address of RSA private exponent | ||
519 | */ | ||
520 | struct rsa_priv_f1_pdb { | ||
521 | u32 sgf; | ||
522 | dma_addr_t g_dma; | ||
523 | dma_addr_t f_dma; | ||
524 | dma_addr_t n_dma; | ||
525 | dma_addr_t d_dma; | ||
526 | } __packed; | ||
527 | |||
479 | #endif | 528 | #endif |
diff --git a/drivers/crypto/caam/pkc_desc.c b/drivers/crypto/caam/pkc_desc.c new file mode 100644 index 000000000000..4e4183e615ea --- /dev/null +++ b/drivers/crypto/caam/pkc_desc.c | |||
@@ -0,0 +1,36 @@ | |||
1 | /* | ||
2 | * caam - Freescale FSL CAAM support for Public Key Cryptography descriptors | ||
3 | * | ||
4 | * Copyright 2016 Freescale Semiconductor, Inc. | ||
5 | * | ||
6 | * There is no Shared Descriptor for PKC so that the Job Descriptor must carry | ||
7 | * all the desired key parameters, input and output pointers. | ||
8 | */ | ||
9 | #include "caampkc.h" | ||
10 | #include "desc_constr.h" | ||
11 | |||
12 | /* Descriptor for RSA Public operation */ | ||
13 | void init_rsa_pub_desc(u32 *desc, struct rsa_pub_pdb *pdb) | ||
14 | { | ||
15 | init_job_desc_pdb(desc, 0, sizeof(*pdb)); | ||
16 | append_cmd(desc, pdb->sgf); | ||
17 | append_ptr(desc, pdb->f_dma); | ||
18 | append_ptr(desc, pdb->g_dma); | ||
19 | append_ptr(desc, pdb->n_dma); | ||
20 | append_ptr(desc, pdb->e_dma); | ||
21 | append_cmd(desc, pdb->f_len); | ||
22 | append_operation(desc, OP_TYPE_UNI_PROTOCOL | OP_PCLID_RSAENC_PUBKEY); | ||
23 | } | ||
24 | |||
25 | /* Descriptor for RSA Private operation - Private Key Form #1 */ | ||
26 | void init_rsa_priv_f1_desc(u32 *desc, struct rsa_priv_f1_pdb *pdb) | ||
27 | { | ||
28 | init_job_desc_pdb(desc, 0, sizeof(*pdb)); | ||
29 | append_cmd(desc, pdb->sgf); | ||
30 | append_ptr(desc, pdb->g_dma); | ||
31 | append_ptr(desc, pdb->f_dma); | ||
32 | append_ptr(desc, pdb->n_dma); | ||
33 | append_ptr(desc, pdb->d_dma); | ||
34 | append_operation(desc, OP_TYPE_UNI_PROTOCOL | OP_PCLID_RSADEC_PRVKEY | | ||
35 | RSA_PRIV_KEY_FRM_1); | ||
36 | } | ||