1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Cryptographic API. 4 * 5 * Serpent Cipher Algorithm. 6 * 7 * Copyright (C) 2002 Dag Arne Osvik <osvik@ii.uib.no> 8 * 2003 Herbert Valerio Riedel <hvr@gnu.org> 9 * 10 * Added tnepres support: 11 * Ruben Jesus Garcia Hernandez <ruben@ugr.es>, 18.10.2004 12 * Based on code by hvr 13 */ 14 15 #include <linux/init.h> 16 #include <linux/module.h> 17 #include <linux/errno.h> 18 #include <asm/byteorder.h> 19 #include <linux/crypto.h> 20 #include <linux/types.h> 21 #include <crypto/serpent.h> 22 23 /* Key is padded to the maximum of 256 bits before round key generation. 24 * Any key length <= 256 bits (32 bytes) is allowed by the algorithm. 25 */ 26 27 #define PHI 0x9e3779b9UL 28 29 #define keyiter(a, b, c, d, i, j) \ 30 ({ b ^= d; b ^= c; b ^= a; b ^= PHI ^ i; b = rol32(b, 11); k[j] = b; }) 31 32 #define loadkeys(x0, x1, x2, x3, i) \ 33 ({ x0 = k[i]; x1 = k[i+1]; x2 = k[i+2]; x3 = k[i+3]; }) 34 35 #define storekeys(x0, x1, x2, x3, i) \ 36 ({ k[i] = x0; k[i+1] = x1; k[i+2] = x2; k[i+3] = x3; }) 37 38 #define store_and_load_keys(x0, x1, x2, x3, s, l) \ 39 ({ storekeys(x0, x1, x2, x3, s); loadkeys(x0, x1, x2, x3, l); }) 40 41 #define K(x0, x1, x2, x3, i) ({ \ 42 x3 ^= k[4*(i)+3]; x2 ^= k[4*(i)+2]; \ 43 x1 ^= k[4*(i)+1]; x0 ^= k[4*(i)+0]; \ 44 }) 45 46 #define LK(x0, x1, x2, x3, x4, i) ({ \ 47 x0 = rol32(x0, 13);\ 48 x2 = rol32(x2, 3); x1 ^= x0; x4 = x0 << 3; \ 49 x3 ^= x2; x1 ^= x2; \ 50 x1 = rol32(x1, 1); x3 ^= x4; \ 51 x3 = rol32(x3, 7); x4 = x1; \ 52 x0 ^= x1; x4 <<= 7; x2 ^= x3; \ 53 x0 ^= x3; x2 ^= x4; x3 ^= k[4*i+3]; \ 54 x1 ^= k[4*i+1]; x0 = rol32(x0, 5); x2 = rol32(x2, 22);\ 55 x0 ^= k[4*i+0]; x2 ^= k[4*i+2]; \ 56 }) 57 58 #define KL(x0, x1, x2, x3, x4, i) ({ \ 59 x0 ^= k[4*i+0]; x1 ^= k[4*i+1]; x2 ^= k[4*i+2]; \ 60 x3 ^= k[4*i+3]; x0 = ror32(x0, 5); x2 = ror32(x2, 22);\ 61 x4 = x1; x2 ^= x3; x0 ^= x3; \ 62 x4 <<= 7; x0 ^= x1; x1 = ror32(x1, 1); \ 63 x2 ^= x4; x3 = ror32(x3, 7); x4 = x0 << 3; \ 64 x1 ^= x0; x3 ^= x4; x0 = ror32(x0, 13);\ 65 x1 ^= x2; x3 ^= x2; x2 = ror32(x2, 3); \ 66 }) 67 68 #define S0(x0, x1, x2, x3, x4) ({ \ 69 x4 = x3; \ 70 x3 |= x0; x0 ^= x4; x4 ^= x2; \ 71 x4 = ~x4; x3 ^= x1; x1 &= x0; \ 72 x1 ^= x4; x2 ^= x0; x0 ^= x3; \ 73 x4 |= x0; x0 ^= x2; x2 &= x1; \ 74 x3 ^= x2; x1 = ~x1; x2 ^= x4; \ 75 x1 ^= x2; \ 76 }) 77 78 #define S1(x0, x1, x2, x3, x4) ({ \ 79 x4 = x1; \ 80 x1 ^= x0; x0 ^= x3; x3 = ~x3; \ 81 x4 &= x1; x0 |= x1; x3 ^= x2; \ 82 x0 ^= x3; x1 ^= x3; x3 ^= x4; \ 83 x1 |= x4; x4 ^= x2; x2 &= x0; \ 84 x2 ^= x1; x1 |= x0; x0 = ~x0; \ 85 x0 ^= x2; x4 ^= x1; \ 86 }) 87 88 #define S2(x0, x1, x2, x3, x4) ({ \ 89 x3 = ~x3; \ 90 x1 ^= x0; x4 = x0; x0 &= x2; \ 91 x0 ^= x3; x3 |= x4; x2 ^= x1; \ 92 x3 ^= x1; x1 &= x0; x0 ^= x2; \ 93 x2 &= x3; x3 |= x1; x0 = ~x0; \ 94 x3 ^= x0; x4 ^= x0; x0 ^= x2; \ 95 x1 |= x2; \ 96 }) 97 98 #define S3(x0, x1, x2, x3, x4) ({ \ 99 x4 = x1; \ 100 x1 ^= x3; x3 |= x0; x4 &= x0; \ 101 x0 ^= x2; x2 ^= x1; x1 &= x3; \ 102 x2 ^= x3; x0 |= x4; x4 ^= x3; \ 103 x1 ^= x0; x0 &= x3; x3 &= x4; \ 104 x3 ^= x2; x4 |= x1; x2 &= x1; \ 105 x4 ^= x3; x0 ^= x3; x3 ^= x2; \ 106 }) 107 108 #define S4(x0, x1, x2, x3, x4) ({ \ 109 x4 = x3; \ 110 x3 &= x0; x0 ^= x4; \ 111 x3 ^= x2; x2 |= x4; x0 ^= x1; \ 112 x4 ^= x3; x2 |= x0; \ 113 x2 ^= x1; x1 &= x0; \ 114 x1 ^= x4; x4 &= x2; x2 ^= x3; \ 115 x4 ^= x0; x3 |= x1; x1 = ~x1; \ 116 x3 ^= x0; \ 117 }) 118 119 #define S5(x0, x1, x2, x3, x4) ({ \ 120 x4 = x1; x1 |= x0; \ 121 x2 ^= x1; x3 = ~x3; x4 ^= x0; \ 122 x0 ^= x2; x1 &= x4; x4 |= x3; \ 123 x4 ^= x0; x0 &= x3; x1 ^= x3; \ 124 x3 ^= x2; x0 ^= x1; x2 &= x4; \ 125 x1 ^= x2; x2 &= x0; \ 126 x3 ^= x2; \ 127 }) 128 129 #define S6(x0, x1, x2, x3, x4) ({ \ 130 x4 = x1; \ 131 x3 ^= x0; x1 ^= x2; x2 ^= x0; \ 132 x0 &= x3; x1 |= x3; x4 = ~x4; \ 133 x0 ^= x1; x1 ^= x2; \ 134 x3 ^= x4; x4 ^= x0; x2 &= x0; \ 135 x4 ^= x1; x2 ^= x3; x3 &= x1; \ 136 x3 ^= x0; x1 ^= x2; \ 137 }) 138 139 #define S7(x0, x1, x2, x3, x4) ({ \ 140 x1 = ~x1; \ 141 x4 = x1; x0 = ~x0; x1 &= x2; \ 142 x1 ^= x3; x3 |= x4; x4 ^= x2; \ 143 x2 ^= x3; x3 ^= x0; x0 |= x1; \ 144 x2 &= x0; x0 ^= x4; x4 ^= x3; \ 145 x3 &= x0; x4 ^= x1; \ 146 x2 ^= x4; x3 ^= x1; x4 |= x0; \ 147 x4 ^= x1; \ 148 }) 149 150 #define SI0(x0, x1, x2, x3, x4) ({ \ 151 x4 = x3; x1 ^= x0; \ 152 x3 |= x1; x4 ^= x1; x0 = ~x0; \ 153 x2 ^= x3; x3 ^= x0; x0 &= x1; \ 154 x0 ^= x2; x2 &= x3; x3 ^= x4; \ 155 x2 ^= x3; x1 ^= x3; x3 &= x0; \ 156 x1 ^= x0; x0 ^= x2; x4 ^= x3; \ 157 }) 158 159 #define SI1(x0, x1, x2, x3, x4) ({ \ 160 x1 ^= x3; x4 = x0; \ 161 x0 ^= x2; x2 = ~x2; x4 |= x1; \ 162 x4 ^= x3; x3 &= x1; x1 ^= x2; \ 163 x2 &= x4; x4 ^= x1; x1 |= x3; \ 164 x3 ^= x0; x2 ^= x0; x0 |= x4; \ 165 x2 ^= x4; x1 ^= x0; \ 166 x4 ^= x1; \ 167 }) 168 169 #define SI2(x0, x1, x2, x3, x4) ({ \ 170 x2 ^= x1; x4 = x3; x3 = ~x3; \ 171 x3 |= x2; x2 ^= x4; x4 ^= x0; \ 172 x3 ^= x1; x1 |= x2; x2 ^= x0; \ 173 x1 ^= x4; x4 |= x3; x2 ^= x3; \ 174 x4 ^= x2; x2 &= x1; \ 175 x2 ^= x3; x3 ^= x4; x4 ^= x0; \ 176 }) 177 178 #define SI3(x0, x1, x2, x3, x4) ({ \ 179 x2 ^= x1; \ 180 x4 = x1; x1 &= x2; \ 181 x1 ^= x0; x0 |= x4; x4 ^= x3; \ 182 x0 ^= x3; x3 |= x1; x1 ^= x2; \ 183 x1 ^= x3; x0 ^= x2; x2 ^= x3; \ 184 x3 &= x1; x1 ^= x0; x0 &= x2; \ 185 x4 ^= x3; x3 ^= x0; x0 ^= x1; \ 186 }) 187 188 #define SI4(x0, x1, x2, x3, x4) ({ \ 189 x2 ^= x3; x4 = x0; x0 &= x1; \ 190 x0 ^= x2; x2 |= x3; x4 = ~x4; \ 191 x1 ^= x0; x0 ^= x2; x2 &= x4; \ 192 x2 ^= x0; x0 |= x4; \ 193 x0 ^= x3; x3 &= x2; \ 194 x4 ^= x3; x3 ^= x1; x1 &= x0; \ 195 x4 ^= x1; x0 ^= x3; \ 196 }) 197 198 #define SI5(x0, x1, x2, x3, x4) ({ \ 199 x4 = x1; x1 |= x2; \ 200 x2 ^= x4; x1 ^= x3; x3 &= x4; \ 201 x2 ^= x3; x3 |= x0; x0 = ~x0; \ 202 x3 ^= x2; x2 |= x0; x4 ^= x1; \ 203 x2 ^= x4; x4 &= x0; x0 ^= x1; \ 204 x1 ^= x3; x0 &= x2; x2 ^= x3; \ 205 x0 ^= x2; x2 ^= x4; x4 ^= x3; \ 206 }) 207 208 #define SI6(x0, x1, x2, x3, x4) ({ \ 209 x0 ^= x2; \ 210 x4 = x0; x0 &= x3; x2 ^= x3; \ 211 x0 ^= x2; x3 ^= x1; x2 |= x4; \ 212 x2 ^= x3; x3 &= x0; x0 = ~x0; \ 213 x3 ^= x1; x1 &= x2; x4 ^= x0; \ 214 x3 ^= x4; x4 ^= x2; x0 ^= x1; \ 215 x2 ^= x0; \ 216 }) 217 218 #define SI7(x0, x1, x2, x3, x4) ({ \ 219 x4 = x3; x3 &= x0; x0 ^= x2; \ 220 x2 |= x4; x4 ^= x1; x0 = ~x0; \ 221 x1 |= x3; x4 ^= x0; x0 &= x2; \ 222 x0 ^= x1; x1 &= x2; x3 ^= x2; \ 223 x4 ^= x3; x2 &= x3; x3 |= x0; \ 224 x1 ^= x4; x3 ^= x4; x4 &= x0; \ 225 x4 ^= x2; \ 226 }) 227 228 static void __serpent_setkey_sbox(u32 r0, u32 r1, u32 r2, u32 r3, u32 r4, u32 *k) 229 { 230 k += 100; 231 S3(r3, r4, r0, r1, r2); store_and_load_keys(r1, r2, r4, r3, 28, 24); 232 S4(r1, r2, r4, r3, r0); store_and_load_keys(r2, r4, r3, r0, 24, 20); 233 S5(r2, r4, r3, r0, r1); store_and_load_keys(r1, r2, r4, r0, 20, 16); 234 S6(r1, r2, r4, r0, r3); store_and_load_keys(r4, r3, r2, r0, 16, 12); 235 S7(r4, r3, r2, r0, r1); store_and_load_keys(r1, r2, r0, r4, 12, 8); 236 S0(r1, r2, r0, r4, r3); store_and_load_keys(r0, r2, r4, r1, 8, 4); 237 S1(r0, r2, r4, r1, r3); store_and_load_keys(r3, r4, r1, r0, 4, 0); 238 S2(r3, r4, r1, r0, r2); store_and_load_keys(r2, r4, r3, r0, 0, -4); 239 S3(r2, r4, r3, r0, r1); store_and_load_keys(r0, r1, r4, r2, -4, -8); 240 S4(r0, r1, r4, r2, r3); store_and_load_keys(r1, r4, r2, r3, -8, -12); 241 S5(r1, r4, r2, r3, r0); store_and_load_keys(r0, r1, r4, r3, -12, -16); 242 S6(r0, r1, r4, r3, r2); store_and_load_keys(r4, r2, r1, r3, -16, -20); 243 S7(r4, r2, r1, r3, r0); store_and_load_keys(r0, r1, r3, r4, -20, -24); 244 S0(r0, r1, r3, r4, r2); store_and_load_keys(r3, r1, r4, r0, -24, -28); 245 k -= 50; 246 S1(r3, r1, r4, r0, r2); store_and_load_keys(r2, r4, r0, r3, 22, 18); 247 S2(r2, r4, r0, r3, r1); store_and_load_keys(r1, r4, r2, r3, 18, 14); 248 S3(r1, r4, r2, r3, r0); store_and_load_keys(r3, r0, r4, r1, 14, 10); 249 S4(r3, r0, r4, r1, r2); store_and_load_keys(r0, r4, r1, r2, 10, 6); 250 S5(r0, r4, r1, r2, r3); store_and_load_keys(r3, r0, r4, r2, 6, 2); 251 S6(r3, r0, r4, r2, r1); store_and_load_keys(r4, r1, r0, r2, 2, -2); 252 S7(r4, r1, r0, r2, r3); store_and_load_keys(r3, r0, r2, r4, -2, -6); 253 S0(r3, r0, r2, r4, r1); store_and_load_keys(r2, r0, r4, r3, -6, -10); 254 S1(r2, r0, r4, r3, r1); store_and_load_keys(r1, r4, r3, r2, -10, -14); 255 S2(r1, r4, r3, r2, r0); store_and_load_keys(r0, r4, r1, r2, -14, -18); 256 S3(r0, r4, r1, r2, r3); store_and_load_keys(r2, r3, r4, r0, -18, -22); 257 k -= 50; 258 S4(r2, r3, r4, r0, r1); store_and_load_keys(r3, r4, r0, r1, 28, 24); 259 S5(r3, r4, r0, r1, r2); store_and_load_keys(r2, r3, r4, r1, 24, 20); 260 S6(r2, r3, r4, r1, r0); store_and_load_keys(r4, r0, r3, r1, 20, 16); 261 S7(r4, r0, r3, r1, r2); store_and_load_keys(r2, r3, r1, r4, 16, 12); 262 S0(r2, r3, r1, r4, r0); store_and_load_keys(r1, r3, r4, r2, 12, 8); 263 S1(r1, r3, r4, r2, r0); store_and_load_keys(r0, r4, r2, r1, 8, 4); 264 S2(r0, r4, r2, r1, r3); store_and_load_keys(r3, r4, r0, r1, 4, 0); 265 S3(r3, r4, r0, r1, r2); storekeys(r1, r2, r4, r3, 0); 266 } 267 268 int __serpent_setkey(struct serpent_ctx *ctx, const u8 *key, 269 unsigned int keylen) 270 { 271 u32 *k = ctx->expkey; 272 u8 *k8 = (u8 *)k; 273 u32 r0, r1, r2, r3, r4; 274 int i; 275 276 /* Copy key, add padding */ 277 278 for (i = 0; i < keylen; ++i) 279 k8[i] = key[i]; 280 if (i < SERPENT_MAX_KEY_SIZE) 281 k8[i++] = 1; 282 while (i < SERPENT_MAX_KEY_SIZE) 283 k8[i++] = 0; 284 285 /* Expand key using polynomial */ 286 287 r0 = le32_to_cpu(k[3]); 288 r1 = le32_to_cpu(k[4]); 289 r2 = le32_to_cpu(k[5]); 290 r3 = le32_to_cpu(k[6]); 291 r4 = le32_to_cpu(k[7]); 292 293 keyiter(le32_to_cpu(k[0]), r0, r4, r2, 0, 0); 294 keyiter(le32_to_cpu(k[1]), r1, r0, r3, 1, 1); 295 keyiter(le32_to_cpu(k[2]), r2, r1, r4, 2, 2); 296 keyiter(le32_to_cpu(k[3]), r3, r2, r0, 3, 3); 297 keyiter(le32_to_cpu(k[4]), r4, r3, r1, 4, 4); 298 keyiter(le32_to_cpu(k[5]), r0, r4, r2, 5, 5); 299 keyiter(le32_to_cpu(k[6]), r1, r0, r3, 6, 6); 300 keyiter(le32_to_cpu(k[7]), r2, r1, r4, 7, 7); 301 302 keyiter(k[0], r3, r2, r0, 8, 8); 303 keyiter(k[1], r4, r3, r1, 9, 9); 304 keyiter(k[2], r0, r4, r2, 10, 10); 305 keyiter(k[3], r1, r0, r3, 11, 11); 306 keyiter(k[4], r2, r1, r4, 12, 12); 307 keyiter(k[5], r3, r2, r0, 13, 13); 308 keyiter(k[6], r4, r3, r1, 14, 14); 309 keyiter(k[7], r0, r4, r2, 15, 15); 310 keyiter(k[8], r1, r0, r3, 16, 16); 311 keyiter(k[9], r2, r1, r4, 17, 17); 312 keyiter(k[10], r3, r2, r0, 18, 18); 313 keyiter(k[11], r4, r3, r1, 19, 19); 314 keyiter(k[12], r0, r4, r2, 20, 20); 315 keyiter(k[13], r1, r0, r3, 21, 21); 316 keyiter(k[14], r2, r1, r4, 22, 22); 317 keyiter(k[15], r3, r2, r0, 23, 23); 318 keyiter(k[16], r4, r3, r1, 24, 24); 319 keyiter(k[17], r0, r4, r2, 25, 25); 320 keyiter(k[18], r1, r0, r3, 26, 26); 321 keyiter(k[19], r2, r1, r4, 27, 27); 322 keyiter(k[20], r3, r2, r0, 28, 28); 323 keyiter(k[21], r4, r3, r1, 29, 29); 324 keyiter(k[22], r0, r4, r2, 30, 30); 325 keyiter(k[23], r1, r0, r3, 31, 31); 326 327 k += 50; 328 329 keyiter(k[-26], r2, r1, r4, 32, -18); 330 keyiter(k[-25], r3, r2, r0, 33, -17); 331 keyiter(k[-24], r4, r3, r1, 34, -16); 332 keyiter(k[-23], r0, r4, r2, 35, -15); 333 keyiter(k[-22], r1, r0, r3, 36, -14); 334 keyiter(k[-21], r2, r1, r4, 37, -13); 335 keyiter(k[-20], r3, r2, r0, 38, -12); 336 keyiter(k[-19], r4, r3, r1, 39, -11); 337 keyiter(k[-18], r0, r4, r2, 40, -10); 338 keyiter(k[-17], r1, r0, r3, 41, -9); 339 keyiter(k[-16], r2, r1, r4, 42, -8); 340 keyiter(k[-15], r3, r2, r0, 43, -7); 341 keyiter(k[-14], r4, r3, r1, 44, -6); 342 keyiter(k[-13], r0, r4, r2, 45, -5); 343 keyiter(k[-12], r1, r0, r3, 46, -4); 344 keyiter(k[-11], r2, r1, r4, 47, -3); 345 keyiter(k[-10], r3, r2, r0, 48, -2); 346 keyiter(k[-9], r4, r3, r1, 49, -1); 347 keyiter(k[-8], r0, r4, r2, 50, 0); 348 keyiter(k[-7], r1, r0, r3, 51, 1); 349 keyiter(k[-6], r2, r1, r4, 52, 2); 350 keyiter(k[-5], r3, r2, r0, 53, 3); 351 keyiter(k[-4], r4, r3, r1, 54, 4); 352 keyiter(k[-3], r0, r4, r2, 55, 5); 353 keyiter(k[-2], r1, r0, r3, 56, 6); 354 keyiter(k[-1], r2, r1, r4, 57, 7); 355 keyiter(k[0], r3, r2, r0, 58, 8); 356 keyiter(k[1], r4, r3, r1, 59, 9); 357 keyiter(k[2], r0, r4, r2, 60, 10); 358 keyiter(k[3], r1, r0, r3, 61, 11); 359 keyiter(k[4], r2, r1, r4, 62, 12); 360 keyiter(k[5], r3, r2, r0, 63, 13); 361 keyiter(k[6], r4, r3, r1, 64, 14); 362 keyiter(k[7], r0, r4, r2, 65, 15); 363 keyiter(k[8], r1, r0, r3, 66, 16); 364 keyiter(k[9], r2, r1, r4, 67, 17); 365 keyiter(k[10], r3, r2, r0, 68, 18); 366 keyiter(k[11], r4, r3, r1, 69, 19); 367 keyiter(k[12], r0, r4, r2, 70, 20); 368 keyiter(k[13], r1, r0, r3, 71, 21); 369 keyiter(k[14], r2, r1, r4, 72, 22); 370 keyiter(k[15], r3, r2, r0, 73, 23); 371 keyiter(k[16], r4, r3, r1, 74, 24); 372 keyiter(k[17], r0, r4, r2, 75, 25); 373 keyiter(k[18], r1, r0, r3, 76, 26); 374 keyiter(k[19], r2, r1, r4, 77, 27); 375 keyiter(k[20], r3, r2, r0, 78, 28); 376 keyiter(k[21], r4, r3, r1, 79, 29); 377 keyiter(k[22], r0, r4, r2, 80, 30); 378 keyiter(k[23], r1, r0, r3, 81, 31); 379 380 k += 50; 381 382 keyiter(k[-26], r2, r1, r4, 82, -18); 383 keyiter(k[-25], r3, r2, r0, 83, -17); 384 keyiter(k[-24], r4, r3, r1, 84, -16); 385 keyiter(k[-23], r0, r4, r2, 85, -15); 386 keyiter(k[-22], r1, r0, r3, 86, -14); 387 keyiter(k[-21], r2, r1, r4, 87, -13); 388 keyiter(k[-20], r3, r2, r0, 88, -12); 389 keyiter(k[-19], r4, r3, r1, 89, -11); 390 keyiter(k[-18], r0, r4, r2, 90, -10); 391 keyiter(k[-17], r1, r0, r3, 91, -9); 392 keyiter(k[-16], r2, r1, r4, 92, -8); 393 keyiter(k[-15], r3, r2, r0, 93, -7); 394 keyiter(k[-14], r4, r3, r1, 94, -6); 395 keyiter(k[-13], r0, r4, r2, 95, -5); 396 keyiter(k[-12], r1, r0, r3, 96, -4); 397 keyiter(k[-11], r2, r1, r4, 97, -3); 398 keyiter(k[-10], r3, r2, r0, 98, -2); 399 keyiter(k[-9], r4, r3, r1, 99, -1); 400 keyiter(k[-8], r0, r4, r2, 100, 0); 401 keyiter(k[-7], r1, r0, r3, 101, 1); 402 keyiter(k[-6], r2, r1, r4, 102, 2); 403 keyiter(k[-5], r3, r2, r0, 103, 3); 404 keyiter(k[-4], r4, r3, r1, 104, 4); 405 keyiter(k[-3], r0, r4, r2, 105, 5); 406 keyiter(k[-2], r1, r0, r3, 106, 6); 407 keyiter(k[-1], r2, r1, r4, 107, 7); 408 keyiter(k[0], r3, r2, r0, 108, 8); 409 keyiter(k[1], r4, r3, r1, 109, 9); 410 keyiter(k[2], r0, r4, r2, 110, 10); 411 keyiter(k[3], r1, r0, r3, 111, 11); 412 keyiter(k[4], r2, r1, r4, 112, 12); 413 keyiter(k[5], r3, r2, r0, 113, 13); 414 keyiter(k[6], r4, r3, r1, 114, 14); 415 keyiter(k[7], r0, r4, r2, 115, 15); 416 keyiter(k[8], r1, r0, r3, 116, 16); 417 keyiter(k[9], r2, r1, r4, 117, 17); 418 keyiter(k[10], r3, r2, r0, 118, 18); 419 keyiter(k[11], r4, r3, r1, 119, 19); 420 keyiter(k[12], r0, r4, r2, 120, 20); 421 keyiter(k[13], r1, r0, r3, 121, 21); 422 keyiter(k[14], r2, r1, r4, 122, 22); 423 keyiter(k[15], r3, r2, r0, 123, 23); 424 keyiter(k[16], r4, r3, r1, 124, 24); 425 keyiter(k[17], r0, r4, r2, 125, 25); 426 keyiter(k[18], r1, r0, r3, 126, 26); 427 keyiter(k[19], r2, r1, r4, 127, 27); 428 keyiter(k[20], r3, r2, r0, 128, 28); 429 keyiter(k[21], r4, r3, r1, 129, 29); 430 keyiter(k[22], r0, r4, r2, 130, 30); 431 keyiter(k[23], r1, r0, r3, 131, 31); 432 433 /* Apply S-boxes */ 434 __serpent_setkey_sbox(r0, r1, r2, r3, r4, ctx->expkey); 435 436 return 0; 437 } 438 EXPORT_SYMBOL_GPL(__serpent_setkey); 439 440 int serpent_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen) 441 { 442 return __serpent_setkey(crypto_tfm_ctx(tfm), key, keylen); 443 } 444 EXPORT_SYMBOL_GPL(serpent_setkey); 445 446 void __serpent_encrypt(struct serpent_ctx *ctx, u8 *dst, const u8 *src) 447 { 448 const u32 *k = ctx->expkey; 449 const __le32 *s = (const __le32 *)src; 450 __le32 *d = (__le32 *)dst; 451 u32 r0, r1, r2, r3, r4; 452 453 /* 454 * Note: The conversions between u8* and u32* might cause trouble 455 * on architectures with stricter alignment rules than x86 456 */ 457 458 r0 = le32_to_cpu(s[0]); 459 r1 = le32_to_cpu(s[1]); 460 r2 = le32_to_cpu(s[2]); 461 r3 = le32_to_cpu(s[3]); 462 463 K(r0, r1, r2, r3, 0); 464 S0(r0, r1, r2, r3, r4); LK(r2, r1, r3, r0, r4, 1); 465 S1(r2, r1, r3, r0, r4); LK(r4, r3, r0, r2, r1, 2); 466 S2(r4, r3, r0, r2, r1); LK(r1, r3, r4, r2, r0, 3); 467 S3(r1, r3, r4, r2, r0); LK(r2, r0, r3, r1, r4, 4); 468 S4(r2, r0, r3, r1, r4); LK(r0, r3, r1, r4, r2, 5); 469 S5(r0, r3, r1, r4, r2); LK(r2, r0, r3, r4, r1, 6); 470 S6(r2, r0, r3, r4, r1); LK(r3, r1, r0, r4, r2, 7); 471 S7(r3, r1, r0, r4, r2); LK(r2, r0, r4, r3, r1, 8); 472 S0(r2, r0, r4, r3, r1); LK(r4, r0, r3, r2, r1, 9); 473 S1(r4, r0, r3, r2, r1); LK(r1, r3, r2, r4, r0, 10); 474 S2(r1, r3, r2, r4, r0); LK(r0, r3, r1, r4, r2, 11); 475 S3(r0, r3, r1, r4, r2); LK(r4, r2, r3, r0, r1, 12); 476 S4(r4, r2, r3, r0, r1); LK(r2, r3, r0, r1, r4, 13); 477 S5(r2, r3, r0, r1, r4); LK(r4, r2, r3, r1, r0, 14); 478 S6(r4, r2, r3, r1, r0); LK(r3, r0, r2, r1, r4, 15); 479 S7(r3, r0, r2, r1, r4); LK(r4, r2, r1, r3, r0, 16); 480 S0(r4, r2, r1, r3, r0); LK(r1, r2, r3, r4, r0, 17); 481 S1(r1, r2, r3, r4, r0); LK(r0, r3, r4, r1, r2, 18); 482 S2(r0, r3, r4, r1, r2); LK(r2, r3, r0, r1, r4, 19); 483 S3(r2, r3, r0, r1, r4); LK(r1, r4, r3, r2, r0, 20); 484 S4(r1, r4, r3, r2, r0); LK(r4, r3, r2, r0, r1, 21); 485 S5(r4, r3, r2, r0, r1); LK(r1, r4, r3, r0, r2, 22); 486 S6(r1, r4, r3, r0, r2); LK(r3, r2, r4, r0, r1, 23); 487 S7(r3, r2, r4, r0, r1); LK(r1, r4, r0, r3, r2, 24); 488 S0(r1, r4, r0, r3, r2); LK(r0, r4, r3, r1, r2, 25); 489 S1(r0, r4, r3, r1, r2); LK(r2, r3, r1, r0, r4, 26); 490 S2(r2, r3, r1, r0, r4); LK(r4, r3, r2, r0, r1, 27); 491 S3(r4, r3, r2, r0, r1); LK(r0, r1, r3, r4, r2, 28); 492 S4(r0, r1, r3, r4, r2); LK(r1, r3, r4, r2, r0, 29); 493 S5(r1, r3, r4, r2, r0); LK(r0, r1, r3, r2, r4, 30); 494 S6(r0, r1, r3, r2, r4); LK(r3, r4, r1, r2, r0, 31); 495 S7(r3, r4, r1, r2, r0); K(r0, r1, r2, r3, 32); 496 497 d[0] = cpu_to_le32(r0); 498 d[1] = cpu_to_le32(r1); 499 d[2] = cpu_to_le32(r2); 500 d[3] = cpu_to_le32(r3); 501 } 502 EXPORT_SYMBOL_GPL(__serpent_encrypt); 503 504 static void serpent_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 505 { 506 struct serpent_ctx *ctx = crypto_tfm_ctx(tfm); 507 508 __serpent_encrypt(ctx, dst, src); 509 } 510 511 void __serpent_decrypt(struct serpent_ctx *ctx, u8 *dst, const u8 *src) 512 { 513 const u32 *k = ctx->expkey; 514 const __le32 *s = (const __le32 *)src; 515 __le32 *d = (__le32 *)dst; 516 u32 r0, r1, r2, r3, r4; 517 518 r0 = le32_to_cpu(s[0]); 519 r1 = le32_to_cpu(s[1]); 520 r2 = le32_to_cpu(s[2]); 521 r3 = le32_to_cpu(s[3]); 522 523 K(r0, r1, r2, r3, 32); 524 SI7(r0, r1, r2, r3, r4); KL(r1, r3, r0, r4, r2, 31); 525 SI6(r1, r3, r0, r4, r2); KL(r0, r2, r4, r1, r3, 30); 526 SI5(r0, r2, r4, r1, r3); KL(r2, r3, r0, r4, r1, 29); 527 SI4(r2, r3, r0, r4, r1); KL(r2, r0, r1, r4, r3, 28); 528 SI3(r2, r0, r1, r4, r3); KL(r1, r2, r3, r4, r0, 27); 529 SI2(r1, r2, r3, r4, r0); KL(r2, r0, r4, r3, r1, 26); 530 SI1(r2, r0, r4, r3, r1); KL(r1, r0, r4, r3, r2, 25); 531 SI0(r1, r0, r4, r3, r2); KL(r4, r2, r0, r1, r3, 24); 532 SI7(r4, r2, r0, r1, r3); KL(r2, r1, r4, r3, r0, 23); 533 SI6(r2, r1, r4, r3, r0); KL(r4, r0, r3, r2, r1, 22); 534 SI5(r4, r0, r3, r2, r1); KL(r0, r1, r4, r3, r2, 21); 535 SI4(r0, r1, r4, r3, r2); KL(r0, r4, r2, r3, r1, 20); 536 SI3(r0, r4, r2, r3, r1); KL(r2, r0, r1, r3, r4, 19); 537 SI2(r2, r0, r1, r3, r4); KL(r0, r4, r3, r1, r2, 18); 538 SI1(r0, r4, r3, r1, r2); KL(r2, r4, r3, r1, r0, 17); 539 SI0(r2, r4, r3, r1, r0); KL(r3, r0, r4, r2, r1, 16); 540 SI7(r3, r0, r4, r2, r1); KL(r0, r2, r3, r1, r4, 15); 541 SI6(r0, r2, r3, r1, r4); KL(r3, r4, r1, r0, r2, 14); 542 SI5(r3, r4, r1, r0, r2); KL(r4, r2, r3, r1, r0, 13); 543 SI4(r4, r2, r3, r1, r0); KL(r4, r3, r0, r1, r2, 12); 544 SI3(r4, r3, r0, r1, r2); KL(r0, r4, r2, r1, r3, 11); 545 SI2(r0, r4, r2, r1, r3); KL(r4, r3, r1, r2, r0, 10); 546 SI1(r4, r3, r1, r2, r0); KL(r0, r3, r1, r2, r4, 9); 547 SI0(r0, r3, r1, r2, r4); KL(r1, r4, r3, r0, r2, 8); 548 SI7(r1, r4, r3, r0, r2); KL(r4, r0, r1, r2, r3, 7); 549 SI6(r4, r0, r1, r2, r3); KL(r1, r3, r2, r4, r0, 6); 550 SI5(r1, r3, r2, r4, r0); KL(r3, r0, r1, r2, r4, 5); 551 SI4(r3, r0, r1, r2, r4); KL(r3, r1, r4, r2, r0, 4); 552 SI3(r3, r1, r4, r2, r0); KL(r4, r3, r0, r2, r1, 3); 553 SI2(r4, r3, r0, r2, r1); KL(r3, r1, r2, r0, r4, 2); 554 SI1(r3, r1, r2, r0, r4); KL(r4, r1, r2, r0, r3, 1); 555 SI0(r4, r1, r2, r0, r3); K(r2, r3, r1, r4, 0); 556 557 d[0] = cpu_to_le32(r2); 558 d[1] = cpu_to_le32(r3); 559 d[2] = cpu_to_le32(r1); 560 d[3] = cpu_to_le32(r4); 561 } 562 EXPORT_SYMBOL_GPL(__serpent_decrypt); 563 564 static void serpent_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 565 { 566 struct serpent_ctx *ctx = crypto_tfm_ctx(tfm); 567 568 __serpent_decrypt(ctx, dst, src); 569 } 570 571 static int tnepres_setkey(struct crypto_tfm *tfm, const u8 *key, 572 unsigned int keylen) 573 { 574 u8 rev_key[SERPENT_MAX_KEY_SIZE]; 575 int i; 576 577 for (i = 0; i < keylen; ++i) 578 rev_key[keylen - i - 1] = key[i]; 579 580 return serpent_setkey(tfm, rev_key, keylen); 581 } 582 583 static void tnepres_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 584 { 585 const u32 * const s = (const u32 * const)src; 586 u32 * const d = (u32 * const)dst; 587 588 u32 rs[4], rd[4]; 589 590 rs[0] = swab32(s[3]); 591 rs[1] = swab32(s[2]); 592 rs[2] = swab32(s[1]); 593 rs[3] = swab32(s[0]); 594 595 serpent_encrypt(tfm, (u8 *)rd, (u8 *)rs); 596 597 d[0] = swab32(rd[3]); 598 d[1] = swab32(rd[2]); 599 d[2] = swab32(rd[1]); 600 d[3] = swab32(rd[0]); 601 } 602 603 static void tnepres_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 604 { 605 const u32 * const s = (const u32 * const)src; 606 u32 * const d = (u32 * const)dst; 607 608 u32 rs[4], rd[4]; 609 610 rs[0] = swab32(s[3]); 611 rs[1] = swab32(s[2]); 612 rs[2] = swab32(s[1]); 613 rs[3] = swab32(s[0]); 614 615 serpent_decrypt(tfm, (u8 *)rd, (u8 *)rs); 616 617 d[0] = swab32(rd[3]); 618 d[1] = swab32(rd[2]); 619 d[2] = swab32(rd[1]); 620 d[3] = swab32(rd[0]); 621 } 622 623 static struct crypto_alg srp_algs[2] = { { 624 .cra_name = "serpent", 625 .cra_driver_name = "serpent-generic", 626 .cra_priority = 100, 627 .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 628 .cra_blocksize = SERPENT_BLOCK_SIZE, 629 .cra_ctxsize = sizeof(struct serpent_ctx), 630 .cra_alignmask = 3, 631 .cra_module = THIS_MODULE, 632 .cra_u = { .cipher = { 633 .cia_min_keysize = SERPENT_MIN_KEY_SIZE, 634 .cia_max_keysize = SERPENT_MAX_KEY_SIZE, 635 .cia_setkey = serpent_setkey, 636 .cia_encrypt = serpent_encrypt, 637 .cia_decrypt = serpent_decrypt } } 638 }, { 639 .cra_name = "tnepres", 640 .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 641 .cra_blocksize = SERPENT_BLOCK_SIZE, 642 .cra_ctxsize = sizeof(struct serpent_ctx), 643 .cra_alignmask = 3, 644 .cra_module = THIS_MODULE, 645 .cra_u = { .cipher = { 646 .cia_min_keysize = SERPENT_MIN_KEY_SIZE, 647 .cia_max_keysize = SERPENT_MAX_KEY_SIZE, 648 .cia_setkey = tnepres_setkey, 649 .cia_encrypt = tnepres_encrypt, 650 .cia_decrypt = tnepres_decrypt } } 651 } }; 652 653 static int __init serpent_mod_init(void) 654 { 655 return crypto_register_algs(srp_algs, ARRAY_SIZE(srp_algs)); 656 } 657 658 static void __exit serpent_mod_fini(void) 659 { 660 crypto_unregister_algs(srp_algs, ARRAY_SIZE(srp_algs)); 661 } 662 663 subsys_initcall(serpent_mod_init); 664 module_exit(serpent_mod_fini); 665 666 MODULE_LICENSE("GPL"); 667 MODULE_DESCRIPTION("Serpent and tnepres (kerneli compatible serpent reversed) Cipher Algorithm"); 668 MODULE_AUTHOR("Dag Arne Osvik <osvik@ii.uib.no>"); 669 MODULE_ALIAS_CRYPTO("tnepres"); 670 MODULE_ALIAS_CRYPTO("serpent"); 671 MODULE_ALIAS_CRYPTO("serpent-generic"); 672