Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions | Variables
des.c File Reference
#include "des.h"
#include <stddef.h>

Go to the source code of this file.

Defines

#define FLIP_RIGHT_DIAGONAL(word, temp)
#define BYTESWAP(word, temp)
#define PC1(left, right, c0, d0, temp)
#define LEFT_SHIFT_1(reg)   (((reg << 1) | (reg >> 27)) & 0x0FFFFFFF)
#define LEFT_SHIFT_2(reg)   (((reg << 2) | (reg >> 26)) & 0x0FFFFFFF)
#define PC2LOOKUP(b, c)   *(HALF *)((BYTE *)&PC2[b][0]+(c))
#define IP(left, right, temp)
#define FP(left, right, temp)
#define KSLOOKUP(s, b)   *(HALF*)((BYTE*)&SP[s][0]+((temp >> b) & 0xFC))
#define ROUND(out, in, r)

Functions

void DES_MakeSchedule (HALF *ks, const BYTE *key, DESDirection direction)
void DES_Do1Block (HALF *ks, const BYTE *inbuf, BYTE *outbuf)

Variables

static const HALF SP [8][64]
static const HALF PC2 [8][64]

Define Documentation

#define BYTESWAP (   word,
  temp 
)
Value:
word = (word >> 16) | (word << 16); \
    temp = 0x00ff00ff; \
    word = ((word & temp) << 8) | ((word >> 8) & temp);

Definition at line 403 of file des.c.

#define FLIP_RIGHT_DIAGONAL (   word,
  temp 
)
Value:
temp  = (word ^ (word >> 18)) & 0x00003333; \
    word ^=  temp | (temp << 18); \
    temp  = (word ^ (word >> 9)) & 0x00550055; \
    word ^=  temp | (temp << 9);

Definition at line 397 of file des.c.

#define FP (   left,
  right,
  temp 
)
Value:
right ^= temp = ((left >> 1) ^ right) & 0x55555555; \
    left  ^= temp << 1; \
    right ^= temp = ((left << 8) ^ right) & 0xff00ff00; \
    left  ^= temp >> 8; \
    right ^= temp = ((left << 2) ^ right) & 0xcccccccc; \
    left  ^= temp >> 2; \
    right ^= temp = ((left >> 16) ^ right) & 0x0000ffff; \
    left  ^= temp << 16; \
    right ^= temp = ((left >> 4) ^  right) & 0x0f0f0f0f; \
    left  ^= temp << 4;

Definition at line 566 of file des.c.

#define IP (   left,
  right,
  temp 
)
Value:
right ^= temp = ((left >> 4) ^  right) & 0x0f0f0f0f; \
    left  ^= temp << 4; \
    right ^= temp = ((left >> 16) ^ right) & 0x0000ffff; \
    left  ^= temp << 16; \
    right ^= temp = ((left << 2) ^ right) & 0xcccccccc; \
    left  ^= temp >> 2; \
    right ^= temp = ((left << 8) ^ right) & 0xff00ff00; \
    left  ^= temp >> 8; \
    right ^= temp = ((left >> 1) ^ right) & 0x55555555; \
    left  ^= temp << 1;

Definition at line 550 of file des.c.

#define KSLOOKUP (   s,
  b 
)    *(HALF*)((BYTE*)&SP[s][0]+((temp >> b) & 0xFC))
#define LEFT_SHIFT_1 (   reg)    (((reg << 1) | (reg >> 27)) & 0x0FFFFFFF)

Definition at line 417 of file des.c.

#define LEFT_SHIFT_2 (   reg)    (((reg << 2) | (reg >> 26)) & 0x0FFFFFFF)

Definition at line 418 of file des.c.

#define PC1 (   left,
  right,
  c0,
  d0,
  temp 
)
Value:
right ^= temp = ((left >> 4) ^ right) & 0x0f0f0f0f; \
    left  ^= temp << 4; \
    FLIP_RIGHT_DIAGONAL(left, temp); \
    FLIP_RIGHT_DIAGONAL(right, temp); \
    BYTESWAP(right, temp); \
    c0 = right >> 4; \
    d0 = ((left & 0x00ffffff) << 4) | (right & 0xf);

Definition at line 408 of file des.c.

