All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] digsig and mpi cleanups
@ 2013-01-30  9:30 Dmitry Kasatkin
  2013-01-30  9:30 ` [PATCH 1/2] digsig: remove unnecessary memory allocation and copying Dmitry Kasatkin
  2013-01-30  9:30 ` [PATCH 2/2] mpilib: use DIV_ROUND_UP and remove unused macros Dmitry Kasatkin
  0 siblings, 2 replies; 5+ messages in thread
From: Dmitry Kasatkin @ 2013-01-30  9:30 UTC (permalink / raw)
  To: linux-security-module, linux-crypto; +Cc: jmorris

Here is couple of cleanups for digsig and libmpi.

- Dmitry

Andy Shevchenko (1):
  mpilib: use DIV_ROUND_UP and remove unused macros

Dmitry Kasatkin (1):
  digsig: remove unnecessary memory allocation and copying

 lib/digsig.c           |   41 ++++++++++++++---------------------------
 lib/mpi/mpi-internal.h |    4 ----
 lib/mpi/mpicoder.c     |    8 ++++----
 3 files changed, 18 insertions(+), 35 deletions(-)

-- 
1.7.10.4

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] digsig: remove unnecessary memory allocation and copying
  2013-01-30  9:30 [PATCH 0/2] digsig and mpi cleanups Dmitry Kasatkin
@ 2013-01-30  9:30 ` Dmitry Kasatkin
  2013-02-01  5:31   ` James Morris
  2013-01-30  9:30 ` [PATCH 2/2] mpilib: use DIV_ROUND_UP and remove unused macros Dmitry Kasatkin
  1 sibling, 1 reply; 5+ messages in thread
From: Dmitry Kasatkin @ 2013-01-30  9:30 UTC (permalink / raw)
  To: linux-security-module, linux-crypto; +Cc: jmorris

In existing use case, copying of the decoded data is unnecessary in
pkcs_1_v1_5_decode_emsa. It is just enough to get pointer to the message.
Removing copying and extra buffer allocation.

Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@intel.com>
---
 lib/digsig.c |   41 ++++++++++++++---------------------------
 1 file changed, 14 insertions(+), 27 deletions(-)

diff --git a/lib/digsig.c b/lib/digsig.c
index dc2be7e..2f31e6a 100644
--- a/lib/digsig.c
+++ b/lib/digsig.c
@@ -30,11 +30,10 @@
 
 static struct crypto_shash *shash;
 
-static int pkcs_1_v1_5_decode_emsa(const unsigned char *msg,
-			unsigned long  msglen,
-			unsigned long  modulus_bitlen,
-			unsigned char *out,
-			unsigned long *outlen)
+static const char *pkcs_1_v1_5_decode_emsa(const unsigned char *msg,
+						unsigned long  msglen,
+						unsigned long  modulus_bitlen,
+						unsigned long *outlen)
 {
 	unsigned long modulus_len, ps_len, i;
 
@@ -42,11 +41,11 @@ static int pkcs_1_v1_5_decode_emsa(const unsigned char *msg,
 
 	/* test message size */
 	if ((msglen > modulus_len) || (modulus_len < 11))
-		return -EINVAL;
+		return NULL;
 
 	/* separate encoded message */
-	if ((msg[0] != 0x00) || (msg[1] != (unsigned char)1))
-		return -EINVAL;
+	if (msg[0] != 0x00 || msg[1] != 0x01)
+		return NULL;
 
 	for (i = 2; i < modulus_len - 1; i++)
 		if (msg[i] != 0xFF)
@@ -56,19 +55,13 @@ static int pkcs_1_v1_5_decode_emsa(const unsigned char *msg,
 	if (msg[i] != 0)
 		/* There was no octet with hexadecimal value 0x00
 		to separate ps from m. */
-		return -EINVAL;
+		return NULL;
 
 	ps_len = i - 2;
 
-	if (*outlen < (msglen - (2 + ps_len + 1))) {
-		*outlen = msglen - (2 + ps_len + 1);
-		return -EOVERFLOW;
-	}
-
 	*outlen = (msglen - (2 + ps_len + 1));
-	memcpy(out, &msg[2 + ps_len + 1], *outlen);
 
-	return 0;
+	return msg + 2 + ps_len + 1;
 }
 
 /*
@@ -83,7 +76,8 @@ static int digsig_verify_rsa(struct key *key,
 	unsigned long mlen, mblen;
 	unsigned nret, l;
 	int head, i;
-	unsigned char *out1 = NULL, *out2 = NULL;
+	unsigned char *out1 = NULL;
+	const char *m;
 	MPI in = NULL, res = NULL, pkey[2];
 	uint8_t *p, *datap, *endp;
 	struct user_key_payload *ukp;
@@ -120,7 +114,7 @@ static int digsig_verify_rsa(struct key *key,
 	}
 
 	mblen = mpi_get_nbits(pkey[0]);
-	mlen = (mblen + 7)/8;
+	mlen = DIV_ROUND_UP(mblen, 8);
 
 	if (mlen == 0)
 		goto err;
@@ -129,10 +123,6 @@ static int digsig_verify_rsa(struct key *key,
 	if (!out1)
 		goto err;
 
-	out2 = kzalloc(mlen, GFP_KERNEL);
-	if (!out2)
-		goto err;
-
 	nret = siglen;
 	in = mpi_read_from_buffer(sig, &nret);
 	if (!in)
@@ -164,18 +154,15 @@ static int digsig_verify_rsa(struct key *key,
 
 	kfree(p);
 
-	err = pkcs_1_v1_5_decode_emsa(out1, len, mblen, out2, &len);
-	if (err)
-		goto err;
+	m = pkcs_1_v1_5_decode_emsa(out1, len, mblen, &len);
 
-	if (len != hlen || memcmp(out2, h, hlen))
+	if (!m || len != hlen || memcmp(m, h, hlen))
 		err = -EINVAL;
 
 err:
 	mpi_free(in);
 	mpi_free(res);
 	kfree(out1);
-	kfree(out2);
 	while (--i >= 0)
 		mpi_free(pkey[i]);
 err1:
-- 
1.7.10.4


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] mpilib: use DIV_ROUND_UP and remove unused macros
  2013-01-30  9:30 [PATCH 0/2] digsig and mpi cleanups Dmitry Kasatkin
  2013-01-30  9:30 ` [PATCH 1/2] digsig: remove unnecessary memory allocation and copying Dmitry Kasatkin
