]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Cryptographic API. | |
3 | * | |
4 | * Digest operations. | |
5 | * | |
6 | * Copyright (c) 2002 James Morris <[email protected]> | |
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 | |
1da177e4 LT |
15 | #include <linux/mm.h> |
16 | #include <linux/errno.h> | |
17 | #include <linux/highmem.h> | |
055bcee3 HX |
18 | #include <linux/module.h> |
19 | #include <linux/scatterlist.h> | |
20 | ||
1da177e4 | 21 | #include "internal.h" |
055bcee3 | 22 | #include "scatterwalk.h" |
1da177e4 | 23 | |
055bcee3 | 24 | void crypto_digest_init(struct crypto_tfm *tfm) |
1da177e4 | 25 | { |
055bcee3 HX |
26 | struct crypto_hash *hash = crypto_hash_cast(tfm); |
27 | struct hash_desc desc = { .tfm = hash, .flags = tfm->crt_flags }; | |
28 | ||
29 | crypto_hash_init(&desc); | |
1da177e4 | 30 | } |
055bcee3 | 31 | EXPORT_SYMBOL_GPL(crypto_digest_init); |
1da177e4 | 32 | |
055bcee3 HX |
33 | void crypto_digest_update(struct crypto_tfm *tfm, |
34 | struct scatterlist *sg, unsigned int nsg) | |
1da177e4 | 35 | { |
055bcee3 HX |
36 | struct crypto_hash *hash = crypto_hash_cast(tfm); |
37 | struct hash_desc desc = { .tfm = hash, .flags = tfm->crt_flags }; | |
38 | unsigned int nbytes = 0; | |
1da177e4 | 39 | unsigned int i; |
055bcee3 HX |
40 | |
41 | for (i = 0; i < nsg; i++) | |
42 | nbytes += sg[i].length; | |
43 | ||
44 | crypto_hash_update(&desc, sg, nbytes); | |
45 | } | |
46 | EXPORT_SYMBOL_GPL(crypto_digest_update); | |
47 | ||
48 | void crypto_digest_final(struct crypto_tfm *tfm, u8 *out) | |
49 | { | |
50 | struct crypto_hash *hash = crypto_hash_cast(tfm); | |
51 | struct hash_desc desc = { .tfm = hash, .flags = tfm->crt_flags }; | |
52 | ||
53 | crypto_hash_final(&desc, out); | |
54 | } | |
55 | EXPORT_SYMBOL_GPL(crypto_digest_final); | |
56 | ||
57 | void crypto_digest_digest(struct crypto_tfm *tfm, | |
58 | struct scatterlist *sg, unsigned int nsg, u8 *out) | |
59 | { | |
60 | struct crypto_hash *hash = crypto_hash_cast(tfm); | |
61 | struct hash_desc desc = { .tfm = hash, .flags = tfm->crt_flags }; | |
62 | unsigned int nbytes = 0; | |
63 | unsigned int i; | |
64 | ||
65 | for (i = 0; i < nsg; i++) | |
66 | nbytes += sg[i].length; | |
67 | ||
68 | crypto_hash_digest(&desc, sg, nbytes, out); | |
69 | } | |
70 | EXPORT_SYMBOL_GPL(crypto_digest_digest); | |
71 | ||
72 | static int init(struct hash_desc *desc) | |
73 | { | |
74 | struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm); | |
75 | ||
76 | tfm->__crt_alg->cra_digest.dia_init(tfm); | |
77 | return 0; | |
78 | } | |
79 | ||
80 | static int update(struct hash_desc *desc, | |
81 | struct scatterlist *sg, unsigned int nbytes) | |
82 | { | |
83 | struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm); | |
e1147d8f | 84 | unsigned int alignmask = crypto_tfm_alg_alignmask(tfm); |
1da177e4 | 85 | |
055bcee3 HX |
86 | if (!nbytes) |
87 | return 0; | |
88 | ||
89 | for (;;) { | |
90 | struct page *pg = sg->page; | |
91 | unsigned int offset = sg->offset; | |
92 | unsigned int l = sg->length; | |
1da177e4 | 93 | |
055bcee3 HX |
94 | if (unlikely(l > nbytes)) |
95 | l = nbytes; | |
96 | nbytes -= l; | |
1da177e4 LT |
97 | |
98 | do { | |
99 | unsigned int bytes_from_page = min(l, ((unsigned int) | |
100 | (PAGE_SIZE)) - | |
101 | offset); | |
e1147d8f AN |
102 | char *src = crypto_kmap(pg, 0); |
103 | char *p = src + offset; | |
1da177e4 | 104 | |
e1147d8f AN |
105 | if (unlikely(offset & alignmask)) { |
106 | unsigned int bytes = | |
107 | alignmask + 1 - (offset & alignmask); | |
108 | bytes = min(bytes, bytes_from_page); | |
6c2bb98b HX |
109 | tfm->__crt_alg->cra_digest.dia_update(tfm, p, |
110 | bytes); | |
e1147d8f AN |
111 | p += bytes; |
112 | bytes_from_page -= bytes; | |
113 | l -= bytes; | |
114 | } | |
6c2bb98b HX |
115 | tfm->__crt_alg->cra_digest.dia_update(tfm, p, |
116 | bytes_from_page); | |
e1147d8f | 117 | crypto_kunmap(src, 0); |
055bcee3 | 118 | crypto_yield(desc->flags); |
1da177e4 LT |
119 | offset = 0; |
120 | pg++; | |
121 | l -= bytes_from_page; | |
122 | } while (l > 0); | |
055bcee3 HX |
123 | |
124 | if (!nbytes) | |
125 | break; | |
126 | sg = sg_next(sg); | |
1da177e4 | 127 | } |
055bcee3 HX |
128 | |
129 | return 0; | |
1da177e4 LT |
130 | } |
131 | ||
055bcee3 | 132 | static int final(struct hash_desc *desc, u8 *out) |
1da177e4 | 133 | { |
055bcee3 | 134 | struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm); |
e1147d8f | 135 | unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); |
ee756416 HX |
136 | struct digest_alg *digest = &tfm->__crt_alg->cra_digest; |
137 | ||
e1147d8f | 138 | if (unlikely((unsigned long)out & alignmask)) { |
ee756416 HX |
139 | unsigned long align = alignmask + 1; |
140 | unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm); | |
141 | u8 *dst = (u8 *)ALIGN(addr, align) + | |
142 | ALIGN(tfm->__crt_alg->cra_ctxsize, align); | |
143 | ||
144 | digest->dia_final(tfm, dst); | |
145 | memcpy(out, dst, digest->dia_digestsize); | |
e1147d8f | 146 | } else |
ee756416 | 147 | digest->dia_final(tfm, out); |
055bcee3 HX |
148 | |
149 | return 0; | |
1da177e4 LT |
150 | } |
151 | ||
055bcee3 | 152 | static int nosetkey(struct crypto_hash *tfm, const u8 *key, unsigned int keylen) |
560c06ae | 153 | { |
055bcee3 | 154 | crypto_hash_clear_flags(tfm, CRYPTO_TFM_RES_MASK); |
560c06ae HX |
155 | return -ENOSYS; |
156 | } | |
157 | ||
055bcee3 | 158 | static int setkey(struct crypto_hash *hash, const u8 *key, unsigned int keylen) |
1da177e4 | 159 | { |
055bcee3 HX |
160 | struct crypto_tfm *tfm = crypto_hash_tfm(hash); |
161 | ||
162 | crypto_hash_clear_flags(hash, CRYPTO_TFM_RES_MASK); | |
560c06ae | 163 | return tfm->__crt_alg->cra_digest.dia_setkey(tfm, key, keylen); |
1da177e4 LT |
164 | } |
165 | ||
055bcee3 HX |
166 | static int digest(struct hash_desc *desc, |
167 | struct scatterlist *sg, unsigned int nbytes, u8 *out) | |
1da177e4 | 168 | { |
055bcee3 HX |
169 | init(desc); |
170 | update(desc, sg, nbytes); | |
171 | return final(desc, out); | |
1da177e4 LT |
172 | } |
173 | ||
174 | int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags) | |
175 | { | |
176 | return flags ? -EINVAL : 0; | |
177 | } | |
178 | ||
179 | int crypto_init_digest_ops(struct crypto_tfm *tfm) | |
180 | { | |
055bcee3 | 181 | struct hash_tfm *ops = &tfm->crt_hash; |
560c06ae | 182 | struct digest_alg *dalg = &tfm->__crt_alg->cra_digest; |
055bcee3 HX |
183 | |
184 | if (dalg->dia_digestsize > crypto_tfm_alg_blocksize(tfm)) | |
185 | return -EINVAL; | |
1da177e4 | 186 | |
055bcee3 HX |
187 | ops->init = init; |
188 | ops->update = update; | |
189 | ops->final = final; | |
190 | ops->digest = digest; | |
191 | ops->setkey = dalg->dia_setkey ? setkey : nosetkey; | |
192 | ops->digestsize = dalg->dia_digestsize; | |
1da177e4 | 193 | |
8425165d | 194 | return 0; |
1da177e4 LT |
195 | } |
196 | ||
197 | void crypto_exit_digest_ops(struct crypto_tfm *tfm) | |
198 | { | |
1da177e4 | 199 | } |