1
0
Fork 0

crypto: sm2 - remove unnecessary reset operations

This is an algorithm optimization. The reset operation when
setting the public key is repeated and redundant, so remove it.
At the same time, `sm2_ecc_os2ec()` is optimized to make the
function more simpler and more in line with the Linux code style.

Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
zero-sugar-mainline-defconfig
Tianjia Zhang 2020-10-15 17:24:41 +08:00 committed by Herbert Xu
parent ad0bb4e4d2
commit 1bc608b465
1 changed files with 29 additions and 46 deletions

View File

@ -119,12 +119,6 @@ static void sm2_ec_ctx_deinit(struct mpi_ec_ctx *ec)
memset(ec, 0, sizeof(*ec)); memset(ec, 0, sizeof(*ec));
} }
static int sm2_ec_ctx_reset(struct mpi_ec_ctx *ec)
{
sm2_ec_ctx_deinit(ec);
return sm2_ec_ctx_init(ec);
}
/* RESULT must have been initialized and is set on success to the /* RESULT must have been initialized and is set on success to the
* point given by VALUE. * point given by VALUE.
*/ */
@ -132,55 +126,48 @@ static int sm2_ecc_os2ec(MPI_POINT result, MPI value)
{ {
int rc; int rc;
size_t n; size_t n;
const unsigned char *buf; unsigned char *buf;
unsigned char *buf_memory;
MPI x, y; MPI x, y;
n = (mpi_get_nbits(value)+7)/8; n = MPI_NBYTES(value);
buf_memory = kmalloc(n, GFP_KERNEL); buf = kmalloc(n, GFP_KERNEL);
rc = mpi_print(GCRYMPI_FMT_USG, buf_memory, n, &n, value); if (!buf)
if (rc) { return -ENOMEM;
kfree(buf_memory);
return rc;
}
buf = buf_memory;
if (n < 1) { rc = mpi_print(GCRYMPI_FMT_USG, buf, n, &n, value);
kfree(buf_memory); if (rc)
return -EINVAL; goto err_freebuf;
}
if (*buf != 4) { rc = -EINVAL;
kfree(buf_memory); if (n < 1 || ((n - 1) % 2))
return -EINVAL; /* No support for point compression. */ goto err_freebuf;
} /* No support for point compression */
if (((n-1)%2)) { if (*buf != 0x4)
kfree(buf_memory); goto err_freebuf;
return -EINVAL;
} rc = -ENOMEM;
n = (n-1)/2; n = (n - 1) / 2;
x = mpi_read_raw_data(buf + 1, n); x = mpi_read_raw_data(buf + 1, n);
if (!x) { if (!x)
kfree(buf_memory); goto err_freebuf;
return -ENOMEM;
}
y = mpi_read_raw_data(buf + 1 + n, n); y = mpi_read_raw_data(buf + 1 + n, n);
kfree(buf_memory); if (!y)
if (!y) { goto err_freex;
mpi_free(x);
return -ENOMEM;
}
mpi_normalize(x); mpi_normalize(x);
mpi_normalize(y); mpi_normalize(y);
mpi_set(result->x, x); mpi_set(result->x, x);
mpi_set(result->y, y); mpi_set(result->y, y);
mpi_set_ui(result->z, 1); mpi_set_ui(result->z, 1);
mpi_free(x); rc = 0;
mpi_free(y);
return 0; mpi_free(y);
err_freex:
mpi_free(x);
err_freebuf:
kfree(buf);
return rc;
} }
struct sm2_signature_ctx { struct sm2_signature_ctx {
@ -399,10 +386,6 @@ static int sm2_set_pub_key(struct crypto_akcipher *tfm,
MPI a; MPI a;
int rc; int rc;
rc = sm2_ec_ctx_reset(ec);
if (rc)
return rc;
ec->Q = mpi_point_new(0); ec->Q = mpi_point_new(0);
if (!ec->Q) if (!ec->Q)
return -ENOMEM; return -ENOMEM;