[CRYPTO] scatterwalk: Use generic scatterlist chaining
[safe/jmp/linux-2.6] / crypto / digest.c
1 /*
2  * Cryptographic API.
3  *
4  * Digest operations.
5  *
6  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the Free
10  * Software Foundation; either version 2 of the License, or (at your option) 
11  * any later version.
12  *
13  */
14
15 #include <linux/mm.h>
16 #include <linux/errno.h>
17 #include <linux/hardirq.h>
18 #include <linux/highmem.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/scatterlist.h>
22
23 #include "internal.h"
24
25 static int init(struct hash_desc *desc)
26 {
27         struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm);
28
29         tfm->__crt_alg->cra_digest.dia_init(tfm);
30         return 0;
31 }
32
33 static int update2(struct hash_desc *desc,
34                    struct scatterlist *sg, unsigned int nbytes)
35 {
36         struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm);
37         unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
38
39         if (!nbytes)
40                 return 0;
41
42         for (;;) {
43                 struct page *pg = sg_page(sg);
44                 unsigned int offset = sg->offset;
45                 unsigned int l = sg->length;
46
47                 if (unlikely(l > nbytes))
48                         l = nbytes;
49                 nbytes -= l;
50
51                 do {
52                         unsigned int bytes_from_page = min(l, ((unsigned int)
53                                                            (PAGE_SIZE)) - 
54                                                            offset);
55                         char *src = crypto_kmap(pg, 0);
56                         char *p = src + offset;
57
58                         if (unlikely(offset & alignmask)) {
59                                 unsigned int bytes =
60                                         alignmask + 1 - (offset & alignmask);
61                                 bytes = min(bytes, bytes_from_page);
62                                 tfm->__crt_alg->cra_digest.dia_update(tfm, p,
63                                                                       bytes);
64                                 p += bytes;
65                                 bytes_from_page -= bytes;
66                                 l -= bytes;
67                         }
68                         tfm->__crt_alg->cra_digest.dia_update(tfm, p,
69                                                               bytes_from_page);
70                         crypto_kunmap(src, 0);
71                         crypto_yield(desc->flags);
72                         offset = 0;
73                         pg++;
74                         l -= bytes_from_page;
75                 } while (l > 0);
76
77                 if (!nbytes)
78                         break;
79                 sg = sg_next(sg);
80         }
81
82         return 0;
83 }
84
85 static int update(struct hash_desc *desc,
86                   struct scatterlist *sg, unsigned int nbytes)
87 {
88         if (WARN_ON_ONCE(in_irq()))
89                 return -EDEADLK;
90         return update2(desc, sg, nbytes);
91 }
92
93 static int final(struct hash_desc *desc, u8 *out)
94 {
95         struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm);
96         unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
97         struct digest_alg *digest = &tfm->__crt_alg->cra_digest;
98
99         if (unlikely((unsigned long)out & alignmask)) {
100                 unsigned long align = alignmask + 1;
101                 unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm);
102                 u8 *dst = (u8 *)ALIGN(addr, align) +
103                           ALIGN(tfm->__crt_alg->cra_ctxsize, align);
104
105                 digest->dia_final(tfm, dst);
106                 memcpy(out, dst, digest->dia_digestsize);
107         } else
108                 digest->dia_final(tfm, out);
109
110         return 0;
111 }
112
113 static int nosetkey(struct crypto_hash *tfm, const u8 *key, unsigned int keylen)
114 {
115         crypto_hash_clear_flags(tfm, CRYPTO_TFM_RES_MASK);
116         return -ENOSYS;
117 }
118
119 static int setkey(struct crypto_hash *hash, const u8 *key, unsigned int keylen)
120 {
121         struct crypto_tfm *tfm = crypto_hash_tfm(hash);
122
123         crypto_hash_clear_flags(hash, CRYPTO_TFM_RES_MASK);
124         return tfm->__crt_alg->cra_digest.dia_setkey(tfm, key, keylen);
125 }
126
127 static int digest(struct hash_desc *desc,
128                   struct scatterlist *sg, unsigned int nbytes, u8 *out)
129 {
130         if (WARN_ON_ONCE(in_irq()))
131                 return -EDEADLK;
132
133         init(desc);
134         update2(desc, sg, nbytes);
135         return final(desc, out);
136 }
137
138 int crypto_init_digest_ops(struct crypto_tfm *tfm)
139 {
140         struct hash_tfm *ops = &tfm->crt_hash;
141         struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
142
143         if (dalg->dia_digestsize > crypto_tfm_alg_blocksize(tfm))
144                 return -EINVAL;
145         
146         ops->init       = init;
147         ops->update     = update;
148         ops->final      = final;
149         ops->digest     = digest;
150         ops->setkey     = dalg->dia_setkey ? setkey : nosetkey;
151         ops->digestsize = dalg->dia_digestsize;
152         
153         return 0;
154 }
155
156 void crypto_exit_digest_ops(struct crypto_tfm *tfm)
157 {
158 }