OSDN Git Service

sparc32: fix struct ipc64_perm type definition
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / crypto / authencesn.c
1 /*
2  * authencesn.c - AEAD wrapper for IPsec with extended sequence numbers,
3  *                 derived from authenc.c
4  *
5  * Copyright (C) 2010 secunet Security Networks AG
6  * Copyright (C) 2010 Steffen Klassert <steffen.klassert@secunet.com>
7  * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the Free
11  * Software Foundation; either version 2 of the License, or (at your option)
12  * any later version.
13  *
14  */
15
16 #include <crypto/internal/aead.h>
17 #include <crypto/internal/hash.h>
18 #include <crypto/internal/skcipher.h>
19 #include <crypto/authenc.h>
20 #include <crypto/null.h>
21 #include <crypto/scatterwalk.h>
22 #include <linux/err.h>
23 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29
30 struct authenc_esn_instance_ctx {
31         struct crypto_ahash_spawn auth;
32         struct crypto_skcipher_spawn enc;
33 };
34
35 struct crypto_authenc_esn_ctx {
36         unsigned int reqoff;
37         struct crypto_ahash *auth;
38         struct crypto_ablkcipher *enc;
39         struct crypto_blkcipher *null;
40 };
41
42 struct authenc_esn_request_ctx {
43         struct scatterlist src[2];
44         struct scatterlist dst[2];
45         char tail[];
46 };
47
48 static void authenc_esn_request_complete(struct aead_request *req, int err)
49 {
50         if (err != -EINPROGRESS)
51                 aead_request_complete(req, err);
52 }
53
54 static int crypto_authenc_esn_setauthsize(struct crypto_aead *authenc_esn,
55                                           unsigned int authsize)
56 {
57         if (authsize > 0 && authsize < 4)
58                 return -EINVAL;
59
60         return 0;
61 }
62
63 static int crypto_authenc_esn_setkey(struct crypto_aead *authenc_esn, const u8 *key,
64                                      unsigned int keylen)
65 {
66         struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
67         struct crypto_ahash *auth = ctx->auth;
68         struct crypto_ablkcipher *enc = ctx->enc;
69         struct crypto_authenc_keys keys;
70         int err = -EINVAL;
71
72         if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
73                 goto badkey;
74
75         crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
76         crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc_esn) &
77                                      CRYPTO_TFM_REQ_MASK);
78         err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
79         crypto_aead_set_flags(authenc_esn, crypto_ahash_get_flags(auth) &
80                                            CRYPTO_TFM_RES_MASK);
81
82         if (err)
83                 goto out;
84
85         crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
86         crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc_esn) &
87                                          CRYPTO_TFM_REQ_MASK);
88         err = crypto_ablkcipher_setkey(enc, keys.enckey, keys.enckeylen);
89         crypto_aead_set_flags(authenc_esn, crypto_ablkcipher_get_flags(enc) &
90                                            CRYPTO_TFM_RES_MASK);
91
92 out:
93         memzero_explicit(&keys, sizeof(keys));
94         return err;
95
96 badkey:
97         crypto_aead_set_flags(authenc_esn, CRYPTO_TFM_RES_BAD_KEY_LEN);
98         goto out;
99 }
100
101 static int crypto_authenc_esn_genicv_tail(struct aead_request *req,
102                                           unsigned int flags)
103 {
104         struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
105         struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
106         struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
107         struct crypto_ahash *auth = ctx->auth;
108         u8 *hash = PTR_ALIGN((u8 *)areq_ctx->tail,
109                              crypto_ahash_alignmask(auth) + 1);
110         unsigned int authsize = crypto_aead_authsize(authenc_esn);
111         unsigned int assoclen = req->assoclen;
112         unsigned int cryptlen = req->cryptlen;
113         struct scatterlist *dst = req->dst;
114         u32 tmp[2];
115
116         /* Move high-order bits of sequence number back. */
117         scatterwalk_map_and_copy(tmp, dst, 4, 4, 0);
118         scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 0);
119         scatterwalk_map_and_copy(tmp, dst, 0, 8, 1);
120
121         scatterwalk_map_and_copy(hash, dst, assoclen + cryptlen, authsize, 1);
122         return 0;
123 }
124
125 static void authenc_esn_geniv_ahash_done(struct crypto_async_request *areq,
126                                          int err)
127 {
128         struct aead_request *req = areq->data;
129
130         err = err ?: crypto_authenc_esn_genicv_tail(req, 0);
131         aead_request_complete(req, err);
132 }
133
134 static int crypto_authenc_esn_genicv(struct aead_request *req,
135                                      unsigned int flags)
136 {
137         struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
138         struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
139         struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
140         struct crypto_ahash *auth = ctx->auth;
141         u8 *hash = PTR_ALIGN((u8 *)areq_ctx->tail,
142                              crypto_ahash_alignmask(auth) + 1);
143         struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
144         unsigned int authsize = crypto_aead_authsize(authenc_esn);
145         unsigned int assoclen = req->assoclen;
146         unsigned int cryptlen = req->cryptlen;
147         struct scatterlist *dst = req->dst;
148         u32 tmp[2];
149
150         if (!authsize)
151                 return 0;
152
153         /* Move high-order bits of sequence number to the end. */
154         scatterwalk_map_and_copy(tmp, dst, 0, 8, 0);
155         scatterwalk_map_and_copy(tmp, dst, 4, 4, 1);
156         scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 1);
157
158         sg_init_table(areq_ctx->dst, 2);
159         dst = scatterwalk_ffwd(areq_ctx->dst, dst, 4);
160
161         ahash_request_set_tfm(ahreq, auth);
162         ahash_request_set_crypt(ahreq, dst, hash, assoclen + cryptlen);
163         ahash_request_set_callback(ahreq, flags,
164                                    authenc_esn_geniv_ahash_done, req);
165
166         return crypto_ahash_digest(ahreq) ?:
167                crypto_authenc_esn_genicv_tail(req, aead_request_flags(req));
168 }
169
170
171 static void crypto_authenc_esn_encrypt_done(struct crypto_async_request *req,
172                                             int err)
173 {
174         struct aead_request *areq = req->data;
175
176         if (!err)
177                 err = crypto_authenc_esn_genicv(areq, 0);
178
179         authenc_esn_request_complete(areq, err);
180 }
181
182 static int crypto_authenc_esn_copy(struct aead_request *req, unsigned int len)
183 {
184         struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
185         struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
186         struct blkcipher_desc desc = {
187                 .tfm = ctx->null,
188         };
189
190         return crypto_blkcipher_encrypt(&desc, req->dst, req->src, len);
191 }
192
193 static int crypto_authenc_esn_encrypt(struct aead_request *req)
194 {
195         struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
196         struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
197         struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
198         struct ablkcipher_request *abreq = (void *)(areq_ctx->tail
199                                                     + ctx->reqoff);
200         struct crypto_ablkcipher *enc = ctx->enc;
201         unsigned int assoclen = req->assoclen;
202         unsigned int cryptlen = req->cryptlen;
203         struct scatterlist *src, *dst;
204         int err;
205
206         sg_init_table(areq_ctx->src, 2);
207         src = scatterwalk_ffwd(areq_ctx->src, req->src, assoclen);
208         dst = src;
209
210         if (req->src != req->dst) {
211                 err = crypto_authenc_esn_copy(req, assoclen);
212                 if (err)
213                         return err;
214
215                 sg_init_table(areq_ctx->dst, 2);
216                 dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, assoclen);
217         }
218
219         ablkcipher_request_set_tfm(abreq, enc);
220         ablkcipher_request_set_callback(abreq, aead_request_flags(req),
221                                         crypto_authenc_esn_encrypt_done, req);
222         ablkcipher_request_set_crypt(abreq, src, dst, cryptlen, req->iv);
223
224         err = crypto_ablkcipher_encrypt(abreq);
225         if (err)
226                 return err;
227
228         return crypto_authenc_esn_genicv(req, aead_request_flags(req));
229 }
230
231 static int crypto_authenc_esn_decrypt_tail(struct aead_request *req,
232                                            unsigned int flags)
233 {
234         struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
235         unsigned int authsize = crypto_aead_authsize(authenc_esn);
236         struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
237         struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
238         struct ablkcipher_request *abreq = (void *)(areq_ctx->tail
239                                                     + ctx->reqoff);
240         struct crypto_ahash *auth = ctx->auth;
241         u8 *ohash = PTR_ALIGN((u8 *)areq_ctx->tail,
242                               crypto_ahash_alignmask(auth) + 1);
243         unsigned int cryptlen = req->cryptlen - authsize;
244         unsigned int assoclen = req->assoclen;
245         struct scatterlist *dst = req->dst;
246         u8 *ihash = ohash + crypto_ahash_digestsize(auth);
247         u32 tmp[2];
248
249         if (!authsize)
250                 goto decrypt;
251
252         /* Move high-order bits of sequence number back. */
253         scatterwalk_map_and_copy(tmp, dst, 4, 4, 0);
254         scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 0);
255         scatterwalk_map_and_copy(tmp, dst, 0, 8, 1);
256
257         if (crypto_memneq(ihash, ohash, authsize))
258                 return -EBADMSG;
259
260 decrypt:
261
262         sg_init_table(areq_ctx->dst, 2);
263         dst = scatterwalk_ffwd(areq_ctx->dst, dst, assoclen);
264
265         ablkcipher_request_set_tfm(abreq, ctx->enc);
266         ablkcipher_request_set_callback(abreq, flags,
267                                         req->base.complete, req->base.data);
268         ablkcipher_request_set_crypt(abreq, dst, dst, cryptlen, req->iv);
269
270         return crypto_ablkcipher_decrypt(abreq);
271 }
272
273 static void authenc_esn_verify_ahash_done(struct crypto_async_request *areq,
274                                           int err)
275 {
276         struct aead_request *req = areq->data;
277
278         err = err ?: crypto_authenc_esn_decrypt_tail(req, 0);
279         authenc_esn_request_complete(req, err);
280 }
281
282 static int crypto_authenc_esn_decrypt(struct aead_request *req)
283 {
284         struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
285         struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
286         struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
287         struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
288         unsigned int authsize = crypto_aead_authsize(authenc_esn);
289         struct crypto_ahash *auth = ctx->auth;
290         u8 *ohash = PTR_ALIGN((u8 *)areq_ctx->tail,
291                               crypto_ahash_alignmask(auth) + 1);
292         unsigned int assoclen = req->assoclen;
293         unsigned int cryptlen = req->cryptlen;
294         u8 *ihash = ohash + crypto_ahash_digestsize(auth);
295         struct scatterlist *dst = req->dst;
296         u32 tmp[2];
297         int err;
298
299         cryptlen -= authsize;
300
301         if (req->src != dst) {
302                 err = crypto_authenc_esn_copy(req, assoclen + cryptlen);
303                 if (err)
304                         return err;
305         }
306
307         scatterwalk_map_and_copy(ihash, req->src, assoclen + cryptlen,
308                                  authsize, 0);
309
310         if (!authsize)
311                 goto tail;
312
313         /* Move high-order bits of sequence number to the end. */
314         scatterwalk_map_and_copy(tmp, dst, 0, 8, 0);
315         scatterwalk_map_and_copy(tmp, dst, 4, 4, 1);
316         scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 1);
317
318         sg_init_table(areq_ctx->dst, 2);
319         dst = scatterwalk_ffwd(areq_ctx->dst, dst, 4);
320
321         ahash_request_set_tfm(ahreq, auth);
322         ahash_request_set_crypt(ahreq, dst, ohash, assoclen + cryptlen);
323         ahash_request_set_callback(ahreq, aead_request_flags(req),
324                                    authenc_esn_verify_ahash_done, req);
325
326         err = crypto_ahash_digest(ahreq);
327         if (err)
328                 return err;
329
330 tail:
331         return crypto_authenc_esn_decrypt_tail(req, aead_request_flags(req));
332 }
333
334 static int crypto_authenc_esn_init_tfm(struct crypto_aead *tfm)
335 {
336         struct aead_instance *inst = aead_alg_instance(tfm);
337         struct authenc_esn_instance_ctx *ictx = aead_instance_ctx(inst);
338         struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(tfm);
339         struct crypto_ahash *auth;
340         struct crypto_ablkcipher *enc;
341         struct crypto_blkcipher *null;
342         int err;
343
344         auth = crypto_spawn_ahash(&ictx->auth);
345         if (IS_ERR(auth))
346                 return PTR_ERR(auth);
347
348         enc = crypto_spawn_skcipher(&ictx->enc);
349         err = PTR_ERR(enc);
350         if (IS_ERR(enc))
351                 goto err_free_ahash;
352
353         null = crypto_get_default_null_skcipher();
354         err = PTR_ERR(null);
355         if (IS_ERR(null))
356                 goto err_free_skcipher;
357
358         ctx->auth = auth;
359         ctx->enc = enc;
360         ctx->null = null;
361
362         ctx->reqoff = ALIGN(2 * crypto_ahash_digestsize(auth),
363                             crypto_ahash_alignmask(auth) + 1);
364
365         crypto_aead_set_reqsize(
366                 tfm,
367                 sizeof(struct authenc_esn_request_ctx) +
368                 ctx->reqoff +
369                 max_t(unsigned int,
370                         crypto_ahash_reqsize(auth) +
371                         sizeof(struct ahash_request),
372                         sizeof(struct skcipher_givcrypt_request) +
373                         crypto_ablkcipher_reqsize(enc)));
374
375         return 0;
376
377 err_free_skcipher:
378         crypto_free_ablkcipher(enc);
379 err_free_ahash:
380         crypto_free_ahash(auth);
381         return err;
382 }
383
384 static void crypto_authenc_esn_exit_tfm(struct crypto_aead *tfm)
385 {
386         struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(tfm);
387
388         crypto_free_ahash(ctx->auth);
389         crypto_free_ablkcipher(ctx->enc);
390         crypto_put_default_null_skcipher();
391 }
392
393 static void crypto_authenc_esn_free(struct aead_instance *inst)
394 {
395         struct authenc_esn_instance_ctx *ctx = aead_instance_ctx(inst);
396
397         crypto_drop_skcipher(&ctx->enc);
398         crypto_drop_ahash(&ctx->auth);
399         kfree(inst);
400 }
401
402 static int crypto_authenc_esn_create(struct crypto_template *tmpl,
403                                      struct rtattr **tb)
404 {
405         struct crypto_attr_type *algt;
406         struct aead_instance *inst;
407         struct hash_alg_common *auth;
408         struct crypto_alg *auth_base;
409         struct crypto_alg *enc;
410         struct authenc_esn_instance_ctx *ctx;
411         const char *enc_name;
412         int err;
413
414         algt = crypto_get_attr_type(tb);
415         if (IS_ERR(algt))
416                 return PTR_ERR(algt);
417
418         if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
419                 return -EINVAL;
420
421         auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
422                                CRYPTO_ALG_TYPE_AHASH_MASK);
423         if (IS_ERR(auth))
424                 return PTR_ERR(auth);
425
426         auth_base = &auth->base;
427
428         enc_name = crypto_attr_alg_name(tb[2]);
429         err = PTR_ERR(enc_name);
430         if (IS_ERR(enc_name))
431                 goto out_put_auth;
432
433         inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
434         err = -ENOMEM;
435         if (!inst)
436                 goto out_put_auth;
437
438         ctx = aead_instance_ctx(inst);
439
440         err = crypto_init_ahash_spawn(&ctx->auth, auth,
441                                       aead_crypto_instance(inst));
442         if (err)
443                 goto err_free_inst;
444
445         crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst));
446         err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
447                                    crypto_requires_sync(algt->type,
448                                                         algt->mask));
449         if (err)
450                 goto err_drop_auth;
451
452         enc = crypto_skcipher_spawn_alg(&ctx->enc);
453
454         err = -ENAMETOOLONG;
455         if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
456                      "authencesn(%s,%s)", auth_base->cra_name,
457                      enc->cra_name) >= CRYPTO_MAX_ALG_NAME)
458                 goto err_drop_enc;
459
460         if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
461                      "authencesn(%s,%s)", auth_base->cra_driver_name,
462                      enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
463                 goto err_drop_enc;
464
465         inst->alg.base.cra_flags = enc->cra_flags & CRYPTO_ALG_ASYNC;
466         inst->alg.base.cra_priority = enc->cra_priority * 10 +
467                                       auth_base->cra_priority;
468         inst->alg.base.cra_blocksize = enc->cra_blocksize;
469         inst->alg.base.cra_alignmask = auth_base->cra_alignmask |
470                                        enc->cra_alignmask;
471         inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_esn_ctx);
472
473         inst->alg.ivsize = enc->cra_ablkcipher.ivsize;
474         inst->alg.maxauthsize = auth->digestsize;
475
476         inst->alg.init = crypto_authenc_esn_init_tfm;
477         inst->alg.exit = crypto_authenc_esn_exit_tfm;
478
479         inst->alg.setkey = crypto_authenc_esn_setkey;
480         inst->alg.setauthsize = crypto_authenc_esn_setauthsize;
481         inst->alg.encrypt = crypto_authenc_esn_encrypt;
482         inst->alg.decrypt = crypto_authenc_esn_decrypt;
483
484         inst->free = crypto_authenc_esn_free,
485
486         err = aead_register_instance(tmpl, inst);
487         if (err)
488                 goto err_drop_enc;
489
490 out:
491         crypto_mod_put(auth_base);
492         return err;
493
494 err_drop_enc:
495         crypto_drop_skcipher(&ctx->enc);
496 err_drop_auth:
497         crypto_drop_ahash(&ctx->auth);
498 err_free_inst:
499         kfree(inst);
500 out_put_auth:
501         goto out;
502 }
503
504 static struct crypto_template crypto_authenc_esn_tmpl = {
505         .name = "authencesn",
506         .create = crypto_authenc_esn_create,
507         .module = THIS_MODULE,
508 };
509
510 static int __init crypto_authenc_esn_module_init(void)
511 {
512         return crypto_register_template(&crypto_authenc_esn_tmpl);
513 }
514
515 static void __exit crypto_authenc_esn_module_exit(void)
516 {
517         crypto_unregister_template(&crypto_authenc_esn_tmpl);
518 }
519
520 module_init(crypto_authenc_esn_module_init);
521 module_exit(crypto_authenc_esn_module_exit);
522
523 MODULE_LICENSE("GPL");
524 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
525 MODULE_DESCRIPTION("AEAD wrapper for IPsec with extended sequence numbers");
526 MODULE_ALIAS_CRYPTO("authencesn");