generic: backport pending driver for Winchip CH348 USB serial
[openwrt/openwrt.git] / package / kernel / lantiq / ltq-deu / src / ifxmips_des.c
1 /******************************************************************************
2 **
3 ** FILE NAME : ifxmips_des.c
4 ** PROJECT : IFX UEIP
5 ** MODULES : DEU Module
6 **
7 ** DATE : September 8, 2009
8 ** AUTHOR : Mohammad Firdaus
9 ** DESCRIPTION : Data Encryption Unit Driver for DES Algorithm
10 ** COPYRIGHT : Copyright (c) 2009
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 *******************************************************************************/
23
24 /*!
25 \defgroup IFX_DEU IFX_DEU_DRIVERS
26 \ingroup API
27 \brief ifx deu driver
28 */
29
30 /*!
31 \file ifxmips_des.c
32 \ingroup IFX_DEU
33 \brief DES encryption DEU driver file
34 */
35
36 /*!
37 \defgroup IFX_DES_FUNCTIONS IFX_DES_FUNCTIONS
38 \ingroup IFX_DEU
39 \brief IFX DES Encryption functions
40 */
41
42 /* Project Header Files */
43 #include <linux/version.h>
44 #include <linux/module.h>
45 #include <linux/init.h>
46 #include <linux/types.h>
47 #include <linux/errno.h>
48 #include <linux/crypto.h>
49 #include <linux/interrupt.h>
50 #include <linux/delay.h>
51 #include <asm/byteorder.h>
52 #include <crypto/algapi.h>
53 #include <crypto/des.h>
54 #include <crypto/internal/skcipher.h>
55 #include "ifxmips_deu.h"
56
57 #if defined(CONFIG_DANUBE)
58 #include "ifxmips_deu_danube.h"
59 extern int ifx_danube_pre_1_4;
60 #elif defined(CONFIG_AR9)
61 #include "ifxmips_deu_ar9.h"
62 #elif defined(CONFIG_VR9) || defined(CONFIG_AR10)
63 #include "ifxmips_deu_vr9.h"
64 #else
65 #error "Unkown platform"
66 #endif
67
68 /* DMA specific header and variables */
69
70 #if 0
71 #define CRTCL_SECT_INIT
72 #define CRTCL_SECT_START
73 #define CRTCL_SECT_END
74 #else
75 spinlock_t des_lock;
76 #define CRTCL_SECT_INIT spin_lock_init(&des_lock)
77 #define CRTCL_SECT_START spin_lock_irqsave(&des_lock, flag)
78 #define CRTCL_SECT_END spin_unlock_irqrestore(&des_lock, flag)
79 #endif
80
81 /* Preprocessor declerations */
82 #ifdef CRYPTO_DEBUG
83 extern char debug_level;
84 #define DPRINTF(level, format, args...) if (level < debug_level) printk(KERN_INFO "[%s %s %d]: " format, __FILE__, __func__, __LINE__, ##args);
85 #else
86 #define DPRINTF(level, format, args...)
87 #endif
88 #define DES_3DES_START IFX_DES_CON
89 #define DES_KEY_SIZE 8
90 #define DES_EXPKEY_WORDS 32
91 #define DES_BLOCK_SIZE 8
92 #define DES3_EDE_KEY_SIZE (3 * DES_KEY_SIZE)
93 #define DES3_EDE_EXPKEY_WORDS (3 * DES_EXPKEY_WORDS)
94 #define DES3_EDE_BLOCK_SIZE DES_BLOCK_SIZE
95
96 /* Function Declaration to prevent warning messages */
97 void des_chip_init (void);
98 u32 endian_swap(u32 input);
99 u32 input_swap(u32 input);
100 int aes_memory_allocate(int value);
101 int des_memory_allocate(int value);
102 void memory_release(u32 *buffer);
103 u32* memory_alignment(const u8 *arg, u32 *buff_alloc, int in_out, int nbytes);
104 void aes_dma_memory_copy(u32 *outcopy, u32 *out_dma, u8 *out_arg, int nbytes);
105 void des_dma_memory_copy(u32 *outcopy, u32 *out_dma, u8 *out_arg, int nbytes);
106
107 void ifx_deu_des (void *ctx_arg, u8 *out_arg, const u8 *in_arg,
108 u8 *iv_arg, u32 nbytes, int encdec, int mode);
109
110 struct ifx_deu_des_ctx {
111 int controlr_M;
112 int key_length;
113 u8 iv[DES_BLOCK_SIZE];
114 u32 expkey[DES3_EDE_EXPKEY_WORDS];
115 struct des_ctx des_context;
116 struct des3_ede_ctx des3_ede_context;
117 };
118
119 extern int disable_multiblock;
120 extern int disable_deudma;
121
122 /*! \fn int des_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
123 * \ingroup IFX_DES_FUNCTIONS
124 * \brief sets DES key
125 * \param tfm linux crypto algo transform
126 * \param key input key
127 * \param keylen key length
128 */
129 int des_setkey(struct crypto_tfm *tfm, const u8 *key,
130 unsigned int keylen)
131 {
132 struct ifx_deu_des_ctx *dctx = crypto_tfm_ctx(tfm);
133 int err;
134
135 //printk("setkey in %s\n", __FILE__);
136
137 err = des_expand_key(&dctx->des_context, key, keylen);
138 if (err == -ENOKEY) {
139 if (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)
140 err = -EINVAL;
141 else
142 err = 0;
143 }
144
145 dctx->controlr_M = 0; // des
146 dctx->key_length = keylen;
147
148 memcpy ((u8 *) (dctx->expkey), key, keylen);
149
150 if (err)
151 memset(dctx, 0, sizeof(*dctx));
152
153 return err;
154 }
155
156 /*! \fn int des_setkey_skcipher (struct crypto_skcipher *tfm, const uint8_t *in_key, unsigned int key_len)
157 * \ingroup IFX_AES_FUNCTIONS
158 * \brief sets the AES keys for skcipher
159 * \param tfm linux crypto skcipher
160 * \param in_key input key
161 * \param key_len key lengths of 16, 24 and 32 bytes supported
162 * \return -EINVAL - bad key length, 0 - SUCCESS
163 */
164 int des_setkey_skcipher (struct crypto_skcipher *tfm, const u8 *in_key, unsigned int key_len)
165 {
166 return des_setkey(crypto_skcipher_tfm(tfm), in_key, key_len);
167 }
168
169 /*! \fn void ifx_deu_des(void *ctx_arg, u8 *out_arg, const u8 *in_arg, u8 *iv_arg, u32 nbytes, int encdec, int mode)
170 * \ingroup IFX_DES_FUNCTIONS
171 * \brief main interface to DES hardware
172 * \param ctx_arg crypto algo context
173 * \param out_arg output bytestream
174 * \param in_arg input bytestream
175 * \param iv_arg initialization vector
176 * \param nbytes length of bytestream
177 * \param encdec 1 for encrypt; 0 for decrypt
178 * \param mode operation mode such as ebc, cbc
179 */
180
181 void ifx_deu_des (void *ctx_arg, u8 *out_arg, const u8 *in_arg,
182 u8 *iv_arg, u32 nbytes, int encdec, int mode)
183 {
184 volatile struct des_t *des = (struct des_t *) DES_3DES_START;
185 struct ifx_deu_des_ctx *dctx = ctx_arg;
186 u32 *key = dctx->expkey;
187 unsigned long flag;
188
189 int i = 0;
190 int nblocks = 0;
191
192 CRTCL_SECT_START;
193
194 des->controlr.M = dctx->controlr_M;
195 if (dctx->controlr_M == 0) // des
196 {
197 des->K1HR = DEU_ENDIAN_SWAP(*((u32 *) key + 0));
198 des->K1LR = DEU_ENDIAN_SWAP(*((u32 *) key + 1));
199
200 }
201 else {
202 /* Hardware Section */
203 switch (dctx->key_length) {
204 case 24:
205 des->K3HR = DEU_ENDIAN_SWAP(*((u32 *) key + 4));
206 des->K3LR = DEU_ENDIAN_SWAP(*((u32 *) key + 5));
207 /* no break; */
208 fallthrough;
209 case 16:
210 des->K2HR = DEU_ENDIAN_SWAP(*((u32 *) key + 2));
211 des->K2LR = DEU_ENDIAN_SWAP(*((u32 *) key + 3));
212
213 /* no break; */
214 fallthrough;
215 case 8:
216 des->K1HR = DEU_ENDIAN_SWAP(*((u32 *) key + 0));
217 des->K1LR = DEU_ENDIAN_SWAP(*((u32 *) key + 1));
218 break;
219
220 default:
221 CRTCL_SECT_END;
222 return;
223 }
224 }
225
226 des->controlr.E_D = !encdec; //encryption
227 des->controlr.O = mode; //0 ECB 1 CBC 2 OFB 3 CFB 4 CTR hexdump(prin,sizeof(*des));
228
229 if (mode > 0) {
230 des->IVHR = DEU_ENDIAN_SWAP(*(u32 *) iv_arg);
231 des->IVLR = DEU_ENDIAN_SWAP(*((u32 *) iv_arg + 1));
232 };
233
234 nblocks = nbytes / 4;
235
236 for (i = 0; i < nblocks; i += 2) {
237 /* wait for busy bit to clear */
238
239 /*--- Workaround ----------------------------------------------------
240 do a dummy read to the busy flag because it is not raised early
241 enough in CFB/OFB 3DES modes */
242 #ifdef CRYPTO_DEBUG
243 printk ("ihr: %x\n", (*((u32 *) in_arg + i)));
244 printk ("ilr: %x\n", (*((u32 *) in_arg + 1 + i)));
245 #endif
246 des->IHR = INPUT_ENDIAN_SWAP(*((u32 *) in_arg + i));
247 des->ILR = INPUT_ENDIAN_SWAP(*((u32 *) in_arg + 1 + i)); /* start crypto */
248
249 while (des->controlr.BUS) {
250 // this will not take long
251 }
252
253 *((u32 *) out_arg + 0 + i) = des->OHR;
254 *((u32 *) out_arg + 1 + i) = des->OLR;
255
256 }
257
258
259
260 if (mode > 0) {
261 *(u32 *) iv_arg = DEU_ENDIAN_SWAP(des->IVHR);
262 *((u32 *) iv_arg + 1) = DEU_ENDIAN_SWAP(des->IVLR);
263 };
264
265 CRTCL_SECT_END;
266 }
267
268 //definitions from linux/include/crypto.h:
269 //#define CRYPTO_TFM_MODE_ECB 0x00000001
270 //#define CRYPTO_TFM_MODE_CBC 0x00000002
271 //#define CRYPTO_TFM_MODE_CFB 0x00000004
272 //#define CRYPTO_TFM_MODE_CTR 0x00000008
273 //#define CRYPTO_TFM_MODE_OFB 0x00000010 // not even defined
274 //but hardware definition: 0 ECB 1 CBC 2 OFB 3 CFB 4 CTR
275
276 /*! \fn void ifx_deu_des(void *ctx_arg, u8 *out_arg, const u8 *in_arg, u8 *iv_arg, u32 nbytes, int encdec, int mode)
277 * \ingroup IFX_DES_FUNCTIONS
278 * \brief main interface to DES hardware
279 * \param ctx_arg crypto algo context
280 * \param out_arg output bytestream
281 * \param in_arg input bytestream
282 * \param iv_arg initialization vector
283 * \param nbytes length of bytestream
284 * \param encdec 1 for encrypt; 0 for decrypt
285 * \param mode operation mode such as ebc, cbc
286 */
287
288 /*! \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)
289 * \ingroup IFX_DES_FUNCTIONS
290 * \brief sets DES hardware to ECB mode
291 * \param ctx crypto algo context
292 * \param dst output bytestream
293 * \param src input bytestream
294 * \param iv initialization vector
295 * \param nbytes length of bytestream
296 * \param encdec 1 for encrypt; 0 for decrypt
297 * \param inplace not used
298 */
299 void ifx_deu_des_ecb (void *ctx, uint8_t *dst, const uint8_t *src,
300 uint8_t *iv, size_t nbytes, int encdec, int inplace)
301 {
302 ifx_deu_des (ctx, dst, src, NULL, nbytes, encdec, 0);
303 }
304
305 /*! \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)
306 * \ingroup IFX_DES_FUNCTIONS
307 * \brief sets DES hardware to CBC mode
308 * \param ctx crypto algo context
309 * \param dst output bytestream
310 * \param src input bytestream
311 * \param iv initialization vector
312 * \param nbytes length of bytestream
313 * \param encdec 1 for encrypt; 0 for decrypt
314 * \param inplace not used
315 */
316 void ifx_deu_des_cbc (void *ctx, uint8_t *dst, const uint8_t *src,
317 uint8_t *iv, size_t nbytes, int encdec, int inplace)
318 {
319 ifx_deu_des (ctx, dst, src, iv, nbytes, encdec, 1);
320 }
321
322 /*! \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)
323 * \ingroup IFX_DES_FUNCTIONS
324 * \brief sets DES hardware to OFB mode
325 * \param ctx crypto algo context
326 * \param dst output bytestream
327 * \param src input bytestream
328 * \param iv initialization vector
329 * \param nbytes length of bytestream
330 * \param encdec 1 for encrypt; 0 for decrypt
331 * \param inplace not used
332 */
333 void ifx_deu_des_ofb (void *ctx, uint8_t *dst, const uint8_t *src,
334 uint8_t *iv, size_t nbytes, int encdec, int inplace)
335 {
336 ifx_deu_des (ctx, dst, src, iv, nbytes, encdec, 2);
337 }
338
339 /*! \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)
340 \ingroup IFX_DES_FUNCTIONS
341 \brief sets DES hardware to CFB mode
342 \param ctx crypto algo context
343 \param dst output bytestream
344 \param src input bytestream
345 \param iv initialization vector
346 \param nbytes length of bytestream
347 \param encdec 1 for encrypt; 0 for decrypt
348 \param inplace not used
349 */
350 void ifx_deu_des_cfb (void *ctx, uint8_t *dst, const uint8_t *src,
351 uint8_t *iv, size_t nbytes, int encdec, int inplace)
352 {
353 ifx_deu_des (ctx, dst, src, iv, nbytes, encdec, 3);
354 }
355
356 /*! \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)
357 * \ingroup IFX_DES_FUNCTIONS
358 * \brief sets DES hardware to CTR mode
359 * \param ctx crypto algo context
360 * \param dst output bytestream
361 * \param src input bytestream
362 * \param iv initialization vector
363 * \param nbytes length of bytestream
364 * \param encdec 1 for encrypt; 0 for decrypt
365 * \param inplace not used
366 */
367 void ifx_deu_des_ctr (void *ctx, uint8_t *dst, const uint8_t *src,
368 uint8_t *iv, size_t nbytes, int encdec, int inplace)
369 {
370 ifx_deu_des (ctx, dst, src, iv, nbytes, encdec, 4);
371 }
372
373 /*! \fn void ifx_deu_des_encrypt (struct crypto_tfm *tfm, uint8_t *out, const uint8_t *in)
374 * \ingroup IFX_DES_FUNCTIONS
375 * \brief encrypt DES_BLOCK_SIZE of data
376 * \param tfm linux crypto algo transform
377 * \param out output bytestream
378 * \param in input bytestream
379 */
380 void ifx_deu_des_encrypt (struct crypto_tfm *tfm, uint8_t * out, const uint8_t * in)
381 {
382 struct ifx_deu_des_ctx *ctx = crypto_tfm_ctx(tfm);
383 ifx_deu_des (ctx, out, in, NULL, DES_BLOCK_SIZE,
384 CRYPTO_DIR_ENCRYPT, 0);
385
386 }
387
388 /*! \fn void ifx_deu_des_decrypt (struct crypto_tfm *tfm, uint8_t *out, const uint8_t *in)
389 * \ingroup IFX_DES_FUNCTIONS
390 * \brief encrypt DES_BLOCK_SIZE of data
391 * \param tfm linux crypto algo transform
392 * \param out output bytestream
393 * \param in input bytestream
394 */
395 void ifx_deu_des_decrypt (struct crypto_tfm *tfm, uint8_t * out, const uint8_t * in)
396 {
397 struct ifx_deu_des_ctx *ctx = crypto_tfm_ctx(tfm);
398 ifx_deu_des (ctx, out, in, NULL, DES_BLOCK_SIZE,
399 CRYPTO_DIR_DECRYPT, 0);
400 }
401
402 /*
403 * \brief RFC2451:
404 *
405 * For DES-EDE3, there is no known need to reject weak or
406 * complementation keys. Any weakness is obviated by the use of
407 * multiple keys.
408 *
409 * However, if the first two or last two independent 64-bit keys are
410 * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
411 * same as DES. Implementers MUST reject keys that exhibit this
412 * property.
413 *
414 */
415
416 /*! \fn int des3_ede_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
417 * \ingroup IFX_DES_FUNCTIONS
418 * \brief sets 3DES key
419 * \param tfm linux crypto algo transform
420 * \param key input key
421 * \param keylen key length
422 */
423 int des3_ede_setkey(struct crypto_tfm *tfm, const u8 *key,
424 unsigned int keylen)
425 {
426 struct ifx_deu_des_ctx *dctx = crypto_tfm_ctx(tfm);
427 int err;
428
429 //printk("setkey in %s\n", __FILE__);
430
431 err = des3_ede_expand_key(&dctx->des3_ede_context, key, keylen);
432 if (err == -ENOKEY) {
433 if (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)
434 err = -EINVAL;
435 else
436 err = 0;
437 }
438
439 dctx->controlr_M = keylen / 8 + 1; // 3DES EDE1 / EDE2 / EDE3 Mode
440 dctx->key_length = keylen;
441
442 memcpy ((u8 *) (dctx->expkey), key, keylen);
443
444 if (err)
445 memset(dctx, 0, sizeof(*dctx));
446
447 return err;
448 }
449
450 /*! \fn int des3_ede_setkey_skcipher(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen)
451 * \ingroup IFX_DES_FUNCTIONS
452 * \brief sets 3DES key
453 * \param tfm linux crypto skcipher transform
454 * \param key input key
455 * \param keylen key length
456 */
457 int des3_ede_setkey_skcipher(struct crypto_skcipher *tfm, const u8 *key,
458 unsigned int keylen)
459 {
460 return des3_ede_setkey(crypto_skcipher_tfm(tfm), key, keylen);
461 }
462
463 /*
464 * \brief DES function mappings
465 */
466 struct crypto_alg ifxdeu_des_alg = {
467 .cra_name = "des",
468 .cra_driver_name = "ifxdeu-des",
469 .cra_priority = 300,
470 .cra_flags = CRYPTO_ALG_TYPE_CIPHER | CRYPTO_ALG_KERN_DRIVER_ONLY,
471 .cra_blocksize = DES_BLOCK_SIZE,
472 .cra_ctxsize = sizeof(struct ifx_deu_des_ctx),
473 .cra_module = THIS_MODULE,
474 .cra_alignmask = 3,
475 .cra_list = LIST_HEAD_INIT(ifxdeu_des_alg.cra_list),
476 .cra_u = { .cipher = {
477 .cia_min_keysize = DES_KEY_SIZE,
478 .cia_max_keysize = DES_KEY_SIZE,
479 .cia_setkey = des_setkey,
480 .cia_encrypt = ifx_deu_des_encrypt,
481 .cia_decrypt = ifx_deu_des_decrypt } }
482 };
483
484 /*
485 * \brief DES function mappings
486 */
487 struct crypto_alg ifxdeu_des3_ede_alg = {
488 .cra_name = "des3_ede",
489 .cra_driver_name = "ifxdeu-des3_ede",
490 .cra_priority = 300,
491 .cra_flags = CRYPTO_ALG_TYPE_CIPHER | CRYPTO_ALG_KERN_DRIVER_ONLY,
492 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
493 .cra_ctxsize = sizeof(struct ifx_deu_des_ctx),
494 .cra_module = THIS_MODULE,
495 .cra_alignmask = 3,
496 .cra_list = LIST_HEAD_INIT(ifxdeu_des3_ede_alg.cra_list),
497 .cra_u = { .cipher = {
498 .cia_min_keysize = DES3_EDE_KEY_SIZE,
499 .cia_max_keysize = DES3_EDE_KEY_SIZE,
500 .cia_setkey = des3_ede_setkey,
501 .cia_encrypt = ifx_deu_des_encrypt,
502 .cia_decrypt = ifx_deu_des_decrypt } }
503 };
504
505 /*! \fn int ecb_des_encrypt(struct skcipher_req *req)
506 * \ingroup IFX_AES_FUNCTIONS
507 * \brief ECB DES encrypt using linux crypto skcipher
508 * \param req skcipher request
509 * \return err
510 */
511 int ecb_des_encrypt(struct skcipher_request *req)
512 {
513 struct ifx_deu_des_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
514 struct skcipher_walk walk;
515 int err;
516 unsigned int enc_bytes, nbytes;
517
518 err = skcipher_walk_virt(&walk, req, false);
519
520 while ((nbytes = enc_bytes = walk.nbytes)) {
521 enc_bytes -= (nbytes % DES_BLOCK_SIZE);
522 ifx_deu_des_ecb(ctx, walk.dst.virt.addr, walk.src.virt.addr,
523 NULL, enc_bytes, CRYPTO_DIR_ENCRYPT, 0);
524 nbytes &= DES_BLOCK_SIZE - 1;
525 err = skcipher_walk_done(&walk, nbytes);
526 }
527
528 return err;
529 }
530
531 /*! \fn int ecb_des_decrypt(struct skcipher_req *req)
532 * \ingroup IFX_AES_FUNCTIONS
533 * \brief ECB DES decrypt using linux crypto skcipher
534 * \param req skcipher request
535 * \return err
536 */
537 int ecb_des_decrypt(struct skcipher_request *req)
538 {
539 struct ifx_deu_des_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
540 struct skcipher_walk walk;
541 int err;
542 unsigned int dec_bytes, nbytes;
543
544 DPRINTF(1, "\n");
545 err = skcipher_walk_virt(&walk, req, false);
546
547 while ((nbytes = dec_bytes = walk.nbytes)) {
548 dec_bytes -= (nbytes % DES_BLOCK_SIZE);
549 ifx_deu_des_ecb(ctx, walk.dst.virt.addr, walk.src.virt.addr,
550 NULL, dec_bytes, CRYPTO_DIR_DECRYPT, 0);
551 nbytes &= DES_BLOCK_SIZE - 1;
552 err = skcipher_walk_done(&walk, nbytes);
553 }
554
555 return err;
556 }
557
558 /*
559 * \brief DES function mappings
560 */
561 struct skcipher_alg ifxdeu_ecb_des_alg = {
562 .base.cra_name = "ecb(des)",
563 .base.cra_driver_name = "ifxdeu-ecb(des)",
564 .base.cra_priority = 400,
565 .base.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_KERN_DRIVER_ONLY,
566 .base.cra_blocksize = DES_BLOCK_SIZE,
567 .base.cra_ctxsize = sizeof(struct ifx_deu_des_ctx),
568 .base.cra_module = THIS_MODULE,
569 .base.cra_list = LIST_HEAD_INIT(ifxdeu_ecb_des_alg.base.cra_list),
570 .min_keysize = DES_KEY_SIZE,
571 .max_keysize = DES_KEY_SIZE,
572 .setkey = des_setkey_skcipher,
573 .encrypt = ecb_des_encrypt,
574 .decrypt = ecb_des_decrypt,
575 };
576
577 /*
578 * \brief DES function mappings
579 */
580 struct skcipher_alg ifxdeu_ecb_des3_ede_alg = {
581 .base.cra_name = "ecb(des3_ede)",
582 .base.cra_driver_name = "ifxdeu-ecb(des3_ede)",
583 .base.cra_priority = 400,
584 .base.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_KERN_DRIVER_ONLY,
585 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
586 .base.cra_ctxsize = sizeof(struct ifx_deu_des_ctx),
587 .base.cra_module = THIS_MODULE,
588 .base.cra_list = LIST_HEAD_INIT(ifxdeu_ecb_des3_ede_alg.base.cra_list),
589 .min_keysize = DES3_EDE_KEY_SIZE,
590 .max_keysize = DES3_EDE_KEY_SIZE,
591 .setkey = des3_ede_setkey_skcipher,
592 .encrypt = ecb_des_encrypt,
593 .decrypt = ecb_des_decrypt,
594 };
595
596 /*! \fn int cbc_des_encrypt(struct skcipher_req *req)
597 * \ingroup IFX_AES_FUNCTIONS
598 * \brief CBC DES encrypt using linux crypto skcipher
599 * \param req skcipher request
600 * \return err
601 */
602 int cbc_des_encrypt(struct skcipher_request *req)
603 {
604 struct ifx_deu_des_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
605 struct skcipher_walk walk;
606 int err;
607 unsigned int enc_bytes, nbytes;
608
609 DPRINTF(1, "\n");
610 err = skcipher_walk_virt(&walk, req, false);
611
612 while ((nbytes = enc_bytes = walk.nbytes)) {
613 u8 *iv = walk.iv;
614 enc_bytes -= (nbytes % DES_BLOCK_SIZE);
615 ifx_deu_des_cbc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
616 iv, enc_bytes, CRYPTO_DIR_ENCRYPT, 0);
617 nbytes &= DES_BLOCK_SIZE - 1;
618 err = skcipher_walk_done(&walk, nbytes);
619 }
620
621 return err;
622 }
623
624 /*! \fn int cbc_des_encrypt(struct skcipher_req *req)
625 * \ingroup IFX_AES_FUNCTIONS
626 * \brief CBC DES decrypt using linux crypto skcipher
627 * \param req skcipher request
628 * \return err
629 */
630 int cbc_des_decrypt(struct skcipher_request *req)
631 {
632 struct ifx_deu_des_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
633 struct skcipher_walk walk;
634 int err;
635 unsigned int dec_bytes, nbytes;
636
637 DPRINTF(1, "\n");
638 err = skcipher_walk_virt(&walk, req, false);
639
640 while ((nbytes = dec_bytes = walk.nbytes)) {
641 u8 *iv = walk.iv;
642 dec_bytes -= (nbytes % DES_BLOCK_SIZE);
643 ifx_deu_des_cbc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
644 iv, dec_bytes, CRYPTO_DIR_DECRYPT, 0);
645 nbytes &= DES_BLOCK_SIZE - 1;
646 err = skcipher_walk_done(&walk, nbytes);
647 }
648
649 return err;
650 }
651
652 /*
653 * \brief DES function mappings
654 */
655 struct skcipher_alg ifxdeu_cbc_des_alg = {
656 .base.cra_name = "cbc(des)",
657 .base.cra_driver_name = "ifxdeu-cbc(des)",
658 .base.cra_priority = 400,
659 .base.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_KERN_DRIVER_ONLY,
660 .base.cra_blocksize = DES_BLOCK_SIZE,
661 .base.cra_ctxsize = sizeof(struct ifx_deu_des_ctx),
662 .base.cra_module = THIS_MODULE,
663 .base.cra_list = LIST_HEAD_INIT(ifxdeu_cbc_des_alg.base.cra_list),
664 .min_keysize = DES_KEY_SIZE,
665 .max_keysize = DES_KEY_SIZE,
666 .ivsize = DES_BLOCK_SIZE,
667 .setkey = des_setkey_skcipher,
668 .encrypt = cbc_des_encrypt,
669 .decrypt = cbc_des_decrypt,
670 };
671
672 /*
673 * \brief DES function mappings
674 */
675 struct skcipher_alg ifxdeu_cbc_des3_ede_alg = {
676 .base.cra_name = "cbc(des3_ede)",
677 .base.cra_driver_name = "ifxdeu-cbc(des3_ede)",
678 .base.cra_priority = 400,
679 .base.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_KERN_DRIVER_ONLY,
680 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
681 .base.cra_ctxsize = sizeof(struct ifx_deu_des_ctx),
682 .base.cra_module = THIS_MODULE,
683 .base.cra_list = LIST_HEAD_INIT(ifxdeu_cbc_des3_ede_alg.base.cra_list),
684 .min_keysize = DES3_EDE_KEY_SIZE,
685 .max_keysize = DES3_EDE_KEY_SIZE,
686 .ivsize = DES_BLOCK_SIZE,
687 .setkey = des3_ede_setkey_skcipher,
688 .encrypt = cbc_des_encrypt,
689 .decrypt = cbc_des_decrypt,
690 };
691
692 /*! \fn int ifxdeu_init_des (void)
693 * \ingroup IFX_DES_FUNCTIONS
694 * \brief initialize des driver
695 */
696 int ifxdeu_init_des (void)
697 {
698 int ret = -ENOSYS;
699
700 des_chip_init();
701
702 ret = crypto_register_alg(&ifxdeu_des_alg);
703 if (ret < 0)
704 goto des_err;
705
706 ret = crypto_register_skcipher(&ifxdeu_ecb_des_alg);
707 if (ret < 0)
708 goto ecb_des_err;
709
710 ret = crypto_register_skcipher(&ifxdeu_cbc_des_alg);
711 if (ret < 0)
712 goto cbc_des_err;
713
714 ret = crypto_register_alg(&ifxdeu_des3_ede_alg);
715 if (ret < 0)
716 goto des3_ede_err;
717
718 ret = crypto_register_skcipher(&ifxdeu_ecb_des3_ede_alg);
719 if (ret < 0)
720 goto ecb_des3_ede_err;
721
722 ret = crypto_register_skcipher(&ifxdeu_cbc_des3_ede_alg);
723 if (ret < 0)
724 goto cbc_des3_ede_err;
725
726 CRTCL_SECT_INIT;
727
728
729
730 printk (KERN_NOTICE "IFX DEU DES initialized%s%s.\n", disable_multiblock ? "" : " (multiblock)", disable_deudma ? "" : " (DMA)");
731 return ret;
732
733 des_err:
734 crypto_unregister_alg(&ifxdeu_des_alg);
735 printk(KERN_ERR "IFX des initialization failed!\n");
736 return ret;
737 ecb_des_err:
738 crypto_unregister_skcipher(&ifxdeu_ecb_des_alg);
739 printk (KERN_ERR "IFX ecb_des initialization failed!\n");
740 return ret;
741 cbc_des_err:
742 crypto_unregister_skcipher(&ifxdeu_cbc_des_alg);
743 printk (KERN_ERR "IFX cbc_des initialization failed!\n");
744 return ret;
745 des3_ede_err:
746 crypto_unregister_alg(&ifxdeu_des3_ede_alg);
747 printk(KERN_ERR "IFX des3_ede initialization failed!\n");
748 return ret;
749 ecb_des3_ede_err:
750 crypto_unregister_skcipher(&ifxdeu_ecb_des3_ede_alg);
751 printk (KERN_ERR "IFX ecb_des3_ede initialization failed!\n");
752 return ret;
753 cbc_des3_ede_err:
754 crypto_unregister_skcipher(&ifxdeu_cbc_des3_ede_alg);
755 printk (KERN_ERR "IFX cbc_des3_ede initialization failed!\n");
756 return ret;
757
758 }
759
760 /*! \fn void ifxdeu_fini_des (void)
761 * \ingroup IFX_DES_FUNCTIONS
762 * \brief unregister des driver
763 */
764 void ifxdeu_fini_des (void)
765 {
766 crypto_unregister_alg (&ifxdeu_des_alg);
767 crypto_unregister_skcipher (&ifxdeu_ecb_des_alg);
768 crypto_unregister_skcipher (&ifxdeu_cbc_des_alg);
769 crypto_unregister_alg (&ifxdeu_des3_ede_alg);
770 crypto_unregister_skcipher (&ifxdeu_ecb_des3_ede_alg);
771 crypto_unregister_skcipher (&ifxdeu_cbc_des3_ede_alg);
772
773 }