aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/sha512_generic.c
diff options
context:
space:
mode:
authorAdrian-Ken Rueegsegger <ken@codelabs.ch>2008-12-17 00:49:02 -0500
committerHerbert Xu <herbert@gondor.apana.org.au>2008-12-24 19:02:27 -0500
commitbd9d20dba182ce4541b16b083eccd30fb252b9f4 (patch)
tree06ddab4f1753b4d7525dc0d40c9b3c7abb0985cd /crypto/sha512_generic.c
parentf9e2bca6c22d75a289a349f869701214d63b5060 (diff)
crypto: sha512 - Switch to shash
This patch changes sha512 and sha384 to the new shash interface. Signed-off-by: Adrian-Ken Rueegsegger <ken@codelabs.ch> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto/sha512_generic.c')
-rw-r--r--crypto/sha512_generic.c112
1 files changed, 59 insertions, 53 deletions
diff --git a/crypto/sha512_generic.c b/crypto/sha512_generic.c
index cb85516d3a78..3bea38d12242 100644
--- a/crypto/sha512_generic.c
+++ b/crypto/sha512_generic.c
@@ -10,7 +10,7 @@
10 * later version. 10 * later version.
11 * 11 *
12 */ 12 */
13 13#include <crypto/internal/hash.h>
14#include <linux/kernel.h> 14#include <linux/kernel.h>
15#include <linux/module.h> 15#include <linux/module.h>
16#include <linux/mm.h> 16#include <linux/mm.h>
@@ -138,10 +138,10 @@ sha512_transform(u64 *state, const u8 *input)
138 put_cpu_var(msg_schedule); 138 put_cpu_var(msg_schedule);
139} 139}
140 140
141static void 141static int
142sha512_init(struct crypto_tfm *tfm) 142sha512_init(struct shash_desc *desc)
143{ 143{
144 struct sha512_ctx *sctx = crypto_tfm_ctx(tfm); 144 struct sha512_ctx *sctx = shash_desc_ctx(desc);
145 sctx->state[0] = SHA512_H0; 145 sctx->state[0] = SHA512_H0;
146 sctx->state[1] = SHA512_H1; 146 sctx->state[1] = SHA512_H1;
147 sctx->state[2] = SHA512_H2; 147 sctx->state[2] = SHA512_H2;
@@ -151,12 +151,14 @@ sha512_init(struct crypto_tfm *tfm)
151 sctx->state[6] = SHA512_H6; 151 sctx->state[6] = SHA512_H6;
152 sctx->state[7] = SHA512_H7; 152 sctx->state[7] = SHA512_H7;
153 sctx->count[0] = sctx->count[1] = sctx->count[2] = sctx->count[3] = 0; 153 sctx->count[0] = sctx->count[1] = sctx->count[2] = sctx->count[3] = 0;
154
155 return 0;
154} 156}
155 157
156static void 158static int
157sha384_init(struct crypto_tfm *tfm) 159sha384_init(struct shash_desc *desc)
158{ 160{
159 struct sha512_ctx *sctx = crypto_tfm_ctx(tfm); 161 struct sha512_ctx *sctx = shash_desc_ctx(desc);
160 sctx->state[0] = SHA384_H0; 162 sctx->state[0] = SHA384_H0;
161 sctx->state[1] = SHA384_H1; 163 sctx->state[1] = SHA384_H1;
162 sctx->state[2] = SHA384_H2; 164 sctx->state[2] = SHA384_H2;
@@ -166,12 +168,14 @@ sha384_init(struct crypto_tfm *tfm)
166 sctx->state[6] = SHA384_H6; 168 sctx->state[6] = SHA384_H6;
167 sctx->state[7] = SHA384_H7; 169 sctx->state[7] = SHA384_H7;
168 sctx->count[0] = sctx->count[1] = sctx->count[2] = sctx->count[3] = 0; 170 sctx->count[0] = sctx->count[1] = sctx->count[2] = sctx->count[3] = 0;
171
172 return 0;
169} 173}
170 174
171static void 175static int
172sha512_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len) 176sha512_update(struct shash_desc *desc, const u8 *data, unsigned int len)
173{ 177{
174 struct sha512_ctx *sctx = crypto_tfm_ctx(tfm); 178 struct sha512_ctx *sctx = shash_desc_ctx(desc);
175 179
176 unsigned int i, index, part_len; 180 unsigned int i, index, part_len;
177 181
@@ -203,12 +207,14 @@ sha512_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
203 207
204 /* Buffer remaining input */ 208 /* Buffer remaining input */
205 memcpy(&sctx->buf[index], &data[i], len - i); 209 memcpy(&sctx->buf[index], &data[i], len - i);
210
211 return 0;
206} 212}
207 213
208static void 214static int
209sha512_final(struct crypto_tfm *tfm, u8 *hash) 215sha512_final(struct shash_desc *desc, u8 *hash)
210{ 216{
211 struct sha512_ctx *sctx = crypto_tfm_ctx(tfm); 217 struct sha512_ctx *sctx = shash_desc_ctx(desc);
212 static u8 padding[128] = { 0x80, }; 218 static u8 padding[128] = { 0x80, };
213 __be64 *dst = (__be64 *)hash; 219 __be64 *dst = (__be64 *)hash;
214 __be32 bits[4]; 220 __be32 bits[4];
@@ -224,10 +230,10 @@ sha512_final(struct crypto_tfm *tfm, u8 *hash)
224 /* Pad out to 112 mod 128. */ 230 /* Pad out to 112 mod 128. */
225 index = (sctx->count[0] >> 3) & 0x7f; 231 index = (sctx->count[0] >> 3) & 0x7f;
226 pad_len = (index < 112) ? (112 - index) : ((128+112) - index); 232 pad_len = (index < 112) ? (112 - index) : ((128+112) - index);
227 sha512_update(tfm, padding, pad_len); 233 sha512_update(desc, padding, pad_len);
228 234
229 /* Append length (before padding) */ 235 /* Append length (before padding) */
230 sha512_update(tfm, (const u8 *)bits, sizeof(bits)); 236 sha512_update(desc, (const u8 *)bits, sizeof(bits));
231 237
232 /* Store state in digest */ 238 /* Store state in digest */
233 for (i = 0; i < 8; i++) 239 for (i = 0; i < 8; i++)
@@ -235,66 +241,66 @@ sha512_final(struct crypto_tfm *tfm, u8 *hash)
235 241
236 /* Zeroize sensitive information. */ 242 /* Zeroize sensitive information. */
237 memset(sctx, 0, sizeof(struct sha512_ctx)); 243 memset(sctx, 0, sizeof(struct sha512_ctx));
244
245 return 0;
238} 246}
239 247
240static void sha384_final(struct crypto_tfm *tfm, u8 *hash) 248static int sha384_final(struct shash_desc *desc, u8 *hash)
241{ 249{
242 u8 D[64]; 250 u8 D[64];
243 251
244 sha512_final(tfm, D); 252 sha512_final(desc, D);
245 253
246 memcpy(hash, D, 48); 254 memcpy(hash, D, 48);
247 memset(D, 0, 64); 255 memset(D, 0, 64);
256
257 return 0;
248} 258}
249 259
250static struct crypto_alg sha512 = { 260static struct shash_alg sha512 = {
251 .cra_name = "sha512", 261 .digestsize = SHA512_DIGEST_SIZE,
252 .cra_flags = CRYPTO_ALG_TYPE_DIGEST, 262 .init = sha512_init,
253 .cra_blocksize = SHA512_BLOCK_SIZE, 263 .update = sha512_update,
254 .cra_ctxsize = sizeof(struct sha512_ctx), 264 .final = sha512_final,
255 .cra_module = THIS_MODULE, 265 .descsize = sizeof(struct sha512_ctx),
256 .cra_alignmask = 3, 266 .base = {
257 .cra_list = LIST_HEAD_INIT(sha512.cra_list), 267 .cra_name = "sha512",
258 .cra_u = { .digest = { 268 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
259 .dia_digestsize = SHA512_DIGEST_SIZE, 269 .cra_blocksize = SHA512_BLOCK_SIZE,
260 .dia_init = sha512_init, 270 .cra_module = THIS_MODULE,
261 .dia_update = sha512_update, 271 }
262 .dia_final = sha512_final }
263 }
264}; 272};
265 273
266static struct crypto_alg sha384 = { 274static struct shash_alg sha384 = {
267 .cra_name = "sha384", 275 .digestsize = SHA384_DIGEST_SIZE,
268 .cra_flags = CRYPTO_ALG_TYPE_DIGEST, 276 .init = sha384_init,
269 .cra_blocksize = SHA384_BLOCK_SIZE, 277 .update = sha512_update,
270 .cra_ctxsize = sizeof(struct sha512_ctx), 278 .final = sha384_final,
271 .cra_alignmask = 3, 279 .descsize = sizeof(struct sha512_ctx),
272 .cra_module = THIS_MODULE, 280 .base = {
273 .cra_list = LIST_HEAD_INIT(sha384.cra_list), 281 .cra_name = "sha384",
274 .cra_u = { .digest = { 282 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
275 .dia_digestsize = SHA384_DIGEST_SIZE, 283 .cra_blocksize = SHA384_BLOCK_SIZE,
276 .dia_init = sha384_init, 284 .cra_module = THIS_MODULE,
277 .dia_update = sha512_update, 285 }
278 .dia_final = sha384_final }
279 }
280}; 286};
281 287
282static int __init sha512_generic_mod_init(void) 288static int __init sha512_generic_mod_init(void)
283{ 289{
284 int ret = 0; 290 int ret = 0;
285 291
286 if ((ret = crypto_register_alg(&sha384)) < 0) 292 if ((ret = crypto_register_shash(&sha384)) < 0)
287 goto out; 293 goto out;
288 if ((ret = crypto_register_alg(&sha512)) < 0) 294 if ((ret = crypto_register_shash(&sha512)) < 0)
289 crypto_unregister_alg(&sha384); 295 crypto_unregister_shash(&sha384);
290out: 296out:
291 return ret; 297 return ret;
292} 298}
293 299
294static void __exit sha512_generic_mod_fini(void) 300static void __exit sha512_generic_mod_fini(void)
295{ 301{
296 crypto_unregister_alg(&sha384); 302 crypto_unregister_shash(&sha384);
297 crypto_unregister_alg(&sha512); 303 crypto_unregister_shash(&sha512);
298} 304}
299 305
300module_init(sha512_generic_mod_init); 306module_init(sha512_generic_mod_init);