[PATCH RFC v7 3/3] crypto: add tests vectors for RSA

2015-06-16 Thread Tadeusz Struk
New test vectors for RSA algorithm.

Signed-off-by: Tadeusz Struk 
---
 crypto/Kconfig   |1 
 crypto/testmgr.c |  158 ++
 crypto/testmgr.h |  187 ++
 3 files changed, 346 insertions(+)

diff --git a/crypto/Kconfig b/crypto/Kconfig
index 52467cf..9471ac8 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -122,6 +122,7 @@ config CRYPTO_MANAGER2
select CRYPTO_HASH2
select CRYPTO_BLKCIPHER2
select CRYPTO_PCOMP2
+   select CRYPTO_AKCIPHER2
 
 config CRYPTO_USER
tristate "Userspace cryptographic algorithm configuration"
diff --git a/crypto/testmgr.c b/crypto/testmgr.c
index ccd19cf..975e1ea 100644
--- a/crypto/testmgr.c
+++ b/crypto/testmgr.c
@@ -30,6 +30,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include "internal.h"
 
@@ -116,6 +117,11 @@ struct drbg_test_suite {
unsigned int count;
 };
 
+struct akcipher_test_suite {
+   struct akcipher_testvec *vecs;
+   unsigned int count;
+};
+
 struct alg_test_desc {
const char *alg;
int (*test)(const struct alg_test_desc *desc, const char *driver,
@@ -130,6 +136,7 @@ struct alg_test_desc {
struct hash_test_suite hash;
struct cprng_test_suite cprng;
struct drbg_test_suite drbg;
+   struct akcipher_test_suite akcipher;
} suite;
 };
 
@@ -1825,6 +1832,147 @@ static int alg_test_drbg(const struct alg_test_desc 
*desc, const char *driver,
 
 }
 
+static int do_test_rsa(struct crypto_akcipher *tfm,
+  struct akcipher_testvec *vecs)
+{
+   struct akcipher_request *req;
+   void *outbuf_enc = NULL;
+   void *outbuf_dec = NULL;
+   struct tcrypt_result result;
+   unsigned int out_len_max, out_len = 0;
+   int err = -ENOMEM;
+
+   req = akcipher_request_alloc(tfm, GFP_KERNEL);
+   if (!req)
+   return err;
+
+   init_completion();
+   err = crypto_akcipher_setkey(tfm, vecs->key, vecs->key_len);
+   if (err)
+   goto free_req;
+
+   akcipher_request_set_crypt(req, vecs->m, outbuf_enc, vecs->m_size,
+  out_len);
+   /* expect this to fail, and update the required buf len */
+   crypto_akcipher_encrypt(req);
+   out_len = req->dst_len;
+   if (!out_len) {
+   err = -EINVAL;
+   goto free_req;
+   }
+
+   out_len_max = out_len;
+   err = -ENOMEM;
+   outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
+   if (!outbuf_enc)
+   goto free_req;
+
+   akcipher_request_set_crypt(req, vecs->m, outbuf_enc, vecs->m_size,
+  out_len);
+   akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
+ tcrypt_complete, );
+
+   /* Run RSA encrypt - c = m^e mod n;*/
+   err = wait_async_op(, crypto_akcipher_encrypt(req));
+   if (err) {
+   pr_err("alg: rsa: encrypt test failed. err %d\n", err);
+   goto free_all;
+   }
+   if (out_len != vecs->c_size) {
+   pr_err("alg: rsa: encrypt test failed. Invalid output len\n");
+   err = -EINVAL;
+   goto free_all;
+   }
+   /* verify that encrypted message is equal to expected */
+   if (memcmp(vecs->c, outbuf_enc, vecs->c_size)) {
+   pr_err("alg: rsa: encrypt test failed. Invalid output\n");
+   err = -EINVAL;
+   goto free_all;
+   }
+   /* Don't invoke decrypt for vectors with public key */
+   if (vecs->public_key_vec) {
+   err = 0;
+   goto free_all;
+   }
+   outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
+   if (!outbuf_dec) {
+   err = -ENOMEM;
+   goto free_all;
+   }
+   init_completion();
+   akcipher_request_set_crypt(req, outbuf_enc, outbuf_dec, vecs->c_size,
+  out_len);
+
+   /* Run RSA decrypt - m = c^d mod n;*/
+   err = wait_async_op(, crypto_akcipher_decrypt(req));
+   if (err) {
+   pr_err("alg: rsa: decrypt test failed. err %d\n", err);
+   goto free_all;
+   }
+   out_len = req->dst_len;
+   if (out_len != vecs->m_size) {
+   pr_err("alg: rsa: decrypt test failed. Invalid output len\n");
+   err = -EINVAL;
+   goto free_all;
+   }
+   /* verify that decrypted message is equal to the original msg */
+   if (memcmp(vecs->m, outbuf_dec, vecs->m_size)) {
+   pr_err("alg: rsa: decrypt test failed. Invalid output\n");
+   err = -EINVAL;
+   }
+free_all:
+   kfree(outbuf_dec);
+   kfree(outbuf_enc);
+free_req:
+   akcipher_request_free(req);
+   return err;
+}
+
+static int test_rsa(struct crypto_akcipher *tfm, struct akcipher_testvec *vecs,
+   

[PATCH RFC v7 3/3] crypto: add tests vectors for RSA

2015-06-16 Thread Tadeusz Struk
New test vectors for RSA algorithm.

Signed-off-by: Tadeusz Struk tadeusz.st...@intel.com
---
 crypto/Kconfig   |1 
 crypto/testmgr.c |  158 ++
 crypto/testmgr.h |  187 ++
 3 files changed, 346 insertions(+)

diff --git a/crypto/Kconfig b/crypto/Kconfig
index 52467cf..9471ac8 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -122,6 +122,7 @@ config CRYPTO_MANAGER2
select CRYPTO_HASH2
select CRYPTO_BLKCIPHER2
select CRYPTO_PCOMP2
+   select CRYPTO_AKCIPHER2
 
 config CRYPTO_USER
tristate Userspace cryptographic algorithm configuration
diff --git a/crypto/testmgr.c b/crypto/testmgr.c
index ccd19cf..975e1ea 100644
--- a/crypto/testmgr.c
+++ b/crypto/testmgr.c
@@ -30,6 +30,7 @@
 #include linux/string.h
 #include crypto/rng.h
 #include crypto/drbg.h
+#include crypto/akcipher.h
 
 #include internal.h
 
@@ -116,6 +117,11 @@ struct drbg_test_suite {
unsigned int count;
 };
 
+struct akcipher_test_suite {
+   struct akcipher_testvec *vecs;
+   unsigned int count;
+};
+
 struct alg_test_desc {
const char *alg;
int (*test)(const struct alg_test_desc *desc, const char *driver,
@@ -130,6 +136,7 @@ struct alg_test_desc {
struct hash_test_suite hash;
struct cprng_test_suite cprng;
struct drbg_test_suite drbg;
+   struct akcipher_test_suite akcipher;
} suite;
 };
 
@@ -1825,6 +1832,147 @@ static int alg_test_drbg(const struct alg_test_desc 
*desc, const char *driver,
 
 }
 
+static int do_test_rsa(struct crypto_akcipher *tfm,
+  struct akcipher_testvec *vecs)
+{
+   struct akcipher_request *req;
+   void *outbuf_enc = NULL;
+   void *outbuf_dec = NULL;
+   struct tcrypt_result result;
+   unsigned int out_len_max, out_len = 0;
+   int err = -ENOMEM;
+
+   req = akcipher_request_alloc(tfm, GFP_KERNEL);
+   if (!req)
+   return err;
+
+   init_completion(result.completion);
+   err = crypto_akcipher_setkey(tfm, vecs-key, vecs-key_len);
+   if (err)
+   goto free_req;
+
+   akcipher_request_set_crypt(req, vecs-m, outbuf_enc, vecs-m_size,
+  out_len);
+   /* expect this to fail, and update the required buf len */
+   crypto_akcipher_encrypt(req);
+   out_len = req-dst_len;
+   if (!out_len) {
+   err = -EINVAL;
+   goto free_req;
+   }
+
+   out_len_max = out_len;
+   err = -ENOMEM;
+   outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
+   if (!outbuf_enc)
+   goto free_req;
+
+   akcipher_request_set_crypt(req, vecs-m, outbuf_enc, vecs-m_size,
+  out_len);
+   akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
+ tcrypt_complete, result);
+
+   /* Run RSA encrypt - c = m^e mod n;*/
+   err = wait_async_op(result, crypto_akcipher_encrypt(req));
+   if (err) {
+   pr_err(alg: rsa: encrypt test failed. err %d\n, err);
+   goto free_all;
+   }
+   if (out_len != vecs-c_size) {
+   pr_err(alg: rsa: encrypt test failed. Invalid output len\n);
+   err = -EINVAL;
+   goto free_all;
+   }
+   /* verify that encrypted message is equal to expected */
+   if (memcmp(vecs-c, outbuf_enc, vecs-c_size)) {
+   pr_err(alg: rsa: encrypt test failed. Invalid output\n);
+   err = -EINVAL;
+   goto free_all;
+   }
+   /* Don't invoke decrypt for vectors with public key */
+   if (vecs-public_key_vec) {
+   err = 0;
+   goto free_all;
+   }
+   outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
+   if (!outbuf_dec) {
+   err = -ENOMEM;
+   goto free_all;
+   }
+   init_completion(result.completion);
+   akcipher_request_set_crypt(req, outbuf_enc, outbuf_dec, vecs-c_size,
+  out_len);
+
+   /* Run RSA decrypt - m = c^d mod n;*/
+   err = wait_async_op(result, crypto_akcipher_decrypt(req));
+   if (err) {
+   pr_err(alg: rsa: decrypt test failed. err %d\n, err);
+   goto free_all;
+   }
+   out_len = req-dst_len;
+   if (out_len != vecs-m_size) {
+   pr_err(alg: rsa: decrypt test failed. Invalid output len\n);
+   err = -EINVAL;
+   goto free_all;
+   }
+   /* verify that decrypted message is equal to the original msg */
+   if (memcmp(vecs-m, outbuf_dec, vecs-m_size)) {
+   pr_err(alg: rsa: decrypt test failed. Invalid output\n);
+   err = -EINVAL;
+   }
+free_all:
+   kfree(outbuf_dec);
+   kfree(outbuf_enc);
+free_req:
+   akcipher_request_free(req);
+   return