#define PC2LOOKUP (   b,
  c 
)    *(HALF *)((BYTE *)&PC2[b][0]+(c))
#define ROUND (   out,
  in,
  r 
)
Value:
temp  = in ^ ks[2*r]; \
    out ^= KSLOOKUP( 1,  24 ); \
    out ^= KSLOOKUP( 3,  16 ); \
    out ^= KSLOOKUP( 5,   8 ); \
    out ^= KSLOOKUP( 7,   0 ); \
    temp  = ((in >> 4) | (in << 28)) ^ ks[2*r+1]; \
    out ^= KSLOOKUP( 0,  24 ); \
    out ^= KSLOOKUP( 2,  16 ); \
    out ^= KSLOOKUP( 4,   8 ); \
    out ^= KSLOOKUP( 6,   0 );

Function Documentation

void DES_Do1Block ( HALF ks,
const BYTE inbuf,
BYTE outbuf 
)

Definition at line 579 of file des.c.

{
    register HALF left, right;
    register HALF temp;

#if defined(_X86_)
    left  = HALFPTR(inbuf)[0]; 
    right = HALFPTR(inbuf)[1]; 
    BYTESWAP(left, temp);
    BYTESWAP(right, temp);
#else
    if (((ptrdiff_t)inbuf & 0x03) == 0) {
       left  = HALFPTR(inbuf)[0]; 
       right = HALFPTR(inbuf)[1]; 
#if defined(IS_LITTLE_ENDIAN)
       BYTESWAP(left, temp);
       BYTESWAP(right, temp);
#endif
    } else {
       left    = ((HALF)inbuf[0] << 24) | ((HALF)inbuf[1] << 16) | 
                ((HALF)inbuf[2] << 8)  | inbuf[3];
       right   = ((HALF)inbuf[4] << 24) | ((HALF)inbuf[5] << 16) | 
                ((HALF)inbuf[6] << 8)  | inbuf[7];
    }
#endif

    IP(left, right, temp);

    /* shift the values left circularly 3 bits. */
    left  = (left  << 3) | (left  >> 29);
    right = (right << 3) | (right >> 29);

#ifdef USE_INDEXING
#define KSLOOKUP(s,b) SP[s][((temp >> (b+2)) & 0x3f)]
#else
#define KSLOOKUP(s,b) *(HALF*)((BYTE*)&SP[s][0]+((temp >> b) & 0xFC))
#endif
#define ROUND(out, in, r) \
    temp  = in ^ ks[2*r]; \
    out ^= KSLOOKUP( 1,  24 ); \
    out ^= KSLOOKUP( 3,  16 ); \
    out ^= KSLOOKUP( 5,   8 ); \
    out ^= KSLOOKUP( 7,   0 ); \
    temp  = ((in >> 4) | (in << 28)) ^ ks[2*r+1]; \
    out ^= KSLOOKUP( 0,  24 ); \
    out ^= KSLOOKUP( 2,  16 ); \
    out ^= KSLOOKUP( 4,   8 ); \
    out ^= KSLOOKUP( 6,   0 ); 

    /* Do the 16 Feistel rounds */
    ROUND(left, right, 0)
    ROUND(right, left, 1)
    ROUND(left, right, 2)
    ROUND(right, left, 3)
    ROUND(left, right, 4)
    ROUND(right, left, 5)
    ROUND(left, right, 6)
    ROUND(right, left, 7)
    ROUND(left, right, 8)
    ROUND(right, left, 9)
    ROUND(left, right, 10)
    ROUND(right, left, 11)
    ROUND(left, right, 12)
    ROUND(right, left, 13)
    ROUND(left, right, 14)
    ROUND(right, left, 15)

    /* now shift circularly right 3 bits to undo the shifting done 
    ** above.  switch left and right here. 
    */
    temp  = (left >> 3) | (left << 29); 
    left  = (right >> 3) | (right << 29); 
    right = temp;

    FP(left, right, temp);

#if defined(_X86_)
    BYTESWAP(left, temp);
    BYTESWAP(right, temp);
    HALFPTR(outbuf)[0]  = left; 
    HALFPTR(outbuf)[1]  = right; 
#else
    if (((ptrdiff_t)outbuf & 0x03) == 0) {
#if defined(IS_LITTLE_ENDIAN)
       BYTESWAP(left, temp);
       BYTESWAP(right, temp);
#endif
       HALFPTR(outbuf)[0]  = left; 
       HALFPTR(outbuf)[1]  = right; 
    } else {
       outbuf[0] = (BYTE)(left >> 24);
       outbuf[1] = (BYTE)(left >> 16);
       outbuf[2] = (BYTE)(left >>  8);
       outbuf[3] = (BYTE)(left      );

       outbuf[4] = (BYTE)(right >> 24);
       outbuf[5] = (BYTE)(right >> 16);
       outbuf[6] = (BYTE)(right >>  8);
       outbuf[7] = (BYTE)(right      );
    }
#endif

}
void DES_MakeSchedule ( HALF ks,
const BYTE key,
DESDirection  direction 
)

