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