Исходные тексты на языке C алгоритмов шифрования DES, ГОСТ, IDEA, Blowfish (1014280), страница 5
Текст из файла (страница 5)
*/void rc5_init(rc5_ctx *c, int rounds){c->nr = rounds;c->xk = (u4 *) malloc(4*(rounds*2+2));}void rc5_encrypt(rc5_ctx *c, u4 *data, int blocks){u4 *d,*sk;int h,i,rc;d = data;sk = (c->xk)+2;for(h=0;h<blocks;h++){d[0] += c->xk[0];d[1] += c->xk[1];for(i=0;i<c->nr*2;i+=2){d[0] ^= d[1];rc = d[1] & 31;d[0] = ROTL32(d[0],rc);d[0] += sk[i];d[1] ^= d[0];rc = d[0] & 31;d[1] = ROTL32(d[1],rc);d[1] += sk[i+1];/*printf("Round %03d : %08lx %08lx sk= %08lx %08lx\n",i/2,d[0],d[1],sk[i],sk[i+1]);*/}d+=2;}}void rc5_decrypt(rc5_ctx *c, u4 *data, int blocks){u4 *d,*sk;int h,i,rc;d = data;sk = (c->xk)+2;for(h=0;h<blocks;h++){for(i=c->nr*2-2;i>=0;i-=2){/*printf("Round %03d: %08lx %08lx sk: %08lx %08lx\n",i/2,d[0],d[1],sk[i],sk[i+1]); */d[1] -= sk[i+1];rc = d[0] & 31;d[1] = ROTR32(d[1],rc);d[1] ^= d[0];}}d+=2;d[0] -= sk[i];rc = d[1] & 31;d[0] = ROTR32(d[0],rc);d[0] ^= d[1];}d[0] -= c->xk[0];d[1] -= c->xk[1];void rc5_key(rc5_ctx *c, u1 *key, int keylen){u4 *pk,A,B; /* padded key */int xk_len, pk_len, i, num_steps,rc;u1 *cp;xk_len = c->nr*2 + 2;pk_len = keylen/4;if((keylen%4)!=0) pk_len += 1;pk = (u4 *) malloc(pk_len * 4);if(pk==NULL) {printf("An error occurred!\n");exit(-1);}/* Initialize pk -- this should work on Intel machines, anyway....
*/for(i=0;i<pk_len;i++) pk[i]=0;cp = (u1 *)pk;for(i=0;i<keylen;i++) cp[i]=key[i];/* Initialize xk. */c->xk[0] = 0xb7e15163; /* P32 */for(i=1;i<xk_len;i++) c->xk[i] = c->xk[i-1] + 0x9e3779b9; /* Q32 *//* TESTING */A = B = 0;for(i=0;i<xk_len;i++) {A = A + c->xk[i];B = B ^ c->xk[i];}/* Expand key into xk. */if(pk_len>xk_len) num_steps = 3*pk_len;else num_steps = 3*xk_len;A = B = 0;for(i=0;i<num_steps;i++){A = c->xk[i%xk_len] = ROTL32(c->xk[i%xk_len] + A + B,3);rc = (A+B) & 31;B = pk[i%pk_len] = ROTL32(pk[i%pk_len] + A + B,rc);}/* Clobber sensitive data before deallocating memory. */for(i=0;i<pk_len;i++) pk[i] =0;free(pk);}void main(void){rc5_ctx c;u4 data[8];char key[] = "ABCDE";int i;printf("-------------------------------------------------\n");for(i=0;i<8;i++) data[i] = i;rc5_init(&c,10); /* 10 rounds */rc5_key(&c,key,5);rc5_encrypt(&c,data,4);printf("Encryptions:\n");for(i=0;i<8;i+=2) printf("Block %01d = %08lx %08lx\n",i/2,data[i],data[i+1]);rc5_decrypt(&c,data,2);rc5_decrypt(&c,data+4,2);printf("Decryptions:\n");for(i=0;i<8;i+=2) printf("Block %01d = %08lx %08lx\n",i/2,data[i],data[i+1]);}A5typedef struct {unsigned long r1,r2,r3;} a5_ctx;static int threshold(r1, r2, r3)unsigned int r1;unsigned int r2;unsigned int r3;{int total;total = (((r1 >> 9) & 0x1) == 1) +(((r2 >> 11) & 0x1) == 1) +(((r3 >> 11) & 0x1) == 1);if (total > 1)return (0);else}return (1);unsigned long clock_r1(ctl, r1)int ctl;unsigned long r1;{unsigned long feedback;}ctl ^= ((r1 >> 9) & 0x1);if (ctl){feedback = (r1 >> 18) ^ (r1 >> 17) ^ (r1 >> 16) ^ (r1 >> 13);r1 = (r1 << 1) & 0x7ffff;if (feedback & 0x01)r1 ^= 0x01;}return (r1);unsigned long clock_r2(ctl, r2)int ctl;unsigned long r2;{unsigned long feedback;}ctl ^= ((r2 >> 11) & 0x1);if (ctl){feedback = (r2 >> 21) ^ (r2 >> 20) ^ (r2 >> 16) ^ (r2 >> 12);r2 = (r2 << 1) & 0x3fffff;if (feedback & 0x01)r2 ^= 0x01;}return (r2);unsigned long clock_r3(ctl, r3)int ctl;unsigned long r3;{unsigned long feedback;}ctl ^= ((r3 >> 11) & 0x1);if (ctl){feedback = (r3 >> 22) ^ (r3 >> 21) ^ (r3 >> 18) ^ (r3 >> 17);r3 = (r3 << 1) & 0x7fffff;if (feedback & 0x01)r3 ^= 0x01;}return (r3);int keystream(key, frame, alice, bob)unsigned char *key;/* 64 bit session keyunsigned long frame; /* 22 bit frame sequence numberunsigned char *alice; /* 114 bit Alice to Bob key streamunsigned char *bob;/* 114 bit Bob to Alice key stream{unsigned long r1;/* 19 bit shift register */unsigned long r2;/* 22 bit shift register */*/*/*/*/unsigned long r3;int i;int clock_ctl;unsigned char *ptr;unsigned char byte;unsigned int bits;unsigned int bit;/*/*/*/*/*/*/*23 bit shift register */counter for loops*/xored with clock enable on each shift register */current position in keystream */byte of keystream being assembled */number of bits of keystream in byte */bit output from keystream generator *//* Initialise shift registers from session key */r1 = (key[0] | (key[1] << 8) | (key[2] << 16) ) & 0x7ffff;r2 = ((key[2] >> 3) | (key[3] << 5) | (key[4] << 13) | (key[5] << 21)) &0x3fffff;r3 = ((key[5] >> 1) | (key[6] << 7) | (key[7] << 15) ) & 0x7fffff;/* Merge frame sequence number into shift register state, by xor'ing it* into the feedback path*/for (i=0;i<22;i++){clock_ctl = threshold(r1, r2, r2);r1 = clock_r1(clock_ctl, r1);r2 = clock_r2(clock_ctl, r2);r3 = clock_r3(clock_ctl, r3);if (frame & 1){r1 ^= 1;r2 ^= 1;r3 ^= 1;}frame = frame >> 1;}/* Run shift registers for 100 clock ticks to allow frame number to* be diffused into all the bits of the shift registers*/for (i=0;i<100;i++){clock_ctl = threshold(r1, r2, r2);r1 = clock_r1(clock_ctl, r1);r2 = clock_r2(clock_ctl, r2);r3 = clock_r3(clock_ctl, r3);}/* Produce 114 bits of Alice->Bob key stream */ptr = alice;bits = 0;byte = 0;for (i=0;i<114;i++){clock_ctl = threshold(r1, r2, r2);r1 = clock_r1(clock_ctl, r1);r2 = clock_r2(clock_ctl, r2);r3 = clock_r3(clock_ctl, r3);bit = ((r1 >> 18) ^ (r2 >> 21) ^ (r3 >> 22)) & 0x01;byte = (byte << 1) | bit;bits++;if (bits{*ptr =ptr++;bits =byte =}== 8)byte;0;0;}if (bits)*ptr = byte;/* Run shift registers for another 100 bits to hide relationship between* Alice->Bob key stream and Bob->Alice key stream.*/for (i=0;i<100;i++){clock_ctl = threshold(r1, r2, r2);r1 = clock_r1(clock_ctl, r1);r2 = clock_r2(clock_ctl, r2);r3 = clock_r3(clock_ctl, r3);}/* Produce 114 bits of Bob->Alice key stream */ptr = bob;bits = 0;byte = 0;for (i=0;i<114;i++){clock_ctl = threshold(r1, r2, r2);r1 = clock_r1(clock_ctl, r1);r2 = clock_r2(clock_ctl, r2);r3 = clock_r3(clock_ctl, r3);bit = ((r1 >> 18) ^ (r2 >> 21) ^ (r3 >> 22)) & 0x01;byte = (byte << 1) | bit;bits++;if (bits == 8){*ptr = byte;ptr++;bits = 0;byte = 0;}}if (bits)*ptr = byte;return (0);}void a5_key(a5_ctx *c, char *k){c->r1 = k[0]<<11|k[1]<<3 | k[2]>>5; /* 19 */c->r2 = k[2]<<17|k[3]<<9 | k[4]<<1 | k[5]>>7; /* 22 */c->r3 = k[5]<<15|k[6]<<8 | k[7]; /* 23 */}/* Step one bit in A5, return 0 or 1 as output bit.
*/int a5_step(a5_ctx *c){int control;}controlc->r1 =c->r2 =c->r3 =return(= threshold(c->r1,c->r2,c->r3);clock_r1(control,c->r1);clock_r2(control,c->r2);clock_r3(control,c->r3);(c->r1^c->r2^c->r3)&1);/* Encrypts a buffer of len bytes. */void a5_encrypt(a5_ctx *c, char *data, int len){int i,j;char t;}for(i=0;i<len;i++){for(j=0;j<8;j++) t = t<<1 | a5_step(c);data[i]^=t;}void a5_decrypt(a5_ctx *c, char *data, int len){a5_encrypt(c,data,len);}void main(void){a5_ctx c;char data[100];char key[] = {1,2,3,4,5,6,7,8};int i,flag;for(i=0;i<100;i++) data[i] = i;a5_key(&c,key);a5_encrypt(&c,data,100);a5_key(&c,key);a5_decrypt(&c,data,1);a5_decrypt(&c,data+1,99);flag = 0;for(i=0;i<100;i++) if(data[i]!=i)flag = 1;if(flag)printf("Decrypt failed\n"); else printf("Decryptsucceeded\n");}SEAL#undef SEAL_DEBUG#define ALG_OK 0#define ALG_NOTOK 1#define WORDS_PER_SEAL_CALL 1024typedef struct {unsigned long t[520]; /* 512 rounded up to a multiple of 5 + 5*/unsigned long s[265]; /* 256 rounded up to a multiple of 5 + 5*/unsigned long r[20]; /* 16 rounded up to multiple of 5 */unsigned long counter; /* 32-bit synch value.
*/unsigned long ks_buf[WORDS_PER_SEAL_CALL];int ks_pos;} seal_ctx;#define#define#define#define#define#defineROT2(x) (((x) >> 2) |ROT9(x) (((x) >> 9) |ROT8(x) (((x) >> 8) |ROT16(x) (((x) >> 16)ROT24(x) (((x) >> 24)ROT27(x) (((x) >> 27)#define WORD(cp)#define#define#define#defineF1(x,F2(x,F3(x,F4(x,y,y,y,y,((x) << 30))((x) << 23))((x) << 24))| ((x) << 16))| ((x) << 8))| ((x) << 5))((cp[0] << 24)|(cp[1] << 16)|(cp[2] << 8)|(cp[3]))z)z)z)z)(((x) & (y)) | ((~(x)) & (z)))((x)^(y)^(z))(((x) & (y)) | ((x) & (z)) | ((y) & (z)))((x)^(y)^(z))int g(in, i, h)unsigned char *in;int i;unsigned long *h;{unsigned long h0;unsigned long h1;unsigned long h2;unsigned long h3;unsigned long h4;unsigned long a;unsigned long b;unsigned long c;unsigned long d;unsigned long e;unsigned char *kp;unsigned long w[80];unsigned long temp;kph0h1h2h3h4======in;WORD(kp);WORD(kp);WORD(kp);WORD(kp);WORD(kp);kpkpkpkpkp+=+=+=+=+=4;4;4;4;4;w[0] = i;for (i=1;i<16;i++)w[i] = 0;for (i=16;i<80;i++)w[i] = w[i-3]^w[i-8]^w[i-14]^w[i-16];abcde=====h0;h1;h2;h3;h4;for (i=0;i<20;i++){temp = ROT27(a) + F1(b, c, d) + e + w[i] + 0x5a827999;e = d;d = c;c = ROT2(b);b = a;a = temp;}for (i=20;i<40;i++){temp = ROT27(a) + F2(b, c, d) + e + w[i] + 0x6ed9eba1;e = d;d = c;c = ROT2(b);b = a;a = temp;}for (i=40;i<60;i++){temp = ROT27(a) + F3(b, c, d) + e + w[i] + 0x8f1bbcdc;e = d;d = c;c = ROT2(b);b = a;a = temp;}for (i=60;i<80;i++){temp = ROT27(a) + F4(b, c, d) + e + w[i] + 0xca62c1d6;e = d;d = c;c = ROT2(b);b = a;a = temp;}h[0] = h0+a;h[1] = h1+b;h[2] = h2+c;h[3] = h3+d;h[4] = h4+e;}return (ALG_OK);unsigned long gamma(a, i)unsigned char *a;int i;{unsigned long h[5];}(void) g(a, i/5, h);return h[i % 5];int seal_init(seal_ctx *result, unsigned char *key){int i;unsigned long h[5];for (i=0;i<510;i+=5)g(key, i/5, &(result->t[i]));/* horrible special case for the end */g(key, 510/5, h);for (i=510;i<512;i++)result->t[i] = h[i-510];/* 0x1000 mod 5 is +1, so have horrible special case for the start */g(key, (-1+0x1000)/5, h);for (i=0;i<4;i++)result->s[i] = h[i+1];for (i=4;i<254;i+=5)}g(key, (i+0x1000)/5, &(result->s[i]));/* horrible special case for the end */g(key, (254+0x1000)/5, h);for (i=254;i<256;i++)result->s[i] = h[i-254];/* 0x2000 mod 5 is +2, so have horrible special case at the start */g(key, (-2+0x2000)/5, h);for (i=0;i<3;i++)result->r[i] = h[i+2];for (i=3;i<13;i+=5)g(key, (i+0x2000)/5, &(result->r[i]));/* horrible special case for the end */g(key, (13+0x2000)/5, h);for (i=13;i<16;i++)result->r[i] = h[i-13];return (ALG_OK);int seal(seal_ctx *key, unsigned long in, unsigned long *out){int i;int j;int l;unsigned long a;unsigned long b;unsigned long c;unsigned long d;unsigned short p;unsigned short q;unsigned long n1;unsigned long n2;unsigned long n3;unsigned long n4;unsigned long *wp;wp = out;for (l=0;l<4;l++){a = in ^ key->r[4*l];b = ROT8(in) ^ key->r[4*l+1];c = ROT16(in) ^ key->r[4*l+2];d = ROT24(in) ^ key->r[4*l+3];for (j=0;j<2;j++){p = a & 0x7fc;b += key->t[p/4];a = ROT9(a);p = b & 0x7fc;c += key->t[p/4];b = ROT9(b);p = c & 0x7fc;d += key->t[p/4];c = ROT9(c);p = d & 0x7fc;a += key->t[p/4];d = ROT9(d);}n1n2n3n4====d;b;a;c;p = a & 0x7fc;b += key->t[p/4];a = ROT9(a);p = b & 0x7fc;c += key->t[p/4];b = ROT9(b);p = c & 0x7fc;d += key->t[p/4];c = ROT9(c);p = d & 0x7fc;a += key->t[p/4];d = ROT9(d);/* This generates 64 32-bit words, or 256 bytes of keystream.