Re: [PATCH 5/6] crypto: testmgr: add ECDSA tests

2017-01-25 Thread Nitin Kumbhar

Hello Stephan,

On 1/20/2017 6:49 PM, Stephan Müller wrote:

Am Freitag, 20. Januar 2017, 17:06:00 CET schrieb Nitin Kumbhar:

Hi Nitin,


Update crypto test manager to include NIST ECDSA
test vectors and various ECDSA tests. These include
tests for ECDSA signing, ECDSA sign-verification,
ECDSA signing and verifying generated signatures and
invalidation of incorrect signatures.

Signed-off-by: Nitin Kumbhar 
---
 crypto/testmgr.c |  452
+- crypto/testmgr.h |
140 +
 2 files changed, 589 insertions(+), 3 deletions(-)

diff --git a/crypto/testmgr.c b/crypto/testmgr.c
index 98eb09782db8..a1db28cbc32d 100644
--- a/crypto/testmgr.c
+++ b/crypto/testmgr.c
@@ -5,6 +5,7 @@
  * Copyright (c) 2002 Jean-Francois Dive 
  * Copyright (c) 2007 Nokia Siemens Networks
  * Copyright (c) 2008 Herbert Xu 
+ * Copyright (c) 2017 NVIDIA Corporation
  *
  * Updated RFC4106 AES-GCM testing.
  *Authors: Aidan O'Mahony (aidan.o.mah...@intel.com)
@@ -2085,6 +2086,436 @@ static int alg_test_kpp(const struct alg_test_desc
*desc, const char *driver, return err;
 }

