162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Glue Code for assembler optimized version of TWOFISH
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Originally Twofish for GPG
562306a36Sopenharmony_ci * By Matthew Skala <mskala@ansuz.sooke.bc.ca>, July 26, 1998
662306a36Sopenharmony_ci * 256-bit key length added March 20, 1999
762306a36Sopenharmony_ci * Some modifications to reduce the text size by Werner Koch, April, 1998
862306a36Sopenharmony_ci * Ported to the kerneli patch by Marc Mutz <Marc@Mutz.com>
962306a36Sopenharmony_ci * Ported to CryptoAPI by Colin Slater <hoho@tacomeat.net>
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The original author has disclaimed all copyright interest in this
1262306a36Sopenharmony_ci * code and thus put it in the public domain. The subsequent authors
1362306a36Sopenharmony_ci * have put this under the GNU General Public License.
1462306a36Sopenharmony_ci *
1562306a36Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
1662306a36Sopenharmony_ci * it under the terms of the GNU General Public License as published by
1762306a36Sopenharmony_ci * the Free Software Foundation; either version 2 of the License, or
1862306a36Sopenharmony_ci * (at your option) any later version.
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci * This program is distributed in the hope that it will be useful,
2162306a36Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
2262306a36Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2362306a36Sopenharmony_ci * GNU General Public License for more details.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * You should have received a copy of the GNU General Public License
2662306a36Sopenharmony_ci * along with this program; if not, write to the Free Software
2762306a36Sopenharmony_ci * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
2862306a36Sopenharmony_ci * USA
2962306a36Sopenharmony_ci *
3062306a36Sopenharmony_ci * This code is a "clean room" implementation, written from the paper
3162306a36Sopenharmony_ci * _Twofish: A 128-Bit Block Cipher_ by Bruce Schneier, John Kelsey,
3262306a36Sopenharmony_ci * Doug Whiting, David Wagner, Chris Hall, and Niels Ferguson, available
3362306a36Sopenharmony_ci * through http://www.counterpane.com/twofish.html
3462306a36Sopenharmony_ci *
3562306a36Sopenharmony_ci * For background information on multiplication in finite fields, used for
3662306a36Sopenharmony_ci * the matrix operations in the key schedule, see the book _Contemporary
3762306a36Sopenharmony_ci * Abstract Algebra_ by Joseph A. Gallian, especially chapter 22 in the
3862306a36Sopenharmony_ci * Third Edition.
3962306a36Sopenharmony_ci */
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#include <crypto/algapi.h>
4262306a36Sopenharmony_ci#include <crypto/twofish.h>
4362306a36Sopenharmony_ci#include <linux/init.h>
4462306a36Sopenharmony_ci#include <linux/module.h>
4562306a36Sopenharmony_ci#include <linux/types.h>
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciasmlinkage void twofish_enc_blk(struct twofish_ctx *ctx, u8 *dst,
4862306a36Sopenharmony_ci				const u8 *src);
4962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(twofish_enc_blk);
5062306a36Sopenharmony_ciasmlinkage void twofish_dec_blk(struct twofish_ctx *ctx, u8 *dst,
5162306a36Sopenharmony_ci				const u8 *src);
5262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(twofish_dec_blk);
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_cistatic void twofish_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
5562306a36Sopenharmony_ci{
5662306a36Sopenharmony_ci	twofish_enc_blk(crypto_tfm_ctx(tfm), dst, src);
5762306a36Sopenharmony_ci}
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_cistatic void twofish_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
6062306a36Sopenharmony_ci{
6162306a36Sopenharmony_ci	twofish_dec_blk(crypto_tfm_ctx(tfm), dst, src);
6262306a36Sopenharmony_ci}
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_cistatic struct crypto_alg alg = {
6562306a36Sopenharmony_ci	.cra_name		=	"twofish",
6662306a36Sopenharmony_ci	.cra_driver_name	=	"twofish-asm",
6762306a36Sopenharmony_ci	.cra_priority		=	200,
6862306a36Sopenharmony_ci	.cra_flags		=	CRYPTO_ALG_TYPE_CIPHER,
6962306a36Sopenharmony_ci	.cra_blocksize		=	TF_BLOCK_SIZE,
7062306a36Sopenharmony_ci	.cra_ctxsize		=	sizeof(struct twofish_ctx),
7162306a36Sopenharmony_ci	.cra_alignmask		=	0,
7262306a36Sopenharmony_ci	.cra_module		=	THIS_MODULE,
7362306a36Sopenharmony_ci	.cra_u			=	{
7462306a36Sopenharmony_ci		.cipher = {
7562306a36Sopenharmony_ci			.cia_min_keysize	=	TF_MIN_KEY_SIZE,
7662306a36Sopenharmony_ci			.cia_max_keysize	=	TF_MAX_KEY_SIZE,
7762306a36Sopenharmony_ci			.cia_setkey		=	twofish_setkey,
7862306a36Sopenharmony_ci			.cia_encrypt		=	twofish_encrypt,
7962306a36Sopenharmony_ci			.cia_decrypt		=	twofish_decrypt
8062306a36Sopenharmony_ci		}
8162306a36Sopenharmony_ci	}
8262306a36Sopenharmony_ci};
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_cistatic int __init twofish_glue_init(void)
8562306a36Sopenharmony_ci{
8662306a36Sopenharmony_ci	return crypto_register_alg(&alg);
8762306a36Sopenharmony_ci}
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_cistatic void __exit twofish_glue_fini(void)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	crypto_unregister_alg(&alg);
9262306a36Sopenharmony_ci}
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_cimodule_init(twofish_glue_init);
9562306a36Sopenharmony_cimodule_exit(twofish_glue_fini);
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ciMODULE_LICENSE("GPL");
9862306a36Sopenharmony_ciMODULE_DESCRIPTION ("Twofish Cipher Algorithm, asm optimized");
9962306a36Sopenharmony_ciMODULE_ALIAS_CRYPTO("twofish");
10062306a36Sopenharmony_ciMODULE_ALIAS_CRYPTO("twofish-asm");
101