[CRYPTO] blkcipher: Added blkcipher_walk_virt_block
authorHerbert Xu <herbert@gondor.apana.org.au>
Thu, 4 Oct 2007 07:24:05 +0000 (15:24 +0800)
committerDavid S. Miller <davem@sunset.davemloft.net>
Wed, 10 Oct 2007 23:55:48 +0000 (16:55 -0700)
This patch adds the helper blkcipher_walk_virt_block which is similar to
blkcipher_walk_virt but uses a supplied block size instead of the block
size of the block cipher.  This is useful for CTR where the block size is
1 but we still want to walk by the block size of the underlying cipher.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/blkcipher.c
include/crypto/algapi.h

index 3d05586a8f34f34a9ff3e96312ccb6797ada3c22..f6c67f9d4e5c2f9085558991a6fc87893e269670 100644 (file)
@@ -84,8 +84,6 @@ static inline unsigned int blkcipher_done_slow(struct crypto_blkcipher *tfm,
 static inline unsigned int blkcipher_done_fast(struct blkcipher_walk *walk,
                                               unsigned int n)
 {
-       n = walk->nbytes - n;
-
        if (walk->flags & BLKCIPHER_WALK_COPY) {
                blkcipher_map_dst(walk);
                memcpy(walk->dst.virt.addr, walk->page, n);
@@ -109,13 +107,15 @@ int blkcipher_walk_done(struct blkcipher_desc *desc,
        unsigned int nbytes = 0;
 
        if (likely(err >= 0)) {
-               unsigned int bsize = crypto_blkcipher_blocksize(tfm);
-               unsigned int n;
+               unsigned int n = walk->nbytes - err;
 
                if (likely(!(walk->flags & BLKCIPHER_WALK_SLOW)))
-                       n = blkcipher_done_fast(walk, err);
-               else
-                       n = blkcipher_done_slow(tfm, walk, bsize);
+                       n = blkcipher_done_fast(walk, n);
+               else if (WARN_ON(err)) {
+                       err = -EINVAL;
+                       goto err;
+               } else
+                       n = blkcipher_done_slow(tfm, walk, n);
 
                nbytes = walk->total - n;
                err = 0;
@@ -132,6 +132,7 @@ int blkcipher_walk_done(struct blkcipher_desc *desc,
                return blkcipher_walk_next(desc, walk);
        }
 
+err:
        if (walk->iv != desc->info)
                memcpy(desc->info, walk->iv, crypto_blkcipher_ivsize(tfm));
        if (walk->buffer != walk->page)
@@ -225,12 +226,12 @@ static int blkcipher_walk_next(struct blkcipher_desc *desc,
 {
        struct crypto_blkcipher *tfm = desc->tfm;
        unsigned int alignmask = crypto_blkcipher_alignmask(tfm);
-       unsigned int bsize = crypto_blkcipher_blocksize(tfm);
+       unsigned int bsize;
        unsigned int n;
        int err;
 
        n = walk->total;
-       if (unlikely(n < bsize)) {
+       if (unlikely(n < crypto_blkcipher_blocksize(tfm))) {
                desc->flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
                return blkcipher_walk_done(desc, walk, -EINVAL);
        }
@@ -247,6 +248,7 @@ static int blkcipher_walk_next(struct blkcipher_desc *desc,
                }
        }
 
+       bsize = min(walk->blocksize, n);
        n = scatterwalk_clamp(&walk->in, n);
        n = scatterwalk_clamp(&walk->out, n);
 
@@ -277,7 +279,7 @@ static inline int blkcipher_copy_iv(struct blkcipher_walk *walk,
                                    struct crypto_blkcipher *tfm,
                                    unsigned int alignmask)
 {
-       unsigned bs = crypto_blkcipher_blocksize(tfm);
+       unsigned bs = walk->blocksize;
        unsigned int ivsize = crypto_blkcipher_ivsize(tfm);
        unsigned aligned_bs = ALIGN(bs, alignmask + 1);
        unsigned int size = aligned_bs * 2 + ivsize + max(aligned_bs, ivsize) -
@@ -302,6 +304,7 @@ int blkcipher_walk_virt(struct blkcipher_desc *desc,
                        struct blkcipher_walk *walk)
 {
        walk->flags &= ~BLKCIPHER_WALK_PHYS;
+       walk->blocksize = crypto_blkcipher_blocksize(desc->tfm);
        return blkcipher_walk_first(desc, walk);
 }
 EXPORT_SYMBOL_GPL(blkcipher_walk_virt);
@@ -310,6 +313,7 @@ int blkcipher_walk_phys(struct blkcipher_desc *desc,
                        struct blkcipher_walk *walk)
 {
        walk->flags |= BLKCIPHER_WALK_PHYS;
+       walk->blocksize = crypto_blkcipher_blocksize(desc->tfm);
        return blkcipher_walk_first(desc, walk);
 }
 EXPORT_SYMBOL_GPL(blkcipher_walk_phys);
@@ -342,6 +346,16 @@ static int blkcipher_walk_first(struct blkcipher_desc *desc,
        return blkcipher_walk_next(desc, walk);
 }
 
+int blkcipher_walk_virt_block(struct blkcipher_desc *desc,
+                             struct blkcipher_walk *walk,
+                             unsigned int blocksize)
+{
+       walk->flags &= ~BLKCIPHER_WALK_PHYS;
+       walk->blocksize = blocksize;
+       return blkcipher_walk_first(desc, walk);
+}
+EXPORT_SYMBOL_GPL(blkcipher_walk_virt_block);
+
 static int setkey_unaligned(struct crypto_tfm *tfm, const u8 *key,
                            unsigned int keylen)
 {
index 4af72dc21202ebd4966c293e240ff5e1e2261623..b9b05d399d2b49bbbe304fbb608336db94d58784 100644 (file)
@@ -91,6 +91,7 @@ struct blkcipher_walk {
        u8 *iv;
 
        int flags;
+       unsigned int blocksize;
 };
 
 extern const struct crypto_type crypto_ablkcipher_type;
@@ -129,6 +130,9 @@ int blkcipher_walk_virt(struct blkcipher_desc *desc,
                        struct blkcipher_walk *walk);
 int blkcipher_walk_phys(struct blkcipher_desc *desc,
                        struct blkcipher_walk *walk);
+int blkcipher_walk_virt_block(struct blkcipher_desc *desc,
+                             struct blkcipher_walk *walk,
+                             unsigned int blocksize);
 
 static inline void *crypto_tfm_ctx_aligned(struct crypto_tfm *tfm)
 {