Back to home page

LXR

 
 

    


0001 /*
0002  * CTS: Cipher Text Stealing mode
0003  *
0004  * COPYRIGHT (c) 2008
0005  * The Regents of the University of Michigan
0006  * ALL RIGHTS RESERVED
0007  *
0008  * Permission is granted to use, copy, create derivative works
0009  * and redistribute this software and such derivative works
0010  * for any purpose, so long as the name of The University of
0011  * Michigan is not used in any advertising or publicity
0012  * pertaining to the use of distribution of this software
0013  * without specific, written prior authorization.  If the
0014  * above copyright notice or any other identification of the
0015  * University of Michigan is included in any copy of any
0016  * portion of this software, then the disclaimer below must
0017  * also be included.
0018  *
0019  * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
0020  * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
0021  * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF
0022  * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
0023  * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
0024  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
0025  * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
0026  * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
0027  * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
0028  * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
0029  * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
0030  * SUCH DAMAGES.
0031  */
0032 
0033 /* Derived from various:
0034  *  Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
0035  */
0036 
0037 /*
0038  * This is the Cipher Text Stealing mode as described by
0039  * Section 8 of rfc2040 and referenced by rfc3962.
0040  * rfc3962 includes errata information in its Appendix A.
0041  */
0042 
0043 #include <crypto/internal/skcipher.h>
0044 #include <linux/err.h>
0045 #include <linux/init.h>
0046 #include <linux/kernel.h>
0047 #include <linux/log2.h>
0048 #include <linux/module.h>
0049 #include <linux/scatterlist.h>
0050 #include <crypto/scatterwalk.h>
0051 #include <linux/slab.h>
0052 
0053 struct crypto_cts_ctx {
0054     struct crypto_skcipher *child;
0055 };
0056 
0057 struct crypto_cts_reqctx {
0058     struct scatterlist sg[2];
0059     unsigned offset;
0060     struct skcipher_request subreq;
0061 };
0062 
0063 static inline u8 *crypto_cts_reqctx_space(struct skcipher_request *req)
0064 {
0065     struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
0066     struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0067     struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
0068     struct crypto_skcipher *child = ctx->child;
0069 
0070     return PTR_ALIGN((u8 *)(rctx + 1) + crypto_skcipher_reqsize(child),
0071              crypto_skcipher_alignmask(tfm) + 1);
0072 }
0073 
0074 static int crypto_cts_setkey(struct crypto_skcipher *parent, const u8 *key,
0075                  unsigned int keylen)
0076 {
0077     struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(parent);
0078     struct crypto_skcipher *child = ctx->child;
0079     int err;
0080 
0081     crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
0082     crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
0083                      CRYPTO_TFM_REQ_MASK);
0084     err = crypto_skcipher_setkey(child, key, keylen);
0085     crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) &
0086                       CRYPTO_TFM_RES_MASK);
0087     return err;
0088 }
0089 
0090 static void cts_cbc_crypt_done(struct crypto_async_request *areq, int err)
0091 {
0092     struct skcipher_request *req = areq->data;
0093 
0094     if (err == -EINPROGRESS)
0095         return;
0096 
0097     skcipher_request_complete(req, err);
0098 }
0099 
0100 static int cts_cbc_encrypt(struct skcipher_request *req)
0101 {
0102     struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
0103     struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0104     struct skcipher_request *subreq = &rctx->subreq;
0105     int bsize = crypto_skcipher_blocksize(tfm);
0106     u8 d[bsize * 2] __attribute__ ((aligned(__alignof__(u32))));
0107     struct scatterlist *sg;
0108     unsigned int offset;
0109     int lastn;
0110 
0111     offset = rctx->offset;
0112     lastn = req->cryptlen - offset;
0113 
0114     sg = scatterwalk_ffwd(rctx->sg, req->dst, offset - bsize);
0115     scatterwalk_map_and_copy(d + bsize, sg, 0, bsize, 0);
0116 
0117     memset(d, 0, bsize);
0118     scatterwalk_map_and_copy(d, req->src, offset, lastn, 0);
0119 
0120     scatterwalk_map_and_copy(d, sg, 0, bsize + lastn, 1);
0121     memzero_explicit(d, sizeof(d));
0122 
0123     skcipher_request_set_callback(subreq, req->base.flags &
0124                           CRYPTO_TFM_REQ_MAY_BACKLOG,
0125                       cts_cbc_crypt_done, req);
0126     skcipher_request_set_crypt(subreq, sg, sg, bsize, req->iv);
0127     return crypto_skcipher_encrypt(subreq);
0128 }
0129 
0130 static void crypto_cts_encrypt_done(struct crypto_async_request *areq, int err)
0131 {
0132     struct skcipher_request *req = areq->data;
0133 
0134     if (err)
0135         goto out;
0136 
0137     err = cts_cbc_encrypt(req);
0138     if (err == -EINPROGRESS ||
0139         (err == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
0140         return;
0141 
0142 out:
0143     skcipher_request_complete(req, err);
0144 }
0145 
0146 static int crypto_cts_encrypt(struct skcipher_request *req)
0147 {
0148     struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0149     struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
0150     struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
0151     struct skcipher_request *subreq = &rctx->subreq;
0152     int bsize = crypto_skcipher_blocksize(tfm);
0153     unsigned int nbytes = req->cryptlen;
0154     int cbc_blocks = (nbytes + bsize - 1) / bsize - 1;
0155     unsigned int offset;
0156 
0157     skcipher_request_set_tfm(subreq, ctx->child);
0158 
0159     if (cbc_blocks <= 0) {
0160         skcipher_request_set_callback(subreq, req->base.flags,
0161                           req->base.complete,
0162                           req->base.data);
0163         skcipher_request_set_crypt(subreq, req->src, req->dst, nbytes,
0164                        req->iv);
0165         return crypto_skcipher_encrypt(subreq);
0166     }
0167 
0168     offset = cbc_blocks * bsize;
0169     rctx->offset = offset;
0170 
0171     skcipher_request_set_callback(subreq, req->base.flags,
0172                       crypto_cts_encrypt_done, req);
0173     skcipher_request_set_crypt(subreq, req->src, req->dst,
0174                    offset, req->iv);
0175 
0176     return crypto_skcipher_encrypt(subreq) ?:
0177            cts_cbc_encrypt(req);
0178 }
0179 
0180 static int cts_cbc_decrypt(struct skcipher_request *req)
0181 {
0182     struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
0183     struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0184     struct skcipher_request *subreq = &rctx->subreq;
0185     int bsize = crypto_skcipher_blocksize(tfm);
0186     u8 d[bsize * 2] __attribute__ ((aligned(__alignof__(u32))));
0187     struct scatterlist *sg;
0188     unsigned int offset;
0189     u8 *space;
0190     int lastn;
0191 
0192     offset = rctx->offset;
0193     lastn = req->cryptlen - offset;
0194 
0195     sg = scatterwalk_ffwd(rctx->sg, req->dst, offset - bsize);
0196 
0197     /* 1. Decrypt Cn-1 (s) to create Dn */
0198     scatterwalk_map_and_copy(d + bsize, sg, 0, bsize, 0);
0199     space = crypto_cts_reqctx_space(req);
0200     crypto_xor(d + bsize, space, bsize);
0201     /* 2. Pad Cn with zeros at the end to create C of length BB */
0202     memset(d, 0, bsize);
0203     scatterwalk_map_and_copy(d, req->src, offset, lastn, 0);
0204     /* 3. Exclusive-or Dn with C to create Xn */
0205     /* 4. Select the first Ln bytes of Xn to create Pn */
0206     crypto_xor(d + bsize, d, lastn);
0207 
0208     /* 5. Append the tail (BB - Ln) bytes of Xn to Cn to create En */
0209     memcpy(d + lastn, d + bsize + lastn, bsize - lastn);
0210     /* 6. Decrypt En to create Pn-1 */
0211 
0212     scatterwalk_map_and_copy(d, sg, 0, bsize + lastn, 1);
0213     memzero_explicit(d, sizeof(d));
0214 
0215     skcipher_request_set_callback(subreq, req->base.flags &
0216                           CRYPTO_TFM_REQ_MAY_BACKLOG,
0217                       cts_cbc_crypt_done, req);
0218 
0219     skcipher_request_set_crypt(subreq, sg, sg, bsize, space);
0220     return crypto_skcipher_decrypt(subreq);
0221 }
0222 
0223 static void crypto_cts_decrypt_done(struct crypto_async_request *areq, int err)
0224 {
0225     struct skcipher_request *req = areq->data;
0226 
0227     if (err)
0228         goto out;
0229 
0230     err = cts_cbc_decrypt(req);
0231     if (err == -EINPROGRESS ||
0232         (err == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
0233         return;
0234 
0235 out:
0236     skcipher_request_complete(req, err);
0237 }
0238 
0239 static int crypto_cts_decrypt(struct skcipher_request *req)
0240 {
0241     struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0242     struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
0243     struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
0244     struct skcipher_request *subreq = &rctx->subreq;
0245     int bsize = crypto_skcipher_blocksize(tfm);
0246     unsigned int nbytes = req->cryptlen;
0247     int cbc_blocks = (nbytes + bsize - 1) / bsize - 1;
0248     unsigned int offset;
0249     u8 *space;
0250 
0251     skcipher_request_set_tfm(subreq, ctx->child);
0252 
0253     if (cbc_blocks <= 0) {
0254         skcipher_request_set_callback(subreq, req->base.flags,
0255                           req->base.complete,
0256                           req->base.data);
0257         skcipher_request_set_crypt(subreq, req->src, req->dst, nbytes,
0258                        req->iv);
0259         return crypto_skcipher_decrypt(subreq);
0260     }
0261 
0262     skcipher_request_set_callback(subreq, req->base.flags,
0263                       crypto_cts_decrypt_done, req);
0264 
0265     space = crypto_cts_reqctx_space(req);
0266 
0267     offset = cbc_blocks * bsize;
0268     rctx->offset = offset;
0269 
0270     if (cbc_blocks <= 1)
0271         memcpy(space, req->iv, bsize);
0272     else
0273         scatterwalk_map_and_copy(space, req->src, offset - 2 * bsize,
0274                      bsize, 0);
0275 
0276     skcipher_request_set_crypt(subreq, req->src, req->dst,
0277                    offset, req->iv);
0278 
0279     return crypto_skcipher_decrypt(subreq) ?:
0280            cts_cbc_decrypt(req);
0281 }
0282 
0283 static int crypto_cts_init_tfm(struct crypto_skcipher *tfm)
0284 {
0285     struct skcipher_instance *inst = skcipher_alg_instance(tfm);
0286     struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
0287     struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
0288     struct crypto_skcipher *cipher;
0289     unsigned reqsize;
0290     unsigned bsize;
0291     unsigned align;
0292 
0293     cipher = crypto_spawn_skcipher(spawn);
0294     if (IS_ERR(cipher))
0295         return PTR_ERR(cipher);
0296 
0297     ctx->child = cipher;
0298 
0299     align = crypto_skcipher_alignmask(tfm);
0300     bsize = crypto_skcipher_blocksize(cipher);
0301     reqsize = ALIGN(sizeof(struct crypto_cts_reqctx) +
0302             crypto_skcipher_reqsize(cipher),
0303             crypto_tfm_ctx_alignment()) +
0304           (align & ~(crypto_tfm_ctx_alignment() - 1)) + bsize;
0305 
0306     crypto_skcipher_set_reqsize(tfm, reqsize);
0307 
0308     return 0;
0309 }
0310 
0311 static void crypto_cts_exit_tfm(struct crypto_skcipher *tfm)
0312 {
0313     struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
0314 
0315     crypto_free_skcipher(ctx->child);
0316 }
0317 
0318 static void crypto_cts_free(struct skcipher_instance *inst)
0319 {
0320     crypto_drop_skcipher(skcipher_instance_ctx(inst));
0321     kfree(inst);
0322 }
0323 
0324 static int crypto_cts_create(struct crypto_template *tmpl, struct rtattr **tb)
0325 {
0326     struct crypto_skcipher_spawn *spawn;
0327     struct skcipher_instance *inst;
0328     struct crypto_attr_type *algt;
0329     struct skcipher_alg *alg;
0330     const char *cipher_name;
0331     int err;
0332 
0333     algt = crypto_get_attr_type(tb);
0334     if (IS_ERR(algt))
0335         return PTR_ERR(algt);
0336 
0337     if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
0338         return -EINVAL;
0339 
0340     cipher_name = crypto_attr_alg_name(tb[1]);
0341     if (IS_ERR(cipher_name))
0342         return PTR_ERR(cipher_name);
0343 
0344     inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
0345     if (!inst)
0346         return -ENOMEM;
0347 
0348     spawn = skcipher_instance_ctx(inst);
0349 
0350     crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst));
0351     err = crypto_grab_skcipher(spawn, cipher_name, 0,
0352                    crypto_requires_sync(algt->type,
0353                             algt->mask));
0354     if (err)
0355         goto err_free_inst;
0356 
0357     alg = crypto_spawn_skcipher_alg(spawn);
0358 
0359     err = -EINVAL;
0360     if (crypto_skcipher_alg_ivsize(alg) != alg->base.cra_blocksize)
0361         goto err_drop_spawn;
0362 
0363     if (strncmp(alg->base.cra_name, "cbc(", 4))
0364         goto err_drop_spawn;
0365 
0366     err = crypto_inst_setname(skcipher_crypto_instance(inst), "cts",
0367                   &alg->base);
0368     if (err)
0369         goto err_drop_spawn;
0370 
0371     inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
0372     inst->alg.base.cra_priority = alg->base.cra_priority;
0373     inst->alg.base.cra_blocksize = alg->base.cra_blocksize;
0374     inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
0375 
0376     /* We access the data as u32s when xoring. */
0377     inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
0378 
0379     inst->alg.ivsize = alg->base.cra_blocksize;
0380     inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
0381     inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg);
0382     inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg);
0383 
0384     inst->alg.base.cra_ctxsize = sizeof(struct crypto_cts_ctx);
0385 
0386     inst->alg.init = crypto_cts_init_tfm;
0387     inst->alg.exit = crypto_cts_exit_tfm;
0388 
0389     inst->alg.setkey = crypto_cts_setkey;
0390     inst->alg.encrypt = crypto_cts_encrypt;
0391     inst->alg.decrypt = crypto_cts_decrypt;
0392 
0393     inst->free = crypto_cts_free;
0394 
0395     err = skcipher_register_instance(tmpl, inst);
0396     if (err)
0397         goto err_drop_spawn;
0398 
0399 out:
0400     return err;
0401 
0402 err_drop_spawn:
0403     crypto_drop_skcipher(spawn);
0404 err_free_inst:
0405     kfree(inst);
0406     goto out;
0407 }
0408 
0409 static struct crypto_template crypto_cts_tmpl = {
0410     .name = "cts",
0411     .create = crypto_cts_create,
0412     .module = THIS_MODULE,
0413 };
0414 
0415 static int __init crypto_cts_module_init(void)
0416 {
0417     return crypto_register_template(&crypto_cts_tmpl);
0418 }
0419 
0420 static void __exit crypto_cts_module_exit(void)
0421 {
0422     crypto_unregister_template(&crypto_cts_tmpl);
0423 }
0424 
0425 module_init(crypto_cts_module_init);
0426 module_exit(crypto_cts_module_exit);
0427 
0428 MODULE_LICENSE("Dual BSD/GPL");
0429 MODULE_DESCRIPTION("CTS-CBC CipherText Stealing for CBC");
0430 MODULE_ALIAS_CRYPTO("cts");