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