void RC5_32_cfb64_encrypt(const unsigned char *in, unsigned char *out, long length, RC5_32_KEY *schedule, unsigned char *ivec, int *num, int encrypt) { register unsigned long v0,v1,t; register int n= *num; register long l=length; unsigned long ti[2]; unsigned char *iv,c,cc; iv=(unsigned char *)ivec; if (encrypt) { while (l--) { if (n == 0) { c2l(iv,v0); ti[0]=v0; c2l(iv,v1); ti[1]=v1; RC5_32_encrypt((unsigned long *)ti,schedule); iv=(unsigned char *)ivec; t=ti[0]; l2c(t,iv); t=ti[1]; l2c(t,iv); iv=(unsigned char *)ivec; } c= *(in++)^iv[n]; *(out++)=c; iv[n]=c; n=(n+1)&0x07; } } else { while (l--) { if (n == 0) { c2l(iv,v0); ti[0]=v0; c2l(iv,v1); ti[1]=v1; RC5_32_encrypt((unsigned long *)ti,schedule); iv=(unsigned char *)ivec; t=ti[0]; l2c(t,iv); t=ti[1]; l2c(t,iv); iv=(unsigned char *)ivec; } cc= *(in++); c=iv[n]; iv[n]=cc; *(out++)=c^cc; n=(n+1)&0x07; } } v0=v1=ti[0]=ti[1]=t=c=cc=0; *num=n; }
int CEncrypt::encdec_rc5(unsigned char *data, unsigned int nLen, bool enc) { if ((0==data)||(!haveKey_rc5)) return -1; unsigned int offset = 0; if(nLen >= 8) { while (offset<=nLen-8) { RC5_32_INT d[2]; memcpy(d, data+offset, sizeof(d)); if (enc) RC5_32_encrypt(d, &key_rc5); else RC5_32_decrypt(d, &key_rc5); /* if (enc) RC5_32_encrypt((RC5_32_INT *)data+offset, key_rc5); else RC5_32_decrypt((RC5_32_INT *)data+offset, key_rc5); */ memcpy(data+offset, d, sizeof(d)); offset += sizeof(d); } } return nLen-offset; }
void RC5_32_ecb_encrypt(const unsigned char *in, unsigned char *out, RC5_32_KEY *ks, int encrypt) { unsigned long l,d[2]; c2l(in,l); d[0]=l; c2l(in,l); d[1]=l; if (encrypt) RC5_32_encrypt(d,ks); else RC5_32_decrypt(d,ks); l=d[0]; l2c(l,out); l=d[1]; l2c(l,out); l=d[0]=d[1]=0; }
/* The input and output encrypted as though 64bit ofb mode is being * used. The extra state information to record how much of the * 64bit block we have used is contained in *num; */ void RC5_32_ofb64_encrypt(const unsigned char *in, unsigned char *out, long length, RC5_32_KEY *schedule, unsigned char *ivec, int *num) { unsigned long v0,v1,t; int n= *num; long l=length; unsigned char d[8]; char *dp; unsigned long ti[2]; unsigned char *iv; int save=0; iv=(unsigned char *)ivec; c2l(iv,v0); c2l(iv,v1); ti[0]=v0; ti[1]=v1; dp=(char *)d; l2c(v0,dp); l2c(v1,dp); while (l--) { if (n == 0) { RC5_32_encrypt((unsigned long *)ti,schedule); dp=(char *)d; t=ti[0]; l2c(t,dp); t=ti[1]; l2c(t,dp); save++; } *(out++)= *(in++)^d[n]; n=(n+1)&0x07; } if (save) { v0=ti[0]; v1=ti[1]; iv=(unsigned char *)ivec; l2c(v0,iv); l2c(v1,iv); } t=v0=v1=ti[0]=ti[1]=0; *num=n; }
SWORD Encrypt::encdec_rc5(BYTE *data,DWORD len,bool enc) { if(!data || !haveKey_rc5) { return -1; } DWORD offset = 0; while(offset <= len) { RC5_32_INT d[2]; memcpy(d,dataoffset,sizeof(d)); if(enc) { RC5_32_encrypt(d,&key_rc5); } else { RC5_32_decrypt(d,&key_rc5); } memcpy(dataoffset,d,sizeof(d)); offset += sizeof(d); } return len - offset; }
int main(int argc, char **argv) { long count; static unsigned char buf[BUFSIZE]; static unsigned char key[] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, }; RC5_32_KEY sch; double a, b, c, d; #ifndef SIGALRM long ca, cb, cc; #endif #ifndef TIMES printf("To get the most accurate results, try to run this\n"); printf("program when this computer is idle.\n"); #endif #ifndef SIGALRM printf("First we calculate the approximate speed ...\n"); RC5_32_set_key(&sch, 16, key, 12); count = 10; do { long i; RC5_32_INT data[2]; count *= 2; Time_F(START); for (i = count; i; i--) RC5_32_encrypt(data, &sch); d = Time_F(STOP); } while (d < 3.0); ca = count / 512; cb = count; cc = count * 8 / BUFSIZE + 1; printf("Doing RC5_32_set_key %ld times\n", ca); # define COND(d) (count != (d)) # define COUNT(d) (d) #else # define COND(c) (run) # define COUNT(d) (count) signal(SIGALRM, sig_done); printf("Doing RC5_32_set_key for 10 seconds\n"); alarm(10); #endif Time_F(START); for (count = 0, run = 1; COND(ca); count += 4) { RC5_32_set_key(&sch, 16, key, 12); RC5_32_set_key(&sch, 16, key, 12); RC5_32_set_key(&sch, 16, key, 12); RC5_32_set_key(&sch, 16, key, 12); } d = Time_F(STOP); printf("%ld RC5_32_set_key's in %.2f seconds\n", count, d); a = ((double)COUNT(ca)) / d; #ifdef SIGALRM printf("Doing RC5_32_encrypt's for 10 seconds\n"); alarm(10); #else printf("Doing RC5_32_encrypt %ld times\n", cb); #endif Time_F(START); for (count = 0, run = 1; COND(cb); count += 4) { RC5_32_INT data[2]; RC5_32_encrypt(data, &sch); RC5_32_encrypt(data, &sch); RC5_32_encrypt(data, &sch); RC5_32_encrypt(data, &sch); } d = Time_F(STOP); printf("%ld RC5_32_encrypt's in %.2f second\n", count, d); b = ((double)COUNT(cb) * 8) / d; #ifdef SIGALRM printf("Doing RC5_32_cbc_encrypt on %ld byte blocks for 10 seconds\n", BUFSIZE); alarm(10); #else printf("Doing RC5_32_cbc_encrypt %ld times on %ld byte blocks\n", cc, BUFSIZE); #endif Time_F(START); for (count = 0, run = 1; COND(cc); count++) RC5_32_cbc_encrypt(buf, buf, BUFSIZE, &sch, &(key[0]), RC5_ENCRYPT); d = Time_F(STOP); printf("%ld RC5_32_cbc_encrypt's of %ld byte blocks in %.2f second\n", count, BUFSIZE, d); c = ((double)COUNT(cc) * BUFSIZE) / d; printf("RC5_32/12/16 set_key per sec = %12.2f (%9.3fuS)\n", a, 1.0e6 / a); printf("RC5_32/12/16 raw ecb bytes per sec = %12.2f (%9.3fuS)\n", b, 8.0e6 / b); printf("RC5_32/12/16 cbc bytes per sec = %12.2f (%9.3fuS)\n", c, 8.0e6 / c); exit(0); #if defined(LINT) || defined(OPENSSL_SYS_MSDOS) return (0); #endif }
void RC5_32_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, RC5_32_KEY *ks, unsigned char *iv, int encrypt) { unsigned long tin0,tin1; unsigned long tout0,tout1,xor0,xor1; long l=length; unsigned long tin[2]; if (encrypt) { c2l(iv,tout0); c2l(iv,tout1); iv-=8; for (l-=8; l>=0; l-=8) { c2l(in,tin0); c2l(in,tin1); tin0^=tout0; tin1^=tout1; tin[0]=tin0; tin[1]=tin1; RC5_32_encrypt(tin,ks); tout0=tin[0]; l2c(tout0,out); tout1=tin[1]; l2c(tout1,out); } if (l != -8) { c2ln(in,tin0,tin1,l+8); tin0^=tout0; tin1^=tout1; tin[0]=tin0; tin[1]=tin1; RC5_32_encrypt(tin,ks); tout0=tin[0]; l2c(tout0,out); tout1=tin[1]; l2c(tout1,out); } l2c(tout0,iv); l2c(tout1,iv); } else { c2l(iv,xor0); c2l(iv,xor1); iv-=8; for (l-=8; l>=0; l-=8) { c2l(in,tin0); tin[0]=tin0; c2l(in,tin1); tin[1]=tin1; RC5_32_decrypt(tin,ks); tout0=tin[0]^xor0; tout1=tin[1]^xor1; l2c(tout0,out); l2c(tout1,out); xor0=tin0; xor1=tin1; } if (l != -8) { c2l(in,tin0); tin[0]=tin0; c2l(in,tin1); tin[1]=tin1; RC5_32_decrypt(tin,ks); tout0=tin[0]^xor0; tout1=tin[1]^xor1; l2cn(tout0,tout1,out,l+8); xor0=tin0; xor1=tin1; } l2c(xor0,iv); l2c(xor1,iv); } tin0=tin1=tout0=tout1=xor0=xor1=0; tin[0]=tin[1]=0; }