blob: 5d3f1303da985417d0ab192c5909aadd5f60ab0b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Cryptographic API.
3 *
4 * Digest operations.
5 *
6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 */
Herbert Xu055bcee2006-08-19 22:24:23 +100014
Herbert Xu18e33e62008-07-10 16:01:22 +080015#include <crypto/internal/hash.h>
Herbert Xu42c271c2007-12-07 18:52:49 +080016#include <crypto/scatterwalk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mm.h>
18#include <linux/errno.h>
Herbert Xufb469842006-12-10 10:45:28 +110019#include <linux/hardirq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/highmem.h>
Herbert Xufb469842006-12-10 10:45:28 +110021#include <linux/kernel.h>
Herbert Xu055bcee2006-08-19 22:24:23 +100022#include <linux/module.h>
23#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Adrian Bunkbc97f192008-03-05 19:05:54 +080025#include "internal.h"
26
Herbert Xu055bcee2006-08-19 22:24:23 +100027static int init(struct hash_desc *desc)
28{
29 struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm);
30
Herbert Xu6c2bb982006-05-16 22:09:29 +100031 tfm->__crt_alg->cra_digest.dia_init(tfm);
Herbert Xu055bcee2006-08-19 22:24:23 +100032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033}
34
Herbert Xufb469842006-12-10 10:45:28 +110035static int update2(struct hash_desc *desc,
36 struct scatterlist *sg, unsigned int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Herbert Xu055bcee2006-08-19 22:24:23 +100038 struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100039 unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Herbert Xu055bcee2006-08-19 22:24:23 +100041 if (!nbytes)
42 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Herbert Xu055bcee2006-08-19 22:24:23 +100044 for (;;) {
Jens Axboe78c2f0b2007-10-22 19:40:16 +020045 struct page *pg = sg_page(sg);
Herbert Xu055bcee2006-08-19 22:24:23 +100046 unsigned int offset = sg->offset;
47 unsigned int l = sg->length;
48
49 if (unlikely(l > nbytes))
50 l = nbytes;
51 nbytes -= l;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 do {
54 unsigned int bytes_from_page = min(l, ((unsigned int)
55 (PAGE_SIZE)) -
56 offset);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100057 char *src = crypto_kmap(pg, 0);
58 char *p = src + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100060 if (unlikely(offset & alignmask)) {
61 unsigned int bytes =
62 alignmask + 1 - (offset & alignmask);
63 bytes = min(bytes, bytes_from_page);
Herbert Xu6c2bb982006-05-16 22:09:29 +100064 tfm->__crt_alg->cra_digest.dia_update(tfm, p,
65 bytes);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100066 p += bytes;
67 bytes_from_page -= bytes;
68 l -= bytes;
69 }
Herbert Xu6c2bb982006-05-16 22:09:29 +100070 tfm->__crt_alg->cra_digest.dia_update(tfm, p,
71 bytes_from_page);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100072 crypto_kunmap(src, 0);
Herbert Xu055bcee2006-08-19 22:24:23 +100073 crypto_yield(desc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 offset = 0;
75 pg++;
76 l -= bytes_from_page;
77 } while (l > 0);
Herbert Xu055bcee2006-08-19 22:24:23 +100078
79 if (!nbytes)
80 break;
Herbert Xub2ab4a52007-12-05 20:59:25 +110081 sg = scatterwalk_sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
Herbert Xu055bcee2006-08-19 22:24:23 +100083
84 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Herbert Xufb469842006-12-10 10:45:28 +110087static int update(struct hash_desc *desc,
88 struct scatterlist *sg, unsigned int nbytes)
89{
90 if (WARN_ON_ONCE(in_irq()))
91 return -EDEADLK;
92 return update2(desc, sg, nbytes);
93}
94
Herbert Xu055bcee2006-08-19 22:24:23 +100095static int final(struct hash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Herbert Xu055bcee2006-08-19 22:24:23 +100097 struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100098 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
Herbert Xuee756412006-07-09 14:49:42 +100099 struct digest_alg *digest = &tfm->__crt_alg->cra_digest;
100
Atsushi Nemotoe1147d82006-04-10 08:42:35 +1000101 if (unlikely((unsigned long)out & alignmask)) {
Herbert Xuee756412006-07-09 14:49:42 +1000102 unsigned long align = alignmask + 1;
103 unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm);
104 u8 *dst = (u8 *)ALIGN(addr, align) +
105 ALIGN(tfm->__crt_alg->cra_ctxsize, align);
106
107 digest->dia_final(tfm, dst);
108 memcpy(out, dst, digest->dia_digestsize);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +1000109 } else
Herbert Xuee756412006-07-09 14:49:42 +1000110 digest->dia_final(tfm, out);
Herbert Xu055bcee2006-08-19 22:24:23 +1000111
112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Herbert Xu055bcee2006-08-19 22:24:23 +1000115static int nosetkey(struct crypto_hash *tfm, const u8 *key, unsigned int keylen)
Herbert Xu560c06a2006-08-13 14:16:39 +1000116{
Herbert Xu055bcee2006-08-19 22:24:23 +1000117 crypto_hash_clear_flags(tfm, CRYPTO_TFM_RES_MASK);
Herbert Xu560c06a2006-08-13 14:16:39 +1000118 return -ENOSYS;
119}
120
Herbert Xu055bcee2006-08-19 22:24:23 +1000121static int setkey(struct crypto_hash *hash, const u8 *key, unsigned int keylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Herbert Xu055bcee2006-08-19 22:24:23 +1000123 struct crypto_tfm *tfm = crypto_hash_tfm(hash);
124
125 crypto_hash_clear_flags(hash, CRYPTO_TFM_RES_MASK);
Herbert Xu560c06a2006-08-13 14:16:39 +1000126 return tfm->__crt_alg->cra_digest.dia_setkey(tfm, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Herbert Xu055bcee2006-08-19 22:24:23 +1000129static int digest(struct hash_desc *desc,
130 struct scatterlist *sg, unsigned int nbytes, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Herbert Xufb469842006-12-10 10:45:28 +1100132 if (WARN_ON_ONCE(in_irq()))
133 return -EDEADLK;
134
Herbert Xu055bcee2006-08-19 22:24:23 +1000135 init(desc);
Herbert Xufb469842006-12-10 10:45:28 +1100136 update2(desc, sg, nbytes);
Herbert Xu055bcee2006-08-19 22:24:23 +1000137 return final(desc, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140int crypto_init_digest_ops(struct crypto_tfm *tfm)
141{
Herbert Xu055bcee2006-08-19 22:24:23 +1000142 struct hash_tfm *ops = &tfm->crt_hash;
Herbert Xu560c06a2006-08-13 14:16:39 +1000143 struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
Herbert Xu055bcee2006-08-19 22:24:23 +1000144
Herbert Xuca786dc2008-07-07 20:23:56 +0800145 if (dalg->dia_digestsize > PAGE_SIZE / 8)
Herbert Xu055bcee2006-08-19 22:24:23 +1000146 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Herbert Xu055bcee2006-08-19 22:24:23 +1000148 ops->init = init;
149 ops->update = update;
150 ops->final = final;
151 ops->digest = digest;
152 ops->setkey = dalg->dia_setkey ? setkey : nosetkey;
153 ops->digestsize = dalg->dia_digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Herbert Xu84251652006-08-20 15:25:22 +1000155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158void crypto_exit_digest_ops(struct crypto_tfm *tfm)
159{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
Loc Ho004a4032008-05-14 20:41:47 +0800161
162static int digest_async_nosetkey(struct crypto_ahash *tfm_async, const u8 *key,
163 unsigned int keylen)
164{
165 crypto_ahash_clear_flags(tfm_async, CRYPTO_TFM_RES_MASK);
166 return -ENOSYS;
167}
168
169static int digest_async_setkey(struct crypto_ahash *tfm_async, const u8 *key,
170 unsigned int keylen)
171{
172 struct crypto_tfm *tfm = crypto_ahash_tfm(tfm_async);
173 struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
174
175 crypto_ahash_clear_flags(tfm_async, CRYPTO_TFM_RES_MASK);
176 return dalg->dia_setkey(tfm, key, keylen);
177}
178
179static int digest_async_init(struct ahash_request *req)
180{
181 struct crypto_tfm *tfm = req->base.tfm;
182 struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
183
184 dalg->dia_init(tfm);
185 return 0;
186}
187
188static int digest_async_update(struct ahash_request *req)
189{
190 struct crypto_tfm *tfm = req->base.tfm;
191 struct hash_desc desc = {
192 .tfm = __crypto_hash_cast(tfm),
193 .flags = req->base.flags,
194 };
195
196 update(&desc, req->src, req->nbytes);
197 return 0;
198}
199
200static int digest_async_final(struct ahash_request *req)
201{
202 struct crypto_tfm *tfm = req->base.tfm;
203 struct hash_desc desc = {
204 .tfm = __crypto_hash_cast(tfm),
205 .flags = req->base.flags,
206 };
207
208 final(&desc, req->result);
209 return 0;
210}
211
212static int digest_async_digest(struct ahash_request *req)
213{
214 struct crypto_tfm *tfm = req->base.tfm;
215 struct hash_desc desc = {
216 .tfm = __crypto_hash_cast(tfm),
217 .flags = req->base.flags,
218 };
219
220 return digest(&desc, req->src, req->nbytes, req->result);
221}
222
223int crypto_init_digest_ops_async(struct crypto_tfm *tfm)
224{
225 struct ahash_tfm *crt = &tfm->crt_ahash;
226 struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
227
Herbert Xudbaaba12008-08-03 21:19:43 +0800228 if (dalg->dia_digestsize > PAGE_SIZE / 8)
Loc Ho004a4032008-05-14 20:41:47 +0800229 return -EINVAL;
230
231 crt->init = digest_async_init;
232 crt->update = digest_async_update;
233 crt->final = digest_async_final;
234 crt->digest = digest_async_digest;
235 crt->setkey = dalg->dia_setkey ? digest_async_setkey :
236 digest_async_nosetkey;
237 crt->digestsize = dalg->dia_digestsize;
Loc Ho004a4032008-05-14 20:41:47 +0800238
239 return 0;
240}