OpenWrt – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /****************************************************************************** |
2 | ** |
||
3 | ** FILE NAME : ifxmips_async_des.c |
||
4 | ** PROJECT : IFX UEIP |
||
5 | ** MODULES : DEU Module |
||
6 | ** |
||
7 | ** DATE : October 11, 2010 |
||
8 | ** AUTHOR : Mohammad Firdaus |
||
9 | ** DESCRIPTION : Data Encryption Unit Driver for DES Algorithm |
||
10 | ** COPYRIGHT : Copyright (c) 2010 |
||
11 | ** Infineon Technologies AG |
||
12 | ** Am Campeon 1-12, 85579 Neubiberg, Germany |
||
13 | ** |
||
14 | ** This program is free software; you can redistribute it and/or modify |
||
15 | ** it under the terms of the GNU General Public License as published by |
||
16 | ** the Free Software Foundation; either version 2 of the License, or |
||
17 | ** (at your option) any later version. |
||
18 | ** |
||
19 | ** HISTORY |
||
20 | ** $Date $Author $Comment |
||
21 | ** 08,Sept 2009 Mohammad Firdaus Initial UEIP release |
||
22 | ** 11, Oct 2010 Mohammad Firdaus Kernel Port incl. Async. Ablkcipher mode |
||
23 | ** 21,March 2011 Mohammad Firdaus Changes for Kernel 2.6.32 and IPSec integration |
||
24 | *******************************************************************************/ |
||
25 | /*! |
||
26 | \defgroup IFX_DEU IFX_DEU_DRIVERS |
||
27 | \ingroup API |
||
28 | \brief ifx DEU driver module |
||
29 | */ |
||
30 | |||
31 | /*! |
||
32 | \file ifxmips_async_des.c |
||
33 | \ingroup IFX_DEU |
||
34 | \brief DES Encryption Driver main file |
||
35 | */ |
||
36 | |||
37 | /*! |
||
38 | \defgroup IFX_DES_FUNCTIONS IFX_DES_FUNCTIONS |
||
39 | \ingroup IFX_DEU |
||
40 | \brief IFX DES driver Functions |
||
41 | */ |
||
42 | |||
43 | #include <linux/wait.h> |
||
44 | #include <linux/crypto.h> |
||
45 | #include <linux/kernel.h> |
||
46 | #include <linux/interrupt.h> |
||
47 | #include <linux/spinlock.h> |
||
48 | #include <linux/list.h> |
||
49 | #include <crypto/ctr.h> |
||
50 | #include <crypto/aes.h> |
||
51 | #include <crypto/algapi.h> |
||
52 | #include <crypto/scatterwalk.h> |
||
53 | |||
54 | #include <asm/ifx/ifx_regs.h> |
||
55 | #include <asm/ifx/ifx_types.h> |
||
56 | #include <asm/ifx/common_routines.h> |
||
57 | #include <asm/ifx/irq.h> |
||
58 | #include <asm/ifx/ifx_pmu.h> |
||
59 | #include <asm/ifx/ifx_gpio.h> |
||
60 | #include <asm/kmap_types.h> |
||
61 | |||
62 | #include "ifxmips_deu.h" |
||
63 | |||
64 | #if defined(CONFIG_DANUBE) |
||
65 | #include "ifxmips_deu_danube.h" |
||
66 | extern int ifx_danube_pre_1_4; |
||
67 | #elif defined(CONFIG_AR9) |
||
68 | #include "ifxmips_deu_ar9.h" |
||
69 | #elif defined(CONFIG_VR9) || defined(CONFIG_AR10) |
||
70 | #include "ifxmips_deu_vr9.h" |
||
71 | #else |
||
72 | #error "Unkown platform" |
||
73 | #endif |
||
74 | |||
75 | /* DMA specific header and variables */ |
||
76 | |||
77 | spinlock_t des_lock; |
||
78 | #define CRTCL_SECT_INIT spin_lock_init(&des_lock) |
||
79 | #define CRTCL_SECT_START spin_lock_irqsave(&des_lock, flag) |
||
80 | #define CRTCL_SECT_END spin_unlock_irqrestore(&des_lock, flag) |
||
81 | |||
82 | /* Preprocessor declerations */ |
||
83 | #ifdef CRYPTO_DEBUG |
||
84 | extern char debug_level; |
||
85 | #define DPRINTF(level, format, args...) if (level < debug_level) printk(KERN_INFO "[%s %s %d]: " format, __FILE__, __func__, __LINE__, ##args); |
||
86 | #else |
||
87 | #define DPRINTF(level, format, args...) |
||
88 | #endif |
||
89 | //#define DES_3DES_START IFX_DES_CON |
||
90 | #define DES_KEY_SIZE 8 |
||
91 | #define DES_EXPKEY_WORDS 32 |
||
92 | #define DES_BLOCK_SIZE 8 |
||
93 | #define DES3_EDE_KEY_SIZE (3 * DES_KEY_SIZE) |
||
94 | #define DES3_EDE_EXPKEY_WORDS (3 * DES_EXPKEY_WORDS) |
||
95 | #define DES3_EDE_BLOCK_SIZE DES_BLOCK_SIZE |
||
96 | |||
97 | /* Function Declaration to prevent warning messages */ |
||
98 | void des_chip_init (void); |
||
99 | u32 endian_swap(u32 input); |
||
100 | u32 input_swap(u32 input); |
||
101 | int aes_memory_allocate(int value); |
||
102 | int des_memory_allocate(int value); |
||
103 | void memory_release(u32 *buffer); |
||
104 | u32* memory_alignment(const u8 *arg, u32 *buff_alloc, int in_out, int nbytes); |
||
105 | void aes_dma_memory_copy(u32 *outcopy, u32 *out_dma, u8 *out_arg, int nbytes); |
||
106 | void des_dma_memory_copy(u32 *outcopy, u32 *out_dma, u8 *out_arg, int nbytes); |
||
107 | |||
108 | static int lq_deu_des_core (void *ctx_arg, u8 *out_arg, const u8 *in_arg, |
||
109 | u8 *iv_arg, u32 nbytes, int encdec, int mode); |
||
110 | |||
111 | struct des_ctx { |
||
112 | int controlr_M; |
||
113 | int key_length; |
||
114 | u8 iv[DES_BLOCK_SIZE]; |
||
115 | u32 expkey[DES3_EDE_EXPKEY_WORDS]; |
||
116 | }; |
||
117 | |||
118 | |||
119 | static int disable_multiblock = 0; |
||
120 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0) |
||
121 | module_param(disable_multiblock, int, 0); |
||
122 | #else |
||
123 | MODULE_PARM_DESC(disable_multiblock, "Disable encryption of whole multiblock buffers"); |
||
124 | #endif |
||
125 | |||
126 | static int disable_deudma = 1; |
||
127 | |||
128 | struct des_container { |
||
129 | u8 *iv; |
||
130 | u8 *dst_buf; |
||
131 | u8 *src_buf; |
||
132 | int mode; |
||
133 | int encdec; |
||
134 | int complete; |
||
135 | int flag; |
||
136 | |||
137 | u32 bytes_processed; |
||
138 | u32 nbytes; |
||
139 | |||
140 | struct ablkcipher_request arequest; |
||
141 | }; |
||
142 | |||
143 | des_priv_t *des_queue; |
||
144 | extern deu_drv_priv_t deu_dma_priv; |
||
145 | |||
146 | void hexdump1(unsigned char *buf, unsigned int len) |
||
147 | { |
||
148 | print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET, |
||
149 | 16, 1, |
||
150 | buf, len, false); |
||
151 | } |
||
152 | |||
153 | |||
154 | /*! \fn int lq_des_setkey(struct crypto_ablkcipher *tfm, const u8 *key, unsigned int keylen) |
||
155 | * \ingroup IFX_DES_FUNCTIONS |
||
156 | * \brief sets DES key |
||
157 | * \param tfm linux crypto algo transform |
||
158 | * \param key input key |
||
159 | * \param keylen key length |
||
160 | */ |
||
161 | static int lq_des_setkey(struct crypto_ablkcipher *tfm, const u8 *key, |
||
162 | unsigned int keylen) |
||
163 | { |
||
164 | struct des_ctx *dctx = crypto_ablkcipher_ctx(tfm); |
||
165 | |||
166 | //printk("setkey in %s\n", __FILE__); |
||
167 | |||
168 | dctx->controlr_M = 0; // des |
||
169 | dctx->key_length = keylen; |
||
170 | |||
171 | memcpy ((u8 *) (dctx->expkey), key, keylen); |
||
172 | |||
173 | return 0; |
||
174 | } |
||
175 | |||
176 | /*! \fn int lq_des3_ede_setkey(struct crypto_ablkcipher *tfm, const u8 *key, unsigned int keylen) |
||
177 | * \ingroup IFX_DES_FUNCTIONS |
||
178 | * \brief sets DES key |
||
179 | * \param tfm linux crypto algo transform |
||
180 | * \param key input key |
||
181 | * \param keylen key length |
||
182 | */ |
||
183 | |||
184 | static int lq_des3_ede_setkey(struct crypto_ablkcipher *tfm, const u8 *in_key, |
||
185 | unsigned int keylen) |
||
186 | { |
||
187 | struct des_ctx *dctx = crypto_ablkcipher_ctx(tfm); |
||
188 | |||
189 | //printk("setkey in %s\n", __FILE__); |
||
190 | |||
191 | dctx->controlr_M = keylen/8 + 1; // des |
||
192 | dctx->key_length = keylen; |
||
193 | |||
194 | memcpy ((u8 *) (dctx->expkey), in_key, keylen); |
||
195 | |||
196 | return 0; |
||
197 | } |
||
198 | |||
199 | /*! \fn void ifx_deu_des_core(void *ctx_arg, u8 *out_arg, const u8 *in_arg, u8 *iv_arg, u32 nbytes, int encdec, int mode) |
||
200 | * \ingroup IFX_DES_FUNCTIONS |
||
201 | * \brief main interface to DES hardware |
||
202 | * \param ctx_arg crypto algo context |
||
203 | * \param out_arg output bytestream |
||
204 | * \param in_arg input bytestream |
||
205 | * \param iv_arg initialization vector |
||
206 | * \param nbytes length of bytestream |
||
207 | * \param encdec 1 for encrypt; 0 for decrypt |
||
208 | * \param mode operation mode such as ebc, cbc |
||
209 | */ |
||
210 | |||
211 | static int lq_deu_des_core (void *ctx_arg, u8 *out_arg, const u8 *in_arg, |
||
212 | u8 *iv_arg, u32 nbytes, int encdec, int mode) |
||
213 | { |
||
214 | volatile struct des_t *des = (struct des_t *) DES_3DES_START; |
||
215 | struct des_ctx *dctx = ctx_arg; |
||
216 | u32 *key = dctx->expkey; |
||
217 | unsigned long flag; |
||
218 | |||
219 | int i = 0; |
||
220 | int nblocks = 0; |
||
221 | |||
222 | CRTCL_SECT_START; |
||
223 | |||
224 | des->controlr.M = dctx->controlr_M; |
||
225 | if (dctx->controlr_M == 0) // des |
||
226 | { |
||
227 | des->K1HR = DEU_ENDIAN_SWAP(*((u32 *) key + 0)); |
||
228 | des->K1LR = DEU_ENDIAN_SWAP(*((u32 *) key + 1)); |
||
229 | |||
230 | } |
||
231 | else { |
||
232 | /* Hardware Section */ |
||
233 | switch (dctx->key_length) { |
||
234 | case 24: |
||
235 | des->K3HR = DEU_ENDIAN_SWAP(*((u32 *) key + 4)); |
||
236 | des->K3LR = DEU_ENDIAN_SWAP(*((u32 *) key + 5)); |
||
237 | /* no break; */ |
||
238 | |||
239 | case 16: |
||
240 | des->K2HR = DEU_ENDIAN_SWAP(*((u32 *) key + 2)); |
||
241 | des->K2LR = DEU_ENDIAN_SWAP(*((u32 *) key + 3)); |
||
242 | |||
243 | /* no break; */ |
||
244 | case 8: |
||
245 | des->K1HR = DEU_ENDIAN_SWAP(*((u32 *) key + 0)); |
||
246 | des->K1LR = DEU_ENDIAN_SWAP(*((u32 *) key + 1)); |
||
247 | break; |
||
248 | |||
249 | default: |
||
250 | CRTCL_SECT_END; |
||
251 | return -EINVAL; |
||
252 | } |
||
253 | } |
||
254 | |||
255 | des->controlr.E_D = !encdec; //encryption |
||
256 | des->controlr.O = mode; //0 ECB 1 CBC 2 OFB 3 CFB 4 CTR hexdump(prin,sizeof(*des)); |
||
257 | |||
258 | if (mode > 0) { |
||
259 | des->IVHR = DEU_ENDIAN_SWAP(*(u32 *) iv_arg); |
||
260 | des->IVLR = DEU_ENDIAN_SWAP(*((u32 *) iv_arg + 1)); |
||
261 | }; |
||
262 | |||
263 | /* memory alignment issue */ |
||
264 | dword_mem_aligned_in = (u32 *) DEU_DWORD_REORDERING(in_arg, des_buff_in, BUFFER_IN, nbytes); |
||
265 | |||
266 | deu_priv->deu_rx_buf = (u32 *) out_arg; |
||
267 | deu_priv->deu_rx_len = nbytes; |
||
268 | |||
269 | dma->controlr.ALGO = 0; //DES |
||
270 | des->controlr.DAU = 0; |
||
271 | dma->controlr.BS = 0; |
||
272 | dma->controlr.EN = 1; |
||
273 | |||
274 | while (des->controlr.BUS) { |
||
275 | }; |
||
276 | |||
277 | wlen = dma_device_write (dma_device, (u8 *) dword_mem_aligned_in, nbytes, NULL); |
||
278 | if (wlen != nbytes) { |
||
279 | dma->controlr.EN = 0; |
||
280 | CRTCL_SECT_END; |
||
281 | printk (KERN_ERR "[%s %s %d]: dma_device_write fail!\n", __FILE__, __func__, __LINE__); |
||
282 | return -EINVAL; |
||
283 | } |
||
284 | |||
285 | |||
286 | /* Prepare Rx buf length used in dma psuedo interrupt */ |
||
287 | outcopy = (u32 *) DEU_DWORD_REORDERING(out_arg, des_buff_out, BUFFER_OUT, nbytes); |
||
288 | deu_priv->outcopy = outcopy; |
||
289 | deu_priv->event_src = DES_ASYNC_EVENT; |
||
290 | |||
291 | if (mode > 0) { |
||
292 | *(u32 *) iv_arg = DEU_ENDIAN_SWAP(des->IVHR); |
||
293 | *((u32 *) iv_arg + 1) = DEU_ENDIAN_SWAP(des->IVLR); |
||
294 | }; |
||
295 | |||
296 | CRTCL_SECT_END; |
||
297 | |||
298 | return -EINPROGRESS; |
||
299 | |||
300 | } |
||
301 | |||
302 | static int count_sgs(struct scatterlist *sl, unsigned int total_bytes) |
||
303 | { |
||
304 | int i = 0; |
||
305 | |||
306 | do { |
||
307 | total_bytes -= sl[i].length; |
||
308 | i++; |
||
309 | |||
310 | } while (total_bytes > 0); |
||
311 | |||
312 | return i; |
||
313 | } |
||
314 | |||
315 | /* \fn static inline struct des_container *des_container_cast ( |
||
316 | * struct scatterlist *dst) |
||
317 | * \ingroup IFX_DES_FUNCTIONS |
||
318 | * \brief Locate the structure des_container in memory. |
||
319 | * \param *areq Pointer to memory location where ablkcipher_request is located |
||
320 | * \return *des_cointainer The function pointer to des_container |
||
321 | */ |
||
322 | |||
323 | static inline struct des_container *des_container_cast( |
||
324 | struct ablkcipher_request *areq) |
||
325 | { |
||
326 | return container_of(areq, struct des_container, arequest); |
||
327 | } |
||
328 | |||
329 | /* \fn static void lq_sg_complete(struct des_container *des_con) |
||
330 | * \ingroup IFX_DES_FUNCTIONS |
||
331 | * \brief Free the used up memory after encryt/decrypt. |
||
332 | */ |
||
333 | |||
334 | static void lq_sg_complete(struct des_container *des_con) |
||
335 | { |
||
336 | unsigned long queue_flag; |
||
337 | |||
338 | spin_lock_irqsave(&des_queue->lock, queue_flag); |
||
339 | kfree(des_con); |
||
340 | spin_unlock_irqrestore(&des_queue->lock, queue_flag); |
||
341 | } |
||
342 | |||
343 | /* \fn void lq_sg_init(struct scatterlist *src, |
||
344 | * struct scatterlist *dst) |
||
345 | * \ingroup IFX_DES_FUNCTIONS |
||
346 | * \brief Maps the scatterlists into a source/destination page. |
||
347 | * \param *src Pointer to the source scatterlist |
||
348 | * \param *dst Pointer to the destination scatterlist |
||
349 | */ |
||
350 | |||
351 | static void lq_sg_init(struct des_container *des_con, struct scatterlist *src, |
||
352 | struct scatterlist *dst) |
||
353 | { |
||
354 | struct page *dst_page, *src_page; |
||
355 | |||
356 | src_page = sg_virt(src); |
||
357 | des_con->src_buf = (char *) src_page; |
||
358 | |||
359 | dst_page = sg_virt(dst); |
||
360 | des_con->dst_buf = (char *) dst_page; |
||
361 | } |
||
362 | |||
363 | /* \fn static int process_next_packet(struct des_container *des_con, struct ablkcipher_request *areq, |
||
364 | * int state) |
||
365 | * \ingroup IFX_DES_FUNCTIONS |
||
366 | * \brief Process the next packet after dequeuing the packet from crypto queue |
||
367 | * \param *des_con Pointer to DES container structure |
||
368 | * \param *areq Pointer to ablkcipher_request container |
||
369 | * \param state State of the packet (scattered packet or new packet to be processed) |
||
370 | * \return -EINVAL: DEU failure, -EINPROGRESS: DEU encrypt/decrypt in progress, 1: no scatterlist left |
||
371 | */ |
||
372 | |||
373 | static int process_next_packet(struct des_container *des_con, struct ablkcipher_request *areq, |
||
374 | int state) |
||
375 | { |
||
376 | u8 *iv; |
||
377 | int mode, encdec, err = -EINVAL; |
||
378 | u32 remain, inc, chunk_size, nbytes; |
||
379 | struct scatterlist *src = NULL; |
||
380 | struct scatterlist *dst = NULL; |
||
381 | struct crypto_ablkcipher *cipher; |
||
382 | struct des_ctx *ctx; |
||
383 | unsigned long queue_flag; |
||
384 | |||
385 | spin_lock_irqsave(&des_queue->lock, queue_flag); |
||
386 | |||
387 | mode = des_con->mode; |
||
388 | encdec = des_con->encdec; |
||
389 | iv = des_con->iv; |
||
390 | |||
391 | if (state & PROCESS_SCATTER) { |
||
392 | src = scatterwalk_sg_next(areq->src); |
||
393 | dst = scatterwalk_sg_next(areq->dst); |
||
394 | |||
395 | if (!src || !dst) { |
||
396 | spin_unlock_irqrestore(&des_queue->lock, queue_flag); |
||
397 | return 1; |
||
398 | } |
||
399 | } |
||
400 | else if (state & PROCESS_NEW_PACKET) { |
||
401 | src = areq->src; |
||
402 | dst = areq->dst; |
||
403 | } |
||
404 | |||
405 | remain = des_con->bytes_processed; |
||
406 | chunk_size = src->length; |
||
407 | |||
408 | //printk("debug ln: %d, func: %s, reqsize: %d, scattersize: %d\n", |
||
409 | // __LINE__, __func__, areq->nbytes, chunk_size); |
||
410 | |||
411 | if (remain > DEU_MAX_PACKET_SIZE) |
||
412 | inc = DEU_MAX_PACKET_SIZE; |
||
413 | else if(remain > chunk_size) |
||
414 | inc = chunk_size; |
||
415 | else |
||
416 | inc = remain; |
||
417 | |||
418 | remain -= inc; |
||
419 | des_con->nbytes = inc; |
||
420 | |||
421 | if (state & PROCESS_SCATTER) { |
||
422 | des_con->src_buf += des_con->nbytes; |
||
423 | des_con->dst_buf += des_con->nbytes; |
||
424 | } |
||
425 | |||
426 | lq_sg_init(des_con, src, dst); |
||
427 | |||
428 | nbytes = des_con->nbytes; |
||
429 | |||
430 | cipher = crypto_ablkcipher_reqtfm(areq); |
||
431 | ctx = crypto_ablkcipher_ctx(cipher); |
||
432 | |||
433 | if (des_queue->hw_status == DES_IDLE) { |
||
434 | des_queue->hw_status = DES_STARTED; |
||
435 | } |
||
436 | |||
437 | des_con->bytes_processed -= des_con->nbytes; |
||
438 | err = ablkcipher_enqueue_request(&des_queue->list, &des_con->arequest); |
||
439 | if (err == -EBUSY) { |
||
440 | printk("Failed to enqueue request, ln: %d, err: %d\n", |
||
441 | __LINE__, err); |
||
442 | spin_unlock_irqrestore(&des_queue->lock, queue_flag); |
||
443 | return -EINVAL; |
||
444 | } |
||
445 | |||
446 | spin_unlock_irqrestore(&des_queue->lock, queue_flag); |
||
447 | err = lq_deu_des_core(ctx, des_con->dst_buf, des_con->src_buf, iv, nbytes, encdec, mode); |
||
448 | |||
449 | return err; |
||
450 | } |
||
451 | |||
452 | /* \fn static void process_queue(unsigned long data) |
||
453 | * \ingroup IFX_DES_FUNCTIONS |
||
454 | * \brief Process next packet in queue |
||
455 | * \param data not used |
||
456 | * \return |
||
457 | */ |
||
458 | |||
459 | static void process_queue(unsigned long data) |
||
460 | { |
||
461 | DEU_WAKEUP_EVENT(deu_dma_priv.deu_thread_wait, DES_ASYNC_EVENT, |
||
462 | deu_dma_priv.des_event_flags); |
||
463 | |||
464 | } |
||
465 | |||
466 | /* \fn static int des_crypto_thread (void *data) |
||
467 | * \ingroup IFX_DES_FUNCTIONS |
||
468 | * \brief DES thread that handles crypto requests from upper layer & DMA |
||
469 | * \param *data Not used |
||
470 | * \return -EINVAL: DEU failure, -EBUSY: DEU HW busy, 0: exit thread |
||
471 | */ |
||
472 | |||
473 | static int des_crypto_thread(void *data) |
||
474 | { |
||
475 | struct des_container *des_con = NULL; |
||
476 | struct ablkcipher_request *areq = NULL; |
||
477 | int err; |
||
478 | unsigned long queue_flag; |
||
479 | |||
480 | daemonize("lq_des_thread"); |
||
481 | |||
482 | while (1) |
||
483 | { |
||
484 | DEU_WAIT_EVENT(deu_dma_priv.deu_thread_wait, DES_ASYNC_EVENT, |
||
485 | deu_dma_priv.des_event_flags); |
||
486 | spin_lock_irqsave(&des_queue->lock, queue_flag); |
||
487 | |||
488 | /* wait to prevent starting a crypto session before |
||
489 | * exiting the dma interrupt thread. |
||
490 | */ |
||
491 | |||
492 | if (des_queue->hw_status == DES_STARTED) { |
||
493 | areq = ablkcipher_dequeue_request(&des_queue->list); |
||
494 | des_con = des_container_cast(areq); |
||
495 | des_queue->hw_status = DES_BUSY; |
||
496 | } |
||
497 | else if (des_queue->hw_status == DES_IDLE) { |
||
498 | areq = ablkcipher_dequeue_request(&des_queue->list); |
||
499 | des_con = des_container_cast(areq); |
||
500 | des_queue->hw_status = DES_STARTED; |
||
501 | } |
||
502 | else if (des_queue->hw_status == DES_BUSY) { |
||
503 | areq = ablkcipher_dequeue_request(&des_queue->list); |
||
504 | des_con = des_container_cast(areq); |
||
505 | } |
||
506 | else if (des_queue->hw_status == DES_COMPLETED) { |
||
507 | areq->base.complete(&areq->base, 0); |
||
508 | lq_sg_complete(des_con); |
||
509 | des_queue->hw_status = DES_IDLE; |
||
510 | spin_unlock_irqrestore(&des_queue->lock, queue_flag); |
||
511 | return 0; |
||
512 | } |
||
513 | spin_unlock_irqrestore(&des_queue->lock, queue_flag); |
||
514 | |||
515 | if ((des_con->bytes_processed == 0)) { |
||
516 | goto des_done; |
||
517 | } |
||
518 | |||
519 | if (!des_con) { |
||
520 | goto des_done; |
||
521 | } |
||
522 | |||
523 | if (des_con->flag & PROCESS_NEW_PACKET) { |
||
524 | des_con->flag = PROCESS_SCATTER; |
||
525 | err = process_next_packet(des_con, areq, PROCESS_NEW_PACKET); |
||
526 | } |
||
527 | else |
||
528 | err = process_next_packet(des_con, areq, PROCESS_SCATTER); |
||
529 | |||
530 | if (err == -EINVAL) { |
||
531 | areq->base.complete(&areq->base, err); |
||
532 | lq_sg_complete(des_con); |
||
533 | printk("src/dst returned -EINVAL in func: %s\n", __func__); |
||
534 | } |
||
535 | else if (err > 0) { |
||
536 | printk("src/dst returned zero in func: %s\n", __func__); |
||
537 | goto des_done; |
||
538 | } |
||
539 | |||
540 | continue; |
||
541 | |||
542 | des_done: |
||
543 | //printk("debug line - %d, func: %s, qlen: %d\n", __LINE__, __func__, des_queue->list.qlen); |
||
544 | areq->base.complete(&areq->base, 0); |
||
545 | lq_sg_complete(des_con); |
||
546 | |||
547 | spin_lock_irqsave(&des_queue->lock, queue_flag); |
||
548 | if (des_queue->list.qlen > 0) { |
||
549 | spin_unlock_irqrestore(&des_queue->lock, queue_flag); |
||
550 | tasklet_schedule(&des_queue->des_task); |
||
551 | } |
||
552 | else { |
||
553 | des_queue->hw_status = DES_IDLE; |
||
554 | spin_unlock_irqrestore(&des_queue->lock, queue_flag); |
||
555 | } |
||
556 | } // while(1) |
||
557 | |||
558 | return 0; |
||
559 | |||
560 | } |
||
561 | |||
562 | /* \fn static int lq_des_queue_mgr(struct des_ctx *ctx, struct ablkcipher_request *areq, |
||
563 | u8 *iv, int encdec, int mode) |
||
564 | * \ingroup IFX_DES_FUNCTIONS |
||
565 | * \brief starts the process of queuing DEU requests |
||
566 | * \param *ctx crypto algo contax |
||
567 | * \param *areq Pointer to the balkcipher requests |
||
568 | * \param *iv Pointer to intput vector location |
||
569 | * \param dir Encrypt/Decrypt |
||
570 | * \mode The mode DES algo is running |
||
571 | * \return 0 if success |
||
572 | */ |
||
573 | |||
574 | static int lq_queue_mgr(struct des_ctx *ctx, struct ablkcipher_request *areq, |
||
575 | u8 *iv, int encdec, int mode) |
||
576 | { |
||
577 | int err = -EINVAL; |
||
578 | unsigned long queue_flag; |
||
579 | struct scatterlist *src = areq->src; |
||
580 | struct scatterlist *dst = areq->dst; |
||
581 | struct des_container *des_con = NULL; |
||
582 | u32 remain, inc, nbytes = areq->nbytes; |
||
583 | u32 chunk_bytes = src->length; |
||
584 | |||
585 | des_con = (struct des_container *)kmalloc(sizeof(struct des_container), |
||
586 | GFP_KERNEL); |
||
587 | |||
588 | if (!(des_con)) { |
||
589 | printk("Cannot allocate memory for AES container, fn %s, ln %d\n", |
||
590 | __func__, __LINE__); |
||
591 | return -ENOMEM; |
||
592 | } |
||
593 | |||
594 | /* DES encrypt/decrypt mode */ |
||
595 | if (mode == 5) { |
||
596 | nbytes = DES_BLOCK_SIZE; |
||
597 | chunk_bytes = DES_BLOCK_SIZE; |
||
598 | mode = 0; |
||
599 | } |
||
600 | |||
601 | des_con->bytes_processed = nbytes; |
||
602 | des_con->arequest = (*areq); |
||
603 | remain = nbytes; |
||
604 | |||
605 | //printk("debug - Line: %d, func: %s, reqsize: %d, scattersize: %d\n", |
||
606 | // __LINE__, __func__, nbytes, chunk_bytes); |
||
607 | |||
608 | if (remain > DEU_MAX_PACKET_SIZE) |
||
609 | inc = DEU_MAX_PACKET_SIZE; |
||
610 | else if(remain > chunk_bytes) |
||
611 | inc = chunk_bytes; |
||
612 | else |
||
613 | inc = remain; |
||
614 | |||
615 | remain -= inc; |
||
616 | lq_sg_init(des_con, src, dst); |
||
617 | |||
618 | if (remain <= 0 ) { |
||
619 | des_con->complete = 1; |
||
620 | } |
||
621 | else |
||
622 | des_con->complete = 0; |
||
623 | |||
624 | des_con->nbytes = inc; |
||
625 | des_con->iv = iv; |
||
626 | des_con->mode = mode; |
||
627 | des_con->encdec = encdec; |
||
628 | |||
629 | spin_lock_irqsave(&des_queue->lock, queue_flag); |
||
630 | |||
631 | if (des_queue->hw_status == DES_STARTED || des_queue->hw_status == DES_BUSY || |
||
632 | des_queue->list.qlen > 0) { |
||
633 | |||
634 | des_con->flag = PROCESS_NEW_PACKET; |
||
635 | err = ablkcipher_enqueue_request(&des_queue->list, &des_con->arequest); |
||
636 | if (err == -EBUSY) { |
||
637 | spin_unlock_irqrestore(&des_queue->lock, queue_flag); |
||
638 | printk("Fail to enqueue ablkcipher request ln: %d, err: %d\n", |
||
639 | __LINE__, err); |
||
640 | return err; |
||
641 | } |
||
642 | |||
643 | spin_unlock_irqrestore(&des_queue->lock, queue_flag); |
||
644 | return -EINPROGRESS; |
||
645 | |||
646 | } |
||
647 | else if (des_queue->hw_status == DES_IDLE) { |
||
648 | des_queue->hw_status = DES_STARTED; |
||
649 | } |
||
650 | |||
651 | des_con->flag = PROCESS_SCATTER; |
||
652 | des_con->bytes_processed -= des_con->nbytes; |
||
653 | |||
654 | err = ablkcipher_enqueue_request(&des_queue->list, &des_con->arequest); |
||
655 | if (err == -EBUSY) { |
||
656 | printk("Fail to enqueue ablkcipher request ln: %d, err: %d\n", |
||
657 | __LINE__, err); |
||
658 | |||
659 | spin_unlock_irqrestore(&des_queue->lock, queue_flag); |
||
660 | return err; |
||
661 | } |
||
662 | |||
663 | spin_unlock_irqrestore(&des_queue->lock, queue_flag); |
||
664 | return lq_deu_des_core(ctx, des_con->dst_buf, des_con->src_buf, iv, inc, encdec, mode); |
||
665 | |||
666 | } |
||
667 | |||
668 | /* \fn static int lq_des_encrypt(struct ablkcipher_request *areq) |
||
669 | * \ingroup IFX_DES_FUNCTIONS |
||
670 | * \brief Decrypt function for DES algo |
||
671 | * \param *areq Pointer to ablkcipher request in memory |
||
672 | * \return 0 is success, -EINPROGRESS if encryting, EINVAL if failure |
||
673 | */ |
||
674 | |||
675 | static int lq_des_encrypt(struct ablkcipher_request *areq) |
||
676 | { |
||
677 | struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq); |
||
678 | struct des_ctx *ctx = crypto_ablkcipher_ctx(cipher); |
||
679 | |||
680 | return lq_queue_mgr(ctx, areq, NULL, CRYPTO_DIR_ENCRYPT, 5); |
||
681 | |||
682 | } |
||
683 | |||
684 | /* \fn static int lq_des_decrypt(struct ablkcipher_request *areq) |
||
685 | * \ingroup IFX_DES_FUNCTIONS |
||
686 | * \brief Decrypt function for DES algo |
||
687 | * \param *areq Pointer to ablkcipher request in memory |
||
688 | * \return 0 is success, -EINPROGRESS if encryting, EINVAL if failure |
||
689 | */ |
||
690 | |||
691 | static int lq_des_decrypt(struct ablkcipher_request *areq) |
||
692 | { |
||
693 | struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq); |
||
694 | struct des_ctx *ctx = crypto_ablkcipher_ctx(cipher); |
||
695 | |||
696 | return lq_queue_mgr(ctx, areq, NULL, CRYPTO_DIR_DECRYPT, 5); |
||
697 | } |
||
698 | |||
699 | /* \fn static int lq_ecb_des_encrypt(struct ablkcipher_request *areq) |
||
700 | * \ingroup IFX_DES_FUNCTIONS |
||
701 | * \brief Decrypt function for DES algo |
||
702 | * \param *areq Pointer to ablkcipher request in memory |
||
703 | * \return 0 is success, -EINPROGRESS if encryting, EINVAL if failure |
||
704 | */ |
||
705 | |||
706 | static int lq_ecb_des_encrypt(struct ablkcipher_request *areq) |
||
707 | { |
||
708 | struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq); |
||
709 | struct des_ctx *ctx = crypto_ablkcipher_ctx(cipher); |
||
710 | |||
711 | return lq_queue_mgr(ctx, areq, areq->info, CRYPTO_DIR_ENCRYPT, 0); |
||
712 | } |
||
713 | |||
714 | /* \fn static int lq_ecb_des_decrypt(struct ablkcipher_request *areq) |
||
715 | * \ingroup IFX_DES_FUNCTIONS |
||
716 | * \brief Decrypt function for DES algo |
||
717 | * \param *areq Pointer to ablkcipher request in memory |
||
718 | * \return 0 is success, -EINPROGRESS if encryting, EINVAL if failure |
||
719 | */ |
||
720 | static int lq_ecb_des_decrypt(struct ablkcipher_request *areq) |
||
721 | { |
||
722 | struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq); |
||
723 | struct des_ctx *ctx = crypto_ablkcipher_ctx(cipher); |
||
724 | |||
725 | return lq_queue_mgr(ctx, areq, areq->info, CRYPTO_DIR_DECRYPT, 0); |
||
726 | |||
727 | } |
||
728 | |||
729 | /* \fn static int lq_cbc_ecb_des_encrypt(struct ablkcipher_request *areq) |
||
730 | * \ingroup IFX_DES_FUNCTIONS |
||
731 | * \brief Decrypt function for DES algo |
||
732 | * \param *areq Pointer to ablkcipher request in memory |
||
733 | * \return 0 is success, -EINPROGRESS if encryting, EINVAL if failure |
||
734 | */ |
||
735 | |||
736 | static int lq_cbc_des_encrypt(struct ablkcipher_request *areq) |
||
737 | { |
||
738 | struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq); |
||
739 | struct des_ctx *ctx = crypto_ablkcipher_ctx(cipher); |
||
740 | |||
741 | return lq_queue_mgr(ctx, areq, areq->info, CRYPTO_DIR_ENCRYPT, 1); |
||
742 | } |
||
743 | /* \fn static int lq_cbc_des_decrypt(struct ablkcipher_request *areq) |
||
744 | * \ingroup IFX_DES_FUNCTIONS |
||
745 | * \brief Decrypt function for DES algo |
||
746 | * \param *areq Pointer to ablkcipher request in memory |
||
747 | * \return 0 is success, -EINPROGRESS if encryting, EINVAL if failure |
||
748 | */ |
||
749 | |||
750 | static int lq_cbc_des_decrypt(struct ablkcipher_request *areq) |
||
751 | { |
||
752 | struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq); |
||
753 | struct des_ctx *ctx = crypto_ablkcipher_ctx(cipher); |
||
754 | |||
755 | return lq_queue_mgr(ctx, areq, areq->info, CRYPTO_DIR_DECRYPT, 1); |
||
756 | } |
||
757 | |||
758 | struct lq_des_alg { |
||
759 | struct crypto_alg alg; |
||
760 | }; |
||
761 | |||
762 | /* DES Supported algo array */ |
||
763 | static struct lq_des_alg des_drivers_alg [] = { |
||
764 | { |
||
765 | .alg = { |
||
766 | .cra_name = "des", |
||
767 | .cra_driver_name = "lqdeu-des", |
||
768 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
||
769 | .cra_blocksize = DES_BLOCK_SIZE, |
||
770 | .cra_ctxsize = sizeof(struct des_ctx), |
||
771 | .cra_type = &crypto_ablkcipher_type, |
||
772 | .cra_priority = 300, |
||
773 | .cra_module = THIS_MODULE, |
||
774 | .cra_ablkcipher = { |
||
775 | .setkey = lq_des_setkey, |
||
776 | .encrypt = lq_des_encrypt, |
||
777 | .decrypt = lq_des_decrypt, |
||
778 | .geniv = "eseqiv", |
||
779 | .min_keysize = DES_KEY_SIZE, |
||
780 | .max_keysize = DES_KEY_SIZE, |
||
781 | .ivsize = DES_BLOCK_SIZE, |
||
782 | } |
||
783 | } |
||
784 | |||
785 | },{ |
||
786 | .alg = { |
||
787 | .cra_name = "ecb(des)", |
||
788 | .cra_driver_name = "lqdeu-ecb(des)", |
||
789 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
||
790 | .cra_blocksize = DES_BLOCK_SIZE, |
||
791 | .cra_ctxsize = sizeof(struct des_ctx), |
||
792 | .cra_type = &crypto_ablkcipher_type, |
||
793 | .cra_priority = 400, |
||
794 | .cra_module = THIS_MODULE, |
||
795 | .cra_ablkcipher = { |
||
796 | .setkey = lq_des_setkey, |
||
797 | .encrypt = lq_ecb_des_encrypt, |
||
798 | .decrypt = lq_ecb_des_decrypt, |
||
799 | .geniv = "eseqiv", |
||
800 | .min_keysize = DES_KEY_SIZE, |
||
801 | .max_keysize = DES_KEY_SIZE, |
||
802 | .ivsize = DES_BLOCK_SIZE, |
||
803 | } |
||
804 | } |
||
805 | },{ |
||
806 | .alg = { |
||
807 | .cra_name = "cbc(des)", |
||
808 | .cra_driver_name = "lqdeu-cbc(des)", |
||
809 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
||
810 | .cra_blocksize = DES_BLOCK_SIZE, |
||
811 | .cra_ctxsize = sizeof(struct des_ctx), |
||
812 | .cra_type = &crypto_ablkcipher_type, |
||
813 | .cra_priority = 400, |
||
814 | .cra_module = THIS_MODULE, |
||
815 | .cra_ablkcipher = { |
||
816 | .setkey = lq_des_setkey, |
||
817 | .encrypt = lq_cbc_des_encrypt, |
||
818 | .decrypt = lq_cbc_des_decrypt, |
||
819 | .geniv = "eseqiv", |
||
820 | .min_keysize = DES3_EDE_KEY_SIZE, |
||
821 | .max_keysize = DES3_EDE_KEY_SIZE, |
||
822 | .ivsize = DES3_EDE_BLOCK_SIZE, |
||
823 | } |
||
824 | } |
||
825 | },{ |
||
826 | .alg = { |
||
827 | .cra_name = "des3_ede", |
||
828 | .cra_driver_name = "lqdeu-des3_ede", |
||
829 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
||
830 | .cra_blocksize = DES_BLOCK_SIZE, |
||
831 | .cra_ctxsize = sizeof(struct des_ctx), |
||
832 | .cra_type = &crypto_ablkcipher_type, |
||
833 | .cra_priority = 300, |
||
834 | .cra_module = THIS_MODULE, |
||
835 | .cra_ablkcipher = { |
||
836 | .setkey = lq_des3_ede_setkey, |
||
837 | .encrypt = lq_des_encrypt, |
||
838 | .decrypt = lq_des_decrypt, |
||
839 | .geniv = "eseqiv", |
||
840 | .min_keysize = DES_KEY_SIZE, |
||
841 | .max_keysize = DES_KEY_SIZE, |
||
842 | .ivsize = DES_BLOCK_SIZE, |
||
843 | } |
||
844 | } |
||
845 | },{ |
||
846 | .alg = { |
||
847 | .cra_name = "ecb(des3_ede)", |
||
848 | .cra_driver_name = "lqdeu-ecb(des3_ede)", |
||
849 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
||
850 | .cra_blocksize = DES_BLOCK_SIZE, |
||
851 | .cra_ctxsize = sizeof(struct des_ctx), |
||
852 | .cra_type = &crypto_ablkcipher_type, |
||
853 | .cra_priority = 400, |
||
854 | .cra_module = THIS_MODULE, |
||
855 | .cra_ablkcipher = { |
||
856 | .setkey = lq_des3_ede_setkey, |
||
857 | .encrypt = lq_ecb_des_encrypt, |
||
858 | .decrypt = lq_ecb_des_decrypt, |
||
859 | .geniv = "eseqiv", |
||
860 | .min_keysize = DES3_EDE_KEY_SIZE, |
||
861 | .max_keysize = DES3_EDE_KEY_SIZE, |
||
862 | .ivsize = DES3_EDE_BLOCK_SIZE, |
||
863 | } |
||
864 | } |
||
865 | },{ |
||
866 | .alg = { |
||
867 | .cra_name = "cbc(des3_ede)", |
||
868 | .cra_driver_name = "lqdeu-cbc(des3_ede)", |
||
869 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
||
870 | .cra_blocksize = DES_BLOCK_SIZE, |
||
871 | .cra_ctxsize = sizeof(struct des_ctx), |
||
872 | .cra_type = &crypto_ablkcipher_type, |
||
873 | .cra_priority = 400, |
||
874 | .cra_module = THIS_MODULE, |
||
875 | .cra_ablkcipher = { |
||
876 | .setkey = lq_des3_ede_setkey, |
||
877 | .encrypt = lq_cbc_des_encrypt, |
||
878 | .decrypt = lq_cbc_des_decrypt, |
||
879 | .geniv = "eseqiv", |
||
880 | .min_keysize = DES3_EDE_KEY_SIZE, |
||
881 | .max_keysize = DES3_EDE_KEY_SIZE, |
||
882 | .ivsize = DES3_EDE_BLOCK_SIZE, |
||
883 | } |
||
884 | } |
||
885 | } |
||
886 | }; |
||
887 | |||
888 | /*! \fn int __init lqdeu_async_des_init (void) |
||
889 | * \ingroup IFX_DES_FUNCTIONS |
||
890 | * \brief initialize des driver |
||
891 | */ |
||
892 | int __init lqdeu_async_des_init (void) |
||
893 | { |
||
894 | int i, j, ret = -EINVAL; |
||
895 | |||
896 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)) |
||
897 | if (!disable_multiblock) { |
||
898 | ifxdeu_des_alg.cra_u.cipher.cia_max_nbytes = DES_BLOCK_SIZE; //(size_t)-1; |
||
899 | ifxdeu_des_alg.cra_u.cipher.cia_req_align = 16; |
||
900 | ifxdeu_des_alg.cra_u.cipher.cia_ecb = ifx_deu_des_ecb; |
||
901 | ifxdeu_des_alg.cra_u.cipher.cia_cbc = ifx_deu_des_cbc; |
||
902 | ifxdeu_des_alg.cra_u.cipher.cia_cfb = ifx_deu_des_cfb; |
||
903 | ifxdeu_des_alg.cra_u.cipher.cia_ofb = ifx_deu_des_ofb; |
||
904 | } |
||
905 | #endif |
||
906 | for (i = 0; i < ARRAY_SIZE(des_drivers_alg); i++) { |
||
907 | ret = crypto_register_alg(&des_drivers_alg[i].alg); |
||
908 | //printk("driver: %s\n", des_drivers_alg[i].alg.cra_name); |
||
909 | if (ret) |
||
910 | goto des_err; |
||
911 | } |
||
912 | |||
913 | des_chip_init(); |
||
914 | CRTCL_SECT_INIT; |
||
915 | |||
916 | |||
917 | printk (KERN_NOTICE "IFX DEU DES initialized%s%s.\n", disable_multiblock ? "" : " (multiblock)", disable_deudma ? "" : " (DMA)"); |
||
918 | return ret; |
||
919 | |||
920 | des_err: |
||
921 | for (j = 0; j < i; j++) |
||
922 | crypto_unregister_alg(&des_drivers_alg[i].alg); |
||
923 | |||
924 | printk(KERN_ERR "Lantiq %s driver initialization failed!\n", (char *)&des_drivers_alg[i].alg.cra_driver_name); |
||
925 | return ret; |
||
926 | |||
927 | cbc_des3_ede_err: |
||
928 | for (i = 0; i < ARRAY_SIZE(des_drivers_alg); i++) { |
||
929 | if (!strcmp((char *)&des_drivers_alg[i].alg.cra_name, "cbc(des3_ede)")) |
||
930 | crypto_unregister_alg(&des_drivers_alg[i].alg); |
||
931 | } |
||
932 | |||
933 | printk(KERN_ERR "Lantiq %s driver initialization failed!\n", (char *)&des_drivers_alg[i].alg.cra_driver_name); |
||
934 | return ret; |
||
935 | } |
||
936 | |||
937 | /*! \fn void __exit lqdeu_fini_async_des (void) |
||
938 | * \ingroup IFX_DES_FUNCTIONS |
||
939 | * \brief unregister des driver |
||
940 | */ |
||
941 | void __exit lqdeu_fini_async_des (void) |
||
942 | { |
||
943 | int i; |
||
944 | |||
945 | for (i = 0; i < ARRAY_SIZE(des_drivers_alg); i++) |
||
946 | crypto_unregister_alg(&des_drivers_alg[i].alg); |
||
947 | |||
948 | des_queue->hw_status = DES_COMPLETED; |
||
949 | DEU_WAKEUP_EVENT(deu_dma_priv.deu_thread_wait, DES_ASYNC_EVENT, |
||
950 | deu_dma_priv.des_event_flags); |
||
951 | |||
952 | kfree(des_queue); |
||
953 | } |
||
954 |