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/err.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/seq_file.h>
27 static inline struct ahash_alg
*crypto_ahash_alg(struct crypto_ahash
*hash
)
29 return container_of(crypto_hash_alg_common(hash
), struct ahash_alg
,
33 static int hash_walk_next(struct crypto_hash_walk
*walk
)
35 unsigned int alignmask
= walk
->alignmask
;
36 unsigned int offset
= walk
->offset
;
37 unsigned int nbytes
= min(walk
->entrylen
,
38 ((unsigned int)(PAGE_SIZE
)) - offset
);
40 walk
->data
= crypto_kmap(walk
->pg
, 0);
43 if (offset
& alignmask
)
44 nbytes
= alignmask
+ 1 - (offset
& alignmask
);
46 walk
->entrylen
-= nbytes
;
50 static int hash_walk_new_entry(struct crypto_hash_walk
*walk
)
52 struct scatterlist
*sg
;
55 walk
->pg
= sg_page(sg
);
56 walk
->offset
= sg
->offset
;
57 walk
->entrylen
= sg
->length
;
59 if (walk
->entrylen
> walk
->total
)
60 walk
->entrylen
= walk
->total
;
61 walk
->total
-= walk
->entrylen
;
63 return hash_walk_next(walk
);
66 int crypto_hash_walk_done(struct crypto_hash_walk
*walk
, int err
)
68 unsigned int alignmask
= walk
->alignmask
;
69 unsigned int nbytes
= walk
->entrylen
;
71 walk
->data
-= walk
->offset
;
73 if (nbytes
&& walk
->offset
& alignmask
&& !err
) {
74 walk
->offset
+= alignmask
- 1;
75 walk
->offset
= ALIGN(walk
->offset
, alignmask
+ 1);
76 walk
->data
+= walk
->offset
;
79 ((unsigned int)(PAGE_SIZE
)) - walk
->offset
);
80 walk
->entrylen
-= nbytes
;
85 crypto_kunmap(walk
->data
, 0);
86 crypto_yield(walk
->flags
);
94 return hash_walk_next(walk
);
100 walk
->sg
= scatterwalk_sg_next(walk
->sg
);
102 return hash_walk_new_entry(walk
);
104 EXPORT_SYMBOL_GPL(crypto_hash_walk_done
);
106 int crypto_hash_walk_first(struct ahash_request
*req
,
107 struct crypto_hash_walk
*walk
)
109 walk
->total
= req
->nbytes
;
114 walk
->alignmask
= crypto_ahash_alignmask(crypto_ahash_reqtfm(req
));
116 walk
->flags
= req
->base
.flags
;
118 return hash_walk_new_entry(walk
);
120 EXPORT_SYMBOL_GPL(crypto_hash_walk_first
);
122 int crypto_hash_walk_first_compat(struct hash_desc
*hdesc
,
123 struct crypto_hash_walk
*walk
,
124 struct scatterlist
*sg
, unsigned int len
)
131 walk
->alignmask
= crypto_hash_alignmask(hdesc
->tfm
);
133 walk
->flags
= hdesc
->flags
;
135 return hash_walk_new_entry(walk
);
138 static int ahash_setkey_unaligned(struct crypto_ahash
*tfm
, const u8
*key
,
141 struct ahash_alg
*ahash
= crypto_ahash_alg(tfm
);
142 unsigned long alignmask
= crypto_ahash_alignmask(tfm
);
144 u8
*buffer
, *alignbuffer
;
145 unsigned long absize
;
147 absize
= keylen
+ alignmask
;
148 buffer
= kmalloc(absize
, GFP_ATOMIC
);
152 alignbuffer
= (u8
*)ALIGN((unsigned long)buffer
, alignmask
+ 1);
153 memcpy(alignbuffer
, key
, keylen
);
154 ret
= ahash
->setkey(tfm
, alignbuffer
, keylen
);
159 static int ahash_setkey(struct crypto_ahash
*tfm
, const u8
*key
,
162 struct ahash_alg
*ahash
= crypto_ahash_alg(tfm
);
163 unsigned long alignmask
= crypto_ahash_alignmask(tfm
);
165 if ((unsigned long)key
& alignmask
)
166 return ahash_setkey_unaligned(tfm
, key
, keylen
);
168 return ahash
->setkey(tfm
, key
, keylen
);
171 static int ahash_nosetkey(struct crypto_ahash
*tfm
, const u8
*key
,
177 static int crypto_ahash_init_tfm(struct crypto_tfm
*tfm
)
179 struct crypto_ahash
*hash
= __crypto_ahash_cast(tfm
);
180 struct ahash_alg
*alg
= crypto_ahash_alg(hash
);
182 if (tfm
->__crt_alg
->cra_type
!= &crypto_ahash_type
)
183 return crypto_init_shash_ops_async(tfm
);
185 hash
->init
= alg
->init
;
186 hash
->update
= alg
->update
;
187 hash
->final
= alg
->final
;
188 hash
->digest
= alg
->digest
;
189 hash
->setkey
= alg
->setkey
? ahash_setkey
: ahash_nosetkey
;
194 static unsigned int crypto_ahash_extsize(struct crypto_alg
*alg
)
196 if (alg
->cra_type
== &crypto_ahash_type
)
197 return alg
->cra_ctxsize
;
199 return sizeof(struct crypto_shash
*);
202 static void crypto_ahash_show(struct seq_file
*m
, struct crypto_alg
*alg
)
203 __attribute__ ((unused
));
204 static void crypto_ahash_show(struct seq_file
*m
, struct crypto_alg
*alg
)
206 seq_printf(m
, "type : ahash\n");
207 seq_printf(m
, "async : %s\n", alg
->cra_flags
& CRYPTO_ALG_ASYNC
?
209 seq_printf(m
, "blocksize : %u\n", alg
->cra_blocksize
);
210 seq_printf(m
, "digestsize : %u\n",
211 __crypto_hash_alg_common(alg
)->digestsize
);
214 const struct crypto_type crypto_ahash_type
= {
215 .extsize
= crypto_ahash_extsize
,
216 .init_tfm
= crypto_ahash_init_tfm
,
217 #ifdef CONFIG_PROC_FS
218 .show
= crypto_ahash_show
,
220 .maskclear
= ~CRYPTO_ALG_TYPE_MASK
,
221 .maskset
= CRYPTO_ALG_TYPE_AHASH_MASK
,
222 .type
= CRYPTO_ALG_TYPE_AHASH
,
223 .tfmsize
= offsetof(struct crypto_ahash
, base
),
225 EXPORT_SYMBOL_GPL(crypto_ahash_type
);
227 struct crypto_ahash
*crypto_alloc_ahash(const char *alg_name
, u32 type
,
230 return crypto_alloc_tfm(alg_name
, &crypto_ahash_type
, type
, mask
);
232 EXPORT_SYMBOL_GPL(crypto_alloc_ahash
);
234 static int ahash_prepare_alg(struct ahash_alg
*alg
)
236 struct crypto_alg
*base
= &alg
->halg
.base
;
238 if (alg
->halg
.digestsize
> PAGE_SIZE
/ 8 ||
239 alg
->halg
.statesize
> PAGE_SIZE
/ 8)
242 base
->cra_type
= &crypto_ahash_type
;
243 base
->cra_flags
&= ~CRYPTO_ALG_TYPE_MASK
;
244 base
->cra_flags
|= CRYPTO_ALG_TYPE_AHASH
;
249 int crypto_register_ahash(struct ahash_alg
*alg
)
251 struct crypto_alg
*base
= &alg
->halg
.base
;
254 err
= ahash_prepare_alg(alg
);
258 return crypto_register_alg(base
);
260 EXPORT_SYMBOL_GPL(crypto_register_ahash
);
262 int crypto_unregister_ahash(struct ahash_alg
*alg
)
264 return crypto_unregister_alg(&alg
->halg
.base
);
266 EXPORT_SYMBOL_GPL(crypto_unregister_ahash
);
268 int ahash_register_instance(struct crypto_template
*tmpl
,
269 struct ahash_instance
*inst
)
273 err
= ahash_prepare_alg(&inst
->alg
);
277 return crypto_register_instance(tmpl
, ahash_crypto_instance(inst
));
279 EXPORT_SYMBOL_GPL(ahash_register_instance
);
281 void ahash_free_instance(struct crypto_instance
*inst
)
283 crypto_drop_spawn(crypto_instance_ctx(inst
));
284 kfree(ahash_instance(inst
));
286 EXPORT_SYMBOL_GPL(ahash_free_instance
);
288 int crypto_init_ahash_spawn(struct crypto_ahash_spawn
*spawn
,
289 struct hash_alg_common
*alg
,
290 struct crypto_instance
*inst
)
292 return crypto_init_spawn2(&spawn
->base
, &alg
->base
, inst
,
295 EXPORT_SYMBOL_GPL(crypto_init_ahash_spawn
);
297 struct hash_alg_common
*ahash_attr_alg(struct rtattr
*rta
, u32 type
, u32 mask
)
299 struct crypto_alg
*alg
;
301 alg
= crypto_attr_alg2(rta
, &crypto_ahash_type
, type
, mask
);
302 return IS_ERR(alg
) ? ERR_CAST(alg
) : __crypto_hash_alg_common(alg
);
304 EXPORT_SYMBOL_GPL(ahash_attr_alg
);
306 MODULE_LICENSE("GPL");
307 MODULE_DESCRIPTION("Asynchronous cryptographic hash type");