1 // SPDX-License-Identifier: GPL-2.0-or-later 1 2 /* 3 * Common Twofish algorithm parts shared betwe 4 * implementations 5 * 6 * Originally Twofish for GPG 7 * By Matthew Skala <mskala@ansuz.sooke.bc.ca> 8 * 256-bit key length added March 20, 1999 9 * Some modifications to reduce the text size 10 * Ported to the kerneli patch by Marc Mutz <M 11 * Ported to CryptoAPI by Colin Slater <hoho@t 12 * 13 * The original author has disclaimed all copy 14 * code and thus put it in the public domain. 15 * have put this under the GNU General Public 16 * 17 * This code is a "clean room" implementation, 18 * _Twofish: A 128-Bit Block Cipher_ by Bruce 19 * Doug Whiting, David Wagner, Chris Hall, and 20 * through http://www.counterpane.com/twofish. 21 * 22 * For background information on multiplicatio 23 * the matrix operations in the key schedule, 24 * Abstract Algebra_ by Joseph A. Gallian, esp 25 * Third Edition. 26 */ 27 28 #include <crypto/algapi.h> 29 #include <crypto/twofish.h> 30 #include <linux/bitops.h> 31 #include <linux/errno.h> 32 #include <linux/init.h> 33 #include <linux/kernel.h> 34 #include <linux/module.h> 35 #include <linux/types.h> 36 37 38 /* The large precomputed tables for the Twofis 39 * Taken from the same source as twofish.c 40 * Marc Mutz <Marc@Mutz.com> 41 */ 42 43 /* These two tables are the q0 and q1 permutat 44 * the Twofish paper. */ 45 46 static const u8 q0[256] = { 47 0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0x 48 0xE4, 0xDD, 0xD1, 0x38, 0x0D, 0xC6, 0x 49 0x43, 0x75, 0x37, 0x26, 0xFA, 0x13, 0x 50 0x84, 0x54, 0xDF, 0x23, 0x19, 0x5B, 0x 51 0x63, 0x01, 0x83, 0x2E, 0xD9, 0x51, 0x 52 0x16, 0x0C, 0xE3, 0x61, 0xC0, 0x8C, 0x 53 0xBB, 0x4E, 0x89, 0x6B, 0x53, 0x6A, 0x 54 0xE2, 0xF4, 0xB6, 0x66, 0xCC, 0x95, 0x 55 0xFB, 0xC3, 0x8E, 0xB5, 0xE9, 0xCF, 0x 56 0x33, 0xC9, 0x62, 0x71, 0x81, 0x79, 0x 57 0xE5, 0xC5, 0xB9, 0x4D, 0x44, 0x08, 0x 58 0x06, 0x70, 0xB2, 0xD2, 0x41, 0x7B, 0x 59 0x20, 0xF6, 0x60, 0xFF, 0x96, 0x5C, 0x 60 0x5F, 0x93, 0x0A, 0xEF, 0x91, 0x85, 0x 61 0x47, 0x87, 0x6D, 0x46, 0xD6, 0x3E, 0x 62 0xFC, 0x97, 0x05, 0x7A, 0xAC, 0x7F, 0x 63 0x28, 0x14, 0x3F, 0x29, 0x88, 0x3C, 0x 64 0x55, 0x1F, 0x8A, 0x7D, 0x57, 0xC7, 0x 65 0x7E, 0x15, 0x22, 0x12, 0x58, 0x07, 0x 66 0x65, 0xBC, 0xDB, 0xF8, 0xC8, 0xA8, 0x 67 0xCA, 0x10, 0x21, 0xF0, 0xD3, 0x5D, 0x 68 0x4A, 0x5E, 0xC1, 0xE0 69 }; 70 71 static const u8 q1[256] = { 72 0x75, 0xF3, 0xC6, 0xF4, 0xDB, 0x7B, 0x 73 0x45, 0x7D, 0xE8, 0x4B, 0xD6, 0x32, 0x 74 0x30, 0x0F, 0xF8, 0x1B, 0x87, 0xFA, 0x 75 0x8A, 0x00, 0xBC, 0x9D, 0x6D, 0xC1, 0x 76 0xA0, 0x84, 0x07, 0x14, 0xB5, 0x90, 0x 77 0x92, 0x74, 0x36, 0x51, 0x38, 0xB0, 0x 78 0x6C, 0x42, 0xF7, 0x10, 0x7C, 0x28, 0x 79 0x24, 0x46, 0x3B, 0x70, 0xCA, 0xE3, 0x 80 0xA6, 0x83, 0x20, 0xFF, 0x9F, 0x77, 0x 81 0x40, 0xE7, 0x2B, 0xE2, 0x79, 0x0C, 0x 82 0xE4, 0x9A, 0xA4, 0x97, 0x7E, 0xDA, 0x 83 0x3D, 0xF0, 0xDE, 0xB3, 0x0B, 0x72, 0x 84 0x8F, 0x33, 0x26, 0x5F, 0xEC, 0x76, 0x 85 0xC4, 0x1A, 0xEB, 0xD9, 0xC5, 0x39, 0x 86 0x18, 0x23, 0xDD, 0x1F, 0x4E, 0x2D, 0x 87 0x78, 0x5C, 0x58, 0x19, 0x8D, 0xE5, 0x 88 0xAF, 0x63, 0xB6, 0xFE, 0xF5, 0xB7, 0x 89 0xE0, 0x4D, 0x43, 0x69, 0x29, 0x2E, 0x 90 0x6E, 0x47, 0xDF, 0x34, 0x35, 0x6A, 0x 91 0x89, 0xD4, 0xED, 0xAB, 0x12, 0xA2, 0x 92 0xD7, 0x61, 0x1E, 0xB4, 0x50, 0x04, 0x 93 0x55, 0x09, 0xBE, 0x91 94 }; 95 96 /* These MDS tables are actually tables of MDS 97 * because it is only ever used that way and w 98 * precomputing. Of course the main saving co 99 * GF(2^8) multiplication involved in the MDS 100 * things up in these tables we reduce the mat 101 * and three XORs. Semi-formally, the definit 102 * mds[0][i] = MDS (q1[i] 0 0 0)^T mds[1][i] 103 * mds[2][i] = MDS (0 0 q1[i] 0)^T mds[3][i] 104 * where ^T means "transpose", the matrix mult 105 * represented as GF(2)[x]/v(x) where v(x)=x^8 106 * by Schneier et al, and I'm casually glossin 107 * conversion issues. */ 108 109 static const u32 mds[4][256] = { 110 { 111 0xBCBC3275, 0xECEC21F3, 0x202043C6, 0x 112 0xE2E22BFB, 0x9E9EFAC8, 0xC9C9EC4A, 0x 113 0x98980E45, 0xB2B2387D, 0xA6A6D2E8, 0x 114 0x8282EED8, 0x525298FD, 0x7B7BD437, 0x 115 0x24243C30, 0x5151E20F, 0xBABAC6F8, 0x 116 0xB0B0B306, 0x7575DE3F, 0xD2D2FD5E, 0x 117 0x59591C8A, 0x00000000, 0xCDCD93BC, 0x 118 0x2B2BC7B1, 0xBEBEB90E, 0xE0E0A080, 0x 119 0xD8D888A0, 0xE7E7A584, 0x5F5FE807, 0x 120 0x3131272C, 0x808065A3, 0x73732AB2, 0x 121 0x4B4B0292, 0x53536974, 0x94948F36, 0x 122 0x2222C8BD, 0xD5D5F85A, 0xBDBDC3FC, 0x 123 0x4141776C, 0xC7C7E642, 0xEBEB24F7, 0x 124 0x6767C027, 0xE9E9AF8C, 0x4444F913, 0x 125 0x3F3F2D24, 0xC0C0E346, 0x7272DB3B, 0x 126 0x0808FE85, 0xC6C617CB, 0xF3F34F11, 0x 127 0x68683BA6, 0xB8B84D83, 0x38382820, 0x 128 0xC8C81DC3, 0x9999FFCC, 0x5858ED03, 0x 129 0x70705040, 0xF7F730E7, 0x6E6ECF2B, 0x 130 0x616134AA, 0x57571682, 0x9F9F0B41, 0x 131 0xAFAFDDE4, 0x4545089A, 0xDFDF8DA4, 0x 132 0xEDEDD07A, 0x4343FC17, 0xF8F8CB66, 0x 133 0xC2C2683D, 0xB4B4CCF0, 0x32325DDE, 0x 134 0x878760A7, 0x15151B1C, 0xF9F93AEF, 0x 135 0xB1B1428F, 0x7C7CD133, 0x88889B26, 0x 136 0x8181942A, 0x91910149, 0x0F0FFB81, 0x 137 0x9797F5C4, 0xA5A5A81A, 0xFEFE3FEB, 0x 138 0x1D1DE599, 0x7676A4CD, 0x3E3EDCAD, 0x 139 0x12121E18, 0x6060C523, 0x6A6AB0DD, 0x 140 0x55559DF9, 0x7E7E5A48, 0x2121B24F, 0x 141 0x5A5A6678, 0x65654B5C, 0x62624E58, 0x 142 0xF2F2BE98, 0x3333AC57, 0x17179067, 0x 143 0x89896AAF, 0x10109563, 0x74742FB6, 0x 144 0x2D2D333C, 0x3030D6A5, 0x2E2E49CE, 0x 145 0xA8A8D8E0, 0x9696044D, 0x2828BD43, 0x 146 0xD1D187AC, 0xF4F44A15, 0x8D8D1559, 0x 147 0xF6F6C16E, 0x2F2FB847, 0xDDDD06DF, 0x 148 0xC1C112CF, 0x8585EBDC, 0x8F8F9E22, 0x 149 0x0101F189, 0x8B8BE1D4, 0x4E4E8CED, 0x 150 0xE6E6540D, 0xDBDBF252, 0x92927BBB, 0x 151 0xD3D30CD7, 0xA7A72361, 0xA2A2AD1E, 0x 152 0x04047FF6, 0x272746C2, 0xACACA716, 0x 153 0x84841A55, 0xE1E15109, 0x7A7A25BE, 0x 154 155 { 156 0xA9D93939, 0x67901717, 0xB3719C9C, 0x 157 0xA3658080, 0x76DFE4E4, 0x9A084545, 0x 158 0xE4DDAFAF, 0xDDB06A6A, 0xD1BF6363, 0x 159 0x3562CCCC, 0x98BEF2F2, 0x181E1212, 0x 160 0x43BD2828, 0x7532BCBC, 0x37D47B7B, 0x 161 0x94B1FBFB, 0x485A7E7E, 0xF27A0303, 0x 162 0x84A5E7E7, 0x54416B6B, 0xDF06DDDD, 0x 163 0x3D68C2C2, 0x59158D8D, 0xF321ECEC, 0x 164 0x63951010, 0x015BEFEF, 0x834DB8B8, 0x 165 0x9B53AAAA, 0x7C635D5D, 0xA63B6868, 0x 166 0x16A7ACAC, 0x0C0F0909, 0xE335F0F0, 0x 167 0x3A809D9D, 0xF5925C5C, 0x73810C0C, 0x 168 0xBB7B9292, 0x4EE9CECE, 0x89F10101, 0x 169 0xB499C3C3, 0xF1975B5B, 0xE1834747, 0x 170 0xE26E1F1F, 0xF4C9B3B3, 0xB62F7474, 0x 171 0x03ED5858, 0x56F7DCDC, 0xD4E18B8B, 0x 172 0xFB2BE2E2, 0xC31DC8C8, 0x8E195E5E, 0x 173 0xBF7E9595, 0xBA207D7D, 0xEA641111, 0x 174 0x33D17C7C, 0xC9A17171, 0x62CEFFFF, 0x 175 0x0951E1E1, 0xADDC3E3E, 0x242D3F3F, 0x 176 0xE5864040, 0xC5AE7878, 0xB9CD2525, 0x 177 0x86135050, 0xE730F7F7, 0xA1D33737, 0x 178 0x06B3B0B0, 0x706C5454, 0xB22A7373, 0x 179 0xA088D8D8, 0x114FF3F3, 0x3167CBCB, 0x 180 0x20283838, 0xF67F0404, 0x60784848, 0x 181 0xB1C72B2B, 0xAB6F8E8E, 0x9E0D4242, 0x 182 0x5FA63D3D, 0x9359A4A4, 0x0ABCB9B9, 0x 183 0x49019191, 0xEE611616, 0x2D7CDEDE, 0x 184 0x47B82F2F, 0x8748BFBF, 0x6D2CAEAE, 0x 185 0x6929A9A9, 0x647D4F4F, 0x2A948181, 0x 186 0xFCC3BDBD, 0x975CA3A3, 0x055EE8E8, 0x 187 0xD5BA6464, 0x1AA8A5A5, 0x4BB72626, 0x 188 0x28223636, 0x14111B1B, 0x3FDE7575, 0x 189 0x4C5F7979, 0x02B6B7B7, 0xB896CACA, 0x 190 0x551A8484, 0x1FF64D4D, 0x8A1C5959, 0x 191 0x8DF40606, 0x74695353, 0xB7749B9B, 0x 192 0x7ED5EAEA, 0x154AF4F4, 0x229E8F8F, 0x 193 0x99E51D1D, 0x34392323, 0x6EC1F6F6, 0x 194 0x6526A0A0, 0xBC93CDCD, 0xDB03DADA, 0x 195 0x2BCF6E6E, 0x40507070, 0xDCEB8585, 0x 196 0xCA4C2929, 0x10141C1C, 0x2173D7D7, 0x 197 0x0FE25151, 0x00000000, 0x6F9A1919, 0x 198 0x4AECC9C9, 0x5EFDD2D2, 0xC1AB7F7F, 0x 199 200 { 201 0xBC75BC32, 0xECF3EC21, 0x20C62043, 0x 202 0xE2FBE22B, 0x9EC89EFA, 0xC94AC9EC, 0x 203 0x9845980E, 0xB27DB238, 0xA6E8A6D2, 0x 204 0x82D882EE, 0x52FD5298, 0x7B377BD4, 0x 205 0x2430243C, 0x510F51E2, 0xBAF8BAC6, 0x 206 0xB006B0B3, 0x753F75DE, 0xD25ED2FD, 0x 207 0x598A591C, 0x00000000, 0xCDBCCD93, 0x 208 0x2BB12BC7, 0xBE0EBEB9, 0xE080E0A0, 0x 209 0xD8A0D888, 0xE784E7A5, 0x5F075FE8, 0x 210 0x312C3127, 0x80A38065, 0x73B2732A, 0x 211 0x4B924B02, 0x53745369, 0x9436948F, 0x 212 0x22BD22C8, 0xD55AD5F8, 0xBDFCBDC3, 0x 213 0x416C4177, 0xC742C7E6, 0xEBF7EB24, 0x 214 0x672767C0, 0xE98CE9AF, 0x441344F9, 0x 215 0x3F243F2D, 0xC046C0E3, 0x723B72DB, 0x 216 0x088508FE, 0xC6CBC617, 0xF311F34F, 0x 217 0x68A6683B, 0xB883B84D, 0x38203828, 0x 218 0xC8C3C81D, 0x99CC99FF, 0x580358ED, 0x 219 0x70407050, 0xF7E7F730, 0x6E2B6ECF, 0x 220 0x61AA6134, 0x57825716, 0x9F419F0B, 0x 221 0xAFE4AFDD, 0x459A4508, 0xDFA4DF8D, 0x 222 0xED7AEDD0, 0x431743FC, 0xF866F8CB, 0x 223 0xC23DC268, 0xB4F0B4CC, 0x32DE325D, 0x 224 0x87A78760, 0x151C151B, 0xF9EFF93A, 0x 225 0xB18FB142, 0x7C337CD1, 0x8826889B, 0x 226 0x812A8194, 0x91499101, 0x0F810FFB, 0x 227 0x97C497F5, 0xA51AA5A8, 0xFEEBFE3F, 0x 228 0x1D991DE5, 0x76CD76A4, 0x3EAD3EDC, 0x 229 0x1218121E, 0x602360C5, 0x6ADD6AB0, 0x 230 0x55F9559D, 0x7E487E5A, 0x214F21B2, 0x 231 0x5A785A66, 0x655C654B, 0x6258624E, 0x 232 0xF298F2BE, 0x335733AC, 0x17671790, 0x 233 0x89AF896A, 0x10631095, 0x74B6742F, 0x 234 0x2D3C2D33, 0x30A530D6, 0x2ECE2E49, 0x 235 0xA8E0A8D8, 0x964D9604, 0x284328BD, 0x 236 0xD1ACD187, 0xF415F44A, 0x8D598D15, 0x 237 0xF66EF6C1, 0x2F472FB8, 0xDDDFDD06, 0x 238 0xC1CFC112, 0x85DC85EB, 0x8F228F9E, 0x 239 0x018901F1, 0x8BD48BE1, 0x4EED4E8C, 0x 240 0xE60DE654, 0xDB52DBF2, 0x92BB927B, 0x 241 0xD3D7D30C, 0xA761A723, 0xA21EA2AD, 0x 242 0x04F6047F, 0x27C22746, 0xAC16ACA7, 0x 243 0x8455841A, 0xE109E151, 0x7ABE7A25, 0x 244 245 { 246 0xD939A9D9, 0x90176790, 0x719CB371, 0x 247 0x6580A365, 0xDFE476DF, 0x08459A08, 0x 248 0xDDAFE4DD, 0xB06ADDB0, 0xBF63D1BF, 0x 249 0x62CC3562, 0xBEF298BE, 0x1E12181E, 0x 250 0xBD2843BD, 0x32BC7532, 0xD47B37D4, 0x 251 0xB1FB94B1, 0x5A7E485A, 0x7A03F27A, 0x 252 0xA5E784A5, 0x416B5441, 0x06DDDF06, 0x 253 0x68C23D68, 0x158D5915, 0x21ECF321, 0x 254 0x95106395, 0x5BEF015B, 0x4DB8834D, 0x 255 0x53AA9B53, 0x635D7C63, 0x3B68A63B, 0x 256 0xA7AC16A7, 0x0F090C0F, 0x35F0E335, 0x 257 0x809D3A80, 0x925CF592, 0x810C7381, 0x 258 0x7B92BB7B, 0xE9CE4EE9, 0xF10189F1, 0x 259 0x99C3B499, 0x975BF197, 0x8347E183, 0x 260 0x6E1FE26E, 0xC9B3F4C9, 0x2F74B62F, 0x 261 0xED5803ED, 0xF7DC56F7, 0xE18BD4E1, 0x 262 0x2BE2FB2B, 0x1DC8C31D, 0x195E8E19, 0x 263 0x7E95BF7E, 0x207DBA20, 0x6411EA64, 0x 264 0xD17C33D1, 0xA171C9A1, 0xCEFF62CE, 0x 265 0x51E10951, 0xDC3EADDC, 0x2D3F242D, 0x 266 0x8640E586, 0xAE78C5AE, 0xCD25B9CD, 0x 267 0x13508613, 0x30F7E730, 0xD337A1D3, 0x 268 0xB3B006B3, 0x6C54706C, 0x2A73B22A, 0x 269 0x88D8A088, 0x4FF3114F, 0x67CB3167, 0x 270 0x28382028, 0x7F04F67F, 0x78486078, 0x 271 0xC72BB1C7, 0x6F8EAB6F, 0x0D429E0D, 0x 272 0xA63D5FA6, 0x59A49359, 0xBCB90ABC, 0x 273 0x01914901, 0x6116EE61, 0x7CDE2D7C, 0x 274 0xB82F47B8, 0x48BF8748, 0x2CAE6D2C, 0x 275 0x29A96929, 0x7D4F647D, 0x94812A94, 0x 276 0xC3BDFCC3, 0x5CA3975C, 0x5EE8055E, 0x 277 0xBA64D5BA, 0xA8A51AA8, 0xB7264BB7, 0x 278 0x22362822, 0x111B1411, 0xDE753FDE, 0x 279 0x5F794C5F, 0xB6B702B6, 0x96CAB896, 0x 280 0x1A84551A, 0xF64D1FF6, 0x1C598A1C, 0x 281 0xF4068DF4, 0x69537469, 0x749BB774, 0x 282 0xD5EA7ED5, 0x4AF4154A, 0x9E8F229E, 0x 283 0xE51D99E5, 0x39233439, 0xC1F66EC1, 0x 284 0x26A06526, 0x93CDBC93, 0x03DADB03, 0x 285 0xCF6E2BCF, 0x50704050, 0xEB85DCEB, 0x 286 0x4C29CA4C, 0x141C1014, 0x73D72173, 0x 287 0xE2510FE2, 0x00000000, 0x9A196F9A, 0x 288 0xECC94AEC, 0xFDD25EFD, 0xAB7FC1AB, 0x 289 }; 290 291 /* The exp_to_poly and poly_to_exp tables are 292 * operations in GF(2^8) represented as GF(2)[ 293 * w(x)=x^8+x^6+x^3+x^2+1. We care about doin 294 * definition of the RS matrix in the key sche 295 * are polynomials of degree not greater than 296 * which can be represented naturally by bytes 297 * form, GF(2^8) addition is the same as bitwi 298 * multiplication is inefficient without hardw 299 * faster, I make use of the fact x is a gener 300 * so that every element p of GF(2)[x]/w(x) is 301 * some n in 0..254. Note that caret is expon 302 * *not* polynomial notation. So if I want to 303 * in GF(2^8), I can just say: 304 * 1. if p=0 or q=0 then pq=0 305 * 2. otherwise, find m and n such that p=x 306 * 3. pq=(x^m)(x^n)=x^(m+n), so add m and n 307 * The translations in steps 2 and 3 are looke 308 * poly_to_exp (for step 2) and exp_to_poly (f 309 * in action, look at the CALC_S macro. As ad 310 * one of my operands is always a constant, so 311 * is done in advance; I included the original 312 * readers can have some chance of recognizing 313 * from the Twofish paper. I've only included 314 * need; I never do a lookup on a variable inp 315 * exponents I'll ever see are 254 (variable) 316 * never sum to more than 491. I'm repeating 317 * so that I don't have to do mod-255 reductio 318 * Since I know my constant operands are never 319 * about zero values in the variable operand, 320 * conditional branch. I know conditionals ar 321 * see a non-horrible way of avoiding them, an 322 * statements so that each if covers four grou 323 324 static const u8 poly_to_exp[255] = { 325 0x00, 0x01, 0x17, 0x02, 0x2E, 0x18, 0x 326 0x34, 0x54, 0x45, 0x04, 0x5C, 0x6B, 0x 327 0x8C, 0x35, 0x81, 0x55, 0xAA, 0x46, 0x 328 0x9B, 0xB7, 0xC1, 0x31, 0x2B, 0xA7, 0x 329 0xE6, 0x8D, 0x73, 0x36, 0xCD, 0x82, 0x 330 0x4F, 0x0E, 0xBD, 0x06, 0xD4, 0x25, 0x 331 0xD6, 0x9C, 0x79, 0xB8, 0x08, 0xC2, 0x 332 0x8A, 0xA4, 0x5A, 0x96, 0x29, 0x99, 0x 333 0x7B, 0xE7, 0x3B, 0x8E, 0x9E, 0x74, 0x 334 0x6F, 0x13, 0xB2, 0x57, 0xE1, 0x63, 0x 335 0x0A, 0x50, 0x42, 0x0F, 0xBA, 0xBE, 0x 336 0x65, 0xD3, 0xD1, 0x5F, 0xE3, 0x28, 0x 337 0xB1, 0xD7, 0xF8, 0x9D, 0xF3, 0x7A, 0x 338 0xAE, 0xE0, 0xDB, 0x33, 0x44, 0x69, 0x 339 0x0C, 0x8B, 0x80, 0xA5, 0x4A, 0x5B, 0x 340 0xC0, 0x23, 0x86, 0x4E, 0xBC, 0x61, 0x 341 0x3D, 0x7C, 0xEB, 0xE8, 0xE9, 0x3C, 0x 342 0x1E, 0xF5, 0x3E, 0x38, 0xF6, 0xD9, 0x 343 0xA0, 0x70, 0xED, 0x14, 0x90, 0xB3, 0x 344 0xD0, 0xDD, 0x77, 0xAD, 0xDA, 0xC5, 0x 345 0xB4, 0x0B, 0x7F, 0x51, 0x15, 0x43, 0x 346 0x85, 0xC8, 0xA1 347 }; 348 349 static const u8 exp_to_poly[492] = { 350 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x 351 0xA9, 0x1F, 0x3E, 0x7C, 0xF8, 0xBD, 0x 352 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0x 353 0x21, 0x42, 0x84, 0x45, 0x8A, 0x59, 0x 354 0x14, 0x28, 0x50, 0xA0, 0x0D, 0x1A, 0x 355 0xC6, 0xC1, 0xCF, 0xD3, 0xEB, 0x9B, 0x 356 0x78, 0xF0, 0xAD, 0x17, 0x2E, 0x5C, 0x 357 0x0E, 0x1C, 0x38, 0x70, 0xE0, 0x8D, 0x 358 0x5D, 0xBA, 0x39, 0x72, 0xE4, 0x85, 0x 359 0x24, 0x48, 0x90, 0x6D, 0xDA, 0xF9, 0x 360 0x83, 0x4B, 0x96, 0x61, 0xC2, 0xC9, 0x 361 0xD8, 0xFD, 0xB7, 0x23, 0x46, 0x8C, 0x 362 0xDD, 0xF7, 0xA3, 0x0B, 0x16, 0x2C, 0x 363 0x4A, 0x94, 0x65, 0xCA, 0xD9, 0xFF, 0x 364 0x54, 0xA8, 0x1D, 0x3A, 0x74, 0xE8, 0x 365 0xF1, 0xAF, 0x13, 0x26, 0x4C, 0x98, 0x 366 0xB5, 0x27, 0x4E, 0x9C, 0x75, 0xEA, 0x 367 0xBC, 0x35, 0x6A, 0xD4, 0xE5, 0x87, 0x 368 0x69, 0xD2, 0xE9, 0x9F, 0x73, 0xE6, 0x 369 0x5F, 0xBE, 0x31, 0x62, 0xC4, 0xC5, 0x 370 0x3B, 0x76, 0xEC, 0x95, 0x67, 0xCE, 0x 371 0x8F, 0x53, 0xA6, 0x01, 0x02, 0x04, 0x 372 0x9A, 0x79, 0xF2, 0xA9, 0x1F, 0x3E, 0x 373 0xF5, 0xA7, 0x03, 0x06, 0x0C, 0x18, 0x 374 0x8B, 0x5B, 0xB6, 0x21, 0x42, 0x84, 0x 375 0xA4, 0x05, 0x0A, 0x14, 0x28, 0x50, 0x 376 0xED, 0x97, 0x63, 0xC6, 0xC1, 0xCF, 0x 377 0x0F, 0x1E, 0x3C, 0x78, 0xF0, 0xAD, 0x 378 0xF4, 0xA5, 0x07, 0x0E, 0x1C, 0x38, 0x 379 0x22, 0x44, 0x88, 0x5D, 0xBA, 0x39, 0x 380 0xA2, 0x09, 0x12, 0x24, 0x48, 0x90, 0x 381 0xCC, 0xD5, 0xE7, 0x83, 0x4B, 0x96, 0x 382 0x1B, 0x36, 0x6C, 0xD8, 0xFD, 0xB7, 0x 383 0x32, 0x64, 0xC8, 0xDD, 0xF7, 0xA3, 0x 384 0x5A, 0xB4, 0x25, 0x4A, 0x94, 0x65, 0x 385 0xAC, 0x15, 0x2A, 0x54, 0xA8, 0x1D, 0x 386 0x91, 0x6F, 0xDE, 0xF1, 0xAF, 0x13, 0x 387 0x3F, 0x7E, 0xFC, 0xB5, 0x27, 0x4E, 0x 388 0xB1, 0x2F, 0x5E, 0xBC, 0x35, 0x6A, 0x 389 0x82, 0x49, 0x92, 0x69, 0xD2, 0xE9, 0x 390 0x71, 0xE2, 0x89, 0x5F, 0xBE, 0x31, 0x 391 }; 392 393 394 /* The table constants are indices of 395 * S-box entries, preprocessed through q0 and 396 static const u8 calc_sb_tbl[512] = { 397 0xA9, 0x75, 0x67, 0xF3, 0xB3, 0xC6, 0x 398 0x04, 0xDB, 0xFD, 0x7B, 0xA3, 0xFB, 0x 399 0x9A, 0x4A, 0x92, 0xD3, 0x80, 0xE6, 0x 400 0xE4, 0x45, 0xDD, 0x7D, 0xD1, 0xE8, 0x 401 0x0D, 0xD6, 0xC6, 0x32, 0x35, 0xD8, 0x 402 0x18, 0x37, 0xF7, 0x71, 0xEC, 0xF1, 0x 403 0x43, 0x30, 0x75, 0x0F, 0x37, 0xF8, 0x 404 0xFA, 0x87, 0x13, 0xFA, 0x94, 0x06, 0x 405 0xF2, 0x5E, 0xD0, 0xBA, 0x8B, 0xAE, 0x 406 0x84, 0x8A, 0x54, 0x00, 0xDF, 0xBC, 0x 407 0x19, 0x6D, 0x5B, 0xC1, 0x3D, 0xB1, 0x 408 0xF3, 0x80, 0xAE, 0x5D, 0xA2, 0xD2, 0x 409 0x63, 0xA0, 0x01, 0x84, 0x83, 0x07, 0x 410 0xD9, 0xB5, 0x51, 0x90, 0x9B, 0x2C, 0x 411 0xA6, 0xB2, 0xEB, 0x73, 0xA5, 0x4C, 0x 412 0x16, 0x92, 0x0C, 0x74, 0xE3, 0x36, 0x 413 0xC0, 0x38, 0x8C, 0xB0, 0x3A, 0xBD, 0x 414 0x73, 0xFC, 0x2C, 0x60, 0x25, 0x62, 0x 415 0xBB, 0x6C, 0x4E, 0x42, 0x89, 0xF7, 0x 416 0x53, 0x7C, 0x6A, 0x28, 0xB4, 0x27, 0x 417 0xE1, 0x13, 0xE6, 0x95, 0xBD, 0x9C, 0x 418 0xE2, 0x24, 0xF4, 0x46, 0xB6, 0x3B, 0x 419 0xCC, 0xCA, 0x95, 0xE3, 0x03, 0x85, 0x 420 0xD4, 0x11, 0x1C, 0xD0, 0x1E, 0x93, 0x 421 0xFB, 0xA6, 0xC3, 0x83, 0x8E, 0x20, 0x 422 0xE9, 0x9F, 0xCF, 0x77, 0xBF, 0xC3, 0x 423 0xEA, 0x03, 0x77, 0x6F, 0x39, 0x08, 0x 424 0x33, 0x40, 0xC9, 0xE7, 0x62, 0x2B, 0x 425 0x81, 0x79, 0x79, 0x0C, 0x09, 0xAA, 0x 426 0x24, 0x41, 0xCD, 0x3A, 0xF9, 0xEA, 0x 427 0xE5, 0xE4, 0xC5, 0x9A, 0xB9, 0xA4, 0x 428 0x44, 0x7E, 0x08, 0xDA, 0x86, 0x7A, 0x 429 0xA1, 0x66, 0x1D, 0x94, 0xAA, 0xA1, 0x 430 0x06, 0x3D, 0x70, 0xF0, 0xB2, 0xDE, 0x 431 0x41, 0x0B, 0x7B, 0x72, 0xA0, 0xA7, 0x 432 0x31, 0xEF, 0xC2, 0xD1, 0x27, 0x53, 0x 433 0x20, 0x8F, 0xF6, 0x33, 0x60, 0x26, 0x 434 0x96, 0xEC, 0x5C, 0x76, 0xB1, 0x2A, 0x 435 0x9E, 0x81, 0x9C, 0x88, 0x52, 0xEE, 0x 436 0x5F, 0xC4, 0x93, 0x1A, 0x0A, 0xEB, 0x 437 0x91, 0xC5, 0x85, 0x39, 0x49, 0x99, 0x 438 0x2D, 0xAD, 0x4F, 0x31, 0x8F, 0x8B, 0x 439 0x47, 0x18, 0x87, 0x23, 0x6D, 0xDD, 0x 440 0xD6, 0x4E, 0x3E, 0x2D, 0x69, 0xF9, 0x 441 0x2A, 0x4F, 0xCE, 0xF2, 0xCB, 0x65, 0x 442 0xFC, 0x78, 0x97, 0x5C, 0x05, 0x58, 0x 443 0xAC, 0x8D, 0x7F, 0xE5, 0xD5, 0x98, 0x 444 0x4B, 0x67, 0x0E, 0x7F, 0xA7, 0x05, 0x 445 0x28, 0xAF, 0x14, 0x63, 0x3F, 0xB6, 0x 446 0x88, 0xF5, 0x3C, 0xB7, 0x4C, 0x3C, 0x 447 0xB8, 0xCE, 0xDA, 0xE9, 0xB0, 0x68, 0x 448 0x55, 0xE0, 0x1F, 0x4D, 0x8A, 0x43, 0x 449 0x57, 0x29, 0xC7, 0x2E, 0x8D, 0xAC, 0x 450 0xB7, 0x59, 0xC4, 0xA8, 0x9F, 0x0A, 0x 451 0x7E, 0x6E, 0x15, 0x47, 0x22, 0xDF, 0x 452 0x58, 0x35, 0x07, 0x6A, 0x99, 0xCF, 0x 453 0x6E, 0x22, 0x50, 0xC9, 0xDE, 0xC0, 0x 454 0x65, 0x89, 0xBC, 0xD4, 0xDB, 0xED, 0x 455 0xC8, 0x12, 0xA8, 0xA2, 0x2B, 0x0D, 0x 456 0xDC, 0xBB, 0xFE, 0x02, 0x32, 0x2F, 0x 457 0xCA, 0xD7, 0x10, 0x61, 0x21, 0x1E, 0x 458 0xD3, 0x50, 0x5D, 0x04, 0x0F, 0xF6, 0x 459 0x6F, 0x16, 0x9D, 0x25, 0x36, 0x86, 0x 460 0x4A, 0x55, 0x5E, 0x09, 0xC1, 0xBE, 0x 461 }; 462 463 /* Macro to perform one column of the RS matri 464 * parameters a, b, c, and d are the four byte 465 * of the key bytes, and w, x, y, and z, are t 466 * the RS matrix, preprocessed through the pol 467 468 #define CALC_S(a, b, c, d, i, w, x, y, z) \ 469 if (key[i]) { \ 470 tmp = poly_to_exp[key[i] - 1]; \ 471 (a) ^= exp_to_poly[tmp + (w)]; \ 472 (b) ^= exp_to_poly[tmp + (x)]; \ 473 (c) ^= exp_to_poly[tmp + (y)]; \ 474 (d) ^= exp_to_poly[tmp + (z)]; \ 475 } 476 477 /* Macros to calculate the key-dependent S-box 478 * the S vector from CALC_S. CALC_SB_2 comput 479 * four S-boxes, where i is the index of the e 480 * are the index numbers preprocessed through 481 * respectively. */ 482 483 #define CALC_SB_2(i, a, b) \ 484 ctx->s[0][i] = mds[0][q0[(a) ^ sa] ^ se]; \ 485 ctx->s[1][i] = mds[1][q0[(b) ^ sb] ^ sf]; \ 486 ctx->s[2][i] = mds[2][q1[(a) ^ sc] ^ sg]; \ 487 ctx->s[3][i] = mds[3][q1[(b) ^ sd] ^ sh] 488 489 /* Macro exactly like CALC_SB_2, but for 192-b 490 491 #define CALC_SB192_2(i, a, b) \ 492 ctx->s[0][i] = mds[0][q0[q0[(b) ^ sa] ^ se] 493 ctx->s[1][i] = mds[1][q0[q1[(b) ^ sb] ^ sf] 494 ctx->s[2][i] = mds[2][q1[q0[(a) ^ sc] ^ sg] 495 ctx->s[3][i] = mds[3][q1[q1[(a) ^ sd] ^ sh] 496 497 /* Macro exactly like CALC_SB_2, but for 256-b 498 499 #define CALC_SB256_2(i, a, b) \ 500 ctx->s[0][i] = mds[0][q0[q0[q1[(b) ^ sa] ^ 501 ctx->s[1][i] = mds[1][q0[q1[q1[(a) ^ sb] ^ 502 ctx->s[2][i] = mds[2][q1[q0[q0[(a) ^ sc] ^ 503 ctx->s[3][i] = mds[3][q1[q1[q0[(b) ^ sd] ^ 504 505 /* Macros to calculate the whitening and round 506 * last two stages of the h() function for a g 507 * a, b, c, and d are the four bytes going int 508 * 128-bit keys, this is the entire h() functi 509 * preprocessed through q0 and q1 respectively 510 * output of previous stages. j is the index 511 * CALC_K computes a pair of subkeys for 128-b 512 * twice, doing the Pseudo-Hadamard Transform, 513 * rotations. Its parameters are: a, the arra 514 * j, the index of the first output entry, k a 515 * for index 2i, and m and n, the preprocessed 516 * CALC_K192_2 expands CALC_K_2 to handle 192- 517 * additional lookup-and-XOR stage. The param 518 * four bytes going into the last three stages 519 * are the index preprocessed through q0, and 520 * preprocessed through q1; j is the index of 521 * CALC_K192 is identical to CALC_K but for us 522 * instead of CALC_K_2. 523 * CALC_K256_2 expands CALC_K192_2 to handle 2 524 * additional lookup-and-XOR stage. The param 525 * preprocessed through q0 and q1 respectively 526 * key byte to use. CALC_K256 is identical to 527 * CALC_K256_2 macro instead of CALC_K_2. */ 528 529 #define CALC_K_2(a, b, c, d, j) \ 530 mds[0][q0[a ^ key[(j) + 8]] ^ key[j]] \ 531 ^ mds[1][q0[b ^ key[(j) + 9]] ^ key[(j) + 1 532 ^ mds[2][q1[c ^ key[(j) + 10]] ^ key[(j) + 533 ^ mds[3][q1[d ^ key[(j) + 11]] ^ key[(j) + 534 535 #define CALC_K(a, j, k, l, m, n) \ 536 x = CALC_K_2 (k, l, k, l, 0); \ 537 y = CALC_K_2 (m, n, m, n, 4); \ 538 y = rol32(y, 8); \ 539 x += y; y += x; ctx->a[j] = x; \ 540 ctx->a[(j) + 1] = rol32(y, 9) 541 542 #define CALC_K192_2(a, b, c, d, j) \ 543 CALC_K_2 (q0[a ^ key[(j) + 16]], \ 544 q1[b ^ key[(j) + 17]], \ 545 q0[c ^ key[(j) + 18]], \ 546 q1[d ^ key[(j) + 19]], j) 547 548 #define CALC_K192(a, j, k, l, m, n) \ 549 x = CALC_K192_2 (l, l, k, k, 0); \ 550 y = CALC_K192_2 (n, n, m, m, 4); \ 551 y = rol32(y, 8); \ 552 x += y; y += x; ctx->a[j] = x; \ 553 ctx->a[(j) + 1] = rol32(y, 9) 554 555 #define CALC_K256_2(a, b, j) \ 556 CALC_K192_2 (q1[b ^ key[(j) + 24]], \ 557 q1[a ^ key[(j) + 25]], \ 558 q0[a ^ key[(j) + 26]], \ 559 q0[b ^ key[(j) + 27]], j) 560 561 #define CALC_K256(a, j, k, l, m, n) \ 562 x = CALC_K256_2 (k, l, 0); \ 563 y = CALC_K256_2 (m, n, 4); \ 564 y = rol32(y, 8); \ 565 x += y; y += x; ctx->a[j] = x; \ 566 ctx->a[(j) + 1] = rol32(y, 9) 567 568 /* Perform the key setup. */ 569 int __twofish_setkey(struct twofish_ctx *ctx, 570 unsigned int key_len) 571 { 572 int i, j, k; 573 574 /* Temporaries for CALC_K. */ 575 u32 x, y; 576 577 /* The S vector used to key the S-boxe 578 * 128-bit keys use only sa through sh 579 u8 sa = 0, sb = 0, sc = 0, sd = 0, se 580 u8 si = 0, sj = 0, sk = 0, sl = 0, sm 581 582 /* Temporary for CALC_S. */ 583 u8 tmp; 584 585 /* Check key length. */ 586 if (key_len % 8) 587 return -EINVAL; /* unsupported 588 589 /* Compute the first two words of the 590 * the entries of the RS matrix, prepr 591 * numbers in the comments are the ori 592 * entries. */ 593 CALC_S (sa, sb, sc, sd, 0, 0x00, 0x2D, 594 CALC_S (sa, sb, sc, sd, 1, 0x2D, 0xA4, 595 CALC_S (sa, sb, sc, sd, 2, 0x8A, 0xD5, 596 CALC_S (sa, sb, sc, sd, 3, 0xD1, 0x7F, 597 CALC_S (sa, sb, sc, sd, 4, 0x99, 0x46, 598 CALC_S (sa, sb, sc, sd, 5, 0x96, 0x3C, 599 CALC_S (sa, sb, sc, sd, 6, 0xED, 0x37, 600 CALC_S (sa, sb, sc, sd, 7, 0xE0, 0xD0, 601 CALC_S (se, sf, sg, sh, 8, 0x00, 0x2D, 602 CALC_S (se, sf, sg, sh, 9, 0x2D, 0xA4, 603 CALC_S (se, sf, sg, sh, 10, 0x8A, 0xD5 604 CALC_S (se, sf, sg, sh, 11, 0xD1, 0x7F 605 CALC_S (se, sf, sg, sh, 12, 0x99, 0x46 606 CALC_S (se, sf, sg, sh, 13, 0x96, 0x3C 607 CALC_S (se, sf, sg, sh, 14, 0xED, 0x37 608 CALC_S (se, sf, sg, sh, 15, 0xE0, 0xD0 609 610 if (key_len == 24 || key_len == 32) { 611 /* Calculate the third word of 612 CALC_S (si, sj, sk, sl, 16, 0x 613 CALC_S (si, sj, sk, sl, 17, 0x 614 CALC_S (si, sj, sk, sl, 18, 0x 615 CALC_S (si, sj, sk, sl, 19, 0x 616 CALC_S (si, sj, sk, sl, 20, 0x 617 CALC_S (si, sj, sk, sl, 21, 0x 618 CALC_S (si, sj, sk, sl, 22, 0x 619 CALC_S (si, sj, sk, sl, 23, 0x 620 } 621 622 if (key_len == 32) { /* 256-bit key */ 623 /* Calculate the fourth word o 624 CALC_S (sm, sn, so, sp, 24, 0x 625 CALC_S (sm, sn, so, sp, 25, 0x 626 CALC_S (sm, sn, so, sp, 26, 0x 627 CALC_S (sm, sn, so, sp, 27, 0x 628 CALC_S (sm, sn, so, sp, 28, 0x 629 CALC_S (sm, sn, so, sp, 29, 0x 630 CALC_S (sm, sn, so, sp, 30, 0x 631 CALC_S (sm, sn, so, sp, 31, 0x 632 633 /* Compute the S-boxes. */ 634 for ( i = j = 0, k = 1; i < 25 635 CALC_SB256_2( i, calc_ 636 } 637 638 /* CALC_K256/CALC_K192/CALC_K 639 * Unrolling produced x2.5 mor 640 * and speeded up key setup by 641 * unrolled: twofish_setkey/se 642 * loop: twofish_setkey/se 643 * CALC_K256: ~100 insns each 644 * CALC_K192: ~90 insns 645 * CALC_K: ~70 insns 646 */ 647 /* Calculate whitening and rou 648 for ( i = 0; i < 8; i += 2 ) { 649 CALC_K256 (w, i, q0[i] 650 } 651 for ( i = 0; i < 32; i += 2 ) 652 CALC_K256 (k, i, q0[i+ 653 } 654 } else if (key_len == 24) { /* 192-bit 655 /* Compute the S-boxes. */ 656 for ( i = j = 0, k = 1; i < 25 657 CALC_SB192_2( i, calc_ 658 } 659 660 /* Calculate whitening and rou 661 for ( i = 0; i < 8; i += 2 ) { 662 CALC_K192 (w, i, q0[i] 663 } 664 for ( i = 0; i < 32; i += 2 ) 665 CALC_K192 (k, i, q0[i+ 666 } 667 } else { /* 128-bit key */ 668 /* Compute the S-boxes. */ 669 for ( i = j = 0, k = 1; i < 25 670 CALC_SB_2( i, calc_sb_ 671 } 672 673 /* Calculate whitening and rou 674 for ( i = 0; i < 8; i += 2 ) { 675 CALC_K (w, i, q0[i], q 676 } 677 for ( i = 0; i < 32; i += 2 ) 678 CALC_K (k, i, q0[i+8], 679 } 680 } 681 682 return 0; 683 } 684 EXPORT_SYMBOL_GPL(__twofish_setkey); 685 686 int twofish_setkey(struct crypto_tfm *tfm, con 687 { 688 return __twofish_setkey(crypto_tfm_ctx 689 } 690 EXPORT_SYMBOL_GPL(twofish_setkey); 691 692 MODULE_LICENSE("GPL"); 693 MODULE_DESCRIPTION("Twofish cipher common func 694
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.