+static int do_test_ecdsa_verify(struct crypto_akcipher *tfm,
+ struct akcipher_testvec *vec)
+{
+   struct akcipher_request *req = NULL;
+   u8 *r_str = NULL, *s_str = NULL;
+   u8 *m_str = NULL;
+   struct scatterlist src_tab[3], dst;
+   struct tcrypt_result result;
+   unsigned int outbuf_maxlen;
+   u8 *outbuf = NULL;
+   unsigned int nbytes;
+   int err;
+
+   /* Alloc akcipher request */
+   req = akcipher_request_alloc(tfm, GFP_KERNEL);
+   if (!req)
+   return -ENOMEM;
+
+   /* Set private key */
+   err = crypto_akcipher_set_pub_key(tfm, vec->key, vec->key_len);
+   if (err)
+   goto error;
+
+   /*
+* vec->c always contains k, R and S in that order. All are
+* of same size and are equal to n i.e. the order of
+* an elliptic curve.
+*/
+   nbytes = vec->c_size / 3;
+
+   r_str = kzalloc(nbytes, GFP_KERNEL);
+   s_str = kzalloc(nbytes, GFP_KERNEL);
+   m_str = kzalloc(vec->m_size, GFP_KERNEL);
+   if (!r_str || !s_str || !m_str) {
+   err = -ENOMEM;
+   goto error;
+   }
+   memcpy(r_str, (u8 *)vec->c + nbytes, nbytes);
+   memcpy(s_str, (u8 *)vec->c + 2 * nbytes, nbytes);
+   memcpy(m_str, vec->m, vec->m_size);
+
+   outbuf_maxlen = crypto_akcipher_maxsize(tfm);
+   if (outbuf_maxlen < 0) {
+   err = outbuf_maxlen;
+   goto error;
+   }
+   outbuf = kzalloc(outbuf_maxlen, GFP_KERNEL);
+   if (!outbuf) {
+   err = -ENOMEM;
+   goto error;
+   }
+
+   /* Set src and dst buffers */
+   sg_init_table(src_tab, 3);
+   sg_set_buf(_tab[0], m_str, vec->m_size);
+   sg_set_buf(_tab[1], r_str, nbytes);
+   sg_set_buf(_tab[2], s_str, nbytes);
+   sg_init_one(, outbuf, outbuf_maxlen);
+
+   akcipher_request_set_crypt(req, src_tab, ,
+  vec->m_size + 2 * nbytes, outbuf_maxlen);
+
+   /* Set up result callback */
+   init_completion();
+   akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
+ tcrypt_complete, );
+
+   /* Run ecdsa verify operation on sig (r,s) */
+   err = wait_async_op(, crypto_akcipher_verify(req));
+   if (err) {
+   pr_err("alg: ecdsa: verify(rs) test failed. err %d\n", err);
+   goto error;
+   }
+error:
+   akcipher_request_free(req);
+   kfree(r_str);
+   kfree(s_str);
+   kfree(m_str);
+   kfree(outbuf);
+   return err;
+}
+
+static int do_test_ecdsa_invalid_verify(struct crypto_akcipher *tfm,
+   struct akcipher_testvec *vec)
+{
+   struct akcipher_request *req = NULL;
+   u8 *r_str = NULL, *s_str = NULL;
+   u8 *m_str = NULL;
+   struct scatterlist src_tab[3], dst;
+   struct tcrypt_result result;
+   unsigned int outbuf_maxlen;
+   u8 *outbuf = NULL;
+   unsigned int nbytes;
+   int err;
+
+   /* Alloc akcipher request */
+   req = akcipher_request_alloc(tfm, GFP_KERNEL);
+   if (!req)
+   return -ENOMEM;
+
+   /* Set private key */
+   err = crypto_akcipher_set_pub_key(tfm, vec->key, vec->key_len);
+   if (err)
+   goto error;
+
+   /*
+* vec->c always contains k, R and S in that order. All are
+* of same size and are equal to n i.e. the order of
+* an elliptic curve.
+*/
+   nbytes = vec->c_size / 3;
+
+   r_str = kzalloc(nbytes, GFP_KERNEL);
+   s_str = kzalloc(nbytes, GFP_KERNEL);
+   m_str = kzalloc(vec->m_size, GFP_KERNEL);
+   if (!r_str || !s_str || !m_str) {
+   

Re: [PATCH 5/6] crypto: testmgr: add ECDSA tests

2017-01-20 Thread Stephan Müller
Am Freitag, 20. Januar 2017, 17:06:00 CET schrieb Nitin Kumbhar:

Hi Nitin,

> Update crypto test manager to include NIST ECDSA
> test vectors and various ECDSA tests. These include
> tests for ECDSA signing, ECDSA sign-verification,
> ECDSA signing and verifying generated signatures and
> invalidation of incorrect signatures.
> 
> Signed-off-by: Nitin Kumbhar 
> ---
>  crypto/testmgr.c |  452
> +- crypto/testmgr.h | 
> 140 +
>  2 files changed, 589 insertions(+), 3 deletions(-)
> 
> diff --git a/crypto/testmgr.c b/crypto/testmgr.c
> index 98eb09782db8..a1db28cbc32d 100644
> --- a/crypto/testmgr.c
> +++ b/crypto/testmgr.c
> @@ -5,6 +5,7 @@
>   * Copyright (c) 2002 Jean-Francois Dive 
>   * Copyright (c) 2007 Nokia Siemens Networks
>   * Copyright (c) 2008 Herbert Xu 
> + * Copyright (c) 2017 NVIDIA Corporation
>   *
>   * Updated RFC4106 AES-GCM testing.
>   *Authors: Aidan O'Mahony (aidan.o.mah...@intel.com)
> @@ -2085,6 +2086,436 @@ static int alg_test_kpp(const struct alg_test_desc
> *desc, const char *driver, return err;
>  }
> 
> +static int do_test_ecdsa_verify(struct crypto_akcipher *tfm,
> +   struct akcipher_testvec *vec)
> +{
> + struct akcipher_request *req = NULL;
> + u8 *r_str = NULL, *s_str = NULL;
> + u8 *m_str = NULL;
> + struct scatterlist src_tab[3], dst;
> + struct tcrypt_result result;
> + unsigned int outbuf_maxlen;
> + u8 *outbuf = NULL;
> + unsigned int nbytes;
> + int err;
> +
> + /* Alloc akcipher request */
> + req = akcipher_request_alloc(tfm, GFP_KERNEL);
> + if (!req)
> + return -ENOMEM;
> +
> + /* Set private key */
> + err = crypto_akcipher_set_pub_key(tfm, vec->key, vec->key_len);
> + if (err)
> + goto error;
> +
> + /*
> +  * vec->c always contains k, R and S in that order. All are
> +  * of same size and are equal to n i.e. the order of
> +  * an elliptic curve.
> +  */
> + nbytes = vec->c_size / 3;
> +
> + r_str = kzalloc(nbytes, GFP_KERNEL);
> + s_str = kzalloc(nbytes, GFP_KERNEL);
> + m_str = kzalloc(vec->m_size, GFP_KERNEL);
> + if (!r_str || !s_str || !m_str) {
> + err = -ENOMEM;
> + goto error;
> + }
> + memcpy(r_str, (u8 *)vec->c + nbytes, nbytes);
> + memcpy(s_str, (u8 *)vec->c + 2 * nbytes, nbytes);
> + memcpy(m_str, vec->m, vec->m_size);
> +
> + outbuf_maxlen = crypto_akcipher_maxsize(tfm);
> + if (outbuf_maxlen < 0) {
> + err = outbuf_maxlen;
> + goto error;
> + }
> + outbuf = kzalloc(outbuf_maxlen, GFP_KERNEL);
> + if (!outbuf) {
> + err = -ENOMEM;
> + goto error;
> + }
> +
> + /* Set src and dst buffers */
> + sg_init_table(src_tab, 3);
> + sg_set_buf(_tab[0], m_str, vec->m_size);
> + sg_set_buf(_tab[1], r_str, nbytes);
> + sg_set_buf(_tab[2], s_str, nbytes);
> + sg_init_one(, outbuf, outbuf_maxlen);
> +
> + akcipher_request_set_crypt(req, src_tab, ,
> +vec->m_size + 2 * nbytes, outbuf_maxlen);
> +
> + /* Set up result callback */
> + init_completion();
> + akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
> +   tcrypt_complete, );
> +
> + /* Run ecdsa verify operation on sig (r,s) */
> + err = wait_async_op(, crypto_akcipher_verify(req));
> + if (err) {
> + pr_err("alg: ecdsa: verify(rs) test failed. err %d\n", err);
> + goto error;
> + }
> +error:
> + akcipher_request_free(req);
> + kfree(r_str);
> + kfree(s_str);
> + kfree(m_str);
> + kfree(outbuf);
> + return err;
> +}
> +
> +static int do_test_ecdsa_invalid_verify(struct crypto_akcipher *tfm,
> + struct akcipher_testvec *vec)
> +{
> + struct akcipher_request *req = NULL;
> + u8 *r_str = NULL, *s_str = NULL;
> + u8 *m_str = NULL;
> + struct scatterlist src_tab[3], dst;
> + struct tcrypt_result result;
> + unsigned int outbuf_maxlen;
> + u8 *outbuf = NULL;
> + unsigned int nbytes;
> + int err;
> +
> + /* Alloc akcipher request */
> + req = akcipher_request_alloc(tfm, GFP_KERNEL);
> + if (!req)
> + return -ENOMEM;
> +
> + /* Set private key */
> + err = crypto_akcipher_set_pub_key(tfm, vec->key, vec->key_len);
> + if (err)
> + goto error;
> +
> + /*
> +  * vec->c always contains k, R and S in that order. All are
> +  * of same size and are equal to n i.e. the order of
> +  * an elliptic curve.
> +  */
> + nbytes = vec->c_size / 3;
> +
> + r_str = kzalloc(nbytes, GFP_KERNEL);
> + s_str = kzalloc(nbytes, GFP_KERNEL);
> + m_str = kzalloc(vec->m_size, GFP_KERNEL);
> + if (!r_str || 

[PATCH 5/6] crypto: testmgr: add ECDSA tests

2017-01-20 Thread Nitin Kumbhar
Update crypto test manager to include NIST ECDSA
test vectors and various ECDSA tests. These include
tests for ECDSA signing, ECDSA sign-verification,
ECDSA signing and verifying generated signatures and
invalidation of incorrect signatures.

Signed-off-by: Nitin Kumbhar 
---
 crypto/testmgr.c |  452 +-
 crypto/testmgr.h |  140 +
 2 files changed, 589 insertions(+), 3 deletions(-)

diff --git a/crypto/testmgr.c b/crypto/testmgr.c
index 98eb09782db8..a1db28cbc32d 100644
--- a/crypto/testmgr.c
+++ b/crypto/testmgr.c
@@ -5,6 +5,7 @@
  * Copyright (c) 2002 Jean-Francois Dive 
  * Copyright (c) 2007 Nokia Siemens Networks
  * Copyright (c) 2008 Herbert Xu 
+ * Copyright (c) 2017 NVIDIA Corporation
  *
  * Updated RFC4106 AES-GCM testing.
  *Authors: Aidan O'Mahony (aidan.o.mah...@intel.com)
@@ -2085,6 +2086,436 @@ static int alg_test_kpp(const struct alg_test_desc 
*desc, const char *driver,
return err;
 }
 
+static int do_test_ecdsa_verify(struct crypto_akcipher *tfm,
+ struct akcipher_testvec *vec)
+{
+   struct akcipher_request *req = NULL;
+   u8 *r_str = NULL, *s_str = NULL;
+   u8 *m_str = NULL;
+   struct scatterlist src_tab[3], dst;
+   struct tcrypt_result result;
+   unsigned int outbuf_maxlen;
+   u8 *outbuf = NULL;
+   unsigned int nbytes;
+   int err;
+
+   /* Alloc akcipher request */
+   req = akcipher_request_alloc(tfm, GFP_KERNEL);
+   if (!req)
+   return -ENOMEM;
+
+   /* Set private key */
+   err = crypto_akcipher_set_pub_key(tfm, vec->key, vec->key_len);
+   if (err)
+   goto error;
+
+   /*
+* vec->c always contains k, R and S in that order. All are
+* of same size and are equal to n i.e. the order of
+* an elliptic curve.
+*/
+   nbytes = vec->c_size / 3;
+
+   r_str = kzalloc(nbytes, GFP_KERNEL);
+   s_str = kzalloc(nbytes, GFP_KERNEL);
+   m_str = kzalloc(vec->m_size, GFP_KERNEL);
+   if (!r_str || !s_str || !m_str) {
+   err = -ENOMEM;
+   goto error;
+   }
+   memcpy(r_str, (u8 *)vec->c + nbytes, nbytes);
+   memcpy(s_str, (u8 *)vec->c + 2 * nbytes, nbytes);
+   memcpy(m_str, vec->m, vec->m_size);
+
+   outbuf_maxlen = crypto_akcipher_maxsize(tfm);
+   if (outbuf_maxlen < 0) {
+   err = outbuf_maxlen;
+   goto error;
+   }
+   outbuf = kzalloc(outbuf_maxlen, GFP_KERNEL);
+   if (!outbuf) {
+   err = -ENOMEM;
+   goto error;
+   }
+
+   /* Set src and dst buffers */
+   sg_init_table(src_tab, 3);
+   sg_set_buf(_tab[0], m_str, vec->m_size);
+   sg_set_buf(_tab[1], r_str, nbytes);
+   sg_set_buf(_tab[2], s_str, nbytes);
+   sg_init_one(, outbuf, outbuf_maxlen);
+
+   akcipher_request_set_crypt(req, src_tab, ,
+  vec->m_size + 2 * nbytes, outbuf_maxlen);
+
+   /* Set up result callback */
+   init_completion();
+   akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
+ tcrypt_complete, );
+
+   /* Run ecdsa verify operation on sig (r,s) */
+   err = wait_async_op(, crypto_akcipher_verify(req));
+   if (err) {
+   pr_err("alg: ecdsa: verify(rs) test failed. err %d\n", err);
+   goto error;
+   }
+error:
+   akcipher_request_free(req);
+   kfree(r_str);
+   kfree(s_str);
+   kfree(m_str);
+   kfree(outbuf);
+   return err;
+}
+
+static int do_test_ecdsa_invalid_verify(struct crypto_akcipher *tfm,
+   struct akcipher_testvec *vec)
+{
+   struct akcipher_request *req = NULL;
+   u8 *r_str = NULL, *s_str = NULL;
+   u8 *m_str = NULL;
+   struct scatterlist src_tab[3], dst;
+   struct tcrypt_result result;
+   unsigned int outbuf_maxlen;
+   u8 *outbuf = NULL;
+   unsigned int nbytes;
+   int err;
+
+   /* Alloc akcipher request */
+   req = akcipher_request_alloc(tfm, GFP_KERNEL);
+   if (!req)
+   return -ENOMEM;
+
+   /* Set private key */
+   err = crypto_akcipher_set_pub_key(tfm, vec->key, vec->key_len);
+   if (err)
+   goto error;
+
+   /*
+* vec->c always contains k, R and S in that order. All are
+* of same size and are equal to n i.e. the order of
+* an elliptic curve.
+*/
+   nbytes = vec->c_size / 3;
+
+   r_str = kzalloc(nbytes, GFP_KERNEL);
+   s_str = kzalloc(nbytes, GFP_KERNEL);
+   m_str = kzalloc(vec->m_size, GFP_KERNEL);
+   if (!r_str || !s_str || !m_str) {
+   err = -ENOMEM;
+   goto error;
+   }
+   memcpy(r_str, (u8 *)vec->c + 1 * nbytes, nbytes);
+