1/*
2 * Symmetric key ciphers.
3 *
4 * Copyright (c) 2007 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_SKCIPHER_H
14#define _CRYPTO_INTERNAL_SKCIPHER_H
15
16#include <crypto/algapi.h>
17#include <crypto/skcipher.h>
18#include <linux/list.h>
19#include <linux/types.h>
20
21struct aead_request;
22struct rtattr;
23
24struct skcipher_instance {
25 void (*free)(struct skcipher_instance *inst);
26 union {
27 struct {
28 char head[offsetof(struct skcipher_alg, base)];
29 struct crypto_instance base;
30 } s;
31 struct skcipher_alg alg;
32 };
33};
34
35struct crypto_skcipher_spawn {
36 struct crypto_spawn base;
37};
38
39struct skcipher_walk {
40 union {
41 struct {
42 struct page *page;
43 unsigned long offset;
44 } phys;
45
46 struct {
47 u8 *page;
48 void *addr;
49 } virt;
50 } src, dst;
51
52 struct scatter_walk in;
53 unsigned int nbytes;
54
55 struct scatter_walk out;
56 unsigned int total;
57
58 struct list_head buffers;
59
60 u8 *page;
61 u8 *buffer;
62 u8 *oiv;
63 void *iv;
64
65 unsigned int ivsize;
66
67 int flags;
68 unsigned int blocksize;
69 unsigned int stride;
70 unsigned int alignmask;
71};
72
73static inline struct crypto_instance *skcipher_crypto_instance(
74 struct skcipher_instance *inst)
75{
76 return &inst->s.base;
77}
78
79static inline struct skcipher_instance *skcipher_alg_instance(
80 struct crypto_skcipher *skcipher)
81{
82 return container_of(crypto_skcipher_alg(skcipher),
83 struct skcipher_instance, alg);
84}
85
86static inline void *skcipher_instance_ctx(struct skcipher_instance *inst)
87{
88 return crypto_instance_ctx(skcipher_crypto_instance(inst));
89}
90
91static inline void skcipher_request_complete(struct skcipher_request *req, int err)
92{
93 req->base.complete(&req->base, err);
94}
95
96static inline void crypto_set_skcipher_spawn(
97 struct crypto_skcipher_spawn *spawn, struct crypto_instance *inst)
98{
99 crypto_set_spawn(&spawn->base, inst);
100}
101
102int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
103 u32 type, u32 mask);
104
105static inline void crypto_drop_skcipher(struct crypto_skcipher_spawn *spawn)
106{
107 crypto_drop_spawn(&spawn->base);
108}
109
110static inline struct skcipher_alg *crypto_skcipher_spawn_alg(
111 struct crypto_skcipher_spawn *spawn)
112{
113 return container_of(spawn->base.alg, struct skcipher_alg, base);
114}
115
116static inline struct skcipher_alg *crypto_spawn_skcipher_alg(
117 struct crypto_skcipher_spawn *spawn)
118{
119 return crypto_skcipher_spawn_alg(spawn);
120}
121
122static inline struct crypto_skcipher *crypto_spawn_skcipher(
123 struct crypto_skcipher_spawn *spawn)
124{
125 return crypto_spawn_tfm2(&spawn->base);
126}
127
128static inline void crypto_skcipher_set_reqsize(
129 struct crypto_skcipher *skcipher, unsigned int reqsize)
130{
131 skcipher->reqsize = reqsize;
132}
133
134int crypto_register_skcipher(struct skcipher_alg *alg);
135void crypto_unregister_skcipher(struct skcipher_alg *alg);
136int crypto_register_skciphers(struct skcipher_alg *algs, int count);
137void crypto_unregister_skciphers(struct skcipher_alg *algs, int count);
138int skcipher_register_instance(struct crypto_template *tmpl,
139 struct skcipher_instance *inst);
140
141int skcipher_walk_done(struct skcipher_walk *walk, int err);
142int skcipher_walk_virt(struct skcipher_walk *walk,
143 struct skcipher_request *req,
144 bool atomic);
145void skcipher_walk_atomise(struct skcipher_walk *walk);
146int skcipher_walk_async(struct skcipher_walk *walk,
147 struct skcipher_request *req);
148int skcipher_walk_aead(struct skcipher_walk *walk, struct aead_request *req,
149 bool atomic);
150int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,
151 struct aead_request *req, bool atomic);
152int skcipher_walk_aead_decrypt(struct skcipher_walk *walk,
153 struct aead_request *req, bool atomic);
154void skcipher_walk_complete(struct skcipher_walk *walk, int err);
155
156static inline void ablkcipher_request_complete(struct ablkcipher_request *req,
157 int err)
158{
159 req->base.complete(&req->base, err);
160}
161
162static inline u32 ablkcipher_request_flags(struct ablkcipher_request *req)
163{
164 return req->base.flags;
165}
166
167static inline void *crypto_skcipher_ctx(struct crypto_skcipher *tfm)
168{
169 return crypto_tfm_ctx(&tfm->base);
170}
171
172static inline void *skcipher_request_ctx(struct skcipher_request *req)
173{
174 return req->__ctx;
175}
176
177static inline u32 skcipher_request_flags(struct skcipher_request *req)
178{
179 return req->base.flags;
180}
181
182static inline unsigned int crypto_skcipher_alg_min_keysize(
183 struct skcipher_alg *alg)
184{
185 if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) ==
186 CRYPTO_ALG_TYPE_BLKCIPHER)
187 return alg->base.cra_blkcipher.min_keysize;
188
189 if (alg->base.cra_ablkcipher.encrypt)
190 return alg->base.cra_ablkcipher.min_keysize;
191
192 return alg->min_keysize;
193}
194
195static inline unsigned int crypto_skcipher_alg_max_keysize(
196 struct skcipher_alg *alg)
197{
198 if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) ==
199 CRYPTO_ALG_TYPE_BLKCIPHER)
200 return alg->base.cra_blkcipher.max_keysize;
201
202 if (alg->base.cra_ablkcipher.encrypt)
203 return alg->base.cra_ablkcipher.max_keysize;
204
205 return alg->max_keysize;
206}
207
208/* Helpers for simple block cipher modes of operation */
209struct skcipher_ctx_simple {
210 struct crypto_cipher *cipher; /* underlying block cipher */
211};
212static inline struct crypto_cipher *
213skcipher_cipher_simple(struct crypto_skcipher *tfm)
214{
215 struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
216
217 return ctx->cipher;
218}
219struct skcipher_instance *
220skcipher_alloc_instance_simple(struct crypto_template *tmpl, struct rtattr **tb,
221 struct crypto_alg **cipher_alg_ret);
222
223#endif /* _CRYPTO_INTERNAL_SKCIPHER_H */
224
225