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 int crypto_shash_import(struct shash_desc
*desc
, const u8
*in
)
177 struct crypto_shash
*tfm
= desc
->tfm
;
178 struct shash_alg
*alg
= crypto_shash_alg(tfm
);
180 memcpy(shash_desc_ctx(desc
), in
, crypto_shash_descsize(tfm
));
187 EXPORT_SYMBOL_GPL(crypto_shash_import
);
189 static int shash_async_setkey(struct crypto_ahash
*tfm
, const u8
*key
,
192 struct crypto_shash
**ctx
= crypto_ahash_ctx(tfm
);
194 return crypto_shash_setkey(*ctx
, key
, keylen
);
197 static int shash_async_init(struct ahash_request
*req
)
199 struct crypto_shash
**ctx
= crypto_ahash_ctx(crypto_ahash_reqtfm(req
));
200 struct shash_desc
*desc
= ahash_request_ctx(req
);
203 desc
->flags
= req
->base
.flags
;
205 return crypto_shash_init(desc
);
208 static int shash_async_update(struct ahash_request
*req
)
210 struct shash_desc
*desc
= ahash_request_ctx(req
);
211 struct crypto_hash_walk walk
;
214 for (nbytes
= crypto_hash_walk_first(req
, &walk
); nbytes
> 0;
215 nbytes
= crypto_hash_walk_done(&walk
, nbytes
))
216 nbytes
= crypto_shash_update(desc
, walk
.data
, nbytes
);
221 static int shash_async_final(struct ahash_request
*req
)
223 return crypto_shash_final(ahash_request_ctx(req
), req
->result
);
226 static int shash_async_digest(struct ahash_request
*req
)
228 struct scatterlist
*sg
= req
->src
;
229 unsigned int offset
= sg
->offset
;
230 unsigned int nbytes
= req
->nbytes
;
233 if (nbytes
< min(sg
->length
, ((unsigned int)(PAGE_SIZE
)) - offset
)) {
234 struct crypto_shash
**ctx
=
235 crypto_ahash_ctx(crypto_ahash_reqtfm(req
));
236 struct shash_desc
*desc
= ahash_request_ctx(req
);
240 desc
->flags
= req
->base
.flags
;
242 data
= crypto_kmap(sg_page(sg
), 0);
243 err
= crypto_shash_digest(desc
, data
+ offset
, nbytes
,
245 crypto_kunmap(data
, 0);
246 crypto_yield(desc
->flags
);
250 err
= shash_async_init(req
);
254 err
= shash_async_update(req
);
258 err
= shash_async_final(req
);
264 static void crypto_exit_shash_ops_async(struct crypto_tfm
*tfm
)
266 struct crypto_shash
**ctx
= crypto_tfm_ctx(tfm
);
268 crypto_free_shash(*ctx
);
271 static int crypto_init_shash_ops_async(struct crypto_tfm
*tfm
)
273 struct crypto_alg
*calg
= tfm
->__crt_alg
;
274 struct shash_alg
*alg
= __crypto_shash_alg(calg
);
275 struct ahash_tfm
*crt
= &tfm
->crt_ahash
;
276 struct crypto_shash
**ctx
= crypto_tfm_ctx(tfm
);
277 struct crypto_shash
*shash
;
279 if (!crypto_mod_get(calg
))
282 shash
= __crypto_shash_cast(crypto_create_tfm(
283 calg
, &crypto_shash_type
));
285 crypto_mod_put(calg
);
286 return PTR_ERR(shash
);
290 tfm
->exit
= crypto_exit_shash_ops_async
;
292 crt
->init
= shash_async_init
;
293 crt
->update
= shash_async_update
;
294 crt
->final
= shash_async_final
;
295 crt
->digest
= shash_async_digest
;
296 crt
->setkey
= shash_async_setkey
;
298 crt
->digestsize
= alg
->digestsize
;
299 crt
->reqsize
= sizeof(struct shash_desc
) + crypto_shash_descsize(shash
);
304 static int crypto_init_shash_ops(struct crypto_tfm
*tfm
, u32 type
, u32 mask
)
306 switch (mask
& CRYPTO_ALG_TYPE_MASK
) {
307 case CRYPTO_ALG_TYPE_AHASH_MASK
:
308 return crypto_init_shash_ops_async(tfm
);
314 static unsigned int crypto_shash_ctxsize(struct crypto_alg
*alg
, u32 type
,
317 switch (mask
& CRYPTO_ALG_TYPE_MASK
) {
318 case CRYPTO_ALG_TYPE_AHASH_MASK
:
319 return sizeof(struct crypto_shash
*);
325 static int crypto_shash_init_tfm(struct crypto_tfm
*tfm
,
326 const struct crypto_type
*frontend
)
328 if (frontend
->type
!= CRYPTO_ALG_TYPE_SHASH
)
333 static unsigned int crypto_shash_extsize(struct crypto_alg
*alg
,
334 const struct crypto_type
*frontend
)
336 return alg
->cra_ctxsize
;
339 static void crypto_shash_show(struct seq_file
*m
, struct crypto_alg
*alg
)
340 __attribute__ ((unused
));
341 static void crypto_shash_show(struct seq_file
*m
, struct crypto_alg
*alg
)
343 struct shash_alg
*salg
= __crypto_shash_alg(alg
);
345 seq_printf(m
, "type : shash\n");
346 seq_printf(m
, "blocksize : %u\n", alg
->cra_blocksize
);
347 seq_printf(m
, "digestsize : %u\n", salg
->digestsize
);
348 seq_printf(m
, "descsize : %u\n", salg
->descsize
);
351 static const struct crypto_type crypto_shash_type
= {
352 .ctxsize
= crypto_shash_ctxsize
,
353 .extsize
= crypto_shash_extsize
,
354 .init
= crypto_init_shash_ops
,
355 .init_tfm
= crypto_shash_init_tfm
,
356 #ifdef CONFIG_PROC_FS
357 .show
= crypto_shash_show
,
359 .maskclear
= ~CRYPTO_ALG_TYPE_MASK
,
360 .maskset
= CRYPTO_ALG_TYPE_MASK
,
361 .type
= CRYPTO_ALG_TYPE_SHASH
,
362 .tfmsize
= offsetof(struct crypto_shash
, base
),
365 struct crypto_shash
*crypto_alloc_shash(const char *alg_name
, u32 type
,
368 return __crypto_shash_cast(
369 crypto_alloc_tfm(alg_name
, &crypto_shash_type
, type
, mask
));
371 EXPORT_SYMBOL_GPL(crypto_alloc_shash
);
373 int crypto_register_shash(struct shash_alg
*alg
)
375 struct crypto_alg
*base
= &alg
->base
;
377 if (alg
->digestsize
> PAGE_SIZE
/ 8 ||
378 alg
->descsize
> PAGE_SIZE
/ 8)
381 base
->cra_type
= &crypto_shash_type
;
382 base
->cra_flags
&= ~CRYPTO_ALG_TYPE_MASK
;
383 base
->cra_flags
|= CRYPTO_ALG_TYPE_SHASH
;
385 return crypto_register_alg(base
);
387 EXPORT_SYMBOL_GPL(crypto_register_shash
);
389 int crypto_unregister_shash(struct shash_alg
*alg
)
391 return crypto_unregister_alg(&alg
->base
);
393 EXPORT_SYMBOL_GPL(crypto_unregister_shash
);
395 MODULE_LICENSE("GPL");
396 MODULE_DESCRIPTION("Synchronous cryptographic hash type");