]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - crypto/cts.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152
[linux.git] / crypto / cts.c
index 4e28d83ae37da025b5eef7f9506df6bf5bdb9ca1..6b6087dbb62a21b53be48a2e3b6d1ae88b1c1a0c 100644 (file)
@@ -152,12 +152,14 @@ static int crypto_cts_encrypt(struct skcipher_request *req)
        struct skcipher_request *subreq = &rctx->subreq;
        int bsize = crypto_skcipher_blocksize(tfm);
        unsigned int nbytes = req->cryptlen;
-       int cbc_blocks = (nbytes + bsize - 1) / bsize - 1;
        unsigned int offset;
 
        skcipher_request_set_tfm(subreq, ctx->child);
 
-       if (cbc_blocks <= 0) {
+       if (nbytes < bsize)
+               return -EINVAL;
+
+       if (nbytes == bsize) {
                skcipher_request_set_callback(subreq, req->base.flags,
                                              req->base.complete,
                                              req->base.data);
@@ -166,7 +168,7 @@ static int crypto_cts_encrypt(struct skcipher_request *req)
                return crypto_skcipher_encrypt(subreq);
        }
 
-       offset = cbc_blocks * bsize;
+       offset = rounddown(nbytes - 1, bsize);
        rctx->offset = offset;
 
        skcipher_request_set_callback(subreq, req->base.flags,
@@ -244,13 +246,15 @@ static int crypto_cts_decrypt(struct skcipher_request *req)
        struct skcipher_request *subreq = &rctx->subreq;
        int bsize = crypto_skcipher_blocksize(tfm);
        unsigned int nbytes = req->cryptlen;
-       int cbc_blocks = (nbytes + bsize - 1) / bsize - 1;
        unsigned int offset;
        u8 *space;
 
        skcipher_request_set_tfm(subreq, ctx->child);
 
-       if (cbc_blocks <= 0) {
+       if (nbytes < bsize)
+               return -EINVAL;
+
+       if (nbytes == bsize) {
                skcipher_request_set_callback(subreq, req->base.flags,
                                              req->base.complete,
                                              req->base.data);
@@ -264,10 +268,10 @@ static int crypto_cts_decrypt(struct skcipher_request *req)
 
        space = crypto_cts_reqctx_space(req);
 
-       offset = cbc_blocks * bsize;
+       offset = rounddown(nbytes - 1, bsize);
        rctx->offset = offset;
 
-       if (cbc_blocks <= 1)
+       if (offset <= bsize)
                memcpy(space, req->iv, bsize);
        else
                scatterwalk_map_and_copy(space, req->src, offset - 2 * bsize,
@@ -419,7 +423,7 @@ static void __exit crypto_cts_module_exit(void)
        crypto_unregister_template(&crypto_cts_tmpl);
 }
 
-module_init(crypto_cts_module_init);
+subsys_initcall(crypto_cts_module_init);
 module_exit(crypto_cts_module_exit);
 
 MODULE_LICENSE("Dual BSD/GPL");