[26647] | 1 | /*
|
---|
| 2 | * UFC-crypt: ultra fast crypt(3) implementation
|
---|
| 3 | *
|
---|
| 4 | * Copyright (C) 1991, Michael Glad, email: [email protected]
|
---|
| 5 | *
|
---|
| 6 | * This library is free software; you can redistribute it and/or
|
---|
| 7 | * modify it under the terms of the GNU Library General Public
|
---|
| 8 | * License as published by the Free Software Foundation; either
|
---|
| 9 | * version 2 of the License, or (at your option) any later version.
|
---|
| 10 | *
|
---|
| 11 | * This library is distributed in the hope that it will be useful,
|
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 14 | * Library General Public License for more details.
|
---|
| 15 | *
|
---|
| 16 | * You should have received a copy of the GNU Library General Public
|
---|
| 17 | * License along with this library; if not, write to the Free
|
---|
| 18 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 19 | *
|
---|
| 20 | * @(#)crypt_util.c 2.2 10/04/91
|
---|
| 21 | *
|
---|
| 22 | * Support routines
|
---|
| 23 | *
|
---|
| 24 | */
|
---|
| 25 |
|
---|
| 26 | #ifdef DEBUG
|
---|
| 27 | #include <stdio.h>
|
---|
| 28 | #endif
|
---|
| 29 |
|
---|
| 30 | /*
|
---|
| 31 | #include "patchlevel.h"
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | /*
|
---|
| 36 | #ifdef SYSV
|
---|
| 37 | */
|
---|
| 38 | #define bzero(addr, cnt) memset(addr, 0, cnt)
|
---|
| 39 | #define bcopy(from, to, len) memcpy(to, from, len)
|
---|
| 40 | /*
|
---|
| 41 | #endif
|
---|
| 42 | */
|
---|
| 43 |
|
---|
| 44 | /* Permutation done once on the 56 bit
|
---|
| 45 | key derived from the original 8 byte ASCII key.
|
---|
| 46 | */
|
---|
| 47 | static unsigned long pc1[56] =
|
---|
| 48 | { 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18,
|
---|
| 49 | 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36,
|
---|
| 50 | 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22,
|
---|
| 51 | 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4
|
---|
| 52 | };
|
---|
| 53 |
|
---|
| 54 | /* How much to rotate each 28 bit half of the pc1 permutated
|
---|
| 55 | 56 bit key before using pc2 to give the i' key
|
---|
| 56 | */
|
---|
| 57 | static unsigned long totrot[16] =
|
---|
| 58 | { 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28 };
|
---|
| 59 |
|
---|
| 60 | /* Permutation giving the key of the i' DES round */
|
---|
| 61 | static unsigned long pc2[48] =
|
---|
| 62 | { 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10,
|
---|
| 63 | 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2,
|
---|
| 64 | 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
|
---|
| 65 | 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32
|
---|
| 66 | };
|
---|
| 67 |
|
---|
| 68 | /* Reference copy of the expansion table which selects
|
---|
| 69 | bits from the 32 bit intermediate result.
|
---|
| 70 | */
|
---|
| 71 | static unsigned long eref[48] =
|
---|
| 72 | { 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,
|
---|
| 73 | 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17,
|
---|
| 74 | 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25,
|
---|
| 75 | 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1
|
---|
| 76 | };
|
---|
| 77 | static unsigned long disturbed_e[48];
|
---|
| 78 | static unsigned long e_inverse[64];
|
---|
| 79 |
|
---|
| 80 | /* Permutation done on the result of sbox lookups */
|
---|
| 81 | static unsigned long perm32[32] =
|
---|
| 82 | { 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
|
---|
| 83 | 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25
|
---|
| 84 | };
|
---|
| 85 |
|
---|
| 86 | /* The sboxes */
|
---|
| 87 | static unsigned long sbox[8][4][16]=
|
---|
| 88 | { { { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 },
|
---|
| 89 | { 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 },
|
---|
| 90 | { 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 },
|
---|
| 91 | { 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 }
|
---|
| 92 | },
|
---|
| 93 |
|
---|
| 94 | { { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10 },
|
---|
| 95 | { 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 },
|
---|
| 96 | { 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15 },
|
---|
| 97 | { 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 }
|
---|
| 98 | },
|
---|
| 99 |
|
---|
| 100 | { { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 },
|
---|
| 101 | { 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 },
|
---|
| 102 | { 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7 },
|
---|
| 103 | { 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 }
|
---|
| 104 | },
|
---|
| 105 |
|
---|
| 106 | { { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 },
|
---|
| 107 | { 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 },
|
---|
| 108 | { 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4 },
|
---|
| 109 | { 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 }
|
---|
| 110 | },
|
---|
| 111 |
|
---|
| 112 | { { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 },
|
---|
| 113 | { 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 },
|
---|
| 114 | { 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14 },
|
---|
| 115 | { 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 }
|
---|
| 116 | },
|
---|
| 117 |
|
---|
| 118 | { { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 },
|
---|
| 119 | { 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 },
|
---|
| 120 | { 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6 },
|
---|
| 121 | { 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 }
|
---|
| 122 | },
|
---|
| 123 |
|
---|
| 124 | { { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 },
|
---|
| 125 | { 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 },
|
---|
| 126 | { 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2 },
|
---|
| 127 | { 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 }
|
---|
| 128 | },
|
---|
| 129 |
|
---|
| 130 | { { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 },
|
---|
| 131 | { 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 },
|
---|
| 132 | { 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8 },
|
---|
| 133 | { 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 }
|
---|
| 134 | }
|
---|
| 135 | };
|
---|
| 136 |
|
---|
| 137 | #ifdef notdef
|
---|
| 138 |
|
---|
| 139 | /* This is the initial permutation matrix -- we have no
|
---|
| 140 | use for it, but it is needed if you will develop
|
---|
| 141 | this module into a general DES package.
|
---|
| 142 | */
|
---|
| 143 | static unsigned char inital_perm[64] =
|
---|
| 144 | { 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
|
---|
| 145 | 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
|
---|
| 146 | 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
|
---|
| 147 | 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7
|
---|
| 148 | };
|
---|
| 149 |
|
---|
| 150 | #endif
|
---|
| 151 |
|
---|
| 152 | /* Final permutation matrix -- not used directly */
|
---|
| 153 | static unsigned char final_perm[64] =
|
---|
| 154 | { 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31,
|
---|
| 155 | 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29,
|
---|
| 156 | 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27,
|
---|
| 157 | 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25
|
---|
| 158 | };
|
---|
| 159 |
|
---|
| 160 | /* The 16 DES keys in BITMASK format */
|
---|
| 161 | unsigned long keytab[16][2];
|
---|
| 162 |
|
---|
| 163 | #define ascii_to_bin(c) ((c)>='a'?(c-59):(c)>='A'?((c)-53):(c)-'.')
|
---|
| 164 | #define bin_to_ascii(c) ((c)>=38?((c)-38+'a'):(c)>=12?((c)-12+'A'):(c)+'.')
|
---|
| 165 |
|
---|
| 166 | /* Macro to set a bit (0..23) */
|
---|
| 167 | #define BITMASK(i) ( (1<<(11-(i)%12+3)) << ((i)<12?16:0) )
|
---|
| 168 |
|
---|
| 169 | /* sb arrays:
|
---|
| 170 |
|
---|
| 171 | Workhorses of the inner loop of the DES implementation.
|
---|
| 172 | They do sbox lookup, shifting of this value, 32 bit
|
---|
| 173 | permutation and E permutation for the next round.
|
---|
| 174 |
|
---|
| 175 | Kept in 'BITMASK' format.
|
---|
| 176 |
|
---|
| 177 | */
|
---|
| 178 |
|
---|
| 179 | unsigned long sb0[8192],sb1[8192],sb2[8192],sb3[8192];
|
---|
| 180 | static unsigned long *sb[4] = {sb0,sb1,sb2,sb3};
|
---|
| 181 |
|
---|
| 182 | /* eperm32tab: do 32 bit permutation and E selection
|
---|
| 183 |
|
---|
| 184 | The first index is the byte number in the 32 bit value to be permuted
|
---|
| 185 | - second - is the value of this byte
|
---|
| 186 | - third - selects the two 32 bit values
|
---|
| 187 |
|
---|
| 188 | The table is used and generated internally in init_des to speed it up
|
---|
| 189 |
|
---|
| 190 | */
|
---|
| 191 | static unsigned long eperm32tab[4][256][2];
|
---|
| 192 |
|
---|
| 193 | /* mk_keytab_table: fast way of generating keytab from ASCII key
|
---|
| 194 |
|
---|
| 195 | The first index is the byte number in the 8 byte ASCII key
|
---|
| 196 | - second - - - current DES round i.e. the key number
|
---|
| 197 | - third - distinguishes between the two 24 bit halfs of
|
---|
| 198 | the selected key
|
---|
| 199 | - fourth - selects the 7 bits actually used of each byte
|
---|
| 200 |
|
---|
| 201 | The table is kept in the format generated by the BITMASK macro
|
---|
| 202 |
|
---|
| 203 | */
|
---|
| 204 | static unsigned long mk_keytab_table[8][16][2][128];
|
---|
| 205 |
|
---|
| 206 |
|
---|
| 207 | /* efp: undo an extra e selection and do final
|
---|
| 208 | permutation giving the DES result.
|
---|
| 209 |
|
---|
| 210 | Invoked 6 bit a time on two 48 bit values
|
---|
| 211 | giving two 32 bit longs.
|
---|
| 212 | */
|
---|
| 213 | static unsigned long efp[16][64][2];
|
---|
| 214 |
|
---|
| 215 |
|
---|
| 216 | static unsigned char bytemask[8] =
|
---|
| 217 | { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
|
---|
| 218 |
|
---|
| 219 |
|
---|
| 220 | static unsigned long longmask[32] =
|
---|
| 221 | { 0x80000000, 0x40000000, 0x20000000, 0x10000000,
|
---|
| 222 | 0x08000000, 0x04000000, 0x02000000, 0x01000000,
|
---|
| 223 | 0x00800000, 0x00400000, 0x00200000, 0x00100000,
|
---|
| 224 | 0x00080000, 0x00040000, 0x00020000, 0x00010000,
|
---|
| 225 | 0x00008000, 0x00004000, 0x00002000, 0x00001000,
|
---|
| 226 | 0x00000800, 0x00000400, 0x00000200, 0x00000100,
|
---|
| 227 | 0x00000080, 0x00000040, 0x00000020, 0x00000010,
|
---|
| 228 | 0x00000008, 0x00000004, 0x00000002, 0x00000001
|
---|
| 229 | };
|
---|
| 230 |
|
---|
| 231 | #ifdef DEBUG
|
---|
| 232 |
|
---|
| 233 | /* For debugging */
|
---|
| 234 |
|
---|
| 235 | void pr_bits(a,n)
|
---|
| 236 | unsigned long *a;
|
---|
| 237 | unsigned long n;
|
---|
| 238 | { unsigned long i,j,t,tmp;
|
---|
| 239 | n/=8;
|
---|
| 240 | for(i=0; i<n; i++)
|
---|
| 241 | { tmp=0;
|
---|
| 242 | for(j=0; j<8; j++)
|
---|
| 243 | { t=8*i+j;
|
---|
| 244 | tmp|=(a[t/24] & BITMASK(t % 24))?bytemask[j]:0;
|
---|
| 245 | }
|
---|
| 246 | (void)printf("%02x ",tmp);
|
---|
| 247 | }
|
---|
| 248 | printf(" ");
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | static void set_bits(v,b)
|
---|
| 252 | unsigned long v;
|
---|
| 253 | unsigned long *b;
|
---|
| 254 | { unsigned long i;
|
---|
| 255 | *b = 0;
|
---|
| 256 | for(i=0; i<24; i++)
|
---|
| 257 | if(v & longmask[8+i])
|
---|
| 258 | *b |= BITMASK(i);
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | #endif
|
---|
| 262 |
|
---|
| 263 | static unsigned long initialized = 0;
|
---|
| 264 |
|
---|
| 265 | /* lookup a 6 bit value in sbox */
|
---|
| 266 |
|
---|
| 267 | #define s_lookup(i,s) sbox[(i)][(((s)>>4) & 0x2)|((s) & 0x1)][((s)>>1) & 0xf];
|
---|
| 268 |
|
---|
| 269 | /* Generate the mk_keytab_table once in a program execution */
|
---|
| 270 |
|
---|
| 271 | void init_des()
|
---|
| 272 | { unsigned long tbl_long,bit_within_long,comes_from_bit;
|
---|
| 273 | unsigned long bit,sg,j;
|
---|
| 274 | unsigned long bit_within_byte,key_byte,byte_value;
|
---|
| 275 | unsigned long round,mask;
|
---|
| 276 |
|
---|
| 277 | bzero((char*)mk_keytab_table,sizeof mk_keytab_table);
|
---|
| 278 |
|
---|
| 279 | for(round=0; round<16; round++)
|
---|
| 280 | for(bit=0; bit<48; bit++)
|
---|
| 281 | { tbl_long = bit / 24;
|
---|
| 282 | bit_within_long = bit % 24;
|
---|
| 283 |
|
---|
| 284 | /* from which bit in the key halves does it origin? */
|
---|
| 285 | comes_from_bit = pc2[bit] - 1;
|
---|
| 286 |
|
---|
| 287 | /* undo the rotation done before pc2 */
|
---|
| 288 | if(comes_from_bit>=28)
|
---|
| 289 | comes_from_bit = 28 + (comes_from_bit + totrot[round]) % 28;
|
---|
| 290 | else
|
---|
| 291 | comes_from_bit = (comes_from_bit + totrot[round]) % 28;
|
---|
| 292 |
|
---|
| 293 | /* undo the initial key half forming permutation */
|
---|
| 294 | comes_from_bit = pc1[comes_from_bit] - 1;
|
---|
| 295 |
|
---|
| 296 | /* Now 'comes_from_bit' is the correct number (0..55)
|
---|
| 297 | of the keybit from which the bit being traced
|
---|
| 298 | in key 'round' comes from
|
---|
| 299 | */
|
---|
| 300 |
|
---|
| 301 | key_byte = comes_from_bit / 8;
|
---|
| 302 | bit_within_byte = (comes_from_bit % 8)+1;
|
---|
| 303 |
|
---|
| 304 | mask = bytemask[bit_within_byte];
|
---|
| 305 |
|
---|
| 306 | for(byte_value=0; byte_value<128; byte_value++)
|
---|
| 307 | if(byte_value & mask)
|
---|
| 308 | mk_keytab_table[key_byte][round][tbl_long][byte_value] |=
|
---|
| 309 | BITMASK(bit_within_long);
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | /* Now generate the table used to do an combined
|
---|
| 313 | 32 bit permutation and e expansion
|
---|
| 314 |
|
---|
| 315 | We use it because we have to permute 16384 32 bit
|
---|
| 316 | longs into 48 bit in order to initialize sb.
|
---|
| 317 |
|
---|
| 318 | Looping 48 rounds per permutation becomes
|
---|
| 319 | just too slow...
|
---|
| 320 |
|
---|
| 321 | */
|
---|
| 322 |
|
---|
| 323 | bzero((char*)eperm32tab,sizeof eperm32tab);
|
---|
| 324 | for(bit=0; bit<48; bit++)
|
---|
| 325 | { unsigned long mask1,comes_from;
|
---|
| 326 |
|
---|
| 327 | comes_from = perm32[eref[bit]-1]-1;
|
---|
| 328 | mask1 = bytemask[comes_from % 8];
|
---|
| 329 |
|
---|
| 330 | for(j=256; j--;)
|
---|
| 331 | if(j & mask1)
|
---|
| 332 | eperm32tab[comes_from/8][j][bit/24] |= BITMASK(bit % 24);
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | /* Create the sb tables:
|
---|
| 336 |
|
---|
| 337 | For each 12 bit segment of an 48 bit intermediate
|
---|
| 338 | result, the sb table precomputes the two 4 bit
|
---|
| 339 | values of the sbox lookups done with the two 6
|
---|
| 340 | bit halves, shifts them to their proper place,
|
---|
| 341 | sends them through perm32 and finally E expands
|
---|
| 342 | them so that they are ready for the next
|
---|
| 343 | DES round.
|
---|
| 344 |
|
---|
| 345 | The value looked up is to be xored onto the
|
---|
| 346 | two 48 bit right halves.
|
---|
| 347 | */
|
---|
| 348 |
|
---|
| 349 | for(sg=0; sg<4; sg++)
|
---|
| 350 | { unsigned long j1,j2;
|
---|
| 351 | unsigned long s1,s2;
|
---|
| 352 |
|
---|
| 353 | for(j1=0; j1<64; j1++)
|
---|
| 354 | { s1 = s_lookup(2*sg,j1);
|
---|
| 355 | for(j2=0; j2<64; j2++)
|
---|
| 356 | { unsigned long to_permute,inx;
|
---|
| 357 |
|
---|
| 358 | s2 = s_lookup(2*sg+1,j2);
|
---|
| 359 | to_permute = ((s1<<4) | s2) << (24-8*sg);
|
---|
| 360 | inx = ((j1<<6) | j2) << 1;
|
---|
| 361 |
|
---|
| 362 | sb[sg][inx ] = eperm32tab[0][(to_permute >> 24) & 0xff][0];
|
---|
| 363 | sb[sg][inx+1] = eperm32tab[0][(to_permute >> 24) & 0xff][1];
|
---|
| 364 |
|
---|
| 365 | sb[sg][inx ] |= eperm32tab[1][(to_permute >> 16) & 0xff][0];
|
---|
| 366 | sb[sg][inx+1] |= eperm32tab[1][(to_permute >> 16) & 0xff][1];
|
---|
| 367 |
|
---|
| 368 | sb[sg][inx ] |= eperm32tab[2][(to_permute >> 8) & 0xff][0];
|
---|
| 369 | sb[sg][inx+1] |= eperm32tab[2][(to_permute >> 8) & 0xff][1];
|
---|
| 370 |
|
---|
| 371 | sb[sg][inx ] |= eperm32tab[3][(to_permute) & 0xff][0];
|
---|
| 372 | sb[sg][inx+1] |= eperm32tab[3][(to_permute) & 0xff][1];
|
---|
| 373 | }
|
---|
| 374 | }
|
---|
| 375 | }
|
---|
| 376 | initialized++;
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | /* Process the elements of the sb table permuting the
|
---|
| 380 | bits swapped in the expansion by the current salt.
|
---|
| 381 | */
|
---|
| 382 |
|
---|
| 383 | void shuffle_sb(k, saltbits)
|
---|
| 384 | unsigned long *k, saltbits;
|
---|
| 385 | { int j, x;
|
---|
| 386 | for(j=4096; j--;) {
|
---|
| 387 | x = (k[0] ^ k[1]) & saltbits;
|
---|
| 388 | *k++ ^= x;
|
---|
| 389 | *k++ ^= x;
|
---|
| 390 | }
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | /* Setup the unit for a new salt
|
---|
| 394 | Hopefully we'll not see a new salt in each crypt call.
|
---|
| 395 | */
|
---|
| 396 |
|
---|
| 397 | static unsigned char current_salt[3]="&&"; /* invalid value */
|
---|
| 398 | static unsigned long oldsaltbits = 0;
|
---|
| 399 |
|
---|
| 400 | void setup_salt(s)
|
---|
| 401 | char *s;
|
---|
| 402 | { unsigned long i,j,saltbits;
|
---|
| 403 |
|
---|
| 404 | if(!initialized)
|
---|
| 405 | init_des();
|
---|
| 406 |
|
---|
| 407 | if(s[0]==current_salt[0] && s[1]==current_salt[1])
|
---|
| 408 | return;
|
---|
| 409 | current_salt[0]=s[0]; current_salt[1]=s[1];
|
---|
| 410 |
|
---|
| 411 | /* This is the only crypt change to DES:
|
---|
| 412 | entries are swapped in the expansion table
|
---|
| 413 | according to the bits set in the salt.
|
---|
| 414 | */
|
---|
| 415 |
|
---|
| 416 | saltbits=0;
|
---|
| 417 | bcopy((char*)eref,(char*)disturbed_e,sizeof eref);
|
---|
| 418 | for(i=0; i<2; i++)
|
---|
| 419 | { long c=ascii_to_bin(s[i]);
|
---|
| 420 | if(c<0 || c>63)
|
---|
| 421 | c=0;
|
---|
| 422 | for(j=0; j<6; j++)
|
---|
| 423 | if((c>>j) & 0x1)
|
---|
| 424 | { disturbed_e[6*i+j ]=eref[6*i+j+24];
|
---|
| 425 | disturbed_e[6*i+j+24]=eref[6*i+j ];
|
---|
| 426 | saltbits |= BITMASK(6*i+j);
|
---|
| 427 | }
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 | /* Permute the sb table values
|
---|
| 431 | to reflect the changed e
|
---|
| 432 | selection table
|
---|
| 433 | */
|
---|
| 434 |
|
---|
| 435 | shuffle_sb(sb0, oldsaltbits ^ saltbits);
|
---|
| 436 | shuffle_sb(sb1, oldsaltbits ^ saltbits);
|
---|
| 437 | shuffle_sb(sb2, oldsaltbits ^ saltbits);
|
---|
| 438 | shuffle_sb(sb3, oldsaltbits ^ saltbits);
|
---|
| 439 |
|
---|
| 440 | oldsaltbits = saltbits;
|
---|
| 441 |
|
---|
| 442 | /* Create an inverse matrix for disturbed_e telling
|
---|
| 443 | where to plug out bits if undoing disturbed_e
|
---|
| 444 | */
|
---|
| 445 |
|
---|
| 446 | for(i=48; i--;)
|
---|
| 447 | { e_inverse[disturbed_e[i]-1 ] = i;
|
---|
| 448 | e_inverse[disturbed_e[i]-1+32] = i+48;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | /* create efp: the matrix used to
|
---|
| 452 | undo the E expansion and effect final permutation
|
---|
| 453 | */
|
---|
| 454 |
|
---|
| 455 | bzero((char*)efp,sizeof efp);
|
---|
| 456 | for(i=0; i<64; i++)
|
---|
| 457 | { unsigned long o_bit,o_long;
|
---|
| 458 | unsigned long word_value,mask1,mask2,comes_from_f_bit,comes_from_e_bit;
|
---|
| 459 | unsigned long comes_from_word,bit_within_word;
|
---|
| 460 |
|
---|
| 461 | /* See where bit i belongs in the two 32 bit long's */
|
---|
| 462 | o_long = i / 32; /* 0..1 */
|
---|
| 463 | o_bit = i % 32; /* 0..31 */
|
---|
| 464 |
|
---|
| 465 | /* And find a bit in the e permutated value setting this bit.
|
---|
| 466 |
|
---|
| 467 | Note: the e selection may have selected the same bit several
|
---|
| 468 | times. By the initialization of e_inverse, we only look
|
---|
| 469 | for one specific instance.
|
---|
| 470 | */
|
---|
| 471 | comes_from_f_bit = final_perm[i]-1; /* 0..63 */
|
---|
| 472 | comes_from_e_bit = e_inverse[comes_from_f_bit]; /* 0..95 */
|
---|
| 473 | comes_from_word = comes_from_e_bit / 6; /* 0..15 */
|
---|
| 474 | bit_within_word = comes_from_e_bit % 6; /* 0..5 */
|
---|
| 475 |
|
---|
| 476 | mask1 = longmask[bit_within_word+26];
|
---|
| 477 | mask2 = longmask[o_bit];
|
---|
| 478 |
|
---|
| 479 | for(word_value=64; word_value--;)
|
---|
| 480 | if(word_value & mask1)
|
---|
| 481 | efp[comes_from_word][word_value][o_long] |= mask2;
|
---|
| 482 |
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | /* Generate the key table before running the 25 DES rounds */
|
---|
| 488 |
|
---|
| 489 | void mk_keytab(key)
|
---|
| 490 | char *key;
|
---|
| 491 | { unsigned long i,j;
|
---|
| 492 | unsigned long *k,*mkt;
|
---|
| 493 | char t;
|
---|
| 494 |
|
---|
| 495 | bzero((char*)keytab, sizeof keytab);
|
---|
| 496 | mkt = &mk_keytab_table[0][0][0][0];
|
---|
| 497 |
|
---|
| 498 | for(i=0; (t=(*key++) & 0x7f) && i<8; i++)
|
---|
| 499 | for(j=0,k = &keytab[0][0]; j<16; j++)
|
---|
| 500 | { *k++ |= mkt[t]; mkt += 128;
|
---|
| 501 | *k++ |= mkt[t]; mkt += 128;
|
---|
| 502 | }
|
---|
| 503 | for(; i<8; i++)
|
---|
| 504 | for(j=0,k = &keytab[0][0]; j<16; j++)
|
---|
| 505 | { *k++ |= mkt[0]; mkt += 128;
|
---|
| 506 | *k++ |= mkt[0]; mkt += 128;
|
---|
| 507 | }
|
---|
| 508 | }
|
---|
| 509 |
|
---|
| 510 | /* Do final permutations and convert to ASCII */
|
---|
| 511 |
|
---|
| 512 | char *output_conversion(l1,l2,r1,r2,salt)
|
---|
| 513 | unsigned long l1,l2,r1,r2;
|
---|
| 514 | char *salt;
|
---|
| 515 | { static char outbuf[14];
|
---|
| 516 | unsigned long i;
|
---|
| 517 | unsigned long s,v1,v2;
|
---|
| 518 |
|
---|
| 519 | /* Unfortunately we've done an extra E
|
---|
| 520 | expansion -- undo it at the same time.
|
---|
| 521 | */
|
---|
| 522 |
|
---|
| 523 | v1=v2=0; l1 >>= 3; l2 >>= 3; r1 >>= 3; r2 >>= 3;
|
---|
| 524 |
|
---|
| 525 | v1 |= efp[ 3][ l1 & 0x3f][0]; v2 |= efp[ 3][ l1 & 0x3f][1];
|
---|
| 526 | v1 |= efp[ 2][(l1>>=6) & 0x3f][0]; v2 |= efp[ 2][ l1 & 0x3f][1];
|
---|
| 527 | v1 |= efp[ 1][(l1>>=10) & 0x3f][0]; v2 |= efp[ 1][ l1 & 0x3f][1];
|
---|
| 528 | v1 |= efp[ 0][(l1>>=6) & 0x3f][0]; v2 |= efp[ 0][ l1 & 0x3f][1];
|
---|
| 529 |
|
---|
| 530 | v1 |= efp[ 7][ l2 & 0x3f][0]; v2 |= efp[ 7][ l2 & 0x3f][1];
|
---|
| 531 | v1 |= efp[ 6][(l2>>=6) & 0x3f][0]; v2 |= efp[ 6][ l2 & 0x3f][1];
|
---|
| 532 | v1 |= efp[ 5][(l2>>=10) & 0x3f][0]; v2 |= efp[ 5][ l2 & 0x3f][1];
|
---|
| 533 | v1 |= efp[ 4][(l2>>=6) & 0x3f][0]; v2 |= efp[ 4][ l2 & 0x3f][1];
|
---|
| 534 |
|
---|
| 535 | v1 |= efp[11][ r1 & 0x3f][0]; v2 |= efp[11][ r1 & 0x3f][1];
|
---|
| 536 | v1 |= efp[10][(r1>>=6) & 0x3f][0]; v2 |= efp[10][ r1 & 0x3f][1];
|
---|
| 537 | v1 |= efp[ 9][(r1>>=10) & 0x3f][0]; v2 |= efp[ 9][ r1 & 0x3f][1];
|
---|
| 538 | v1 |= efp[ 8][(r1>>=6) & 0x3f][0]; v2 |= efp[ 8][ r1 & 0x3f][1];
|
---|
| 539 |
|
---|
| 540 | v1 |= efp[15][ r2 & 0x3f][0]; v2 |= efp[15][ r2 & 0x3f][1];
|
---|
| 541 | v1 |= efp[14][(r2>>=6) & 0x3f][0]; v2 |= efp[14][ r2 & 0x3f][1];
|
---|
| 542 | v1 |= efp[13][(r2>>=10) & 0x3f][0]; v2 |= efp[13][ r2 & 0x3f][1];
|
---|
| 543 | v1 |= efp[12][(r2>>=6) & 0x3f][0]; v2 |= efp[12][ r2 & 0x3f][1];
|
---|
| 544 |
|
---|
| 545 | outbuf[0] = salt[0];
|
---|
| 546 | outbuf[1] = salt[1] ? salt[1] : salt[0];
|
---|
| 547 |
|
---|
| 548 | for(i=0; i<5; i++)
|
---|
| 549 | outbuf[i+2] = bin_to_ascii((v1>>(26-6*i)) & 0x3f);
|
---|
| 550 |
|
---|
| 551 | s = (v2 & 0xf) << 2; /* Save the rightmost 4 bit a moment */
|
---|
| 552 | v2 = (v2>>2) | ((v1 & 0x3)<<30); /* Shift two bits of v1 onto v2 */
|
---|
| 553 |
|
---|
| 554 | for(i=5; i<10; i++)
|
---|
| 555 | outbuf[i+2] = bin_to_ascii((v2>>(56-6*i)) & 0x3f);
|
---|
| 556 |
|
---|
| 557 | outbuf[12] = bin_to_ascii(s);
|
---|
| 558 | outbuf[13] = 0;
|
---|
| 559 |
|
---|
| 560 | return outbuf;
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 | char *crypt();
|
---|
| 564 |
|
---|
| 565 | /* Stub to provide fcrypt compatibility */
|
---|
| 566 |
|
---|
| 567 | char *fcrypt(key, salt)
|
---|
| 568 | char *key;
|
---|
| 569 | char *salt;
|
---|
| 570 | { return crypt(key, salt);
|
---|
| 571 | }
|
---|
| 572 |
|
---|