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 CTR 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/ctr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.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/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <asm/vio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "nx_csbcpb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "nx.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static int ctr_aes_nx_set_key(struct crypto_skcipher *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 			      const u8               *in_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 			      unsigned int            key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	nx_ctx_init(nx_ctx, HCOP_FC_AES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	switch (key_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	case AES_KEYSIZE_128:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	case AES_KEYSIZE_192:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	case AES_KEYSIZE_256:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	csbcpb->cpb.hdr.mode = NX_MODE_AES_CTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	memcpy(csbcpb->cpb.aes_ctr.key, in_key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	return 0;
^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) static int ctr3686_aes_nx_set_key(struct crypto_skcipher *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 				  const u8               *in_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 				  unsigned int            key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	if (key_len < CTR_RFC3686_NONCE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	memcpy(nx_ctx->priv.ctr.nonce,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	       in_key + key_len - CTR_RFC3686_NONCE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	       CTR_RFC3686_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	key_len -= CTR_RFC3686_NONCE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	return ctr_aes_nx_set_key(tfm, in_key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static int ctr_aes_nx_crypt(struct skcipher_request *req, u8 *iv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	unsigned long irq_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	unsigned int processed = 0, to_process;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	spin_lock_irqsave(&nx_ctx->lock, irq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		to_process = req->cryptlen - processed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		rc = nx_build_sg_lists(nx_ctx, iv, req->dst, req->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 				       &to_process, processed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 				       csbcpb->cpb.aes_ctr.iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			goto out;
^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) 		rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		memcpy(iv, csbcpb->cpb.aes_cbc.cv, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		atomic_inc(&(nx_ctx->stats->aes_ops));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		atomic64_add(csbcpb->csb.processed_byte_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			     &(nx_ctx->stats->aes_bytes));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		processed += to_process;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	} while (processed < req->cryptlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	return rc;
^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) static int ctr3686_aes_nx_crypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	u8 iv[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	memcpy(iv, nx_ctx->priv.ctr.nonce, CTR_RFC3686_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	memcpy(iv + CTR_RFC3686_NONCE_SIZE, req->iv, CTR_RFC3686_IV_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	iv[12] = iv[13] = iv[14] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	iv[15] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	return ctr_aes_nx_crypt(req, iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct skcipher_alg nx_ctr3686_aes_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	.base.cra_name		= "rfc3686(ctr(aes))",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	.base.cra_driver_name	= "rfc3686-ctr-aes-nx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	.base.cra_priority	= 300,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	.base.cra_blocksize	= 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	.base.cra_ctxsize	= sizeof(struct nx_crypto_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	.base.cra_module	= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	.init			= nx_crypto_ctx_aes_ctr_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	.exit			= nx_crypto_ctx_skcipher_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	.min_keysize		= AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	.max_keysize		= AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	.ivsize			= CTR_RFC3686_IV_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	.setkey			= ctr3686_aes_nx_set_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	.encrypt		= ctr3686_aes_nx_crypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	.decrypt		= ctr3686_aes_nx_crypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	.chunksize		= AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) };