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_setkey_unaligned(struct crypto_shash
*tfm
, const u8
*key
,
28 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
29 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
31 u8
*buffer
, *alignbuffer
;
34 absize
= keylen
+ (alignmask
& ~(CRYPTO_MINALIGN
- 1));
35 buffer
= kmalloc(absize
, GFP_KERNEL
);
39 alignbuffer
= (u8
*)ALIGN((unsigned long)buffer
, alignmask
+ 1);
40 memcpy(alignbuffer
, key
, keylen
);
41 err
= shash
->setkey(tfm
, alignbuffer
, keylen
);
42 memset(alignbuffer
, 0, keylen
);
47 int crypto_shash_setkey(struct crypto_shash
*tfm
, const u8
*key
,
50 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
51 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
56 if ((unsigned long)key
& alignmask
)
57 return shash_setkey_unaligned(tfm
, key
, keylen
);
59 return shash
->setkey(tfm
, key
, keylen
);
61 EXPORT_SYMBOL_GPL(crypto_shash_setkey
);
63 static inline unsigned int shash_align_buffer_size(unsigned len
,
66 return len
+ (mask
& ~(__alignof__(u8
__attribute__ ((aligned
))) - 1));
69 static int shash_update_unaligned(struct shash_desc
*desc
, const u8
*data
,
72 struct crypto_shash
*tfm
= desc
->tfm
;
73 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
74 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
75 unsigned int unaligned_len
= alignmask
+ 1 -
76 ((unsigned long)data
& alignmask
);
77 u8 buf
[shash_align_buffer_size(unaligned_len
, alignmask
)]
78 __attribute__ ((aligned
));
80 memcpy(buf
, data
, unaligned_len
);
82 return shash
->update(desc
, buf
, unaligned_len
) ?:
83 shash
->update(desc
, data
+ unaligned_len
, len
- unaligned_len
);
86 int crypto_shash_update(struct shash_desc
*desc
, const u8
*data
,
89 struct crypto_shash
*tfm
= desc
->tfm
;
90 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
91 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
93 if ((unsigned long)data
& alignmask
)
94 return shash_update_unaligned(desc
, data
, len
);
96 return shash
->update(desc
, data
, len
);
98 EXPORT_SYMBOL_GPL(crypto_shash_update
);
100 static int shash_final_unaligned(struct shash_desc
*desc
, u8
*out
)
102 struct crypto_shash
*tfm
= desc
->tfm
;
103 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
104 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
105 unsigned int ds
= crypto_shash_digestsize(tfm
);
106 u8 buf
[shash_align_buffer_size(ds
, alignmask
)]
107 __attribute__ ((aligned
));
110 err
= shash
->final(desc
, buf
);
111 memcpy(out
, buf
, ds
);
115 int crypto_shash_final(struct shash_desc
*desc
, u8
*out
)
117 struct crypto_shash
*tfm
= desc
->tfm
;
118 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
119 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
121 if ((unsigned long)out
& alignmask
)
122 return shash_final_unaligned(desc
, out
);
124 return shash
->final(desc
, out
);
126 EXPORT_SYMBOL_GPL(crypto_shash_final
);
128 static int shash_finup_unaligned(struct shash_desc
*desc
, const u8
*data
,
129 unsigned int len
, u8
*out
)
131 return crypto_shash_update(desc
, data
, len
) ?:
132 crypto_shash_final(desc
, out
);
135 int crypto_shash_finup(struct shash_desc
*desc
, const u8
*data
,
136 unsigned int len
, u8
*out
)
138 struct crypto_shash
*tfm
= desc
->tfm
;
139 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
140 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
142 if (((unsigned long)data
| (unsigned long)out
) & alignmask
||
144 return shash_finup_unaligned(desc
, data
, len
, out
);
146 return shash
->finup(desc
, data
, len
, out
);
148 EXPORT_SYMBOL_GPL(crypto_shash_finup
);
150 static int shash_digest_unaligned(struct shash_desc
*desc
, const u8
*data
,
151 unsigned int len
, u8
*out
)
153 return crypto_shash_init(desc
) ?:
154 crypto_shash_update(desc
, data
, len
) ?:
155 crypto_shash_final(desc
, out
);
158 int crypto_shash_digest(struct shash_desc
*desc
, const u8
*data
,
159 unsigned int len
, u8
*out
)
161 struct crypto_shash
*tfm
= desc
->tfm
;
162 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
163 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
165 if (((unsigned long)data
| (unsigned long)out
) & alignmask
||
167 return shash_digest_unaligned(desc
, data
, len
, out
);
169 return shash
->digest(desc
, data
, len
, out
);
171 EXPORT_SYMBOL_GPL(crypto_shash_digest
);
173 int crypto_shash_import(struct shash_desc
*desc
, const u8
*in
)
175 struct crypto_shash
*tfm
= desc
->tfm
;
176 struct shash_alg
*alg
= crypto_shash_alg(tfm
);
178 memcpy(shash_desc_ctx(desc
), in
, crypto_shash_descsize(tfm
));
185 EXPORT_SYMBOL_GPL(crypto_shash_import
);
187 static int shash_async_setkey(struct crypto_ahash
*tfm
, const u8
*key
,
190 struct crypto_shash
**ctx
= crypto_ahash_ctx(tfm
);
192 return crypto_shash_setkey(*ctx
, key
, keylen
);
195 static int shash_async_init(struct ahash_request
*req
)
197 struct crypto_shash
**ctx
= crypto_ahash_ctx(crypto_ahash_reqtfm(req
));
198 struct shash_desc
*desc
= ahash_request_ctx(req
);
201 desc
->flags
= req
->base
.flags
;
203 return crypto_shash_init(desc
);
206 static int shash_async_update(struct ahash_request
*req
)
208 struct shash_desc
*desc
= ahash_request_ctx(req
);
209 struct crypto_hash_walk walk
;
212 for (nbytes
= crypto_hash_walk_first(req
, &walk
); nbytes
> 0;
213 nbytes
= crypto_hash_walk_done(&walk
, nbytes
))
214 nbytes
= crypto_shash_update(desc
, walk
.data
, nbytes
);
219 static int shash_async_final(struct ahash_request
*req
)
221 return crypto_shash_final(ahash_request_ctx(req
), req
->result
);
224 static int shash_async_digest(struct ahash_request
*req
)
226 struct scatterlist
*sg
= req
->src
;
227 unsigned int offset
= sg
->offset
;
228 unsigned int nbytes
= req
->nbytes
;
231 if (nbytes
< min(sg
->length
, ((unsigned int)(PAGE_SIZE
)) - offset
)) {
232 struct crypto_shash
**ctx
=
233 crypto_ahash_ctx(crypto_ahash_reqtfm(req
));
234 struct shash_desc
*desc
= ahash_request_ctx(req
);
238 desc
->flags
= req
->base
.flags
;
240 data
= crypto_kmap(sg_page(sg
), 0);
241 err
= crypto_shash_digest(desc
, data
+ offset
, nbytes
,
243 crypto_kunmap(data
, 0);
244 crypto_yield(desc
->flags
);
248 err
= shash_async_init(req
);
252 err
= shash_async_update(req
);
256 err
= shash_async_final(req
);
262 static void crypto_exit_shash_ops_async(struct crypto_tfm
*tfm
)
264 struct crypto_shash
**ctx
= crypto_tfm_ctx(tfm
);
266 crypto_free_shash(*ctx
);
269 static int crypto_init_shash_ops_async(struct crypto_tfm
*tfm
)
271 struct crypto_alg
*calg
= tfm
->__crt_alg
;
272 struct shash_alg
*alg
= __crypto_shash_alg(calg
);
273 struct ahash_tfm
*crt
= &tfm
->crt_ahash
;
274 struct crypto_shash
**ctx
= crypto_tfm_ctx(tfm
);
275 struct crypto_shash
*shash
;
277 if (!crypto_mod_get(calg
))
280 shash
= crypto_create_tfm(calg
, &crypto_shash_type
);
282 crypto_mod_put(calg
);
283 return PTR_ERR(shash
);
287 tfm
->exit
= crypto_exit_shash_ops_async
;
289 crt
->init
= shash_async_init
;
290 crt
->update
= shash_async_update
;
291 crt
->final
= shash_async_final
;
292 crt
->digest
= shash_async_digest
;
293 crt
->setkey
= shash_async_setkey
;
295 crt
->digestsize
= alg
->digestsize
;
296 crt
->reqsize
= sizeof(struct shash_desc
) + crypto_shash_descsize(shash
);
301 static int shash_compat_setkey(struct crypto_hash
*tfm
, const u8
*key
,
304 struct shash_desc
*desc
= crypto_hash_ctx(tfm
);
306 return crypto_shash_setkey(desc
->tfm
, key
, keylen
);
309 static int shash_compat_init(struct hash_desc
*hdesc
)
311 struct shash_desc
*desc
= crypto_hash_ctx(hdesc
->tfm
);
313 desc
->flags
= hdesc
->flags
;
315 return crypto_shash_init(desc
);
318 static int shash_compat_update(struct hash_desc
*hdesc
, struct scatterlist
*sg
,
321 struct shash_desc
*desc
= crypto_hash_ctx(hdesc
->tfm
);
322 struct crypto_hash_walk walk
;
325 for (nbytes
= crypto_hash_walk_first_compat(hdesc
, &walk
, sg
, len
);
326 nbytes
> 0; nbytes
= crypto_hash_walk_done(&walk
, nbytes
))
327 nbytes
= crypto_shash_update(desc
, walk
.data
, nbytes
);
332 static int shash_compat_final(struct hash_desc
*hdesc
, u8
*out
)
334 return crypto_shash_final(crypto_hash_ctx(hdesc
->tfm
), out
);
337 static int shash_compat_digest(struct hash_desc
*hdesc
, struct scatterlist
*sg
,
338 unsigned int nbytes
, u8
*out
)
340 unsigned int offset
= sg
->offset
;
343 if (nbytes
< min(sg
->length
, ((unsigned int)(PAGE_SIZE
)) - offset
)) {
344 struct shash_desc
*desc
= crypto_hash_ctx(hdesc
->tfm
);
347 desc
->flags
= hdesc
->flags
;
349 data
= crypto_kmap(sg_page(sg
), 0);
350 err
= crypto_shash_digest(desc
, data
+ offset
, nbytes
, out
);
351 crypto_kunmap(data
, 0);
352 crypto_yield(desc
->flags
);
356 err
= shash_compat_init(hdesc
);
360 err
= shash_compat_update(hdesc
, sg
, nbytes
);
364 err
= shash_compat_final(hdesc
, out
);
370 static void crypto_exit_shash_ops_compat(struct crypto_tfm
*tfm
)
372 struct shash_desc
*desc
= crypto_tfm_ctx(tfm
);
374 crypto_free_shash(desc
->tfm
);
377 static int crypto_init_shash_ops_compat(struct crypto_tfm
*tfm
)
379 struct hash_tfm
*crt
= &tfm
->crt_hash
;
380 struct crypto_alg
*calg
= tfm
->__crt_alg
;
381 struct shash_alg
*alg
= __crypto_shash_alg(calg
);
382 struct shash_desc
*desc
= crypto_tfm_ctx(tfm
);
383 struct crypto_shash
*shash
;
385 if (!crypto_mod_get(calg
))
388 shash
= crypto_create_tfm(calg
, &crypto_shash_type
);
390 crypto_mod_put(calg
);
391 return PTR_ERR(shash
);
395 tfm
->exit
= crypto_exit_shash_ops_compat
;
397 crt
->init
= shash_compat_init
;
398 crt
->update
= shash_compat_update
;
399 crt
->final
= shash_compat_final
;
400 crt
->digest
= shash_compat_digest
;
401 crt
->setkey
= shash_compat_setkey
;
403 crt
->digestsize
= alg
->digestsize
;
408 static int crypto_init_shash_ops(struct crypto_tfm
*tfm
, u32 type
, u32 mask
)
410 switch (mask
& CRYPTO_ALG_TYPE_MASK
) {
411 case CRYPTO_ALG_TYPE_HASH_MASK
:
412 return crypto_init_shash_ops_compat(tfm
);
413 case CRYPTO_ALG_TYPE_AHASH_MASK
:
414 return crypto_init_shash_ops_async(tfm
);
420 static unsigned int crypto_shash_ctxsize(struct crypto_alg
*alg
, u32 type
,
423 struct shash_alg
*salg
= __crypto_shash_alg(alg
);
425 switch (mask
& CRYPTO_ALG_TYPE_MASK
) {
426 case CRYPTO_ALG_TYPE_HASH_MASK
:
427 return sizeof(struct shash_desc
) + salg
->descsize
;
428 case CRYPTO_ALG_TYPE_AHASH_MASK
:
429 return sizeof(struct crypto_shash
*);
435 static int crypto_shash_init_tfm(struct crypto_tfm
*tfm
,
436 const struct crypto_type
*frontend
)
441 static unsigned int crypto_shash_extsize(struct crypto_alg
*alg
,
442 const struct crypto_type
*frontend
)
444 return alg
->cra_ctxsize
;
447 static void crypto_shash_show(struct seq_file
*m
, struct crypto_alg
*alg
)
448 __attribute__ ((unused
));
449 static void crypto_shash_show(struct seq_file
*m
, struct crypto_alg
*alg
)
451 struct shash_alg
*salg
= __crypto_shash_alg(alg
);
453 seq_printf(m
, "type : shash\n");
454 seq_printf(m
, "blocksize : %u\n", alg
->cra_blocksize
);
455 seq_printf(m
, "digestsize : %u\n", salg
->digestsize
);
456 seq_printf(m
, "descsize : %u\n", salg
->descsize
);
459 static const struct crypto_type crypto_shash_type
= {
460 .ctxsize
= crypto_shash_ctxsize
,
461 .extsize
= crypto_shash_extsize
,
462 .init
= crypto_init_shash_ops
,
463 .init_tfm
= crypto_shash_init_tfm
,
464 #ifdef CONFIG_PROC_FS
465 .show
= crypto_shash_show
,
467 .maskclear
= ~CRYPTO_ALG_TYPE_MASK
,
468 .maskset
= CRYPTO_ALG_TYPE_MASK
,
469 .type
= CRYPTO_ALG_TYPE_SHASH
,
470 .tfmsize
= offsetof(struct crypto_shash
, base
),
473 struct crypto_shash
*crypto_alloc_shash(const char *alg_name
, u32 type
,
476 return crypto_alloc_tfm(alg_name
, &crypto_shash_type
, type
, mask
);
478 EXPORT_SYMBOL_GPL(crypto_alloc_shash
);
480 int crypto_register_shash(struct shash_alg
*alg
)
482 struct crypto_alg
*base
= &alg
->base
;
484 if (alg
->digestsize
> PAGE_SIZE
/ 8 ||
485 alg
->descsize
> PAGE_SIZE
/ 8)
488 base
->cra_type
= &crypto_shash_type
;
489 base
->cra_flags
&= ~CRYPTO_ALG_TYPE_MASK
;
490 base
->cra_flags
|= CRYPTO_ALG_TYPE_SHASH
;
492 return crypto_register_alg(base
);
494 EXPORT_SYMBOL_GPL(crypto_register_shash
);
496 int crypto_unregister_shash(struct shash_alg
*alg
)
498 return crypto_unregister_alg(&alg
->base
);
500 EXPORT_SYMBOL_GPL(crypto_unregister_shash
);
502 MODULE_LICENSE("GPL");
503 MODULE_DESCRIPTION("Synchronous cryptographic hash type");