aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/ubicom32/files/arch/ubicom32/crypto/des_ubicom32.c
blob: 14e5fb24e6dcd79a94e78f88bba1e200d8ec86cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/*
 * arch/ubicom32/crypto/des_ubicom32.c
 *   Ubicom32 implementation of the DES Cipher Algorithm.
 *
 * (C) Copyright 2009, Ubicom, Inc.
 *
 * This file is part of the Ubicom32 Linux Kernel Port.
 *
 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 *
 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Ubicom32 Linux Kernel Port.  If not,
 * see <http://www.gnu.org/licenses/>.
 *
 * Ubicom32 implementation derived from (with many thanks):
 *   arch/m68knommu
 *   arch/blackfin
 *   arch/parisc
 */
#include <crypto/algapi.h>
#include <linux/init.h>
#include <linux/module.h>

#include "crypto_ubicom32.h"
extern int crypto_des_check_key(const u8 *key, unsigned int keylen, u32 *flags);

#define DES_BLOCK_SIZE 8
#define DES_KEY_SIZE 8

#define DES3_192_KEY_SIZE	(3 * DES_KEY_SIZE)
#define DES3_192_BLOCK_SIZE	DES_BLOCK_SIZE

#define DES3_SUB_KEY(key, i) 	(((u8 *)key) + (i * DES_KEY_SIZE))

enum des_ops {
	DES_ENCRYPT,
	DES_DECRYPT,

	DES3_EDE_ENCRYPT,
	DES3_EDE_DECRYPT,

#ifdef DES3_EEE
	DES3_EEE_ENCRYPT,
	DES3_EEE_DECRYPT,
#endif
};

struct ubicom32_des_ctx {
	u8 key[3 * DES_KEY_SIZE];
	u32 ctrl;
	int key_len;
};

static inline void des_hw_set_key(const u8 *key, u8 key_len)
{
	/*
	 * HW 3DES is not tested yet, use DES just as ipOS
	 */
	DES_SET_KEY(key);
}

static inline void des_hw_cipher(u8 *out, const u8 *in)
{
	SEC_SET_INPUT_2W(in);

	asm volatile (
	"	; start DES by writing 0x38(SECURITY_BASE)	\n\t"
	"	move.4 0x38(%0), #0x01				\n\t"
	"	pipe_flush 0					\n\t"
	"							\n\t"
	"	; wait for the module to calculate the output	\n\t"
	"	btst 0x04(%0), #0				\n\t"
	"	jmpne.f .-4					\n\t"
		:
		: "a" (SEC_BASE)
		: "cc"
	);

	SEC_GET_OUTPUT_2W(out);
}


static void inline des3_hw_ede_encrypt(u8 *keys, u8 *out, const u8 *in)
{
	hw_crypto_set_ctrl(SEC_ALG_DES | SEC_DIR_ENCRYPT);
	des_hw_set_key(DES3_SUB_KEY(keys, 0), DES_KEY_SIZE);
	des_hw_cipher(out, in);

	hw_crypto_set_ctrl(SEC_ALG_DES | SEC_DIR_DECRYPT);
	des_hw_set_key(DES3_SUB_KEY(keys, 1), DES_KEY_SIZE);
	des_hw_cipher(out, out);

	hw_crypto_set_ctrl(SEC_ALG_DES | SEC_DIR_ENCRYPT);
	des_hw_set_key(DES3_SUB_KEY(keys, 2), DES_KEY_SIZE);
	des_hw_cipher(out, out);
}

static void inline des3_hw_ede_decrypt(u8 *keys, u8 *out, const u8 *in)
{
	hw_crypto_set_ctrl(SEC_ALG_DES | SEC_DIR_DECRYPT);
	des_hw_set_key(DES3_SUB_KEY(keys, 2), DES_KEY_SIZE);
	des_hw_cipher(out, in);

	hw_crypto_set_ctrl(SEC_ALG_DES | SEC_DIR_ENCRYPT);
	des_hw_set_key(DES3_SUB_KEY(keys, 1), DES_KEY_SIZE);
	des_hw_cipher(out, out);

	hw_crypto_set_ctrl(SEC_ALG_DES | SEC_DIR_DECRYPT);
	des_hw_set_key(DES3_SUB_KEY(keys, 0), DES_KEY_SIZE);
	des_hw_cipher(out, out);
}

