2017-08-14 13:19:39

by Corentin Labbe

[permalink] [raw]
Subject: [PATCH 0/3 v3] crypto: engine - Permit to enqueue skcipher request

Hello

The crypto engine could actually only enqueue hash and ablkcipher request.
This patch serie permit it to enqueue skcipher requests by adding all necessary
functions.

Changes since v2
- added two patch for finding request type according to its cra_type

Changes since v1
- Aligned to column struct *dev in include
- Added missing mutex_unlock in crypto_engine_start()

Corentin Labbe (3):
crypto: skcipher - export crypto_skcipher_type2
crypto: engine - find request type with cra_type
crypto: engine - Permit to enqueue skcipher request

crypto/crypto_engine.c | 133 ++++++++++++++++++++++++++++++++++++++++++++----
crypto/skcipher.c | 3 +-
include/crypto/algapi.h | 1 +
include/crypto/engine.h | 14 +++++
4 files changed, 139 insertions(+), 12 deletions(-)

--
2.13.0


2017-08-14 13:19:43

by Corentin Labbe

[permalink] [raw]
Subject: [PATCH 3/3] crypto: engine - Permit to enqueue skcipher request

The crypto engine could actually only enqueue hash and ablkcipher request.
This patch permit it to enqueue skcipher requets by adding all necessary
functions.

Signed-off-by: Corentin Labbe <[email protected]>
---
crypto/crypto_engine.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++
include/crypto/engine.h | 14 ++++++
2 files changed, 128 insertions(+)

