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 | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 15 | #include <crypto/internal/aead.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 | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 30 | static int aead_null_givencrypt(struct aead_givcrypt_request *req); |
| 31 | static int aead_null_givdecrypt(struct aead_givcrypt_request *req); |
| 32 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 33 | static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key, |
| 34 | unsigned int keylen) |
| 35 | { |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 36 | unsigned long alignmask = crypto_aead_alignmask(tfm); |
| 37 | int ret; |
| 38 | u8 *buffer, *alignbuffer; |
| 39 | unsigned long absize; |
| 40 | |
| 41 | absize = keylen + alignmask; |
| 42 | buffer = kmalloc(absize, GFP_ATOMIC); |
| 43 | if (!buffer) |
| 44 | return -ENOMEM; |
| 45 | |
| 46 | alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); |
| 47 | memcpy(alignbuffer, key, keylen); |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 48 | ret = tfm->setkey(tfm, alignbuffer, keylen); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 49 | memset(alignbuffer, 0, keylen); |
| 50 | kfree(buffer); |
| 51 | return ret; |
| 52 | } |
| 53 | |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 54 | int crypto_aead_setkey(struct crypto_aead *tfm, |
| 55 | const u8 *key, unsigned int keylen) |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 56 | { |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 57 | unsigned long alignmask = crypto_aead_alignmask(tfm); |
| 58 | |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 59 | tfm = tfm->child; |
| 60 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 61 | if ((unsigned long)key & alignmask) |
| 62 | return setkey_unaligned(tfm, key, keylen); |
| 63 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 64 | return tfm->setkey(tfm, key, keylen); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 65 | } |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 66 | EXPORT_SYMBOL_GPL(crypto_aead_setkey); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 67 | |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 68 | int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize) |
| 69 | { |
| 70 | int err; |
| 71 | |
Herbert Xu | 30e4c01 | 2015-05-22 16:30:48 +0800 | [diff] [blame] | 72 | if (authsize > crypto_aead_maxauthsize(tfm)) |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 73 | return -EINVAL; |
| 74 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 75 | if (tfm->setauthsize) { |
| 76 | err = tfm->setauthsize(tfm->child, authsize); |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 77 | if (err) |
| 78 | return err; |
| 79 | } |
| 80 | |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 81 | tfm->child->authsize = authsize; |
| 82 | tfm->authsize = authsize; |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | EXPORT_SYMBOL_GPL(crypto_aead_setauthsize); |
| 86 | |
Herbert Xu | 996d98d | 2015-05-21 15:11:01 +0800 | [diff] [blame] | 87 | struct aead_old_request { |
| 88 | struct scatterlist srcbuf[2]; |
| 89 | struct scatterlist dstbuf[2]; |
| 90 | struct aead_request subreq; |
| 91 | }; |
| 92 | |
| 93 | unsigned int crypto_aead_reqsize(struct crypto_aead *tfm) |
| 94 | { |
| 95 | return tfm->reqsize + sizeof(struct aead_old_request); |
| 96 | } |
| 97 | EXPORT_SYMBOL_GPL(crypto_aead_reqsize); |
| 98 | |
| 99 | static int old_crypt(struct aead_request *req, |
| 100 | int (*crypt)(struct aead_request *req)) |
| 101 | { |
| 102 | struct aead_old_request *nreq = aead_request_ctx(req); |
| 103 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 104 | struct scatterlist *src, *dst; |
| 105 | |
| 106 | if (req->old) |
| 107 | return crypt(req); |
| 108 | |
| 109 | src = scatterwalk_ffwd(nreq->srcbuf, req->src, |
| 110 | req->assoclen + req->cryptoff); |
| 111 | dst = scatterwalk_ffwd(nreq->dstbuf, req->dst, |
| 112 | req->assoclen + req->cryptoff); |
| 113 | |
| 114 | aead_request_set_tfm(&nreq->subreq, aead); |
| 115 | aead_request_set_callback(&nreq->subreq, aead_request_flags(req), |
| 116 | req->base.complete, req->base.data); |
| 117 | aead_request_set_crypt(&nreq->subreq, src, dst, req->cryptlen, |
| 118 | req->iv); |
| 119 | aead_request_set_assoc(&nreq->subreq, req->src, req->assoclen); |
| 120 | |
| 121 | return crypt(&nreq->subreq); |
| 122 | } |
| 123 | |
| 124 | static int old_encrypt(struct aead_request *req) |
| 125 | { |
| 126 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Herbert Xu | 2d0f230 | 2015-05-21 15:11:02 +0800 | [diff] [blame] | 127 | struct old_aead_alg *alg = crypto_old_aead_alg(aead); |
Herbert Xu | 996d98d | 2015-05-21 15:11:01 +0800 | [diff] [blame] | 128 | |
| 129 | return old_crypt(req, alg->encrypt); |
| 130 | } |
| 131 | |
| 132 | static int old_decrypt(struct aead_request *req) |
| 133 | { |
| 134 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Herbert Xu | 2d0f230 | 2015-05-21 15:11:02 +0800 | [diff] [blame] | 135 | struct old_aead_alg *alg = crypto_old_aead_alg(aead); |
Herbert Xu | 996d98d | 2015-05-21 15:11:01 +0800 | [diff] [blame] | 136 | |
| 137 | return old_crypt(req, alg->decrypt); |
| 138 | } |
| 139 | |
Herbert Xu | aedb30d | 2007-12-12 19:27:25 +0800 | [diff] [blame] | 140 | static int no_givcrypt(struct aead_givcrypt_request *req) |
Herbert Xu | 743edf5 | 2007-12-10 16:18:01 +0800 | [diff] [blame] | 141 | { |
| 142 | return -ENOSYS; |
| 143 | } |
| 144 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 145 | static int crypto_old_aead_init_tfm(struct crypto_tfm *tfm) |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 146 | { |
Herbert Xu | 2d0f230 | 2015-05-21 15:11:02 +0800 | [diff] [blame] | 147 | struct old_aead_alg *alg = &tfm->__crt_alg->cra_aead; |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 148 | struct crypto_aead *crt = __crypto_aead_cast(tfm); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 149 | |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 150 | if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8) |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 151 | return -EINVAL; |
| 152 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 153 | crt->setkey = alg->setkey; |
| 154 | crt->setauthsize = alg->setauthsize; |
Herbert Xu | 996d98d | 2015-05-21 15:11:01 +0800 | [diff] [blame] | 155 | crt->encrypt = old_encrypt; |
| 156 | crt->decrypt = old_decrypt; |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 157 | if (alg->ivsize) { |
| 158 | crt->givencrypt = alg->givencrypt ?: no_givcrypt; |
| 159 | crt->givdecrypt = alg->givdecrypt ?: no_givcrypt; |
| 160 | } else { |
| 161 | crt->givencrypt = aead_null_givencrypt; |
| 162 | crt->givdecrypt = aead_null_givdecrypt; |
| 163 | } |
| 164 | crt->child = __crypto_aead_cast(tfm); |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 165 | crt->authsize = alg->maxauthsize; |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 170 | static int crypto_aead_init_tfm(struct crypto_tfm *tfm) |
| 171 | { |
| 172 | struct crypto_aead *aead = __crypto_aead_cast(tfm); |
| 173 | struct aead_alg *alg = crypto_aead_alg(aead); |
| 174 | |
| 175 | if (crypto_old_aead_alg(aead)->encrypt) |
| 176 | return crypto_old_aead_init_tfm(tfm); |
| 177 | |
| 178 | aead->setkey = alg->setkey; |
| 179 | aead->setauthsize = alg->setauthsize; |
| 180 | aead->encrypt = alg->encrypt; |
| 181 | aead->decrypt = alg->decrypt; |
| 182 | aead->child = __crypto_aead_cast(tfm); |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 183 | aead->authsize = alg->maxauthsize; |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
Herbert Xu | 3acc847 | 2011-11-03 23:46:07 +1100 | [diff] [blame] | 188 | #ifdef CONFIG_NET |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 189 | 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] | 190 | { |
| 191 | struct crypto_report_aead raead; |
Herbert Xu | 2d0f230 | 2015-05-21 15:11:02 +0800 | [diff] [blame] | 192 | struct old_aead_alg *aead = &alg->cra_aead; |
Steffen Klassert | 6ad414f | 2011-09-27 07:44:27 +0200 | [diff] [blame] | 193 | |
Mathias Krause | 9a5467bf | 2013-02-05 18:19:13 +0100 | [diff] [blame] | 194 | strncpy(raead.type, "aead", sizeof(raead.type)); |
| 195 | strncpy(raead.geniv, aead->geniv ?: "<built-in>", sizeof(raead.geniv)); |
Steffen Klassert | 6ad414f | 2011-09-27 07:44:27 +0200 | [diff] [blame] | 196 | |
| 197 | raead.blocksize = alg->cra_blocksize; |
| 198 | raead.maxauthsize = aead->maxauthsize; |
| 199 | raead.ivsize = aead->ivsize; |
| 200 | |
David S. Miller | 6662df3 | 2012-04-01 20:19:05 -0400 | [diff] [blame] | 201 | if (nla_put(skb, CRYPTOCFGA_REPORT_AEAD, |
| 202 | sizeof(struct crypto_report_aead), &raead)) |
| 203 | goto nla_put_failure; |
Steffen Klassert | 6ad414f | 2011-09-27 07:44:27 +0200 | [diff] [blame] | 204 | return 0; |
| 205 | |
| 206 | nla_put_failure: |
| 207 | return -EMSGSIZE; |
| 208 | } |
Herbert Xu | 3acc847 | 2011-11-03 23:46:07 +1100 | [diff] [blame] | 209 | #else |
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) |
| 211 | { |
| 212 | return -ENOSYS; |
| 213 | } |
| 214 | #endif |
| 215 | |
| 216 | static void crypto_old_aead_show(struct seq_file *m, struct crypto_alg *alg) |
| 217 | __attribute__ ((unused)); |
| 218 | static void crypto_old_aead_show(struct seq_file *m, struct crypto_alg *alg) |
| 219 | { |
| 220 | struct old_aead_alg *aead = &alg->cra_aead; |
| 221 | |
| 222 | seq_printf(m, "type : aead\n"); |
| 223 | seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ? |
| 224 | "yes" : "no"); |
| 225 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); |
| 226 | seq_printf(m, "ivsize : %u\n", aead->ivsize); |
| 227 | seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize); |
| 228 | seq_printf(m, "geniv : %s\n", aead->geniv ?: "<built-in>"); |
| 229 | } |
| 230 | |
| 231 | const struct crypto_type crypto_aead_type = { |
| 232 | .extsize = crypto_alg_extsize, |
| 233 | .init_tfm = crypto_aead_init_tfm, |
| 234 | #ifdef CONFIG_PROC_FS |
| 235 | .show = crypto_old_aead_show, |
| 236 | #endif |
| 237 | .report = crypto_old_aead_report, |
| 238 | .lookup = crypto_lookup_aead, |
| 239 | .maskclear = ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV), |
| 240 | .maskset = CRYPTO_ALG_TYPE_MASK, |
| 241 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 242 | .tfmsize = offsetof(struct crypto_aead, base), |
| 243 | }; |
| 244 | EXPORT_SYMBOL_GPL(crypto_aead_type); |
| 245 | |
| 246 | #ifdef CONFIG_NET |
| 247 | static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg) |
| 248 | { |
| 249 | struct crypto_report_aead raead; |
| 250 | struct aead_alg *aead = container_of(alg, struct aead_alg, base); |
| 251 | |
| 252 | strncpy(raead.type, "aead", sizeof(raead.type)); |
| 253 | strncpy(raead.geniv, "<none>", sizeof(raead.geniv)); |
| 254 | |
| 255 | raead.blocksize = alg->cra_blocksize; |
| 256 | raead.maxauthsize = aead->maxauthsize; |
| 257 | raead.ivsize = aead->ivsize; |
| 258 | |
| 259 | if (nla_put(skb, CRYPTOCFGA_REPORT_AEAD, |
| 260 | sizeof(struct crypto_report_aead), &raead)) |
| 261 | goto nla_put_failure; |
| 262 | return 0; |
| 263 | |
| 264 | nla_put_failure: |
| 265 | return -EMSGSIZE; |
| 266 | } |
| 267 | #else |
Herbert Xu | 3acc847 | 2011-11-03 23:46:07 +1100 | [diff] [blame] | 268 | static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg) |
| 269 | { |
| 270 | return -ENOSYS; |
| 271 | } |
| 272 | #endif |
Steffen Klassert | 6ad414f | 2011-09-27 07:44:27 +0200 | [diff] [blame] | 273 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 274 | static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg) |
| 275 | __attribute__ ((unused)); |
| 276 | static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg) |
| 277 | { |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 278 | struct aead_alg *aead = container_of(alg, struct aead_alg, base); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 279 | |
| 280 | seq_printf(m, "type : aead\n"); |
Herbert Xu | 189ed66 | 2007-12-14 22:29:37 +0800 | [diff] [blame] | 281 | seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ? |
| 282 | "yes" : "no"); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 283 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); |
| 284 | seq_printf(m, "ivsize : %u\n", aead->ivsize); |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 285 | seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize); |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 286 | seq_printf(m, "geniv : <none>\n"); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 287 | } |
| 288 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 289 | static const struct crypto_type crypto_new_aead_type = { |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 290 | .extsize = crypto_alg_extsize, |
| 291 | .init_tfm = crypto_aead_init_tfm, |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 292 | #ifdef CONFIG_PROC_FS |
| 293 | .show = crypto_aead_show, |
| 294 | #endif |
Steffen Klassert | 6ad414f | 2011-09-27 07:44:27 +0200 | [diff] [blame] | 295 | .report = crypto_aead_report, |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 296 | .maskclear = ~CRYPTO_ALG_TYPE_MASK, |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 297 | .maskset = CRYPTO_ALG_TYPE_MASK, |
| 298 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 299 | .tfmsize = offsetof(struct crypto_aead, base), |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 300 | }; |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 301 | |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 302 | static int aead_null_givencrypt(struct aead_givcrypt_request *req) |
| 303 | { |
| 304 | return crypto_aead_encrypt(&req->areq); |
| 305 | } |
| 306 | |
| 307 | static int aead_null_givdecrypt(struct aead_givcrypt_request *req) |
| 308 | { |
| 309 | return crypto_aead_decrypt(&req->areq); |
| 310 | } |
| 311 | |
Herbert Xu | 3acc847 | 2011-11-03 23:46:07 +1100 | [diff] [blame] | 312 | #ifdef CONFIG_NET |
Steffen Klassert | b735d0a | 2011-09-27 07:45:44 +0200 | [diff] [blame] | 313 | static int crypto_nivaead_report(struct sk_buff *skb, struct crypto_alg *alg) |
| 314 | { |
| 315 | struct crypto_report_aead raead; |
Herbert Xu | 2d0f230 | 2015-05-21 15:11:02 +0800 | [diff] [blame] | 316 | struct old_aead_alg *aead = &alg->cra_aead; |
Steffen Klassert | b735d0a | 2011-09-27 07:45:44 +0200 | [diff] [blame] | 317 | |
Mathias Krause | 9a5467bf | 2013-02-05 18:19:13 +0100 | [diff] [blame] | 318 | strncpy(raead.type, "nivaead", sizeof(raead.type)); |
| 319 | strncpy(raead.geniv, aead->geniv, sizeof(raead.geniv)); |
Steffen Klassert | b735d0a | 2011-09-27 07:45:44 +0200 | [diff] [blame] | 320 | |
| 321 | raead.blocksize = alg->cra_blocksize; |
| 322 | raead.maxauthsize = aead->maxauthsize; |
| 323 | raead.ivsize = aead->ivsize; |
| 324 | |
David S. Miller | 6662df3 | 2012-04-01 20:19:05 -0400 | [diff] [blame] | 325 | if (nla_put(skb, CRYPTOCFGA_REPORT_AEAD, |
| 326 | sizeof(struct crypto_report_aead), &raead)) |
| 327 | goto nla_put_failure; |
Steffen Klassert | b735d0a | 2011-09-27 07:45:44 +0200 | [diff] [blame] | 328 | return 0; |
| 329 | |
| 330 | nla_put_failure: |
| 331 | return -EMSGSIZE; |
| 332 | } |
Herbert Xu | 3acc847 | 2011-11-03 23:46:07 +1100 | [diff] [blame] | 333 | #else |
| 334 | static int crypto_nivaead_report(struct sk_buff *skb, struct crypto_alg *alg) |
| 335 | { |
| 336 | return -ENOSYS; |
| 337 | } |
| 338 | #endif |
Steffen Klassert | b735d0a | 2011-09-27 07:45:44 +0200 | [diff] [blame] | 339 | |
| 340 | |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 341 | static void crypto_nivaead_show(struct seq_file *m, struct crypto_alg *alg) |
| 342 | __attribute__ ((unused)); |
| 343 | static void crypto_nivaead_show(struct seq_file *m, struct crypto_alg *alg) |
| 344 | { |
Herbert Xu | 2d0f230 | 2015-05-21 15:11:02 +0800 | [diff] [blame] | 345 | struct old_aead_alg *aead = &alg->cra_aead; |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 346 | |
| 347 | seq_printf(m, "type : nivaead\n"); |
Herbert Xu | 189ed66 | 2007-12-14 22:29:37 +0800 | [diff] [blame] | 348 | seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ? |
| 349 | "yes" : "no"); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 350 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); |
| 351 | seq_printf(m, "ivsize : %u\n", aead->ivsize); |
| 352 | seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize); |
| 353 | seq_printf(m, "geniv : %s\n", aead->geniv); |
| 354 | } |
| 355 | |
| 356 | const struct crypto_type crypto_nivaead_type = { |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 357 | .extsize = crypto_alg_extsize, |
| 358 | .init_tfm = crypto_aead_init_tfm, |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 359 | #ifdef CONFIG_PROC_FS |
| 360 | .show = crypto_nivaead_show, |
| 361 | #endif |
Steffen Klassert | b735d0a | 2011-09-27 07:45:44 +0200 | [diff] [blame] | 362 | .report = crypto_nivaead_report, |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 363 | .maskclear = ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV), |
| 364 | .maskset = CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV, |
| 365 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 366 | .tfmsize = offsetof(struct crypto_aead, base), |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 367 | }; |
| 368 | EXPORT_SYMBOL_GPL(crypto_nivaead_type); |
| 369 | |
| 370 | static int crypto_grab_nivaead(struct crypto_aead_spawn *spawn, |
| 371 | const char *name, u32 type, u32 mask) |
| 372 | { |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 373 | spawn->base.frontend = &crypto_nivaead_type; |
| 374 | return crypto_grab_spawn(&spawn->base, name, type, mask); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 375 | } |
| 376 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 377 | struct aead_instance *aead_geniv_alloc(struct crypto_template *tmpl, |
| 378 | struct rtattr **tb, u32 type, u32 mask) |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 379 | { |
| 380 | const char *name; |
| 381 | struct crypto_aead_spawn *spawn; |
| 382 | struct crypto_attr_type *algt; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 383 | struct aead_instance *inst; |
| 384 | struct aead_alg *alg; |
| 385 | unsigned int ivsize; |
| 386 | unsigned int maxauthsize; |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 387 | int err; |
| 388 | |
| 389 | algt = crypto_get_attr_type(tb); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 390 | if (IS_ERR(algt)) |
Julia Lawall | 3e8afe3 | 2013-01-22 12:29:26 +0100 | [diff] [blame] | 391 | return ERR_CAST(algt); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 392 | |
| 393 | if ((algt->type ^ (CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV)) & |
| 394 | algt->mask) |
| 395 | return ERR_PTR(-EINVAL); |
| 396 | |
| 397 | name = crypto_attr_alg_name(tb[1]); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 398 | if (IS_ERR(name)) |
Julia Lawall | 3e8afe3 | 2013-01-22 12:29:26 +0100 | [diff] [blame] | 399 | return ERR_CAST(name); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 400 | |
| 401 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
| 402 | if (!inst) |
| 403 | return ERR_PTR(-ENOMEM); |
| 404 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 405 | spawn = aead_instance_ctx(inst); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 406 | |
| 407 | /* Ignore async algorithms if necessary. */ |
| 408 | mask |= crypto_requires_sync(algt->type, algt->mask); |
| 409 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 410 | crypto_set_aead_spawn(spawn, aead_crypto_instance(inst)); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 411 | err = crypto_grab_nivaead(spawn, name, type, mask); |
| 412 | if (err) |
| 413 | goto err_free_inst; |
| 414 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 415 | alg = crypto_spawn_aead_alg(spawn); |
| 416 | |
Herbert Xu | 30e4c01 | 2015-05-22 16:30:48 +0800 | [diff] [blame] | 417 | ivsize = crypto_aead_alg_ivsize(alg); |
| 418 | maxauthsize = crypto_aead_alg_maxauthsize(alg); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 419 | |
| 420 | err = -EINVAL; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 421 | if (!ivsize) |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 422 | goto err_drop_alg; |
| 423 | |
| 424 | /* |
| 425 | * This is only true if we're constructing an algorithm with its |
| 426 | * default IV generator. For the default generator we elide the |
| 427 | * template name and double-check the IV generator. |
| 428 | */ |
| 429 | if (algt->mask & CRYPTO_ALG_GENIV) { |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 430 | if (!alg->base.cra_aead.encrypt) |
| 431 | goto err_drop_alg; |
| 432 | if (strcmp(tmpl->name, alg->base.cra_aead.geniv)) |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 433 | goto err_drop_alg; |
| 434 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 435 | memcpy(inst->alg.base.cra_name, alg->base.cra_name, |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 436 | CRYPTO_MAX_ALG_NAME); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 437 | memcpy(inst->alg.base.cra_driver_name, |
| 438 | alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME); |
| 439 | |
| 440 | inst->alg.base.cra_flags = CRYPTO_ALG_TYPE_AEAD | |
| 441 | CRYPTO_ALG_GENIV; |
| 442 | inst->alg.base.cra_flags |= alg->base.cra_flags & |
| 443 | CRYPTO_ALG_ASYNC; |
| 444 | inst->alg.base.cra_priority = alg->base.cra_priority; |
| 445 | inst->alg.base.cra_blocksize = alg->base.cra_blocksize; |
| 446 | inst->alg.base.cra_alignmask = alg->base.cra_alignmask; |
| 447 | inst->alg.base.cra_type = &crypto_aead_type; |
| 448 | |
| 449 | inst->alg.base.cra_aead.ivsize = ivsize; |
| 450 | inst->alg.base.cra_aead.maxauthsize = maxauthsize; |
| 451 | |
| 452 | inst->alg.base.cra_aead.setkey = alg->base.cra_aead.setkey; |
| 453 | inst->alg.base.cra_aead.setauthsize = |
| 454 | alg->base.cra_aead.setauthsize; |
| 455 | inst->alg.base.cra_aead.encrypt = alg->base.cra_aead.encrypt; |
| 456 | inst->alg.base.cra_aead.decrypt = alg->base.cra_aead.decrypt; |
| 457 | |
| 458 | goto out; |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 459 | } |
| 460 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 461 | err = -ENAMETOOLONG; |
| 462 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
| 463 | "%s(%s)", tmpl->name, alg->base.cra_name) >= |
| 464 | CRYPTO_MAX_ALG_NAME) |
| 465 | goto err_drop_alg; |
| 466 | if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
| 467 | "%s(%s)", tmpl->name, alg->base.cra_driver_name) >= |
| 468 | CRYPTO_MAX_ALG_NAME) |
| 469 | goto err_drop_alg; |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 470 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 471 | inst->alg.base.cra_flags = CRYPTO_ALG_TYPE_AEAD; |
| 472 | inst->alg.base.cra_flags |= alg->base.cra_flags & CRYPTO_ALG_ASYNC; |
| 473 | inst->alg.base.cra_priority = alg->base.cra_priority; |
| 474 | inst->alg.base.cra_blocksize = alg->base.cra_blocksize; |
| 475 | inst->alg.base.cra_alignmask = alg->base.cra_alignmask; |
| 476 | inst->alg.base.cra_type = &crypto_new_aead_type; |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 477 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 478 | inst->alg.ivsize = ivsize; |
| 479 | inst->alg.maxauthsize = maxauthsize; |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 480 | |
| 481 | out: |
| 482 | return inst; |
| 483 | |
| 484 | err_drop_alg: |
| 485 | crypto_drop_aead(spawn); |
| 486 | err_free_inst: |
| 487 | kfree(inst); |
| 488 | inst = ERR_PTR(err); |
| 489 | goto out; |
| 490 | } |
| 491 | EXPORT_SYMBOL_GPL(aead_geniv_alloc); |
| 492 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 493 | void aead_geniv_free(struct aead_instance *inst) |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 494 | { |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 495 | crypto_drop_aead(aead_instance_ctx(inst)); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 496 | kfree(inst); |
| 497 | } |
| 498 | EXPORT_SYMBOL_GPL(aead_geniv_free); |
| 499 | |
| 500 | int aead_geniv_init(struct crypto_tfm *tfm) |
| 501 | { |
| 502 | struct crypto_instance *inst = (void *)tfm->__crt_alg; |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 503 | struct crypto_aead *child; |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 504 | struct crypto_aead *aead; |
| 505 | |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 506 | aead = __crypto_aead_cast(tfm); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 507 | |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 508 | child = crypto_spawn_aead(crypto_instance_ctx(inst)); |
| 509 | if (IS_ERR(child)) |
| 510 | return PTR_ERR(child); |
| 511 | |
| 512 | aead->child = child; |
| 513 | aead->reqsize += crypto_aead_reqsize(child); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | EXPORT_SYMBOL_GPL(aead_geniv_init); |
| 518 | |
| 519 | void aead_geniv_exit(struct crypto_tfm *tfm) |
| 520 | { |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 521 | crypto_free_aead(__crypto_aead_cast(tfm)->child); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 522 | } |
| 523 | EXPORT_SYMBOL_GPL(aead_geniv_exit); |
| 524 | |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 525 | static int crypto_nivaead_default(struct crypto_alg *alg, u32 type, u32 mask) |
| 526 | { |
| 527 | struct rtattr *tb[3]; |
| 528 | struct { |
| 529 | struct rtattr attr; |
| 530 | struct crypto_attr_type data; |
| 531 | } ptype; |
| 532 | struct { |
| 533 | struct rtattr attr; |
| 534 | struct crypto_attr_alg data; |
| 535 | } palg; |
| 536 | struct crypto_template *tmpl; |
| 537 | struct crypto_instance *inst; |
| 538 | struct crypto_alg *larval; |
| 539 | const char *geniv; |
| 540 | int err; |
| 541 | |
| 542 | larval = crypto_larval_lookup(alg->cra_driver_name, |
| 543 | CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV, |
| 544 | CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV); |
| 545 | err = PTR_ERR(larval); |
| 546 | if (IS_ERR(larval)) |
| 547 | goto out; |
| 548 | |
| 549 | err = -EAGAIN; |
| 550 | if (!crypto_is_larval(larval)) |
| 551 | goto drop_larval; |
| 552 | |
| 553 | ptype.attr.rta_len = sizeof(ptype); |
| 554 | ptype.attr.rta_type = CRYPTOA_TYPE; |
| 555 | ptype.data.type = type | CRYPTO_ALG_GENIV; |
| 556 | /* GENIV tells the template that we're making a default geniv. */ |
| 557 | ptype.data.mask = mask | CRYPTO_ALG_GENIV; |
| 558 | tb[0] = &ptype.attr; |
| 559 | |
| 560 | palg.attr.rta_len = sizeof(palg); |
| 561 | palg.attr.rta_type = CRYPTOA_ALG; |
| 562 | /* Must use the exact name to locate ourselves. */ |
| 563 | memcpy(palg.data.name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME); |
| 564 | tb[1] = &palg.attr; |
| 565 | |
| 566 | tb[2] = NULL; |
| 567 | |
| 568 | geniv = alg->cra_aead.geniv; |
| 569 | |
| 570 | tmpl = crypto_lookup_template(geniv); |
| 571 | err = -ENOENT; |
| 572 | if (!tmpl) |
| 573 | goto kill_larval; |
| 574 | |
Herbert Xu | 72af865 | 2015-05-23 15:41:49 +0800 | [diff] [blame^] | 575 | if (tmpl->create) { |
| 576 | err = tmpl->create(tmpl, tb); |
| 577 | if (err) |
| 578 | goto put_tmpl; |
| 579 | goto ok; |
| 580 | } |
| 581 | |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 582 | inst = tmpl->alloc(tb); |
| 583 | err = PTR_ERR(inst); |
| 584 | if (IS_ERR(inst)) |
| 585 | goto put_tmpl; |
| 586 | |
Joshua I. James | 4fad478 | 2014-12-05 14:24:44 +0900 | [diff] [blame] | 587 | err = crypto_register_instance(tmpl, inst); |
| 588 | if (err) { |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 589 | tmpl->free(inst); |
| 590 | goto put_tmpl; |
| 591 | } |
| 592 | |
Herbert Xu | 72af865 | 2015-05-23 15:41:49 +0800 | [diff] [blame^] | 593 | ok: |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 594 | /* Redo the lookup to use the instance we just registered. */ |
| 595 | err = -EAGAIN; |
| 596 | |
| 597 | put_tmpl: |
| 598 | crypto_tmpl_put(tmpl); |
| 599 | kill_larval: |
| 600 | crypto_larval_kill(larval); |
| 601 | drop_larval: |
| 602 | crypto_mod_put(larval); |
| 603 | out: |
| 604 | crypto_mod_put(alg); |
| 605 | return err; |
| 606 | } |
| 607 | |
Steffen Klassert | 1e12299 | 2012-03-29 09:03:47 +0200 | [diff] [blame] | 608 | 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] | 609 | { |
| 610 | struct crypto_alg *alg; |
| 611 | |
| 612 | alg = crypto_alg_mod_lookup(name, type, mask); |
| 613 | if (IS_ERR(alg)) |
| 614 | return alg; |
| 615 | |
| 616 | if (alg->cra_type == &crypto_aead_type) |
| 617 | return alg; |
| 618 | |
| 619 | if (!alg->cra_aead.ivsize) |
| 620 | return alg; |
| 621 | |
Herbert Xu | 5852ae4 | 2009-02-18 20:41:47 +0800 | [diff] [blame] | 622 | crypto_mod_put(alg); |
| 623 | alg = crypto_alg_mod_lookup(name, type | CRYPTO_ALG_TESTED, |
| 624 | mask & ~CRYPTO_ALG_TESTED); |
| 625 | if (IS_ERR(alg)) |
| 626 | return alg; |
| 627 | |
| 628 | if (alg->cra_type == &crypto_aead_type) { |
Herbert Xu | 80f7b35 | 2015-04-23 16:37:46 +0800 | [diff] [blame] | 629 | if (~alg->cra_flags & (type ^ ~mask) & CRYPTO_ALG_TESTED) { |
Herbert Xu | 5852ae4 | 2009-02-18 20:41:47 +0800 | [diff] [blame] | 630 | crypto_mod_put(alg); |
| 631 | alg = ERR_PTR(-ENOENT); |
| 632 | } |
| 633 | return alg; |
| 634 | } |
| 635 | |
| 636 | BUG_ON(!alg->cra_aead.ivsize); |
| 637 | |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 638 | return ERR_PTR(crypto_nivaead_default(alg, type, mask)); |
| 639 | } |
Steffen Klassert | 1e12299 | 2012-03-29 09:03:47 +0200 | [diff] [blame] | 640 | EXPORT_SYMBOL_GPL(crypto_lookup_aead); |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 641 | |
| 642 | int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name, |
| 643 | u32 type, u32 mask) |
| 644 | { |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 645 | spawn->base.frontend = &crypto_aead_type; |
| 646 | return crypto_grab_spawn(&spawn->base, name, type, mask); |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 647 | } |
| 648 | EXPORT_SYMBOL_GPL(crypto_grab_aead); |
| 649 | |
| 650 | struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask) |
| 651 | { |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 652 | return crypto_alloc_tfm(alg_name, &crypto_aead_type, type, mask); |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 653 | } |
| 654 | EXPORT_SYMBOL_GPL(crypto_alloc_aead); |
| 655 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 656 | static int aead_prepare_alg(struct aead_alg *alg) |
| 657 | { |
| 658 | struct crypto_alg *base = &alg->base; |
| 659 | |
| 660 | if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8) |
| 661 | return -EINVAL; |
| 662 | |
| 663 | base->cra_type = &crypto_new_aead_type; |
| 664 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; |
| 665 | base->cra_flags |= CRYPTO_ALG_TYPE_AEAD; |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | int crypto_register_aead(struct aead_alg *alg) |
| 671 | { |
| 672 | struct crypto_alg *base = &alg->base; |
| 673 | int err; |
| 674 | |
| 675 | err = aead_prepare_alg(alg); |
| 676 | if (err) |
| 677 | return err; |
| 678 | |
| 679 | return crypto_register_alg(base); |
| 680 | } |
| 681 | EXPORT_SYMBOL_GPL(crypto_register_aead); |
| 682 | |
| 683 | int crypto_unregister_aead(struct aead_alg *alg) |
| 684 | { |
| 685 | return crypto_unregister_alg(&alg->base); |
| 686 | } |
| 687 | EXPORT_SYMBOL_GPL(crypto_unregister_aead); |
| 688 | |
| 689 | int aead_register_instance(struct crypto_template *tmpl, |
| 690 | struct aead_instance *inst) |
| 691 | { |
| 692 | int err; |
| 693 | |
| 694 | err = aead_prepare_alg(&inst->alg); |
| 695 | if (err) |
| 696 | return err; |
| 697 | |
| 698 | return crypto_register_instance(tmpl, aead_crypto_instance(inst)); |
| 699 | } |
| 700 | EXPORT_SYMBOL_GPL(aead_register_instance); |
| 701 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 702 | MODULE_LICENSE("GPL"); |
| 703 | MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)"); |