Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 1 | /* |
| 2 | * AEAD: Authenticated Encryption with Associated Data |
Richard Hartmann | 3922538 | 2010-02-16 20:24:07 +0800 | [diff] [blame] | 3 | * |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 4 | * This file provides API support for AEAD algorithms. |
| 5 | * |
| 6 | * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the Free |
Richard Hartmann | 3922538 | 2010-02-16 20:24:07 +0800 | [diff] [blame] | 10 | * Software Foundation; either version 2 of the License, or (at your option) |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 11 | * any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
Herbert Xu | 6350449 | 2015-05-27 14:37:30 +0800 | [diff] [blame] | 15 | #include <crypto/internal/geniv.h> |
Herbert Xu | 149a397 | 2015-08-13 17:28:58 +0800 | [diff] [blame^] | 16 | #include <crypto/internal/rng.h> |
| 17 | #include <crypto/null.h> |
Herbert Xu | 996d98d | 2015-05-21 15:11:01 +0800 | [diff] [blame] | 18 | #include <crypto/scatterwalk.h> |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 19 | #include <linux/err.h> |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 20 | #include <linux/init.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 23 | #include <linux/rtnetlink.h> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 24 | #include <linux/sched.h> |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 25 | #include <linux/slab.h> |
| 26 | #include <linux/seq_file.h> |
Steffen Klassert | 6ad414f | 2011-09-27 07:44:27 +0200 | [diff] [blame] | 27 | #include <linux/cryptouser.h> |
| 28 | #include <net/netlink.h> |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 29 | |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 30 | #include "internal.h" |
| 31 | |
Herbert Xu | 6350449 | 2015-05-27 14:37:30 +0800 | [diff] [blame] | 32 | struct compat_request_ctx { |
| 33 | struct scatterlist src[2]; |
| 34 | struct scatterlist dst[2]; |
| 35 | struct scatterlist ivbuf[2]; |
| 36 | struct scatterlist *ivsg; |
| 37 | struct aead_givcrypt_request subreq; |
| 38 | }; |
| 39 | |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 40 | static int aead_null_givencrypt(struct aead_givcrypt_request *req); |
| 41 | static int aead_null_givdecrypt(struct aead_givcrypt_request *req); |
| 42 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 43 | static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key, |
| 44 | unsigned int keylen) |
| 45 | { |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 46 | unsigned long alignmask = crypto_aead_alignmask(tfm); |
| 47 | int ret; |
| 48 | u8 *buffer, *alignbuffer; |
| 49 | unsigned long absize; |
| 50 | |
| 51 | absize = keylen + alignmask; |
| 52 | buffer = kmalloc(absize, GFP_ATOMIC); |
| 53 | if (!buffer) |
| 54 | return -ENOMEM; |
| 55 | |
| 56 | alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); |
| 57 | memcpy(alignbuffer, key, keylen); |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 58 | ret = tfm->setkey(tfm, alignbuffer, keylen); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 59 | memset(alignbuffer, 0, keylen); |
| 60 | kfree(buffer); |
| 61 | return ret; |
| 62 | } |
| 63 | |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 64 | int crypto_aead_setkey(struct crypto_aead *tfm, |
| 65 | const u8 *key, unsigned int keylen) |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 66 | { |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 67 | unsigned long alignmask = crypto_aead_alignmask(tfm); |
| 68 | |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 69 | tfm = tfm->child; |
| 70 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 71 | if ((unsigned long)key & alignmask) |
| 72 | return setkey_unaligned(tfm, key, keylen); |
| 73 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 74 | return tfm->setkey(tfm, key, keylen); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 75 | } |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 76 | EXPORT_SYMBOL_GPL(crypto_aead_setkey); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 77 | |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 78 | int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize) |
| 79 | { |
| 80 | int err; |
| 81 | |
Herbert Xu | 30e4c01 | 2015-05-22 16:30:48 +0800 | [diff] [blame] | 82 | if (authsize > crypto_aead_maxauthsize(tfm)) |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 83 | return -EINVAL; |
| 84 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 85 | if (tfm->setauthsize) { |
| 86 | err = tfm->setauthsize(tfm->child, authsize); |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 87 | if (err) |
| 88 | return err; |
| 89 | } |
| 90 | |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 91 | tfm->child->authsize = authsize; |
| 92 | tfm->authsize = authsize; |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | EXPORT_SYMBOL_GPL(crypto_aead_setauthsize); |
| 96 | |
Herbert Xu | 996d98d | 2015-05-21 15:11:01 +0800 | [diff] [blame] | 97 | struct aead_old_request { |
| 98 | struct scatterlist srcbuf[2]; |
| 99 | struct scatterlist dstbuf[2]; |
| 100 | struct aead_request subreq; |
| 101 | }; |
| 102 | |
| 103 | unsigned int crypto_aead_reqsize(struct crypto_aead *tfm) |
| 104 | { |
| 105 | return tfm->reqsize + sizeof(struct aead_old_request); |
| 106 | } |
| 107 | EXPORT_SYMBOL_GPL(crypto_aead_reqsize); |
| 108 | |
| 109 | static int old_crypt(struct aead_request *req, |
| 110 | int (*crypt)(struct aead_request *req)) |
| 111 | { |
| 112 | struct aead_old_request *nreq = aead_request_ctx(req); |
| 113 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 114 | struct scatterlist *src, *dst; |
| 115 | |
| 116 | if (req->old) |
| 117 | return crypt(req); |
| 118 | |
Herbert Xu | 374d4ad | 2015-05-23 15:41:57 +0800 | [diff] [blame] | 119 | src = scatterwalk_ffwd(nreq->srcbuf, req->src, req->assoclen); |
Herbert Xu | b455eb8 | 2015-05-27 14:37:29 +0800 | [diff] [blame] | 120 | dst = req->src == req->dst ? |
| 121 | src : scatterwalk_ffwd(nreq->dstbuf, req->dst, req->assoclen); |
Herbert Xu | 996d98d | 2015-05-21 15:11:01 +0800 | [diff] [blame] | 122 | |
| 123 | aead_request_set_tfm(&nreq->subreq, aead); |
| 124 | aead_request_set_callback(&nreq->subreq, aead_request_flags(req), |
| 125 | req->base.complete, req->base.data); |
| 126 | aead_request_set_crypt(&nreq->subreq, src, dst, req->cryptlen, |
| 127 | req->iv); |
| 128 | aead_request_set_assoc(&nreq->subreq, req->src, req->assoclen); |
| 129 | |
| 130 | return crypt(&nreq->subreq); |
| 131 | } |
| 132 | |
| 133 | static int old_encrypt(struct aead_request *req) |
| 134 | { |
| 135 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Herbert Xu | 2d0f230 | 2015-05-21 15:11:02 +0800 | [diff] [blame] | 136 | struct old_aead_alg *alg = crypto_old_aead_alg(aead); |
Herbert Xu | 996d98d | 2015-05-21 15:11:01 +0800 | [diff] [blame] | 137 | |
| 138 | return old_crypt(req, alg->encrypt); |
| 139 | } |
| 140 | |
| 141 | static int old_decrypt(struct aead_request *req) |
| 142 | { |
| 143 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Herbert Xu | 2d0f230 | 2015-05-21 15:11:02 +0800 | [diff] [blame] | 144 | struct old_aead_alg *alg = crypto_old_aead_alg(aead); |
Herbert Xu | 996d98d | 2015-05-21 15:11:01 +0800 | [diff] [blame] | 145 | |
| 146 | return old_crypt(req, alg->decrypt); |
| 147 | } |
| 148 | |
Herbert Xu | aedb30d | 2007-12-12 19:27:25 +0800 | [diff] [blame] | 149 | static int no_givcrypt(struct aead_givcrypt_request *req) |
Herbert Xu | 743edf5 | 2007-12-10 16:18:01 +0800 | [diff] [blame] | 150 | { |
| 151 | return -ENOSYS; |
| 152 | } |
| 153 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 154 | static int crypto_old_aead_init_tfm(struct crypto_tfm *tfm) |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 155 | { |
Herbert Xu | 2d0f230 | 2015-05-21 15:11:02 +0800 | [diff] [blame] | 156 | struct old_aead_alg *alg = &tfm->__crt_alg->cra_aead; |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 157 | struct crypto_aead *crt = __crypto_aead_cast(tfm); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 158 | |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 159 | if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8) |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 160 | return -EINVAL; |
| 161 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 162 | crt->setkey = alg->setkey; |
| 163 | crt->setauthsize = alg->setauthsize; |
Herbert Xu | 996d98d | 2015-05-21 15:11:01 +0800 | [diff] [blame] | 164 | crt->encrypt = old_encrypt; |
| 165 | crt->decrypt = old_decrypt; |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 166 | if (alg->ivsize) { |
| 167 | crt->givencrypt = alg->givencrypt ?: no_givcrypt; |
| 168 | crt->givdecrypt = alg->givdecrypt ?: no_givcrypt; |
| 169 | } else { |
| 170 | crt->givencrypt = aead_null_givencrypt; |
| 171 | crt->givdecrypt = aead_null_givdecrypt; |
| 172 | } |
| 173 | crt->child = __crypto_aead_cast(tfm); |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 174 | crt->authsize = alg->maxauthsize; |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Herbert Xu | 5eb8ec6 | 2015-05-28 22:07:53 +0800 | [diff] [blame] | 179 | static void crypto_aead_exit_tfm(struct crypto_tfm *tfm) |
| 180 | { |
| 181 | struct crypto_aead *aead = __crypto_aead_cast(tfm); |
| 182 | struct aead_alg *alg = crypto_aead_alg(aead); |
| 183 | |
| 184 | alg->exit(aead); |
| 185 | } |
| 186 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 187 | static int crypto_aead_init_tfm(struct crypto_tfm *tfm) |
| 188 | { |
| 189 | struct crypto_aead *aead = __crypto_aead_cast(tfm); |
| 190 | struct aead_alg *alg = crypto_aead_alg(aead); |
| 191 | |
| 192 | if (crypto_old_aead_alg(aead)->encrypt) |
| 193 | return crypto_old_aead_init_tfm(tfm); |
| 194 | |
| 195 | aead->setkey = alg->setkey; |
| 196 | aead->setauthsize = alg->setauthsize; |
| 197 | aead->encrypt = alg->encrypt; |
| 198 | aead->decrypt = alg->decrypt; |
| 199 | aead->child = __crypto_aead_cast(tfm); |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 200 | aead->authsize = alg->maxauthsize; |
| 201 | |
Herbert Xu | 5eb8ec6 | 2015-05-28 22:07:53 +0800 | [diff] [blame] | 202 | if (alg->exit) |
| 203 | aead->base.exit = crypto_aead_exit_tfm; |
| 204 | |
| 205 | if (alg->init) |
| 206 | return alg->init(aead); |
| 207 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
Herbert Xu | 3acc847 | 2011-11-03 23:46:07 +1100 | [diff] [blame] | 211 | #ifdef CONFIG_NET |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 212 | static int crypto_old_aead_report(struct sk_buff *skb, struct crypto_alg *alg) |
Steffen Klassert | 6ad414f | 2011-09-27 07:44:27 +0200 | [diff] [blame] | 213 | { |
| 214 | struct crypto_report_aead raead; |
Herbert Xu | 2d0f230 | 2015-05-21 15:11:02 +0800 | [diff] [blame] | 215 | struct old_aead_alg *aead = &alg->cra_aead; |
Steffen Klassert | 6ad414f | 2011-09-27 07:44:27 +0200 | [diff] [blame] | 216 | |
Mathias Krause | 9a5467bf | 2013-02-05 18:19:13 +0100 | [diff] [blame] | 217 | strncpy(raead.type, "aead", sizeof(raead.type)); |
| 218 | strncpy(raead.geniv, aead->geniv ?: "<built-in>", sizeof(raead.geniv)); |
Steffen Klassert | 6ad414f | 2011-09-27 07:44:27 +0200 | [diff] [blame] | 219 | |
| 220 | raead.blocksize = alg->cra_blocksize; |
| 221 | raead.maxauthsize = aead->maxauthsize; |
| 222 | raead.ivsize = aead->ivsize; |
| 223 | |
David S. Miller | 6662df3 | 2012-04-01 20:19:05 -0400 | [diff] [blame] | 224 | if (nla_put(skb, CRYPTOCFGA_REPORT_AEAD, |
| 225 | sizeof(struct crypto_report_aead), &raead)) |
| 226 | goto nla_put_failure; |
Steffen Klassert | 6ad414f | 2011-09-27 07:44:27 +0200 | [diff] [blame] | 227 | return 0; |
| 228 | |
| 229 | nla_put_failure: |
| 230 | return -EMSGSIZE; |
| 231 | } |
Herbert Xu | 3acc847 | 2011-11-03 23:46:07 +1100 | [diff] [blame] | 232 | #else |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 233 | static int crypto_old_aead_report(struct sk_buff *skb, struct crypto_alg *alg) |
| 234 | { |
| 235 | return -ENOSYS; |
| 236 | } |
| 237 | #endif |
| 238 | |
| 239 | static void crypto_old_aead_show(struct seq_file *m, struct crypto_alg *alg) |
| 240 | __attribute__ ((unused)); |
| 241 | static void crypto_old_aead_show(struct seq_file *m, struct crypto_alg *alg) |
| 242 | { |
| 243 | struct old_aead_alg *aead = &alg->cra_aead; |
| 244 | |
| 245 | seq_printf(m, "type : aead\n"); |
| 246 | seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ? |
| 247 | "yes" : "no"); |
| 248 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); |
| 249 | seq_printf(m, "ivsize : %u\n", aead->ivsize); |
| 250 | seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize); |
| 251 | seq_printf(m, "geniv : %s\n", aead->geniv ?: "<built-in>"); |
| 252 | } |
| 253 | |
| 254 | const struct crypto_type crypto_aead_type = { |
| 255 | .extsize = crypto_alg_extsize, |
| 256 | .init_tfm = crypto_aead_init_tfm, |
| 257 | #ifdef CONFIG_PROC_FS |
| 258 | .show = crypto_old_aead_show, |
| 259 | #endif |
| 260 | .report = crypto_old_aead_report, |
| 261 | .lookup = crypto_lookup_aead, |
| 262 | .maskclear = ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV), |
| 263 | .maskset = CRYPTO_ALG_TYPE_MASK, |
| 264 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 265 | .tfmsize = offsetof(struct crypto_aead, base), |
| 266 | }; |
| 267 | EXPORT_SYMBOL_GPL(crypto_aead_type); |
| 268 | |
| 269 | #ifdef CONFIG_NET |
| 270 | static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg) |
| 271 | { |
| 272 | struct crypto_report_aead raead; |
| 273 | struct aead_alg *aead = container_of(alg, struct aead_alg, base); |
| 274 | |
| 275 | strncpy(raead.type, "aead", sizeof(raead.type)); |
| 276 | strncpy(raead.geniv, "<none>", sizeof(raead.geniv)); |
| 277 | |
| 278 | raead.blocksize = alg->cra_blocksize; |
| 279 | raead.maxauthsize = aead->maxauthsize; |
| 280 | raead.ivsize = aead->ivsize; |
| 281 | |
| 282 | if (nla_put(skb, CRYPTOCFGA_REPORT_AEAD, |
| 283 | sizeof(struct crypto_report_aead), &raead)) |
| 284 | goto nla_put_failure; |
| 285 | return 0; |
| 286 | |
| 287 | nla_put_failure: |
| 288 | return -EMSGSIZE; |
| 289 | } |
| 290 | #else |
Herbert Xu | 3acc847 | 2011-11-03 23:46:07 +1100 | [diff] [blame] | 291 | static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg) |
| 292 | { |
| 293 | return -ENOSYS; |
| 294 | } |
| 295 | #endif |
Steffen Klassert | 6ad414f | 2011-09-27 07:44:27 +0200 | [diff] [blame] | 296 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 297 | static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg) |
| 298 | __attribute__ ((unused)); |
| 299 | static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg) |
| 300 | { |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 301 | struct aead_alg *aead = container_of(alg, struct aead_alg, base); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 302 | |
| 303 | seq_printf(m, "type : aead\n"); |
Herbert Xu | 189ed66 | 2007-12-14 22:29:37 +0800 | [diff] [blame] | 304 | seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ? |
| 305 | "yes" : "no"); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 306 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); |
| 307 | seq_printf(m, "ivsize : %u\n", aead->ivsize); |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 308 | seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize); |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 309 | seq_printf(m, "geniv : <none>\n"); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 310 | } |
| 311 | |
Herbert Xu | ba75e15 | 2015-07-09 07:17:17 +0800 | [diff] [blame] | 312 | static void crypto_aead_free_instance(struct crypto_instance *inst) |
| 313 | { |
| 314 | struct aead_instance *aead = aead_instance(inst); |
| 315 | |
| 316 | if (!aead->free) { |
| 317 | inst->tmpl->free(inst); |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | aead->free(aead); |
| 322 | } |
| 323 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 324 | static const struct crypto_type crypto_new_aead_type = { |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 325 | .extsize = crypto_alg_extsize, |
| 326 | .init_tfm = crypto_aead_init_tfm, |
Herbert Xu | ba75e15 | 2015-07-09 07:17:17 +0800 | [diff] [blame] | 327 | .free = crypto_aead_free_instance, |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 328 | #ifdef CONFIG_PROC_FS |
| 329 | .show = crypto_aead_show, |
| 330 | #endif |
Steffen Klassert | 6ad414f | 2011-09-27 07:44:27 +0200 | [diff] [blame] | 331 | .report = crypto_aead_report, |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 332 | .maskclear = ~CRYPTO_ALG_TYPE_MASK, |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 333 | .maskset = CRYPTO_ALG_TYPE_MASK, |
| 334 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 335 | .tfmsize = offsetof(struct crypto_aead, base), |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 336 | }; |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 337 | |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 338 | static int aead_null_givencrypt(struct aead_givcrypt_request *req) |
| 339 | { |
| 340 | return crypto_aead_encrypt(&req->areq); |
| 341 | } |
| 342 | |
| 343 | static int aead_null_givdecrypt(struct aead_givcrypt_request *req) |
| 344 | { |
| 345 | return crypto_aead_decrypt(&req->areq); |
| 346 | } |
| 347 | |
Herbert Xu | 3acc847 | 2011-11-03 23:46:07 +1100 | [diff] [blame] | 348 | #ifdef CONFIG_NET |
Steffen Klassert | b735d0a | 2011-09-27 07:45:44 +0200 | [diff] [blame] | 349 | static int crypto_nivaead_report(struct sk_buff *skb, struct crypto_alg *alg) |
| 350 | { |
| 351 | struct crypto_report_aead raead; |
Herbert Xu | 2d0f230 | 2015-05-21 15:11:02 +0800 | [diff] [blame] | 352 | struct old_aead_alg *aead = &alg->cra_aead; |
Steffen Klassert | b735d0a | 2011-09-27 07:45:44 +0200 | [diff] [blame] | 353 | |
Mathias Krause | 9a5467bf | 2013-02-05 18:19:13 +0100 | [diff] [blame] | 354 | strncpy(raead.type, "nivaead", sizeof(raead.type)); |
| 355 | strncpy(raead.geniv, aead->geniv, sizeof(raead.geniv)); |
Steffen Klassert | b735d0a | 2011-09-27 07:45:44 +0200 | [diff] [blame] | 356 | |
| 357 | raead.blocksize = alg->cra_blocksize; |
| 358 | raead.maxauthsize = aead->maxauthsize; |
| 359 | raead.ivsize = aead->ivsize; |
| 360 | |
David S. Miller | 6662df3 | 2012-04-01 20:19:05 -0400 | [diff] [blame] | 361 | if (nla_put(skb, CRYPTOCFGA_REPORT_AEAD, |
| 362 | sizeof(struct crypto_report_aead), &raead)) |
| 363 | goto nla_put_failure; |
Steffen Klassert | b735d0a | 2011-09-27 07:45:44 +0200 | [diff] [blame] | 364 | return 0; |
| 365 | |
| 366 | nla_put_failure: |
| 367 | return -EMSGSIZE; |
| 368 | } |
Herbert Xu | 3acc847 | 2011-11-03 23:46:07 +1100 | [diff] [blame] | 369 | #else |
| 370 | static int crypto_nivaead_report(struct sk_buff *skb, struct crypto_alg *alg) |
| 371 | { |
| 372 | return -ENOSYS; |
| 373 | } |
| 374 | #endif |
Steffen Klassert | b735d0a | 2011-09-27 07:45:44 +0200 | [diff] [blame] | 375 | |
| 376 | |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 377 | static void crypto_nivaead_show(struct seq_file *m, struct crypto_alg *alg) |
| 378 | __attribute__ ((unused)); |
| 379 | static void crypto_nivaead_show(struct seq_file *m, struct crypto_alg *alg) |
| 380 | { |
Herbert Xu | 2d0f230 | 2015-05-21 15:11:02 +0800 | [diff] [blame] | 381 | struct old_aead_alg *aead = &alg->cra_aead; |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 382 | |
| 383 | seq_printf(m, "type : nivaead\n"); |
Herbert Xu | 189ed66 | 2007-12-14 22:29:37 +0800 | [diff] [blame] | 384 | seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ? |
| 385 | "yes" : "no"); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 386 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); |
| 387 | seq_printf(m, "ivsize : %u\n", aead->ivsize); |
| 388 | seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize); |
| 389 | seq_printf(m, "geniv : %s\n", aead->geniv); |
| 390 | } |
| 391 | |
| 392 | const struct crypto_type crypto_nivaead_type = { |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 393 | .extsize = crypto_alg_extsize, |
| 394 | .init_tfm = crypto_aead_init_tfm, |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 395 | #ifdef CONFIG_PROC_FS |
| 396 | .show = crypto_nivaead_show, |
| 397 | #endif |
Steffen Klassert | b735d0a | 2011-09-27 07:45:44 +0200 | [diff] [blame] | 398 | .report = crypto_nivaead_report, |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 399 | .maskclear = ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV), |
| 400 | .maskset = CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV, |
| 401 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 402 | .tfmsize = offsetof(struct crypto_aead, base), |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 403 | }; |
| 404 | EXPORT_SYMBOL_GPL(crypto_nivaead_type); |
| 405 | |
| 406 | static int crypto_grab_nivaead(struct crypto_aead_spawn *spawn, |
| 407 | const char *name, u32 type, u32 mask) |
| 408 | { |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 409 | spawn->base.frontend = &crypto_nivaead_type; |
| 410 | return crypto_grab_spawn(&spawn->base, name, type, mask); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 411 | } |
| 412 | |
Herbert Xu | 6350449 | 2015-05-27 14:37:30 +0800 | [diff] [blame] | 413 | static int aead_geniv_setkey(struct crypto_aead *tfm, |
| 414 | const u8 *key, unsigned int keylen) |
| 415 | { |
| 416 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm); |
| 417 | |
| 418 | return crypto_aead_setkey(ctx->child, key, keylen); |
| 419 | } |
| 420 | |
| 421 | static int aead_geniv_setauthsize(struct crypto_aead *tfm, |
| 422 | unsigned int authsize) |
| 423 | { |
| 424 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm); |
| 425 | |
| 426 | return crypto_aead_setauthsize(ctx->child, authsize); |
| 427 | } |
| 428 | |
| 429 | static void compat_encrypt_complete2(struct aead_request *req, int err) |
| 430 | { |
| 431 | struct compat_request_ctx *rctx = aead_request_ctx(req); |
| 432 | struct aead_givcrypt_request *subreq = &rctx->subreq; |
| 433 | struct crypto_aead *geniv; |
| 434 | |
| 435 | if (err == -EINPROGRESS) |
| 436 | return; |
| 437 | |
| 438 | if (err) |
| 439 | goto out; |
| 440 | |
| 441 | geniv = crypto_aead_reqtfm(req); |
| 442 | scatterwalk_map_and_copy(subreq->giv, rctx->ivsg, 0, |
| 443 | crypto_aead_ivsize(geniv), 1); |
| 444 | |
| 445 | out: |
| 446 | kzfree(subreq->giv); |
| 447 | } |
| 448 | |
| 449 | static void compat_encrypt_complete(struct crypto_async_request *base, int err) |
| 450 | { |
| 451 | struct aead_request *req = base->data; |
| 452 | |
| 453 | compat_encrypt_complete2(req, err); |
| 454 | aead_request_complete(req, err); |
| 455 | } |
| 456 | |
| 457 | static int compat_encrypt(struct aead_request *req) |
| 458 | { |
| 459 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
| 460 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
| 461 | struct compat_request_ctx *rctx = aead_request_ctx(req); |
| 462 | struct aead_givcrypt_request *subreq = &rctx->subreq; |
| 463 | unsigned int ivsize = crypto_aead_ivsize(geniv); |
| 464 | struct scatterlist *src, *dst; |
| 465 | crypto_completion_t compl; |
| 466 | void *data; |
| 467 | u8 *info; |
| 468 | __be64 seq; |
| 469 | int err; |
| 470 | |
| 471 | if (req->cryptlen < ivsize) |
| 472 | return -EINVAL; |
| 473 | |
| 474 | compl = req->base.complete; |
| 475 | data = req->base.data; |
| 476 | |
| 477 | rctx->ivsg = scatterwalk_ffwd(rctx->ivbuf, req->dst, req->assoclen); |
| 478 | info = PageHighMem(sg_page(rctx->ivsg)) ? NULL : sg_virt(rctx->ivsg); |
| 479 | |
| 480 | if (!info) { |
| 481 | info = kmalloc(ivsize, req->base.flags & |
| 482 | CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL: |
| 483 | GFP_ATOMIC); |
| 484 | if (!info) |
| 485 | return -ENOMEM; |
| 486 | |
| 487 | compl = compat_encrypt_complete; |
| 488 | data = req; |
| 489 | } |
| 490 | |
| 491 | memcpy(&seq, req->iv + ivsize - sizeof(seq), sizeof(seq)); |
| 492 | |
| 493 | src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen + ivsize); |
| 494 | dst = req->src == req->dst ? |
| 495 | src : scatterwalk_ffwd(rctx->dst, rctx->ivsg, ivsize); |
| 496 | |
| 497 | aead_givcrypt_set_tfm(subreq, ctx->child); |
| 498 | aead_givcrypt_set_callback(subreq, req->base.flags, |
| 499 | req->base.complete, req->base.data); |
| 500 | aead_givcrypt_set_crypt(subreq, src, dst, |
| 501 | req->cryptlen - ivsize, req->iv); |
| 502 | aead_givcrypt_set_assoc(subreq, req->src, req->assoclen); |
| 503 | aead_givcrypt_set_giv(subreq, info, be64_to_cpu(seq)); |
| 504 | |
| 505 | err = crypto_aead_givencrypt(subreq); |
| 506 | if (unlikely(PageHighMem(sg_page(rctx->ivsg)))) |
| 507 | compat_encrypt_complete2(req, err); |
| 508 | return err; |
| 509 | } |
| 510 | |
| 511 | static int compat_decrypt(struct aead_request *req) |
| 512 | { |
| 513 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
| 514 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
| 515 | struct compat_request_ctx *rctx = aead_request_ctx(req); |
| 516 | struct aead_request *subreq = &rctx->subreq.areq; |
| 517 | unsigned int ivsize = crypto_aead_ivsize(geniv); |
| 518 | struct scatterlist *src, *dst; |
| 519 | crypto_completion_t compl; |
| 520 | void *data; |
| 521 | |
| 522 | if (req->cryptlen < ivsize) |
| 523 | return -EINVAL; |
| 524 | |
| 525 | aead_request_set_tfm(subreq, ctx->child); |
| 526 | |
| 527 | compl = req->base.complete; |
| 528 | data = req->base.data; |
| 529 | |
| 530 | src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen + ivsize); |
| 531 | dst = req->src == req->dst ? |
| 532 | src : scatterwalk_ffwd(rctx->dst, req->dst, |
| 533 | req->assoclen + ivsize); |
| 534 | |
| 535 | aead_request_set_callback(subreq, req->base.flags, compl, data); |
| 536 | aead_request_set_crypt(subreq, src, dst, |
| 537 | req->cryptlen - ivsize, req->iv); |
| 538 | aead_request_set_assoc(subreq, req->src, req->assoclen); |
| 539 | |
| 540 | scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0); |
| 541 | |
| 542 | return crypto_aead_decrypt(subreq); |
| 543 | } |
| 544 | |
| 545 | static int compat_encrypt_first(struct aead_request *req) |
| 546 | { |
| 547 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
| 548 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
| 549 | int err = 0; |
| 550 | |
| 551 | spin_lock_bh(&ctx->lock); |
| 552 | if (geniv->encrypt != compat_encrypt_first) |
| 553 | goto unlock; |
| 554 | |
| 555 | geniv->encrypt = compat_encrypt; |
| 556 | |
| 557 | unlock: |
| 558 | spin_unlock_bh(&ctx->lock); |
| 559 | |
| 560 | if (err) |
| 561 | return err; |
| 562 | |
| 563 | return compat_encrypt(req); |
| 564 | } |
| 565 | |
| 566 | static int aead_geniv_init_compat(struct crypto_tfm *tfm) |
| 567 | { |
| 568 | struct crypto_aead *geniv = __crypto_aead_cast(tfm); |
| 569 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
| 570 | int err; |
| 571 | |
| 572 | spin_lock_init(&ctx->lock); |
| 573 | |
| 574 | crypto_aead_set_reqsize(geniv, sizeof(struct compat_request_ctx)); |
| 575 | |
| 576 | err = aead_geniv_init(tfm); |
| 577 | |
| 578 | ctx->child = geniv->child; |
| 579 | geniv->child = geniv; |
| 580 | |
| 581 | return err; |
| 582 | } |
| 583 | |
| 584 | static void aead_geniv_exit_compat(struct crypto_tfm *tfm) |
| 585 | { |
| 586 | struct crypto_aead *geniv = __crypto_aead_cast(tfm); |
| 587 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
| 588 | |
| 589 | crypto_free_aead(ctx->child); |
| 590 | } |
| 591 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 592 | struct aead_instance *aead_geniv_alloc(struct crypto_template *tmpl, |
| 593 | struct rtattr **tb, u32 type, u32 mask) |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 594 | { |
| 595 | const char *name; |
| 596 | struct crypto_aead_spawn *spawn; |
| 597 | struct crypto_attr_type *algt; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 598 | struct aead_instance *inst; |
| 599 | struct aead_alg *alg; |
| 600 | unsigned int ivsize; |
| 601 | unsigned int maxauthsize; |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 602 | int err; |
| 603 | |
| 604 | algt = crypto_get_attr_type(tb); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 605 | if (IS_ERR(algt)) |
Julia Lawall | 3e8afe3 | 2013-01-22 12:29:26 +0100 | [diff] [blame] | 606 | return ERR_CAST(algt); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 607 | |
| 608 | if ((algt->type ^ (CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV)) & |
Herbert Xu | e13f0f6 | 2015-07-09 07:17:23 +0800 | [diff] [blame] | 609 | algt->mask & ~CRYPTO_ALG_AEAD_NEW) |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 610 | return ERR_PTR(-EINVAL); |
| 611 | |
| 612 | name = crypto_attr_alg_name(tb[1]); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 613 | if (IS_ERR(name)) |
Julia Lawall | 3e8afe3 | 2013-01-22 12:29:26 +0100 | [diff] [blame] | 614 | return ERR_CAST(name); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 615 | |
| 616 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
| 617 | if (!inst) |
| 618 | return ERR_PTR(-ENOMEM); |
| 619 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 620 | spawn = aead_instance_ctx(inst); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 621 | |
| 622 | /* Ignore async algorithms if necessary. */ |
| 623 | mask |= crypto_requires_sync(algt->type, algt->mask); |
| 624 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 625 | crypto_set_aead_spawn(spawn, aead_crypto_instance(inst)); |
Herbert Xu | 6350449 | 2015-05-27 14:37:30 +0800 | [diff] [blame] | 626 | err = (algt->mask & CRYPTO_ALG_GENIV) ? |
| 627 | crypto_grab_nivaead(spawn, name, type, mask) : |
| 628 | crypto_grab_aead(spawn, name, type, mask); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 629 | if (err) |
| 630 | goto err_free_inst; |
| 631 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 632 | alg = crypto_spawn_aead_alg(spawn); |
| 633 | |
Herbert Xu | 30e4c01 | 2015-05-22 16:30:48 +0800 | [diff] [blame] | 634 | ivsize = crypto_aead_alg_ivsize(alg); |
| 635 | maxauthsize = crypto_aead_alg_maxauthsize(alg); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 636 | |
| 637 | err = -EINVAL; |
Herbert Xu | 6350449 | 2015-05-27 14:37:30 +0800 | [diff] [blame] | 638 | if (ivsize < sizeof(u64)) |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 639 | goto err_drop_alg; |
| 640 | |
| 641 | /* |
| 642 | * This is only true if we're constructing an algorithm with its |
| 643 | * default IV generator. For the default generator we elide the |
| 644 | * template name and double-check the IV generator. |
| 645 | */ |
| 646 | if (algt->mask & CRYPTO_ALG_GENIV) { |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 647 | if (!alg->base.cra_aead.encrypt) |
| 648 | goto err_drop_alg; |
| 649 | if (strcmp(tmpl->name, alg->base.cra_aead.geniv)) |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 650 | goto err_drop_alg; |
| 651 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 652 | memcpy(inst->alg.base.cra_name, alg->base.cra_name, |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 653 | CRYPTO_MAX_ALG_NAME); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 654 | memcpy(inst->alg.base.cra_driver_name, |
| 655 | alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME); |
| 656 | |
| 657 | inst->alg.base.cra_flags = CRYPTO_ALG_TYPE_AEAD | |
| 658 | CRYPTO_ALG_GENIV; |
| 659 | inst->alg.base.cra_flags |= alg->base.cra_flags & |
| 660 | CRYPTO_ALG_ASYNC; |
| 661 | inst->alg.base.cra_priority = alg->base.cra_priority; |
| 662 | inst->alg.base.cra_blocksize = alg->base.cra_blocksize; |
| 663 | inst->alg.base.cra_alignmask = alg->base.cra_alignmask; |
| 664 | inst->alg.base.cra_type = &crypto_aead_type; |
| 665 | |
| 666 | inst->alg.base.cra_aead.ivsize = ivsize; |
| 667 | inst->alg.base.cra_aead.maxauthsize = maxauthsize; |
| 668 | |
| 669 | inst->alg.base.cra_aead.setkey = alg->base.cra_aead.setkey; |
| 670 | inst->alg.base.cra_aead.setauthsize = |
| 671 | alg->base.cra_aead.setauthsize; |
| 672 | inst->alg.base.cra_aead.encrypt = alg->base.cra_aead.encrypt; |
| 673 | inst->alg.base.cra_aead.decrypt = alg->base.cra_aead.decrypt; |
| 674 | |
| 675 | goto out; |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 676 | } |
| 677 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 678 | err = -ENAMETOOLONG; |
| 679 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
| 680 | "%s(%s)", tmpl->name, alg->base.cra_name) >= |
| 681 | CRYPTO_MAX_ALG_NAME) |
| 682 | goto err_drop_alg; |
| 683 | if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
| 684 | "%s(%s)", tmpl->name, alg->base.cra_driver_name) >= |
| 685 | CRYPTO_MAX_ALG_NAME) |
| 686 | goto err_drop_alg; |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 687 | |
Herbert Xu | e13f0f6 | 2015-07-09 07:17:23 +0800 | [diff] [blame] | 688 | inst->alg.base.cra_flags = alg->base.cra_flags & |
| 689 | (CRYPTO_ALG_ASYNC | CRYPTO_ALG_AEAD_NEW); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 690 | inst->alg.base.cra_priority = alg->base.cra_priority; |
| 691 | inst->alg.base.cra_blocksize = alg->base.cra_blocksize; |
| 692 | inst->alg.base.cra_alignmask = alg->base.cra_alignmask; |
Herbert Xu | 6350449 | 2015-05-27 14:37:30 +0800 | [diff] [blame] | 693 | inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx); |
| 694 | |
| 695 | inst->alg.setkey = aead_geniv_setkey; |
| 696 | inst->alg.setauthsize = aead_geniv_setauthsize; |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 697 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 698 | inst->alg.ivsize = ivsize; |
| 699 | inst->alg.maxauthsize = maxauthsize; |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 700 | |
Herbert Xu | 6350449 | 2015-05-27 14:37:30 +0800 | [diff] [blame] | 701 | inst->alg.encrypt = compat_encrypt_first; |
| 702 | inst->alg.decrypt = compat_decrypt; |
| 703 | |
| 704 | inst->alg.base.cra_init = aead_geniv_init_compat; |
| 705 | inst->alg.base.cra_exit = aead_geniv_exit_compat; |
| 706 | |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 707 | out: |
| 708 | return inst; |
| 709 | |
| 710 | err_drop_alg: |
| 711 | crypto_drop_aead(spawn); |
| 712 | err_free_inst: |
| 713 | kfree(inst); |
| 714 | inst = ERR_PTR(err); |
| 715 | goto out; |
| 716 | } |
| 717 | EXPORT_SYMBOL_GPL(aead_geniv_alloc); |
| 718 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 719 | void aead_geniv_free(struct aead_instance *inst) |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 720 | { |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 721 | crypto_drop_aead(aead_instance_ctx(inst)); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 722 | kfree(inst); |
| 723 | } |
| 724 | EXPORT_SYMBOL_GPL(aead_geniv_free); |
| 725 | |
| 726 | int aead_geniv_init(struct crypto_tfm *tfm) |
| 727 | { |
| 728 | struct crypto_instance *inst = (void *)tfm->__crt_alg; |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 729 | struct crypto_aead *child; |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 730 | struct crypto_aead *aead; |
| 731 | |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 732 | aead = __crypto_aead_cast(tfm); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 733 | |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 734 | child = crypto_spawn_aead(crypto_instance_ctx(inst)); |
| 735 | if (IS_ERR(child)) |
| 736 | return PTR_ERR(child); |
| 737 | |
| 738 | aead->child = child; |
| 739 | aead->reqsize += crypto_aead_reqsize(child); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | EXPORT_SYMBOL_GPL(aead_geniv_init); |
| 744 | |
| 745 | void aead_geniv_exit(struct crypto_tfm *tfm) |
| 746 | { |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 747 | crypto_free_aead(__crypto_aead_cast(tfm)->child); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 748 | } |
| 749 | EXPORT_SYMBOL_GPL(aead_geniv_exit); |
| 750 | |
Herbert Xu | 149a397 | 2015-08-13 17:28:58 +0800 | [diff] [blame^] | 751 | int aead_init_geniv(struct crypto_aead *aead) |
| 752 | { |
| 753 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(aead); |
| 754 | struct aead_instance *inst = aead_alg_instance(aead); |
| 755 | struct crypto_aead *child; |
| 756 | int err; |
| 757 | |
| 758 | spin_lock_init(&ctx->lock); |
| 759 | |
| 760 | err = crypto_get_default_rng(); |
| 761 | if (err) |
| 762 | goto out; |
| 763 | |
| 764 | err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt, |
| 765 | crypto_aead_ivsize(aead)); |
| 766 | crypto_put_default_rng(); |
| 767 | if (err) |
| 768 | goto out; |
| 769 | |
| 770 | ctx->null = crypto_get_default_null_skcipher(); |
| 771 | err = PTR_ERR(ctx->null); |
| 772 | if (IS_ERR(ctx->null)) |
| 773 | goto out; |
| 774 | |
| 775 | child = crypto_spawn_aead(aead_instance_ctx(inst)); |
| 776 | err = PTR_ERR(child); |
| 777 | if (IS_ERR(child)) |
| 778 | goto drop_null; |
| 779 | |
| 780 | ctx->child = child; |
| 781 | crypto_aead_set_reqsize(aead, crypto_aead_reqsize(child) + |
| 782 | sizeof(struct aead_request)); |
| 783 | |
| 784 | err = 0; |
| 785 | |
| 786 | out: |
| 787 | return err; |
| 788 | |
| 789 | drop_null: |
| 790 | crypto_put_default_null_skcipher(); |
| 791 | goto out; |
| 792 | } |
| 793 | EXPORT_SYMBOL_GPL(aead_init_geniv); |
| 794 | |
| 795 | void aead_exit_geniv(struct crypto_aead *tfm) |
| 796 | { |
| 797 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm); |
| 798 | |
| 799 | crypto_free_aead(ctx->child); |
| 800 | crypto_put_default_null_skcipher(); |
| 801 | } |
| 802 | EXPORT_SYMBOL_GPL(aead_exit_geniv); |
| 803 | |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 804 | static int crypto_nivaead_default(struct crypto_alg *alg, u32 type, u32 mask) |
| 805 | { |
| 806 | struct rtattr *tb[3]; |
| 807 | struct { |
| 808 | struct rtattr attr; |
| 809 | struct crypto_attr_type data; |
| 810 | } ptype; |
| 811 | struct { |
| 812 | struct rtattr attr; |
| 813 | struct crypto_attr_alg data; |
| 814 | } palg; |
| 815 | struct crypto_template *tmpl; |
| 816 | struct crypto_instance *inst; |
| 817 | struct crypto_alg *larval; |
| 818 | const char *geniv; |
| 819 | int err; |
| 820 | |
| 821 | larval = crypto_larval_lookup(alg->cra_driver_name, |
| 822 | CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV, |
| 823 | CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV); |
| 824 | err = PTR_ERR(larval); |
| 825 | if (IS_ERR(larval)) |
| 826 | goto out; |
| 827 | |
| 828 | err = -EAGAIN; |
| 829 | if (!crypto_is_larval(larval)) |
| 830 | goto drop_larval; |
| 831 | |
| 832 | ptype.attr.rta_len = sizeof(ptype); |
| 833 | ptype.attr.rta_type = CRYPTOA_TYPE; |
| 834 | ptype.data.type = type | CRYPTO_ALG_GENIV; |
| 835 | /* GENIV tells the template that we're making a default geniv. */ |
| 836 | ptype.data.mask = mask | CRYPTO_ALG_GENIV; |
| 837 | tb[0] = &ptype.attr; |
| 838 | |
| 839 | palg.attr.rta_len = sizeof(palg); |
| 840 | palg.attr.rta_type = CRYPTOA_ALG; |
| 841 | /* Must use the exact name to locate ourselves. */ |
| 842 | memcpy(palg.data.name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME); |
| 843 | tb[1] = &palg.attr; |
| 844 | |
| 845 | tb[2] = NULL; |
| 846 | |
| 847 | geniv = alg->cra_aead.geniv; |
| 848 | |
| 849 | tmpl = crypto_lookup_template(geniv); |
| 850 | err = -ENOENT; |
| 851 | if (!tmpl) |
| 852 | goto kill_larval; |
| 853 | |
Herbert Xu | 72af865 | 2015-05-23 15:41:49 +0800 | [diff] [blame] | 854 | if (tmpl->create) { |
| 855 | err = tmpl->create(tmpl, tb); |
| 856 | if (err) |
| 857 | goto put_tmpl; |
| 858 | goto ok; |
| 859 | } |
| 860 | |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 861 | inst = tmpl->alloc(tb); |
| 862 | err = PTR_ERR(inst); |
| 863 | if (IS_ERR(inst)) |
| 864 | goto put_tmpl; |
| 865 | |
Joshua I. James | 4fad478 | 2014-12-05 14:24:44 +0900 | [diff] [blame] | 866 | err = crypto_register_instance(tmpl, inst); |
| 867 | if (err) { |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 868 | tmpl->free(inst); |
| 869 | goto put_tmpl; |
| 870 | } |
| 871 | |
Herbert Xu | 72af865 | 2015-05-23 15:41:49 +0800 | [diff] [blame] | 872 | ok: |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 873 | /* Redo the lookup to use the instance we just registered. */ |
| 874 | err = -EAGAIN; |
| 875 | |
| 876 | put_tmpl: |
| 877 | crypto_tmpl_put(tmpl); |
| 878 | kill_larval: |
| 879 | crypto_larval_kill(larval); |
| 880 | drop_larval: |
| 881 | crypto_mod_put(larval); |
| 882 | out: |
| 883 | crypto_mod_put(alg); |
| 884 | return err; |
| 885 | } |
| 886 | |
Steffen Klassert | 1e12299 | 2012-03-29 09:03:47 +0200 | [diff] [blame] | 887 | struct crypto_alg *crypto_lookup_aead(const char *name, u32 type, u32 mask) |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 888 | { |
| 889 | struct crypto_alg *alg; |
| 890 | |
| 891 | alg = crypto_alg_mod_lookup(name, type, mask); |
| 892 | if (IS_ERR(alg)) |
| 893 | return alg; |
| 894 | |
| 895 | if (alg->cra_type == &crypto_aead_type) |
| 896 | return alg; |
| 897 | |
| 898 | if (!alg->cra_aead.ivsize) |
| 899 | return alg; |
| 900 | |
Herbert Xu | 5852ae4 | 2009-02-18 20:41:47 +0800 | [diff] [blame] | 901 | crypto_mod_put(alg); |
| 902 | alg = crypto_alg_mod_lookup(name, type | CRYPTO_ALG_TESTED, |
| 903 | mask & ~CRYPTO_ALG_TESTED); |
| 904 | if (IS_ERR(alg)) |
| 905 | return alg; |
| 906 | |
| 907 | if (alg->cra_type == &crypto_aead_type) { |
Herbert Xu | 80f7b35 | 2015-04-23 16:37:46 +0800 | [diff] [blame] | 908 | if (~alg->cra_flags & (type ^ ~mask) & CRYPTO_ALG_TESTED) { |
Herbert Xu | 5852ae4 | 2009-02-18 20:41:47 +0800 | [diff] [blame] | 909 | crypto_mod_put(alg); |
| 910 | alg = ERR_PTR(-ENOENT); |
| 911 | } |
| 912 | return alg; |
| 913 | } |
| 914 | |
| 915 | BUG_ON(!alg->cra_aead.ivsize); |
| 916 | |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 917 | return ERR_PTR(crypto_nivaead_default(alg, type, mask)); |
| 918 | } |
Steffen Klassert | 1e12299 | 2012-03-29 09:03:47 +0200 | [diff] [blame] | 919 | EXPORT_SYMBOL_GPL(crypto_lookup_aead); |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 920 | |
| 921 | int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name, |
| 922 | u32 type, u32 mask) |
| 923 | { |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 924 | spawn->base.frontend = &crypto_aead_type; |
| 925 | return crypto_grab_spawn(&spawn->base, name, type, mask); |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 926 | } |
| 927 | EXPORT_SYMBOL_GPL(crypto_grab_aead); |
| 928 | |
| 929 | struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask) |
| 930 | { |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 931 | return crypto_alloc_tfm(alg_name, &crypto_aead_type, type, mask); |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 932 | } |
| 933 | EXPORT_SYMBOL_GPL(crypto_alloc_aead); |
| 934 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 935 | static int aead_prepare_alg(struct aead_alg *alg) |
| 936 | { |
| 937 | struct crypto_alg *base = &alg->base; |
| 938 | |
| 939 | if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8) |
| 940 | return -EINVAL; |
| 941 | |
| 942 | base->cra_type = &crypto_new_aead_type; |
| 943 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; |
| 944 | base->cra_flags |= CRYPTO_ALG_TYPE_AEAD; |
| 945 | |
| 946 | return 0; |
| 947 | } |
| 948 | |
| 949 | int crypto_register_aead(struct aead_alg *alg) |
| 950 | { |
| 951 | struct crypto_alg *base = &alg->base; |
| 952 | int err; |
| 953 | |
| 954 | err = aead_prepare_alg(alg); |
| 955 | if (err) |
| 956 | return err; |
| 957 | |
| 958 | return crypto_register_alg(base); |
| 959 | } |
| 960 | EXPORT_SYMBOL_GPL(crypto_register_aead); |
| 961 | |
Herbert Xu | 4361536 | 2015-05-28 22:07:57 +0800 | [diff] [blame] | 962 | void crypto_unregister_aead(struct aead_alg *alg) |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 963 | { |
Herbert Xu | 4361536 | 2015-05-28 22:07:57 +0800 | [diff] [blame] | 964 | crypto_unregister_alg(&alg->base); |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 965 | } |
| 966 | EXPORT_SYMBOL_GPL(crypto_unregister_aead); |
| 967 | |
Herbert Xu | caab946 | 2015-05-28 22:07:59 +0800 | [diff] [blame] | 968 | int crypto_register_aeads(struct aead_alg *algs, int count) |
| 969 | { |
| 970 | int i, ret; |
| 971 | |
| 972 | for (i = 0; i < count; i++) { |
| 973 | ret = crypto_register_aead(&algs[i]); |
| 974 | if (ret) |
| 975 | goto err; |
| 976 | } |
| 977 | |
| 978 | return 0; |
| 979 | |
| 980 | err: |
| 981 | for (--i; i >= 0; --i) |
| 982 | crypto_unregister_aead(&algs[i]); |
| 983 | |
| 984 | return ret; |
| 985 | } |
| 986 | EXPORT_SYMBOL_GPL(crypto_register_aeads); |
| 987 | |
| 988 | void crypto_unregister_aeads(struct aead_alg *algs, int count) |
| 989 | { |
| 990 | int i; |
| 991 | |
| 992 | for (i = count - 1; i >= 0; --i) |
| 993 | crypto_unregister_aead(&algs[i]); |
| 994 | } |
| 995 | EXPORT_SYMBOL_GPL(crypto_unregister_aeads); |
| 996 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 997 | int aead_register_instance(struct crypto_template *tmpl, |
| 998 | struct aead_instance *inst) |
| 999 | { |
| 1000 | int err; |
| 1001 | |
| 1002 | err = aead_prepare_alg(&inst->alg); |
| 1003 | if (err) |
| 1004 | return err; |
| 1005 | |
| 1006 | return crypto_register_instance(tmpl, aead_crypto_instance(inst)); |
| 1007 | } |
| 1008 | EXPORT_SYMBOL_GPL(aead_register_instance); |
| 1009 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 1010 | MODULE_LICENSE("GPL"); |
| 1011 | MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)"); |