aboutsummaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorHerbert Xu <herbert@gondor.apana.org.au>2007-11-20 04:33:39 -0500
committerHerbert Xu <herbert@gondor.apana.org.au>2008-01-10 16:16:18 -0500
commit3c7f076da557eadb37240d70b0399ff9763fa2ae (patch)
treef063bc4bd3f1a656d88e8dfbf1451d4b93c0e849 /crypto
parent7613636def82092a5c7b6322078a2af832410417 (diff)
[CRYPTO] cbc: Use crypto_xor
This patch replaces the custom xor in CBC with the generic crypto_xor. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto')
-rw-r--r--crypto/cbc.c93
1 files changed, 16 insertions, 77 deletions
diff --git a/crypto/cbc.c b/crypto/cbc.c
index 1f2649e13b42..b013d6fec1eb 100644
--- a/crypto/cbc.c
+++ b/crypto/cbc.c
@@ -20,7 +20,6 @@
20 20
21struct crypto_cbc_ctx { 21struct crypto_cbc_ctx {
22 struct crypto_cipher *child; 22 struct crypto_cipher *child;
23 void (*xor)(u8 *dst, const u8 *src, unsigned int bs);
24}; 23};
25 24
26static int crypto_cbc_setkey(struct crypto_tfm *parent, const u8 *key, 25static int crypto_cbc_setkey(struct crypto_tfm *parent, const u8 *key,
@@ -41,9 +40,7 @@ static int crypto_cbc_setkey(struct crypto_tfm *parent, const u8 *key,
41 40
42static int crypto_cbc_encrypt_segment(struct blkcipher_desc *desc, 41static int crypto_cbc_encrypt_segment(struct blkcipher_desc *desc,
43 struct blkcipher_walk *walk, 42 struct blkcipher_walk *walk,
44 struct crypto_cipher *tfm, 43 struct crypto_cipher *tfm)
45 void (*xor)(u8 *, const u8 *,
46 unsigned int))
47{ 44{
48 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = 45 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
49 crypto_cipher_alg(tfm)->cia_encrypt; 46 crypto_cipher_alg(tfm)->cia_encrypt;
@@ -54,7 +51,7 @@ static int crypto_cbc_encrypt_segment(struct blkcipher_desc *desc,
54 u8 *iv = walk->iv; 51 u8 *iv = walk->iv;
55 52
56 do { 53 do {
57 xor(iv, src, bsize); 54 crypto_xor(iv, src, bsize);
58 fn(crypto_cipher_tfm(tfm), dst, iv); 55 fn(crypto_cipher_tfm(tfm), dst, iv);
59 memcpy(iv, dst, bsize); 56 memcpy(iv, dst, bsize);
60 57
@@ -67,9 +64,7 @@ static int crypto_cbc_encrypt_segment(struct blkcipher_desc *desc,
67 64
68static int crypto_cbc_encrypt_inplace(struct blkcipher_desc *desc, 65static int crypto_cbc_encrypt_inplace(struct blkcipher_desc *desc,
69 struct blkcipher_walk *walk, 66 struct blkcipher_walk *walk,
70 struct crypto_cipher *tfm, 67 struct crypto_cipher *tfm)
71 void (*xor)(u8 *, const u8 *,
72 unsigned int))
73{ 68{
74 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = 69 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
75 crypto_cipher_alg(tfm)->cia_encrypt; 70 crypto_cipher_alg(tfm)->cia_encrypt;
@@ -79,7 +74,7 @@ static int crypto_cbc_encrypt_inplace(struct blkcipher_desc *desc,
79 u8 *iv = walk->iv; 74 u8 *iv = walk->iv;
80 75
81 do { 76 do {
82 xor(src, iv, bsize); 77 crypto_xor(src, iv, bsize);
83 fn(crypto_cipher_tfm(tfm), src, src); 78 fn(crypto_cipher_tfm(tfm), src, src);
84 iv = src; 79 iv = src;
85 80
@@ -99,7 +94,6 @@ static int crypto_cbc_encrypt(struct blkcipher_desc *desc,
99 struct crypto_blkcipher *tfm = desc->tfm; 94 struct crypto_blkcipher *tfm = desc->tfm;
100 struct crypto_cbc_ctx *ctx = crypto_blkcipher_ctx(tfm); 95 struct crypto_cbc_ctx *ctx = crypto_blkcipher_ctx(tfm);
101 struct crypto_cipher *child = ctx->child; 96 struct crypto_cipher *child = ctx->child;
102 void (*xor)(u8 *, const u8 *, unsigned int bs) = ctx->xor;
103 int err; 97 int err;
104 98
105 blkcipher_walk_init(&walk, dst, src, nbytes); 99 blkcipher_walk_init(&walk, dst, src, nbytes);
@@ -107,11 +101,9 @@ static int crypto_cbc_encrypt(struct blkcipher_desc *desc,
107 101
108 while ((nbytes = walk.nbytes)) { 102 while ((nbytes = walk.nbytes)) {
109 if (walk.src.virt.addr == walk.dst.virt.addr) 103 if (walk.src.virt.addr == walk.dst.virt.addr)
110 nbytes = crypto_cbc_encrypt_inplace(desc, &walk, child, 104 nbytes = crypto_cbc_encrypt_inplace(desc, &walk, child);
111 xor);
112 else 105 else
113 nbytes = crypto_cbc_encrypt_segment(desc, &walk, child, 106 nbytes = crypto_cbc_encrypt_segment(desc, &walk, child);
114 xor);
115 err = blkcipher_walk_done(desc, &walk, nbytes); 107 err = blkcipher_walk_done(desc, &walk, nbytes);
116 } 108 }
117 109
@@ -120,9 +112,7 @@ static int crypto_cbc_encrypt(struct blkcipher_desc *desc,
120 112
121static int crypto_cbc_decrypt_segment(struct blkcipher_desc *desc, 113static int crypto_cbc_decrypt_segment(struct blkcipher_desc *desc,
122 struct blkcipher_walk *walk, 114 struct blkcipher_walk *walk,
123 struct crypto_cipher *tfm, 115 struct crypto_cipher *tfm)
124 void (*xor)(u8 *, const u8 *,
125 unsigned int))
126{ 116{
127 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = 117 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
128 crypto_cipher_alg(tfm)->cia_decrypt; 118 crypto_cipher_alg(tfm)->cia_decrypt;
@@ -134,7 +124,7 @@ static int crypto_cbc_decrypt_segment(struct blkcipher_desc *desc,
134 124
135 do { 125 do {
136 fn(crypto_cipher_tfm(tfm), dst, src); 126 fn(crypto_cipher_tfm(tfm), dst, src);
137 xor(dst, iv, bsize); 127 crypto_xor(dst, iv, bsize);
138 iv = src; 128 iv = src;
139 129
140 src += bsize; 130 src += bsize;
@@ -148,9 +138,7 @@ static int crypto_cbc_decrypt_segment(struct blkcipher_desc *desc,
148 138
149static int crypto_cbc_decrypt_inplace(struct blkcipher_desc *desc, 139static int crypto_cbc_decrypt_inplace(struct blkcipher_desc *desc,
150 struct blkcipher_walk *walk, 140 struct blkcipher_walk *walk,
151 struct crypto_cipher *tfm, 141 struct crypto_cipher *tfm)
152 void (*xor)(u8 *, const u8 *,
153 unsigned int))
154{ 142{
155 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = 143 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
156 crypto_cipher_alg(tfm)->cia_decrypt; 144 crypto_cipher_alg(tfm)->cia_decrypt;
@@ -171,11 +159,11 @@ static int crypto_cbc_decrypt_inplace(struct blkcipher_desc *desc,
171 fn(crypto_cipher_tfm(tfm), src, src); 159 fn(crypto_cipher_tfm(tfm), src, src);
172 if ((nbytes -= bsize) < bsize) 160 if ((nbytes -= bsize) < bsize)
173 break; 161 break;
174 xor(src, src - bsize, bsize); 162 crypto_xor(src, src - bsize, bsize);
175 src -= bsize; 163 src -= bsize;
176 } 164 }
177 165
178 xor(src, first_iv, bsize); 166 crypto_xor(src, first_iv, bsize);
179 167
180 return nbytes; 168 return nbytes;
181} 169}
@@ -188,7 +176,6 @@ static int crypto_cbc_decrypt(struct blkcipher_desc *desc,
188 struct crypto_blkcipher *tfm = desc->tfm; 176 struct crypto_blkcipher *tfm = desc->tfm;
189 struct crypto_cbc_ctx *ctx = crypto_blkcipher_ctx(tfm); 177 struct crypto_cbc_ctx *ctx = crypto_blkcipher_ctx(tfm);
190 struct crypto_cipher *child = ctx->child; 178 struct crypto_cipher *child = ctx->child;
191 void (*xor)(u8 *, const u8 *, unsigned int bs) = ctx->xor;
192 int err; 179 int err;
193 180
194 blkcipher_walk_init(&walk, dst, src, nbytes); 181 blkcipher_walk_init(&walk, dst, src, nbytes);
@@ -196,48 +183,15 @@ static int crypto_cbc_decrypt(struct blkcipher_desc *desc,
196 183
197 while ((nbytes = walk.nbytes)) { 184 while ((nbytes = walk.nbytes)) {
198 if (walk.src.virt.addr == walk.dst.virt.addr) 185 if (walk.src.virt.addr == walk.dst.virt.addr)
199 nbytes = crypto_cbc_decrypt_inplace(desc, &walk, child, 186 nbytes = crypto_cbc_decrypt_inplace(desc, &walk, child);
200 xor);
201 else 187 else
202 nbytes = crypto_cbc_decrypt_segment(desc, &walk, child, 188 nbytes = crypto_cbc_decrypt_segment(desc, &walk, child);
203 xor);
204 err = blkcipher_walk_done(desc, &walk, nbytes); 189 err = blkcipher_walk_done(desc, &walk, nbytes);
205 } 190 }
206 191
207 return err; 192 return err;
208} 193}
209 194
210static void xor_byte(u8 *a, const u8 *b, unsigned int bs)
211{
212 do {
213 *a++ ^= *b++;
214 } while (--bs);
215}
216
217static void xor_quad(u8 *dst, const u8 *src, unsigned int bs)
218{
219 u32 *a = (u32 *)dst;
220 u32 *b = (u32 *)src;
221
222 do {
223 *a++ ^= *b++;
224 } while ((bs -= 4));
225}
226
227static void xor_64(u8 *a, const u8 *b, unsigned int bs)
228{
229 ((u32 *)a)[0] ^= ((u32 *)b)[0];
230 ((u32 *)a)[1] ^= ((u32 *)b)[1];
231}
232
233static void xor_128(u8 *a, const u8 *b, unsigned int bs)
234{
235 ((u32 *)a)[0] ^= ((u32 *)b)[0];
236 ((u32 *)a)[1] ^= ((u32 *)b)[1];
237 ((u32 *)a)[2] ^= ((u32 *)b)[2];
238 ((u32 *)a)[3] ^= ((u32 *)b)[3];
239}
240
241static int crypto_cbc_init_tfm(struct crypto_tfm *tfm) 195static int crypto_cbc_init_tfm(struct crypto_tfm *tfm)
242{ 196{
243 struct crypto_instance *inst = (void *)tfm->__crt_alg; 197 struct crypto_instance *inst = (void *)tfm->__crt_alg;
@@ -245,22 +199,6 @@ static int crypto_cbc_init_tfm(struct crypto_tfm *tfm)
245 struct crypto_cbc_ctx *ctx = crypto_tfm_ctx(tfm); 199 struct crypto_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
246 struct crypto_cipher *cipher; 200 struct crypto_cipher *cipher;
247 201
248 switch (crypto_tfm_alg_blocksize(tfm)) {
249 case 8:
250 ctx->xor = xor_64;
251 break;
252
253 case 16:
254 ctx->xor = xor_128;
255 break;
256
257 default:
258 if (crypto_tfm_alg_blocksize(tfm) % 4)
259 ctx->xor = xor_byte;
260 else
261 ctx->xor = xor_quad;
262 }
263
264 cipher = crypto_spawn_cipher(spawn); 202 cipher = crypto_spawn_cipher(spawn);
265 if (IS_ERR(cipher)) 203 if (IS_ERR(cipher))
266 return PTR_ERR(cipher); 204 return PTR_ERR(cipher);
@@ -300,8 +238,9 @@ static struct crypto_instance *crypto_cbc_alloc(struct rtattr **tb)
300 inst->alg.cra_alignmask = alg->cra_alignmask; 238 inst->alg.cra_alignmask = alg->cra_alignmask;
301 inst->alg.cra_type = &crypto_blkcipher_type; 239 inst->alg.cra_type = &crypto_blkcipher_type;
302 240
303 if (!(alg->cra_blocksize % 4)) 241 /* We access the data as u32s when xoring. */
304 inst->alg.cra_alignmask |= 3; 242 inst->alg.cra_alignmask |= __alignof__(u32) - 1;
243
305 inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize; 244 inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize;
306 inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize; 245 inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
307 inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize; 246 inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize;