TCP: Fix shrinking windows with window scaling
[linux-2.6.22.y-op.git] / crypto / blkcipher.c
blobcce92365de36a4a26427f6100d01dbe0718b3a34
1 /*
2 * Block chaining cipher operations.
3 *
4 * Generic encrypt/decrypt wrapper for ciphers, handles operations across
5 * multiple page boundaries by using temporary blocks. In user context,
6 * the kernel is given a chance to schedule us once per page.
8 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
17 #include <linux/crypto.h>
18 #include <linux/errno.h>
19 #include <linux/hardirq.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/scatterlist.h>
23 #include <linux/seq_file.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
27 #include "internal.h"
28 #include "scatterwalk.h"
30 enum {
31 BLKCIPHER_WALK_PHYS = 1 << 0,
32 BLKCIPHER_WALK_SLOW = 1 << 1,
33 BLKCIPHER_WALK_COPY = 1 << 2,
34 BLKCIPHER_WALK_DIFF = 1 << 3,
37 static int blkcipher_walk_next(struct blkcipher_desc *desc,
38 struct blkcipher_walk *walk);
39 static int blkcipher_walk_first(struct blkcipher_desc *desc,
40 struct blkcipher_walk *walk);
42 static inline void blkcipher_map_src(struct blkcipher_walk *walk)
44 walk->src.virt.addr = scatterwalk_map(&walk->in, 0);
47 static inline void blkcipher_map_dst(struct blkcipher_walk *walk)
49 walk->dst.virt.addr = scatterwalk_map(&walk->out, 1);
52 static inline void blkcipher_unmap_src(struct blkcipher_walk *walk)
54 scatterwalk_unmap(walk->src.virt.addr, 0);
57 static inline void blkcipher_unmap_dst(struct blkcipher_walk *walk)
59 scatterwalk_unmap(walk->dst.virt.addr, 1);
62 /* Get a spot of the specified length that does not straddle a page.
63 * The caller needs to ensure that there is enough space for this operation.
65 static inline u8 *blkcipher_get_spot(u8 *start, unsigned int len)
67 u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
68 return start > end_page ? start : end_page;
71 static inline unsigned int blkcipher_done_slow(struct crypto_blkcipher *tfm,
72 struct blkcipher_walk *walk,
73 unsigned int bsize)
75 u8 *addr;
76 unsigned int alignmask = crypto_blkcipher_alignmask(tfm);
78 addr = (u8 *)ALIGN((unsigned long)walk->buffer, alignmask + 1);
79 addr = blkcipher_get_spot(addr, bsize);
80 scatterwalk_copychunks(addr, &walk->out, bsize, 1);
81 return bsize;
84 static inline unsigned int blkcipher_done_fast(struct blkcipher_walk *walk,
85 unsigned int n)
87 n = walk->nbytes - n;
89 if (walk->flags & BLKCIPHER_WALK_COPY) {
90 blkcipher_map_dst(walk);
91 memcpy(walk->dst.virt.addr, walk->page, n);
92 blkcipher_unmap_dst(walk);
93 } else if (!(walk->flags & BLKCIPHER_WALK_PHYS)) {
94 blkcipher_unmap_src(walk);
95 if (walk->flags & BLKCIPHER_WALK_DIFF)
96 blkcipher_unmap_dst(walk);
99 scatterwalk_advance(&walk->in, n);
100 scatterwalk_advance(&walk->out, n);
102 return n;
105 int blkcipher_walk_done(struct blkcipher_desc *desc,
106 struct blkcipher_walk *walk, int err)
108 struct crypto_blkcipher *tfm = desc->tfm;
109 unsigned int nbytes = 0;
111 if (likely(err >= 0)) {
112 unsigned int bsize = crypto_blkcipher_blocksize(tfm);
113 unsigned int n;
115 if (likely(!(walk->flags & BLKCIPHER_WALK_SLOW)))
116 n = blkcipher_done_fast(walk, err);
117 else
118 n = blkcipher_done_slow(tfm, walk, bsize);
120 nbytes = walk->total - n;
121 err = 0;
124 scatterwalk_done(&walk->in, 0, nbytes);
125 scatterwalk_done(&walk->out, 1, nbytes);
127 walk->total = nbytes;
128 walk->nbytes = nbytes;
130 if (nbytes) {
131 crypto_yield(desc->flags);
132 return blkcipher_walk_next(desc, walk);
135 if (walk->iv != desc->info)
136 memcpy(desc->info, walk->iv, crypto_blkcipher_ivsize(tfm));
137 if (walk->buffer != walk->page)
138 kfree(walk->buffer);
139 if (walk->page)
140 free_page((unsigned long)walk->page);
142 return err;
144 EXPORT_SYMBOL_GPL(blkcipher_walk_done);
146 static inline int blkcipher_next_slow(struct blkcipher_desc *desc,
147 struct blkcipher_walk *walk,
148 unsigned int bsize,
149 unsigned int alignmask)
151 unsigned int n;
153 if (walk->buffer)
154 goto ok;
156 walk->buffer = walk->page;
157 if (walk->buffer)
158 goto ok;
160 n = bsize * 3 - (alignmask + 1) +
161 (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
162 walk->buffer = kmalloc(n, GFP_ATOMIC);
163 if (!walk->buffer)
164 return blkcipher_walk_done(desc, walk, -ENOMEM);
167 walk->dst.virt.addr = (u8 *)ALIGN((unsigned long)walk->buffer,
168 alignmask + 1);
169 walk->dst.virt.addr = blkcipher_get_spot(walk->dst.virt.addr, bsize);
170 walk->src.virt.addr = blkcipher_get_spot(walk->dst.virt.addr + bsize,
171 bsize);
173 scatterwalk_copychunks(walk->src.virt.addr, &walk->in, bsize, 0);
175 walk->nbytes = bsize;
176 walk->flags |= BLKCIPHER_WALK_SLOW;
178 return 0;
181 static inline int blkcipher_next_copy(struct blkcipher_walk *walk)
183 u8 *tmp = walk->page;
185 blkcipher_map_src(walk);
186 memcpy(tmp, walk->src.virt.addr, walk->nbytes);
187 blkcipher_unmap_src(walk);
189 walk->src.virt.addr = tmp;
190 walk->dst.virt.addr = tmp;
192 return 0;
195 static inline int blkcipher_next_fast(struct blkcipher_desc *desc,
196 struct blkcipher_walk *walk)
198 unsigned long diff;
200 walk->src.phys.page = scatterwalk_page(&walk->in);
201 walk->src.phys.offset = offset_in_page(walk->in.offset);
202 walk->dst.phys.page = scatterwalk_page(&walk->out);
203 walk->dst.phys.offset = offset_in_page(walk->out.offset);
205 if (walk->flags & BLKCIPHER_WALK_PHYS)
206 return 0;
208 diff = walk->src.phys.offset - walk->dst.phys.offset;
209 diff |= walk->src.virt.page - walk->dst.virt.page;
211 blkcipher_map_src(walk);
212 walk->dst.virt.addr = walk->src.virt.addr;
214 if (diff) {
215 walk->flags |= BLKCIPHER_WALK_DIFF;
216 blkcipher_map_dst(walk);
219 return 0;
222 static int blkcipher_walk_next(struct blkcipher_desc *desc,
223 struct blkcipher_walk *walk)
225 struct crypto_blkcipher *tfm = desc->tfm;
226 unsigned int alignmask = crypto_blkcipher_alignmask(tfm);
227 unsigned int bsize = crypto_blkcipher_blocksize(tfm);
228 unsigned int n;
229 int err;
231 n = walk->total;
232 if (unlikely(n < bsize)) {
233 desc->flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
234 return blkcipher_walk_done(desc, walk, -EINVAL);
237 walk->flags &= ~(BLKCIPHER_WALK_SLOW | BLKCIPHER_WALK_COPY |
238 BLKCIPHER_WALK_DIFF);
239 if (!scatterwalk_aligned(&walk->in, alignmask) ||
240 !scatterwalk_aligned(&walk->out, alignmask)) {
241 walk->flags |= BLKCIPHER_WALK_COPY;
242 if (!walk->page) {
243 walk->page = (void *)__get_free_page(GFP_ATOMIC);
244 if (!walk->page)
245 n = 0;
249 n = scatterwalk_clamp(&walk->in, n);
250 n = scatterwalk_clamp(&walk->out, n);
252 if (unlikely(n < bsize)) {
253 err = blkcipher_next_slow(desc, walk, bsize, alignmask);
254 goto set_phys_lowmem;
257 walk->nbytes = n;
258 if (walk->flags & BLKCIPHER_WALK_COPY) {
259 err = blkcipher_next_copy(walk);
260 goto set_phys_lowmem;
263 return blkcipher_next_fast(desc, walk);
265 set_phys_lowmem:
266 if (walk->flags & BLKCIPHER_WALK_PHYS) {
267 walk->src.phys.page = virt_to_page(walk->src.virt.addr);
268 walk->dst.phys.page = virt_to_page(walk->dst.virt.addr);
269 walk->src.phys.offset &= PAGE_SIZE - 1;
270 walk->dst.phys.offset &= PAGE_SIZE - 1;
272 return err;
275 static inline int blkcipher_copy_iv(struct blkcipher_walk *walk,
276 struct crypto_blkcipher *tfm,
277 unsigned int alignmask)
279 unsigned bs = crypto_blkcipher_blocksize(tfm);
280 unsigned int ivsize = crypto_blkcipher_ivsize(tfm);
281 unsigned int size = bs * 2 + ivsize + max(bs, ivsize) - (alignmask + 1);
282 u8 *iv;
284 size += alignmask & ~(crypto_tfm_ctx_alignment() - 1);
285 walk->buffer = kmalloc(size, GFP_ATOMIC);
286 if (!walk->buffer)
287 return -ENOMEM;
289 iv = (u8 *)ALIGN((unsigned long)walk->buffer, alignmask + 1);
290 iv = blkcipher_get_spot(iv, bs) + bs;
291 iv = blkcipher_get_spot(iv, bs) + bs;
292 iv = blkcipher_get_spot(iv, ivsize);
294 walk->iv = memcpy(iv, walk->iv, ivsize);
295 return 0;
298 int blkcipher_walk_virt(struct blkcipher_desc *desc,
299 struct blkcipher_walk *walk)
301 walk->flags &= ~BLKCIPHER_WALK_PHYS;
302 return blkcipher_walk_first(desc, walk);
304 EXPORT_SYMBOL_GPL(blkcipher_walk_virt);
306 int blkcipher_walk_phys(struct blkcipher_desc *desc,
307 struct blkcipher_walk *walk)
309 walk->flags |= BLKCIPHER_WALK_PHYS;
310 return blkcipher_walk_first(desc, walk);
312 EXPORT_SYMBOL_GPL(blkcipher_walk_phys);
314 static int blkcipher_walk_first(struct blkcipher_desc *desc,
315 struct blkcipher_walk *walk)
317 struct crypto_blkcipher *tfm = desc->tfm;
318 unsigned int alignmask = crypto_blkcipher_alignmask(tfm);
320 if (WARN_ON_ONCE(in_irq()))
321 return -EDEADLK;
323 walk->nbytes = walk->total;
324 if (unlikely(!walk->total))
325 return 0;
327 walk->buffer = NULL;
328 walk->iv = desc->info;
329 if (unlikely(((unsigned long)walk->iv & alignmask))) {
330 int err = blkcipher_copy_iv(walk, tfm, alignmask);
331 if (err)
332 return err;
335 scatterwalk_start(&walk->in, walk->in.sg);
336 scatterwalk_start(&walk->out, walk->out.sg);
337 walk->page = NULL;
339 return blkcipher_walk_next(desc, walk);
342 static int setkey(struct crypto_tfm *tfm, const u8 *key,
343 unsigned int keylen)
345 struct blkcipher_alg *cipher = &tfm->__crt_alg->cra_blkcipher;
347 if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
348 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
349 return -EINVAL;
352 return cipher->setkey(tfm, key, keylen);
355 static int async_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
356 unsigned int keylen)
358 return setkey(crypto_ablkcipher_tfm(tfm), key, keylen);
361 static int async_encrypt(struct ablkcipher_request *req)
363 struct crypto_tfm *tfm = req->base.tfm;
364 struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
365 struct blkcipher_desc desc = {
366 .tfm = __crypto_blkcipher_cast(tfm),
367 .info = req->info,
368 .flags = req->base.flags,
372 return alg->encrypt(&desc, req->dst, req->src, req->nbytes);
375 static int async_decrypt(struct ablkcipher_request *req)
377 struct crypto_tfm *tfm = req->base.tfm;
378 struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
379 struct blkcipher_desc desc = {
380 .tfm = __crypto_blkcipher_cast(tfm),
381 .info = req->info,
382 .flags = req->base.flags,
385 return alg->decrypt(&desc, req->dst, req->src, req->nbytes);
388 static unsigned int crypto_blkcipher_ctxsize(struct crypto_alg *alg, u32 type,
389 u32 mask)
391 struct blkcipher_alg *cipher = &alg->cra_blkcipher;
392 unsigned int len = alg->cra_ctxsize;
394 type ^= CRYPTO_ALG_ASYNC;
395 mask &= CRYPTO_ALG_ASYNC;
396 if ((type & mask) && cipher->ivsize) {
397 len = ALIGN(len, (unsigned long)alg->cra_alignmask + 1);
398 len += cipher->ivsize;
401 return len;
404 static int crypto_init_blkcipher_ops_async(struct crypto_tfm *tfm)
406 struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
407 struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
409 crt->setkey = async_setkey;
410 crt->encrypt = async_encrypt;
411 crt->decrypt = async_decrypt;
412 crt->ivsize = alg->ivsize;
414 return 0;
417 static int crypto_init_blkcipher_ops_sync(struct crypto_tfm *tfm)
419 struct blkcipher_tfm *crt = &tfm->crt_blkcipher;
420 struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
421 unsigned long align = crypto_tfm_alg_alignmask(tfm) + 1;
422 unsigned long addr;
424 crt->setkey = setkey;
425 crt->encrypt = alg->encrypt;
426 crt->decrypt = alg->decrypt;
428 addr = (unsigned long)crypto_tfm_ctx(tfm);
429 addr = ALIGN(addr, align);
430 addr += ALIGN(tfm->__crt_alg->cra_ctxsize, align);
431 crt->iv = (void *)addr;
433 return 0;
436 static int crypto_init_blkcipher_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
438 struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
440 if (alg->ivsize > PAGE_SIZE / 8)
441 return -EINVAL;
443 type ^= CRYPTO_ALG_ASYNC;
444 mask &= CRYPTO_ALG_ASYNC;
445 if (type & mask)
446 return crypto_init_blkcipher_ops_sync(tfm);
447 else
448 return crypto_init_blkcipher_ops_async(tfm);
451 static void crypto_blkcipher_show(struct seq_file *m, struct crypto_alg *alg)
452 __attribute__ ((unused));
453 static void crypto_blkcipher_show(struct seq_file *m, struct crypto_alg *alg)
455 seq_printf(m, "type : blkcipher\n");
456 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
457 seq_printf(m, "min keysize : %u\n", alg->cra_blkcipher.min_keysize);
458 seq_printf(m, "max keysize : %u\n", alg->cra_blkcipher.max_keysize);
459 seq_printf(m, "ivsize : %u\n", alg->cra_blkcipher.ivsize);
462 const struct crypto_type crypto_blkcipher_type = {
463 .ctxsize = crypto_blkcipher_ctxsize,
464 .init = crypto_init_blkcipher_ops,
465 #ifdef CONFIG_PROC_FS
466 .show = crypto_blkcipher_show,
467 #endif
469 EXPORT_SYMBOL_GPL(crypto_blkcipher_type);
471 MODULE_LICENSE("GPL");
472 MODULE_DESCRIPTION("Generic block chaining cipher type");