0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, 0xaace1e7c, 0xd3375fec, 0xce78a399, 0x406b2a42, 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b, 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, 0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb, 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, 0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33, 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c, 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, 0x95c11548, 0xe4c66d22, 0x48c1133f, 0xc70f86dc, 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, 0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b, 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115, 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, 0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728, 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0, 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, 0x0a476341, 0x992eff74, 0x3a6f6eab, 0xf4f8fd37, 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d, 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, 0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b, 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, 0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d, 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c, 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, 0x1a6b1018, 0x11caedfa, 0x3d25bdd8, 0xe2e1c3c9, 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, 0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d, 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc, 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, 0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61, 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2, 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, 0x7aeb2661, 0x8b1ddf84, 0x846a0e79, 0x915f95e2, 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c, 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, 0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633, 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, 0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52, 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027, 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, 0xf0177a28, 0xc0f586e0, 0x006058aa, 0x30dc7d62, 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, 0x6f05e409, 0x4b7c0188,
0x39720a3d, 0x7c927c24, 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc, 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, 0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c, 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837, 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0}; unsigned long ks3[] = { 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe, 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, 0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8, 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6, 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, 0xa1fad5f0, 0x6a2d519a, 0x63ef8ce2, 0x9a86ee22, 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, 0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9, 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59, 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, 0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51, 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, 0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b, 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, 0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd, 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, 0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb, 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f, 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, 0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32, 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, 0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae, 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, 0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47, 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, 0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84, 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, 0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd, 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, 0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38, 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, 0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525, 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, 0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964, 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, 0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d, 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, 0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02, 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, 0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a, 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, 0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0, 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, 0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9, 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6}; /* Initialize s-boxes without file read. */ 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) {
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 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 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 STRT_E 0x0b0b /* round constant of first encryption round */ #define STRT_D 0xb1b1 /* round constant of first decryption round */ #define NMBR 11 /* number of rounds is 11 */ typedef unsigned 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 is caller's responsibility. All bulk encryption is done in ECB mode by these calls. Other modes may be added easily enough. */ /* 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) /* inverts the order of the bits of a */ { int i ; word32 b[3] ; 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] ; a[2] = b[2] ; } void gamma(word32 *a) /* the nonlinear step */ { word32 b[3] ; 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] ; a[2] = b[2] ; } void theta(word32 *a) /* the linear step */ { word32 b[3]; b[0] = a[0] ^ (a[0]>>16) ^ (a[1]<<16) ^ (a[1]>>16) ^ (a[2]<<16) ^ (a[1]>>24) ^ (a[2]<<8) ^ (a[2]>>8) ^ (a[0]<<24) ^ (a[2]>>16) ^ (a[0]<<16) ^ (a[2]>>24) ^ (a[0]<<8) ; b[1] = a[1] ^ (a[1]>>16) ^ (a[2]<<16) ^ (a[2]>>16) ^ (a[0]<<16) ^ (a[2]>>24) ^ (a[0]<<8) ^ (a[0]>>8) ^ (a[1]<<24) ^ (a[0]>>16) ^ (a[1]<<16) ^ (a[0]>>24) ^ (a[1]<<8) ; b[2] = a[2] ^ (a[2]>>16) ^ (a[0]<<16) ^ (a[0]>>16) ^ (a[1]<<16) ^ (a[0]>>24) ^ (a[1]<<8) ^ (a[1]>>8) ^ (a[2]<<24) ^ (a[1]>>16) ^ (a[2]<<16) ^ (a[1]>>24) ^ (a[2]<<8) ; a[0] = b[0] ; a[1] = b[1] ; 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) /* the round function */ { theta(a) ; pi_1(a) ; gamma(a) ; pi_2(a) ; } 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 { 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 { 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 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 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 VALUES key : 00000000 00000000 00000000 plaintext : 00000001 00000001 00000001 ciphertext : ad21ecf7 83ae9dc4 4059c76e key : 00000004 00000005 00000006 plaintext : 00000001 00000002 00000003 ciphertext : cab920cd d6144138 d2f05b5e key : bcdef012 456789ab def01234 plaintext : 01234567 9abcdef0 23456789 ciphertext : 7cdb76b2 9cdddb6d 0aa55dbb key : cab920cd d6144138 d2f05b5e plaintext : ad21ecf7 83ae9dc4 4059c76e ciphertext : 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 /* 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. */ 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 d[0] += c->xk[0]; d[1] += c->xk[1]; for(i=0;inr*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 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[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]; d+=2; } } 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 cp = (u1 *)pk; for(i=0;i /* Initialize xk. */ c->xk[0] = 0xb7e15163; /* P32 */ for(i=1;ixk[i] = c->xk[i-1] + 0x9e3779b9; /* Q32 */ /* TESTING */ A = B = 0; for(i=0;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 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 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]); } A5 typedef 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; 32> |