drm/i915: use I915_GEM_GPU_DOMAINS
[linux/fpc-iii.git] / crypto / ahash.c
blobf3476374f764cf18dcf76c806d86fd350569bf1d
1 /*
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)
12 * any later version.
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>
25 #include "internal.h"
27 static int hash_walk_next(struct crypto_hash_walk *walk)
29 unsigned int alignmask = walk->alignmask;
30 unsigned int offset = walk->offset;
31 unsigned int nbytes = min(walk->entrylen,
32 ((unsigned int)(PAGE_SIZE)) - offset);
34 walk->data = crypto_kmap(walk->pg, 0);
35 walk->data += offset;
37 if (offset & alignmask)
38 nbytes = alignmask + 1 - (offset & alignmask);
40 walk->entrylen -= nbytes;
41 return nbytes;
44 static int hash_walk_new_entry(struct crypto_hash_walk *walk)
46 struct scatterlist *sg;
48 sg = walk->sg;
49 walk->pg = sg_page(sg);
50 walk->offset = sg->offset;
51 walk->entrylen = sg->length;
53 if (walk->entrylen > walk->total)
54 walk->entrylen = walk->total;
55 walk->total -= walk->entrylen;
57 return hash_walk_next(walk);
60 int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
62 unsigned int alignmask = walk->alignmask;
63 unsigned int nbytes = walk->entrylen;
65 walk->data -= walk->offset;
67 if (nbytes && walk->offset & alignmask && !err) {
68 walk->offset += alignmask - 1;
69 walk->offset = ALIGN(walk->offset, alignmask + 1);
70 walk->data += walk->offset;
72 nbytes = min(nbytes,
73 ((unsigned int)(PAGE_SIZE)) - walk->offset);
74 walk->entrylen -= nbytes;
76 return nbytes;
79 crypto_kunmap(walk->data, 0);
80 crypto_yield(walk->flags);
82 if (err)
83 return err;
85 if (nbytes) {
86 walk->offset = 0;
87 walk->pg++;
88 return hash_walk_next(walk);
91 if (!walk->total)
92 return 0;
94 walk->sg = scatterwalk_sg_next(walk->sg);
96 return hash_walk_new_entry(walk);
98 EXPORT_SYMBOL_GPL(crypto_hash_walk_done);
100 int crypto_hash_walk_first(struct ahash_request *req,
101 struct crypto_hash_walk *walk)
103 walk->total = req->nbytes;
105 if (!walk->total)
106 return 0;
108 walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
109 walk->sg = req->src;
110 walk->flags = req->base.flags;
112 return hash_walk_new_entry(walk);
114 EXPORT_SYMBOL_GPL(crypto_hash_walk_first);
116 int crypto_hash_walk_first_compat(struct hash_desc *hdesc,
117 struct crypto_hash_walk *walk,
118 struct scatterlist *sg, unsigned int len)
120 walk->total = len;
122 if (!walk->total)
123 return 0;
125 walk->alignmask = crypto_hash_alignmask(hdesc->tfm);
126 walk->sg = sg;
127 walk->flags = hdesc->flags;
129 return hash_walk_new_entry(walk);
132 static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
133 unsigned int keylen)
135 struct ahash_alg *ahash = crypto_ahash_alg(tfm);
136 unsigned long alignmask = crypto_ahash_alignmask(tfm);
137 int ret;
138 u8 *buffer, *alignbuffer;
139 unsigned long absize;
141 absize = keylen + alignmask;
142 buffer = kmalloc(absize, GFP_ATOMIC);
143 if (!buffer)
144 return -ENOMEM;
146 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
147 memcpy(alignbuffer, key, keylen);
148 ret = ahash->setkey(tfm, alignbuffer, keylen);
149 memset(alignbuffer, 0, keylen);
150 kfree(buffer);
151 return ret;
154 static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
155 unsigned int keylen)
157 struct ahash_alg *ahash = crypto_ahash_alg(tfm);
158 unsigned long alignmask = crypto_ahash_alignmask(tfm);
160 if ((unsigned long)key & alignmask)
161 return ahash_setkey_unaligned(tfm, key, keylen);
163 return ahash->setkey(tfm, key, keylen);
166 static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
167 unsigned int keylen)
169 return -ENOSYS;
172 int crypto_ahash_import(struct ahash_request *req, const u8 *in)
174 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
175 struct ahash_alg *alg = crypto_ahash_alg(tfm);
177 memcpy(ahash_request_ctx(req), in, crypto_ahash_reqsize(tfm));
179 if (alg->reinit)
180 alg->reinit(req);
182 return 0;
184 EXPORT_SYMBOL_GPL(crypto_ahash_import);
186 static unsigned int crypto_ahash_ctxsize(struct crypto_alg *alg, u32 type,
187 u32 mask)
189 return alg->cra_ctxsize;
192 static int crypto_init_ahash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
194 struct ahash_alg *alg = &tfm->__crt_alg->cra_ahash;
195 struct ahash_tfm *crt = &tfm->crt_ahash;
197 if (alg->digestsize > PAGE_SIZE / 8)
198 return -EINVAL;
200 crt->init = alg->init;
201 crt->update = alg->update;
202 crt->final = alg->final;
203 crt->digest = alg->digest;
204 crt->setkey = alg->setkey ? ahash_setkey : ahash_nosetkey;
205 crt->digestsize = alg->digestsize;
207 return 0;
210 static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
211 __attribute__ ((unused));
212 static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
214 seq_printf(m, "type : ahash\n");
215 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
216 "yes" : "no");
217 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
218 seq_printf(m, "digestsize : %u\n", alg->cra_ahash.digestsize);
221 const struct crypto_type crypto_ahash_type = {
222 .ctxsize = crypto_ahash_ctxsize,
223 .init = crypto_init_ahash_ops,
224 #ifdef CONFIG_PROC_FS
225 .show = crypto_ahash_show,
226 #endif
228 EXPORT_SYMBOL_GPL(crypto_ahash_type);
230 MODULE_LICENSE("GPL");
231 MODULE_DESCRIPTION("Asynchronous cryptographic hash type");