UBUNTU: Ubuntu-2.6.38-12.51
[linux-flexiantxendom0-natty.git] / crypto / authenc.c
index 6287cfd..5ef7ba6 100644 (file)
@@ -46,6 +46,12 @@ struct authenc_request_ctx {
        char tail[];
 };
 
+static void authenc_request_complete(struct aead_request *req, int err)
+{
+       if (err != -EINPROGRESS)
+               aead_request_complete(req, err);
+}
+
 static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
                                 unsigned int keylen)
 {
@@ -101,20 +107,6 @@ badkey:
        goto out;
 }
 
-static void authenc_chain(struct scatterlist *head, struct scatterlist *sg,
-                         int chain)
-{
-       if (chain) {
-               head->length += sg->length;
-               sg = scatterwalk_sg_next(sg);
-       }
-
-       if (sg)
-               scatterwalk_sg_chain(head, 2, sg);
-       else
-               sg_mark_end(head);
-}
-
 static void authenc_geniv_ahash_update_done(struct crypto_async_request *areq,
                                            int err)
 {
@@ -142,7 +134,7 @@ static void authenc_geniv_ahash_update_done(struct crypto_async_request *areq,
                                 crypto_aead_authsize(authenc), 1);
 
 out:
-       aead_request_complete(req, err);
+       authenc_request_complete(req, err);
 }
 
 static void authenc_geniv_ahash_done(struct crypto_async_request *areq, int err)
@@ -175,6 +167,7 @@ static void authenc_verify_ahash_update_done(struct crypto_async_request *areq,
        struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
        struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
        struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
+       unsigned int cryptlen = req->cryptlen;
 
        if (err)
                goto out;
@@ -190,6 +183,7 @@ static void authenc_verify_ahash_update_done(struct crypto_async_request *areq,
                goto out;
 
        authsize = crypto_aead_authsize(authenc);
+       cryptlen -= authsize;
        ihash = ahreq->result + authsize;
        scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
                                 authsize, 0);
@@ -203,12 +197,12 @@ static void authenc_verify_ahash_update_done(struct crypto_async_request *areq,
        ablkcipher_request_set_callback(abreq, aead_request_flags(req),
                                        req->base.complete, req->base.data);
        ablkcipher_request_set_crypt(abreq, req->src, req->dst,
-                                    req->cryptlen, req->iv);
+                                    cryptlen, req->iv);
 
        err = crypto_ablkcipher_decrypt(abreq);
 
 out:
-       aead_request_complete(req, err);
+       authenc_request_complete(req, err);
 }
 
 static void authenc_verify_ahash_done(struct crypto_async_request *areq,
@@ -222,11 +216,13 @@ static void authenc_verify_ahash_done(struct crypto_async_request *areq,
        struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
        struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
        struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
+       unsigned int cryptlen = req->cryptlen;
 
        if (err)
                goto out;
 
        authsize = crypto_aead_authsize(authenc);
+       cryptlen -= authsize;
        ihash = ahreq->result + authsize;
        scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
                                 authsize, 0);
@@ -240,12 +236,12 @@ static void authenc_verify_ahash_done(struct crypto_async_request *areq,
        ablkcipher_request_set_callback(abreq, aead_request_flags(req),
                                        req->base.complete, req->base.data);
        ablkcipher_request_set_crypt(abreq, req->src, req->dst,
-                                    req->cryptlen, req->iv);
+                                    cryptlen, req->iv);
 
        err = crypto_ablkcipher_decrypt(abreq);
 
 out:
-       aead_request_complete(req, err);
+       authenc_request_complete(req, err);
 }
 
 static u8 *crypto_authenc_ahash_fb(struct aead_request *req, unsigned int flags)
@@ -335,7 +331,7 @@ static int crypto_authenc_genicv(struct aead_request *req, u8 *iv,
        if (ivsize) {
                sg_init_table(cipher, 2);
                sg_set_buf(cipher, iv, ivsize);
-               authenc_chain(cipher, dst, vdst == iv + ivsize);
+               scatterwalk_crypto_chain(cipher, dst, vdst == iv + ivsize, 2);
                dst = cipher;
                cryptlen += ivsize;
        }
@@ -344,7 +340,7 @@ static int crypto_authenc_genicv(struct aead_request *req, u8 *iv,
                authenc_ahash_fn = crypto_authenc_ahash;
                sg_init_table(asg, 2);
                sg_set_page(asg, sg_page(assoc), assoc->length, assoc->offset);
-               authenc_chain(asg, dst, 0);
+               scatterwalk_crypto_chain(asg, dst, 0, 2);
                dst = asg;
                cryptlen += req->assoclen;
        }
@@ -379,18 +375,20 @@ static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
                err = crypto_authenc_genicv(areq, iv, 0);
        }
 
