| 5 |
| #include <inttypes.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <openssl/aes.h> |
| 8 | #include <unistd.h> |
| 9 | #include "utils.h" |
| 10 | #include "image.h" |
| 11 |
| 12 | int registerNativeTgNetFunctions(JavaVM *vm, JNIEnv *env); |
| 13 | int gifvideoOnJNILoad(JavaVM *vm, JNIEnv *env); |
| 14 |
| 15 | jint JNI_OnLoad(JavaVM *vm, void *reserved) { |
| 16 | JNIEnv *env = 0; |
| 17 | srand(time(NULL)); |
| 18 |
| 19 | if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6) != JNI_OK) { |
| 20 | return -1; |
| 21 | } |
| 22 |
| 23 | if (imageOnJNILoad(vm, reserved, env) == -1) { |
| 24 | return -1; |
| 25 | } |
| 26 |
| 27 | if (gifvideoOnJNILoad(vm, env) == -1) { |
| 28 | return -1; |
| 29 | } |
| 30 |
| 31 | if (registerNativeTgNetFunctions(vm, env) != JNI_TRUE) { |
| 32 | return -1; |
| 33 | } |
| 34 |
| 35 | return JNI_VERSION_1_6; |
| 36 | } |
| 37 |
| 38 | void JNI_OnUnload(JavaVM *vm, void *reserved) { |
| 39 |
| 40 | } |
| 41 |
| 42 | JNIEXPORT void Java_org_telegram_messenger_Utilities_aesIgeEncryption(JNIEnv *env, jclass class, jobject buffer, jbyteArray key, jbyteArray iv, jboolean encrypt, int offset, int length) { |
| 43 | jbyte *what = (*env)->GetDirectBufferAddress(env, buffer) + offset; |
| 44 | unsigned char *keyBuff = (unsigned char *)(*env)->GetByteArrayElements(env, key, NULL); |
| 45 | unsigned char *ivBuff = (unsigned char *)(*env)->GetByteArrayElements(env, iv, NULL); |
| 46 |
| 47 | AES_KEY akey; |
| 48 | if (!encrypt) { |
| 49 | AES_set_decrypt_key(keyBuff, 32 * 8, &akey); |
| 50 | AES_ige_encrypt(what, what, length, &akey, ivBuff, AES_DECRYPT); |
| 51 | } else { |
| 52 | AES_set_encrypt_key(keyBuff, 32 * 8, &akey); |
| 53 | AES_ige_encrypt(what, what, length, &akey, ivBuff, AES_ENCRYPT); |
| 54 | } |
| 55 | (*env)->ReleaseByteArrayElements(env, key, keyBuff, JNI_ABORT); |
| 56 | (*env)->ReleaseByteArrayElements(env, iv, ivBuff, 0); |
| 57 | } |
| 58 |
| 59 | JNIEXPORT jint Java_org_telegram_messenger_Utilities_aesCtrDecryption(JNIEnv *env, jclass class, jobject buffer, jbyteArray key, jbyteArray iv, int offset, int length) { |
| 60 | jbyte *what = (*env)->GetDirectBufferAddress(env, buffer) + offset; |
| 61 | unsigned char *keyBuff = (unsigned char *)(*env)->GetByteArrayElements(env, key, NULL); |
| 62 | unsigned char *ivBuff = (unsigned char *)(*env)->GetByteArrayElements(env, iv, NULL); |
| 63 |
| 64 | AES_KEY akey; |
| 65 | unsigned int num = 0; |
| 66 | uint8_t count[16]; |
| 67 | memset(count, 0, 16); |
| 68 | AES_set_encrypt_key(keyBuff, 32 * 8, &akey); |
| 69 | AES_ctr128_encrypt(what, what, length, &akey, ivBuff, count, &num); |
| 70 | (*env)->ReleaseByteArrayElements(env, key, keyBuff, JNI_ABORT); |
| 71 | (*env)->ReleaseByteArrayElements(env, iv, ivBuff, JNI_ABORT); |
| 72 | return num; |
| 73 | } |
| 74 |
| 75 | JNIEXPORT jint Java_org_telegram_messenger_Utilities_aesCtrDecryptionByteArray(JNIEnv *env, jclass class, jbyteArray buffer, jbyteArray key, jbyteArray iv, int offset, int length, int fileOffset) { |
| 76 | unsigned char *bufferBuff = (unsigned char *)(*env)->GetByteArrayElements(env, buffer, NULL); |
| 77 | unsigned char *keyBuff = (unsigned char *)(*env)->GetByteArrayElements(env, key, NULL); |
| 78 | unsigned char *ivBuff = (unsigned char *)(*env)->GetByteArrayElements(env, iv, NULL); |
| 79 |
| 80 | AES_KEY akey; |
| 81 | uint8_t count[16]; |
| 82 | AES_set_encrypt_key(keyBuff, 32 * 8, &akey); |
| 83 | unsigned int num = (unsigned int) (fileOffset % 16); |
| 84 |
| 85 | int o = fileOffset / 16; |
| 86 | ivBuff[15] = (uint8_t) (o & 0xff); |
| 87 | ivBuff[14] = (uint8_t) ((o >> 8) & 0xff); |
| 88 | ivBuff[13] = (uint8_t) ((o >> 16) & 0xff); |
| 89 | ivBuff[12] = (uint8_t) ((o >> 24) & 0xff); |
| 90 | AES_encrypt(ivBuff, count, &akey); |
| 91 |
| 92 | o = (fileOffset + 15) / 16; |
| 93 | ivBuff[15] = (uint8_t) (o & 0xff); |
| 94 | ivBuff[14] = (uint8_t) ((o >> 8) & 0xff); |
| 95 | ivBuff[13] = (uint8_t) ((o >> 16) & 0xff); |
| 96 | ivBuff[12] = (uint8_t) ((o >> 24) & 0xff); |
| 97 |
| 98 | AES_ctr128_encrypt(bufferBuff + offset, bufferBuff + offset, length, &akey, ivBuff, count, &num); |
| 99 |
| 100 | (*env)->ReleaseByteArrayElements(env, key, keyBuff, JNI_ABORT); |
| 101 | (*env)->ReleaseByteArrayElements(env, iv, ivBuff, JNI_ABORT); |
| 102 | (*env)->ReleaseByteArrayElements(env, buffer, bufferBuff, 0); |
| 103 | return num; |
| 104 | } |
| 105 |
| 106 | JNIEXPORT jstring Java_org_telegram_messenger_Utilities_readlink(JNIEnv *env, jclass class, jstring path) { |
| 107 | static char buf[1000]; |
| 108 | char *fileName = (*env)->GetStringUTFChars(env, path, NULL); |
| 109 | int result = readlink(fileName, buf, 999); |
| 110 | jstring value = 0; |
| 111 | if (result != -1) { |
| 112 | buf[result] = '\0'; |
| 113 | value = (*env)->NewStringUTF(env, buf); |
| 114 | } |
| 115 | (*env)->ReleaseStringUTFChars(env, path, fileName); |
| 116 | return value; |
| 117 | } |
| 118 |