⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 appendix-b.html

📁 Wiley - Applied Cryptography, Protocols, Algorthms, and Source Code in C
💻 HTML
📖 第 1 页 / 共 5 页
字号:
       right ^= (work << 16);
       work = ((right >> 4) ^ leftt) &amp 0×0f0f0f0fL;
       leftt ^= work;
       right ^= (work << 4);
       *block++ = right;
       *block = leftt;
       return;
}
/* Validation sets:
 *
 * Single–length key, single–length plaintext –
 * Key    : 0123 4567 89ab cdef
 * Plain  : 0123 4567 89ab cde7
 * Cipher : c957 4425 6a5e d31d
 *
 **********************************************************************/

void des_key(des_ctx *dc, unsigned char *key){
        deskey(key,EN0);
        cpkey(dc–>ek);
        deskey(key,DE1);
        cpkey(dc–>dk);
}
/* Encrypt several blocks in ECB mode.  Caller is responsible for
   short blocks. */
void des_enc(des_ctx *dc, unsigned char *data, int blocks){
        unsigned long work[2];
        int i;
        unsigned char *cp;
        cp = data;
        for(i=0;i<blocks;i++){
                scrunch(cp,work);
                desfunc(work,dc–>ek);
                unscrun(work,cp);
                cp+=8;
        }
}

void des_dec(des_ctx *dc, unsigned char *data, int blocks){
        unsigned long work[2];
        int i;
        unsigned char *cp;

        cp = data;
        for(i=0;i<blocks;i++){
                scrunch(cp,work);
                desfunc(work,dc–>dk);
                unscrun(work,cp);
                cp+=8;
        }
}
void main(void){
        des_ctx dc;
        int i;
        unsigned long data[10];
        char *cp,key[8] = {0×01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
        char x[8] = {0×01,0x23,0x45,0x67,0x89,0xab,0xcd,0xe7};

        cp = x;

        des_key(&ampdc,key);
        des_enc(&ampdc,cp,1);
        printf(“Enc(0..7,0..7) = ”);
        for(i=0;i<8;i++) printf(“%02x ”, ((unsigned int) cp[i])&amp0×00ff);
        printf(“\n”);

        des_dec(&ampdc,cp,1);

        printf(“Dec(above,0..7) = ”);
        for(i=0;i<8;i++) printf(“%02x ”,((unsigned int)cp[i])&amp0×00ff);
        printf(“\n”);

        cp = (char *) data;
        for(i=0;i<10;i++)data[i]=i;

        des_enc(&ampdc,cp,5); /* Enc 5 blocks. */
        for(i=0;i<10;i+=2) printf(“Block %01d = %08lx %08lx.\n”,
                                i/2,data[i],data[i+1]);

        des_dec(&ampdc,cp,1);
        des_dec(&ampdc,cp+8,4);
        for(i=0;i<10;i+=2) printf(“Block %01d = %08lx %08lx.\n”,
                                i/2,data[i],data[i+1]);
}
</PRE>
<!-- END CODE //-->
<H3><A NAME="Heading2"></A><FONT COLOR="#000077">LOKI91</FONT></H3>
<!-- CODE //-->
<PRE>
#include &lt;stdio.h&gt;

#define LOKIBLK        8              /* No of bytes in a LOKI data&#150;block          */
#define ROUNDS         16             /* No of LOKI rounds                        */

typedef unsigned long          Long;   /* type specification for aligned LOKI blocks */

extern Long    lokikey[2];     /* 64&#150;bit key used by LOKI routines          */
extern char    *loki_lib_ver;         /* String with version no. &amp copyright       */

#ifdef __STDC__                       /* declare prototypes for library functions  */
extern void enloki(char *b);
extern void deloki(char *b);
extern void setlokikey(char key[LOKIBLK]);
#else                          /* else just declare library functions extern */
extern void enloki(), deloki(), setlokikey();
#endif __STDC__

char P[32] = {
        31, 23, 15, 7, 30, 22, 14, 6,
        29, 21, 13, 5, 28, 20, 12, 4,
        27, 19, 11, 3, 26, 18, 10, 2,
        25, 17, 9, 1, 24, 16, 8, 0
        };

typedef        struct {
        short  gen;            /* irreducible polynomial used in this field */
        short  exp;            /* exponent used to generate this s function */

        } sfn_desc;
sfn_desc sfn[] = {
        { /* 101110111 */ 375, 31}, { /* 101111011 */ 379, 31},
        { /* 110000111 */ 391, 31}, { /* 110001011 */ 395, 31},
        { /* 110001101 */ 397, 31}, { /* 110011111 */ 415, 31},
        { /* 110100011 */ 419, 31}, { /* 110101001 */ 425, 31},
        { /* 110110001 */ 433, 31}, { /* 110111101 */ 445, 31},
        { /* 111000011 */ 451, 31}, { /* 111001111 */ 463, 31},
        { /* 111010111 */ 471, 31}, { /* 111011101 */ 477, 31},
        { /* 111100111 */ 487, 31}, { /* 111110011 */ 499, 31},
        { 00, 00}      };

typedef struct {
        Long loki_subkeys[ROUNDS];
} loki_ctx;

static Long    f();                   /* declare LOKI function f */
static short   s();                   /* declare LOKI S&#150;box fn s */

#define ROL12(b) b = ((b &lt;&lt; 12) | (b &gt;&gt; 20));
#define ROL13(b) b = ((b &lt;&lt; 13) | (b &gt;&gt; 19));

#ifdef  LITTLE_ENDIAN
#define bswap(cb) {                             \
        register char   c;                      \
        c = cb[0]; cb[0] = cb[3]; cb[3] = c;    \
        c = cb[1]; cb[1] = cb[2]; cb[2] = c;    \
        c = cb[4]; cb[4] = cb[7]; cb[7] = c;    \
        c = cb[5]; cb[5] = cb[6]; cb[6] = c;    \
}
#endif

