des.c (17496B)
1 #include "os.h" 2 #include "libsec.h" 3 4 /* 5 * integrated sbox & p perm 6 */ 7 static uint32 spbox[] = { 8 9 0x00808200,0x00000000,0x00008000,0x00808202,0x00808002,0x00008202,0x00000002,0x00008000, 10 0x00000200,0x00808200,0x00808202,0x00000200,0x00800202,0x00808002,0x00800000,0x00000002, 11 0x00000202,0x00800200,0x00800200,0x00008200,0x00008200,0x00808000,0x00808000,0x00800202, 12 0x00008002,0x00800002,0x00800002,0x00008002,0x00000000,0x00000202,0x00008202,0x00800000, 13 0x00008000,0x00808202,0x00000002,0x00808000,0x00808200,0x00800000,0x00800000,0x00000200, 14 0x00808002,0x00008000,0x00008200,0x00800002,0x00000200,0x00000002,0x00800202,0x00008202, 15 0x00808202,0x00008002,0x00808000,0x00800202,0x00800002,0x00000202,0x00008202,0x00808200, 16 0x00000202,0x00800200,0x00800200,0x00000000,0x00008002,0x00008200,0x00000000,0x00808002, 17 18 0x40084010,0x40004000,0x00004000,0x00084010,0x00080000,0x00000010,0x40080010,0x40004010, 19 0x40000010,0x40084010,0x40084000,0x40000000,0x40004000,0x00080000,0x00000010,0x40080010, 20 0x00084000,0x00080010,0x40004010,0x00000000,0x40000000,0x00004000,0x00084010,0x40080000, 21 0x00080010,0x40000010,0x00000000,0x00084000,0x00004010,0x40084000,0x40080000,0x00004010, 22 0x00000000,0x00084010,0x40080010,0x00080000,0x40004010,0x40080000,0x40084000,0x00004000, 23 0x40080000,0x40004000,0x00000010,0x40084010,0x00084010,0x00000010,0x00004000,0x40000000, 24 0x00004010,0x40084000,0x00080000,0x40000010,0x00080010,0x40004010,0x40000010,0x00080010, 25 0x00084000,0x00000000,0x40004000,0x00004010,0x40000000,0x40080010,0x40084010,0x00084000, 26 27 0x00000104,0x04010100,0x00000000,0x04010004,0x04000100,0x00000000,0x00010104,0x04000100, 28 0x00010004,0x04000004,0x04000004,0x00010000,0x04010104,0x00010004,0x04010000,0x00000104, 29 0x04000000,0x00000004,0x04010100,0x00000100,0x00010100,0x04010000,0x04010004,0x00010104, 30 0x04000104,0x00010100,0x00010000,0x04000104,0x00000004,0x04010104,0x00000100,0x04000000, 31 0x04010100,0x04000000,0x00010004,0x00000104,0x00010000,0x04010100,0x04000100,0x00000000, 32 0x00000100,0x00010004,0x04010104,0x04000100,0x04000004,0x00000100,0x00000000,0x04010004, 33 0x04000104,0x00010000,0x04000000,0x04010104,0x00000004,0x00010104,0x00010100,0x04000004, 34 0x04010000,0x04000104,0x00000104,0x04010000,0x00010104,0x00000004,0x04010004,0x00010100, 35 36 0x80401000,0x80001040,0x80001040,0x00000040,0x00401040,0x80400040,0x80400000,0x80001000, 37 0x00000000,0x00401000,0x00401000,0x80401040,0x80000040,0x00000000,0x00400040,0x80400000, 38 0x80000000,0x00001000,0x00400000,0x80401000,0x00000040,0x00400000,0x80001000,0x00001040, 39 0x80400040,0x80000000,0x00001040,0x00400040,0x00001000,0x00401040,0x80401040,0x80000040, 40 0x00400040,0x80400000,0x00401000,0x80401040,0x80000040,0x00000000,0x00000000,0x00401000, 41 0x00001040,0x00400040,0x80400040,0x80000000,0x80401000,0x80001040,0x80001040,0x00000040, 42 0x80401040,0x80000040,0x80000000,0x00001000,0x80400000,0x80001000,0x00401040,0x80400040, 43 0x80001000,0x00001040,0x00400000,0x80401000,0x00000040,0x00400000,0x00001000,0x00401040, 44 45 0x00000080,0x01040080,0x01040000,0x21000080,0x00040000,0x00000080,0x20000000,0x01040000, 46 0x20040080,0x00040000,0x01000080,0x20040080,0x21000080,0x21040000,0x00040080,0x20000000, 47 0x01000000,0x20040000,0x20040000,0x00000000,0x20000080,0x21040080,0x21040080,0x01000080, 48 0x21040000,0x20000080,0x00000000,0x21000000,0x01040080,0x01000000,0x21000000,0x00040080, 49 0x00040000,0x21000080,0x00000080,0x01000000,0x20000000,0x01040000,0x21000080,0x20040080, 50 0x01000080,0x20000000,0x21040000,0x01040080,0x20040080,0x00000080,0x01000000,0x21040000, 51 0x21040080,0x00040080,0x21000000,0x21040080,0x01040000,0x00000000,0x20040000,0x21000000, 52 0x00040080,0x01000080,0x20000080,0x00040000,0x00000000,0x20040000,0x01040080,0x20000080, 53 54 0x10000008,0x10200000,0x00002000,0x10202008,0x10200000,0x00000008,0x10202008,0x00200000, 55 0x10002000,0x00202008,0x00200000,0x10000008,0x00200008,0x10002000,0x10000000,0x00002008, 56 0x00000000,0x00200008,0x10002008,0x00002000,0x00202000,0x10002008,0x00000008,0x10200008, 57 0x10200008,0x00000000,0x00202008,0x10202000,0x00002008,0x00202000,0x10202000,0x10000000, 58 0x10002000,0x00000008,0x10200008,0x00202000,0x10202008,0x00200000,0x00002008,0x10000008, 59 0x00200000,0x10002000,0x10000000,0x00002008,0x10000008,0x10202008,0x00202000,0x10200000, 60 0x00202008,0x10202000,0x00000000,0x10200008,0x00000008,0x00002000,0x10200000,0x00202008, 61 0x00002000,0x00200008,0x10002008,0x00000000,0x10202000,0x10000000,0x00200008,0x10002008, 62 63 0x00100000,0x02100001,0x02000401,0x00000000,0x00000400,0x02000401,0x00100401,0x02100400, 64 0x02100401,0x00100000,0x00000000,0x02000001,0x00000001,0x02000000,0x02100001,0x00000401, 65 0x02000400,0x00100401,0x00100001,0x02000400,0x02000001,0x02100000,0x02100400,0x00100001, 66 0x02100000,0x00000400,0x00000401,0x02100401,0x00100400,0x00000001,0x02000000,0x00100400, 67 0x02000000,0x00100400,0x00100000,0x02000401,0x02000401,0x02100001,0x02100001,0x00000001, 68 0x00100001,0x02000000,0x02000400,0x00100000,0x02100400,0x00000401,0x00100401,0x02100400, 69 0x00000401,0x02000001,0x02100401,0x02100000,0x00100400,0x00000000,0x00000001,0x02100401, 70 0x00000000,0x00100401,0x02100000,0x00000400,0x02000001,0x02000400,0x00000400,0x00100001, 71 72 0x08000820,0x00000800,0x00020000,0x08020820,0x08000000,0x08000820,0x00000020,0x08000000, 73 0x00020020,0x08020000,0x08020820,0x00020800,0x08020800,0x00020820,0x00000800,0x00000020, 74 0x08020000,0x08000020,0x08000800,0x00000820,0x00020800,0x00020020,0x08020020,0x08020800, 75 0x00000820,0x00000000,0x00000000,0x08020020,0x08000020,0x08000800,0x00020820,0x00020000, 76 0x00020820,0x00020000,0x08020800,0x00000800,0x00000020,0x08020020,0x00000800,0x00020820, 77 0x08000800,0x00000020,0x08000020,0x08020000,0x08020020,0x08000000,0x00020000,0x08000820, 78 0x00000000,0x08020820,0x00020020,0x08000020,0x08020000,0x08000800,0x08000820,0x00000000, 79 0x08020820,0x00020800,0x00020800,0x00000820,0x00000820,0x00020020,0x08000000,0x08020800, 80 }; 81 82 /* 83 * for manual index calculation 84 * #define fetch(box, i, sh) (*((uint32*)((uchar*)spbox + (box << 8) + ((i >> (sh)) & 0xfc)))) 85 */ 86 #define fetch(box, i, sh) ((spbox+(box << 6))[((i >> (sh + 2)) & 0x3f)]) 87 88 /* 89 * DES electronic codebook encryption of one block 90 */ 91 void 92 block_cipher(ulong key[32], uchar text[8], int decrypting) 93 { 94 uint32 right, left, v0, v1; 95 int i, keystep; 96 97 /* 98 * initial permutation 99 */ 100 v0 = text[0] | ((uint32)text[2]<<8) | ((uint32)text[4]<<16) | ((uint32)text[6]<<24); 101 left = text[1] | ((uint32)text[3]<<8) | ((uint32)text[5]<<16) | ((uint32)text[7]<<24); 102 right = (left & 0xaaaaaaaa) | ((v0 >> 1) & 0x55555555); 103 left = ((left << 1) & 0xaaaaaaaa) | (v0 & 0x55555555); 104 left = ((left << 6) & 0x33003300) 105 | (left & 0xcc33cc33) 106 | ((left >> 6) & 0x00cc00cc); 107 left = ((left << 12) & 0x0f0f0000) 108 | (left & 0xf0f00f0f) 109 | ((left >> 12) & 0x0000f0f0); 110 right = ((right << 6) & 0x33003300) 111 | (right & 0xcc33cc33) 112 | ((right >> 6) & 0x00cc00cc); 113 right = ((right << 12) & 0x0f0f0000) 114 | (right & 0xf0f00f0f) 115 | ((right >> 12) & 0x0000f0f0); 116 117 if (decrypting) { 118 keystep = -2; 119 key = key + 32 - 2; 120 } else 121 keystep = 2; 122 for (i = 0; i < 8; i++) { 123 v0 = key[0]; 124 v0 ^= (right >> 1) | (right << 31); 125 left ^= fetch(0, v0, 24) 126 ^ fetch(2, v0, 16) 127 ^ fetch(4, v0, 8) 128 ^ fetch(6, v0, 0); 129 v1 = key[1]; 130 v1 ^= (right << 3) | (right >> 29); 131 left ^= fetch(1, v1, 24) 132 ^ fetch(3, v1, 16) 133 ^ fetch(5, v1, 8) 134 ^ fetch(7, v1, 0); 135 key += keystep; 136 137 v0 = key[0]; 138 v0 ^= (left >> 1) | (left << 31); 139 right ^= fetch(0, v0, 24) 140 ^ fetch(2, v0, 16) 141 ^ fetch(4, v0, 8) 142 ^ fetch(6, v0, 0); 143 v1 = key[1]; 144 v1 ^= (left << 3) | (left >> 29); 145 right ^= fetch(1, v1, 24) 146 ^ fetch(3, v1, 16) 147 ^ fetch(5, v1, 8) 148 ^ fetch(7, v1, 0); 149 key += keystep; 150 } 151 152 /* 153 * final permutation, inverse initial permutation 154 */ 155 v0 = ((left << 1) & 0xaaaaaaaa) | (right & 0x55555555); 156 v1 = (left & 0xaaaaaaaa) | ((right >> 1) & 0x55555555); 157 v1 = ((v1 << 6) & 0x33003300) 158 | (v1 & 0xcc33cc33) 159 | ((v1 >> 6) & 0x00cc00cc); 160 v1 = ((v1 << 12) & 0x0f0f0000) 161 | (v1 & 0xf0f00f0f) 162 | ((v1 >> 12) & 0x0000f0f0); 163 v0 = ((v0 << 6) & 0x33003300) 164 | (v0 & 0xcc33cc33) 165 | ((v0 >> 6) & 0x00cc00cc); 166 v0 = ((v0 << 12) & 0x0f0f0000) 167 | (v0 & 0xf0f00f0f) 168 | ((v0 >> 12) & 0x0000f0f0); 169 text[0] = v0; 170 text[2] = v0 >> 8; 171 text[4] = v0 >> 16; 172 text[6] = v0 >> 24; 173 text[1] = v1; 174 text[3] = v1 >> 8; 175 text[5] = v1 >> 16; 176 text[7] = v1 >> 24; 177 } 178 179 /* 180 * triple DES electronic codebook encryption of one block 181 */ 182 void 183 triple_block_cipher(ulong expanded_key[3][32], uchar text[8], int ende) 184 { 185 ulong *key; 186 uint32 right, left, v0, v1; 187 int i, j, keystep; 188 189 /* 190 * initial permutation 191 */ 192 v0 = text[0] | ((uint32)text[2]<<8) | ((uint32)text[4]<<16) | ((uint32)text[6]<<24); 193 left = text[1] | ((uint32)text[3]<<8) | ((uint32)text[5]<<16) | ((uint32)text[7]<<24); 194 right = (left & 0xaaaaaaaa) | ((v0 >> 1) & 0x55555555); 195 left = ((left << 1) & 0xaaaaaaaa) | (v0 & 0x55555555); 196 left = ((left << 6) & 0x33003300) 197 | (left & 0xcc33cc33) 198 | ((left >> 6) & 0x00cc00cc); 199 left = ((left << 12) & 0x0f0f0000) 200 | (left & 0xf0f00f0f) 201 | ((left >> 12) & 0x0000f0f0); 202 right = ((right << 6) & 0x33003300) 203 | (right & 0xcc33cc33) 204 | ((right >> 6) & 0x00cc00cc); 205 right = ((right << 12) & 0x0f0f0000) 206 | (right & 0xf0f00f0f) 207 | ((right >> 12) & 0x0000f0f0); 208 209 for(j = 0; j < 3; j++){ 210 if((ende & 1) == DES3D) { 211 key = &expanded_key[2-j][32-2]; 212 keystep = -2; 213 } else { 214 key = &expanded_key[j][0]; 215 keystep = 2; 216 } 217 ende >>= 1; 218 for (i = 0; i < 8; i++) { 219 v0 = key[0]; 220 v0 ^= (right >> 1) | (right << 31); 221 left ^= fetch(0, v0, 24) 222 ^ fetch(2, v0, 16) 223 ^ fetch(4, v0, 8) 224 ^ fetch(6, v0, 0); 225 v1 = key[1]; 226 v1 ^= (right << 3) | (right >> 29); 227 left ^= fetch(1, v1, 24) 228 ^ fetch(3, v1, 16) 229 ^ fetch(5, v1, 8) 230 ^ fetch(7, v1, 0); 231 key += keystep; 232 233 v0 = key[0]; 234 v0 ^= (left >> 1) | (left << 31); 235 right ^= fetch(0, v0, 24) 236 ^ fetch(2, v0, 16) 237 ^ fetch(4, v0, 8) 238 ^ fetch(6, v0, 0); 239 v1 = key[1]; 240 v1 ^= (left << 3) | (left >> 29); 241 right ^= fetch(1, v1, 24) 242 ^ fetch(3, v1, 16) 243 ^ fetch(5, v1, 8) 244 ^ fetch(7, v1, 0); 245 key += keystep; 246 } 247 248 v0 = left; 249 left = right; 250 right = v0; 251 } 252 253 /* 254 * final permutation, inverse initial permutation 255 * left and right are swapped here 256 */ 257 v0 = ((right << 1) & 0xaaaaaaaa) | (left & 0x55555555); 258 v1 = (right & 0xaaaaaaaa) | ((left >> 1) & 0x55555555); 259 v1 = ((v1 << 6) & 0x33003300) 260 | (v1 & 0xcc33cc33) 261 | ((v1 >> 6) & 0x00cc00cc); 262 v1 = ((v1 << 12) & 0x0f0f0000) 263 | (v1 & 0xf0f00f0f) 264 | ((v1 >> 12) & 0x0000f0f0); 265 v0 = ((v0 << 6) & 0x33003300) 266 | (v0 & 0xcc33cc33) 267 | ((v0 >> 6) & 0x00cc00cc); 268 v0 = ((v0 << 12) & 0x0f0f0000) 269 | (v0 & 0xf0f00f0f) 270 | ((v0 >> 12) & 0x0000f0f0); 271 text[0] = v0; 272 text[2] = v0 >> 8; 273 text[4] = v0 >> 16; 274 text[6] = v0 >> 24; 275 text[1] = v1; 276 text[3] = v1 >> 8; 277 text[5] = v1 >> 16; 278 text[7] = v1 >> 24; 279 } 280 281 /* 282 * key compression permutation, 4 bits at a time 283 */ 284 static uint32 comptab[] = { 285 286 0x000000,0x010000,0x000008,0x010008,0x000080,0x010080,0x000088,0x010088, 287 0x000000,0x010000,0x000008,0x010008,0x000080,0x010080,0x000088,0x010088, 288 289 0x000000,0x100000,0x000800,0x100800,0x000000,0x100000,0x000800,0x100800, 290 0x002000,0x102000,0x002800,0x102800,0x002000,0x102000,0x002800,0x102800, 291 292 0x000000,0x000004,0x000400,0x000404,0x000000,0x000004,0x000400,0x000404, 293 0x400000,0x400004,0x400400,0x400404,0x400000,0x400004,0x400400,0x400404, 294 295 0x000000,0x000020,0x008000,0x008020,0x800000,0x800020,0x808000,0x808020, 296 0x000002,0x000022,0x008002,0x008022,0x800002,0x800022,0x808002,0x808022, 297 298 0x000000,0x000200,0x200000,0x200200,0x001000,0x001200,0x201000,0x201200, 299 0x000000,0x000200,0x200000,0x200200,0x001000,0x001200,0x201000,0x201200, 300 301 0x000000,0x000040,0x000010,0x000050,0x004000,0x004040,0x004010,0x004050, 302 0x040000,0x040040,0x040010,0x040050,0x044000,0x044040,0x044010,0x044050, 303 304 0x000000,0x000100,0x020000,0x020100,0x000001,0x000101,0x020001,0x020101, 305 0x080000,0x080100,0x0a0000,0x0a0100,0x080001,0x080101,0x0a0001,0x0a0101, 306 307 0x000000,0x000100,0x040000,0x040100,0x000000,0x000100,0x040000,0x040100, 308 0x000040,0x000140,0x040040,0x040140,0x000040,0x000140,0x040040,0x040140, 309 310 0x000000,0x400000,0x008000,0x408000,0x000008,0x400008,0x008008,0x408008, 311 0x000400,0x400400,0x008400,0x408400,0x000408,0x400408,0x008408,0x408408, 312 313 0x000000,0x001000,0x080000,0x081000,0x000020,0x001020,0x080020,0x081020, 314 0x004000,0x005000,0x084000,0x085000,0x004020,0x005020,0x084020,0x085020, 315 316 0x000000,0x000800,0x000000,0x000800,0x000010,0x000810,0x000010,0x000810, 317 0x800000,0x800800,0x800000,0x800800,0x800010,0x800810,0x800010,0x800810, 318 319 0x000000,0x010000,0x000200,0x010200,0x000000,0x010000,0x000200,0x010200, 320 0x100000,0x110000,0x100200,0x110200,0x100000,0x110000,0x100200,0x110200, 321 322 0x000000,0x000004,0x000000,0x000004,0x000080,0x000084,0x000080,0x000084, 323 0x002000,0x002004,0x002000,0x002004,0x002080,0x002084,0x002080,0x002084, 324 325 0x000000,0x000001,0x200000,0x200001,0x020000,0x020001,0x220000,0x220001, 326 0x000002,0x000003,0x200002,0x200003,0x020002,0x020003,0x220002,0x220003, 327 }; 328 329 static int keysh[] = 330 { 331 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 332 }; 333 334 static void 335 keycompperm(uint32 left, uint32 right, ulong *ek) 336 { 337 uint32 v0, v1; 338 int i; 339 340 for(i = 0; i < 16; i++){ 341 left = (left << keysh[i]) | (left >> (28 - keysh[i])); 342 left &= 0xfffffff0; 343 right = (right << keysh[i]) | (right >> (28 - keysh[i])); 344 right &= 0xfffffff0; 345 v0 = comptab[6 * (1 << 4) + ((left >> (32-4)) & 0xf)] 346 | comptab[5 * (1 << 4) + ((left >> (32-8)) & 0xf)] 347 | comptab[4 * (1 << 4) + ((left >> (32-12)) & 0xf)] 348 | comptab[3 * (1 << 4) + ((left >> (32-16)) & 0xf)] 349 | comptab[2 * (1 << 4) + ((left >> (32-20)) & 0xf)] 350 | comptab[1 * (1 << 4) + ((left >> (32-24)) & 0xf)] 351 | comptab[0 * (1 << 4) + ((left >> (32-28)) & 0xf)]; 352 v1 = comptab[13 * (1 << 4) + ((right >> (32-4)) & 0xf)] 353 | comptab[12 * (1 << 4) + ((right >> (32-8)) & 0xf)] 354 | comptab[11 * (1 << 4) + ((right >> (32-12)) & 0xf)] 355 | comptab[10 * (1 << 4) + ((right >> (32-16)) & 0xf)] 356 | comptab[9 * (1 << 4) + ((right >> (32-20)) & 0xf)] 357 | comptab[8 * (1 << 4) + ((right >> (32-24)) & 0xf)] 358 | comptab[7 * (1 << 4) + ((right >> (32-28)) & 0xf)]; 359 ek[0] = (((v0 >> (24-6)) & 0x3f) << 26) 360 | (((v0 >> (24-18)) & 0x3f) << 18) 361 | (((v1 >> (24-6)) & 0x3f) << 10) 362 | (((v1 >> (24-18)) & 0x3f) << 2); 363 ek[1] = (((v0 >> (24-12)) & 0x3f) << 26) 364 | (((v0 >> (24-24)) & 0x3f) << 18) 365 | (((v1 >> (24-12)) & 0x3f) << 10) 366 | (((v1 >> (24-24)) & 0x3f) << 2); 367 ek += 2; 368 } 369 } 370 371 void 372 des_key_setup(uchar key[8], ulong *ek) 373 { 374 uint32 left, right, v0, v1; 375 376 v0 = key[0] | ((uint32)key[2] << 8) | ((uint32)key[4] << 16) | ((uint32)key[6] << 24); 377 v1 = key[1] | ((uint32)key[3] << 8) | ((uint32)key[5] << 16) | ((uint32)key[7] << 24); 378 left = ((v0 >> 1) & 0x40404040) 379 | ((v0 >> 2) & 0x10101010) 380 | ((v0 >> 3) & 0x04040404) 381 | ((v0 >> 4) & 0x01010101) 382 | ((v1 >> 0) & 0x80808080) 383 | ((v1 >> 1) & 0x20202020) 384 | ((v1 >> 2) & 0x08080808) 385 | ((v1 >> 3) & 0x02020202); 386 right = ((v0 >> 1) & 0x04040404) 387 | ((v0 << 2) & 0x10101010) 388 | ((v0 << 5) & 0x40404040) 389 | ((v1 << 0) & 0x08080808) 390 | ((v1 << 3) & 0x20202020) 391 | ((v1 << 6) & 0x80808080); 392 left = ((left << 6) & 0x33003300) 393 | (left & 0xcc33cc33) 394 | ((left >> 6) & 0x00cc00cc); 395 v0 = ((left << 12) & 0x0f0f0000) 396 | (left & 0xf0f00f0f) 397 | ((left >> 12) & 0x0000f0f0); 398 right = ((right << 6) & 0x33003300) 399 | (right & 0xcc33cc33) 400 | ((right >> 6) & 0x00cc00cc); 401 v1 = ((right << 12) & 0x0f0f0000) 402 | (right & 0xf0f00f0f) 403 | ((right >> 12) & 0x0000f0f0); 404 left = v0 & 0xfffffff0; 405 right = (v1 & 0xffffff00) | ((v0 << 4) & 0xf0); 406 407 keycompperm(left, right, ek); 408 } 409 410 static uchar parity[128] = 411 { 412 0x01, 0x02, 0x04, 0x07, 0x08, 0x0b, 0x0d, 0x0e, 413 0x10, 0x13, 0x15, 0x16, 0x19, 0x1a, 0x1c, 0x1f, 414 0x20, 0x23, 0x25, 0x26, 0x29, 0x2a, 0x2c, 0x2f, 415 0x31, 0x32, 0x34, 0x37, 0x38, 0x3b, 0x3d, 0x3e, 416 0x40, 0x43, 0x45, 0x46, 0x49, 0x4a, 0x4c, 0x4f, 417 0x51, 0x52, 0x54, 0x57, 0x58, 0x5b, 0x5d, 0x5e, 418 0x61, 0x62, 0x64, 0x67, 0x68, 0x6b, 0x6d, 0x6e, 419 0x70, 0x73, 0x75, 0x76, 0x79, 0x7a, 0x7c, 0x7f, 420 0x80, 0x83, 0x85, 0x86, 0x89, 0x8a, 0x8c, 0x8f, 421 0x91, 0x92, 0x94, 0x97, 0x98, 0x9b, 0x9d, 0x9e, 422 0xa1, 0xa2, 0xa4, 0xa7, 0xa8, 0xab, 0xad, 0xae, 423 0xb0, 0xb3, 0xb5, 0xb6, 0xb9, 0xba, 0xbc, 0xbf, 424 0xc1, 0xc2, 0xc4, 0xc7, 0xc8, 0xcb, 0xcd, 0xce, 425 0xd0, 0xd3, 0xd5, 0xd6, 0xd9, 0xda, 0xdc, 0xdf, 426 0xe0, 0xe3, 0xe5, 0xe6, 0xe9, 0xea, 0xec, 0xef, 427 0xf1, 0xf2, 0xf4, 0xf7, 0xf8, 0xfb, 0xfd, 0xfe, 428 }; 429 430 /* 431 * convert a 7 byte key to an 8 byte one 432 */ 433 void 434 des56to64(uchar *k56, uchar *k64) 435 { 436 uint32 hi, lo; 437 438 hi = ((uint32)k56[0]<<24)|((uint32)k56[1]<<16)|((uint32)k56[2]<<8)|k56[3]; 439 lo = ((uint32)k56[4]<<24)|((uint32)k56[5]<<16)|((uint32)k56[6]<<8); 440 441 k64[0] = parity[(hi>>25)&0x7f]; 442 k64[1] = parity[(hi>>18)&0x7f]; 443 k64[2] = parity[(hi>>11)&0x7f]; 444 k64[3] = parity[(hi>>4)&0x7f]; 445 k64[4] = parity[((hi<<3)|(lo>>29))&0x7f]; 446 k64[5] = parity[(lo>>22)&0x7f]; 447 k64[6] = parity[(lo>>15)&0x7f]; 448 k64[7] = parity[(lo>>8)&0x7f]; 449 } 450 451 /* 452 * convert an 8 byte key to a 7 byte one 453 */ 454 void 455 des64to56(uchar *k64, uchar *k56) 456 { 457 uint32 hi, lo; 458 459 hi = (((uint32)k64[0]&0xfe)<<24)|(((uint32)k64[1]&0xfe)<<17)|(((uint32)k64[2]&0xfe)<<10) 460 |((k64[3]&0xfe)<<3)|(k64[4]>>4); 461 lo = (((uint32)k64[4]&0xfe)<<28)|(((uint32)k64[5]&0xfe)<<21)|(((uint32)k64[6]&0xfe)<<14) 462 |(((uint32)k64[7]&0xfe)<<7); 463 464 k56[0] = hi>>24; 465 k56[1] = hi>>16; 466 k56[2] = hi>>8; 467 k56[3] = hi>>0; 468 k56[4] = lo>>24; 469 k56[5] = lo>>16; 470 k56[6] = lo>>8; 471 } 472 473 void 474 key_setup(uchar key[7], ulong *ek) 475 { 476 uchar k64[8]; 477 478 des56to64(key, k64); 479 des_key_setup(k64, ek); 480 }