#ifdef DES3_EEE
static void inline des3_hw_eee_encrypt(u8 *keys, u8 *out, const u8 *in)
{
	hw_crypto_set_ctrl(SEC_ALG_DES | SEC_DIR_ENCRYPT);
	des_hw_set_key(DES3_SUB_KEY(keys, 0), 2);
	des_hw_cipher(out, in);

	hw_crypto_set_ctrl(SEC_ALG_DES | SEC_DIR_ENCRYPT);
	des_hw_set_key(DES3_SUB_KEY(keys, 1), 2);
	des_hw_cipher(out, out);

	hw_crypto_set_ctrl(SEC_ALG_DES | SEC_DIR_ENCRYPT);
	des_hw_set_key(DES3_SUB_KEY(keys, 2), 2);
	des_hw_cipher(out, out);
}

static void inline des3_hw_eee_decrypt(u8 *keys, u8 *out, const u8 *in)
{
	hw_crypto_set_ctrl(SEC_ALG_DES | SEC_DIR_DECRYPT);
	des_hw_set_key(DES3_SUB_KEY(keys, 2), 2);
	des_hw_cipher(out, in);

	hw_crypto_set_ctrl(SEC_ALG_DES | SEC_DIR_DECRYPT);
	des_hw_set_key(DES3_SUB_KEY(keys, 1), 2);
	des_hw_cipher(out, out);

	hw_crypto_set_ctrl(SEC_ALG_DES | SEC_DIR_DECRYPT);
	des_hw_set_key(DES3_SUB_KEY(keys, 0), 2);
	des_hw_cipher(out, out);
}
#endif

static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
		      unsigned int keylen)
{
	struct ubicom32_des_ctx *dctx = crypto_tfm_ctx(tfm);
	u32 *flags = &tfm->crt_flags;
	int ret;

	/* test if key is valid (not a weak key) */
	ret = crypto_des_check_key(key, keylen, flags);
	if (ret == 0) {
		memcpy(dctx->key, key, keylen);
		dctx->key_len = keylen;
		//dctx->ctrl = (keylen == DES_KEY_SIZE) ? SEC_ALG_DES : SEC_ALG_3DES
		/* 2DES and 3DES are both implemented with DES hw function */
		dctx->ctrl = SEC_ALG_DES;
	}
	return ret;
}

static inline void des_cipher_1b(struct crypto_tfm *tfm, u8 *out, const u8 *in, u32 extra_flags)
{
	const struct ubicom32_des_ctx *uctx = crypto_tfm_ctx(tfm);

	hw_crypto_lock();
	hw_crypto_check();
	hw_crypto_set_ctrl(uctx->ctrl | extra_flags);

	des_hw_set_key(uctx->key, uctx->key_len);
	des_hw_cipher(out, in);

	hw_crypto_unlock();
}

static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
{
	des_cipher_1b(tfm, out, in, SEC_DIR_ENCRYPT);
}

static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
{
	des_cipher_1b(tfm, out, in, SEC_DIR_DECRYPT);
}

static struct crypto_alg des_alg = {
	.cra_name		=	"des",
	.cra_driver_name	=	"des-ubicom32",
	.cra_priority		=	CRYPTO_UBICOM32_PRIORITY,
	.cra_flags		=	CRYPTO_ALG_TYPE_CIPHER,
	.cra_blocksize		=	DES_BLOCK_SIZE,
	.cra_ctxsize		=	sizeof(struct ubicom32_des_ctx),
	.cra_alignmask		=	CRYPTO_UBICOM32_ALIGNMENT - 1,
	.cra_alignmask		=	CRYPTO_UBICOM32_ALIGNMENT - 1,
	.cra_module		=	THIS_MODULE,
	.cra_list		=	LIST_HEAD_INIT(des_alg.cra_list),
	.cra_u			= {
		.cipher = {
			.cia_min_keysize	=	DES_KEY_SIZE,
			.cia_max_keysize	=	DES_KEY_SIZE,
			.cia_setkey		=	des_setkey,
			.cia_encrypt		=	des_encrypt,
			.cia_decrypt		=	des_decrypt,
		}
	}
};

static void ecb_des_ciper_loop(u8 *out, u8 *in, unsigned int n)
{
	while (likely(n)) {
		des_hw_cipher(out, in);
		out += DES_BLOCK_SIZE;
		in += DES_BLOCK_SIZE;
		n -= DES_BLOCK_SIZE;
	}
}

static void ecb_des3_ede_encrypt_loop(u8 *keys, u8 *out, u8 *in, unsigned int n)
{
	while (likely(n)) {
		des3_hw_ede_encrypt(keys, out, in);

		out += DES_BLOCK_SIZE;
		in += DES_BLOCK_SIZE;
		n -= DES_BLOCK_SIZE;
	}
}

static void ecb_des3_ede_decrypt_loop(u8 *keys, u8 *out, u8 *in, unsigned int n)
{
	while (likely(n)) {
		des3_hw_ede_decrypt(keys, out, in);

		out += DES_BLOCK_SIZE;
		in += DES_BLOCK_SIZE;
		n -= DES_BLOCK_SIZE;
	}
}

#ifdef DES3_EEE
static void ecb_des3_eee_encrypt_loop(u8 *keys, u8 *out, u8 *in, unsigned int n)
{
	while (likely(n)) {
		des3_hw_eee_encrypt(keys, out, in);

		out += DES_BLOCK_SIZE;
		in += DES_BLOCK_SIZE;
		n -= DES_BLOCK_SIZE;
	}
}

static void ecb_des3_eee_decrypt_loop(u8 *keys, u8 *out, u8 *in, unsigned int n)
{
	while (likely(n)) {
		des3_hw_eee_decrypt(keys, out, in);

		out += DES_BLOCK_SIZE;
		in += DES_BLOCK_SIZE;
		n -= DES_BLOCK_SIZE;
	}
}
#endif

static inline void ecb_des_cipher_n(struct ubicom32_des_ctx *uctx, enum des_ops op, u8 *out, u8 *in, unsigned int n)
{
	switch (op) {
	case DES_ENCRYPT:
	case DES_DECRYPT:
		/* set the right algo, direction and key once */
		hw_crypto_set_ctrl(SEC_ALG_DES | (op == DES_ENCRYPT ? SEC_DIR_ENCRYPT : 0));
		des_hw_set_key(uctx->key, uctx->key_len);
		ecb_des_ciper_loop(out, in, n);
		break;

	case DES3_EDE_ENCRYPT:
		ecb_des3_ede_encrypt_loop(uctx->key, out, in, n);
		break;

	case DES3_EDE_DECRYPT:
		ecb_des3_ede_decrypt_loop(uctx->key, out, in, n);
		break;

#ifdef DES3_EEE
	case DES3_EEE_ENCRYPT:
		ecb_des3_eee_encrypt_loop(uctx->key, out, in, n);
		break;

	case DES3_EEE_DECRYPT:
		ecb_des3_eee_decrypt_loop(uctx->key, out, in, n);
		break;
#endif
	}
}

static inline void des_xor_2w(u32 *data, u32 *iv)
{
	data[0] ^= iv[0];
	data[1] ^= iv[1];
}

static void cbc_des_encrypt_loop(u8 *out, u8 *in, u8 *iv, unsigned int n)
{
	while (likely(n)) {
		des_xor_2w((u32 *)in, (u32 *)iv);
		des_hw_cipher(out, in);
		SEC_COPY_2W(iv, out);
		out += DES_BLOCK_SIZE;
		in += DES_BLOCK_SIZE;
		n -= DES_BLOCK_SIZE;
	}
}

static void cbc_des_decrypt_loop(u8 *out, u8 *in, u8 *iv, unsigned int n)
{
	u8 next_iv[DES_BLOCK_SIZE];
	while (likely(n)) {
		SEC_COPY_2W(next_iv, in);
		des_hw_cipher(out, in);
		des_xor_2w((u32 *)out, (u32 *)iv);
		SEC_COPY_2W(iv, next_iv);

		out += DES_BLOCK_SIZE;
		in += DES_BLOCK_SIZE;
		n -= DES_BLOCK_SIZE;
	}
}

static void cbc_des3_ede_encrypt_loop(u8 *keys, u8 *out, u8 *in, u8 *iv, unsigned int n)
{
	while (likely(n)) {
		des_xor_2w((u32 *)in, (u32 *)iv);
		des3_hw_ede_encrypt(keys, out, in);
		SEC_COPY_2W(iv, out);

		out += DES_BLOCK_SIZE;
		in += DES_BLOCK_SIZE;
		n -= DES_BLOCK_SIZE;
	}
}

static void cbc_des3_ede_decrypt_loop(u8 *keys, u8 *out, u8 *in, u8 *iv, unsigned int n)
{
	u8 next_iv[DES_BLOCK_SIZE];
	while (likely(n)) {
		SEC_COPY_2W(next_iv, in);
		des3_hw_ede_decrypt(keys, out, in);
		des_xor_2w((u32 *)out, (u32 *)iv);
		SEC_COPY_2W(iv, next_iv);

		out += DES_BLOCK_SIZE;
		in += DES_BLOCK_SIZE;
		n -= DES_BLOCK_SIZE;
	}
}

#ifdef DES3_EEE
static void cbc_des3_eee_encrypt_loop(u8 *keys, u8 *out, u8 *in, u8 *iv, unsigned int n)
{
	while (likely(n)) {
		des_xor_2w((u32 *)in, (u32 *)iv);
		des3_hw_eee_encrypt(keys, out, in);
		SEC_COPY_2W(iv, out);

		out += DES_BLOCK_SIZE;
		in += DES_BLOCK_SIZE;
		n -= DES_BLOCK_SIZE;
	}
}

static void cbc_des3_eee_decrypt_loop(u8 *keys, u8 *out, u8 *in, u8 *iv, unsigned int n)
{
	u8 next_iv[DES_BLOCK_SIZE];
	while (likely(n)) {
		SEC_COPY_2W(next_iv, in);
		des3_hw_eee_decrypt(keys, out, in);
		des_xor_2w((u32 *)out, (u32 *)iv);
		SEC_COPY_2W(iv, next_iv);

		out += DES_BLOCK_SIZE;
		in += DES_BLOCK_SIZE;
		n -= DES_BLOCK_SIZE;
	}
}
#endif

static inline void cbc_des_cipher_n(struct ubicom32_des_ctx *uctx, enum des_ops op, u8 *out, u8 *in, u8 *iv, unsigned int n)
{
	switch (op) {
	case DES_ENCRYPT:
		hw_crypto_set_ctrl(SEC_ALG_DES | SEC_DIR_ENCRYPT);
		des_hw_set_key(uctx->key, uctx->key_len);
		cbc_des_encrypt_loop(out, in, iv, n);
		break;

	case DES_DECRYPT:
		/* set the right algo, direction and key once */
		hw_crypto_set_ctrl(SEC_ALG_DES | SEC_DIR_DECRYPT);
		des_hw_set_key(uctx->key, uctx->key_len);
		cbc_des_decrypt_loop(out, in, iv, n);
		break;

	case DES3_EDE_ENCRYPT:
		cbc_des3_ede_encrypt_loop(uctx->key, out, in, iv, n);
		break;

	case DES3_EDE_DECRYPT:
		cbc_des3_ede_decrypt_loop(uctx->key, out, in, iv, n);
		break;

#ifdef DES3_EEE
	case DES3_EEE_ENCRYPT:
		cbc_des3_eee_encrypt_loop(uctx->key, out, in, iv, n);
		break;

	case DES3_EEE_DECRYPT:
		cbc_des3_eee_decrypt_loop(uctx->key, out, in, iv, n);
		break;
#endif
	}
}

static int des_cipher(struct blkcipher_desc *desc, struct scatterlist *dst,
		      struct scatterlist *src, unsigned int nbytes, u32 extra_flags, enum des_ops op)
{
	struct ubicom32_des_ctx *uctx = crypto_blkcipher_ctx(desc->tfm);
	int ret;

