2 * Glue code for AES implementation for SPE instructions (PPC)
4 * Based on generic implementation. The assembler module takes care
5 * about the SPE registers so it can run from interrupt context.
7 * Copyright (c) 2015 Markus Stockhausen <stockhausen@collogia.de>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
16 #include <crypto/aes.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/types.h>
20 #include <linux/errno.h>
21 #include <linux/crypto.h>
22 #include <asm/byteorder.h>
23 #include <asm/switch_to.h>
24 #include <crypto/algapi.h>
27 * MAX_BYTES defines the number of bytes that are allowed to be processed
28 * between preempt_disable() and preempt_enable(). e500 cores can issue two
29 * instructions per clock cycle using one 32/64 bit unit (SU1) and one 32
30 * bit unit (SU2). One of these can be a memory access that is executed via
31 * a single load and store unit (LSU). XTS-AES-256 takes ~780 operations per
32 * 16 byte block block or 25 cycles per byte. Thus 768 bytes of input data
33 * will need an estimated maximum of 20,000 cycles. Headroom for cache misses
34 * included. Even with the low end model clocked at 667 MHz this equals to a
35 * critical time window of less than 30us. The value has been choosen to
36 * process a 512 byte disk block in one or a large 1400 bytes IPsec network
43 u32 key_enc[AES_MAX_KEYLENGTH_U32];
44 u32 key_dec[AES_MAX_KEYLENGTH_U32];
49 u32 key_enc[AES_MAX_KEYLENGTH_U32];
50 u32 key_dec[AES_MAX_KEYLENGTH_U32];
51 u32 key_twk[AES_MAX_KEYLENGTH_U32];
55 extern void ppc_encrypt_aes(u8 *out, const u8 *in, u32 *key_enc, u32 rounds);
56 extern void ppc_decrypt_aes(u8 *out, const u8 *in, u32 *key_dec, u32 rounds);
57 extern void ppc_encrypt_ecb(u8 *out, const u8 *in, u32 *key_enc, u32 rounds,
59 extern void ppc_decrypt_ecb(u8 *out, const u8 *in, u32 *key_dec, u32 rounds,
61 extern void ppc_encrypt_cbc(u8 *out, const u8 *in, u32 *key_enc, u32 rounds,
63 extern void ppc_decrypt_cbc(u8 *out, const u8 *in, u32 *key_dec, u32 rounds,
65 extern void ppc_crypt_ctr (u8 *out, const u8 *in, u32 *key_enc, u32 rounds,
67 extern void ppc_encrypt_xts(u8 *out, const u8 *in, u32 *key_enc, u32 rounds,
68 u32 bytes, u8 *iv, u32 *key_twk);
69 extern void ppc_decrypt_xts(u8 *out, const u8 *in, u32 *key_dec, u32 rounds,
70 u32 bytes, u8 *iv, u32 *key_twk);
72 extern void ppc_expand_key_128(u32 *key_enc, const u8 *key);
73 extern void ppc_expand_key_192(u32 *key_enc, const u8 *key);
74 extern void ppc_expand_key_256(u32 *key_enc, const u8 *key);
76 extern void ppc_generate_decrypt_key(u32 *key_dec,u32 *key_enc,
77 unsigned int key_len);
79 static void spe_begin(void)
81 /* disable preemption and save users SPE registers if required */
86 static void spe_end(void)
89 /* reenable preemption */
93 static int ppc_aes_setkey(struct crypto_tfm *tfm, const u8 *in_key,
96 struct ppc_aes_ctx *ctx = crypto_tfm_ctx(tfm);
98 if (key_len != AES_KEYSIZE_128 &&
99 key_len != AES_KEYSIZE_192 &&
100 key_len != AES_KEYSIZE_256) {
101 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
106 case AES_KEYSIZE_128:
108 ppc_expand_key_128(ctx->key_enc, in_key);
110 case AES_KEYSIZE_192:
112 ppc_expand_key_192(ctx->key_enc, in_key);
114 case AES_KEYSIZE_256:
116 ppc_expand_key_256(ctx->key_enc, in_key);
120 ppc_generate_decrypt_key(ctx->key_dec, ctx->key_enc, key_len);
125 static int ppc_xts_setkey(struct crypto_tfm *tfm, const u8 *in_key,
126 unsigned int key_len)
128 struct ppc_xts_ctx *ctx = crypto_tfm_ctx(tfm);
132 if (key_len != AES_KEYSIZE_128 &&
133 key_len != AES_KEYSIZE_192 &&
134 key_len != AES_KEYSIZE_256) {
135 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
140 case AES_KEYSIZE_128:
142 ppc_expand_key_128(ctx->key_enc, in_key);
143 ppc_expand_key_128(ctx->key_twk, in_key + AES_KEYSIZE_128);
145 case AES_KEYSIZE_192:
147 ppc_expand_key_192(ctx->key_enc, in_key);
148 ppc_expand_key_192(ctx->key_twk, in_key + AES_KEYSIZE_192);
150 case AES_KEYSIZE_256:
152 ppc_expand_key_256(ctx->key_enc, in_key);
153 ppc_expand_key_256(ctx->key_twk, in_key + AES_KEYSIZE_256);
157 ppc_generate_decrypt_key(ctx->key_dec, ctx->key_enc, key_len);
162 static void ppc_aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
164 struct ppc_aes_ctx *ctx = crypto_tfm_ctx(tfm);
167 ppc_encrypt_aes(out, in, ctx->key_enc, ctx->rounds);
171 static void ppc_aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
173 struct ppc_aes_ctx *ctx = crypto_tfm_ctx(tfm);
176 ppc_decrypt_aes(out, in, ctx->key_dec, ctx->rounds);
180 static int ppc_ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
181 struct scatterlist *src, unsigned int nbytes)
183 struct ppc_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
184 struct blkcipher_walk walk;
188 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
189 blkcipher_walk_init(&walk, dst, src, nbytes);
190 err = blkcipher_walk_virt(desc, &walk);
192 while ((nbytes = walk.nbytes)) {
193 ubytes = nbytes > MAX_BYTES ?
194 nbytes - MAX_BYTES : nbytes & (AES_BLOCK_SIZE - 1);
198 ppc_encrypt_ecb(walk.dst.virt.addr, walk.src.virt.addr,
199 ctx->key_enc, ctx->rounds, nbytes);
202 err = blkcipher_walk_done(desc, &walk, ubytes);
208 static int ppc_ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
209 struct scatterlist *src, unsigned int nbytes)
211 struct ppc_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
212 struct blkcipher_walk walk;
216 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
217 blkcipher_walk_init(&walk, dst, src, nbytes);
218 err = blkcipher_walk_virt(desc, &walk);
220 while ((nbytes = walk.nbytes)) {
221 ubytes = nbytes > MAX_BYTES ?
222 nbytes - MAX_BYTES : nbytes & (AES_BLOCK_SIZE - 1);
226 ppc_decrypt_ecb(walk.dst.virt.addr, walk.src.virt.addr,
227 ctx->key_dec, ctx->rounds, nbytes);
230 err = blkcipher_walk_done(desc, &walk, ubytes);
236 static int ppc_cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
237 struct scatterlist *src, unsigned int nbytes)
239 struct ppc_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
240 struct blkcipher_walk walk;
244 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
245 blkcipher_walk_init(&walk, dst, src, nbytes);
246 err = blkcipher_walk_virt(desc, &walk);
248 while ((nbytes = walk.nbytes)) {
249 ubytes = nbytes > MAX_BYTES ?
250 nbytes - MAX_BYTES : nbytes & (AES_BLOCK_SIZE - 1);
254 ppc_encrypt_cbc(walk.dst.virt.addr, walk.src.virt.addr,
255 ctx->key_enc, ctx->rounds, nbytes, walk.iv);
258 err = blkcipher_walk_done(desc, &walk, ubytes);
264 static int ppc_cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
265 struct scatterlist *src, unsigned int nbytes)
267 struct ppc_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
268 struct blkcipher_walk walk;
272 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
273 blkcipher_walk_init(&walk, dst, src, nbytes);
274 err = blkcipher_walk_virt(desc, &walk);
276 while ((nbytes = walk.nbytes)) {
277 ubytes = nbytes > MAX_BYTES ?
278 nbytes - MAX_BYTES : nbytes & (AES_BLOCK_SIZE - 1);
282 ppc_decrypt_cbc(walk.dst.virt.addr, walk.src.virt.addr,
283 ctx->key_dec, ctx->rounds, nbytes, walk.iv);
286 err = blkcipher_walk_done(desc, &walk, ubytes);
292 static int ppc_ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
293 struct scatterlist *src, unsigned int nbytes)
295 struct ppc_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
296 struct blkcipher_walk walk;
297 unsigned int pbytes, ubytes;
300 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
301 blkcipher_walk_init(&walk, dst, src, nbytes);
302 err = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
304 while ((pbytes = walk.nbytes)) {
305 pbytes = pbytes > MAX_BYTES ? MAX_BYTES : pbytes;
306 pbytes = pbytes == nbytes ?
307 nbytes : pbytes & ~(AES_BLOCK_SIZE - 1);
308 ubytes = walk.nbytes - pbytes;
311 ppc_crypt_ctr(walk.dst.virt.addr, walk.src.virt.addr,
312 ctx->key_enc, ctx->rounds, pbytes , walk.iv);
316 err = blkcipher_walk_done(desc, &walk, ubytes);
322 static int ppc_xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
323 struct scatterlist *src, unsigned int nbytes)
325 struct ppc_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
326 struct blkcipher_walk walk;
331 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
332 blkcipher_walk_init(&walk, dst, src, nbytes);
333 err = blkcipher_walk_virt(desc, &walk);
336 while ((nbytes = walk.nbytes)) {
337 ubytes = nbytes > MAX_BYTES ?
338 nbytes - MAX_BYTES : nbytes & (AES_BLOCK_SIZE - 1);
342 ppc_encrypt_xts(walk.dst.virt.addr, walk.src.virt.addr,
343 ctx->key_enc, ctx->rounds, nbytes, walk.iv, twk);
347 err = blkcipher_walk_done(desc, &walk, ubytes);
353 static int ppc_xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
354 struct scatterlist *src, unsigned int nbytes)
356 struct ppc_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
357 struct blkcipher_walk walk;
362 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
363 blkcipher_walk_init(&walk, dst, src, nbytes);
364 err = blkcipher_walk_virt(desc, &walk);
367 while ((nbytes = walk.nbytes)) {
368 ubytes = nbytes > MAX_BYTES ?
369 nbytes - MAX_BYTES : nbytes & (AES_BLOCK_SIZE - 1);
373 ppc_decrypt_xts(walk.dst.virt.addr, walk.src.virt.addr,
374 ctx->key_dec, ctx->rounds, nbytes, walk.iv, twk);
378 err = blkcipher_walk_done(desc, &walk, ubytes);
385 * Algorithm definitions. Disabling alignment (cra_alignmask=0) was chosen
386 * because the e500 platform can handle unaligned reads/writes very efficently.
387 * This improves IPsec thoughput by another few percent. Additionally we assume
388 * that AES context is always aligned to at least 8 bytes because it is created
389 * with kmalloc() in the crypto infrastructure
392 static struct crypto_alg aes_algs[] = { {
394 .cra_driver_name = "aes-ppc-spe",
396 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
397 .cra_blocksize = AES_BLOCK_SIZE,
398 .cra_ctxsize = sizeof(struct ppc_aes_ctx),
400 .cra_module = THIS_MODULE,
403 .cia_min_keysize = AES_MIN_KEY_SIZE,
404 .cia_max_keysize = AES_MAX_KEY_SIZE,
405 .cia_setkey = ppc_aes_setkey,
406 .cia_encrypt = ppc_aes_encrypt,
407 .cia_decrypt = ppc_aes_decrypt
411 .cra_name = "ecb(aes)",
412 .cra_driver_name = "ecb-ppc-spe",
414 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
415 .cra_blocksize = AES_BLOCK_SIZE,
416 .cra_ctxsize = sizeof(struct ppc_aes_ctx),
418 .cra_type = &crypto_blkcipher_type,
419 .cra_module = THIS_MODULE,
422 .min_keysize = AES_MIN_KEY_SIZE,
423 .max_keysize = AES_MAX_KEY_SIZE,
424 .ivsize = AES_BLOCK_SIZE,
425 .setkey = ppc_aes_setkey,
426 .encrypt = ppc_ecb_encrypt,
427 .decrypt = ppc_ecb_decrypt,
431 .cra_name = "cbc(aes)",
432 .cra_driver_name = "cbc-ppc-spe",
434 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
435 .cra_blocksize = AES_BLOCK_SIZE,
436 .cra_ctxsize = sizeof(struct ppc_aes_ctx),
438 .cra_type = &crypto_blkcipher_type,
439 .cra_module = THIS_MODULE,
442 .min_keysize = AES_MIN_KEY_SIZE,
443 .max_keysize = AES_MAX_KEY_SIZE,
444 .ivsize = AES_BLOCK_SIZE,
445 .setkey = ppc_aes_setkey,
446 .encrypt = ppc_cbc_encrypt,
447 .decrypt = ppc_cbc_decrypt,
451 .cra_name = "ctr(aes)",
452 .cra_driver_name = "ctr-ppc-spe",
454 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
456 .cra_ctxsize = sizeof(struct ppc_aes_ctx),
458 .cra_type = &crypto_blkcipher_type,
459 .cra_module = THIS_MODULE,
462 .min_keysize = AES_MIN_KEY_SIZE,
463 .max_keysize = AES_MAX_KEY_SIZE,
464 .ivsize = AES_BLOCK_SIZE,
465 .setkey = ppc_aes_setkey,
466 .encrypt = ppc_ctr_crypt,
467 .decrypt = ppc_ctr_crypt,
471 .cra_name = "xts(aes)",
472 .cra_driver_name = "xts-ppc-spe",
474 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
475 .cra_blocksize = AES_BLOCK_SIZE,
476 .cra_ctxsize = sizeof(struct ppc_xts_ctx),
478 .cra_type = &crypto_blkcipher_type,
479 .cra_module = THIS_MODULE,
482 .min_keysize = AES_MIN_KEY_SIZE * 2,
483 .max_keysize = AES_MAX_KEY_SIZE * 2,
484 .ivsize = AES_BLOCK_SIZE,
485 .setkey = ppc_xts_setkey,
486 .encrypt = ppc_xts_encrypt,
487 .decrypt = ppc_xts_decrypt,
492 static int __init ppc_aes_mod_init(void)
494 return crypto_register_algs(aes_algs, ARRAY_SIZE(aes_algs));
497 static void __exit ppc_aes_mod_fini(void)
499 crypto_unregister_algs(aes_algs, ARRAY_SIZE(aes_algs));
502 module_init(ppc_aes_mod_init);
503 module_exit(ppc_aes_mod_fini);
505 MODULE_LICENSE("GPL");
506 MODULE_DESCRIPTION("AES-ECB/CBC/CTR/XTS, SPE optimized");
508 MODULE_ALIAS_CRYPTO("aes");
509 MODULE_ALIAS_CRYPTO("ecb(aes)");
510 MODULE_ALIAS_CRYPTO("cbc(aes)");
511 MODULE_ALIAS_CRYPTO("ctr(aes)");
512 MODULE_ALIAS_CRYPTO("xts(aes)");
513 MODULE_ALIAS_CRYPTO("aes-ppc-spe");