@ 2013-01-30  9:30 ` Dmitry Kasatkin
  2013-02-01  5:31   ` James Morris
  1 sibling, 1 reply; 5+ messages in thread
From: Dmitry Kasatkin @ 2013-01-30  9:30 UTC (permalink / raw)
  To: linux-security-module, linux-crypto; +Cc: jmorris, Andy Shevchenko

From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Remove MIN, MAX and ABS macros that are duplicates kernel's native
implementation.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 lib/mpi/mpi-internal.h |    4 ----
 lib/mpi/mpicoder.c     |    8 ++++----
 2 files changed, 4 insertions(+), 8 deletions(-)

diff --git a/lib/mpi/mpi-internal.h b/lib/mpi/mpi-internal.h
index 77adcf6..60cf765 100644
--- a/lib/mpi/mpi-internal.h
+++ b/lib/mpi/mpi-internal.h
@@ -65,10 +65,6 @@
 typedef mpi_limb_t *mpi_ptr_t;	/* pointer to a limb */
 typedef int mpi_size_t;		/* (must be a signed type) */
 
-#define ABS(x) (x >= 0 ? x : -x)
-#define MIN(l, o) ((l) < (o) ? (l) : (o))
-#define MAX(h, i) ((h) > (i) ? (h) : (i))
-
 static inline int RESIZE_IF_NEEDED(MPI a, unsigned b)
 {
 	if (a->alloced < b)
diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c
index 3962b7f..5f9c44c 100644
--- a/lib/mpi/mpicoder.c
+++ b/lib/mpi/mpicoder.c
@@ -52,7 +52,7 @@ MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes)
 	else
 		nbits = 0;
 
-	nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
+	nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
 	val = mpi_alloc(nlimbs);
 	if (!val)
 		return NULL;
@@ -96,8 +96,8 @@ MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread)
 	buffer += 2;
 	nread = 2;
 
-	nbytes = (nbits + 7) / 8;
-	nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
+	nbytes = DIV_ROUND_UP(nbits, 8);
+	nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
 	val = mpi_alloc(nlimbs);
 	if (!val)
 		return NULL;
@@ -193,7 +193,7 @@ int mpi_set_buffer(MPI a, const void *xbuffer, unsigned nbytes, int sign)
 	int nlimbs;
 	int i;
 
-	nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
+	nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
 	if (RESIZE_IF_NEEDED(a, nlimbs) < 0)
 		return -ENOMEM;
 	a->sign = sign;
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] digsig: remove unnecessary memory allocation and copying
  2013-01-30  9:30 ` [PATCH 1/2] digsig: remove unnecessary memory allocation and copying Dmitry Kasatkin
@ 2013-02-01  5:31   ` James Morris
  0 siblings, 0 replies; 5+ messages in thread
From: James Morris @ 2013-02-01  5:31 UTC (permalink / raw)
  To: Dmitry Kasatkin; +Cc: linux-security-module, linux-crypto

On Wed, 30 Jan 2013, Dmitry Kasatkin wrote:

> In existing use case, copying of the decoded data is unnecessary in
> pkcs_1_v1_5_decode_emsa. It is just enough to get pointer to the message.
> Removing copying and extra buffer allocation.

Applied.

-- 
James Morris
<jmorris@namei.org>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] mpilib: use DIV_ROUND_UP and remove unused macros
  2013-01-30  9:30 ` [PATCH 2/2] mpilib: use DIV_ROUND_UP and remove unused macros Dmitry Kasatkin
@ 2013-02-01  5:31   ` James Morris
  0 siblings, 0 replies; 5+ messages in thread
From: James Morris @ 2013-02-01  5:31 UTC (permalink / raw)
  To: Dmitry Kasatkin; +Cc: linux-security-module, linux-crypto, Andy Shevchenko

On Wed, 30 Jan 2013, Dmitry Kasatkin wrote:

> From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> 
> Remove MIN, MAX and ABS macros that are duplicates kernel's native
> implementation.
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Applied.

As maintainer, you should have added your signed-off-by.


-- 
James Morris
<jmorris@namei.org>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-02-01  5:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-01-30  9:30 [PATCH 0/2] digsig and mpi cleanups Dmitry Kasatkin
2013-01-30  9:30 ` [PATCH 1/2] digsig: remove unnecessary memory allocation and copying Dmitry Kasatkin
2013-02-01  5:31   ` James Morris
2013-01-30  9:30 ` [PATCH 2/2] mpilib: use DIV_ROUND_UP and remove unused macros Dmitry Kasatkin
2013-02-01  5:31   ` James Morris

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.