Back to home page

LXR

 
 

    


0001 /*
0002  * authencesn.c - AEAD wrapper for IPsec with extended sequence numbers,
0003  *                 derived from authenc.c
0004  *
0005  * Copyright (C) 2010 secunet Security Networks AG
0006  * Copyright (C) 2010 Steffen Klassert <steffen.klassert@secunet.com>
0007  * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
0008  *
0009  * This program is free software; you can redistribute it and/or modify it
0010  * under the terms of the GNU General Public License as published by the Free
0011  * Software Foundation; either version 2 of the License, or (at your option)
0012  * any later version.
0013  *
0014  */
0015 
0016 #include <crypto/internal/aead.h>
0017 #include <crypto/internal/hash.h>
0018 #include <crypto/internal/skcipher.h>
0019 #include <crypto/authenc.h>
0020 #include <crypto/null.h>
0021 #include <crypto/scatterwalk.h>
0022 #include <linux/err.h>
0023 #include <linux/init.h>
0024 #include <linux/kernel.h>
0025 #include <linux/module.h>
0026 #include <linux/rtnetlink.h>
0027 #include <linux/slab.h>
0028 #include <linux/spinlock.h>
0029 
0030 struct authenc_esn_instance_ctx {
0031     struct crypto_ahash_spawn auth;
0032     struct crypto_skcipher_spawn enc;
0033 };
0034 
0035 struct crypto_authenc_esn_ctx {
0036     unsigned int reqoff;
0037     struct crypto_ahash *auth;
0038     struct crypto_skcipher *enc;
0039     struct crypto_skcipher *null;
0040 };
0041 
0042 struct authenc_esn_request_ctx {
0043     struct scatterlist src[2];
0044     struct scatterlist dst[2];
0045     char tail[];
0046 };
0047 
0048 static void authenc_esn_request_complete(struct aead_request *req, int err)
0049 {
0050     if (err != -EINPROGRESS)
0051         aead_request_complete(req, err);
0052 }
0053 
0054 static int crypto_authenc_esn_setauthsize(struct crypto_aead *authenc_esn,
0055                       unsigned int authsize)
0056 {
0057     if (authsize > 0 && authsize < 4)
0058         return -EINVAL;
0059 
0060     return 0;
0061 }
0062 
0063 static int crypto_authenc_esn_setkey(struct crypto_aead *authenc_esn, const u8 *key,
0064                      unsigned int keylen)
0065 {
0066     struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
0067     struct crypto_ahash *auth = ctx->auth;
0068     struct crypto_skcipher *enc = ctx->enc;
0069     struct crypto_authenc_keys keys;
0070     int err = -EINVAL;
0071 
0072     if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
0073         goto badkey;
0074 
0075     crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
0076     crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc_esn) &
0077                      CRYPTO_TFM_REQ_MASK);
0078     err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
0079     crypto_aead_set_flags(authenc_esn, crypto_ahash_get_flags(auth) &
0080                        CRYPTO_TFM_RES_MASK);
0081 
0082     if (err)
0083         goto out;
0084 
0085     crypto_skcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
0086     crypto_skcipher_set_flags(enc, crypto_aead_get_flags(authenc_esn) &
0087                      CRYPTO_TFM_REQ_MASK);
0088     err = crypto_skcipher_setkey(enc, keys.enckey, keys.enckeylen);
0089     crypto_aead_set_flags(authenc_esn, crypto_skcipher_get_flags(enc) &
0090                        CRYPTO_TFM_RES_MASK);
0091 
0092 out:
0093     return err;
0094 
0095 badkey:
0096     crypto_aead_set_flags(authenc_esn, CRYPTO_TFM_RES_BAD_KEY_LEN);
0097     goto out;
0098 }
0099 
0100 static int crypto_authenc_esn_genicv_tail(struct aead_request *req,
0101                       unsigned int flags)
0102 {
0103     struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
0104     struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
0105     struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
0106     struct crypto_ahash *auth = ctx->auth;
0107     u8 *hash = PTR_ALIGN((u8 *)areq_ctx->tail,
0108                  crypto_ahash_alignmask(auth) + 1);
0109     unsigned int authsize = crypto_aead_authsize(authenc_esn);
0110     unsigned int assoclen = req->assoclen;
0111     unsigned int cryptlen = req->cryptlen;
0112     struct scatterlist *dst = req->dst;
0113     u32 tmp[2];
0114 
0115     /* Move high-order bits of sequence number back. */
0116     scatterwalk_map_and_copy(tmp, dst, 4, 4, 0);
0117     scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 0);
0118     scatterwalk_map_and_copy(tmp, dst, 0, 8, 1);
0119 
0120     scatterwalk_map_and_copy(hash, dst, assoclen + cryptlen, authsize, 1);
0121     return 0;
0122 }
0123 
0124 static void authenc_esn_geniv_ahash_done(struct crypto_async_request *areq,
0125                      int err)
0126 {
0127     struct aead_request *req = areq->data;
0128 
0129     err = err ?: crypto_authenc_esn_genicv_tail(req, 0);
0130     aead_request_complete(req, err);
0131 }
0132 
0133 static int crypto_authenc_esn_genicv(struct aead_request *req,
0134                      unsigned int flags)
0135 {
0136     struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
0137     struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
0138     struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
0139     struct crypto_ahash *auth = ctx->auth;
0140     u8 *hash = PTR_ALIGN((u8 *)areq_ctx->tail,
0141                  crypto_ahash_alignmask(auth) + 1);
0142     struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
0143     unsigned int authsize = crypto_aead_authsize(authenc_esn);
0144     unsigned int assoclen = req->assoclen;
0145     unsigned int cryptlen = req->cryptlen;
0146     struct scatterlist *dst = req->dst;
0147     u32 tmp[2];
0148 
0149     if (!authsize)
0150         return 0;
0151 
0152     /* Move high-order bits of sequence number to the end. */
0153     scatterwalk_map_and_copy(tmp, dst, 0, 8, 0);
0154     scatterwalk_map_and_copy(tmp, dst, 4, 4, 1);
0155     scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 1);
0156 
0157     sg_init_table(areq_ctx->dst, 2);
0158     dst = scatterwalk_ffwd(areq_ctx->dst, dst, 4);
0159 
0160     ahash_request_set_tfm(ahreq, auth);
0161     ahash_request_set_crypt(ahreq, dst, hash, assoclen + cryptlen);
0162     ahash_request_set_callback(ahreq, flags,
0163                    authenc_esn_geniv_ahash_done, req);
0164 
0165     return crypto_ahash_digest(ahreq) ?:
0166            crypto_authenc_esn_genicv_tail(req, aead_request_flags(req));
0167 }
0168 
0169 
0170 static void crypto_authenc_esn_encrypt_done(struct crypto_async_request *req,
0171                         int err)
0172 {
0173     struct aead_request *areq = req->data;
0174 
0175     if (!err)
0176         err = crypto_authenc_esn_genicv(areq, 0);
0177 
0178     authenc_esn_request_complete(areq, err);
0179 }
0180 
0181 static int crypto_authenc_esn_copy(struct aead_request *req, unsigned int len)
0182 {
0183     struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
0184     struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
0185     SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
0186 
0187     skcipher_request_set_tfm(skreq, ctx->null);
0188     skcipher_request_set_callback(skreq, aead_request_flags(req),
0189                       NULL, NULL);
0190     skcipher_request_set_crypt(skreq, req->src, req->dst, len, NULL);
0191 
0192     return crypto_skcipher_encrypt(skreq);
0193 }
0194 
0195 static int crypto_authenc_esn_encrypt(struct aead_request *req)
0196 {
0197     struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
0198     struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
0199     struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
0200     struct skcipher_request *skreq = (void *)(areq_ctx->tail +
0201                           ctx->reqoff);
0202     struct crypto_skcipher *enc = ctx->enc;
0203     unsigned int assoclen = req->assoclen;
0204     unsigned int cryptlen = req->cryptlen;
0205     struct scatterlist *src, *dst;
0206     int err;
0207 
0208     sg_init_table(areq_ctx->src, 2);
0209     src = scatterwalk_ffwd(areq_ctx->src, req->src, assoclen);
0210     dst = src;
0211 
0212     if (req->src != req->dst) {
0213         err = crypto_authenc_esn_copy(req, assoclen);
0214         if (err)
0215             return err;
0216 
0217         sg_init_table(areq_ctx->dst, 2);
0218         dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, assoclen);
0219     }
0220 
0221     skcipher_request_set_tfm(skreq, enc);
0222     skcipher_request_set_callback(skreq, aead_request_flags(req),
0223                       crypto_authenc_esn_encrypt_done, req);
0224     skcipher_request_set_crypt(skreq, src, dst, cryptlen, req->iv);
0225 
0226     err = crypto_skcipher_encrypt(skreq);
0227     if (err)
0228         return err;
0229 
0230     return crypto_authenc_esn_genicv(req, aead_request_flags(req));
0231 }
0232 
0233 static int crypto_authenc_esn_decrypt_tail(struct aead_request *req,
0234                        unsigned int flags)
0235 {
0236     struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
0237     unsigned int authsize = crypto_aead_authsize(authenc_esn);
0238     struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
0239     struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
0240     struct skcipher_request *skreq = (void *)(areq_ctx->tail +
0241                           ctx->reqoff);
0242     struct crypto_ahash *auth = ctx->auth;
0243     u8 *ohash = PTR_ALIGN((u8 *)areq_ctx->tail,
0244                   crypto_ahash_alignmask(auth) + 1);
0245     unsigned int cryptlen = req->cryptlen - authsize;
0246     unsigned int assoclen = req->assoclen;
0247     struct scatterlist *dst = req->dst;
0248     u8 *ihash = ohash + crypto_ahash_digestsize(auth);
0249     u32 tmp[2];
0250 
0251     /* Move high-order bits of sequence number back. */
0252     scatterwalk_map_and_copy(tmp, dst, 4, 4, 0);
0253     scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 0);
0254     scatterwalk_map_and_copy(tmp, dst, 0, 8, 1);
0255 
0256     if (crypto_memneq(ihash, ohash, authsize))
0257         return -EBADMSG;
0258 
0259     sg_init_table(areq_ctx->dst, 2);
0260     dst = scatterwalk_ffwd(areq_ctx->dst, dst, assoclen);
0261 
0262     skcipher_request_set_tfm(skreq, ctx->enc);
0263     skcipher_request_set_callback(skreq, flags,
0264                       req->base.complete, req->base.data);
0265     skcipher_request_set_crypt(skreq, dst, dst, cryptlen, req->iv);
0266 
0267     return crypto_skcipher_decrypt(skreq);
0268 }
0269 
0270 static void authenc_esn_verify_ahash_done(struct crypto_async_request *areq,
0271                       int err)
0272 {
0273     struct aead_request *req = areq->data;
0274 
0275     err = err ?: crypto_authenc_esn_decrypt_tail(req, 0);
0276     aead_request_complete(req, err);
0277 }
0278 
0279 static int crypto_authenc_esn_decrypt(struct aead_request *req)
0280 {
0281     struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
0282     struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
0283     struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
0284     struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
0285     unsigned int authsize = crypto_aead_authsize(authenc_esn);
0286     struct crypto_ahash *auth = ctx->auth;
0287     u8 *ohash = PTR_ALIGN((u8 *)areq_ctx->tail,
0288                   crypto_ahash_alignmask(auth) + 1);
0289     unsigned int assoclen = req->assoclen;
0290     unsigned int cryptlen = req->cryptlen;
0291     u8 *ihash = ohash + crypto_ahash_digestsize(auth);
0292     struct scatterlist *dst = req->dst;
0293     u32 tmp[2];
0294     int err;
0295 
0296     cryptlen -= authsize;
0297 
0298     if (req->src != dst) {
0299         err = crypto_authenc_esn_copy(req, assoclen + cryptlen);
0300         if (err)
0301             return err;
0302     }
0303 
0304     scatterwalk_map_and_copy(ihash, req->src, assoclen + cryptlen,
0305                  authsize, 0);
0306 
0307     if (!authsize)
0308         goto tail;
0309 
0310     /* Move high-order bits of sequence number to the end. */
0311     scatterwalk_map_and_copy(tmp, dst, 0, 8, 0);
0312     scatterwalk_map_and_copy(tmp, dst, 4, 4, 1);
0313     scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 1);
0314 
0315     sg_init_table(areq_ctx->dst, 2);
0316     dst = scatterwalk_ffwd(areq_ctx->dst, dst, 4);
0317 
0318     ahash_request_set_tfm(ahreq, auth);
0319     ahash_request_set_crypt(ahreq, dst, ohash, assoclen + cryptlen);
0320     ahash_request_set_callback(ahreq, aead_request_flags(req),
0321                    authenc_esn_verify_ahash_done, req);
0322 
0323     err = crypto_ahash_digest(ahreq);
0324     if (err)
0325         return err;
0326 
0327 tail:
0328     return crypto_authenc_esn_decrypt_tail(req, aead_request_flags(req));
0329 }
0330 
0331 static int crypto_authenc_esn_init_tfm(struct crypto_aead *tfm)
0332 {
0333     struct aead_instance *inst = aead_alg_instance(tfm);
0334     struct authenc_esn_instance_ctx *ictx = aead_instance_ctx(inst);
0335     struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(tfm);
0336     struct crypto_ahash *auth;
0337     struct crypto_skcipher *enc;
0338     struct crypto_skcipher *null;
0339     int err;
0340 
0341     auth = crypto_spawn_ahash(&ictx->auth);
0342     if (IS_ERR(auth))
0343         return PTR_ERR(auth);
0344 
0345     enc = crypto_spawn_skcipher(&ictx->enc);
0346     err = PTR_ERR(enc);
0347     if (IS_ERR(enc))
0348         goto err_free_ahash;
0349 
0350     null = crypto_get_default_null_skcipher2();
0351     err = PTR_ERR(null);
0352     if (IS_ERR(null))
0353         goto err_free_skcipher;
0354 
0355     ctx->auth = auth;
0356     ctx->enc = enc;
0357     ctx->null = null;
0358 
0359     ctx->reqoff = ALIGN(2 * crypto_ahash_digestsize(auth),
0360                 crypto_ahash_alignmask(auth) + 1);
0361 
0362     crypto_aead_set_reqsize(
0363         tfm,
0364         sizeof(struct authenc_esn_request_ctx) +
0365         ctx->reqoff +
0366         max_t(unsigned int,
0367               crypto_ahash_reqsize(auth) +
0368               sizeof(struct ahash_request),
0369               sizeof(struct skcipher_request) +
0370               crypto_skcipher_reqsize(enc)));
0371 
0372     return 0;
0373 
0374 err_free_skcipher:
0375     crypto_free_skcipher(enc);
0376 err_free_ahash:
0377     crypto_free_ahash(auth);
0378     return err;
0379 }
0380 
0381 static void crypto_authenc_esn_exit_tfm(struct crypto_aead *tfm)
0382 {
0383     struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(tfm);
0384 
0385     crypto_free_ahash(ctx->auth);
0386     crypto_free_skcipher(ctx->enc);
0387     crypto_put_default_null_skcipher2();
0388 }
0389 
0390 static void crypto_authenc_esn_free(struct aead_instance *inst)
0391 {
0392     struct authenc_esn_instance_ctx *ctx = aead_instance_ctx(inst);
0393 
0394     crypto_drop_skcipher(&ctx->enc);
0395     crypto_drop_ahash(&ctx->auth);
0396     kfree(inst);
0397 }
0398 
0399 static int crypto_authenc_esn_create(struct crypto_template *tmpl,
0400                      struct rtattr **tb)
0401 {
0402     struct crypto_attr_type *algt;
0403     struct aead_instance *inst;
0404     struct hash_alg_common *auth;
0405     struct crypto_alg *auth_base;
0406     struct skcipher_alg *enc;
0407     struct authenc_esn_instance_ctx *ctx;
0408     const char *enc_name;
0409     int err;
0410 
0411     algt = crypto_get_attr_type(tb);
0412     if (IS_ERR(algt))
0413         return PTR_ERR(algt);
0414 
0415     if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
0416         return -EINVAL;
0417 
0418     auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
0419                   CRYPTO_ALG_TYPE_AHASH_MASK |
0420                   crypto_requires_sync(algt->type, algt->mask));
0421     if (IS_ERR(auth))
0422         return PTR_ERR(auth);
0423 
0424     auth_base = &auth->base;
0425 
0426     enc_name = crypto_attr_alg_name(tb[2]);
0427     err = PTR_ERR(enc_name);
0428     if (IS_ERR(enc_name))
0429         goto out_put_auth;
0430 
0431     inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
0432     err = -ENOMEM;
0433     if (!inst)
0434         goto out_put_auth;
0435 
0436     ctx = aead_instance_ctx(inst);
0437 
0438     err = crypto_init_ahash_spawn(&ctx->auth, auth,
0439                       aead_crypto_instance(inst));
0440     if (err)
0441         goto err_free_inst;
0442 
0443     crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst));
0444     err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
0445                    crypto_requires_sync(algt->type,
0446                             algt->mask));
0447     if (err)
0448         goto err_drop_auth;
0449 
0450     enc = crypto_spawn_skcipher_alg(&ctx->enc);
0451 
0452     err = -ENAMETOOLONG;
0453     if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
0454              "authencesn(%s,%s)", auth_base->cra_name,
0455              enc->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
0456         goto err_drop_enc;
0457 
0458     if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
0459              "authencesn(%s,%s)", auth_base->cra_driver_name,
0460              enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
0461         goto err_drop_enc;
0462 
0463     inst->alg.base.cra_flags = (auth_base->cra_flags |
0464                     enc->base.cra_flags) & CRYPTO_ALG_ASYNC;
0465     inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
0466                       auth_base->cra_priority;
0467     inst->alg.base.cra_blocksize = enc->base.cra_blocksize;
0468     inst->alg.base.cra_alignmask = auth_base->cra_alignmask |
0469                        enc->base.cra_alignmask;
0470     inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_esn_ctx);
0471 
0472     inst->alg.ivsize = crypto_skcipher_alg_ivsize(enc);
0473     inst->alg.chunksize = crypto_skcipher_alg_chunksize(enc);
0474     inst->alg.maxauthsize = auth->digestsize;
0475 
0476     inst->alg.init = crypto_authenc_esn_init_tfm;
0477     inst->alg.exit = crypto_authenc_esn_exit_tfm;
0478 
0479     inst->alg.setkey = crypto_authenc_esn_setkey;
0480     inst->alg.setauthsize = crypto_authenc_esn_setauthsize;
0481     inst->alg.encrypt = crypto_authenc_esn_encrypt;
0482     inst->alg.decrypt = crypto_authenc_esn_decrypt;
0483 
0484     inst->free = crypto_authenc_esn_free,
0485 
0486     err = aead_register_instance(tmpl, inst);
0487     if (err)
0488         goto err_drop_enc;
0489 
0490 out:
0491     crypto_mod_put(auth_base);
0492     return err;
0493 
0494 err_drop_enc:
0495     crypto_drop_skcipher(&ctx->enc);
0496 err_drop_auth:
0497     crypto_drop_ahash(&ctx->auth);
0498 err_free_inst:
0499     kfree(inst);
0500 out_put_auth:
0501     goto out;
0502 }
0503 
0504 static struct crypto_template crypto_authenc_esn_tmpl = {
0505     .name = "authencesn",
0506     .create = crypto_authenc_esn_create,
0507     .module = THIS_MODULE,
0508 };
0509 
0510 static int __init crypto_authenc_esn_module_init(void)
0511 {
0512     return crypto_register_template(&crypto_authenc_esn_tmpl);
0513 }
0514 
0515 static void __exit crypto_authenc_esn_module_exit(void)
0516 {
0517     crypto_unregister_template(&crypto_authenc_esn_tmpl);
0518 }
0519 
0520 module_init(crypto_authenc_esn_module_init);
0521 module_exit(crypto_authenc_esn_module_exit);
0522 
0523 MODULE_LICENSE("GPL");
0524 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
0525 MODULE_DESCRIPTION("AEAD wrapper for IPsec with extended sequence numbers");
0526 MODULE_ALIAS_CRYPTO("authencesn");