Merge branches 'release' and 'menlo' into release
[linux-2.6-block.git] / crypto / digest.c
CommitLineData
1da177e4
LT
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 */
055bcee3 14
42c271c6 15#include <crypto/scatterwalk.h>
1da177e4
LT
16#include <linux/mm.h>
17#include <linux/errno.h>
fb469840 18#include <linux/hardirq.h>
1da177e4 19#include <linux/highmem.h>
fb469840 20#include <linux/kernel.h>
055bcee3
HX
21#include <linux/module.h>
22#include <linux/scatterlist.h>
23
055bcee3
HX
24static int init(struct hash_desc *desc)
25{
26 struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm);
27
28 tfm->__crt_alg->cra_digest.dia_init(tfm);
29 return 0;
30}
31
fb469840
HX
32static int update2(struct hash_desc *desc,
33 struct scatterlist *sg, unsigned int nbytes)
055bcee3
HX
34{
35 struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm);
e1147d8f 36 unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
1da177e4 37
055bcee3
HX
38 if (!nbytes)
39 return 0;
40
41 for (;;) {
78c2f0b8 42 struct page *pg = sg_page(sg);
055bcee3
HX
43 unsigned int offset = sg->offset;
44 unsigned int l = sg->length;
1da177e4 45
055bcee3
HX
46 if (unlikely(l > nbytes))
47 l = nbytes;
48 nbytes -= l;
1da177e4
LT
49
50 do {
51 unsigned int bytes_from_page = min(l, ((unsigned int)
52 (PAGE_SIZE)) -
53 offset);
e1147d8f
AN
54 char *src = crypto_kmap(pg, 0);
55 char *p = src + offset;
1da177e4 56
e1147d8f
AN
57 if (unlikely(offset & alignmask)) {
58 unsigned int bytes =
59 alignmask + 1 - (offset & alignmask);
60 bytes = min(bytes, bytes_from_page);
6c2bb98b
HX
61 tfm->__crt_alg->cra_digest.dia_update(tfm, p,
62 bytes);
e1147d8f
AN
63 p += bytes;
64 bytes_from_page -= bytes;
65 l -= bytes;
66 }
6c2bb98b
HX
67 tfm->__crt_alg->cra_digest.dia_update(tfm, p,
68 bytes_from_page);
e1147d8f 69 crypto_kunmap(src, 0);
055bcee3 70 crypto_yield(desc->flags);
1da177e4
LT
71 offset = 0;
72 pg++;
73 l -= bytes_from_page;
74 } while (l > 0);
055bcee3
HX
75
76 if (!nbytes)
77 break;
b2ab4a57 78 sg = scatterwalk_sg_next(sg);
1da177e4 79 }
055bcee3
HX
80
81 return 0;
1da177e4
LT
82}
83
fb469840
HX
84static int update(struct hash_desc *desc,
85 struct scatterlist *sg, unsigned int nbytes)
86{
87 if (WARN_ON_ONCE(in_irq()))
88 return -EDEADLK;
89 return update2(desc, sg, nbytes);
90}
91
055bcee3 92static int final(struct hash_desc *desc, u8 *out)
1da177e4 93{
055bcee3 94 struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm);
e1147d8f 95 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
ee756416
HX
96 struct digest_alg *digest = &tfm->__crt_alg->cra_digest;
97
e1147d8f 98 if (unlikely((unsigned long)out & alignmask)) {
ee756416
HX
99 unsigned long align = alignmask + 1;
100 unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm);
101 u8 *dst = (u8 *)ALIGN(addr, align) +
102 ALIGN(tfm->__crt_alg->cra_ctxsize, align);
103
104 digest->dia_final(tfm, dst);
105 memcpy(out, dst, digest->dia_digestsize);
e1147d8f 106 } else
ee756416 107 digest->dia_final(tfm, out);
055bcee3
HX
108
109 return 0;
1da177e4
LT
110}
111
055bcee3 112static int nosetkey(struct crypto_hash *tfm, const u8 *key, unsigned int keylen)
560c06ae 113{
055bcee3 114 crypto_hash_clear_flags(tfm, CRYPTO_TFM_RES_MASK);
560c06ae
HX
115 return -ENOSYS;
116}
117
055bcee3 118static int setkey(struct crypto_hash *hash, const u8 *key, unsigned int keylen)
1da177e4 119{
055bcee3
HX
120 struct crypto_tfm *tfm = crypto_hash_tfm(hash);
121
122 crypto_hash_clear_flags(hash, CRYPTO_TFM_RES_MASK);
560c06ae 123 return tfm->__crt_alg->cra_digest.dia_setkey(tfm, key, keylen);
1da177e4
LT
124}
125
055bcee3
HX
126static int digest(struct hash_desc *desc,
127 struct scatterlist *sg, unsigned int nbytes, u8 *out)
1da177e4 128{
fb469840
HX
129 if (WARN_ON_ONCE(in_irq()))
130 return -EDEADLK;
131
055bcee3 132 init(desc);
fb469840 133 update2(desc, sg, nbytes);
055bcee3 134 return final(desc, out);
1da177e4
LT
135}
136
1da177e4
LT
137int crypto_init_digest_ops(struct crypto_tfm *tfm)
138{
055bcee3 139 struct hash_tfm *ops = &tfm->crt_hash;
560c06ae 140 struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
055bcee3
HX
141
142 if (dalg->dia_digestsize > crypto_tfm_alg_blocksize(tfm))
143 return -EINVAL;
1da177e4 144
055bcee3
HX
145 ops->init = init;
146 ops->update = update;
147 ops->final = final;
148 ops->digest = digest;
149 ops->setkey = dalg->dia_setkey ? setkey : nosetkey;
150 ops->digestsize = dalg->dia_digestsize;
1da177e4 151
8425165d 152 return 0;
1da177e4
LT
153}
154
155void crypto_exit_digest_ops(struct crypto_tfm *tfm)
156{
1da177e4 157}