MCPcopy Create free account
hub / github.com/Tripwire/tripwire-open-source / DES

Method DES

src/cryptlib/des.cpp:166–213  ·  view source on GitHub ↗

Set key (initialize key schedule array) */

Source from the content-addressed store, hash-verified

164
165/* Set key (initialize key schedule array) */
166DES::DES(const byte *key, CipherDir dir)
167 : k(32)
168{
169 SecByteBlock buffer(56+56+8);
170 byte *const pc1m=buffer; /* place to modify pc1 into */
171 byte *const pcr=pc1m+56; /* place to rotate pc1 into */
172 byte *const ks=pcr+56;
173 register unsigned int i,j,l;
174 int m;
175
176 for (j=0; j<56; j++) { /* convert pc1 to bits of key */
177 l=pc1[j]-1; /* integer bit location */
178 m = l & 07; /* find bit */
179 pc1m[j]=(key[l>>3] & /* find which key byte l is in */
180 bytebit[m]) /* and which bit of that byte */
181 ? 1 : 0; /* and store 1-bit result */
182 }
183 for (i=0; i<16; i++) { /* key chunk for each iteration */
184 memset(ks,0,8); /* Clear key schedule */
185 for (j=0; j<56; j++) /* rotate pc1 the right amount */
186 pcr[j] = pc1m[(l=j+totrot[i])<(j<28? 28 : 56) ? l: l-28];
187 /* rotate left and right halves independently */
188 for (j=0; j<48; j++){ /* select bits individually */
189 /* check bit that goes to ks[j] */
190 if (pcr[pc2[j]-1]){
191 /* mask it in if it's there */
192 l= j % 6;
193 ks[j/6] |= bytebit[l] >> 2;
194 }
195 }
196 /* Now convert to odd/even interleaved form for use in F */
197 k[2*i] = ((word32)ks[0] << 24)
198 | ((word32)ks[2] << 16)
199 | ((word32)ks[4] << 8)
200 | ((word32)ks[6]);
201 k[2*i+1] = ((word32)ks[1] << 24)
202 | ((word32)ks[3] << 16)
203 | ((word32)ks[5] << 8)
204 | ((word32)ks[7]);
205 }
206
207 if (dir==DECRYPTION) // reverse key schedule order
208 for (i=0; i<16; i+=2)
209 {
210 std::swap(k[i], k[32-2-i]);
211 std::swap(k[i+1], k[32-1-i]);
212 }
213}
214/* End of C code common to both versions */
215
216/* C code only in portable version */

Callers

nothing calls this directly

Calls 1

swapFunction · 0.85

Tested by

no test coverage detected