Definition at line 425 of file des.c.

{
    register HALF left, right;
    register HALF c0, d0;
    register HALF temp;
    int           delta;
    unsigned int  ls;

#if defined(_X86_)
    left  = HALFPTR(key)[0]; 
    right = HALFPTR(key)[1]; 
    BYTESWAP(left, temp);
    BYTESWAP(right, temp);
#else
    if (((ptrdiff_t)key & 0x03) == 0) {
       left  = HALFPTR(key)[0]; 
       right = HALFPTR(key)[1]; 
#if defined(IS_LITTLE_ENDIAN)
       BYTESWAP(left, temp);
       BYTESWAP(right, temp);
#endif
    } else {
       left    = ((HALF)key[0] << 24) | ((HALF)key[1] << 16) | 
                ((HALF)key[2] << 8)  | key[3];
       right   = ((HALF)key[4] << 24) | ((HALF)key[5] << 16) | 
                ((HALF)key[6] << 8)  | key[7];
    }
#endif

    PC1(left, right, c0, d0, temp);

    if (direction == DES_ENCRYPT) {
       delta = 2 * (int)sizeof(HALF);
    } else {
       ks += 30;
       delta = (-2) * (int)sizeof(HALF);
    }

    for (ls = 0x8103; ls; ls >>= 1) {
       if ( ls & 1 ) {
           c0 = LEFT_SHIFT_1( c0 );
           d0 = LEFT_SHIFT_1( d0 );
       } else {
           c0 = LEFT_SHIFT_2( c0 );
           d0 = LEFT_SHIFT_2( d0 );
       }

#ifdef USE_INDEXING
#define PC2LOOKUP(b,c) PC2[b][c]

       left   = PC2LOOKUP(0, ((c0 >> 22) & 0x3F) );
       left  |= PC2LOOKUP(1, ((c0 >> 13) & 0x3F) );
       left  |= PC2LOOKUP(2, ((c0 >>  4) & 0x38) | (c0 & 0x7) );
       left  |= PC2LOOKUP(3, ((c0>>18)&0xC) | ((c0>>11)&0x3) | (c0&0x30));

       right  = PC2LOOKUP(4, ((d0 >> 22) & 0x3F) );
       right |= PC2LOOKUP(5, ((d0 >> 15) & 0x30) | ((d0 >> 14) & 0xf) );
       right |= PC2LOOKUP(6, ((d0 >>  7) & 0x3F) );
       right |= PC2LOOKUP(7, ((d0 >>  1) & 0x3C) | (d0 & 0x3));
#else
#define PC2LOOKUP(b,c) *(HALF *)((BYTE *)&PC2[b][0]+(c))

       left   = PC2LOOKUP(0, ((c0 >> 20) & 0xFC) );
       left  |= PC2LOOKUP(1, ((c0 >> 11) & 0xFC) );
       left  |= PC2LOOKUP(2, ((c0 >>  2) & 0xE0) | ((c0 <<  2) & 0x1C) );
       left  |= PC2LOOKUP(3, ((c0>>16)&0x30)|((c0>>9)&0xC)|((c0<<2)&0xC0));

       right  = PC2LOOKUP(4, ((d0 >> 20) & 0xFC) );
       right |= PC2LOOKUP(5, ((d0 >> 13) & 0xC0) | ((d0 >> 12) & 0x3C) );
       right |= PC2LOOKUP(6, ((d0 >>  5) & 0xFC) );
       right |= PC2LOOKUP(7, ((d0 <<  1) & 0xF0) | ((d0 << 2) & 0x0C));
#endif
       /* left  contains key bits for S1 S3 S2 S4 */
       /* right contains key bits for S6 S8 S5 S7 */
       temp = (left  << 16)        /* S2 S4 XX XX */
            | (right >> 16);       /* XX XX S6 S8 */
       ks[0] = temp;

       temp = (left  & 0xffff0000) /* S1 S3 XX XX */
            | (right & 0x0000ffff);/* XX XX S5 S7 */
       ks[1] = temp;

       ks = (HALF*)((BYTE *)ks + delta);
    }
}

Variable Documentation

const HALF PC2[8][64] [static]

Definition at line 203 of file des.c.

const HALF SP[8][64] [static]

Definition at line 56 of file des.c.