Skip to content

Commit 004a403

Browse files
Loc Hoherbertx
authored andcommitted
[CRYPTO] hash: Add asynchronous hash support
This patch adds asynchronous hash and digest support. Signed-off-by: Loc Ho <lho@amcc.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 534fe2c commit 004a403

8 files changed

Lines changed: 507 additions & 15 deletions

File tree

‎crypto/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ obj-$(CONFIG_CRYPTO_BLKCIPHER) += crypto_blkcipher.o
1919
obj-$(CONFIG_CRYPTO_SEQIV) += seqiv.o
2020

2121
crypto_hash-objs := hash.o
22+
crypto_hash-objs += ahash.o
2223
obj-$(CONFIG_CRYPTO_HASH) += crypto_hash.o
2324

2425
obj-$(CONFIG_CRYPTO_MANAGER) += cryptomgr.o

‎crypto/ahash.c‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Asynchronous Cryptographic Hash operations.
3+
*
4+
* This is the asynchronous version of hash.c with notification of
5+
* completion via a callback.
6+
*
7+
* Copyright (c) 2008 Loc Ho <lho@amcc.com>
8+
*
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)
12+
* any later version.
13+
*
14+
*/
15+
16+
#include <crypto/algapi.h>
17+
#include <linux/err.h>
18+
#include <linux/kernel.h>
19+
#include <linux/module.h>
20+
#include <linux/sched.h>
21+
#include <linux/slab.h>
22+
#include <linux/seq_file.h>
23+
24+
#include "internal.h"
25+
26+
static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
27+
unsigned int keylen)
28+
{
29+
struct ahash_alg *ahash = crypto_ahash_alg(tfm);
30+
unsigned long alignmask = crypto_ahash_alignmask(tfm);
31+
int ret;
32+
u8 *buffer, *alignbuffer;
33+
unsigned long absize;
34+
35+
absize = keylen + alignmask;
36+
buffer = kmalloc(absize, GFP_ATOMIC);
37+
if (!buffer)
38+
return -ENOMEM;
39+
40+
alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
41+
memcpy(alignbuffer, key, keylen);
42+
ret = ahash->setkey(tfm, alignbuffer, keylen);
43+
memset(alignbuffer, 0, keylen);
44+
kfree(buffer);
45+
return ret;
46+
}
47+
48+
static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
49+
unsigned int keylen)
50+
{
51+
struct ahash_alg *ahash = crypto_ahash_alg(tfm);
52+
unsigned long alignmask = crypto_ahash_alignmask(tfm);
53+
54+
if ((unsigned long)key & alignmask)
55+
return ahash_setkey_unaligned(tfm, key, keylen);
56+
57+
return ahash->setkey(tfm, key, keylen);
58+
}
59+
60+
static unsigned int crypto_ahash_ctxsize(struct crypto_alg *alg, u32 type,
61+
u32 mask)
62+
{
63+
return alg->cra_ctxsize;
64+
}
65+
66+
static int crypto_init_ahash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
67+
{
68+
struct ahash_alg *alg = &tfm->__crt_alg->cra_ahash;
69+
struct ahash_tfm *crt = &tfm->crt_ahash;
70+
71+
if (alg->digestsize > crypto_tfm_alg_blocksize(tfm))
72+
return -EINVAL;
73+
74+
crt->init = alg->init;
75+
crt->update = alg->update;
76+
crt->final = alg->final;
77+
crt->digest = alg->digest;
78+
crt->setkey = ahash_setkey;
79+
crt->base = __crypto_ahash_cast(tfm);
80+
crt->digestsize = alg->digestsize;
81+
82+
return 0;
83+
}
84+
85+
static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
86+
__attribute__ ((unused));
87+
static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
88+
{
89+
seq_printf(m, "type : ahash\n");
90+
seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
91+
"yes" : "no");
92+
seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
93+
seq_printf(m, "digestsize : %u\n", alg->cra_hash.digestsize);
94+
}
95+
96+
const struct crypto_type crypto_ahash_type = {
97+
.ctxsize = crypto_ahash_ctxsize,
98+
.init = crypto_init_ahash_ops,
99+
#ifdef CONFIG_PROC_FS
100+
.show = crypto_ahash_show,
101+
#endif
102+
};
103+
EXPORT_SYMBOL_GPL(crypto_ahash_type);
104+
105+
MODULE_LICENSE("GPL");
106+
MODULE_DESCRIPTION("Asynchronous cryptographic hash type");

‎crypto/api.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,12 @@ static int crypto_init_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
235235
return crypto_init_cipher_ops(tfm);
236236

237237
case CRYPTO_ALG_TYPE_DIGEST:
238-
return crypto_init_digest_ops(tfm);
239-
238+
if ((mask & CRYPTO_ALG_TYPE_HASH_MASK) !=
239+
CRYPTO_ALG_TYPE_HASH_MASK)
240+
return crypto_init_digest_ops_async(tfm);
241+
else
242+
return crypto_init_digest_ops(tfm);
243+
240244
case CRYPTO_ALG_TYPE_COMPRESS:
241245
return crypto_init_compress_ops(tfm);
242246

