void CAST_cfb64_encrypt(const unsigned char *in, unsigned char *out, long length, const CAST_KEY *schedule, unsigned char *ivec, int *num, int enc) { register CAST_LONG v0, v1, t; register int n = *num; register long l = length; CAST_LONG ti[2]; unsigned char *iv, c, cc; iv = ivec; if (enc) { while (l--) { if (n == 0) { n2l(iv, v0); ti[0] = v0; n2l(iv, v1); ti[1] = v1; CAST_encrypt((CAST_LONG *)ti, schedule); iv = ivec; t = ti[0]; l2n(t, iv); t = ti[1]; l2n(t, iv); iv = 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; CAST_encrypt((CAST_LONG *)ti, schedule); iv = ivec; t = ti[0]; l2n(t, iv); t = ti[1]; l2n(t, iv); iv = 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 CAST_ecb_encrypt(const uint8_t *in, uint8_t *out, const CAST_KEY *ks, int enc) { uint32_t d[2]; n2l(in, d[0]); n2l(in, d[1]); if (enc) { CAST_encrypt(d, ks); } else { CAST_decrypt(d, ks); } l2n(d[0], out); l2n(d[1], out); }
void CAST_ecb_encrypt(const unsigned char *in, unsigned char *out, CAST_KEY *ks, int enc) { CAST_LONG l,d[2]; n2l(in,l); d[0]=l; n2l(in,l); d[1]=l; if (enc) CAST_encrypt(d,ks); else CAST_decrypt(d,ks); l=d[0]; l2n(l,out); l=d[1]; l2n(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 CAST_ofb64_encrypt(const unsigned char *in, unsigned char *out, long length, const CAST_KEY *schedule, unsigned char *ivec, int *num) { register CAST_LONG v0,v1,t; register int n= *num; register long l=length; unsigned char d[8]; register char *dp; CAST_LONG ti[2]; unsigned char *iv; int save=0; iv=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) { CAST_encrypt((CAST_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=ivec; l2n(v0,iv); l2n(v1,iv); } t=v0=v1=ti[0]=ti[1]=0; *num=n; }
//============================================================================ // CAST-128 //============================================================================ static int ssh_cast5_ctr(EVP_CIPHER_CTX *ctx, unsigned char *dest, const unsigned char *src, unsigned int len) { struct ssh_cast5_ctr_ctx *c; unsigned int n = 0; unsigned char buf[CAST_BLOCK]; int i, j; CAST_LONG tmp[(CAST_BLOCK + 3) / 4]; unsigned char *p; if (len == 0) return (1); if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) return (0); while ((len--) > 0) { if (n == 0) { for (i = j = 0, p = c->cast5_counter; i < CAST_BLOCK; i += 4, j++) { tmp[j] = ((CAST_LONG)*p++) << 24; tmp[j] |= ((CAST_LONG)*p++) << 16; tmp[j] |= ((CAST_LONG)*p++) << 8; tmp[j] |= ((CAST_LONG)*p++); } CAST_encrypt(tmp, &c->cast5_ctx); for (i = j = 0, p = buf; i < CAST_BLOCK; i += 4, j++) { *p++ = (unsigned char)(tmp[j] >> 24); *p++ = (unsigned char)(tmp[j] >> 16); *p++ = (unsigned char)(tmp[j] >> 8); *p++ = (unsigned char)tmp[j]; } ssh_ctr_inc(c->cast5_counter, CAST_BLOCK); } *(dest++) = *(src++) ^ buf[n]; n = (n + 1) % CAST_BLOCK; } return (1); }
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, }; CAST_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"); CAST_set_key (&sch, 16, key); count = 10; do { long i; CAST_LONG data[2]; count *= 2; Time_F (START); for (i = count; i; i--) CAST_encrypt (data, &sch); d = Time_F (STOP); } while (d < 3.0); ca = count / 512; cb = count; cc = count * 8 / BUFSIZE + 1; printf ("Doing CAST_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 CAST_set_key for 10 seconds\n"); alarm (10); #endif Time_F (START); for (count = 0, run = 1; COND (ca); count += 4) { CAST_set_key (&sch, 16, key); CAST_set_key (&sch, 16, key); CAST_set_key (&sch, 16, key); CAST_set_key (&sch, 16, key); } d = Time_F (STOP); printf ("%ld cast set_key's in %.2f seconds\n", count, d); a = ((double) COUNT (ca)) / d; #ifdef SIGALRM printf ("Doing CAST_encrypt's for 10 seconds\n"); alarm (10); #else printf ("Doing CAST_encrypt %ld times\n", cb); #endif Time_F (START); for (count = 0, run = 1; COND (cb); count += 4) { CAST_LONG data[2]; CAST_encrypt (data, &sch); CAST_encrypt (data, &sch); CAST_encrypt (data, &sch); CAST_encrypt (data, &sch); } d = Time_F (STOP); printf ("%ld CAST_encrypt's in %.2f second\n", count, d); b = ((double) COUNT (cb) * 8) / d; #ifdef SIGALRM printf ("Doing CAST_cbc_encrypt on %ld byte blocks for 10 seconds\n", BUFSIZE); alarm (10); #else printf ("Doing CAST_cbc_encrypt %ld times on %ld byte blocks\n", cc, BUFSIZE); #endif Time_F (START); for (count = 0, run = 1; COND (cc); count++) CAST_cbc_encrypt (buf, buf, BUFSIZE, &sch, &(key[0]), CAST_ENCRYPT); d = Time_F (STOP); printf ("%ld CAST_cbc_encrypt's of %ld byte blocks in %.2f second\n", count, BUFSIZE, d); c = ((double) COUNT (cc) * BUFSIZE) / d; printf ("CAST set_key per sec = %12.2f (%9.3fuS)\n", a, 1.0e6 / a); printf ("CAST raw ecb bytes per sec = %12.2f (%9.3fuS)\n", b, 8.0e6 / b); printf ("CAST 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 }
int main(int argc, char **argv) { long count; static unsigned char buf[BUFSIZE]; static char key[16]={ 0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0, 0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0}; CAST_KEY sch; 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 fprintf(stderr,"To get the most accurate results, try to run this\n"); fprintf(stderr,"program when this computer is idle.\n"); #endif CAST_set_key(&sch,16,key); #ifndef SIGALRM fprintf(stderr,"First we calculate the approximate speed ...\n"); count=10; do { long i; CAST_LONG data[2]; count*=2; Time_F(START); for (i=count; i; i--) CAST_encrypt(data,&sch); 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 time_it(CAST_encrypt_normal, "CAST_encrypt_normal ", 0); time_it(CAST_encrypt_ptr, "CAST_encrypt_ptr ", 1); time_it(CAST_encrypt_ptr2, "CAST_encrypt_ptr2 ", 2); num+=3; str[0]="<nothing>"; print_it("CAST_encrypt_normal ",0); max=tm[0]; max_idx=0; str[1]="ptr "; print_it("CAST_encrypt_ptr ",1); if (max < tm[1]) { max=tm[1]; max_idx=1; } str[2]="ptr2 "; print_it("CAST_encrypt_ptr2 ",2); if (max < tm[2]) { max=tm[2]; max_idx=2; } printf("options CAST ecb/s\n"); 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<3; i++) { if (max < tm[i]) { max=tm[i]; j=i; } } if (max < 0.0) break; 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: printf("-DCAST_DEFAULT_OPTIONS\n"); break; case 1: printf("-DCAST_PTR\n"); break; case 2: printf("-DCAST_PTR2\n"); break; } exit(0); #if defined(LINT) || defined(OPENSSL_SYS_MSDOS) return(0); #endif }