xref: /linux/arch/powerpc/crypto/aes.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
1*10930333SDanny Tsen // SPDX-License-Identifier: GPL-2.0-only
2*10930333SDanny Tsen /*
3*10930333SDanny Tsen  * AES routines supporting VMX instructions on the Power 8
4*10930333SDanny Tsen  *
5*10930333SDanny Tsen  * Copyright (C) 2015 International Business Machines Inc.
6*10930333SDanny Tsen  *
7*10930333SDanny Tsen  * Author: Marcelo Henrique Cerri <mhcerri@br.ibm.com>
8*10930333SDanny Tsen  */
9*10930333SDanny Tsen 
10*10930333SDanny Tsen #include <linux/types.h>
11*10930333SDanny Tsen #include <linux/err.h>
12*10930333SDanny Tsen #include <linux/crypto.h>
13*10930333SDanny Tsen #include <linux/delay.h>
14*10930333SDanny Tsen #include <asm/simd.h>
15*10930333SDanny Tsen #include <asm/switch_to.h>
16*10930333SDanny Tsen #include <crypto/aes.h>
17*10930333SDanny Tsen #include <crypto/internal/cipher.h>
18*10930333SDanny Tsen #include <crypto/internal/simd.h>
19*10930333SDanny Tsen 
20*10930333SDanny Tsen #include "aesp8-ppc.h"
21*10930333SDanny Tsen 
22*10930333SDanny Tsen struct p8_aes_ctx {
23*10930333SDanny Tsen 	struct crypto_cipher *fallback;
24*10930333SDanny Tsen 	struct aes_key enc_key;
25*10930333SDanny Tsen 	struct aes_key dec_key;
26*10930333SDanny Tsen };
27*10930333SDanny Tsen 
p8_aes_init(struct crypto_tfm * tfm)28*10930333SDanny Tsen static int p8_aes_init(struct crypto_tfm *tfm)
29*10930333SDanny Tsen {
30*10930333SDanny Tsen 	const char *alg = crypto_tfm_alg_name(tfm);
31*10930333SDanny Tsen 	struct crypto_cipher *fallback;
32*10930333SDanny Tsen 	struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
33*10930333SDanny Tsen 
34*10930333SDanny Tsen 	fallback = crypto_alloc_cipher(alg, 0, CRYPTO_ALG_NEED_FALLBACK);
35*10930333SDanny Tsen 	if (IS_ERR(fallback)) {
36*10930333SDanny Tsen 		printk(KERN_ERR
37*10930333SDanny Tsen 		       "Failed to allocate transformation for '%s': %ld\n",
38*10930333SDanny Tsen 		       alg, PTR_ERR(fallback));
39*10930333SDanny Tsen 		return PTR_ERR(fallback);
40*10930333SDanny Tsen 	}
41*10930333SDanny Tsen 
42*10930333SDanny Tsen 	crypto_cipher_set_flags(fallback,
43*10930333SDanny Tsen 				crypto_cipher_get_flags((struct
44*10930333SDanny Tsen 							 crypto_cipher *)
45*10930333SDanny Tsen 							tfm));
46*10930333SDanny Tsen 	ctx->fallback = fallback;
47*10930333SDanny Tsen 
48*10930333SDanny Tsen 	return 0;
49*10930333SDanny Tsen }
50*10930333SDanny Tsen 
p8_aes_exit(struct crypto_tfm * tfm)51*10930333SDanny Tsen static void p8_aes_exit(struct crypto_tfm *tfm)
52*10930333SDanny Tsen {
53*10930333SDanny Tsen 	struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
54*10930333SDanny Tsen 
55*10930333SDanny Tsen 	if (ctx->fallback) {
56*10930333SDanny Tsen 		crypto_free_cipher(ctx->fallback);
57*10930333SDanny Tsen 		ctx->fallback = NULL;
58*10930333SDanny Tsen 	}
59*10930333SDanny Tsen }
60*10930333SDanny Tsen 
p8_aes_setkey(struct crypto_tfm * tfm,const u8 * key,unsigned int keylen)61*10930333SDanny Tsen static int p8_aes_setkey(struct crypto_tfm *tfm, const u8 *key,
62*10930333SDanny Tsen 			 unsigned int keylen)
63*10930333SDanny Tsen {
64*10930333SDanny Tsen 	int ret;
65*10930333SDanny Tsen 	struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
66*10930333SDanny Tsen 
67*10930333SDanny Tsen 	preempt_disable();
68*10930333SDanny Tsen 	pagefault_disable();
69*10930333SDanny Tsen 	enable_kernel_vsx();
70*10930333SDanny Tsen 	ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key);
71*10930333SDanny Tsen 	ret |= aes_p8_set_decrypt_key(key, keylen * 8, &ctx->dec_key);
72*10930333SDanny Tsen 	disable_kernel_vsx();
73*10930333SDanny Tsen 	pagefault_enable();
74*10930333SDanny Tsen 	preempt_enable();
75*10930333SDanny Tsen 
76*10930333SDanny Tsen 	ret |= crypto_cipher_setkey(ctx->fallback, key, keylen);
77*10930333SDanny Tsen 
78*10930333SDanny Tsen 	return ret ? -EINVAL : 0;
79*10930333SDanny Tsen }
80*10930333SDanny Tsen 
p8_aes_encrypt(struct crypto_tfm * tfm,u8 * dst,const u8 * src)81*10930333SDanny Tsen static void p8_aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
82*10930333SDanny Tsen {
83*10930333SDanny Tsen 	struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
84*10930333SDanny Tsen 
85*10930333SDanny Tsen 	if (!crypto_simd_usable()) {
86*10930333SDanny Tsen 		crypto_cipher_encrypt_one(ctx->fallback, dst, src);
87*10930333SDanny Tsen 	} else {
88*10930333SDanny Tsen 		preempt_disable();
89*10930333SDanny Tsen 		pagefault_disable();
90*10930333SDanny Tsen 		enable_kernel_vsx();
91*10930333SDanny Tsen 		aes_p8_encrypt(src, dst, &ctx->enc_key);
92*10930333SDanny Tsen 		disable_kernel_vsx();
93*10930333SDanny Tsen 		pagefault_enable();
94*10930333SDanny Tsen 		preempt_enable();
95*10930333SDanny Tsen 	}
96*10930333SDanny Tsen }
97*10930333SDanny Tsen 
p8_aes_decrypt(struct crypto_tfm * tfm,u8 * dst,const u8 * src)98*10930333SDanny Tsen static void p8_aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
99*10930333SDanny Tsen {
100*10930333SDanny Tsen 	struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
101*10930333SDanny Tsen 
102*10930333SDanny Tsen 	if (!crypto_simd_usable()) {
103*10930333SDanny Tsen 		crypto_cipher_decrypt_one(ctx->fallback, dst, src);
104*10930333SDanny Tsen 	} else {
105*10930333SDanny Tsen 		preempt_disable();
106*10930333SDanny Tsen 		pagefault_disable();
107*10930333SDanny Tsen 		enable_kernel_vsx();
108*10930333SDanny Tsen 		aes_p8_decrypt(src, dst, &ctx->dec_key);
109*10930333SDanny Tsen 		disable_kernel_vsx();
110*10930333SDanny Tsen 		pagefault_enable();
111*10930333SDanny Tsen 		preempt_enable();
112*10930333SDanny Tsen 	}
113*10930333SDanny Tsen }
114*10930333SDanny Tsen 
115*10930333SDanny Tsen struct crypto_alg p8_aes_alg = {
116*10930333SDanny Tsen 	.cra_name = "aes",
117*10930333SDanny Tsen 	.cra_driver_name = "p8_aes",
118*10930333SDanny Tsen 	.cra_module = THIS_MODULE,
119*10930333SDanny Tsen 	.cra_priority = 1000,
120*10930333SDanny Tsen 	.cra_type = NULL,
121*10930333SDanny Tsen 	.cra_flags = CRYPTO_ALG_TYPE_CIPHER | CRYPTO_ALG_NEED_FALLBACK,
122*10930333SDanny Tsen 	.cra_alignmask = 0,
123*10930333SDanny Tsen 	.cra_blocksize = AES_BLOCK_SIZE,
124*10930333SDanny Tsen 	.cra_ctxsize = sizeof(struct p8_aes_ctx),
125*10930333SDanny Tsen 	.cra_init = p8_aes_init,
126*10930333SDanny Tsen 	.cra_exit = p8_aes_exit,
127*10930333SDanny Tsen 	.cra_cipher = {
128*10930333SDanny Tsen 		       .cia_min_keysize = AES_MIN_KEY_SIZE,
129*10930333SDanny Tsen 		       .cia_max_keysize = AES_MAX_KEY_SIZE,
130*10930333SDanny Tsen 		       .cia_setkey = p8_aes_setkey,
131*10930333SDanny Tsen 		       .cia_encrypt = p8_aes_encrypt,
132*10930333SDanny Tsen 		       .cia_decrypt = p8_aes_decrypt,
133*10930333SDanny Tsen 	},
134*10930333SDanny Tsen };
135