xref: /openbmc/linux/crypto/ahash.c (revision 20036252fc61c624a49770fb89684ea5cfdfa05e)
1004a403cSLoc Ho /*
2004a403cSLoc Ho  * Asynchronous Cryptographic Hash operations.
3004a403cSLoc Ho  *
4004a403cSLoc Ho  * This is the asynchronous version of hash.c with notification of
5004a403cSLoc Ho  * completion via a callback.
6004a403cSLoc Ho  *
7004a403cSLoc Ho  * Copyright (c) 2008 Loc Ho <lho@amcc.com>
8004a403cSLoc Ho  *
9004a403cSLoc Ho  * This program is free software; you can redistribute it and/or modify it
10004a403cSLoc Ho  * under the terms of the GNU General Public License as published by the Free
11004a403cSLoc Ho  * Software Foundation; either version 2 of the License, or (at your option)
12004a403cSLoc Ho  * any later version.
13004a403cSLoc Ho  *
14004a403cSLoc Ho  */
15004a403cSLoc Ho 
16*20036252SHerbert Xu #include <crypto/internal/hash.h>
17*20036252SHerbert Xu #include <crypto/scatterwalk.h>
18004a403cSLoc Ho #include <linux/err.h>
19004a403cSLoc Ho #include <linux/kernel.h>
20004a403cSLoc Ho #include <linux/module.h>
21004a403cSLoc Ho #include <linux/sched.h>
22004a403cSLoc Ho #include <linux/slab.h>
23004a403cSLoc Ho #include <linux/seq_file.h>
24004a403cSLoc Ho 
25004a403cSLoc Ho #include "internal.h"
26004a403cSLoc Ho 
27*20036252SHerbert Xu static int hash_walk_next(struct crypto_hash_walk *walk)
28*20036252SHerbert Xu {
29*20036252SHerbert Xu 	unsigned int alignmask = walk->alignmask;
30*20036252SHerbert Xu 	unsigned int offset = walk->offset;
31*20036252SHerbert Xu 	unsigned int nbytes = min(walk->entrylen,
32*20036252SHerbert Xu 				  ((unsigned int)(PAGE_SIZE)) - offset);
33*20036252SHerbert Xu 
34*20036252SHerbert Xu 	walk->data = crypto_kmap(walk->pg, 0);
35*20036252SHerbert Xu 	walk->data += offset;
36*20036252SHerbert Xu 
37*20036252SHerbert Xu 	if (offset & alignmask)
38*20036252SHerbert Xu 		nbytes = alignmask + 1 - (offset & alignmask);
39*20036252SHerbert Xu 
40*20036252SHerbert Xu 	walk->entrylen -= nbytes;
41*20036252SHerbert Xu 	return nbytes;
42*20036252SHerbert Xu }
43*20036252SHerbert Xu 
44*20036252SHerbert Xu static int hash_walk_new_entry(struct crypto_hash_walk *walk)
45*20036252SHerbert Xu {
46*20036252SHerbert Xu 	struct scatterlist *sg;
47*20036252SHerbert Xu 
48*20036252SHerbert Xu 	sg = walk->sg;
49*20036252SHerbert Xu 	walk->pg = sg_page(sg);
50*20036252SHerbert Xu 	walk->offset = sg->offset;
51*20036252SHerbert Xu 	walk->entrylen = sg->length;
52*20036252SHerbert Xu 
53*20036252SHerbert Xu 	if (walk->entrylen > walk->total)
54*20036252SHerbert Xu 		walk->entrylen = walk->total;
55*20036252SHerbert Xu 	walk->total -= walk->entrylen;
56*20036252SHerbert Xu 
57*20036252SHerbert Xu 	return hash_walk_next(walk);
58*20036252SHerbert Xu }
59*20036252SHerbert Xu 
60*20036252SHerbert Xu int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
61*20036252SHerbert Xu {
62*20036252SHerbert Xu 	unsigned int alignmask = walk->alignmask;
63*20036252SHerbert Xu 	unsigned int nbytes = walk->entrylen;
64*20036252SHerbert Xu 
65*20036252SHerbert Xu 	walk->data -= walk->offset;
66*20036252SHerbert Xu 
67*20036252SHerbert Xu 	if (nbytes && walk->offset & alignmask && !err) {
68*20036252SHerbert Xu 		walk->offset += alignmask - 1;
69*20036252SHerbert Xu 		walk->offset = ALIGN(walk->offset, alignmask + 1);
70*20036252SHerbert Xu 		walk->data += walk->offset;
71*20036252SHerbert Xu 
72*20036252SHerbert Xu 		nbytes = min(nbytes,
73*20036252SHerbert Xu 			     ((unsigned int)(PAGE_SIZE)) - walk->offset);
74*20036252SHerbert Xu 		walk->entrylen -= nbytes;
75*20036252SHerbert Xu 
76*20036252SHerbert Xu 		return nbytes;
77*20036252SHerbert Xu 	}
78*20036252SHerbert Xu 
79*20036252SHerbert Xu 	crypto_kunmap(walk->data, 0);
80*20036252SHerbert Xu 	crypto_yield(walk->flags);
81*20036252SHerbert Xu 
82*20036252SHerbert Xu 	if (err)
83*20036252SHerbert Xu 		return err;
84*20036252SHerbert Xu 
85*20036252SHerbert Xu 	walk->offset = 0;
86*20036252SHerbert Xu 
87*20036252SHerbert Xu 	if (nbytes)
88*20036252SHerbert Xu 		return hash_walk_next(walk);
89*20036252SHerbert Xu 
90*20036252SHerbert Xu 	if (!walk->total)
91*20036252SHerbert Xu 		return 0;
92*20036252SHerbert Xu 
93*20036252SHerbert Xu 	walk->sg = scatterwalk_sg_next(walk->sg);
94*20036252SHerbert Xu 
95*20036252SHerbert Xu 	return hash_walk_new_entry(walk);
96*20036252SHerbert Xu }
97*20036252SHerbert Xu EXPORT_SYMBOL_GPL(crypto_hash_walk_done);
98*20036252SHerbert Xu 
99*20036252SHerbert Xu int crypto_hash_walk_first(struct ahash_request *req,
100*20036252SHerbert Xu 			   struct crypto_hash_walk *walk)
101*20036252SHerbert Xu {
102*20036252SHerbert Xu 	walk->total = req->nbytes;
103*20036252SHerbert Xu 
104*20036252SHerbert Xu 	if (!walk->total)
105*20036252SHerbert Xu 		return 0;
106*20036252SHerbert Xu 
107*20036252SHerbert Xu 	walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
108*20036252SHerbert Xu 	walk->sg = req->src;
109*20036252SHerbert Xu 	walk->flags = req->base.flags;
110*20036252SHerbert Xu 
111*20036252SHerbert Xu 	return hash_walk_new_entry(walk);
112*20036252SHerbert Xu }
113*20036252SHerbert Xu EXPORT_SYMBOL_GPL(crypto_hash_walk_first);
114*20036252SHerbert Xu 
115004a403cSLoc Ho static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
116004a403cSLoc Ho 				unsigned int keylen)
117004a403cSLoc Ho {
118004a403cSLoc Ho 	struct ahash_alg *ahash = crypto_ahash_alg(tfm);
119004a403cSLoc Ho 	unsigned long alignmask = crypto_ahash_alignmask(tfm);
120004a403cSLoc Ho 	int ret;
121004a403cSLoc Ho 	u8 *buffer, *alignbuffer;
122004a403cSLoc Ho 	unsigned long absize;
123004a403cSLoc Ho 
124004a403cSLoc Ho 	absize = keylen + alignmask;
125004a403cSLoc Ho 	buffer = kmalloc(absize, GFP_ATOMIC);
126004a403cSLoc Ho 	if (!buffer)
127004a403cSLoc Ho 		return -ENOMEM;
128004a403cSLoc Ho 
129004a403cSLoc Ho 	alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
130004a403cSLoc Ho 	memcpy(alignbuffer, key, keylen);
131004a403cSLoc Ho 	ret = ahash->setkey(tfm, alignbuffer, keylen);
132004a403cSLoc Ho 	memset(alignbuffer, 0, keylen);
133004a403cSLoc Ho 	kfree(buffer);
134004a403cSLoc Ho 	return ret;
135004a403cSLoc Ho }
136004a403cSLoc Ho 
137004a403cSLoc Ho static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
138004a403cSLoc Ho 			unsigned int keylen)
139004a403cSLoc Ho {
140004a403cSLoc Ho 	struct ahash_alg *ahash = crypto_ahash_alg(tfm);
141004a403cSLoc Ho 	unsigned long alignmask = crypto_ahash_alignmask(tfm);
142004a403cSLoc Ho 
143004a403cSLoc Ho 	if ((unsigned long)key & alignmask)
144004a403cSLoc Ho 		return ahash_setkey_unaligned(tfm, key, keylen);
145004a403cSLoc Ho 
146004a403cSLoc Ho 	return ahash->setkey(tfm, key, keylen);
147004a403cSLoc Ho }
148004a403cSLoc Ho 
149004a403cSLoc Ho static unsigned int crypto_ahash_ctxsize(struct crypto_alg *alg, u32 type,
150004a403cSLoc Ho 					u32 mask)
151004a403cSLoc Ho {
152004a403cSLoc Ho 	return alg->cra_ctxsize;
153004a403cSLoc Ho }
154004a403cSLoc Ho 
155004a403cSLoc Ho static int crypto_init_ahash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
156004a403cSLoc Ho {
157004a403cSLoc Ho 	struct ahash_alg *alg = &tfm->__crt_alg->cra_ahash;
158004a403cSLoc Ho 	struct ahash_tfm *crt   = &tfm->crt_ahash;
159004a403cSLoc Ho 
160ca786dc7SHerbert Xu 	if (alg->digestsize > PAGE_SIZE / 8)
161004a403cSLoc Ho 		return -EINVAL;
162004a403cSLoc Ho 
163004a403cSLoc Ho 	crt->init = alg->init;
164004a403cSLoc Ho 	crt->update = alg->update;
165004a403cSLoc Ho 	crt->final  = alg->final;
166004a403cSLoc Ho 	crt->digest = alg->digest;
167004a403cSLoc Ho 	crt->setkey = ahash_setkey;
168004a403cSLoc Ho 	crt->digestsize = alg->digestsize;
169004a403cSLoc Ho 
170004a403cSLoc Ho 	return 0;
171004a403cSLoc Ho }
172004a403cSLoc Ho 
173004a403cSLoc Ho static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
174004a403cSLoc Ho 	__attribute__ ((unused));
175004a403cSLoc Ho static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
176004a403cSLoc Ho {
177004a403cSLoc Ho 	seq_printf(m, "type         : ahash\n");
178004a403cSLoc Ho 	seq_printf(m, "async        : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
179004a403cSLoc Ho 					     "yes" : "no");
180004a403cSLoc Ho 	seq_printf(m, "blocksize    : %u\n", alg->cra_blocksize);
181004a403cSLoc Ho 	seq_printf(m, "digestsize   : %u\n", alg->cra_hash.digestsize);
182004a403cSLoc Ho }
183004a403cSLoc Ho 
184004a403cSLoc Ho const struct crypto_type crypto_ahash_type = {
185004a403cSLoc Ho 	.ctxsize = crypto_ahash_ctxsize,
186004a403cSLoc Ho 	.init = crypto_init_ahash_ops,
187004a403cSLoc Ho #ifdef CONFIG_PROC_FS
188004a403cSLoc Ho 	.show = crypto_ahash_show,
189004a403cSLoc Ho #endif
190004a403cSLoc Ho };
191004a403cSLoc Ho EXPORT_SYMBOL_GPL(crypto_ahash_type);
192004a403cSLoc Ho 
193004a403cSLoc Ho MODULE_LICENSE("GPL");
194004a403cSLoc Ho MODULE_DESCRIPTION("Asynchronous cryptographic hash type");
195