‎crypto/digest.c‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,84 @@ int crypto_init_digest_ops(struct crypto_tfm *tfm)
157157
void crypto_exit_digest_ops(struct crypto_tfm *tfm)
158158
{
159159
}
160+
161+
static int digest_async_nosetkey(struct crypto_ahash *tfm_async, const u8 *key,
162+
unsigned int keylen)
163+
{
164+
crypto_ahash_clear_flags(tfm_async, CRYPTO_TFM_RES_MASK);
165+
return -ENOSYS;
166+
}
167+
168+
static int digest_async_setkey(struct crypto_ahash *tfm_async, const u8 *key,
169+
unsigned int keylen)
170+
{
171+
struct crypto_tfm *tfm = crypto_ahash_tfm(tfm_async);
172+
struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
173+
174+
crypto_ahash_clear_flags(tfm_async, CRYPTO_TFM_RES_MASK);
175+
return dalg->dia_setkey(tfm, key, keylen);
176+
}
177+
178+
static int digest_async_init(struct ahash_request *req)
179+
{
180+
struct crypto_tfm *tfm = req->base.tfm;
181+
struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
182+
183+
dalg->dia_init(tfm);
184+
return 0;
185+
}
186+
187+
static int digest_async_update(struct ahash_request *req)
188+
{
189+
struct crypto_tfm *tfm = req->base.tfm;
190+
struct hash_desc desc = {
191+
.tfm = __crypto_hash_cast(tfm),
192+
.flags = req->base.flags,
193+
};
194+
195+
update(&desc, req->src, req->nbytes);
196+
return 0;
197+
}
198+
199+
static int digest_async_final(struct ahash_request *req)
200+
{
201+
struct crypto_tfm *tfm = req->base.tfm;
202+
struct hash_desc desc = {
203+
.tfm = __crypto_hash_cast(tfm),
204+
.flags = req->base.flags,
205+
};
206+
207+
final(&desc, req->result);
208+
return 0;
209+
}
210+
211+
static int digest_async_digest(struct ahash_request *req)
212+
{
213+
struct crypto_tfm *tfm = req->base.tfm;
214+
struct hash_desc desc = {
215+
.tfm = __crypto_hash_cast(tfm),
216+
.flags = req->base.flags,
217+
};
218+
219+
return digest(&desc, req->src, req->nbytes, req->result);
220+
}
221+
222+
int crypto_init_digest_ops_async(struct crypto_tfm *tfm)
223+
{
224+
struct ahash_tfm *crt = &tfm->crt_ahash;
225+
struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
226+
227+
if (dalg->dia_digestsize > crypto_tfm_alg_blocksize(tfm))
228+
return -EINVAL;
229+
230+
crt->init = digest_async_init;
231+
crt->update = digest_async_update;
232+
crt->final = digest_async_final;
233+
crt->digest = digest_async_digest;
234+
crt->setkey = dalg->dia_setkey ? digest_async_setkey :
235+
digest_async_nosetkey;
236+
crt->digestsize = dalg->dia_digestsize;
237+
crt->base = __crypto_ahash_cast(tfm);
238+
239+
return 0;
240+
}

‎crypto/hash.c‎

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,108 @@ static int hash_setkey(struct crypto_hash *crt, const u8 *key,
5959
return alg->setkey(crt, key, keylen);
6060
}
6161

