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

📄 appendix-b.html

📁 Applied Cryptography
💻 HTML
📖 第 1 页 / 共 5 页
字号:
                        i/2,data[i],data[i&#43;1]);        loki_dec(&amplc,cp,1);        loki_dec(&amplc,cp&#43;8,4);        for(i=0;i&lt;10;i&#43;=2) printf(&#147;Block %01d = %08lx %08lx\n&#148;,                        i/2,data[i],data[i&#43;1]);}</PRE><!-- END CODE //--><H3><A NAME="Heading3"></A><FONT COLOR="#000077">IDEA</FONT></H3><!-- CODE //--><PRE>typedef unsigned char boolean;      /* values are TRUE or FALSE */typedef unsigned char byte; /* values are 0&#150;255 */typedef byte *byteptr;      /* pointer to byte */typedef char *string;/* pointer to ASCII character string */typedef unsigned short word16;      /* values are 0&#150;65535 */typedef unsigned long word32;       /* values are 0&#150;4294967295 */#ifndef TRUE#define FALSE 0#define TRUE (!FALSE)#endif /* if TRUE not already defined */#ifndef min   /* if min macro not already defined */#define min(a,b) ( (a)&lt;(b) ? (a) : (b) )#define max(a,b) ( (a)&gt;(b) ? (a) : (b) )#endif /* if min macro not already defined */#define IDEAKEYSIZE 16#define IDEABLOCKSIZE 8#define IDEAROUNDS 8#define IDEAKEYLEN (6*IDEAROUNDS&#43;4)typedef struct{       word16 ek[IDEAKEYLEN],dk[IDEAKEYLEN];}idea_ctx;/* End includes for IDEA.C */#ifdef IDEA32        /* Use &gt;16&#150;bit temporaries */#define low16(x) ((x) &amp 0xFFFF)typedef unsigned int uint16;/* at LEAST 16 bits, maybe more */#else#define low16(x) (x) /* this is only ever applied to uint16&#146;s */typedef word16 uint16;#endif#ifdef SMALL_CACHEstatic uint16mul(register uint16 a, register uint16 b){       register word32 p;       p = (word32)a * b;       if (p) {              b = low16(p);              a = p&gt;&gt;16;              return (b &#150; a) &#43; (b &lt; a);       } else if (a) {              return 1&#150;b;       } else {              return 1&#150;a;       }} /* mul */#endif /* SMALL_CACHE */static uint16mulInv(uint16 x){       uint16 t0, t1;       uint16 q, y;       if (x &lt;= 1)              return x;     /* 0 and 1 are self&#150;inverse */       t1 = 0x10001L / x;   /* Since x &gt;= 2, this fits into 16 bits */       y = 0x10001L % x;       if (y == 1)              return low16(1&#150;t1);       t0 = 1;       do {              q = x / y;              x = x % y;              t0 &#43;= q * t1;              if (x == 1)                     return t0;              q = y / x;              y = y % x;              t1 &#43;= q * t0;       } while (y != 1);       return low16(1&#150;t1);} /* mukInv */static voidideaExpandKey(byte const *userkey, word16 *EK){       int i,j;       for (j=0; j&lt;8; j&#43;&#43;) {              EK[j] = (userkey[0]&lt;&lt;8) &#43; userkey[1];              userkey &#43;= 2;       }       for (i=0; j &lt; IDEAKEYLEN; j&#43;&#43;) {              i&#43;&#43;;              EK[i&#43;7] = EK[i &amp 7] &lt;&lt; 9 | EK[i&#43;1 &amp 7] &gt;&gt; 7;              EK &#43;= i &amp 8;              i &amp= 7;       }} /* ideaExpandKey */static voidideaInvertKey(word16 const *EK, word16 DK[IDEAKEYLEN]){       int i;       uint16 t1, t2, t3;       word16 temp[IDEAKEYLEN];       word16 *p = temp &#43; IDEAKEYLEN;       t1 = mulInv(*EK&#43;&#43;);       t2 = &#150;*EK&#43;&#43;;       t3 = &#150;*EK&#43;&#43;;       *&#150;&#150;p = mulInv(*EK&#43;&#43;);       *&#150;&#150;p = t3;       *&#150;&#150;p = t2;       *&#150;&#150;p = t1;       for (i = 0; i &lt; IDEAROUNDS&#150;1; i&#43;&#43;) {              t1 = *EK&#43;&#43;;              *&#150;&#150;p = *EK&#43;&#43;;              *&#150;&#150;p = t1;              t1 = mulInv(*EK&#43;&#43;);              t2 = &#150;*EK&#43;&#43;;              t3 = &#150;*EK&#43;&#43;;              *&#150;&#150;p = mulInv(*EK&#43;&#43;);              *&#150;&#150;p = t2;              *&#150;&#150;p = t3;              *&#150;&#150;p = t1;       }       t1 = *EK&#43;&#43;;       *&#150;&#150;p = *EK&#43;&#43;;       *&#150;&#150;p = t1;       t1 = mulInv(*EK&#43;&#43;);       t2 = &#150;*EK&#43;&#43;;       t3 = &#150;*EK&#43;&#43;;       *&#150;&#150;p = mulInv(*EK&#43;&#43;);       *&#150;&#150;p = t3;       *&#150;&#150;p = t2;       *&#150;&#150;p = t1;/* Copy and destroy temp copy */       memcpy(DK, temp, sizeof(temp));       for(i=0;i&lt;IDEAKEYLEN;i&#43;&#43;)temp[i]=0;} /* ideaInvertKey */#ifdef SMALL_CACHE#define MUL(x,y) (x = mul(low16(x),y))#else /* !SMALL_CACHE */#ifdef AVOID_JUMPS#define MUL(x,y) (x = low16(x&#150;1), t16 = low16((y)&#150;1), \              t32 = (word32)x*t16 &#43; x &#43; t16 &#43; 1, x = low16(t32), \              t16 = t32&gt;&gt;16, x = (x&#150;t16) &#43; (x&lt;t16) )#else /* !AVOID_JUMPS (default) */#define MUL(x,y) \       ((t16 = (y)) ? \              (x=low16(x)) ? \                     t32 = (word32)x*t16, \                     x = low16(t32), \                     t16 = t32&gt;&gt;16, \                     x = (x&#150;t16)&#43;(x&lt;t16) \              : \                     (x = 1&#150;t16) \       : \              (x = 1&#150;x))#endif#endifstatic voidideaCipher(byte *inbuf, byte *outbuf, word16 *key){       register uint16 x1, x2, x3, x4, s2, s3;       word16 *in, *out;#ifndef SMALL_CACHE       register uint16 t16; /* Temporaries needed by MUL macro */       register word32 t32;#endif       int r = IDEAROUNDS;       in = (word16 *)inbuf;       x1 = *in&#43;&#43;;  x2 = *in&#43;&#43;;       x3 = *in&#43;&#43;;  x4 = *in;#ifndef HIGHFIRST       x1 = (x1 &gt;&gt;8) | (x1&lt;&lt;8);       x2 = (x2 &gt;&gt;8) | (x2&lt;&lt;8);       x3 = (x3 &gt;&gt;8) | (x3&lt;&lt;8);       x4 = (x4 &gt;&gt;8) | (x4&lt;&lt;8);#endif       do {              MUL(x1,*key&#43;&#43;);              x2 &#43;= *key&#43;&#43;;              x3 &#43;= *key&#43;&#43;;              MUL(x4, *key&#43;&#43;);              s3 = x3;              x3 ^= x1;              MUL(x3, *key&#43;&#43;);              s2 = x2;              x2 ^= x4;              x2 &#43;= x3;              MUL(x2, *key&#43;&#43;);              x3 &#43;= x2;              x1 ^= x2;  x4 ^= x3;              x2 ^= s3;  x3 ^= s2;       } while (&#150;&#150;r);       MUL(x1, *key&#43;&#43;);       x3 &#43;= *key&#43;&#43;;       x2 &#43;= *key&#43;&#43;;       MUL(x4, *key);       out = (word16 *)outbuf;#ifdef HIGHFIRST       *out&#43;&#43; = x1;       *out&#43;&#43; = x3;       *out&#43;&#43; = x2;       *out = x4;#else /* !HIGHFIRST */       *out&#43;&#43; = (x1 &gt;&gt;8) | (x1&lt;&lt;8);       *out&#43;&#43; = (x3 &gt;&gt;8) | (x3&lt;&lt;8);       *out&#43;&#43; = (x2 &gt;&gt;8) | (x2&lt;&lt;8);       *out = (x4 &gt;&gt;8) | (x4&lt;&lt;8);#endif} /* ideaCipher */void idea_key(idea_ctx *c, unsigned char *key){       ideaExpandKey(key,c&#150;&gt;ek);       ideaInvertKey(c&#150;&gt;ek,c&#150;&gt;dk);}void idea_enc(idea_ctx *c, unsigned char *data, int blocks){       int i;       unsigned char *d = data;       for(i=0;i&lt;blocks;i&#43;&#43;){              ideaCipher(d,d,c&#150;&gt;ek);              d&#43;=8;       }}void idea_dec(idea_ctx *c, unsigned char *data, int blocks){       int i;       unsigned char *d = data;       for(i=0;i&lt;blocks;i&#43;&#43;){              ideaCipher(d,d,c&#150;&gt;dk);              d&#43;=8;       }}#include &lt;stdio.h&gt;#ifndef BLOCKS#ifndef KBYTES#define KBYTES 1024#endif#define BLOCKS (64*KBYTES)#endifintmain(void){      /* Test driver for IDEA cipher */       int i, j, k;       idea_ctx c;       byte userkey[16];       word16 EK[IDEAKEYLEN], DK[IDEAKEYLEN];       byte XX[8], YY[8], ZZ[8];       word32 long_block[10]; /* 5 blocks */       long l;       char *lbp;       /* Make a sample user key for testing... */       for(i=0; i&lt;16; i&#43;&#43;)              userkey[i] = i&#43;1;       idea_key(&ampc,userkey);       /* Make a sample plaintext pattern for testing... */       for (k=0; k&lt;8; k&#43;&#43;)              XX[k] = k;       idea_enc(&ampc,XX,1); /* encrypt */       lbp = (unsigned char *) long_block;       for(i=0;i&lt;10;i&#43;&#43;) long_block[i] = i;       idea_enc(&ampc,lbp,5);       for(i=0;i&lt;10;i&#43;=2) printf(&#147;Block %01d = %08lx %08lx.\n&#148;,                               i/2,long_block[i],long_block[i&#43;1]);       idea_dec(&ampc,lbp,3);       idea_dec(&ampc,lbp&#43;24,2);       for(i=0;i&lt;10;i&#43;=2) printf(&#147;Block %01d = %08lx %08lx.\n&#148;,                               i/2,long_block[i],long_block[i&#43;1]);       return 0;       /* normal exit */} /* main */</PRE><!-- END CODE //--><H3><A NAME="Heading4"></A><FONT COLOR="#000077">GOST</FONT></H3><!-- CODE //--><PRE>typedef unsigned long u4;typedef unsigned char byte;typedef struct {        u4 k[8];        /* Constant s&#150;boxes &#150;&#150; set up in gost_init(). */        char k87[256],k65[256],k43[256],k21[256];} gost_ctx;/* Note:  encrypt and decrypt expect full blocks&#150;&#150;padding blocks is          caller&#146;s responsibility.  All bulk encryption is done in          ECB mode by these calls.  Other modes may be added easily          enough.                                                    */void gost_enc(gost_ctx *, u4 *, int);void gost_dec(gost_ctx *, u4 *, int);void gost_key(gost_ctx *, u4 *);void gost_init(gost_ctx *);void gost_destroy(gost_ctx *);#ifdef __alpha  /* Any other 64&#150;bit machines? */typedef unsigned int word32;#elsetypedef unsigned long word32;#endifkboxinit(gost_ctx *c){        int i;        byte k8[16] = {14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6,                       12,  5,  9,  0,  7 };        byte k7[16] = {15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2,                       13, 12,  0,  5, 10 };        byte k6[16] = {10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,                        7, 11,  4,  2,  8 };        byte k5[16] = { 7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,                        5, 11, 12,  4, 15 };        byte k4[16] = { 2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3,

⌨️ 快捷键说明

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