2008-03-24 16:26:16 +03:00
|
|
|
/*
|
|
|
|
* CTS: Cipher Text Stealing mode
|
|
|
|
*
|
|
|
|
* COPYRIGHT (c) 2008
|
|
|
|
* The Regents of the University of Michigan
|
|
|
|
* ALL RIGHTS RESERVED
|
|
|
|
*
|
|
|
|
* Permission is granted to use, copy, create derivative works
|
|
|
|
* and redistribute this software and such derivative works
|
|
|
|
* for any purpose, so long as the name of The University of
|
|
|
|
* Michigan is not used in any advertising or publicity
|
|
|
|
* pertaining to the use of distribution of this software
|
|
|
|
* without specific, written prior authorization. If the
|
|
|
|
* above copyright notice or any other identification of the
|
|
|
|
* University of Michigan is included in any copy of any
|
|
|
|
* portion of this software, then the disclaimer below must
|
|
|
|
* also be included.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
|
|
|
|
* FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
|
|
|
|
* PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF
|
|
|
|
* MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
|
|
|
|
* WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
|
|
|
* REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
|
|
|
|
* FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
|
|
|
|
* IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGES.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Derived from various:
|
|
|
|
* Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the Cipher Text Stealing mode as described by
|
|
|
|
* Section 8 of rfc2040 and referenced by rfc3962.
|
|
|
|
* rfc3962 includes errata information in its Appendix A.
|
|
|
|
*/
|
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
#include <crypto/internal/skcipher.h>
|
2008-03-24 16:26:16 +03:00
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/log2.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/scatterlist.h>
|
|
|
|
#include <crypto/scatterwalk.h>
|
|
|
|
#include <linux/slab.h>
|
2016-12-31 18:56:23 +03:00
|
|
|
#include <linux/compiler.h>
|
2008-03-24 16:26:16 +03:00
|
|
|
|
|
|
|
struct crypto_cts_ctx {
|
2016-07-12 08:17:48 +03:00
|
|
|
struct crypto_skcipher *child;
|
2008-03-24 16:26:16 +03:00
|
|
|
};
|
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
struct crypto_cts_reqctx {
|
|
|
|
struct scatterlist sg[2];
|
|
|
|
unsigned offset;
|
|
|
|
struct skcipher_request subreq;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline u8 *crypto_cts_reqctx_space(struct skcipher_request *req)
|
2008-03-24 16:26:16 +03:00
|
|
|
{
|
2016-07-12 08:17:48 +03:00
|
|
|
struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
|
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
|
|
|
struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
|
|
|
|
struct crypto_skcipher *child = ctx->child;
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
return PTR_ALIGN((u8 *)(rctx + 1) + crypto_skcipher_reqsize(child),
|
|
|
|
crypto_skcipher_alignmask(tfm) + 1);
|
2008-03-24 16:26:16 +03:00
|
|
|
}
|
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
static int crypto_cts_setkey(struct crypto_skcipher *parent, const u8 *key,
|
|
|
|
unsigned int keylen)
|
2008-03-24 16:26:16 +03:00
|
|
|
{
|
2016-07-12 08:17:48 +03:00
|
|
|
struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(parent);
|
|
|
|
struct crypto_skcipher *child = ctx->child;
|
2008-03-24 16:26:16 +03:00
|
|
|
int err;
|
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
|
|
|
|
crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
|
|
|
|
CRYPTO_TFM_REQ_MASK);
|
|
|
|
err = crypto_skcipher_setkey(child, key, keylen);
|
|
|
|
crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) &
|
|
|
|
CRYPTO_TFM_RES_MASK);
|
|
|
|
return err;
|
|
|
|
}
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
static void cts_cbc_crypt_done(struct crypto_async_request *areq, int err)
|
|
|
|
{
|
|
|
|
struct skcipher_request *req = areq->data;
|
2008-06-02 09:46:51 +04:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
if (err == -EINPROGRESS)
|
|
|
|
return;
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
skcipher_request_complete(req, err);
|
|
|
|
}
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
static int cts_cbc_encrypt(struct skcipher_request *req)
|
|
|
|
{
|
|
|
|
struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
|
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
|
|
|
struct skcipher_request *subreq = &rctx->subreq;
|
|
|
|
int bsize = crypto_skcipher_blocksize(tfm);
|
2016-12-31 18:56:23 +03:00
|
|
|
u8 d[bsize * 2] __aligned(__alignof__(u32));
|
2016-07-12 08:17:48 +03:00
|
|
|
struct scatterlist *sg;
|
|
|
|
unsigned int offset;
|
|
|
|
int lastn;
|
|
|
|
|
|
|
|
offset = rctx->offset;
|
|
|
|
lastn = req->cryptlen - offset;
|
|
|
|
|
|
|
|
sg = scatterwalk_ffwd(rctx->sg, req->dst, offset - bsize);
|
|
|
|
scatterwalk_map_and_copy(d + bsize, sg, 0, bsize, 0);
|
|
|
|
|
|
|
|
memset(d, 0, bsize);
|
|
|
|
scatterwalk_map_and_copy(d, req->src, offset, lastn, 0);
|
|
|
|
|
|
|
|
scatterwalk_map_and_copy(d, sg, 0, bsize + lastn, 1);
|
|
|
|
memzero_explicit(d, sizeof(d));
|
|
|
|
|
|
|
|
skcipher_request_set_callback(subreq, req->base.flags &
|
|
|
|
CRYPTO_TFM_REQ_MAY_BACKLOG,
|
|
|
|
cts_cbc_crypt_done, req);
|
|
|
|
skcipher_request_set_crypt(subreq, sg, sg, bsize, req->iv);
|
|
|
|
return crypto_skcipher_encrypt(subreq);
|
|
|
|
}
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
static void crypto_cts_encrypt_done(struct crypto_async_request *areq, int err)
|
|
|
|
{
|
|
|
|
struct skcipher_request *req = areq->data;
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
if (err)
|
|
|
|
goto out;
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
err = cts_cbc_encrypt(req);
|
2017-10-18 10:00:36 +03:00
|
|
|
if (err == -EINPROGRESS || err == -EBUSY)
|
2016-07-12 08:17:48 +03:00
|
|
|
return;
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
out:
|
|
|
|
skcipher_request_complete(req, err);
|
|
|
|
}
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
static int crypto_cts_encrypt(struct skcipher_request *req)
|
|
|
|
{
|
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
|
|
|
struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
|
|
|
|
struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
|
|
|
|
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) {
|
|
|
|
skcipher_request_set_callback(subreq, req->base.flags,
|
|
|
|
req->base.complete,
|
|
|
|
req->base.data);
|
|
|
|
skcipher_request_set_crypt(subreq, req->src, req->dst, nbytes,
|
|
|
|
req->iv);
|
|
|
|
return crypto_skcipher_encrypt(subreq);
|
|
|
|
}
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
offset = cbc_blocks * bsize;
|
|
|
|
rctx->offset = offset;
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
skcipher_request_set_callback(subreq, req->base.flags,
|
|
|
|
crypto_cts_encrypt_done, req);
|
|
|
|
skcipher_request_set_crypt(subreq, req->src, req->dst,
|
|
|
|
offset, req->iv);
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
return crypto_skcipher_encrypt(subreq) ?:
|
|
|
|
cts_cbc_encrypt(req);
|
2008-03-24 16:26:16 +03:00
|
|
|
}
|
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
static int cts_cbc_decrypt(struct skcipher_request *req)
|
2008-03-24 16:26:16 +03:00
|
|
|
{
|
2016-07-12 08:17:48 +03:00
|
|
|
struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
|
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
|
|
|
struct skcipher_request *subreq = &rctx->subreq;
|
|
|
|
int bsize = crypto_skcipher_blocksize(tfm);
|
2016-12-31 18:56:23 +03:00
|
|
|
u8 d[bsize * 2] __aligned(__alignof__(u32));
|
2016-07-12 08:17:48 +03:00
|
|
|
struct scatterlist *sg;
|
|
|
|
unsigned int offset;
|
|
|
|
u8 *space;
|
|
|
|
int lastn;
|
|
|
|
|
|
|
|
offset = rctx->offset;
|
|
|
|
lastn = req->cryptlen - offset;
|
|
|
|
|
|
|
|
sg = scatterwalk_ffwd(rctx->sg, req->dst, offset - bsize);
|
|
|
|
|
|
|
|
/* 1. Decrypt Cn-1 (s) to create Dn */
|
|
|
|
scatterwalk_map_and_copy(d + bsize, sg, 0, bsize, 0);
|
|
|
|
space = crypto_cts_reqctx_space(req);
|
|
|
|
crypto_xor(d + bsize, space, bsize);
|
|
|
|
/* 2. Pad Cn with zeros at the end to create C of length BB */
|
|
|
|
memset(d, 0, bsize);
|
|
|
|
scatterwalk_map_and_copy(d, req->src, offset, lastn, 0);
|
|
|
|
/* 3. Exclusive-or Dn with C to create Xn */
|
|
|
|
/* 4. Select the first Ln bytes of Xn to create Pn */
|
|
|
|
crypto_xor(d + bsize, d, lastn);
|
|
|
|
|
|
|
|
/* 5. Append the tail (BB - Ln) bytes of Xn to Cn to create En */
|
|
|
|
memcpy(d + lastn, d + bsize + lastn, bsize - lastn);
|
|
|
|
/* 6. Decrypt En to create Pn-1 */
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
scatterwalk_map_and_copy(d, sg, 0, bsize + lastn, 1);
|
|
|
|
memzero_explicit(d, sizeof(d));
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
skcipher_request_set_callback(subreq, req->base.flags &
|
|
|
|
CRYPTO_TFM_REQ_MAY_BACKLOG,
|
|
|
|
cts_cbc_crypt_done, req);
|
|
|
|
|
|
|
|
skcipher_request_set_crypt(subreq, sg, sg, bsize, space);
|
|
|
|
return crypto_skcipher_decrypt(subreq);
|
2008-03-24 16:26:16 +03:00
|
|
|
}
|
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
static void crypto_cts_decrypt_done(struct crypto_async_request *areq, int err)
|
2008-03-24 16:26:16 +03:00
|
|
|
{
|
2016-07-12 08:17:48 +03:00
|
|
|
struct skcipher_request *req = areq->data;
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
if (err)
|
|
|
|
goto out;
|
2008-06-02 09:46:51 +04:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
err = cts_cbc_decrypt(req);
|
2017-10-18 10:00:36 +03:00
|
|
|
if (err == -EINPROGRESS || err == -EBUSY)
|
2016-07-12 08:17:48 +03:00
|
|
|
return;
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
out:
|
|
|
|
skcipher_request_complete(req, err);
|
|
|
|
}
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
static int crypto_cts_decrypt(struct skcipher_request *req)
|
|
|
|
{
|
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
|
|
|
struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
|
|
|
|
struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
|
|
|
|
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) {
|
|
|
|
skcipher_request_set_callback(subreq, req->base.flags,
|
|
|
|
req->base.complete,
|
|
|
|
req->base.data);
|
|
|
|
skcipher_request_set_crypt(subreq, req->src, req->dst, nbytes,
|
|
|
|
req->iv);
|
|
|
|
return crypto_skcipher_decrypt(subreq);
|
|
|
|
}
|
crypto: memzero_explicit - make sure to clear out sensitive data
Recently, in commit 13aa93c70e71 ("random: add and use memzero_explicit()
for clearing data"), we have found that GCC may optimize some memset()
cases away when it detects a stack variable is not being used anymore
and going out of scope. This can happen, for example, in cases when we
are clearing out sensitive information such as keying material or any
e.g. intermediate results from crypto computations, etc.
With the help of Coccinelle, we can figure out and fix such occurences
in the crypto subsytem as well. Julia Lawall provided the following
Coccinelle program:
@@
type T;
identifier x;
@@
T x;
... when exists
when any
-memset
+memzero_explicit
(&x,
-0,
...)
... when != x
when strict
@@
type T;
identifier x;
@@
T x[...];
... when exists
when any
-memset
+memzero_explicit
(x,
-0,
...)
... when != x
when strict
Therefore, make use of the drop-in replacement memzero_explicit() for
exactly such cases instead of using memset().
Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
Cc: Julia Lawall <julia.lawall@lip6.fr>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Theodore Ts'o <tytso@mit.edu>
Cc: Hannes Frederic Sowa <hannes@stressinduktion.org>
Acked-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
Acked-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2014-09-08 01:23:38 +04:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
skcipher_request_set_callback(subreq, req->base.flags,
|
|
|
|
crypto_cts_decrypt_done, req);
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
space = crypto_cts_reqctx_space(req);
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
offset = cbc_blocks * bsize;
|
|
|
|
rctx->offset = offset;
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
if (cbc_blocks <= 1)
|
|
|
|
memcpy(space, req->iv, bsize);
|
|
|
|
else
|
|
|
|
scatterwalk_map_and_copy(space, req->src, offset - 2 * bsize,
|
|
|
|
bsize, 0);
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
skcipher_request_set_crypt(subreq, req->src, req->dst,
|
|
|
|
offset, req->iv);
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
return crypto_skcipher_decrypt(subreq) ?:
|
|
|
|
cts_cbc_decrypt(req);
|
2008-03-24 16:26:16 +03:00
|
|
|
}
|
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
static int crypto_cts_init_tfm(struct crypto_skcipher *tfm)
|
2008-03-24 16:26:16 +03:00
|
|
|
{
|
2016-07-12 08:17:48 +03:00
|
|
|
struct skcipher_instance *inst = skcipher_alg_instance(tfm);
|
|
|
|
struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
|
|
|
|
struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
|
|
|
|
struct crypto_skcipher *cipher;
|
|
|
|
unsigned reqsize;
|
|
|
|
unsigned bsize;
|
|
|
|
unsigned align;
|
|
|
|
|
2016-10-28 19:52:19 +03:00
|
|
|
cipher = crypto_spawn_skcipher(spawn);
|
2008-03-24 16:26:16 +03:00
|
|
|
if (IS_ERR(cipher))
|
|
|
|
return PTR_ERR(cipher);
|
|
|
|
|
|
|
|
ctx->child = cipher;
|
2016-07-12 08:17:48 +03:00
|
|
|
|
|
|
|
align = crypto_skcipher_alignmask(tfm);
|
|
|
|
bsize = crypto_skcipher_blocksize(cipher);
|
|
|
|
reqsize = ALIGN(sizeof(struct crypto_cts_reqctx) +
|
|
|
|
crypto_skcipher_reqsize(cipher),
|
|
|
|
crypto_tfm_ctx_alignment()) +
|
|
|
|
(align & ~(crypto_tfm_ctx_alignment() - 1)) + bsize;
|
|
|
|
|
|
|
|
crypto_skcipher_set_reqsize(tfm, reqsize);
|
|
|
|
|
2008-03-24 16:26:16 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
static void crypto_cts_exit_tfm(struct crypto_skcipher *tfm)
|
2008-03-24 16:26:16 +03:00
|
|
|
{
|
2016-07-12 08:17:48 +03:00
|
|
|
struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
|
|
|
|
|
|
|
|
crypto_free_skcipher(ctx->child);
|
2008-03-24 16:26:16 +03:00
|
|
|
}
|
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
static void crypto_cts_free(struct skcipher_instance *inst)
|
2008-03-24 16:26:16 +03:00
|
|
|
{
|
2016-07-12 08:17:48 +03:00
|
|
|
crypto_drop_skcipher(skcipher_instance_ctx(inst));
|
|
|
|
kfree(inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int crypto_cts_create(struct crypto_template *tmpl, struct rtattr **tb)
|
|
|
|
{
|
|
|
|
struct crypto_skcipher_spawn *spawn;
|
|
|
|
struct skcipher_instance *inst;
|
|
|
|
struct crypto_attr_type *algt;
|
|
|
|
struct skcipher_alg *alg;
|
|
|
|
const char *cipher_name;
|
2008-03-24 16:26:16 +03:00
|
|
|
int err;
|
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
algt = crypto_get_attr_type(tb);
|
|
|
|
if (IS_ERR(algt))
|
|
|
|
return PTR_ERR(algt);
|
|
|
|
|
|
|
|
if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
cipher_name = crypto_attr_alg_name(tb[1]);
|
|
|
|
if (IS_ERR(cipher_name))
|
|
|
|
return PTR_ERR(cipher_name);
|
|
|
|
|
|
|
|
inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
|
|
|
|
if (!inst)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
spawn = skcipher_instance_ctx(inst);
|
|
|
|
|
|
|
|
crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst));
|
2016-10-28 19:51:13 +03:00
|
|
|
err = crypto_grab_skcipher(spawn, cipher_name, 0,
|
|
|
|
crypto_requires_sync(algt->type,
|
|
|
|
algt->mask));
|
2008-03-24 16:26:16 +03:00
|
|
|
if (err)
|
2016-07-12 08:17:48 +03:00
|
|
|
goto err_free_inst;
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
alg = crypto_spawn_skcipher_alg(spawn);
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
err = -EINVAL;
|
|
|
|
if (crypto_skcipher_alg_ivsize(alg) != alg->base.cra_blocksize)
|
|
|
|
goto err_drop_spawn;
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
if (strncmp(alg->base.cra_name, "cbc(", 4))
|
|
|
|
goto err_drop_spawn;
|
2015-01-16 11:38:17 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
err = crypto_inst_setname(skcipher_crypto_instance(inst), "cts",
|
|
|
|
&alg->base);
|
|
|
|
if (err)
|
|
|
|
goto err_drop_spawn;
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
|
|
|
|
inst->alg.base.cra_priority = alg->base.cra_priority;
|
|
|
|
inst->alg.base.cra_blocksize = alg->base.cra_blocksize;
|
|
|
|
inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
inst->alg.ivsize = alg->base.cra_blocksize;
|
|
|
|
inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
|
|
|
|
inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg);
|
|
|
|
inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg);
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
inst->alg.base.cra_ctxsize = sizeof(struct crypto_cts_ctx);
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
inst->alg.init = crypto_cts_init_tfm;
|
|
|
|
inst->alg.exit = crypto_cts_exit_tfm;
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
inst->alg.setkey = crypto_cts_setkey;
|
|
|
|
inst->alg.encrypt = crypto_cts_encrypt;
|
|
|
|
inst->alg.decrypt = crypto_cts_decrypt;
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
inst->free = crypto_cts_free;
|
2008-03-24 16:26:16 +03:00
|
|
|
|
2016-07-12 08:17:48 +03:00
|
|
|
err = skcipher_register_instance(tmpl, inst);
|
|
|
|
if (err)
|
|
|
|
goto err_drop_spawn;
|
|
|
|
|
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
|
|
|
|
err_drop_spawn:
|
|
|
|
crypto_drop_skcipher(spawn);
|
|
|
|
err_free_inst:
|
2008-03-24 16:26:16 +03:00
|
|
|
kfree(inst);
|
2016-07-12 08:17:48 +03:00
|
|
|
goto out;
|
2008-03-24 16:26:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct crypto_template crypto_cts_tmpl = {
|
|
|
|
.name = "cts",
|
2016-07-12 08:17:48 +03:00
|
|
|
.create = crypto_cts_create,
|
2008-03-24 16:26:16 +03:00
|
|
|
.module = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init crypto_cts_module_init(void)
|
|
|
|
{
|
|
|
|
return crypto_register_template(&crypto_cts_tmpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit crypto_cts_module_exit(void)
|
|
|
|
{
|
|
|
|
crypto_unregister_template(&crypto_cts_tmpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(crypto_cts_module_init);
|
|
|
|
module_exit(crypto_cts_module_exit);
|
|
|
|
|
|
|
|
MODULE_LICENSE("Dual BSD/GPL");
|
|
|
|
MODULE_DESCRIPTION("CTS-CBC CipherText Stealing for CBC");
|
2014-11-25 03:32:38 +03:00
|
|
|
MODULE_ALIAS_CRYPTO("cts");
|