void BF_ecb_encrypt(const unsigned char *in,unsigned char *out,BF_KEY *ks,int encrypt) { BF_LONG l,d[2]; n2l(in,l); d[0]=l; n2l(in,l); d[1]=l; if (encrypt) BF_encrypt(d,ks); else BF_decrypt(d,ks); l=d[0]; l2n(l,out); l=d[1]; l2n(l,out); l=d[0]=d[1]=0; }
static void blf_decrypt(caddr_t key, u_int8_t *blk) { BF_LONG t[2]; memcpy(t, blk, sizeof (t)); t[0] = ntohl(t[0]); t[1] = ntohl(t[1]); /* NB: BF_decrypt expects the block in host order! */ BF_decrypt(t, (BF_KEY *) key); t[0] = htonl(t[0]); t[1] = htonl(t[1]); memcpy(blk, t, sizeof (t)); }
bool Packet::decodeBlowfish( bool bUseStaticBFKey ) { int blen = (int)getPacketSize(); int datalen = blen - 2; int n8bc = datalen / 8; int rest = datalen - n8bc*8; if( rest > 0 ) rest = 8; int newdatalen = datalen + rest; int newbuflen = newdatalen + 2; if( blen < 1 ) return false; unsigned char *buf = b.getBytesPtr(); if( !buf ) return false; BF_KEY bfkey; if( bUseStaticBFKey ) BF_set_key( &bfkey, (int)this->STATIC_BLOWFISH_KEY_LEN, this->STATIC_BLOWFISH_KEY ); else BF_set_key( &bfkey, (int)this->NEW_BLOWFISH_KEY_LEN, this->NEW_BLOWFISH_KEY ); int offset = 0; int nPasses = 0; unsigned char outbuf [1024]; /* if( !outbuf ) { return false; } */ memset( outbuf, 0, newbuflen ); outbuf[0] = buf[0]; outbuf[1] = buf[1]; nPasses = 0; for( offset=2; offset<newdatalen; offset+=8 ) { unsigned char data[8] = {0,0,0,0,0,0,0,0}; memcpy( data, buf+offset, 8 ); BF_decrypt( (BF_LONG *)data, &bfkey ); memcpy( outbuf+offset, data, 8 ); nPasses++; } this->setBytes( outbuf, blen ); return true; }
static int test_bf_ecb_raw(int n) { int ret = 1; BF_KEY key; BF_LONG data[2]; 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 (!TEST_mem_eq(&(bf_cipher[n][0]), BF_BLOCK, &(data[0]), BF_BLOCK)) ret = 0; BF_decrypt(&(data[0]), &key); if (!TEST_mem_eq(&(bf_plain[n][0]), BF_BLOCK, &(data[0]), BF_BLOCK)) ret = 0; return ret; }
void AIM_Decrypt(char base64_blob[], wchar_t passw[]) { BF_KEY key; uint8_t aim_key[MAX_KEY_LEN]; AIM_PASSWORD_BLOB blob; int i; base64_decode(&blob, base64_blob); memcpy(&aim_key, blob.Salt, MAX_SALT_LEN); memcpy(&aim_key[MAX_SALT_LEN], static_key, sizeof(static_key)); BF_set_key(&key, sizeof(aim_key), aim_key); BF_LONG *in = (BF_LONG*)blob.Password; BF_LONG *out = (BF_LONG*)passw; for (i = 0; i < MAX_PASS_LEN * 2 / sizeof(BF_LONG); i += 2) { memcpy(&out[i], &in[i], sizeof(BF_LONG)*2); BF_decrypt(&out[i], &key); } }
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); }
void BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, const BF_KEY *schedule, unsigned char *ivec, int encrypt) { register BF_LONG tin0, tin1; register BF_LONG tout0, tout1, xor0, xor1; register long l = length; BF_LONG tin[2]; if (encrypt) { n2l(ivec, tout0); n2l(ivec, tout1); ivec -= 8; for (l -= 8; l >= 0; l -= 8) { n2l(in, tin0); n2l(in, tin1); tin0 ^= tout0; tin1 ^= tout1; tin[0] = tin0; tin[1] = tin1; BF_encrypt(tin, schedule); tout0 = tin[0]; tout1 = tin[1]; l2n(tout0, out); l2n(tout1, out); } if (l != -8) { n2ln(in, tin0, tin1, l + 8); tin0 ^= tout0; tin1 ^= tout1; tin[0] = tin0; tin[1] = tin1; BF_encrypt(tin, schedule); tout0 = tin[0]; tout1 = tin[1]; l2n(tout0, out); l2n(tout1, out); } l2n(tout0, ivec); l2n(tout1, ivec); } else { n2l(ivec, xor0); n2l(ivec, xor1); ivec -= 8; for (l -= 8; l >= 0; l -= 8) { n2l(in, tin0); n2l(in, tin1); tin[0] = tin0; tin[1] = tin1; BF_decrypt(tin, schedule); tout0 = tin[0] ^ xor0; tout1 = tin[1] ^ xor1; l2n(tout0, out); l2n(tout1, out); xor0 = tin0; xor1 = tin1; } if (l != -8) { n2l(in, tin0); n2l(in, tin1); tin[0] = tin0; tin[1] = tin1; BF_decrypt(tin, schedule); tout0 = tin[0] ^ xor0; tout1 = tin[1] ^ xor1; l2nn(tout0, tout1, out, l + 8); xor0 = tin0; xor1 = tin1; } l2n(xor0, ivec); l2n(xor1, ivec); } tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0; tin[0] = tin[1] = 0; }
bool L2LoginPacket::decodeBlowfish( bool bUseStaticBFKey ) { int blen = (int)getPacketSize(); // all array length int datalen = blen - 2; // only data len, w/o 1st 2 bytes - packet size int n8bc = datalen / 8; // 8-byte blocks count int rest = datalen - n8bc*8; // bytes left if( rest > 0 ) rest = 8; // do we need addition? int newdatalen = datalen + rest; // dlina novogo u4astka dannih int newbuflen = newdatalen + 2; // dlina novogo paketa if( blen < 1 ) return false; // TODO: throw? unsigned char *buf = b.getBytesPtr(); if( !buf ) return false; // TODO: throw? // initialize Blowfish key BF_KEY bfkey; if( bUseStaticBFKey ) BF_set_key( &bfkey, (int)this->STATIC_BLOWFISH_KEY_LEN, this->STATIC_BLOWFISH_KEY ); else BF_set_key( &bfkey, (int)this->NEW_BLOWFISH_KEY_LEN, this->NEW_BLOWFISH_KEY ); int offset = 0; int nPasses = 0; unsigned char *outbuf = NULL; // we will decode to this buffer outbuf = (unsigned char *)malloc( newbuflen ); if( !outbuf ) { #ifdef L2LOGINP_DEBUGOUT_BLOWFISH printf( "L2LoginPacket::decodeBlowfish(): memory error!\n" ); #endif // L2LOGINP_DEBUGOUT_BLOWFISH return false; } memset( outbuf, 0, newbuflen ); outbuf[0] = buf[0]; // copy packet len outbuf[1] = buf[1]; nPasses = 0; #ifdef L2LOGINP_DEBUGOUT_BLOWFISH if( bUseStaticBFKey ) printf( "L2LoginPacket::decodeBlowfish(): using STATIC BF KEY\n" ); else printf( "L2LoginPacket::decodeBlowfish(): using DYNAMIC BF KEY\n" ); #endif // L2LOGINP_DEBUGOUT_BLOWFISH for( offset=2; offset<newdatalen; offset+=8 ) { unsigned char data[8] = {0,0,0,0,0,0,0,0}; memcpy( data, buf+offset, 8 ); BF_decrypt( (BF_LONG *)data, &bfkey ); memcpy( outbuf+offset, data, 8 ); nPasses++; } #ifdef L2LOGINP_DEBUGOUT_BLOWFISH printf( "L2LoginPacket::decodeBlowfish(): Decode2: %d passes, %d 8-byte blocks\n", nPasses, n8bc ); //L2LoginPacket *dec2 = new L2LoginPacket( outbuf, blen ); //dec2->dumpToFile( stdout ); //dec2->saveToFileRaw( "pdecodedbf.dat" ); //delete dec2; #endif // L2LOGINP_DEBUGOUT_BLOWFISH this->setBytes( outbuf, blen ); free( outbuf ); #ifdef L2LOGINP_DEBUGOUT_BLOWFISH printf( "L2LoginPacket::decodeBlowfish(): decode complete\n" ); #endif // L2LOGINP_DEBUGOUT_BLOWFISH return true; }
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); }