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 CBC routines supporting the Power 7+ Nest Accelerators driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2011-2012 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: Kent Yoder <yoder1@us.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 <crypto/aes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <crypto/algapi.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/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <asm/vio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "nx_csbcpb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "nx.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static int cbc_aes_nx_set_key(struct crypto_skcipher *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 			      const u8               *in_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 			      unsigned int            key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	nx_ctx_init(nx_ctx, HCOP_FC_AES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	switch (key_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	case AES_KEYSIZE_128:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	case AES_KEYSIZE_192:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	case AES_KEYSIZE_256:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		return -EINVAL;
^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) 	csbcpb->cpb.hdr.mode = NX_MODE_AES_CBC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	memcpy(csbcpb->cpb.aes_cbc.key, in_key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static int cbc_aes_nx_crypt(struct skcipher_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 			    int                      enc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	unsigned long irq_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	unsigned int processed = 0, to_process;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	spin_lock_irqsave(&nx_ctx->lock, irq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	if (enc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		to_process = req->cryptlen - processed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		rc = nx_build_sg_lists(nx_ctx, req->iv, req->dst, req->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 				       &to_process, processed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 				       csbcpb->cpb.aes_cbc.iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		memcpy(req->iv, csbcpb->cpb.aes_cbc.cv, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		atomic_inc(&(nx_ctx->stats->aes_ops));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		atomic64_add(csbcpb->csb.processed_byte_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			     &(nx_ctx->stats->aes_bytes));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		processed += to_process;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	} while (processed < req->cryptlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static int cbc_aes_nx_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	return cbc_aes_nx_crypt(req, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static int cbc_aes_nx_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	return cbc_aes_nx_crypt(req, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct skcipher_alg nx_cbc_aes_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	.base.cra_name		= "cbc(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	.base.cra_driver_name	= "cbc-aes-nx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	.base.cra_priority	= 300,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	.base.cra_blocksize	= AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	.base.cra_ctxsize	= sizeof(struct nx_crypto_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	.base.cra_alignmask	= 0xf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	.base.cra_module	= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	.init			= nx_crypto_ctx_aes_cbc_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	.exit			= nx_crypto_ctx_skcipher_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	.min_keysize		= AES_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	.max_keysize		= AES_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	.ivsize			= AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	.setkey			= cbc_aes_nx_set_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	.encrypt		= cbc_aes_nx_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	.decrypt		= cbc_aes_nx_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) };