crypto: af_alg - whitelist mask and type
[pandora-kernel.git] / crypto / ahash.c
1 /*
2  * Asynchronous Cryptographic Hash operations.
3  *
4  * This is the asynchronous version of hash.c with notification of
5  * completion via a callback.
6  *
7  * Copyright (c) 2008 Loc Ho <lho@amcc.com>
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/hash.h>
17 #include <crypto/scatterwalk.h>
18 #include <linux/err.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/seq_file.h>
24 #include <linux/cryptouser.h>
25 #include <net/netlink.h>
26
27 #include "internal.h"
28
29 struct ahash_request_priv {
30         crypto_completion_t complete;
31         void *data;
32         u8 *result;
33         u32 flags;
34         void *ubuf[] CRYPTO_MINALIGN_ATTR;
35 };
36
37 static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash)
38 {
39         return container_of(crypto_hash_alg_common(hash), struct ahash_alg,
40                             halg);
41 }
42
43 static int hash_walk_next(struct crypto_hash_walk *walk)
44 {
45         unsigned int alignmask = walk->alignmask;
46         unsigned int offset = walk->offset;
47         unsigned int nbytes = min(walk->entrylen,
48                                   ((unsigned int)(PAGE_SIZE)) - offset);
49
50         walk->data = crypto_kmap(walk->pg, 0);
51         walk->data += offset;
52
53         if (offset & alignmask) {
54                 unsigned int unaligned = alignmask + 1 - (offset & alignmask);
55                 if (nbytes > unaligned)
56                         nbytes = unaligned;
57         }
58
59         walk->entrylen -= nbytes;
60         return nbytes;
61 }
62
63 static int hash_walk_new_entry(struct crypto_hash_walk *walk)
64 {
65         struct scatterlist *sg;
66
67         sg = walk->sg;
68         walk->offset = sg->offset;
69         walk->pg = sg_page(walk->sg) + (walk->offset >> PAGE_SHIFT);
70         walk->offset = offset_in_page(walk->offset);
71         walk->entrylen = sg->length;
72
73         if (walk->entrylen > walk->total)
74                 walk->entrylen = walk->total;
75         walk->total -= walk->entrylen;
76
77         return hash_walk_next(walk);
78 }
79
80 int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
81 {
82         unsigned int alignmask = walk->alignmask;
83         unsigned int nbytes = walk->entrylen;
84
85         walk->data -= walk->offset;
86
87         if (nbytes && walk->offset & alignmask && !err) {
88                 walk->offset = ALIGN(walk->offset, alignmask + 1);
89                 walk->data += walk->offset;
90
91                 nbytes = min(nbytes,
92                              ((unsigned int)(PAGE_SIZE)) - walk->offset);
93                 walk->entrylen -= nbytes;
94
95                 return nbytes;
96         }
97
98         crypto_kunmap(walk->data, 0);
99         crypto_yield(walk->flags);
100
101         if (err)
102                 return err;
103
104         if (nbytes) {
105                 walk->offset = 0;
106                 walk->pg++;
107                 return hash_walk_next(walk);
108         }
109
110         if (!walk->total)
111                 return 0;
112
113         walk->sg = scatterwalk_sg_next(walk->sg);
114
115         return hash_walk_new_entry(walk);
116 }
117 EXPORT_SYMBOL_GPL(crypto_hash_walk_done);
118
119 int crypto_hash_walk_first(struct ahash_request *req,
120                            struct crypto_hash_walk *walk)
121 {
122         walk->total = req->nbytes;
123
124         if (!walk->total)
125                 return 0;
126
127         walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
128         walk->sg = req->src;
129         walk->flags = req->base.flags;
130
131         return hash_walk_new_entry(walk);
132 }
133 EXPORT_SYMBOL_GPL(crypto_hash_walk_first);
134
135 int crypto_hash_walk_first_compat(struct hash_desc *hdesc,
136                                   struct crypto_hash_walk *walk,
137                                   struct scatterlist *sg, unsigned int len)
138 {
139         walk->total = len;
140
141         if (!walk->total)
142                 return 0;
143
144         walk->alignmask = crypto_hash_alignmask(hdesc->tfm);
145         walk->sg = sg;
146         walk->flags = hdesc->flags;
147
148         return hash_walk_new_entry(walk);
149 }
150
151 static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
152                                 unsigned int keylen)
153 {
154         unsigned long alignmask = crypto_ahash_alignmask(tfm);
155         int ret;
156         u8 *buffer, *alignbuffer;
157         unsigned long absize;
158
159         absize = keylen + alignmask;
160         buffer = kmalloc(absize, GFP_KERNEL);
161         if (!buffer)
162                 return -ENOMEM;
163
164         alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
165         memcpy(alignbuffer, key, keylen);
166         ret = tfm->setkey(tfm, alignbuffer, keylen);
167         kzfree(buffer);
168         return ret;
169 }
170
171 int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
172                         unsigned int keylen)
173 {
174         unsigned long alignmask = crypto_ahash_alignmask(tfm);
175
176         if ((unsigned long)key & alignmask)
177                 return ahash_setkey_unaligned(tfm, key, keylen);
178
179         return tfm->setkey(tfm, key, keylen);
180 }
181 EXPORT_SYMBOL_GPL(crypto_ahash_setkey);
182
183 static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
184                           unsigned int keylen)
185 {
186         return -ENOSYS;
187 }
188
189 static inline unsigned int ahash_align_buffer_size(unsigned len,
190                                                    unsigned long mask)
191 {
192         return len + (mask & ~(crypto_tfm_ctx_alignment() - 1));
193 }
194
195 static int ahash_save_req(struct ahash_request *req, crypto_completion_t cplt)
196 {
197         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
198         unsigned long alignmask = crypto_ahash_alignmask(tfm);
199         unsigned int ds = crypto_ahash_digestsize(tfm);
200         struct ahash_request_priv *priv;
201
202         priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask),
203                        (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
204                        GFP_KERNEL : GFP_ATOMIC);
205         if (!priv)
206                 return -ENOMEM;
207
208         /*
209          * WARNING: Voodoo programming below!
210          *
211          * The code below is obscure and hard to understand, thus explanation
212          * is necessary. See include/crypto/hash.h and include/linux/crypto.h
213          * to understand the layout of structures used here!
214          *
215          * The code here will replace portions of the ORIGINAL request with
216          * pointers to new code and buffers so the hashing operation can store
217          * the result in aligned buffer. We will call the modified request
218          * an ADJUSTED request.
219          *
220          * The newly mangled request will look as such:
221          *
222          * req {
223          *   .result        = ADJUSTED[new aligned buffer]
224          *   .base.complete = ADJUSTED[pointer to completion function]
225          *   .base.data     = ADJUSTED[*req (pointer to self)]
226          *   .priv          = ADJUSTED[new priv] {
227          *           .result   = ORIGINAL(result)
228          *           .complete = ORIGINAL(base.complete)
229          *           .data     = ORIGINAL(base.data)
230          *   }
231          */
232
233         priv->result = req->result;
234         priv->complete = req->base.complete;
235         priv->data = req->base.data;
236         priv->flags = req->base.flags;
237
238         /*
239          * WARNING: We do not backup req->priv here! The req->priv
240          *          is for internal use of the Crypto API and the
241          *          user must _NOT_ _EVER_ depend on it's content!
242          */
243
244         req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1);
245         req->base.complete = cplt;
246         req->base.data = req;
247         req->priv = priv;
248
249         return 0;
250 }
251
252 static void ahash_restore_req(struct ahash_request *req, int err)
253 {
254         struct ahash_request_priv *priv = req->priv;
255
256         if (!err)
257                 memcpy(priv->result, req->result,
258                        crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
259
260         /* Restore the original crypto request. */
261         req->result = priv->result;
262
263         ahash_request_set_callback(req, priv->flags,
264                                    priv->complete, priv->data);
265         req->priv = NULL;
266
267         /* Free the req->priv.priv from the ADJUSTED request. */
268         kzfree(priv);
269 }
270
271 static void ahash_notify_einprogress(struct ahash_request *req)
272 {
273         struct ahash_request_priv *priv = req->priv;
274         struct crypto_async_request oreq;
275
276         oreq.data = priv->data;
277
278         priv->complete(&oreq, -EINPROGRESS);
279 }
280
281 static void ahash_op_unaligned_done(struct crypto_async_request *req, int err)
282 {
283         struct ahash_request *areq = req->data;
284
285         if (err == -EINPROGRESS) {
286                 ahash_notify_einprogress(areq);
287                 return;
288         }
289
290         /*
291          * Restore the original request, see ahash_op_unaligned() for what
292          * goes where.
293          *
294          * The "struct ahash_request *req" here is in fact the "req.base"
295          * from the ADJUSTED request from ahash_op_unaligned(), thus as it
296          * is a pointer to self, it is also the ADJUSTED "req" .
297          */
298
299         /* First copy req->result into req->priv.result */
300         ahash_restore_req(areq, err);
301
302         /* Complete the ORIGINAL request. */
303         areq->base.complete(&areq->base, err);
304 }
305
306 static int ahash_op_unaligned(struct ahash_request *req,
307                               int (*op)(struct ahash_request *))
308 {
309         int err;
310
311         err = ahash_save_req(req, ahash_op_unaligned_done);
312         if (err)
313                 return err;
314
315         err = op(req);
316         if (err == -EINPROGRESS ||
317             (err == -EBUSY && (ahash_request_flags(req) &
318                                CRYPTO_TFM_REQ_MAY_BACKLOG)))
319                 return err;
320
321         ahash_restore_req(req, err);
322
323         return err;
324 }
325
326 static int crypto_ahash_op(struct ahash_request *req,
327                            int (*op)(struct ahash_request *))
328 {
329         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
330         unsigned long alignmask = crypto_ahash_alignmask(tfm);
331
332         if ((unsigned long)req->result & alignmask)
333                 return ahash_op_unaligned(req, op);
334
335         return op(req);
336 }
337
338 int crypto_ahash_final(struct ahash_request *req)
339 {
340         return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final);
341 }
342 EXPORT_SYMBOL_GPL(crypto_ahash_final);
343
344 int crypto_ahash_finup(struct ahash_request *req)
345 {
346         return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup);
347 }
348 EXPORT_SYMBOL_GPL(crypto_ahash_finup);
349
350 int crypto_ahash_digest(struct ahash_request *req)
351 {
352         return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->digest);
353 }
354 EXPORT_SYMBOL_GPL(crypto_ahash_digest);
355
356 static void ahash_def_finup_done2(struct crypto_async_request *req, int err)
357 {
358         struct ahash_request *areq = req->data;
359
360         if (err == -EINPROGRESS)
361                 return;
362
363         ahash_restore_req(areq, err);
364
365         areq->base.complete(&areq->base, err);
366 }
367
368 static int ahash_def_finup_finish1(struct ahash_request *req, int err)
369 {
370         if (err)
371                 goto out;
372
373         req->base.complete = ahash_def_finup_done2;
374
375         err = crypto_ahash_reqtfm(req)->final(req);
376         if (err == -EINPROGRESS ||
377             (err == -EBUSY && (ahash_request_flags(req) &
378                                CRYPTO_TFM_REQ_MAY_BACKLOG)))
379                 return err;
380
381 out:
382         ahash_restore_req(req, err);
383         return err;
384 }
385
386 static void ahash_def_finup_done1(struct crypto_async_request *req, int err)
387 {
388         struct ahash_request *areq = req->data;
389
390         if (err == -EINPROGRESS) {
391                 ahash_notify_einprogress(areq);
392                 return;
393         }
394
395         areq->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
396
397         err = ahash_def_finup_finish1(areq, err);
398         if (areq->priv)
399                 return;
400
401         areq->base.complete(&areq->base, err);
402 }
403
404 static int ahash_def_finup(struct ahash_request *req)
405 {
406         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
407         int err;
408
409         err = ahash_save_req(req, ahash_def_finup_done1);
410         if (err)
411                 return err;
412
413         err = tfm->update(req);
414         if (err == -EINPROGRESS ||
415             (err == -EBUSY && (ahash_request_flags(req) &
416                                CRYPTO_TFM_REQ_MAY_BACKLOG)))
417                 return err;
418
419         return ahash_def_finup_finish1(req, err);
420 }
421
422 static int ahash_no_export(struct ahash_request *req, void *out)
423 {
424         return -ENOSYS;
425 }
426
427 static int ahash_no_import(struct ahash_request *req, const void *in)
428 {
429         return -ENOSYS;
430 }
431
432 static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
433 {
434         struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
435         struct ahash_alg *alg = crypto_ahash_alg(hash);
436
437         hash->setkey = ahash_nosetkey;
438         hash->has_setkey = false;
439         hash->export = ahash_no_export;
440         hash->import = ahash_no_import;
441
442         if (tfm->__crt_alg->cra_type != &crypto_ahash_type)
443                 return crypto_init_shash_ops_async(tfm);
444
445         hash->init = alg->init;
446         hash->update = alg->update;
447         hash->final = alg->final;
448         hash->finup = alg->finup ?: ahash_def_finup;
449         hash->digest = alg->digest;
450
451         if (alg->setkey) {
452                 hash->setkey = alg->setkey;
453                 hash->has_setkey = true;
454         }
455         if (alg->export)
456                 hash->export = alg->export;
457         if (alg->import)
458                 hash->import = alg->import;
459
460         return 0;
461 }
462
463 static unsigned int crypto_ahash_extsize(struct crypto_alg *alg)
464 {
465         if (alg->cra_type == &crypto_ahash_type)
466                 return alg->cra_ctxsize;
467
468         return sizeof(struct crypto_shash *);
469 }
470
471 #ifdef CONFIG_NET
472 static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
473 {
474         struct crypto_report_hash rhash;
475
476         strncpy(rhash.type, "ahash", sizeof(rhash.type));
477
478         rhash.blocksize = alg->cra_blocksize;
479         rhash.digestsize = __crypto_hash_alg_common(alg)->digestsize;
480
481         NLA_PUT(skb, CRYPTOCFGA_REPORT_HASH,
482                 sizeof(struct crypto_report_hash), &rhash);
483
484         return 0;
485
486 nla_put_failure:
487         return -EMSGSIZE;
488 }
489 #else
490 static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
491 {
492         return -ENOSYS;
493 }
494 #endif
495
496 static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
497         __attribute__ ((unused));
498 static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
499 {
500         seq_printf(m, "type         : ahash\n");
501         seq_printf(m, "async        : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
502                                              "yes" : "no");
503         seq_printf(m, "blocksize    : %u\n", alg->cra_blocksize);
504         seq_printf(m, "digestsize   : %u\n",
505                    __crypto_hash_alg_common(alg)->digestsize);
506 }
507
508 const struct crypto_type crypto_ahash_type = {
509         .extsize = crypto_ahash_extsize,
510         .init_tfm = crypto_ahash_init_tfm,
511 #ifdef CONFIG_PROC_FS
512         .show = crypto_ahash_show,
513 #endif
514         .report = crypto_ahash_report,
515         .maskclear = ~CRYPTO_ALG_TYPE_MASK,
516         .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
517         .type = CRYPTO_ALG_TYPE_AHASH,
518         .tfmsize = offsetof(struct crypto_ahash, base),
519 };
520 EXPORT_SYMBOL_GPL(crypto_ahash_type);
521
522 struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
523                                         u32 mask)
524 {
525         return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask);
526 }
527 EXPORT_SYMBOL_GPL(crypto_alloc_ahash);
528
529 static int ahash_prepare_alg(struct ahash_alg *alg)
530 {
531         struct crypto_alg *base = &alg->halg.base;
532
533         if (alg->halg.digestsize > PAGE_SIZE / 8 ||
534             alg->halg.statesize > PAGE_SIZE / 8 ||
535             alg->halg.statesize == 0)
536                 return -EINVAL;
537
538         base->cra_type = &crypto_ahash_type;
539         base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
540         base->cra_flags |= CRYPTO_ALG_TYPE_AHASH;
541
542         return 0;
543 }
544
545 int crypto_register_ahash(struct ahash_alg *alg)
546 {
547         struct crypto_alg *base = &alg->halg.base;
548         int err;
549
550         err = ahash_prepare_alg(alg);
551         if (err)
552                 return err;
553
554         return crypto_register_alg(base);
555 }
556 EXPORT_SYMBOL_GPL(crypto_register_ahash);
557
558 int crypto_unregister_ahash(struct ahash_alg *alg)
559 {
560         return crypto_unregister_alg(&alg->halg.base);
561 }
562 EXPORT_SYMBOL_GPL(crypto_unregister_ahash);
563
564 int ahash_register_instance(struct crypto_template *tmpl,
565                             struct ahash_instance *inst)
566 {
567         int err;
568
569         err = ahash_prepare_alg(&inst->alg);
570         if (err)
571                 return err;
572
573         return crypto_register_instance(tmpl, ahash_crypto_instance(inst));
574 }
575 EXPORT_SYMBOL_GPL(ahash_register_instance);
576
577 void ahash_free_instance(struct crypto_instance *inst)
578 {
579         crypto_drop_spawn(crypto_instance_ctx(inst));
580         kfree(ahash_instance(inst));
581 }
582 EXPORT_SYMBOL_GPL(ahash_free_instance);
583
584 int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn,
585                             struct hash_alg_common *alg,
586                             struct crypto_instance *inst)
587 {
588         return crypto_init_spawn2(&spawn->base, &alg->base, inst,
589                                   &crypto_ahash_type);
590 }
591 EXPORT_SYMBOL_GPL(crypto_init_ahash_spawn);
592
593 struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
594 {
595         struct crypto_alg *alg;
596
597         alg = crypto_attr_alg2(rta, &crypto_ahash_type, type, mask);
598         return IS_ERR(alg) ? ERR_CAST(alg) : __crypto_hash_alg_common(alg);
599 }
600 EXPORT_SYMBOL_GPL(ahash_attr_alg);
601
602 MODULE_LICENSE("GPL");
603 MODULE_DESCRIPTION("Asynchronous cryptographic hash type");