ffceb2868d7262065950e7619349a4627dbe698a
[openwrt/svn-archive/archive.git] / target / linux / ifxmips / files / drivers / crypto / ifxmips / ifxmips_des.c
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
15 *
16 * Copyright (C) 2010 Ralph Hempel <ralph.hempel@lantiq.com>
17 * Copyright (C) 2009 Mohammad Firdaus
18 */
19
20 /*!
21 \defgroup IFX_DEU IFX_DEU_DRIVERS
22 \ingroup API
23 \brief ifx deu driver
24 */
25
26 /*!
27 \file ifxmips_des.c
28 \ingroup IFX_DEU
29 \brief DES encryption DEU driver file
30 */
31
32 /*!
33 \defgroup IFX_DES_FUNCTIONS IFX_DES_FUNCTIONS
34 \ingroup IFX_DEU
35 \brief IFX DES Encryption functions
36 */
37
38 /* Project Header Files */
39 #include <linux/version.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/types.h>
43 #include <linux/errno.h>
44 #include <linux/crypto.h>
45 #include <linux/interrupt.h>
46 #include <linux/delay.h>
47 #include <asm/byteorder.h>
48 #include <crypto/algapi.h>
49 #include "ifxmips_deu.h"
50
51 /* DMA specific header and variables */
52 #ifdef CONFIG_CRYPTO_DEV_IFXMIPS_DMA
53 #include "ifxmips_deu_dma.h"
54 #include <asm/ifx/irq.h>
55 #include <asm/ifx/ifx_dma_core.h>
56 extern _ifx_deu_device ifx_deu[1];
57 extern u32 *des_buff_in;
58 extern u32 *des_buff_out;
59 #ifndef CONFIG_CRYPTO_DEV_IFXMIPS_POLL_DMA
60 #define CONFIG_CRYPTO_DEV_IFXMIPS_POLL_DMA
61 #endif /* CONFIG_CRYPTO_DEV_IFXMIPS_POLL_DMA */
62 #endif /* CONFIG_CRYPTO_DEV_IFXMIPS_DMA */
63
64 spinlock_t des_lock;
65 #define CRTCL_SECT_INIT spin_lock_init(&des_lock)
66 #define CRTCL_SECT_START spin_lock_irqsave(&des_lock, flag)
67 #define CRTCL_SECT_END spin_unlock_irqrestore(&des_lock, flag)
68
69 /* Preprocessor declarations */
70 #define DES_3DES_START IFX_DES_CON
71 #define DES_KEY_SIZE 8
72 #define DES_EXPKEY_WORDS 32
73 #define DES_BLOCK_SIZE 8
74 #define DES3_EDE_KEY_SIZE (3 * DES_KEY_SIZE)
75 #define DES3_EDE_EXPKEY_WORDS (3 * DES_EXPKEY_WORDS)
76 #define DES3_EDE_BLOCK_SIZE DES_BLOCK_SIZE
77
78 /* Function Declaration to prevent warning messages */
79 void des_chip_init (void);
80 u32 endian_swap(u32 input);
81 u32 input_swap(u32 input);
82 int aes_memory_allocate(int value);
83 int des_memory_allocate(int value);
84 void memory_release(u32 *buffer);
85 u32* memory_alignment(const u8 *arg, u32 *buff_alloc, int in_out, int nbytes);
86 void aes_dma_memory_copy(u32 *outcopy, u32 *out_dma, u8 *out_arg, int nbytes);
87 void des_dma_memory_copy(u32 *outcopy, u32 *out_dma, u8 *out_arg, int nbytes);
88
89 #ifndef CONFIG_CRYPTO_DEV_IFXMIPS_DMA
90 void ifx_deu_des (void *ctx_arg, u8 *out_arg, const u8 *in_arg,
91 u8 *iv_arg, u32 nbytes, int encdec, int mode);
92 #else
93 void ifx_deu_des_core (void *ctx_arg, u8 *out_arg, const u8 *in_arg,
94 u8 *iv_arg, u32 nbytes, int encdec, int mode);
95 #endif /* CONFIG_CRYPTO_DEV_IFXMIPS_DMA */
96
97 struct des_ctx {
98 int controlr_M;
99 int key_length;
100 u8 iv[DES_BLOCK_SIZE];
101 u32 expkey[DES3_EDE_EXPKEY_WORDS];
102 };
103
104 extern int disable_deudma;
105
106 /*! \fn int des_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int key_len)
107 * \ingroup IFX_DES_FUNCTIONS
108 * \brief sets DES key
109 * \param tfm linux crypto algo transform
110 * \param key input key
111 * \param key_len key length
112 */
113 int des_setkey(struct crypto_tfm *tfm, const u8 *key,
114 unsigned int key_len)
115 {
116 struct des_ctx *ctx = crypto_tfm_ctx(tfm);
117
118 DPRINTF(0, "ctx @%p, key_len %d %d\n", ctx, key_len);
119
120 ctx->controlr_M = 0; /* des */
121 ctx->key_length = key_len;
122
123 memcpy ((u8 *) (ctx->expkey), key, key_len);
124
125 return 0;
126 }
127
128 #ifndef CONFIG_CRYPTO_DEV_IFXMIPS_DMA
129 /*! \fn void ifx_deu_des(void *ctx_arg, u8 *out_arg, const u8 *in_arg, u8 *iv_arg, u32 nbytes, int encdec, int mode)
130 * \ingroup IFX_DES_FUNCTIONS
131 * \brief main interface to DES hardware
132 * \param ctx_arg crypto algo context
133 * \param out_arg output bytestream
134 * \param in_arg input bytestream
135 * \param iv_arg initialization vector
136 * \param nbytes length of bytestream
137 * \param encdec 1 for encrypt; 0 for decrypt
138 * \param mode operation mode such as ebc, cbc
139 */
140
141 void ifx_deu_des (void *ctx_arg, u8 *out_arg, const u8 *in_arg,
142 u8 *iv_arg, u32 nbytes, int encdec, int mode)
143 #else
144 /*! \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)
145 * \ingroup IFX_DES_FUNCTIONS
146 * \brief main interface to DES hardware
147 * \param ctx_arg crypto algo context
148 * \param out_arg output bytestream
149 * \param in_arg input bytestream
150 * \param iv_arg initialization vector
151 * \param nbytes length of bytestream
152 * \param encdec 1 for encrypt; 0 for decrypt
153 * \param mode operation mode such as ebc, cbc
154 */
155
156 void ifx_deu_des_core (void *ctx_arg, u8 *out_arg, const u8 *in_arg,
157 u8 *iv_arg, u32 nbytes, int encdec, int mode)
158 #endif
159 {
160 volatile struct des_t *des = (struct des_t *) DES_3DES_START;
161 struct des_ctx *dctx = ctx_arg;
162 u32 *key = dctx->expkey;
163 ulong flag;
164
165 #ifndef CONFIG_CRYPTO_DEV_IFXMIPS_DMA
166 int i = 0;
167 int nblocks = 0;
168 #else
169 volatile struct deu_dma_t *dma = (struct deu_dma_t *) IFX_DEU_DMA_CON;
170 struct dma_device_info *dma_device = ifx_deu[0].dma_device;
171 //deu_drv_priv_t *deu_priv = (deu_drv_priv_t *)dma_device->priv;
172 int wlen = 0;
173 u32 *outcopy = NULL;
174 u32 *dword_mem_aligned_in = NULL;
175
176 #ifdef CONFIG_CRYPTO_DEV_IFXMIPS_POLL_DMA
177 u32 timeout = 0;
178 u32 *out_dma = NULL;
179 #endif
180
181 #endif
182
183 DPRINTF(0, "ctx @%p, mode %d, encdec %d\n", dctx, mode, encdec);
184
185 CRTCL_SECT_START;
186
187 des->controlr.E_D = !encdec; /* encryption */
188 des->controlr.O = mode; /* 0 ECB, 1 CBC, 2 OFB, 3 CFB, 4 CTR */
189 des->controlr.SM = 1; /* start after writing input register */
190 des->controlr.DAU = 0; /* Disable Automatic Update of init vector */
191 des->controlr.ARS = 1; /* Autostart Select - write to IHR */
192
193 des->controlr.M = dctx->controlr_M;
194 /* write keys */
195 if (dctx->controlr_M == 0) {
196 /* DES mode */
197 des->K1HR = DEU_ENDIAN_SWAP(*((u32 *) key + 0));
198 des->K1LR = DEU_ENDIAN_SWAP(*((u32 *) key + 1));
199 #ifdef CRYPTO_DEBUG
200 printk ("key1: %x\n", (*((u32 *) key + 0)));
201 printk ("key2: %x\n", (*((u32 *) key + 1)));
202 #endif
203 } else {
204 /* 3DES mode (EDE-x) */
205 switch (dctx->key_length) {
206 case 24:
207 des->K3HR = DEU_ENDIAN_SWAP(*((u32 *) key + 4));
208 des->K3LR = DEU_ENDIAN_SWAP(*((u32 *) key + 5));
209 /* no break; */
210 case 16:
211 des->K2HR = DEU_ENDIAN_SWAP(*((u32 *) key + 2));
212 des->K2LR = DEU_ENDIAN_SWAP(*((u32 *) key + 3));
213 /* no break; */
214 case 8:
215 des->K1HR = DEU_ENDIAN_SWAP(*((u32 *) key + 0));
216 des->K1LR = DEU_ENDIAN_SWAP(*((u32 *) key + 1));
217 break;
218 default:
219 CRTCL_SECT_END;
220 return;
221 }
222 }
223
224 /* write init vector (not required for ECB mode) */
225 if (mode > 0) {
226 des->IVHR = DEU_ENDIAN_SWAP(*(u32 *) iv_arg);
227 des->IVLR = DEU_ENDIAN_SWAP(*((u32 *) iv_arg + 1));
228 }
229
230 #ifndef CONFIG_CRYPTO_DEV_IFXMIPS_DMA
231 nblocks = nbytes / 4;
232
233 for (i = 0; i < nblocks; i += 2) {
234 /* wait for busy bit to clear */
235
236 /*--- Workaround ----------------------------------------------------
237 do a dummy read to the busy flag because it is not raised early
238 enough in CFB/OFB 3DES modes */
239 #ifdef CRYPTO_DEBUG
240 printk ("ihr: %x\n", (*((u32 *) in_arg + i)));
241 printk ("ilr: %x\n", (*((u32 *) in_arg + 1 + i)));
242 #endif
243 des->IHR = INPUT_ENDIAN_SWAP(*((u32 *) in_arg + i));
244 des->ILR = INPUT_ENDIAN_SWAP(*((u32 *) in_arg + 1 + i)); /* start crypto */
245
246 while (des->controlr.BUS) {
247 /* this will not take long */
248 }
249
250 *((u32 *) out_arg + 0 + i) = des->OHR;
251 *((u32 *) out_arg + 1 + i) = des->OLR;
252
253 #ifdef CRYPTO_DEBUG
254 printk ("ohr: %x\n", (*((u32 *) out_arg + i)));
255 printk ("olr: %x\n", (*((u32 *) out_arg + 1 + i)));
256 #endif
257 }
258
259 #else /* dma mode */
260
261 /* Prepare Rx buf length used in dma psuedo interrupt */
262 //deu_priv->deu_rx_buf = out_arg;
263 //deu_priv->deu_rx_len = nbytes;
264
265 /* memory alignment issue */
266 dword_mem_aligned_in = (u32 *) DEU_DWORD_REORDERING(in_arg, des_buff_in, BUFFER_IN, nbytes);
267
268 dma->controlr.ALGO = 0; //DES
269 des->controlr.DAU = 0;
270 dma->controlr.BS = 0;
271 dma->controlr.EN = 1;
272
273 while (des->controlr.BUS) {
274 // wait for AES to be ready
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 WAIT_DES_DMA_READY();
286
287 #ifdef CONFIG_CRYPTO_DEV_IFXMIPS_POLL_DMA
288
289 outcopy = (u32 *) DEU_DWORD_REORDERING(out_arg, des_buff_out, BUFFER_OUT, nbytes);
290
291 // polling DMA rx channel
292 while ((dma_device_read (dma_device, (u8 **) &out_dma, NULL)) == 0) {
293 timeout++;
294
295 if (timeout >= 333000) {
296 dma->controlr.EN = 0;
297 CRTCL_SECT_END;
298 printk (KERN_ERR "[%s %s %d]: timeout!!\n", __FILE__, __func__, __LINE__);
299 return; // -EINVAL;
300 }
301 }
302
303 WAIT_DES_DMA_READY();
304
305 DES_MEMORY_COPY(outcopy, out_dma, out_arg, nbytes);
306
307 #else
308
309 CRTCL_SECT_END; /* Sleep and wait for Rx finished */
310 DEU_WAIT_EVENT(deu_priv->deu_thread_wait, DEU_EVENT, deu_priv->deu_event_flags);
311 CRTCL_SECT_START;
312
313 #endif
314
315 #endif /* dma mode */
316
317 if (mode > 0) {
318 *(u32 *) iv_arg = DEU_ENDIAN_SWAP(des->IVHR);
319 *((u32 *) iv_arg + 1) = DEU_ENDIAN_SWAP(des->IVLR);
320 };
321
322 CRTCL_SECT_END;
323 }
324
325 //definitions from linux/include/crypto.h:
326 //#define CRYPTO_TFM_MODE_ECB 0x00000001
327 //#define CRYPTO_TFM_MODE_CBC 0x00000002
328 //#define CRYPTO_TFM_MODE_CFB 0x00000004
329 //#define CRYPTO_TFM_MODE_CTR 0x00000008
330 //#define CRYPTO_TFM_MODE_OFB 0x00000010 // not even defined
331 //but hardware definition: 0 ECB 1 CBC 2 OFB 3 CFB 4 CTR
332
333 /*! \fn void ifx_deu_des(void *ctx_arg, u8 *out_arg, const u8 *in_arg, u8 *iv_arg, u32 nbytes, int encdec, int mode)
334 * \ingroup IFX_DES_FUNCTIONS
335 * \brief main interface to DES hardware
336 * \param ctx_arg crypto algo context
337 * \param out_arg output bytestream
338 * \param in_arg input bytestream
339 * \param iv_arg initialization vector
340 * \param nbytes length of bytestream
341 * \param encdec 1 for encrypt; 0 for decrypt
342 * \param mode operation mode such as ebc, cbc
343 */
344
345 #ifdef CONFIG_CRYPTO_DEV_IFXMIPS_DMA
346 void ifx_deu_des (void *ctx_arg, u8 *out_arg, const u8 *in_arg,
347 u8 *iv_arg, u32 nbytes, int encdec, int mode)
348 {
349 u32 remain = nbytes;
350 u32 inc;
351
352 DPRINTF(0, "\n");
353
354 while (remain > 0)
355 {
356 if (remain >= DEU_MAX_PACKET_SIZE)
357 {
358 inc = DEU_MAX_PACKET_SIZE;
359 }
360 else
361 {
362 inc = remain;
363 }
364
365 remain -= inc;
366
367 ifx_deu_des_core(ctx_arg, out_arg, in_arg, iv_arg, inc, encdec, mode);
368
369 out_arg += inc;
370 in_arg += inc;
371 }
372 }
373 #endif
374
375
376 /*! \fn void ifx_deu_des_ecb (void *ctx, uint8_t *dst, const uint8_t *src, uint8_t *iv, size_t nbytes, int encdec, int inplace)
377 * \ingroup IFX_DES_FUNCTIONS
378 * \brief sets DES hardware to ECB mode
379 * \param ctx crypto algo context
380 * \param dst output bytestream
381 * \param src input bytestream
382 * \param iv initialization vector
383 * \param nbytes length of bytestream
384 * \param encdec 1 for encrypt; 0 for decrypt
385 * \param inplace not used
386 */
387
388 void ifx_deu_des_ecb (void *ctx, uint8_t *dst, const uint8_t *src,
389 uint8_t *iv, size_t nbytes, int encdec, int inplace)
390 {
391 DPRINTF(0, "ctx @%p\n", ctx);
392 ifx_deu_des (ctx, dst, src, NULL, nbytes, encdec, 0);
393 }
394
395 /*! \fn void ifx_deu_des_cbc (void *ctx, uint8_t *dst, const uint8_t *src, uint8_t *iv, size_t nbytes, int encdec, int inplace)
396 * \ingroup IFX_DES_FUNCTIONS
397 * \brief sets DES hardware to CBC mode
398 * \param ctx crypto algo context
399 * \param dst output bytestream
400 * \param src input bytestream
401 * \param iv initialization vector
402 * \param nbytes length of bytestream
403 * \param encdec 1 for encrypt; 0 for decrypt
404 * \param inplace not used
405 */
406 void ifx_deu_des_cbc (void *ctx, uint8_t *dst, const uint8_t *src,
407 uint8_t *iv, size_t nbytes, int encdec, int inplace)
408 {
409 DPRINTF(0, "ctx @%p\n", ctx);
410 ifx_deu_des (ctx, dst, src, iv, nbytes, encdec, 1);
411 }
412
413 /*! \fn void ifx_deu_des_ofb (void *ctx, uint8_t *dst, const uint8_t *src, uint8_t *iv, size_t nbytes, int encdec, int inplace)
414 * \ingroup IFX_DES_FUNCTIONS
415 * \brief sets DES hardware to OFB mode
416 * \param ctx crypto algo context
417 * \param dst output bytestream
418 * \param src input bytestream
419 * \param iv initialization vector
420 * \param nbytes length of bytestream
421 * \param encdec 1 for encrypt; 0 for decrypt
422 * \param inplace not used
423 */
424 void ifx_deu_des_ofb (void *ctx, uint8_t *dst, const uint8_t *src,
425 uint8_t *iv, size_t nbytes, int encdec, int inplace)
426 {
427 DPRINTF(0, "ctx @%p\n", ctx);
428 ifx_deu_des (ctx, dst, src, iv, nbytes, encdec, 2);
429 }
430
431 /*! \fn void ifx_deu_des_cfb (void *ctx, uint8_t *dst, const uint8_t *src, uint8_t *iv, size_t nbytes, int encdec, int inplace)
432 \ingroup IFX_DES_FUNCTIONS
433 \brief sets DES hardware to CFB mode
434 \param ctx crypto algo context
435 \param dst output bytestream
436 \param src input bytestream
437 \param iv initialization vector
438 \param nbytes length of bytestream
439 \param encdec 1 for encrypt; 0 for decrypt
440 \param inplace not used
441 */
442 void ifx_deu_des_cfb (void *ctx, uint8_t *dst, const uint8_t *src,
443 uint8_t *iv, size_t nbytes, int encdec, int inplace)
444 {
445 DPRINTF(0, "ctx @%p\n", ctx);
446 ifx_deu_des (ctx, dst, src, iv, nbytes, encdec, 3);
447 }
448
449 /*! \fn void ifx_deu_des_ctr (void *ctx, uint8_t *dst, const uint8_t *src, uint8_t *iv, size_t nbytes, int encdec, int inplace)
450 * \ingroup IFX_DES_FUNCTIONS
451 * \brief sets DES hardware to CTR mode
452 * \param ctx crypto algo context
453 * \param dst output bytestream
454 * \param src input bytestream
455 * \param iv initialization vector
456 * \param nbytes length of bytestream
457 * \param encdec 1 for encrypt; 0 for decrypt
458 * \param inplace not used
459 */
460 void ifx_deu_des_ctr (void *ctx, uint8_t *dst, const uint8_t *src,
461 uint8_t *iv, size_t nbytes, int encdec, int inplace)
462 {
463 DPRINTF(0, "ctx @%p\n", ctx);
464 ifx_deu_des (ctx, dst, src, iv, nbytes, encdec, 4);
465 }
466
467 /*! \fn void des_encrypt (struct crypto_tfm *tfm, uint8_t *out, const uint8_t *in)
468 * \ingroup IFX_DES_FUNCTIONS
469 * \brief encrypt DES_BLOCK_SIZE of data
470 * \param tfm linux crypto algo transform
471 * \param out output bytestream
472 * \param in input bytestream
473 */
474 void des_encrypt (struct crypto_tfm *tfm, uint8_t * out, const uint8_t * in)
475 {
476 struct des_ctx *ctx = crypto_tfm_ctx(tfm);
477 DPRINTF(0, "ctx @%p\n", ctx);
478 ifx_deu_des (ctx, out, in, NULL, DES_BLOCK_SIZE, CRYPTO_DIR_ENCRYPT, 0);
479 }
480
481 /*! \fn void des_decrypt (struct crypto_tfm *tfm, uint8_t *out, const uint8_t *in)
482 * \ingroup IFX_DES_FUNCTIONS
483 * \brief encrypt DES_BLOCK_SIZE of data
484 * \param tfm linux crypto algo transform
485 * \param out output bytestream
486 * \param in input bytestream
487 */
488 void des_decrypt (struct crypto_tfm *tfm, uint8_t * out, const uint8_t * in)
489 {
490 struct des_ctx *ctx = crypto_tfm_ctx(tfm);
491 DPRINTF(0, "ctx @%p\n", ctx);
492 ifx_deu_des (ctx, out, in, NULL, DES_BLOCK_SIZE, CRYPTO_DIR_DECRYPT, 0);
493 }
494
495 /*
496 * \brief RFC2451:
497 *
498 * For DES-EDE3, there is no known need to reject weak or
499 * complementation keys. Any weakness is obviated by the use of
500 * multiple keys.
501 *
502 * However, if the first two or last two independent 64-bit keys are
503 * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
504 * same as DES. Implementers MUST reject keys that exhibit this
505 * property.
506 *
507 */
508
509 /*! \fn int des3_ede_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
510 * \ingroup IFX_DES_FUNCTIONS
511 * \brief sets 3DES key
512 * \param tfm linux crypto algo transform
513 * \param key input key
514 * \param keylen key length
515 */
516 int des3_ede_setkey(struct crypto_tfm *tfm, const u8 *key,
517 unsigned int key_len)
518 {
519 struct des_ctx *ctx = crypto_tfm_ctx(tfm);
520
521 DPRINTF(0, "ctx @%p, key_len %d\n", ctx, key_len);
522
523 ctx->controlr_M = key_len / 8 + 1; // 3DES EDE1 / EDE2 / EDE3 Mode
524 ctx->key_length = key_len;
525
526 memcpy ((u8 *) (ctx->expkey), key, key_len);
527
528 return 0;
529 }
530
531 /*
532 * \brief DES function mappings
533 */
534 struct crypto_alg ifxdeu_des_alg = {
535 .cra_name = "des",
536 .cra_driver_name = "ifxdeu-des",
537 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
538 .cra_blocksize = DES_BLOCK_SIZE,
539 .cra_ctxsize = sizeof(struct des_ctx),
540 .cra_module = THIS_MODULE,
541 .cra_alignmask = 3,
542 .cra_list = LIST_HEAD_INIT(ifxdeu_des_alg.cra_list),
543 .cra_u = { .cipher = {
544 .cia_min_keysize = DES_KEY_SIZE,
545 .cia_max_keysize = DES_KEY_SIZE,
546 .cia_setkey = des_setkey,
547 .cia_encrypt = des_encrypt,
548 .cia_decrypt = des_decrypt } }
549 };
550
551 /*
552 * \brief DES function mappings
553 */
554 struct crypto_alg ifxdeu_des3_ede_alg = {
555 .cra_name = "des3_ede",
556 .cra_driver_name = "ifxdeu-des3_ede",
557 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
558 .cra_blocksize = DES_BLOCK_SIZE,
559 .cra_ctxsize = sizeof(struct des_ctx),
560 .cra_module = THIS_MODULE,
561 .cra_alignmask = 3,
562 .cra_list = LIST_HEAD_INIT(ifxdeu_des3_ede_alg.cra_list),
563 .cra_u = { .cipher = {
564 .cia_min_keysize = DES_KEY_SIZE,
565 .cia_max_keysize = DES_KEY_SIZE,
566 .cia_setkey = des3_ede_setkey,
567 .cia_encrypt = des_encrypt,
568 .cia_decrypt = des_decrypt } }
569 };
570
571 /*! \fn int ecb_des_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst, struct scatterlist *src, unsigned int nbytes)
572 * \ingroup IFX_DES_FUNCTIONS
573 * \brief ECB DES encrypt using linux crypto blkcipher
574 * \param desc blkcipher descriptor
575 * \param dst output scatterlist
576 * \param src input scatterlist
577 * \param nbytes data size in bytes
578 */
579 int ecb_des_encrypt(struct blkcipher_desc *desc,
580 struct scatterlist *dst, struct scatterlist *src,
581 unsigned int nbytes)
582 {
583 struct des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
584 struct blkcipher_walk walk;
585 int err;
586
587 DPRINTF(0, "ctx @%p\n", ctx);
588
589 blkcipher_walk_init(&walk, dst, src, nbytes);
590 err = blkcipher_walk_virt(desc, &walk);
591
592 while ((nbytes = walk.nbytes)) {
593 nbytes -= (nbytes % DES_BLOCK_SIZE);
594 ifx_deu_des_ecb(ctx, walk.dst.virt.addr, walk.src.virt.addr,
595 NULL, nbytes, CRYPTO_DIR_ENCRYPT, 0);
596 nbytes &= DES_BLOCK_SIZE - 1;
597 err = blkcipher_walk_done(desc, &walk, nbytes);
598 }
599
600 return err;
601 }
602
603 /*! \fn int ecb_des_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst, struct scatterlist *src, unsigned int nbytes)
604 * \ingroup IFX_DES_FUNCTIONS
605 * \brief ECB DES decrypt using linux crypto blkcipher
606 * \param desc blkcipher descriptor
607 * \param dst output scatterlist
608 * \param src input scatterlist
609 * \param nbytes data size in bytes
610 * \return err
611 */
612 int ecb_des_decrypt(struct blkcipher_desc *desc,
613 struct scatterlist *dst, struct scatterlist *src,
614 unsigned int nbytes)
615 {
616 struct des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
617 struct blkcipher_walk walk;
618 int err;
619
620 DPRINTF(0, "ctx @%p\n", ctx);
621
622 blkcipher_walk_init(&walk, dst, src, nbytes);
623 err = blkcipher_walk_virt(desc, &walk);
624
625 while ((nbytes = walk.nbytes)) {
626 nbytes -= (nbytes % DES_BLOCK_SIZE);
627 ifx_deu_des_ecb(ctx, walk.dst.virt.addr, walk.src.virt.addr,
628 NULL, nbytes, CRYPTO_DIR_DECRYPT, 0);
629 nbytes &= DES_BLOCK_SIZE - 1;
630 err = blkcipher_walk_done(desc, &walk, nbytes);
631 }
632
633 return err;
634 }
635
636 /*
637 * \brief DES function mappings
638 */
639 struct crypto_alg ifxdeu_ecb_des_alg = {
640 .cra_name = "ecb(des)",
641 .cra_driver_name = "ifxdeu-ecb(des)",
642 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
643 .cra_blocksize = DES_BLOCK_SIZE,
644 .cra_ctxsize = sizeof(struct des_ctx),
645 .cra_type = &crypto_blkcipher_type,
646 .cra_module = THIS_MODULE,
647 .cra_list = LIST_HEAD_INIT(ifxdeu_ecb_des_alg.cra_list),
648 .cra_u = {
649 .blkcipher = {
650 .min_keysize = DES_KEY_SIZE,
651 .max_keysize = DES_KEY_SIZE,
652 .setkey = des_setkey,
653 .encrypt = ecb_des_encrypt,
654 .decrypt = ecb_des_decrypt,
655 }
656 }
657 };
658
659 /*
660 * \brief DES function mappings
661 */
662 struct crypto_alg ifxdeu_ecb_des3_ede_alg = {
663 .cra_name = "ecb(des3_ede)",
664 .cra_driver_name = "ifxdeu-ecb(des3_ede)",
665 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
666 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
667 .cra_ctxsize = sizeof(struct des_ctx),
668 .cra_type = &crypto_blkcipher_type,
669 .cra_module = THIS_MODULE,
670 .cra_list = LIST_HEAD_INIT(ifxdeu_ecb_des3_ede_alg.cra_list),
671 .cra_u = {
672 .blkcipher = {
673 .min_keysize = DES3_EDE_KEY_SIZE,
674 .max_keysize = DES3_EDE_KEY_SIZE,
675 .setkey = des3_ede_setkey,
676 .encrypt = ecb_des_encrypt,
677 .decrypt = ecb_des_decrypt,
678 }
679 }
680 };
681
682 /*! \fn int cbc_des_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst, struct scatterlist *src, unsigned int nbytes)
683 * \ingroup IFX_DES_FUNCTIONS
684 * \brief CBC DES encrypt using linux crypto blkcipher
685 * \param desc blkcipher descriptor
686 * \param dst output scatterlist
687 * \param src input scatterlist
688 * \param nbytes data size in bytes
689 * \return err
690 */
691 int cbc_des_encrypt(struct blkcipher_desc *desc,
692 struct scatterlist *dst, struct scatterlist *src,
693 unsigned int nbytes)
694 {
695 struct des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
696 struct blkcipher_walk walk;
697 int err;
698
699 DPRINTF(0, "ctx @%p\n", ctx);
700
701 blkcipher_walk_init(&walk, dst, src, nbytes);
702 err = blkcipher_walk_virt(desc, &walk);
703
704 while ((nbytes = walk.nbytes)) {
705 u8 *iv = walk.iv;
706 //printk("iv = %08x\n", *(u32 *)iv);
707 nbytes -= (nbytes % DES_BLOCK_SIZE);
708 ifx_deu_des_cbc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
709 iv, nbytes, CRYPTO_DIR_ENCRYPT, 0);
710 nbytes &= DES_BLOCK_SIZE - 1;
711 err = blkcipher_walk_done(desc, &walk, nbytes);
712 }
713
714 return err;
715 }
716
717 /*! \fn int cbc_des_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst, struct scatterlist *src, unsigned int nbytes)
718 * \ingroup IFX_DES_FUNCTIONS
719 * \brief CBC DES decrypt using linux crypto blkcipher
720 * \param desc blkcipher descriptor
721 * \param dst output scatterlist
722 * \param src input scatterlist
723 * \param nbytes data size in bytes
724 * \return err
725 */
726 int cbc_des_decrypt(struct blkcipher_desc *desc,
727 struct scatterlist *dst, struct scatterlist *src,
728 unsigned int nbytes)
729 {
730 struct des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
731 struct blkcipher_walk walk;
732 int err;
733
734 DPRINTF(0, "ctx @%p\n", ctx);
735
736 blkcipher_walk_init(&walk, dst, src, nbytes);
737 err = blkcipher_walk_virt(desc, &walk);
738
739 while ((nbytes = walk.nbytes)) {
740 u8 *iv = walk.iv;
741 //printk("iv = %08x\n", *(u32 *)iv);
742 nbytes -= (nbytes % DES_BLOCK_SIZE);
743 ifx_deu_des_cbc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
744 iv, nbytes, CRYPTO_DIR_DECRYPT, 0);
745 nbytes &= DES_BLOCK_SIZE - 1;
746 err = blkcipher_walk_done(desc, &walk, nbytes);
747 }
748
749 return err;
750 }
751
752 /*
753 * \brief DES function mappings
754 */
755 struct crypto_alg ifxdeu_cbc_des_alg = {
756 .cra_name = "cbc(des)",
757 .cra_driver_name = "ifxdeu-cbc(des)",
758 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
759 .cra_blocksize = DES_BLOCK_SIZE,
760 .cra_ctxsize = sizeof(struct des_ctx),
761 .cra_type = &crypto_blkcipher_type,
762 .cra_module = THIS_MODULE,
763 .cra_list = LIST_HEAD_INIT(ifxdeu_cbc_des_alg.cra_list),
764 .cra_u = {
765 .blkcipher = {
766 .min_keysize = DES_KEY_SIZE,
767 .max_keysize = DES_KEY_SIZE,
768 .ivsize = DES_BLOCK_SIZE,
769 .setkey = des_setkey,
770 .encrypt = cbc_des_encrypt,
771 .decrypt = cbc_des_decrypt,
772 }
773 }
774 };
775
776 /*
777 * \brief DES function mappings
778 */
779 struct crypto_alg ifxdeu_cbc_des3_ede_alg = {
780 .cra_name = "cbc(des3_ede)",
781 .cra_driver_name = "ifxdeu-cbc(des3_ede)",
782 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
783 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
784 .cra_ctxsize = sizeof(struct des_ctx),
785 .cra_type = &crypto_blkcipher_type,
786 .cra_module = THIS_MODULE,
787 .cra_list = LIST_HEAD_INIT(ifxdeu_cbc_des3_ede_alg.cra_list),
788 .cra_u = {
789 .blkcipher = {
790 .min_keysize = DES3_EDE_KEY_SIZE,
791 .max_keysize = DES3_EDE_KEY_SIZE,
792 .ivsize = DES_BLOCK_SIZE,
793 .setkey = des3_ede_setkey,
794 .encrypt = cbc_des_encrypt,
795 .decrypt = cbc_des_decrypt,
796 }
797 }
798 };
799
800 /*! \fn int __init ifxdeu_init_des (void)
801 * \ingroup IFX_DES_FUNCTIONS
802 * \brief initialize des driver
803 */
804 int __init ifxdeu_init_des (void)
805 {
806 int ret = 0;
807
808 ret = crypto_register_alg(&ifxdeu_des_alg);
809 if (ret < 0)
810 goto des_err;
811
812 ret = crypto_register_alg(&ifxdeu_ecb_des_alg);
813 if (ret < 0)
814 goto ecb_des_err;
815
816 ret = crypto_register_alg(&ifxdeu_cbc_des_alg);
817 if (ret < 0)
818 goto cbc_des_err;
819
820 ret = crypto_register_alg(&ifxdeu_des3_ede_alg);
821 if (ret < 0)
822 goto des3_ede_err;
823
824 ret = crypto_register_alg(&ifxdeu_ecb_des3_ede_alg);
825 if (ret < 0)
826 goto ecb_des3_ede_err;
827
828 ret = crypto_register_alg(&ifxdeu_cbc_des3_ede_alg);
829 if (ret < 0)
830 goto cbc_des3_ede_err;
831
832 des_chip_init();
833
834 CRTCL_SECT_INIT;
835
836 #ifdef CONFIG_CRYPTO_DEV_IFXMIPS_DMA
837 if (ALLOCATE_MEMORY(BUFFER_IN, DES_ALGO) < 0) {
838 printk(KERN_ERR "[%s %s %d]: malloc memory fail!\n", __FILE__, __func__, __LINE__);
839 goto cbc_des3_ede_err;
840 }
841 if (ALLOCATE_MEMORY(BUFFER_OUT, DES_ALGO) < 0) {
842 printk(KERN_ERR "[%s %s %d]: malloc memory fail!\n", __FILE__, __func__, __LINE__);
843 goto cbc_des3_ede_err;
844 }
845 #endif
846
847 printk (KERN_NOTICE "IFX DEU DES initialized %s.\n", disable_deudma ? "" : " (DMA)");
848 return ret;
849
850 des_err:
851 crypto_unregister_alg(&ifxdeu_des_alg);
852 printk(KERN_ERR "IFX des initialization failed!\n");
853 return ret;
854 ecb_des_err:
855 crypto_unregister_alg(&ifxdeu_ecb_des_alg);
856 printk (KERN_ERR "IFX ecb_des initialization failed!\n");
857 return ret;
858 cbc_des_err:
859 crypto_unregister_alg(&ifxdeu_cbc_des_alg);
860 printk (KERN_ERR "IFX cbc_des initialization failed!\n");
861 return ret;
862 des3_ede_err:
863 crypto_unregister_alg(&ifxdeu_des3_ede_alg);
864 printk(KERN_ERR "IFX des3_ede initialization failed!\n");
865 return ret;
866 ecb_des3_ede_err:
867 crypto_unregister_alg(&ifxdeu_ecb_des3_ede_alg);
868 printk (KERN_ERR "IFX ecb_des3_ede initialization failed!\n");
869 return ret;
870 cbc_des3_ede_err:
871 crypto_unregister_alg(&ifxdeu_cbc_des3_ede_alg);
872 printk (KERN_ERR "IFX cbc_des3_ede initialization failed!\n");
873 return ret;
874 }
875
876 /*! \fn void __exit ifxdeu_fini_des (void)
877 * \ingroup IFX_DES_FUNCTIONS
878 * \brief unregister des driver
879 */
880 void __exit ifxdeu_fini_des (void)
881 {
882 crypto_unregister_alg (&ifxdeu_des_alg);
883 crypto_unregister_alg (&ifxdeu_ecb_des_alg);
884 crypto_unregister_alg (&ifxdeu_cbc_des_alg);
885 crypto_unregister_alg (&ifxdeu_des3_ede_alg);
886 crypto_unregister_alg (&ifxdeu_ecb_des3_ede_alg);
887 crypto_unregister_alg (&ifxdeu_cbc_des3_ede_alg);
888
889 #ifdef CONFIG_CRYPTO_DEV_IFXMIPS_DMA
890 FREE_MEMORY(des_buff_in);
891 FREE_MEMORY(des_buff_out);
892 #endif /* CONFIG_CRYPTO_DEV_IFXMIPS_DMA_DANUBE */
893 }