Blame view
crypto/aead.c
11 KB
2874c5fd2 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
1ae978208 [CRYPTO] api: Add... |
2 3 |
/* * AEAD: Authenticated Encryption with Associated Data |
3922538fe crypto: aead - Fi... |
4 |
* |
1ae978208 [CRYPTO] api: Add... |
5 6 |
* This file provides API support for AEAD algorithms. * |
b0d955ba4 crypto: aead - Re... |
7 |
* Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au> |
1ae978208 [CRYPTO] api: Add... |
8 |
*/ |
6350449fb crypto: aead - Ad... |
9 |
#include <crypto/internal/geniv.h> |
149a39717 crypto: aead - Ad... |
10 11 |
#include <crypto/internal/rng.h> #include <crypto/null.h> |
996d98d85 crypto: aead - Ad... |
12 |
#include <crypto/scatterwalk.h> |
5b6d2d7fd [CRYPTO] aead: Ad... |
13 |
#include <linux/err.h> |
1ae978208 [CRYPTO] api: Add... |
14 15 16 |
#include <linux/init.h> #include <linux/kernel.h> #include <linux/module.h> |
d29ce988a [CRYPTO] aead: Cr... |
17 |
#include <linux/rtnetlink.h> |
1ae978208 [CRYPTO] api: Add... |
18 19 |
#include <linux/slab.h> #include <linux/seq_file.h> |
6ad414fe7 crypto: Add users... |
20 |
#include <linux/cryptouser.h> |
d8c34b949 crypto: Replaced ... |
21 |
#include <linux/compiler.h> |
6ad414fe7 crypto: Add users... |
22 |
#include <net/netlink.h> |
1ae978208 [CRYPTO] api: Add... |
23 |
|
5b6d2d7fd [CRYPTO] aead: Ad... |
24 |
#include "internal.h" |
1ae978208 [CRYPTO] api: Add... |
25 26 27 |
static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key, unsigned int keylen) { |
1ae978208 [CRYPTO] api: Add... |
28 29 30 31 32 33 34 35 36 37 38 39 |
unsigned long alignmask = crypto_aead_alignmask(tfm); int ret; u8 *buffer, *alignbuffer; unsigned long absize; absize = keylen + alignmask; buffer = kmalloc(absize, GFP_ATOMIC); if (!buffer) return -ENOMEM; alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); memcpy(alignbuffer, key, keylen); |
b0d955ba4 crypto: aead - Re... |
40 |
ret = crypto_aead_alg(tfm)->setkey(tfm, alignbuffer, keylen); |
1ae978208 [CRYPTO] api: Add... |
41 42 43 44 |
memset(alignbuffer, 0, keylen); kfree(buffer); return ret; } |
5d1d65f8b crypto: aead - Co... |
45 46 |
int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key, unsigned int keylen) |
1ae978208 [CRYPTO] api: Add... |
47 |
{ |
1ae978208 [CRYPTO] api: Add... |
48 |
unsigned long alignmask = crypto_aead_alignmask(tfm); |
dc26c17f7 crypto: aead - pr... |
49 |
int err; |
1ae978208 [CRYPTO] api: Add... |
50 51 |
if ((unsigned long)key & alignmask) |
dc26c17f7 crypto: aead - pr... |
52 53 54 |
err = setkey_unaligned(tfm, key, keylen); else err = crypto_aead_alg(tfm)->setkey(tfm, key, keylen); |
6ebc97006 crypto: aead - se... |
55 56 |
if (unlikely(err)) { crypto_aead_set_flags(tfm, CRYPTO_TFM_NEED_KEY); |
dc26c17f7 crypto: aead - pr... |
57 |
return err; |
6ebc97006 crypto: aead - se... |
58 |
} |
1ae978208 [CRYPTO] api: Add... |
59 |
|
dc26c17f7 crypto: aead - pr... |
60 61 |
crypto_aead_clear_flags(tfm, CRYPTO_TFM_NEED_KEY); return 0; |
1ae978208 [CRYPTO] api: Add... |
62 |
} |
5d1d65f8b crypto: aead - Co... |
63 |
EXPORT_SYMBOL_GPL(crypto_aead_setkey); |
1ae978208 [CRYPTO] api: Add... |
64 |
|
7ba683a6d [CRYPTO] aead: Ma... |
65 66 67 |
int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize) { int err; |
a62084d29 crypto: aead - Do... |
68 69 |
if ((!authsize && crypto_aead_maxauthsize(tfm)) || authsize > crypto_aead_maxauthsize(tfm)) |
7ba683a6d [CRYPTO] aead: Ma... |
70 |
return -EINVAL; |
b0d955ba4 crypto: aead - Re... |
71 72 |
if (crypto_aead_alg(tfm)->setauthsize) { err = crypto_aead_alg(tfm)->setauthsize(tfm, authsize); |
7ba683a6d [CRYPTO] aead: Ma... |
73 74 75 |
if (err) return err; } |
5d1d65f8b crypto: aead - Co... |
76 |
tfm->authsize = authsize; |
7ba683a6d [CRYPTO] aead: Ma... |
77 78 79 |
return 0; } EXPORT_SYMBOL_GPL(crypto_aead_setauthsize); |
f2fe11545 crypto: aead - un... |
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
int crypto_aead_encrypt(struct aead_request *req) { struct crypto_aead *aead = crypto_aead_reqtfm(req); struct crypto_alg *alg = aead->base.__crt_alg; unsigned int cryptlen = req->cryptlen; int ret; crypto_stats_get(alg); if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY) ret = -ENOKEY; else ret = crypto_aead_alg(aead)->encrypt(req); crypto_stats_aead_encrypt(cryptlen, alg, ret); return ret; } EXPORT_SYMBOL_GPL(crypto_aead_encrypt); int crypto_aead_decrypt(struct aead_request *req) { struct crypto_aead *aead = crypto_aead_reqtfm(req); struct crypto_alg *alg = aead->base.__crt_alg; unsigned int cryptlen = req->cryptlen; int ret; crypto_stats_get(alg); if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY) ret = -ENOKEY; else if (req->cryptlen < crypto_aead_authsize(aead)) ret = -EINVAL; else ret = crypto_aead_alg(aead)->decrypt(req); crypto_stats_aead_decrypt(cryptlen, alg, ret); return ret; } EXPORT_SYMBOL_GPL(crypto_aead_decrypt); |
5eb8ec6dc crypto: aead - Ad... |
115 116 117 118 119 120 121 |
static void crypto_aead_exit_tfm(struct crypto_tfm *tfm) { struct crypto_aead *aead = __crypto_aead_cast(tfm); struct aead_alg *alg = crypto_aead_alg(aead); alg->exit(aead); } |
63293c611 crypto: aead - Ad... |
122 123 124 125 |
static int crypto_aead_init_tfm(struct crypto_tfm *tfm) { struct crypto_aead *aead = __crypto_aead_cast(tfm); struct aead_alg *alg = crypto_aead_alg(aead); |
dc26c17f7 crypto: aead - pr... |
126 |
crypto_aead_set_flags(aead, CRYPTO_TFM_NEED_KEY); |
63293c611 crypto: aead - Ad... |
127 |
aead->authsize = alg->maxauthsize; |
5eb8ec6dc crypto: aead - Ad... |
128 129 130 131 132 |
if (alg->exit) aead->base.exit = crypto_aead_exit_tfm; if (alg->init) return alg->init(aead); |
63293c611 crypto: aead - Ad... |
133 134 |
return 0; } |
3acc84739 crypto: algapi - ... |
135 |
#ifdef CONFIG_NET |
63293c611 crypto: aead - Ad... |
136 137 138 139 |
static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg) { struct crypto_report_aead raead; struct aead_alg *aead = container_of(alg, struct aead_alg, base); |
37db69e0b crypto: user - cl... |
140 141 142 143 |
memset(&raead, 0, sizeof(raead)); strscpy(raead.type, "aead", sizeof(raead.type)); strscpy(raead.geniv, "<none>", sizeof(raead.geniv)); |
63293c611 crypto: aead - Ad... |
144 145 146 147 |
raead.blocksize = alg->cra_blocksize; raead.maxauthsize = aead->maxauthsize; raead.ivsize = aead->ivsize; |
37db69e0b crypto: user - cl... |
148 |
return nla_put(skb, CRYPTOCFGA_REPORT_AEAD, sizeof(raead), &raead); |
63293c611 crypto: aead - Ad... |
149 150 151 152 153 154 155 156 157 |
} #else static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg) { return -ENOSYS; } #endif static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg) |
d8c34b949 crypto: Replaced ... |
158 |
__maybe_unused; |
63293c611 crypto: aead - Ad... |
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg) { struct aead_alg *aead = container_of(alg, struct aead_alg, base); seq_printf(m, "type : aead "); seq_printf(m, "async : %s ", alg->cra_flags & CRYPTO_ALG_ASYNC ? "yes" : "no"); seq_printf(m, "blocksize : %u ", alg->cra_blocksize); seq_printf(m, "ivsize : %u ", aead->ivsize); seq_printf(m, "maxauthsize : %u ", aead->maxauthsize); seq_printf(m, "geniv : <none> "); } |
ba75e15fa crypto: aead - Ad... |
177 178 179 180 181 182 183 184 185 186 187 |
static void crypto_aead_free_instance(struct crypto_instance *inst) { struct aead_instance *aead = aead_instance(inst); if (!aead->free) { inst->tmpl->free(inst); return; } aead->free(aead); } |
b0d955ba4 crypto: aead - Re... |
188 |
static const struct crypto_type crypto_aead_type = { |
63293c611 crypto: aead - Ad... |
189 190 |
.extsize = crypto_alg_extsize, .init_tfm = crypto_aead_init_tfm, |
ba75e15fa crypto: aead - Ad... |
191 |
.free = crypto_aead_free_instance, |
63293c611 crypto: aead - Ad... |
192 193 194 195 196 197 198 199 200 |
#ifdef CONFIG_PROC_FS .show = crypto_aead_show, #endif .report = crypto_aead_report, .maskclear = ~CRYPTO_ALG_TYPE_MASK, .maskset = CRYPTO_ALG_TYPE_MASK, .type = CRYPTO_ALG_TYPE_AEAD, .tfmsize = offsetof(struct crypto_aead, base), }; |
6350449fb crypto: aead - Ad... |
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
static int aead_geniv_setkey(struct crypto_aead *tfm, const u8 *key, unsigned int keylen) { struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm); return crypto_aead_setkey(ctx->child, key, keylen); } static int aead_geniv_setauthsize(struct crypto_aead *tfm, unsigned int authsize) { struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm); return crypto_aead_setauthsize(ctx->child, authsize); } |
856e3f409 crypto: seqiv - A... |
216 217 |
struct aead_instance *aead_geniv_alloc(struct crypto_template *tmpl, struct rtattr **tb, u32 type, u32 mask) |
5b6d2d7fd [CRYPTO] aead: Ad... |
218 219 220 221 |
{ const char *name; struct crypto_aead_spawn *spawn; struct crypto_attr_type *algt; |
856e3f409 crypto: seqiv - A... |
222 223 224 225 |
struct aead_instance *inst; struct aead_alg *alg; unsigned int ivsize; unsigned int maxauthsize; |
5b6d2d7fd [CRYPTO] aead: Ad... |
226 227 228 |
int err; algt = crypto_get_attr_type(tb); |
5b6d2d7fd [CRYPTO] aead: Ad... |
229 |
if (IS_ERR(algt)) |
3e8afe35c crypto: use ERR_CAST |
230 |
return ERR_CAST(algt); |
5b6d2d7fd [CRYPTO] aead: Ad... |
231 |
|
5e4b8c1fc crypto: aead - Re... |
232 |
if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) |
5b6d2d7fd [CRYPTO] aead: Ad... |
233 234 235 |
return ERR_PTR(-EINVAL); name = crypto_attr_alg_name(tb[1]); |
5b6d2d7fd [CRYPTO] aead: Ad... |
236 |
if (IS_ERR(name)) |
3e8afe35c crypto: use ERR_CAST |
237 |
return ERR_CAST(name); |
5b6d2d7fd [CRYPTO] aead: Ad... |
238 239 240 241 |
inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); if (!inst) return ERR_PTR(-ENOMEM); |
856e3f409 crypto: seqiv - A... |
242 |
spawn = aead_instance_ctx(inst); |
5b6d2d7fd [CRYPTO] aead: Ad... |
243 244 245 |
/* Ignore async algorithms if necessary. */ mask |= crypto_requires_sync(algt->type, algt->mask); |
856e3f409 crypto: seqiv - A... |
246 |
crypto_set_aead_spawn(spawn, aead_crypto_instance(inst)); |
b0d955ba4 crypto: aead - Re... |
247 |
err = crypto_grab_aead(spawn, name, type, mask); |
5b6d2d7fd [CRYPTO] aead: Ad... |
248 249 |
if (err) goto err_free_inst; |
856e3f409 crypto: seqiv - A... |
250 |
alg = crypto_spawn_aead_alg(spawn); |
30e4c010a crypto: aead - Ad... |
251 252 |
ivsize = crypto_aead_alg_ivsize(alg); maxauthsize = crypto_aead_alg_maxauthsize(alg); |
5b6d2d7fd [CRYPTO] aead: Ad... |
253 254 |
err = -EINVAL; |
6350449fb crypto: aead - Ad... |
255 |
if (ivsize < sizeof(u64)) |
5b6d2d7fd [CRYPTO] aead: Ad... |
256 |
goto err_drop_alg; |
856e3f409 crypto: seqiv - A... |
257 258 259 260 261 262 263 264 265 |
err = -ENAMETOOLONG; if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", tmpl->name, alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME) goto err_drop_alg; if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", tmpl->name, alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME) goto err_drop_alg; |
5b6d2d7fd [CRYPTO] aead: Ad... |
266 |
|
5e4b8c1fc crypto: aead - Re... |
267 |
inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC; |
856e3f409 crypto: seqiv - A... |
268 269 270 |
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; |
6350449fb crypto: aead - Ad... |
271 272 273 274 |
inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx); inst->alg.setkey = aead_geniv_setkey; inst->alg.setauthsize = aead_geniv_setauthsize; |
5b6d2d7fd [CRYPTO] aead: Ad... |
275 |
|
856e3f409 crypto: seqiv - A... |
276 277 |
inst->alg.ivsize = ivsize; inst->alg.maxauthsize = maxauthsize; |
5b6d2d7fd [CRYPTO] aead: Ad... |
278 279 280 281 282 283 284 285 286 287 288 289 |
out: return inst; err_drop_alg: crypto_drop_aead(spawn); err_free_inst: kfree(inst); inst = ERR_PTR(err); goto out; } EXPORT_SYMBOL_GPL(aead_geniv_alloc); |
856e3f409 crypto: seqiv - A... |
290 |
void aead_geniv_free(struct aead_instance *inst) |
5b6d2d7fd [CRYPTO] aead: Ad... |
291 |
{ |
856e3f409 crypto: seqiv - A... |
292 |
crypto_drop_aead(aead_instance_ctx(inst)); |
5b6d2d7fd [CRYPTO] aead: Ad... |
293 294 295 |
kfree(inst); } EXPORT_SYMBOL_GPL(aead_geniv_free); |
149a39717 crypto: aead - Ad... |
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
int aead_init_geniv(struct crypto_aead *aead) { struct aead_geniv_ctx *ctx = crypto_aead_ctx(aead); struct aead_instance *inst = aead_alg_instance(aead); struct crypto_aead *child; int err; spin_lock_init(&ctx->lock); err = crypto_get_default_rng(); if (err) goto out; err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt, crypto_aead_ivsize(aead)); crypto_put_default_rng(); if (err) goto out; |
3a2d4fb51 crypto: null - Ge... |
314 |
ctx->sknull = crypto_get_default_null_skcipher(); |
ca0494c09 crypto: aead - Ad... |
315 316 317 |
err = PTR_ERR(ctx->sknull); if (IS_ERR(ctx->sknull)) goto out; |
149a39717 crypto: aead - Ad... |
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
child = crypto_spawn_aead(aead_instance_ctx(inst)); err = PTR_ERR(child); if (IS_ERR(child)) goto drop_null; ctx->child = child; crypto_aead_set_reqsize(aead, crypto_aead_reqsize(child) + sizeof(struct aead_request)); err = 0; out: return err; drop_null: |
3a2d4fb51 crypto: null - Ge... |
333 |
crypto_put_default_null_skcipher(); |
149a39717 crypto: aead - Ad... |
334 335 336 337 338 339 340 341 342 |
goto out; } EXPORT_SYMBOL_GPL(aead_init_geniv); void aead_exit_geniv(struct crypto_aead *tfm) { struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm); crypto_free_aead(ctx->child); |
3a2d4fb51 crypto: null - Ge... |
343 |
crypto_put_default_null_skcipher(); |
149a39717 crypto: aead - Ad... |
344 345 |
} EXPORT_SYMBOL_GPL(aead_exit_geniv); |
d29ce988a [CRYPTO] aead: Cr... |
346 347 348 |
int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name, u32 type, u32 mask) { |
5d1d65f8b crypto: aead - Co... |
349 350 |
spawn->base.frontend = &crypto_aead_type; return crypto_grab_spawn(&spawn->base, name, type, mask); |
d29ce988a [CRYPTO] aead: Cr... |
351 352 353 354 355 |
} EXPORT_SYMBOL_GPL(crypto_grab_aead); struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask) { |
5d1d65f8b crypto: aead - Co... |
356 |
return crypto_alloc_tfm(alg_name, &crypto_aead_type, type, mask); |
d29ce988a [CRYPTO] aead: Cr... |
357 358 |
} EXPORT_SYMBOL_GPL(crypto_alloc_aead); |
63293c611 crypto: aead - Ad... |
359 360 361 |
static int aead_prepare_alg(struct aead_alg *alg) { struct crypto_alg *base = &alg->base; |
7a530aa9c crypto: aead - Ad... |
362 363 |
if (max3(alg->maxauthsize, alg->ivsize, alg->chunksize) > PAGE_SIZE / 8) |
63293c611 crypto: aead - Ad... |
364 |
return -EINVAL; |
7a530aa9c crypto: aead - Ad... |
365 366 |
if (!alg->chunksize) alg->chunksize = base->cra_blocksize; |
b0d955ba4 crypto: aead - Re... |
367 |
base->cra_type = &crypto_aead_type; |
63293c611 crypto: aead - Ad... |
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; base->cra_flags |= CRYPTO_ALG_TYPE_AEAD; return 0; } int crypto_register_aead(struct aead_alg *alg) { struct crypto_alg *base = &alg->base; int err; err = aead_prepare_alg(alg); if (err) return err; return crypto_register_alg(base); } EXPORT_SYMBOL_GPL(crypto_register_aead); |
43615369a crypto: aead - Ig... |
386 |
void crypto_unregister_aead(struct aead_alg *alg) |
63293c611 crypto: aead - Ad... |
387 |
{ |
43615369a crypto: aead - Ig... |
388 |
crypto_unregister_alg(&alg->base); |
63293c611 crypto: aead - Ad... |
389 390 |
} EXPORT_SYMBOL_GPL(crypto_unregister_aead); |
caab94612 crypto: aead - Ad... |
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
int crypto_register_aeads(struct aead_alg *algs, int count) { int i, ret; for (i = 0; i < count; i++) { ret = crypto_register_aead(&algs[i]); if (ret) goto err; } return 0; err: for (--i; i >= 0; --i) crypto_unregister_aead(&algs[i]); return ret; } EXPORT_SYMBOL_GPL(crypto_register_aeads); void crypto_unregister_aeads(struct aead_alg *algs, int count) { int i; for (i = count - 1; i >= 0; --i) crypto_unregister_aead(&algs[i]); } EXPORT_SYMBOL_GPL(crypto_unregister_aeads); |
63293c611 crypto: aead - Ad... |
419 420 421 422 423 424 425 426 427 428 429 430 |
int aead_register_instance(struct crypto_template *tmpl, struct aead_instance *inst) { int err; err = aead_prepare_alg(&inst->alg); if (err) return err; return crypto_register_instance(tmpl, aead_crypto_instance(inst)); } EXPORT_SYMBOL_GPL(aead_register_instance); |
1ae978208 [CRYPTO] api: Add... |
431 432 |
MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)"); |