Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Glue code for AES implementation for SPE instructions (PPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Based on generic implementation. The assembler module takes care
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * about the SPE registers so it can run from interrupt context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Copyright (c) 2015 Markus Stockhausen <stockhausen@collogia.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <crypto/aes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <asm/switch_to.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <crypto/xts.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <crypto/gf128mul.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * MAX_BYTES defines the number of bytes that are allowed to be processed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * between preempt_disable() and preempt_enable(). e500 cores can issue two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * instructions per clock cycle using one 32/64 bit unit (SU1) and one 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * bit unit (SU2). One of these can be a memory access that is executed via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * a single load and store unit (LSU). XTS-AES-256 takes ~780 operations per
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * 16 byte block block or 25 cycles per byte. Thus 768 bytes of input data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * will need an estimated maximum of 20,000 cycles. Headroom for cache misses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * included. Even with the low end model clocked at 667 MHz this equals to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * critical time window of less than 30us. The value has been chosen to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * process a 512 byte disk block in one or a large 1400 bytes IPsec network
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * packet in two runs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define MAX_BYTES 768
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) struct ppc_aes_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	u32 key_enc[AES_MAX_KEYLENGTH_U32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	u32 key_dec[AES_MAX_KEYLENGTH_U32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	u32 rounds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) struct ppc_xts_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	u32 key_enc[AES_MAX_KEYLENGTH_U32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	u32 key_dec[AES_MAX_KEYLENGTH_U32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	u32 key_twk[AES_MAX_KEYLENGTH_U32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	u32 rounds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) extern void ppc_encrypt_aes(u8 *out, const u8 *in, u32 *key_enc, u32 rounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) extern void ppc_decrypt_aes(u8 *out, const u8 *in, u32 *key_dec, u32 rounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) extern void ppc_encrypt_ecb(u8 *out, const u8 *in, u32 *key_enc, u32 rounds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			    u32 bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) extern void ppc_decrypt_ecb(u8 *out, const u8 *in, u32 *key_dec, u32 rounds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 			    u32 bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) extern void ppc_encrypt_cbc(u8 *out, const u8 *in, u32 *key_enc, u32 rounds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 			    u32 bytes, u8 *iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) extern void ppc_decrypt_cbc(u8 *out, const u8 *in, u32 *key_dec, u32 rounds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			    u32 bytes, u8 *iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) extern void ppc_crypt_ctr  (u8 *out, const u8 *in, u32 *key_enc, u32 rounds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 			    u32 bytes, u8 *iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) extern void ppc_encrypt_xts(u8 *out, const u8 *in, u32 *key_enc, u32 rounds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			    u32 bytes, u8 *iv, u32 *key_twk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) extern void ppc_decrypt_xts(u8 *out, const u8 *in, u32 *key_dec, u32 rounds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			    u32 bytes, u8 *iv, u32 *key_twk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) extern void ppc_expand_key_128(u32 *key_enc, const u8 *key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) extern void ppc_expand_key_192(u32 *key_enc, const u8 *key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) extern void ppc_expand_key_256(u32 *key_enc, const u8 *key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) extern void ppc_generate_decrypt_key(u32 *key_dec,u32 *key_enc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 				     unsigned int key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static void spe_begin(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	/* disable preemption and save users SPE registers if required */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	enable_kernel_spe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static void spe_end(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	disable_kernel_spe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	/* reenable preemption */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static int ppc_aes_setkey(struct crypto_tfm *tfm, const u8 *in_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	struct ppc_aes_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	switch (key_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	case AES_KEYSIZE_128:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		ctx->rounds = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		ppc_expand_key_128(ctx->key_enc, in_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	case AES_KEYSIZE_192:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		ctx->rounds = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		ppc_expand_key_192(ctx->key_enc, in_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	case AES_KEYSIZE_256:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		ctx->rounds = 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		ppc_expand_key_256(ctx->key_enc, in_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	ppc_generate_decrypt_key(ctx->key_dec, ctx->key_enc, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int ppc_aes_setkey_skcipher(struct crypto_skcipher *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 				   const u8 *in_key, unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	return ppc_aes_setkey(crypto_skcipher_tfm(tfm), in_key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int ppc_xts_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		   unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	struct ppc_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	err = xts_verify_key(tfm, in_key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	key_len >>= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	switch (key_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	case AES_KEYSIZE_128:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		ctx->rounds = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		ppc_expand_key_128(ctx->key_enc, in_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		ppc_expand_key_128(ctx->key_twk, in_key + AES_KEYSIZE_128);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	case AES_KEYSIZE_192:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		ctx->rounds = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		ppc_expand_key_192(ctx->key_enc, in_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		ppc_expand_key_192(ctx->key_twk, in_key + AES_KEYSIZE_192);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	case AES_KEYSIZE_256:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		ctx->rounds = 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		ppc_expand_key_256(ctx->key_enc, in_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		ppc_expand_key_256(ctx->key_twk, in_key + AES_KEYSIZE_256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	ppc_generate_decrypt_key(ctx->key_dec, ctx->key_enc, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static void ppc_aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	struct ppc_aes_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	spe_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	ppc_encrypt_aes(out, in, ctx->key_enc, ctx->rounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	spe_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static void ppc_aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct ppc_aes_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	spe_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	ppc_decrypt_aes(out, in, ctx->key_dec, ctx->rounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	spe_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static int ppc_ecb_crypt(struct skcipher_request *req, bool enc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	struct ppc_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	struct skcipher_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	unsigned int nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	err = skcipher_walk_virt(&walk, req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	while ((nbytes = walk.nbytes) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		nbytes = min_t(unsigned int, nbytes, MAX_BYTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		nbytes = round_down(nbytes, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		spe_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		if (enc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			ppc_encrypt_ecb(walk.dst.virt.addr, walk.src.virt.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 					ctx->key_enc, ctx->rounds, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			ppc_decrypt_ecb(walk.dst.virt.addr, walk.src.virt.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 					ctx->key_dec, ctx->rounds, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		spe_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static int ppc_ecb_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	return ppc_ecb_crypt(req, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static int ppc_ecb_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return ppc_ecb_crypt(req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static int ppc_cbc_crypt(struct skcipher_request *req, bool enc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	struct ppc_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	struct skcipher_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	unsigned int nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	err = skcipher_walk_virt(&walk, req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	while ((nbytes = walk.nbytes) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		nbytes = min_t(unsigned int, nbytes, MAX_BYTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		nbytes = round_down(nbytes, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		spe_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		if (enc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			ppc_encrypt_cbc(walk.dst.virt.addr, walk.src.virt.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 					ctx->key_enc, ctx->rounds, nbytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 					walk.iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			ppc_decrypt_cbc(walk.dst.virt.addr, walk.src.virt.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 					ctx->key_dec, ctx->rounds, nbytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 					walk.iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		spe_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static int ppc_cbc_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	return ppc_cbc_crypt(req, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static int ppc_cbc_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	return ppc_cbc_crypt(req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static int ppc_ctr_crypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	struct ppc_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	struct skcipher_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	unsigned int nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	err = skcipher_walk_virt(&walk, req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	while ((nbytes = walk.nbytes) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		nbytes = min_t(unsigned int, nbytes, MAX_BYTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		if (nbytes < walk.total)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			nbytes = round_down(nbytes, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		spe_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		ppc_crypt_ctr(walk.dst.virt.addr, walk.src.virt.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			      ctx->key_enc, ctx->rounds, nbytes, walk.iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		spe_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static int ppc_xts_crypt(struct skcipher_request *req, bool enc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	struct ppc_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	struct skcipher_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	unsigned int nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	u32 *twk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	err = skcipher_walk_virt(&walk, req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	twk = ctx->key_twk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	while ((nbytes = walk.nbytes) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		nbytes = min_t(unsigned int, nbytes, MAX_BYTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		nbytes = round_down(nbytes, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		spe_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		if (enc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			ppc_encrypt_xts(walk.dst.virt.addr, walk.src.virt.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 					ctx->key_enc, ctx->rounds, nbytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 					walk.iv, twk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			ppc_decrypt_xts(walk.dst.virt.addr, walk.src.virt.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 					ctx->key_dec, ctx->rounds, nbytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 					walk.iv, twk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		spe_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		twk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static int ppc_xts_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	struct ppc_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	int tail = req->cryptlen % AES_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	int offset = req->cryptlen - tail - AES_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	struct skcipher_request subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	u8 b[2][AES_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (req->cryptlen < AES_BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	if (tail) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		subreq = *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		skcipher_request_set_crypt(&subreq, req->src, req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 					   req->cryptlen - tail, req->iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		req = &subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	err = ppc_xts_crypt(req, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	if (err || !tail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	scatterwalk_map_and_copy(b[0], req->dst, offset, AES_BLOCK_SIZE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	memcpy(b[1], b[0], tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	scatterwalk_map_and_copy(b[0], req->src, offset + AES_BLOCK_SIZE, tail, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	spe_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	ppc_encrypt_xts(b[0], b[0], ctx->key_enc, ctx->rounds, AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			req->iv, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	spe_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	scatterwalk_map_and_copy(b[0], req->dst, offset, AES_BLOCK_SIZE + tail, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) static int ppc_xts_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	struct ppc_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	int tail = req->cryptlen % AES_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	int offset = req->cryptlen - tail - AES_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	struct skcipher_request subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	u8 b[3][AES_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	le128 twk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	if (req->cryptlen < AES_BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	if (tail) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		subreq = *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		skcipher_request_set_crypt(&subreq, req->src, req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 					   offset, req->iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		req = &subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	err = ppc_xts_crypt(req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (err || !tail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	scatterwalk_map_and_copy(b[1], req->src, offset, AES_BLOCK_SIZE + tail, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	spe_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (!offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		ppc_encrypt_ecb(req->iv, req->iv, ctx->key_twk, ctx->rounds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 				AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	gf128mul_x_ble(&twk, (le128 *)req->iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	ppc_decrypt_xts(b[1], b[1], ctx->key_dec, ctx->rounds, AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 			(u8 *)&twk, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	memcpy(b[0], b[2], tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	memcpy(b[0] + tail, b[1] + tail, AES_BLOCK_SIZE - tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	ppc_decrypt_xts(b[0], b[0], ctx->key_dec, ctx->rounds, AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 			req->iv, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	spe_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	scatterwalk_map_and_copy(b[0], req->dst, offset, AES_BLOCK_SIZE + tail, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)  * Algorithm definitions. Disabling alignment (cra_alignmask=0) was chosen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)  * because the e500 platform can handle unaligned reads/writes very efficently.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)  * This improves IPsec thoughput by another few percent. Additionally we assume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)  * that AES context is always aligned to at least 8 bytes because it is created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)  * with kmalloc() in the crypto infrastructure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) static struct crypto_alg aes_cipher_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	.cra_name		=	"aes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	.cra_driver_name	=	"aes-ppc-spe",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	.cra_priority		=	300,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	.cra_flags		=	CRYPTO_ALG_TYPE_CIPHER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	.cra_blocksize		=	AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	.cra_ctxsize		=	sizeof(struct ppc_aes_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	.cra_alignmask		=	0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	.cra_module		=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	.cra_u			=	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		.cipher = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 			.cia_min_keysize	=	AES_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 			.cia_max_keysize	=	AES_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 			.cia_setkey		=	ppc_aes_setkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 			.cia_encrypt		=	ppc_aes_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 			.cia_decrypt		=	ppc_aes_decrypt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) static struct skcipher_alg aes_skcipher_algs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		.base.cra_name		=	"ecb(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		.base.cra_driver_name	=	"ecb-ppc-spe",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		.base.cra_priority	=	300,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		.base.cra_blocksize	=	AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		.base.cra_ctxsize	=	sizeof(struct ppc_aes_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		.base.cra_module	=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		.min_keysize		=	AES_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		.max_keysize		=	AES_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		.setkey			=	ppc_aes_setkey_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		.encrypt		=	ppc_ecb_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		.decrypt		=	ppc_ecb_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		.base.cra_name		=	"cbc(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		.base.cra_driver_name	=	"cbc-ppc-spe",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		.base.cra_priority	=	300,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		.base.cra_blocksize	=	AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		.base.cra_ctxsize	=	sizeof(struct ppc_aes_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		.base.cra_module	=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		.min_keysize		=	AES_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		.max_keysize		=	AES_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		.ivsize			=	AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		.setkey			=	ppc_aes_setkey_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		.encrypt		=	ppc_cbc_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		.decrypt		=	ppc_cbc_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		.base.cra_name		=	"ctr(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		.base.cra_driver_name	=	"ctr-ppc-spe",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		.base.cra_priority	=	300,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		.base.cra_blocksize	=	1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		.base.cra_ctxsize	=	sizeof(struct ppc_aes_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		.base.cra_module	=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		.min_keysize		=	AES_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		.max_keysize		=	AES_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		.ivsize			=	AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		.setkey			=	ppc_aes_setkey_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		.encrypt		=	ppc_ctr_crypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		.decrypt		=	ppc_ctr_crypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		.chunksize		=	AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		.base.cra_name		=	"xts(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		.base.cra_driver_name	=	"xts-ppc-spe",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		.base.cra_priority	=	300,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		.base.cra_blocksize	=	AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		.base.cra_ctxsize	=	sizeof(struct ppc_xts_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		.base.cra_module	=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		.min_keysize		=	AES_MIN_KEY_SIZE * 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		.max_keysize		=	AES_MAX_KEY_SIZE * 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		.ivsize			=	AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		.setkey			=	ppc_xts_setkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		.encrypt		=	ppc_xts_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		.decrypt		=	ppc_xts_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) static int __init ppc_aes_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	err = crypto_register_alg(&aes_cipher_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	err = crypto_register_skciphers(aes_skcipher_algs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 					ARRAY_SIZE(aes_skcipher_algs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		crypto_unregister_alg(&aes_cipher_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) static void __exit ppc_aes_mod_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	crypto_unregister_alg(&aes_cipher_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	crypto_unregister_skciphers(aes_skcipher_algs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 				    ARRAY_SIZE(aes_skcipher_algs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) module_init(ppc_aes_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) module_exit(ppc_aes_mod_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) MODULE_DESCRIPTION("AES-ECB/CBC/CTR/XTS, SPE optimized");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) MODULE_ALIAS_CRYPTO("aes");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) MODULE_ALIAS_CRYPTO("ecb(aes)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) MODULE_ALIAS_CRYPTO("cbc(aes)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) MODULE_ALIAS_CRYPTO("ctr(aes)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) MODULE_ALIAS_CRYPTO("xts(aes)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) MODULE_ALIAS_CRYPTO("aes-ppc-spe");