2 * Asynchronous Cryptographic Hash operations.
4 * This is the asynchronous version of hash.c with notification of
5 * completion via a callback.
7 * Copyright (c) 2008 Loc Ho <lho@amcc.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
16 #include <crypto/internal/hash.h>
17 #include <crypto/scatterwalk.h>
18 #include <linux/bug.h>
19 #include <linux/err.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <linux/seq_file.h>
25 #include <linux/cryptouser.h>
26 #include <net/netlink.h>
30 struct ahash_request_priv
{
31 crypto_completion_t complete
;
34 void *ubuf
[] CRYPTO_MINALIGN_ATTR
;
37 static inline struct ahash_alg
*crypto_ahash_alg(struct crypto_ahash
*hash
)
39 return container_of(crypto_hash_alg_common(hash
), struct ahash_alg
,
43 static int hash_walk_next(struct crypto_hash_walk
*walk
)
45 unsigned int alignmask
= walk
->alignmask
;
46 unsigned int offset
= walk
->offset
;
47 unsigned int nbytes
= min(walk
->entrylen
,
48 ((unsigned int)(PAGE_SIZE
)) - offset
);
50 if (walk
->flags
& CRYPTO_ALG_ASYNC
)
51 walk
->data
= kmap(walk
->pg
);
53 walk
->data
= kmap_atomic(walk
->pg
);
56 if (offset
& alignmask
) {
57 unsigned int unaligned
= alignmask
+ 1 - (offset
& alignmask
);
58 if (nbytes
> unaligned
)
62 walk
->entrylen
-= nbytes
;
66 static int hash_walk_new_entry(struct crypto_hash_walk
*walk
)
68 struct scatterlist
*sg
;
71 walk
->pg
= sg_page(sg
);
72 walk
->offset
= sg
->offset
;
73 walk
->entrylen
= sg
->length
;
75 if (walk
->entrylen
> walk
->total
)
76 walk
->entrylen
= walk
->total
;
77 walk
->total
-= walk
->entrylen
;
79 return hash_walk_next(walk
);
82 int crypto_hash_walk_done(struct crypto_hash_walk
*walk
, int err
)
84 unsigned int alignmask
= walk
->alignmask
;
85 unsigned int nbytes
= walk
->entrylen
;
87 walk
->data
-= walk
->offset
;
89 if (nbytes
&& walk
->offset
& alignmask
&& !err
) {
90 walk
->offset
= ALIGN(walk
->offset
, alignmask
+ 1);
91 walk
->data
+= walk
->offset
;
94 ((unsigned int)(PAGE_SIZE
)) - walk
->offset
);
95 walk
->entrylen
-= nbytes
;
100 if (walk
->flags
& CRYPTO_ALG_ASYNC
)
103 kunmap_atomic(walk
->data
);
105 * The may sleep test only makes sense for sync users.
106 * Async users don't need to sleep here anyway.
108 crypto_yield(walk
->flags
);
117 return hash_walk_next(walk
);
123 walk
->sg
= scatterwalk_sg_next(walk
->sg
);
125 return hash_walk_new_entry(walk
);
127 EXPORT_SYMBOL_GPL(crypto_hash_walk_done
);
129 int crypto_hash_walk_first(struct ahash_request
*req
,
130 struct crypto_hash_walk
*walk
)
132 walk
->total
= req
->nbytes
;
137 walk
->alignmask
= crypto_ahash_alignmask(crypto_ahash_reqtfm(req
));
139 walk
->flags
= req
->base
.flags
& CRYPTO_TFM_REQ_MASK
;
141 return hash_walk_new_entry(walk
);
143 EXPORT_SYMBOL_GPL(crypto_hash_walk_first
);
145 int crypto_ahash_walk_first(struct ahash_request
*req
,
146 struct crypto_hash_walk
*walk
)
148 walk
->total
= req
->nbytes
;
153 walk
->alignmask
= crypto_ahash_alignmask(crypto_ahash_reqtfm(req
));
155 walk
->flags
= req
->base
.flags
& CRYPTO_TFM_REQ_MASK
;
156 walk
->flags
|= CRYPTO_ALG_ASYNC
;
158 BUILD_BUG_ON(CRYPTO_TFM_REQ_MASK
& CRYPTO_ALG_ASYNC
);
160 return hash_walk_new_entry(walk
);
162 EXPORT_SYMBOL_GPL(crypto_ahash_walk_first
);
164 int crypto_hash_walk_first_compat(struct hash_desc
*hdesc
,
165 struct crypto_hash_walk
*walk
,
166 struct scatterlist
*sg
, unsigned int len
)
173 walk
->alignmask
= crypto_hash_alignmask(hdesc
->tfm
);
175 walk
->flags
= hdesc
->flags
& CRYPTO_TFM_REQ_MASK
;
177 return hash_walk_new_entry(walk
);
180 static int ahash_setkey_unaligned(struct crypto_ahash
*tfm
, const u8
*key
,
183 unsigned long alignmask
= crypto_ahash_alignmask(tfm
);
185 u8
*buffer
, *alignbuffer
;
186 unsigned long absize
;
188 absize
= keylen
+ alignmask
;
189 buffer
= kmalloc(absize
, GFP_KERNEL
);
193 alignbuffer
= (u8
*)ALIGN((unsigned long)buffer
, alignmask
+ 1);
194 memcpy(alignbuffer
, key
, keylen
);
195 ret
= tfm
->setkey(tfm
, alignbuffer
, keylen
);
200 int crypto_ahash_setkey(struct crypto_ahash
*tfm
, const u8
*key
,
203 unsigned long alignmask
= crypto_ahash_alignmask(tfm
);
205 if ((unsigned long)key
& alignmask
)
206 return ahash_setkey_unaligned(tfm
, key
, keylen
);
208 return tfm
->setkey(tfm
, key
, keylen
);
210 EXPORT_SYMBOL_GPL(crypto_ahash_setkey
);
212 static int ahash_nosetkey(struct crypto_ahash
*tfm
, const u8
*key
,
218 static inline unsigned int ahash_align_buffer_size(unsigned len
,
221 return len
+ (mask
& ~(crypto_tfm_ctx_alignment() - 1));
224 static int ahash_save_req(struct ahash_request
*req
, crypto_completion_t cplt
)
226 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
227 unsigned long alignmask
= crypto_ahash_alignmask(tfm
);
228 unsigned int ds
= crypto_ahash_digestsize(tfm
);
229 struct ahash_request_priv
*priv
;
231 priv
= kmalloc(sizeof(*priv
) + ahash_align_buffer_size(ds
, alignmask
),
232 (req
->base
.flags
& CRYPTO_TFM_REQ_MAY_SLEEP
) ?
233 GFP_KERNEL
: GFP_ATOMIC
);
238 * WARNING: Voodoo programming below!
240 * The code below is obscure and hard to understand, thus explanation
241 * is necessary. See include/crypto/hash.h and include/linux/crypto.h
242 * to understand the layout of structures used here!
244 * The code here will replace portions of the ORIGINAL request with
245 * pointers to new code and buffers so the hashing operation can store
246 * the result in aligned buffer. We will call the modified request
247 * an ADJUSTED request.
249 * The newly mangled request will look as such:
252 * .result = ADJUSTED[new aligned buffer]
253 * .base.complete = ADJUSTED[pointer to completion function]
254 * .base.data = ADJUSTED[*req (pointer to self)]
255 * .priv = ADJUSTED[new priv] {
256 * .result = ORIGINAL(result)
257 * .complete = ORIGINAL(base.complete)
258 * .data = ORIGINAL(base.data)
262 priv
->result
= req
->result
;
263 priv
->complete
= req
->base
.complete
;
264 priv
->data
= req
->base
.data
;
266 * WARNING: We do not backup req->priv here! The req->priv
267 * is for internal use of the Crypto API and the
268 * user must _NOT_ _EVER_ depend on it's content!
271 req
->result
= PTR_ALIGN((u8
*)priv
->ubuf
, alignmask
+ 1);
272 req
->base
.complete
= cplt
;
273 req
->base
.data
= req
;
279 static void ahash_restore_req(struct ahash_request
*req
)
281 struct ahash_request_priv
*priv
= req
->priv
;
283 /* Restore the original crypto request. */
284 req
->result
= priv
->result
;
285 req
->base
.complete
= priv
->complete
;
286 req
->base
.data
= priv
->data
;
289 /* Free the req->priv.priv from the ADJUSTED request. */
293 static void ahash_op_unaligned_finish(struct ahash_request
*req
, int err
)
295 struct ahash_request_priv
*priv
= req
->priv
;
297 if (err
== -EINPROGRESS
)
301 memcpy(priv
->result
, req
->result
,
302 crypto_ahash_digestsize(crypto_ahash_reqtfm(req
)));
304 ahash_restore_req(req
);
307 static void ahash_op_unaligned_done(struct crypto_async_request
*req
, int err
)
309 struct ahash_request
*areq
= req
->data
;
312 * Restore the original request, see ahash_op_unaligned() for what
315 * The "struct ahash_request *req" here is in fact the "req.base"
316 * from the ADJUSTED request from ahash_op_unaligned(), thus as it
317 * is a pointer to self, it is also the ADJUSTED "req" .
320 /* First copy req->result into req->priv.result */
321 ahash_op_unaligned_finish(areq
, err
);
323 /* Complete the ORIGINAL request. */
324 areq
->base
.complete(&areq
->base
, err
);
327 static int ahash_op_unaligned(struct ahash_request
*req
,
328 int (*op
)(struct ahash_request
*))
332 err
= ahash_save_req(req
, ahash_op_unaligned_done
);
337 ahash_op_unaligned_finish(req
, err
);
342 static int crypto_ahash_op(struct ahash_request
*req
,
343 int (*op
)(struct ahash_request
*))
345 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
346 unsigned long alignmask
= crypto_ahash_alignmask(tfm
);
348 if ((unsigned long)req
->result
& alignmask
)
349 return ahash_op_unaligned(req
, op
);
354 int crypto_ahash_final(struct ahash_request
*req
)
356 return crypto_ahash_op(req
, crypto_ahash_reqtfm(req
)->final
);
358 EXPORT_SYMBOL_GPL(crypto_ahash_final
);
360 int crypto_ahash_finup(struct ahash_request
*req
)
362 return crypto_ahash_op(req
, crypto_ahash_reqtfm(req
)->finup
);
364 EXPORT_SYMBOL_GPL(crypto_ahash_finup
);
366 int crypto_ahash_digest(struct ahash_request
*req
)
368 return crypto_ahash_op(req
, crypto_ahash_reqtfm(req
)->digest
);
370 EXPORT_SYMBOL_GPL(crypto_ahash_digest
);
372 static void ahash_def_finup_finish2(struct ahash_request
*req
, int err
)
374 struct ahash_request_priv
*priv
= req
->priv
;
376 if (err
== -EINPROGRESS
)
380 memcpy(priv
->result
, req
->result
,
381 crypto_ahash_digestsize(crypto_ahash_reqtfm(req
)));
383 ahash_restore_req(req
);
386 static void ahash_def_finup_done2(struct crypto_async_request
*req
, int err
)
388 struct ahash_request
*areq
= req
->data
;
390 ahash_def_finup_finish2(areq
, err
);
392 areq
->base
.complete(&areq
->base
, err
);
395 static int ahash_def_finup_finish1(struct ahash_request
*req
, int err
)
400 req
->base
.complete
= ahash_def_finup_done2
;
401 req
->base
.flags
&= ~CRYPTO_TFM_REQ_MAY_SLEEP
;
402 err
= crypto_ahash_reqtfm(req
)->final(req
);
405 ahash_def_finup_finish2(req
, err
);
409 static void ahash_def_finup_done1(struct crypto_async_request
*req
, int err
)
411 struct ahash_request
*areq
= req
->data
;
413 err
= ahash_def_finup_finish1(areq
, err
);
415 areq
->base
.complete(&areq
->base
, err
);
418 static int ahash_def_finup(struct ahash_request
*req
)
420 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
423 err
= ahash_save_req(req
, ahash_def_finup_done1
);
427 err
= tfm
->update(req
);
428 return ahash_def_finup_finish1(req
, err
);
431 static int ahash_no_export(struct ahash_request
*req
, void *out
)
436 static int ahash_no_import(struct ahash_request
*req
, const void *in
)
441 static int crypto_ahash_init_tfm(struct crypto_tfm
*tfm
)
443 struct crypto_ahash
*hash
= __crypto_ahash_cast(tfm
);
444 struct ahash_alg
*alg
= crypto_ahash_alg(hash
);
446 hash
->setkey
= ahash_nosetkey
;
447 hash
->has_setkey
= false;
448 hash
->export
= ahash_no_export
;
449 hash
->import
= ahash_no_import
;
451 if (tfm
->__crt_alg
->cra_type
!= &crypto_ahash_type
)
452 return crypto_init_shash_ops_async(tfm
);
454 hash
->init
= alg
->init
;
455 hash
->update
= alg
->update
;
456 hash
->final
= alg
->final
;
457 hash
->finup
= alg
->finup
?: ahash_def_finup
;
458 hash
->digest
= alg
->digest
;
461 hash
->setkey
= alg
->setkey
;
462 hash
->has_setkey
= true;
465 hash
->export
= alg
->export
;
467 hash
->import
= alg
->import
;
472 static unsigned int crypto_ahash_extsize(struct crypto_alg
*alg
)
474 if (alg
->cra_type
== &crypto_ahash_type
)
475 return alg
->cra_ctxsize
;
477 return sizeof(struct crypto_shash
*);
481 static int crypto_ahash_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
483 struct crypto_report_hash rhash
;
485 strncpy(rhash
.type
, "ahash", sizeof(rhash
.type
));
487 rhash
.blocksize
= alg
->cra_blocksize
;
488 rhash
.digestsize
= __crypto_hash_alg_common(alg
)->digestsize
;
490 if (nla_put(skb
, CRYPTOCFGA_REPORT_HASH
,
491 sizeof(struct crypto_report_hash
), &rhash
))
492 goto nla_put_failure
;
499 static int crypto_ahash_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
505 static void crypto_ahash_show(struct seq_file
*m
, struct crypto_alg
*alg
)
506 __attribute__ ((unused
));
507 static void crypto_ahash_show(struct seq_file
*m
, struct crypto_alg
*alg
)
509 seq_printf(m
, "type : ahash\n");
510 seq_printf(m
, "async : %s\n", alg
->cra_flags
& CRYPTO_ALG_ASYNC
?
512 seq_printf(m
, "blocksize : %u\n", alg
->cra_blocksize
);
513 seq_printf(m
, "digestsize : %u\n",
514 __crypto_hash_alg_common(alg
)->digestsize
);
517 const struct crypto_type crypto_ahash_type
= {
518 .extsize
= crypto_ahash_extsize
,
519 .init_tfm
= crypto_ahash_init_tfm
,
520 #ifdef CONFIG_PROC_FS
521 .show
= crypto_ahash_show
,
523 .report
= crypto_ahash_report
,
524 .maskclear
= ~CRYPTO_ALG_TYPE_MASK
,
525 .maskset
= CRYPTO_ALG_TYPE_AHASH_MASK
,
526 .type
= CRYPTO_ALG_TYPE_AHASH
,
527 .tfmsize
= offsetof(struct crypto_ahash
, base
),
529 EXPORT_SYMBOL_GPL(crypto_ahash_type
);
531 struct crypto_ahash
*crypto_alloc_ahash(const char *alg_name
, u32 type
,
534 return crypto_alloc_tfm(alg_name
, &crypto_ahash_type
, type
, mask
);
536 EXPORT_SYMBOL_GPL(crypto_alloc_ahash
);
538 static int ahash_prepare_alg(struct ahash_alg
*alg
)
540 struct crypto_alg
*base
= &alg
->halg
.base
;
542 if (alg
->halg
.digestsize
> PAGE_SIZE
/ 8 ||
543 alg
->halg
.statesize
> PAGE_SIZE
/ 8 ||
544 alg
->halg
.statesize
== 0)
547 base
->cra_type
= &crypto_ahash_type
;
548 base
->cra_flags
&= ~CRYPTO_ALG_TYPE_MASK
;
549 base
->cra_flags
|= CRYPTO_ALG_TYPE_AHASH
;
554 int crypto_register_ahash(struct ahash_alg
*alg
)
556 struct crypto_alg
*base
= &alg
->halg
.base
;
559 err
= ahash_prepare_alg(alg
);
563 return crypto_register_alg(base
);
565 EXPORT_SYMBOL_GPL(crypto_register_ahash
);
567 int crypto_unregister_ahash(struct ahash_alg
*alg
)
569 return crypto_unregister_alg(&alg
->halg
.base
);
571 EXPORT_SYMBOL_GPL(crypto_unregister_ahash
);
573 int ahash_register_instance(struct crypto_template
*tmpl
,
574 struct ahash_instance
*inst
)
578 err
= ahash_prepare_alg(&inst
->alg
);
582 return crypto_register_instance(tmpl
, ahash_crypto_instance(inst
));
584 EXPORT_SYMBOL_GPL(ahash_register_instance
);
586 void ahash_free_instance(struct crypto_instance
*inst
)
588 crypto_drop_spawn(crypto_instance_ctx(inst
));
589 kfree(ahash_instance(inst
));
591 EXPORT_SYMBOL_GPL(ahash_free_instance
);
593 int crypto_init_ahash_spawn(struct crypto_ahash_spawn
*spawn
,
594 struct hash_alg_common
*alg
,
595 struct crypto_instance
*inst
)
597 return crypto_init_spawn2(&spawn
->base
, &alg
->base
, inst
,
600 EXPORT_SYMBOL_GPL(crypto_init_ahash_spawn
);
602 struct hash_alg_common
*ahash_attr_alg(struct rtattr
*rta
, u32 type
, u32 mask
)
604 struct crypto_alg
*alg
;
606 alg
= crypto_attr_alg2(rta
, &crypto_ahash_type
, type
, mask
);
607 return IS_ERR(alg
) ? ERR_CAST(alg
) : __crypto_hash_alg_common(alg
);
609 EXPORT_SYMBOL_GPL(ahash_attr_alg
);
611 MODULE_LICENSE("GPL");
612 MODULE_DESCRIPTION("Asynchronous cryptographic hash type");