The sg length could be more than remaining data on it.
So check the length requirement against the minimum between those two
values.
Signed-off-by: Corentin Labbe <clabbe@baylibre.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
        struct scatterlist *sg;
        struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
        struct sun8i_ce_alg_template *algt;
+       unsigned int todo, len;
 
        algt = container_of(alg, struct sun8i_ce_alg_template, alg.skcipher);
 
                return true;
        }
 
+       len = areq->cryptlen;
        sg = areq->src;
        while (sg) {
                if (!IS_ALIGNED(sg->offset, sizeof(u32))) {
                        algt->stat_fb_srcali++;
                        return true;
                }
-               if (sg->length % 4) {
+               todo = min(len, sg->length);
+               if (todo % 4) {
                        algt->stat_fb_srclen++;
                        return true;
                }
+               len -= todo;
                sg = sg_next(sg);
        }
+
+       len = areq->cryptlen;
        sg = areq->dst;
        while (sg) {
                if (!IS_ALIGNED(sg->offset, sizeof(u32))) {
                        algt->stat_fb_dstali++;
                        return true;
                }
-               if (sg->length % 4) {
+               todo = min(len, sg->length);
+               if (todo % 4) {
                        algt->stat_fb_dstlen++;
                        return true;
                }
+               len -= todo;
                sg = sg_next(sg);
        }
        return false;