Print this page
first pass


 109 void
 110 des_setparity(char *p)
 111 {
 112         int i;
 113 
 114         for (i = 0; i < 8; i++) {
 115                 *p = partab[*p & 0x7f];
 116                 p++;
 117         }
 118 }
 119 #endif /* def _KERNEL */
 120 
 121 #ifdef CRYPT
 122 /*
 123  * Software encrypt or decrypt a block of data (multiple of 8 bytes)
 124  * Do the CBC ourselves if needed.
 125  */
 126 int
 127 __des_crypt(char *buf, unsigned int len, struct desparams *desp)
 128 {
 129 /* EXPORT DELETE START */
 130         short i;
 131         unsigned mode;
 132         unsigned dir;
 133         char nextiv[8];
 134         struct deskeydata softkey;
 135 
 136         mode = (unsigned)desp->des_mode;
 137         dir = (unsigned)desp->des_dir;
 138         des_setkey(desp->des_key, &softkey, dir);
 139         while (len != 0) {
 140                 switch (mode) {
 141                 case CBC:
 142                         switch (dir) {
 143                         case ENCRYPT:
 144                                 for (i = 0; i < 8; i++)
 145                                         buf[i] ^= desp->des_ivec[i];
 146                                 des_encrypt((uchar_t *)buf, &softkey);
 147                                 for (i = 0; i < 8; i++)
 148                                         desp->des_ivec[i] = buf[i];
 149                                 break;
 150                         case DECRYPT:
 151                                 for (i = 0; i < 8; i++)
 152                                         nextiv[i] = buf[i];
 153                                 des_encrypt((uchar_t *)buf, &softkey);
 154                                 for (i = 0; i < 8; i++) {
 155                                         buf[i] ^= desp->des_ivec[i];
 156                                         desp->des_ivec[i] = nextiv[i];
 157                                 }
 158                                 break;
 159                         }
 160                         break;
 161                 case ECB:
 162                         des_encrypt((uchar_t *)buf, &softkey);
 163                         break;
 164                 }
 165                 buf += 8;
 166                 len -= 8;
 167         }
 168 /* EXPORT DELETE END */
 169         return (1);
 170 }
 171 
 172 
 173 /*
 174  * Set the key and direction for an encryption operation
 175  * We build the 16 key entries here
 176  */
 177 static void
 178 des_setkey(uchar_t userkey[8], struct deskeydata *kd, unsigned int dir)
 179 {
 180 /* EXPORT DELETE START */
 181         long C, D;
 182         short i;
 183 
 184         /*
 185          * First, generate C and D by permuting
 186          * the key. The low order bit of each
 187          * 8-bit char is not used, so C and D are only 28
 188          * bits apiece.
 189          */
 190         {
 191                 short bit;
 192                 const short *pcc = PC1_C, *pcd = PC1_D;
 193 
 194                 C = D = 0;
 195                 for (i = 0; i < 28; i++) {
 196                         C <<= 1;
 197                         D <<= 1;
 198                         bit = *pcc++;
 199                         if (btst(userkey, bit))
 200                                 C |= 1;


 240                 case ENCRYPT:
 241                         c = &kd->keyval[i]; break;
 242                 case DECRYPT:
 243                         c = &kd->keyval[15 - i]; break;
 244                 }
 245                 c->long0 = 0;
 246                 c->long1 = 0;
 247                 bbit = (1 << 5) << 24;
 248                 for (j = 0; j < 4; j++) {
 249                         for (k = 0; k < 6; k++) {
 250                                 if (C & (BIT28 >> PC2_C[bit]))
 251                                         c->long0 |= bbit >> k;
 252                                 if (D & (BIT28 >> PC2_D[bit]))
 253                                         c->long1 |= bbit >> k;
 254                                 bit++;
 255                         }
 256                         bbit >>= 8;
 257                 }
 258 
 259         }
 260 /* EXPORT DELETE END */
 261 }
 262 
 263 
 264 
 265 /*
 266  * Do an encryption operation
 267  * Much pain is taken (with preprocessor) to avoid loops so the compiler
 268  * can do address arithmetic instead of doing it at runtime.
 269  * Note that the byte-to-chunk conversion is necessary to guarantee
 270  * processor byte-order independence.
 271  */
 272 static void
 273 des_encrypt(uchar_t *data, struct deskeydata *kd)
 274 {
 275 /* EXPORT DELETE START */
 276         chunk_t work1, work2;
 277 
 278         /*
 279          * Initial permutation
 280          * and byte to chunk conversion
 281          */
 282         {
 283                 const uint32_t *lp;
 284                 uint32_t l0, l1, w;
 285                 short i, pbit;
 286 
 287                 work1.byte0 = data[0];
 288                 work1.byte1 = data[1];
 289                 work1.byte2 = data[2];
 290                 work1.byte3 = data[3];
 291                 work1.byte4 = data[4];
 292                 work1.byte5 = data[5];
 293                 work1.byte6 = data[6];
 294                 work1.byte7 = data[7];
 295                 l0 = l1 = 0;


 423                 for (lp = &longtab[0], i = 32; i < 64; i++) {
 424                         if (w & *lp++) {
 425                                 pbit = FPtab[i];
 426                                 if (pbit < 32)
 427                                         l0 |= longtab[pbit];
 428                                 else
 429                                         l1 |= longtab[pbit-32];
 430                         }
 431                 }
 432                 work2.long0 = l0;
 433                 work2.long1 = l1;
 434         }
 435         data[0] = work2.byte0;
 436         data[1] = work2.byte1;
 437         data[2] = work2.byte2;
 438         data[3] = work2.byte3;
 439         data[4] = work2.byte4;
 440         data[5] = work2.byte5;
 441         data[6] = work2.byte6;
 442         data[7] = work2.byte7;
 443 
 444 /* EXPORT DELETE END */
 445 }
 446 #endif /* def CRYPT */


 109 void
 110 des_setparity(char *p)
 111 {
 112         int i;
 113 
 114         for (i = 0; i < 8; i++) {
 115                 *p = partab[*p & 0x7f];
 116                 p++;
 117         }
 118 }
 119 #endif /* def _KERNEL */
 120 
 121 #ifdef CRYPT
 122 /*
 123  * Software encrypt or decrypt a block of data (multiple of 8 bytes)
 124  * Do the CBC ourselves if needed.
 125  */
 126 int
 127 __des_crypt(char *buf, unsigned int len, struct desparams *desp)
 128 {

 129         short i;
 130         unsigned mode;
 131         unsigned dir;
 132         char nextiv[8];
 133         struct deskeydata softkey;
 134 
 135         mode = (unsigned)desp->des_mode;
 136         dir = (unsigned)desp->des_dir;
 137         des_setkey(desp->des_key, &softkey, dir);
 138         while (len != 0) {
 139                 switch (mode) {
 140                 case CBC:
 141                         switch (dir) {
 142                         case ENCRYPT:
 143                                 for (i = 0; i < 8; i++)
 144                                         buf[i] ^= desp->des_ivec[i];
 145                                 des_encrypt((uchar_t *)buf, &softkey);
 146                                 for (i = 0; i < 8; i++)
 147                                         desp->des_ivec[i] = buf[i];
 148                                 break;
 149                         case DECRYPT:
 150                                 for (i = 0; i < 8; i++)
 151                                         nextiv[i] = buf[i];
 152                                 des_encrypt((uchar_t *)buf, &softkey);
 153                                 for (i = 0; i < 8; i++) {
 154                                         buf[i] ^= desp->des_ivec[i];
 155                                         desp->des_ivec[i] = nextiv[i];
 156                                 }
 157                                 break;
 158                         }
 159                         break;
 160                 case ECB:
 161                         des_encrypt((uchar_t *)buf, &softkey);
 162                         break;
 163                 }
 164                 buf += 8;
 165                 len -= 8;
 166         }

 167         return (1);
 168 }
 169 
 170 
 171 /*
 172  * Set the key and direction for an encryption operation
 173  * We build the 16 key entries here
 174  */
 175 static void
 176 des_setkey(uchar_t userkey[8], struct deskeydata *kd, unsigned int dir)
 177 {

 178         long C, D;
 179         short i;
 180 
 181         /*
 182          * First, generate C and D by permuting
 183          * the key. The low order bit of each
 184          * 8-bit char is not used, so C and D are only 28
 185          * bits apiece.
 186          */
 187         {
 188                 short bit;
 189                 const short *pcc = PC1_C, *pcd = PC1_D;
 190 
 191                 C = D = 0;
 192                 for (i = 0; i < 28; i++) {
 193                         C <<= 1;
 194                         D <<= 1;
 195                         bit = *pcc++;
 196                         if (btst(userkey, bit))
 197                                 C |= 1;


 237                 case ENCRYPT:
 238                         c = &kd->keyval[i]; break;
 239                 case DECRYPT:
 240                         c = &kd->keyval[15 - i]; break;
 241                 }
 242                 c->long0 = 0;
 243                 c->long1 = 0;
 244                 bbit = (1 << 5) << 24;
 245                 for (j = 0; j < 4; j++) {
 246                         for (k = 0; k < 6; k++) {
 247                                 if (C & (BIT28 >> PC2_C[bit]))
 248                                         c->long0 |= bbit >> k;
 249                                 if (D & (BIT28 >> PC2_D[bit]))
 250                                         c->long1 |= bbit >> k;
 251                                 bit++;
 252                         }
 253                         bbit >>= 8;
 254                 }
 255 
 256         }

 257 }
 258 
 259 
 260 
 261 /*
 262  * Do an encryption operation
 263  * Much pain is taken (with preprocessor) to avoid loops so the compiler
 264  * can do address arithmetic instead of doing it at runtime.
 265  * Note that the byte-to-chunk conversion is necessary to guarantee
 266  * processor byte-order independence.
 267  */
 268 static void
 269 des_encrypt(uchar_t *data, struct deskeydata *kd)
 270 {

 271         chunk_t work1, work2;
 272 
 273         /*
 274          * Initial permutation
 275          * and byte to chunk conversion
 276          */
 277         {
 278                 const uint32_t *lp;
 279                 uint32_t l0, l1, w;
 280                 short i, pbit;
 281 
 282                 work1.byte0 = data[0];
 283                 work1.byte1 = data[1];
 284                 work1.byte2 = data[2];
 285                 work1.byte3 = data[3];
 286                 work1.byte4 = data[4];
 287                 work1.byte5 = data[5];
 288                 work1.byte6 = data[6];
 289                 work1.byte7 = data[7];
 290                 l0 = l1 = 0;


 418                 for (lp = &longtab[0], i = 32; i < 64; i++) {
 419                         if (w & *lp++) {
 420                                 pbit = FPtab[i];
 421                                 if (pbit < 32)
 422                                         l0 |= longtab[pbit];
 423                                 else
 424                                         l1 |= longtab[pbit-32];
 425                         }
 426                 }
 427                 work2.long0 = l0;
 428                 work2.long1 = l1;
 429         }
 430         data[0] = work2.byte0;
 431         data[1] = work2.byte1;
 432         data[2] = work2.byte2;
 433         data[3] = work2.byte3;
 434         data[4] = work2.byte4;
 435         data[5] = work2.byte5;
 436         data[6] = work2.byte6;
 437         data[7] = work2.byte7;


 438 }
 439 #endif /* def CRYPT */