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