As it stands chacha cannot do chaining.  That is, it has to handle
each request as a whole.  This patch adds support for chaining when
the CRYPTO_TFM_REQ_MORE flag is set.
    
Signed-off-by: Herbert Xu <[email protected]>
---

 arch/mips/crypto/chacha-glue.c |   41 +++++++++++++++++++++++++++--------------
 1 file changed, 27 insertions(+), 14 deletions(-)

diff --git a/arch/mips/crypto/chacha-glue.c b/arch/mips/crypto/chacha-glue.c
index d1fd23e6ef844..658412bfdea29 100644
--- a/arch/mips/crypto/chacha-glue.c
+++ b/arch/mips/crypto/chacha-glue.c
@@ -7,9 +7,7 @@
  */
 
 #include <asm/byteorder.h>
-#include <crypto/algapi.h>
 #include <crypto/internal/chacha.h>
-#include <crypto/internal/skcipher.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 
@@ -26,16 +24,16 @@ void chacha_init_arch(u32 *state, const u32 *key, const u8 
*iv)
 }
 EXPORT_SYMBOL(chacha_init_arch);
 
-static int chacha_mips_stream_xor(struct skcipher_request *req,
-                                 const struct chacha_ctx *ctx, const u8 *iv)
+static int chacha_mips_stream_xor(struct skcipher_request *req, int nrounds)
 {
+       struct chacha_reqctx *rctx = skcipher_request_ctx(req);
        struct skcipher_walk walk;
-       u32 state[16];
+       u32 *state = rctx->state;
        int err;
 
-       err = skcipher_walk_virt(&walk, req, false);
+       rctx->init = req->base.flags & CRYPTO_TFM_REQ_MORE;
 
-       chacha_init_generic(state, ctx->key, iv);
+       err = skcipher_walk_virt(&walk, req, false);
 
        while (walk.nbytes > 0) {
                unsigned int nbytes = walk.nbytes;
@@ -44,7 +42,7 @@ static int chacha_mips_stream_xor(struct skcipher_request 
*req,
                        nbytes = round_down(nbytes, walk.stride);
 
                chacha_crypt(state, walk.dst.virt.addr, walk.src.virt.addr,
-                            nbytes, ctx->nrounds);
+                            nbytes, nrounds);
                err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
        }
 
@@ -54,27 +52,39 @@ static int chacha_mips_stream_xor(struct skcipher_request 
*req,
 static int chacha_mips(struct skcipher_request *req)
 {
        struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
+       struct chacha_reqctx *rctx = skcipher_request_ctx(req);
        struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
 
-       return chacha_mips_stream_xor(req, ctx, req->iv);
+       if (!rctx->init)
+               chacha_init_generic(rctx->state, ctx->key, req->iv);
+
+       return chacha_mips_stream_xor(req, ctx->nrounds);
 }
 
 static int xchacha_mips(struct skcipher_request *req)
 {
        struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
+       struct chacha_reqctx *rctx = skcipher_request_ctx(req);
        struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
-       struct chacha_ctx subctx;
-       u32 state[16];
+       int nrounds = ctx->nrounds;
+       u32 *state = rctx->state;
        u8 real_iv[16];
+       u32 key[8];
+
+       if (rctx->init)
+               goto skip_init;
 
        chacha_init_generic(state, ctx->key, req->iv);
 
-       hchacha_block(state, subctx.key, ctx->nrounds);
-       subctx.nrounds = ctx->nrounds;
+       hchacha_block(state, key, nrounds);
 
        memcpy(&real_iv[0], req->iv + 24, 8);
        memcpy(&real_iv[8], req->iv + 16, 8);
-       return chacha_mips_stream_xor(req, &subctx, real_iv);
+
+       chacha_init_generic(rctx->state, key, real_iv);
+
+skip_init:
+       return chacha_mips_stream_xor(req, nrounds);
 }
 
 static struct skcipher_alg algs[] = {
@@ -90,6 +100,7 @@ static struct skcipher_alg algs[] = {
                .max_keysize            = CHACHA_KEY_SIZE,
                .ivsize                 = CHACHA_IV_SIZE,
                .chunksize              = CHACHA_BLOCK_SIZE,
+               .reqsize                = sizeof(struct chacha_reqctx),
                .setkey                 = chacha20_setkey,
                .encrypt                = chacha_mips,
                .decrypt                = chacha_mips,
@@ -105,6 +116,7 @@ static struct skcipher_alg algs[] = {
                .max_keysize            = CHACHA_KEY_SIZE,
                .ivsize                 = XCHACHA_IV_SIZE,
                .chunksize              = CHACHA_BLOCK_SIZE,
+               .reqsize                = sizeof(struct chacha_reqctx),
                .setkey                 = chacha20_setkey,
                .encrypt                = xchacha_mips,
                .decrypt                = xchacha_mips,
@@ -120,6 +132,7 @@ static struct skcipher_alg algs[] = {
                .max_keysize            = CHACHA_KEY_SIZE,
                .ivsize                 = XCHACHA_IV_SIZE,
                .chunksize              = CHACHA_BLOCK_SIZE,
+               .reqsize                = sizeof(struct chacha_reqctx),
                .setkey                 = chacha12_setkey,
                .encrypt                = xchacha_mips,
                .decrypt                = xchacha_mips,

Reply via email to