Set key (initialize key schedule array) */
| 282 | |
| 283 | /* Set key (initialize key schedule array) */ |
| 284 | void RawDES::RawSetKey(CipherDir dir, const byte *key) |
| 285 | { |
| 286 | #if (_MSC_VER >= 1600) || (__cplusplus >= 201103L) |
| 287 | # define register /* Define to nothing for C++11 and above */ |
| 288 | #endif |
| 289 | |
| 290 | SecByteBlock buffer(56+56+8); |
| 291 | byte *const pc1m=buffer; /* place to modify pc1 into */ |
| 292 | byte *const pcr=pc1m+56; /* place to rotate pc1 into */ |
| 293 | byte *const ks=pcr+56; |
| 294 | register int i,j,l; |
| 295 | int m; |
| 296 | |
| 297 | for (j=0; j<56; j++) { /* convert pc1 to bits of key */ |
| 298 | l=pc1[j]-1; /* integer bit location */ |
| 299 | m = l & 07; /* find bit */ |
| 300 | pc1m[j]=(key[l>>3] & /* find which key byte l is in */ |
| 301 | bytebit[m]) /* and which bit of that byte */ |
| 302 | ? 1 : 0; /* and store 1-bit result */ |
| 303 | } |
| 304 | for (i=0; i<16; i++) { /* key chunk for each iteration */ |
| 305 | memset(ks,0,8); /* Clear key schedule */ |
| 306 | for (j=0; j<56; j++) /* rotate pc1 the right amount */ |
| 307 | pcr[j] = pc1m[(l=j+totrot[i])<(j<28? 28 : 56) ? l: l-28]; |
| 308 | /* rotate left and right halves independently */ |
| 309 | for (j=0; j<48; j++){ /* select bits individually */ |
| 310 | /* check bit that goes to ks[j] */ |
| 311 | if (pcr[pc2[j]-1]){ |
| 312 | /* mask it in if it's there */ |
| 313 | l= j % 6; |
| 314 | ks[j/6] |= bytebit[l] >> 2; |
| 315 | } |
| 316 | } |
| 317 | /* Now convert to odd/even interleaved form for use in F */ |
| 318 | k[2*i] = ((word32)ks[0] << 24) |
| 319 | | ((word32)ks[2] << 16) |
| 320 | | ((word32)ks[4] << 8) |
| 321 | | ((word32)ks[6]); |
| 322 | k[2*i+1] = ((word32)ks[1] << 24) |
| 323 | | ((word32)ks[3] << 16) |
| 324 | | ((word32)ks[5] << 8) |
| 325 | | ((word32)ks[7]); |
| 326 | } |
| 327 | |
| 328 | if (dir==DECRYPTION) // reverse key schedule order |
| 329 | for (i=0; i<16; i+=2) |
| 330 | { |
| 331 | std::swap(k[i], k[32-2-i]); |
| 332 | std::swap(k[i+1], k[32-1-i]); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | void RawDES::RawProcessBlock(word32 &l_, word32 &r_) const |
| 337 | { |