[PATCH 4/4] crypto: RSA padding algorithm

2015-11-19 Thread Andrew Zaborowski
This patch adds PKCS#1 v1.5 standard RSA padding as a separate template.
This way an RSA cipher with padding can be obtained by instantiating
"pkcs1pad(rsa)".  The reason for adding this is that RSA is almost
never used without this padding (or OAEP) so it will be needed for
either certificate work in the kernel or the userspace, and I also hear
that it is likely implemented by hardware RSA in which case hardware
implementations of the whole of pkcs1pad(rsa) can be provided.

Signed-off-by: Andrew Zaborowski 
---
v2: rename rsa-padding.c to rsa-pkcs1pad.c,
use a memset instead of a loop,
add a key size check in pkcs1pad_sign,
add a general comment about pkcs1pad_verify
v3: rewrite the initialisation to avoid an obsolete and less flexible
mechanism, now following the aead template initialisation.
---
 crypto/Makefile   |   1 +
 crypto/rsa-pkcs1pad.c | 604 ++
 crypto/rsa.c  |  16 +-
 include/crypto/internal/rsa.h |   2 +
 4 files changed, 622 insertions(+), 1 deletion(-)
 create mode 100644 crypto/rsa-pkcs1pad.c

diff --git a/crypto/Makefile b/crypto/Makefile
index f7aba92..2acdbbd 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -40,6 +40,7 @@ rsa_generic-y := rsapubkey-asn1.o
 rsa_generic-y += rsaprivkey-asn1.o
 rsa_generic-y += rsa.o
 rsa_generic-y += rsa_helper.o
+rsa_generic-y += rsa-pkcs1pad.o
 obj-$(CONFIG_CRYPTO_RSA) += rsa_generic.o
 
 cryptomgr-y := algboss.o testmgr.o
diff --git a/crypto/rsa-pkcs1pad.c b/crypto/rsa-pkcs1pad.c
new file mode 100644
index 000..8ee22a2
--- /dev/null
+++ b/crypto/rsa-pkcs1pad.c
@@ -0,0 +1,604 @@
+/*
+ * RSA padding templates.
+ *
+ * Copyright (c) 2015  Intel Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct pkcs1pad_ctx {
+   struct crypto_akcipher *child;
+
+   unsigned int key_size;
+};
+
+struct pkcs1pad_request {
+   struct akcipher_request child_req;
+
+   struct scatterlist in_sg[3], out_sg[2];
+   uint8_t *in_buf, *out_buf;
+};
+
+static int pkcs1pad_set_pub_key(struct crypto_akcipher *tfm, const void *key,
+   unsigned int keylen)
+{
+   struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
+   int err, size;
+
+   err = crypto_akcipher_set_pub_key(ctx->child, key, keylen);
+
+   if (!err) {
+   /* Find out new modulus size from rsa implementation */
+   size = crypto_akcipher_maxsize(ctx->child);
+
+   ctx->key_size = size > 0 ? size : 0;
+   if (size <= 0)
+   err = size;
+   }
+
+   return err;
+}
+
+static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void *key,
+   unsigned int keylen)
+{
+   struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
+   int err, size;
+
+   err = crypto_akcipher_set_priv_key(ctx->child, key, keylen);
+
+   if (!err) {
+   /* Find out new modulus size from rsa implementation */
+   size = crypto_akcipher_maxsize(ctx->child);
+
+   ctx->key_size = size > 0 ? size : 0;
+   if (size <= 0)
+   err = size;
+   }
+
+   return err;
+}
+
+static int pkcs1pad_get_max_size(struct crypto_akcipher *tfm)
+{
+   struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
+
+   /*
+* The maximum destination buffer size for the encrypt/sign operations
+* will be the same as for RSA, even though it's smaller for
+* decrypt/verify.
+*/
+
+   return ctx->key_size ?: -EINVAL;
+}
+
+static void pkcs1pad_sg_set_buf(struct scatterlist *sg, void *buf, size_t len,
+   struct scatterlist *next)
+{
+   int nsegs = next ? 1 : 0;
+
+   if (offset_in_page(buf) + len <= PAGE_SIZE) {
+   nsegs += 1;
+   sg_init_table(sg, nsegs);
+   sg_set_buf(sg, buf, len);
+   } else {
+   nsegs += 2;
+   sg_init_table(sg, nsegs);
+   sg_set_buf(sg + 0, buf, PAGE_SIZE - offset_in_page(buf));
+   sg_set_buf(sg + 1, buf + PAGE_SIZE - offset_in_page(buf),
+   offset_in_page(buf) + len - PAGE_SIZE);
+   }
+
+   if (next)
+   sg_chain(sg, nsegs, next);
+}
+
+static int pkcs1pad_encrypt_sign_complete(struct akcipher_request *req, int 
err)
+{
+   struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
+   struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
+   struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
+   uint8_t zeros[ctx->key_size - req_ctx->child_req.dst_len];
+
+   if 

[PATCH 4/4] crypto: RSA padding algorithm

2015-11-13 Thread Andrew Zaborowski
This patch adds PKCS#1 v1.5 standard RSA padding as a separate template.
This way an RSA cipher with padding can be obtained by instantiating
"pkcs1pad(rsa)".  The reason for adding this is that RSA is almost
never used without this padding (or OAEP) so it will be needed for
either certificate work in the kernel or the userspace, and also I hear
that it is likely implemented by hardware RSA in which case an
implementation of the whole of pkcs1pad(rsa) can be provided.

Signed-off-by: Andrew Zaborowski 
---
 crypto/Makefile   |   1 +
 crypto/rsa-pkcs1pad.c | 593 ++
 crypto/rsa.c  |  16 +-
 include/crypto/internal/rsa.h |   2 +
 4 files changed, 611 insertions(+), 1 deletion(-)
 create mode 100644 crypto/rsa-pkcs1pad.c

diff --git a/crypto/Makefile b/crypto/Makefile
index f7aba92..2acdbbd 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -40,6 +40,7 @@ rsa_generic-y := rsapubkey-asn1.o
 rsa_generic-y += rsaprivkey-asn1.o
 rsa_generic-y += rsa.o
 rsa_generic-y += rsa_helper.o
+rsa_generic-y += rsa-pkcs1pad.o
 obj-$(CONFIG_CRYPTO_RSA) += rsa_generic.o
 
 cryptomgr-y := algboss.o testmgr.o
diff --git a/crypto/rsa-pkcs1pad.c b/crypto/rsa-pkcs1pad.c
new file mode 100644
index 000..d8e0c71
--- /dev/null
+++ b/crypto/rsa-pkcs1pad.c
@@ -0,0 +1,593 @@
+/*
+ * RSA padding templates.
+ *
+ * Copyright (c) 2015  Intel Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct pkcs1pad_ctx {
+   struct crypto_akcipher *child;
+
+   unsigned int key_size;
+};
+
+struct pkcs1pad_request {
+   struct akcipher_request child_req;
+
+   struct scatterlist in_sg[3], out_sg[2];
+   uint8_t *in_buf, *out_buf;
+};
+
+static int pkcs1pad_set_pub_key(struct crypto_akcipher *tfm, const void *key,
+   unsigned int keylen)
+{
+   struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
+   int err, size;
+
+   err = crypto_akcipher_set_pub_key(ctx->child, key, keylen);
+
+   if (!err) {
+   /* Find out new modulus size from rsa implementation */
+   size = crypto_akcipher_maxsize(ctx->child);
+
+   ctx->key_size = size > 0 ? size : 0;
+   if (size <= 0)
+   err = size;
+   }
+
+   return err;
+}
+
+static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void *key,
+   unsigned int keylen)
+{
+   struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
+   int err, size;
+
+   err = crypto_akcipher_set_priv_key(ctx->child, key, keylen);
+
+   if (!err) {
+   /* Find out new modulus size from rsa implementation */
+   size = crypto_akcipher_maxsize(ctx->child);
+
+   ctx->key_size = size > 0 ? size : 0;
+   if (size <= 0)
+   err = size;
+   }
+
+   return err;
+}
+
+static int pkcs1pad_get_max_size(struct crypto_akcipher *tfm)
+{
+   struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
+
+   /*
+* The maximum destination buffer size for the encrypt/sign operations
+* will be the same as for RSA, even though it's smaller for
+* decrypt/verify.
+*/
+
+   return ctx->key_size ?: -EINVAL;
+}
+
+static void pkcs1pad_sg_set_buf(struct scatterlist *sg, void *buf, size_t len,
+   struct scatterlist *next)
+{
+   int nsegs = next ? 1 : 0;
+
+   if (offset_in_page(buf) + len <= PAGE_SIZE) {
+   nsegs += 1;
+   sg_init_table(sg, nsegs);
+   sg_set_buf(sg, buf, len);
+   } else {
+   nsegs += 2;
+   sg_init_table(sg, nsegs);
+   sg_set_buf(sg + 0, buf, PAGE_SIZE - offset_in_page(buf));
+   sg_set_buf(sg + 1, buf + PAGE_SIZE - offset_in_page(buf),
+   offset_in_page(buf) + len - PAGE_SIZE);
+   }
+
+   if (next)
+   sg_chain(sg, nsegs, next);
+}
+
+static int pkcs1pad_encrypt_sign_complete(struct akcipher_request *req, int 
err)
+{
+   struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
+   struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
+   struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
+   uint8_t zeros[ctx->key_size - req_ctx->child_req.dst_len];
+
+   if (!err) {
+   if (req_ctx->child_req.dst_len < ctx->key_size) {
+   memset(zeros, 0, sizeof(zeros));
+   sg_copy_from_buffer(req->dst,
+   sg_nents_for_len(req->dst,
+   

Re: [PATCH 4/4] crypto: RSA padding algorithm

2015-11-13 Thread Andrzej Zaborowski
Hi Stephan,

On 11 November 2015 at 14:19, Stephan Mueller  wrote:
> Am Mittwoch, 11. November 2015, 01:58:45 schrieb Andrew Zaborowski:
>
> Hi Andrew,
>
>>This patch adds PKCS#1 v1.5 standard RSA padding as a separate template.
>>This way an RSA cipher with padding can be obtained by instantiating
>>"pkcs1pad(rsa)".  The reason for adding this is that RSA is almost
>>never used without this padding (or OAEP) so it will be needed for
>>either certificate work in the kernel or the userspace, and also I hear
>>that it is likely implemented by hardware RSA in which case an
>>implementation of the whole "pkcs1pad(rsa)" can be provided.
>
> In general, I think that there is a PKCS 1 implementation in the kernel in
> crypto/asymmetric_keys/rsa.c
>
> Shouldn't that all somehow being synchronized?

Probably as Marcel says the certificate code should use the crypto
algorithm API.  In its current form it won't be able to take advantage
of hardware acceleration but it must have tons of overhead less than
if it used the crypto API.

>
> Maybe this patch should go in but then crypto/asymmetric_keys/rsa.c should
> kind of being removed or point to kernel crypto API?
>>
>>Signed-off-by: Andrew Zaborowski 
>>---
>> crypto/Makefile   |   1 +
>> crypto/rsa-padding.c  | 586
>>++ crypto/rsa.c  |
>>16 +-
>> include/crypto/internal/rsa.h |   2 +
>> 4 files changed, 604 insertions(+), 1 deletion(-)
>> create mode 100644 crypto/rsa-padding.c
>>
>>diff --git a/crypto/Makefile b/crypto/Makefile
>>index f7aba92..46fe0b4 100644
>>--- a/crypto/Makefile
>>+++ b/crypto/Makefile
>>@@ -40,6 +40,7 @@ rsa_generic-y := rsapubkey-asn1.o
>> rsa_generic-y += rsaprivkey-asn1.o
>> rsa_generic-y += rsa.o
>> rsa_generic-y += rsa_helper.o
>>+rsa_generic-y += rsa-padding.o
>> obj-$(CONFIG_CRYPTO_RSA) += rsa_generic.o
>>
>> cryptomgr-y := algboss.o testmgr.o
>>diff --git a/crypto/rsa-padding.c b/crypto/rsa-padding.c
>>new file mode 100644
>>index 000..b9f9f31
>>--- /dev/null
>>+++ b/crypto/rsa-padding.c
>>@@ -0,0 +1,586 @@
>>+/*
>>+ * RSA padding templates.
>>+ *
>>+ * Copyright (c) 2015  Intel Corporation
>>+ *
>>+ * This program is free software; you can redistribute it and/or modify it
>>+ * under the terms of the GNU General Public License as published by the
>>Free + * Software Foundation; either version 2 of the License, or (at your
>>option) + * any later version.
>>+ */
>>+
>>+#include 
>>+#include 
>>+#include 
>>+#include 
>>+#include 
>>+#include 
>>+#include 
>>+#include 
>>+
>>+struct pkcs1pad_ctx {
>>+  struct crypto_akcipher *child;
>>+
>>+  unsigned int key_size;
>>+};
>>+
>>+struct pkcs1pad_request {
>>+  struct akcipher_request child_req;
>>+
>>+  struct scatterlist in_sg[3], out_sg[2];
>>+  uint8_t *in_buf, *out_buf;
>>+};
>>+
>>+static int pkcs1pad_set_pub_key(struct crypto_akcipher *tfm, const void
>>*key, +unsigned int keylen)
>>+{
>>+  struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
>>+  int err, size;
>>+
>>+  err = crypto_akcipher_set_pub_key(ctx->child, key, keylen);
>>+
>>+  if (!err) {
>>+  /* Find out new modulus size from rsa implementation */
>>+  size = crypto_akcipher_maxsize(ctx->child);
>>+
>>+  ctx->key_size = size > 0 ? size : 0;
>>+  if (size <= 0)
>>+  err = size;
>>+  }
>>+
>>+  return err;
>>+}
>>+
>>+static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void
>>*key, +unsigned int keylen)
>>+{
>>+  struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
>>+  int err, size;
>>+
>>+  err = crypto_akcipher_set_priv_key(ctx->child, key, keylen);
>>+
>>+  if (!err) {
>>+  /* Find out new modulus size from rsa implementation */
>>+  size = crypto_akcipher_maxsize(ctx->child);
>>+
>>+  ctx->key_size = size > 0 ? size : 0;
>>+  if (size <= 0)
>>+  err = size;
>>+  }
>>+
>>+  return err;
>>+}
>>+
>>+static int pkcs1pad_get_max_size(struct crypto_akcipher *tfm)
>>+{
>>+  struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
>>+
>>+  /*
>>+   * The maximum destination buffer size for the encrypt/sign operations
>>+   * will be the same as for RSA, even though it's smaller for
>>+   * decrypt/verify.
>>+   */
>>+
>>+  return ctx->key_size ?: -EINVAL;
>>+}
>>+
>>+static void pkcs1pad_sg_set_buf(struct scatterlist *sg, void *buf, size_t
>>len, + struct scatterlist *next)
>>+{
>>+  int nsegs = next ? 1 : 0;
>>+
>>+  if (offset_in_page(buf) + len <= PAGE_SIZE) {
>>+  nsegs += 1;
>>+  sg_init_table(sg, nsegs);
>>+  sg_set_buf(sg, buf, len);
>>+  } else {
>>+  nsegs += 2;
>>+  sg_init_table(sg, nsegs);
>>+  sg_set_buf(sg + 0, buf, PAGE_SIZE - 

Re: [PATCH 4/4] crypto: RSA padding algorithm

2015-11-11 Thread Stephan Mueller
Am Mittwoch, 11. November 2015, 01:58:45 schrieb Andrew Zaborowski:

Hi Andrew,

>This patch adds PKCS#1 v1.5 standard RSA padding as a separate template.
>This way an RSA cipher with padding can be obtained by instantiating
>"pkcs1pad(rsa)".  The reason for adding this is that RSA is almost
>never used without this padding (or OAEP) so it will be needed for
>either certificate work in the kernel or the userspace, and also I hear
>that it is likely implemented by hardware RSA in which case an
>implementation of the whole "pkcs1pad(rsa)" can be provided.

In general, I think that there is a PKCS 1 implementation in the kernel in 
crypto/asymmetric_keys/rsa.c

Shouldn't that all somehow being synchronized?

Maybe this patch should go in but then crypto/asymmetric_keys/rsa.c should 
kind of being removed or point to kernel crypto API?
>
>Signed-off-by: Andrew Zaborowski 
>---
> crypto/Makefile   |   1 +
> crypto/rsa-padding.c  | 586
>++ crypto/rsa.c  | 
>16 +-
> include/crypto/internal/rsa.h |   2 +
> 4 files changed, 604 insertions(+), 1 deletion(-)
> create mode 100644 crypto/rsa-padding.c
>
>diff --git a/crypto/Makefile b/crypto/Makefile
>index f7aba92..46fe0b4 100644
>--- a/crypto/Makefile
>+++ b/crypto/Makefile
>@@ -40,6 +40,7 @@ rsa_generic-y := rsapubkey-asn1.o
> rsa_generic-y += rsaprivkey-asn1.o
> rsa_generic-y += rsa.o
> rsa_generic-y += rsa_helper.o
>+rsa_generic-y += rsa-padding.o
> obj-$(CONFIG_CRYPTO_RSA) += rsa_generic.o
>
> cryptomgr-y := algboss.o testmgr.o
>diff --git a/crypto/rsa-padding.c b/crypto/rsa-padding.c
>new file mode 100644
>index 000..b9f9f31
>--- /dev/null
>+++ b/crypto/rsa-padding.c
>@@ -0,0 +1,586 @@
>+/*
>+ * RSA padding templates.
>+ *
>+ * Copyright (c) 2015  Intel Corporation
>+ *
>+ * This program is free software; you can redistribute it and/or modify it
>+ * under the terms of the GNU General Public License as published by the
>Free + * Software Foundation; either version 2 of the License, or (at your
>option) + * any later version.
>+ */
>+
>+#include 
>+#include 
>+#include 
>+#include 
>+#include 
>+#include 
>+#include 
>+#include 
>+
>+struct pkcs1pad_ctx {
>+  struct crypto_akcipher *child;
>+
>+  unsigned int key_size;
>+};
>+
>+struct pkcs1pad_request {
>+  struct akcipher_request child_req;
>+
>+  struct scatterlist in_sg[3], out_sg[2];
>+  uint8_t *in_buf, *out_buf;
>+};
>+
>+static int pkcs1pad_set_pub_key(struct crypto_akcipher *tfm, const void
>*key, +unsigned int keylen)
>+{
>+  struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
>+  int err, size;
>+
>+  err = crypto_akcipher_set_pub_key(ctx->child, key, keylen);
>+
>+  if (!err) {
>+  /* Find out new modulus size from rsa implementation */
>+  size = crypto_akcipher_maxsize(ctx->child);
>+
>+  ctx->key_size = size > 0 ? size : 0;
>+  if (size <= 0)
>+  err = size;
>+  }
>+
>+  return err;
>+}
>+
>+static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void
>*key, +unsigned int keylen)
>+{
>+  struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
>+  int err, size;
>+
>+  err = crypto_akcipher_set_priv_key(ctx->child, key, keylen);
>+
>+  if (!err) {
>+  /* Find out new modulus size from rsa implementation */
>+  size = crypto_akcipher_maxsize(ctx->child);
>+
>+  ctx->key_size = size > 0 ? size : 0;
>+  if (size <= 0)
>+  err = size;
>+  }
>+
>+  return err;
>+}
>+
>+static int pkcs1pad_get_max_size(struct crypto_akcipher *tfm)
>+{
>+  struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
>+
>+  /*
>+   * The maximum destination buffer size for the encrypt/sign operations
>+   * will be the same as for RSA, even though it's smaller for
>+   * decrypt/verify.
>+   */
>+
>+  return ctx->key_size ?: -EINVAL;
>+}
>+
>+static void pkcs1pad_sg_set_buf(struct scatterlist *sg, void *buf, size_t
>len, + struct scatterlist *next)
>+{
>+  int nsegs = next ? 1 : 0;
>+
>+  if (offset_in_page(buf) + len <= PAGE_SIZE) {
>+  nsegs += 1;
>+  sg_init_table(sg, nsegs);
>+  sg_set_buf(sg, buf, len);
>+  } else {
>+  nsegs += 2;
>+  sg_init_table(sg, nsegs);
>+  sg_set_buf(sg + 0, buf, PAGE_SIZE - offset_in_page(buf));
>+  sg_set_buf(sg + 1, buf + PAGE_SIZE - offset_in_page(buf),
>+  offset_in_page(buf) + len - PAGE_SIZE);
>+  }
>+
>+  if (next)
>+  sg_chain(sg, nsegs, next);
>+}
>+
>+static int pkcs1pad_encrypt_sign_complete(struct akcipher_request *req, int
>err) +{
>+  struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
>+  struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
>+  struct 

Re: [PATCH 4/4] crypto: RSA padding algorithm

2015-11-11 Thread Marcel Holtmann
Hi Stephan,

>> This patch adds PKCS#1 v1.5 standard RSA padding as a separate template.
>> This way an RSA cipher with padding can be obtained by instantiating
>> "pkcs1pad(rsa)".  The reason for adding this is that RSA is almost
>> never used without this padding (or OAEP) so it will be needed for
>> either certificate work in the kernel or the userspace, and also I hear
>> that it is likely implemented by hardware RSA in which case an
>> implementation of the whole "pkcs1pad(rsa)" can be provided.
> 
> In general, I think that there is a PKCS 1 implementation in the kernel in 
> crypto/asymmetric_keys/rsa.c
> 
> Shouldn't that all somehow being synchronized?
> 
> Maybe this patch should go in but then crypto/asymmetric_keys/rsa.c should 
> kind of being removed or point to kernel crypto API?

I think crypto/asymmetric_keys/ needs to move to security/keys/asymmetric/ and 
then utilize akcipher and also PKCS 1 from crypto/

Regards

Marcel

--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH 4/4] crypto: RSA padding algorithm

2015-11-11 Thread Tadeusz Struk
On 11/10/2015 04:58 PM, Andrew Zaborowski wrote:
> This patch adds PKCS#1 v1.5 standard RSA padding as a separate template.
> This way an RSA cipher with padding can be obtained by instantiating
> "pkcs1pad(rsa)".  The reason for adding this is that RSA is almost
> never used without this padding (or OAEP) so it will be needed for
> either certificate work in the kernel or the userspace, and also I hear
> that it is likely implemented by hardware RSA in which case an
> implementation of the whole "pkcs1pad(rsa)" can be provided.
> 
> Signed-off-by: Andrew Zaborowski 
> ---
>  crypto/Makefile   |   1 +
>  crypto/rsa-padding.c  | 586 
> ++
>  crypto/rsa.c  |  16 +-
>  include/crypto/internal/rsa.h |   2 +
>  4 files changed, 604 insertions(+), 1 deletion(-)
>  create mode 100644 crypto/rsa-padding.c

Can we call this new file rsa-pkcs1pad.c instead?
Thanks
--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 4/4] crypto: RSA padding algorithm

2015-11-10 Thread Andrew Zaborowski
This patch adds PKCS#1 v1.5 standard RSA padding as a separate template.
This way an RSA cipher with padding can be obtained by instantiating
"pkcs1pad(rsa)".  The reason for adding this is that RSA is almost
never used without this padding (or OAEP) so it will be needed for
either certificate work in the kernel or the userspace, and also I hear
that it is likely implemented by hardware RSA in which case an
implementation of the whole "pkcs1pad(rsa)" can be provided.

Signed-off-by: Andrew Zaborowski 
---
 crypto/Makefile   |   1 +
 crypto/rsa-padding.c  | 586 ++
 crypto/rsa.c  |  16 +-
 include/crypto/internal/rsa.h |   2 +
 4 files changed, 604 insertions(+), 1 deletion(-)
 create mode 100644 crypto/rsa-padding.c

diff --git a/crypto/Makefile b/crypto/Makefile
index f7aba92..46fe0b4 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -40,6 +40,7 @@ rsa_generic-y := rsapubkey-asn1.o
 rsa_generic-y += rsaprivkey-asn1.o
 rsa_generic-y += rsa.o
 rsa_generic-y += rsa_helper.o
+rsa_generic-y += rsa-padding.o
 obj-$(CONFIG_CRYPTO_RSA) += rsa_generic.o
 
 cryptomgr-y := algboss.o testmgr.o
diff --git a/crypto/rsa-padding.c b/crypto/rsa-padding.c
new file mode 100644
index 000..b9f9f31
--- /dev/null
+++ b/crypto/rsa-padding.c
@@ -0,0 +1,586 @@
+/*
+ * RSA padding templates.
+ *
+ * Copyright (c) 2015  Intel Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct pkcs1pad_ctx {
+   struct crypto_akcipher *child;
+
+   unsigned int key_size;
+};
+
+struct pkcs1pad_request {
+   struct akcipher_request child_req;
+
+   struct scatterlist in_sg[3], out_sg[2];
+   uint8_t *in_buf, *out_buf;
+};
+
+static int pkcs1pad_set_pub_key(struct crypto_akcipher *tfm, const void *key,
+   unsigned int keylen)
+{
+   struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
+   int err, size;
+
+   err = crypto_akcipher_set_pub_key(ctx->child, key, keylen);
+
+   if (!err) {
+   /* Find out new modulus size from rsa implementation */
+   size = crypto_akcipher_maxsize(ctx->child);
+
+   ctx->key_size = size > 0 ? size : 0;
+   if (size <= 0)
+   err = size;
+   }
+
+   return err;
+}
+
+static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void *key,
+   unsigned int keylen)
+{
+   struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
+   int err, size;
+
+   err = crypto_akcipher_set_priv_key(ctx->child, key, keylen);
+
+   if (!err) {
+   /* Find out new modulus size from rsa implementation */
+   size = crypto_akcipher_maxsize(ctx->child);
+
+   ctx->key_size = size > 0 ? size : 0;
+   if (size <= 0)
+   err = size;
+   }
+
+   return err;
+}
+
+static int pkcs1pad_get_max_size(struct crypto_akcipher *tfm)
+{
+   struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
+
+   /*
+* The maximum destination buffer size for the encrypt/sign operations
+* will be the same as for RSA, even though it's smaller for
+* decrypt/verify.
+*/
+
+   return ctx->key_size ?: -EINVAL;
+}
+
+static void pkcs1pad_sg_set_buf(struct scatterlist *sg, void *buf, size_t len,
+   struct scatterlist *next)
+{
+   int nsegs = next ? 1 : 0;
+
+   if (offset_in_page(buf) + len <= PAGE_SIZE) {
+   nsegs += 1;
+   sg_init_table(sg, nsegs);
+   sg_set_buf(sg, buf, len);
+   } else {
+   nsegs += 2;
+   sg_init_table(sg, nsegs);
+   sg_set_buf(sg + 0, buf, PAGE_SIZE - offset_in_page(buf));
+   sg_set_buf(sg + 1, buf + PAGE_SIZE - offset_in_page(buf),
+   offset_in_page(buf) + len - PAGE_SIZE);
+   }
+
+   if (next)
+   sg_chain(sg, nsegs, next);
+}
+
+static int pkcs1pad_encrypt_sign_complete(struct akcipher_request *req, int 
err)
+{
+   struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
+   struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
+   struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
+   uint8_t zeros[ctx->key_size - req_ctx->child_req.dst_len];
+
+   if (!err) {
+   if (req_ctx->child_req.dst_len < ctx->key_size) {
+   memset(zeros, 0, sizeof(zeros));
+   sg_copy_from_buffer(req->dst,
+   sg_nents_for_len(req->dst,
+   sizeof(zeros)),
+