/* Until Aug 1 2003 this function did not correctly implement CFB-r, so it * will not be compatible with any encryption prior to that date. Ben. */ void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits, long length, DES_key_schedule *schedule, DES_cblock *ivec, int enc) { register DES_LONG d0,d1,v0,v1; register unsigned long l=length; register int num=numbits/8,n=(numbits+7)/8,i,rem=numbits%8; DES_LONG ti[2]; unsigned char *iv; #if _BYTE_ORDER != _LITTLE_ENDIAN unsigned char ovec[16]; #else unsigned int sh[4]; unsigned char *ovec=(unsigned char *)sh; /* I kind of count that compiler optimizes away this assertioni,*/ assert (sizeof(sh[0])==4); /* as this holds true for all, */ /* but 16-bit platforms... */ #endif if (numbits<=0 || numbits > 64) return; iv = &(*ivec)[0]; c2l(iv,v0); c2l(iv,v1); if (enc) { while (l >= (unsigned long)n) { l-=n; ti[0]=v0; ti[1]=v1; DES_encrypt1((DES_LONG *)ti,schedule,DES_ENCRYPT); c2ln(in,d0,d1,n); in+=n; d0^=ti[0]; d1^=ti[1]; l2cn(d0,d1,out,n); out+=n; /* 30-08-94 - eay - changed because l>>32 and * l<<32 are bad under gcc :-( */ if (numbits == 32) { v0=v1; v1=d0; } else if (numbits == 64) { v0=d0; v1=d1; } else { #if _BYTE_ORDER != _LITTLE_ENDIAN iv=&ovec[0]; l2c(v0,iv); l2c(v1,iv); l2c(d0,iv); l2c(d1,iv); #else sh[0]=v0, sh[1]=v1, sh[2]=d0, sh[3]=d1; #endif if (rem==0) memmove(ovec,ovec+num,8); else for(i=0 ; i < 8 ; ++i) ovec[i]=ovec[i+num]<<rem | ovec[i+num+1]>>(8-rem); #if _BYTE_ORDER == _LITTLE_ENDIAN v0=sh[0], v1=sh[1]; #else iv=&ovec[0]; c2l(iv,v0); c2l(iv,v1); #endif } } } else { while (l >= (unsigned long)n)
void DES_ncbc_encrypt(const unsigned char *in, unsigned char *out, long length, DES_key_schedule *_schedule, DES_cblock *ivec, int enc) #endif { register DES_LONG tin0,tin1; register DES_LONG tout0,tout1,xor0,xor1; register long l=length; DES_LONG tin[2]; unsigned char *iv; iv = &(*ivec)[0]; if (enc) { c2l(iv,tout0); c2l(iv,tout1); for (l-=8; l>=0; l-=8) { c2l(in,tin0); c2l(in,tin1); tin0^=tout0; tin[0]=tin0; tin1^=tout1; tin[1]=tin1; DES_encrypt1((DES_LONG *)tin,_schedule,DES_ENCRYPT); tout0=tin[0]; l2c(tout0,out); tout1=tin[1]; l2c(tout1,out); } if (l != -8) { c2ln(in,tin0,tin1,l+8); tin0^=tout0; tin[0]=tin0; tin1^=tout1; tin[1]=tin1; DES_encrypt1((DES_LONG *)tin,_schedule,DES_ENCRYPT); tout0=tin[0]; l2c(tout0,out); tout1=tin[1]; l2c(tout1,out); } #ifndef CBC_ENC_C__DONT_UPDATE_IV iv = &(*ivec)[0]; l2c(tout0,iv); l2c(tout1,iv); #endif } else { c2l(iv,xor0); c2l(iv,xor1); for (l-=8; l>=0; l-=8) { c2l(in,tin0); tin[0]=tin0; c2l(in,tin1); tin[1]=tin1; DES_encrypt1((DES_LONG *)tin,_schedule,DES_DECRYPT); 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; DES_encrypt1((DES_LONG *)tin,_schedule,DES_DECRYPT); tout0=tin[0]^xor0; tout1=tin[1]^xor1; l2cn(tout0,tout1,out,l+8); #ifndef CBC_ENC_C__DONT_UPDATE_IV xor0=tin0; xor1=tin1; #endif } #ifndef CBC_ENC_C__DONT_UPDATE_IV iv = &(*ivec)[0]; l2c(xor0,iv); l2c(xor1,iv); #endif } tin0=tin1=tout0=tout1=xor0=xor1=0; tin[0]=tin[1]=0; }
void DES_pcbc_encrypt(const unsigned char *input, unsigned char *output, long length, DES_key_schedule *schedule, DES_cblock *ivec, int enc) { register DES_LONG sin0,sin1,xor0,xor1,tout0,tout1; DES_LONG tin[2]; const unsigned char *in; unsigned char *out,*iv; in=input; out=output; iv = &(*ivec)[0]; if (enc) { c2l(iv,xor0); c2l(iv,xor1); for (; length>0; length-=8) { if (length >= 8) { c2l(in,sin0); c2l(in,sin1); } else c2ln(in,sin0,sin1,length); tin[0]=sin0^xor0; tin[1]=sin1^xor1; DES_encrypt1((DES_LONG *)tin,schedule,DES_ENCRYPT); tout0=tin[0]; tout1=tin[1]; xor0=sin0^tout0; xor1=sin1^tout1; l2c(tout0,out); l2c(tout1,out); } } else { c2l(iv,xor0); c2l(iv,xor1); for (; length>0; length-=8) { c2l(in,sin0); c2l(in,sin1); tin[0]=sin0; tin[1]=sin1; DES_encrypt1((DES_LONG *)tin,schedule,DES_DECRYPT); tout0=tin[0]^xor0; tout1=tin[1]^xor1; if (length >= 8) { l2c(tout0,out); l2c(tout1,out); } else l2cn(tout0,tout1,out,length); xor0=tout0^sin0; xor1=tout1^sin1; } } tin[0]=tin[1]=0; sin0=sin1=xor0=xor1=tout0=tout1=0; }
int main(int argc, char **argv) { long count; static unsigned char buf[BUFSIZE]; static DES_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0}; static DES_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12}; static DES_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34}; DES_key_schedule sch,sch2,sch3; double a,b,c,d,e; #ifndef SIGALRM long ca,cb,cc,cd,ce; #endif #ifndef TIMES printf("To get the most accurate results, try to run this\n"); printf("program when this computer is idle.\n"); #endif DES_set_key_unchecked(&key2,&sch2); DES_set_key_unchecked(&key3,&sch3); #ifndef SIGALRM printf("First we calculate the approximate speed ...\n"); DES_set_key_unchecked(&key,&sch); count=10; do { long i; DES_LONG data[2]; count*=2; Time_F(START); for (i=count; i; i--) DES_encrypt1(data,&sch,DES_ENCRYPT); d=Time_F(STOP); } while (d < 3.0); ca=count; cb=count*3; cc=count*3*8/BUFSIZE+1; cd=count*8/BUFSIZE+1; ce=count/20+1; printf("Doing 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 set_key for 10 seconds\n"); alarm(10); #endif Time_F(START); for (count=0,run=1; COND(ca); count++) DES_set_key_unchecked(&key,&sch); d=Time_F(STOP); printf("%ld set_key's in %.2f seconds\n",count,d); a=((double)COUNT(ca))/d; #ifdef SIGALRM printf("Doing DES_encrypt's for 10 seconds\n"); alarm(10); #else printf("Doing DES_encrypt %ld times\n",cb); #endif Time_F(START); for (count=0,run=1; COND(cb); count++) { DES_LONG data[2]; DES_encrypt1(data,&sch,DES_ENCRYPT); } d=Time_F(STOP); printf("%ld DES_encrypt's in %.2f second\n",count,d); b=((double)COUNT(cb)*8)/d; #ifdef SIGALRM printf("Doing DES_cbc_encrypt on %ld byte blocks for 10 seconds\n", BUFSIZE); alarm(10); #else printf("Doing DES_cbc_encrypt %ld times on %ld byte blocks\n",cc, BUFSIZE); #endif Time_F(START); for (count=0,run=1; COND(cc); count++) DES_ncbc_encrypt(buf,buf,BUFSIZE,&sch, &key,DES_ENCRYPT); d=Time_F(STOP); printf("%ld DES_cbc_encrypt's of %ld byte blocks in %.2f second\n", count,BUFSIZE,d); c=((double)COUNT(cc)*BUFSIZE)/d; #ifdef SIGALRM printf("Doing DES_ede_cbc_encrypt on %ld byte blocks for 10 seconds\n", BUFSIZE); alarm(10); #else printf("Doing DES_ede_cbc_encrypt %ld times on %ld byte blocks\n",cd, BUFSIZE); #endif Time_F(START); for (count=0,run=1; COND(cd); count++) DES_ede3_cbc_encrypt(buf,buf,BUFSIZE, &sch, &sch2, &sch3, &key, DES_ENCRYPT); d=Time_F(STOP); printf("%ld DES_ede_cbc_encrypt's of %ld byte blocks in %.2f second\n", count,BUFSIZE,d); d=((double)COUNT(cd)*BUFSIZE)/d; #ifdef SIGALRM printf("Doing crypt for 10 seconds\n"); alarm(10); #else printf("Doing crypt %ld times\n",ce); #endif Time_F(START); for (count=0,run=1; COND(ce); count++) crypt("testing1","ef"); e=Time_F(STOP); printf("%ld crypts in %.2f second\n",count,e); e=((double)COUNT(ce))/e; printf("set_key per sec = %12.2f (%9.3fuS)\n",a,1.0e6/a); printf("DES raw ecb bytes per sec = %12.2f (%9.3fuS)\n",b,8.0e6/b); printf("DES cbc bytes per sec = %12.2f (%9.3fuS)\n",c,8.0e6/c); printf("DES ede cbc bytes per sec = %12.2f (%9.3fuS)\n",d,8.0e6/d); printf("crypt per sec = %12.2f (%9.3fuS)\n",e,1.0e6/e); exit(0); #if defined(LINT) || defined(OPENSSL_SYS_MSDOS) return(0); #endif }
void DES_xcbc_encrypt(const unsigned char *in, unsigned char *out, long length, DES_key_schedule *schedule, DES_cblock *ivec, const_DES_cblock *inw, const_DES_cblock *outw, int enc) { register DES_LONG tin0,tin1; register DES_LONG tout0,tout1,xor0,xor1; register DES_LONG inW0,inW1,outW0,outW1; register const unsigned char *in2; register long l=length; DES_LONG tin[2]; unsigned char *iv; in2 = &(*inw)[0]; c2l(in2,inW0); c2l(in2,inW1); in2 = &(*outw)[0]; c2l(in2,outW0); c2l(in2,outW1); iv = &(*ivec)[0]; if (enc) { c2l(iv,tout0); c2l(iv,tout1); for (l-=8; l>=0; l-=8) { c2l(in,tin0); c2l(in,tin1); tin0^=tout0^inW0; tin[0]=tin0; tin1^=tout1^inW1; tin[1]=tin1; DES_encrypt1(tin,schedule,DES_ENCRYPT); tout0=tin[0]^outW0; l2c(tout0,out); tout1=tin[1]^outW1; l2c(tout1,out); } if (l != -8) { c2ln(in,tin0,tin1,l+8); tin0^=tout0^inW0; tin[0]=tin0; tin1^=tout1^inW1; tin[1]=tin1; DES_encrypt1(tin,schedule,DES_ENCRYPT); tout0=tin[0]^outW0; l2c(tout0,out); tout1=tin[1]^outW1; l2c(tout1,out); } iv = &(*ivec)[0]; l2c(tout0,iv); l2c(tout1,iv); } else { c2l(iv,xor0); c2l(iv,xor1); for (l-=8; l>0; l-=8) { c2l(in,tin0); tin[0]=tin0^outW0; c2l(in,tin1); tin[1]=tin1^outW1; DES_encrypt1(tin,schedule,DES_DECRYPT); tout0=tin[0]^xor0^inW0; tout1=tin[1]^xor1^inW1; l2c(tout0,out); l2c(tout1,out); xor0=tin0; xor1=tin1; } if (l != -8) { c2l(in,tin0); tin[0]=tin0^outW0; c2l(in,tin1); tin[1]=tin1^outW1; DES_encrypt1(tin,schedule,DES_DECRYPT); tout0=tin[0]^xor0^inW0; tout1=tin[1]^xor1^inW1; l2cn(tout0,tout1,out,l+8); xor0=tin0; xor1=tin1; } iv = &(*ivec)[0]; l2c(xor0,iv); l2c(xor1,iv); } tin0=tin1=tout0=tout1=xor0=xor1=0; inW0=inW1=outW0=outW1=0; tin[0]=tin[1]=0; }
void DES_ede3_cbcm_encrypt(const unsigned char *in, unsigned char *out, long length, DES_key_schedule *ks1, DES_key_schedule *ks2, DES_key_schedule *ks3, DES_cblock *ivec1, DES_cblock *ivec2, int enc) { register DES_LONG tin0,tin1; register DES_LONG tout0,tout1,xor0,xor1,m0,m1; register long l=length; DES_LONG tin[2]; unsigned char *iv1,*iv2; iv1 = &(*ivec1)[0]; iv2 = &(*ivec2)[0]; if (enc) { c2l(iv1,m0); c2l(iv1,m1); c2l(iv2,tout0); c2l(iv2,tout1); for (l-=8; l>=-7; l-=8) { tin[0]=m0; tin[1]=m1; DES_encrypt1(tin,ks3,1); m0=tin[0]; m1=tin[1]; if(l < 0) { c2ln(in,tin0,tin1,l+8); } else { c2l(in,tin0); c2l(in,tin1); } tin0^=tout0; tin1^=tout1; tin[0]=tin0; tin[1]=tin1; DES_encrypt1(tin,ks1,1); tin[0]^=m0; tin[1]^=m1; DES_encrypt1(tin,ks2,0); tin[0]^=m0; tin[1]^=m1; DES_encrypt1(tin,ks1,1); tout0=tin[0]; tout1=tin[1]; l2c(tout0,out); l2c(tout1,out); } iv1=&(*ivec1)[0]; l2c(m0,iv1); l2c(m1,iv1); iv2=&(*ivec2)[0]; l2c(tout0,iv2); l2c(tout1,iv2); } else { register DES_LONG t0,t1; c2l(iv1,m0); c2l(iv1,m1); c2l(iv2,xor0); c2l(iv2,xor1); for (l-=8; l>=-7; l-=8) { tin[0]=m0; tin[1]=m1; DES_encrypt1(tin,ks3,1); m0=tin[0]; m1=tin[1]; c2l(in,tin0); c2l(in,tin1); t0=tin0; t1=tin1; tin[0]=tin0; tin[1]=tin1; DES_encrypt1(tin,ks1,0); tin[0]^=m0; tin[1]^=m1; DES_encrypt1(tin,ks2,1); tin[0]^=m0; tin[1]^=m1; DES_encrypt1(tin,ks1,0); tout0=tin[0]; tout1=tin[1]; tout0^=xor0; tout1^=xor1; if(l < 0) { l2cn(tout0,tout1,out,l+8); } else { l2c(tout0,out); l2c(tout1,out); } xor0=t0; xor1=t1; } iv1=&(*ivec1)[0]; l2c(m0,iv1); l2c(m1,iv1); iv2=&(*ivec2)[0]; l2c(xor0,iv2); l2c(xor1,iv2); } 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 DES_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0}; static DES_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12}; static DES_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34}; DES_key_schedule sch,sch2,sch3; double d,tm[16],max=0; int rank[16]; char *str[16]; int max_idx=0,i,num=0,j; #ifndef SIGALARM long ca,cb,cc,cd,ce; #endif for (i=0; i<12; i++) { tm[i]=0.0; rank[i]=0; } #ifndef TIMES TINYCLR_SSL_FPRINTF(OPENSSL_TYPE__FILE_STDERR,"To get the most accurate results, try to run this\n"); TINYCLR_SSL_FPRINTF(OPENSSL_TYPE__FILE_STDERR,"program when this computer is idle.\n"); #endif DES_set_key_unchecked(&key,&sch); DES_set_key_unchecked(&key2,&sch2); DES_set_key_unchecked(&key3,&sch3); #ifndef SIGALRM TINYCLR_SSL_FPRINTF(OPENSSL_TYPE__FILE_STDERR,"First we calculate the approximate speed ...\n"); DES_set_key_unchecked(&key,sch); count=10; do { long i; unsigned long data[2]; count*=2; Time_F(START); for (i=count; i; i--) DES_encrypt1(data,&(sch[0]),DES_ENCRYPT); d=Time_F(STOP); } while (d < 3.0); ca=count; cb=count*3; cc=count*3*8/BUFSIZE+1; cd=count*8/BUFSIZE+1; ce=count/20+1; #define COND(d) (count != (d)) #define COUNT(d) (d) #else #define COND(c) (run) #define COUNT(d) (count) signal(SIGALRM,sig_done); alarm(10); #endif #ifdef PART1 time_it(des_encrypt_u4_cisc_idx, "des_encrypt_u4_cisc_idx ", 0); time_it(des_encrypt_u16_cisc_idx, "des_encrypt_u16_cisc_idx ", 1); time_it(des_encrypt_u4_risc1_idx, "des_encrypt_u4_risc1_idx ", 2); num+=3; #endif #ifdef PART2 time_it(des_encrypt_u16_risc1_idx,"des_encrypt_u16_risc1_idx", 3); time_it(des_encrypt_u4_risc2_idx, "des_encrypt_u4_risc2_idx ", 4); time_it(des_encrypt_u16_risc2_idx,"des_encrypt_u16_risc2_idx", 5); num+=3; #endif #ifdef PART3 time_it(des_encrypt_u4_cisc_ptr, "des_encrypt_u4_cisc_ptr ", 6); time_it(des_encrypt_u16_cisc_ptr, "des_encrypt_u16_cisc_ptr ", 7); time_it(des_encrypt_u4_risc1_ptr, "des_encrypt_u4_risc1_ptr ", 8); num+=3; #endif #ifdef PART4 time_it(des_encrypt_u16_risc1_ptr,"des_encrypt_u16_risc1_ptr", 9); time_it(des_encrypt_u4_risc2_ptr, "des_encrypt_u4_risc2_ptr ",10); time_it(des_encrypt_u16_risc2_ptr,"des_encrypt_u16_risc2_ptr",11); num+=3; #endif #ifdef PART1 str[0]=" 4 c i"; print_it("des_encrypt_u4_cisc_idx ",0); max=tm[0]; max_idx=0; str[1]="16 c i"; print_it("des_encrypt_u16_cisc_idx ",1); if (max < tm[1]) { max=tm[1]; max_idx=1; } str[2]=" 4 r1 i"; print_it("des_encrypt_u4_risc1_idx ",2); if (max < tm[2]) { max=tm[2]; max_idx=2; } #endif #ifdef PART2 str[3]="16 r1 i"; print_it("des_encrypt_u16_risc1_idx",3); if (max < tm[3]) { max=tm[3]; max_idx=3; } str[4]=" 4 r2 i"; print_it("des_encrypt_u4_risc2_idx ",4); if (max < tm[4]) { max=tm[4]; max_idx=4; } str[5]="16 r2 i"; print_it("des_encrypt_u16_risc2_idx",5); if (max < tm[5]) { max=tm[5]; max_idx=5; } #endif #ifdef PART3 str[6]=" 4 c p"; print_it("des_encrypt_u4_cisc_ptr ",6); if (max < tm[6]) { max=tm[6]; max_idx=6; } str[7]="16 c p"; print_it("des_encrypt_u16_cisc_ptr ",7); if (max < tm[7]) { max=tm[7]; max_idx=7; } str[8]=" 4 r1 p"; print_it("des_encrypt_u4_risc1_ptr ",8); if (max < tm[8]) { max=tm[8]; max_idx=8; } #endif #ifdef PART4 str[9]="16 r1 p"; print_it("des_encrypt_u16_risc1_ptr",9); if (max < tm[9]) { max=tm[9]; max_idx=9; } str[10]=" 4 r2 p"; print_it("des_encrypt_u4_risc2_ptr ",10); if (max < tm[10]) { max=tm[10]; max_idx=10; } str[11]="16 r2 p"; print_it("des_encrypt_u16_risc2_ptr",11); if (max < tm[11]) { max=tm[11]; max_idx=11; } #endif TINYCLR_SSL_PRINTF("options des ecb/s\n"); TINYCLR_SSL_PRINTF("%s %12.2f 100.0%%\n",str[max_idx],tm[max_idx]); d=tm[max_idx]; tm[max_idx]= -2.0; max= -1.0; for (;;) { for (i=0; i<12; i++) { if (max < tm[i]) { max=tm[i]; j=i; } } if (max < 0.0) break; TINYCLR_SSL_PRINTF("%s %12.2f %4.1f%%\n",str[j],tm[j],tm[j]/d*100.0); tm[j]= -2.0; max= -1.0; } switch (max_idx) { case 0: TINYCLR_SSL_PRINTF("-DDES_DEFAULT_OPTIONS\n"); break; case 1: TINYCLR_SSL_PRINTF("-DDES_UNROLL\n"); break; case 2: TINYCLR_SSL_PRINTF("-DDES_RISC1\n"); break; case 3: TINYCLR_SSL_PRINTF("-DDES_UNROLL -DDES_RISC1\n"); break; case 4: TINYCLR_SSL_PRINTF("-DDES_RISC2\n"); break; case 5: TINYCLR_SSL_PRINTF("-DDES_UNROLL -DDES_RISC2\n"); break; case 6: TINYCLR_SSL_PRINTF("-DDES_PTR\n"); break; case 7: TINYCLR_SSL_PRINTF("-DDES_UNROLL -DDES_PTR\n"); break; case 8: TINYCLR_SSL_PRINTF("-DDES_RISC1 -DDES_PTR\n"); break; case 9: TINYCLR_SSL_PRINTF("-DDES_UNROLL -DDES_RISC1 -DDES_PTR\n"); break; case 10: TINYCLR_SSL_PRINTF("-DDES_RISC2 -DDES_PTR\n"); break; case 11: TINYCLR_SSL_PRINTF("-DDES_UNROLL -DDES_RISC2 -DDES_PTR\n"); break; } TINYCLR_SSL_EXIT(0); #if defined(LINT) || defined(OPENSSL_SYS_MSDOS) return(0); #endif }
void _ossl_old_des_encrypt(DES_LONG *data,des_key_schedule ks, int enc) { DES_encrypt1(data, (DES_key_schedule *)ks, enc); }