static void *blowfish_init(u_char *sesskey, int len) { struct blowfish_state *state; state = malloc(sizeof(*state)); BF_set_key(&state->key, len, sesskey); memset(state->iv, 0, 8); return (state); }
/* the wrapper functions for blowfish */ static int blowfish_set_key(struct crypto_struct *cipher, void *key){ if (cipher->key == NULL) { if (alloc_key(cipher) < 0) { return -1; } BF_set_key(cipher->key, 16, key); } return 0; }
void CSporeEncrypt::BF_decrypt(const unsigned char *keydata, int keydatalen, unsigned char *in, unsigned char *out, unsigned int inlen) { BF_KEY key; unsigned char ivec[32]; int num = 0; // set up for decryption BF_set_key(&key, keydatalen, keydata); memset(ivec, '\0', 32); BF_cfb64_encrypt(in, out, inlen, &key, ivec, &num, BF_DECRYPT); }
/* the wrapper functions for blowfish */ static int blowfish_set_key(struct ssh_cipher_struct *cipher, void *key, void *IV){ if (cipher->key == NULL) { if (alloc_key(cipher) < 0) { return -1; } BF_set_key(cipher->key, 16, key); } cipher->IV = IV; return 0; }
void decrypt_chal(char *chal, char *pwd) { register int i; BF_KEY key; BF_set_key(&key, 16, MD5(pwd,strlen(pwd),NULL)); for(i=0; i < VTUN_CHAL_SIZE; i += 8 ) BF_ecb_encrypt(chal + i, chal + i, &key, BF_DECRYPT); }
static void bf_ssh1_init (EVP_CIPHER_CTX * ctx, const unsigned char *key, const unsigned char *iv, int enc) { if (iv != NULL) memcpy (&(ctx->oiv[0]), iv, 8); memcpy (&(ctx->iv[0]), &(ctx->oiv[0]), 8); if (key != NULL) BF_set_key (&(ctx->c.bf_ks), EVP_CIPHER_CTX_key_length (ctx), key); }
unsigned char *bf_str_encrypt(unsigned char * str) { if(!strlen(str)) return "."; unsigned char *ptr=str; unsigned char *encrypt=(unsigned char *)malloc(sizeof(char)*512); unsigned char *tmp=(unsigned char *)malloc(sizeof(char)*9); unsigned char *out = calloc(9, sizeof(unsigned char *)); BF_KEY *key = calloc(9, 9); BF_set_key(key, SIZE, (const unsigned char*)"TestKey" ); unsigned int jmp=0,counter=0; bzero(encrypt,512); while(*ptr != '\0' && counter != 512) { *(tmp+jmp)=*ptr; if(jmp==7) { BF_ecb_encrypt(tmp, out, key, BF_ENCRYPT); strcat(encrypt,out); bzero(out,9); bzero(tmp,9); jmp=-1; } ptr++; jmp++; counter++; } if(strlen(tmp)<=8) { bzero(out,9); while(strlen(tmp)<7) strcat(tmp,"."); BF_ecb_encrypt(tmp, out, key, BF_ENCRYPT); strcat(encrypt,out); } bzero(out,9); bzero(tmp,9); if(out) free(out); if(tmp) free(tmp); fprintf(stdout,"Result %s\n",encrypt); return encrypt; }
static int blf_setkey(u_int8_t **sched, const u_int8_t *key, int len) { *sched = malloc(sizeof(BF_KEY), M_CRYPTO_DATA, M_NOWAIT|M_ZERO); if (*sched == NULL) return ENOMEM; BF_set_key((BF_KEY *) *sched, len, key); return 0; }
static void *blowfish_init(u_char *sesskey, int len) { struct blowfish_state *state; state = malloc(sizeof(*state)); if (state == NULL) /* oops, couldn't allocate memory */ return NULL; BF_set_key(&state->key, len, sesskey); memset(state->iv, 0, 8); return (state); }
bool Packet::encodeBlowfish( bool bUseStaticBFKey ) { unsigned char *buf = this->b.getBytesPtr(); if( !buf ) return false; unsigned int blen = getPacketSize(); if( blen < 1 ) 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 ); unsigned int offset = 0; int nPasses = 0; unsigned char outbuf [1024]; /* if( !outbuf ) { return false; } */ memset( outbuf, 0, blen ); outbuf[0] = buf[0]; outbuf[1] = buf[1]; for( offset=2; offset<real_size-2; offset+=8 ) { unsigned char data[8] = {0,0,0,0,0,0,0,0}; memcpy( data, buf+offset, 8 ); BF_encrypt( (BF_LONG *)data, &bfkey ); memcpy( outbuf+offset, data, 8 ); nPasses++; } this->setBytes( outbuf, blen ); return true; }
int main(void) { int i; BF_KEY key; BIO* bio_out; static unsigned char key_data[BF_KEY_LENGTH] = { 0x52,0x69,0xf1,0x49,0xd4,0x1b,0xa0,0x15, 0x24,0x97,0x57,0x4d,0x7f,0x15,0x31,0x25 }; unsigned char ciphertext[BF_BLOCK]; unsigned char plaintext[BF_BLOCK]; /* Open SSL's DES ECB encrypt/decrypt function only handles 8 bytes of data */ char* data_to_encrypt = "8 Bytes."; /* set the key structure using the predefined key */ BF_set_key(&key, BF_KEY_LENGTH, key_data); BF_ecb_encrypt(data_to_encrypt, ciphertext, &key, BF_ENCRYPT); bio_out = BIO_new_fp(stdout, BIO_NOCLOSE); BIO_printf(bio_out, "Original plaintext: %s\n", data_to_encrypt); BIO_printf(bio_out, "Ciphertext: "); /* print out the ciphertext */ for (i = 0; i < BF_BLOCK; i++) BIO_printf(bio_out, "%02x", ((unsigned char*)ciphertext)[i]); BIO_printf(bio_out, "\n"); /* start the decryption process */ BF_ecb_encrypt(ciphertext, plaintext, &key, BF_DECRYPT); BIO_printf(bio_out, "Recovered plaintext: "); /* print out the plaintext */ for (i = 0; i < BF_BLOCK; i++) BIO_printf(bio_out, "%c", ((unsigned char*)plaintext)[i]); BIO_printf(bio_out, "\n"); BIO_free(bio_out); free(ciphertext); free(plaintext); return 0; }
static int blf_setkey(u_int8_t **sched, u_int8_t *key, int len) { int err; *sched = kmalloc(sizeof(BF_KEY), M_CRYPTO_DATA, M_INTWAIT | M_ZERO); if (*sched != NULL) { BF_set_key((BF_KEY *) *sched, len, key); err = 0; } else err = ENOMEM; return err; }
void * blowfish_init(u_char *sesskey, int len) { struct blowfish_state *state; if ((state = malloc(sizeof(*state))) == NULL) err(1, "malloc"); BF_set_key(&state->key, len, sesskey); memset(state->iv, 0, 8); return (state); }
static int bf_init(PX_Cipher * c, const uint8 *key, unsigned klen, const uint8 *iv) { ossldata *od = c->ptr; BF_set_key(&od->u.bf.key, klen, key); if (iv) memcpy(od->iv, iv, BF_BLOCK); else memset(od->iv, 0, BF_BLOCK); od->u.bf.num = 0; return 0; }
unsigned char *bf_str_decrypt(unsigned char * str) { if(!strlen(str)) return "."; unsigned char *ptr=str; unsigned char *decrypt=(unsigned char *)malloc(sizeof(char)*512); unsigned char *tmp=(unsigned char *)malloc(sizeof(char)*9); unsigned char *out = calloc(9, sizeof(unsigned char *)); BF_KEY *key = calloc(9, 9); BF_set_key(key, SIZE, (const unsigned char*)"TestKey" ); unsigned int jmp=0,counter=0; bzero(decrypt,512); while(*ptr != '\0' && counter != 511) { *(tmp+jmp)=*ptr; if(jmp==7) { BF_ecb_encrypt(tmp, out, key, BF_DECRYPT); strcat(decrypt,out); bzero(out,9); bzero(tmp,9); jmp=-1; } ptr++; jmp++; counter++; } if( jmp > 0 && jmp < 8) { BF_ecb_encrypt(tmp, out, key, BF_DECRYPT); strcat(decrypt,out); } bzero(out,9); bzero(tmp,9); if(out) free(out); if(tmp) free(tmp); return decrypt; }
static int test_bf_set_key(int n) { int ret = 1; BF_KEY key; unsigned char out[8]; BF_set_key(&key, n+1, key_test); BF_ecb_encrypt(key_data, out, &key, BF_ENCRYPT); /* mips-sgi-irix6.5-gcc vv -mabi=64 bug workaround */ if (!TEST_mem_eq(out, 8, &(key_out[n][0]), 8)) ret = 0; return ret; }
int main(int argc, char *argv[]) { char *interface; pcap_t *p; int i; if (argc < 3 || strncmp(argv[1], "-h", 2) == 0) help(argv[0]); while ((i = getopt(argc, argv, "rk:")) >= 0) { switch (i) { case 'r': rawmode = 1; thc_ipv6_rawmode(1); break; case 'k': key = optarg; break; default: fprintf(stderr, "Unknown option\n"); exit(-1); } } interface = argv[optind]; if ((f = fopen(argv[optind + 1], "w")) == NULL) { fprintf(stderr, "Error: file %s cout not be created\n", argv[optind + 1]); exit(-1); } if (key != NULL) { memset(&bfkey, 0, sizeof(bfkey)); SHA1((unsigned char *) key, strlen(key), (unsigned char *) hash); BF_set_key(&bfkey, sizeof(hash), (unsigned char *) hash); memset(vec, 0, sizeof(vec)); num = 0; } if ((p = thc_pcap_init(interface, "ip6")) == NULL) { fprintf(stderr, "Error: could not capture on interface %s\n", interface); exit(-1); } while (1) { thc_pcap_check(p, (char *) check_packets, NULL); usleep(50); } return 0; }
static void bf_chunk(unsigned char *dst, const unsigned char *src, const unsigned char *secret, int enc) { BF_KEY bf_key; int i; BF_set_key(&bf_key, CHUNK_SIZE, secret); /* BF_ecb_encrypt works with 64bits at a time */ for (i = 0; i < CHUNK_SIZE/8; i ++) { BF_ecb_encrypt(src, dst, &bf_key, enc); src += 8; dst += 8; } }
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 do_encrypt(apr_pool_t *p, const char *key, char *str, int len, int enc) { apr_md5_ctx_t my_md5; unsigned char *hash = apr_pcalloc(p, APR_MD5_DIGESTSIZE); apr_md5_init(&my_md5); apr_md5_update(&my_md5, key, (unsigned int)len); apr_md5_final(hash, &my_md5); BF_KEY my_bf; BF_set_key(&my_bf, APR_MD5_DIGESTSIZE, hash); int num = 0; unsigned char iv[8] = {0,0,0,0,0,0,0,0}; BF_cfb64_encrypt(str, str, len, &my_bf, iv, &num, enc); }
unsigned int SRNG_init(struct SRNG_st *st) { if(!st) goto end; if(!RAND_bytes(st->keydata, sizeof(st->keydata)) || !RAND_bytes(st->rnd, sizeof(st->rnd))) { ERR_print_errors_fp(stderr); Throw(lib_crypto_exception); } BF_set_key(&st->key, sizeof(st->keydata), st->keydata); st->idx = 0; end: return sizeof(struct SRNG_st); }
static int ssh_bf_ctr_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc) { struct ssh_blowfish_ctr_ctx *c; if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { c = malloc(sizeof(*c)); EVP_CIPHER_CTX_set_app_data(ctx, c); } if (key != NULL) { BF_set_key(&c->blowfish_ctx, EVP_CIPHER_CTX_key_length(ctx), key); } if (iv != NULL) memcpy(c->blowfish_counter, iv, BF_BLOCK); return (1); }
bool BlowfishFilter::init() { pBlowFishKey_ = new BF_KEY; if (MIN_KEY_SIZE <= keySize_ && keySize_ <= MAX_KEY_SIZE) { BF_set_key(this->pBlowFishKey(), key_.Len(), reinterpret_cast<const unsigned char*>(TCHAR_TO_ANSI(*key_))); isGood_ = true; } else { ERROR_MSG("BlowfishFilter::init: invalid length %d", key_.Len()); isGood_ = false; } return isGood_; }
static int test_bf_ecb(int n) { int ret = 1; BF_KEY key; unsigned char out[8]; BF_set_key(&key, 8, ecb_data[n]); BF_ecb_encrypt(&(plain_data[n][0]), out, &key, BF_ENCRYPT); if (!TEST_mem_eq(&(cipher_data[n][0]), BF_BLOCK, out, BF_BLOCK)) ret = 0; BF_ecb_encrypt(out, out, &key, BF_DECRYPT); if (!TEST_mem_eq(&(plain_data[n][0]), BF_BLOCK, out, BF_BLOCK)) ret = 0; return ret; }
/* * Standard CSPContext init, called from CSPFullPluginSession::init(). * Reusable, e.g., query followed by en/decrypt. */ void BlowfishContext::init( const Context &context, bool encrypting) { if(mInitFlag && !opStarted()) { return; } CSSM_SIZE keyLen; uint8 *keyData = NULL; bool sameKeySize = false; /* obtain key from context */ symmetricKeyBits(context, session(), CSSM_ALGID_BLOWFISH, encrypting ? CSSM_KEYUSE_ENCRYPT : CSSM_KEYUSE_DECRYPT, keyData, keyLen); if((keyLen < BF_MIN_KEY_SIZE_BYTES) || (keyLen > BF_MAX_KEY_SIZE_BYTES)) { CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY); } /* * Delete existing key if key size changed */ if(mRawKeySize == keyLen) { sameKeySize = true; } else { deleteKey(); } /* init key only if key size or key bits have changed */ if(!sameKeySize || memcmp(mRawKey, keyData, mRawKeySize)) { BF_set_key(&mBfKey, (int)keyLen, keyData); /* save this raw key data */ memmove(mRawKey, keyData, keyLen); mRawKeySize = (unsigned int)keyLen; } /* Finally, have BlockCryptor do its setup */ setup(BF_BLOCK, context); mInitFlag = true; }
int main() { unsigned char *in = (unsigned char *)"TestData"; unsigned char *out = calloc(1024, sizeof(unsigned char *)); unsigned char *out2 = calloc(1024, sizeof(unsigned char *)); unsigned char *out3 = calloc(1024, sizeof(unsigned char *)); BF_KEY *key = calloc(256, 256); /* set up a test key */ BF_set_key(key, SIZE, (const unsigned char*)"TestKey" ); /* test out encryption */ BF_ecb_encrypt(in, out, key, BF_ENCRYPT); printf("%s\n", out); /* test out decryption */ BF_ecb_encrypt(out, out2, key, BF_DECRYPT); printf("%s\n", out2); // printf ("result %s \n",bf_str_encrypt("Linux")); unsigned char *out4=bf_str_encrypt("seu madruga na vila"); printf ("result %s \n",out4); puts("OK!"); unsigned char *out5=bf_str_decrypt(out4); printf ("result decrypt %s \n",out5); if(out) free(out); if(out2) free(out2); if(out4) free(out4); if(out5) free(out5); return 0; }
caddr_t cgd_cipher_bf_init(int keylen, caddr_t key, int *blocksize) { struct bf_privdata *bp; if (!blocksize) return NULL; if (keylen < 40 || keylen > 448 || (keylen % 8 != 0)) return NULL; if (*blocksize == -1) *blocksize = 64; if (*blocksize != 64) return NULL; bp = malloc(sizeof(*bp), M_DEVBUF, 0); if (!bp) return NULL; BF_set_key(&bp->bp_key, keylen / 8, key); return (caddr_t)bp; }
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; }
//------------------------------------------------------------------------------------- bool KBEBlowfish::init() { pBlowFishKey_ = new BF_KEY; if ((MIN_KEY_SIZE <= keySize_) && (keySize_ <= MAX_KEY_SIZE)) { BF_set_key(this->pBlowFishKey(), key_.size(), (unsigned char*)key_.c_str() ); isGood_ = true; } else { ERROR_MSG(fmt::format("KBEBlowfish::init: " "invalid length {}\n", keySize_)); isGood_ = false; } return isGood_; }
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); } }