// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Common Twofish algorithm parts shared between the c and assembler
* implementations
*
* Originally Twofish for GPG
* By Matthew Skala < mskala @ ansuz . sooke . bc . ca > , July 26 , 1998
* 256 - bit key length added March 20 , 1999
* Some modifications to reduce the text size by Werner Koch , April , 1998
* Ported to the kerneli patch by Marc Mutz < Marc @ Mutz . com >
* Ported to CryptoAPI by Colin Slater < hoho @ tacomeat . net >
*
* The original author has disclaimed all copyright interest in this
* code and thus put it in the public domain . The subsequent authors
* have put this under the GNU General Public License .
*
* This code is a " clean room " implementation , written from the paper
* _ Twofish : A 128 - Bit Block Cipher_ by Bruce Schneier , John Kelsey ,
* Doug Whiting , David Wagner , Chris Hall , and Niels Ferguson , available
* through http : //www.counterpane.com/twofish.html
*
* For background information on multiplication in finite fields , used for
* the matrix operations in the key schedule , see the book _ Contemporary
* Abstract Algebra_ by Joseph A . Gallian , especially chapter 22 in the
* Third Edition .
*/
#include <crypto/algapi.h>
#include <crypto/twofish.h>
#include <linux/bitops.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/types.h>
/* The large precomputed tables for the Twofish cipher (twofish.c)
* Taken from the same source as twofish . c
* Marc Mutz < Marc @ Mutz . com >
*/
/* These two tables are the q0 and q1 permutations, exactly as described in
* the Twofish paper. */
static const u8 q0[256 ] = {
0 xA9, 0 x67, 0 xB3, 0 xE8, 0 x04, 0 xFD, 0 xA3, 0 x76, 0 x9A, 0 x92, 0 x80, 0 x78,
0 xE4, 0 xDD, 0 xD1, 0 x38, 0 x0D, 0 xC6, 0 x35, 0 x98, 0 x18, 0 xF7, 0 xEC, 0 x6C,
0 x43, 0 x75, 0 x37, 0 x26, 0 xFA, 0 x13, 0 x94, 0 x48, 0 xF2, 0 xD0, 0 x8B, 0 x30,
0 x84, 0 x54, 0 xDF, 0 x23, 0 x19, 0 x5B, 0 x3D, 0 x59, 0 xF3, 0 xAE, 0 xA2, 0 x82,
0 x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xBE, 0 x91
};
/* These MDS tables are actually tables of MDS composed with q0 and q1,
* because it is only ever used that way and we can save some time by
* precomputing . Of course the main saving comes from precomputing the
* GF ( 2 ^ 8 ) multiplication involved in the MDS matrix multiply ; by looking
* things up in these tables we reduce the matrix multiply to four lookups
* and three XORs . Semi - formally , the definition of these tables is :
* mds [ 0 ] [ i ] = MDS ( q1 [ i ] 0 0 0 ) ^ T mds [ 1 ] [ i ] = MDS ( 0 q0 [ i ] 0 0 ) ^ T
* mds [ 2 ] [ i ] = MDS ( 0 0 q1 [ i ] 0 ) ^ T mds [ 3 ] [ i ] = MDS ( 0 0 0 q0 [ i ] ) ^ T
* where ^ T means " transpose " , the matrix multiply is performed in GF ( 2 ^ 8 )
* represented as GF ( 2 ) [ x ] / v ( x ) where v ( x ) = x ^ 8 + x ^ 6 + x ^ 5 + x ^ 3 + 1 as described
* by Schneier et al , and I ' m casually glossing over the byte / word
* conversion issues. */
static const u32 mds[4 ][256 ] = {
{
0 xBCBC3275, 0 xECEC21F3, 0 x202043C6, 0 xB3B3C9F4, 0 xDADA03DB, 0 x02028B7B,
0 xE2E22BFB, 0 x9E9EFAC8, 0 xC9C9EC4A, 0 xD4D409D3, 0 x18186BE6, 0 x1E1E9F6B,
0 x98980E45, 0 xB2B2387D, 0 xA6A6D2E8, 0 x2626B74B, 0 x3C3C57D6, 0 x93938A32,
0 x8282EED8, 0 x525298FD, 0 x7B7BD437, 0 xBBBB3771, 0 x5B5B97F1, 0 x474783E1,
0 x24243C30, 0 x5151E20F, 0 xBABAC6F8, 0 x4A4AF31B, 0 xBFBF4887, 0 x0D0D70FA,
0 xB0B0B306, 0 x7575DE3F, 0 xD2D2FD5E, 0 x7D7D20BA, 0 x666631AE, 0 x3A3AA35B,
0 x59591C8A, 0 x00000000, 0 xCDCD93BC, 0 x1A1AE09D, 0 xAEAE2C6D, 0 x7F7FABC1,
0 x2B2BC7B1, 0 xBEBEB90E, 0 xE0E0A080, 0 x8A8A105D, 0 x3B3B52D2, 0 x6464BAD5,
0 xD8D888A0, 0 xE7E7A584, 0 x5F5FE807, 0 x1B1B1114, 0 x2C2CC2B5, 0 xFCFCB490,
0 x3131272C, 0 x808065A3, 0 x73732AB2, 0 x0C0C8173, 0 x79795F4C, 0 x6B6B4154,
0 x4B4B0292, 0 x53536974, 0 x94948F36, 0 x83831F51, 0 x2A2A3638, 0 xC4C49CB0,
0 x2222C8BD, 0 xD5D5F85A, 0 xBDBDC3FC, 0 x48487860, 0 xFFFFCE62, 0 x4C4C0796,
0 x4141776C, 0 xC7C7E642, 0 xEBEB24F7, 0 x1C1C1410, 0 x5D5D637C, 0 x36362228,
0 x6767C027, 0 xE9E9AF8C, 0 x4444F913, 0 x1414EA95, 0 xF5F5BB9C, 0 xCFCF18C7,
0 x3F3F2D24, 0 xC0C0E346, 0 x7272DB3B, 0 x54546C70, 0 x29294CCA, 0 xF0F035E3,
0 x0808FE85, 0 xC6C617CB, 0 xF3F34F11, 0 x8C8CE4D0, 0 xA4A45993, 0 xCACA96B8,
0 x68683BA6, 0 xB8B84D83, 0 x38382820, 0 xE5E52EFF, 0 xADAD569F, 0 x0B0B8477,
0 xC8C81DC3, 0 x9999FFCC, 0 x5858ED03, 0 x19199A6F, 0 x0E0E0A08, 0 x95957EBF,
0 x70705040, 0 xF7F730E7, 0 x6E6ECF2B, 0 x1F1F6EE2, 0 xB5B53D79, 0 x09090F0C,
0 x616134AA, 0 x57571682, 0 x9F9F0B41, 0 x9D9D803A, 0 x111164EA, 0 x2525CDB9,
0 xAFAFDDE4, 0 x4545089A, 0 xDFDF8DA4, 0 xA3A35C97, 0 xEAEAD57E, 0 x353558DA,
0 xEDEDD07A, 0 x4343FC17, 0 xF8F8CB66, 0 xFBFBB194, 0 x3737D3A1, 0 xFAFA401D,
0 xC2C2683D, 0 xB4B4CCF0, 0 x32325DDE, 0 x9C9C71B3, 0 x5656E70B, 0 xE3E3DA72,
0 x878760A7, 0 x15151B1C, 0 xF9F93AEF, 0 x6363BFD1, 0 x3434A953, 0 x9A9A853E,
0 xB1B1428F, 0 x7C7CD133, 0 x88889B26, 0 x3D3DA65F, 0 xA1A1D7EC, 0 xE4E4DF76,
0 x8181942A, 0 x91910149, 0 x0F0FFB81, 0 xEEEEAA88, 0 x161661EE, 0 xD7D77321,
0 x9797F5C4, 0 xA5A5A81A, 0 xFEFE3FEB, 0 x6D6DB5D9, 0 x7878AEC5, 0 xC5C56D39,
0 x1D1DE599, 0 x7676A4CD, 0 x3E3EDCAD, 0 xCBCB6731, 0 xB6B6478B, 0 xEFEF5B01,
0 x12121E18, 0 x6060C523, 0 x6A6AB0DD, 0 x4D4DF61F, 0 xCECEE94E, 0 xDEDE7C2D,
0 x55559DF9, 0 x7E7E5A48, 0 x2121B24F, 0 x03037AF2, 0 xA0A02665, 0 x5E5E198E,
0 x5A5A6678, 0 x65654B5C, 0 x62624E58, 0 xFDFD4519, 0 x0606F48D, 0 x404086E5,
0 xF2F2BE98, 0 x3333AC57, 0 x17179067, 0 x05058E7F, 0 xE8E85E05, 0 x4F4F7D64,
0 x89896AAF, 0 x10109563, 0 x74742FB6, 0 x0A0A75FE, 0 x5C5C92F5, 0 x9B9B74B7,
0 x2D2D333C, 0 x3030D6A5, 0 x2E2E49CE, 0 x494989E9, 0 x46467268, 0 x77775544,
0 xA8A8D8E0, 0 x9696044D, 0 x2828BD43, 0 xA9A92969, 0 xD9D97929, 0 x8686912E,
0 xD1D187AC, 0 xF4F44A15, 0 x8D8D1559, 0 xD6D682A8, 0 xB9B9BC0A, 0 x42420D9E,
0 xF6F6C16E, 0 x2F2FB847, 0 xDDDD06DF, 0 x23233934, 0 xCCCC6235, 0 xF1F1C46A,
0 xC1C112CF, 0 x8585EBDC, 0 x8F8F9E22, 0 x7171A1C9, 0 x9090F0C0, 0 xAAAA539B,
0 x0101F189, 0 x8B8BE1D4, 0 x4E4E8CED, 0 x8E8E6FAB, 0 xABABA212, 0 x6F6F3EA2,
0 xE6E6540D, 0 xDBDBF252, 0 x92927BBB, 0 xB7B7B602, 0 x6969CA2F, 0 x3939D9A9,
0 xD3D30CD7, 0 xA7A72361, 0 xA2A2AD1E, 0 xC3C399B4, 0 x6C6C4450, 0 x07070504,
0 x04047FF6, 0 x272746C2, 0 xACACA716, 0 xD0D07625, 0 x50501386, 0 xDCDCF756,
0 x84841A55, 0 xE1E15109, 0 x7A7A25BE, 0 x1313EF91},
{
0 xA9D93939, 0 x67901717, 0 xB3719C9C, 0 xE8D2A6A6, 0 x04050707, 0 xFD985252,
0 xA3658080, 0 x76DFE4E4, 0 x9A084545, 0 x92024B4B, 0 x80A0E0E0, 0 x78665A5A,
0 xE4DDAFAF, 0 xDDB06A6A, 0 xD1BF6363, 0 x38362A2A, 0 x0D54E6E6, 0 xC6432020,
0 x3562CCCC, 0 x98BEF2F2, 0 x181E1212, 0 xF724EBEB, 0 xECD7A1A1, 0 x6C774141,
0 x43BD2828, 0 x7532BCBC, 0 x37D47B7B, 0 x269B8888, 0 xFA700D0D, 0 x13F94444,
0 x94B1FBFB, 0 x485A7E7E, 0 xF27A0303, 0 xD0E48C8C, 0 x8B47B6B6, 0 x303C2424,
0 x84A5E7E7, 0 x54416B6B, 0 xDF06DDDD, 0 x23C56060, 0 x1945FDFD, 0 x5BA33A3A,
0 x3D68C2C2, 0 x59158D8D, 0 xF321ECEC, 0 xAE316666, 0 xA23E6F6F, 0 x82165757,
0 x63951010, 0 x015BEFEF, 0 x834DB8B8, 0 x2E918686, 0 xD9B56D6D, 0 x511F8383,
0 x9B53AAAA, 0 x7C635D5D, 0 xA63B6868, 0 xEB3FFEFE, 0 xA5D63030, 0 xBE257A7A,
0 x16A7ACAC, 0 x0C0F0909, 0 xE335F0F0, 0 x6123A7A7, 0 xC0F09090, 0 x8CAFE9E9,
0 x3A809D9D, 0 xF5925C5C, 0 x73810C0C, 0 x2C273131, 0 x2576D0D0, 0 x0BE75656,
0 xBB7B9292, 0 x4EE9CECE, 0 x89F10101, 0 x6B9F1E1E, 0 x53A93434, 0 x6AC4F1F1,
0 xB499C3C3, 0 xF1975B5B, 0 xE1834747, 0 xE66B1818, 0 xBDC82222, 0 x450E9898,
0 xE26E1F1F, 0 xF4C9B3B3, 0 xB62F7474, 0 x66CBF8F8, 0 xCCFF9999, 0 x95EA1414,
0 x03ED5858, 0 x56F7DCDC, 0 xD4E18B8B, 0 x1C1B1515, 0 x1EADA2A2, 0 xD70CD3D3,
0 xFB2BE2E2, 0 xC31DC8C8, 0 x8E195E5E, 0 xB5C22C2C, 0 xE9894949, 0 xCF12C1C1,
0 xBF7E9595, 0 xBA207D7D, 0 xEA641111, 0 x77840B0B, 0 x396DC5C5, 0 xAF6A8989,
0 x33D17C7C, 0 xC9A17171, 0 x62CEFFFF, 0 x7137BBBB, 0 x81FB0F0F, 0 x793DB5B5,
0 x0951E1E1, 0 xADDC3E3E, 0 x242D3F3F, 0 xCDA47676, 0 xF99D5555, 0 xD8EE8282,
0 xE5864040, 0 xC5AE7878, 0 xB9CD2525, 0 x4D049696, 0 x44557777, 0 x080A0E0E,
0 x86135050, 0 xE730F7F7, 0 xA1D33737, 0 x1D40FAFA, 0 xAA346161, 0 xED8C4E4E,
0 x06B3B0B0, 0 x706C5454, 0 xB22A7373, 0 xD2523B3B, 0 x410B9F9F, 0 x7B8B0202,
0 xA088D8D8, 0 x114FF3F3, 0 x3167CBCB, 0 xC2462727, 0 x27C06767, 0 x90B4FCFC,
0 x20283838, 0 xF67F0404, 0 x60784848, 0 xFF2EE5E5, 0 x96074C4C, 0 x5C4B6565,
0 xB1C72B2B, 0 xAB6F8E8E, 0 x9E0D4242, 0 x9CBBF5F5, 0 x52F2DBDB, 0 x1BF34A4A,
0 x5FA63D3D, 0 x9359A4A4, 0 x0ABCB9B9, 0 xEF3AF9F9, 0 x91EF1313, 0 x85FE0808,
0 x49019191, 0 xEE611616, 0 x2D7CDEDE, 0 x4FB22121, 0 x8F42B1B1, 0 x3BDB7272,
0 x47B82F2F, 0 x8748BFBF, 0 x6D2CAEAE, 0 x46E3C0C0, 0 xD6573C3C, 0 x3E859A9A,
0 x6929A9A9, 0 x647D4F4F, 0 x2A948181, 0 xCE492E2E, 0 xCB17C6C6, 0 x2FCA6969,
0 xFCC3BDBD, 0 x975CA3A3, 0 x055EE8E8, 0 x7AD0EDED, 0 xAC87D1D1, 0 x7F8E0505,
0 xD5BA6464, 0 x1AA8A5A5, 0 x4BB72626, 0 x0EB9BEBE, 0 xA7608787, 0 x5AF8D5D5,
0 x28223636, 0 x14111B1B, 0 x3FDE7575, 0 x2979D9D9, 0 x88AAEEEE, 0 x3C332D2D,
0 x4C5F7979, 0 x02B6B7B7, 0 xB896CACA, 0 xDA583535, 0 xB09CC4C4, 0 x17FC4343,
0 x551A8484, 0 x1FF64D4D, 0 x8A1C5959, 0 x7D38B2B2, 0 x57AC3333, 0 xC718CFCF,
0 x8DF40606, 0 x74695353, 0 xB7749B9B, 0 xC4F59797, 0 x9F56ADAD, 0 x72DAE3E3,
0 x7ED5EAEA, 0 x154AF4F4, 0 x229E8F8F, 0 x12A2ABAB, 0 x584E6262, 0 x07E85F5F,
0 x99E51D1D, 0 x34392323, 0 x6EC1F6F6, 0 x50446C6C, 0 xDE5D3232, 0 x68724646,
0 x6526A0A0, 0 xBC93CDCD, 0 xDB03DADA, 0 xF8C6BABA, 0 xC8FA9E9E, 0 xA882D6D6,
0 x2BCF6E6E, 0 x40507070, 0 xDCEB8585, 0 xFE750A0A, 0 x328A9393, 0 xA48DDFDF,
0 xCA4C2929, 0 x10141C1C, 0 x2173D7D7, 0 xF0CCB4B4, 0 xD309D4D4, 0 x5D108A8A,
0 x0FE25151, 0 x00000000, 0 x6F9A1919, 0 x9DE01A1A, 0 x368F9494, 0 x42E6C7C7,
0 x4AECC9C9, 0 x5EFDD2D2, 0 xC1AB7F7F, 0 xE0D8A8A8},
{
0 xBC75BC32, 0 xECF3EC21, 0 x20C62043, 0 xB3F4B3C9, 0 xDADBDA03, 0 x027B028B,
0 xE2FBE22B, 0 x9EC89EFA, 0 xC94AC9EC, 0 xD4D3D409, 0 x18E6186B, 0 x1E6B1E9F,
0 x9845980E, 0 xB27DB238, 0 xA6E8A6D2, 0 x264B26B7, 0 x3CD63C57, 0 x9332938A,
0 x82D882EE, 0 x52FD5298, 0 x7B377BD4, 0 xBB71BB37, 0 x5BF15B97, 0 x47E14783,
0 x2430243C, 0 x510F51E2, 0 xBAF8BAC6, 0 x4A1B4AF3, 0 xBF87BF48, 0 x0DFA0D70,
0 xB006B0B3, 0 x753F75DE, 0 xD25ED2FD, 0 x7DBA7D20, 0 x66AE6631, 0 x3A5B3AA3,
0 x598A591C, 0 x00000000, 0 xCDBCCD93, 0 x1A9D1AE0, 0 xAE6DAE2C, 0 x7FC17FAB,
0 x2BB12BC7, 0 xBE0EBEB9, 0 xE080E0A0, 0 x8A5D8A10, 0 x3BD23B52, 0 x64D564BA,
0 xD8A0D888, 0 xE784E7A5, 0 x5F075FE8, 0 x1B141B11, 0 x2CB52CC2, 0 xFC90FCB4,
0 x312C3127, 0 x80A38065, 0 x73B2732A, 0 x0C730C81, 0 x794C795F, 0 x6B546B41,
0 x4B924B02, 0 x53745369, 0 x9436948F, 0 x8351831F, 0 x2A382A36, 0 xC4B0C49C,
0 x22BD22C8, 0 xD55AD5F8, 0 xBDFCBDC3, 0 x48604878, 0 xFF62FFCE, 0 x4C964C07,
0 x416C4177, 0 xC742C7E6, 0 xEBF7EB24, 0 x1C101C14, 0 x5D7C5D63, 0 x36283622,
0 x672767C0, 0 xE98CE9AF, 0 x441344F9, 0 x149514EA, 0 xF59CF5BB, 0 xCFC7CF18,
0 x3F243F2D, 0 xC046C0E3, 0 x723B72DB, 0 x5470546C, 0 x29CA294C, 0 xF0E3F035,
0 x088508FE, 0 xC6CBC617, 0 xF311F34F, 0 x8CD08CE4, 0 xA493A459, 0 xCAB8CA96,
0 x68A6683B, 0 xB883B84D, 0 x38203828, 0 xE5FFE52E, 0 xAD9FAD56, 0 x0B770B84,
0 xC8C3C81D, 0 x99CC99FF, 0 x580358ED, 0 x196F199A, 0 x0E080E0A, 0 x95BF957E,
0 x70407050, 0 xF7E7F730, 0 x6E2B6ECF, 0 x1FE21F6E, 0 xB579B53D, 0 x090C090F,
0 x61AA6134, 0 x57825716, 0 x9F419F0B, 0 x9D3A9D80, 0 x11EA1164, 0 x25B925CD,
0 xAFE4AFDD, 0 x459A4508, 0 xDFA4DF8D, 0 xA397A35C, 0 xEA7EEAD5, 0 x35DA3558,
0 xED7AEDD0, 0 x431743FC, 0 xF866F8CB, 0 xFB94FBB1, 0 x37A137D3, 0 xFA1DFA40,
0 xC23DC268, 0 xB4F0B4CC, 0 x32DE325D, 0 x9CB39C71, 0 x560B56E7, 0 xE372E3DA,
0 x87A78760, 0 x151C151B, 0 xF9EFF93A, 0 x63D163BF, 0 x345334A9, 0 x9A3E9A85,
0 xB18FB142, 0 x7C337CD1, 0 x8826889B, 0 x3D5F3DA6, 0 xA1ECA1D7, 0 xE476E4DF,
0 x812A8194, 0 x91499101, 0 x0F810FFB, 0 xEE88EEAA, 0 x16EE1661, 0 xD721D773,
0 x97C497F5, 0 xA51AA5A8, 0 xFEEBFE3F, 0 x6DD96DB5, 0 x78C578AE, 0 xC539C56D,
0 x1D991DE5, 0 x76CD76A4, 0 x3EAD3EDC, 0 xCB31CB67, 0 xB68BB647, 0 xEF01EF5B,
0 x1218121E, 0 x602360C5, 0 x6ADD6AB0, 0 x4D1F4DF6, 0 xCE4ECEE9, 0 xDE2DDE7C,
0 x55F9559D, 0 x7E487E5A, 0 x214F21B2, 0 x03F2037A, 0 xA065A026, 0 x5E8E5E19,
0 x5A785A66, 0 x655C654B, 0 x6258624E, 0 xFD19FD45, 0 x068D06F4, 0 x40E54086,
0 xF298F2BE, 0 x335733AC, 0 x17671790, 0 x057F058E, 0 xE805E85E, 0 x4F644F7D,
0 x89AF896A, 0 x10631095, 0 x74B6742F, 0 x0AFE0A75, 0 x5CF55C92, 0 x9BB79B74,
0 x2D3C2D33, 0 x30A530D6, 0 x2ECE2E49, 0 x49E94989, 0 x46684672, 0 x77447755,
0 xA8E0A8D8, 0 x964D9604, 0 x284328BD, 0 xA969A929, 0 xD929D979, 0 x862E8691,
0 xD1ACD187, 0 xF415F44A, 0 x8D598D15, 0 xD6A8D682, 0 xB90AB9BC, 0 x429E420D,
0 xF66EF6C1, 0 x2F472FB8, 0 xDDDFDD06, 0 x23342339, 0 xCC35CC62, 0 xF16AF1C4,
0 xC1CFC112, 0 x85DC85EB, 0 x8F228F9E, 0 x71C971A1, 0 x90C090F0, 0 xAA9BAA53,
0 x018901F1, 0 x8BD48BE1, 0 x4EED4E8C, 0 x8EAB8E6F, 0 xAB12ABA2, 0 x6FA26F3E,
0 xE60DE654, 0 xDB52DBF2, 0 x92BB927B, 0 xB702B7B6, 0 x692F69CA, 0 x39A939D9,
0 xD3D7D30C, 0 xA761A723, 0 xA21EA2AD, 0 xC3B4C399, 0 x6C506C44, 0 x07040705,
0 x04F6047F, 0 x27C22746, 0 xAC16ACA7, 0 xD025D076, 0 x50865013, 0 xDC56DCF7,
0 x8455841A, 0 xE109E151, 0 x7ABE7A25, 0 x139113EF},
{
0 xD939A9D9, 0 x90176790, 0 x719CB371, 0 xD2A6E8D2, 0 x05070405, 0 x9852FD98,
0 x6580A365, 0 xDFE476DF, 0 x08459A08, 0 x024B9202, 0 xA0E080A0, 0 x665A7866,
0 xDDAFE4DD, 0 xB06ADDB0, 0 xBF63D1BF, 0 x362A3836, 0 x54E60D54, 0 x4320C643,
0 x62CC3562, 0 xBEF298BE, 0 x1E12181E, 0 x24EBF724, 0 xD7A1ECD7, 0 x77416C77,
0 xBD2843BD, 0 x32BC7532, 0 xD47B37D4, 0 x9B88269B, 0 x700DFA70, 0 xF94413F9,
0 xB1FB94B1, 0 x5A7E485A, 0 x7A03F27A, 0 xE48CD0E4, 0 x47B68B47, 0 x3C24303C,
0 xA5E784A5, 0 x416B5441, 0 x06DDDF06, 0 xC56023C5, 0 x45FD1945, 0 xA33A5BA3,
0 x68C23D68, 0 x158D5915, 0 x21ECF321, 0 x3166AE31, 0 x3E6FA23E, 0 x16578216,
0 x95106395, 0 x5BEF015B, 0 x4DB8834D, 0 x91862E91, 0 xB56DD9B5, 0 x1F83511F,
0 x53AA9B53, 0 x635D7C63, 0 x3B68A63B, 0 x3FFEEB3F, 0 xD630A5D6, 0 x257ABE25,
0 xA7AC16A7, 0 x0F090C0F, 0 x35F0E335, 0 x23A76123, 0 xF090C0F0, 0 xAFE98CAF,
0 x809D3A80, 0 x925CF592, 0 x810C7381, 0 x27312C27, 0 x76D02576, 0 xE7560BE7,
0 x7B92BB7B, 0 xE9CE4EE9, 0 xF10189F1, 0 x9F1E6B9F, 0 xA93453A9, 0 xC4F16AC4,
0 x99C3B499, 0 x975BF197, 0 x8347E183, 0 x6B18E66B, 0 xC822BDC8, 0 x0E98450E,
0 x6E1FE26E, 0 xC9B3F4C9, 0 x2F74B62F, 0 xCBF866CB, 0 xFF99CCFF, 0 xEA1495EA,
0 xED5803ED, 0 xF7DC56F7, 0 xE18BD4E1, 0 x1B151C1B, 0 xADA21EAD, 0 x0CD3D70C,
0 x2BE2FB2B, 0 x1DC8C31D, 0 x195E8E19, 0 xC22CB5C2, 0 x8949E989, 0 x12C1CF12,
0 x7E95BF7E, 0 x207DBA20, 0 x6411EA64, 0 x840B7784, 0 x6DC5396D, 0 x6A89AF6A,
0 xD17C33D1, 0 xA171C9A1, 0 xCEFF62CE, 0 x37BB7137, 0 xFB0F81FB, 0 x3DB5793D,
0 x51E10951, 0 xDC3EADDC, 0 x2D3F242D, 0 xA476CDA4, 0 x9D55F99D, 0 xEE82D8EE,
0 x8640E586, 0 xAE78C5AE, 0 xCD25B9CD, 0 x04964D04, 0 x55774455, 0 x0A0E080A,
0 x13508613, 0 x30F7E730, 0 xD337A1D3, 0 x40FA1D40, 0 x3461AA34, 0 x8C4EED8C,
0 xB3B006B3, 0 x6C54706C, 0 x2A73B22A, 0 x523BD252, 0 x0B9F410B, 0 x8B027B8B,
0 x88D8A088, 0 x4FF3114F, 0 x67CB3167, 0 x4627C246, 0 xC06727C0, 0 xB4FC90B4,
0 x28382028, 0 x7F04F67F, 0 x78486078, 0 x2EE5FF2E, 0 x074C9607, 0 x4B655C4B,
0 xC72BB1C7, 0 x6F8EAB6F, 0 x0D429E0D, 0 xBBF59CBB, 0 xF2DB52F2, 0 xF34A1BF3,
0 xA63D5FA6, 0 x59A49359, 0 xBCB90ABC, 0 x3AF9EF3A, 0 xEF1391EF, 0 xFE0885FE,
0 x01914901, 0 x6116EE61, 0 x7CDE2D7C, 0 xB2214FB2, 0 x42B18F42, 0 xDB723BDB,
0 xB82F47B8, 0 x48BF8748, 0 x2CAE6D2C, 0 xE3C046E3, 0 x573CD657, 0 x859A3E85,
0 x29A96929, 0 x7D4F647D, 0 x94812A94, 0 x492ECE49, 0 x17C6CB17, 0 xCA692FCA,
0 xC3BDFCC3, 0 x5CA3975C, 0 x5EE8055E, 0 xD0ED7AD0, 0 x87D1AC87, 0 x8E057F8E,
0 xBA64D5BA, 0 xA8A51AA8, 0 xB7264BB7, 0 xB9BE0EB9, 0 x6087A760, 0 xF8D55AF8,
0 x22362822, 0 x111B1411, 0 xDE753FDE, 0 x79D92979, 0 xAAEE88AA, 0 x332D3C33,
0 x5F794C5F, 0 xB6B702B6, 0 x96CAB896, 0 x5835DA58, 0 x9CC4B09C, 0 xFC4317FC,
0 x1A84551A, 0 xF64D1FF6, 0 x1C598A1C, 0 x38B27D38, 0 xAC3357AC, 0 x18CFC718,
0 xF4068DF4, 0 x69537469, 0 x749BB774, 0 xF597C4F5, 0 x56AD9F56, 0 xDAE372DA,
0 xD5EA7ED5, 0 x4AF4154A, 0 x9E8F229E, 0 xA2AB12A2, 0 x4E62584E, 0 xE85F07E8,
0 xE51D99E5, 0 x39233439, 0 xC1F66EC1, 0 x446C5044, 0 x5D32DE5D, 0 x72466872,
0 x26A06526, 0 x93CDBC93, 0 x03DADB03, 0 xC6BAF8C6, 0 xFA9EC8FA, 0 x82D6A882,
0 xCF6E2BCF, 0 x50704050, 0 xEB85DCEB, 0 x750AFE75, 0 x8A93328A, 0 x8DDFA48D,
0 x4C29CA4C, 0 x141C1014, 0 x73D72173, 0 xCCB4F0CC, 0 x09D4D309, 0 x108A5D10,
0 xE2510FE2, 0 x00000000, 0 x9A196F9A, 0 xE01A9DE0, 0 x8F94368F, 0 xE6C742E6,
0 xECC94AEC, 0 xFDD25EFD, 0 xAB7FC1AB, 0 xD8A8E0D8}
};
/* The exp_to_poly and poly_to_exp tables are used to perform efficient
* operations in GF ( 2 ^ 8 ) represented as GF ( 2 ) [ x ] / w ( x ) where
* w ( x ) = x ^ 8 + x ^ 6 + x ^ 3 + x ^ 2 + 1 . We care about doing that because it ' s part of the
* definition of the RS matrix in the key schedule . Elements of that field
* are polynomials of degree not greater than 7 and all coefficients 0 or 1 ,
* which can be represented naturally by bytes ( just substitute x = 2 ) . In that
* form , GF ( 2 ^ 8 ) addition is the same as bitwise XOR , but GF ( 2 ^ 8 )
* multiplication is inefficient without hardware support . To multiply
* faster , I make use of the fact x is a generator for the nonzero elements ,
* so that every element p of GF ( 2 ) [ x ] / w ( x ) is either 0 or equal to ( x ) ^ n for
* some n in 0 . . 254 . Note that caret is exponentiation in GF ( 2 ^ 8 ) ,
* * not * polynomial notation . So if I want to compute pq where p and q are
* in GF ( 2 ^ 8 ) , I can just say :
* 1 . if p = 0 or q = 0 then pq = 0
* 2 . otherwise , find m and n such that p = x ^ m and q = x ^ n
* 3 . pq = ( x ^ m ) ( x ^ n ) = x ^ ( m + n ) , so add m and n and find pq
* The translations in steps 2 and 3 are looked up in the tables
* poly_to_exp ( for step 2 ) and exp_to_poly ( for step 3 ) . To see this
* in action , look at the CALC_S macro . As additional wrinkles , note that
* one of my operands is always a constant , so the poly_to_exp lookup on it
* is done in advance ; I included the original values in the comments so
* readers can have some chance of recognizing that this * is * the RS matrix
* from the Twofish paper . I ' ve only included the table entries I actually
* need ; I never do a lookup on a variable input of zero and the biggest
* exponents I ' ll ever see are 254 ( variable ) and 237 ( constant ) , so they ' ll
* never sum to more than 491 . I ' m repeating part of the exp_to_poly table
* so that I don ' t have to do mod - 255 reduction in the exponent arithmetic .
* Since I know my constant operands are never zero , I only have to worry
* about zero values in the variable operand , and I do it with a simple
* conditional branch . I know conditionals are expensive , but I couldn ' t
* see a non - horrible way of avoiding them , and I did manage to group the
* statements so that each if covers four group multiplications. */
static const u8 poly_to_exp[255 ] = {
0 x00, 0 x01, 0 x17, 0 x02, 0 x2E, 0 x18, 0 x53, 0 x03, 0 x6A, 0 x2F, 0 x93, 0 x19,
0 x34, 0 x54, 0 x45, 0 x04, 0 x5C, 0 x6B, 0 xB6, 0 x30, 0 xA6, 0 x94, 0 x4B, 0 x1A,
0 x8C, 0 x35, 0 x81, 0 x55, 0 xAA, 0 x46, 0 x0D, 0 x05, 0 x24, 0 x5D, 0 x87, 0 x6C,
0 x9B, 0 xB7, 0 xC1, 0 x31, 0 x2B, 0 xA7, 0 xA3, 0 x95, 0 x98, 0 x4C, 0 xCA, 0 x1B,
0 xE6, 0 x8D, 0 x73, 0 x36, 0 xCD, 0 x82, 0 x12, 0 x56, 0 x62, 0 xAB, 0 xF0, 0 x47,
0 x4F, 0 x0E, 0 xBD, 0 x06, 0 xD4, 0 x25, 0 xD2, 0 x5E, 0 x27, 0 x88, 0 x66, 0 x6D,
0 xD6, 0 x9C, 0 x79, 0 xB8, 0 x08, 0 xC2, 0 xDF, 0 x32, 0 x68, 0 x2C, 0 xFD, 0 xA8,
0 x8A, 0 xA4, 0 x5A, 0 x96, 0 x29, 0 x99, 0 x22, 0 x4D, 0 x60, 0 xCB, 0 xE4, 0 x1C,
0 x7B, 0 xE7, 0 x3B, 0 x8E, 0 x9E, 0 x74, 0 xF4, 0 x37, 0 xD8, 0 xCE, 0 xF9, 0 x83,
0 x6F, 0 x13, 0 xB2, 0 x57, 0 xE1, 0 x63, 0 xDC, 0 xAC, 0 xC4, 0 xF1, 0 xAF, 0 x48,
0 x0A, 0 x50, 0 x42, 0 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E, 0 x31, 0 x62, 0 xC4, 0 xC5, 0 xC7, 0 xC3, 0 xCB
};
/* The table constants are indices of
* S-box entries, preprocessed through q0 and q1. */
static const u8 calc_sb_tbl[512 ] = {
0 xA9, 0 x75, 0 x67, 0 xF3, 0 xB3, 0 xC6, 0 xE8, 0 xF4,
0 x04, 0 xDB, 0 xFD, 0 x7B, 0 xA3, 0 xFB, 0 x76, 0 xC8,
0 x9A, 0 x4A, 0 x92, 0 xD3, 0 x80, 0 xE6, 0 x78, 0 x6B,
0 xE4, 0 x45, 0 xDD, 0 x7D, 0 xD1, 0 xE8, 0 x38, 0 x4B,
0 x0D, 0 xD6, 0 xC6, 0 x32, 0 x35, 0 xD8, 0 x98, 0 xFD,
0 x18, 0 x37, 0 xF7, 0 x71, 0 xEC, 0 xF1, 0 x6C, 0 xE1,
0 x43, 0 x30, 0 x75, 0 x0F, 0 x37, 0 xF8, 0 x26, 0 x1B,
0 xFA, 0 x87, 0 x13, 0 xFA, 0 x94, 0 x06, 0 x48, 0 x3F,
0 xF2, 0 x5E, 0 xD0, 0 xBA, 0 x8B, 0 xAE, 0 x30, 0 x5B,
0 x84, 0 x8A, 0 x54, 0 x00, 0 xDF, 0 xBC, 0 x23, 0 x9D,
0 x19, 0 x6D, 0 x5B, 0 xC1, 0 x3D, 0 xB1, 0 x59, 0 x0E,
0 xF3, 0 x80, 0 xAE, 0 x5D, 0 xA2, 0 xD2, 0 x82, 0 xD5,
0 x63, 0 xA0, 0 x01, 0 x84, 0 x83, 0 x07, 0 x2E, 0 x14,
0 xD9, 0 xB5, 0 x51, 0 x90, 0 x9B, 0 x2C, 0 x7C, 0 xA3,
0 xA6, 0 xB2, 0 xEB, 0 x73, 0 xA5, 0 x4C, 0 xBE, 0 x54,
0 x16, 0 x92, 0 x0C, 0 x74, 0 xE3, 0 x36, 0 x61, 0 x51,
0 xC0, 0 x38, 0 x8C, 0 xB0, 0 x3A, 0 xBD, 0 xF5, 0 x5A,
0 x73, 0 xFC, 0 x2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 0 x22, 0 x50, 0 xC9, 0 xDE, 0 xC0, 0 x68, 0 x9B,
0 x65, 0 x89, 0 xBC, 0 xD4, 0 xDB, 0 xED, 0 xF8, 0 xAB,
0 xC8, 0 x12, 0 xA8, 0 xA2, 0 x2B, 0 x0D, 0 x40, 0 x52,
0 xDC, 0 xBB, 0 xFE, 0 x02, 0 x32, 0 x2F, 0 xA4, 0 xA9,
0 xCA, 0 xD7, 0 x10, 0 x61, 0 x21, 0 x1E, 0 xF0, 0 xB4,
0 xD3, 0 x50, 0 x5D, 0 x04, 0 x0F, 0 xF6, 0 x00, 0 xC2,
0 x6F, 0 x16, 0 x9D, 0 x25, 0 x36, 0 x86, 0 x42, 0 x56,
0 x4A, 0 x55, 0 x5E, 0 x09, 0 xC1, 0 xBE, 0 xE0, 0 x91
};
/* Macro to perform one column of the RS matrix multiplication. The
* parameters a , b , c , and d are the four bytes of output ; i is the index
* of the key bytes , and w , x , y , and z , are the column of constants from
* the RS matrix, preprocessed through the poly_to_exp table. */
#define CALC_S(a, b, c, d, i, w, x, y, z) \
if (key[i]) { \
tmp = poly_to_exp[key[i] - 1 ]; \
(a) ^= exp_to_poly[tmp + (w)]; \
(b) ^= exp_to_poly[tmp + (x)]; \
(c) ^= exp_to_poly[tmp + (y)]; \
(d) ^= exp_to_poly[tmp + (z)]; \
}
/* Macros to calculate the key-dependent S-boxes for a 128-bit key using
* the S vector from CALC_S . CALC_SB_2 computes a single entry in all
* four S - boxes , where i is the index of the entry to compute , and a and b
* are the index numbers preprocessed through the q0 and q1 tables
* respectively. */
#define CALC_SB_2(i, a, b) \
ctx->s[0 ][i] = mds[0 ][q0[(a) ^ sa] ^ se]; \
ctx->s[1 ][i] = mds[1 ][q0[(b) ^ sb] ^ sf]; \
ctx->s[2 ][i] = mds[2 ][q1[(a) ^ sc] ^ sg]; \
ctx->s[3 ][i] = mds[3 ][q1[(b) ^ sd] ^ sh]
/* Macro exactly like CALC_SB_2, but for 192-bit keys. */
#define CALC_SB192_2(i, a, b) \
ctx->s[0 ][i] = mds[0 ][q0[q0[(b) ^ sa] ^ se] ^ si]; \
ctx->s[1 ][i] = mds[1 ][q0[q1[(b) ^ sb] ^ sf] ^ sj]; \
ctx->s[2 ][i] = mds[2 ][q1[q0[(a) ^ sc] ^ sg] ^ sk]; \
ctx->s[3 ][i] = mds[3 ][q1[q1[(a) ^ sd] ^ sh] ^ sl];
/* Macro exactly like CALC_SB_2, but for 256-bit keys. */
#define CALC_SB256_2(i, a, b) \
ctx->s[0 ][i] = mds[0 ][q0[q0[q1[(b) ^ sa] ^ se] ^ si] ^ sm]; \
ctx->s[1 ][i] = mds[1 ][q0[q1[q1[(a) ^ sb] ^ sf] ^ sj] ^ sn]; \
ctx->s[2 ][i] = mds[2 ][q1[q0[q0[(a) ^ sc] ^ sg] ^ sk] ^ so]; \
ctx->s[3 ][i] = mds[3 ][q1[q1[q0[(b) ^ sd] ^ sh] ^ sl] ^ sp];
/* Macros to calculate the whitening and round subkeys. CALC_K_2 computes the
* last two stages of the h ( ) function for a given index ( either 2 i or 2 i + 1 ) .
* a , b , c , and d are the four bytes going into the last two stages . For
* 128 - bit keys , this is the entire h ( ) function and a and c are the index
* preprocessed through q0 and q1 respectively ; for longer keys they are the
* output of previous stages . j is the index of the first key byte to use .
* CALC_K computes a pair of subkeys for 128 - bit Twofish , by calling CALC_K_2
* twice , doing the Pseudo - Hadamard Transform , and doing the necessary
* rotations . Its parameters are : a , the array to write the results into ,
* j , the index of the first output entry , k and l , the preprocessed indices
* for index 2 i , and m and n , the preprocessed indices for index 2 i + 1 .
* CALC_K192_2 expands CALC_K_2 to handle 192 - bit keys , by doing an
* additional lookup - and - XOR stage . The parameters a , b , c and d are the
* four bytes going into the last three stages . For 192 - bit keys , c = d
* are the index preprocessed through q0 , and a = b are the index
* preprocessed through q1 ; j is the index of the first key byte to use .
* CALC_K192 is identical to CALC_K but for using the CALC_K192_2 macro
* instead of CALC_K_2 .
* CALC_K256_2 expands CALC_K192_2 to handle 256 - bit keys , by doing an
* additional lookup - and - XOR stage . The parameters a and b are the index
* preprocessed through q0 and q1 respectively ; j is the index of the first
* key byte to use . CALC_K256 is identical to CALC_K but for using the
* CALC_K256_2 macro instead of CALC_K_2. */
#define CALC_K_2(a, b, c, d, j) \
mds[0 ][q0[a ^ key[(j) + 8 ]] ^ key[j]] \
^ mds[1 ][q0[b ^ key[(j) + 9 ]] ^ key[(j) + 1 ]] \
^ mds[2 ][q1[c ^ key[(j) + 10 ]] ^ key[(j) + 2 ]] \
^ mds[3 ][q1[d ^ key[(j) + 11 ]] ^ key[(j) + 3 ]]
#define CALC_K(a, j, k, l, m, n) \
x = CALC_K_2 (k, l, k, l, 0 ); \
y = CALC_K_2 (m, n, m, n, 4 ); \
y = rol32(y, 8 ); \
x += y; y += x; ctx->a[j] = x; \
ctx->a[(j) + 1 ] = rol32(y, 9 )
#define CALC_K192_2(a, b, c, d, j) \
CALC_K_2 (q0[a ^ key[(j) + 16 ]], \
q1[b ^ key[(j) + 17 ]], \
q0[c ^ key[(j) + 18 ]], \
q1[d ^ key[(j) + 19 ]], j)
#define CALC_K192(a, j, k, l, m, n) \
x = CALC_K192_2 (l, l, k, k, 0 ); \
y = CALC_K192_2 (n, n, m, m, 4 ); \
y = rol32(y, 8 ); \
x += y; y += x; ctx->a[j] = x; \
ctx->a[(j) + 1 ] = rol32(y, 9 )
#define CALC_K256_2(a, b, j) \
CALC_K192_2 (q1[b ^ key[(j) + 24 ]], \
q1[a ^ key[(j) + 25 ]], \
q0[a ^ key[(j) + 26 ]], \
q0[b ^ key[(j) + 27 ]], j)
#define CALC_K256(a, j, k, l, m, n) \
x = CALC_K256_2 (k, l, 0 ); \
y = CALC_K256_2 (m, n, 4 ); \
y = rol32(y, 8 ); \
x += y; y += x; ctx->a[j] = x; \
ctx->a[(j) + 1 ] = rol32(y, 9 )
/* Perform the key setup. */
int __twofish_setkey(struct twofish_ctx *ctx, const u8 *key,
unsigned int key_len)
{
int i, j, k;
/* Temporaries for CALC_K. */
u32 x, y;
/* The S vector used to key the S-boxes, split up into individual bytes.
* 128-bit keys use only sa through sh; 256-bit use all of them. */
u8 sa = 0 , sb = 0 , sc = 0 , sd = 0 , se = 0 , sf = 0 , sg = 0 , sh = 0 ;
u8 si = 0 , sj = 0 , sk = 0 , sl = 0 , sm = 0 , sn = 0 , so = 0 , sp = 0 ;
/* Temporary for CALC_S. */
u8 tmp;
/* Check key length. */
if (key_len % 8 )
return -EINVAL; /* unsupported key length */
/* Compute the first two words of the S vector. The magic numbers are
* the entries of the RS matrix , preprocessed through poly_to_exp . The
* numbers in the comments are the original ( polynomial form ) matrix
* entries. */
CALC_S (sa, sb, sc, sd, 0 , 0 x00, 0 x2D, 0 x01, 0 x2D); /* 01 A4 02 A4 */
CALC_S (sa, sb, sc, sd, 1 , 0 x2D, 0 xA4, 0 x44, 0 x8A); /* A4 56 A1 55 */
CALC_S (sa, sb, sc, sd, 2 , 0 x8A, 0 xD5, 0 xBF, 0 xD1); /* 55 82 FC 87 */
CALC_S (sa, sb, sc, sd, 3 , 0 xD1, 0 x7F, 0 x3D, 0 x99); /* 87 F3 C1 5A */
CALC_S (sa, sb, sc, sd, 4 , 0 x99, 0 x46, 0 x66, 0 x96); /* 5A 1E 47 58 */
CALC_S (sa, sb, sc, sd, 5 , 0 x96, 0 x3C, 0 x5B, 0 xED); /* 58 C6 AE DB */
CALC_S (sa, sb, sc, sd, 6 , 0 xED, 0 x37, 0 x4F, 0 xE0); /* DB 68 3D 9E */
CALC_S (sa, sb, sc, sd, 7 , 0 xE0, 0 xD0, 0 x8C, 0 x17); /* 9E E5 19 03 */
CALC_S (se, sf, sg, sh, 8 , 0 x00, 0 x2D, 0 x01, 0 x2D); /* 01 A4 02 A4 */
CALC_S (se, sf, sg, sh, 9 , 0 x2D, 0 xA4, 0 x44, 0 x8A); /* A4 56 A1 55 */
CALC_S (se, sf, sg, sh, 10 , 0 x8A, 0 xD5, 0 xBF, 0 xD1); /* 55 82 FC 87 */
CALC_S (se, sf, sg, sh, 11 , 0 xD1, 0 x7F, 0 x3D, 0 x99); /* 87 F3 C1 5A */
CALC_S (se, sf, sg, sh, 12 , 0 x99, 0 x46, 0 x66, 0 x96); /* 5A 1E 47 58 */
CALC_S (se, sf, sg, sh, 13 , 0 x96, 0 x3C, 0 x5B, 0 xED); /* 58 C6 AE DB */
CALC_S (se, sf, sg, sh, 14 , 0 xED, 0 x37, 0 x4F, 0 xE0); /* DB 68 3D 9E */
CALC_S (se, sf, sg, sh, 15 , 0 xE0, 0 xD0, 0 x8C, 0 x17); /* 9E E5 19 03 */
if (key_len == 24 || key_len == 32 ) { /* 192- or 256-bit key */
/* Calculate the third word of the S vector */
CALC_S (si, sj, sk, sl, 16 , 0 x00, 0 x2D, 0 x01, 0 x2D); /* 01 A4 02 A4 */
CALC_S (si, sj, sk, sl, 17 , 0 x2D, 0 xA4, 0 x44, 0 x8A); /* A4 56 A1 55 */
CALC_S (si, sj, sk, sl, 18 , 0 x8A, 0 xD5, 0 xBF, 0 xD1); /* 55 82 FC 87 */
CALC_S (si, sj, sk, sl, 19 , 0 xD1, 0 x7F, 0 x3D, 0 x99); /* 87 F3 C1 5A */
CALC_S (si, sj, sk, sl, 20 , 0 x99, 0 x46, 0 x66, 0 x96); /* 5A 1E 47 58 */
CALC_S (si, sj, sk, sl, 21 , 0 x96, 0 x3C, 0 x5B, 0 xED); /* 58 C6 AE DB */
CALC_S (si, sj, sk, sl, 22 , 0 xED, 0 x37, 0 x4F, 0 xE0); /* DB 68 3D 9E */
CALC_S (si, sj, sk, sl, 23 , 0 xE0, 0 xD0, 0 x8C, 0 x17); /* 9E E5 19 03 */
}
if (key_len == 32 ) { /* 256-bit key */
/* Calculate the fourth word of the S vector */
CALC_S (sm, sn, so, sp, 24 , 0 x00, 0 x2D, 0 x01, 0 x2D); /* 01 A4 02 A4 */
CALC_S (sm, sn, so, sp, 25 , 0 x2D, 0 xA4, 0 x44, 0 x8A); /* A4 56 A1 55 */
CALC_S (sm, sn, so, sp, 26 , 0 x8A, 0 xD5, 0 xBF, 0 xD1); /* 55 82 FC 87 */
CALC_S (sm, sn, so, sp, 27 , 0 xD1, 0 x7F, 0 x3D, 0 x99); /* 87 F3 C1 5A */
CALC_S (sm, sn, so, sp, 28 , 0 x99, 0 x46, 0 x66, 0 x96); /* 5A 1E 47 58 */
CALC_S (sm, sn, so, sp, 29 , 0 x96, 0 x3C, 0 x5B, 0 xED); /* 58 C6 AE DB */
CALC_S (sm, sn, so, sp, 30 , 0 xED, 0 x37, 0 x4F, 0 xE0); /* DB 68 3D 9E */
CALC_S (sm, sn, so, sp, 31 , 0 xE0, 0 xD0, 0 x8C, 0 x17); /* 9E E5 19 03 */
/* Compute the S-boxes. */
for ( i = j = 0 , k = 1 ; i < 256 ; i++, j += 2 , k += 2 ) {
CALC_SB256_2( i, calc_sb_tbl[j], calc_sb_tbl[k] );
}
/* CALC_K256/CALC_K192/CALC_K loops were unrolled.
* Unrolling produced x2 . 5 more code ( + 18 k on i386 ) ,
* and speeded up key setup by 7 % :
* unrolled : twofish_setkey / sec : 41128
* loop : twofish_setkey / sec : 38148
* CALC_K256 : ~ 100 insns each
* CALC_K192 : ~ 90 insns
* CALC_K : ~ 70 insns
*/
/* Calculate whitening and round subkeys */
for ( i = 0 ; i < 8 ; i += 2 ) {
CALC_K256 (w, i, q0[i], q1[i], q0[i+1 ], q1[i+1 ]);
}
for ( i = 0 ; i < 32 ; i += 2 ) {
CALC_K256 (k, i, q0[i+8 ], q1[i+8 ], q0[i+9 ], q1[i+9 ]);
}
} else if (key_len == 24 ) { /* 192-bit key */
/* Compute the S-boxes. */
for ( i = j = 0 , k = 1 ; i < 256 ; i++, j += 2 , k += 2 ) {
CALC_SB192_2( i, calc_sb_tbl[j], calc_sb_tbl[k] );
}
/* Calculate whitening and round subkeys */
for ( i = 0 ; i < 8 ; i += 2 ) {
CALC_K192 (w, i, q0[i], q1[i], q0[i+1 ], q1[i+1 ]);
}
for ( i = 0 ; i < 32 ; i += 2 ) {
CALC_K192 (k, i, q0[i+8 ], q1[i+8 ], q0[i+9 ], q1[i+9 ]);
}
} else { /* 128-bit key */
/* Compute the S-boxes. */
for ( i = j = 0 , k = 1 ; i < 256 ; i++, j += 2 , k += 2 ) {
CALC_SB_2( i, calc_sb_tbl[j], calc_sb_tbl[k] );
}
/* Calculate whitening and round subkeys */
for ( i = 0 ; i < 8 ; i += 2 ) {
CALC_K (w, i, q0[i], q1[i], q0[i+1 ], q1[i+1 ]);
}
for ( i = 0 ; i < 32 ; i += 2 ) {
CALC_K (k, i, q0[i+8 ], q1[i+8 ], q0[i+9 ], q1[i+9 ]);
}
}
return 0 ;
}
EXPORT_SYMBOL_GPL(__twofish_setkey);
int twofish_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int key_len)
{
return __twofish_setkey(crypto_tfm_ctx(tfm), key, key_len);
}
EXPORT_SYMBOL_GPL(twofish_setkey);
MODULE_LICENSE("GPL" );
MODULE_DESCRIPTION("Twofish cipher common functions" );
Messung V0.5 in Prozent C=91 H=58 G=75