encryption - Malloc in openssl -


i got problem when encrypt data aes encryption. source code:

    std::string aes_encrypt( std::string text, std::string password ){     evp_cipher_ctx ectx;     std::string key = sha256(password);     std::string iv = sha256("aes_iv_"+password);     int size = text.size();     unsigned char* out = (unsigned char*)malloc( size );     int outlen = 0;     int tlen = 0;      evp_cipher_ctx_init( &ectx );     evp_encryptinit( &ectx, evp_aes_256_cbc(), (const unsigned char*)key.c_str(), (const unsigned char*)iv.c_str() );     evp_encryptupdate( &ectx, out, &outlen, (const unsigned char*)text.c_str(), text.size() );     tlen += outlen;     evp_encryptfinal( &ectx, out+tlen, &outlen );     tlen += outlen;     evp_cipher_ctx_cleanup( &ectx );      std::string data( (const char*)out, tlen );     free( out );     return data; } 

my application crash in line: free(out); solution fix problem?

in line "out = (unsigned char*) malloc(size)" assume output never longer input. not case (openssl documentation says have add block size or so), believe have buffer overflow in malloc'ed buffer during encryption, causes crash during free(...) call --- that's quite usual thing when destroy malloc/free data structures overflowing malloc'ated buffer.


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -