aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/ubicom32/files/arch/ubicom32/crypto/aes_ubicom32.c
blob: f0b9f86bcab041f461e15ae35c8ce4cb737b9436 (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
/*
 * arch/ubicom32/crypto/aes_ubicom32.c
 *   Ubicom32 implementation of the AES 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/aes.h>
#include <crypto/algapi.h>
#include <linux/err.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/spinlock.h>
#include "crypto_ubicom32.h"
#include <asm/linkage.h>

struct ubicom32_aes_ctx {
	u8 key[AES_MAX_KEY_SIZE];
	u32 ctrl;
	int key_len;
};

static inline void aes_hw_set_key(const u8 *key, u8 key_len)
{
	/*
	 * switch case has more overhead than 4 move.4 instructions, so just copy 256 bits
	 */
	SEC_SET_KEY_256(key);
}

static inline void aes_hw_set_iv(const u8 *iv)
{
	SEC_SET_IV_4W(iv);
}

static inline void aes_hw_cipher(u8 *out, const u8 *in)
{
	SEC_SET_INPUT_4W(in);

	asm volatile (
	"	; start AES by writing 0x40(SECURITY_BASE)	\n\t"
	"	move.4 0x40(%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_4W(out);
}

static int __ocm_text aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
		       unsigned int key_len)
{
	struct ubicom32_aes_ctx *uctx = crypto_tfm_ctx(tfm);

	uctx->key_len = key_len;
	memcpy(uctx->key, in_key, key_len);

	/*
	 * leave out HASH_ALG (none = 0), CBC (no = 0), DIR (unknown) yet
	 */
	switch (uctx->key_len) {
	case 16:
		uctx->ctrl = SEC_KEY_128_BITS | SEC_ALG_AES;
		break;
	case 24:
		uctx->ctrl = SEC_KEY_192_BITS | SEC_ALG_AES;
		break;
	case 32:
		uctx->ctrl = SEC_KEY_256_BITS | SEC_ALG_AES;
		break;
	}

	return 0;
}

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

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

	aes_hw_set_key(uctx->key, uctx->key_len);
	aes_hw_cipher(out, in);

	hw_crypto_unlock();
}

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

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

static struct crypto_alg aes_alg = {
	.cra_name		=	"aes",
	.cra_driver_name	=	"aes-ubicom32",
	.cra_priority		=	CRYPTO_UBICOM32_PRIORITY,
	.cra_flags		=	CRYPTO_ALG_TYPE_CIPHER,
	.cra_blocksize		=	AES_BLOCK_SIZE,
	.cra_ctxsize		=	sizeof(struct ubicom32_aes_ctx),
	.cra_alignmask		=	CRYPTO_UBICOM32_ALIGNMENT - 1,
	.cra_module		=	THIS_MODULE,
	.cra_list		=	LIST_HEAD_INIT(aes_alg.cra_list),
	.cra_u			=	{
		.cipher = {
			.cia_min_keysize	=	AES_MIN_KEY_SIZE,
			.cia_max_keysize	=	AES_MAX_KEY_SIZE,
			.cia_setkey		=	aes_set_key,
			.cia_encrypt		=	aes_encrypt,
			.cia_decrypt		=	aes_decrypt,
		}
	}
};

static void __ocm_text ecb_aes_crypt_loop(u8 *out, u8 *in, unsigned int n)
{
	while (likely(n)) {
		aes_hw_cipher(out, in);
		out += AES_BLOCK_SIZE;
		in += AES_BLOCK_SIZE;
		n -= AES_BLOCK_SIZE;
	}
}

static int __ocm_text ecb_aes_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
			 struct scatterlist *src, unsigned int nbytes, u32 extra_flags)
{
	const struct ubicom32_aes_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();

        hw_crypto_set_ctrl(uctx->ctrl | extra_flags);
        aes_hw_set_key(uctx->key, uctx->key_len);

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

		/* finish n/16 blocks */
		ecb_aes_crypt_loop(out, in, n);

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

	hw_crypto_unlock();
	return ret;
}

static int ecb_aes_encrypt(struct blkcipher_desc *desc,
			   struct scatterlist *dst, struct scatterlist *src,
			   unsigned int nbytes)
{
	return ecb_aes_crypt(desc, dst, src, nbytes, SEC_DIR_ENCRYPT);
}

static int ecb_aes_decrypt(struct blkcipher_desc *desc,
			   struct scatterlist *dst, struct scatterlist *src,
			   unsigned int nbytes)
{
	return ecb_aes_crypt(desc, dst, src, nbytes, SEC_DIR_DECRYPT);
}

