Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 2 | /* |
| 3 | * AEAD: Authenticated Encryption with Associated Data |
Richard Hartmann | 3922538 | 2010-02-16 20:24:07 +0800 | [diff] [blame] | 4 | * |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 5 | * This file provides API support for AEAD algorithms. |
| 6 | * |
Herbert Xu | b0d955b | 2015-08-14 15:30:41 +0800 | [diff] [blame] | 7 | * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au> |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 8 | */ |
| 9 | |
Herbert Xu | 20cc01b | 2019-11-08 18:41:58 +0800 | [diff] [blame] | 10 | #include <crypto/internal/aead.h> |
| 11 | #include <linux/errno.h> |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 12 | #include <linux/init.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/seq_file.h> |
Steffen Klassert | 6ad414f | 2011-09-27 07:44:27 +0200 | [diff] [blame] | 17 | #include <linux/cryptouser.h> |
| 18 | #include <net/netlink.h> |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 19 | |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 20 | #include "internal.h" |
| 21 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 22 | static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key, |
| 23 | unsigned int keylen) |
| 24 | { |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 25 | unsigned long alignmask = crypto_aead_alignmask(tfm); |
| 26 | int ret; |
| 27 | u8 *buffer, *alignbuffer; |
| 28 | unsigned long absize; |
| 29 | |
| 30 | absize = keylen + alignmask; |
| 31 | buffer = kmalloc(absize, GFP_ATOMIC); |
| 32 | if (!buffer) |
| 33 | return -ENOMEM; |
| 34 | |
| 35 | alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); |
| 36 | memcpy(alignbuffer, key, keylen); |
Herbert Xu | b0d955b | 2015-08-14 15:30:41 +0800 | [diff] [blame] | 37 | ret = crypto_aead_alg(tfm)->setkey(tfm, alignbuffer, keylen); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 38 | memset(alignbuffer, 0, keylen); |
| 39 | kfree(buffer); |
| 40 | return ret; |
| 41 | } |
| 42 | |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 43 | int crypto_aead_setkey(struct crypto_aead *tfm, |
| 44 | const u8 *key, unsigned int keylen) |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 45 | { |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 46 | unsigned long alignmask = crypto_aead_alignmask(tfm); |
Eric Biggers | dc26c17 | 2018-01-03 11:16:30 -0800 | [diff] [blame] | 47 | int err; |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 48 | |
| 49 | if ((unsigned long)key & alignmask) |
Eric Biggers | dc26c17 | 2018-01-03 11:16:30 -0800 | [diff] [blame] | 50 | err = setkey_unaligned(tfm, key, keylen); |
| 51 | else |
| 52 | err = crypto_aead_alg(tfm)->setkey(tfm, key, keylen); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 53 | |
Eric Biggers | 6ebc970 | 2019-01-06 18:47:44 -0800 | [diff] [blame] | 54 | if (unlikely(err)) { |
| 55 | crypto_aead_set_flags(tfm, CRYPTO_TFM_NEED_KEY); |
Eric Biggers | dc26c17 | 2018-01-03 11:16:30 -0800 | [diff] [blame] | 56 | return err; |
Eric Biggers | 6ebc970 | 2019-01-06 18:47:44 -0800 | [diff] [blame] | 57 | } |
Eric Biggers | dc26c17 | 2018-01-03 11:16:30 -0800 | [diff] [blame] | 58 | |
| 59 | crypto_aead_clear_flags(tfm, CRYPTO_TFM_NEED_KEY); |
| 60 | return 0; |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 61 | } |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 62 | EXPORT_SYMBOL_GPL(crypto_aead_setkey); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 63 | |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 64 | int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize) |
| 65 | { |
| 66 | int err; |
| 67 | |
Pascal van Leeuwen | a62084d | 2019-08-09 17:51:07 +0200 | [diff] [blame] | 68 | if ((!authsize && crypto_aead_maxauthsize(tfm)) || |
| 69 | authsize > crypto_aead_maxauthsize(tfm)) |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 70 | return -EINVAL; |
| 71 | |
Herbert Xu | b0d955b | 2015-08-14 15:30:41 +0800 | [diff] [blame] | 72 | if (crypto_aead_alg(tfm)->setauthsize) { |
| 73 | err = crypto_aead_alg(tfm)->setauthsize(tfm, authsize); |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 74 | if (err) |
| 75 | return err; |
| 76 | } |
| 77 | |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 78 | tfm->authsize = authsize; |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | EXPORT_SYMBOL_GPL(crypto_aead_setauthsize); |
| 82 | |
Eric Biggers | f2fe115 | 2019-06-02 22:45:16 -0700 | [diff] [blame] | 83 | int crypto_aead_encrypt(struct aead_request *req) |
| 84 | { |
| 85 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 86 | struct crypto_alg *alg = aead->base.__crt_alg; |
| 87 | unsigned int cryptlen = req->cryptlen; |
| 88 | int ret; |
| 89 | |
| 90 | crypto_stats_get(alg); |
| 91 | if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY) |
| 92 | ret = -ENOKEY; |
| 93 | else |
| 94 | ret = crypto_aead_alg(aead)->encrypt(req); |
| 95 | crypto_stats_aead_encrypt(cryptlen, alg, ret); |
| 96 | return ret; |
| 97 | } |
| 98 | EXPORT_SYMBOL_GPL(crypto_aead_encrypt); |
| 99 | |
| 100 | int crypto_aead_decrypt(struct aead_request *req) |
| 101 | { |
| 102 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 103 | struct crypto_alg *alg = aead->base.__crt_alg; |
| 104 | unsigned int cryptlen = req->cryptlen; |
| 105 | int ret; |
| 106 | |
| 107 | crypto_stats_get(alg); |
| 108 | if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY) |
| 109 | ret = -ENOKEY; |
| 110 | else if (req->cryptlen < crypto_aead_authsize(aead)) |
| 111 | ret = -EINVAL; |
| 112 | else |
| 113 | ret = crypto_aead_alg(aead)->decrypt(req); |
| 114 | crypto_stats_aead_decrypt(cryptlen, alg, ret); |
| 115 | return ret; |
| 116 | } |
| 117 | EXPORT_SYMBOL_GPL(crypto_aead_decrypt); |
| 118 | |
Herbert Xu | 5eb8ec6 | 2015-05-28 22:07:53 +0800 | [diff] [blame] | 119 | static void crypto_aead_exit_tfm(struct crypto_tfm *tfm) |
| 120 | { |
| 121 | struct crypto_aead *aead = __crypto_aead_cast(tfm); |
| 122 | struct aead_alg *alg = crypto_aead_alg(aead); |
| 123 | |
| 124 | alg->exit(aead); |
| 125 | } |
| 126 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 127 | static int crypto_aead_init_tfm(struct crypto_tfm *tfm) |
| 128 | { |
| 129 | struct crypto_aead *aead = __crypto_aead_cast(tfm); |
| 130 | struct aead_alg *alg = crypto_aead_alg(aead); |
| 131 | |
Eric Biggers | dc26c17 | 2018-01-03 11:16:30 -0800 | [diff] [blame] | 132 | crypto_aead_set_flags(aead, CRYPTO_TFM_NEED_KEY); |
| 133 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 134 | aead->authsize = alg->maxauthsize; |
| 135 | |
Herbert Xu | 5eb8ec6 | 2015-05-28 22:07:53 +0800 | [diff] [blame] | 136 | if (alg->exit) |
| 137 | aead->base.exit = crypto_aead_exit_tfm; |
| 138 | |
| 139 | if (alg->init) |
| 140 | return alg->init(aead); |
| 141 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
Herbert Xu | 3acc847 | 2011-11-03 23:46:07 +1100 | [diff] [blame] | 145 | #ifdef CONFIG_NET |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 146 | static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg) |
| 147 | { |
| 148 | struct crypto_report_aead raead; |
| 149 | struct aead_alg *aead = container_of(alg, struct aead_alg, base); |
| 150 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 151 | memset(&raead, 0, sizeof(raead)); |
| 152 | |
| 153 | strscpy(raead.type, "aead", sizeof(raead.type)); |
| 154 | strscpy(raead.geniv, "<none>", sizeof(raead.geniv)); |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 155 | |
| 156 | raead.blocksize = alg->cra_blocksize; |
| 157 | raead.maxauthsize = aead->maxauthsize; |
| 158 | raead.ivsize = aead->ivsize; |
| 159 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 160 | return nla_put(skb, CRYPTOCFGA_REPORT_AEAD, sizeof(raead), &raead); |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 161 | } |
| 162 | #else |
Herbert Xu | 3acc847 | 2011-11-03 23:46:07 +1100 | [diff] [blame] | 163 | static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg) |
| 164 | { |
| 165 | return -ENOSYS; |
| 166 | } |
| 167 | #endif |
Steffen Klassert | 6ad414f | 2011-09-27 07:44:27 +0200 | [diff] [blame] | 168 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 169 | static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg) |
Gideon Israel Dsouza | d8c34b9 | 2016-12-31 21:26:23 +0530 | [diff] [blame] | 170 | __maybe_unused; |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 171 | static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg) |
| 172 | { |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 173 | struct aead_alg *aead = container_of(alg, struct aead_alg, base); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 174 | |
| 175 | seq_printf(m, "type : aead\n"); |
Herbert Xu | 189ed66 | 2007-12-14 22:29:37 +0800 | [diff] [blame] | 176 | seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ? |
| 177 | "yes" : "no"); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 178 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); |
| 179 | seq_printf(m, "ivsize : %u\n", aead->ivsize); |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 180 | seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize); |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 181 | seq_printf(m, "geniv : <none>\n"); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 182 | } |
| 183 | |
Herbert Xu | ba75e15 | 2015-07-09 07:17:17 +0800 | [diff] [blame] | 184 | static void crypto_aead_free_instance(struct crypto_instance *inst) |
| 185 | { |
| 186 | struct aead_instance *aead = aead_instance(inst); |
| 187 | |
Herbert Xu | ba75e15 | 2015-07-09 07:17:17 +0800 | [diff] [blame] | 188 | aead->free(aead); |
| 189 | } |
| 190 | |
Herbert Xu | b0d955b | 2015-08-14 15:30:41 +0800 | [diff] [blame] | 191 | static const struct crypto_type crypto_aead_type = { |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 192 | .extsize = crypto_alg_extsize, |
| 193 | .init_tfm = crypto_aead_init_tfm, |
Herbert Xu | ba75e15 | 2015-07-09 07:17:17 +0800 | [diff] [blame] | 194 | .free = crypto_aead_free_instance, |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 195 | #ifdef CONFIG_PROC_FS |
| 196 | .show = crypto_aead_show, |
| 197 | #endif |
Steffen Klassert | 6ad414f | 2011-09-27 07:44:27 +0200 | [diff] [blame] | 198 | .report = crypto_aead_report, |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 199 | .maskclear = ~CRYPTO_ALG_TYPE_MASK, |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 200 | .maskset = CRYPTO_ALG_TYPE_MASK, |
| 201 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 202 | .tfmsize = offsetof(struct crypto_aead, base), |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 203 | }; |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 204 | |
Eric Biggers | cd900f0 | 2020-01-02 19:58:46 -0800 | [diff] [blame] | 205 | int crypto_grab_aead(struct crypto_aead_spawn *spawn, |
| 206 | struct crypto_instance *inst, |
| 207 | const char *name, u32 type, u32 mask) |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 208 | { |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 209 | spawn->base.frontend = &crypto_aead_type; |
Eric Biggers | de95c95 | 2020-01-02 19:58:48 -0800 | [diff] [blame] | 210 | return crypto_grab_spawn(&spawn->base, inst, name, type, mask); |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 211 | } |
| 212 | EXPORT_SYMBOL_GPL(crypto_grab_aead); |
| 213 | |
| 214 | struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask) |
| 215 | { |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 216 | return crypto_alloc_tfm(alg_name, &crypto_aead_type, type, mask); |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 217 | } |
| 218 | EXPORT_SYMBOL_GPL(crypto_alloc_aead); |
| 219 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 220 | static int aead_prepare_alg(struct aead_alg *alg) |
| 221 | { |
| 222 | struct crypto_alg *base = &alg->base; |
| 223 | |
Herbert Xu | 7a530aa | 2016-07-12 13:17:33 +0800 | [diff] [blame] | 224 | if (max3(alg->maxauthsize, alg->ivsize, alg->chunksize) > |
| 225 | PAGE_SIZE / 8) |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 226 | return -EINVAL; |
| 227 | |
Herbert Xu | 7a530aa | 2016-07-12 13:17:33 +0800 | [diff] [blame] | 228 | if (!alg->chunksize) |
| 229 | alg->chunksize = base->cra_blocksize; |
| 230 | |
Herbert Xu | b0d955b | 2015-08-14 15:30:41 +0800 | [diff] [blame] | 231 | base->cra_type = &crypto_aead_type; |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 232 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; |
| 233 | base->cra_flags |= CRYPTO_ALG_TYPE_AEAD; |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | int crypto_register_aead(struct aead_alg *alg) |
| 239 | { |
| 240 | struct crypto_alg *base = &alg->base; |
| 241 | int err; |
| 242 | |
| 243 | err = aead_prepare_alg(alg); |
| 244 | if (err) |
| 245 | return err; |
| 246 | |
| 247 | return crypto_register_alg(base); |
| 248 | } |
| 249 | EXPORT_SYMBOL_GPL(crypto_register_aead); |
| 250 | |
Herbert Xu | 4361536 | 2015-05-28 22:07:57 +0800 | [diff] [blame] | 251 | void crypto_unregister_aead(struct aead_alg *alg) |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 252 | { |
Herbert Xu | 4361536 | 2015-05-28 22:07:57 +0800 | [diff] [blame] | 253 | crypto_unregister_alg(&alg->base); |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 254 | } |
| 255 | EXPORT_SYMBOL_GPL(crypto_unregister_aead); |
| 256 | |
Herbert Xu | caab946 | 2015-05-28 22:07:59 +0800 | [diff] [blame] | 257 | int crypto_register_aeads(struct aead_alg *algs, int count) |
| 258 | { |
| 259 | int i, ret; |
| 260 | |
| 261 | for (i = 0; i < count; i++) { |
| 262 | ret = crypto_register_aead(&algs[i]); |
| 263 | if (ret) |
| 264 | goto err; |
| 265 | } |
| 266 | |
| 267 | return 0; |
| 268 | |
| 269 | err: |
| 270 | for (--i; i >= 0; --i) |
| 271 | crypto_unregister_aead(&algs[i]); |
| 272 | |
| 273 | return ret; |
| 274 | } |
| 275 | EXPORT_SYMBOL_GPL(crypto_register_aeads); |
| 276 | |
| 277 | void crypto_unregister_aeads(struct aead_alg *algs, int count) |
| 278 | { |
| 279 | int i; |
| 280 | |
| 281 | for (i = count - 1; i >= 0; --i) |
| 282 | crypto_unregister_aead(&algs[i]); |
| 283 | } |
| 284 | EXPORT_SYMBOL_GPL(crypto_unregister_aeads); |
| 285 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 286 | int aead_register_instance(struct crypto_template *tmpl, |
| 287 | struct aead_instance *inst) |
| 288 | { |
| 289 | int err; |
| 290 | |
Eric Biggers | d4fdc2d | 2020-01-02 20:04:40 -0800 | [diff] [blame] | 291 | if (WARN_ON(!inst->free)) |
| 292 | return -EINVAL; |
| 293 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 294 | err = aead_prepare_alg(&inst->alg); |
| 295 | if (err) |
| 296 | return err; |
| 297 | |
| 298 | return crypto_register_instance(tmpl, aead_crypto_instance(inst)); |
| 299 | } |
| 300 | EXPORT_SYMBOL_GPL(aead_register_instance); |
| 301 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 302 | MODULE_LICENSE("GPL"); |
| 303 | MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)"); |