aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHerbert Xu <herbert@gondor.apana.org.au>2015-05-11 05:48:12 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2015-05-12 22:31:53 -0400
commit5d1d65f8bea6de3d9c2c60fdfdd2da02da5ea672 (patch)
tree0ae938f7c50e7a8227f2f93087edf843b44621e5
parent53033d4d36b0299ef02e28155913414ec1089aac (diff)
crypto: aead - Convert top level interface to new style
This patch converts the top-level aead interface to the new style. All user-level AEAD interface code have been moved into crypto/aead.h. The allocation/free functions have switched over to the new way of allocating tfms. This patch also removes the double indrection on setkey so the indirection now exists only at the alg level. Apart from these there are no user-visible changes. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r--crypto/aead.c161
-rw-r--r--include/crypto/aead.h436
-rw-r--r--include/crypto/algapi.h33
-rw-r--r--include/crypto/internal/aead.h38
-rw-r--r--include/linux/crypto.h442
5 files changed, 516 insertions, 594 deletions
diff --git a/crypto/aead.c b/crypto/aead.c
index d6ad0c66ee83..717b2f6ec9bb 100644
--- a/crypto/aead.c
+++ b/crypto/aead.c
@@ -26,6 +26,9 @@
26 26
27#include "internal.h" 27#include "internal.h"
28 28
29static int aead_null_givencrypt(struct aead_givcrypt_request *req);
30static int aead_null_givdecrypt(struct aead_givcrypt_request *req);
31
29static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key, 32static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key,
30 unsigned int keylen) 33 unsigned int keylen)
31{ 34{
@@ -48,63 +51,63 @@ static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key,
48 return ret; 51 return ret;
49} 52}
50 53
51static int setkey(struct crypto_aead *tfm, const u8 *key, unsigned int keylen) 54int crypto_aead_setkey(struct crypto_aead *tfm,
55 const u8 *key, unsigned int keylen)
52{ 56{
53 struct aead_alg *aead = crypto_aead_alg(tfm); 57 struct aead_alg *aead = crypto_aead_alg(tfm);
54 unsigned long alignmask = crypto_aead_alignmask(tfm); 58 unsigned long alignmask = crypto_aead_alignmask(tfm);
55 59
60 tfm = tfm->child;
61
56 if ((unsigned long)key & alignmask) 62 if ((unsigned long)key & alignmask)
57 return setkey_unaligned(tfm, key, keylen); 63 return setkey_unaligned(tfm, key, keylen);
58 64
59 return aead->setkey(tfm, key, keylen); 65 return aead->setkey(tfm, key, keylen);
60} 66}
67EXPORT_SYMBOL_GPL(crypto_aead_setkey);
61 68
62int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize) 69int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
63{ 70{
64 struct aead_tfm *crt = crypto_aead_crt(tfm);
65 int err; 71 int err;
66 72
67 if (authsize > crypto_aead_alg(tfm)->maxauthsize) 73 if (authsize > crypto_aead_alg(tfm)->maxauthsize)
68 return -EINVAL; 74 return -EINVAL;
69 75
70 if (crypto_aead_alg(tfm)->setauthsize) { 76 if (crypto_aead_alg(tfm)->setauthsize) {
71 err = crypto_aead_alg(tfm)->setauthsize(crt->base, authsize); 77 err = crypto_aead_alg(tfm)->setauthsize(tfm->child, authsize);
72 if (err) 78 if (err)
73 return err; 79 return err;
74 } 80 }
75 81
76 crypto_aead_crt(crt->base)->authsize = authsize; 82 tfm->child->authsize = authsize;
77 crt->authsize = authsize; 83 tfm->authsize = authsize;
78 return 0; 84 return 0;
79} 85}
80EXPORT_SYMBOL_GPL(crypto_aead_setauthsize); 86EXPORT_SYMBOL_GPL(crypto_aead_setauthsize);
81 87
82static unsigned int crypto_aead_ctxsize(struct crypto_alg *alg, u32 type,
83 u32 mask)
84{
85 return alg->cra_ctxsize;
86}
87
88static int no_givcrypt(struct aead_givcrypt_request *req) 88static int no_givcrypt(struct aead_givcrypt_request *req)
89{ 89{
90 return -ENOSYS; 90 return -ENOSYS;
91} 91}
92 92
93static int crypto_init_aead_ops(struct crypto_tfm *tfm, u32 type, u32 mask) 93static int crypto_aead_init_tfm(struct crypto_tfm *tfm)
94{ 94{
95 struct aead_alg *alg = &tfm->__crt_alg->cra_aead; 95 struct aead_alg *alg = &tfm->__crt_alg->cra_aead;
96 struct aead_tfm *crt = &tfm->crt_aead; 96 struct crypto_aead *crt = __crypto_aead_cast(tfm);
97 97
98 if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8) 98 if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8)
99 return -EINVAL; 99 return -EINVAL;
100 100
101 crt->setkey = tfm->__crt_alg->cra_flags & CRYPTO_ALG_GENIV ?
102 alg->setkey : setkey;
103 crt->encrypt = alg->encrypt; 101 crt->encrypt = alg->encrypt;
104 crt->decrypt = alg->decrypt; 102 crt->decrypt = alg->decrypt;
105 crt->givencrypt = alg->givencrypt ?: no_givcrypt; 103 if (alg->ivsize) {
106 crt->givdecrypt = alg->givdecrypt ?: no_givcrypt; 104 crt->givencrypt = alg->givencrypt ?: no_givcrypt;
107 crt->base = __crypto_aead_cast(tfm); 105 crt->givdecrypt = alg->givdecrypt ?: no_givcrypt;
106 } else {
107 crt->givencrypt = aead_null_givencrypt;
108 crt->givdecrypt = aead_null_givdecrypt;
109 }
110 crt->child = __crypto_aead_cast(tfm);
108 crt->ivsize = alg->ivsize; 111 crt->ivsize = alg->ivsize;
109 crt->authsize = alg->maxauthsize; 112 crt->authsize = alg->maxauthsize;
110 113
@@ -155,12 +158,17 @@ static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
155} 158}
156 159
157const struct crypto_type crypto_aead_type = { 160const struct crypto_type crypto_aead_type = {
158 .ctxsize = crypto_aead_ctxsize, 161 .extsize = crypto_alg_extsize,
159 .init = crypto_init_aead_ops, 162 .init_tfm = crypto_aead_init_tfm,
160#ifdef CONFIG_PROC_FS 163#ifdef CONFIG_PROC_FS
161 .show = crypto_aead_show, 164 .show = crypto_aead_show,
162#endif 165#endif
163 .report = crypto_aead_report, 166 .report = crypto_aead_report,
167 .lookup = crypto_lookup_aead,
168 .maskclear = ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV),
169 .maskset = CRYPTO_ALG_TYPE_MASK,
170 .type = CRYPTO_ALG_TYPE_AEAD,
171 .tfmsize = offsetof(struct crypto_aead, base),
164}; 172};
165EXPORT_SYMBOL_GPL(crypto_aead_type); 173EXPORT_SYMBOL_GPL(crypto_aead_type);
166 174
@@ -174,28 +182,6 @@ static int aead_null_givdecrypt(struct aead_givcrypt_request *req)
174 return crypto_aead_decrypt(&req->areq); 182 return crypto_aead_decrypt(&req->areq);
175} 183}
176 184
177static int crypto_init_nivaead_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
178{
179 struct aead_alg *alg = &tfm->__crt_alg->cra_aead;
180 struct aead_tfm *crt = &tfm->crt_aead;
181
182 if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8)
183 return -EINVAL;
184
185 crt->setkey = setkey;
186 crt->encrypt = alg->encrypt;
187 crt->decrypt = alg->decrypt;
188 if (!alg->ivsize) {
189 crt->givencrypt = aead_null_givencrypt;
190 crt->givdecrypt = aead_null_givdecrypt;
191 }
192 crt->base = __crypto_aead_cast(tfm);
193 crt->ivsize = alg->ivsize;
194 crt->authsize = alg->maxauthsize;
195
196 return 0;
197}
198
199#ifdef CONFIG_NET 185#ifdef CONFIG_NET
200static int crypto_nivaead_report(struct sk_buff *skb, struct crypto_alg *alg) 186static int crypto_nivaead_report(struct sk_buff *skb, struct crypto_alg *alg)
201{ 187{
@@ -241,32 +227,24 @@ static void crypto_nivaead_show(struct seq_file *m, struct crypto_alg *alg)
241} 227}
242 228
243const struct crypto_type crypto_nivaead_type = { 229const struct crypto_type crypto_nivaead_type = {
244 .ctxsize = crypto_aead_ctxsize, 230 .extsize = crypto_alg_extsize,
245 .init = crypto_init_nivaead_ops, 231 .init_tfm = crypto_aead_init_tfm,
246#ifdef CONFIG_PROC_FS 232#ifdef CONFIG_PROC_FS
247 .show = crypto_nivaead_show, 233 .show = crypto_nivaead_show,
248#endif 234#endif
249 .report = crypto_nivaead_report, 235 .report = crypto_nivaead_report,
236 .maskclear = ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV),
237 .maskset = CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV,
238 .type = CRYPTO_ALG_TYPE_AEAD,
239 .tfmsize = offsetof(struct crypto_aead, base),
250}; 240};
251EXPORT_SYMBOL_GPL(crypto_nivaead_type); 241EXPORT_SYMBOL_GPL(crypto_nivaead_type);
252 242
253static int crypto_grab_nivaead(struct crypto_aead_spawn *spawn, 243static int crypto_grab_nivaead(struct crypto_aead_spawn *spawn,
254 const char *name, u32 type, u32 mask) 244 const char *name, u32 type, u32 mask)
255{ 245{
256 struct crypto_alg *alg; 246 spawn->base.frontend = &crypto_nivaead_type;
257 int err; 247 return crypto_grab_spawn(&spawn->base, name, type, mask);
258
259 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
260 type |= CRYPTO_ALG_TYPE_AEAD;
261 mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV;
262
263 alg = crypto_alg_mod_lookup(name, type, mask);
264 if (IS_ERR(alg))
265 return PTR_ERR(alg);
266
267 err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
268 crypto_mod_put(alg);
269 return err;
270} 248}
271 249
272struct crypto_instance *aead_geniv_alloc(struct crypto_template *tmpl, 250struct crypto_instance *aead_geniv_alloc(struct crypto_template *tmpl,
@@ -374,14 +352,17 @@ EXPORT_SYMBOL_GPL(aead_geniv_free);
374int aead_geniv_init(struct crypto_tfm *tfm) 352int aead_geniv_init(struct crypto_tfm *tfm)
375{ 353{
376 struct crypto_instance *inst = (void *)tfm->__crt_alg; 354 struct crypto_instance *inst = (void *)tfm->__crt_alg;
355 struct crypto_aead *child;
377 struct crypto_aead *aead; 356 struct crypto_aead *aead;
378 357
379 aead = crypto_spawn_aead(crypto_instance_ctx(inst)); 358 aead = __crypto_aead_cast(tfm);
380 if (IS_ERR(aead))
381 return PTR_ERR(aead);
382 359
383 tfm->crt_aead.base = aead; 360 child = crypto_spawn_aead(crypto_instance_ctx(inst));
384 tfm->crt_aead.reqsize += crypto_aead_reqsize(aead); 361 if (IS_ERR(child))
362 return PTR_ERR(child);
363
364 aead->child = child;
365 aead->reqsize += crypto_aead_reqsize(child);
385 366
386 return 0; 367 return 0;
387} 368}
@@ -389,7 +370,7 @@ EXPORT_SYMBOL_GPL(aead_geniv_init);
389 370
390void aead_geniv_exit(struct crypto_tfm *tfm) 371void aead_geniv_exit(struct crypto_tfm *tfm)
391{ 372{
392 crypto_free_aead(tfm->crt_aead.base); 373 crypto_free_aead(__crypto_aead_cast(tfm)->child);
393} 374}
394EXPORT_SYMBOL_GPL(aead_geniv_exit); 375EXPORT_SYMBOL_GPL(aead_geniv_exit);
395 376
@@ -505,60 +486,14 @@ EXPORT_SYMBOL_GPL(crypto_lookup_aead);
505int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name, 486int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name,
506 u32 type, u32 mask) 487 u32 type, u32 mask)
507{ 488{
508 struct crypto_alg *alg; 489 spawn->base.frontend = &crypto_aead_type;
509 int err; 490 return crypto_grab_spawn(&spawn->base, name, type, mask);
510
511 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
512 type |= CRYPTO_ALG_TYPE_AEAD;
513 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
514 mask |= CRYPTO_ALG_TYPE_MASK;
515
516 alg = crypto_lookup_aead(name, type, mask);
517 if (IS_ERR(alg))
518 return PTR_ERR(alg);
519
520 err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
521 crypto_mod_put(alg);
522 return err;
523} 491}
524EXPORT_SYMBOL_GPL(crypto_grab_aead); 492EXPORT_SYMBOL_GPL(crypto_grab_aead);
525 493
526struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask) 494struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask)
527{ 495{
528 struct crypto_tfm *tfm; 496 return crypto_alloc_tfm(alg_name, &crypto_aead_type, type, mask);
529 int err;
530
531 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
532 type |= CRYPTO_ALG_TYPE_AEAD;
533 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
534 mask |= CRYPTO_ALG_TYPE_MASK;
535
536 for (;;) {
537 struct crypto_alg *alg;
538
539 alg = crypto_lookup_aead(alg_name, type, mask);
540 if (IS_ERR(alg)) {
541 err = PTR_ERR(alg);
542 goto err;
543 }
544
545 tfm = __crypto_alloc_tfm(alg, type, mask);
546 if (!IS_ERR(tfm))
547 return __crypto_aead_cast(tfm);
548
549 crypto_mod_put(alg);
550 err = PTR_ERR(tfm);
551
552err:
553 if (err != -EAGAIN)
554 break;
555 if (signal_pending(current)) {
556 err = -EINTR;
557 break;
558 }
559 }
560
561 return ERR_PTR(err);
562} 497}
563EXPORT_SYMBOL_GPL(crypto_alloc_aead); 498EXPORT_SYMBOL_GPL(crypto_alloc_aead);
564 499
diff --git a/include/crypto/aead.h b/include/crypto/aead.h
index 94b19be67574..dbcad08f4891 100644
--- a/include/crypto/aead.h
+++ b/include/crypto/aead.h
@@ -18,6 +18,62 @@
18#include <linux/slab.h> 18#include <linux/slab.h>
19 19
20/** 20/**
21 * DOC: Authenticated Encryption With Associated Data (AEAD) Cipher API
22 *
23 * The AEAD cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_AEAD
24 * (listed as type "aead" in /proc/crypto)
25 *
26 * The most prominent examples for this type of encryption is GCM and CCM.
27 * However, the kernel supports other types of AEAD ciphers which are defined
28 * with the following cipher string:
29 *
30 * authenc(keyed message digest, block cipher)
31 *
32 * For example: authenc(hmac(sha256), cbc(aes))
33 *
34 * The example code provided for the asynchronous block cipher operation
35 * applies here as well. Naturally all *ablkcipher* symbols must be exchanged
36 * the *aead* pendants discussed in the following. In addtion, for the AEAD
37 * operation, the aead_request_set_assoc function must be used to set the
38 * pointer to the associated data memory location before performing the
39 * encryption or decryption operation. In case of an encryption, the associated
40 * data memory is filled during the encryption operation. For decryption, the
41 * associated data memory must contain data that is used to verify the integrity
42 * of the decrypted data. Another deviation from the asynchronous block cipher
43 * operation is that the caller should explicitly check for -EBADMSG of the
44 * crypto_aead_decrypt. That error indicates an authentication error, i.e.
45 * a breach in the integrity of the message. In essence, that -EBADMSG error
46 * code is the key bonus an AEAD cipher has over "standard" block chaining
47 * modes.
48 */
49
50/**
51 * struct aead_request - AEAD request
52 * @base: Common attributes for async crypto requests
53 * @assoclen: Length in bytes of associated data for authentication
54 * @cryptlen: Length of data to be encrypted or decrypted
55 * @iv: Initialisation vector
56 * @assoc: Associated data
57 * @src: Source data
58 * @dst: Destination data
59 * @__ctx: Start of private context data
60 */
61struct aead_request {
62 struct crypto_async_request base;
63
64 unsigned int assoclen;
65 unsigned int cryptlen;
66
67 u8 *iv;
68
69 struct scatterlist *assoc;
70 struct scatterlist *src;
71 struct scatterlist *dst;
72
73 void *__ctx[] CRYPTO_MINALIGN_ATTR;
74};
75
76/**
21 * struct aead_givcrypt_request - AEAD request with IV generation 77 * struct aead_givcrypt_request - AEAD request with IV generation
22 * @seq: Sequence number for IV generation 78 * @seq: Sequence number for IV generation
23 * @giv: Space for generated IV 79 * @giv: Space for generated IV
@@ -30,6 +86,380 @@ struct aead_givcrypt_request {
30 struct aead_request areq; 86 struct aead_request areq;
31}; 87};
32 88
89struct crypto_aead {
90 int (*encrypt)(struct aead_request *req);
91 int (*decrypt)(struct aead_request *req);
92 int (*givencrypt)(struct aead_givcrypt_request *req);
93 int (*givdecrypt)(struct aead_givcrypt_request *req);
94
95 struct crypto_aead *child;
96
97 unsigned int ivsize;
98 unsigned int authsize;
99 unsigned int reqsize;
100
101 struct crypto_tfm base;
102};
103
104static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
105{
106 return container_of(tfm, struct crypto_aead, base);
107}
108
109/**
110 * crypto_alloc_aead() - allocate AEAD cipher handle
111 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
112 * AEAD cipher
113 * @type: specifies the type of the cipher
114 * @mask: specifies the mask for the cipher
115 *
116 * Allocate a cipher handle for an AEAD. The returned struct
117 * crypto_aead is the cipher handle that is required for any subsequent
118 * API invocation for that AEAD.
119 *
120 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
121 * of an error, PTR_ERR() returns the error code.
122 */
123struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
124
125static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
126{
127 return &tfm->base;
128}
129
130/**
131 * crypto_free_aead() - zeroize and free aead handle
132 * @tfm: cipher handle to be freed
133 */
134static inline void crypto_free_aead(struct crypto_aead *tfm)
135{
136 crypto_destroy_tfm(tfm, crypto_aead_tfm(tfm));
137}
138
139static inline struct crypto_aead *crypto_aead_crt(struct crypto_aead *tfm)
140{
141 return tfm;
142}
143
144/**
145 * crypto_aead_ivsize() - obtain IV size
146 * @tfm: cipher handle
147 *
148 * The size of the IV for the aead referenced by the cipher handle is
149 * returned. This IV size may be zero if the cipher does not need an IV.
150 *
151 * Return: IV size in bytes
152 */
153static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
154{
155 return tfm->ivsize;
156}
157
158/**
159 * crypto_aead_authsize() - obtain maximum authentication data size
160 * @tfm: cipher handle
161 *
162 * The maximum size of the authentication data for the AEAD cipher referenced
163 * by the AEAD cipher handle is returned. The authentication data size may be
164 * zero if the cipher implements a hard-coded maximum.
165 *
166 * The authentication data may also be known as "tag value".
167 *
168 * Return: authentication data size / tag size in bytes
169 */
170static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
171{
172 return tfm->authsize;
173}
174
175/**
176 * crypto_aead_blocksize() - obtain block size of cipher
177 * @tfm: cipher handle
178 *
179 * The block size for the AEAD referenced with the cipher handle is returned.
180 * The caller may use that information to allocate appropriate memory for the
181 * data returned by the encryption or decryption operation
182 *
183 * Return: block size of cipher
184 */
185static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
186{
187 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
188}
189
190static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
191{
192 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
193}
194
195static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
196{
197 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
198}
199
200static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
201{
202 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
203}
204
205static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
206{
207 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
208}
209
210/**
211 * crypto_aead_setkey() - set key for cipher
212 * @tfm: cipher handle
213 * @key: buffer holding the key
214 * @keylen: length of the key in bytes
215 *
216 * The caller provided key is set for the AEAD referenced by the cipher
217 * handle.
218 *
219 * Note, the key length determines the cipher type. Many block ciphers implement
220 * different cipher modes depending on the key size, such as AES-128 vs AES-192
221 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
222 * is performed.
223 *
224 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
225 */
226int crypto_aead_setkey(struct crypto_aead *tfm,
227 const u8 *key, unsigned int keylen);
228
229/**
230 * crypto_aead_setauthsize() - set authentication data size
231 * @tfm: cipher handle
232 * @authsize: size of the authentication data / tag in bytes
233 *
234 * Set the authentication data size / tag size. AEAD requires an authentication
235 * tag (or MAC) in addition to the associated data.
236 *
237 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
238 */
239int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
240
241static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
242{
243 return __crypto_aead_cast(req->base.tfm);
244}
245
246/**
247 * crypto_aead_encrypt() - encrypt plaintext
248 * @req: reference to the aead_request handle that holds all information
249 * needed to perform the cipher operation
250 *
251 * Encrypt plaintext data using the aead_request handle. That data structure
252 * and how it is filled with data is discussed with the aead_request_*
253 * functions.
254 *
255 * IMPORTANT NOTE The encryption operation creates the authentication data /
256 * tag. That data is concatenated with the created ciphertext.
257 * The ciphertext memory size is therefore the given number of
258 * block cipher blocks + the size defined by the
259 * crypto_aead_setauthsize invocation. The caller must ensure
260 * that sufficient memory is available for the ciphertext and
261 * the authentication tag.
262 *
263 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
264 */
265static inline int crypto_aead_encrypt(struct aead_request *req)
266{
267 return crypto_aead_reqtfm(req)->encrypt(req);
268}
269
270/**
271 * crypto_aead_decrypt() - decrypt ciphertext
272 * @req: reference to the ablkcipher_request handle that holds all information
273 * needed to perform the cipher operation
274 *
275 * Decrypt ciphertext data using the aead_request handle. That data structure
276 * and how it is filled with data is discussed with the aead_request_*
277 * functions.
278 *
279 * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the
280 * authentication data / tag. That authentication data / tag
281 * must have the size defined by the crypto_aead_setauthsize
282 * invocation.
283 *
284 *
285 * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD
286 * cipher operation performs the authentication of the data during the
287 * decryption operation. Therefore, the function returns this error if
288 * the authentication of the ciphertext was unsuccessful (i.e. the
289 * integrity of the ciphertext or the associated data was violated);
290 * < 0 if an error occurred.
291 */
292static inline int crypto_aead_decrypt(struct aead_request *req)
293{
294 if (req->cryptlen < crypto_aead_authsize(crypto_aead_reqtfm(req)))
295 return -EINVAL;
296
297 return crypto_aead_reqtfm(req)->decrypt(req);
298}
299
300/**
301 * DOC: Asynchronous AEAD Request Handle
302 *
303 * The aead_request data structure contains all pointers to data required for
304 * the AEAD cipher operation. This includes the cipher handle (which can be
305 * used by multiple aead_request instances), pointer to plaintext and
306 * ciphertext, asynchronous callback function, etc. It acts as a handle to the
307 * aead_request_* API calls in a similar way as AEAD handle to the
308 * crypto_aead_* API calls.
309 */
310
311/**
312 * crypto_aead_reqsize() - obtain size of the request data structure
313 * @tfm: cipher handle
314 *
315 * Return: number of bytes
316 */
317static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
318{
319 return tfm->reqsize;
320}
321
322/**
323 * aead_request_set_tfm() - update cipher handle reference in request
324 * @req: request handle to be modified
325 * @tfm: cipher handle that shall be added to the request handle
326 *
327 * Allow the caller to replace the existing aead handle in the request
328 * data structure with a different one.
329 */
330static inline void aead_request_set_tfm(struct aead_request *req,
331 struct crypto_aead *tfm)
332{
333 req->base.tfm = crypto_aead_tfm(tfm->child);
334}
335
336/**
337 * aead_request_alloc() - allocate request data structure
338 * @tfm: cipher handle to be registered with the request
339 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
340 *
341 * Allocate the request data structure that must be used with the AEAD
342 * encrypt and decrypt API calls. During the allocation, the provided aead
343 * handle is registered in the request data structure.
344 *
345 * Return: allocated request handle in case of success; IS_ERR() is true in case
346 * of an error, PTR_ERR() returns the error code.
347 */
348static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
349 gfp_t gfp)
350{
351 struct aead_request *req;
352
353 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
354
355 if (likely(req))
356 aead_request_set_tfm(req, tfm);
357
358 return req;
359}
360
361/**
362 * aead_request_free() - zeroize and free request data structure
363 * @req: request data structure cipher handle to be freed
364 */
365static inline void aead_request_free(struct aead_request *req)
366{
367 kzfree(req);
368}
369
370/**
371 * aead_request_set_callback() - set asynchronous callback function
372 * @req: request handle
373 * @flags: specify zero or an ORing of the flags
374 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
375 * increase the wait queue beyond the initial maximum size;
376 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
377 * @compl: callback function pointer to be registered with the request handle
378 * @data: The data pointer refers to memory that is not used by the kernel
379 * crypto API, but provided to the callback function for it to use. Here,
380 * the caller can provide a reference to memory the callback function can
381 * operate on. As the callback function is invoked asynchronously to the
382 * related functionality, it may need to access data structures of the
383 * related functionality which can be referenced using this pointer. The
384 * callback function can access the memory via the "data" field in the
385 * crypto_async_request data structure provided to the callback function.
386 *
387 * Setting the callback function that is triggered once the cipher operation
388 * completes
389 *
390 * The callback function is registered with the aead_request handle and
391 * must comply with the following template
392 *
393 * void callback_function(struct crypto_async_request *req, int error)
394 */
395static inline void aead_request_set_callback(struct aead_request *req,
396 u32 flags,
397 crypto_completion_t compl,
398 void *data)
399{
400 req->base.complete = compl;
401 req->base.data = data;
402 req->base.flags = flags;
403}
404
405/**
406 * aead_request_set_crypt - set data buffers
407 * @req: request handle
408 * @src: source scatter / gather list
409 * @dst: destination scatter / gather list
410 * @cryptlen: number of bytes to process from @src
411 * @iv: IV for the cipher operation which must comply with the IV size defined
412 * by crypto_aead_ivsize()
413 *
414 * Setting the source data and destination data scatter / gather lists.
415 *
416 * For encryption, the source is treated as the plaintext and the
417 * destination is the ciphertext. For a decryption operation, the use is
418 * reversed - the source is the ciphertext and the destination is the plaintext.
419 *
420 * IMPORTANT NOTE AEAD requires an authentication tag (MAC). For decryption,
421 * the caller must concatenate the ciphertext followed by the
422 * authentication tag and provide the entire data stream to the
423 * decryption operation (i.e. the data length used for the
424 * initialization of the scatterlist and the data length for the
425 * decryption operation is identical). For encryption, however,
426 * the authentication tag is created while encrypting the data.
427 * The destination buffer must hold sufficient space for the
428 * ciphertext and the authentication tag while the encryption
429 * invocation must only point to the plaintext data size. The
430 * following code snippet illustrates the memory usage
431 * buffer = kmalloc(ptbuflen + (enc ? authsize : 0));
432 * sg_init_one(&sg, buffer, ptbuflen + (enc ? authsize : 0));
433 * aead_request_set_crypt(req, &sg, &sg, ptbuflen, iv);
434 */
435static inline void aead_request_set_crypt(struct aead_request *req,
436 struct scatterlist *src,
437 struct scatterlist *dst,
438 unsigned int cryptlen, u8 *iv)
439{
440 req->src = src;
441 req->dst = dst;
442 req->cryptlen = cryptlen;
443 req->iv = iv;
444}
445
446/**
447 * aead_request_set_assoc() - set the associated data scatter / gather list
448 * @req: request handle
449 * @assoc: associated data scatter / gather list
450 * @assoclen: number of bytes to process from @assoc
451 *
452 * For encryption, the memory is filled with the associated data. For
453 * decryption, the memory must point to the associated data.
454 */
455static inline void aead_request_set_assoc(struct aead_request *req,
456 struct scatterlist *assoc,
457 unsigned int assoclen)
458{
459 req->assoc = assoc;
460 req->assoclen = assoclen;
461}
462
33static inline struct crypto_aead *aead_givcrypt_reqtfm( 463static inline struct crypto_aead *aead_givcrypt_reqtfm(
34 struct aead_givcrypt_request *req) 464 struct aead_givcrypt_request *req)
35{ 465{
@@ -38,14 +468,12 @@ static inline struct crypto_aead *aead_givcrypt_reqtfm(
38 468
39static inline int crypto_aead_givencrypt(struct aead_givcrypt_request *req) 469static inline int crypto_aead_givencrypt(struct aead_givcrypt_request *req)
40{ 470{
41 struct aead_tfm *crt = crypto_aead_crt(aead_givcrypt_reqtfm(req)); 471 return aead_givcrypt_reqtfm(req)->givencrypt(req);
42 return crt->givencrypt(req);
43}; 472};
44 473
45static inline int crypto_aead_givdecrypt(struct aead_givcrypt_request *req) 474static inline int crypto_aead_givdecrypt(struct aead_givcrypt_request *req)
46{ 475{
47 struct aead_tfm *crt = crypto_aead_crt(aead_givcrypt_reqtfm(req)); 476 return aead_givcrypt_reqtfm(req)->givdecrypt(req);
48 return crt->givdecrypt(req);
49}; 477};
50 478
51static inline void aead_givcrypt_set_tfm(struct aead_givcrypt_request *req, 479static inline void aead_givcrypt_set_tfm(struct aead_givcrypt_request *req,
diff --git a/include/crypto/algapi.h b/include/crypto/algapi.h
index a949bf70983b..d4ebf6e9af6a 100644
--- a/include/crypto/algapi.h
+++ b/include/crypto/algapi.h
@@ -17,6 +17,7 @@
17#include <linux/kernel.h> 17#include <linux/kernel.h>
18#include <linux/skbuff.h> 18#include <linux/skbuff.h>
19 19
20struct crypto_aead;
20struct module; 21struct module;
21struct rtattr; 22struct rtattr;
22struct seq_file; 23struct seq_file;
@@ -126,7 +127,6 @@ struct ablkcipher_walk {
126}; 127};
127 128
128extern const struct crypto_type crypto_ablkcipher_type; 129extern const struct crypto_type crypto_ablkcipher_type;
129extern const struct crypto_type crypto_aead_type;
130extern const struct crypto_type crypto_blkcipher_type; 130extern const struct crypto_type crypto_blkcipher_type;
131 131
132void crypto_mod_put(struct crypto_alg *alg); 132void crypto_mod_put(struct crypto_alg *alg);
@@ -241,22 +241,6 @@ static inline void *crypto_ablkcipher_ctx_aligned(struct crypto_ablkcipher *tfm)
241 return crypto_tfm_ctx_aligned(&tfm->base); 241 return crypto_tfm_ctx_aligned(&tfm->base);
242} 242}
243 243
244static inline struct aead_alg *crypto_aead_alg(struct crypto_aead *tfm)
245{
246 return &crypto_aead_tfm(tfm)->__crt_alg->cra_aead;
247}
248
249static inline void *crypto_aead_ctx(struct crypto_aead *tfm)
250{
251 return crypto_tfm_ctx(&tfm->base);
252}
253
254static inline struct crypto_instance *crypto_aead_alg_instance(
255 struct crypto_aead *aead)
256{
257 return crypto_tfm_alg_instance(&aead->base);
258}
259
260static inline struct crypto_blkcipher *crypto_spawn_blkcipher( 244static inline struct crypto_blkcipher *crypto_spawn_blkcipher(
261 struct crypto_spawn *spawn) 245 struct crypto_spawn *spawn)
262{ 246{
@@ -365,21 +349,6 @@ static inline int ablkcipher_tfm_in_queue(struct crypto_queue *queue,
365 return crypto_tfm_in_queue(queue, crypto_ablkcipher_tfm(tfm)); 349 return crypto_tfm_in_queue(queue, crypto_ablkcipher_tfm(tfm));
366} 350}
367 351
368static inline void *aead_request_ctx(struct aead_request *req)
369{
370 return req->__ctx;
371}
372
373static inline void aead_request_complete(struct aead_request *req, int err)
374{
375 req->base.complete(&req->base, err);
376}
377
378static inline u32 aead_request_flags(struct aead_request *req)
379{
380 return req->base.flags;
381}
382
383static inline struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb, 352static inline struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb,
384 u32 type, u32 mask) 353 u32 type, u32 mask)
385{ 354{
diff --git a/include/crypto/internal/aead.h b/include/crypto/internal/aead.h
index 750948cf4621..a2d104aa3430 100644
--- a/include/crypto/internal/aead.h
+++ b/include/crypto/internal/aead.h
@@ -23,8 +23,40 @@ struct crypto_aead_spawn {
23 struct crypto_spawn base; 23 struct crypto_spawn base;
24}; 24};
25 25
26extern const struct crypto_type crypto_aead_type;
26extern const struct crypto_type crypto_nivaead_type; 27extern const struct crypto_type crypto_nivaead_type;
27 28
29static inline struct aead_alg *crypto_aead_alg(struct crypto_aead *tfm)
30{
31 return &crypto_aead_tfm(tfm)->__crt_alg->cra_aead;
32}
33
34static inline void *crypto_aead_ctx(struct crypto_aead *tfm)
35{
36 return crypto_tfm_ctx(&tfm->base);
37}
38
39static inline struct crypto_instance *crypto_aead_alg_instance(
40 struct crypto_aead *aead)
41{
42 return crypto_tfm_alg_instance(&aead->base);
43}
44
45static inline void *aead_request_ctx(struct aead_request *req)
46{
47 return req->__ctx;
48}
49
50static inline void aead_request_complete(struct aead_request *req, int err)
51{
52 req->base.complete(&req->base, err);
53}
54
55static inline u32 aead_request_flags(struct aead_request *req)
56{
57 return req->base.flags;
58}
59
28static inline void crypto_set_aead_spawn( 60static inline void crypto_set_aead_spawn(
29 struct crypto_aead_spawn *spawn, struct crypto_instance *inst) 61 struct crypto_aead_spawn *spawn, struct crypto_instance *inst)
30{ 62{
@@ -50,9 +82,7 @@ static inline struct crypto_alg *crypto_aead_spawn_alg(
50static inline struct crypto_aead *crypto_spawn_aead( 82static inline struct crypto_aead *crypto_spawn_aead(
51 struct crypto_aead_spawn *spawn) 83 struct crypto_aead_spawn *spawn)
52{ 84{
53 return __crypto_aead_cast( 85 return crypto_spawn_tfm2(&spawn->base);
54 crypto_spawn_tfm(&spawn->base, CRYPTO_ALG_TYPE_AEAD,
55 CRYPTO_ALG_TYPE_MASK));
56} 86}
57 87
58struct crypto_instance *aead_geniv_alloc(struct crypto_template *tmpl, 88struct crypto_instance *aead_geniv_alloc(struct crypto_template *tmpl,
@@ -64,7 +94,7 @@ void aead_geniv_exit(struct crypto_tfm *tfm);
64 94
65static inline struct crypto_aead *aead_geniv_base(struct crypto_aead *geniv) 95static inline struct crypto_aead *aead_geniv_base(struct crypto_aead *geniv)
66{ 96{
67 return crypto_aead_crt(geniv)->base; 97 return geniv->child;
68} 98}
69 99
70static inline void *aead_givcrypt_reqctx(struct aead_givcrypt_request *req) 100static inline void *aead_givcrypt_reqctx(struct aead_givcrypt_request *req)
diff --git a/include/linux/crypto.h b/include/linux/crypto.h
index ee14140f8893..59ca4086ce6a 100644
--- a/include/linux/crypto.h
+++ b/include/linux/crypto.h
@@ -140,6 +140,7 @@ struct crypto_blkcipher;
140struct crypto_hash; 140struct crypto_hash;
141struct crypto_tfm; 141struct crypto_tfm;
142struct crypto_type; 142struct crypto_type;
143struct aead_request;
143struct aead_givcrypt_request; 144struct aead_givcrypt_request;
144struct skcipher_givcrypt_request; 145struct skcipher_givcrypt_request;
145 146
@@ -174,32 +175,6 @@ struct ablkcipher_request {
174 void *__ctx[] CRYPTO_MINALIGN_ATTR; 175 void *__ctx[] CRYPTO_MINALIGN_ATTR;
175}; 176};
176 177
177/**
178 * struct aead_request - AEAD request
179 * @base: Common attributes for async crypto requests
180 * @assoclen: Length in bytes of associated data for authentication
181 * @cryptlen: Length of data to be encrypted or decrypted
182 * @iv: Initialisation vector
183 * @assoc: Associated data
184 * @src: Source data
185 * @dst: Destination data
186 * @__ctx: Start of private context data
187 */
188struct aead_request {
189 struct crypto_async_request base;
190
191 unsigned int assoclen;
192 unsigned int cryptlen;
193
194 u8 *iv;
195
196 struct scatterlist *assoc;
197 struct scatterlist *src;
198 struct scatterlist *dst;
199
200 void *__ctx[] CRYPTO_MINALIGN_ATTR;
201};
202
203struct blkcipher_desc { 178struct blkcipher_desc {
204 struct crypto_blkcipher *tfm; 179 struct crypto_blkcipher *tfm;
205 void *info; 180 void *info;
@@ -572,21 +547,6 @@ struct ablkcipher_tfm {
572 unsigned int reqsize; 547 unsigned int reqsize;
573}; 548};
574 549
575struct aead_tfm {
576 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
577 unsigned int keylen);
578 int (*encrypt)(struct aead_request *req);
579 int (*decrypt)(struct aead_request *req);
580 int (*givencrypt)(struct aead_givcrypt_request *req);
581 int (*givdecrypt)(struct aead_givcrypt_request *req);
582
583 struct crypto_aead *base;
584
585 unsigned int ivsize;
586 unsigned int authsize;
587 unsigned int reqsize;
588};
589
590struct blkcipher_tfm { 550struct blkcipher_tfm {
591 void *iv; 551 void *iv;
592 int (*setkey)(struct crypto_tfm *tfm, const u8 *key, 552 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
@@ -626,7 +586,6 @@ struct compress_tfm {
626}; 586};
627 587
628#define crt_ablkcipher crt_u.ablkcipher 588#define crt_ablkcipher crt_u.ablkcipher
629#define crt_aead crt_u.aead
630#define crt_blkcipher crt_u.blkcipher 589#define crt_blkcipher crt_u.blkcipher
631#define crt_cipher crt_u.cipher 590#define crt_cipher crt_u.cipher
632#define crt_hash crt_u.hash 591#define crt_hash crt_u.hash
@@ -638,7 +597,6 @@ struct crypto_tfm {
638 597
639 union { 598 union {
640 struct ablkcipher_tfm ablkcipher; 599 struct ablkcipher_tfm ablkcipher;
641 struct aead_tfm aead;
642 struct blkcipher_tfm blkcipher; 600 struct blkcipher_tfm blkcipher;
643 struct cipher_tfm cipher; 601 struct cipher_tfm cipher;
644 struct hash_tfm hash; 602 struct hash_tfm hash;
@@ -656,10 +614,6 @@ struct crypto_ablkcipher {
656 struct crypto_tfm base; 614 struct crypto_tfm base;
657}; 615};
658 616
659struct crypto_aead {
660 struct crypto_tfm base;
661};
662
663struct crypto_blkcipher { 617struct crypto_blkcipher {
664 struct crypto_tfm base; 618 struct crypto_tfm base;
665}; 619};
@@ -1152,400 +1106,6 @@ static inline void ablkcipher_request_set_crypt(
1152} 1106}
1153 1107
1154/** 1108/**
1155 * DOC: Authenticated Encryption With Associated Data (AEAD) Cipher API
1156 *
1157 * The AEAD cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_AEAD
1158 * (listed as type "aead" in /proc/crypto)
1159 *
1160 * The most prominent examples for this type of encryption is GCM and CCM.
1161 * However, the kernel supports other types of AEAD ciphers which are defined
1162 * with the following cipher string:
1163 *
1164 * authenc(keyed message digest, block cipher)
1165 *
1166 * For example: authenc(hmac(sha256), cbc(aes))
1167 *
1168 * The example code provided for the asynchronous block cipher operation
1169 * applies here as well. Naturally all *ablkcipher* symbols must be exchanged
1170 * the *aead* pendants discussed in the following. In addtion, for the AEAD
1171 * operation, the aead_request_set_assoc function must be used to set the
1172 * pointer to the associated data memory location before performing the
1173 * encryption or decryption operation. In case of an encryption, the associated
1174 * data memory is filled during the encryption operation. For decryption, the
1175 * associated data memory must contain data that is used to verify the integrity
1176 * of the decrypted data. Another deviation from the asynchronous block cipher
1177 * operation is that the caller should explicitly check for -EBADMSG of the
1178 * crypto_aead_decrypt. That error indicates an authentication error, i.e.
1179 * a breach in the integrity of the message. In essence, that -EBADMSG error
1180 * code is the key bonus an AEAD cipher has over "standard" block chaining
1181 * modes.
1182 */
1183
1184static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
1185{
1186 return (struct crypto_aead *)tfm;
1187}
1188
1189/**
1190 * crypto_alloc_aead() - allocate AEAD cipher handle
1191 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1192 * AEAD cipher
1193 * @type: specifies the type of the cipher
1194 * @mask: specifies the mask for the cipher
1195 *
1196 * Allocate a cipher handle for an AEAD. The returned struct
1197 * crypto_aead is the cipher handle that is required for any subsequent
1198 * API invocation for that AEAD.
1199 *
1200 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1201 * of an error, PTR_ERR() returns the error code.
1202 */
1203struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
1204
1205static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
1206{
1207 return &tfm->base;
1208}
1209
1210/**
1211 * crypto_free_aead() - zeroize and free aead handle
1212 * @tfm: cipher handle to be freed
1213 */
1214static inline void crypto_free_aead(struct crypto_aead *tfm)
1215{
1216 crypto_free_tfm(crypto_aead_tfm(tfm));
1217}
1218
1219static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
1220{
1221 return &crypto_aead_tfm(tfm)->crt_aead;
1222}
1223
1224/**
1225 * crypto_aead_ivsize() - obtain IV size
1226 * @tfm: cipher handle
1227 *
1228 * The size of the IV for the aead referenced by the cipher handle is
1229 * returned. This IV size may be zero if the cipher does not need an IV.
1230 *
1231 * Return: IV size in bytes
1232 */
1233static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
1234{
1235 return crypto_aead_crt(tfm)->ivsize;
1236}
1237
1238/**
1239 * crypto_aead_authsize() - obtain maximum authentication data size
1240 * @tfm: cipher handle
1241 *
1242 * The maximum size of the authentication data for the AEAD cipher referenced
1243 * by the AEAD cipher handle is returned. The authentication data size may be
1244 * zero if the cipher implements a hard-coded maximum.
1245 *
1246 * The authentication data may also be known as "tag value".
1247 *
1248 * Return: authentication data size / tag size in bytes
1249 */
1250static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
1251{
1252 return crypto_aead_crt(tfm)->authsize;
1253}
1254
1255/**
1256 * crypto_aead_blocksize() - obtain block size of cipher
1257 * @tfm: cipher handle
1258 *
1259 * The block size for the AEAD referenced with the cipher handle is returned.
1260 * The caller may use that information to allocate appropriate memory for the
1261 * data returned by the encryption or decryption operation
1262 *
1263 * Return: block size of cipher
1264 */
1265static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
1266{
1267 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
1268}
1269
1270static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
1271{
1272 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
1273}
1274
1275static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
1276{
1277 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
1278}
1279
1280static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
1281{
1282 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
1283}
1284
1285static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
1286{
1287 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
1288}
1289
1290/**
1291 * crypto_aead_setkey() - set key for cipher
1292 * @tfm: cipher handle
1293 * @key: buffer holding the key
1294 * @keylen: length of the key in bytes
1295 *
1296 * The caller provided key is set for the AEAD referenced by the cipher
1297 * handle.
1298 *
1299 * Note, the key length determines the cipher type. Many block ciphers implement
1300 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1301 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1302 * is performed.
1303 *
1304 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1305 */
1306static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
1307 unsigned int keylen)
1308{
1309 struct aead_tfm *crt = crypto_aead_crt(tfm);
1310
1311 return crt->setkey(crt->base, key, keylen);
1312}
1313
1314/**
1315 * crypto_aead_setauthsize() - set authentication data size
1316 * @tfm: cipher handle
1317 * @authsize: size of the authentication data / tag in bytes
1318 *
1319 * Set the authentication data size / tag size. AEAD requires an authentication
1320 * tag (or MAC) in addition to the associated data.
1321 *
1322 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1323 */
1324int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
1325
1326static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
1327{
1328 return __crypto_aead_cast(req->base.tfm);
1329}
1330
1331/**
1332 * crypto_aead_encrypt() - encrypt plaintext
1333 * @req: reference to the aead_request handle that holds all information
1334 * needed to perform the cipher operation
1335 *
1336 * Encrypt plaintext data using the aead_request handle. That data structure
1337 * and how it is filled with data is discussed with the aead_request_*
1338 * functions.
1339 *
1340 * IMPORTANT NOTE The encryption operation creates the authentication data /
1341 * tag. That data is concatenated with the created ciphertext.
1342 * The ciphertext memory size is therefore the given number of
1343 * block cipher blocks + the size defined by the
1344 * crypto_aead_setauthsize invocation. The caller must ensure
1345 * that sufficient memory is available for the ciphertext and
1346 * the authentication tag.
1347 *
1348 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1349 */
1350static inline int crypto_aead_encrypt(struct aead_request *req)
1351{
1352 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
1353}
1354
1355/**
1356 * crypto_aead_decrypt() - decrypt ciphertext
1357 * @req: reference to the ablkcipher_request handle that holds all information
1358 * needed to perform the cipher operation
1359 *
1360 * Decrypt ciphertext data using the aead_request handle. That data structure
1361 * and how it is filled with data is discussed with the aead_request_*
1362 * functions.
1363 *
1364 * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the
1365 * authentication data / tag. That authentication data / tag
1366 * must have the size defined by the crypto_aead_setauthsize
1367 * invocation.
1368 *
1369 *
1370 * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD
1371 * cipher operation performs the authentication of the data during the
1372 * decryption operation. Therefore, the function returns this error if
1373 * the authentication of the ciphertext was unsuccessful (i.e. the
1374 * integrity of the ciphertext or the associated data was violated);
1375 * < 0 if an error occurred.
1376 */
1377static inline int crypto_aead_decrypt(struct aead_request *req)
1378{
1379 if (req->cryptlen < crypto_aead_authsize(crypto_aead_reqtfm(req)))
1380 return -EINVAL;
1381
1382 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
1383}
1384
1385/**
1386 * DOC: Asynchronous AEAD Request Handle
1387 *
1388 * The aead_request data structure contains all pointers to data required for
1389 * the AEAD cipher operation. This includes the cipher handle (which can be
1390 * used by multiple aead_request instances), pointer to plaintext and
1391 * ciphertext, asynchronous callback function, etc. It acts as a handle to the
1392 * aead_request_* API calls in a similar way as AEAD handle to the
1393 * crypto_aead_* API calls.
1394 */
1395
1396/**
1397 * crypto_aead_reqsize() - obtain size of the request data structure
1398 * @tfm: cipher handle
1399 *
1400 * Return: number of bytes
1401 */
1402static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
1403{
1404 return crypto_aead_crt(tfm)->reqsize;
1405}
1406
1407/**
1408 * aead_request_set_tfm() - update cipher handle reference in request
1409 * @req: request handle to be modified
1410 * @tfm: cipher handle that shall be added to the request handle
1411 *
1412 * Allow the caller to replace the existing aead handle in the request
1413 * data structure with a different one.
1414 */
1415static inline void aead_request_set_tfm(struct aead_request *req,
1416 struct crypto_aead *tfm)
1417{
1418 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
1419}
1420
1421/**
1422 * aead_request_alloc() - allocate request data structure
1423 * @tfm: cipher handle to be registered with the request
1424 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1425 *
1426 * Allocate the request data structure that must be used with the AEAD
1427 * encrypt and decrypt API calls. During the allocation, the provided aead
1428 * handle is registered in the request data structure.
1429 *
1430 * Return: allocated request handle in case of success; IS_ERR() is true in case
1431 * of an error, PTR_ERR() returns the error code.
1432 */
1433static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
1434 gfp_t gfp)
1435{
1436 struct aead_request *req;
1437
1438 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
1439
1440 if (likely(req))
1441 aead_request_set_tfm(req, tfm);
1442
1443 return req;
1444}
1445
1446/**
1447 * aead_request_free() - zeroize and free request data structure
1448 * @req: request data structure cipher handle to be freed
1449 */
1450static inline void aead_request_free(struct aead_request *req)
1451{
1452 kzfree(req);
1453}
1454
1455/**
1456 * aead_request_set_callback() - set asynchronous callback function
1457 * @req: request handle
1458 * @flags: specify zero or an ORing of the flags
1459 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1460 * increase the wait queue beyond the initial maximum size;
1461 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1462 * @compl: callback function pointer to be registered with the request handle
1463 * @data: The data pointer refers to memory that is not used by the kernel
1464 * crypto API, but provided to the callback function for it to use. Here,
1465 * the caller can provide a reference to memory the callback function can
1466 * operate on. As the callback function is invoked asynchronously to the
1467 * related functionality, it may need to access data structures of the
1468 * related functionality which can be referenced using this pointer. The
1469 * callback function can access the memory via the "data" field in the
1470 * crypto_async_request data structure provided to the callback function.
1471 *
1472 * Setting the callback function that is triggered once the cipher operation
1473 * completes
1474 *
1475 * The callback function is registered with the aead_request handle and
1476 * must comply with the following template
1477 *
1478 * void callback_function(struct crypto_async_request *req, int error)
1479 */
1480static inline void aead_request_set_callback(struct aead_request *req,
1481 u32 flags,
1482 crypto_completion_t compl,
1483 void *data)
1484{
1485 req->base.complete = compl;
1486 req->base.data = data;
1487 req->base.flags = flags;
1488}
1489
1490/**
1491 * aead_request_set_crypt - set data buffers
1492 * @req: request handle
1493 * @src: source scatter / gather list
1494 * @dst: destination scatter / gather list
1495 * @cryptlen: number of bytes to process from @src
1496 * @iv: IV for the cipher operation which must comply with the IV size defined
1497 * by crypto_aead_ivsize()
1498 *
1499 * Setting the source data and destination data scatter / gather lists.
1500 *
1501 * For encryption, the source is treated as the plaintext and the
1502 * destination is the ciphertext. For a decryption operation, the use is
1503 * reversed - the source is the ciphertext and the destination is the plaintext.
1504 *
1505 * IMPORTANT NOTE AEAD requires an authentication tag (MAC). For decryption,
1506 * the caller must concatenate the ciphertext followed by the
1507 * authentication tag and provide the entire data stream to the
1508 * decryption operation (i.e. the data length used for the
1509 * initialization of the scatterlist and the data length for the
1510 * decryption operation is identical). For encryption, however,
1511 * the authentication tag is created while encrypting the data.
1512 * The destination buffer must hold sufficient space for the
1513 * ciphertext and the authentication tag while the encryption
1514 * invocation must only point to the plaintext data size. The
1515 * following code snippet illustrates the memory usage
1516 * buffer = kmalloc(ptbuflen + (enc ? authsize : 0));
1517 * sg_init_one(&sg, buffer, ptbuflen + (enc ? authsize : 0));
1518 * aead_request_set_crypt(req, &sg, &sg, ptbuflen, iv);
1519 */
1520static inline void aead_request_set_crypt(struct aead_request *req,
1521 struct scatterlist *src,
1522 struct scatterlist *dst,
1523 unsigned int cryptlen, u8 *iv)
1524{
1525 req->src = src;
1526 req->dst = dst;
1527 req->cryptlen = cryptlen;
1528 req->iv = iv;
1529}
1530
1531/**
1532 * aead_request_set_assoc() - set the associated data scatter / gather list
1533 * @req: request handle
1534 * @assoc: associated data scatter / gather list
1535 * @assoclen: number of bytes to process from @assoc
1536 *
1537 * For encryption, the memory is filled with the associated data. For
1538 * decryption, the memory must point to the associated data.
1539 */
1540static inline void aead_request_set_assoc(struct aead_request *req,
1541 struct scatterlist *assoc,
1542 unsigned int assoclen)
1543{
1544 req->assoc = assoc;
1545 req->assoclen = assoclen;
1546}
1547
1548/**
1549 * DOC: Synchronous Block Cipher API 1109 * DOC: Synchronous Block Cipher API
1550 * 1110 *
1551 * The synchronous block cipher API is used with the ciphers of type 1111 * The synchronous block cipher API is used with the ciphers of type