Blame view
crypto/aead.c
7.24 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 |
*/ |
20cc01baa crypto: aead - Sp... |
9 10 |
#include <crypto/internal/aead.h> #include <linux/errno.h> |
1ae978208 [CRYPTO] api: Add... |
11 12 13 14 15 |
#include <linux/init.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/seq_file.h> |
6ad414fe7 crypto: Add users... |
16 17 |
#include <linux/cryptouser.h> #include <net/netlink.h> |
1ae978208 [CRYPTO] api: Add... |
18 |
|
5b6d2d7fd [CRYPTO] aead: Ad... |
19 |
#include "internal.h" |
1ae978208 [CRYPTO] api: Add... |
20 21 22 |
static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key, unsigned int keylen) { |
1ae978208 [CRYPTO] api: Add... |
23 24 25 26 27 28 29 30 31 32 33 34 |
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... |
35 |
ret = crypto_aead_alg(tfm)->setkey(tfm, alignbuffer, keylen); |
1ae978208 [CRYPTO] api: Add... |
36 37 38 39 |
memset(alignbuffer, 0, keylen); kfree(buffer); return ret; } |
5d1d65f8b crypto: aead - Co... |
40 41 |
int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key, unsigned int keylen) |
1ae978208 [CRYPTO] api: Add... |
42 |
{ |
1ae978208 [CRYPTO] api: Add... |
43 |
unsigned long alignmask = crypto_aead_alignmask(tfm); |
dc26c17f7 crypto: aead - pr... |
44 |
int err; |
1ae978208 [CRYPTO] api: Add... |
45 46 |
if ((unsigned long)key & alignmask) |
dc26c17f7 crypto: aead - pr... |
47 48 49 |
err = setkey_unaligned(tfm, key, keylen); else err = crypto_aead_alg(tfm)->setkey(tfm, key, keylen); |
6ebc97006 crypto: aead - se... |
50 51 |
if (unlikely(err)) { crypto_aead_set_flags(tfm, CRYPTO_TFM_NEED_KEY); |
dc26c17f7 crypto: aead - pr... |
52 |
return err; |
6ebc97006 crypto: aead - se... |
53 |
} |
1ae978208 [CRYPTO] api: Add... |
54 |
|
dc26c17f7 crypto: aead - pr... |
55 56 |
crypto_aead_clear_flags(tfm, CRYPTO_TFM_NEED_KEY); return 0; |
1ae978208 [CRYPTO] api: Add... |
57 |
} |
5d1d65f8b crypto: aead - Co... |
58 |
EXPORT_SYMBOL_GPL(crypto_aead_setkey); |
1ae978208 [CRYPTO] api: Add... |
59 |
|
7ba683a6d [CRYPTO] aead: Ma... |
60 61 62 |
int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize) { int err; |
a62084d29 crypto: aead - Do... |
63 64 |
if ((!authsize && crypto_aead_maxauthsize(tfm)) || authsize > crypto_aead_maxauthsize(tfm)) |
7ba683a6d [CRYPTO] aead: Ma... |
65 |
return -EINVAL; |
b0d955ba4 crypto: aead - Re... |
66 67 |
if (crypto_aead_alg(tfm)->setauthsize) { err = crypto_aead_alg(tfm)->setauthsize(tfm, authsize); |
7ba683a6d [CRYPTO] aead: Ma... |
68 69 70 |
if (err) return err; } |
5d1d65f8b crypto: aead - Co... |
71 |
tfm->authsize = authsize; |
7ba683a6d [CRYPTO] aead: Ma... |
72 73 74 |
return 0; } EXPORT_SYMBOL_GPL(crypto_aead_setauthsize); |
f2fe11545 crypto: aead - un... |
75 76 77 78 79 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 |
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... |
110 111 112 113 114 115 116 |
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... |
117 118 119 120 |
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... |
121 |
crypto_aead_set_flags(aead, CRYPTO_TFM_NEED_KEY); |
63293c611 crypto: aead - Ad... |
122 |
aead->authsize = alg->maxauthsize; |
5eb8ec6dc crypto: aead - Ad... |
123 124 125 126 127 |
if (alg->exit) aead->base.exit = crypto_aead_exit_tfm; if (alg->init) return alg->init(aead); |
63293c611 crypto: aead - Ad... |
128 129 |
return 0; } |
3acc84739 crypto: algapi - ... |
130 |
#ifdef CONFIG_NET |
63293c611 crypto: aead - Ad... |
131 132 133 134 |
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... |
135 136 137 138 |
memset(&raead, 0, sizeof(raead)); strscpy(raead.type, "aead", sizeof(raead.type)); strscpy(raead.geniv, "<none>", sizeof(raead.geniv)); |
63293c611 crypto: aead - Ad... |
139 140 141 142 |
raead.blocksize = alg->cra_blocksize; raead.maxauthsize = aead->maxauthsize; raead.ivsize = aead->ivsize; |
37db69e0b crypto: user - cl... |
143 |
return nla_put(skb, CRYPTOCFGA_REPORT_AEAD, sizeof(raead), &raead); |
63293c611 crypto: aead - Ad... |
144 145 146 147 148 149 150 151 152 |
} #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 ... |
153 |
__maybe_unused; |
63293c611 crypto: aead - Ad... |
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
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... |
172 173 174 |
static void crypto_aead_free_instance(struct crypto_instance *inst) { struct aead_instance *aead = aead_instance(inst); |
ba75e15fa crypto: aead - Ad... |
175 176 |
aead->free(aead); } |
b0d955ba4 crypto: aead - Re... |
177 |
static const struct crypto_type crypto_aead_type = { |
63293c611 crypto: aead - Ad... |
178 179 |
.extsize = crypto_alg_extsize, .init_tfm = crypto_aead_init_tfm, |
ba75e15fa crypto: aead - Ad... |
180 |
.free = crypto_aead_free_instance, |
63293c611 crypto: aead - Ad... |
181 182 183 184 185 186 187 188 189 |
#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), }; |
cd900f0ca crypto: aead - pa... |
190 191 192 |
int crypto_grab_aead(struct crypto_aead_spawn *spawn, struct crypto_instance *inst, const char *name, u32 type, u32 mask) |
d29ce988a [CRYPTO] aead: Cr... |
193 |
{ |
5d1d65f8b crypto: aead - Co... |
194 |
spawn->base.frontend = &crypto_aead_type; |
de95c9574 crypto: algapi - ... |
195 |
return crypto_grab_spawn(&spawn->base, inst, name, type, mask); |
d29ce988a [CRYPTO] aead: Cr... |
196 197 198 199 200 |
} EXPORT_SYMBOL_GPL(crypto_grab_aead); struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask) { |
5d1d65f8b crypto: aead - Co... |
201 |
return crypto_alloc_tfm(alg_name, &crypto_aead_type, type, mask); |
d29ce988a [CRYPTO] aead: Cr... |
202 203 |
} EXPORT_SYMBOL_GPL(crypto_alloc_aead); |
63293c611 crypto: aead - Ad... |
204 205 206 |
static int aead_prepare_alg(struct aead_alg *alg) { struct crypto_alg *base = &alg->base; |
7a530aa9c crypto: aead - Ad... |
207 208 |
if (max3(alg->maxauthsize, alg->ivsize, alg->chunksize) > PAGE_SIZE / 8) |
63293c611 crypto: aead - Ad... |
209 |
return -EINVAL; |
7a530aa9c crypto: aead - Ad... |
210 211 |
if (!alg->chunksize) alg->chunksize = base->cra_blocksize; |
b0d955ba4 crypto: aead - Re... |
212 |
base->cra_type = &crypto_aead_type; |
63293c611 crypto: aead - Ad... |
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
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... |
231 |
void crypto_unregister_aead(struct aead_alg *alg) |
63293c611 crypto: aead - Ad... |
232 |
{ |
43615369a crypto: aead - Ig... |
233 |
crypto_unregister_alg(&alg->base); |
63293c611 crypto: aead - Ad... |
234 235 |
} EXPORT_SYMBOL_GPL(crypto_unregister_aead); |
caab94612 crypto: aead - Ad... |
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
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... |
264 265 266 267 |
int aead_register_instance(struct crypto_template *tmpl, struct aead_instance *inst) { int err; |
d4fdc2dfa crypto: algapi - ... |
268 269 |
if (WARN_ON(!inst->free)) return -EINVAL; |
63293c611 crypto: aead - Ad... |
270 271 272 273 274 275 276 |
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... |
277 278 |
MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)"); |