static struct crypto_alg ecb_aes_alg = {
	.cra_name		=	"ecb(aes)",
	.cra_driver_name	=	"ecb-aes-ubicom32",
	.cra_priority		=	CRYPTO_UBICOM32_COMPOSITE_PRIORITY,
	.cra_flags		=	CRYPTO_ALG_TYPE_BLKCIPHER,
	.cra_blocksize		=	AES_BLOCK_SIZE,
	.cra_ctxsize		=	sizeof(struct ubicom32_aes_ctx),
	.cra_alignmask          =       CRYPTO_UBICOM32_ALIGNMENT - 1,
	.cra_type		=	&crypto_blkcipher_type,
	.cra_module		=	THIS_MODULE,
	.cra_list		=	LIST_HEAD_INIT(ecb_aes_alg.cra_list),
	.cra_u			=	{
		.blkcipher = {
			.min_keysize		=	AES_MIN_KEY_SIZE,
			.max_keysize		=	AES_MAX_KEY_SIZE,
			.setkey			=	aes_set_key,
			.encrypt		=	ecb_aes_encrypt,
			.decrypt		=	ecb_aes_decrypt,
		}
	}
};

#if CRYPTO_UBICOM32_LOOP_ASM
void __ocm_text cbc_aes_encrypt_loop(u8 *out, u8 *in, u8 *iv, unsigned int n)
{
	asm volatile (
	"; set init. iv 4w			\n\t"
	"	move.4 0x50(%0), 0x0(%3)	\n\t"
	"	move.4 0x54(%0), 0x4(%3)	\n\t"
	"	move.4 0x58(%0), 0x8(%3)	\n\t"
	"	move.4 0x5c(%0), 0xc(%3)	\n\t"
	"					\n\t"
	"; we know n > 0, so we can always	\n\t"
	"; load the first block			\n\t"
	"; set input 4w				\n\t"
	"	move.4 0x30(%0), 0x0(%2)	\n\t"
	"	move.4 0x34(%0), 0x4(%2)	\n\t"
	"	move.4 0x38(%0), 0x8(%2)	\n\t"
	"	move.4 0x3c(%0), 0xc(%2)	\n\t"
	"					\n\t"
	"; kickoff hw				\n\t"
	"	move.4 0x40(%0), %2		\n\t"
	"					\n\t"
	"; update n & flush			\n\t"
	"	add.4 %4, #-16, %4		\n\t"
	"	pipe_flush 0			\n\t"
	"					\n\t"
	"; while (n):  work on 2nd block	\n\t"
	" 1:	lsl.4 d15, %4, #0x0		\n\t"
	"	jmpeq.f 5f			\n\t"
	"					\n\t"
	"; set input 4w	(2nd)			\n\t"
	"	move.4 0x30(%0), 0x10(%2)	\n\t"
	"	move.4 0x34(%0), 0x14(%2)	\n\t"
	"	move.4 0x38(%0), 0x18(%2)	\n\t"
	"	move.4 0x3c(%0), 0x1c(%2)	\n\t"
	"					\n\t"
	"; update n/in asap while waiting	\n\t"
	"	add.4 %4, #-16, %4		\n\t"
	"	move.4 d15, 16(%2)++		\n\t"
	"					\n\t"
	"; wait for the previous output		\n\t"
	"	btst 0x04(%0), #0		\n\t"
	"	jmpne.f -4			\n\t"
	"					\n\t"
	"; read previous output			\n\t"
	"	move.4 0x0(%1), 0x50(%0)	\n\t"
	"	move.4 0x4(%1), 0x54(%0)	\n\t"
	"	move.4 0x8(%1), 0x58(%0)	\n\t"
	"	move.4 0xc(%1), 0x5c(%0)	\n\t"
	"					\n\t"
	"; kick off hw for 2nd input		\n\t"
	"	move.4 0x40(%0), %2		\n\t"
	"					\n\t"
	"; update out asap			\n\t"
	"	move.4 d15, 16(%1)++		\n\t"
	"					\n\t"
	"; go back to loop			\n\t"
	"	jmpt 1b				\n\t"
	"					\n\t"
	"; wait for last output			\n\t"
	" 5:	btst 0x04(%0), #0               \n\t"
        "       jmpne.f -4                      \n\t"
        "                                       \n\t"
	"; read last output			\n\t"
	"	move.4 0x0(%1), 0x50(%0)	\n\t"
	"	move.4 0x4(%1), 0x54(%0)	\n\t"
	"	move.4 0x8(%1), 0x58(%0)	\n\t"
	"	move.4 0xc(%1), 0x5c(%0)	\n\t"
        "                                       \n\t"
	"; copy out iv				\n\t"
	"	move.4 0x0(%3), 0x50(%0)	\n\t"
	"	move.4 0x4(%3), 0x54(%0)	\n\t"
	"	move.4 0x8(%3), 0x58(%0)	\n\t"
	"	move.4 0xc(%3), 0x5c(%0)	\n\t"
        "                                       \n\t"
		:
		: "a" (SEC_BASE), "a" (out), "a" (in), "a" (iv), "d" (n)
		: "d15", "cc"
	);
}