	struct blkcipher_walk walk;
	blkcipher_walk_init(&walk, dst, src, nbytes);
	ret = blkcipher_walk_virt(desc, &walk);
	if (ret) {
		return ret;
	}

	hw_crypto_lock();
	hw_crypto_check();

	while ((nbytes = walk.nbytes)) {
		/* only use complete blocks */
		unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
		u8 *out = walk.dst.virt.addr;
		u8 *in = walk.src.virt.addr;

		/* finish n/16 blocks */
		if (extra_flags & SEC_CBC_SET) {
			cbc_des_cipher_n(uctx, op, out, in, walk.iv, n);
		} else {
			ecb_des_cipher_n(uctx, op, out, in, n);
		}

		nbytes &= DES_BLOCK_SIZE - 1;
		ret = blkcipher_walk_done(desc, &walk, nbytes);
	}

	hw_crypto_unlock();
	return ret;
}

static int ecb_des_encrypt(struct blkcipher_desc *desc,
			   struct scatterlist *dst, struct scatterlist *src,
			   unsigned int nbytes)
{
	return des_cipher(desc, dst, src, nbytes, SEC_CBC_NONE, DES_ENCRYPT);
}

static int ecb_des_decrypt(struct blkcipher_desc *desc,
			   struct scatterlist *dst, struct scatterlist *src,
			   unsigned int nbytes)
{
	return des_cipher(desc, dst, src, nbytes, SEC_CBC_NONE, DES_DECRYPT);
}

static struct crypto_alg ecb_des_alg = {
	.cra_name		=	"ecb(des)",
	.cra_driver_name	=	"ecb-des-ubicom32",
	.cra_priority		=	CRYPTO_UBICOM32_COMPOSITE_PRIORITY,
	.cra_flags		=	CRYPTO_ALG_TYPE_BLKCIPHER,
	.cra_blocksize		=	DES_BLOCK_SIZE,
	.cra_ctxsize		=	sizeof(struct ubicom32_des_ctx),
	.cra_alignmask		=	CRYPTO_UBICOM32_ALIGNMENT - 1,
	.cra_type		=	&crypto_blkcipher_type,
	.cra_module		=	THIS_MODULE,
	.cra_list		=	LIST_HEAD_INIT(ecb_des_alg.cra_list),
	.cra_u			= {
		.blkcipher = {
			.min_keysize		=	DES_KEY_SIZE,
			.max_keysize		=	DES_KEY_SIZE,
			.setkey			=	des_setkey,
			.encrypt		=	ecb_des_encrypt,
			.decrypt		=	ecb_des_decrypt,
		}
	}
};

static int cbc_des_encrypt(struct blkcipher_desc *desc,
			   struct scatterlist *dst, struct scatterlist *src,
			   unsigned int nbytes)
{
	return des_cipher(desc, dst, src, nbytes, SEC_CBC_SET, DES_ENCRYPT);
}

static int cbc_des_decrypt(struct blkcipher_desc *desc,
			   struct scatterlist *dst, struct scatterlist *src,
			   unsigned int nbytes)
{
	return des_cipher(desc, dst, src, nbytes, SEC_CBC_SET, DES_DECRYPT);
}

static struct crypto_alg cbc_des_alg = {
	.cra_name		=	"cbc(des)",
	.cra_driver_name	=	"cbc-des-ubicom32",
	.cra_priority		=	CRYPTO_UBICOM32_COMPOSITE_PRIORITY,
	.cra_flags		=	CRYPTO_ALG_TYPE_BLKCIPHER,
	.cra_blocksize		=	DES_BLOCK_SIZE,
	.cra_ctxsize		=	sizeof(struct ubicom32_des_ctx),
	.cra_alignmask		=	CRYPTO_UBICOM32_ALIGNMENT - 1,
	.cra_type		=	&crypto_blkcipher_type,
	.cra_module		=	THIS_MODULE,
	.cra_list		=	LIST_HEAD_INIT(cbc_des_alg.cra_list),
	.cra_u			= {
		.blkcipher = {
			.min_keysize		=	DES_KEY_SIZE,
			.max_keysize		=	DES_KEY_SIZE,
			.ivsize			=	DES_BLOCK_SIZE,
			.setkey			=	des_setkey,
			.encrypt		=	cbc_des_encrypt,
			.decrypt		=	cbc_des_decrypt,
		}
	}
};

