diff options
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/digest.c | 240 | ||||
-rw-r--r-- | crypto/hash.c | 183 |
2 files changed, 0 insertions, 423 deletions
diff --git a/crypto/digest.c b/crypto/digest.c deleted file mode 100644 index 5d3f1303da98..000000000000 --- a/crypto/digest.c +++ /dev/null | |||
@@ -1,240 +0,0 @@ | |||
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 <crypto/internal/hash.h> | ||
16 | #include <crypto/scatterwalk.h> | ||
17 | #include <linux/mm.h> | ||
18 | #include <linux/errno.h> | ||
19 | #include <linux/hardirq.h> | ||
20 | #include <linux/highmem.h> | ||
21 | #include <linux/kernel.h> | ||
22 | #include <linux/module.h> | ||
23 | #include <linux/scatterlist.h> | ||
24 | |||
25 | #include "internal.h" | ||
26 | |||
27 | static int init(struct hash_desc *desc) | ||
28 | { | ||
29 | struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm); | ||
30 | |||
31 | tfm->__crt_alg->cra_digest.dia_init(tfm); | ||
32 | return 0; | ||
33 | } | ||
34 | |||
35 | static int update2(struct hash_desc *desc, | ||
36 | struct scatterlist *sg, unsigned int nbytes) | ||
37 | { | ||
38 | struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm); | ||
39 | unsigned int alignmask = crypto_tfm_alg_alignmask(tfm); | ||
40 | |||
41 | if (!nbytes) | ||
42 | return 0; | ||
43 | |||
44 | for (;;) { | ||
45 | struct page *pg = sg_page(sg); | ||
46 | unsigned int offset = sg->offset; | ||
47 | unsigned int l = sg->length; | ||
48 | |||
49 | if (unlikely(l > nbytes)) | ||
50 | l = nbytes; | ||
51 | nbytes -= l; | ||
52 | |||
53 | do { | ||
54 | unsigned int bytes_from_page = min(l, ((unsigned int) | ||
55 | (PAGE_SIZE)) - | ||
56 | offset); | ||
57 | char *src = crypto_kmap(pg, 0); | ||
58 | char *p = src + offset; | ||
59 | |||
60 | if (unlikely(offset & alignmask)) { | ||
61 | unsigned int bytes = | ||
62 | alignmask + 1 - (offset & alignmask); | ||
63 | bytes = min(bytes, bytes_from_page); | ||
64 | tfm->__crt_alg->cra_digest.dia_update(tfm, p, | ||
65 | bytes); | ||
66 | p += bytes; | ||
67 | bytes_from_page -= bytes; | ||
68 | l -= bytes; | ||
69 | } | ||
70 | tfm->__crt_alg->cra_digest.dia_update(tfm, p, | ||
71 | bytes_from_page); | ||
72 | crypto_kunmap(src, 0); | ||
73 | crypto_yield(desc->flags); | ||
74 | offset = 0; | ||
75 | pg++; | ||
76 | l -= bytes_from_page; | ||
77 | } while (l > 0); | ||
78 | |||
79 | if (!nbytes) | ||
80 | break; | ||
81 | sg = scatterwalk_sg_next(sg); | ||
82 | } | ||
83 | |||
84 | return 0; | ||
85 | } | ||
86 | |||
87 | static int update(struct hash_desc *desc, | ||
88 | struct scatterlist *sg, unsigned int nbytes) | ||
89 | { | ||
90 | if (WARN_ON_ONCE(in_irq())) | ||
91 | return -EDEADLK; | ||
92 | return update2(desc, sg, nbytes); | ||
93 | } | ||
94 | |||
95 | static int final(struct hash_desc *desc, u8 *out) | ||
96 | { | ||
97 | struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm); | ||
98 | unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); | ||
99 | struct digest_alg *digest = &tfm->__crt_alg->cra_digest; | ||
100 | |||
101 | if (unlikely((unsigned long)out & alignmask)) { | ||
102 | unsigned long align = alignmask + 1; | ||
103 | unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm); | ||
104 | u8 *dst = (u8 *)ALIGN(addr, align) + | ||
105 | ALIGN(tfm->__crt_alg->cra_ctxsize, align); | ||
106 | |||
107 | digest->dia_final(tfm, dst); | ||
108 | memcpy(out, dst, digest->dia_digestsize); | ||
109 | } else | ||
110 | digest->dia_final(tfm, out); | ||
111 | |||
112 | return 0; | ||
113 | } | ||
114 | |||
115 | static int nosetkey(struct crypto_hash *tfm, const u8 *key, unsigned int keylen) | ||
116 | { | ||
117 | crypto_hash_clear_flags(tfm, CRYPTO_TFM_RES_MASK); | ||
118 | return -ENOSYS; | ||
119 | } | ||
120 | |||
121 | static int setkey(struct crypto_hash *hash, const u8 *key, unsigned int keylen) | ||
122 | { | ||
123 | struct crypto_tfm *tfm = crypto_hash_tfm(hash); | ||
124 | |||
125 | crypto_hash_clear_flags(hash, CRYPTO_TFM_RES_MASK); | ||
126 | return tfm->__crt_alg->cra_digest.dia_setkey(tfm, key, keylen); | ||
127 | } | ||
128 | |||
129 | static int digest(struct hash_desc *desc, | ||
130 | struct scatterlist *sg, unsigned int nbytes, u8 *out) | ||
131 | { | ||
132 | if (WARN_ON_ONCE(in_irq())) | ||
133 | return -EDEADLK; | ||
134 | |||
135 | init(desc); | ||
136 | update2(desc, sg, nbytes); | ||
137 | return final(desc, out); | ||
138 | } | ||
139 | |||
140 | int crypto_init_digest_ops(struct crypto_tfm *tfm) | ||
141 | { | ||
142 | struct hash_tfm *ops = &tfm->crt_hash; | ||
143 | struct digest_alg *dalg = &tfm->__crt_alg->cra_digest; | ||
144 | |||
145 | if (dalg->dia_digestsize > PAGE_SIZE / 8) | ||
146 | return -EINVAL; | ||
147 | |||
148 | ops->init = init; | ||
149 | ops->update = update; | ||
150 | ops->final = final; | ||
151 | ops->digest = digest; | ||
152 | ops->setkey = dalg->dia_setkey ? setkey : nosetkey; | ||
153 | ops->digestsize = dalg->dia_digestsize; | ||
154 | |||
155 | return 0; | ||
156 | } | ||
157 | |||
158 | void crypto_exit_digest_ops(struct crypto_tfm *tfm) | ||
159 | { | ||
160 | } | ||
161 | |||
162 | static int digest_async_nosetkey(struct crypto_ahash *tfm_async, const u8 *key, | ||
163 | unsigned int keylen) | ||
164 | { | ||
165 | crypto_ahash_clear_flags(tfm_async, CRYPTO_TFM_RES_MASK); | ||
166 | return -ENOSYS; | ||
167 | } | ||
168 | |||
169 | static int digest_async_setkey(struct crypto_ahash *tfm_async, const u8 *key, | ||
170 | unsigned int keylen) | ||
171 | { | ||
172 | struct crypto_tfm *tfm = crypto_ahash_tfm(tfm_async); | ||
173 | struct digest_alg *dalg = &tfm->__crt_alg->cra_digest; | ||
174 | |||
175 | crypto_ahash_clear_flags(tfm_async, CRYPTO_TFM_RES_MASK); | ||
176 | return dalg->dia_setkey(tfm, key, keylen); | ||
177 | } | ||
178 | |||
179 | static int digest_async_init(struct ahash_request *req) | ||
180 | { | ||
181 | struct crypto_tfm *tfm = req->base.tfm; | ||
182 | struct digest_alg *dalg = &tfm->__crt_alg->cra_digest; | ||
183 | |||
184 | dalg->dia_init(tfm); | ||
185 | return 0; | ||
186 | } | ||
187 | |||
188 | static int digest_async_update(struct ahash_request *req) | ||
189 | { | ||
190 | struct crypto_tfm *tfm = req->base.tfm; | ||
191 | struct hash_desc desc = { | ||
192 | .tfm = __crypto_hash_cast(tfm), | ||
193 | .flags = req->base.flags, | ||
194 | }; | ||
195 | |||
196 | update(&desc, req->src, req->nbytes); | ||
197 | return 0; | ||
198 | } | ||
199 | |||
200 | static int digest_async_final(struct ahash_request *req) | ||
201 | { | ||
202 | struct crypto_tfm *tfm = req->base.tfm; | ||
203 | struct hash_desc desc = { | ||
204 | .tfm = __crypto_hash_cast(tfm), | ||
205 | .flags = req->base.flags, | ||
206 | }; | ||
207 | |||
208 | final(&desc, req->result); | ||
209 | return 0; | ||
210 | } | ||
211 | |||
212 | static int digest_async_digest(struct ahash_request *req) | ||
213 | { | ||
214 | struct crypto_tfm *tfm = req->base.tfm; | ||
215 | struct hash_desc desc = { | ||
216 | .tfm = __crypto_hash_cast(tfm), | ||
217 | .flags = req->base.flags, | ||
218 | }; | ||
219 | |||
220 | return digest(&desc, req->src, req->nbytes, req->result); | ||
221 | } | ||
222 | |||
223 | int crypto_init_digest_ops_async(struct crypto_tfm *tfm) | ||
224 | { | ||
225 | struct ahash_tfm *crt = &tfm->crt_ahash; | ||
226 | struct digest_alg *dalg = &tfm->__crt_alg->cra_digest; | ||
227 | |||
228 | if (dalg->dia_digestsize > PAGE_SIZE / 8) | ||
229 | return -EINVAL; | ||
230 | |||
231 | crt->init = digest_async_init; | ||
232 | crt->update = digest_async_update; | ||
233 | crt->final = digest_async_final; | ||
234 | crt->digest = digest_async_digest; | ||
235 | crt->setkey = dalg->dia_setkey ? digest_async_setkey : | ||
236 | digest_async_nosetkey; | ||
237 | crt->digestsize = dalg->dia_digestsize; | ||
238 | |||
239 | return 0; | ||
240 | } | ||
diff --git a/crypto/hash.c b/crypto/hash.c deleted file mode 100644 index cb86b19fd105..000000000000 --- a/crypto/hash.c +++ /dev/null | |||
@@ -1,183 +0,0 @@ | |||
1 | /* | ||
2 | * Cryptographic Hash operations. | ||
3 | * | ||
4 | * Copyright (c) 2006 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 | #include <crypto/internal/hash.h> | ||
13 | #include <linux/errno.h> | ||
14 | #include <linux/kernel.h> | ||
15 | #include <linux/module.h> | ||
16 | #include <linux/slab.h> | ||
17 | #include <linux/seq_file.h> | ||
18 | |||
19 | #include "internal.h" | ||
20 | |||
21 | static unsigned int crypto_hash_ctxsize(struct crypto_alg *alg, u32 type, | ||
22 | u32 mask) | ||
23 | { | ||
24 | return alg->cra_ctxsize; | ||
25 | } | ||
26 | |||
27 | static int hash_setkey_unaligned(struct crypto_hash *crt, const u8 *key, | ||
28 | unsigned int keylen) | ||
29 | { | ||
30 | struct crypto_tfm *tfm = crypto_hash_tfm(crt); | ||
31 | struct hash_alg *alg = &tfm->__crt_alg->cra_hash; | ||
32 | unsigned long alignmask = crypto_hash_alignmask(crt); | ||
33 | int ret; | ||
34 | u8 *buffer, *alignbuffer; | ||
35 | unsigned long absize; | ||
36 | |||
37 | absize = keylen + alignmask; | ||
38 | buffer = kmalloc(absize, GFP_ATOMIC); | ||
39 | if (!buffer) | ||
40 | return -ENOMEM; | ||
41 | |||
42 | alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); | ||
43 | memcpy(alignbuffer, key, keylen); | ||
44 | ret = alg->setkey(crt, alignbuffer, keylen); | ||
45 | memset(alignbuffer, 0, keylen); | ||
46 | kfree(buffer); | ||
47 | return ret; | ||
48 | } | ||
49 | |||
50 | static int hash_setkey(struct crypto_hash *crt, const u8 *key, | ||
51 | unsigned int keylen) | ||
52 | { | ||
53 | struct crypto_tfm *tfm = crypto_hash_tfm(crt); | ||
54 | struct hash_alg *alg = &tfm->__crt_alg->cra_hash; | ||
55 | unsigned long alignmask = crypto_hash_alignmask(crt); | ||
56 | |||
57 | if ((unsigned long)key & alignmask) | ||
58 | return hash_setkey_unaligned(crt, key, keylen); | ||
59 | |||
60 | return alg->setkey(crt, key, keylen); | ||
61 | } | ||
62 | |||
63 | static int hash_async_setkey(struct crypto_ahash *tfm_async, const u8 *key, | ||
64 | unsigned int keylen) | ||
65 | { | ||
66 | struct crypto_tfm *tfm = crypto_ahash_tfm(tfm_async); | ||
67 | struct crypto_hash *tfm_hash = __crypto_hash_cast(tfm); | ||
68 | struct hash_alg *alg = &tfm->__crt_alg->cra_hash; | ||
69 | |||
70 | return alg->setkey(tfm_hash, key, keylen); | ||
71 | } | ||
72 | |||
73 | static int hash_async_init(struct ahash_request *req) | ||
74 | { | ||
75 | struct crypto_tfm *tfm = req->base.tfm; | ||
76 | struct hash_alg *alg = &tfm->__crt_alg->cra_hash; | ||
77 | struct hash_desc desc = { | ||
78 | .tfm = __crypto_hash_cast(tfm), | ||
79 | .flags = req->base.flags, | ||
80 | }; | ||
81 | |||
82 | return alg->init(&desc); | ||
83 | } | ||
84 | |||
85 | static int hash_async_update(struct ahash_request *req) | ||
86 | { | ||
87 | struct crypto_tfm *tfm = req->base.tfm; | ||
88 | struct hash_alg *alg = &tfm->__crt_alg->cra_hash; | ||
89 | struct hash_desc desc = { | ||
90 | .tfm = __crypto_hash_cast(tfm), | ||
91 | .flags = req->base.flags, | ||
92 | }; | ||
93 | |||
94 | return alg->update(&desc, req->src, req->nbytes); | ||
95 | } | ||
96 | |||
97 | static int hash_async_final(struct ahash_request *req) | ||
98 | { | ||
99 | struct crypto_tfm *tfm = req->base.tfm; | ||
100 | struct hash_alg *alg = &tfm->__crt_alg->cra_hash; | ||
101 | struct hash_desc desc = { | ||
102 | .tfm = __crypto_hash_cast(tfm), | ||
103 | .flags = req->base.flags, | ||
104 | }; | ||
105 | |||
106 | return alg->final(&desc, req->result); | ||
107 | } | ||
108 | |||
109 | static int hash_async_digest(struct ahash_request *req) | ||
110 | { | ||
111 | struct crypto_tfm *tfm = req->base.tfm; | ||
112 | struct hash_alg *alg = &tfm->__crt_alg->cra_hash; | ||
113 | struct hash_desc desc = { | ||
114 | .tfm = __crypto_hash_cast(tfm), | ||
115 | .flags = req->base.flags, | ||
116 | }; | ||
117 | |||
118 | return alg->digest(&desc, req->src, req->nbytes, req->result); | ||
119 | } | ||
120 | |||
121 | static int crypto_init_hash_ops_async(struct crypto_tfm *tfm) | ||
122 | { | ||
123 | struct ahash_tfm *crt = &tfm->crt_ahash; | ||
124 | struct hash_alg *alg = &tfm->__crt_alg->cra_hash; | ||
125 | |||
126 | crt->init = hash_async_init; | ||
127 | crt->update = hash_async_update; | ||
128 | crt->final = hash_async_final; | ||
129 | crt->digest = hash_async_digest; | ||
130 | crt->setkey = hash_async_setkey; | ||
131 | crt->digestsize = alg->digestsize; | ||
132 | |||
133 | return 0; | ||
134 | } | ||
135 | |||
136 | static int crypto_init_hash_ops_sync(struct crypto_tfm *tfm) | ||
137 | { | ||
138 | struct hash_tfm *crt = &tfm->crt_hash; | ||
139 | struct hash_alg *alg = &tfm->__crt_alg->cra_hash; | ||
140 | |||
141 | crt->init = alg->init; | ||
142 | crt->update = alg->update; | ||
143 | crt->final = alg->final; | ||
144 | crt->digest = alg->digest; | ||
145 | crt->setkey = hash_setkey; | ||
146 | crt->digestsize = alg->digestsize; | ||
147 | |||
148 | return 0; | ||
149 | } | ||
150 | |||
151 | static int crypto_init_hash_ops(struct crypto_tfm *tfm, u32 type, u32 mask) | ||
152 | { | ||
153 | struct hash_alg *alg = &tfm->__crt_alg->cra_hash; | ||
154 | |||
155 | if (alg->digestsize > PAGE_SIZE / 8) | ||
156 | return -EINVAL; | ||
157 | |||
158 | if ((mask & CRYPTO_ALG_TYPE_HASH_MASK) != CRYPTO_ALG_TYPE_HASH_MASK) | ||
159 | return crypto_init_hash_ops_async(tfm); | ||
160 | else | ||
161 | return crypto_init_hash_ops_sync(tfm); | ||
162 | } | ||
163 | |||
164 | static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg) | ||
165 | __attribute__ ((unused)); | ||
166 | static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg) | ||
167 | { | ||
168 | seq_printf(m, "type : hash\n"); | ||
169 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); | ||
170 | seq_printf(m, "digestsize : %u\n", alg->cra_hash.digestsize); | ||
171 | } | ||
172 | |||
173 | const struct crypto_type crypto_hash_type = { | ||
174 | .ctxsize = crypto_hash_ctxsize, | ||
175 | .init = crypto_init_hash_ops, | ||
176 | #ifdef CONFIG_PROC_FS | ||
177 | .show = crypto_hash_show, | ||
178 | #endif | ||
179 | }; | ||
180 | EXPORT_SYMBOL_GPL(crypto_hash_type); | ||
181 | |||
182 | MODULE_LICENSE("GPL"); | ||
183 | MODULE_DESCRIPTION("Generic cryptographic hash type"); | ||