void idea_cfb64_encrypt(const unsigned char *in, unsigned char *out, long length, IDEA_KEY_SCHEDULE *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) { n2l(iv, v0); ti[0] = v0; n2l(iv, v1); ti[1] = v1; idea_encrypt((unsigned long *)ti, schedule); iv = (unsigned char *)ivec; t = ti[0]; l2n(t, iv); t = ti[1]; l2n(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) { n2l(iv, v0); ti[0] = v0; n2l(iv, v1); ti[1] = v1; idea_encrypt((unsigned long *)ti, schedule); iv = (unsigned char *)ivec; t = ti[0]; l2n(t, iv); t = ti[1]; l2n(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; }
void idea_ecb_encrypt(const unsigned char *in, unsigned char *out, IDEA_KEY_SCHEDULE *ks) { unsigned long l0,l1,d[2]; n2l(in,l0); d[0]=l0; n2l(in,l1); d[1]=l1; idea_encrypt(d,ks); l0=d[0]; l2n(l0,out); l1=d[1]; l2n(l1,out); l0=l1=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 idea_ofb64_encrypt(const unsigned char *in, unsigned char *out, long length, IDEA_KEY_SCHEDULE *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; n2l(iv,v0); n2l(iv,v1); ti[0]=v0; ti[1]=v1; dp=(char *)d; l2n(v0,dp); l2n(v1,dp); while (l--) { if (n == 0) { idea_encrypt((unsigned long *)ti,schedule); dp=(char *)d; t=ti[0]; l2n(t,dp); t=ti[1]; l2n(t,dp); save++; } *(out++)= *(in++)^d[n]; n=(n+1)&0x07; } if (save) { v0=ti[0]; v1=ti[1]; iv=(unsigned char *)ivec; l2n(v0,iv); l2n(v1,iv); } t=v0=v1=ti[0]=ti[1]=0; *num=n; }
void idea_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, IDEA_KEY_SCHEDULE *ks, unsigned char *iv, int encrypt) { register unsigned long tin0,tin1; register unsigned long tout0,tout1,xor0,xor1; register long l=length; unsigned long tin[2]; if (encrypt) { n2l(iv,tout0); n2l(iv,tout1); iv-=8; for (l-=8; l>=0; l-=8) { n2l(in,tin0); n2l(in,tin1); tin0^=tout0; tin1^=tout1; tin[0]=tin0; tin[1]=tin1; idea_encrypt(tin,ks); tout0=tin[0]; l2n(tout0,out); tout1=tin[1]; l2n(tout1,out); } if (l != -8) { n2ln(in,tin0,tin1,l+8); tin0^=tout0; tin1^=tout1; tin[0]=tin0; tin[1]=tin1; idea_encrypt(tin,ks); tout0=tin[0]; l2n(tout0,out); tout1=tin[1]; l2n(tout1,out); } l2n(tout0,iv); l2n(tout1,iv); } else { n2l(iv,xor0); n2l(iv,xor1); iv-=8; for (l-=8; l>=0; l-=8) { n2l(in,tin0); tin[0]=tin0; n2l(in,tin1); tin[1]=tin1; idea_encrypt(tin,ks); tout0=tin[0]^xor0; tout1=tin[1]^xor1; l2n(tout0,out); l2n(tout1,out); xor0=tin0; xor1=tin1; } if (l != -8) { n2l(in,tin0); tin[0]=tin0; n2l(in,tin1); tin[1]=tin1; idea_encrypt(tin,ks); tout0=tin[0]^xor0; tout1=tin[1]^xor1; l2nn(tout0,tout1,out,l+8); xor0=tin0; xor1=tin1; } l2n(xor0,iv); l2n(xor1,iv); } tin0=tin1=tout0=tout1=xor0=xor1=0; tin[0]=tin[1]=0; }
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, }; IDEA_KEY_SCHEDULE sch; double a, aa, b, c, d; #ifndef SIGALRM long ca, cca, 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"); idea_set_encrypt_key (key, &sch); count = 10; do { long i; IDEA_INT data[2]; count *= 2; Time_F (START); for (i = count; i; i--) idea_encrypt (data, &sch); d = Time_F (STOP); } while (d < 3.0); ca = count / 4; cca = count / 200; cb = count; cc = count * 8 / BUFSIZE + 1; printf ("idea_set_encrypt_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 idea_set_encrypt_key for 10 seconds\n"); alarm (10); #endif Time_F (START); for (count = 0, run = 1; COND (ca); count += 4) { idea_set_encrypt_key (key, &sch); idea_set_encrypt_key (key, &sch); idea_set_encrypt_key (key, &sch); idea_set_encrypt_key (key, &sch); } d = Time_F (STOP); printf ("%ld idea idea_set_encrypt_key's in %.2f seconds\n", count, d); a = ((double) COUNT (ca)) / d; #ifdef SIGALRM printf ("Doing idea_set_decrypt_key for 10 seconds\n"); alarm (10); #else printf ("Doing idea_set_decrypt_key %ld times\n", cca); #endif Time_F (START); for (count = 0, run = 1; COND (cca); count += 4) { idea_set_decrypt_key (&sch, &sch); idea_set_decrypt_key (&sch, &sch); idea_set_decrypt_key (&sch, &sch); idea_set_decrypt_key (&sch, &sch); } d = Time_F (STOP); printf ("%ld idea idea_set_decrypt_key's in %.2f seconds\n", count, d); aa = ((double) COUNT (cca)) / d; #ifdef SIGALRM printf ("Doing idea_encrypt's for 10 seconds\n"); alarm (10); #else printf ("Doing idea_encrypt %ld times\n", cb); #endif Time_F (START); for (count = 0, run = 1; COND (cb); count += 4) { unsigned long data[2]; idea_encrypt (data, &sch); idea_encrypt (data, &sch); idea_encrypt (data, &sch); idea_encrypt (data, &sch); } d = Time_F (STOP); printf ("%ld idea_encrypt's in %.2f second\n", count, d); b = ((double) COUNT (cb) * 8) / d; #ifdef SIGALRM printf ("Doing idea_cbc_encrypt on %ld byte blocks for 10 seconds\n", BUFSIZE); alarm (10); #else printf ("Doing idea_cbc_encrypt %ld times on %ld byte blocks\n", cc, BUFSIZE); #endif Time_F (START); for (count = 0, run = 1; COND (cc); count++) idea_cbc_encrypt (buf, buf, BUFSIZE, &sch, &(key[0]), IDEA_ENCRYPT); d = Time_F (STOP); printf ("%ld idea_cbc_encrypt's of %ld byte blocks in %.2f second\n", count, BUFSIZE, d); c = ((double) COUNT (cc) * BUFSIZE) / d; printf ("IDEA set_encrypt_key per sec = %12.2f (%9.3fuS)\n", a, 1.0e6 / a); printf ("IDEA set_decrypt_key per sec = %12.2f (%9.3fuS)\n", aa, 1.0e6 / aa); printf ("IDEA raw ecb bytes per sec = %12.2f (%9.3fuS)\n", b, 8.0e6 / b); printf ("IDEA 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 }