/*
 * RFC2451:
 *
 *   For DES-EDE3, there is no known need to reject weak or
 *   complementation keys.  Any weakness is obviated by the use of
 *   multiple keys.
 *
 *   However, if the first two or last two independent 64-bit keys are
 *   equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
 *   same as DES.  Implementers MUST reject keys that exhibit this
 *   property.
 *
 */
static int des3_192_setkey(struct crypto_tfm *tfm, const u8 *key,
			   unsigned int keylen)
{
	int i, ret;
	struct ubicom32_des_ctx *dctx = crypto_tfm_ctx(tfm);
	const u8 *temp_key = key;
	u32 *flags = &tfm->crt_flags;

	if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
	    memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
		   DES_KEY_SIZE))) {

		*flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
		return -EINVAL;
	}
	for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) {
		ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
		if (ret < 0)
			return ret;
	}
	memcpy(dctx->key, key, keylen);
	dctx->ctrl = SEC_ALG_DES;	//hw 3DES not working yet
	dctx->key_len = keylen;
	return 0;
}

static void des3_192_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
{
	struct ubicom32_des_ctx *uctx = crypto_tfm_ctx(tfm);

	hw_crypto_lock();
	hw_crypto_check();

	des3_hw_ede_encrypt(uctx->key, dst, src);

	hw_crypto_unlock();
}

static void des3_192_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
{
	struct ubicom32_des_ctx *uctx = crypto_tfm_ctx(tfm);

	hw_crypto_lock();
	hw_crypto_check();

	des3_hw_ede_decrypt(uctx->key, dst, src);

	hw_crypto_unlock();
}

static struct crypto_alg des3_192_alg = {
	.cra_name		=	"des3_ede",
	.cra_driver_name	=	"des3_ede-ubicom32",
	.cra_priority		=	CRYPTO_UBICOM32_PRIORITY,
	.cra_flags		=	CRYPTO_ALG_TYPE_CIPHER,
	.cra_blocksize		=	DES3_192_BLOCK_SIZE,
	.cra_ctxsize		=	sizeof(struct ubicom32_des_ctx),
	.cra_alignmask		=	CRYPTO_UBICOM32_ALIGNMENT - 1,
	.cra_module		=	THIS_MODULE,
	.cra_list		=	LIST_HEAD_INIT(des3_192_alg.cra_list),
	.cra_u			= {
		.cipher = {
			.cia_min_keysize	=	DES3_192_KEY_SIZE,
			.cia_max_keysize	=	DES3_192_KEY_SIZE,
			.cia_setkey		=	des3_192_setkey,
			.cia_encrypt		=	des3_192_encrypt,
			.cia_decrypt		=	des3_192_decrypt,
		}
	}
};

static int ecb_des3_192_encrypt(struct blkcipher_desc *desc,
				struct scatterlist *dst,
				struct scatterlist *src, unsigned int nbytes)
{
	return des_cipher(desc, dst, src, nbytes, SEC_CBC_NONE, DES3_EDE_ENCRYPT);
}

static int ecb_des3_192_decrypt(struct blkcipher_desc *desc,
				struct scatterlist *dst,
				struct scatterlist *src, unsigned int nbytes)
{
	return des_cipher(desc, dst, src, nbytes, SEC_CBC_NONE, DES3_EDE_DECRYPT);
}

static struct crypto_alg ecb_des3_192_alg = {
	.cra_name		=	"ecb(des3_ede)",
	.cra_driver_name	=	"ecb-des3_ede-ubicom32",
	.cra_priority		=	CRYPTO_UBICOM32_COMPOSITE_PRIORITY,
	.cra_flags		=	CRYPTO_ALG_TYPE_BLKCIPHER,
	.cra_blocksize		=	DES3_192_BLOCK_SIZE,
	.cra_ctxsize		=	sizeof(struct ubicom32_des_ctx),
	.cra_alignmask		=	CRYPTO_UBICOM32_ALIGNMENT - 1,
	.cra_type		=	&crypto_blkcipher_type,
	.cra_module		=	THIS_MODULE,
	.cra_list		=	LIST_HEAD_INIT(
						ecb_des3_192_alg.cra_list),
	.cra_u			= {
		.blkcipher = {
			.min_keysize		=	DES3_192_KEY_SIZE,
			.max_keysize		=	DES3_192_KEY_SIZE,
			.setkey			=	des3_192_setkey,
			.encrypt		=	ecb_des3_192_encrypt,
			.decrypt		=	ecb_des3_192_decrypt,
		}
	}
};

