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