Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Glue Code for assembler optimized version of 3DES
4 *
5 * Copyright © 2014 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
6 *
7 * CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
8 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
9 */
10
11#include <crypto/algapi.h>
12#include <crypto/des.h>
13#include <crypto/internal/skcipher.h>
14#include <linux/crypto.h>
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/types.h>
18
19struct des3_ede_x86_ctx {
20 struct des3_ede_ctx enc;
21 struct des3_ede_ctx dec;
22};
23
24/* regular block cipher functions */
25asmlinkage void des3_ede_x86_64_crypt_blk(const u32 *expkey, u8 *dst,
26 const u8 *src);
27
28/* 3-way parallel cipher functions */
29asmlinkage void des3_ede_x86_64_crypt_blk_3way(const u32 *expkey, u8 *dst,
30 const u8 *src);
31
32static inline void des3_ede_enc_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
33 const u8 *src)
34{
35 u32 *enc_ctx = ctx->enc.expkey;
36
37 des3_ede_x86_64_crypt_blk(enc_ctx, dst, src);
38}
39
40static inline void des3_ede_dec_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
41 const u8 *src)
42{
43 u32 *dec_ctx = ctx->dec.expkey;
44
45 des3_ede_x86_64_crypt_blk(dec_ctx, dst, src);
46}
47
48static inline void des3_ede_dec_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst,
49 const u8 *src)
50{
51 u32 *dec_ctx = ctx->dec.expkey;
52
53 des3_ede_x86_64_crypt_blk_3way(dec_ctx, dst, src);
54}
55
56static void des3_ede_x86_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
57{
58 des3_ede_enc_blk(crypto_tfm_ctx(tfm), dst, src);
59}
60
61static void des3_ede_x86_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
62{
63 des3_ede_dec_blk(crypto_tfm_ctx(tfm), dst, src);
64}
65
66static int ecb_crypt(struct skcipher_request *req, const u32 *expkey)
67{
68 const unsigned int bsize = DES3_EDE_BLOCK_SIZE;
69 struct skcipher_walk walk;
70 unsigned int nbytes;
71 int err;
72
73 err = skcipher_walk_virt(&walk, req, false);
74
75 while ((nbytes = walk.nbytes)) {
76 u8 *wsrc = walk.src.virt.addr;
77 u8 *wdst = walk.dst.virt.addr;
78
79 /* Process four block batch */
80 if (nbytes >= bsize * 3) {
81 do {
82 des3_ede_x86_64_crypt_blk_3way(expkey, wdst,
83 wsrc);
84
85 wsrc += bsize * 3;
86 wdst += bsize * 3;
87 nbytes -= bsize * 3;
88 } while (nbytes >= bsize * 3);
89
90 if (nbytes < bsize)
91 goto done;
92 }
93
94 /* Handle leftovers */
95 do {
96 des3_ede_x86_64_crypt_blk(expkey, wdst, wsrc);
97
98 wsrc += bsize;
99 wdst += bsize;
100 nbytes -= bsize;
101 } while (nbytes >= bsize);
102
103done:
104 err = skcipher_walk_done(&walk, nbytes);
105 }
106
107 return err;
108}
109
110static int ecb_encrypt(struct skcipher_request *req)
111{
112 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
113 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
114
115 return ecb_crypt(req, ctx->enc.expkey);
116}
117
118static int ecb_decrypt(struct skcipher_request *req)
119{
120 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
121 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
122
123 return ecb_crypt(req, ctx->dec.expkey);
124}
125
126static unsigned int __cbc_encrypt(struct des3_ede_x86_ctx *ctx,
127 struct skcipher_walk *walk)
128{
129 unsigned int bsize = DES3_EDE_BLOCK_SIZE;
130 unsigned int nbytes = walk->nbytes;
131 u64 *src = (u64 *)walk->src.virt.addr;
132 u64 *dst = (u64 *)walk->dst.virt.addr;
133 u64 *iv = (u64 *)walk->iv;
134
135 do {
136 *dst = *src ^ *iv;
137 des3_ede_enc_blk(ctx, (u8 *)dst, (u8 *)dst);
138 iv = dst;
139
140 src += 1;
141 dst += 1;
142 nbytes -= bsize;
143 } while (nbytes >= bsize);
144
145 *(u64 *)walk->iv = *iv;
146 return nbytes;
147}
148
149static int cbc_encrypt(struct skcipher_request *req)
150{
151 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
152 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
153 struct skcipher_walk walk;
154 unsigned int nbytes;
155 int err;
156
157 err = skcipher_walk_virt(&walk, req, false);
158
159 while (walk.nbytes) {
160 nbytes = __cbc_encrypt(ctx, &walk);
161 err = skcipher_walk_done(&walk, nbytes);
162 }
163
164 return err;
165}
166
167static unsigned int __cbc_decrypt(struct des3_ede_x86_ctx *ctx,
168 struct skcipher_walk *walk)
169{
170 unsigned int bsize = DES3_EDE_BLOCK_SIZE;
171 unsigned int nbytes = walk->nbytes;
172 u64 *src = (u64 *)walk->src.virt.addr;
173 u64 *dst = (u64 *)walk->dst.virt.addr;
174 u64 ivs[3 - 1];
175 u64 last_iv;
176
177 /* Start of the last block. */
178 src += nbytes / bsize - 1;
179 dst += nbytes / bsize - 1;
180
181 last_iv = *src;
182
183 /* Process four block batch */
184 if (nbytes >= bsize * 3) {
185 do {
186 nbytes -= bsize * 3 - bsize;
187 src -= 3 - 1;
188 dst -= 3 - 1;
189
190 ivs[0] = src[0];
191 ivs[1] = src[1];
192
193 des3_ede_dec_blk_3way(ctx, (u8 *)dst, (u8 *)src);
194
195 dst[1] ^= ivs[0];
196 dst[2] ^= ivs[1];
197
198 nbytes -= bsize;
199 if (nbytes < bsize)
200 goto done;
201
202 *dst ^= *(src - 1);
203 src -= 1;
204 dst -= 1;
205 } while (nbytes >= bsize * 3);
206 }
207
208 /* Handle leftovers */
209 for (;;) {
210 des3_ede_dec_blk(ctx, (u8 *)dst, (u8 *)src);
211
212 nbytes -= bsize;
213 if (nbytes < bsize)
214 break;
215
216 *dst ^= *(src - 1);
217 src -= 1;
218 dst -= 1;
219 }
220
221done:
222 *dst ^= *(u64 *)walk->iv;
223 *(u64 *)walk->iv = last_iv;
224
225 return nbytes;
226}
227
228static int cbc_decrypt(struct skcipher_request *req)
229{
230 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
231 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
232 struct skcipher_walk walk;
233 unsigned int nbytes;
234 int err;
235
236 err = skcipher_walk_virt(&walk, req, false);
237
238 while (walk.nbytes) {
239 nbytes = __cbc_decrypt(ctx, &walk);
240 err = skcipher_walk_done(&walk, nbytes);
241 }
242
243 return err;
244}
245
246static int des3_ede_x86_setkey(struct crypto_tfm *tfm, const u8 *key,
247 unsigned int keylen)
248{
249 struct des3_ede_x86_ctx *ctx = crypto_tfm_ctx(tfm);
250 u32 i, j, tmp;
251 int err;
252
253 err = des3_ede_expand_key(&ctx->enc, key, keylen);
254 if (err == -ENOKEY) {
255 if (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)
256 err = -EINVAL;
257 else
258 err = 0;
259 }
260
261 if (err) {
262 memset(ctx, 0, sizeof(*ctx));
263 return err;
264 }
265
266 /* Fix encryption context for this implementation and form decryption
267 * context. */
268 j = DES3_EDE_EXPKEY_WORDS - 2;
269 for (i = 0; i < DES3_EDE_EXPKEY_WORDS; i += 2, j -= 2) {
270 tmp = ror32(ctx->enc.expkey[i + 1], 4);
271 ctx->enc.expkey[i + 1] = tmp;
272
273 ctx->dec.expkey[j + 0] = ctx->enc.expkey[i + 0];
274 ctx->dec.expkey[j + 1] = tmp;
275 }
276
277 return 0;
278}
279
280static int des3_ede_x86_setkey_skcipher(struct crypto_skcipher *tfm,
281 const u8 *key,
282 unsigned int keylen)
283{
284 return des3_ede_x86_setkey(&tfm->base, key, keylen);
285}
286
287static struct crypto_alg des3_ede_cipher = {
288 .cra_name = "des3_ede",
289 .cra_driver_name = "des3_ede-asm",
290 .cra_priority = 200,
291 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
292 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
293 .cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
294 .cra_alignmask = 0,
295 .cra_module = THIS_MODULE,
296 .cra_u = {
297 .cipher = {
298 .cia_min_keysize = DES3_EDE_KEY_SIZE,
299 .cia_max_keysize = DES3_EDE_KEY_SIZE,
300 .cia_setkey = des3_ede_x86_setkey,
301 .cia_encrypt = des3_ede_x86_encrypt,
302 .cia_decrypt = des3_ede_x86_decrypt,
303 }
304 }
305};
306
307static struct skcipher_alg des3_ede_skciphers[] = {
308 {
309 .base.cra_name = "ecb(des3_ede)",
310 .base.cra_driver_name = "ecb-des3_ede-asm",
311 .base.cra_priority = 300,
312 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
313 .base.cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
314 .base.cra_module = THIS_MODULE,
315 .min_keysize = DES3_EDE_KEY_SIZE,
316 .max_keysize = DES3_EDE_KEY_SIZE,
317 .setkey = des3_ede_x86_setkey_skcipher,
318 .encrypt = ecb_encrypt,
319 .decrypt = ecb_decrypt,
320 }, {
321 .base.cra_name = "cbc(des3_ede)",
322 .base.cra_driver_name = "cbc-des3_ede-asm",
323 .base.cra_priority = 300,
324 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
325 .base.cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
326 .base.cra_module = THIS_MODULE,
327 .min_keysize = DES3_EDE_KEY_SIZE,
328 .max_keysize = DES3_EDE_KEY_SIZE,
329 .ivsize = DES3_EDE_BLOCK_SIZE,
330 .setkey = des3_ede_x86_setkey_skcipher,
331 .encrypt = cbc_encrypt,
332 .decrypt = cbc_decrypt,
333 }
334};
335
336static bool is_blacklisted_cpu(void)
337{
338 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
339 return false;
340
341 if (boot_cpu_data.x86 == 0x0f) {
342 /*
343 * On Pentium 4, des3_ede-x86_64 is slower than generic C
344 * implementation because use of 64bit rotates (which are really
345 * slow on P4). Therefore blacklist P4s.
346 */
347 return true;
348 }
349
350 return false;
351}
352
353static int force;
354module_param(force, int, 0);
355MODULE_PARM_DESC(force, "Force module load, ignore CPU blacklist");
356
357static int __init des3_ede_x86_init(void)
358{
359 int err;
360
361 if (!force && is_blacklisted_cpu()) {
362 pr_info("des3_ede-x86_64: performance on this CPU would be suboptimal: disabling des3_ede-x86_64.\n");
363 return -ENODEV;
364 }
365
366 err = crypto_register_alg(&des3_ede_cipher);
367 if (err)
368 return err;
369
370 err = crypto_register_skciphers(des3_ede_skciphers,
371 ARRAY_SIZE(des3_ede_skciphers));
372 if (err)
373 crypto_unregister_alg(&des3_ede_cipher);
374
375 return err;
376}
377
378static void __exit des3_ede_x86_fini(void)
379{
380 crypto_unregister_alg(&des3_ede_cipher);
381 crypto_unregister_skciphers(des3_ede_skciphers,
382 ARRAY_SIZE(des3_ede_skciphers));
383}
384
385module_init(des3_ede_x86_init);
386module_exit(des3_ede_x86_fini);
387
388MODULE_LICENSE("GPL");
389MODULE_DESCRIPTION("Triple DES EDE Cipher Algorithm, asm optimized");
390MODULE_ALIAS_CRYPTO("des3_ede");
391MODULE_ALIAS_CRYPTO("des3_ede-asm");
392MODULE_AUTHOR("Jussi Kivilinna <jussi.kivilinna@iki.fi>");
1/*
2 * Glue Code for assembler optimized version of 3DES
3 *
4 * Copyright © 2014 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
5 *
6 * CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
7 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
8 * CTR part based on code (crypto/ctr.c) by:
9 * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 */
22
23#include <crypto/algapi.h>
24#include <crypto/des.h>
25#include <crypto/internal/skcipher.h>
26#include <linux/crypto.h>
27#include <linux/init.h>
28#include <linux/module.h>
29#include <linux/types.h>
30
31struct des3_ede_x86_ctx {
32 u32 enc_expkey[DES3_EDE_EXPKEY_WORDS];
33 u32 dec_expkey[DES3_EDE_EXPKEY_WORDS];
34};
35
36/* regular block cipher functions */
37asmlinkage void des3_ede_x86_64_crypt_blk(const u32 *expkey, u8 *dst,
38 const u8 *src);
39
40/* 3-way parallel cipher functions */
41asmlinkage void des3_ede_x86_64_crypt_blk_3way(const u32 *expkey, u8 *dst,
42 const u8 *src);
43
44static inline void des3_ede_enc_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
45 const u8 *src)
46{
47 u32 *enc_ctx = ctx->enc_expkey;
48
49 des3_ede_x86_64_crypt_blk(enc_ctx, dst, src);
50}
51
52static inline void des3_ede_dec_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
53 const u8 *src)
54{
55 u32 *dec_ctx = ctx->dec_expkey;
56
57 des3_ede_x86_64_crypt_blk(dec_ctx, dst, src);
58}
59
60static inline void des3_ede_enc_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst,
61 const u8 *src)
62{
63 u32 *enc_ctx = ctx->enc_expkey;
64
65 des3_ede_x86_64_crypt_blk_3way(enc_ctx, dst, src);
66}
67
68static inline void des3_ede_dec_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst,
69 const u8 *src)
70{
71 u32 *dec_ctx = ctx->dec_expkey;
72
73 des3_ede_x86_64_crypt_blk_3way(dec_ctx, dst, src);
74}
75
76static void des3_ede_x86_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
77{
78 des3_ede_enc_blk(crypto_tfm_ctx(tfm), dst, src);
79}
80
81static void des3_ede_x86_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
82{
83 des3_ede_dec_blk(crypto_tfm_ctx(tfm), dst, src);
84}
85
86static int ecb_crypt(struct skcipher_request *req, const u32 *expkey)
87{
88 const unsigned int bsize = DES3_EDE_BLOCK_SIZE;
89 struct skcipher_walk walk;
90 unsigned int nbytes;
91 int err;
92
93 err = skcipher_walk_virt(&walk, req, false);
94
95 while ((nbytes = walk.nbytes)) {
96 u8 *wsrc = walk.src.virt.addr;
97 u8 *wdst = walk.dst.virt.addr;
98
99 /* Process four block batch */
100 if (nbytes >= bsize * 3) {
101 do {
102 des3_ede_x86_64_crypt_blk_3way(expkey, wdst,
103 wsrc);
104
105 wsrc += bsize * 3;
106 wdst += bsize * 3;
107 nbytes -= bsize * 3;
108 } while (nbytes >= bsize * 3);
109
110 if (nbytes < bsize)
111 goto done;
112 }
113
114 /* Handle leftovers */
115 do {
116 des3_ede_x86_64_crypt_blk(expkey, wdst, wsrc);
117
118 wsrc += bsize;
119 wdst += bsize;
120 nbytes -= bsize;
121 } while (nbytes >= bsize);
122
123done:
124 err = skcipher_walk_done(&walk, nbytes);
125 }
126
127 return err;
128}
129
130static int ecb_encrypt(struct skcipher_request *req)
131{
132 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
133 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
134
135 return ecb_crypt(req, ctx->enc_expkey);
136}
137
138static int ecb_decrypt(struct skcipher_request *req)
139{
140 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
141 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
142
143 return ecb_crypt(req, ctx->dec_expkey);
144}
145
146static unsigned int __cbc_encrypt(struct des3_ede_x86_ctx *ctx,
147 struct skcipher_walk *walk)
148{
149 unsigned int bsize = DES3_EDE_BLOCK_SIZE;
150 unsigned int nbytes = walk->nbytes;
151 u64 *src = (u64 *)walk->src.virt.addr;
152 u64 *dst = (u64 *)walk->dst.virt.addr;
153 u64 *iv = (u64 *)walk->iv;
154
155 do {
156 *dst = *src ^ *iv;
157 des3_ede_enc_blk(ctx, (u8 *)dst, (u8 *)dst);
158 iv = dst;
159
160 src += 1;
161 dst += 1;
162 nbytes -= bsize;
163 } while (nbytes >= bsize);
164
165 *(u64 *)walk->iv = *iv;
166 return nbytes;
167}
168
169static int cbc_encrypt(struct skcipher_request *req)
170{
171 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
172 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
173 struct skcipher_walk walk;
174 unsigned int nbytes;
175 int err;
176
177 err = skcipher_walk_virt(&walk, req, false);
178
179 while ((nbytes = walk.nbytes)) {
180 nbytes = __cbc_encrypt(ctx, &walk);
181 err = skcipher_walk_done(&walk, nbytes);
182 }
183
184 return err;
185}
186
187static unsigned int __cbc_decrypt(struct des3_ede_x86_ctx *ctx,
188 struct skcipher_walk *walk)
189{
190 unsigned int bsize = DES3_EDE_BLOCK_SIZE;
191 unsigned int nbytes = walk->nbytes;
192 u64 *src = (u64 *)walk->src.virt.addr;
193 u64 *dst = (u64 *)walk->dst.virt.addr;
194 u64 ivs[3 - 1];
195 u64 last_iv;
196
197 /* Start of the last block. */
198 src += nbytes / bsize - 1;
199 dst += nbytes / bsize - 1;
200
201 last_iv = *src;
202
203 /* Process four block batch */
204 if (nbytes >= bsize * 3) {
205 do {
206 nbytes -= bsize * 3 - bsize;
207 src -= 3 - 1;
208 dst -= 3 - 1;
209
210 ivs[0] = src[0];
211 ivs[1] = src[1];
212
213 des3_ede_dec_blk_3way(ctx, (u8 *)dst, (u8 *)src);
214
215 dst[1] ^= ivs[0];
216 dst[2] ^= ivs[1];
217
218 nbytes -= bsize;
219 if (nbytes < bsize)
220 goto done;
221
222 *dst ^= *(src - 1);
223 src -= 1;
224 dst -= 1;
225 } while (nbytes >= bsize * 3);
226 }
227
228 /* Handle leftovers */
229 for (;;) {
230 des3_ede_dec_blk(ctx, (u8 *)dst, (u8 *)src);
231
232 nbytes -= bsize;
233 if (nbytes < bsize)
234 break;
235
236 *dst ^= *(src - 1);
237 src -= 1;
238 dst -= 1;
239 }
240
241done:
242 *dst ^= *(u64 *)walk->iv;
243 *(u64 *)walk->iv = last_iv;
244
245 return nbytes;
246}
247
248static int cbc_decrypt(struct skcipher_request *req)
249{
250 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
251 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
252 struct skcipher_walk walk;
253 unsigned int nbytes;
254 int err;
255
256 err = skcipher_walk_virt(&walk, req, false);
257
258 while ((nbytes = walk.nbytes)) {
259 nbytes = __cbc_decrypt(ctx, &walk);
260 err = skcipher_walk_done(&walk, nbytes);
261 }
262
263 return err;
264}
265
266static void ctr_crypt_final(struct des3_ede_x86_ctx *ctx,
267 struct skcipher_walk *walk)
268{
269 u8 *ctrblk = walk->iv;
270 u8 keystream[DES3_EDE_BLOCK_SIZE];
271 u8 *src = walk->src.virt.addr;
272 u8 *dst = walk->dst.virt.addr;
273 unsigned int nbytes = walk->nbytes;
274
275 des3_ede_enc_blk(ctx, keystream, ctrblk);
276 crypto_xor_cpy(dst, keystream, src, nbytes);
277
278 crypto_inc(ctrblk, DES3_EDE_BLOCK_SIZE);
279}
280
281static unsigned int __ctr_crypt(struct des3_ede_x86_ctx *ctx,
282 struct skcipher_walk *walk)
283{
284 unsigned int bsize = DES3_EDE_BLOCK_SIZE;
285 unsigned int nbytes = walk->nbytes;
286 __be64 *src = (__be64 *)walk->src.virt.addr;
287 __be64 *dst = (__be64 *)walk->dst.virt.addr;
288 u64 ctrblk = be64_to_cpu(*(__be64 *)walk->iv);
289 __be64 ctrblocks[3];
290
291 /* Process four block batch */
292 if (nbytes >= bsize * 3) {
293 do {
294 /* create ctrblks for parallel encrypt */
295 ctrblocks[0] = cpu_to_be64(ctrblk++);
296 ctrblocks[1] = cpu_to_be64(ctrblk++);
297 ctrblocks[2] = cpu_to_be64(ctrblk++);
298
299 des3_ede_enc_blk_3way(ctx, (u8 *)ctrblocks,
300 (u8 *)ctrblocks);
301
302 dst[0] = src[0] ^ ctrblocks[0];
303 dst[1] = src[1] ^ ctrblocks[1];
304 dst[2] = src[2] ^ ctrblocks[2];
305
306 src += 3;
307 dst += 3;
308 } while ((nbytes -= bsize * 3) >= bsize * 3);
309
310 if (nbytes < bsize)
311 goto done;
312 }
313
314 /* Handle leftovers */
315 do {
316 ctrblocks[0] = cpu_to_be64(ctrblk++);
317
318 des3_ede_enc_blk(ctx, (u8 *)ctrblocks, (u8 *)ctrblocks);
319
320 dst[0] = src[0] ^ ctrblocks[0];
321
322 src += 1;
323 dst += 1;
324 } while ((nbytes -= bsize) >= bsize);
325
326done:
327 *(__be64 *)walk->iv = cpu_to_be64(ctrblk);
328 return nbytes;
329}
330
331static int ctr_crypt(struct skcipher_request *req)
332{
333 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
334 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
335 struct skcipher_walk walk;
336 unsigned int nbytes;
337 int err;
338
339 err = skcipher_walk_virt(&walk, req, false);
340
341 while ((nbytes = walk.nbytes) >= DES3_EDE_BLOCK_SIZE) {
342 nbytes = __ctr_crypt(ctx, &walk);
343 err = skcipher_walk_done(&walk, nbytes);
344 }
345
346 if (nbytes) {
347 ctr_crypt_final(ctx, &walk);
348 err = skcipher_walk_done(&walk, 0);
349 }
350
351 return err;
352}
353
354static int des3_ede_x86_setkey(struct crypto_tfm *tfm, const u8 *key,
355 unsigned int keylen)
356{
357 struct des3_ede_x86_ctx *ctx = crypto_tfm_ctx(tfm);
358 u32 i, j, tmp;
359 int err;
360
361 /* Generate encryption context using generic implementation. */
362 err = __des3_ede_setkey(ctx->enc_expkey, &tfm->crt_flags, key, keylen);
363 if (err < 0)
364 return err;
365
366 /* Fix encryption context for this implementation and form decryption
367 * context. */
368 j = DES3_EDE_EXPKEY_WORDS - 2;
369 for (i = 0; i < DES3_EDE_EXPKEY_WORDS; i += 2, j -= 2) {
370 tmp = ror32(ctx->enc_expkey[i + 1], 4);
371 ctx->enc_expkey[i + 1] = tmp;
372
373 ctx->dec_expkey[j + 0] = ctx->enc_expkey[i + 0];
374 ctx->dec_expkey[j + 1] = tmp;
375 }
376
377 return 0;
378}
379
380static int des3_ede_x86_setkey_skcipher(struct crypto_skcipher *tfm,
381 const u8 *key,
382 unsigned int keylen)
383{
384 return des3_ede_x86_setkey(&tfm->base, key, keylen);
385}
386
387static struct crypto_alg des3_ede_cipher = {
388 .cra_name = "des3_ede",
389 .cra_driver_name = "des3_ede-asm",
390 .cra_priority = 200,
391 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
392 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
393 .cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
394 .cra_alignmask = 0,
395 .cra_module = THIS_MODULE,
396 .cra_u = {
397 .cipher = {
398 .cia_min_keysize = DES3_EDE_KEY_SIZE,
399 .cia_max_keysize = DES3_EDE_KEY_SIZE,
400 .cia_setkey = des3_ede_x86_setkey,
401 .cia_encrypt = des3_ede_x86_encrypt,
402 .cia_decrypt = des3_ede_x86_decrypt,
403 }
404 }
405};
406
407static struct skcipher_alg des3_ede_skciphers[] = {
408 {
409 .base.cra_name = "ecb(des3_ede)",
410 .base.cra_driver_name = "ecb-des3_ede-asm",
411 .base.cra_priority = 300,
412 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
413 .base.cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
414 .base.cra_module = THIS_MODULE,
415 .min_keysize = DES3_EDE_KEY_SIZE,
416 .max_keysize = DES3_EDE_KEY_SIZE,
417 .setkey = des3_ede_x86_setkey_skcipher,
418 .encrypt = ecb_encrypt,
419 .decrypt = ecb_decrypt,
420 }, {
421 .base.cra_name = "cbc(des3_ede)",
422 .base.cra_driver_name = "cbc-des3_ede-asm",
423 .base.cra_priority = 300,
424 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
425 .base.cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
426 .base.cra_module = THIS_MODULE,
427 .min_keysize = DES3_EDE_KEY_SIZE,
428 .max_keysize = DES3_EDE_KEY_SIZE,
429 .ivsize = DES3_EDE_BLOCK_SIZE,
430 .setkey = des3_ede_x86_setkey_skcipher,
431 .encrypt = cbc_encrypt,
432 .decrypt = cbc_decrypt,
433 }, {
434 .base.cra_name = "ctr(des3_ede)",
435 .base.cra_driver_name = "ctr-des3_ede-asm",
436 .base.cra_priority = 300,
437 .base.cra_blocksize = 1,
438 .base.cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
439 .base.cra_module = THIS_MODULE,
440 .min_keysize = DES3_EDE_KEY_SIZE,
441 .max_keysize = DES3_EDE_KEY_SIZE,
442 .ivsize = DES3_EDE_BLOCK_SIZE,
443 .chunksize = DES3_EDE_BLOCK_SIZE,
444 .setkey = des3_ede_x86_setkey_skcipher,
445 .encrypt = ctr_crypt,
446 .decrypt = ctr_crypt,
447 }
448};
449
450static bool is_blacklisted_cpu(void)
451{
452 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
453 return false;
454
455 if (boot_cpu_data.x86 == 0x0f) {
456 /*
457 * On Pentium 4, des3_ede-x86_64 is slower than generic C
458 * implementation because use of 64bit rotates (which are really
459 * slow on P4). Therefore blacklist P4s.
460 */
461 return true;
462 }
463
464 return false;
465}
466
467static int force;
468module_param(force, int, 0);
469MODULE_PARM_DESC(force, "Force module load, ignore CPU blacklist");
470
471static int __init des3_ede_x86_init(void)
472{
473 int err;
474
475 if (!force && is_blacklisted_cpu()) {
476 pr_info("des3_ede-x86_64: performance on this CPU would be suboptimal: disabling des3_ede-x86_64.\n");
477 return -ENODEV;
478 }
479
480 err = crypto_register_alg(&des3_ede_cipher);
481 if (err)
482 return err;
483
484 err = crypto_register_skciphers(des3_ede_skciphers,
485 ARRAY_SIZE(des3_ede_skciphers));
486 if (err)
487 crypto_unregister_alg(&des3_ede_cipher);
488
489 return err;
490}
491
492static void __exit des3_ede_x86_fini(void)
493{
494 crypto_unregister_alg(&des3_ede_cipher);
495 crypto_unregister_skciphers(des3_ede_skciphers,
496 ARRAY_SIZE(des3_ede_skciphers));
497}
498
499module_init(des3_ede_x86_init);
500module_exit(des3_ede_x86_fini);
501
502MODULE_LICENSE("GPL");
503MODULE_DESCRIPTION("Triple DES EDE Cipher Algorithm, asm optimized");
504MODULE_ALIAS_CRYPTO("des3_ede");
505MODULE_ALIAS_CRYPTO("des3_ede-asm");
506MODULE_AUTHOR("Jussi Kivilinna <jussi.kivilinna@iki.fi>");