xref: /linux/arch/x86/crypto/twofish_glue.c (revision 454cb97726fe62a04b187a0d631ec0a69f6b713a)
115e7b445SSebastian Siewior /*
215e7b445SSebastian Siewior  * Glue Code for assembler optimized version of TWOFISH
315e7b445SSebastian Siewior  *
415e7b445SSebastian Siewior  * Originally Twofish for GPG
515e7b445SSebastian Siewior  * By Matthew Skala <mskala@ansuz.sooke.bc.ca>, July 26, 1998
615e7b445SSebastian Siewior  * 256-bit key length added March 20, 1999
715e7b445SSebastian Siewior  * Some modifications to reduce the text size by Werner Koch, April, 1998
815e7b445SSebastian Siewior  * Ported to the kerneli patch by Marc Mutz <Marc@Mutz.com>
915e7b445SSebastian Siewior  * Ported to CryptoAPI by Colin Slater <hoho@tacomeat.net>
1015e7b445SSebastian Siewior  *
1115e7b445SSebastian Siewior  * The original author has disclaimed all copyright interest in this
1215e7b445SSebastian Siewior  * code and thus put it in the public domain. The subsequent authors
1315e7b445SSebastian Siewior  * have put this under the GNU General Public License.
1415e7b445SSebastian Siewior  *
1515e7b445SSebastian Siewior  * This program is free software; you can redistribute it and/or modify
1615e7b445SSebastian Siewior  * it under the terms of the GNU General Public License as published by
1715e7b445SSebastian Siewior  * the Free Software Foundation; either version 2 of the License, or
1815e7b445SSebastian Siewior  * (at your option) any later version.
1915e7b445SSebastian Siewior  *
2015e7b445SSebastian Siewior  * This program is distributed in the hope that it will be useful,
2115e7b445SSebastian Siewior  * but WITHOUT ANY WARRANTY; without even the implied warranty of
2215e7b445SSebastian Siewior  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2315e7b445SSebastian Siewior  * GNU General Public License for more details.
2415e7b445SSebastian Siewior  *
2515e7b445SSebastian Siewior  * You should have received a copy of the GNU General Public License
2615e7b445SSebastian Siewior  * along with this program; if not, write to the Free Software
2715e7b445SSebastian Siewior  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
2815e7b445SSebastian Siewior  * USA
2915e7b445SSebastian Siewior  *
3015e7b445SSebastian Siewior  * This code is a "clean room" implementation, written from the paper
3115e7b445SSebastian Siewior  * _Twofish: A 128-Bit Block Cipher_ by Bruce Schneier, John Kelsey,
3215e7b445SSebastian Siewior  * Doug Whiting, David Wagner, Chris Hall, and Niels Ferguson, available
3315e7b445SSebastian Siewior  * through http://www.counterpane.com/twofish.html
3415e7b445SSebastian Siewior  *
3515e7b445SSebastian Siewior  * For background information on multiplication in finite fields, used for
3615e7b445SSebastian Siewior  * the matrix operations in the key schedule, see the book _Contemporary
3715e7b445SSebastian Siewior  * Abstract Algebra_ by Joseph A. Gallian, especially chapter 22 in the
3815e7b445SSebastian Siewior  * Third Edition.
3915e7b445SSebastian Siewior  */
4015e7b445SSebastian Siewior 
41*14386d47SHerbert Xu #include <crypto/algapi.h>
4215e7b445SSebastian Siewior #include <crypto/twofish.h>
4315e7b445SSebastian Siewior #include <linux/init.h>
4415e7b445SSebastian Siewior #include <linux/module.h>
4515e7b445SSebastian Siewior #include <linux/types.h>
4615e7b445SSebastian Siewior 
4791d41f15SJussi Kivilinna asmlinkage void twofish_enc_blk(struct twofish_ctx *ctx, u8 *dst,
4891d41f15SJussi Kivilinna 				const u8 *src);
4991d41f15SJussi Kivilinna EXPORT_SYMBOL_GPL(twofish_enc_blk);
5091d41f15SJussi Kivilinna asmlinkage void twofish_dec_blk(struct twofish_ctx *ctx, u8 *dst,
5191d41f15SJussi Kivilinna 				const u8 *src);
5291d41f15SJussi Kivilinna EXPORT_SYMBOL_GPL(twofish_dec_blk);
5315e7b445SSebastian Siewior 
5415e7b445SSebastian Siewior static void twofish_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
5515e7b445SSebastian Siewior {
5691d41f15SJussi Kivilinna 	twofish_enc_blk(crypto_tfm_ctx(tfm), dst, src);
5715e7b445SSebastian Siewior }
5815e7b445SSebastian Siewior 
5915e7b445SSebastian Siewior static void twofish_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
6015e7b445SSebastian Siewior {
6191d41f15SJussi Kivilinna 	twofish_dec_blk(crypto_tfm_ctx(tfm), dst, src);
6215e7b445SSebastian Siewior }
6315e7b445SSebastian Siewior 
6415e7b445SSebastian Siewior static struct crypto_alg alg = {
6515e7b445SSebastian Siewior 	.cra_name		=	"twofish",
6615e7b445SSebastian Siewior 	.cra_driver_name	=	"twofish-asm",
6715e7b445SSebastian Siewior 	.cra_priority		=	200,
6815e7b445SSebastian Siewior 	.cra_flags		=	CRYPTO_ALG_TYPE_CIPHER,
6915e7b445SSebastian Siewior 	.cra_blocksize		=	TF_BLOCK_SIZE,
7015e7b445SSebastian Siewior 	.cra_ctxsize		=	sizeof(struct twofish_ctx),
7115e7b445SSebastian Siewior 	.cra_module		=	THIS_MODULE,
7215e7b445SSebastian Siewior 	.cra_u			=	{
7315e7b445SSebastian Siewior 		.cipher = {
7415e7b445SSebastian Siewior 			.cia_min_keysize	=	TF_MIN_KEY_SIZE,
7515e7b445SSebastian Siewior 			.cia_max_keysize	=	TF_MAX_KEY_SIZE,
7615e7b445SSebastian Siewior 			.cia_setkey		=	twofish_setkey,
7715e7b445SSebastian Siewior 			.cia_encrypt		=	twofish_encrypt,
7815e7b445SSebastian Siewior 			.cia_decrypt		=	twofish_decrypt
7915e7b445SSebastian Siewior 		}
8015e7b445SSebastian Siewior 	}
8115e7b445SSebastian Siewior };
8215e7b445SSebastian Siewior 
83f16a005cSRandy Dunlap static int __init twofish_glue_init(void)
8415e7b445SSebastian Siewior {
8515e7b445SSebastian Siewior 	return crypto_register_alg(&alg);
8615e7b445SSebastian Siewior }
8715e7b445SSebastian Siewior 
88f16a005cSRandy Dunlap static void __exit twofish_glue_fini(void)
8915e7b445SSebastian Siewior {
9015e7b445SSebastian Siewior 	crypto_unregister_alg(&alg);
9115e7b445SSebastian Siewior }
9215e7b445SSebastian Siewior 
93f16a005cSRandy Dunlap module_init(twofish_glue_init);
94f16a005cSRandy Dunlap module_exit(twofish_glue_fini);
9515e7b445SSebastian Siewior 
9615e7b445SSebastian Siewior MODULE_LICENSE("GPL");
9715e7b445SSebastian Siewior MODULE_DESCRIPTION ("Twofish Cipher Algorithm, asm optimized");
985d26a105SKees Cook MODULE_ALIAS_CRYPTO("twofish");
995d26a105SKees Cook MODULE_ALIAS_CRYPTO("twofish-asm");
100