static int cbc_des3_192_encrypt(struct blkcipher_desc *desc,
				struct scatterlist *dst,
				struct scatterlist *src, unsigned int nbytes)
{
	return des_cipher(desc, dst, src, nbytes, SEC_CBC_SET, DES3_EDE_ENCRYPT);
}

static int cbc_des3_192_decrypt(struct blkcipher_desc *desc,
				struct scatterlist *dst,
				struct scatterlist *src, unsigned int nbytes)
{
	return des_cipher(desc, dst, src, nbytes, SEC_CBC_SET, DES3_EDE_DECRYPT);
}

static struct crypto_alg cbc_des3_192_alg = {
	.cra_name		=	"cbc(des3_ede)",
	.cra_driver_name	=	"cbc-des3_ede-ubicom32",
	.cra_priority		=	CRYPTO_UBICOM32_COMPOSITE_PRIORITY,
	.cra_flags		=	CRYPTO_ALG_TYPE_BLKCIPHER,
	.cra_blocksize		=	DES3_192_BLOCK_SIZE,
	.cra_ctxsize		=	sizeof(struct ubicom32_des_ctx),
	.cra_alignmask		=	CRYPTO_UBICOM32_ALIGNMENT - 1,
	.cra_type		=	&crypto_blkcipher_type,
	.cra_module		=	THIS_MODULE,
	.cra_list		=	LIST_HEAD_INIT(
						cbc_des3_192_alg.cra_list),
	.cra_u			= {
		.blkcipher = {
			.min_keysize		=	DES3_192_KEY_SIZE,
			.max_keysize		=	DES3_192_KEY_SIZE,
			.ivsize			=	DES3_192_BLOCK_SIZE,
			.setkey			=	des3_192_setkey,
			.encrypt		=	cbc_des3_192_encrypt,
			.decrypt		=	cbc_des3_192_decrypt,
		}
	}
};

static int init(void)
{
	int ret = 0;

	hw_crypto_init();

	ret = crypto_register_alg(&des_alg);
	if (ret)
		goto des_err;
	ret = crypto_register_alg(&ecb_des_alg);
	if (ret)
		goto ecb_des_err;
	ret = crypto_register_alg(&cbc_des_alg);
	if (ret)
		goto cbc_des_err;

	ret = crypto_register_alg(&des3_192_alg);
	if (ret)
		goto des3_192_err;
	ret = crypto_register_alg(&ecb_des3_192_alg);
	if (ret)
		goto ecb_des3_192_err;
	ret = crypto_register_alg(&cbc_des3_192_alg);
	if (ret)
		goto cbc_des3_192_err;

out:
	return ret;

cbc_des3_192_err:
	crypto_unregister_alg(&ecb_des3_192_alg);
ecb_des3_192_err:
	crypto_unregister_alg(&des3_192_alg);
des3_192_err:
	crypto_unregister_alg(&cbc_des_alg);
cbc_des_err:
	crypto_unregister_alg(&ecb_des_alg);
ecb_des_err:
	crypto_unregister_alg(&des_alg);
des_err:
	goto out;
}

static void __exit fini(void)
{
	crypto_unregister_alg(&cbc_des3_192_alg);
	crypto_unregister_alg(&ecb_des3_192_alg);
	crypto_unregister_alg(&des3_192_alg);
	crypto_unregister_alg(&cbc_des_alg);
	crypto_unregister_alg(&ecb_des_alg);
	crypto_unregister_alg(&des_alg);
}

module_init(init);
module_exit(fini);

MODULE_ALIAS("des");
MODULE_ALIAS("des3_ede");

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");