blob: 8da644364cb4225824575ec4813741b9d46d7041 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Cryptographic API.
3 *
4 * Cipher operations.
5 *
6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
Herbert Xuc774e932005-07-06 13:51:31 -07007 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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#include <linux/compiler.h>
16#include <linux/kernel.h>
17#include <linux/crypto.h>
18#include <linux/errno.h>
19#include <linux/mm.h>
20#include <linux/slab.h>
21#include <linux/string.h>
22#include <asm/scatterlist.h>
23#include "internal.h"
24#include "scatterwalk.h"
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static inline void xor_64(u8 *a, const u8 *b)
27{
28 ((u32 *)a)[0] ^= ((u32 *)b)[0];
29 ((u32 *)a)[1] ^= ((u32 *)b)[1];
30}
31
32static inline void xor_128(u8 *a, const u8 *b)
33{
34 ((u32 *)a)[0] ^= ((u32 *)b)[0];
35 ((u32 *)a)[1] ^= ((u32 *)b)[1];
36 ((u32 *)a)[2] ^= ((u32 *)b)[2];
37 ((u32 *)a)[3] ^= ((u32 *)b)[3];
38}
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Herbert Xuc774e932005-07-06 13:51:31 -070040static unsigned int crypt_slow(const struct cipher_desc *desc,
41 struct scatter_walk *in,
42 struct scatter_walk *out, unsigned int bsize)
43{
Herbert Xu9d853c32005-07-15 07:41:31 -070044 unsigned long alignmask = crypto_tfm_alg_alignmask(desc->tfm);
Herbert Xu95477372005-07-06 13:52:09 -070045 u8 buffer[bsize * 2 + alignmask];
46 u8 *src = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
47 u8 *dst = src + bsize;
Herbert Xuc774e932005-07-06 13:51:31 -070048 unsigned int n;
49
50 n = scatterwalk_copychunks(src, in, bsize, 0);
51 scatterwalk_advance(in, n);
52
53 desc->prfn(desc, dst, src, bsize);
54
55 n = scatterwalk_copychunks(dst, out, bsize, 1);
56 scatterwalk_advance(out, n);
57
58 return bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Herbert Xuc774e932005-07-06 13:51:31 -070061static inline unsigned int crypt_fast(const struct cipher_desc *desc,
62 struct scatter_walk *in,
63 struct scatter_walk *out,
Herbert Xu95477372005-07-06 13:52:09 -070064 unsigned int nbytes, u8 *tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Herbert Xuc774e932005-07-06 13:51:31 -070066 u8 *src, *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Herbert Xuc774e932005-07-06 13:51:31 -070068 src = in->data;
69 dst = scatterwalk_samebuf(in, out) ? src : out->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Herbert Xu95477372005-07-06 13:52:09 -070071 if (tmp) {
72 memcpy(tmp, in->data, nbytes);
73 src = tmp;
74 dst = tmp;
75 }
76
Herbert Xuc774e932005-07-06 13:51:31 -070077 nbytes = desc->prfn(desc, dst, src, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Herbert Xu95477372005-07-06 13:52:09 -070079 if (tmp)
80 memcpy(out->data, tmp, nbytes);
81
Herbert Xuc774e932005-07-06 13:51:31 -070082 scatterwalk_advance(in, nbytes);
83 scatterwalk_advance(out, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Herbert Xuc774e932005-07-06 13:51:31 -070085 return nbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88/*
89 * Generic encrypt/decrypt wrapper for ciphers, handles operations across
90 * multiple page boundaries by using temporary blocks. In user context,
Herbert Xuc774e932005-07-06 13:51:31 -070091 * the kernel is given a chance to schedule us once per page.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
Herbert Xuc774e932005-07-06 13:51:31 -070093static int crypt(const struct cipher_desc *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct scatterlist *dst,
95 struct scatterlist *src,
Herbert Xuc774e932005-07-06 13:51:31 -070096 unsigned int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 struct scatter_walk walk_in, walk_out;
Herbert Xuc774e932005-07-06 13:51:31 -070099 struct crypto_tfm *tfm = desc->tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 const unsigned int bsize = crypto_tfm_alg_blocksize(tfm);
Herbert Xufbdae9f2005-07-06 13:53:29 -0700101 unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
Herbert Xu95477372005-07-06 13:52:09 -0700102 unsigned long buffer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 if (!nbytes)
105 return 0;
106
107 if (nbytes % bsize) {
108 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
109 return -EINVAL;
110 }
111
112 scatterwalk_start(&walk_in, src);
113 scatterwalk_start(&walk_out, dst);
114
115 for(;;) {
Herbert Xu95477372005-07-06 13:52:09 -0700116 unsigned int n = nbytes;
117 u8 *tmp = NULL;
118
119 if (!scatterwalk_aligned(&walk_in, alignmask) ||
120 !scatterwalk_aligned(&walk_out, alignmask)) {
121 if (!buffer) {
122 buffer = __get_free_page(GFP_ATOMIC);
123 if (!buffer)
124 n = 0;
125 }
126 tmp = (u8 *)buffer;
127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 scatterwalk_map(&walk_in, 0);
130 scatterwalk_map(&walk_out, 1);
131
Herbert Xu95477372005-07-06 13:52:09 -0700132 n = scatterwalk_clamp(&walk_in, n);
Herbert Xuc774e932005-07-06 13:51:31 -0700133 n = scatterwalk_clamp(&walk_out, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Herbert Xuc774e932005-07-06 13:51:31 -0700135 if (likely(n >= bsize))
Herbert Xu95477372005-07-06 13:52:09 -0700136 n = crypt_fast(desc, &walk_in, &walk_out, n, tmp);
Herbert Xuc774e932005-07-06 13:51:31 -0700137 else
138 n = crypt_slow(desc, &walk_in, &walk_out, bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Herbert Xuc774e932005-07-06 13:51:31 -0700140 nbytes -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 scatterwalk_done(&walk_in, 0, nbytes);
143 scatterwalk_done(&walk_out, 1, nbytes);
144
145 if (!nbytes)
Herbert Xu95477372005-07-06 13:52:09 -0700146 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 crypto_yield(tfm);
149 }
Herbert Xu95477372005-07-06 13:52:09 -0700150
151 if (buffer)
152 free_page(buffer);
153
154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Herbert Xu915e8562005-07-06 13:53:47 -0700157static int crypt_iv_unaligned(struct cipher_desc *desc,
158 struct scatterlist *dst,
159 struct scatterlist *src,
160 unsigned int nbytes)
161{
162 struct crypto_tfm *tfm = desc->tfm;
Herbert Xu9d853c32005-07-15 07:41:31 -0700163 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
Herbert Xu915e8562005-07-06 13:53:47 -0700164 u8 *iv = desc->info;
165
166 if (unlikely(((unsigned long)iv & alignmask))) {
167 unsigned int ivsize = tfm->crt_cipher.cit_ivsize;
168 u8 buffer[ivsize + alignmask];
169 u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
170 int err;
171
172 desc->info = memcpy(tmp, iv, ivsize);
173 err = crypt(desc, dst, src, nbytes);
174 memcpy(iv, tmp, ivsize);
175
176 return err;
177 }
178
179 return crypt(desc, dst, src, nbytes);
180}
181
Herbert Xuc774e932005-07-06 13:51:31 -0700182static unsigned int cbc_process_encrypt(const struct cipher_desc *desc,
183 u8 *dst, const u8 *src,
184 unsigned int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Herbert Xuc774e932005-07-06 13:51:31 -0700186 struct crypto_tfm *tfm = desc->tfm;
187 void (*xor)(u8 *, const u8 *) = tfm->crt_u.cipher.cit_xor_block;
188 int bsize = crypto_tfm_alg_blocksize(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Herbert Xuc774e932005-07-06 13:51:31 -0700190 void (*fn)(void *, u8 *, const u8 *) = desc->crfn;
191 u8 *iv = desc->info;
192 unsigned int done = 0;
193
194 do {
195 xor(iv, src);
196 fn(crypto_tfm_ctx(tfm), dst, iv);
197 memcpy(iv, dst, bsize);
198
199 src += bsize;
200 dst += bsize;
201 } while ((done += bsize) < nbytes);
202
203 return done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Herbert Xuc774e932005-07-06 13:51:31 -0700206static unsigned int cbc_process_decrypt(const struct cipher_desc *desc,
207 u8 *dst, const u8 *src,
208 unsigned int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Herbert Xuc774e932005-07-06 13:51:31 -0700210 struct crypto_tfm *tfm = desc->tfm;
211 void (*xor)(u8 *, const u8 *) = tfm->crt_u.cipher.cit_xor_block;
212 int bsize = crypto_tfm_alg_blocksize(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Herbert Xuc774e932005-07-06 13:51:31 -0700214 u8 stack[src == dst ? bsize : 0];
215 u8 *buf = stack;
216 u8 **dst_p = src == dst ? &buf : &dst;
217
218 void (*fn)(void *, u8 *, const u8 *) = desc->crfn;
219 u8 *iv = desc->info;
220 unsigned int done = 0;
221
222 do {
223 u8 *tmp_dst = *dst_p;
224
225 fn(crypto_tfm_ctx(tfm), tmp_dst, src);
226 xor(tmp_dst, iv);
227 memcpy(iv, src, bsize);
228 if (tmp_dst != dst)
229 memcpy(dst, tmp_dst, bsize);
230
231 src += bsize;
232 dst += bsize;
233 } while ((done += bsize) < nbytes);
234
235 return done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Herbert Xuc774e932005-07-06 13:51:31 -0700238static unsigned int ecb_process(const struct cipher_desc *desc, u8 *dst,
239 const u8 *src, unsigned int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Herbert Xuc774e932005-07-06 13:51:31 -0700241 struct crypto_tfm *tfm = desc->tfm;
242 int bsize = crypto_tfm_alg_blocksize(tfm);
243 void (*fn)(void *, u8 *, const u8 *) = desc->crfn;
244 unsigned int done = 0;
245
246 do {
247 fn(crypto_tfm_ctx(tfm), dst, src);
248
249 src += bsize;
250 dst += bsize;
251 } while ((done += bsize) < nbytes);
252
253 return done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
257{
258 struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
259
260 if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
261 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
262 return -EINVAL;
263 } else
264 return cia->cia_setkey(crypto_tfm_ctx(tfm), key, keylen,
265 &tfm->crt_flags);
266}
267
268static int ecb_encrypt(struct crypto_tfm *tfm,
269 struct scatterlist *dst,
270 struct scatterlist *src, unsigned int nbytes)
271{
Herbert Xuc774e932005-07-06 13:51:31 -0700272 struct cipher_desc desc;
Herbert Xu40725182005-07-06 13:51:52 -0700273 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
Herbert Xuc774e932005-07-06 13:51:31 -0700274
275 desc.tfm = tfm;
Herbert Xu40725182005-07-06 13:51:52 -0700276 desc.crfn = cipher->cia_encrypt;
277 desc.prfn = cipher->cia_encrypt_ecb ?: ecb_process;
Herbert Xuc774e932005-07-06 13:51:31 -0700278
279 return crypt(&desc, dst, src, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282static int ecb_decrypt(struct crypto_tfm *tfm,
283 struct scatterlist *dst,
284 struct scatterlist *src,
285 unsigned int nbytes)
286{
Herbert Xuc774e932005-07-06 13:51:31 -0700287 struct cipher_desc desc;
Herbert Xu40725182005-07-06 13:51:52 -0700288 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
Herbert Xuc774e932005-07-06 13:51:31 -0700289
290 desc.tfm = tfm;
Herbert Xu40725182005-07-06 13:51:52 -0700291 desc.crfn = cipher->cia_decrypt;
292 desc.prfn = cipher->cia_decrypt_ecb ?: ecb_process;
Herbert Xuc774e932005-07-06 13:51:31 -0700293
294 return crypt(&desc, dst, src, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297static int cbc_encrypt(struct crypto_tfm *tfm,
298 struct scatterlist *dst,
299 struct scatterlist *src,
300 unsigned int nbytes)
301{
Herbert Xuc774e932005-07-06 13:51:31 -0700302 struct cipher_desc desc;
Herbert Xu40725182005-07-06 13:51:52 -0700303 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
Herbert Xuc774e932005-07-06 13:51:31 -0700304
305 desc.tfm = tfm;
Herbert Xu40725182005-07-06 13:51:52 -0700306 desc.crfn = cipher->cia_encrypt;
307 desc.prfn = cipher->cia_encrypt_cbc ?: cbc_process_encrypt;
Herbert Xuc774e932005-07-06 13:51:31 -0700308 desc.info = tfm->crt_cipher.cit_iv;
309
310 return crypt(&desc, dst, src, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313static int cbc_encrypt_iv(struct crypto_tfm *tfm,
314 struct scatterlist *dst,
315 struct scatterlist *src,
316 unsigned int nbytes, u8 *iv)
317{
Herbert Xuc774e932005-07-06 13:51:31 -0700318 struct cipher_desc desc;
Herbert Xu40725182005-07-06 13:51:52 -0700319 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
Herbert Xuc774e932005-07-06 13:51:31 -0700320
321 desc.tfm = tfm;
Herbert Xu40725182005-07-06 13:51:52 -0700322 desc.crfn = cipher->cia_encrypt;
323 desc.prfn = cipher->cia_encrypt_cbc ?: cbc_process_encrypt;
Herbert Xuc774e932005-07-06 13:51:31 -0700324 desc.info = iv;
325
Herbert Xu915e8562005-07-06 13:53:47 -0700326 return crypt_iv_unaligned(&desc, dst, src, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329static int cbc_decrypt(struct crypto_tfm *tfm,
330 struct scatterlist *dst,
331 struct scatterlist *src,
332 unsigned int nbytes)
333{
Herbert Xuc774e932005-07-06 13:51:31 -0700334 struct cipher_desc desc;
Herbert Xu40725182005-07-06 13:51:52 -0700335 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
Herbert Xuc774e932005-07-06 13:51:31 -0700336
337 desc.tfm = tfm;
Herbert Xu40725182005-07-06 13:51:52 -0700338 desc.crfn = cipher->cia_decrypt;
339 desc.prfn = cipher->cia_decrypt_cbc ?: cbc_process_decrypt;
Herbert Xuc774e932005-07-06 13:51:31 -0700340 desc.info = tfm->crt_cipher.cit_iv;
341
342 return crypt(&desc, dst, src, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345static int cbc_decrypt_iv(struct crypto_tfm *tfm,
346 struct scatterlist *dst,
347 struct scatterlist *src,
348 unsigned int nbytes, u8 *iv)
349{
Herbert Xuc774e932005-07-06 13:51:31 -0700350 struct cipher_desc desc;
Herbert Xu40725182005-07-06 13:51:52 -0700351 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
Herbert Xuc774e932005-07-06 13:51:31 -0700352
353 desc.tfm = tfm;
Herbert Xu40725182005-07-06 13:51:52 -0700354 desc.crfn = cipher->cia_decrypt;
355 desc.prfn = cipher->cia_decrypt_cbc ?: cbc_process_decrypt;
Herbert Xuc774e932005-07-06 13:51:31 -0700356 desc.info = iv;
357
Herbert Xu915e8562005-07-06 13:53:47 -0700358 return crypt_iv_unaligned(&desc, dst, src, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361static int nocrypt(struct crypto_tfm *tfm,
362 struct scatterlist *dst,
363 struct scatterlist *src,
364 unsigned int nbytes)
365{
366 return -ENOSYS;
367}
368
369static int nocrypt_iv(struct crypto_tfm *tfm,
370 struct scatterlist *dst,
371 struct scatterlist *src,
372 unsigned int nbytes, u8 *iv)
373{
374 return -ENOSYS;
375}
376
377int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags)
378{
379 u32 mode = flags & CRYPTO_TFM_MODE_MASK;
380
381 tfm->crt_cipher.cit_mode = mode ? mode : CRYPTO_TFM_MODE_ECB;
382 if (flags & CRYPTO_TFM_REQ_WEAK_KEY)
383 tfm->crt_flags = CRYPTO_TFM_REQ_WEAK_KEY;
384
385 return 0;
386}
387
388int crypto_init_cipher_ops(struct crypto_tfm *tfm)
389{
390 int ret = 0;
391 struct cipher_tfm *ops = &tfm->crt_cipher;
392
393 ops->cit_setkey = setkey;
394
395 switch (tfm->crt_cipher.cit_mode) {
396 case CRYPTO_TFM_MODE_ECB:
397 ops->cit_encrypt = ecb_encrypt;
398 ops->cit_decrypt = ecb_decrypt;
399 break;
400
401 case CRYPTO_TFM_MODE_CBC:
402 ops->cit_encrypt = cbc_encrypt;
403 ops->cit_decrypt = cbc_decrypt;
404 ops->cit_encrypt_iv = cbc_encrypt_iv;
405 ops->cit_decrypt_iv = cbc_decrypt_iv;
406 break;
407
408 case CRYPTO_TFM_MODE_CFB:
409 ops->cit_encrypt = nocrypt;
410 ops->cit_decrypt = nocrypt;
411 ops->cit_encrypt_iv = nocrypt_iv;
412 ops->cit_decrypt_iv = nocrypt_iv;
413 break;
414
415 case CRYPTO_TFM_MODE_CTR:
416 ops->cit_encrypt = nocrypt;
417 ops->cit_decrypt = nocrypt;
418 ops->cit_encrypt_iv = nocrypt_iv;
419 ops->cit_decrypt_iv = nocrypt_iv;
420 break;
421
422 default:
423 BUG();
424 }
425
426 if (ops->cit_mode == CRYPTO_TFM_MODE_CBC) {
Herbert Xu9d853c32005-07-15 07:41:31 -0700427 unsigned long align;
Herbert Xufbdae9f2005-07-06 13:53:29 -0700428 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 switch (crypto_tfm_alg_blocksize(tfm)) {
431 case 8:
432 ops->cit_xor_block = xor_64;
433 break;
434
435 case 16:
436 ops->cit_xor_block = xor_128;
437 break;
438
439 default:
440 printk(KERN_WARNING "%s: block size %u not supported\n",
441 crypto_tfm_alg_name(tfm),
442 crypto_tfm_alg_blocksize(tfm));
443 ret = -EINVAL;
444 goto out;
445 }
446
447 ops->cit_ivsize = crypto_tfm_alg_blocksize(tfm);
Herbert Xufbdae9f2005-07-06 13:53:29 -0700448 align = crypto_tfm_alg_alignmask(tfm) + 1;
449 addr = (unsigned long)crypto_tfm_ctx(tfm);
450 addr = ALIGN(addr, align);
451 addr += ALIGN(tfm->__crt_alg->cra_ctxsize, align);
452 ops->cit_iv = (void *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
455out:
456 return ret;
457}
458
459void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
460{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}