1*c5a511f1SSebastian Siewior /* 2*c5a511f1SSebastian Siewior * Cryptographic API. 3*c5a511f1SSebastian Siewior * 4*c5a511f1SSebastian Siewior * DES & Triple DES EDE Cipher Algorithms. 5*c5a511f1SSebastian Siewior * 6*c5a511f1SSebastian Siewior * Copyright (c) 2005 Dag Arne Osvik <da@osvik.no> 7*c5a511f1SSebastian Siewior * 8*c5a511f1SSebastian Siewior * This program is free software; you can redistribute it and/or modify 9*c5a511f1SSebastian Siewior * it under the terms of the GNU General Public License as published by 10*c5a511f1SSebastian Siewior * the Free Software Foundation; either version 2 of the License, or 11*c5a511f1SSebastian Siewior * (at your option) any later version. 12*c5a511f1SSebastian Siewior * 13*c5a511f1SSebastian Siewior */ 14*c5a511f1SSebastian Siewior 15*c5a511f1SSebastian Siewior #include <asm/byteorder.h> 16*c5a511f1SSebastian Siewior #include <linux/bitops.h> 17*c5a511f1SSebastian Siewior #include <linux/init.h> 18*c5a511f1SSebastian Siewior #include <linux/module.h> 19*c5a511f1SSebastian Siewior #include <linux/errno.h> 20*c5a511f1SSebastian Siewior #include <linux/crypto.h> 21*c5a511f1SSebastian Siewior #include <linux/types.h> 22*c5a511f1SSebastian Siewior 23*c5a511f1SSebastian Siewior #define DES_KEY_SIZE 8 24*c5a511f1SSebastian Siewior #define DES_EXPKEY_WORDS 32 25*c5a511f1SSebastian Siewior #define DES_BLOCK_SIZE 8 26*c5a511f1SSebastian Siewior 27*c5a511f1SSebastian Siewior #define DES3_EDE_KEY_SIZE (3 * DES_KEY_SIZE) 28*c5a511f1SSebastian Siewior #define DES3_EDE_EXPKEY_WORDS (3 * DES_EXPKEY_WORDS) 29*c5a511f1SSebastian Siewior #define DES3_EDE_BLOCK_SIZE DES_BLOCK_SIZE 30*c5a511f1SSebastian Siewior 31*c5a511f1SSebastian Siewior #define ROL(x, r) ((x) = rol32((x), (r))) 32*c5a511f1SSebastian Siewior #define ROR(x, r) ((x) = ror32((x), (r))) 33*c5a511f1SSebastian Siewior 34*c5a511f1SSebastian Siewior struct des_ctx { 35*c5a511f1SSebastian Siewior u32 expkey[DES_EXPKEY_WORDS]; 36*c5a511f1SSebastian Siewior }; 37*c5a511f1SSebastian Siewior 38*c5a511f1SSebastian Siewior struct des3_ede_ctx { 39*c5a511f1SSebastian Siewior u32 expkey[DES3_EDE_EXPKEY_WORDS]; 40*c5a511f1SSebastian Siewior }; 41*c5a511f1SSebastian Siewior 42*c5a511f1SSebastian Siewior /* Lookup tables for key expansion */ 43*c5a511f1SSebastian Siewior 44*c5a511f1SSebastian Siewior static const u8 pc1[256] = { 45*c5a511f1SSebastian Siewior 0x00, 0x00, 0x40, 0x04, 0x10, 0x10, 0x50, 0x14, 46*c5a511f1SSebastian Siewior 0x04, 0x40, 0x44, 0x44, 0x14, 0x50, 0x54, 0x54, 47*c5a511f1SSebastian Siewior 0x02, 0x02, 0x42, 0x06, 0x12, 0x12, 0x52, 0x16, 48*c5a511f1SSebastian Siewior 0x06, 0x42, 0x46, 0x46, 0x16, 0x52, 0x56, 0x56, 49*c5a511f1SSebastian Siewior 0x80, 0x08, 0xc0, 0x0c, 0x90, 0x18, 0xd0, 0x1c, 50*c5a511f1SSebastian Siewior 0x84, 0x48, 0xc4, 0x4c, 0x94, 0x58, 0xd4, 0x5c, 51*c5a511f1SSebastian Siewior 0x82, 0x0a, 0xc2, 0x0e, 0x92, 0x1a, 0xd2, 0x1e, 52*c5a511f1SSebastian Siewior 0x86, 0x4a, 0xc6, 0x4e, 0x96, 0x5a, 0xd6, 0x5e, 53*c5a511f1SSebastian Siewior 0x20, 0x20, 0x60, 0x24, 0x30, 0x30, 0x70, 0x34, 54*c5a511f1SSebastian Siewior 0x24, 0x60, 0x64, 0x64, 0x34, 0x70, 0x74, 0x74, 55*c5a511f1SSebastian Siewior 0x22, 0x22, 0x62, 0x26, 0x32, 0x32, 0x72, 0x36, 56*c5a511f1SSebastian Siewior 0x26, 0x62, 0x66, 0x66, 0x36, 0x72, 0x76, 0x76, 57*c5a511f1SSebastian Siewior 0xa0, 0x28, 0xe0, 0x2c, 0xb0, 0x38, 0xf0, 0x3c, 58*c5a511f1SSebastian Siewior 0xa4, 0x68, 0xe4, 0x6c, 0xb4, 0x78, 0xf4, 0x7c, 59*c5a511f1SSebastian Siewior 0xa2, 0x2a, 0xe2, 0x2e, 0xb2, 0x3a, 0xf2, 0x3e, 60*c5a511f1SSebastian Siewior 0xa6, 0x6a, 0xe6, 0x6e, 0xb6, 0x7a, 0xf6, 0x7e, 61*c5a511f1SSebastian Siewior 0x08, 0x80, 0x48, 0x84, 0x18, 0x90, 0x58, 0x94, 62*c5a511f1SSebastian Siewior 0x0c, 0xc0, 0x4c, 0xc4, 0x1c, 0xd0, 0x5c, 0xd4, 63*c5a511f1SSebastian Siewior 0x0a, 0x82, 0x4a, 0x86, 0x1a, 0x92, 0x5a, 0x96, 64*c5a511f1SSebastian Siewior 0x0e, 0xc2, 0x4e, 0xc6, 0x1e, 0xd2, 0x5e, 0xd6, 65*c5a511f1SSebastian Siewior 0x88, 0x88, 0xc8, 0x8c, 0x98, 0x98, 0xd8, 0x9c, 66*c5a511f1SSebastian Siewior 0x8c, 0xc8, 0xcc, 0xcc, 0x9c, 0xd8, 0xdc, 0xdc, 67*c5a511f1SSebastian Siewior 0x8a, 0x8a, 0xca, 0x8e, 0x9a, 0x9a, 0xda, 0x9e, 68*c5a511f1SSebastian Siewior 0x8e, 0xca, 0xce, 0xce, 0x9e, 0xda, 0xde, 0xde, 69*c5a511f1SSebastian Siewior 0x28, 0xa0, 0x68, 0xa4, 0x38, 0xb0, 0x78, 0xb4, 70*c5a511f1SSebastian Siewior 0x2c, 0xe0, 0x6c, 0xe4, 0x3c, 0xf0, 0x7c, 0xf4, 71*c5a511f1SSebastian Siewior 0x2a, 0xa2, 0x6a, 0xa6, 0x3a, 0xb2, 0x7a, 0xb6, 72*c5a511f1SSebastian Siewior 0x2e, 0xe2, 0x6e, 0xe6, 0x3e, 0xf2, 0x7e, 0xf6, 73*c5a511f1SSebastian Siewior 0xa8, 0xa8, 0xe8, 0xac, 0xb8, 0xb8, 0xf8, 0xbc, 74*c5a511f1SSebastian Siewior 0xac, 0xe8, 0xec, 0xec, 0xbc, 0xf8, 0xfc, 0xfc, 75*c5a511f1SSebastian Siewior 0xaa, 0xaa, 0xea, 0xae, 0xba, 0xba, 0xfa, 0xbe, 76*c5a511f1SSebastian Siewior 0xae, 0xea, 0xee, 0xee, 0xbe, 0xfa, 0xfe, 0xfe 77*c5a511f1SSebastian Siewior }; 78*c5a511f1SSebastian Siewior 79*c5a511f1SSebastian Siewior static const u8 rs[256] = { 80*c5a511f1SSebastian Siewior 0x00, 0x00, 0x80, 0x80, 0x02, 0x02, 0x82, 0x82, 81*c5a511f1SSebastian Siewior 0x04, 0x04, 0x84, 0x84, 0x06, 0x06, 0x86, 0x86, 82*c5a511f1SSebastian Siewior 0x08, 0x08, 0x88, 0x88, 0x0a, 0x0a, 0x8a, 0x8a, 83*c5a511f1SSebastian Siewior 0x0c, 0x0c, 0x8c, 0x8c, 0x0e, 0x0e, 0x8e, 0x8e, 84*c5a511f1SSebastian Siewior 0x10, 0x10, 0x90, 0x90, 0x12, 0x12, 0x92, 0x92, 85*c5a511f1SSebastian Siewior 0x14, 0x14, 0x94, 0x94, 0x16, 0x16, 0x96, 0x96, 86*c5a511f1SSebastian Siewior 0x18, 0x18, 0x98, 0x98, 0x1a, 0x1a, 0x9a, 0x9a, 87*c5a511f1SSebastian Siewior 0x1c, 0x1c, 0x9c, 0x9c, 0x1e, 0x1e, 0x9e, 0x9e, 88*c5a511f1SSebastian Siewior 0x20, 0x20, 0xa0, 0xa0, 0x22, 0x22, 0xa2, 0xa2, 89*c5a511f1SSebastian Siewior 0x24, 0x24, 0xa4, 0xa4, 0x26, 0x26, 0xa6, 0xa6, 90*c5a511f1SSebastian Siewior 0x28, 0x28, 0xa8, 0xa8, 0x2a, 0x2a, 0xaa, 0xaa, 91*c5a511f1SSebastian Siewior 0x2c, 0x2c, 0xac, 0xac, 0x2e, 0x2e, 0xae, 0xae, 92*c5a511f1SSebastian Siewior 0x30, 0x30, 0xb0, 0xb0, 0x32, 0x32, 0xb2, 0xb2, 93*c5a511f1SSebastian Siewior 0x34, 0x34, 0xb4, 0xb4, 0x36, 0x36, 0xb6, 0xb6, 94*c5a511f1SSebastian Siewior 0x38, 0x38, 0xb8, 0xb8, 0x3a, 0x3a, 0xba, 0xba, 95*c5a511f1SSebastian Siewior 0x3c, 0x3c, 0xbc, 0xbc, 0x3e, 0x3e, 0xbe, 0xbe, 96*c5a511f1SSebastian Siewior 0x40, 0x40, 0xc0, 0xc0, 0x42, 0x42, 0xc2, 0xc2, 97*c5a511f1SSebastian Siewior 0x44, 0x44, 0xc4, 0xc4, 0x46, 0x46, 0xc6, 0xc6, 98*c5a511f1SSebastian Siewior 0x48, 0x48, 0xc8, 0xc8, 0x4a, 0x4a, 0xca, 0xca, 99*c5a511f1SSebastian Siewior 0x4c, 0x4c, 0xcc, 0xcc, 0x4e, 0x4e, 0xce, 0xce, 100*c5a511f1SSebastian Siewior 0x50, 0x50, 0xd0, 0xd0, 0x52, 0x52, 0xd2, 0xd2, 101*c5a511f1SSebastian Siewior 0x54, 0x54, 0xd4, 0xd4, 0x56, 0x56, 0xd6, 0xd6, 102*c5a511f1SSebastian Siewior 0x58, 0x58, 0xd8, 0xd8, 0x5a, 0x5a, 0xda, 0xda, 103*c5a511f1SSebastian Siewior 0x5c, 0x5c, 0xdc, 0xdc, 0x5e, 0x5e, 0xde, 0xde, 104*c5a511f1SSebastian Siewior 0x60, 0x60, 0xe0, 0xe0, 0x62, 0x62, 0xe2, 0xe2, 105*c5a511f1SSebastian Siewior 0x64, 0x64, 0xe4, 0xe4, 0x66, 0x66, 0xe6, 0xe6, 106*c5a511f1SSebastian Siewior 0x68, 0x68, 0xe8, 0xe8, 0x6a, 0x6a, 0xea, 0xea, 107*c5a511f1SSebastian Siewior 0x6c, 0x6c, 0xec, 0xec, 0x6e, 0x6e, 0xee, 0xee, 108*c5a511f1SSebastian Siewior 0x70, 0x70, 0xf0, 0xf0, 0x72, 0x72, 0xf2, 0xf2, 109*c5a511f1SSebastian Siewior 0x74, 0x74, 0xf4, 0xf4, 0x76, 0x76, 0xf6, 0xf6, 110*c5a511f1SSebastian Siewior 0x78, 0x78, 0xf8, 0xf8, 0x7a, 0x7a, 0xfa, 0xfa, 111*c5a511f1SSebastian Siewior 0x7c, 0x7c, 0xfc, 0xfc, 0x7e, 0x7e, 0xfe, 0xfe 112*c5a511f1SSebastian Siewior }; 113*c5a511f1SSebastian Siewior 114*c5a511f1SSebastian Siewior static const u32 pc2[1024] = { 115*c5a511f1SSebastian Siewior 0x00000000, 0x00000000, 0x00000000, 0x00000000, 116*c5a511f1SSebastian Siewior 0x00040000, 0x00000000, 0x04000000, 0x00100000, 117*c5a511f1SSebastian Siewior 0x00400000, 0x00000008, 0x00000800, 0x40000000, 118*c5a511f1SSebastian Siewior 0x00440000, 0x00000008, 0x04000800, 0x40100000, 119*c5a511f1SSebastian Siewior 0x00000400, 0x00000020, 0x08000000, 0x00000100, 120*c5a511f1SSebastian Siewior 0x00040400, 0x00000020, 0x0c000000, 0x00100100, 121*c5a511f1SSebastian Siewior 0x00400400, 0x00000028, 0x08000800, 0x40000100, 122*c5a511f1SSebastian Siewior 0x00440400, 0x00000028, 0x0c000800, 0x40100100, 123*c5a511f1SSebastian Siewior 0x80000000, 0x00000010, 0x00000000, 0x00800000, 124*c5a511f1SSebastian Siewior 0x80040000, 0x00000010, 0x04000000, 0x00900000, 125*c5a511f1SSebastian Siewior 0x80400000, 0x00000018, 0x00000800, 0x40800000, 126*c5a511f1SSebastian Siewior 0x80440000, 0x00000018, 0x04000800, 0x40900000, 127*c5a511f1SSebastian Siewior 0x80000400, 0x00000030, 0x08000000, 0x00800100, 128*c5a511f1SSebastian Siewior 0x80040400, 0x00000030, 0x0c000000, 0x00900100, 129*c5a511f1SSebastian Siewior 0x80400400, 0x00000038, 0x08000800, 0x40800100, 130*c5a511f1SSebastian Siewior 0x80440400, 0x00000038, 0x0c000800, 0x40900100, 131*c5a511f1SSebastian Siewior 0x10000000, 0x00000000, 0x00200000, 0x00001000, 132*c5a511f1SSebastian Siewior 0x10040000, 0x00000000, 0x04200000, 0x00101000, 133*c5a511f1SSebastian Siewior 0x10400000, 0x00000008, 0x00200800, 0x40001000, 134*c5a511f1SSebastian Siewior 0x10440000, 0x00000008, 0x04200800, 0x40101000, 135*c5a511f1SSebastian Siewior 0x10000400, 0x00000020, 0x08200000, 0x00001100, 136*c5a511f1SSebastian Siewior 0x10040400, 0x00000020, 0x0c200000, 0x00101100, 137*c5a511f1SSebastian Siewior 0x10400400, 0x00000028, 0x08200800, 0x40001100, 138*c5a511f1SSebastian Siewior 0x10440400, 0x00000028, 0x0c200800, 0x40101100, 139*c5a511f1SSebastian Siewior 0x90000000, 0x00000010, 0x00200000, 0x00801000, 140*c5a511f1SSebastian Siewior 0x90040000, 0x00000010, 0x04200000, 0x00901000, 141*c5a511f1SSebastian Siewior 0x90400000, 0x00000018, 0x00200800, 0x40801000, 142*c5a511f1SSebastian Siewior 0x90440000, 0x00000018, 0x04200800, 0x40901000, 143*c5a511f1SSebastian Siewior 0x90000400, 0x00000030, 0x08200000, 0x00801100, 144*c5a511f1SSebastian Siewior 0x90040400, 0x00000030, 0x0c200000, 0x00901100, 145*c5a511f1SSebastian Siewior 0x90400400, 0x00000038, 0x08200800, 0x40801100, 146*c5a511f1SSebastian Siewior 0x90440400, 0x00000038, 0x0c200800, 0x40901100, 147*c5a511f1SSebastian Siewior 0x00000200, 0x00080000, 0x00000000, 0x00000004, 148*c5a511f1SSebastian Siewior 0x00040200, 0x00080000, 0x04000000, 0x00100004, 149*c5a511f1SSebastian Siewior 0x00400200, 0x00080008, 0x00000800, 0x40000004, 150*c5a511f1SSebastian Siewior 0x00440200, 0x00080008, 0x04000800, 0x40100004, 151*c5a511f1SSebastian Siewior 0x00000600, 0x00080020, 0x08000000, 0x00000104, 152*c5a511f1SSebastian Siewior 0x00040600, 0x00080020, 0x0c000000, 0x00100104, 153*c5a511f1SSebastian Siewior 0x00400600, 0x00080028, 0x08000800, 0x40000104, 154*c5a511f1SSebastian Siewior 0x00440600, 0x00080028, 0x0c000800, 0x40100104, 155*c5a511f1SSebastian Siewior 0x80000200, 0x00080010, 0x00000000, 0x00800004, 156*c5a511f1SSebastian Siewior 0x80040200, 0x00080010, 0x04000000, 0x00900004, 157*c5a511f1SSebastian Siewior 0x80400200, 0x00080018, 0x00000800, 0x40800004, 158*c5a511f1SSebastian Siewior 0x80440200, 0x00080018, 0x04000800, 0x40900004, 159*c5a511f1SSebastian Siewior 0x80000600, 0x00080030, 0x08000000, 0x00800104, 160*c5a511f1SSebastian Siewior 0x80040600, 0x00080030, 0x0c000000, 0x00900104, 161*c5a511f1SSebastian Siewior 0x80400600, 0x00080038, 0x08000800, 0x40800104, 162*c5a511f1SSebastian Siewior 0x80440600, 0x00080038, 0x0c000800, 0x40900104, 163*c5a511f1SSebastian Siewior 0x10000200, 0x00080000, 0x00200000, 0x00001004, 164*c5a511f1SSebastian Siewior 0x10040200, 0x00080000, 0x04200000, 0x00101004, 165*c5a511f1SSebastian Siewior 0x10400200, 0x00080008, 0x00200800, 0x40001004, 166*c5a511f1SSebastian Siewior 0x10440200, 0x00080008, 0x04200800, 0x40101004, 167*c5a511f1SSebastian Siewior 0x10000600, 0x00080020, 0x08200000, 0x00001104, 168*c5a511f1SSebastian Siewior 0x10040600, 0x00080020, 0x0c200000, 0x00101104, 169*c5a511f1SSebastian Siewior 0x10400600, 0x00080028, 0x08200800, 0x40001104, 170*c5a511f1SSebastian Siewior 0x10440600, 0x00080028, 0x0c200800, 0x40101104, 171*c5a511f1SSebastian Siewior 0x90000200, 0x00080010, 0x00200000, 0x00801004, 172*c5a511f1SSebastian Siewior 0x90040200, 0x00080010, 0x04200000, 0x00901004, 173*c5a511f1SSebastian Siewior 0x90400200, 0x00080018, 0x00200800, 0x40801004, 174*c5a511f1SSebastian Siewior 0x90440200, 0x00080018, 0x04200800, 0x40901004, 175*c5a511f1SSebastian Siewior 0x90000600, 0x00080030, 0x08200000, 0x00801104, 176*c5a511f1SSebastian Siewior 0x90040600, 0x00080030, 0x0c200000, 0x00901104, 177*c5a511f1SSebastian Siewior 0x90400600, 0x00080038, 0x08200800, 0x40801104, 178*c5a511f1SSebastian Siewior 0x90440600, 0x00080038, 0x0c200800, 0x40901104, 179*c5a511f1SSebastian Siewior 0x00000002, 0x00002000, 0x20000000, 0x00000001, 180*c5a511f1SSebastian Siewior 0x00040002, 0x00002000, 0x24000000, 0x00100001, 181*c5a511f1SSebastian Siewior 0x00400002, 0x00002008, 0x20000800, 0x40000001, 182*c5a511f1SSebastian Siewior 0x00440002, 0x00002008, 0x24000800, 0x40100001, 183*c5a511f1SSebastian Siewior 0x00000402, 0x00002020, 0x28000000, 0x00000101, 184*c5a511f1SSebastian Siewior 0x00040402, 0x00002020, 0x2c000000, 0x00100101, 185*c5a511f1SSebastian Siewior 0x00400402, 0x00002028, 0x28000800, 0x40000101, 186*c5a511f1SSebastian Siewior 0x00440402, 0x00002028, 0x2c000800, 0x40100101, 187*c5a511f1SSebastian Siewior 0x80000002, 0x00002010, 0x20000000, 0x00800001, 188*c5a511f1SSebastian Siewior 0x80040002, 0x00002010, 0x24000000, 0x00900001, 189*c5a511f1SSebastian Siewior 0x80400002, 0x00002018, 0x20000800, 0x40800001, 190*c5a511f1SSebastian Siewior 0x80440002, 0x00002018, 0x24000800, 0x40900001, 191*c5a511f1SSebastian Siewior 0x80000402, 0x00002030, 0x28000000, 0x00800101, 192*c5a511f1SSebastian Siewior 0x80040402, 0x00002030, 0x2c000000, 0x00900101, 193*c5a511f1SSebastian Siewior 0x80400402, 0x00002038, 0x28000800, 0x40800101, 194*c5a511f1SSebastian Siewior 0x80440402, 0x00002038, 0x2c000800, 0x40900101, 195*c5a511f1SSebastian Siewior 0x10000002, 0x00002000, 0x20200000, 0x00001001, 196*c5a511f1SSebastian Siewior 0x10040002, 0x00002000, 0x24200000, 0x00101001, 197*c5a511f1SSebastian Siewior 0x10400002, 0x00002008, 0x20200800, 0x40001001, 198*c5a511f1SSebastian Siewior 0x10440002, 0x00002008, 0x24200800, 0x40101001, 199*c5a511f1SSebastian Siewior 0x10000402, 0x00002020, 0x28200000, 0x00001101, 200*c5a511f1SSebastian Siewior 0x10040402, 0x00002020, 0x2c200000, 0x00101101, 201*c5a511f1SSebastian Siewior 0x10400402, 0x00002028, 0x28200800, 0x40001101, 202*c5a511f1SSebastian Siewior 0x10440402, 0x00002028, 0x2c200800, 0x40101101, 203*c5a511f1SSebastian Siewior 0x90000002, 0x00002010, 0x20200000, 0x00801001, 204*c5a511f1SSebastian Siewior 0x90040002, 0x00002010, 0x24200000, 0x00901001, 205*c5a511f1SSebastian Siewior 0x90400002, 0x00002018, 0x20200800, 0x40801001, 206*c5a511f1SSebastian Siewior 0x90440002, 0x00002018, 0x24200800, 0x40901001, 207*c5a511f1SSebastian Siewior 0x90000402, 0x00002030, 0x28200000, 0x00801101, 208*c5a511f1SSebastian Siewior 0x90040402, 0x00002030, 0x2c200000, 0x00901101, 209*c5a511f1SSebastian Siewior 0x90400402, 0x00002038, 0x28200800, 0x40801101, 210*c5a511f1SSebastian Siewior 0x90440402, 0x00002038, 0x2c200800, 0x40901101, 211*c5a511f1SSebastian Siewior 0x00000202, 0x00082000, 0x20000000, 0x00000005, 212*c5a511f1SSebastian Siewior 0x00040202, 0x00082000, 0x24000000, 0x00100005, 213*c5a511f1SSebastian Siewior 0x00400202, 0x00082008, 0x20000800, 0x40000005, 214*c5a511f1SSebastian Siewior 0x00440202, 0x00082008, 0x24000800, 0x40100005, 215*c5a511f1SSebastian Siewior 0x00000602, 0x00082020, 0x28000000, 0x00000105, 216*c5a511f1SSebastian Siewior 0x00040602, 0x00082020, 0x2c000000, 0x00100105, 217*c5a511f1SSebastian Siewior 0x00400602, 0x00082028, 0x28000800, 0x40000105, 218*c5a511f1SSebastian Siewior 0x00440602, 0x00082028, 0x2c000800, 0x40100105, 219*c5a511f1SSebastian Siewior 0x80000202, 0x00082010, 0x20000000, 0x00800005, 220*c5a511f1SSebastian Siewior 0x80040202, 0x00082010, 0x24000000, 0x00900005, 221*c5a511f1SSebastian Siewior 0x80400202, 0x00082018, 0x20000800, 0x40800005, 222*c5a511f1SSebastian Siewior 0x80440202, 0x00082018, 0x24000800, 0x40900005, 223*c5a511f1SSebastian Siewior 0x80000602, 0x00082030, 0x28000000, 0x00800105, 224*c5a511f1SSebastian Siewior 0x80040602, 0x00082030, 0x2c000000, 0x00900105, 225*c5a511f1SSebastian Siewior 0x80400602, 0x00082038, 0x28000800, 0x40800105, 226*c5a511f1SSebastian Siewior 0x80440602, 0x00082038, 0x2c000800, 0x40900105, 227*c5a511f1SSebastian Siewior 0x10000202, 0x00082000, 0x20200000, 0x00001005, 228*c5a511f1SSebastian Siewior 0x10040202, 0x00082000, 0x24200000, 0x00101005, 229*c5a511f1SSebastian Siewior 0x10400202, 0x00082008, 0x20200800, 0x40001005, 230*c5a511f1SSebastian Siewior 0x10440202, 0x00082008, 0x24200800, 0x40101005, 231*c5a511f1SSebastian Siewior 0x10000602, 0x00082020, 0x28200000, 0x00001105, 232*c5a511f1SSebastian Siewior 0x10040602, 0x00082020, 0x2c200000, 0x00101105, 233*c5a511f1SSebastian Siewior 0x10400602, 0x00082028, 0x28200800, 0x40001105, 234*c5a511f1SSebastian Siewior 0x10440602, 0x00082028, 0x2c200800, 0x40101105, 235*c5a511f1SSebastian Siewior 0x90000202, 0x00082010, 0x20200000, 0x00801005, 236*c5a511f1SSebastian Siewior 0x90040202, 0x00082010, 0x24200000, 0x00901005, 237*c5a511f1SSebastian Siewior 0x90400202, 0x00082018, 0x20200800, 0x40801005, 238*c5a511f1SSebastian Siewior 0x90440202, 0x00082018, 0x24200800, 0x40901005, 239*c5a511f1SSebastian Siewior 0x90000602, 0x00082030, 0x28200000, 0x00801105, 240*c5a511f1SSebastian Siewior 0x90040602, 0x00082030, 0x2c200000, 0x00901105, 241*c5a511f1SSebastian Siewior 0x90400602, 0x00082038, 0x28200800, 0x40801105, 242*c5a511f1SSebastian Siewior 0x90440602, 0x00082038, 0x2c200800, 0x40901105, 243*c5a511f1SSebastian Siewior 244*c5a511f1SSebastian Siewior 0x00000000, 0x00000000, 0x00000000, 0x00000000, 245*c5a511f1SSebastian Siewior 0x00000000, 0x00000008, 0x00080000, 0x10000000, 246*c5a511f1SSebastian Siewior 0x02000000, 0x00000000, 0x00000080, 0x00001000, 247*c5a511f1SSebastian Siewior 0x02000000, 0x00000008, 0x00080080, 0x10001000, 248*c5a511f1SSebastian Siewior 0x00004000, 0x00000000, 0x00000040, 0x00040000, 249*c5a511f1SSebastian Siewior 0x00004000, 0x00000008, 0x00080040, 0x10040000, 250*c5a511f1SSebastian Siewior 0x02004000, 0x00000000, 0x000000c0, 0x00041000, 251*c5a511f1SSebastian Siewior 0x02004000, 0x00000008, 0x000800c0, 0x10041000, 252*c5a511f1SSebastian Siewior 0x00020000, 0x00008000, 0x08000000, 0x00200000, 253*c5a511f1SSebastian Siewior 0x00020000, 0x00008008, 0x08080000, 0x10200000, 254*c5a511f1SSebastian Siewior 0x02020000, 0x00008000, 0x08000080, 0x00201000, 255*c5a511f1SSebastian Siewior 0x02020000, 0x00008008, 0x08080080, 0x10201000, 256*c5a511f1SSebastian Siewior 0x00024000, 0x00008000, 0x08000040, 0x00240000, 257*c5a511f1SSebastian Siewior 0x00024000, 0x00008008, 0x08080040, 0x10240000, 258*c5a511f1SSebastian Siewior 0x02024000, 0x00008000, 0x080000c0, 0x00241000, 259*c5a511f1SSebastian Siewior 0x02024000, 0x00008008, 0x080800c0, 0x10241000, 260*c5a511f1SSebastian Siewior 0x00000000, 0x01000000, 0x00002000, 0x00000020, 261*c5a511f1SSebastian Siewior 0x00000000, 0x01000008, 0x00082000, 0x10000020, 262*c5a511f1SSebastian Siewior 0x02000000, 0x01000000, 0x00002080, 0x00001020, 263*c5a511f1SSebastian Siewior 0x02000000, 0x01000008, 0x00082080, 0x10001020, 264*c5a511f1SSebastian Siewior 0x00004000, 0x01000000, 0x00002040, 0x00040020, 265*c5a511f1SSebastian Siewior 0x00004000, 0x01000008, 0x00082040, 0x10040020, 266*c5a511f1SSebastian Siewior 0x02004000, 0x01000000, 0x000020c0, 0x00041020, 267*c5a511f1SSebastian Siewior 0x02004000, 0x01000008, 0x000820c0, 0x10041020, 268*c5a511f1SSebastian Siewior 0x00020000, 0x01008000, 0x08002000, 0x00200020, 269*c5a511f1SSebastian Siewior 0x00020000, 0x01008008, 0x08082000, 0x10200020, 270*c5a511f1SSebastian Siewior 0x02020000, 0x01008000, 0x08002080, 0x00201020, 271*c5a511f1SSebastian Siewior 0x02020000, 0x01008008, 0x08082080, 0x10201020, 272*c5a511f1SSebastian Siewior 0x00024000, 0x01008000, 0x08002040, 0x00240020, 273*c5a511f1SSebastian Siewior 0x00024000, 0x01008008, 0x08082040, 0x10240020, 274*c5a511f1SSebastian Siewior 0x02024000, 0x01008000, 0x080020c0, 0x00241020, 275*c5a511f1SSebastian Siewior 0x02024000, 0x01008008, 0x080820c0, 0x10241020, 276*c5a511f1SSebastian Siewior 0x00000400, 0x04000000, 0x00100000, 0x00000004, 277*c5a511f1SSebastian Siewior 0x00000400, 0x04000008, 0x00180000, 0x10000004, 278*c5a511f1SSebastian Siewior 0x02000400, 0x04000000, 0x00100080, 0x00001004, 279*c5a511f1SSebastian Siewior 0x02000400, 0x04000008, 0x00180080, 0x10001004, 280*c5a511f1SSebastian Siewior 0x00004400, 0x04000000, 0x00100040, 0x00040004, 281*c5a511f1SSebastian Siewior 0x00004400, 0x04000008, 0x00180040, 0x10040004, 282*c5a511f1SSebastian Siewior 0x02004400, 0x04000000, 0x001000c0, 0x00041004, 283*c5a511f1SSebastian Siewior 0x02004400, 0x04000008, 0x001800c0, 0x10041004, 284*c5a511f1SSebastian Siewior 0x00020400, 0x04008000, 0x08100000, 0x00200004, 285*c5a511f1SSebastian Siewior 0x00020400, 0x04008008, 0x08180000, 0x10200004, 286*c5a511f1SSebastian Siewior 0x02020400, 0x04008000, 0x08100080, 0x00201004, 287*c5a511f1SSebastian Siewior 0x02020400, 0x04008008, 0x08180080, 0x10201004, 288*c5a511f1SSebastian Siewior 0x00024400, 0x04008000, 0x08100040, 0x00240004, 289*c5a511f1SSebastian Siewior 0x00024400, 0x04008008, 0x08180040, 0x10240004, 290*c5a511f1SSebastian Siewior 0x02024400, 0x04008000, 0x081000c0, 0x00241004, 291*c5a511f1SSebastian Siewior 0x02024400, 0x04008008, 0x081800c0, 0x10241004, 292*c5a511f1SSebastian Siewior 0x00000400, 0x05000000, 0x00102000, 0x00000024, 293*c5a511f1SSebastian Siewior 0x00000400, 0x05000008, 0x00182000, 0x10000024, 294*c5a511f1SSebastian Siewior 0x02000400, 0x05000000, 0x00102080, 0x00001024, 295*c5a511f1SSebastian Siewior 0x02000400, 0x05000008, 0x00182080, 0x10001024, 296*c5a511f1SSebastian Siewior 0x00004400, 0x05000000, 0x00102040, 0x00040024, 297*c5a511f1SSebastian Siewior 0x00004400, 0x05000008, 0x00182040, 0x10040024, 298*c5a511f1SSebastian Siewior 0x02004400, 0x05000000, 0x001020c0, 0x00041024, 299*c5a511f1SSebastian Siewior 0x02004400, 0x05000008, 0x001820c0, 0x10041024, 300*c5a511f1SSebastian Siewior 0x00020400, 0x05008000, 0x08102000, 0x00200024, 301*c5a511f1SSebastian Siewior 0x00020400, 0x05008008, 0x08182000, 0x10200024, 302*c5a511f1SSebastian Siewior 0x02020400, 0x05008000, 0x08102080, 0x00201024, 303*c5a511f1SSebastian Siewior 0x02020400, 0x05008008, 0x08182080, 0x10201024, 304*c5a511f1SSebastian Siewior 0x00024400, 0x05008000, 0x08102040, 0x00240024, 305*c5a511f1SSebastian Siewior 0x00024400, 0x05008008, 0x08182040, 0x10240024, 306*c5a511f1SSebastian Siewior 0x02024400, 0x05008000, 0x081020c0, 0x00241024, 307*c5a511f1SSebastian Siewior 0x02024400, 0x05008008, 0x081820c0, 0x10241024, 308*c5a511f1SSebastian Siewior 0x00000800, 0x00010000, 0x20000000, 0x00000010, 309*c5a511f1SSebastian Siewior 0x00000800, 0x00010008, 0x20080000, 0x10000010, 310*c5a511f1SSebastian Siewior 0x02000800, 0x00010000, 0x20000080, 0x00001010, 311*c5a511f1SSebastian Siewior 0x02000800, 0x00010008, 0x20080080, 0x10001010, 312*c5a511f1SSebastian Siewior 0x00004800, 0x00010000, 0x20000040, 0x00040010, 313*c5a511f1SSebastian Siewior 0x00004800, 0x00010008, 0x20080040, 0x10040010, 314*c5a511f1SSebastian Siewior 0x02004800, 0x00010000, 0x200000c0, 0x00041010, 315*c5a511f1SSebastian Siewior 0x02004800, 0x00010008, 0x200800c0, 0x10041010, 316*c5a511f1SSebastian Siewior 0x00020800, 0x00018000, 0x28000000, 0x00200010, 317*c5a511f1SSebastian Siewior 0x00020800, 0x00018008, 0x28080000, 0x10200010, 318*c5a511f1SSebastian Siewior 0x02020800, 0x00018000, 0x28000080, 0x00201010, 319*c5a511f1SSebastian Siewior 0x02020800, 0x00018008, 0x28080080, 0x10201010, 320*c5a511f1SSebastian Siewior 0x00024800, 0x00018000, 0x28000040, 0x00240010, 321*c5a511f1SSebastian Siewior 0x00024800, 0x00018008, 0x28080040, 0x10240010, 322*c5a511f1SSebastian Siewior 0x02024800, 0x00018000, 0x280000c0, 0x00241010, 323*c5a511f1SSebastian Siewior 0x02024800, 0x00018008, 0x280800c0, 0x10241010, 324*c5a511f1SSebastian Siewior 0x00000800, 0x01010000, 0x20002000, 0x00000030, 325*c5a511f1SSebastian Siewior 0x00000800, 0x01010008, 0x20082000, 0x10000030, 326*c5a511f1SSebastian Siewior 0x02000800, 0x01010000, 0x20002080, 0x00001030, 327*c5a511f1SSebastian Siewior 0x02000800, 0x01010008, 0x20082080, 0x10001030, 328*c5a511f1SSebastian Siewior 0x00004800, 0x01010000, 0x20002040, 0x00040030, 329*c5a511f1SSebastian Siewior 0x00004800, 0x01010008, 0x20082040, 0x10040030, 330*c5a511f1SSebastian Siewior 0x02004800, 0x01010000, 0x200020c0, 0x00041030, 331*c5a511f1SSebastian Siewior 0x02004800, 0x01010008, 0x200820c0, 0x10041030, 332*c5a511f1SSebastian Siewior 0x00020800, 0x01018000, 0x28002000, 0x00200030, 333*c5a511f1SSebastian Siewior 0x00020800, 0x01018008, 0x28082000, 0x10200030, 334*c5a511f1SSebastian Siewior 0x02020800, 0x01018000, 0x28002080, 0x00201030, 335*c5a511f1SSebastian Siewior 0x02020800, 0x01018008, 0x28082080, 0x10201030, 336*c5a511f1SSebastian Siewior 0x00024800, 0x01018000, 0x28002040, 0x00240030, 337*c5a511f1SSebastian Siewior 0x00024800, 0x01018008, 0x28082040, 0x10240030, 338*c5a511f1SSebastian Siewior 0x02024800, 0x01018000, 0x280020c0, 0x00241030, 339*c5a511f1SSebastian Siewior 0x02024800, 0x01018008, 0x280820c0, 0x10241030, 340*c5a511f1SSebastian Siewior 0x00000c00, 0x04010000, 0x20100000, 0x00000014, 341*c5a511f1SSebastian Siewior 0x00000c00, 0x04010008, 0x20180000, 0x10000014, 342*c5a511f1SSebastian Siewior 0x02000c00, 0x04010000, 0x20100080, 0x00001014, 343*c5a511f1SSebastian Siewior 0x02000c00, 0x04010008, 0x20180080, 0x10001014, 344*c5a511f1SSebastian Siewior 0x00004c00, 0x04010000, 0x20100040, 0x00040014, 345*c5a511f1SSebastian Siewior 0x00004c00, 0x04010008, 0x20180040, 0x10040014, 346*c5a511f1SSebastian Siewior 0x02004c00, 0x04010000, 0x201000c0, 0x00041014, 347*c5a511f1SSebastian Siewior 0x02004c00, 0x04010008, 0x201800c0, 0x10041014, 348*c5a511f1SSebastian Siewior 0x00020c00, 0x04018000, 0x28100000, 0x00200014, 349*c5a511f1SSebastian Siewior 0x00020c00, 0x04018008, 0x28180000, 0x10200014, 350*c5a511f1SSebastian Siewior 0x02020c00, 0x04018000, 0x28100080, 0x00201014, 351*c5a511f1SSebastian Siewior 0x02020c00, 0x04018008, 0x28180080, 0x10201014, 352*c5a511f1SSebastian Siewior 0x00024c00, 0x04018000, 0x28100040, 0x00240014, 353*c5a511f1SSebastian Siewior 0x00024c00, 0x04018008, 0x28180040, 0x10240014, 354*c5a511f1SSebastian Siewior 0x02024c00, 0x04018000, 0x281000c0, 0x00241014, 355*c5a511f1SSebastian Siewior 0x02024c00, 0x04018008, 0x281800c0, 0x10241014, 356*c5a511f1SSebastian Siewior 0x00000c00, 0x05010000, 0x20102000, 0x00000034, 357*c5a511f1SSebastian Siewior 0x00000c00, 0x05010008, 0x20182000, 0x10000034, 358*c5a511f1SSebastian Siewior 0x02000c00, 0x05010000, 0x20102080, 0x00001034, 359*c5a511f1SSebastian Siewior 0x02000c00, 0x05010008, 0x20182080, 0x10001034, 360*c5a511f1SSebastian Siewior 0x00004c00, 0x05010000, 0x20102040, 0x00040034, 361*c5a511f1SSebastian Siewior 0x00004c00, 0x05010008, 0x20182040, 0x10040034, 362*c5a511f1SSebastian Siewior 0x02004c00, 0x05010000, 0x201020c0, 0x00041034, 363*c5a511f1SSebastian Siewior 0x02004c00, 0x05010008, 0x201820c0, 0x10041034, 364*c5a511f1SSebastian Siewior 0x00020c00, 0x05018000, 0x28102000, 0x00200034, 365*c5a511f1SSebastian Siewior 0x00020c00, 0x05018008, 0x28182000, 0x10200034, 366*c5a511f1SSebastian Siewior 0x02020c00, 0x05018000, 0x28102080, 0x00201034, 367*c5a511f1SSebastian Siewior 0x02020c00, 0x05018008, 0x28182080, 0x10201034, 368*c5a511f1SSebastian Siewior 0x00024c00, 0x05018000, 0x28102040, 0x00240034, 369*c5a511f1SSebastian Siewior 0x00024c00, 0x05018008, 0x28182040, 0x10240034, 370*c5a511f1SSebastian Siewior 0x02024c00, 0x05018000, 0x281020c0, 0x00241034, 371*c5a511f1SSebastian Siewior 0x02024c00, 0x05018008, 0x281820c0, 0x10241034 372*c5a511f1SSebastian Siewior }; 373*c5a511f1SSebastian Siewior 374*c5a511f1SSebastian Siewior /* S-box lookup tables */ 375*c5a511f1SSebastian Siewior 376*c5a511f1SSebastian Siewior static const u32 S1[64] = { 377*c5a511f1SSebastian Siewior 0x01010400, 0x00000000, 0x00010000, 0x01010404, 378*c5a511f1SSebastian Siewior 0x01010004, 0x00010404, 0x00000004, 0x00010000, 379*c5a511f1SSebastian Siewior 0x00000400, 0x01010400, 0x01010404, 0x00000400, 380*c5a511f1SSebastian Siewior 0x01000404, 0x01010004, 0x01000000, 0x00000004, 381*c5a511f1SSebastian Siewior 0x00000404, 0x01000400, 0x01000400, 0x00010400, 382*c5a511f1SSebastian Siewior 0x00010400, 0x01010000, 0x01010000, 0x01000404, 383*c5a511f1SSebastian Siewior 0x00010004, 0x01000004, 0x01000004, 0x00010004, 384*c5a511f1SSebastian Siewior 0x00000000, 0x00000404, 0x00010404, 0x01000000, 385*c5a511f1SSebastian Siewior 0x00010000, 0x01010404, 0x00000004, 0x01010000, 386*c5a511f1SSebastian Siewior 0x01010400, 0x01000000, 0x01000000, 0x00000400, 387*c5a511f1SSebastian Siewior 0x01010004, 0x00010000, 0x00010400, 0x01000004, 388*c5a511f1SSebastian Siewior 0x00000400, 0x00000004, 0x01000404, 0x00010404, 389*c5a511f1SSebastian Siewior 0x01010404, 0x00010004, 0x01010000, 0x01000404, 390*c5a511f1SSebastian Siewior 0x01000004, 0x00000404, 0x00010404, 0x01010400, 391*c5a511f1SSebastian Siewior 0x00000404, 0x01000400, 0x01000400, 0x00000000, 392*c5a511f1SSebastian Siewior 0x00010004, 0x00010400, 0x00000000, 0x01010004 393*c5a511f1SSebastian Siewior }; 394*c5a511f1SSebastian Siewior 395*c5a511f1SSebastian Siewior static const u32 S2[64] = { 396*c5a511f1SSebastian Siewior 0x80108020, 0x80008000, 0x00008000, 0x00108020, 397*c5a511f1SSebastian Siewior 0x00100000, 0x00000020, 0x80100020, 0x80008020, 398*c5a511f1SSebastian Siewior 0x80000020, 0x80108020, 0x80108000, 0x80000000, 399*c5a511f1SSebastian Siewior 0x80008000, 0x00100000, 0x00000020, 0x80100020, 400*c5a511f1SSebastian Siewior 0x00108000, 0x00100020, 0x80008020, 0x00000000, 401*c5a511f1SSebastian Siewior 0x80000000, 0x00008000, 0x00108020, 0x80100000, 402*c5a511f1SSebastian Siewior 0x00100020, 0x80000020, 0x00000000, 0x00108000, 403*c5a511f1SSebastian Siewior 0x00008020, 0x80108000, 0x80100000, 0x00008020, 404*c5a511f1SSebastian Siewior 0x00000000, 0x00108020, 0x80100020, 0x00100000, 405*c5a511f1SSebastian Siewior 0x80008020, 0x80100000, 0x80108000, 0x00008000, 406*c5a511f1SSebastian Siewior 0x80100000, 0x80008000, 0x00000020, 0x80108020, 407*c5a511f1SSebastian Siewior 0x00108020, 0x00000020, 0x00008000, 0x80000000, 408*c5a511f1SSebastian Siewior 0x00008020, 0x80108000, 0x00100000, 0x80000020, 409*c5a511f1SSebastian Siewior 0x00100020, 0x80008020, 0x80000020, 0x00100020, 410*c5a511f1SSebastian Siewior 0x00108000, 0x00000000, 0x80008000, 0x00008020, 411*c5a511f1SSebastian Siewior 0x80000000, 0x80100020, 0x80108020, 0x00108000 412*c5a511f1SSebastian Siewior }; 413*c5a511f1SSebastian Siewior 414*c5a511f1SSebastian Siewior static const u32 S3[64] = { 415*c5a511f1SSebastian Siewior 0x00000208, 0x08020200, 0x00000000, 0x08020008, 416*c5a511f1SSebastian Siewior 0x08000200, 0x00000000, 0x00020208, 0x08000200, 417*c5a511f1SSebastian Siewior 0x00020008, 0x08000008, 0x08000008, 0x00020000, 418*c5a511f1SSebastian Siewior 0x08020208, 0x00020008, 0x08020000, 0x00000208, 419*c5a511f1SSebastian Siewior 0x08000000, 0x00000008, 0x08020200, 0x00000200, 420*c5a511f1SSebastian Siewior 0x00020200, 0x08020000, 0x08020008, 0x00020208, 421*c5a511f1SSebastian Siewior 0x08000208, 0x00020200, 0x00020000, 0x08000208, 422*c5a511f1SSebastian Siewior 0x00000008, 0x08020208, 0x00000200, 0x08000000, 423*c5a511f1SSebastian Siewior 0x08020200, 0x08000000, 0x00020008, 0x00000208, 424*c5a511f1SSebastian Siewior 0x00020000, 0x08020200, 0x08000200, 0x00000000, 425*c5a511f1SSebastian Siewior 0x00000200, 0x00020008, 0x08020208, 0x08000200, 426*c5a511f1SSebastian Siewior 0x08000008, 0x00000200, 0x00000000, 0x08020008, 427*c5a511f1SSebastian Siewior 0x08000208, 0x00020000, 0x08000000, 0x08020208, 428*c5a511f1SSebastian Siewior 0x00000008, 0x00020208, 0x00020200, 0x08000008, 429*c5a511f1SSebastian Siewior 0x08020000, 0x08000208, 0x00000208, 0x08020000, 430*c5a511f1SSebastian Siewior 0x00020208, 0x00000008, 0x08020008, 0x00020200 431*c5a511f1SSebastian Siewior }; 432*c5a511f1SSebastian Siewior 433*c5a511f1SSebastian Siewior static const u32 S4[64] = { 434*c5a511f1SSebastian Siewior 0x00802001, 0x00002081, 0x00002081, 0x00000080, 435*c5a511f1SSebastian Siewior 0x00802080, 0x00800081, 0x00800001, 0x00002001, 436*c5a511f1SSebastian Siewior 0x00000000, 0x00802000, 0x00802000, 0x00802081, 437*c5a511f1SSebastian Siewior 0x00000081, 0x00000000, 0x00800080, 0x00800001, 438*c5a511f1SSebastian Siewior 0x00000001, 0x00002000, 0x00800000, 0x00802001, 439*c5a511f1SSebastian Siewior 0x00000080, 0x00800000, 0x00002001, 0x00002080, 440*c5a511f1SSebastian Siewior 0x00800081, 0x00000001, 0x00002080, 0x00800080, 441*c5a511f1SSebastian Siewior 0x00002000, 0x00802080, 0x00802081, 0x00000081, 442*c5a511f1SSebastian Siewior 0x00800080, 0x00800001, 0x00802000, 0x00802081, 443*c5a511f1SSebastian Siewior 0x00000081, 0x00000000, 0x00000000, 0x00802000, 444*c5a511f1SSebastian Siewior 0x00002080, 0x00800080, 0x00800081, 0x00000001, 445*c5a511f1SSebastian Siewior 0x00802001, 0x00002081, 0x00002081, 0x00000080, 446*c5a511f1SSebastian Siewior 0x00802081, 0x00000081, 0x00000001, 0x00002000, 447*c5a511f1SSebastian Siewior 0x00800001, 0x00002001, 0x00802080, 0x00800081, 448*c5a511f1SSebastian Siewior 0x00002001, 0x00002080, 0x00800000, 0x00802001, 449*c5a511f1SSebastian Siewior 0x00000080, 0x00800000, 0x00002000, 0x00802080 450*c5a511f1SSebastian Siewior }; 451*c5a511f1SSebastian Siewior 452*c5a511f1SSebastian Siewior static const u32 S5[64] = { 453*c5a511f1SSebastian Siewior 0x00000100, 0x02080100, 0x02080000, 0x42000100, 454*c5a511f1SSebastian Siewior 0x00080000, 0x00000100, 0x40000000, 0x02080000, 455*c5a511f1SSebastian Siewior 0x40080100, 0x00080000, 0x02000100, 0x40080100, 456*c5a511f1SSebastian Siewior 0x42000100, 0x42080000, 0x00080100, 0x40000000, 457*c5a511f1SSebastian Siewior 0x02000000, 0x40080000, 0x40080000, 0x00000000, 458*c5a511f1SSebastian Siewior 0x40000100, 0x42080100, 0x42080100, 0x02000100, 459*c5a511f1SSebastian Siewior 0x42080000, 0x40000100, 0x00000000, 0x42000000, 460*c5a511f1SSebastian Siewior 0x02080100, 0x02000000, 0x42000000, 0x00080100, 461*c5a511f1SSebastian Siewior 0x00080000, 0x42000100, 0x00000100, 0x02000000, 462*c5a511f1SSebastian Siewior 0x40000000, 0x02080000, 0x42000100, 0x40080100, 463*c5a511f1SSebastian Siewior 0x02000100, 0x40000000, 0x42080000, 0x02080100, 464*c5a511f1SSebastian Siewior 0x40080100, 0x00000100, 0x02000000, 0x42080000, 465*c5a511f1SSebastian Siewior 0x42080100, 0x00080100, 0x42000000, 0x42080100, 466*c5a511f1SSebastian Siewior 0x02080000, 0x00000000, 0x40080000, 0x42000000, 467*c5a511f1SSebastian Siewior 0x00080100, 0x02000100, 0x40000100, 0x00080000, 468*c5a511f1SSebastian Siewior 0x00000000, 0x40080000, 0x02080100, 0x40000100 469*c5a511f1SSebastian Siewior }; 470*c5a511f1SSebastian Siewior 471*c5a511f1SSebastian Siewior static const u32 S6[64] = { 472*c5a511f1SSebastian Siewior 0x20000010, 0x20400000, 0x00004000, 0x20404010, 473*c5a511f1SSebastian Siewior 0x20400000, 0x00000010, 0x20404010, 0x00400000, 474*c5a511f1SSebastian Siewior 0x20004000, 0x00404010, 0x00400000, 0x20000010, 475*c5a511f1SSebastian Siewior 0x00400010, 0x20004000, 0x20000000, 0x00004010, 476*c5a511f1SSebastian Siewior 0x00000000, 0x00400010, 0x20004010, 0x00004000, 477*c5a511f1SSebastian Siewior 0x00404000, 0x20004010, 0x00000010, 0x20400010, 478*c5a511f1SSebastian Siewior 0x20400010, 0x00000000, 0x00404010, 0x20404000, 479*c5a511f1SSebastian Siewior 0x00004010, 0x00404000, 0x20404000, 0x20000000, 480*c5a511f1SSebastian Siewior 0x20004000, 0x00000010, 0x20400010, 0x00404000, 481*c5a511f1SSebastian Siewior 0x20404010, 0x00400000, 0x00004010, 0x20000010, 482*c5a511f1SSebastian Siewior 0x00400000, 0x20004000, 0x20000000, 0x00004010, 483*c5a511f1SSebastian Siewior 0x20000010, 0x20404010, 0x00404000, 0x20400000, 484*c5a511f1SSebastian Siewior 0x00404010, 0x20404000, 0x00000000, 0x20400010, 485*c5a511f1SSebastian Siewior 0x00000010, 0x00004000, 0x20400000, 0x00404010, 486*c5a511f1SSebastian Siewior 0x00004000, 0x00400010, 0x20004010, 0x00000000, 487*c5a511f1SSebastian Siewior 0x20404000, 0x20000000, 0x00400010, 0x20004010 488*c5a511f1SSebastian Siewior }; 489*c5a511f1SSebastian Siewior 490*c5a511f1SSebastian Siewior static const u32 S7[64] = { 491*c5a511f1SSebastian Siewior 0x00200000, 0x04200002, 0x04000802, 0x00000000, 492*c5a511f1SSebastian Siewior 0x00000800, 0x04000802, 0x00200802, 0x04200800, 493*c5a511f1SSebastian Siewior 0x04200802, 0x00200000, 0x00000000, 0x04000002, 494*c5a511f1SSebastian Siewior 0x00000002, 0x04000000, 0x04200002, 0x00000802, 495*c5a511f1SSebastian Siewior 0x04000800, 0x00200802, 0x00200002, 0x04000800, 496*c5a511f1SSebastian Siewior 0x04000002, 0x04200000, 0x04200800, 0x00200002, 497*c5a511f1SSebastian Siewior 0x04200000, 0x00000800, 0x00000802, 0x04200802, 498*c5a511f1SSebastian Siewior 0x00200800, 0x00000002, 0x04000000, 0x00200800, 499*c5a511f1SSebastian Siewior 0x04000000, 0x00200800, 0x00200000, 0x04000802, 500*c5a511f1SSebastian Siewior 0x04000802, 0x04200002, 0x04200002, 0x00000002, 501*c5a511f1SSebastian Siewior 0x00200002, 0x04000000, 0x04000800, 0x00200000, 502*c5a511f1SSebastian Siewior 0x04200800, 0x00000802, 0x00200802, 0x04200800, 503*c5a511f1SSebastian Siewior 0x00000802, 0x04000002, 0x04200802, 0x04200000, 504*c5a511f1SSebastian Siewior 0x00200800, 0x00000000, 0x00000002, 0x04200802, 505*c5a511f1SSebastian Siewior 0x00000000, 0x00200802, 0x04200000, 0x00000800, 506*c5a511f1SSebastian Siewior 0x04000002, 0x04000800, 0x00000800, 0x00200002 507*c5a511f1SSebastian Siewior }; 508*c5a511f1SSebastian Siewior 509*c5a511f1SSebastian Siewior static const u32 S8[64] = { 510*c5a511f1SSebastian Siewior 0x10001040, 0x00001000, 0x00040000, 0x10041040, 511*c5a511f1SSebastian Siewior 0x10000000, 0x10001040, 0x00000040, 0x10000000, 512*c5a511f1SSebastian Siewior 0x00040040, 0x10040000, 0x10041040, 0x00041000, 513*c5a511f1SSebastian Siewior 0x10041000, 0x00041040, 0x00001000, 0x00000040, 514*c5a511f1SSebastian Siewior 0x10040000, 0x10000040, 0x10001000, 0x00001040, 515*c5a511f1SSebastian Siewior 0x00041000, 0x00040040, 0x10040040, 0x10041000, 516*c5a511f1SSebastian Siewior 0x00001040, 0x00000000, 0x00000000, 0x10040040, 517*c5a511f1SSebastian Siewior 0x10000040, 0x10001000, 0x00041040, 0x00040000, 518*c5a511f1SSebastian Siewior 0x00041040, 0x00040000, 0x10041000, 0x00001000, 519*c5a511f1SSebastian Siewior 0x00000040, 0x10040040, 0x00001000, 0x00041040, 520*c5a511f1SSebastian Siewior 0x10001000, 0x00000040, 0x10000040, 0x10040000, 521*c5a511f1SSebastian Siewior 0x10040040, 0x10000000, 0x00040000, 0x10001040, 522*c5a511f1SSebastian Siewior 0x00000000, 0x10041040, 0x00040040, 0x10000040, 523*c5a511f1SSebastian Siewior 0x10040000, 0x10001000, 0x10001040, 0x00000000, 524*c5a511f1SSebastian Siewior 0x10041040, 0x00041000, 0x00041000, 0x00001040, 525*c5a511f1SSebastian Siewior 0x00001040, 0x00040040, 0x10000000, 0x10041000 526*c5a511f1SSebastian Siewior }; 527*c5a511f1SSebastian Siewior 528*c5a511f1SSebastian Siewior /* Encryption components: IP, FP, and round function */ 529*c5a511f1SSebastian Siewior 530*c5a511f1SSebastian Siewior #define IP(L, R, T) \ 531*c5a511f1SSebastian Siewior ROL(R, 4); \ 532*c5a511f1SSebastian Siewior T = L; \ 533*c5a511f1SSebastian Siewior L ^= R; \ 534*c5a511f1SSebastian Siewior L &= 0xf0f0f0f0; \ 535*c5a511f1SSebastian Siewior R ^= L; \ 536*c5a511f1SSebastian Siewior L ^= T; \ 537*c5a511f1SSebastian Siewior ROL(R, 12); \ 538*c5a511f1SSebastian Siewior T = L; \ 539*c5a511f1SSebastian Siewior L ^= R; \ 540*c5a511f1SSebastian Siewior L &= 0xffff0000; \ 541*c5a511f1SSebastian Siewior R ^= L; \ 542*c5a511f1SSebastian Siewior L ^= T; \ 543*c5a511f1SSebastian Siewior ROR(R, 14); \ 544*c5a511f1SSebastian Siewior T = L; \ 545*c5a511f1SSebastian Siewior L ^= R; \ 546*c5a511f1SSebastian Siewior L &= 0xcccccccc; \ 547*c5a511f1SSebastian Siewior R ^= L; \ 548*c5a511f1SSebastian Siewior L ^= T; \ 549*c5a511f1SSebastian Siewior ROL(R, 6); \ 550*c5a511f1SSebastian Siewior T = L; \ 551*c5a511f1SSebastian Siewior L ^= R; \ 552*c5a511f1SSebastian Siewior L &= 0xff00ff00; \ 553*c5a511f1SSebastian Siewior R ^= L; \ 554*c5a511f1SSebastian Siewior L ^= T; \ 555*c5a511f1SSebastian Siewior ROR(R, 7); \ 556*c5a511f1SSebastian Siewior T = L; \ 557*c5a511f1SSebastian Siewior L ^= R; \ 558*c5a511f1SSebastian Siewior L &= 0xaaaaaaaa; \ 559*c5a511f1SSebastian Siewior R ^= L; \ 560*c5a511f1SSebastian Siewior L ^= T; \ 561*c5a511f1SSebastian Siewior ROL(L, 1); 562*c5a511f1SSebastian Siewior 563*c5a511f1SSebastian Siewior #define FP(L, R, T) \ 564*c5a511f1SSebastian Siewior ROR(L, 1); \ 565*c5a511f1SSebastian Siewior T = L; \ 566*c5a511f1SSebastian Siewior L ^= R; \ 567*c5a511f1SSebastian Siewior L &= 0xaaaaaaaa; \ 568*c5a511f1SSebastian Siewior R ^= L; \ 569*c5a511f1SSebastian Siewior L ^= T; \ 570*c5a511f1SSebastian Siewior ROL(R, 7); \ 571*c5a511f1SSebastian Siewior T = L; \ 572*c5a511f1SSebastian Siewior L ^= R; \ 573*c5a511f1SSebastian Siewior L &= 0xff00ff00; \ 574*c5a511f1SSebastian Siewior R ^= L; \ 575*c5a511f1SSebastian Siewior L ^= T; \ 576*c5a511f1SSebastian Siewior ROR(R, 6); \ 577*c5a511f1SSebastian Siewior T = L; \ 578*c5a511f1SSebastian Siewior L ^= R; \ 579*c5a511f1SSebastian Siewior L &= 0xcccccccc; \ 580*c5a511f1SSebastian Siewior R ^= L; \ 581*c5a511f1SSebastian Siewior L ^= T; \ 582*c5a511f1SSebastian Siewior ROL(R, 14); \ 583*c5a511f1SSebastian Siewior T = L; \ 584*c5a511f1SSebastian Siewior L ^= R; \ 585*c5a511f1SSebastian Siewior L &= 0xffff0000; \ 586*c5a511f1SSebastian Siewior R ^= L; \ 587*c5a511f1SSebastian Siewior L ^= T; \ 588*c5a511f1SSebastian Siewior ROR(R, 12); \ 589*c5a511f1SSebastian Siewior T = L; \ 590*c5a511f1SSebastian Siewior L ^= R; \ 591*c5a511f1SSebastian Siewior L &= 0xf0f0f0f0; \ 592*c5a511f1SSebastian Siewior R ^= L; \ 593*c5a511f1SSebastian Siewior L ^= T; \ 594*c5a511f1SSebastian Siewior ROR(R, 4); 595*c5a511f1SSebastian Siewior 596*c5a511f1SSebastian Siewior #define ROUND(L, R, A, B, K, d) \ 597*c5a511f1SSebastian Siewior B = K[0]; A = K[1]; K += d; \ 598*c5a511f1SSebastian Siewior B ^= R; A ^= R; \ 599*c5a511f1SSebastian Siewior B &= 0x3f3f3f3f; ROR(A, 4); \ 600*c5a511f1SSebastian Siewior L ^= S8[0xff & B]; A &= 0x3f3f3f3f; \ 601*c5a511f1SSebastian Siewior L ^= S6[0xff & (B >> 8)]; B >>= 16; \ 602*c5a511f1SSebastian Siewior L ^= S7[0xff & A]; \ 603*c5a511f1SSebastian Siewior L ^= S5[0xff & (A >> 8)]; A >>= 16; \ 604*c5a511f1SSebastian Siewior L ^= S4[0xff & B]; \ 605*c5a511f1SSebastian Siewior L ^= S2[0xff & (B >> 8)]; \ 606*c5a511f1SSebastian Siewior L ^= S3[0xff & A]; \ 607*c5a511f1SSebastian Siewior L ^= S1[0xff & (A >> 8)]; 608*c5a511f1SSebastian Siewior 609*c5a511f1SSebastian Siewior /* 610*c5a511f1SSebastian Siewior * PC2 lookup tables are organized as 2 consecutive sets of 4 interleaved 611*c5a511f1SSebastian Siewior * tables of 128 elements. One set is for C_i and the other for D_i, while 612*c5a511f1SSebastian Siewior * the 4 interleaved tables correspond to four 7-bit subsets of C_i or D_i. 613*c5a511f1SSebastian Siewior * 614*c5a511f1SSebastian Siewior * After PC1 each of the variables a,b,c,d contains a 7 bit subset of C_i 615*c5a511f1SSebastian Siewior * or D_i in bits 7-1 (bit 0 being the least significant). 616*c5a511f1SSebastian Siewior */ 617*c5a511f1SSebastian Siewior 618*c5a511f1SSebastian Siewior #define T1(x) pt[2 * (x) + 0] 619*c5a511f1SSebastian Siewior #define T2(x) pt[2 * (x) + 1] 620*c5a511f1SSebastian Siewior #define T3(x) pt[2 * (x) + 2] 621*c5a511f1SSebastian Siewior #define T4(x) pt[2 * (x) + 3] 622*c5a511f1SSebastian Siewior 623*c5a511f1SSebastian Siewior #define PC2(a, b, c, d) (T4(d) | T3(c) | T2(b) | T1(a)) 624*c5a511f1SSebastian Siewior 625*c5a511f1SSebastian Siewior /* 626*c5a511f1SSebastian Siewior * Encryption key expansion 627*c5a511f1SSebastian Siewior * 628*c5a511f1SSebastian Siewior * RFC2451: Weak key checks SHOULD be performed. 629*c5a511f1SSebastian Siewior * 630*c5a511f1SSebastian Siewior * FIPS 74: 631*c5a511f1SSebastian Siewior * 632*c5a511f1SSebastian Siewior * Keys having duals are keys which produce all zeros, all ones, or 633*c5a511f1SSebastian Siewior * alternating zero-one patterns in the C and D registers after Permuted 634*c5a511f1SSebastian Siewior * Choice 1 has operated on the key. 635*c5a511f1SSebastian Siewior * 636*c5a511f1SSebastian Siewior */ 637*c5a511f1SSebastian Siewior static unsigned long ekey(u32 *pe, const u8 *k) 638*c5a511f1SSebastian Siewior { 639*c5a511f1SSebastian Siewior /* K&R: long is at least 32 bits */ 640*c5a511f1SSebastian Siewior unsigned long a, b, c, d, w; 641*c5a511f1SSebastian Siewior const u32 *pt = pc2; 642*c5a511f1SSebastian Siewior 643*c5a511f1SSebastian Siewior d = k[4]; d &= 0x0e; d <<= 4; d |= k[0] & 0x1e; d = pc1[d]; 644*c5a511f1SSebastian Siewior c = k[5]; c &= 0x0e; c <<= 4; c |= k[1] & 0x1e; c = pc1[c]; 645*c5a511f1SSebastian Siewior b = k[6]; b &= 0x0e; b <<= 4; b |= k[2] & 0x1e; b = pc1[b]; 646*c5a511f1SSebastian Siewior a = k[7]; a &= 0x0e; a <<= 4; a |= k[3] & 0x1e; a = pc1[a]; 647*c5a511f1SSebastian Siewior 648*c5a511f1SSebastian Siewior pe[15 * 2 + 0] = PC2(a, b, c, d); d = rs[d]; 649*c5a511f1SSebastian Siewior pe[14 * 2 + 0] = PC2(d, a, b, c); c = rs[c]; b = rs[b]; 650*c5a511f1SSebastian Siewior pe[13 * 2 + 0] = PC2(b, c, d, a); a = rs[a]; d = rs[d]; 651*c5a511f1SSebastian Siewior pe[12 * 2 + 0] = PC2(d, a, b, c); c = rs[c]; b = rs[b]; 652*c5a511f1SSebastian Siewior pe[11 * 2 + 0] = PC2(b, c, d, a); a = rs[a]; d = rs[d]; 653*c5a511f1SSebastian Siewior pe[10 * 2 + 0] = PC2(d, a, b, c); c = rs[c]; b = rs[b]; 654*c5a511f1SSebastian Siewior pe[ 9 * 2 + 0] = PC2(b, c, d, a); a = rs[a]; d = rs[d]; 655*c5a511f1SSebastian Siewior pe[ 8 * 2 + 0] = PC2(d, a, b, c); c = rs[c]; 656*c5a511f1SSebastian Siewior pe[ 7 * 2 + 0] = PC2(c, d, a, b); b = rs[b]; a = rs[a]; 657*c5a511f1SSebastian Siewior pe[ 6 * 2 + 0] = PC2(a, b, c, d); d = rs[d]; c = rs[c]; 658*c5a511f1SSebastian Siewior pe[ 5 * 2 + 0] = PC2(c, d, a, b); b = rs[b]; a = rs[a]; 659*c5a511f1SSebastian Siewior pe[ 4 * 2 + 0] = PC2(a, b, c, d); d = rs[d]; c = rs[c]; 660*c5a511f1SSebastian Siewior pe[ 3 * 2 + 0] = PC2(c, d, a, b); b = rs[b]; a = rs[a]; 661*c5a511f1SSebastian Siewior pe[ 2 * 2 + 0] = PC2(a, b, c, d); d = rs[d]; c = rs[c]; 662*c5a511f1SSebastian Siewior pe[ 1 * 2 + 0] = PC2(c, d, a, b); b = rs[b]; 663*c5a511f1SSebastian Siewior pe[ 0 * 2 + 0] = PC2(b, c, d, a); 664*c5a511f1SSebastian Siewior 665*c5a511f1SSebastian Siewior /* Check if first half is weak */ 666*c5a511f1SSebastian Siewior w = (a ^ c) | (b ^ d) | (rs[a] ^ c) | (b ^ rs[d]); 667*c5a511f1SSebastian Siewior 668*c5a511f1SSebastian Siewior /* Skip to next table set */ 669*c5a511f1SSebastian Siewior pt += 512; 670*c5a511f1SSebastian Siewior 671*c5a511f1SSebastian Siewior d = k[0]; d &= 0xe0; d >>= 4; d |= k[4] & 0xf0; d = pc1[d + 1]; 672*c5a511f1SSebastian Siewior c = k[1]; c &= 0xe0; c >>= 4; c |= k[5] & 0xf0; c = pc1[c + 1]; 673*c5a511f1SSebastian Siewior b = k[2]; b &= 0xe0; b >>= 4; b |= k[6] & 0xf0; b = pc1[b + 1]; 674*c5a511f1SSebastian Siewior a = k[3]; a &= 0xe0; a >>= 4; a |= k[7] & 0xf0; a = pc1[a + 1]; 675*c5a511f1SSebastian Siewior 676*c5a511f1SSebastian Siewior /* Check if second half is weak */ 677*c5a511f1SSebastian Siewior w |= (a ^ c) | (b ^ d) | (rs[a] ^ c) | (b ^ rs[d]); 678*c5a511f1SSebastian Siewior 679*c5a511f1SSebastian Siewior pe[15 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; 680*c5a511f1SSebastian Siewior pe[14 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; b = rs[b]; 681*c5a511f1SSebastian Siewior pe[13 * 2 + 1] = PC2(b, c, d, a); a = rs[a]; d = rs[d]; 682*c5a511f1SSebastian Siewior pe[12 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; b = rs[b]; 683*c5a511f1SSebastian Siewior pe[11 * 2 + 1] = PC2(b, c, d, a); a = rs[a]; d = rs[d]; 684*c5a511f1SSebastian Siewior pe[10 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; b = rs[b]; 685*c5a511f1SSebastian Siewior pe[ 9 * 2 + 1] = PC2(b, c, d, a); a = rs[a]; d = rs[d]; 686*c5a511f1SSebastian Siewior pe[ 8 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; 687*c5a511f1SSebastian Siewior pe[ 7 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; a = rs[a]; 688*c5a511f1SSebastian Siewior pe[ 6 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; c = rs[c]; 689*c5a511f1SSebastian Siewior pe[ 5 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; a = rs[a]; 690*c5a511f1SSebastian Siewior pe[ 4 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; c = rs[c]; 691*c5a511f1SSebastian Siewior pe[ 3 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; a = rs[a]; 692*c5a511f1SSebastian Siewior pe[ 2 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; c = rs[c]; 693*c5a511f1SSebastian Siewior pe[ 1 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; 694*c5a511f1SSebastian Siewior pe[ 0 * 2 + 1] = PC2(b, c, d, a); 695*c5a511f1SSebastian Siewior 696*c5a511f1SSebastian Siewior /* Fixup: 2413 5768 -> 1357 2468 */ 697*c5a511f1SSebastian Siewior for (d = 0; d < 16; ++d) { 698*c5a511f1SSebastian Siewior a = pe[2 * d]; 699*c5a511f1SSebastian Siewior b = pe[2 * d + 1]; 700*c5a511f1SSebastian Siewior c = a ^ b; 701*c5a511f1SSebastian Siewior c &= 0xffff0000; 702*c5a511f1SSebastian Siewior a ^= c; 703*c5a511f1SSebastian Siewior b ^= c; 704*c5a511f1SSebastian Siewior ROL(b, 18); 705*c5a511f1SSebastian Siewior pe[2 * d] = a; 706*c5a511f1SSebastian Siewior pe[2 * d + 1] = b; 707*c5a511f1SSebastian Siewior } 708*c5a511f1SSebastian Siewior 709*c5a511f1SSebastian Siewior /* Zero if weak key */ 710*c5a511f1SSebastian Siewior return w; 711*c5a511f1SSebastian Siewior } 712*c5a511f1SSebastian Siewior 713*c5a511f1SSebastian Siewior /* 714*c5a511f1SSebastian Siewior * Decryption key expansion 715*c5a511f1SSebastian Siewior * 716*c5a511f1SSebastian Siewior * No weak key checking is performed, as this is only used by triple DES 717*c5a511f1SSebastian Siewior * 718*c5a511f1SSebastian Siewior */ 719*c5a511f1SSebastian Siewior static void dkey(u32 *pe, const u8 *k) 720*c5a511f1SSebastian Siewior { 721*c5a511f1SSebastian Siewior /* K&R: long is at least 32 bits */ 722*c5a511f1SSebastian Siewior unsigned long a, b, c, d; 723*c5a511f1SSebastian Siewior const u32 *pt = pc2; 724*c5a511f1SSebastian Siewior 725*c5a511f1SSebastian Siewior d = k[4]; d &= 0x0e; d <<= 4; d |= k[0] & 0x1e; d = pc1[d]; 726*c5a511f1SSebastian Siewior c = k[5]; c &= 0x0e; c <<= 4; c |= k[1] & 0x1e; c = pc1[c]; 727*c5a511f1SSebastian Siewior b = k[6]; b &= 0x0e; b <<= 4; b |= k[2] & 0x1e; b = pc1[b]; 728*c5a511f1SSebastian Siewior a = k[7]; a &= 0x0e; a <<= 4; a |= k[3] & 0x1e; a = pc1[a]; 729*c5a511f1SSebastian Siewior 730*c5a511f1SSebastian Siewior pe[ 0 * 2] = PC2(a, b, c, d); d = rs[d]; 731*c5a511f1SSebastian Siewior pe[ 1 * 2] = PC2(d, a, b, c); c = rs[c]; b = rs[b]; 732*c5a511f1SSebastian Siewior pe[ 2 * 2] = PC2(b, c, d, a); a = rs[a]; d = rs[d]; 733*c5a511f1SSebastian Siewior pe[ 3 * 2] = PC2(d, a, b, c); c = rs[c]; b = rs[b]; 734*c5a511f1SSebastian Siewior pe[ 4 * 2] = PC2(b, c, d, a); a = rs[a]; d = rs[d]; 735*c5a511f1SSebastian Siewior pe[ 5 * 2] = PC2(d, a, b, c); c = rs[c]; b = rs[b]; 736*c5a511f1SSebastian Siewior pe[ 6 * 2] = PC2(b, c, d, a); a = rs[a]; d = rs[d]; 737*c5a511f1SSebastian Siewior pe[ 7 * 2] = PC2(d, a, b, c); c = rs[c]; 738*c5a511f1SSebastian Siewior pe[ 8 * 2] = PC2(c, d, a, b); b = rs[b]; a = rs[a]; 739*c5a511f1SSebastian Siewior pe[ 9 * 2] = PC2(a, b, c, d); d = rs[d]; c = rs[c]; 740*c5a511f1SSebastian Siewior pe[10 * 2] = PC2(c, d, a, b); b = rs[b]; a = rs[a]; 741*c5a511f1SSebastian Siewior pe[11 * 2] = PC2(a, b, c, d); d = rs[d]; c = rs[c]; 742*c5a511f1SSebastian Siewior pe[12 * 2] = PC2(c, d, a, b); b = rs[b]; a = rs[a]; 743*c5a511f1SSebastian Siewior pe[13 * 2] = PC2(a, b, c, d); d = rs[d]; c = rs[c]; 744*c5a511f1SSebastian Siewior pe[14 * 2] = PC2(c, d, a, b); b = rs[b]; 745*c5a511f1SSebastian Siewior pe[15 * 2] = PC2(b, c, d, a); 746*c5a511f1SSebastian Siewior 747*c5a511f1SSebastian Siewior /* Skip to next table set */ 748*c5a511f1SSebastian Siewior pt += 512; 749*c5a511f1SSebastian Siewior 750*c5a511f1SSebastian Siewior d = k[0]; d &= 0xe0; d >>= 4; d |= k[4] & 0xf0; d = pc1[d + 1]; 751*c5a511f1SSebastian Siewior c = k[1]; c &= 0xe0; c >>= 4; c |= k[5] & 0xf0; c = pc1[c + 1]; 752*c5a511f1SSebastian Siewior b = k[2]; b &= 0xe0; b >>= 4; b |= k[6] & 0xf0; b = pc1[b + 1]; 753*c5a511f1SSebastian Siewior a = k[3]; a &= 0xe0; a >>= 4; a |= k[7] & 0xf0; a = pc1[a + 1]; 754*c5a511f1SSebastian Siewior 755*c5a511f1SSebastian Siewior pe[ 0 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; 756*c5a511f1SSebastian Siewior pe[ 1 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; b = rs[b]; 757*c5a511f1SSebastian Siewior pe[ 2 * 2 + 1] = PC2(b, c, d, a); a = rs[a]; d = rs[d]; 758*c5a511f1SSebastian Siewior pe[ 3 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; b = rs[b]; 759*c5a511f1SSebastian Siewior pe[ 4 * 2 + 1] = PC2(b, c, d, a); a = rs[a]; d = rs[d]; 760*c5a511f1SSebastian Siewior pe[ 5 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; b = rs[b]; 761*c5a511f1SSebastian Siewior pe[ 6 * 2 + 1] = PC2(b, c, d, a); a = rs[a]; d = rs[d]; 762*c5a511f1SSebastian Siewior pe[ 7 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; 763*c5a511f1SSebastian Siewior pe[ 8 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; a = rs[a]; 764*c5a511f1SSebastian Siewior pe[ 9 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; c = rs[c]; 765*c5a511f1SSebastian Siewior pe[10 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; a = rs[a]; 766*c5a511f1SSebastian Siewior pe[11 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; c = rs[c]; 767*c5a511f1SSebastian Siewior pe[12 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; a = rs[a]; 768*c5a511f1SSebastian Siewior pe[13 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; c = rs[c]; 769*c5a511f1SSebastian Siewior pe[14 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; 770*c5a511f1SSebastian Siewior pe[15 * 2 + 1] = PC2(b, c, d, a); 771*c5a511f1SSebastian Siewior 772*c5a511f1SSebastian Siewior /* Fixup: 2413 5768 -> 1357 2468 */ 773*c5a511f1SSebastian Siewior for (d = 0; d < 16; ++d) { 774*c5a511f1SSebastian Siewior a = pe[2 * d]; 775*c5a511f1SSebastian Siewior b = pe[2 * d + 1]; 776*c5a511f1SSebastian Siewior c = a ^ b; 777*c5a511f1SSebastian Siewior c &= 0xffff0000; 778*c5a511f1SSebastian Siewior a ^= c; 779*c5a511f1SSebastian Siewior b ^= c; 780*c5a511f1SSebastian Siewior ROL(b, 18); 781*c5a511f1SSebastian Siewior pe[2 * d] = a; 782*c5a511f1SSebastian Siewior pe[2 * d + 1] = b; 783*c5a511f1SSebastian Siewior } 784*c5a511f1SSebastian Siewior } 785*c5a511f1SSebastian Siewior 786*c5a511f1SSebastian Siewior static int des_setkey(struct crypto_tfm *tfm, const u8 *key, 787*c5a511f1SSebastian Siewior unsigned int keylen) 788*c5a511f1SSebastian Siewior { 789*c5a511f1SSebastian Siewior struct des_ctx *dctx = crypto_tfm_ctx(tfm); 790*c5a511f1SSebastian Siewior u32 *flags = &tfm->crt_flags; 791*c5a511f1SSebastian Siewior u32 tmp[DES_EXPKEY_WORDS]; 792*c5a511f1SSebastian Siewior int ret; 793*c5a511f1SSebastian Siewior 794*c5a511f1SSebastian Siewior /* Expand to tmp */ 795*c5a511f1SSebastian Siewior ret = ekey(tmp, key); 796*c5a511f1SSebastian Siewior 797*c5a511f1SSebastian Siewior if (unlikely(ret == 0) && (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) { 798*c5a511f1SSebastian Siewior *flags |= CRYPTO_TFM_RES_WEAK_KEY; 799*c5a511f1SSebastian Siewior return -EINVAL; 800*c5a511f1SSebastian Siewior } 801*c5a511f1SSebastian Siewior 802*c5a511f1SSebastian Siewior /* Copy to output */ 803*c5a511f1SSebastian Siewior memcpy(dctx->expkey, tmp, sizeof(dctx->expkey)); 804*c5a511f1SSebastian Siewior 805*c5a511f1SSebastian Siewior return 0; 806*c5a511f1SSebastian Siewior } 807*c5a511f1SSebastian Siewior 808*c5a511f1SSebastian Siewior static void des_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 809*c5a511f1SSebastian Siewior { 810*c5a511f1SSebastian Siewior struct des_ctx *ctx = crypto_tfm_ctx(tfm); 811*c5a511f1SSebastian Siewior const u32 *K = ctx->expkey; 812*c5a511f1SSebastian Siewior const __le32 *s = (const __le32 *)src; 813*c5a511f1SSebastian Siewior __le32 *d = (__le32 *)dst; 814*c5a511f1SSebastian Siewior u32 L, R, A, B; 815*c5a511f1SSebastian Siewior int i; 816*c5a511f1SSebastian Siewior 817*c5a511f1SSebastian Siewior L = le32_to_cpu(s[0]); 818*c5a511f1SSebastian Siewior R = le32_to_cpu(s[1]); 819*c5a511f1SSebastian Siewior 820*c5a511f1SSebastian Siewior IP(L, R, A); 821*c5a511f1SSebastian Siewior for (i = 0; i < 8; i++) { 822*c5a511f1SSebastian Siewior ROUND(L, R, A, B, K, 2); 823*c5a511f1SSebastian Siewior ROUND(R, L, A, B, K, 2); 824*c5a511f1SSebastian Siewior } 825*c5a511f1SSebastian Siewior FP(R, L, A); 826*c5a511f1SSebastian Siewior 827*c5a511f1SSebastian Siewior d[0] = cpu_to_le32(R); 828*c5a511f1SSebastian Siewior d[1] = cpu_to_le32(L); 829*c5a511f1SSebastian Siewior } 830*c5a511f1SSebastian Siewior 831*c5a511f1SSebastian Siewior static void des_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 832*c5a511f1SSebastian Siewior { 833*c5a511f1SSebastian Siewior struct des_ctx *ctx = crypto_tfm_ctx(tfm); 834*c5a511f1SSebastian Siewior const u32 *K = ctx->expkey + DES_EXPKEY_WORDS - 2; 835*c5a511f1SSebastian Siewior const __le32 *s = (const __le32 *)src; 836*c5a511f1SSebastian Siewior __le32 *d = (__le32 *)dst; 837*c5a511f1SSebastian Siewior u32 L, R, A, B; 838*c5a511f1SSebastian Siewior int i; 839*c5a511f1SSebastian Siewior 840*c5a511f1SSebastian Siewior L = le32_to_cpu(s[0]); 841*c5a511f1SSebastian Siewior R = le32_to_cpu(s[1]); 842*c5a511f1SSebastian Siewior 843*c5a511f1SSebastian Siewior IP(L, R, A); 844*c5a511f1SSebastian Siewior for (i = 0; i < 8; i++) { 845*c5a511f1SSebastian Siewior ROUND(L, R, A, B, K, -2); 846*c5a511f1SSebastian Siewior ROUND(R, L, A, B, K, -2); 847*c5a511f1SSebastian Siewior } 848*c5a511f1SSebastian Siewior FP(R, L, A); 849*c5a511f1SSebastian Siewior 850*c5a511f1SSebastian Siewior d[0] = cpu_to_le32(R); 851*c5a511f1SSebastian Siewior d[1] = cpu_to_le32(L); 852*c5a511f1SSebastian Siewior } 853*c5a511f1SSebastian Siewior 854*c5a511f1SSebastian Siewior /* 855*c5a511f1SSebastian Siewior * RFC2451: 856*c5a511f1SSebastian Siewior * 857*c5a511f1SSebastian Siewior * For DES-EDE3, there is no known need to reject weak or 858*c5a511f1SSebastian Siewior * complementation keys. Any weakness is obviated by the use of 859*c5a511f1SSebastian Siewior * multiple keys. 860*c5a511f1SSebastian Siewior * 861*c5a511f1SSebastian Siewior * However, if the first two or last two independent 64-bit keys are 862*c5a511f1SSebastian Siewior * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the 863*c5a511f1SSebastian Siewior * same as DES. Implementers MUST reject keys that exhibit this 864*c5a511f1SSebastian Siewior * property. 865*c5a511f1SSebastian Siewior * 866*c5a511f1SSebastian Siewior */ 867*c5a511f1SSebastian Siewior static int des3_ede_setkey(struct crypto_tfm *tfm, const u8 *key, 868*c5a511f1SSebastian Siewior unsigned int keylen) 869*c5a511f1SSebastian Siewior { 870*c5a511f1SSebastian Siewior const u32 *K = (const u32 *)key; 871*c5a511f1SSebastian Siewior struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm); 872*c5a511f1SSebastian Siewior u32 *expkey = dctx->expkey; 873*c5a511f1SSebastian Siewior u32 *flags = &tfm->crt_flags; 874*c5a511f1SSebastian Siewior 875*c5a511f1SSebastian Siewior if (unlikely(!((K[0] ^ K[2]) | (K[1] ^ K[3])) || 876*c5a511f1SSebastian Siewior !((K[2] ^ K[4]) | (K[3] ^ K[5])))) 877*c5a511f1SSebastian Siewior { 878*c5a511f1SSebastian Siewior *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED; 879*c5a511f1SSebastian Siewior return -EINVAL; 880*c5a511f1SSebastian Siewior } 881*c5a511f1SSebastian Siewior 882*c5a511f1SSebastian Siewior ekey(expkey, key); expkey += DES_EXPKEY_WORDS; key += DES_KEY_SIZE; 883*c5a511f1SSebastian Siewior dkey(expkey, key); expkey += DES_EXPKEY_WORDS; key += DES_KEY_SIZE; 884*c5a511f1SSebastian Siewior ekey(expkey, key); 885*c5a511f1SSebastian Siewior 886*c5a511f1SSebastian Siewior return 0; 887*c5a511f1SSebastian Siewior } 888*c5a511f1SSebastian Siewior 889*c5a511f1SSebastian Siewior static void des3_ede_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 890*c5a511f1SSebastian Siewior { 891*c5a511f1SSebastian Siewior struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm); 892*c5a511f1SSebastian Siewior const u32 *K = dctx->expkey; 893*c5a511f1SSebastian Siewior const __le32 *s = (const __le32 *)src; 894*c5a511f1SSebastian Siewior __le32 *d = (__le32 *)dst; 895*c5a511f1SSebastian Siewior u32 L, R, A, B; 896*c5a511f1SSebastian Siewior int i; 897*c5a511f1SSebastian Siewior 898*c5a511f1SSebastian Siewior L = le32_to_cpu(s[0]); 899*c5a511f1SSebastian Siewior R = le32_to_cpu(s[1]); 900*c5a511f1SSebastian Siewior 901*c5a511f1SSebastian Siewior IP(L, R, A); 902*c5a511f1SSebastian Siewior for (i = 0; i < 8; i++) { 903*c5a511f1SSebastian Siewior ROUND(L, R, A, B, K, 2); 904*c5a511f1SSebastian Siewior ROUND(R, L, A, B, K, 2); 905*c5a511f1SSebastian Siewior } 906*c5a511f1SSebastian Siewior for (i = 0; i < 8; i++) { 907*c5a511f1SSebastian Siewior ROUND(R, L, A, B, K, 2); 908*c5a511f1SSebastian Siewior ROUND(L, R, A, B, K, 2); 909*c5a511f1SSebastian Siewior } 910*c5a511f1SSebastian Siewior for (i = 0; i < 8; i++) { 911*c5a511f1SSebastian Siewior ROUND(L, R, A, B, K, 2); 912*c5a511f1SSebastian Siewior ROUND(R, L, A, B, K, 2); 913*c5a511f1SSebastian Siewior } 914*c5a511f1SSebastian Siewior FP(R, L, A); 915*c5a511f1SSebastian Siewior 916*c5a511f1SSebastian Siewior d[0] = cpu_to_le32(R); 917*c5a511f1SSebastian Siewior d[1] = cpu_to_le32(L); 918*c5a511f1SSebastian Siewior } 919*c5a511f1SSebastian Siewior 920*c5a511f1SSebastian Siewior static void des3_ede_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 921*c5a511f1SSebastian Siewior { 922*c5a511f1SSebastian Siewior struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm); 923*c5a511f1SSebastian Siewior const u32 *K = dctx->expkey + DES3_EDE_EXPKEY_WORDS - 2; 924*c5a511f1SSebastian Siewior const __le32 *s = (const __le32 *)src; 925*c5a511f1SSebastian Siewior __le32 *d = (__le32 *)dst; 926*c5a511f1SSebastian Siewior u32 L, R, A, B; 927*c5a511f1SSebastian Siewior int i; 928*c5a511f1SSebastian Siewior 929*c5a511f1SSebastian Siewior L = le32_to_cpu(s[0]); 930*c5a511f1SSebastian Siewior R = le32_to_cpu(s[1]); 931*c5a511f1SSebastian Siewior 932*c5a511f1SSebastian Siewior IP(L, R, A); 933*c5a511f1SSebastian Siewior for (i = 0; i < 8; i++) { 934*c5a511f1SSebastian Siewior ROUND(L, R, A, B, K, -2); 935*c5a511f1SSebastian Siewior ROUND(R, L, A, B, K, -2); 936*c5a511f1SSebastian Siewior } 937*c5a511f1SSebastian Siewior for (i = 0; i < 8; i++) { 938*c5a511f1SSebastian Siewior ROUND(R, L, A, B, K, -2); 939*c5a511f1SSebastian Siewior ROUND(L, R, A, B, K, -2); 940*c5a511f1SSebastian Siewior } 941*c5a511f1SSebastian Siewior for (i = 0; i < 8; i++) { 942*c5a511f1SSebastian Siewior ROUND(L, R, A, B, K, -2); 943*c5a511f1SSebastian Siewior ROUND(R, L, A, B, K, -2); 944*c5a511f1SSebastian Siewior } 945*c5a511f1SSebastian Siewior FP(R, L, A); 946*c5a511f1SSebastian Siewior 947*c5a511f1SSebastian Siewior d[0] = cpu_to_le32(R); 948*c5a511f1SSebastian Siewior d[1] = cpu_to_le32(L); 949*c5a511f1SSebastian Siewior } 950*c5a511f1SSebastian Siewior 951*c5a511f1SSebastian Siewior static struct crypto_alg des_alg = { 952*c5a511f1SSebastian Siewior .cra_name = "des", 953*c5a511f1SSebastian Siewior .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 954*c5a511f1SSebastian Siewior .cra_blocksize = DES_BLOCK_SIZE, 955*c5a511f1SSebastian Siewior .cra_ctxsize = sizeof(struct des_ctx), 956*c5a511f1SSebastian Siewior .cra_module = THIS_MODULE, 957*c5a511f1SSebastian Siewior .cra_alignmask = 3, 958*c5a511f1SSebastian Siewior .cra_list = LIST_HEAD_INIT(des_alg.cra_list), 959*c5a511f1SSebastian Siewior .cra_u = { .cipher = { 960*c5a511f1SSebastian Siewior .cia_min_keysize = DES_KEY_SIZE, 961*c5a511f1SSebastian Siewior .cia_max_keysize = DES_KEY_SIZE, 962*c5a511f1SSebastian Siewior .cia_setkey = des_setkey, 963*c5a511f1SSebastian Siewior .cia_encrypt = des_encrypt, 964*c5a511f1SSebastian Siewior .cia_decrypt = des_decrypt } } 965*c5a511f1SSebastian Siewior }; 966*c5a511f1SSebastian Siewior 967*c5a511f1SSebastian Siewior static struct crypto_alg des3_ede_alg = { 968*c5a511f1SSebastian Siewior .cra_name = "des3_ede", 969*c5a511f1SSebastian Siewior .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 970*c5a511f1SSebastian Siewior .cra_blocksize = DES3_EDE_BLOCK_SIZE, 971*c5a511f1SSebastian Siewior .cra_ctxsize = sizeof(struct des3_ede_ctx), 972*c5a511f1SSebastian Siewior .cra_module = THIS_MODULE, 973*c5a511f1SSebastian Siewior .cra_alignmask = 3, 974*c5a511f1SSebastian Siewior .cra_list = LIST_HEAD_INIT(des3_ede_alg.cra_list), 975*c5a511f1SSebastian Siewior .cra_u = { .cipher = { 976*c5a511f1SSebastian Siewior .cia_min_keysize = DES3_EDE_KEY_SIZE, 977*c5a511f1SSebastian Siewior .cia_max_keysize = DES3_EDE_KEY_SIZE, 978*c5a511f1SSebastian Siewior .cia_setkey = des3_ede_setkey, 979*c5a511f1SSebastian Siewior .cia_encrypt = des3_ede_encrypt, 980*c5a511f1SSebastian Siewior .cia_decrypt = des3_ede_decrypt } } 981*c5a511f1SSebastian Siewior }; 982*c5a511f1SSebastian Siewior 983*c5a511f1SSebastian Siewior MODULE_ALIAS("des3_ede"); 984*c5a511f1SSebastian Siewior 985*c5a511f1SSebastian Siewior static int __init init(void) 986*c5a511f1SSebastian Siewior { 987*c5a511f1SSebastian Siewior int ret = 0; 988*c5a511f1SSebastian Siewior 989*c5a511f1SSebastian Siewior ret = crypto_register_alg(&des_alg); 990*c5a511f1SSebastian Siewior if (ret < 0) 991*c5a511f1SSebastian Siewior goto out; 992*c5a511f1SSebastian Siewior 993*c5a511f1SSebastian Siewior ret = crypto_register_alg(&des3_ede_alg); 994*c5a511f1SSebastian Siewior if (ret < 0) 995*c5a511f1SSebastian Siewior crypto_unregister_alg(&des_alg); 996*c5a511f1SSebastian Siewior out: 997*c5a511f1SSebastian Siewior return ret; 998*c5a511f1SSebastian Siewior } 999*c5a511f1SSebastian Siewior 1000*c5a511f1SSebastian Siewior static void __exit fini(void) 1001*c5a511f1SSebastian Siewior { 1002*c5a511f1SSebastian Siewior crypto_unregister_alg(&des3_ede_alg); 1003*c5a511f1SSebastian Siewior crypto_unregister_alg(&des_alg); 1004*c5a511f1SSebastian Siewior } 1005*c5a511f1SSebastian Siewior 1006*c5a511f1SSebastian Siewior module_init(init); 1007*c5a511f1SSebastian Siewior module_exit(fini); 1008*c5a511f1SSebastian Siewior 1009*c5a511f1SSebastian Siewior MODULE_LICENSE("GPL"); 1010*c5a511f1SSebastian Siewior MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms"); 1011*c5a511f1SSebastian Siewior MODULE_AUTHOR("Dag Arne Osvik <da@osvik.no>"); 1012*c5a511f1SSebastian Siewior MODULE_ALIAS("des"); 1013