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);
55 u8 tmp_src[nbytes > src->length ? bsize : 0];
56 u8 tmp_dst[nbytes > dst->length ? bsize : 0];
62 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
66 scatterwalk_start(&walk_in, src);
67 scatterwalk_start(&walk_out, dst);
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);
79 scatterwalk_copychunks(src_p, &walk_in, bsize, 0);
81 prfn(tfm, dst_p, src_p, crfn, enc, info,
82 scatterwalk_samebuf(&walk_in, &walk_out,
85 scatterwalk_done(&walk_in, 0, nbytes);
87 scatterwalk_copychunks(dst_p, &walk_out, bsize, 1);
88 scatterwalk_done(&walk_out, 1, nbytes);
97 static void cbc_process(struct crypto_tfm *tfm, u8 *dst, u8 *src,
98 cryptfn_t fn, int enc, void *info, int in_place)
102 /* Null encryption */
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));
111 u8 stack[in_place ? crypto_tfm_alg_blocksize(tfm) : 0];
112 u8 *buf = in_place ? stack : dst;
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));
118 memcpy(dst, buf, crypto_tfm_alg_blocksize(tfm));
122 static void ecb_process(struct crypto_tfm *tfm, u8 *dst, u8 *src,
123 cryptfn_t fn, int enc, void *info, int in_place)
125 fn(crypto_tfm_ctx(tfm), dst, src);
128 static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
130 struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
132 if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
133 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
136 return cia->cia_setkey(crypto_tfm_ctx(tfm), key, keylen,
140 static int ecb_encrypt(struct crypto_tfm *tfm,
141 struct scatterlist *dst,
142 struct scatterlist *src, unsigned int nbytes)
144 return crypt(tfm, dst, src, nbytes,
145 tfm->__crt_alg->cra_cipher.cia_encrypt,
146 ecb_process, 1, NULL);
149 static int ecb_decrypt(struct crypto_tfm *tfm,
150 struct scatterlist *dst,
151 struct scatterlist *src,
154 return crypt(tfm, dst, src, nbytes,
155 tfm->__crt_alg->cra_cipher.cia_decrypt,
156 ecb_process, 1, NULL);
159 static int cbc_encrypt(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_encrypt,
166 cbc_process, 1, tfm->crt_cipher.cit_iv);
169 static int cbc_encrypt_iv(struct crypto_tfm *tfm,
170 struct scatterlist *dst,
171 struct scatterlist *src,
172 unsigned int nbytes, u8 *iv)
174 return crypt(tfm, dst, src, nbytes,
175 tfm->__crt_alg->cra_cipher.cia_encrypt,
179 static int cbc_decrypt(struct crypto_tfm *tfm,
180 struct scatterlist *dst,
181 struct scatterlist *src,
184 return crypt(tfm, dst, src, nbytes,
185 tfm->__crt_alg->cra_cipher.cia_decrypt,
186 cbc_process, 0, tfm->crt_cipher.cit_iv);
189 static int cbc_decrypt_iv(struct crypto_tfm *tfm,
190 struct scatterlist *dst,
191 struct scatterlist *src,
192 unsigned int nbytes, u8 *iv)
194 return crypt(tfm, dst, src, nbytes,
195 tfm->__crt_alg->cra_cipher.cia_decrypt,
199 static int nocrypt(struct crypto_tfm *tfm,
200 struct scatterlist *dst,
201 struct scatterlist *src,
207 static int nocrypt_iv(struct crypto_tfm *tfm,
208 struct scatterlist *dst,
209 struct scatterlist *src,
210 unsigned int nbytes, u8 *iv)
215 int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags)
217 u32 mode = flags & CRYPTO_TFM_MODE_MASK;
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;
226 int crypto_init_cipher_ops(struct crypto_tfm *tfm)
229 struct cipher_tfm *ops = &tfm->crt_cipher;
231 ops->cit_setkey = setkey;
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;
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;
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;
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;
264 if (ops->cit_mode == CRYPTO_TFM_MODE_CBC) {
266 switch (crypto_tfm_alg_blocksize(tfm)) {
268 ops->cit_xor_block = xor_64;
272 ops->cit_xor_block = xor_128;
276 printk(KERN_WARNING "%s: block size %u not supported\n",
277 crypto_tfm_alg_name(tfm),
278 crypto_tfm_alg_blocksize(tfm));
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)
293 void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
295 if (tfm->crt_cipher.cit_iv)
296 kfree(tfm->crt_cipher.cit_iv);