xref: /linux/include/crypto/internal/aead.h (revision b85d45947951d23cb22d90caecf4c1eb81342c96)
1 /*
2  * AEAD: Authenticated Encryption with Associated Data
3  *
4  * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 2 of the License, or (at your option)
9  * any later version.
10  *
11  */
12 
13 #ifndef _CRYPTO_INTERNAL_AEAD_H
14 #define _CRYPTO_INTERNAL_AEAD_H
15 
16 #include <crypto/aead.h>
17 #include <crypto/algapi.h>
18 #include <linux/stddef.h>
19 #include <linux/types.h>
20 
21 struct rtattr;
22 
23 struct aead_instance {
24 	void (*free)(struct aead_instance *inst);
25 	union {
26 		struct {
27 			char head[offsetof(struct aead_alg, base)];
28 			struct crypto_instance base;
29 		} s;
30 		struct aead_alg alg;
31 	};
32 };
33 
34 struct crypto_aead_spawn {
35 	struct crypto_spawn base;
36 };
37 
38 struct aead_queue {
39 	struct crypto_queue base;
40 };
41 
42 static inline void *crypto_aead_ctx(struct crypto_aead *tfm)
43 {
44 	return crypto_tfm_ctx(&tfm->base);
45 }
46 
47 static inline struct crypto_instance *aead_crypto_instance(
48 	struct aead_instance *inst)
49 {
50 	return container_of(&inst->alg.base, struct crypto_instance, alg);
51 }
52 
53 static inline struct aead_instance *aead_instance(struct crypto_instance *inst)
54 {
55 	return container_of(&inst->alg, struct aead_instance, alg.base);
56 }
57 
58 static inline struct aead_instance *aead_alg_instance(struct crypto_aead *aead)
59 {
60 	return aead_instance(crypto_tfm_alg_instance(&aead->base));
61 }
62 
63 static inline void *aead_instance_ctx(struct aead_instance *inst)
64 {
65 	return crypto_instance_ctx(aead_crypto_instance(inst));
66 }
67 
68 static inline void *aead_request_ctx(struct aead_request *req)
69 {
70 	return req->__ctx;
71 }
72 
73 static inline void aead_request_complete(struct aead_request *req, int err)
74 {
75 	req->base.complete(&req->base, err);
76 }
77 
78 static inline u32 aead_request_flags(struct aead_request *req)
79 {
80 	return req->base.flags;
81 }
82 
83 static inline void crypto_set_aead_spawn(
84 	struct crypto_aead_spawn *spawn, struct crypto_instance *inst)
85 {
86 	crypto_set_spawn(&spawn->base, inst);
87 }
88 
89 int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name,
90 		     u32 type, u32 mask);
91 
92 static inline void crypto_drop_aead(struct crypto_aead_spawn *spawn)
93 {
94 	crypto_drop_spawn(&spawn->base);
95 }
96 
97 static inline struct aead_alg *crypto_spawn_aead_alg(
98 	struct crypto_aead_spawn *spawn)
99 {
100 	return container_of(spawn->base.alg, struct aead_alg, base);
101 }
102 
103 static inline struct crypto_aead *crypto_spawn_aead(
104 	struct crypto_aead_spawn *spawn)
105 {
106 	return crypto_spawn_tfm2(&spawn->base);
107 }
108 
109 static inline void crypto_aead_set_reqsize(struct crypto_aead *aead,
110 					   unsigned int reqsize)
111 {
112 	aead->reqsize = reqsize;
113 }
114 
115 static inline unsigned int crypto_aead_alg_maxauthsize(struct aead_alg *alg)
116 {
117 	return alg->maxauthsize;
118 }
119 
120 static inline unsigned int crypto_aead_maxauthsize(struct crypto_aead *aead)
121 {
122 	return crypto_aead_alg_maxauthsize(crypto_aead_alg(aead));
123 }
124 
125 static inline void aead_init_queue(struct aead_queue *queue,
126 				   unsigned int max_qlen)
127 {
128 	crypto_init_queue(&queue->base, max_qlen);
129 }
130 
131 static inline int aead_enqueue_request(struct aead_queue *queue,
132 				       struct aead_request *request)
133 {
134 	return crypto_enqueue_request(&queue->base, &request->base);
135 }
136 
137 static inline struct aead_request *aead_dequeue_request(
138 	struct aead_queue *queue)
139 {
140 	struct crypto_async_request *req;
141 
142 	req = crypto_dequeue_request(&queue->base);
143 
144 	return req ? container_of(req, struct aead_request, base) : NULL;
145 }
146 
147 static inline struct aead_request *aead_get_backlog(struct aead_queue *queue)
148 {
149 	struct crypto_async_request *req;
150 
151 	req = crypto_get_backlog(&queue->base);
152 
153 	return req ? container_of(req, struct aead_request, base) : NULL;
154 }
155 
156 int crypto_register_aead(struct aead_alg *alg);
157 void crypto_unregister_aead(struct aead_alg *alg);
158 int crypto_register_aeads(struct aead_alg *algs, int count);
159 void crypto_unregister_aeads(struct aead_alg *algs, int count);
160 int aead_register_instance(struct crypto_template *tmpl,
161 			   struct aead_instance *inst);
162 
163 #endif	/* _CRYPTO_INTERNAL_AEAD_H */
164 
165