void
setlokikey(loki_ctx *c, char *key)
{
        register       i;
        register Long  KL, KR;
#ifdef LITTLE_ENDIAN
        bswap(key);                   /* swap bytes round if little&#150;endian */
#endif
        KL = ((Long *)key)[0];
        KR = ((Long *)key)[1];
        for (i=0; i&lt;ROUNDS; i&#43;=4) {           /* Generate the 16 subkeys */
            c&#150;&gt;loki_subkeys[i] = KL;
            ROL12 (KL);
            c&#150;&gt;loki_subkeys[i&#43;1] = KL;
            ROL13 (KL);
            c&#150;&gt;loki_subkeys[i&#43;2] = KR;
            ROL12 (KR);
            c&#150;&gt;loki_subkeys[i&#43;3] = KR;
            ROL13 (KR);
        }

#ifdef LITTLE_ENDIAN
        bswap(key);                   /* swap bytes back if little&#150;endian */
#endif
}
void
enloki (loki_ctx *c, char *b)
{
        register       i;
        register Long  L, R;          /* left &amp right data halves  */

#ifdef LITTLE_ENDIAN
        bswap(b);                     /* swap bytes round if little&#150;endian */
#endif
        L = ((Long *)b)[0];
        R = ((Long *)b)[1];

        for (i=0; i&lt;ROUNDS; i&#43;=2) {           /* Encrypt with the 16 subkeys */
            L ^= f (R, c&#150;&gt;loki_subkeys[i]);
            R ^= f (L, c&#150;&gt;loki_subkeys[i&#43;1]);
        }

        ((Long *)b)[0] = R;           /* Y = swap(LR) */
        ((Long *)b)[1] = L;

#ifdef LITTLE_ENDIAN
        bswap(b);                     /* swap bytes round if little&#150;endian */
#endif
}

void
deloki(loki_ctx *c, char *b)
{
        register       i;
        register Long  L, R;                  /* left &amp right data halves  */

#ifdef LITTLE_ENDIAN
        bswap(b);                     /* swap bytes round if little&#150;endian */
#endif

        L = ((Long *)b)[0];                   /* LR = X XOR K */
        R = ((Long *)b)[1];

        for (i=ROUNDS; i&gt;0; i&#150;=2) {                   /* subkeys in reverse order */
            L ^= f(R, c&#150;&gt;loki_subkeys[i&#150;1]);
            R ^= f(L, c&#150;&gt;loki_subkeys[i&#150;2]);
        }

        ((Long *)b)[0] = R;                   /* Y = LR XOR K */
        ((Long *)b)[1] = L;
}

#define MASK12         0&#215;0fff                 /* 12 bit mask for expansion E */

static Long
f(r, k)
register Long  r;      /* Data value R(i&#150;1) */
Long           k;      /* Key     K(i)   */
{
        Long   a, b, c;               /* 32 bit S&#150;box output, &amp P output */
        a = r ^ k;                    /* A = R(i&#150;1) XOR K(i) */

        /* want to use slow speed/small size version */
        b = ((Long)s((a         &amp MASK12))      ) | /* B = S(E(R(i&#150;1))^K(i)) */
            ((Long)s(((a &gt;&gt;  8) &amp MASK12)) &lt;&lt;  8) |
            ((Long)s(((a &gt;&gt; 16) &amp MASK12)) &lt;&lt; 16) |
            ((Long)s((((a &gt;&gt; 24) | (a &lt;&lt; 8)) &amp MASK12)) &lt;&lt; 24);

        perm32(&ampc, &ampb, P);            /* C = P(S( E(R(i&#150;1)) XOR K(i))) */

        return(c);                    /* f returns the result C */
}

static short s(i)
register Long i;       /* return S&#150;box value for input i */
{
        register short r, c, v, t;
        short  exp8();                /* exponentiation routine for GF(2^8) */

        r = ((i&gt;&gt;8) &amp 0xc) | (i &amp 0x3);               /* row value&#150;top 2 &amp bottom 2 */
        c = (i&gt;&gt;2) &amp 0xff;                            /* column value&#150;middle 8 bits */
        t = (c &#43; ((r * 17) ^ 0xff)) &amp 0xff;           /* base value for Sfn */
        v = exp8(t, sfn[r].exp, sfn[r].gen);          /* Sfn[r] = t ^ exp mod gen */
        return(v);
}

#define        MSB     0x80000000L            /* MSB of 32&#150;bit word */

perm32(out, in , perm)
Long    *out;          /* Output 32&#150;bit block to be permuted                */
Long    *in;           /* Input  32&#150;bit block after permutation             */
char    perm[32];      /* Permutation array                                 */
{
        Long   mask = MSB;                    /* mask used to set bit in output    */
        register int   i, o, b;       /* input bit no, output bit no, value */
        register char  *p = perm;     /* ptr to permutation array  */

        *out = 0;                     /* clear output block */
        for (o=0; o&lt;32; o&#43;&#43;) {                /* For each output bit position o */
               i =(int)*p&#43;&#43;;                  /* get input bit permuted to output o */
               b = (*in &gt;&gt; i) &amp 01;           /* value of input bit i */
               if (b)                 /* If the input bit i is set */
                       *out |= mask;                  /*  OR in mask to output i */
               mask &gt;&gt;= 1;                            /* Shift mask to next bit    */
        }
}

#define SIZE 256               /* 256 elements in GF(2^8) */

short mult8(a, b, gen)
short   a, b;          /* operands for multiply */
short   gen;           /* irreducible polynomial generating Galois Field */
{
        short  product = 0;           /* result of multiplication */

        while(b != 0) {                       /* while multiplier is non&#150;zero */
               if (b &amp 01)
                       product ^= a;          /*   add multiplicand if LSB of b set */
               a &lt;&lt;= 1;               /*   shift multiplicand one place */
               if (a &gt;= SIZE)
                       a ^= gen;      /*   and modulo reduce if needed */
               b &gt;&gt;= 1;               /*   shift multiplier one place  */
        }
        return(product);
}

short exp8(base, exponent, gen)
short   base;          /* base of exponentiation      */
short   exponent;      /* exponent                    */
short   gen;           /* irreducible polynomial generating Galois Field */
{
        short  accum = base;          /* superincreasing sequence of base */
        short  result = 1;     /* result of exponentiation       */

        if (base == 0)                /* if zero base specified then      */
               return(0);      /* the result is &#147;0&#148; if base = 0    */

        while (exponent != 0) {       /* repeat while exponent non&#150;zero */
               if (( exponent &amp 0&#215;0001) == 0&#215;0001)           /* multiply if exp 1 */
                       result = mult8(result, accum, gen);
               exponent &gt;&gt;= 1;                /* shift exponent to next digit */
               accum = mult8(accum, accum, gen);             /* &amp square  */
        }
        return(result);
}

void loki_key(loki_ctx *c, unsigned char *key){
        setlokikey(c,key);
}
void loki_enc(loki_ctx *c, unsigned char *data, int blocks){
        unsigned char *cp;
        int i;

        cp = data;
        for(i=0;i&lt;blocks;i&#43;&#43;){
                enloki(c,cp);
                cp&#43;=8;
        }
}

void loki_dec(loki_ctx *c, unsigned char *data, int blocks){
        unsigned char *cp;
        int i;

        cp = data;
        for(i=0;i&lt;blocks;i&#43;&#43;){
                deloki(c,cp);
                cp&#43;=8;
        }
}

void main(void){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -