Ремонт принтеров, сканнеров, факсов и остальной офисной техники


назад Оглавление вперед




[193]

} /* ideaExpandKey */

static void

ideaInvertKey(word16 const *EK, word16 DK[IDEAKEYLEN])

uint16 t1, t2, t3;

word16 temp[IDEAKEYLEN];

word16 *p = temp + IDEAKEYLEN;

t1 = mulInv(*EK++);

t2 = -*EK++;

t3 = -*EK++;

*--p = mulInv(*EK++);

*-- p = t3; *-- p = t2; *-- p = t1;

for (i = 0; i < IDEAROUNDS-1;{

t1 = *EK++; *-p = *EK++;

*-- p = t1;

t1 = mulInv(*EK++);

t2 = -*EK++;

t3 = -*EK++;

*--p = mulInv(*EK++);

*-- p = t2; *-- p = t3; *-- p = t1;

t1 = *EK++; *-p = *EK++;

*-- p = t1;

t1 = mulInv(*EK++);

t2 = -*EK++;

t3 = -*EK++;

*--p = mulInv(*EK++);

*-- p = t3; *-- p = t2; *-- p = t1;

/* Copy and destroy temp copy */

memcpy(DK, temp, sizeof(temp));

for(i=0;i<IDEAKEYLEN;i++)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-1), t16 = low16((y)-1), \

t32 = (word32)x*t16 + x + t16 + 1, x = low16(t32), \ t16 = t32>>16, x = (x-t16) + (x<t16) )

#else /* !AVOID JUMPS (default) */ #define MUL(x,y) \

((t16 = (y)) ? \

(x=low16(x)) ? \

t32 = (word32)x*t16, \ x = low16(t32), \ t16 = t32>>16, \

x = (x-t16) + (x<t16) \


(x = 1-t16) \ (x = 1-x))

#endif #endif

static void

ideaCipher(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;

int r = IDEAROUNDS;

in = (word16 *)inbuf; x1 = *in++; x2 = *in++; x3 = *in++; x4 = *in;

#ifndef HIGHFIRST

x1 = (x1 >>8) (x1<<8); x2 = (x2 >>8) (x2<<8); x3 = (x3 >>8) (x3<<8); x4 = (x4 >>8) (x4<<8);

MUL(x1,*key++); x2 += *key++; x3 += *key++; MUL(x4, *key++);

s3 = x3; x3 A= x1; MUL(x3, *key++); s2 = x2;

x2 A= x4;

x2 += x3; MUL(x2, *key++); x3 += x2;

x1 A= x2; x4 A= x3;

x2 A= s3; x3 A= s2; } while (--r); MUL(x1, *key++); x3 += *key++; x2 += *key++;

MUL(x4, *key);

out = (word16 *)outbuf;

#ifdef HIGHFIRST

*out++ = x1; *out++ = x3; *out++ = x2; *out = x4;

#else /* !HIGHFIRST */

*out++ = (x1 >>8) (x1<<8); *out++ = (x3 >>8) (x3<<8); *out++ = (x2 >>8) (x2<<8);

*out = (x4 >>8) (x4<<8);


} /* ideaCipher */

void idea key(idea ctx *c, unsigned char *key){ ideaExpandKey(key,c->ek); ideaInvertKey(c->ek,c->dk);

void idea enc(idea ctx *c, unsigned char *data, int blocks){ int i;

unsigned char *d = data;

for(i=0;i<blocks;i++){

ideaCipher(d,d,c->ek); d+=8;

void idea dec(idea ctx *c, unsigned char *data, int blocks){ int i;

unsigned char *d = data;

for(i=0;i<blocks;i++){

ideaCipher(d,d,c->dk); d+=8;

#include <stdio.h>

#ifndef BLOCKS #ifndef KBYTES #define KBYTES 1024 #endif

#define BLOCKS (64*KBYTES) #endif

main(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<16;

userkey[i] =

idea key(&c,userkey);

/* Make a sample plaintext pattern for testing... */ for (k=0; k<8; k++)

XX[k] = k;

idea enc(&c,XX,1); /* encrypt */ lbp = (unsigned char *) long block;



[стр.Начало] [стр.1] [стр.2] [стр.3] [стр.4] [стр.5] [стр.6] [стр.7] [стр.8] [стр.9] [стр.10] [стр.11] [стр.12] [стр.13] [стр.14] [стр.15] [стр.16] [стр.17] [стр.18] [стр.19] [стр.20] [стр.21] [стр.22] [стр.23] [стр.24] [стр.25] [стр.26] [стр.27] [стр.28] [стр.29] [стр.30] [стр.31] [стр.32] [стр.33] [стр.34] [стр.35] [стр.36] [стр.37] [стр.38] [стр.39] [стр.40] [стр.41] [стр.42] [стр.43] [стр.44] [стр.45] [стр.46] [стр.47] [стр.48] [стр.49] [стр.50] [стр.51] [стр.52] [стр.53] [стр.54] [стр.55] [стр.56] [стр.57] [стр.58] [стр.59] [стр.60] [стр.61] [стр.62] [стр.63] [стр.64] [стр.65] [стр.66] [стр.67] [стр.68] [стр.69] [стр.70] [стр.71] [стр.72] [стр.73] [стр.74] [стр.75] [стр.76] [стр.77] [стр.78] [стр.79] [стр.80] [стр.81] [стр.82] [стр.83] [стр.84] [стр.85] [стр.86] [стр.87] [стр.88] [стр.89] [стр.90] [стр.91] [стр.92] [стр.93] [стр.94] [стр.95] [стр.96] [стр.97] [стр.98] [стр.99] [стр.100] [стр.101] [стр.102] [стр.103] [стр.104] [стр.105] [стр.106] [стр.107] [стр.108] [стр.109] [стр.110] [стр.111] [стр.112] [стр.113] [стр.114] [стр.115] [стр.116] [стр.117] [стр.118] [стр.119] [стр.120] [стр.121] [стр.122] [стр.123] [стр.124] [стр.125] [стр.126] [стр.127] [стр.128] [стр.129] [стр.130] [стр.131] [стр.132] [стр.133] [стр.134] [стр.135] [стр.136] [стр.137] [стр.138] [стр.139] [стр.140] [стр.141] [стр.142] [стр.143] [стр.144] [стр.145] [стр.146] [стр.147] [стр.148] [стр.149] [стр.150] [стр.151] [стр.152] [стр.153] [стр.154] [стр.155] [стр.156] [стр.157] [стр.158] [стр.159] [стр.160] [стр.161] [стр.162] [стр.163] [стр.164] [стр.165] [стр.166] [стр.167] [стр.168] [стр.169] [стр.170] [стр.171] [стр.172] [стр.173] [стр.174] [стр.175] [стр.176] [стр.177] [стр.178] [стр.179] [стр.180] [стр.181] [стр.182] [стр.183] [стр.184] [стр.185] [стр.186] [стр.187] [стр.188] [стр.189] [стр.190] [стр.191] [стр.192] [стр.193] [стр.194] [стр.195] [стр.196] [стр.197] [стр.198] [стр.199] [стр.200] [стр.201] [стр.202] [стр.203]