blob: 65bcea0cd17c867ae88dde90aefc9e137ebd3259 [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
Herbert Xufe2d5292005-09-06 14:49:44 -0700194 nbytes -= bsize;
195
Herbert Xuc774e932005-07-06 13:51:31 -0700196 do {
197 xor(iv, src);
198 fn(crypto_tfm_ctx(tfm), dst, iv);
199 memcpy(iv, dst, bsize);
200
201 src += bsize;
202 dst += bsize;
Herbert Xufe2d5292005-09-06 14:49:44 -0700203 } while ((done += bsize) <= nbytes);
Herbert Xuc774e932005-07-06 13:51:31 -0700204
205 return done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Herbert Xuc774e932005-07-06 13:51:31 -0700208static unsigned int cbc_process_decrypt(const struct cipher_desc *desc,
209 u8 *dst, const u8 *src,
210 unsigned int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Herbert Xuc774e932005-07-06 13:51:31 -0700212 struct crypto_tfm *tfm = desc->tfm;
213 void (*xor)(u8 *, const u8 *) = tfm->crt_u.cipher.cit_xor_block;
214 int bsize = crypto_tfm_alg_blocksize(tfm);
Herbert Xu827c3912005-11-29 22:04:41 +1100215 unsigned long alignmask = crypto_tfm_alg_alignmask(desc->tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Herbert Xu827c3912005-11-29 22:04:41 +1100217 u8 stack[src == dst ? bsize + alignmask : 0];
218 u8 *buf = (u8 *)ALIGN((unsigned long)stack, alignmask + 1);
Herbert Xuc774e932005-07-06 13:51:31 -0700219 u8 **dst_p = src == dst ? &buf : &dst;
220
221 void (*fn)(void *, u8 *, const u8 *) = desc->crfn;
222 u8 *iv = desc->info;
223 unsigned int done = 0;
224
Herbert Xufe2d5292005-09-06 14:49:44 -0700225 nbytes -= bsize;
226
Herbert Xuc774e932005-07-06 13:51:31 -0700227 do {
228 u8 *tmp_dst = *dst_p;
229
230 fn(crypto_tfm_ctx(tfm), tmp_dst, src);
231 xor(tmp_dst, iv);
232 memcpy(iv, src, bsize);
233 if (tmp_dst != dst)
234 memcpy(dst, tmp_dst, bsize);
235
236 src += bsize;
237 dst += bsize;
Herbert Xufe2d5292005-09-06 14:49:44 -0700238 } while ((done += bsize) <= nbytes);
Herbert Xuc774e932005-07-06 13:51:31 -0700239
240 return done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Herbert Xuc774e932005-07-06 13:51:31 -0700243static unsigned int ecb_process(const struct cipher_desc *desc, u8 *dst,
244 const u8 *src, unsigned int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Herbert Xuc774e932005-07-06 13:51:31 -0700246 struct crypto_tfm *tfm = desc->tfm;
247 int bsize = crypto_tfm_alg_blocksize(tfm);
248 void (*fn)(void *, u8 *, const u8 *) = desc->crfn;
249 unsigned int done = 0;
250
Herbert Xufe2d5292005-09-06 14:49:44 -0700251 nbytes -= bsize;
252
Herbert Xuc774e932005-07-06 13:51:31 -0700253 do {
254 fn(crypto_tfm_ctx(tfm), dst, src);
255
256 src += bsize;
257 dst += bsize;
Herbert Xufe2d5292005-09-06 14:49:44 -0700258 } while ((done += bsize) <= nbytes);
Herbert Xuc774e932005-07-06 13:51:31 -0700259
260 return done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
264{
265 struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
266
267 if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
268 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
269 return -EINVAL;
270 } else
271 return cia->cia_setkey(crypto_tfm_ctx(tfm), key, keylen,
272 &tfm->crt_flags);
273}
274
275static int ecb_encrypt(struct crypto_tfm *tfm,
276 struct scatterlist *dst,
277 struct scatterlist *src, unsigned int nbytes)
278{
Herbert Xuc774e932005-07-06 13:51:31 -0700279 struct cipher_desc desc;
Herbert Xu40725182005-07-06 13:51:52 -0700280 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
Herbert Xuc774e932005-07-06 13:51:31 -0700281
282 desc.tfm = tfm;
Herbert Xu40725182005-07-06 13:51:52 -0700283 desc.crfn = cipher->cia_encrypt;
284 desc.prfn = cipher->cia_encrypt_ecb ?: ecb_process;
Herbert Xuc774e932005-07-06 13:51:31 -0700285
286 return crypt(&desc, dst, src, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289static int ecb_decrypt(struct crypto_tfm *tfm,
290 struct scatterlist *dst,
291 struct scatterlist *src,
292 unsigned int nbytes)
293{
Herbert Xuc774e932005-07-06 13:51:31 -0700294 struct cipher_desc desc;
Herbert Xu40725182005-07-06 13:51:52 -0700295 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
Herbert Xuc774e932005-07-06 13:51:31 -0700296
297 desc.tfm = tfm;
Herbert Xu40725182005-07-06 13:51:52 -0700298 desc.crfn = cipher->cia_decrypt;
299 desc.prfn = cipher->cia_decrypt_ecb ?: ecb_process;
Herbert Xuc774e932005-07-06 13:51:31 -0700300
301 return crypt(&desc, dst, src, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304static int cbc_encrypt(struct crypto_tfm *tfm,
305 struct scatterlist *dst,
306 struct scatterlist *src,
307 unsigned int nbytes)
308{
Herbert Xuc774e932005-07-06 13:51:31 -0700309 struct cipher_desc desc;
Herbert Xu40725182005-07-06 13:51:52 -0700310 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
Herbert Xuc774e932005-07-06 13:51:31 -0700311
312 desc.tfm = tfm;
Herbert Xu40725182005-07-06 13:51:52 -0700313 desc.crfn = cipher->cia_encrypt;
314 desc.prfn = cipher->cia_encrypt_cbc ?: cbc_process_encrypt;
Herbert Xuc774e932005-07-06 13:51:31 -0700315 desc.info = tfm->crt_cipher.cit_iv;
316
317 return crypt(&desc, dst, src, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
320static int cbc_encrypt_iv(struct crypto_tfm *tfm,
321 struct scatterlist *dst,
322 struct scatterlist *src,
323 unsigned int nbytes, u8 *iv)
324{
Herbert Xuc774e932005-07-06 13:51:31 -0700325 struct cipher_desc desc;
Herbert Xu40725182005-07-06 13:51:52 -0700326 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
Herbert Xuc774e932005-07-06 13:51:31 -0700327
328 desc.tfm = tfm;
Herbert Xu40725182005-07-06 13:51:52 -0700329 desc.crfn = cipher->cia_encrypt;
330 desc.prfn = cipher->cia_encrypt_cbc ?: cbc_process_encrypt;
Herbert Xuc774e932005-07-06 13:51:31 -0700331 desc.info = iv;
332
Herbert Xu915e8562005-07-06 13:53:47 -0700333 return crypt_iv_unaligned(&desc, dst, src, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
336static int cbc_decrypt(struct crypto_tfm *tfm,
337 struct scatterlist *dst,
338 struct scatterlist *src,
339 unsigned int nbytes)
340{
Herbert Xuc774e932005-07-06 13:51:31 -0700341 struct cipher_desc desc;
Herbert Xu40725182005-07-06 13:51:52 -0700342 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
Herbert Xuc774e932005-07-06 13:51:31 -0700343
344 desc.tfm = tfm;
Herbert Xu40725182005-07-06 13:51:52 -0700345 desc.crfn = cipher->cia_decrypt;
346 desc.prfn = cipher->cia_decrypt_cbc ?: cbc_process_decrypt;
Herbert Xuc774e932005-07-06 13:51:31 -0700347 desc.info = tfm->crt_cipher.cit_iv;
348
349 return crypt(&desc, dst, src, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352static int cbc_decrypt_iv(struct crypto_tfm *tfm,
353 struct scatterlist *dst,
354 struct scatterlist *src,
355 unsigned int nbytes, u8 *iv)
356{
Herbert Xuc774e932005-07-06 13:51:31 -0700357 struct cipher_desc desc;
Herbert Xu40725182005-07-06 13:51:52 -0700358 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
Herbert Xuc774e932005-07-06 13:51:31 -0700359
360 desc.tfm = tfm;
Herbert Xu40725182005-07-06 13:51:52 -0700361 desc.crfn = cipher->cia_decrypt;
362 desc.prfn = cipher->cia_decrypt_cbc ?: cbc_process_decrypt;
Herbert Xuc774e932005-07-06 13:51:31 -0700363 desc.info = iv;
364
Herbert Xu915e8562005-07-06 13:53:47 -0700365 return crypt_iv_unaligned(&desc, dst, src, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
368static int nocrypt(struct crypto_tfm *tfm,
369 struct scatterlist *dst,
370 struct scatterlist *src,
371 unsigned int nbytes)
372{
373 return -ENOSYS;
374}
375
376static int nocrypt_iv(struct crypto_tfm *tfm,
377 struct scatterlist *dst,
378 struct scatterlist *src,
379 unsigned int nbytes, u8 *iv)
380{
381 return -ENOSYS;
382}
383
384int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags)
385{
386 u32 mode = flags & CRYPTO_TFM_MODE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 tfm->crt_cipher.cit_mode = mode ? mode : CRYPTO_TFM_MODE_ECB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return 0;
389}
390
391int crypto_init_cipher_ops(struct crypto_tfm *tfm)
392{
393 int ret = 0;
394 struct cipher_tfm *ops = &tfm->crt_cipher;
395
396 ops->cit_setkey = setkey;
397
398 switch (tfm->crt_cipher.cit_mode) {
399 case CRYPTO_TFM_MODE_ECB:
400 ops->cit_encrypt = ecb_encrypt;
401 ops->cit_decrypt = ecb_decrypt;
402 break;
403
404 case CRYPTO_TFM_MODE_CBC:
405 ops->cit_encrypt = cbc_encrypt;
406 ops->cit_decrypt = cbc_decrypt;
407 ops->cit_encrypt_iv = cbc_encrypt_iv;
408 ops->cit_decrypt_iv = cbc_decrypt_iv;
409 break;
410
411 case CRYPTO_TFM_MODE_CFB:
412 ops->cit_encrypt = nocrypt;
413 ops->cit_decrypt = nocrypt;
414 ops->cit_encrypt_iv = nocrypt_iv;
415 ops->cit_decrypt_iv = nocrypt_iv;
416 break;
417
418 case CRYPTO_TFM_MODE_CTR:
419 ops->cit_encrypt = nocrypt;
420 ops->cit_decrypt = nocrypt;
421 ops->cit_encrypt_iv = nocrypt_iv;
422 ops->cit_decrypt_iv = nocrypt_iv;
423 break;
424
425 default:
426 BUG();
427 }
428
429 if (ops->cit_mode == CRYPTO_TFM_MODE_CBC) {
Herbert Xu9d853c32005-07-15 07:41:31 -0700430 unsigned long align;
Herbert Xufbdae9f2005-07-06 13:53:29 -0700431 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 switch (crypto_tfm_alg_blocksize(tfm)) {
434 case 8:
435 ops->cit_xor_block = xor_64;
436 break;
437
438 case 16:
439 ops->cit_xor_block = xor_128;
440 break;
441
442 default:
443 printk(KERN_WARNING "%s: block size %u not supported\n",
444 crypto_tfm_alg_name(tfm),
445 crypto_tfm_alg_blocksize(tfm));
446 ret = -EINVAL;
447 goto out;
448 }
449
450 ops->cit_ivsize = crypto_tfm_alg_blocksize(tfm);
Herbert Xufbdae9f2005-07-06 13:53:29 -0700451 align = crypto_tfm_alg_alignmask(tfm) + 1;
452 addr = (unsigned long)crypto_tfm_ctx(tfm);
453 addr = ALIGN(addr, align);
454 addr += ALIGN(tfm->__crt_alg->cra_ctxsize, align);
455 ops->cit_iv = (void *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457
458out:
459 return ret;
460}
461
462void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
463{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}