Исходные тексты на языке C алгоритмов шифрования DES, ГОСТ, IDEA, Blowfish (1014280), страница 4
Текст из файла (страница 4)
*/for(i=0;i<256;i++){c->S[0][i] = ks0[i];c->S[1][i] = ks1[i];c->S[2][i] = ks2[i];c->S[3][i] = ks3[i];}j = 0;for (i = 0; i < N + 2; ++i) {data = 0x00000000;for (k = 0; k < 4; ++k) {0x4fa33742,0xc700c47b,0xbc946e79,0x4cd04dc6,0x6a2d519a,0x9cf2d0a4,0xa73a3ae1,0x77fa0a59,0x9e34d797,0x7c7d2d28,0xe019a5e6,0x79132e28,0x88f46dba,0xa93a072a,0xb155fdf5,0xccad925f,0xfb3e7bce,0x6413e680,0x6445c0dd,0x6bb4e3bb,0xfa6484bb,0xf64e6370,0x4eb4e2cc,0xce6ea048,0xe7933fdc,0xa01fbac9,0xd44fbd9a,0xe0b12b4f,0x15e6fc2a,0x12baa8d1,0x1698db3b,0x8971f21e,0x9b992f2e,0xcd3e7e6f,0xa6327623,0x88d273cc,0x327a140a,0x35bdd2f6,0x1640e3d3,0x20756060,0x02fb8a8c,0xc208e69f,data = (data << 8) | key[j];j = j + 1;if (j >= keybytes) {j = 0;}}c->P[i] = c->P[i] ^ data;}datal = 0x00000000;datar = 0x00000000;for (i = 0; i < N + 2; i += 2) {Blowfish_encipher(c,&datal, &datar);}c->P[i] = datal;c->P[i + 1] = datar;for (i = 0; i < 4; ++i) {for (j = 0; j < 256; j += 2) {Blowfish_encipher(c,&datal, &datar);}}}c->S[i][j] = datal;c->S[i][j + 1] = datar;void blf_key(blf_ctx *c, char *k, int len){InitializeBlowfish(c,k,len);}void blf_enc(blf_ctx *c, unsigned long *data, int blocks){unsigned long *d;int i;}d = data;for(i=0;i<blocks;i++){Blowfish_encipher(c,d,d+1);d += 2;}void blf_dec(blf_ctx *c, unsigned long *data, int blocks){unsigned long *d;int i;}d = data;for(i=0;i<blocks;i++){Blowfish_decipher(c,d,d+1);d += 2;}void main(void){blf_ctx c;char key[]="AAAAA";unsigned long data[10];int i;for(i=0;i<10;i++) data[i] = i;}blf_key(&c,key,5);blf_enc(&c,data,5);blf_dec(&c,data,1);blf_dec(&c,data+2,4);for(i=0;i<10;i+=2) printf("Block %01d decrypts to: %08lx %08lx.\n",i/2,data[i],data[i+1]);3-WAY#define#define#defineSTRT_ESTRT_DNMBR0x0b0b /* round constant of first encryption round */0xb1b1 /* round constant of first decryption round */11 /* number of rounds is 11*/typedefunsigned long int word32 ;/* the program only works correctly if long = 32bits */typedef unsigned long u4;typedef unsigned char u1;typedef struct {u4 k[3],ki[3], ercon[NMBR+1],drcon[NMBR+1];} twy_ctx;/* Note:encrypt and decrypt expect full blocks--padding blocks iscaller's responsibility.
All bulk encryption is done inECB mode by these calls. Other modes may be added easilyenough.*//* destroy: Context. *//* Scrub context of all sensitive data. */void twy_destroy(twy_ctx *);/* encrypt: Context, ptr to data block, # of blocks. */void twy_enc(twy_ctx *, u4 *, int);/* decrypt: Context, ptr to data block, # of blocks. */void twy_dec(twy_ctx *, u4 *, int);/* key: Context, ptr to key data. */void twy_key(twy_ctx *, u4 *);/* ACCODE----------------------------------------------------------- *//* End of AC code prototypes and structures.*//* ----------------------------------------------------------------- */void mu(word32 *a){int i ;word32 b[3] ;/* inverts the order of the bits of a */b[0] = b[1] = b[2] = 0 ;for( i=0 ; i<32 ; i++ ){b[0] <<= 1 ; b[1] <<= 1 ; b[2] <<= 1 ;if(a[0]&1) b[2] |= 1 ;if(a[1]&1) b[1] |= 1 ;if(a[2]&1) b[0] |= 1 ;a[0] >>= 1 ; a[1] >>= 1 ; a[2] >>= 1 ;}a[0] = b[0] ;}a[1] = b[1] ;void gamma(word32 *a){word32 b[3] ;a[2] = b[2] ;/* the nonlinear step */b[0] = a[0] ^ (a[1]|(~a[2])) ;b[1] = a[1] ^ (a[2]|(~a[0])) ;b[2] = a[2] ^ (a[0]|(~a[1])) ;a[0] = b[0] ;}a[1] = b[1] ;void theta(word32 *a){word32 b[3];b[0] = a[0] ^b[1] = a[1] ^b[2] = a[2] ^a[0] = b[0] ;}a[2] = b[2] ;/* the linear step */(a[0]>>16)(a[1]>>24)(a[2]>>16)(a[1]>>16)(a[2]>>24)(a[0]>>16)(a[2]>>16)(a[0]>>24)(a[1]>>16)^^^^^^^^^(a[1]<<16)(a[2]<<8)(a[0]<<16)(a[2]<<16)(a[0]<<8)(a[1]<<16)(a[0]<<16)(a[1]<<8)(a[2]<<16)a[1] = b[1] ;^^^^^^^^^(a[1]>>16)(a[2]>>8)(a[2]>>24)(a[2]>>16)(a[0]>>8)(a[0]>>24)(a[0]>>16)(a[1]>>8)(a[1]>>24)^^^^^^^^^(a[2]<<16)(a[0]<<24)(a[0]<<8)(a[0]<<16)(a[1]<<24)(a[1]<<8)(a[1]<<16)(a[2]<<24)(a[2]<<8)a[2] = b[2] ;void pi_1(word32 *a){a[0] = (a[0]>>10) ^ (a[0]<<22);a[2] = (a[2]<<1) ^ (a[2]>>31);}void pi_2(word32 *a){a[0] = (a[0]<<1) ^ (a[0]>>31);a[2] = (a[2]>>10) ^ (a[2]<<22);}void rho(word32 *a){theta(a) ;pi_1(a) ;gamma(a) ;pi_2(a) ;}/* the round function*/void rndcon_gen(word32 strt,word32 *rtab){/* generates the round constants */int i ;for(i=0 ; i<=NMBR ; i++ )^^;^^;^^;}{rtab[i] = strt ;strt <<= 1 ;if( strt&0x10000 ) strt ^= 0x11011 ;}/* Modified slightly to fit the caller's needs.
*/void encrypt(twy_ctx *c, word32 *a){char i ;for( i=0 ; i<NMBR ; i++ ){a[0] ^= c->k[0] ^ (c->ercon[i]<<16) ;a[1] ^= c->k[1] ;a[2] ^= c->k[2] ^ c->ercon[i] ;rho(a) ;}a[0] ^= c->k[0] ^ (c->ercon[NMBR]<<16) ;a[1] ^= c->k[1] ;a[2] ^= c->k[2] ^ c->ercon[NMBR] ;theta(a) ;}/* Modified slightly to meet caller's needs. */void decrypt(twy_ctx *c, word32 *a){char i ;mu(a) ;for( i=0 ; i<NMBR ; i++ ){a[0] ^= c->ki[0] ^ (c->drcon[i]<<16) ;a[1] ^= c->ki[1] ;a[2] ^= c->ki[2] ^ c->drcon[i] ;rho(a) ;}a[0] ^= c->ki[0] ^ (c->drcon[NMBR]<<16) ;a[1] ^= c->ki[1] ;a[2] ^= c->ki[2] ^ c->drcon[NMBR] ;theta(a) ;mu(a) ;}void twy_key(twy_ctx *c, u4 *key){c->ki[0] = c->k[0] = key[0];c->ki[1] = c->k[1] = key[1];c->ki[2] = c->k[2] = key[2];theta(c->ki);mu(c->ki);rndcon_gen(STRT_E,c->ercon);rndcon_gen(STRT_D,c->drcon);}/* Encrypt in ECB mode.
*/void twy_enc(twy_ctx *c, u4 *data, int blkcnt){u4 *d;int i;d = data;for(i=0;i<blkcnt;i++) {}}encrypt(c,d);d +=3;/* Decrypt in ECB mode. */void twy_dec(twy_ctx *c, u4 *data, int blkcnt){u4 *d;int i;}d = data;for(i=0;i<blkcnt;i++){decrypt(c,d);d+=3;}/* Scrub sensitive values from memory before deallocating. */void twy_destroy(twy_ctx *c){int i;}for(i=0;i<3;i++) c->k[i] = c->ki[i] = 0;void printvec(char *chrs, word32 *d){printf("%20s : %08lx %08lx %08lx \n",chrs,d[2],d[1],d[0]);}main(){twy_ctx gc;word32 a[9],k[3];int i;/* Test vector 1. */k[0]=k[1]=k[2]=0;a[0]=a[1]=a[2]=1;twy_key(&gc,k);printf("**********\n");printvec("KEY = ",k);printvec("PLAIN = ",a);encrypt(&gc,a);printvec("CIPHER = ",a);/* Test vector 2. */k[0]=6;k[1]=5;k[2]=4;a[0]=3;a[1]=2;a[2]=1;twy_key(&gc,k);printf("**********\n");printvec("KEY = ",k);printvec("PLAIN = ",a);encrypt(&gc,a);printvec("CIPHER = ",a);/* Test vector 3.
*/k[2]=0xbcdef012;k[1]=0x456789ab;k[0]=0xdef01234;a[2]=0x01234567;a[1]=0x9abcdef0;a[0]=0x23456789;twy_key(&gc,k);printf("**********\n");printvec("KEY = ",k);printvec("PLAIN = ",a);encrypt(&gc,a);printvec("CIPHER = ",a);/* Test vector 4. */k[2]=0xcab920cd;k[1]=0xd6144138;k[0]=0xd2f05b5e;a[2]=0xad21ecf7;a[1]=0x83ae9dc4;a[0]=0x4059c76e;twy_key(&gc,k);printf("**********\n");printvec("KEY = ",k);printvec("PLAIN = ",a);encrypt(&gc,a);printvec("CIPHER = ",a);/*TEST VALUESkey: 00000000 00000000 00000000plaintext : 00000001 00000001 00000001ciphertext : ad21ecf7 83ae9dc4 4059c76ekey: 00000004 00000005 00000006plaintext : 00000001 00000002 00000003ciphertext : cab920cd d6144138 d2f05b5ekey: bcdef012 456789ab def01234plaintext : 01234567 9abcdef0 23456789ciphertext : 7cdb76b2 9cdddb6d 0aa55dbbkey: cab920cd d6144138 d2f05b5eplaintext : ad21ecf7 83ae9dc4 4059c76eciphertext : 15b155ed 6b13f17c 478ea871*//* Enc/dec test: */for(i=0;i<9;i++) a[i]=i;twy_enc(&gc,a,3);for(i=0;i<9;i+=3) printf("Block %01d encrypts to %08lx %08lx %08lx\n",i/3,a[i],a[i+1],a[i+2]);twy_dec(&gc,a,2);twy_dec(&gc,a+6,1);for(i=0;i<9;i+=3) printf("Block %01d decrypts to %08lx %08lx %08lx\n",i/3,a[i],a[i+1],a[i+2]);}RC5#include <stdio.h>/* An RC5 context needs to know how many rounds it has, and its subkeys.
*/typedef struct {u4 *xk;int nr;} rc5_ctx;/* Where possible, these should be replaced with actual rotate instructions.For Turbo C++, this is done with _lrotl and _lrotr. */#define ROTL32(X,C) (((X)<<(C))|((X)>>(32-(C))))#define ROTR32(X,C) (((X)>>(C))|((X)<<(32-(C))))/* Function prototypes for dealing with RC5 basic operations. */void rc5_init(rc5_ctx *, int);void rc5_destroy(rc5_ctx *);void rc5_key(rc5_ctx *, u1 *, int);void rc5_encrypt(rc5_ctx *, u4 *, int);void rc5_decrypt(rc5_ctx *, u4 *, int);/* Function implementations for RC5. *//* Scrub out all sensitive values. */void rc5_destroy(rc5_ctx *c){int i;for(i=0;i<(c->nr)*2+2;i++) c->xk[i]=0;free(c->xk);}/* Allocate memory for rc5 context's xk and such.