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)
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>
23 static const struct crypto_type crypto_shash_type
;
25 static int shash_no_setkey(struct crypto_shash
*tfm
, const u8
*key
,
31 static int shash_setkey_unaligned(struct crypto_shash
*tfm
, const u8
*key
,
34 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
35 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
37 u8
*buffer
, *alignbuffer
;
40 absize
= keylen
+ (alignmask
& ~(CRYPTO_MINALIGN
- 1));
41 buffer
= kmalloc(absize
, GFP_KERNEL
);
45 alignbuffer
= (u8
*)ALIGN((unsigned long)buffer
, alignmask
+ 1);
46 memcpy(alignbuffer
, key
, keylen
);
47 err
= shash
->setkey(tfm
, alignbuffer
, keylen
);
52 int crypto_shash_setkey(struct crypto_shash
*tfm
, const u8
*key
,
55 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
56 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
58 if ((unsigned long)key
& alignmask
)
59 return shash_setkey_unaligned(tfm
, key
, keylen
);
61 return shash
->setkey(tfm
, key
, keylen
);
63 EXPORT_SYMBOL_GPL(crypto_shash_setkey
);
65 static inline unsigned int shash_align_buffer_size(unsigned len
,
68 return len
+ (mask
& ~(__alignof__(u8
__attribute__ ((aligned
))) - 1));
71 static int shash_update_unaligned(struct shash_desc
*desc
, const u8
*data
,
74 struct crypto_shash
*tfm
= desc
->tfm
;
75 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
76 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
77 unsigned int unaligned_len
= alignmask
+ 1 -
78 ((unsigned long)data
& alignmask
);
79 u8 buf
[shash_align_buffer_size(unaligned_len
, alignmask
)]
80 __attribute__ ((aligned
));
83 if (unaligned_len
> len
)
86 memcpy(buf
, data
, unaligned_len
);
87 err
= shash
->update(desc
, buf
, unaligned_len
);
88 memset(buf
, 0, unaligned_len
);
91 shash
->update(desc
, data
+ unaligned_len
, len
- unaligned_len
);
94 int crypto_shash_update(struct shash_desc
*desc
, const u8
*data
,
97 struct crypto_shash
*tfm
= desc
->tfm
;
98 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
99 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
101 if ((unsigned long)data
& alignmask
)
102 return shash_update_unaligned(desc
, data
, len
);
104 return shash
->update(desc
, data
, len
);
106 EXPORT_SYMBOL_GPL(crypto_shash_update
);
108 static int shash_final_unaligned(struct shash_desc
*desc
, u8
*out
)
110 struct crypto_shash
*tfm
= desc
->tfm
;
111 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
112 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
113 unsigned int ds
= crypto_shash_digestsize(tfm
);
114 u8 buf
[shash_align_buffer_size(ds
, alignmask
)]
115 __attribute__ ((aligned
));
118 err
= shash
->final(desc
, buf
);
122 memcpy(out
, buf
, ds
);
129 int crypto_shash_final(struct shash_desc
*desc
, u8
*out
)
131 struct crypto_shash
*tfm
= desc
->tfm
;
132 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
133 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
135 if ((unsigned long)out
& alignmask
)
136 return shash_final_unaligned(desc
, out
);
138 return shash
->final(desc
, out
);
140 EXPORT_SYMBOL_GPL(crypto_shash_final
);
142 static int shash_finup_unaligned(struct shash_desc
*desc
, const u8
*data
,
143 unsigned int len
, u8
*out
)
145 return crypto_shash_update(desc
, data
, len
) ?:
146 crypto_shash_final(desc
, out
);
149 int crypto_shash_finup(struct shash_desc
*desc
, const u8
*data
,
150 unsigned int len
, u8
*out
)
152 struct crypto_shash
*tfm
= desc
->tfm
;
153 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
154 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
156 if (((unsigned long)data
| (unsigned long)out
) & alignmask
)
157 return shash_finup_unaligned(desc
, data
, len
, out
);
159 return shash
->finup(desc
, data
, len
, out
);
161 EXPORT_SYMBOL_GPL(crypto_shash_finup
);
163 static int shash_digest_unaligned(struct shash_desc
*desc
, const u8
*data
,
164 unsigned int len
, u8
*out
)
166 return crypto_shash_init(desc
) ?:
167 crypto_shash_finup(desc
, data
, len
, out
);
170 int crypto_shash_digest(struct shash_desc
*desc
, const u8
*data
,
171 unsigned int len
, u8
*out
)
173 struct crypto_shash
*tfm
= desc
->tfm
;
174 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
175 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
177 if (((unsigned long)data
| (unsigned long)out
) & alignmask
)
178 return shash_digest_unaligned(desc
, data
, len
, out
);
180 return shash
->digest(desc
, data
, len
, out
);
182 EXPORT_SYMBOL_GPL(crypto_shash_digest
);
184 static int shash_no_export(struct shash_desc
*desc
, void *out
)
189 static int shash_no_import(struct shash_desc
*desc
, const void *in
)
194 static int shash_async_setkey(struct crypto_ahash
*tfm
, const u8
*key
,
197 struct crypto_shash
**ctx
= crypto_ahash_ctx(tfm
);
199 return crypto_shash_setkey(*ctx
, key
, keylen
);
202 static int shash_async_init(struct ahash_request
*req
)
204 struct crypto_shash
**ctx
= crypto_ahash_ctx(crypto_ahash_reqtfm(req
));
205 struct shash_desc
*desc
= ahash_request_ctx(req
);
208 desc
->flags
= req
->base
.flags
;
210 return crypto_shash_init(desc
);
213 int shash_ahash_update(struct ahash_request
*req
, struct shash_desc
*desc
)
215 struct crypto_hash_walk walk
;
218 for (nbytes
= crypto_hash_walk_first(req
, &walk
); nbytes
> 0;
219 nbytes
= crypto_hash_walk_done(&walk
, nbytes
))
220 nbytes
= crypto_shash_update(desc
, walk
.data
, nbytes
);
224 EXPORT_SYMBOL_GPL(shash_ahash_update
);
226 static int shash_async_update(struct ahash_request
*req
)
228 return shash_ahash_update(req
, ahash_request_ctx(req
));
231 static int shash_async_final(struct ahash_request
*req
)
233 return crypto_shash_final(ahash_request_ctx(req
), req
->result
);
236 int shash_ahash_digest(struct ahash_request
*req
, struct shash_desc
*desc
)
238 struct scatterlist
*sg
= req
->src
;
239 unsigned int offset
= sg
->offset
;
240 unsigned int nbytes
= req
->nbytes
;
243 if (nbytes
< min(sg
->length
, ((unsigned int)(PAGE_SIZE
)) - offset
)) {
246 data
= crypto_kmap(sg_page(sg
), 0);
247 err
= crypto_shash_digest(desc
, data
+ offset
, nbytes
,
249 crypto_kunmap(data
, 0);
250 crypto_yield(desc
->flags
);
252 err
= crypto_shash_init(desc
) ?:
253 shash_ahash_update(req
, desc
) ?:
254 crypto_shash_final(desc
, req
->result
);
258 EXPORT_SYMBOL_GPL(shash_ahash_digest
);
260 static int shash_async_digest(struct ahash_request
*req
)
262 struct crypto_shash
**ctx
= crypto_ahash_ctx(crypto_ahash_reqtfm(req
));
263 struct shash_desc
*desc
= ahash_request_ctx(req
);
266 desc
->flags
= req
->base
.flags
;
268 return shash_ahash_digest(req
, desc
);
271 static void crypto_exit_shash_ops_async(struct crypto_tfm
*tfm
)
273 struct crypto_shash
**ctx
= crypto_tfm_ctx(tfm
);
275 crypto_free_shash(*ctx
);
278 int crypto_init_shash_ops_async(struct crypto_tfm
*tfm
)
280 struct crypto_alg
*calg
= tfm
->__crt_alg
;
281 struct crypto_ahash
*crt
= __crypto_ahash_cast(tfm
);
282 struct crypto_shash
**ctx
= crypto_tfm_ctx(tfm
);
283 struct crypto_shash
*shash
;
285 if (!crypto_mod_get(calg
))
288 shash
= crypto_create_tfm(calg
, &crypto_shash_type
);
290 crypto_mod_put(calg
);
291 return PTR_ERR(shash
);
295 tfm
->exit
= crypto_exit_shash_ops_async
;
297 crt
->init
= shash_async_init
;
298 crt
->update
= shash_async_update
;
299 crt
->final
= shash_async_final
;
300 crt
->digest
= shash_async_digest
;
301 crt
->setkey
= shash_async_setkey
;
303 crt
->reqsize
= sizeof(struct shash_desc
) + crypto_shash_descsize(shash
);
308 static int shash_compat_setkey(struct crypto_hash
*tfm
, const u8
*key
,
311 struct shash_desc
**descp
= crypto_hash_ctx(tfm
);
312 struct shash_desc
*desc
= *descp
;
314 return crypto_shash_setkey(desc
->tfm
, key
, keylen
);
317 static int shash_compat_init(struct hash_desc
*hdesc
)
319 struct shash_desc
**descp
= crypto_hash_ctx(hdesc
->tfm
);
320 struct shash_desc
*desc
= *descp
;
322 desc
->flags
= hdesc
->flags
;
324 return crypto_shash_init(desc
);
327 static int shash_compat_update(struct hash_desc
*hdesc
, struct scatterlist
*sg
,
330 struct shash_desc
**descp
= crypto_hash_ctx(hdesc
->tfm
);
331 struct shash_desc
*desc
= *descp
;
332 struct crypto_hash_walk walk
;
335 for (nbytes
= crypto_hash_walk_first_compat(hdesc
, &walk
, sg
, len
);
336 nbytes
> 0; nbytes
= crypto_hash_walk_done(&walk
, nbytes
))
337 nbytes
= crypto_shash_update(desc
, walk
.data
, nbytes
);
342 static int shash_compat_final(struct hash_desc
*hdesc
, u8
*out
)
344 struct shash_desc
**descp
= crypto_hash_ctx(hdesc
->tfm
);
346 return crypto_shash_final(*descp
, out
);
349 static int shash_compat_digest(struct hash_desc
*hdesc
, struct scatterlist
*sg
,
350 unsigned int nbytes
, u8
*out
)
352 unsigned int offset
= sg
->offset
;
355 if (nbytes
< min(sg
->length
, ((unsigned int)(PAGE_SIZE
)) - offset
)) {
356 struct shash_desc
**descp
= crypto_hash_ctx(hdesc
->tfm
);
357 struct shash_desc
*desc
= *descp
;
360 desc
->flags
= hdesc
->flags
;
362 data
= crypto_kmap(sg_page(sg
), 0);
363 err
= crypto_shash_digest(desc
, data
+ offset
, nbytes
, out
);
364 crypto_kunmap(data
, 0);
365 crypto_yield(desc
->flags
);
369 err
= shash_compat_init(hdesc
);
373 err
= shash_compat_update(hdesc
, sg
, nbytes
);
377 err
= shash_compat_final(hdesc
, out
);
383 static void crypto_exit_shash_ops_compat(struct crypto_tfm
*tfm
)
385 struct shash_desc
**descp
= crypto_tfm_ctx(tfm
);
386 struct shash_desc
*desc
= *descp
;
388 crypto_free_shash(desc
->tfm
);
392 static int crypto_init_shash_ops_compat(struct crypto_tfm
*tfm
)
394 struct hash_tfm
*crt
= &tfm
->crt_hash
;
395 struct crypto_alg
*calg
= tfm
->__crt_alg
;
396 struct shash_alg
*alg
= __crypto_shash_alg(calg
);
397 struct shash_desc
**descp
= crypto_tfm_ctx(tfm
);
398 struct crypto_shash
*shash
;
399 struct shash_desc
*desc
;
401 if (!crypto_mod_get(calg
))
404 shash
= crypto_create_tfm(calg
, &crypto_shash_type
);
406 crypto_mod_put(calg
);
407 return PTR_ERR(shash
);
410 desc
= kmalloc(sizeof(*desc
) + crypto_shash_descsize(shash
),
413 crypto_free_shash(shash
);
419 tfm
->exit
= crypto_exit_shash_ops_compat
;
421 crt
->init
= shash_compat_init
;
422 crt
->update
= shash_compat_update
;
423 crt
->final
= shash_compat_final
;
424 crt
->digest
= shash_compat_digest
;
425 crt
->setkey
= shash_compat_setkey
;
427 crt
->digestsize
= alg
->digestsize
;
432 static int crypto_init_shash_ops(struct crypto_tfm
*tfm
, u32 type
, u32 mask
)
434 switch (mask
& CRYPTO_ALG_TYPE_MASK
) {
435 case CRYPTO_ALG_TYPE_HASH_MASK
:
436 return crypto_init_shash_ops_compat(tfm
);
442 static unsigned int crypto_shash_ctxsize(struct crypto_alg
*alg
, u32 type
,
445 switch (mask
& CRYPTO_ALG_TYPE_MASK
) {
446 case CRYPTO_ALG_TYPE_HASH_MASK
:
447 return sizeof(struct shash_desc
*);
453 static int crypto_shash_init_tfm(struct crypto_tfm
*tfm
)
455 struct crypto_shash
*hash
= __crypto_shash_cast(tfm
);
457 hash
->descsize
= crypto_shash_alg(hash
)->descsize
;
461 static unsigned int crypto_shash_extsize(struct crypto_alg
*alg
)
463 return alg
->cra_ctxsize
;
466 static void crypto_shash_show(struct seq_file
*m
, struct crypto_alg
*alg
)
467 __attribute__ ((unused
));
468 static void crypto_shash_show(struct seq_file
*m
, struct crypto_alg
*alg
)
470 struct shash_alg
*salg
= __crypto_shash_alg(alg
);
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
);
477 static const struct crypto_type crypto_shash_type
= {
478 .ctxsize
= crypto_shash_ctxsize
,
479 .extsize
= crypto_shash_extsize
,
480 .init
= crypto_init_shash_ops
,
481 .init_tfm
= crypto_shash_init_tfm
,
482 #ifdef CONFIG_PROC_FS
483 .show
= crypto_shash_show
,
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
),
491 struct crypto_shash
*crypto_alloc_shash(const char *alg_name
, u32 type
,
494 return crypto_alloc_tfm(alg_name
, &crypto_shash_type
, type
, mask
);
496 EXPORT_SYMBOL_GPL(crypto_alloc_shash
);
498 static int shash_prepare_alg(struct shash_alg
*alg
)
500 struct crypto_alg
*base
= &alg
->base
;
502 if (alg
->digestsize
> PAGE_SIZE
/ 8 ||
503 alg
->descsize
> PAGE_SIZE
/ 8 ||
504 alg
->statesize
> PAGE_SIZE
/ 8)
507 base
->cra_type
= &crypto_shash_type
;
508 base
->cra_flags
&= ~CRYPTO_ALG_TYPE_MASK
;
509 base
->cra_flags
|= CRYPTO_ALG_TYPE_SHASH
;
512 alg
->finup
= shash_finup_unaligned
;
514 alg
->digest
= shash_digest_unaligned
;
516 alg
->import
= shash_no_import
;
518 alg
->export
= shash_no_export
;
520 alg
->setkey
= shash_no_setkey
;
525 int crypto_register_shash(struct shash_alg
*alg
)
527 struct crypto_alg
*base
= &alg
->base
;
530 err
= shash_prepare_alg(alg
);
534 return crypto_register_alg(base
);
536 EXPORT_SYMBOL_GPL(crypto_register_shash
);
538 int crypto_unregister_shash(struct shash_alg
*alg
)
540 return crypto_unregister_alg(&alg
->base
);
542 EXPORT_SYMBOL_GPL(crypto_unregister_shash
);
544 int shash_register_instance(struct crypto_template
*tmpl
,
545 struct shash_instance
*inst
)
549 err
= shash_prepare_alg(&inst
->alg
);
553 return crypto_register_instance(tmpl
, shash_crypto_instance(inst
));
555 EXPORT_SYMBOL_GPL(shash_register_instance
);
557 void shash_free_instance(struct crypto_instance
*inst
)
559 crypto_drop_spawn(crypto_instance_ctx(inst
));
560 kfree(shash_instance(inst
));
562 EXPORT_SYMBOL_GPL(shash_free_instance
);
564 int crypto_init_shash_spawn(struct crypto_shash_spawn
*spawn
,
565 struct shash_alg
*alg
,
566 struct crypto_instance
*inst
)
568 return crypto_init_spawn2(&spawn
->base
, &alg
->base
, inst
,
571 EXPORT_SYMBOL_GPL(crypto_init_shash_spawn
);
573 struct shash_alg
*shash_attr_alg(struct rtattr
*rta
, u32 type
, u32 mask
)
575 struct crypto_alg
*alg
;
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
);
581 EXPORT_SYMBOL_GPL(shash_attr_alg
);
583 MODULE_LICENSE("GPL");
584 MODULE_DESCRIPTION("Synchronous cryptographic hash type");