static void blowfish_decrypt(u_char *src, u_char *dst, int len, void *state) { struct blowfish_state *dstate; dstate = (struct blowfish_state *)state; swap_bytes(src, dst, len); BF_cbc_encrypt((void *)dst, dst, len, &dstate->key, dstate->iv, BF_DECRYPT); swap_bytes(dst, dst, len); }
void blowfish_encrypt(u_char *src, u_char *dst, int len, void *state) { struct blowfish_state *estate; estate = (struct blowfish_state *)state; swap_bytes(src, dst, len); BF_cbc_encrypt((void *)dst, dst, len, &estate->key, estate->iv, BF_ENCRYPT); swap_bytes(dst, dst, len); }
std::string HashUtils::blowfish(const char *data, unsigned long data_len, const char *key, unsigned long key_len, const char *ivec) { unsigned char ivec_buffer[8]; memset(ivec_buffer, 0, 8); memcpy(ivec_buffer, ivec, std::min(strlen(ivec), (size_t)8)); BF_KEY bfkey; BF_set_key(&bfkey, key_len, (unsigned char *)key); size_t padded_datasize = (data_len % 8) ? 8*(data_len/8 + 1) : data_len; unsigned char buffer[padded_datasize]; if (padded_datasize > data_len) { ByteArrayType input; input.reserve(padded_datasize); input.insert(input.end(), data, data + data_len); input.insert(input.end(), padded_datasize - data_len, '\0'); BF_cbc_encrypt((unsigned char *)&*input.begin(), (unsigned char *)buffer, padded_datasize, &bfkey, (unsigned char *)ivec_buffer, BF_ENCRYPT); } else { BF_cbc_encrypt((unsigned char *)data, (unsigned char *)buffer, padded_datasize, &bfkey, (unsigned char *)ivec_buffer, BF_ENCRYPT); } return std::string(buffer, buffer + padded_datasize); }
int OS_BF_Str(char *input, char *output, char *charkey, long size, short int action) { BF_KEY key; static unsigned char cbc_iv [8]={0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10}; unsigned char iv[8]; memcpy(iv,cbc_iv,sizeof(iv)); BF_set_key(&key, strlen(charkey), (uchar *)charkey); BF_cbc_encrypt((uchar *)input, (uchar *)output, size, &key, iv, action); return(1); }
static void blowfish_decrypt(struct crypto_struct *cipher, void *in, void *out, unsigned long len, void *IV) { BF_cbc_encrypt(in, out, len, cipher->key, IV, BF_DECRYPT); }
static int test(void) { unsigned char cbc_in[40],cbc_out[40],iv[8]; int i,n,err=0; BF_KEY key; BF_LONG data[2]; unsigned char out[8]; BF_LONG len; #ifdef CHARSET_EBCDIC ebcdic2ascii(cbc_data, cbc_data, TINYCLR_SSL_STRLEN(cbc_data)); #endif TINYCLR_SSL_PRINTF("testing blowfish in raw ecb mode\n"); for (n=0; n<2; n++) { #ifdef CHARSET_EBCDIC ebcdic2ascii(bf_key[n], bf_key[n], TINYCLR_SSL_STRLEN(bf_key[n])); #endif BF_set_key(&key,TINYCLR_SSL_STRLEN(bf_key[n]),(unsigned char *)bf_key[n]); data[0]=bf_plain[n][0]; data[1]=bf_plain[n][1]; BF_encrypt(data,&key); if (TINYCLR_SSL_MEMCMP(&(bf_cipher[n][0]),&(data[0]),8) != 0) { TINYCLR_SSL_PRINTF("BF_encrypt error encrypting\n"); TINYCLR_SSL_PRINTF("got :"); for (i=0; i<2; i++) TINYCLR_SSL_PRINTF("%08lX ",(unsigned long)data[i]); TINYCLR_SSL_PRINTF("\n"); TINYCLR_SSL_PRINTF("expected:"); for (i=0; i<2; i++) TINYCLR_SSL_PRINTF("%08lX ",(unsigned long)bf_cipher[n][i]); err=1; TINYCLR_SSL_PRINTF("\n"); } BF_decrypt(&(data[0]),&key); if (TINYCLR_SSL_MEMCMP(&(bf_plain[n][0]),&(data[0]),8) != 0) { TINYCLR_SSL_PRINTF("BF_encrypt error decrypting\n"); TINYCLR_SSL_PRINTF("got :"); for (i=0; i<2; i++) TINYCLR_SSL_PRINTF("%08lX ",(unsigned long)data[i]); TINYCLR_SSL_PRINTF("\n"); TINYCLR_SSL_PRINTF("expected:"); for (i=0; i<2; i++) TINYCLR_SSL_PRINTF("%08lX ",(unsigned long)bf_plain[n][i]); TINYCLR_SSL_PRINTF("\n"); err=1; } } TINYCLR_SSL_PRINTF("testing blowfish in ecb mode\n"); for (n=0; n<NUM_TESTS; n++) { BF_set_key(&key,8,ecb_data[n]); BF_ecb_encrypt(&(plain_data[n][0]),out,&key,BF_ENCRYPT); if (TINYCLR_SSL_MEMCMP(&(cipher_data[n][0]),out,8) != 0) { TINYCLR_SSL_PRINTF("BF_ecb_encrypt blowfish error encrypting\n"); TINYCLR_SSL_PRINTF("got :"); for (i=0; i<8; i++) TINYCLR_SSL_PRINTF("%02X ",out[i]); TINYCLR_SSL_PRINTF("\n"); TINYCLR_SSL_PRINTF("expected:"); for (i=0; i<8; i++) TINYCLR_SSL_PRINTF("%02X ",cipher_data[n][i]); err=1; TINYCLR_SSL_PRINTF("\n"); } BF_ecb_encrypt(out,out,&key,BF_DECRYPT); if (TINYCLR_SSL_MEMCMP(&(plain_data[n][0]),out,8) != 0) { TINYCLR_SSL_PRINTF("BF_ecb_encrypt error decrypting\n"); TINYCLR_SSL_PRINTF("got :"); for (i=0; i<8; i++) TINYCLR_SSL_PRINTF("%02X ",out[i]); TINYCLR_SSL_PRINTF("\n"); TINYCLR_SSL_PRINTF("expected:"); for (i=0; i<8; i++) TINYCLR_SSL_PRINTF("%02X ",plain_data[n][i]); TINYCLR_SSL_PRINTF("\n"); err=1; } } TINYCLR_SSL_PRINTF("testing blowfish set_key\n"); for (n=1; n<KEY_TEST_NUM; n++) { BF_set_key(&key,n,key_test); BF_ecb_encrypt(key_data,out,&key,BF_ENCRYPT); /* mips-sgi-irix6.5-gcc vv -mabi=64 bug workaround */ if (TINYCLR_SSL_MEMCMP(out,&(key_out[i=n-1][0]),8) != 0) { TINYCLR_SSL_PRINTF("blowfish setkey error\n"); err=1; } } TINYCLR_SSL_PRINTF("testing blowfish in cbc mode\n"); len=TINYCLR_SSL_STRLEN(cbc_data)+1; BF_set_key(&key,16,cbc_key); TINYCLR_SSL_MEMSET(cbc_in,0,sizeof cbc_in); TINYCLR_SSL_MEMSET(cbc_out,0,sizeof cbc_out); TINYCLR_SSL_MEMCPY(iv,cbc_iv,sizeof iv); BF_cbc_encrypt((unsigned char *)cbc_data,cbc_out,len, &key,iv,BF_ENCRYPT); if (TINYCLR_SSL_MEMCMP(cbc_out,cbc_ok,32) != 0) { err=1; TINYCLR_SSL_PRINTF("BF_cbc_encrypt encrypt error\n"); for (i=0; i<32; i++) TINYCLR_SSL_PRINTF("0x%02X,",cbc_out[i]); } TINYCLR_SSL_MEMCPY(iv,cbc_iv,8); BF_cbc_encrypt(cbc_out,cbc_in,len, &key,iv,BF_DECRYPT); if (TINYCLR_SSL_MEMCMP(cbc_in,cbc_data,TINYCLR_SSL_STRLEN(cbc_data)+1) != 0) { TINYCLR_SSL_PRINTF("BF_cbc_encrypt decrypt error\n"); err=1; } TINYCLR_SSL_PRINTF("testing blowfish in cfb64 mode\n"); BF_set_key(&key,16,cbc_key); TINYCLR_SSL_MEMSET(cbc_in,0,40); TINYCLR_SSL_MEMSET(cbc_out,0,40); TINYCLR_SSL_MEMCPY(iv,cbc_iv,8); n=0; BF_cfb64_encrypt((unsigned char *)cbc_data,cbc_out,(long)13, &key,iv,&n,BF_ENCRYPT); BF_cfb64_encrypt((unsigned char *)&(cbc_data[13]),&(cbc_out[13]),len-13, &key,iv,&n,BF_ENCRYPT); if (TINYCLR_SSL_MEMCMP(cbc_out,cfb64_ok,(int)len) != 0) { err=1; TINYCLR_SSL_PRINTF("BF_cfb64_encrypt encrypt error\n"); for (i=0; i<(int)len; i++) TINYCLR_SSL_PRINTF("0x%02X,",cbc_out[i]); } n=0; TINYCLR_SSL_MEMCPY(iv,cbc_iv,8); BF_cfb64_encrypt(cbc_out,cbc_in,17, &key,iv,&n,BF_DECRYPT); BF_cfb64_encrypt(&(cbc_out[17]),&(cbc_in[17]),len-17, &key,iv,&n,BF_DECRYPT); if (TINYCLR_SSL_MEMCMP(cbc_in,cbc_data,(int)len) != 0) { TINYCLR_SSL_PRINTF("BF_cfb64_encrypt decrypt error\n"); err=1; } TINYCLR_SSL_PRINTF("testing blowfish in ofb64\n"); BF_set_key(&key,16,cbc_key); TINYCLR_SSL_MEMSET(cbc_in,0,40); TINYCLR_SSL_MEMSET(cbc_out,0,40); TINYCLR_SSL_MEMCPY(iv,cbc_iv,8); n=0; BF_ofb64_encrypt((unsigned char *)cbc_data,cbc_out,(long)13,&key,iv,&n); BF_ofb64_encrypt((unsigned char *)&(cbc_data[13]), &(cbc_out[13]),len-13,&key,iv,&n); if (TINYCLR_SSL_MEMCMP(cbc_out,ofb64_ok,(int)len) != 0) { err=1; TINYCLR_SSL_PRINTF("BF_ofb64_encrypt encrypt error\n"); for (i=0; i<(int)len; i++) TINYCLR_SSL_PRINTF("0x%02X,",cbc_out[i]); } n=0; TINYCLR_SSL_MEMCPY(iv,cbc_iv,8); BF_ofb64_encrypt(cbc_out,cbc_in,17,&key,iv,&n); BF_ofb64_encrypt(&(cbc_out[17]),&(cbc_in[17]),len-17,&key,iv,&n); if (TINYCLR_SSL_MEMCMP(cbc_in,cbc_data,(int)len) != 0) { TINYCLR_SSL_PRINTF("BF_ofb64_encrypt decrypt error\n"); err=1; } return(err); }
static void blowfish_encrypt(struct ssh_cipher_struct *cipher, void *in, void *out, unsigned long len) { BF_cbc_encrypt(in, out, len, cipher->key, cipher->IV, BF_ENCRYPT); }
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, }; BF_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"); BF_set_key(&sch,16,key); count=10; do { long i; BF_LONG data[2]; count*=2; Time_F(START); for (i=count; i; i--) BF_encrypt(data,&sch); d=Time_F(STOP); } while (d < 3.0); ca=count/512; cb=count; cc=count*8/BUFSIZE+1; printf("Doing BF_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 BF_set_key for 10 seconds\n"); alarm(10); #endif Time_F(START); for (count=0,run=1; COND(ca); count+=4) { BF_set_key(&sch,16,key); BF_set_key(&sch,16,key); BF_set_key(&sch,16,key); BF_set_key(&sch,16,key); } d=Time_F(STOP); printf("%ld BF_set_key's in %.2f seconds\n",count,d); a=((double)COUNT(ca))/d; #ifdef SIGALRM printf("Doing BF_encrypt's for 10 seconds\n"); alarm(10); #else printf("Doing BF_encrypt %ld times\n",cb); #endif Time_F(START); for (count=0,run=1; COND(cb); count+=4) { BF_LONG data[2]; BF_encrypt(data,&sch); BF_encrypt(data,&sch); BF_encrypt(data,&sch); BF_encrypt(data,&sch); } d=Time_F(STOP); printf("%ld BF_encrypt's in %.2f second\n",count,d); b=((double)COUNT(cb)*8)/d; #ifdef SIGALRM printf("Doing BF_cbc_encrypt on %ld byte blocks for 10 seconds\n", BUFSIZE); alarm(10); #else printf("Doing BF_cbc_encrypt %ld times on %ld byte blocks\n",cc, BUFSIZE); #endif Time_F(START); for (count=0,run=1; COND(cc); count++) BF_cbc_encrypt(buf,buf,BUFSIZE,&sch, &(key[0]),BF_ENCRYPT); d=Time_F(STOP); printf("%ld BF_cbc_encrypt's of %ld byte blocks in %.2f second\n", count,BUFSIZE,d); c=((double)COUNT(cc)*BUFSIZE)/d; printf("Blowfish set_key per sec = %12.3f (%9.3fuS)\n",a,1.0e6/a); printf("Blowfish raw ecb bytes per sec = %12.3f (%9.3fuS)\n",b,8.0e6/b); printf("Blowfish cbc bytes per sec = %12.3f (%9.3fuS)\n",c,8.0e6/c); exit(0); #if defined(LINT) || defined(OPENSSL_SYS_MSDOS) return(0); #endif }
static int test(void) { unsigned char cbc_in[40],cbc_out[40],iv[8]; int i,n,err=0; BF_KEY key; BF_LONG data[2]; unsigned char out[8]; BF_LONG len; printf("testing blowfish in raw ecb mode\n"); for (n=0; n<2; n++) { BF_set_key(&key,strlen(bf_key[n]),(unsigned char *)bf_key[n]); data[0]=bf_plain[n][0]; data[1]=bf_plain[n][1]; BF_encrypt(data,&key); if (memcmp(&(bf_cipher[n][0]),&(data[0]), sizeof data) != 0) { printf("BF_encrypt error encrypting\n"); printf("got :"); for (i=0; i<2; i++) printf("%08lX ",(unsigned long)data[i]); printf("\n"); printf("expected:"); for (i=0; i<2; i++) printf("%08lX ",(unsigned long)bf_cipher[n][i]); err=1; printf("\n"); } BF_decrypt(&(data[0]),&key); if (memcmp(&(bf_plain[n][0]),&(data[0]),8) != 0) { printf("BF_encrypt error decrypting\n"); printf("got :"); for (i=0; i<2; i++) printf("%08lX ",(unsigned long)data[i]); printf("\n"); printf("expected:"); for (i=0; i<2; i++) printf("%08lX ",(unsigned long)bf_plain[n][i]); printf("\n"); err=1; } } printf("testing blowfish in ecb mode\n"); for (n=0; n<NUM_TESTS; n++) { BF_set_key(&key,8,ecb_data[n]); BF_ecb_encrypt(&(plain_data[n][0]),out,&key,BF_ENCRYPT); if (memcmp(&(cipher_data[n][0]),out,8) != 0) { printf("BF_ecb_encrypt blowfish error encrypting\n"); printf("got :"); for (i=0; i<8; i++) printf("%02X ",out[i]); printf("\n"); printf("expected:"); for (i=0; i<8; i++) printf("%02X ",cipher_data[n][i]); err=1; printf("\n"); } BF_ecb_encrypt(out,out,&key,BF_DECRYPT); if (memcmp(&(plain_data[n][0]),out,8) != 0) { printf("BF_ecb_encrypt error decrypting\n"); printf("got :"); for (i=0; i<8; i++) printf("%02X ",out[i]); printf("\n"); printf("expected:"); for (i=0; i<8; i++) printf("%02X ",plain_data[n][i]); printf("\n"); err=1; } } printf("testing blowfish set_key\n"); for (n=1; n<KEY_TEST_NUM; n++) { BF_set_key(&key,n,key_test); BF_ecb_encrypt(key_data,out,&key,BF_ENCRYPT); if (memcmp(out,&(key_out[n-1][0]),8) != 0) { printf("blowfish setkey error\n"); err=1; } } printf("testing blowfish in cbc mode\n"); len=strlen(cbc_data)+1; BF_set_key(&key,16,cbc_key); memset(cbc_in,0,sizeof cbc_in); memset(cbc_out,0,sizeof cbc_out); memcpy(iv,cbc_iv,sizeof iv); BF_cbc_encrypt((unsigned char *)cbc_data,cbc_out,len, &key,iv,BF_ENCRYPT); if (memcmp(cbc_out,cbc_ok,32) != 0) { err=1; printf("BF_cbc_encrypt encrypt error\n"); for (i=0; i<32; i++) printf("0x%02X,",cbc_out[i]); } memcpy(iv,cbc_iv,8); BF_cbc_encrypt(cbc_out,cbc_in,len, &key,iv,BF_DECRYPT); if (memcmp(cbc_in,cbc_data,strlen(cbc_data)+1) != 0) { printf("BF_cbc_encrypt decrypt error\n"); err=1; } printf("testing blowfish in cfb64 mode\n"); BF_set_key(&key,16,cbc_key); memset(cbc_in,0,40); memset(cbc_out,0,40); memcpy(iv,cbc_iv,8); n=0; BF_cfb64_encrypt((unsigned char *)cbc_data,cbc_out,(long)13, &key,iv,&n,BF_ENCRYPT); BF_cfb64_encrypt((unsigned char *)&(cbc_data[13]),&(cbc_out[13]),len-13, &key,iv,&n,BF_ENCRYPT); if (memcmp(cbc_out,cfb64_ok,(int)len) != 0) { err=1; printf("BF_cfb64_encrypt encrypt error\n"); for (i=0; i<(int)len; i++) printf("0x%02X,",cbc_out[i]); } n=0; memcpy(iv,cbc_iv,8); BF_cfb64_encrypt(cbc_out,cbc_in,17, &key,iv,&n,BF_DECRYPT); BF_cfb64_encrypt(&(cbc_out[17]),&(cbc_in[17]),len-17, &key,iv,&n,BF_DECRYPT); if (memcmp(cbc_in,cbc_data,(int)len) != 0) { printf("BF_cfb64_encrypt decrypt error\n"); err=1; } printf("testing blowfish in ofb64\n"); BF_set_key(&key,16,cbc_key); memset(cbc_in,0,40); memset(cbc_out,0,40); memcpy(iv,cbc_iv,8); n=0; BF_ofb64_encrypt((unsigned char *)cbc_data,cbc_out,(long)13,&key,iv,&n); BF_ofb64_encrypt((unsigned char *)&(cbc_data[13]), &(cbc_out[13]),len-13,&key,iv,&n); if (memcmp(cbc_out,ofb64_ok,(int)len) != 0) { err=1; printf("BF_ofb64_encrypt encrypt error\n"); for (i=0; i<(int)len; i++) printf("0x%02X,",cbc_out[i]); } n=0; memcpy(iv,cbc_iv,8); BF_ofb64_encrypt(cbc_out,cbc_in,17,&key,iv,&n); BF_ofb64_encrypt(&(cbc_out[17]),&(cbc_in[17]),len-17,&key,iv,&n); if (memcmp(cbc_in,cbc_data,(int)len) != 0) { printf("BF_ofb64_encrypt decrypt error\n"); err=1; } return(err); }