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.
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.
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.
16 * Copyright (C) 2010 Ralph Hempel <ralph.hempel@lantiq.com>
17 * Copyright (C) 2009 Mohammad Firdaus
21 \defgroup IFX_DEU IFX_DEU_DRIVERS
29 \brief DES encryption DEU driver file
33 \defgroup IFX_DES_FUNCTIONS IFX_DES_FUNCTIONS
35 \brief IFX DES Encryption functions
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"
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 */
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)
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
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
);
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
);
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 */
100 u8 iv
[DES_BLOCK_SIZE
];
101 u32 expkey
[DES3_EDE_EXPKEY_WORDS
];
104 extern int disable_deudma
;
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
113 int des_setkey(struct crypto_tfm
*tfm
, const u8
*key
,
114 unsigned int key_len
)
116 struct des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
118 DPRINTF(0, "ctx @%p, key_len %d %d\n", ctx
, key_len
);
120 ctx
->controlr_M
= 0; /* des */
121 ctx
->key_length
= key_len
;
123 memcpy ((u8
*) (ctx
->expkey
), key
, key_len
);
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
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
)
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
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
)
160 volatile struct des_t
*des
= (struct des_t
*) DES_3DES_START
;
161 struct des_ctx
*dctx
= ctx_arg
;
162 u32
*key
= dctx
->expkey
;
165 #ifndef CONFIG_CRYPTO_DEV_IFXMIPS_DMA
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;
174 u32
*dword_mem_aligned_in
= NULL
;
176 #ifdef CONFIG_CRYPTO_DEV_IFXMIPS_POLL_DMA
183 DPRINTF(0, "ctx @%p, mode %d, encdec %d\n", dctx
, mode
, encdec
);
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 */
193 des
->controlr
.M
= dctx
->controlr_M
;
195 if (dctx
->controlr_M
== 0) {
197 des
->K1HR
= DEU_ENDIAN_SWAP(*((u32
*) key
+ 0));
198 des
->K1LR
= DEU_ENDIAN_SWAP(*((u32
*) key
+ 1));
200 printk ("key1: %x\n", (*((u32
*) key
+ 0)));
201 printk ("key2: %x\n", (*((u32
*) key
+ 1)));
204 /* 3DES mode (EDE-x) */
205 switch (dctx
->key_length
) {
207 des
->K3HR
= DEU_ENDIAN_SWAP(*((u32
*) key
+ 4));
208 des
->K3LR
= DEU_ENDIAN_SWAP(*((u32
*) key
+ 5));
211 des
->K2HR
= DEU_ENDIAN_SWAP(*((u32
*) key
+ 2));
212 des
->K2LR
= DEU_ENDIAN_SWAP(*((u32
*) key
+ 3));
215 des
->K1HR
= DEU_ENDIAN_SWAP(*((u32
*) key
+ 0));
216 des
->K1LR
= DEU_ENDIAN_SWAP(*((u32
*) key
+ 1));
224 /* write init vector (not required for ECB mode) */
226 des
->IVHR
= DEU_ENDIAN_SWAP(*(u32
*) iv_arg
);
227 des
->IVLR
= DEU_ENDIAN_SWAP(*((u32
*) iv_arg
+ 1));
230 #ifndef CONFIG_CRYPTO_DEV_IFXMIPS_DMA
231 nblocks
= nbytes
/ 4;
233 for (i
= 0; i
< nblocks
; i
+= 2) {
234 /* wait for busy bit to clear */
236 /*--- Workaround ----------------------------------------------------
237 do a dummy read to the busy flag because it is not raised early
238 enough in CFB/OFB 3DES modes */
240 printk ("ihr: %x\n", (*((u32
*) in_arg
+ i
)));
241 printk ("ilr: %x\n", (*((u32
*) in_arg
+ 1 + i
)));
243 des
->IHR
= INPUT_ENDIAN_SWAP(*((u32
*) in_arg
+ i
));
244 des
->ILR
= INPUT_ENDIAN_SWAP(*((u32
*) in_arg
+ 1 + i
)); /* start crypto */
246 while (des
->controlr
.BUS
) {
247 /* this will not take long */
250 *((u32
*) out_arg
+ 0 + i
) = des
->OHR
;
251 *((u32
*) out_arg
+ 1 + i
) = des
->OLR
;
254 printk ("ohr: %x\n", (*((u32
*) out_arg
+ i
)));
255 printk ("olr: %x\n", (*((u32
*) out_arg
+ 1 + i
)));
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;
265 /* memory alignment issue */
266 dword_mem_aligned_in
= (u32
*) DEU_DWORD_REORDERING(in_arg
, des_buff_in
, BUFFER_IN
, nbytes
);
268 dma
->controlr
.ALGO
= 0; //DES
269 des
->controlr
.DAU
= 0;
270 dma
->controlr
.BS
= 0;
271 dma
->controlr
.EN
= 1;
273 while (des
->controlr
.BUS
) {
274 // wait for AES to be ready
277 wlen
= dma_device_write (dma_device
, (u8
*) dword_mem_aligned_in
, nbytes
, NULL
);
278 if (wlen
!= nbytes
) {
279 dma
->controlr
.EN
= 0;
281 printk (KERN_ERR
"[%s %s %d]: dma_device_write fail!\n", __FILE__
, __func__
, __LINE__
);
285 WAIT_DES_DMA_READY();
287 #ifdef CONFIG_CRYPTO_DEV_IFXMIPS_POLL_DMA
289 outcopy
= (u32
*) DEU_DWORD_REORDERING(out_arg
, des_buff_out
, BUFFER_OUT
, nbytes
);
291 // polling DMA rx channel
292 while ((dma_device_read (dma_device
, (u8
**) &out_dma
, NULL
)) == 0) {
295 if (timeout
>= 333000) {
296 dma
->controlr
.EN
= 0;
298 printk (KERN_ERR
"[%s %s %d]: timeout!!\n", __FILE__
, __func__
, __LINE__
);
303 WAIT_DES_DMA_READY();
305 DES_MEMORY_COPY(outcopy
, out_dma
, out_arg
, nbytes
);
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
);
315 #endif /* dma mode */
318 *(u32
*) iv_arg
= DEU_ENDIAN_SWAP(des
->IVHR
);
319 *((u32
*) iv_arg
+ 1) = DEU_ENDIAN_SWAP(des
->IVLR
);
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
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
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
)
356 if (remain
>= DEU_MAX_PACKET_SIZE
)
358 inc
= DEU_MAX_PACKET_SIZE
;
367 ifx_deu_des_core(ctx_arg
, out_arg
, in_arg
, iv_arg
, inc
, encdec
, mode
);
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
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
)
391 DPRINTF(0, "ctx @%p\n", ctx
);
392 ifx_deu_des (ctx
, dst
, src
, NULL
, nbytes
, encdec
, 0);
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
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
)
409 DPRINTF(0, "ctx @%p\n", ctx
);
410 ifx_deu_des (ctx
, dst
, src
, iv
, nbytes
, encdec
, 1);
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
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
)
427 DPRINTF(0, "ctx @%p\n", ctx
);
428 ifx_deu_des (ctx
, dst
, src
, iv
, nbytes
, encdec
, 2);
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
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
)
445 DPRINTF(0, "ctx @%p\n", ctx
);
446 ifx_deu_des (ctx
, dst
, src
, iv
, nbytes
, encdec
, 3);
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
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
)
463 DPRINTF(0, "ctx @%p\n", ctx
);
464 ifx_deu_des (ctx
, dst
, src
, iv
, nbytes
, encdec
, 4);
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
474 void des_encrypt (struct crypto_tfm
*tfm
, uint8_t * out
, const uint8_t * in
)
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);
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
488 void des_decrypt (struct crypto_tfm
*tfm
, uint8_t * out
, const uint8_t * in
)
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);
498 * For DES-EDE3, there is no known need to reject weak or
499 * complementation keys. Any weakness is obviated by the use of
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
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
516 int des3_ede_setkey(struct crypto_tfm
*tfm
, const u8
*key
,
517 unsigned int key_len
)
519 struct des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
521 DPRINTF(0, "ctx @%p, key_len %d\n", ctx
, key_len
);
523 ctx
->controlr_M
= key_len
/ 8 + 1; // 3DES EDE1 / EDE2 / EDE3 Mode
524 ctx
->key_length
= key_len
;
526 memcpy ((u8
*) (ctx
->expkey
), key
, key_len
);
532 * \brief DES function mappings
534 struct crypto_alg ifxdeu_des_alg
= {
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
,
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
} }
552 * \brief DES function mappings
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
,
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
} }
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
579 int ecb_des_encrypt(struct blkcipher_desc
*desc
,
580 struct scatterlist
*dst
, struct scatterlist
*src
,
583 struct des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
584 struct blkcipher_walk walk
;
587 DPRINTF(0, "ctx @%p\n", ctx
);
589 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
590 err
= blkcipher_walk_virt(desc
, &walk
);
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
);
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
612 int ecb_des_decrypt(struct blkcipher_desc
*desc
,
613 struct scatterlist
*dst
, struct scatterlist
*src
,
616 struct des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
617 struct blkcipher_walk walk
;
620 DPRINTF(0, "ctx @%p\n", ctx
);
622 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
623 err
= blkcipher_walk_virt(desc
, &walk
);
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
);
637 * \brief DES function mappings
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
),
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
,
660 * \brief DES function mappings
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
),
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
,
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
691 int cbc_des_encrypt(struct blkcipher_desc
*desc
,
692 struct scatterlist
*dst
, struct scatterlist
*src
,
695 struct des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
696 struct blkcipher_walk walk
;
699 DPRINTF(0, "ctx @%p\n", ctx
);
701 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
702 err
= blkcipher_walk_virt(desc
, &walk
);
704 while ((nbytes
= walk
.nbytes
)) {
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
);
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
726 int cbc_des_decrypt(struct blkcipher_desc
*desc
,
727 struct scatterlist
*dst
, struct scatterlist
*src
,
730 struct des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
731 struct blkcipher_walk walk
;
734 DPRINTF(0, "ctx @%p\n", ctx
);
736 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
737 err
= blkcipher_walk_virt(desc
, &walk
);
739 while ((nbytes
= walk
.nbytes
)) {
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
);
753 * \brief DES function mappings
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
),
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
,
777 * \brief DES function mappings
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
),
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
,
800 /*! \fn int __init ifxdeu_init_des (void)
801 * \ingroup IFX_DES_FUNCTIONS
802 * \brief initialize des driver
804 int __init
ifxdeu_init_des (void)
808 ret
= crypto_register_alg(&ifxdeu_des_alg
);
812 ret
= crypto_register_alg(&ifxdeu_ecb_des_alg
);
816 ret
= crypto_register_alg(&ifxdeu_cbc_des_alg
);
820 ret
= crypto_register_alg(&ifxdeu_des3_ede_alg
);
824 ret
= crypto_register_alg(&ifxdeu_ecb_des3_ede_alg
);
826 goto ecb_des3_ede_err
;
828 ret
= crypto_register_alg(&ifxdeu_cbc_des3_ede_alg
);
830 goto cbc_des3_ede_err
;
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
;
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
;
847 printk (KERN_NOTICE
"IFX DEU DES initialized %s.\n", disable_deudma
? "" : " (DMA)");
851 crypto_unregister_alg(&ifxdeu_des_alg
);
852 printk(KERN_ERR
"IFX des initialization failed!\n");
855 crypto_unregister_alg(&ifxdeu_ecb_des_alg
);
856 printk (KERN_ERR
"IFX ecb_des initialization failed!\n");
859 crypto_unregister_alg(&ifxdeu_cbc_des_alg
);
860 printk (KERN_ERR
"IFX cbc_des initialization failed!\n");
863 crypto_unregister_alg(&ifxdeu_des3_ede_alg
);
864 printk(KERN_ERR
"IFX des3_ede initialization failed!\n");
867 crypto_unregister_alg(&ifxdeu_ecb_des3_ede_alg
);
868 printk (KERN_ERR
"IFX ecb_des3_ede initialization failed!\n");
871 crypto_unregister_alg(&ifxdeu_cbc_des3_ede_alg
);
872 printk (KERN_ERR
"IFX cbc_des3_ede initialization failed!\n");
876 /*! \fn void __exit ifxdeu_fini_des (void)
877 * \ingroup IFX_DES_FUNCTIONS
878 * \brief unregister des driver
880 void __exit
ifxdeu_fini_des (void)
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
);
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 */