ibft: fix the display of a few fields in the NIC attribute structure in sysfs
[linux-2.6/mini2440.git] / crypto / shash.c
blob2ccc8b0076ce36ed7d67ff6202e44e90076d7ee2
1 /*
2 * Synchronous Cryptographic Hash operations.
4 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
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.
13 #include <crypto/scatterwalk.h>
14 #include <crypto/internal/hash.h>
15 #include <linux/err.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/seq_file.h>
21 #include "internal.h"
23 static const struct crypto_type crypto_shash_type;
25 static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
26 unsigned int keylen)
28 struct shash_alg *shash = crypto_shash_alg(tfm);
29 unsigned long alignmask = crypto_shash_alignmask(tfm);
30 unsigned long absize;
31 u8 *buffer, *alignbuffer;
32 int err;
34 absize = keylen + (alignmask & ~(CRYPTO_MINALIGN - 1));
35 buffer = kmalloc(absize, GFP_KERNEL);
36 if (!buffer)
37 return -ENOMEM;
39 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
40 memcpy(alignbuffer, key, keylen);
41 err = shash->setkey(tfm, alignbuffer, keylen);
42 memset(alignbuffer, 0, keylen);
43 kfree(buffer);
44 return err;
47 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
48 unsigned int keylen)
50 struct shash_alg *shash = crypto_shash_alg(tfm);
51 unsigned long alignmask = crypto_shash_alignmask(tfm);
53 if (!shash->setkey)
54 return -ENOSYS;
56 if ((unsigned long)key & alignmask)
57 return shash_setkey_unaligned(tfm, key, keylen);
59 return shash->setkey(tfm, key, keylen);
61 EXPORT_SYMBOL_GPL(crypto_shash_setkey);
63 static inline unsigned int shash_align_buffer_size(unsigned len,
64 unsigned long mask)
66 return len + (mask & ~(__alignof__(u8 __attribute__ ((aligned))) - 1));
69 static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
70 unsigned int len)
72 struct crypto_shash *tfm = desc->tfm;
73 struct shash_alg *shash = crypto_shash_alg(tfm);
74 unsigned long alignmask = crypto_shash_alignmask(tfm);
75 unsigned int unaligned_len = alignmask + 1 -
76 ((unsigned long)data & alignmask);
77 u8 buf[shash_align_buffer_size(unaligned_len, alignmask)]
78 __attribute__ ((aligned));
80 if (unaligned_len > len)
81 unaligned_len = len;
83 memcpy(buf, data, unaligned_len);
85 return shash->update(desc, buf, unaligned_len) ?:
86 shash->update(desc, data + unaligned_len, len - unaligned_len);
89 int crypto_shash_update(struct shash_desc *desc, const u8 *data,
90 unsigned int len)
92 struct crypto_shash *tfm = desc->tfm;
93 struct shash_alg *shash = crypto_shash_alg(tfm);
94 unsigned long alignmask = crypto_shash_alignmask(tfm);
96 if ((unsigned long)data & alignmask)
97 return shash_update_unaligned(desc, data, len);
99 return shash->update(desc, data, len);
101 EXPORT_SYMBOL_GPL(crypto_shash_update);
103 static int shash_final_unaligned(struct shash_desc *desc, u8 *out)
105 struct crypto_shash *tfm = desc->tfm;
106 unsigned long alignmask = crypto_shash_alignmask(tfm);
107 struct shash_alg *shash = crypto_shash_alg(tfm);
108 unsigned int ds = crypto_shash_digestsize(tfm);
109 u8 buf[shash_align_buffer_size(ds, alignmask)]
110 __attribute__ ((aligned));
111 int err;
113 err = shash->final(desc, buf);
114 memcpy(out, buf, ds);
115 return err;
118 int crypto_shash_final(struct shash_desc *desc, u8 *out)
120 struct crypto_shash *tfm = desc->tfm;
121 struct shash_alg *shash = crypto_shash_alg(tfm);
122 unsigned long alignmask = crypto_shash_alignmask(tfm);
124 if ((unsigned long)out & alignmask)
125 return shash_final_unaligned(desc, out);
127 return shash->final(desc, out);
129 EXPORT_SYMBOL_GPL(crypto_shash_final);
131 static int shash_finup_unaligned(struct shash_desc *desc, const u8 *data,
132 unsigned int len, u8 *out)
134 return crypto_shash_update(desc, data, len) ?:
135 crypto_shash_final(desc, out);
138 int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
139 unsigned int len, u8 *out)
141 struct crypto_shash *tfm = desc->tfm;
142 struct shash_alg *shash = crypto_shash_alg(tfm);
143 unsigned long alignmask = crypto_shash_alignmask(tfm);
145 if (((unsigned long)data | (unsigned long)out) & alignmask ||
146 !shash->finup)
147 return shash_finup_unaligned(desc, data, len, out);
149 return shash->finup(desc, data, len, out);
151 EXPORT_SYMBOL_GPL(crypto_shash_finup);
153 static int shash_digest_unaligned(struct shash_desc *desc, const u8 *data,
154 unsigned int len, u8 *out)
156 return crypto_shash_init(desc) ?:
157 crypto_shash_update(desc, data, len) ?:
158 crypto_shash_final(desc, out);
161 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
162 unsigned int len, u8 *out)
164 struct crypto_shash *tfm = desc->tfm;
165 struct shash_alg *shash = crypto_shash_alg(tfm);
166 unsigned long alignmask = crypto_shash_alignmask(tfm);
168 if (((unsigned long)data | (unsigned long)out) & alignmask ||
169 !shash->digest)
170 return shash_digest_unaligned(desc, data, len, out);
172 return shash->digest(desc, data, len, out);
174 EXPORT_SYMBOL_GPL(crypto_shash_digest);
176 int crypto_shash_import(struct shash_desc *desc, const u8 *in)
178 struct crypto_shash *tfm = desc->tfm;
179 struct shash_alg *alg = crypto_shash_alg(tfm);
181 memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(tfm));
183 if (alg->reinit)
184 alg->reinit(desc);
186 return 0;
188 EXPORT_SYMBOL_GPL(crypto_shash_import);
190 static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
191 unsigned int keylen)
193 struct crypto_shash **ctx = crypto_ahash_ctx(tfm);
195 return crypto_shash_setkey(*ctx, key, keylen);
198 static int shash_async_init(struct ahash_request *req)
200 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
201 struct shash_desc *desc = ahash_request_ctx(req);
203 desc->tfm = *ctx;
204 desc->flags = req->base.flags;
206 return crypto_shash_init(desc);
209 static int shash_async_update(struct ahash_request *req)
211 struct shash_desc *desc = ahash_request_ctx(req);
212 struct crypto_hash_walk walk;
213 int nbytes;
215 for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0;
216 nbytes = crypto_hash_walk_done(&walk, nbytes))
217 nbytes = crypto_shash_update(desc, walk.data, nbytes);
219 return nbytes;
222 static int shash_async_final(struct ahash_request *req)
224 return crypto_shash_final(ahash_request_ctx(req), req->result);
227 static int shash_async_digest(struct ahash_request *req)
229 struct scatterlist *sg = req->src;
230 unsigned int offset = sg->offset;
231 unsigned int nbytes = req->nbytes;
232 int err;
234 if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
235 struct crypto_shash **ctx =
236 crypto_ahash_ctx(crypto_ahash_reqtfm(req));
237 struct shash_desc *desc = ahash_request_ctx(req);
238 void *data;
240 desc->tfm = *ctx;
241 desc->flags = req->base.flags;
243 data = crypto_kmap(sg_page(sg), 0);
244 err = crypto_shash_digest(desc, data + offset, nbytes,
245 req->result);
246 crypto_kunmap(data, 0);
247 crypto_yield(desc->flags);
248 goto out;
251 err = shash_async_init(req);
252 if (err)
253 goto out;
255 err = shash_async_update(req);
256 if (err)
257 goto out;
259 err = shash_async_final(req);
261 out:
262 return err;
265 static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
267 struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
269 crypto_free_shash(*ctx);
272 static int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
274 struct crypto_alg *calg = tfm->__crt_alg;
275 struct shash_alg *alg = __crypto_shash_alg(calg);
276 struct ahash_tfm *crt = &tfm->crt_ahash;
277 struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
278 struct crypto_shash *shash;
280 if (!crypto_mod_get(calg))
281 return -EAGAIN;
283 shash = crypto_create_tfm(calg, &crypto_shash_type);
284 if (IS_ERR(shash)) {
285 crypto_mod_put(calg);
286 return PTR_ERR(shash);
289 *ctx = shash;
290 tfm->exit = crypto_exit_shash_ops_async;
292 crt->init = shash_async_init;
293 crt->update = shash_async_update;
294 crt->final = shash_async_final;
295 crt->digest = shash_async_digest;
296 crt->setkey = shash_async_setkey;
298 crt->digestsize = alg->digestsize;
299 crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
301 return 0;
304 static int shash_compat_setkey(struct crypto_hash *tfm, const u8 *key,
305 unsigned int keylen)
307 struct shash_desc *desc = crypto_hash_ctx(tfm);
309 return crypto_shash_setkey(desc->tfm, key, keylen);
312 static int shash_compat_init(struct hash_desc *hdesc)
314 struct shash_desc *desc = crypto_hash_ctx(hdesc->tfm);
316 desc->flags = hdesc->flags;
318 return crypto_shash_init(desc);
321 static int shash_compat_update(struct hash_desc *hdesc, struct scatterlist *sg,
322 unsigned int len)
324 struct shash_desc *desc = crypto_hash_ctx(hdesc->tfm);
325 struct crypto_hash_walk walk;
326 int nbytes;
328 for (nbytes = crypto_hash_walk_first_compat(hdesc, &walk, sg, len);
329 nbytes > 0; nbytes = crypto_hash_walk_done(&walk, nbytes))
330 nbytes = crypto_shash_update(desc, walk.data, nbytes);
332 return nbytes;
335 static int shash_compat_final(struct hash_desc *hdesc, u8 *out)
337 return crypto_shash_final(crypto_hash_ctx(hdesc->tfm), out);
340 static int shash_compat_digest(struct hash_desc *hdesc, struct scatterlist *sg,
341 unsigned int nbytes, u8 *out)
343 unsigned int offset = sg->offset;
344 int err;
346 if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
347 struct shash_desc *desc = crypto_hash_ctx(hdesc->tfm);
348 void *data;
350 desc->flags = hdesc->flags;
352 data = crypto_kmap(sg_page(sg), 0);
353 err = crypto_shash_digest(desc, data + offset, nbytes, out);
354 crypto_kunmap(data, 0);
355 crypto_yield(desc->flags);
356 goto out;
359 err = shash_compat_init(hdesc);
360 if (err)
361 goto out;
363 err = shash_compat_update(hdesc, sg, nbytes);
364 if (err)
365 goto out;
367 err = shash_compat_final(hdesc, out);
369 out:
370 return err;
373 static void crypto_exit_shash_ops_compat(struct crypto_tfm *tfm)
375 struct shash_desc *desc= crypto_tfm_ctx(tfm);
377 crypto_free_shash(desc->tfm);
380 static int crypto_init_shash_ops_compat(struct crypto_tfm *tfm)
382 struct hash_tfm *crt = &tfm->crt_hash;
383 struct crypto_alg *calg = tfm->__crt_alg;
384 struct shash_alg *alg = __crypto_shash_alg(calg);
385 struct shash_desc *desc = crypto_tfm_ctx(tfm);
386 struct crypto_shash *shash;
388 if (!crypto_mod_get(calg))
389 return -EAGAIN;
391 shash = crypto_create_tfm(calg, &crypto_shash_type);
392 if (IS_ERR(shash)) {
393 crypto_mod_put(calg);
394 return PTR_ERR(shash);
397 desc->tfm = shash;
398 tfm->exit = crypto_exit_shash_ops_compat;
400 crt->init = shash_compat_init;
401 crt->update = shash_compat_update;
402 crt->final = shash_compat_final;
403 crt->digest = shash_compat_digest;
404 crt->setkey = shash_compat_setkey;
406 crt->digestsize = alg->digestsize;
408 return 0;
411 static int crypto_init_shash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
413 switch (mask & CRYPTO_ALG_TYPE_MASK) {
414 case CRYPTO_ALG_TYPE_HASH_MASK:
415 return crypto_init_shash_ops_compat(tfm);
416 case CRYPTO_ALG_TYPE_AHASH_MASK:
417 return crypto_init_shash_ops_async(tfm);
420 return -EINVAL;
423 static unsigned int crypto_shash_ctxsize(struct crypto_alg *alg, u32 type,
424 u32 mask)
426 struct shash_alg *salg = __crypto_shash_alg(alg);
428 switch (mask & CRYPTO_ALG_TYPE_MASK) {
429 case CRYPTO_ALG_TYPE_HASH_MASK:
430 return sizeof(struct shash_desc) + salg->descsize;
431 case CRYPTO_ALG_TYPE_AHASH_MASK:
432 return sizeof(struct crypto_shash *);
435 return 0;
438 static int crypto_shash_init_tfm(struct crypto_tfm *tfm,
439 const struct crypto_type *frontend)
441 return 0;
444 static unsigned int crypto_shash_extsize(struct crypto_alg *alg,
445 const struct crypto_type *frontend)
447 return alg->cra_ctxsize;
450 static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
451 __attribute__ ((unused));
452 static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
454 struct shash_alg *salg = __crypto_shash_alg(alg);
456 seq_printf(m, "type : shash\n");
457 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
458 seq_printf(m, "digestsize : %u\n", salg->digestsize);
459 seq_printf(m, "descsize : %u\n", salg->descsize);
462 static const struct crypto_type crypto_shash_type = {
463 .ctxsize = crypto_shash_ctxsize,
464 .extsize = crypto_shash_extsize,
465 .init = crypto_init_shash_ops,
466 .init_tfm = crypto_shash_init_tfm,
467 #ifdef CONFIG_PROC_FS
468 .show = crypto_shash_show,
469 #endif
470 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
471 .maskset = CRYPTO_ALG_TYPE_MASK,
472 .type = CRYPTO_ALG_TYPE_SHASH,
473 .tfmsize = offsetof(struct crypto_shash, base),
476 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
477 u32 mask)
479 return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask);
481 EXPORT_SYMBOL_GPL(crypto_alloc_shash);
483 int crypto_register_shash(struct shash_alg *alg)
485 struct crypto_alg *base = &alg->base;
487 if (alg->digestsize > PAGE_SIZE / 8 ||
488 alg->descsize > PAGE_SIZE / 8)
489 return -EINVAL;
491 base->cra_type = &crypto_shash_type;
492 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
493 base->cra_flags |= CRYPTO_ALG_TYPE_SHASH;
495 return crypto_register_alg(base);
497 EXPORT_SYMBOL_GPL(crypto_register_shash);
499 int crypto_unregister_shash(struct shash_alg *alg)
501 return crypto_unregister_alg(&alg->base);
503 EXPORT_SYMBOL_GPL(crypto_unregister_shash);
505 MODULE_LICENSE("GPL");
506 MODULE_DESCRIPTION("Synchronous cryptographic hash type");