6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
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)
14 #include <linux/kernel.h>
15 #include <linux/crypto.h>
16 #include <linux/errno.h>
18 #include <linux/slab.h>
19 #include <asm/scatterlist.h>
21 #include "scatterwalk.h"
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);
27 static inline void xor_64(u8 *a, const u8 *b)
29 ((u32 *)a)[0] ^= ((u32 *)b)[0];
30 ((u32 *)a)[1] ^= ((u32 *)b)[1];
33 static inline void xor_128(u8 *a, const u8 *b)
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];
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.
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)
53 struct scatter_walk walk_in, walk_out;
54 const unsigned int bsize = crypto_tfm_alg_blocksize(tfm);
62 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
66 scatterwalk_start(&walk_in, src);
67 scatterwalk_start(&walk_out, dst);
73 scatterwalk_map(&walk_in, 0);
74 scatterwalk_map(&walk_out, 1);
75 src_p = scatterwalk_whichbuf(&walk_in, bsize, tmp_src);
76 dst_p = scatterwalk_whichbuf(&walk_out, bsize, tmp_dst);
77 in_place = scatterwalk_samebuf(&walk_in, &walk_out,
82 scatterwalk_copychunks(src_p, &walk_in, bsize, 0);
84 prfn(tfm, dst_p, src_p, crfn, enc, info, in_place);
86 scatterwalk_done(&walk_in, 0, nbytes);
88 scatterwalk_copychunks(dst_p, &walk_out, bsize, 1);
89 scatterwalk_done(&walk_out, 1, nbytes);
98 static void cbc_process(struct crypto_tfm *tfm, u8 *dst, u8 *src,
99 cryptfn_t fn, int enc, void *info, int in_place)
103 /* Null encryption */
108 tfm->crt_u.cipher.cit_xor_block(iv, src);
109 fn(crypto_tfm_ctx(tfm), dst, iv);
110 memcpy(iv, dst, crypto_tfm_alg_blocksize(tfm));
112 u8 stack[in_place ? crypto_tfm_alg_blocksize(tfm) : 0];
113 u8 *buf = in_place ? stack : dst;
115 fn(crypto_tfm_ctx(tfm), buf, src);
116 tfm->crt_u.cipher.cit_xor_block(buf, iv);
117 memcpy(iv, src, crypto_tfm_alg_blocksize(tfm));
119 memcpy(dst, buf, crypto_tfm_alg_blocksize(tfm));
123 static void ecb_process(struct crypto_tfm *tfm, u8 *dst, u8 *src,
124 cryptfn_t fn, int enc, void *info, int in_place)
126 fn(crypto_tfm_ctx(tfm), dst, src);
129 static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
131 struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
133 if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
134 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
137 return cia->cia_setkey(crypto_tfm_ctx(tfm), key, keylen,
141 static int ecb_encrypt(struct crypto_tfm *tfm,
142 struct scatterlist *dst,
143 struct scatterlist *src, unsigned int nbytes)
145 return crypt(tfm, dst, src, nbytes,
146 tfm->__crt_alg->cra_cipher.cia_encrypt,
147 ecb_process, 1, NULL);
150 static int ecb_encrypt_iv(struct crypto_tfm *tfm,
151 struct scatterlist *dst,
152 struct scatterlist *src,
153 unsigned int nbytes, u8 *iv)
155 ecb_encrypt(tfm, dst, src, nbytes);
159 static int ecb_decrypt(struct crypto_tfm *tfm,
160 struct scatterlist *dst,
161 struct scatterlist *src,
164 return crypt(tfm, dst, src, nbytes,
165 tfm->__crt_alg->cra_cipher.cia_decrypt,
166 ecb_process, 1, NULL);
169 static int ecb_decrypt_iv(struct crypto_tfm *tfm,
170 struct scatterlist *dst,
171 struct scatterlist *src,
172 unsigned int nbytes, u8 *iv)
174 ecb_decrypt(tfm, dst, src, nbytes);
178 static int cbc_encrypt(struct crypto_tfm *tfm,
179 struct scatterlist *dst,
180 struct scatterlist *src,
183 return crypt(tfm, dst, src, nbytes,
184 tfm->__crt_alg->cra_cipher.cia_encrypt,
185 cbc_process, 1, tfm->crt_cipher.cit_iv);
188 static int cbc_encrypt_iv(struct crypto_tfm *tfm,
189 struct scatterlist *dst,
190 struct scatterlist *src,
191 unsigned int nbytes, u8 *iv)
193 return crypt(tfm, dst, src, nbytes,
194 tfm->__crt_alg->cra_cipher.cia_encrypt,
198 static int cbc_decrypt(struct crypto_tfm *tfm,
199 struct scatterlist *dst,
200 struct scatterlist *src,
203 return crypt(tfm, dst, src, nbytes,
204 tfm->__crt_alg->cra_cipher.cia_decrypt,
205 cbc_process, 0, tfm->crt_cipher.cit_iv);
208 static int cbc_decrypt_iv(struct crypto_tfm *tfm,
209 struct scatterlist *dst,
210 struct scatterlist *src,
211 unsigned int nbytes, u8 *iv)
213 return crypt(tfm, dst, src, nbytes,
214 tfm->__crt_alg->cra_cipher.cia_decrypt,
219 * nocrypt*() zeroize the destination buffer to make sure we don't leak
220 * uninitialized memory contents if the caller ignores the return value.
221 * This is bad since the data in the source buffer is unused and may be
222 * lost, but an infoleak would be even worse. The performance cost of
223 * memset() is irrelevant since a well-behaved caller would not bump into
224 * the error repeatedly.
226 static int nocrypt(struct crypto_tfm *tfm,
227 struct scatterlist *dst,
228 struct scatterlist *src,
231 memset(dst, 0, nbytes);
235 static int nocrypt_iv(struct crypto_tfm *tfm,
236 struct scatterlist *dst,
237 struct scatterlist *src,
238 unsigned int nbytes, u8 *iv)
240 memset(dst, 0, nbytes);
244 int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags)
246 u32 mode = flags & CRYPTO_TFM_MODE_MASK;
248 tfm->crt_cipher.cit_mode = mode ? mode : CRYPTO_TFM_MODE_ECB;
249 if (flags & CRYPTO_TFM_REQ_WEAK_KEY)
250 tfm->crt_flags = CRYPTO_TFM_REQ_WEAK_KEY;
255 int crypto_init_cipher_ops(struct crypto_tfm *tfm)
258 struct cipher_tfm *ops = &tfm->crt_cipher;
260 ops->cit_setkey = setkey;
262 switch (tfm->crt_cipher.cit_mode) {
263 case CRYPTO_TFM_MODE_ECB:
264 ops->cit_encrypt = ecb_encrypt;
265 ops->cit_decrypt = ecb_decrypt;
266 /* These should have been nocrypt_iv, but patch-cryptoloop-jari-2.4.22.0
267 * (and its other revisions) directly calls the *_iv() functions even in
268 * ECB mode and ignores their return value. */
269 ops->cit_encrypt_iv = ecb_encrypt_iv;
270 ops->cit_decrypt_iv = ecb_decrypt_iv;
273 case CRYPTO_TFM_MODE_CBC:
274 ops->cit_encrypt = cbc_encrypt;
275 ops->cit_decrypt = cbc_decrypt;
276 ops->cit_encrypt_iv = cbc_encrypt_iv;
277 ops->cit_decrypt_iv = cbc_decrypt_iv;
280 case CRYPTO_TFM_MODE_CFB:
281 ops->cit_encrypt = nocrypt;
282 ops->cit_decrypt = nocrypt;
283 ops->cit_encrypt_iv = nocrypt_iv;
284 ops->cit_decrypt_iv = nocrypt_iv;
287 case CRYPTO_TFM_MODE_CTR:
288 ops->cit_encrypt = nocrypt;
289 ops->cit_decrypt = nocrypt;
290 ops->cit_encrypt_iv = nocrypt_iv;
291 ops->cit_decrypt_iv = nocrypt_iv;
298 if (ops->cit_mode == CRYPTO_TFM_MODE_CBC) {
300 switch (crypto_tfm_alg_blocksize(tfm)) {
302 ops->cit_xor_block = xor_64;
306 ops->cit_xor_block = xor_128;
310 printk(KERN_WARNING "%s: block size %u not supported\n",
311 crypto_tfm_alg_name(tfm),
312 crypto_tfm_alg_blocksize(tfm));
317 ops->cit_ivsize = crypto_tfm_alg_blocksize(tfm);
318 ops->cit_iv = kmalloc(ops->cit_ivsize, GFP_KERNEL);
319 if (ops->cit_iv == NULL)
327 void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
329 if (tfm->crt_cipher.cit_iv)
330 kfree(tfm->crt_cipher.cit_iv);