2
* Glue Code for assembler optimized version of Blowfish
4
* Copyright (c) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
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>
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.
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.
21
* You should have received a copy of the GNU General Public License
22
* along with this program; if not, write to the Free Software
23
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28
#include <crypto/blowfish.h>
29
#include <linux/crypto.h>
30
#include <linux/init.h>
31
#include <linux/module.h>
32
#include <linux/types.h>
33
#include <crypto/algapi.h>
35
/* regular block cipher functions */
36
asmlinkage void __blowfish_enc_blk(struct bf_ctx *ctx, u8 *dst, const u8 *src,
38
asmlinkage void blowfish_dec_blk(struct bf_ctx *ctx, u8 *dst, const u8 *src);
40
/* 4-way parallel cipher functions */
41
asmlinkage void __blowfish_enc_blk_4way(struct bf_ctx *ctx, u8 *dst,
42
const u8 *src, bool xor);
43
asmlinkage void blowfish_dec_blk_4way(struct bf_ctx *ctx, u8 *dst,
46
static inline void blowfish_enc_blk(struct bf_ctx *ctx, u8 *dst, const u8 *src)
48
__blowfish_enc_blk(ctx, dst, src, false);
51
static inline void blowfish_enc_blk_xor(struct bf_ctx *ctx, u8 *dst,
54
__blowfish_enc_blk(ctx, dst, src, true);
57
static inline void blowfish_enc_blk_4way(struct bf_ctx *ctx, u8 *dst,
60
__blowfish_enc_blk_4way(ctx, dst, src, false);
63
static inline void blowfish_enc_blk_xor_4way(struct bf_ctx *ctx, u8 *dst,
66
__blowfish_enc_blk_4way(ctx, dst, src, true);
69
static void blowfish_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
71
blowfish_enc_blk(crypto_tfm_ctx(tfm), dst, src);
74
static void blowfish_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
76
blowfish_dec_blk(crypto_tfm_ctx(tfm), dst, src);
79
static struct crypto_alg bf_alg = {
80
.cra_name = "blowfish",
81
.cra_driver_name = "blowfish-asm",
83
.cra_flags = CRYPTO_ALG_TYPE_CIPHER,
84
.cra_blocksize = BF_BLOCK_SIZE,
85
.cra_ctxsize = sizeof(struct bf_ctx),
87
.cra_module = THIS_MODULE,
88
.cra_list = LIST_HEAD_INIT(bf_alg.cra_list),
91
.cia_min_keysize = BF_MIN_KEY_SIZE,
92
.cia_max_keysize = BF_MAX_KEY_SIZE,
93
.cia_setkey = blowfish_setkey,
94
.cia_encrypt = blowfish_encrypt,
95
.cia_decrypt = blowfish_decrypt,
100
static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
101
void (*fn)(struct bf_ctx *, u8 *, const u8 *),
102
void (*fn_4way)(struct bf_ctx *, u8 *, const u8 *))
104
struct bf_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
105
unsigned int bsize = BF_BLOCK_SIZE;
109
err = blkcipher_walk_virt(desc, walk);
111
while ((nbytes = walk->nbytes)) {
112
u8 *wsrc = walk->src.virt.addr;
113
u8 *wdst = walk->dst.virt.addr;
115
/* Process four block batch */
116
if (nbytes >= bsize * 4) {
118
fn_4way(ctx, wdst, wsrc);
123
} while (nbytes >= bsize * 4);
129
/* Handle leftovers */
136
} while (nbytes >= bsize);
139
err = blkcipher_walk_done(desc, walk, nbytes);
145
static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
146
struct scatterlist *src, unsigned int nbytes)
148
struct blkcipher_walk walk;
150
blkcipher_walk_init(&walk, dst, src, nbytes);
151
return ecb_crypt(desc, &walk, blowfish_enc_blk, blowfish_enc_blk_4way);
154
static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
155
struct scatterlist *src, unsigned int nbytes)
157
struct blkcipher_walk walk;
159
blkcipher_walk_init(&walk, dst, src, nbytes);
160
return ecb_crypt(desc, &walk, blowfish_dec_blk, blowfish_dec_blk_4way);
163
static struct crypto_alg blk_ecb_alg = {
164
.cra_name = "ecb(blowfish)",
165
.cra_driver_name = "ecb-blowfish-asm",
167
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
168
.cra_blocksize = BF_BLOCK_SIZE,
169
.cra_ctxsize = sizeof(struct bf_ctx),
171
.cra_type = &crypto_blkcipher_type,
172
.cra_module = THIS_MODULE,
173
.cra_list = LIST_HEAD_INIT(blk_ecb_alg.cra_list),
176
.min_keysize = BF_MIN_KEY_SIZE,
177
.max_keysize = BF_MAX_KEY_SIZE,
178
.setkey = blowfish_setkey,
179
.encrypt = ecb_encrypt,
180
.decrypt = ecb_decrypt,
185
static unsigned int __cbc_encrypt(struct blkcipher_desc *desc,
186
struct blkcipher_walk *walk)
188
struct bf_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
189
unsigned int bsize = BF_BLOCK_SIZE;
190
unsigned int nbytes = walk->nbytes;
191
u64 *src = (u64 *)walk->src.virt.addr;
192
u64 *dst = (u64 *)walk->dst.virt.addr;
193
u64 *iv = (u64 *)walk->iv;
197
blowfish_enc_blk(ctx, (u8 *)dst, (u8 *)dst);
203
} while (nbytes >= bsize);
205
*(u64 *)walk->iv = *iv;
209
static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
210
struct scatterlist *src, unsigned int nbytes)
212
struct blkcipher_walk walk;
215
blkcipher_walk_init(&walk, dst, src, nbytes);
216
err = blkcipher_walk_virt(desc, &walk);
218
while ((nbytes = walk.nbytes)) {
219
nbytes = __cbc_encrypt(desc, &walk);
220
err = blkcipher_walk_done(desc, &walk, nbytes);
226
static unsigned int __cbc_decrypt(struct blkcipher_desc *desc,
227
struct blkcipher_walk *walk)
229
struct bf_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
230
unsigned int bsize = BF_BLOCK_SIZE;
231
unsigned int nbytes = walk->nbytes;
232
u64 *src = (u64 *)walk->src.virt.addr;
233
u64 *dst = (u64 *)walk->dst.virt.addr;
237
/* Start of the last block. */
238
src += nbytes / bsize - 1;
239
dst += nbytes / bsize - 1;
243
/* Process four block batch */
244
if (nbytes >= bsize * 4) {
246
nbytes -= bsize * 4 - bsize;
254
blowfish_dec_blk_4way(ctx, (u8 *)dst, (u8 *)src);
267
} while (nbytes >= bsize * 4);
273
/* Handle leftovers */
275
blowfish_dec_blk(ctx, (u8 *)dst, (u8 *)src);
287
*dst ^= *(u64 *)walk->iv;
288
*(u64 *)walk->iv = last_iv;
293
static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
294
struct scatterlist *src, unsigned int nbytes)
296
struct blkcipher_walk walk;
299
blkcipher_walk_init(&walk, dst, src, nbytes);
300
err = blkcipher_walk_virt(desc, &walk);
302
while ((nbytes = walk.nbytes)) {
303
nbytes = __cbc_decrypt(desc, &walk);
304
err = blkcipher_walk_done(desc, &walk, nbytes);
310
static struct crypto_alg blk_cbc_alg = {
311
.cra_name = "cbc(blowfish)",
312
.cra_driver_name = "cbc-blowfish-asm",
314
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
315
.cra_blocksize = BF_BLOCK_SIZE,
316
.cra_ctxsize = sizeof(struct bf_ctx),
318
.cra_type = &crypto_blkcipher_type,
319
.cra_module = THIS_MODULE,
320
.cra_list = LIST_HEAD_INIT(blk_cbc_alg.cra_list),
323
.min_keysize = BF_MIN_KEY_SIZE,
324
.max_keysize = BF_MAX_KEY_SIZE,
325
.ivsize = BF_BLOCK_SIZE,
326
.setkey = blowfish_setkey,
327
.encrypt = cbc_encrypt,
328
.decrypt = cbc_decrypt,
333
static void ctr_crypt_final(struct bf_ctx *ctx, struct blkcipher_walk *walk)
335
u8 *ctrblk = walk->iv;
336
u8 keystream[BF_BLOCK_SIZE];
337
u8 *src = walk->src.virt.addr;
338
u8 *dst = walk->dst.virt.addr;
339
unsigned int nbytes = walk->nbytes;
341
blowfish_enc_blk(ctx, keystream, ctrblk);
342
crypto_xor(keystream, src, nbytes);
343
memcpy(dst, keystream, nbytes);
345
crypto_inc(ctrblk, BF_BLOCK_SIZE);
348
static unsigned int __ctr_crypt(struct blkcipher_desc *desc,
349
struct blkcipher_walk *walk)
351
struct bf_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
352
unsigned int bsize = BF_BLOCK_SIZE;
353
unsigned int nbytes = walk->nbytes;
354
u64 *src = (u64 *)walk->src.virt.addr;
355
u64 *dst = (u64 *)walk->dst.virt.addr;
356
u64 ctrblk = be64_to_cpu(*(__be64 *)walk->iv);
359
/* Process four block batch */
360
if (nbytes >= bsize * 4) {
369
/* create ctrblks for parallel encrypt */
370
ctrblocks[0] = cpu_to_be64(ctrblk++);
371
ctrblocks[1] = cpu_to_be64(ctrblk++);
372
ctrblocks[2] = cpu_to_be64(ctrblk++);
373
ctrblocks[3] = cpu_to_be64(ctrblk++);
375
blowfish_enc_blk_xor_4way(ctx, (u8 *)dst,
380
} while ((nbytes -= bsize * 4) >= bsize * 4);
386
/* Handle leftovers */
391
ctrblocks[0] = cpu_to_be64(ctrblk++);
393
blowfish_enc_blk_xor(ctx, (u8 *)dst, (u8 *)ctrblocks);
397
} while ((nbytes -= bsize) >= bsize);
400
*(__be64 *)walk->iv = cpu_to_be64(ctrblk);
404
static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
405
struct scatterlist *src, unsigned int nbytes)
407
struct blkcipher_walk walk;
410
blkcipher_walk_init(&walk, dst, src, nbytes);
411
err = blkcipher_walk_virt_block(desc, &walk, BF_BLOCK_SIZE);
413
while ((nbytes = walk.nbytes) >= BF_BLOCK_SIZE) {
414
nbytes = __ctr_crypt(desc, &walk);
415
err = blkcipher_walk_done(desc, &walk, nbytes);
419
ctr_crypt_final(crypto_blkcipher_ctx(desc->tfm), &walk);
420
err = blkcipher_walk_done(desc, &walk, 0);
426
static struct crypto_alg blk_ctr_alg = {
427
.cra_name = "ctr(blowfish)",
428
.cra_driver_name = "ctr-blowfish-asm",
430
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
432
.cra_ctxsize = sizeof(struct bf_ctx),
434
.cra_type = &crypto_blkcipher_type,
435
.cra_module = THIS_MODULE,
436
.cra_list = LIST_HEAD_INIT(blk_ctr_alg.cra_list),
439
.min_keysize = BF_MIN_KEY_SIZE,
440
.max_keysize = BF_MAX_KEY_SIZE,
441
.ivsize = BF_BLOCK_SIZE,
442
.setkey = blowfish_setkey,
443
.encrypt = ctr_crypt,
444
.decrypt = ctr_crypt,
449
static int __init init(void)
453
err = crypto_register_alg(&bf_alg);
456
err = crypto_register_alg(&blk_ecb_alg);
459
err = crypto_register_alg(&blk_cbc_alg);
462
err = crypto_register_alg(&blk_ctr_alg);
469
crypto_unregister_alg(&blk_cbc_alg);
471
crypto_unregister_alg(&blk_ecb_alg);
473
crypto_unregister_alg(&bf_alg);
478
static void __exit fini(void)
480
crypto_unregister_alg(&blk_ctr_alg);
481
crypto_unregister_alg(&blk_cbc_alg);
482
crypto_unregister_alg(&blk_ecb_alg);
483
crypto_unregister_alg(&bf_alg);
489
MODULE_LICENSE("GPL");
490
MODULE_DESCRIPTION("Blowfish Cipher Algorithm, asm optimized");
491
MODULE_ALIAS("blowfish");
492
MODULE_ALIAS("blowfish-asm");