diff --git a/crypto/crypto_engine.c b/crypto/crypto_engine.c
index 74b840749074..8567224d7609 100644
--- a/crypto/crypto_engine.c
+++ b/crypto/crypto_engine.c
@@ -36,6 +36,7 @@ static void crypto_pump_requests(struct crypto_engine *engine,
struct crypto_async_request *async_req, *backlog;
struct ahash_request *hreq;
struct ablkcipher_request *breq;
+ struct skcipher_request *skreq;
unsigned long flags;
bool was_busy = false;
int ret;
@@ -139,6 +140,23 @@ static void crypto_pump_requests(struct crypto_engine *engine,
goto req_err;
}
return;
+ } else if (cratype == &crypto_skcipher_type2) {
+ skreq = skcipher_request_cast(engine->cur_req);
+ if (engine->prepare_skcipher_request) {
+ ret = engine->prepare_skcipher_request(engine, skreq);
+ if (ret) {
+ dev_err(engine->dev, "failed to prepare request: %d\n",
+ ret);
+ goto req_err;
+ }
+ engine->cur_req_prepared = true;
+ }
+ ret = engine->skcipher_one_request(engine, skreq);
+ if (ret) {
+ dev_err(engine->dev, "failed to cipher one request from queue\n");
+ goto req_err;
+ }
+ return;
} else {
dev_err(engine->dev, "failed to prepare request of unknown type\n");
return;
@@ -151,6 +169,9 @@ static void crypto_pump_requests(struct crypto_engine *engine,
} else if (cratype == &crypto_ablkcipher_type) {
breq = ablkcipher_request_cast(engine->cur_req);
crypto_finalize_cipher_request(engine, breq, ret);
+ } else if (cratype == &crypto_skcipher_type2) {
+ skreq = skcipher_request_cast(engine->cur_req);
+ crypto_finalize_skcipher_request(engine, skreq, ret);
}
return;

@@ -210,6 +231,49 @@ int crypto_transfer_cipher_request_to_engine(struct crypto_engine *engine,
EXPORT_SYMBOL_GPL(crypto_transfer_cipher_request_to_engine);

/**
+ * crypto_transfer_skcipher_request - transfer the new request into the
+ * enginequeue
+ * @engine: the hardware engine
+ * @req: the request need to be listed into the engine queue
+ */
+int crypto_transfer_skcipher_request(struct crypto_engine *engine,
+ struct skcipher_request *req,
+ bool need_pump)
+{
+ unsigned long flags;
+ int ret;
+
+ spin_lock_irqsave(&engine->queue_lock, flags);
+
+ if (!engine->running) {
+ spin_unlock_irqrestore(&engine->queue_lock, flags);
+ return -ESHUTDOWN;
+ }
+
+ ret = crypto_enqueue_request(&engine->queue, &req->base);
+
+ if (!engine->busy && need_pump)
+ kthread_queue_work(engine->kworker, &engine->pump_requests);
+
+ spin_unlock_irqrestore(&engine->queue_lock, flags);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(crypto_transfer_skcipher_request);
+
+/**
+ * crypto_transfer_skcipher_request_to_engine - transfer one request to list
+ * into the engine queue
+ * @engine: the hardware engine
+ * @req: the request need to be listed into the engine queue
+ */
+int crypto_transfer_skcipher_request_to_engine(struct crypto_engine *engine,
+ struct skcipher_request *req)
+{
+ return crypto_transfer_skcipher_request(engine, req, true);
+}
+EXPORT_SYMBOL_GPL(crypto_transfer_skcipher_request_to_engine);
+
+/**
* crypto_transfer_hash_request - transfer the new request into the
* enginequeue
* @engine: the hardware engine
@@ -289,6 +353,43 @@ void crypto_finalize_cipher_request(struct crypto_engine *engine,
EXPORT_SYMBOL_GPL(crypto_finalize_cipher_request);

/**
+ * crypto_finalize_skcipher_request - finalize one request if the request is done
+ * @engine: the hardware engine
+ * @req: the request need to be finalized
+ * @err: error number
+ */
+void crypto_finalize_skcipher_request(struct crypto_engine *engine,
+ struct skcipher_request *req, int err)
+{
+ unsigned long flags;
+ bool finalize_cur_req = false;
+ int ret;
+
+ spin_lock_irqsave(&engine->queue_lock, flags);
+ if (engine->cur_req == &req->base)
+ finalize_cur_req = true;
+ spin_unlock_irqrestore(&engine->queue_lock, flags);
+
+ if (finalize_cur_req) {
+ if (engine->cur_req_prepared &&
+ engine->unprepare_skcipher_request) {
+ ret = engine->unprepare_skcipher_request(engine, req);
+ if (ret)
+ dev_err(engine->dev, "failed to unprepare request\n");
+ }
+ spin_lock_irqsave(&engine->queue_lock, flags);
+ engine->cur_req = NULL;
+ engine->cur_req_prepared = false;
+ spin_unlock_irqrestore(&engine->queue_lock, flags);
+ }
+
+ req->base.complete(&req->base, err);
+
+ kthread_queue_work(engine->kworker, &engine->pump_requests);
+}
+EXPORT_SYMBOL_GPL(crypto_finalize_skcipher_request);
+
+/**
* crypto_finalize_hash_request - finalize one request if the request is done
* @engine: the hardware engine
* @req: the request need to be finalized
@@ -342,6 +443,19 @@ int crypto_engine_start(struct crypto_engine *engine)
return -EBUSY;
}

+ if (!engine->skcipher_one_request && !engine->cipher_one_request &&
+ !engine->hash_one_request) {
+ spin_unlock_irqrestore(&engine->queue_lock, flags);
+ dev_err(engine->dev, "need at least one request type\n");
+ return -EINVAL;
+ }
+
+ if (engine->skcipher_one_request && engine->cipher_one_request) {
+ spin_unlock_irqrestore(&engine->queue_lock, flags);
+ dev_err(engine->dev, "Cannot use both skcipher and ablkcipher\n");
+ return -EINVAL;
+ }
+
engine->running = true;
spin_unlock_irqrestore(&engine->queue_lock, flags);

diff --git a/include/crypto/engine.h b/include/crypto/engine.h
index dd04c1699b51..a8f6e6ed377b 100644
--- a/include/crypto/engine.h
+++ b/include/crypto/engine.h
@@ -18,6 +18,7 @@
#include <linux/kthread.h>
#include <crypto/algapi.h>
#include <crypto/hash.h>
+#include <crypto/skcipher.h>

#define ENGINE_NAME_LEN 30
/*
@@ -69,12 +70,18 @@ struct crypto_engine {
struct ablkcipher_request *req);
int (*unprepare_cipher_request)(struct crypto_engine *engine,
struct ablkcipher_request *req);
+ int (*prepare_skcipher_request)(struct crypto_engine *engine,
+ struct skcipher_request *req);
+ int (*unprepare_skcipher_request)(struct crypto_engine *engine,
+ struct skcipher_request *req);
int (*prepare_hash_request)(struct crypto_engine *engine,
struct ahash_request *req);
int (*unprepare_hash_request)(struct crypto_engine *engine,
struct ahash_request *req);
int (*cipher_one_request)(struct crypto_engine *engine,
struct ablkcipher_request *req);
+ int (*skcipher_one_request)(struct crypto_engine *engine,
+ struct skcipher_request *req);
int (*hash_one_request)(struct crypto_engine *engine,
struct ahash_request *req);

@@ -90,12 +97,19 @@ int crypto_transfer_cipher_request(struct crypto_engine *engine,
bool need_pump);
int crypto_transfer_cipher_request_to_engine(struct crypto_engine *engine,
struct ablkcipher_request *req);
+int crypto_transfer_skcipher_request(struct crypto_engine *engine,
+ struct skcipher_request *req,
+ bool need_pump);
+int crypto_transfer_skcipher_request_to_engine(struct crypto_engine *engine,
+ struct skcipher_request *req);
int crypto_transfer_hash_request(struct crypto_engine *engine,
struct ahash_request *req, bool need_pump);
int crypto_transfer_hash_request_to_engine(struct crypto_engine *engine,
struct ahash_request *req);
void crypto_finalize_cipher_request(struct crypto_engine *engine,
struct ablkcipher_request *req, int err);
+void crypto_finalize_skcipher_request(struct crypto_engine *engine,
+ struct skcipher_request *req, int err);
void crypto_finalize_hash_request(struct crypto_engine *engine,
struct ahash_request *req, int err);
int crypto_engine_start(struct crypto_engine *engine);
--
2.13.0

2017-08-14 13:20:02

by Corentin Labbe

[permalink] [raw]
Subject: [PATCH 2/3] crypto: engine - find request type with cra_type

The current method for finding request type is based on crypto_tfm_alg_type.

But in case of skcipher, it is the same than ablkcipher.
Using cra_type for this work permits to make the distinction between the two.

Signed-off-by: Corentin Labbe <[email protected]>
---
crypto/crypto_engine.c | 19 ++++++++-----------
1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/crypto/crypto_engine.c b/crypto/crypto_engine.c
index 61e7c4e02fd2..74b840749074 100644
--- a/crypto/crypto_engine.c
+++ b/crypto/crypto_engine.c
@@ -38,7 +38,8 @@ static void crypto_pump_requests(struct crypto_engine *engine,
struct ablkcipher_request *breq;
unsigned long flags;
bool was_busy = false;
- int ret, rtype;
+ int ret;
+ const struct crypto_type *cratype;

spin_lock_irqsave(&engine->queue_lock, flags);

@@ -94,7 +95,7 @@ static void crypto_pump_requests(struct crypto_engine *engine,

spin_unlock_irqrestore(&engine->queue_lock, flags);

- rtype = crypto_tfm_alg_type(engine->cur_req->tfm);
+ cratype = engine->cur_req->tfm->__crt_alg->cra_type;
/* Until here we get the request need to be encrypted successfully */
if (!was_busy && engine->prepare_crypt_hardware) {
ret = engine->prepare_crypt_hardware(engine);
@@ -104,8 +105,7 @@ static void crypto_pump_requests(struct crypto_engine *engine,
}
}

- switch (rtype) {
- case CRYPTO_ALG_TYPE_AHASH:
+ if (cratype == &crypto_ahash_type) {
hreq = ahash_request_cast(engine->cur_req);
if (engine->prepare_hash_request) {
ret = engine->prepare_hash_request(engine, hreq);
@@ -122,7 +122,7 @@ static void crypto_pump_requests(struct crypto_engine *engine,
goto req_err;
}
return;
- case CRYPTO_ALG_TYPE_ABLKCIPHER:
+ } else if (cratype == &crypto_ablkcipher_type) {
breq = ablkcipher_request_cast(engine->cur_req);
if (engine->prepare_cipher_request) {
ret = engine->prepare_cipher_request(engine, breq);
@@ -139,21 +139,18 @@ static void crypto_pump_requests(struct crypto_engine *engine,
goto req_err;
}
return;
- default:
+ } else {
dev_err(engine->dev, "failed to prepare request of unknown type\n");
return;
}

req_err:
- switch (rtype) {
- case CRYPTO_ALG_TYPE_AHASH:
+ if (cratype == &crypto_ahash_type) {
hreq = ahash_request_cast(engine->cur_req);
crypto_finalize_hash_request(engine, hreq, ret);
- break;
- case CRYPTO_ALG_TYPE_ABLKCIPHER:
+ } else if (cratype == &crypto_ablkcipher_type) {
breq = ablkcipher_request_cast(engine->cur_req);
crypto_finalize_cipher_request(engine, breq, ret);
- break;
}
return;

--
2.13.0

2017-08-14 13:20:24

by Corentin Labbe

[permalink] [raw]
Subject: [PATCH 1/3] crypto: skcipher - export crypto_skcipher_type2

This patch export crypto_skcipher_type2 like others cra_type

Signed-off-by: Corentin Labbe <[email protected]>
---
crypto/skcipher.c | 3 ++-
include/crypto/algapi.h | 1 +
2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/crypto/skcipher.c b/crypto/skcipher.c
index 4faa0fd53b0c..c6523826890f 100644
--- a/crypto/skcipher.c
+++ b/crypto/skcipher.c
@@ -893,7 +893,7 @@ static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
}
#endif

-static const struct crypto_type crypto_skcipher_type2 = {
+const struct crypto_type crypto_skcipher_type2 = {
.extsize = crypto_skcipher_extsize,
.init_tfm = crypto_skcipher_init_tfm,
.free = crypto_skcipher_free_instance,
@@ -906,6 +906,7 @@ static const struct crypto_type crypto_skcipher_type2 = {
.type = CRYPTO_ALG_TYPE_SKCIPHER,
.tfmsize = offsetof(struct crypto_skcipher, base),
};
+EXPORT_SYMBOL_GPL(crypto_skcipher_type2);

int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
const char *name, u32 type, u32 mask)
diff --git a/include/crypto/algapi.h b/include/crypto/algapi.h
index e3cebf640c00..c6a4090ed2ed 100644
--- a/include/crypto/algapi.h
+++ b/include/crypto/algapi.h
@@ -130,6 +130,7 @@ struct ablkcipher_walk {

extern const struct crypto_type crypto_ablkcipher_type;
extern const struct crypto_type crypto_blkcipher_type;
+extern const struct crypto_type crypto_skcipher_type2;

void crypto_mod_put(struct crypto_alg *alg);

--
2.13.0

2017-08-15 07:51:31

by Fabien DESSENNE

[permalink] [raw]
Subject: RE: [PATCH 2/3] crypto: engine - find request type with cra_type

Hi Corentin,

Since I have just sent a patch to add the support of "aead_request" to crypto engine, I am wondering if your proposed change (checking cra_type instead of crypto_tfm_alg_type) and mine are compatible.
It looks like they are (assuming we export crypto_aead_type): can you confirm?
BR

Fabien.

>-----Original Message-----
>From: [email protected] [mailto:linux-crypto-
>[email protected]] On Behalf Of Corentin Labbe
>Sent: lundi 14 ao?t 2017 15:17
>To: [email protected]; [email protected]
>Cc: [email protected]; [email protected]; Corentin Labbe
><[email protected]>
>Subject: [PATCH 2/3] crypto: engine - find request type with cra_type
>
>The current method for finding request type is based on crypto_tfm_alg_type.
>
>But in case of skcipher, it is the same than ablkcipher.
>Using cra_type for this work permits to make the distinction between the two.
>
>Signed-off-by: Corentin Labbe <[email protected]>
>---
> crypto/crypto_engine.c | 19 ++++++++-----------
> 1 file changed, 8 insertions(+), 11 deletions(-)
>
>diff --git a/crypto/crypto_engine.c b/crypto/crypto_engine.c index
>61e7c4e02fd2..74b840749074 100644
>--- a/crypto/crypto_engine.c
>+++ b/crypto/crypto_engine.c
>@@ -38,7 +38,8 @@ static void crypto_pump_requests(struct crypto_engine
>*engine,
> struct ablkcipher_request *breq;
> unsigned long flags;
> bool was_busy = false;
>- int ret, rtype;
>+ int ret;
>+ const struct crypto_type *cratype;
>
> spin_lock_irqsave(&engine->queue_lock, flags);
>
>@@ -94,7 +95,7 @@ static void crypto_pump_requests(struct crypto_engine
>*engine,
>
> spin_unlock_irqrestore(&engine->queue_lock, flags);
>
>- rtype = crypto_tfm_alg_type(engine->cur_req->tfm);
>+ cratype = engine->cur_req->tfm->__crt_alg->cra_type;
> /* Until here we get the request need to be encrypted successfully */
> if (!was_busy && engine->prepare_crypt_hardware) {
> ret = engine->prepare_crypt_hardware(engine);
>@@ -104,8 +105,7 @@ static void crypto_pump_requests(struct crypto_engine
>*engine,
> }
> }
>
>- switch (rtype) {
>- case CRYPTO_ALG_TYPE_AHASH:
>+ if (cratype == &crypto_ahash_type) {
> hreq = ahash_request_cast(engine->cur_req);
> if (engine->prepare_hash_request) {
> ret = engine->prepare_hash_request(engine, hreq); @@
>-122,7 +122,7 @@ static void crypto_pump_requests(struct crypto_engine
>*engine,
> goto req_err;
> }
> return;
>- case CRYPTO_ALG_TYPE_ABLKCIPHER:
>+ } else if (cratype == &crypto_ablkcipher_type) {
> breq = ablkcipher_request_cast(engine->cur_req);
> if (engine->prepare_cipher_request) {
> ret = engine->prepare_cipher_request(engine, breq);
>@@ -139,21 +139,18 @@ static void crypto_pump_requests(struct
>crypto_engine *engine,
> goto req_err;
> }
> return;
>- default:
>+ } else {
> dev_err(engine->dev, "failed to prepare request of unknown
>type\n");
> return;
> }
>
> req_err:
>- switch (rtype) {
>- case CRYPTO_ALG_TYPE_AHASH:
>+ if (cratype == &crypto_ahash_type) {
> hreq = ahash_request_cast(engine->cur_req);
> crypto_finalize_hash_request(engine, hreq, ret);
>- break;
>- case CRYPTO_ALG_TYPE_ABLKCIPHER:
>+ } else if (cratype == &crypto_ablkcipher_type) {
> breq = ablkcipher_request_cast(engine->cur_req);
> crypto_finalize_cipher_request(engine, breq, ret);
>- break;
> }
> return;
>
>--
>2.13.0

2017-08-16 08:58:26

by Corentin Labbe

[permalink] [raw]
Subject: Re: [PATCH 2/3] crypto: engine - find request type with cra_type

On Tue, Aug 15, 2017 at 07:51:14AM +0000, Fabien DESSENNE wrote:
> Hi Corentin,
>
> Since I have just sent a patch to add the support of "aead_request" to crypto engine, I am wondering if your proposed change (checking cra_type instead of crypto_tfm_alg_type) and mine are compatible.
> It looks like they are (assuming we export crypto_aead_type): can you confirm?
> BR
>
> Fabien.
>

Hello

My change is incompatible with yours since I remove a switch(crypto_tfm_alg_type) that you use.
Anyway you will need my change:) because you use ablkcipher which is obsolete and you need to convert to skcipher.

regards
Corentin Labbe

2017-08-18 08:28:50

by Herbert Xu

[permalink] [raw]
Subject: Re: [PATCH 2/3] crypto: engine - find request type with cra_type

On Mon, Aug 14, 2017 at 03:17:24PM +0200, Corentin Labbe wrote:
> The current method for finding request type is based on crypto_tfm_alg_type.
>
> But in case of skcipher, it is the same than ablkcipher.
> Using cra_type for this work permits to make the distinction between the two.
>
> Signed-off-by: Corentin Labbe <[email protected]>

I think you misunderstood my suggestion. I'm not saying that
you should use cra_type to distinguish all crypto types, it should
only be used to distinguish ablkcipher from skcipher.

Thanks,
--
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

2017-08-18 11:40:19

by Corentin Labbe

[permalink] [raw]
Subject: Re: [PATCH 2/3] crypto: engine - find request type with cra_type

On Fri, Aug 18, 2017 at 04:28:37PM +0800, Herbert Xu wrote:
> On Mon, Aug 14, 2017 at 03:17:24PM +0200, Corentin Labbe wrote:
> > The current method for finding request type is based on crypto_tfm_alg_type.
> >
> > But in case of skcipher, it is the same than ablkcipher.
> > Using cra_type for this work permits to make the distinction between the two.
> >
> > Signed-off-by: Corentin Labbe <[email protected]>
>
> I think you misunderstood my suggestion. I'm not saying that
> you should use cra_type to distinguish all crypto types, it should
> only be used to distinguish ablkcipher from skcipher.
>

Will change that in v4

Thanks