Unverified Commit 56197cf0 authored by openeuler-ci-bot's avatar openeuler-ci-bot Committed by Gitee
Browse files

!15078 [OLK-6.6] crypto: zhaoxin - Optimize the assembly code in the SM3 driver

Merge Pull Request from: @leoliu-oc 
 
1. crypto: zhaoxin - Optimize the assembly code in the SM3 driver

Remove register saving/restore operations, mark in as in - out operand,
add blksz and ctrl as inputs, and include "memory" clobber to handle
potential memory access.

### Issue
https://gitee.com/openeuler/kernel/issues/IBMCWP

### Change Default config
N/A

### Test
Pass. 
 
Link:https://gitee.com/openeuler/kernel/pulls/15078

 

Reviewed-by: default avatarZhang Peng <zhangpeng362@huawei.com>
Signed-off-by: default avatarZhang Peng <zhangpeng362@huawei.com>
parents 254b4334 bfe6aa2b
Loading
Loading
Loading
Loading
+29 −72
Original line number Diff line number Diff line
@@ -22,15 +22,6 @@
#include <linux/cpufeature.h>
#include <linux/processor.h>


const u8 zx_sm3_zero_message_hash[SM3_DIGEST_SIZE] = {
	0x1A, 0xB2, 0x1D, 0x83, 0x55, 0xCF, 0xA1, 0x7F,
	0x8e, 0x61, 0x19, 0x48, 0x31, 0xE8, 0x1A, 0x8F,
	0x22, 0xBE, 0xC8, 0xC7, 0x28, 0xFE, 0xFB, 0x74,
	0x7E, 0xD0, 0x35, 0xEB, 0x50, 0x82, 0xAA, 0x2B
};
EXPORT_SYMBOL_GPL(zx_sm3_zero_message_hash);

/*
 * Load supported features of the CPU to see if the SM3/SM4 is available.
 */
@@ -39,13 +30,9 @@ static int gmi_available(void)
	struct cpuinfo_x86 *c = &cpu_data(0);
	u32 eax, edx;

	if (((c->x86 == 6) && (c->x86_model >= 0x0f))
		|| ((c->x86 == 6) && (c->x86_model == 0x09))
		|| (c->x86 > 6)) {

		if (!boot_cpu_has(X86_FEATURE_CCS) ||
			!boot_cpu_has(X86_FEATURE_CCS_EN)) {

	if (((c->x86 == 6) && (c->x86_model >= 0x0f)) ||
	    ((c->x86 == 6) && (c->x86_model == 0x09)) || (c->x86 > 6)) {
		if (!boot_cpu_has(X86_FEATURE_CCS) || !boot_cpu_has(X86_FEATURE_CCS_EN)) {
			eax = 0xC0000001;
			__asm__ __volatile__("cpuid" : "=d"(edx) : "a"(eax) :);

@@ -61,49 +48,24 @@ static int gmi_available(void)
	return -ENODEV;
}

void sm3_generic_block_fn(struct sm3_state *sst, const u8 *inp, int blockcnt)
static void sm3_generic_block_fn(struct sm3_state *sst, const u8 *inp, int blockcnt)
{
	unsigned long in, out, cnt;
	unsigned long cnt, blksz, ctrl;
	unsigned char *in, *out;

	if (!blockcnt)
		return;

	in  = (unsigned long)inp;
	out = (unsigned long)(sst->state);
	in = (unsigned char *)inp;
	out = (unsigned char *)(sst->state);
	cnt = (unsigned long)blockcnt;
	blksz = 0x20;
	ctrl = -1;

	__asm__ __volatile__(
		#ifdef __x86_64__
			"pushq %%rbp\n"
			"pushq %%rbx\n"
			"pushq %%rsi\n"
			"pushq %%rdi\n"
			"movq $-1, %%rax\n"
			"movq $0x20, %%rbx\n"
      #else
			"pushl %%ebp\n"
			"pushl %%ebx\n"
			"pushl %%esi\n"
			"pushl %%edi\n"
			"movl $-1, %%eax\n"
			"movl $0x20, %%ebx\n"
		#endif
		".byte 0xf3,0x0f,0xa6,0xe8\n"
		#ifdef __x86_64__
			"popq %%rdi\n"
			"popq %%rsi\n"
			"popq %%rbx\n"
			"popq %%rbp\n"
      #else
			"popl %%edi\n"
			"popl %%esi\n"
			"popl %%ebx\n"
			"popl %%ebp\n"
		#endif
		:
		: "S"(in), "D"(out), "c"(cnt)
		:
	);
	__asm__ __volatile__(".byte 0xf3, 0x0f, 0xa6, 0xe8\n"
			     : "+S"(in)
			     : "S"(in), "D"(out), "c"(cnt), "b"(blksz), "a"(ctrl)
			     : "memory");
}

static inline int zx_sm3_init(struct shash_desc *desc)
@@ -132,20 +94,17 @@ static inline int zx_sm3_init(struct shash_desc *desc)
static inline int zx_sm3_base_finish(struct shash_desc *desc, u8 *out)
{
	struct sm3_state *sctx = shash_desc_ctx(desc);
	__be32 *digest = (__be32 *)out;

	memcpy(digest, sctx->state, SM3_DIGEST_SIZE);
	memcpy(out, sctx->state, SM3_DIGEST_SIZE);
	memzero_explicit(sctx, sizeof(*sctx));

	*sctx = (struct sm3_state){};
	return 0;
}

int zx_sm3_update(struct shash_desc *desc, const u8 *data,
		unsigned int len)
static int zx_sm3_update(struct shash_desc *desc, const u8 *data, unsigned int len)
{
	return sm3_base_do_update(desc, data, len, sm3_generic_block_fn);
}
EXPORT_SYMBOL(zx_sm3_update);

static int zx_sm3_final(struct shash_desc *desc, u8 *out)
{
@@ -154,14 +113,12 @@ static int zx_sm3_final(struct shash_desc *desc, u8 *out)
	return zx_sm3_base_finish(desc, out);
}

int zx_sm3_finup(struct shash_desc *desc, const u8 *data,
	unsigned int len, u8 *hash)
static int zx_sm3_finup(struct shash_desc *desc, const u8 *data, unsigned int len, u8 *hash)
{
	sm3_base_do_update(desc, data, len, sm3_generic_block_fn);

	return zx_sm3_final(desc, hash);
}
EXPORT_SYMBOL(zx_sm3_finup);

static struct shash_alg zx_sm3_alg = {
	.digestsize = SM3_DIGEST_SIZE,