-       aead_request_complete(areq, err);
+       authenc_request_complete(areq, err);
 }
 
 static int crypto_authenc_encrypt(struct aead_request *req)
 {
        struct crypto_aead *authenc = crypto_aead_reqtfm(req);
        struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
-       struct ablkcipher_request *abreq = aead_request_ctx(req);
+       struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
        struct crypto_ablkcipher *enc = ctx->enc;
        struct scatterlist *dst = req->dst;
        unsigned int cryptlen = req->cryptlen;
-       u8 *iv = (u8 *)(abreq + 1) + crypto_ablkcipher_reqsize(enc);
+       struct ablkcipher_request *abreq = (void *)(areq_ctx->tail
+                                                   + ctx->reqoff);
+       u8 *iv = (u8 *)abreq - crypto_ablkcipher_ivsize(enc);
        int err;
 
        ablkcipher_request_set_tfm(abreq, enc);
@@ -418,7 +416,7 @@ static void crypto_authenc_givencrypt_done(struct crypto_async_request *req,
                err = crypto_authenc_genicv(areq, greq->giv, 0);
        }
 
-       aead_request_complete(areq, err);
+       authenc_request_complete(areq, err);
 }
 
 static int crypto_authenc_givencrypt(struct aead_givcrypt_request *req)
@@ -487,7 +485,7 @@ static int crypto_authenc_iverify(struct aead_request *req, u8 *iv,
        if (ivsize) {
                sg_init_table(cipher, 2);
                sg_set_buf(cipher, iv, ivsize);
-               authenc_chain(cipher, src, vsrc == iv + ivsize);
+               scatterwalk_crypto_chain(cipher, src, vsrc == iv + ivsize, 2);
                src = cipher;
                cryptlen += ivsize;
        }
@@ -496,7 +494,7 @@ static int crypto_authenc_iverify(struct aead_request *req, u8 *iv,
                authenc_ahash_fn = crypto_authenc_ahash;
                sg_init_table(asg, 2);
                sg_set_page(asg, sg_page(assoc), assoc->length, assoc->offset);
-               authenc_chain(asg, src, 0);
+               scatterwalk_crypto_chain(asg, src, 0, 2);
                src = asg;
                cryptlen += req->assoclen;
        }
@@ -546,10 +544,6 @@ static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
        if (IS_ERR(auth))
                return PTR_ERR(auth);
 
-       ctx->reqoff = ALIGN(2 * crypto_ahash_digestsize(auth) +
-                           crypto_ahash_alignmask(auth),
-                           crypto_ahash_alignmask(auth) + 1);
-
        enc = crypto_spawn_skcipher(&ictx->enc);
        err = PTR_ERR(enc);
        if (IS_ERR(enc))
@@ -558,13 +552,18 @@ static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
        ctx->auth = auth;
        ctx->enc = enc;
 
-       tfm->crt_aead.reqsize = max_t(unsigned int,
-                               crypto_ahash_reqsize(auth) + ctx->reqoff +
-                               sizeof(struct authenc_request_ctx) +
+       ctx->reqoff = ALIGN(2 * crypto_ahash_digestsize(auth) +
+                           crypto_ahash_alignmask(auth),
+                           crypto_ahash_alignmask(auth) + 1) +
+                     crypto_ablkcipher_ivsize(enc);
+
+       tfm->crt_aead.reqsize = sizeof(struct authenc_request_ctx) +
+                               ctx->reqoff +
+                               max_t(unsigned int,
+                               crypto_ahash_reqsize(auth) +
                                sizeof(struct ahash_request),
                                sizeof(struct skcipher_givcrypt_request) +
-                               crypto_ablkcipher_reqsize(enc) +
-                               crypto_ablkcipher_ivsize(enc));
+                               crypto_ablkcipher_reqsize(enc));
 
        return 0;
 
@@ -603,7 +602,7 @@ static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
        auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
                               CRYPTO_ALG_TYPE_AHASH_MASK);
        if (IS_ERR(auth))
-               return ERR_PTR(PTR_ERR(auth));
+               return ERR_CAST(auth);
 
        auth_base = &auth->base;