blob: 3b1b06b3dcf3e30cdb1f1dcf2d7b683c2f7f81f9 [file] [log] [blame]
Herbert Xu7b5a080b2008-08-31 15:47:27 +10001/*
2 * Synchronous Cryptographic Hash operations.
3 *
4 * Copyright (c) 2008 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
Herbert Xu3b2f6df2008-08-31 18:52:18 +100013#include <crypto/scatterwalk.h>
Herbert Xu7b5a080b2008-08-31 15:47:27 +100014#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>
20
Herbert Xu3b2f6df2008-08-31 18:52:18 +100021#include "internal.h"
22
Herbert Xu3f683d62009-02-18 16:56:59 +080023static const struct crypto_type crypto_shash_type;
24
Herbert Xu57cfe442009-07-11 22:17:39 +080025static int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
26 unsigned int keylen)
27{
28 return -ENOSYS;
29}
30
Herbert Xu7b5a080b2008-08-31 15:47:27 +100031static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
32 unsigned int keylen)
33{
34 struct shash_alg *shash = crypto_shash_alg(tfm);
35 unsigned long alignmask = crypto_shash_alignmask(tfm);
36 unsigned long absize;
37 u8 *buffer, *alignbuffer;
38 int err;
39
40 absize = keylen + (alignmask & ~(CRYPTO_MINALIGN - 1));
41 buffer = kmalloc(absize, GFP_KERNEL);
42 if (!buffer)
43 return -ENOMEM;
44
45 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
46 memcpy(alignbuffer, key, keylen);
47 err = shash->setkey(tfm, alignbuffer, keylen);
48 memset(alignbuffer, 0, keylen);
49 kfree(buffer);
50 return err;
51}
52
53int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
54 unsigned int keylen)
55{
56 struct shash_alg *shash = crypto_shash_alg(tfm);
57 unsigned long alignmask = crypto_shash_alignmask(tfm);
58
59 if ((unsigned long)key & alignmask)
60 return shash_setkey_unaligned(tfm, key, keylen);
61
62 return shash->setkey(tfm, key, keylen);
63}
64EXPORT_SYMBOL_GPL(crypto_shash_setkey);
65
66static inline unsigned int shash_align_buffer_size(unsigned len,
67 unsigned long mask)
68{
69 return len + (mask & ~(__alignof__(u8 __attribute__ ((aligned))) - 1));
70}
71
72static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
73 unsigned int len)
74{
75 struct crypto_shash *tfm = desc->tfm;
76 struct shash_alg *shash = crypto_shash_alg(tfm);
77 unsigned long alignmask = crypto_shash_alignmask(tfm);
78 unsigned int unaligned_len = alignmask + 1 -
79 ((unsigned long)data & alignmask);
80 u8 buf[shash_align_buffer_size(unaligned_len, alignmask)]
81 __attribute__ ((aligned));
82
Yehuda Sadehf4f68992009-03-27 13:03:51 +080083 if (unaligned_len > len)
84 unaligned_len = len;
85
Herbert Xu7b5a080b2008-08-31 15:47:27 +100086 memcpy(buf, data, unaligned_len);
87
88 return shash->update(desc, buf, unaligned_len) ?:
89 shash->update(desc, data + unaligned_len, len - unaligned_len);
90}
91
92int crypto_shash_update(struct shash_desc *desc, const u8 *data,
93 unsigned int len)
94{
95 struct crypto_shash *tfm = desc->tfm;
96 struct shash_alg *shash = crypto_shash_alg(tfm);
97 unsigned long alignmask = crypto_shash_alignmask(tfm);
98
99 if ((unsigned long)data & alignmask)
100 return shash_update_unaligned(desc, data, len);
101
102 return shash->update(desc, data, len);
103}
104EXPORT_SYMBOL_GPL(crypto_shash_update);
105
106static int shash_final_unaligned(struct shash_desc *desc, u8 *out)
107{
108 struct crypto_shash *tfm = desc->tfm;
109 unsigned long alignmask = crypto_shash_alignmask(tfm);
110 struct shash_alg *shash = crypto_shash_alg(tfm);
111 unsigned int ds = crypto_shash_digestsize(tfm);
112 u8 buf[shash_align_buffer_size(ds, alignmask)]
113 __attribute__ ((aligned));
114 int err;
115
116 err = shash->final(desc, buf);
117 memcpy(out, buf, ds);
118 return err;
119}
120
121int crypto_shash_final(struct shash_desc *desc, u8 *out)
122{
123 struct crypto_shash *tfm = desc->tfm;
124 struct shash_alg *shash = crypto_shash_alg(tfm);
125 unsigned long alignmask = crypto_shash_alignmask(tfm);
126
127 if ((unsigned long)out & alignmask)
128 return shash_final_unaligned(desc, out);
129
130 return shash->final(desc, out);
131}
132EXPORT_SYMBOL_GPL(crypto_shash_final);
133
134static int shash_finup_unaligned(struct shash_desc *desc, const u8 *data,
135 unsigned int len, u8 *out)
136{
137 return crypto_shash_update(desc, data, len) ?:
138 crypto_shash_final(desc, out);
139}
140
141int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
142 unsigned int len, u8 *out)
143{
144 struct crypto_shash *tfm = desc->tfm;
145 struct shash_alg *shash = crypto_shash_alg(tfm);
146 unsigned long alignmask = crypto_shash_alignmask(tfm);
147
Herbert Xu8267ada2009-07-09 20:36:44 +0800148 if (((unsigned long)data | (unsigned long)out) & alignmask)
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000149 return shash_finup_unaligned(desc, data, len, out);
150
151 return shash->finup(desc, data, len, out);
152}
153EXPORT_SYMBOL_GPL(crypto_shash_finup);
154
155static int shash_digest_unaligned(struct shash_desc *desc, const u8 *data,
156 unsigned int len, u8 *out)
157{
158 return crypto_shash_init(desc) ?:
Herbert Xuf88ad8d2009-07-08 23:32:08 +0800159 crypto_shash_finup(desc, data, len, out);
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000160}
161
162int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
163 unsigned int len, u8 *out)
164{
165 struct crypto_shash *tfm = desc->tfm;
166 struct shash_alg *shash = crypto_shash_alg(tfm);
167 unsigned long alignmask = crypto_shash_alignmask(tfm);
168
Herbert Xu8267ada2009-07-09 20:36:44 +0800169 if (((unsigned long)data | (unsigned long)out) & alignmask)
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000170 return shash_digest_unaligned(desc, data, len, out);
171
172 return shash->digest(desc, data, len, out);
173}
174EXPORT_SYMBOL_GPL(crypto_shash_digest);
175
Herbert Xu99d27e12009-07-09 20:30:57 +0800176static int shash_no_export(struct shash_desc *desc, void *out)
Herbert Xudec8b782008-11-02 21:38:11 +0800177{
Herbert Xu99d27e12009-07-09 20:30:57 +0800178 return -ENOSYS;
Herbert Xudec8b782008-11-02 21:38:11 +0800179}
Herbert Xu99d27e12009-07-09 20:30:57 +0800180
181static int shash_no_import(struct shash_desc *desc, const void *in)
182{
183 return -ENOSYS;
184}
Herbert Xudec8b782008-11-02 21:38:11 +0800185
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000186static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
187 unsigned int keylen)
188{
189 struct crypto_shash **ctx = crypto_ahash_ctx(tfm);
190
191 return crypto_shash_setkey(*ctx, key, keylen);
192}
193
194static int shash_async_init(struct ahash_request *req)
195{
196 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
197 struct shash_desc *desc = ahash_request_ctx(req);
198
199 desc->tfm = *ctx;
200 desc->flags = req->base.flags;
201
202 return crypto_shash_init(desc);
203}
204
Herbert Xu7eddf952009-07-12 21:25:20 +0800205int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc)
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000206{
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000207 struct crypto_hash_walk walk;
208 int nbytes;
209
210 for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0;
211 nbytes = crypto_hash_walk_done(&walk, nbytes))
212 nbytes = crypto_shash_update(desc, walk.data, nbytes);
213
214 return nbytes;
215}
Herbert Xu7eddf952009-07-12 21:25:20 +0800216EXPORT_SYMBOL_GPL(shash_ahash_update);
217
218static int shash_async_update(struct ahash_request *req)
219{
220 return shash_ahash_update(req, ahash_request_ctx(req));
221}
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000222
223static int shash_async_final(struct ahash_request *req)
224{
225 return crypto_shash_final(ahash_request_ctx(req), req->result);
226}
227
Herbert Xu7eddf952009-07-12 21:25:20 +0800228int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc)
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000229{
230 struct scatterlist *sg = req->src;
231 unsigned int offset = sg->offset;
232 unsigned int nbytes = req->nbytes;
233 int err;
234
235 if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000236 void *data;
237
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000238 data = crypto_kmap(sg_page(sg), 0);
239 err = crypto_shash_digest(desc, data + offset, nbytes,
240 req->result);
241 crypto_kunmap(data, 0);
242 crypto_yield(desc->flags);
Herbert Xu7eddf952009-07-12 21:25:20 +0800243 } else
244 err = crypto_shash_init(desc) ?:
245 shash_ahash_update(req, desc) ?:
246 crypto_shash_final(desc, req->result);
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000247
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000248 return err;
249}
Herbert Xu7eddf952009-07-12 21:25:20 +0800250EXPORT_SYMBOL_GPL(shash_ahash_digest);
251
252static int shash_async_digest(struct ahash_request *req)
253{
254 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
255 struct shash_desc *desc = ahash_request_ctx(req);
256
257 desc->tfm = *ctx;
258 desc->flags = req->base.flags;
259
260 return shash_ahash_digest(req, desc);
261}
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000262
263static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
264{
265 struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
266
267 crypto_free_shash(*ctx);
268}
269
270static int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
271{
272 struct crypto_alg *calg = tfm->__crt_alg;
273 struct shash_alg *alg = __crypto_shash_alg(calg);
274 struct ahash_tfm *crt = &tfm->crt_ahash;
275 struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
276 struct crypto_shash *shash;
277
278 if (!crypto_mod_get(calg))
279 return -EAGAIN;
280
Herbert Xu3f683d62009-02-18 16:56:59 +0800281 shash = crypto_create_tfm(calg, &crypto_shash_type);
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000282 if (IS_ERR(shash)) {
283 crypto_mod_put(calg);
284 return PTR_ERR(shash);
285 }
286
287 *ctx = shash;
288 tfm->exit = crypto_exit_shash_ops_async;
289
290 crt->init = shash_async_init;
291 crt->update = shash_async_update;
292 crt->final = shash_async_final;
293 crt->digest = shash_async_digest;
294 crt->setkey = shash_async_setkey;
295
296 crt->digestsize = alg->digestsize;
297 crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
298
299 return 0;
300}
301
Herbert Xu5f7082e2008-08-31 22:21:09 +1000302static int shash_compat_setkey(struct crypto_hash *tfm, const u8 *key,
303 unsigned int keylen)
304{
Herbert Xu113adef2009-07-14 12:50:12 +0800305 struct shash_desc **descp = crypto_hash_ctx(tfm);
306 struct shash_desc *desc = *descp;
Herbert Xu5f7082e2008-08-31 22:21:09 +1000307
308 return crypto_shash_setkey(desc->tfm, key, keylen);
309}
310
311static int shash_compat_init(struct hash_desc *hdesc)
312{
Herbert Xu113adef2009-07-14 12:50:12 +0800313 struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
314 struct shash_desc *desc = *descp;
Herbert Xu5f7082e2008-08-31 22:21:09 +1000315
316 desc->flags = hdesc->flags;
317
318 return crypto_shash_init(desc);
319}
320
321static int shash_compat_update(struct hash_desc *hdesc, struct scatterlist *sg,
322 unsigned int len)
323{
Herbert Xu113adef2009-07-14 12:50:12 +0800324 struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
325 struct shash_desc *desc = *descp;
Herbert Xu5f7082e2008-08-31 22:21:09 +1000326 struct crypto_hash_walk walk;
327 int nbytes;
328
329 for (nbytes = crypto_hash_walk_first_compat(hdesc, &walk, sg, len);
330 nbytes > 0; nbytes = crypto_hash_walk_done(&walk, nbytes))
331 nbytes = crypto_shash_update(desc, walk.data, nbytes);
332
333 return nbytes;
334}
335
336static int shash_compat_final(struct hash_desc *hdesc, u8 *out)
337{
Herbert Xu113adef2009-07-14 12:50:12 +0800338 struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
339
340 return crypto_shash_final(*descp, out);
Herbert Xu5f7082e2008-08-31 22:21:09 +1000341}
342
343static int shash_compat_digest(struct hash_desc *hdesc, struct scatterlist *sg,
344 unsigned int nbytes, u8 *out)
345{
346 unsigned int offset = sg->offset;
347 int err;
348
349 if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
Herbert Xu113adef2009-07-14 12:50:12 +0800350 struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
351 struct shash_desc *desc = *descp;
Herbert Xu5f7082e2008-08-31 22:21:09 +1000352 void *data;
353
354 desc->flags = hdesc->flags;
355
356 data = crypto_kmap(sg_page(sg), 0);
357 err = crypto_shash_digest(desc, data + offset, nbytes, out);
358 crypto_kunmap(data, 0);
359 crypto_yield(desc->flags);
360 goto out;
361 }
362
363 err = shash_compat_init(hdesc);
364 if (err)
365 goto out;
366
367 err = shash_compat_update(hdesc, sg, nbytes);
368 if (err)
369 goto out;
370
371 err = shash_compat_final(hdesc, out);
372
373out:
374 return err;
375}
376
377static void crypto_exit_shash_ops_compat(struct crypto_tfm *tfm)
378{
Herbert Xu113adef2009-07-14 12:50:12 +0800379 struct shash_desc **descp = crypto_tfm_ctx(tfm);
380 struct shash_desc *desc = *descp;
Herbert Xu5f7082e2008-08-31 22:21:09 +1000381
382 crypto_free_shash(desc->tfm);
Herbert Xu113adef2009-07-14 12:50:12 +0800383 kzfree(desc);
Herbert Xu5f7082e2008-08-31 22:21:09 +1000384}
385
386static int crypto_init_shash_ops_compat(struct crypto_tfm *tfm)
387{
388 struct hash_tfm *crt = &tfm->crt_hash;
389 struct crypto_alg *calg = tfm->__crt_alg;
390 struct shash_alg *alg = __crypto_shash_alg(calg);
Herbert Xu113adef2009-07-14 12:50:12 +0800391 struct shash_desc **descp = crypto_tfm_ctx(tfm);
Herbert Xu5f7082e2008-08-31 22:21:09 +1000392 struct crypto_shash *shash;
Herbert Xu113adef2009-07-14 12:50:12 +0800393 struct shash_desc *desc;
Herbert Xu5f7082e2008-08-31 22:21:09 +1000394
Adrian-Ken Rueegsegger4abfd732009-02-05 16:19:31 +1100395 if (!crypto_mod_get(calg))
396 return -EAGAIN;
397
Herbert Xu3f683d62009-02-18 16:56:59 +0800398 shash = crypto_create_tfm(calg, &crypto_shash_type);
Adrian-Ken Rueegsegger4abfd732009-02-05 16:19:31 +1100399 if (IS_ERR(shash)) {
400 crypto_mod_put(calg);
Herbert Xu5f7082e2008-08-31 22:21:09 +1000401 return PTR_ERR(shash);
Adrian-Ken Rueegsegger4abfd732009-02-05 16:19:31 +1100402 }
Herbert Xu5f7082e2008-08-31 22:21:09 +1000403
Herbert Xu113adef2009-07-14 12:50:12 +0800404 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(shash),
405 GFP_KERNEL);
406 if (!desc) {
407 crypto_free_shash(shash);
408 return -ENOMEM;
409 }
410
411 *descp = desc;
Herbert Xu5f7082e2008-08-31 22:21:09 +1000412 desc->tfm = shash;
413 tfm->exit = crypto_exit_shash_ops_compat;
414
415 crt->init = shash_compat_init;
416 crt->update = shash_compat_update;
417 crt->final = shash_compat_final;
418 crt->digest = shash_compat_digest;
419 crt->setkey = shash_compat_setkey;
420
421 crt->digestsize = alg->digestsize;
422
423 return 0;
424}
425
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000426static int crypto_init_shash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
427{
428 switch (mask & CRYPTO_ALG_TYPE_MASK) {
Herbert Xu5f7082e2008-08-31 22:21:09 +1000429 case CRYPTO_ALG_TYPE_HASH_MASK:
430 return crypto_init_shash_ops_compat(tfm);
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000431 case CRYPTO_ALG_TYPE_AHASH_MASK:
432 return crypto_init_shash_ops_async(tfm);
433 }
434
435 return -EINVAL;
436}
437
438static unsigned int crypto_shash_ctxsize(struct crypto_alg *alg, u32 type,
439 u32 mask)
440{
441 switch (mask & CRYPTO_ALG_TYPE_MASK) {
Herbert Xu5f7082e2008-08-31 22:21:09 +1000442 case CRYPTO_ALG_TYPE_HASH_MASK:
Herbert Xu113adef2009-07-14 12:50:12 +0800443 return sizeof(struct shash_desc *);
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000444 case CRYPTO_ALG_TYPE_AHASH_MASK:
445 return sizeof(struct crypto_shash *);
446 }
447
448 return 0;
449}
450
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000451static int crypto_shash_init_tfm(struct crypto_tfm *tfm,
452 const struct crypto_type *frontend)
453{
Herbert Xu113adef2009-07-14 12:50:12 +0800454 struct crypto_shash *hash = __crypto_shash_cast(tfm);
455
456 hash->descsize = crypto_shash_alg(hash)->descsize;
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000457 return 0;
458}
459
460static unsigned int crypto_shash_extsize(struct crypto_alg *alg,
461 const struct crypto_type *frontend)
462{
463 return alg->cra_ctxsize;
464}
465
466static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
467 __attribute__ ((unused));
468static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
469{
470 struct shash_alg *salg = __crypto_shash_alg(alg);
471
472 seq_printf(m, "type : shash\n");
473 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
474 seq_printf(m, "digestsize : %u\n", salg->digestsize);
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000475}
476
477static const struct crypto_type crypto_shash_type = {
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000478 .ctxsize = crypto_shash_ctxsize,
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000479 .extsize = crypto_shash_extsize,
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000480 .init = crypto_init_shash_ops,
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000481 .init_tfm = crypto_shash_init_tfm,
482#ifdef CONFIG_PROC_FS
483 .show = crypto_shash_show,
484#endif
485 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
486 .maskset = CRYPTO_ALG_TYPE_MASK,
487 .type = CRYPTO_ALG_TYPE_SHASH,
488 .tfmsize = offsetof(struct crypto_shash, base),
489};
490
491struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
492 u32 mask)
493{
Herbert Xu3f683d62009-02-18 16:56:59 +0800494 return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask);
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000495}
496EXPORT_SYMBOL_GPL(crypto_alloc_shash);
497
Herbert Xu619a6eb2009-07-08 18:46:23 +0800498static int shash_prepare_alg(struct shash_alg *alg)
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000499{
500 struct crypto_alg *base = &alg->base;
501
502 if (alg->digestsize > PAGE_SIZE / 8 ||
Herbert Xu99d27e12009-07-09 20:30:57 +0800503 alg->descsize > PAGE_SIZE / 8 ||
504 alg->statesize > PAGE_SIZE / 8)
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000505 return -EINVAL;
506
507 base->cra_type = &crypto_shash_type;
508 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
509 base->cra_flags |= CRYPTO_ALG_TYPE_SHASH;
Herbert Xu99d27e12009-07-09 20:30:57 +0800510
Herbert Xu8267ada2009-07-09 20:36:44 +0800511 if (!alg->finup)
512 alg->finup = shash_finup_unaligned;
513 if (!alg->digest)
514 alg->digest = shash_digest_unaligned;
Herbert Xu99d27e12009-07-09 20:30:57 +0800515 if (!alg->import)
516 alg->import = shash_no_import;
517 if (!alg->export)
518 alg->export = shash_no_export;
Herbert Xu57cfe442009-07-11 22:17:39 +0800519 if (!alg->setkey)
520 alg->setkey = shash_no_setkey;
Herbert Xu99d27e12009-07-09 20:30:57 +0800521
Herbert Xu619a6eb2009-07-08 18:46:23 +0800522 return 0;
523}
524
525int crypto_register_shash(struct shash_alg *alg)
526{
527 struct crypto_alg *base = &alg->base;
528 int err;
529
530 err = shash_prepare_alg(alg);
531 if (err)
532 return err;
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000533
534 return crypto_register_alg(base);
535}
536EXPORT_SYMBOL_GPL(crypto_register_shash);
537
538int crypto_unregister_shash(struct shash_alg *alg)
539{
540 return crypto_unregister_alg(&alg->base);
541}
542EXPORT_SYMBOL_GPL(crypto_unregister_shash);
543
Herbert Xu619a6eb2009-07-08 18:46:23 +0800544int shash_register_instance(struct crypto_template *tmpl,
545 struct shash_instance *inst)
546{
547 int err;
548
549 err = shash_prepare_alg(&inst->alg);
550 if (err)
551 return err;
552
553 return crypto_register_instance(tmpl, shash_crypto_instance(inst));
554}
555EXPORT_SYMBOL_GPL(shash_register_instance);
556
Herbert Xu2e4fddd2009-07-07 15:17:12 +0800557void shash_free_instance(struct crypto_instance *inst)
558{
559 crypto_drop_spawn(crypto_instance_ctx(inst));
560 kfree(shash_instance(inst));
561}
562EXPORT_SYMBOL_GPL(shash_free_instance);
563
Herbert Xu94296992009-07-08 17:21:37 +0800564int crypto_init_shash_spawn(struct crypto_shash_spawn *spawn,
565 struct shash_alg *alg,
566 struct crypto_instance *inst)
567{
568 return crypto_init_spawn2(&spawn->base, &alg->base, inst,
569 &crypto_shash_type);
570}
571EXPORT_SYMBOL_GPL(crypto_init_shash_spawn);
572
Herbert Xu7d6f56402009-07-08 17:56:28 +0800573struct shash_alg *shash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
574{
575 struct crypto_alg *alg;
576
577 alg = crypto_attr_alg2(rta, &crypto_shash_type, type, mask);
578 return IS_ERR(alg) ? ERR_CAST(alg) :
579 container_of(alg, struct shash_alg, base);
580}
581EXPORT_SYMBOL_GPL(shash_attr_alg);
582
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000583MODULE_LICENSE("GPL");
584MODULE_DESCRIPTION("Synchronous cryptographic hash type");