[CRYPTO] authenc: Fix typo in ivsize
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / crypto / authenc.c
CommitLineData
3c09f17c
HX
1/*
2 * Authenc: Simple AEAD wrapper for IPsec
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#include <crypto/algapi.h>
e236d4a8 14#include <crypto/authenc.h>
42c271c6 15#include <crypto/scatterwalk.h>
3c09f17c
HX
16#include <linux/err.h>
17#include <linux/init.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
e236d4a8 20#include <linux/rtnetlink.h>
3c09f17c
HX
21#include <linux/slab.h>
22#include <linux/spinlock.h>
23
3c09f17c
HX
24struct authenc_instance_ctx {
25 struct crypto_spawn auth;
26 struct crypto_spawn enc;
3c09f17c
HX
27};
28
29struct crypto_authenc_ctx {
30 spinlock_t auth_lock;
31 struct crypto_hash *auth;
32 struct crypto_ablkcipher *enc;
33};
34
35static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
36 unsigned int keylen)
37{
3c09f17c 38 unsigned int authkeylen;
e236d4a8 39 unsigned int enckeylen;
3c09f17c
HX
40 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
41 struct crypto_hash *auth = ctx->auth;
42 struct crypto_ablkcipher *enc = ctx->enc;
e236d4a8
HX
43 struct rtattr *rta = (void *)key;
44 struct crypto_authenc_key_param *param;
3c09f17c
HX
45 int err = -EINVAL;
46
e236d4a8
HX
47 if (keylen < sizeof(*rta))
48 goto badkey;
49 if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
50 goto badkey;
51 if (RTA_PAYLOAD(rta) < sizeof(*param))
52 goto badkey;
53
54 param = RTA_DATA(rta);
55 enckeylen = be32_to_cpu(param->enckeylen);
56
57 key += RTA_ALIGN(rta->rta_len);
58 keylen -= RTA_ALIGN(rta->rta_len);
59
60 if (keylen < enckeylen)
61 goto badkey;
62
3c09f17c
HX
63 authkeylen = keylen - enckeylen;
64
65 crypto_hash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
66 crypto_hash_set_flags(auth, crypto_aead_get_flags(authenc) &
67 CRYPTO_TFM_REQ_MASK);
68 err = crypto_hash_setkey(auth, key, authkeylen);
69 crypto_aead_set_flags(authenc, crypto_hash_get_flags(auth) &
70 CRYPTO_TFM_RES_MASK);
71
72 if (err)
73 goto out;
74
75 crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
76 crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
77 CRYPTO_TFM_REQ_MASK);
78 err = crypto_ablkcipher_setkey(enc, key + authkeylen, enckeylen);
79 crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) &
80 CRYPTO_TFM_RES_MASK);
81
82out:
83 return err;
e236d4a8
HX
84
85badkey:
86 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
87 goto out;
3c09f17c
HX
88}
89
90static int crypto_authenc_hash(struct aead_request *req)
91{
92 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
3c09f17c
HX
93 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
94 struct crypto_hash *auth = ctx->auth;
95 struct hash_desc desc = {
96 .tfm = auth,
97 };
98 u8 *hash = aead_request_ctx(req);
f347c4fa
HX
99 struct scatterlist *dst = req->dst;
100 unsigned int cryptlen = req->cryptlen;
3c09f17c
HX
101 int err;
102
103 hash = (u8 *)ALIGN((unsigned long)hash + crypto_hash_alignmask(auth),
104 crypto_hash_alignmask(auth) + 1);
105
106 spin_lock_bh(&ctx->auth_lock);
107 err = crypto_hash_init(&desc);
108 if (err)
109 goto auth_unlock;
110
111 err = crypto_hash_update(&desc, req->assoc, req->assoclen);
112 if (err)
113 goto auth_unlock;
114
3c09f17c
HX
115 err = crypto_hash_update(&desc, dst, cryptlen);
116 if (err)
117 goto auth_unlock;
118
119 err = crypto_hash_final(&desc, hash);
120auth_unlock:
121 spin_unlock_bh(&ctx->auth_lock);
122
123 if (err)
124 return err;
125
7ba683a6
HX
126 scatterwalk_map_and_copy(hash, dst, cryptlen,
127 crypto_aead_authsize(authenc), 1);
3c09f17c
HX
128 return 0;
129}
130
131static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
132 int err)
133{
134 if (!err)
135 err = crypto_authenc_hash(req->data);
136
137 aead_request_complete(req->data, err);
138}
139
140static int crypto_authenc_encrypt(struct aead_request *req)
141{
142 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
143 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
144 struct ablkcipher_request *abreq = aead_request_ctx(req);
145 int err;
146
147 ablkcipher_request_set_tfm(abreq, ctx->enc);
148 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
149 crypto_authenc_encrypt_done, req);
150 ablkcipher_request_set_crypt(abreq, req->src, req->dst, req->cryptlen,
151 req->iv);
152
153 err = crypto_ablkcipher_encrypt(abreq);
154 if (err)
155 return err;
156
157 return crypto_authenc_hash(req);
158}
159
481f34ae
HX
160static int crypto_authenc_verify(struct aead_request *req,
161 unsigned int cryptlen)
3c09f17c
HX
162{
163 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
3c09f17c
HX
164 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
165 struct crypto_hash *auth = ctx->auth;
166 struct hash_desc desc = {
167 .tfm = auth,
168 .flags = aead_request_flags(req),
169 };
170 u8 *ohash = aead_request_ctx(req);
171 u8 *ihash;
f347c4fa 172 struct scatterlist *src = req->src;
3c09f17c
HX
173 unsigned int authsize;
174 int err;
175
176 ohash = (u8 *)ALIGN((unsigned long)ohash + crypto_hash_alignmask(auth),
177 crypto_hash_alignmask(auth) + 1);
178 ihash = ohash + crypto_hash_digestsize(auth);
179
180 spin_lock_bh(&ctx->auth_lock);
181 err = crypto_hash_init(&desc);
182 if (err)
183 goto auth_unlock;
184
185 err = crypto_hash_update(&desc, req->assoc, req->assoclen);
186 if (err)
187 goto auth_unlock;
188
3c09f17c
HX
189 err = crypto_hash_update(&desc, src, cryptlen);
190 if (err)
191 goto auth_unlock;
192
193 err = crypto_hash_final(&desc, ohash);
194auth_unlock:
195 spin_unlock_bh(&ctx->auth_lock);
196
197 if (err)
198 return err;
199
7ba683a6 200 authsize = crypto_aead_authsize(authenc);
3c09f17c 201 scatterwalk_map_and_copy(ihash, src, cryptlen, authsize, 0);
fe70f5df 202 return memcmp(ihash, ohash, authsize) ? -EBADMSG: 0;
3c09f17c
HX
203}
204
205static void crypto_authenc_decrypt_done(struct crypto_async_request *req,
206 int err)
207{
208 aead_request_complete(req->data, err);
209}
210
211static int crypto_authenc_decrypt(struct aead_request *req)
212{
213 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
214 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
215 struct ablkcipher_request *abreq = aead_request_ctx(req);
481f34ae
HX
216 unsigned int cryptlen = req->cryptlen;
217 unsigned int authsize = crypto_aead_authsize(authenc);
3c09f17c
HX
218 int err;
219
481f34ae
HX
220 if (cryptlen < authsize)
221 return -EINVAL;
222 cryptlen -= authsize;
223
224 err = crypto_authenc_verify(req, cryptlen);
3c09f17c
HX
225 if (err)
226 return err;
227
228 ablkcipher_request_set_tfm(abreq, ctx->enc);
229 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
230 crypto_authenc_decrypt_done, req);
481f34ae 231 ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen,
3c09f17c
HX
232 req->iv);
233
234 return crypto_ablkcipher_decrypt(abreq);
235}
236
237static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
238{
239 struct crypto_instance *inst = (void *)tfm->__crt_alg;
240 struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst);
241 struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
242 struct crypto_hash *auth;
243 struct crypto_ablkcipher *enc;
3c09f17c
HX
244 int err;
245
246 auth = crypto_spawn_hash(&ictx->auth);
247 if (IS_ERR(auth))
248 return PTR_ERR(auth);
249
3c09f17c
HX
250 enc = crypto_spawn_ablkcipher(&ictx->enc);
251 err = PTR_ERR(enc);
252 if (IS_ERR(enc))
253 goto err_free_hash;
254
255 ctx->auth = auth;
256 ctx->enc = enc;
257 tfm->crt_aead.reqsize = max_t(unsigned int,
258 (crypto_hash_alignmask(auth) &
259 ~(crypto_tfm_ctx_alignment() - 1)) +
7ba683a6 260 crypto_hash_digestsize(auth) * 2,
3c09f17c
HX
261 sizeof(struct ablkcipher_request) +
262 crypto_ablkcipher_reqsize(enc));
263
264 spin_lock_init(&ctx->auth_lock);
265
266 return 0;
267
268err_free_hash:
269 crypto_free_hash(auth);
270 return err;
271}
272
273static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm)
274{
275 struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
276
277 crypto_free_hash(ctx->auth);
278 crypto_free_ablkcipher(ctx->enc);
279}
280
281static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
282{
283 struct crypto_instance *inst;
284 struct crypto_alg *auth;
285 struct crypto_alg *enc;
286 struct authenc_instance_ctx *ctx;
3c09f17c
HX
287 int err;
288
289 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD);
290 if (err)
291 return ERR_PTR(err);
292
293 auth = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
294 CRYPTO_ALG_TYPE_HASH_MASK);
295 if (IS_ERR(auth))
296 return ERR_PTR(PTR_ERR(auth));
297
7ba683a6 298 enc = crypto_attr_alg(tb[2], CRYPTO_ALG_TYPE_BLKCIPHER,
332f8840 299 CRYPTO_ALG_TYPE_BLKCIPHER_MASK);
3c09f17c
HX
300 inst = ERR_PTR(PTR_ERR(enc));
301 if (IS_ERR(enc))
302 goto out_put_auth;
303
3c09f17c
HX
304 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
305 err = -ENOMEM;
306 if (!inst)
307 goto out_put_enc;
308
309 err = -ENAMETOOLONG;
310 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
e236d4a8
HX
311 "authenc(%s,%s)", auth->cra_name, enc->cra_name) >=
312 CRYPTO_MAX_ALG_NAME)
3c09f17c
HX
313 goto err_free_inst;
314
315 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
e236d4a8
HX
316 "authenc(%s,%s)", auth->cra_driver_name,
317 enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
3c09f17c
HX
318 goto err_free_inst;
319
320 ctx = crypto_instance_ctx(inst);
3c09f17c
HX
321
322 err = crypto_init_spawn(&ctx->auth, auth, inst, CRYPTO_ALG_TYPE_MASK);
323 if (err)
324 goto err_free_inst;
325
326 err = crypto_init_spawn(&ctx->enc, enc, inst, CRYPTO_ALG_TYPE_MASK);
327 if (err)
328 goto err_drop_auth;
329
330 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC;
331 inst->alg.cra_priority = enc->cra_priority * 10 + auth->cra_priority;
332 inst->alg.cra_blocksize = enc->cra_blocksize;
e29bc6ad 333 inst->alg.cra_alignmask = auth->cra_alignmask | enc->cra_alignmask;
3c09f17c
HX
334 inst->alg.cra_type = &crypto_aead_type;
335
c2c61f51 336 inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize;
7ba683a6
HX
337 inst->alg.cra_aead.maxauthsize = auth->cra_type == &crypto_hash_type ?
338 auth->cra_hash.digestsize :
339 auth->cra_digest.dia_digestsize;
3c09f17c
HX
340
341 inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
342
343 inst->alg.cra_init = crypto_authenc_init_tfm;
344 inst->alg.cra_exit = crypto_authenc_exit_tfm;
345
346 inst->alg.cra_aead.setkey = crypto_authenc_setkey;
347 inst->alg.cra_aead.encrypt = crypto_authenc_encrypt;
348 inst->alg.cra_aead.decrypt = crypto_authenc_decrypt;
349
350out:
351 crypto_mod_put(enc);
352out_put_auth:
353 crypto_mod_put(auth);
354 return inst;
355
356err_drop_auth:
357 crypto_drop_spawn(&ctx->auth);
358err_free_inst:
359 kfree(inst);
360out_put_enc:
361 inst = ERR_PTR(err);
362 goto out;
363}
364
365static void crypto_authenc_free(struct crypto_instance *inst)
366{
367 struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst);
368
369 crypto_drop_spawn(&ctx->enc);
370 crypto_drop_spawn(&ctx->auth);
371 kfree(inst);
372}
373
374static struct crypto_template crypto_authenc_tmpl = {
375 .name = "authenc",
376 .alloc = crypto_authenc_alloc,
377 .free = crypto_authenc_free,
378 .module = THIS_MODULE,
379};
380
381static int __init crypto_authenc_module_init(void)
382{
383 return crypto_register_template(&crypto_authenc_tmpl);
384}
385
386static void __exit crypto_authenc_module_exit(void)
387{
388 crypto_unregister_template(&crypto_authenc_tmpl);
389}
390
391module_init(crypto_authenc_module_init);
392module_exit(crypto_authenc_module_exit);
393
394MODULE_LICENSE("GPL");
395MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");