/* Allocate space and initialize DES lookup arrays * mode == 0: standard Data Encryption Algorithm * mode == 1: DEA without initial and final permutations for speed * mode == 2: DEA without permutations and with 128-byte key (completely * independent subkeys for each round)
*/ int desinit(int mode) { //char *malloc(); // Co3
sp_ = NULL;
iperm = NULL;
fperm = NULL;
kn = NULL;
} /* Set key (initialize key schedule array) */ void setkey(char *key) { /* 64 bits (will use only 56) */ char pc1m[56]; /* place to modify pc1 into */ char pcr[56]; /* place to rotate pc1 into */ int i,j,l; int m;
/* In mode 2, the 128 bytes of subkey are set directly from the * user's key, allowing him to use completely independent * subkeys for each round. Note that the user MUST specify a * full 128 bytes. * * I would like to think that this technique gives the NSA a real * headache, but I'm not THAT naive.
*/ if(desmode == 2){ for(i=0;i<16;i++) for(j=0;j<8;j++)
kn[i][j] = *key++; return;
} /* Clear key schedule */ for (i=0; i<16; i++) for (j=0; j<8; j++)
kn[i][j]=0;
for (j=0; j<56; j++) { /* convert pc1 to bits of key */
l=pc1[j]-1; /* integer bit location */
m = l & 07; /* find bit */
pc1m[j]=(key[l>>3] & /* find which key byte l is in */
bytebit[m]) /* and which bit of that byte */
? 1 : 0; /* and store 1-bit result */
} for (i=0; i<16; i++) { /* key chunk for each iteration */ for (j=0; j<56; j++) /* rotate pc1 the right amount */
pcr[j] = pc1m[(l=j+totrot[i])<(j<28? 28 : 56) ? l: l-28]; /* rotate left and right halves independently */ for (j=0; j<48; j++){ /* select bits individually */ /* check bit that goes to kn[j] */ if (pcr[pc2[j]-1]){ /* mask it in if it's there */
l= j % 6;
kn[i][j/6] |= bytebit[l] >> 2;
}
}
}
} /* In-place encryption of 64-bit block */ void endes(char *block) { int i; unsignedlong work[2]; /* Working data storage */ unsignedlong tmp;
if(perm == NULL){ /* No permutation, just copy */ for(i=8; i!=0; i--)
*outblock++ = *inblock++; return;
} /* Clear output block */ for (i=8, ob = outblock; i != 0; i--)
*ob++ = 0;
ib = inblock; for (j = 0; j < 16; j += 2, ib++) { /* for each input nibble */ int ix=(*ib >> 4) & 017; int iy=*ib & 017;
ob = outblock;
p = perm[j][ix];
q = perm[j + 1][iy]; #if TRACE
printf("(ix,iy)=(%i,%i) (p,q)=(%02x%02x%02x%02x%02x%02x%02x%02x,%02x%02x%02x%02x%02x%02x%02x%02x)\n",
ix,iy,p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7],q[0],q[1],q[2],q[3],q[4],q[5],q[6],q[7]); #endif for (i = 8; i != 0; i--){ /* and each output byte */
*ob++ |= *p++ | *q++; /* OR the masks together*/
}
} #if TRACE
printf("outblock=%02x%02x%02x%02x%02x%02x%02x%02x\n",
outblock[0],outblock[1],outblock[2],outblock[3],outblock[4],outblock[5],outblock[6],outblock[7]); #endif
}
/* Do one DES cipher round */ staticvoid desround(int num,unsignedlong *block) { /* i.e. the num-th one */ //long f();
/* The rounds are numbered from 0 to 15. On even rounds * the right half is fed to f() and the result exclusive-ORs * the left half; on odd rounds the reverse is done.
*/ if(num & 1){
block[1] ^= f(block[0],kn[num]);
} else {
block[0] ^= f(block[1],kn[num]);
}
} /* The nonlinear function f(r,k), the heart of DES */ staticlong f(unsignedlong r,char subkey[8]) { /* 32 bits */ /* 48-bit key for this round */
long rval,rt; #if TRACE char *cp; int i;
printf("f(%08lx, %02x %02x %02x %02x %02x %02x %02x %02x) = ",
r,
subkey[0], subkey[1], subkey[2],
subkey[3], subkey[4], subkey[5],
subkey[6], subkey[7]); #endif /* Run E(R) ^ K through the combined S & P boxes * This code takes advantage of a convenient regularity in * E, namely that each group of 6 bits in E(R) feeding * a single S-box is a contiguous segment of R.
*/
rt = (r >> 1) | ((r & 1) ? 0x80000000 : 0);
rval = 0;
rval |= sp_[0][((rt >> 26) ^ *subkey++) & 0x3f];
rval |= sp_[1][((rt >> 22) ^ *subkey++) & 0x3f];
rval |= sp_[2][((rt >> 18) ^ *subkey++) & 0x3f];
rval |= sp_[3][((rt >> 14) ^ *subkey++) & 0x3f];
rval |= sp_[4][((rt >> 10) ^ *subkey++) & 0x3f];
rval |= sp_[5][((rt >> 6) ^ *subkey++) & 0x3f];
rval |= sp_[6][((rt >> 2) ^ *subkey++) & 0x3f];
rt = (r << 1) | ((r & 0x80000000) ? 1 : 0);
rval |= sp_[7][(rt ^ *subkey) & 0x3f]; #if TRACE
printf(" %08lx\n",rval); #endif return rval;
} /* initialize a perm array */ staticvoid perminit(char perm[16][16][8],char p[64]) { /* 64-bit, either init or final */ int l, j, k; int i,m;
/* Clear the permutation array */ for (i=0; i<16; i++) for (j=0; j<16; j++) for (k=0; k<8; k++)
perm[i][j][k]=0;
for (i=0;i<16;i++) { /* each input nibble position */ for (j = 0;j<16;j++) { /* each possible input nibble */ for (k = 0; k < 64; k++)/* each output bit position */
{ l = p[k] - 1; /* where does this bit come from*/ if ((l >> 2) != i) /* does it come from input posn?*/ continue; /* if not, bit k is 0 */ if (!(j & nibblebit[l & 3])) continue; /* any such bit in input? */
m = k & 07; /* which bit is this in the byte*/
perm[i][j][k>>3] |= bytebit[m]; #if TRACE
printf("perminit l=%i perm[%i][%i][%i]=%i\n",l,i,j,k>>3,perm[i][j][k>>3]); #endif
}}}
}
/* Initialize the lookup table for the combined S and P boxes */ staticvoid spinit() { char pbox[32]; int p,i,s,j,rowcol; long val;
/* Compute pbox, the inverse of p32i. * This is easier to work with
*/ for(p=0;p<32;p++){ for(i=0;i<32;i++){ if(p32i[i]-1 == p){
pbox[p] = i; break;
}
}
} for(s = 0; s < 8; s++){ /* For each S-box */ for(i=0; i<64; i++){ /* For each possible input */
val = 0; /* The row number is formed from the first and last * bits; the column number is from the middle 4
*/
rowcol = (i & 32) | ((i & 1) ? 16 : 0) | ((i >> 1) & 0xf); for(j=0;j<4;j++){ /* For each output bit */ if(si[s][rowcol] & (8 >> j)){
val |= 1L << (31 - pbox[4*s + j]);
}
}
sp_[s][i] = val;
return x;
} #endif //------------------------------------------------------------------------------ // E n d e d i e s e r Q u e l l e //------------------------------------------------------------------------------
¤ Diese beiden folgenden Angebotsgruppen bietet das Unternehmen0.8Angebot
Wie Sie bei der Firma Beratungs- und Dienstleistungen beauftragen können
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.