ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / crypto / cipher.c
1 /*
2  * Cryptographic API.
3  *
4  * Cipher 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  */
14 #include <linux/kernel.h>
15 #include <linux/crypto.h>
16 #include <linux/errno.h>
17 #include <linux/mm.h>
18 #include <linux/slab.h>
19 #include <asm/scatterlist.h>
20 #include "internal.h"
21 #include "scatterwalk.h"
22
23 typedef void (cryptfn_t)(void *, u8 *, const u8 *);
24 typedef void (procfn_t)(struct crypto_tfm *, u8 *,
25                         u8*, cryptfn_t, int enc, void *, int);
26
27 static inline void xor_64(u8 *a, const u8 *b)
28 {
29         ((u32 *)a)[0] ^= ((u32 *)b)[0];
30         ((u32 *)a)[1] ^= ((u32 *)b)[1];
31 }
32
33 static inline void xor_128(u8 *a, const u8 *b)
34 {
35         ((u32 *)a)[0] ^= ((u32 *)b)[0];
36         ((u32 *)a)[1] ^= ((u32 *)b)[1];
37         ((u32 *)a)[2] ^= ((u32 *)b)[2];
38         ((u32 *)a)[3] ^= ((u32 *)b)[3];
39 }
40
41
42 /* 
43  * Generic encrypt/decrypt wrapper for ciphers, handles operations across
44  * multiple page boundaries by using temporary blocks.  In user context,
45  * the kernel is given a chance to schedule us once per block.
46  */
47 static int crypt(struct crypto_tfm *tfm,
48                  struct scatterlist *dst,
49                  struct scatterlist *src,
50                  unsigned int nbytes, cryptfn_t crfn,
51                  procfn_t prfn, int enc, void *info)
52 {
53         struct scatter_walk walk_in, walk_out;
54         const unsigned int bsize = crypto_tfm_alg_blocksize(tfm);
55         u8 tmp_src[nbytes > src->length ? bsize : 0];
56         u8 tmp_dst[nbytes > dst->length ? bsize : 0];
57
58         if (!nbytes)
59                 return 0;
60
61         if (nbytes % bsize) {
62                 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
63                 return -EINVAL;
64         }
65
66         scatterwalk_start(&walk_in, src);
67         scatterwalk_start(&walk_out, dst);
68
69         for(;;) {
70                 u8 *src_p, *dst_p;
71
72                 scatterwalk_map(&walk_in, 0);
73                 scatterwalk_map(&walk_out, 1);
74                 src_p = scatterwalk_whichbuf(&walk_in, bsize, tmp_src);
75                 dst_p = scatterwalk_whichbuf(&walk_out, bsize, tmp_dst);
76
77                 nbytes -= bsize;
78
79                 scatterwalk_copychunks(src_p, &walk_in, bsize, 0);
80
81                 prfn(tfm, dst_p, src_p, crfn, enc, info,
82                      scatterwalk_samebuf(&walk_in, &walk_out,
83                                          src_p, dst_p));
84
85                 scatterwalk_done(&walk_in, 0, nbytes);
86
87                 scatterwalk_copychunks(dst_p, &walk_out, bsize, 1);
88                 scatterwalk_done(&walk_out, 1, nbytes);
89
90                 if (!nbytes)
91                         return 0;
92
93                 crypto_yield(tfm);
94         }
95 }
96
97 static void cbc_process(struct crypto_tfm *tfm, u8 *dst, u8 *src,
98                         cryptfn_t fn, int enc, void *info, int in_place)
99 {
100         u8 *iv = info;
101         
102         /* Null encryption */
103         if (!iv)
104                 return;
105                 
106         if (enc) {
107                 tfm->crt_u.cipher.cit_xor_block(iv, src);
108                 fn(crypto_tfm_ctx(tfm), dst, iv);
109                 memcpy(iv, dst, crypto_tfm_alg_blocksize(tfm));
110         } else {
111                 u8 stack[in_place ? crypto_tfm_alg_blocksize(tfm) : 0];
112                 u8 *buf = in_place ? stack : dst;
113
114                 fn(crypto_tfm_ctx(tfm), buf, src);
115                 tfm->crt_u.cipher.cit_xor_block(buf, iv);
116                 memcpy(iv, src, crypto_tfm_alg_blocksize(tfm));
117                 if (buf != dst)
118                         memcpy(dst, buf, crypto_tfm_alg_blocksize(tfm));
119         }
120 }
121
122 static void ecb_process(struct crypto_tfm *tfm, u8 *dst, u8 *src,
123                         cryptfn_t fn, int enc, void *info, int in_place)
124 {
125         fn(crypto_tfm_ctx(tfm), dst, src);
126 }
127
128 static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
129 {
130         struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
131         
132         if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
133                 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
134                 return -EINVAL;
135         } else
136                 return cia->cia_setkey(crypto_tfm_ctx(tfm), key, keylen,
137                                        &tfm->crt_flags);
138 }
139
140 static int ecb_encrypt(struct crypto_tfm *tfm,
141                        struct scatterlist *dst,
142                        struct scatterlist *src, unsigned int nbytes)
143 {
144         return crypt(tfm, dst, src, nbytes,
145                      tfm->__crt_alg->cra_cipher.cia_encrypt,
146                      ecb_process, 1, NULL);
147 }
148
149 static int ecb_decrypt(struct crypto_tfm *tfm,
150                        struct scatterlist *dst,
151                        struct scatterlist *src,
152                        unsigned int nbytes)
153 {
154         return crypt(tfm, dst, src, nbytes,
155                      tfm->__crt_alg->cra_cipher.cia_decrypt,
156                      ecb_process, 1, NULL);
157 }
158
159 static int cbc_encrypt(struct crypto_tfm *tfm,
160                        struct scatterlist *dst,
161                        struct scatterlist *src,
162                        unsigned int nbytes)
163 {
164         return crypt(tfm, dst, src, nbytes,
165                      tfm->__crt_alg->cra_cipher.cia_encrypt,
166                      cbc_process, 1, tfm->crt_cipher.cit_iv);
167 }
168
169 static int cbc_encrypt_iv(struct crypto_tfm *tfm,
170                           struct scatterlist *dst,
171                           struct scatterlist *src,
172                           unsigned int nbytes, u8 *iv)
173 {
174         return crypt(tfm, dst, src, nbytes,
175                      tfm->__crt_alg->cra_cipher.cia_encrypt,
176                      cbc_process, 1, iv);
177 }
178
179 static int cbc_decrypt(struct crypto_tfm *tfm,
180                        struct scatterlist *dst,
181                        struct scatterlist *src,
182                        unsigned int nbytes)
183 {
184         return crypt(tfm, dst, src, nbytes,
185                      tfm->__crt_alg->cra_cipher.cia_decrypt,
186                      cbc_process, 0, tfm->crt_cipher.cit_iv);
187 }
188
189 static int cbc_decrypt_iv(struct crypto_tfm *tfm,
190                           struct scatterlist *dst,
191                           struct scatterlist *src,
192                           unsigned int nbytes, u8 *iv)
193 {
194         return crypt(tfm, dst, src, nbytes,
195                      tfm->__crt_alg->cra_cipher.cia_decrypt,
196                      cbc_process, 0, iv);
197 }
198
199 static int nocrypt(struct crypto_tfm *tfm,
200                    struct scatterlist *dst,
201                    struct scatterlist *src,
202                    unsigned int nbytes)
203 {
204         return -ENOSYS;
205 }
206
207 static int nocrypt_iv(struct crypto_tfm *tfm,
208                       struct scatterlist *dst,
209                       struct scatterlist *src,
210                       unsigned int nbytes, u8 *iv)
211 {
212         return -ENOSYS;
213 }
214
215 int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags)
216 {
217         u32 mode = flags & CRYPTO_TFM_MODE_MASK;
218         
219         tfm->crt_cipher.cit_mode = mode ? mode : CRYPTO_TFM_MODE_ECB;
220         if (flags & CRYPTO_TFM_REQ_WEAK_KEY)
221                 tfm->crt_flags = CRYPTO_TFM_REQ_WEAK_KEY;
222         
223         return 0;
224 }
225
226 int crypto_init_cipher_ops(struct crypto_tfm *tfm)
227 {
228         int ret = 0;
229         struct cipher_tfm *ops = &tfm->crt_cipher;
230
231         ops->cit_setkey = setkey;
232
233         switch (tfm->crt_cipher.cit_mode) {
234         case CRYPTO_TFM_MODE_ECB:
235                 ops->cit_encrypt = ecb_encrypt;
236                 ops->cit_decrypt = ecb_decrypt;
237                 break;
238                 
239         case CRYPTO_TFM_MODE_CBC:
240                 ops->cit_encrypt = cbc_encrypt;
241                 ops->cit_decrypt = cbc_decrypt;
242                 ops->cit_encrypt_iv = cbc_encrypt_iv;
243                 ops->cit_decrypt_iv = cbc_decrypt_iv;
244                 break;
245                 
246         case CRYPTO_TFM_MODE_CFB:
247                 ops->cit_encrypt = nocrypt;
248                 ops->cit_decrypt = nocrypt;
249                 ops->cit_encrypt_iv = nocrypt_iv;
250                 ops->cit_decrypt_iv = nocrypt_iv;
251                 break;
252         
253         case CRYPTO_TFM_MODE_CTR:
254                 ops->cit_encrypt = nocrypt;
255                 ops->cit_decrypt = nocrypt;
256                 ops->cit_encrypt_iv = nocrypt_iv;
257                 ops->cit_decrypt_iv = nocrypt_iv;
258                 break;
259
260         default:
261                 BUG();
262         }
263         
264         if (ops->cit_mode == CRYPTO_TFM_MODE_CBC) {
265                 
266                 switch (crypto_tfm_alg_blocksize(tfm)) {
267                 case 8:
268                         ops->cit_xor_block = xor_64;
269                         break;
270                         
271                 case 16:
272                         ops->cit_xor_block = xor_128;
273                         break;
274                         
275                 default:
276                         printk(KERN_WARNING "%s: block size %u not supported\n",
277                                crypto_tfm_alg_name(tfm),
278                                crypto_tfm_alg_blocksize(tfm));
279                         ret = -EINVAL;
280                         goto out;
281                 }
282                 
283                 ops->cit_ivsize = crypto_tfm_alg_blocksize(tfm);
284                 ops->cit_iv = kmalloc(ops->cit_ivsize, GFP_KERNEL);
285                 if (ops->cit_iv == NULL)
286                         ret = -ENOMEM;
287         }
288
289 out:    
290         return ret;
291 }
292
293 void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
294 {
295         if (tfm->crt_cipher.cit_iv)
296                 kfree(tfm->crt_cipher.cit_iv);
297 }