62-
static int crypto_init_hash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
62+
static int hash_async_setkey(struct crypto_ahash *tfm_async, const u8 *key,
63+
unsigned int keylen)
64+
{
65+
struct crypto_tfm *tfm = crypto_ahash_tfm(tfm_async);
66+
struct crypto_hash *tfm_hash = __crypto_hash_cast(tfm);
67+
struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
68+
69+
return alg->setkey(tfm_hash, key, keylen);
70+
}
71+
72+
static int hash_async_init(struct ahash_request *req)
73+
{
74+
struct crypto_tfm *tfm = req->base.tfm;
75+
struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
76+
struct hash_desc desc = {
77+
.tfm = __crypto_hash_cast(tfm),
78+
.flags = req->base.flags,
79+
};
80+
81+
return alg->init(&desc);
82+
}
83+
84+
static int hash_async_update(struct ahash_request *req)
85+
{
86+
struct crypto_tfm *tfm = req->base.tfm;
87+
struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
88+
struct hash_desc desc = {
89+
.tfm = __crypto_hash_cast(tfm),
90+
.flags = req->base.flags,
91+
};
92+
93+
return alg->update(&desc, req->src, req->nbytes);
94+
}
95+
96+
static int hash_async_final(struct ahash_request *req)
97+
{
98+
struct crypto_tfm *tfm = req->base.tfm;
99+
struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
100+
struct hash_desc desc = {
101+
.tfm = __crypto_hash_cast(tfm),
102+
.flags = req->base.flags,
103+
};
104+
105+
return alg->final(&desc, req->result);
106+
}
107+
108+
static int hash_async_digest(struct ahash_request *req)
109+
{
110+
struct crypto_tfm *tfm = req->base.tfm;
111+
struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
112+
struct hash_desc desc = {
113+
.tfm = __crypto_hash_cast(tfm),
114+
.flags = req->base.flags,
115+
};
116+
117+
return alg->digest(&desc, req->src, req->nbytes, req->result);
118+
}
119+
120+
static int crypto_init_hash_ops_async(struct crypto_tfm *tfm)
121+
{
122+
struct ahash_tfm *crt = &tfm->crt_ahash;
123+
struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
124+
125+
crt->init = hash_async_init;
126+
crt->update = hash_async_update;
127+
crt->final = hash_async_final;
128+
crt->digest = hash_async_digest;
129+
crt->setkey = hash_async_setkey;
130+
crt->digestsize = alg->digestsize;
131+
crt->base = __crypto_ahash_cast(tfm);
132+
133+
return 0;
134+
}
135+
136+
static int crypto_init_hash_ops_sync(struct crypto_tfm *tfm)
63137
{
64138
struct hash_tfm *crt = &tfm->crt_hash;
65139
struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
66140

67-
if (alg->digestsize > crypto_tfm_alg_blocksize(tfm))
68-
return -EINVAL;
69-
70-
crt->init = alg->init;
71-
crt->update = alg->update;
72-
crt->final = alg->final;
73-
crt->digest = alg->digest;
74-
crt->setkey = hash_setkey;
141+
crt->init = alg->init;
142+
crt->update = alg->update;
143+
crt->final = alg->final;
144+
crt->digest = alg->digest;
145+
crt->setkey = hash_setkey;
75146
crt->digestsize = alg->digestsize;
76147

77148
return 0;
78149
}
79150

151+
static int crypto_init_hash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
152+
{
153+
struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
154+
155+
if (alg->digestsize > crypto_tfm_alg_blocksize(tfm))
156+
return -EINVAL;
157+
158+
if ((mask & CRYPTO_ALG_TYPE_HASH_MASK) != CRYPTO_ALG_TYPE_HASH_MASK)
159+
return crypto_init_hash_ops_async(tfm);
160+
else
161+
return crypto_init_hash_ops_sync(tfm);
162+
}
163+
80164
static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg)
81165
__attribute__ ((unused));
82166
static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg)

‎crypto/internal.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type, u32 mask);
8686
struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask);
8787

8888
int crypto_init_digest_ops(struct crypto_tfm *tfm);
89+
int crypto_init_digest_ops_async(struct crypto_tfm *tfm);
8990
int crypto_init_cipher_ops(struct crypto_tfm *tfm);
9091
int crypto_init_compress_ops(struct crypto_tfm *tfm);
9192

‎include/crypto/algapi.h‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ extern const struct crypto_type crypto_ablkcipher_type;
9898
extern const struct crypto_type crypto_aead_type;
9999
extern const struct crypto_type crypto_blkcipher_type;
100100
extern const struct crypto_type crypto_hash_type;
101+
extern const struct crypto_type crypto_ahash_type;
101102

102103
void crypto_mod_put(struct crypto_alg *alg);
103104

@@ -314,5 +315,40 @@ static inline int crypto_requires_sync(u32 type, u32 mask)
314315
return (type ^ CRYPTO_ALG_ASYNC) & mask & CRYPTO_ALG_ASYNC;
315316
}
316317

318+
static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm)
319+
{
320+
return crypto_tfm_ctx(&tfm->base);
321+
}
322+
323+
static inline struct ahash_alg *crypto_ahash_alg(
324+
struct crypto_ahash *tfm)
325+
{
326+
return &crypto_ahash_tfm(tfm)->__crt_alg->cra_ahash;
327+
}
328+
329+
static inline int ahash_enqueue_request(struct crypto_queue *queue,
330+
struct ahash_request *request)
331+
{
332+
return crypto_enqueue_request(queue, &request->base);
333+
}
334+
335+
static inline struct ahash_request *ahash_dequeue_request(
336+
struct crypto_queue *queue)
337+
{
338+
return ahash_request_cast(crypto_dequeue_request(queue));
339+
}
340+
341+
static inline void *ahash_request_ctx(struct ahash_request *req)
342+
{
343+
return req->__ctx;
344+
}
345+
346+
static inline int ahash_tfm_in_queue(struct crypto_queue *queue,
347+
struct crypto_ahash *tfm)
348+
{
349+
return crypto_tfm_in_queue(queue, crypto_ahash_tfm(tfm));
350+
}
351+
352+
317353
#endif /* _CRYPTO_ALGAPI_H */
318354

0 commit comments

Comments
 (0)