#else

static void __ocm_text cbc_aes_encrypt_loop(u8 *out, u8 *in, u8 *iv, unsigned int n)
{
	aes_hw_set_iv(iv);
	while (likely(n)) {
		aes_hw_cipher(out, in);
		out += AES_BLOCK_SIZE;
		in += AES_BLOCK_SIZE;
		n -= AES_BLOCK_SIZE;
	}
	SEC_COPY_4W(iv, out - AES_BLOCK_SIZE);
}

#endif

static void __ocm_text cbc_aes_decrypt_loop(u8 *out, u8 *in, u8 *iv, unsigned int n)
{
        while (likely(n)) {
                aes_hw_set_iv(iv);
		SEC_COPY_4W(iv, in);
                aes_hw_cipher(out, in);
                out += AES_BLOCK_SIZE;
                in += AES_BLOCK_SIZE;
                n -= AES_BLOCK_SIZE;
        }
}

static int __ocm_text cbc_aes_crypt(struct blkcipher_desc *desc,
                           struct scatterlist *dst, struct scatterlist *src,
                           unsigned int nbytes, u32 extra_flags)
{
	struct ubicom32_aes_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 (unlikely(ret)) {
		return ret;
	}

        hw_crypto_lock();
	hw_crypto_check();

        hw_crypto_set_ctrl(uctx->ctrl | extra_flags);
        aes_hw_set_key(uctx->key, uctx->key_len);

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

			if (extra_flags & SEC_DIR_ENCRYPT) {
				cbc_aes_encrypt_loop(out, in, walk.iv, n);
			} else {
				cbc_aes_decrypt_loop(out, in, walk.iv, n);
			}
		}

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

	return ret;
}

static int __ocm_text cbc_aes_encrypt(struct blkcipher_desc *desc,
			   struct scatterlist *dst, struct scatterlist *src,
			   unsigned int nbytes)
{
	return cbc_aes_crypt(desc, dst, src, nbytes, SEC_DIR_ENCRYPT | SEC_CBC_SET);
}

static int __ocm_text cbc_aes_decrypt(struct blkcipher_desc *desc,
			   struct scatterlist *dst, struct scatterlist *src,
			   unsigned int nbytes)
{
	return cbc_aes_crypt(desc, dst, src, nbytes, SEC_DIR_DECRYPT | SEC_CBC_SET);
}

static struct crypto_alg cbc_aes_alg = {
	.cra_name		=	"cbc(aes)",
	.cra_driver_name	=	"cbc-aes-ubicom32",
	.cra_priority		=	CRYPTO_UBICOM32_COMPOSITE_PRIORITY,
	.cra_flags		=	CRYPTO_ALG_TYPE_BLKCIPHER,
	.cra_blocksize		=	AES_BLOCK_SIZE,
	.cra_ctxsize		=	sizeof(struct ubicom32_aes_ctx),
	.cra_alignmask          =       CRYPTO_UBICOM32_ALIGNMENT - 1,
	.cra_type		=	&crypto_blkcipher_type,
	.cra_module		=	THIS_MODULE,
	.cra_list		=	LIST_HEAD_INIT(cbc_aes_alg.cra_list),
	.cra_u			=	{
		.blkcipher = {
			.min_keysize		=	AES_MIN_KEY_SIZE,
			.max_keysize		=	AES_MAX_KEY_SIZE,
			.ivsize			=	AES_BLOCK_SIZE,
			.setkey			=	aes_set_key,
			.encrypt		=	cbc_aes_encrypt,
			.decrypt		=	cbc_aes_decrypt,
		}
	}
};

static int __init aes_init(void)
{
	int ret;

	hw_crypto_init();

	ret = crypto_register_alg(&aes_alg);
	if (ret)
		goto aes_err;

	ret = crypto_register_alg(&ecb_aes_alg);
	if (ret)
		goto ecb_aes_err;

	ret = crypto_register_alg(&cbc_aes_alg);
	if (ret)
		goto cbc_aes_err;

out:
	return ret;

cbc_aes_err:
	crypto_unregister_alg(&ecb_aes_alg);
ecb_aes_err:
	crypto_unregister_alg(&aes_alg);
aes_err:
	goto out;
}

static void __exit aes_fini(void)
{
	crypto_unregister_alg(&cbc_aes_alg);
	crypto_unregister_alg(&ecb_aes_alg);
	crypto_unregister_alg(&aes_alg);
}

module_init(aes_init);
module_exit(aes_fini);

MODULE_ALIAS("aes");

MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
MODULE_LICENSE("GPL");