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>
21 static const struct crypto_type crypto_shash_type
;
23 static inline struct crypto_shash
*__crypto_shash_cast(struct crypto_tfm
*tfm
)
25 return container_of(tfm
, struct crypto_shash
, base
);
30 static int shash_setkey_unaligned(struct crypto_shash
*tfm
, const u8
*key
,
33 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
34 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
36 u8
*buffer
, *alignbuffer
;
39 absize
= keylen
+ (alignmask
& ~(CRYPTO_MINALIGN
- 1));
40 buffer
= kmalloc(absize
, GFP_KERNEL
);
44 alignbuffer
= (u8
*)ALIGN((unsigned long)buffer
, alignmask
+ 1);
45 memcpy(alignbuffer
, key
, keylen
);
46 err
= shash
->setkey(tfm
, alignbuffer
, keylen
);
47 memset(alignbuffer
, 0, 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
));
82 memcpy(buf
, data
, unaligned_len
);
84 return shash
->update(desc
, buf
, unaligned_len
) ?:
85 shash
->update(desc
, data
+ unaligned_len
, len
- unaligned_len
);
88 int crypto_shash_update(struct shash_desc
*desc
, const u8
*data
,
91 struct crypto_shash
*tfm
= desc
->tfm
;
92 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
93 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
95 if ((unsigned long)data
& alignmask
)
96 return shash_update_unaligned(desc
, data
, len
);
98 return shash
->update(desc
, data
, len
);
100 EXPORT_SYMBOL_GPL(crypto_shash_update
);
102 static int shash_final_unaligned(struct shash_desc
*desc
, u8
*out
)
104 struct crypto_shash
*tfm
= desc
->tfm
;
105 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
106 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
107 unsigned int ds
= crypto_shash_digestsize(tfm
);
108 u8 buf
[shash_align_buffer_size(ds
, alignmask
)]
109 __attribute__ ((aligned
));
112 err
= shash
->final(desc
, buf
);
113 memcpy(out
, buf
, ds
);
117 int crypto_shash_final(struct shash_desc
*desc
, u8
*out
)
119 struct crypto_shash
*tfm
= desc
->tfm
;
120 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
121 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
123 if ((unsigned long)out
& alignmask
)
124 return shash_final_unaligned(desc
, out
);
126 return shash
->final(desc
, out
);
128 EXPORT_SYMBOL_GPL(crypto_shash_final
);
130 static int shash_finup_unaligned(struct shash_desc
*desc
, const u8
*data
,
131 unsigned int len
, u8
*out
)
133 return crypto_shash_update(desc
, data
, len
) ?:
134 crypto_shash_final(desc
, out
);
137 int crypto_shash_finup(struct shash_desc
*desc
, const u8
*data
,
138 unsigned int len
, u8
*out
)
140 struct crypto_shash
*tfm
= desc
->tfm
;
141 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
142 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
144 if (((unsigned long)data
| (unsigned long)out
) & alignmask
||
146 return shash_finup_unaligned(desc
, data
, len
, out
);
148 return shash
->finup(desc
, data
, len
, out
);
150 EXPORT_SYMBOL_GPL(crypto_shash_finup
);
152 static int shash_digest_unaligned(struct shash_desc
*desc
, const u8
*data
,
153 unsigned int len
, u8
*out
)
155 return crypto_shash_init(desc
) ?:
156 crypto_shash_update(desc
, data
, len
) ?:
157 crypto_shash_final(desc
, out
);
160 int crypto_shash_digest(struct shash_desc
*desc
, const u8
*data
,
161 unsigned int len
, u8
*out
)
163 struct crypto_shash
*tfm
= desc
->tfm
;
164 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
165 unsigned long alignmask
= crypto_shash_alignmask(tfm
);
167 if (((unsigned long)data
| (unsigned long)out
) & alignmask
||
169 return shash_digest_unaligned(desc
, data
, len
, out
);
171 return shash
->digest(desc
, data
, len
, out
);
173 EXPORT_SYMBOL_GPL(crypto_shash_digest
);
175 static int shash_async_setkey(struct crypto_ahash
*tfm
, const u8
*key
,
178 struct crypto_shash
**ctx
= crypto_ahash_ctx(tfm
);
180 return crypto_shash_setkey(*ctx
, key
, keylen
);
183 static int shash_async_init(struct ahash_request
*req
)
185 struct crypto_shash
**ctx
= crypto_ahash_ctx(crypto_ahash_reqtfm(req
));
186 struct shash_desc
*desc
= ahash_request_ctx(req
);
189 desc
->flags
= req
->base
.flags
;
191 return crypto_shash_init(desc
);
194 static int shash_async_update(struct ahash_request
*req
)
196 struct shash_desc
*desc
= ahash_request_ctx(req
);
197 struct crypto_hash_walk walk
;
200 for (nbytes
= crypto_hash_walk_first(req
, &walk
); nbytes
> 0;
201 nbytes
= crypto_hash_walk_done(&walk
, nbytes
))
202 nbytes
= crypto_shash_update(desc
, walk
.data
, nbytes
);
207 static int shash_async_final(struct ahash_request
*req
)
209 return crypto_shash_final(ahash_request_ctx(req
), req
->result
);
212 static int shash_async_digest(struct ahash_request
*req
)
214 struct scatterlist
*sg
= req
->src
;
215 unsigned int offset
= sg
->offset
;
216 unsigned int nbytes
= req
->nbytes
;
219 if (nbytes
< min(sg
->length
, ((unsigned int)(PAGE_SIZE
)) - offset
)) {
220 struct crypto_shash
**ctx
=
221 crypto_ahash_ctx(crypto_ahash_reqtfm(req
));
222 struct shash_desc
*desc
= ahash_request_ctx(req
);
226 desc
->flags
= req
->base
.flags
;
228 data
= crypto_kmap(sg_page(sg
), 0);
229 err
= crypto_shash_digest(desc
, data
+ offset
, nbytes
,
231 crypto_kunmap(data
, 0);
232 crypto_yield(desc
->flags
);
236 err
= shash_async_init(req
);
240 err
= shash_async_update(req
);
244 err
= shash_async_final(req
);
250 static void crypto_exit_shash_ops_async(struct crypto_tfm
*tfm
)
252 struct crypto_shash
**ctx
= crypto_tfm_ctx(tfm
);
254 crypto_free_shash(*ctx
);
257 static int crypto_init_shash_ops_async(struct crypto_tfm
*tfm
)
259 struct crypto_alg
*calg
= tfm
->__crt_alg
;
260 struct shash_alg
*alg
= __crypto_shash_alg(calg
);
261 struct ahash_tfm
*crt
= &tfm
->crt_ahash
;
262 struct crypto_shash
**ctx
= crypto_tfm_ctx(tfm
);
263 struct crypto_shash
*shash
;
265 if (!crypto_mod_get(calg
))
268 shash
= __crypto_shash_cast(crypto_create_tfm(
269 calg
, &crypto_shash_type
));
271 crypto_mod_put(calg
);
272 return PTR_ERR(shash
);
276 tfm
->exit
= crypto_exit_shash_ops_async
;
278 crt
->init
= shash_async_init
;
279 crt
->update
= shash_async_update
;
280 crt
->final
= shash_async_final
;
281 crt
->digest
= shash_async_digest
;
282 crt
->setkey
= shash_async_setkey
;
284 crt
->digestsize
= alg
->digestsize
;
285 crt
->reqsize
= sizeof(struct shash_desc
) + crypto_shash_descsize(shash
);
290 static int crypto_init_shash_ops(struct crypto_tfm
*tfm
, u32 type
, u32 mask
)
292 switch (mask
& CRYPTO_ALG_TYPE_MASK
) {
293 case CRYPTO_ALG_TYPE_AHASH_MASK
:
294 return crypto_init_shash_ops_async(tfm
);
300 static unsigned int crypto_shash_ctxsize(struct crypto_alg
*alg
, u32 type
,
303 switch (mask
& CRYPTO_ALG_TYPE_MASK
) {
304 case CRYPTO_ALG_TYPE_AHASH_MASK
:
305 return sizeof(struct crypto_shash
*);
311 static int crypto_shash_init_tfm(struct crypto_tfm
*tfm
,
312 const struct crypto_type
*frontend
)
314 if (frontend
->type
!= CRYPTO_ALG_TYPE_SHASH
)
319 static unsigned int crypto_shash_extsize(struct crypto_alg
*alg
,
320 const struct crypto_type
*frontend
)
322 return alg
->cra_ctxsize
;
325 static void crypto_shash_show(struct seq_file
*m
, struct crypto_alg
*alg
)
326 __attribute__ ((unused
));
327 static void crypto_shash_show(struct seq_file
*m
, struct crypto_alg
*alg
)
329 struct shash_alg
*salg
= __crypto_shash_alg(alg
);
331 seq_printf(m
, "type : shash\n");
332 seq_printf(m
, "blocksize : %u\n", alg
->cra_blocksize
);
333 seq_printf(m
, "digestsize : %u\n", salg
->digestsize
);
334 seq_printf(m
, "descsize : %u\n", salg
->descsize
);
337 static const struct crypto_type crypto_shash_type
= {
338 .ctxsize
= crypto_shash_ctxsize
,
339 .extsize
= crypto_shash_extsize
,
340 .init
= crypto_init_shash_ops
,
341 .init_tfm
= crypto_shash_init_tfm
,
342 #ifdef CONFIG_PROC_FS
343 .show
= crypto_shash_show
,
345 .maskclear
= ~CRYPTO_ALG_TYPE_MASK
,
346 .maskset
= CRYPTO_ALG_TYPE_MASK
,
347 .type
= CRYPTO_ALG_TYPE_SHASH
,
348 .tfmsize
= offsetof(struct crypto_shash
, base
),
351 struct crypto_shash
*crypto_alloc_shash(const char *alg_name
, u32 type
,
354 return __crypto_shash_cast(
355 crypto_alloc_tfm(alg_name
, &crypto_shash_type
, type
, mask
));
357 EXPORT_SYMBOL_GPL(crypto_alloc_shash
);
359 int crypto_register_shash(struct shash_alg
*alg
)
361 struct crypto_alg
*base
= &alg
->base
;
363 if (alg
->digestsize
> PAGE_SIZE
/ 8 ||
364 alg
->descsize
> PAGE_SIZE
/ 8)
367 base
->cra_type
= &crypto_shash_type
;
368 base
->cra_flags
&= ~CRYPTO_ALG_TYPE_MASK
;
369 base
->cra_flags
|= CRYPTO_ALG_TYPE_SHASH
;
371 return crypto_register_alg(base
);
373 EXPORT_SYMBOL_GPL(crypto_register_shash
);
375 int crypto_unregister_shash(struct shash_alg
*alg
)
377 return crypto_unregister_alg(&alg
->base
);
379 EXPORT_SYMBOL_GPL(crypto_unregister_shash
);
381 MODULE_LICENSE("GPL");
382 MODULE_DESCRIPTION("Synchronous cryptographic hash type");