android - App crashes when calling native function encrypt hundred of times -
i have finished aes encryption wrapper written in c using openssl library. used wrapper in android project. when called encrypt function hundred of times encrypt lot of small files (images) caused crash error:
02-06 14:39:44.110: a/libc(5114): @@@ aborting: invalid heap address in dlfree 02-06 14:39:44.110: a/libc(5114): fatal signal 11 (sigsegv) @ 0xdeadbaad (code=1)
this memory leak error can't figure out myself. guess went wrong in native code. below encrypt function written in c using openssl library. (compiled ndk)
function init encrypt key
int aes_init_encrypt(unsigned char *key_data, int key_data_len, unsigned char *salt, evp_cipher_ctx *e_ctx) { int i, nrounds = 4; unsigned char key[16], iv[16]; /* * gen key & iv aes 128 cbc mode. sha1 digest used hash supplied key material. * nrounds number of times hash material. more rounds more secure * slower. */ = evp_bytestokey(evp_aes_128_cbc(), evp_sha1(), salt, key_data, key_data_len, nrounds, key, iv); if (i != 16) { //__android_log_write(android_log_error, "tamnv-encryption", "key size %d bits - should 128 bits\n"); return -1; } evp_cipher_ctx_init(e_ctx); evp_encryptinit_ex(e_ctx, evp_aes_128_cbc(), null, key, iv); return 0; }
function encrypt
jint java_com_openssl_aes_wrapperaes_encryptaes(jnienv *env, jobject obj, jstring password, jstring source, jstring destination) { file* source_file; file* destination_file; const char *source_path = (*env)->getstringutfchars(env, source, null); const char *destination_path = (*env)->getstringutfchars(env, destination, null); source_file = fopen(source_path, "rb"); destination_file = fopen(destination_path, "wb"); if (source_file == null || destination_file == null) { return -1; } // prepare encrypt // 1. password const char *pass = (*env)->getstringutfchars(env, password, null); unsigned char *key_data = malloc(strlen(pass)); if (key_data) strcpy(key_data, pass); else return -1; // 2. init evp_cipher evp_cipher_ctx e_ctx; if (aes_init_encrypt(key_data, strlen(key_data), key_data, &e_ctx) == -1) return -1; /* buffers */ unsigned char inbuf[buffer_size]; int inlen; /* allow enough space in output buffer additional cipher block */ unsigned char outbuf[buffer_size + aes_block_size]; int outlen; int writelen; while ((inlen = fread(inbuf, 1, buffer_size, source_file)) > 0) { // fwrite(inbuf, 1, inlen, destination_file); if (!evp_cipherupdate(&e_ctx, outbuf, &outlen, inbuf, inlen)) { /* error */ evp_cipher_ctx_cleanup(&e_ctx); return -1; } writelen = fwrite(outbuf, sizeof(*outbuf), outlen, destination_file); if (writelen != outlen) { /* error */ evp_cipher_ctx_cleanup(&e_ctx); return -1; } } /* handle remaining cipher block + padding */ if (!evp_cipherfinal_ex(&e_ctx, outbuf, &outlen)) { /* error */ evp_cipher_ctx_cleanup(&e_ctx); return -1; } /* write remainign cipher block + padding*/ fwrite(outbuf, sizeof(*inbuf), outlen, destination_file); evp_cipher_ctx_cleanup(&e_ctx); fclose(source_file); fclose(destination_file); free(key_data); // __android_log_print(android_log_debug, "tamnv","encrypt --- %s", source_path); return 0; }
what's wrong native code? appreciated!
Comments
Post a Comment