Back to home page

LXR

 
 

    


0001 /*
0002  * CCM: Counter with CBC-MAC
0003  *
0004  * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
0005  *
0006  * This program is free software; you can redistribute it and/or modify it
0007  * under the terms of the GNU General Public License as published by the Free
0008  * Software Foundation; either version 2 of the License, or (at your option)
0009  * any later version.
0010  *
0011  */
0012 
0013 #include <crypto/internal/aead.h>
0014 #include <crypto/internal/skcipher.h>
0015 #include <crypto/scatterwalk.h>
0016 #include <linux/err.h>
0017 #include <linux/init.h>
0018 #include <linux/kernel.h>
0019 #include <linux/module.h>
0020 #include <linux/slab.h>
0021 
0022 #include "internal.h"
0023 
0024 struct ccm_instance_ctx {
0025     struct crypto_skcipher_spawn ctr;
0026     struct crypto_spawn cipher;
0027 };
0028 
0029 struct crypto_ccm_ctx {
0030     struct crypto_cipher *cipher;
0031     struct crypto_skcipher *ctr;
0032 };
0033 
0034 struct crypto_rfc4309_ctx {
0035     struct crypto_aead *child;
0036     u8 nonce[3];
0037 };
0038 
0039 struct crypto_rfc4309_req_ctx {
0040     struct scatterlist src[3];
0041     struct scatterlist dst[3];
0042     struct aead_request subreq;
0043 };
0044 
0045 struct crypto_ccm_req_priv_ctx {
0046     u8 odata[16];
0047     u8 idata[16];
0048     u8 auth_tag[16];
0049     u32 ilen;
0050     u32 flags;
0051     struct scatterlist src[3];
0052     struct scatterlist dst[3];
0053     struct skcipher_request skreq;
0054 };
0055 
0056 static inline struct crypto_ccm_req_priv_ctx *crypto_ccm_reqctx(
0057     struct aead_request *req)
0058 {
0059     unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
0060 
0061     return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
0062 }
0063 
0064 static int set_msg_len(u8 *block, unsigned int msglen, int csize)
0065 {
0066     __be32 data;
0067 
0068     memset(block, 0, csize);
0069     block += csize;
0070 
0071     if (csize >= 4)
0072         csize = 4;
0073     else if (msglen > (1 << (8 * csize)))
0074         return -EOVERFLOW;
0075 
0076     data = cpu_to_be32(msglen);
0077     memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
0078 
0079     return 0;
0080 }
0081 
0082 static int crypto_ccm_setkey(struct crypto_aead *aead, const u8 *key,
0083                  unsigned int keylen)
0084 {
0085     struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
0086     struct crypto_skcipher *ctr = ctx->ctr;
0087     struct crypto_cipher *tfm = ctx->cipher;
0088     int err = 0;
0089 
0090     crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
0091     crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
0092                        CRYPTO_TFM_REQ_MASK);
0093     err = crypto_skcipher_setkey(ctr, key, keylen);
0094     crypto_aead_set_flags(aead, crypto_skcipher_get_flags(ctr) &
0095                   CRYPTO_TFM_RES_MASK);
0096     if (err)
0097         goto out;
0098 
0099     crypto_cipher_clear_flags(tfm, CRYPTO_TFM_REQ_MASK);
0100     crypto_cipher_set_flags(tfm, crypto_aead_get_flags(aead) &
0101                     CRYPTO_TFM_REQ_MASK);
0102     err = crypto_cipher_setkey(tfm, key, keylen);
0103     crypto_aead_set_flags(aead, crypto_cipher_get_flags(tfm) &
0104                   CRYPTO_TFM_RES_MASK);
0105 
0106 out:
0107     return err;
0108 }
0109 
0110 static int crypto_ccm_setauthsize(struct crypto_aead *tfm,
0111                   unsigned int authsize)
0112 {
0113     switch (authsize) {
0114     case 4:
0115     case 6:
0116     case 8:
0117     case 10:
0118     case 12:
0119     case 14:
0120     case 16:
0121         break;
0122     default:
0123         return -EINVAL;
0124     }
0125 
0126     return 0;
0127 }
0128 
0129 static int format_input(u8 *info, struct aead_request *req,
0130             unsigned int cryptlen)
0131 {
0132     struct crypto_aead *aead = crypto_aead_reqtfm(req);
0133     unsigned int lp = req->iv[0];
0134     unsigned int l = lp + 1;
0135     unsigned int m;
0136 
0137     m = crypto_aead_authsize(aead);
0138 
0139     memcpy(info, req->iv, 16);
0140 
0141     /* format control info per RFC 3610 and
0142      * NIST Special Publication 800-38C
0143      */
0144     *info |= (8 * ((m - 2) / 2));
0145     if (req->assoclen)
0146         *info |= 64;
0147 
0148     return set_msg_len(info + 16 - l, cryptlen, l);
0149 }
0150 
0151 static int format_adata(u8 *adata, unsigned int a)
0152 {
0153     int len = 0;
0154 
0155     /* add control info for associated data
0156      * RFC 3610 and NIST Special Publication 800-38C
0157      */
0158     if (a < 65280) {
0159         *(__be16 *)adata = cpu_to_be16(a);
0160         len = 2;
0161     } else  {
0162         *(__be16 *)adata = cpu_to_be16(0xfffe);
0163         *(__be32 *)&adata[2] = cpu_to_be32(a);
0164         len = 6;
0165     }
0166 
0167     return len;
0168 }
0169 
0170 static void compute_mac(struct crypto_cipher *tfm, u8 *data, int n,
0171                struct crypto_ccm_req_priv_ctx *pctx)
0172 {
0173     unsigned int bs = 16;
0174     u8 *odata = pctx->odata;
0175     u8 *idata = pctx->idata;
0176     int datalen, getlen;
0177 
0178     datalen = n;
0179 
0180     /* first time in here, block may be partially filled. */
0181     getlen = bs - pctx->ilen;
0182     if (datalen >= getlen) {
0183         memcpy(idata + pctx->ilen, data, getlen);
0184         crypto_xor(odata, idata, bs);
0185         crypto_cipher_encrypt_one(tfm, odata, odata);
0186         datalen -= getlen;
0187         data += getlen;
0188         pctx->ilen = 0;
0189     }
0190 
0191     /* now encrypt rest of data */
0192     while (datalen >= bs) {
0193         crypto_xor(odata, data, bs);
0194         crypto_cipher_encrypt_one(tfm, odata, odata);
0195 
0196         datalen -= bs;
0197         data += bs;
0198     }
0199 
0200     /* check and see if there's leftover data that wasn't
0201      * enough to fill a block.
0202      */
0203     if (datalen) {
0204         memcpy(idata + pctx->ilen, data, datalen);
0205         pctx->ilen += datalen;
0206     }
0207 }
0208 
0209 static void get_data_to_compute(struct crypto_cipher *tfm,
0210                    struct crypto_ccm_req_priv_ctx *pctx,
0211                    struct scatterlist *sg, unsigned int len)
0212 {
0213     struct scatter_walk walk;
0214     u8 *data_src;
0215     int n;
0216 
0217     scatterwalk_start(&walk, sg);
0218 
0219     while (len) {
0220         n = scatterwalk_clamp(&walk, len);
0221         if (!n) {
0222             scatterwalk_start(&walk, sg_next(walk.sg));
0223             n = scatterwalk_clamp(&walk, len);
0224         }
0225         data_src = scatterwalk_map(&walk);
0226 
0227         compute_mac(tfm, data_src, n, pctx);
0228         len -= n;
0229 
0230         scatterwalk_unmap(data_src);
0231         scatterwalk_advance(&walk, n);
0232         scatterwalk_done(&walk, 0, len);
0233         if (len)
0234             crypto_yield(pctx->flags);
0235     }
0236 
0237     /* any leftover needs padding and then encrypted */
0238     if (pctx->ilen) {
0239         int padlen;
0240         u8 *odata = pctx->odata;
0241         u8 *idata = pctx->idata;
0242 
0243         padlen = 16 - pctx->ilen;
0244         memset(idata + pctx->ilen, 0, padlen);
0245         crypto_xor(odata, idata, 16);
0246         crypto_cipher_encrypt_one(tfm, odata, odata);
0247         pctx->ilen = 0;
0248     }
0249 }
0250 
0251 static int crypto_ccm_auth(struct aead_request *req, struct scatterlist *plain,
0252                unsigned int cryptlen)
0253 {
0254     struct crypto_aead *aead = crypto_aead_reqtfm(req);
0255     struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
0256     struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
0257     struct crypto_cipher *cipher = ctx->cipher;
0258     unsigned int assoclen = req->assoclen;
0259     u8 *odata = pctx->odata;
0260     u8 *idata = pctx->idata;
0261     int err;
0262 
0263     /* format control data for input */
0264     err = format_input(odata, req, cryptlen);
0265     if (err)
0266         goto out;
0267 
0268     /* encrypt first block to use as start in computing mac  */
0269     crypto_cipher_encrypt_one(cipher, odata, odata);
0270 
0271     /* format associated data and compute into mac */
0272     if (assoclen) {
0273         pctx->ilen = format_adata(idata, assoclen);
0274         get_data_to_compute(cipher, pctx, req->src, req->assoclen);
0275     } else {
0276         pctx->ilen = 0;
0277     }
0278 
0279     /* compute plaintext into mac */
0280     if (cryptlen)
0281         get_data_to_compute(cipher, pctx, plain, cryptlen);
0282 
0283 out:
0284     return err;
0285 }
0286 
0287 static void crypto_ccm_encrypt_done(struct crypto_async_request *areq, int err)
0288 {
0289     struct aead_request *req = areq->data;
0290     struct crypto_aead *aead = crypto_aead_reqtfm(req);
0291     struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
0292     u8 *odata = pctx->odata;
0293 
0294     if (!err)
0295         scatterwalk_map_and_copy(odata, req->dst,
0296                      req->assoclen + req->cryptlen,
0297                      crypto_aead_authsize(aead), 1);
0298     aead_request_complete(req, err);
0299 }
0300 
0301 static inline int crypto_ccm_check_iv(const u8 *iv)
0302 {
0303     /* 2 <= L <= 8, so 1 <= L' <= 7. */
0304     if (1 > iv[0] || iv[0] > 7)
0305         return -EINVAL;
0306 
0307     return 0;
0308 }
0309 
0310 static int crypto_ccm_init_crypt(struct aead_request *req, u8 *tag)
0311 {
0312     struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
0313     struct scatterlist *sg;
0314     u8 *iv = req->iv;
0315     int err;
0316 
0317     err = crypto_ccm_check_iv(iv);
0318     if (err)
0319         return err;
0320 
0321     pctx->flags = aead_request_flags(req);
0322 
0323      /* Note: rfc 3610 and NIST 800-38C require counter of
0324      * zero to encrypt auth tag.
0325      */
0326     memset(iv + 15 - iv[0], 0, iv[0] + 1);
0327 
0328     sg_init_table(pctx->src, 3);
0329     sg_set_buf(pctx->src, tag, 16);
0330     sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen);
0331     if (sg != pctx->src + 1)
0332         sg_chain(pctx->src, 2, sg);
0333 
0334     if (req->src != req->dst) {
0335         sg_init_table(pctx->dst, 3);
0336         sg_set_buf(pctx->dst, tag, 16);
0337         sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen);
0338         if (sg != pctx->dst + 1)
0339             sg_chain(pctx->dst, 2, sg);
0340     }
0341 
0342     return 0;
0343 }
0344 
0345 static int crypto_ccm_encrypt(struct aead_request *req)
0346 {
0347     struct crypto_aead *aead = crypto_aead_reqtfm(req);
0348     struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
0349     struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
0350     struct skcipher_request *skreq = &pctx->skreq;
0351     struct scatterlist *dst;
0352     unsigned int cryptlen = req->cryptlen;
0353     u8 *odata = pctx->odata;
0354     u8 *iv = req->iv;
0355     int err;
0356 
0357     err = crypto_ccm_init_crypt(req, odata);
0358     if (err)
0359         return err;
0360 
0361     err = crypto_ccm_auth(req, sg_next(pctx->src), cryptlen);
0362     if (err)
0363         return err;
0364 
0365     dst = pctx->src;
0366     if (req->src != req->dst)
0367         dst = pctx->dst;
0368 
0369     skcipher_request_set_tfm(skreq, ctx->ctr);
0370     skcipher_request_set_callback(skreq, pctx->flags,
0371                       crypto_ccm_encrypt_done, req);
0372     skcipher_request_set_crypt(skreq, pctx->src, dst, cryptlen + 16, iv);
0373     err = crypto_skcipher_encrypt(skreq);
0374     if (err)
0375         return err;
0376 
0377     /* copy authtag to end of dst */
0378     scatterwalk_map_and_copy(odata, sg_next(dst), cryptlen,
0379                  crypto_aead_authsize(aead), 1);
0380     return err;
0381 }
0382 
0383 static void crypto_ccm_decrypt_done(struct crypto_async_request *areq,
0384                    int err)
0385 {
0386     struct aead_request *req = areq->data;
0387     struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
0388     struct crypto_aead *aead = crypto_aead_reqtfm(req);
0389     unsigned int authsize = crypto_aead_authsize(aead);
0390     unsigned int cryptlen = req->cryptlen - authsize;
0391     struct scatterlist *dst;
0392 
0393     pctx->flags = 0;
0394 
0395     dst = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
0396 
0397     if (!err) {
0398         err = crypto_ccm_auth(req, dst, cryptlen);
0399         if (!err && crypto_memneq(pctx->auth_tag, pctx->odata, authsize))
0400             err = -EBADMSG;
0401     }
0402     aead_request_complete(req, err);
0403 }
0404 
0405 static int crypto_ccm_decrypt(struct aead_request *req)
0406 {
0407     struct crypto_aead *aead = crypto_aead_reqtfm(req);
0408     struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
0409     struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
0410     struct skcipher_request *skreq = &pctx->skreq;
0411     struct scatterlist *dst;
0412     unsigned int authsize = crypto_aead_authsize(aead);
0413     unsigned int cryptlen = req->cryptlen;
0414     u8 *authtag = pctx->auth_tag;
0415     u8 *odata = pctx->odata;
0416     u8 *iv = req->iv;
0417     int err;
0418 
0419     cryptlen -= authsize;
0420 
0421     err = crypto_ccm_init_crypt(req, authtag);
0422     if (err)
0423         return err;
0424 
0425     scatterwalk_map_and_copy(authtag, sg_next(pctx->src), cryptlen,
0426                  authsize, 0);
0427 
0428     dst = pctx->src;
0429     if (req->src != req->dst)
0430         dst = pctx->dst;
0431 
0432     skcipher_request_set_tfm(skreq, ctx->ctr);
0433     skcipher_request_set_callback(skreq, pctx->flags,
0434                       crypto_ccm_decrypt_done, req);
0435     skcipher_request_set_crypt(skreq, pctx->src, dst, cryptlen + 16, iv);
0436     err = crypto_skcipher_decrypt(skreq);
0437     if (err)
0438         return err;
0439 
0440     err = crypto_ccm_auth(req, sg_next(dst), cryptlen);
0441     if (err)
0442         return err;
0443 
0444     /* verify */
0445     if (crypto_memneq(authtag, odata, authsize))
0446         return -EBADMSG;
0447 
0448     return err;
0449 }
0450 
0451 static int crypto_ccm_init_tfm(struct crypto_aead *tfm)
0452 {
0453     struct aead_instance *inst = aead_alg_instance(tfm);
0454     struct ccm_instance_ctx *ictx = aead_instance_ctx(inst);
0455     struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm);
0456     struct crypto_cipher *cipher;
0457     struct crypto_skcipher *ctr;
0458     unsigned long align;
0459     int err;
0460 
0461     cipher = crypto_spawn_cipher(&ictx->cipher);
0462     if (IS_ERR(cipher))
0463         return PTR_ERR(cipher);
0464 
0465     ctr = crypto_spawn_skcipher(&ictx->ctr);
0466     err = PTR_ERR(ctr);
0467     if (IS_ERR(ctr))
0468         goto err_free_cipher;
0469 
0470     ctx->cipher = cipher;
0471     ctx->ctr = ctr;
0472 
0473     align = crypto_aead_alignmask(tfm);
0474     align &= ~(crypto_tfm_ctx_alignment() - 1);
0475     crypto_aead_set_reqsize(
0476         tfm,
0477         align + sizeof(struct crypto_ccm_req_priv_ctx) +
0478         crypto_skcipher_reqsize(ctr));
0479 
0480     return 0;
0481 
0482 err_free_cipher:
0483     crypto_free_cipher(cipher);
0484     return err;
0485 }
0486 
0487 static void crypto_ccm_exit_tfm(struct crypto_aead *tfm)
0488 {
0489     struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm);
0490 
0491     crypto_free_cipher(ctx->cipher);
0492     crypto_free_skcipher(ctx->ctr);
0493 }
0494 
0495 static void crypto_ccm_free(struct aead_instance *inst)
0496 {
0497     struct ccm_instance_ctx *ctx = aead_instance_ctx(inst);
0498 
0499     crypto_drop_spawn(&ctx->cipher);
0500     crypto_drop_skcipher(&ctx->ctr);
0501     kfree(inst);
0502 }
0503 
0504 static int crypto_ccm_create_common(struct crypto_template *tmpl,
0505                     struct rtattr **tb,
0506                     const char *full_name,
0507                     const char *ctr_name,
0508                     const char *cipher_name)
0509 {
0510     struct crypto_attr_type *algt;
0511     struct aead_instance *inst;
0512     struct skcipher_alg *ctr;
0513     struct crypto_alg *cipher;
0514     struct ccm_instance_ctx *ictx;
0515     int err;
0516 
0517     algt = crypto_get_attr_type(tb);
0518     if (IS_ERR(algt))
0519         return PTR_ERR(algt);
0520 
0521     if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
0522         return -EINVAL;
0523 
0524     cipher = crypto_alg_mod_lookup(cipher_name,  CRYPTO_ALG_TYPE_CIPHER,
0525                        CRYPTO_ALG_TYPE_MASK);
0526     if (IS_ERR(cipher))
0527         return PTR_ERR(cipher);
0528 
0529     err = -EINVAL;
0530     if (cipher->cra_blocksize != 16)
0531         goto out_put_cipher;
0532 
0533     inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
0534     err = -ENOMEM;
0535     if (!inst)
0536         goto out_put_cipher;
0537 
0538     ictx = aead_instance_ctx(inst);
0539 
0540     err = crypto_init_spawn(&ictx->cipher, cipher,
0541                 aead_crypto_instance(inst),
0542                 CRYPTO_ALG_TYPE_MASK);
0543     if (err)
0544         goto err_free_inst;
0545 
0546     crypto_set_skcipher_spawn(&ictx->ctr, aead_crypto_instance(inst));
0547     err = crypto_grab_skcipher(&ictx->ctr, ctr_name, 0,
0548                    crypto_requires_sync(algt->type,
0549                             algt->mask));
0550     if (err)
0551         goto err_drop_cipher;
0552 
0553     ctr = crypto_spawn_skcipher_alg(&ictx->ctr);
0554 
0555     /* Not a stream cipher? */
0556     err = -EINVAL;
0557     if (ctr->base.cra_blocksize != 1)
0558         goto err_drop_ctr;
0559 
0560     /* We want the real thing! */
0561     if (crypto_skcipher_alg_ivsize(ctr) != 16)
0562         goto err_drop_ctr;
0563 
0564     err = -ENAMETOOLONG;
0565     if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
0566              "ccm_base(%s,%s)", ctr->base.cra_driver_name,
0567              cipher->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
0568         goto err_drop_ctr;
0569 
0570     memcpy(inst->alg.base.cra_name, full_name, CRYPTO_MAX_ALG_NAME);
0571 
0572     inst->alg.base.cra_flags = ctr->base.cra_flags & CRYPTO_ALG_ASYNC;
0573     inst->alg.base.cra_priority = (cipher->cra_priority +
0574                        ctr->base.cra_priority) / 2;
0575     inst->alg.base.cra_blocksize = 1;
0576     inst->alg.base.cra_alignmask = cipher->cra_alignmask |
0577                        ctr->base.cra_alignmask |
0578                        (__alignof__(u32) - 1);
0579     inst->alg.ivsize = 16;
0580     inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
0581     inst->alg.maxauthsize = 16;
0582     inst->alg.base.cra_ctxsize = sizeof(struct crypto_ccm_ctx);
0583     inst->alg.init = crypto_ccm_init_tfm;
0584     inst->alg.exit = crypto_ccm_exit_tfm;
0585     inst->alg.setkey = crypto_ccm_setkey;
0586     inst->alg.setauthsize = crypto_ccm_setauthsize;
0587     inst->alg.encrypt = crypto_ccm_encrypt;
0588     inst->alg.decrypt = crypto_ccm_decrypt;
0589 
0590     inst->free = crypto_ccm_free;
0591 
0592     err = aead_register_instance(tmpl, inst);
0593     if (err)
0594         goto err_drop_ctr;
0595 
0596 out_put_cipher:
0597     crypto_mod_put(cipher);
0598     return err;
0599 
0600 err_drop_ctr:
0601     crypto_drop_skcipher(&ictx->ctr);
0602 err_drop_cipher:
0603     crypto_drop_spawn(&ictx->cipher);
0604 err_free_inst:
0605     kfree(inst);
0606     goto out_put_cipher;
0607 }
0608 
0609 static int crypto_ccm_create(struct crypto_template *tmpl, struct rtattr **tb)
0610 {
0611     const char *cipher_name;
0612     char ctr_name[CRYPTO_MAX_ALG_NAME];
0613     char full_name[CRYPTO_MAX_ALG_NAME];
0614 
0615     cipher_name = crypto_attr_alg_name(tb[1]);
0616     if (IS_ERR(cipher_name))
0617         return PTR_ERR(cipher_name);
0618 
0619     if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)",
0620              cipher_name) >= CRYPTO_MAX_ALG_NAME)
0621         return -ENAMETOOLONG;
0622 
0623     if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "ccm(%s)", cipher_name) >=
0624         CRYPTO_MAX_ALG_NAME)
0625         return -ENAMETOOLONG;
0626 
0627     return crypto_ccm_create_common(tmpl, tb, full_name, ctr_name,
0628                     cipher_name);
0629 }
0630 
0631 static struct crypto_template crypto_ccm_tmpl = {
0632     .name = "ccm",
0633     .create = crypto_ccm_create,
0634     .module = THIS_MODULE,
0635 };
0636 
0637 static int crypto_ccm_base_create(struct crypto_template *tmpl,
0638                   struct rtattr **tb)
0639 {
0640     const char *ctr_name;
0641     const char *cipher_name;
0642     char full_name[CRYPTO_MAX_ALG_NAME];
0643 
0644     ctr_name = crypto_attr_alg_name(tb[1]);
0645     if (IS_ERR(ctr_name))
0646         return PTR_ERR(ctr_name);
0647 
0648     cipher_name = crypto_attr_alg_name(tb[2]);
0649     if (IS_ERR(cipher_name))
0650         return PTR_ERR(cipher_name);
0651 
0652     if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "ccm_base(%s,%s)",
0653              ctr_name, cipher_name) >= CRYPTO_MAX_ALG_NAME)
0654         return -ENAMETOOLONG;
0655 
0656     return crypto_ccm_create_common(tmpl, tb, full_name, ctr_name,
0657                     cipher_name);
0658 }
0659 
0660 static struct crypto_template crypto_ccm_base_tmpl = {
0661     .name = "ccm_base",
0662     .create = crypto_ccm_base_create,
0663     .module = THIS_MODULE,
0664 };
0665 
0666 static int crypto_rfc4309_setkey(struct crypto_aead *parent, const u8 *key,
0667                  unsigned int keylen)
0668 {
0669     struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent);
0670     struct crypto_aead *child = ctx->child;
0671     int err;
0672 
0673     if (keylen < 3)
0674         return -EINVAL;
0675 
0676     keylen -= 3;
0677     memcpy(ctx->nonce, key + keylen, 3);
0678 
0679     crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
0680     crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
0681                      CRYPTO_TFM_REQ_MASK);
0682     err = crypto_aead_setkey(child, key, keylen);
0683     crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
0684                       CRYPTO_TFM_RES_MASK);
0685 
0686     return err;
0687 }
0688 
0689 static int crypto_rfc4309_setauthsize(struct crypto_aead *parent,
0690                       unsigned int authsize)
0691 {
0692     struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent);
0693 
0694     switch (authsize) {
0695     case 8:
0696     case 12:
0697     case 16:
0698         break;
0699     default:
0700         return -EINVAL;
0701     }
0702 
0703     return crypto_aead_setauthsize(ctx->child, authsize);
0704 }
0705 
0706 static struct aead_request *crypto_rfc4309_crypt(struct aead_request *req)
0707 {
0708     struct crypto_rfc4309_req_ctx *rctx = aead_request_ctx(req);
0709     struct aead_request *subreq = &rctx->subreq;
0710     struct crypto_aead *aead = crypto_aead_reqtfm(req);
0711     struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(aead);
0712     struct crypto_aead *child = ctx->child;
0713     struct scatterlist *sg;
0714     u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
0715                crypto_aead_alignmask(child) + 1);
0716 
0717     /* L' */
0718     iv[0] = 3;
0719 
0720     memcpy(iv + 1, ctx->nonce, 3);
0721     memcpy(iv + 4, req->iv, 8);
0722 
0723     scatterwalk_map_and_copy(iv + 16, req->src, 0, req->assoclen - 8, 0);
0724 
0725     sg_init_table(rctx->src, 3);
0726     sg_set_buf(rctx->src, iv + 16, req->assoclen - 8);
0727     sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
0728     if (sg != rctx->src + 1)
0729         sg_chain(rctx->src, 2, sg);
0730 
0731     if (req->src != req->dst) {
0732         sg_init_table(rctx->dst, 3);
0733         sg_set_buf(rctx->dst, iv + 16, req->assoclen - 8);
0734         sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
0735         if (sg != rctx->dst + 1)
0736             sg_chain(rctx->dst, 2, sg);
0737     }
0738 
0739     aead_request_set_tfm(subreq, child);
0740     aead_request_set_callback(subreq, req->base.flags, req->base.complete,
0741                   req->base.data);
0742     aead_request_set_crypt(subreq, rctx->src,
0743                    req->src == req->dst ? rctx->src : rctx->dst,
0744                    req->cryptlen, iv);
0745     aead_request_set_ad(subreq, req->assoclen - 8);
0746 
0747     return subreq;
0748 }
0749 
0750 static int crypto_rfc4309_encrypt(struct aead_request *req)
0751 {
0752     if (req->assoclen != 16 && req->assoclen != 20)
0753         return -EINVAL;
0754 
0755     req = crypto_rfc4309_crypt(req);
0756 
0757     return crypto_aead_encrypt(req);
0758 }
0759 
0760 static int crypto_rfc4309_decrypt(struct aead_request *req)
0761 {
0762     if (req->assoclen != 16 && req->assoclen != 20)
0763         return -EINVAL;
0764 
0765     req = crypto_rfc4309_crypt(req);
0766 
0767     return crypto_aead_decrypt(req);
0768 }
0769 
0770 static int crypto_rfc4309_init_tfm(struct crypto_aead *tfm)
0771 {
0772     struct aead_instance *inst = aead_alg_instance(tfm);
0773     struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
0774     struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(tfm);
0775     struct crypto_aead *aead;
0776     unsigned long align;
0777 
0778     aead = crypto_spawn_aead(spawn);
0779     if (IS_ERR(aead))
0780         return PTR_ERR(aead);
0781 
0782     ctx->child = aead;
0783 
0784     align = crypto_aead_alignmask(aead);
0785     align &= ~(crypto_tfm_ctx_alignment() - 1);
0786     crypto_aead_set_reqsize(
0787         tfm,
0788         sizeof(struct crypto_rfc4309_req_ctx) +
0789         ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
0790         align + 32);
0791 
0792     return 0;
0793 }
0794 
0795 static void crypto_rfc4309_exit_tfm(struct crypto_aead *tfm)
0796 {
0797     struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(tfm);
0798 
0799     crypto_free_aead(ctx->child);
0800 }
0801 
0802 static void crypto_rfc4309_free(struct aead_instance *inst)
0803 {
0804     crypto_drop_aead(aead_instance_ctx(inst));
0805     kfree(inst);
0806 }
0807 
0808 static int crypto_rfc4309_create(struct crypto_template *tmpl,
0809                  struct rtattr **tb)
0810 {
0811     struct crypto_attr_type *algt;
0812     struct aead_instance *inst;
0813     struct crypto_aead_spawn *spawn;
0814     struct aead_alg *alg;
0815     const char *ccm_name;
0816     int err;
0817 
0818     algt = crypto_get_attr_type(tb);
0819     if (IS_ERR(algt))
0820         return PTR_ERR(algt);
0821 
0822     if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
0823         return -EINVAL;
0824 
0825     ccm_name = crypto_attr_alg_name(tb[1]);
0826     if (IS_ERR(ccm_name))
0827         return PTR_ERR(ccm_name);
0828 
0829     inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
0830     if (!inst)
0831         return -ENOMEM;
0832 
0833     spawn = aead_instance_ctx(inst);
0834     crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
0835     err = crypto_grab_aead(spawn, ccm_name, 0,
0836                    crypto_requires_sync(algt->type, algt->mask));
0837     if (err)
0838         goto out_free_inst;
0839 
0840     alg = crypto_spawn_aead_alg(spawn);
0841 
0842     err = -EINVAL;
0843 
0844     /* We only support 16-byte blocks. */
0845     if (crypto_aead_alg_ivsize(alg) != 16)
0846         goto out_drop_alg;
0847 
0848     /* Not a stream cipher? */
0849     if (alg->base.cra_blocksize != 1)
0850         goto out_drop_alg;
0851 
0852     err = -ENAMETOOLONG;
0853     if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
0854              "rfc4309(%s)", alg->base.cra_name) >=
0855         CRYPTO_MAX_ALG_NAME ||
0856         snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
0857              "rfc4309(%s)", alg->base.cra_driver_name) >=
0858         CRYPTO_MAX_ALG_NAME)
0859         goto out_drop_alg;
0860 
0861     inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
0862     inst->alg.base.cra_priority = alg->base.cra_priority;
0863     inst->alg.base.cra_blocksize = 1;
0864     inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
0865 
0866     inst->alg.ivsize = 8;
0867     inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
0868     inst->alg.maxauthsize = 16;
0869 
0870     inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4309_ctx);
0871 
0872     inst->alg.init = crypto_rfc4309_init_tfm;
0873     inst->alg.exit = crypto_rfc4309_exit_tfm;
0874 
0875     inst->alg.setkey = crypto_rfc4309_setkey;
0876     inst->alg.setauthsize = crypto_rfc4309_setauthsize;
0877     inst->alg.encrypt = crypto_rfc4309_encrypt;
0878     inst->alg.decrypt = crypto_rfc4309_decrypt;
0879 
0880     inst->free = crypto_rfc4309_free;
0881 
0882     err = aead_register_instance(tmpl, inst);
0883     if (err)
0884         goto out_drop_alg;
0885 
0886 out:
0887     return err;
0888 
0889 out_drop_alg:
0890     crypto_drop_aead(spawn);
0891 out_free_inst:
0892     kfree(inst);
0893     goto out;
0894 }
0895 
0896 static struct crypto_template crypto_rfc4309_tmpl = {
0897     .name = "rfc4309",
0898     .create = crypto_rfc4309_create,
0899     .module = THIS_MODULE,
0900 };
0901 
0902 static int __init crypto_ccm_module_init(void)
0903 {
0904     int err;
0905 
0906     err = crypto_register_template(&crypto_ccm_base_tmpl);
0907     if (err)
0908         goto out;
0909 
0910     err = crypto_register_template(&crypto_ccm_tmpl);
0911     if (err)
0912         goto out_undo_base;
0913 
0914     err = crypto_register_template(&crypto_rfc4309_tmpl);
0915     if (err)
0916         goto out_undo_ccm;
0917 
0918 out:
0919     return err;
0920 
0921 out_undo_ccm:
0922     crypto_unregister_template(&crypto_ccm_tmpl);
0923 out_undo_base:
0924     crypto_unregister_template(&crypto_ccm_base_tmpl);
0925     goto out;
0926 }
0927 
0928 static void __exit crypto_ccm_module_exit(void)
0929 {
0930     crypto_unregister_template(&crypto_rfc4309_tmpl);
0931     crypto_unregister_template(&crypto_ccm_tmpl);
0932     crypto_unregister_template(&crypto_ccm_base_tmpl);
0933 }
0934 
0935 module_init(crypto_ccm_module_init);
0936 module_exit(crypto_ccm_module_exit);
0937 
0938 MODULE_LICENSE("GPL");
0939 MODULE_DESCRIPTION("Counter with CBC MAC");
0940 MODULE_ALIAS_CRYPTO("ccm_base");
0941 MODULE_ALIAS_CRYPTO("rfc4309");
0942 MODULE_ALIAS_CRYPTO("ccm");