Back to home page

LXR

 
 

    


0001 /*
0002  * AEAD: Authenticated Encryption with Associated Data
0003  *
0004  * This file provides API support for AEAD algorithms.
0005  *
0006  * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
0007  *
0008  * This program is free software; you can redistribute it and/or modify it
0009  * under the terms of the GNU General Public License as published by the Free
0010  * Software Foundation; either version 2 of the License, or (at your option)
0011  * any later version.
0012  *
0013  */
0014 
0015 #include <crypto/internal/geniv.h>
0016 #include <crypto/internal/rng.h>
0017 #include <crypto/null.h>
0018 #include <crypto/scatterwalk.h>
0019 #include <linux/err.h>
0020 #include <linux/init.h>
0021 #include <linux/kernel.h>
0022 #include <linux/module.h>
0023 #include <linux/rtnetlink.h>
0024 #include <linux/slab.h>
0025 #include <linux/seq_file.h>
0026 #include <linux/cryptouser.h>
0027 #include <net/netlink.h>
0028 
0029 #include "internal.h"
0030 
0031 static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key,
0032                 unsigned int keylen)
0033 {
0034     unsigned long alignmask = crypto_aead_alignmask(tfm);
0035     int ret;
0036     u8 *buffer, *alignbuffer;
0037     unsigned long absize;
0038 
0039     absize = keylen + alignmask;
0040     buffer = kmalloc(absize, GFP_ATOMIC);
0041     if (!buffer)
0042         return -ENOMEM;
0043 
0044     alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
0045     memcpy(alignbuffer, key, keylen);
0046     ret = crypto_aead_alg(tfm)->setkey(tfm, alignbuffer, keylen);
0047     memset(alignbuffer, 0, keylen);
0048     kfree(buffer);
0049     return ret;
0050 }
0051 
0052 int crypto_aead_setkey(struct crypto_aead *tfm,
0053                const u8 *key, unsigned int keylen)
0054 {
0055     unsigned long alignmask = crypto_aead_alignmask(tfm);
0056 
0057     if ((unsigned long)key & alignmask)
0058         return setkey_unaligned(tfm, key, keylen);
0059 
0060     return crypto_aead_alg(tfm)->setkey(tfm, key, keylen);
0061 }
0062 EXPORT_SYMBOL_GPL(crypto_aead_setkey);
0063 
0064 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
0065 {
0066     int err;
0067 
0068     if (authsize > crypto_aead_maxauthsize(tfm))
0069         return -EINVAL;
0070 
0071     if (crypto_aead_alg(tfm)->setauthsize) {
0072         err = crypto_aead_alg(tfm)->setauthsize(tfm, authsize);
0073         if (err)
0074             return err;
0075     }
0076 
0077     tfm->authsize = authsize;
0078     return 0;
0079 }
0080 EXPORT_SYMBOL_GPL(crypto_aead_setauthsize);
0081 
0082 static void crypto_aead_exit_tfm(struct crypto_tfm *tfm)
0083 {
0084     struct crypto_aead *aead = __crypto_aead_cast(tfm);
0085     struct aead_alg *alg = crypto_aead_alg(aead);
0086 
0087     alg->exit(aead);
0088 }
0089 
0090 static int crypto_aead_init_tfm(struct crypto_tfm *tfm)
0091 {
0092     struct crypto_aead *aead = __crypto_aead_cast(tfm);
0093     struct aead_alg *alg = crypto_aead_alg(aead);
0094 
0095     aead->authsize = alg->maxauthsize;
0096 
0097     if (alg->exit)
0098         aead->base.exit = crypto_aead_exit_tfm;
0099 
0100     if (alg->init)
0101         return alg->init(aead);
0102 
0103     return 0;
0104 }
0105 
0106 #ifdef CONFIG_NET
0107 static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg)
0108 {
0109     struct crypto_report_aead raead;
0110     struct aead_alg *aead = container_of(alg, struct aead_alg, base);
0111 
0112     strncpy(raead.type, "aead", sizeof(raead.type));
0113     strncpy(raead.geniv, "<none>", sizeof(raead.geniv));
0114 
0115     raead.blocksize = alg->cra_blocksize;
0116     raead.maxauthsize = aead->maxauthsize;
0117     raead.ivsize = aead->ivsize;
0118 
0119     if (nla_put(skb, CRYPTOCFGA_REPORT_AEAD,
0120             sizeof(struct crypto_report_aead), &raead))
0121         goto nla_put_failure;
0122     return 0;
0123 
0124 nla_put_failure:
0125     return -EMSGSIZE;
0126 }
0127 #else
0128 static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg)
0129 {
0130     return -ENOSYS;
0131 }
0132 #endif
0133 
0134 static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
0135     __attribute__ ((unused));
0136 static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
0137 {
0138     struct aead_alg *aead = container_of(alg, struct aead_alg, base);
0139 
0140     seq_printf(m, "type         : aead\n");
0141     seq_printf(m, "async        : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
0142                          "yes" : "no");
0143     seq_printf(m, "blocksize    : %u\n", alg->cra_blocksize);
0144     seq_printf(m, "ivsize       : %u\n", aead->ivsize);
0145     seq_printf(m, "maxauthsize  : %u\n", aead->maxauthsize);
0146     seq_printf(m, "geniv        : <none>\n");
0147 }
0148 
0149 static void crypto_aead_free_instance(struct crypto_instance *inst)
0150 {
0151     struct aead_instance *aead = aead_instance(inst);
0152 
0153     if (!aead->free) {
0154         inst->tmpl->free(inst);
0155         return;
0156     }
0157 
0158     aead->free(aead);
0159 }
0160 
0161 static const struct crypto_type crypto_aead_type = {
0162     .extsize = crypto_alg_extsize,
0163     .init_tfm = crypto_aead_init_tfm,
0164     .free = crypto_aead_free_instance,
0165 #ifdef CONFIG_PROC_FS
0166     .show = crypto_aead_show,
0167 #endif
0168     .report = crypto_aead_report,
0169     .maskclear = ~CRYPTO_ALG_TYPE_MASK,
0170     .maskset = CRYPTO_ALG_TYPE_MASK,
0171     .type = CRYPTO_ALG_TYPE_AEAD,
0172     .tfmsize = offsetof(struct crypto_aead, base),
0173 };
0174 
0175 static int aead_geniv_setkey(struct crypto_aead *tfm,
0176                  const u8 *key, unsigned int keylen)
0177 {
0178     struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm);
0179 
0180     return crypto_aead_setkey(ctx->child, key, keylen);
0181 }
0182 
0183 static int aead_geniv_setauthsize(struct crypto_aead *tfm,
0184                   unsigned int authsize)
0185 {
0186     struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm);
0187 
0188     return crypto_aead_setauthsize(ctx->child, authsize);
0189 }
0190 
0191 struct aead_instance *aead_geniv_alloc(struct crypto_template *tmpl,
0192                        struct rtattr **tb, u32 type, u32 mask)
0193 {
0194     const char *name;
0195     struct crypto_aead_spawn *spawn;
0196     struct crypto_attr_type *algt;
0197     struct aead_instance *inst;
0198     struct aead_alg *alg;
0199     unsigned int ivsize;
0200     unsigned int maxauthsize;
0201     int err;
0202 
0203     algt = crypto_get_attr_type(tb);
0204     if (IS_ERR(algt))
0205         return ERR_CAST(algt);
0206 
0207     if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
0208         return ERR_PTR(-EINVAL);
0209 
0210     name = crypto_attr_alg_name(tb[1]);
0211     if (IS_ERR(name))
0212         return ERR_CAST(name);
0213 
0214     inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
0215     if (!inst)
0216         return ERR_PTR(-ENOMEM);
0217 
0218     spawn = aead_instance_ctx(inst);
0219 
0220     /* Ignore async algorithms if necessary. */
0221     mask |= crypto_requires_sync(algt->type, algt->mask);
0222 
0223     crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
0224     err = crypto_grab_aead(spawn, name, type, mask);
0225     if (err)
0226         goto err_free_inst;
0227 
0228     alg = crypto_spawn_aead_alg(spawn);
0229 
0230     ivsize = crypto_aead_alg_ivsize(alg);
0231     maxauthsize = crypto_aead_alg_maxauthsize(alg);
0232 
0233     err = -EINVAL;
0234     if (ivsize < sizeof(u64))
0235         goto err_drop_alg;
0236 
0237     err = -ENAMETOOLONG;
0238     if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
0239              "%s(%s)", tmpl->name, alg->base.cra_name) >=
0240         CRYPTO_MAX_ALG_NAME)
0241         goto err_drop_alg;
0242     if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
0243              "%s(%s)", tmpl->name, alg->base.cra_driver_name) >=
0244         CRYPTO_MAX_ALG_NAME)
0245         goto err_drop_alg;
0246 
0247     inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
0248     inst->alg.base.cra_priority = alg->base.cra_priority;
0249     inst->alg.base.cra_blocksize = alg->base.cra_blocksize;
0250     inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
0251     inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
0252 
0253     inst->alg.setkey = aead_geniv_setkey;
0254     inst->alg.setauthsize = aead_geniv_setauthsize;
0255 
0256     inst->alg.ivsize = ivsize;
0257     inst->alg.maxauthsize = maxauthsize;
0258 
0259 out:
0260     return inst;
0261 
0262 err_drop_alg:
0263     crypto_drop_aead(spawn);
0264 err_free_inst:
0265     kfree(inst);
0266     inst = ERR_PTR(err);
0267     goto out;
0268 }
0269 EXPORT_SYMBOL_GPL(aead_geniv_alloc);
0270 
0271 void aead_geniv_free(struct aead_instance *inst)
0272 {
0273     crypto_drop_aead(aead_instance_ctx(inst));
0274     kfree(inst);
0275 }
0276 EXPORT_SYMBOL_GPL(aead_geniv_free);
0277 
0278 int aead_init_geniv(struct crypto_aead *aead)
0279 {
0280     struct aead_geniv_ctx *ctx = crypto_aead_ctx(aead);
0281     struct aead_instance *inst = aead_alg_instance(aead);
0282     struct crypto_aead *child;
0283     int err;
0284 
0285     spin_lock_init(&ctx->lock);
0286 
0287     err = crypto_get_default_rng();
0288     if (err)
0289         goto out;
0290 
0291     err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
0292                    crypto_aead_ivsize(aead));
0293     crypto_put_default_rng();
0294     if (err)
0295         goto out;
0296 
0297     ctx->sknull = crypto_get_default_null_skcipher2();
0298     err = PTR_ERR(ctx->sknull);
0299     if (IS_ERR(ctx->sknull))
0300         goto out;
0301 
0302     child = crypto_spawn_aead(aead_instance_ctx(inst));
0303     err = PTR_ERR(child);
0304     if (IS_ERR(child))
0305         goto drop_null;
0306 
0307     ctx->child = child;
0308     crypto_aead_set_reqsize(aead, crypto_aead_reqsize(child) +
0309                       sizeof(struct aead_request));
0310 
0311     err = 0;
0312 
0313 out:
0314     return err;
0315 
0316 drop_null:
0317     crypto_put_default_null_skcipher2();
0318     goto out;
0319 }
0320 EXPORT_SYMBOL_GPL(aead_init_geniv);
0321 
0322 void aead_exit_geniv(struct crypto_aead *tfm)
0323 {
0324     struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm);
0325 
0326     crypto_free_aead(ctx->child);
0327     crypto_put_default_null_skcipher2();
0328 }
0329 EXPORT_SYMBOL_GPL(aead_exit_geniv);
0330 
0331 int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name,
0332              u32 type, u32 mask)
0333 {
0334     spawn->base.frontend = &crypto_aead_type;
0335     return crypto_grab_spawn(&spawn->base, name, type, mask);
0336 }
0337 EXPORT_SYMBOL_GPL(crypto_grab_aead);
0338 
0339 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask)
0340 {
0341     return crypto_alloc_tfm(alg_name, &crypto_aead_type, type, mask);
0342 }
0343 EXPORT_SYMBOL_GPL(crypto_alloc_aead);
0344 
0345 static int aead_prepare_alg(struct aead_alg *alg)
0346 {
0347     struct crypto_alg *base = &alg->base;
0348 
0349     if (max3(alg->maxauthsize, alg->ivsize, alg->chunksize) >
0350         PAGE_SIZE / 8)
0351         return -EINVAL;
0352 
0353     if (!alg->chunksize)
0354         alg->chunksize = base->cra_blocksize;
0355 
0356     base->cra_type = &crypto_aead_type;
0357     base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
0358     base->cra_flags |= CRYPTO_ALG_TYPE_AEAD;
0359 
0360     return 0;
0361 }
0362 
0363 int crypto_register_aead(struct aead_alg *alg)
0364 {
0365     struct crypto_alg *base = &alg->base;
0366     int err;
0367 
0368     err = aead_prepare_alg(alg);
0369     if (err)
0370         return err;
0371 
0372     return crypto_register_alg(base);
0373 }
0374 EXPORT_SYMBOL_GPL(crypto_register_aead);
0375 
0376 void crypto_unregister_aead(struct aead_alg *alg)
0377 {
0378     crypto_unregister_alg(&alg->base);
0379 }
0380 EXPORT_SYMBOL_GPL(crypto_unregister_aead);
0381 
0382 int crypto_register_aeads(struct aead_alg *algs, int count)
0383 {
0384     int i, ret;
0385 
0386     for (i = 0; i < count; i++) {
0387         ret = crypto_register_aead(&algs[i]);
0388         if (ret)
0389             goto err;
0390     }
0391 
0392     return 0;
0393 
0394 err:
0395     for (--i; i >= 0; --i)
0396         crypto_unregister_aead(&algs[i]);
0397 
0398     return ret;
0399 }
0400 EXPORT_SYMBOL_GPL(crypto_register_aeads);
0401 
0402 void crypto_unregister_aeads(struct aead_alg *algs, int count)
0403 {
0404     int i;
0405 
0406     for (i = count - 1; i >= 0; --i)
0407         crypto_unregister_aead(&algs[i]);
0408 }
0409 EXPORT_SYMBOL_GPL(crypto_unregister_aeads);
0410 
0411 int aead_register_instance(struct crypto_template *tmpl,
0412                struct aead_instance *inst)
0413 {
0414     int err;
0415 
0416     err = aead_prepare_alg(&inst->alg);
0417     if (err)
0418         return err;
0419 
0420     return crypto_register_instance(tmpl, aead_crypto_instance(inst));
0421 }
0422 EXPORT_SYMBOL_GPL(aead_register_instance);
0423 
0424 MODULE_LICENSE("GPL");
0425 MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)");