| 5 |
| #include <inttypes.h> |
| 6 | #include <stdlib.h> |
| 7 | #include "aes/aes.h" |
| 8 | #include "utils.h" |
| 9 | #include "sqlite.h" |
| 10 | #include "gif.h" |
| 11 |
| 12 | jint JNI_OnLoad(JavaVM *vm, void *reserved) { |
| 13 | JNIEnv *env = 0; |
| 14 | srand(time(NULL)); |
| 15 |
| 16 | if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_4) != JNI_OK) { |
| 17 | return -1; |
| 18 | } |
| 19 |
| 20 | if (sqliteOnJNILoad(vm, reserved, env) == -1) { |
| 21 | return -1; |
| 22 | } |
| 23 |
| 24 | gifOnJNILoad(vm, reserved, env); |
| 25 |
| 26 | return JNI_VERSION_1_4; |
| 27 | } |
| 28 |
| 29 | void JNI_OnUnload(JavaVM *vm, void *reserved) { |
| 30 | gifOnJNIUnload(vm, reserved); |
| 31 | } |
| 32 |
| 33 | JNIEXPORT jbyteArray Java_org_telegram_messenger_Utilities_aesIgeEncryption(JNIEnv *env, jclass class, jbyteArray _what, jbyteArray _key, jbyteArray _iv, jboolean encrypt, jboolean changeIv, jint l) { |
| 34 | unsigned char *what = (unsigned char *)(*env)->GetByteArrayElements(env, _what, NULL); |
| 35 | unsigned char *key = (unsigned char *)(*env)->GetByteArrayElements(env, _key, NULL); |
| 36 | unsigned char *__iv = (unsigned char *)(*env)->GetByteArrayElements(env, _iv, NULL); |
| 37 | unsigned char *iv = 0; |
| 38 |
| 39 | if (!changeIv) { |
| 40 | iv = (unsigned char *)malloc((*env)->GetArrayLength(env, _iv)); |
| 41 | memcpy(iv, __iv, (*env)->GetArrayLength(env, _iv)); |
| 42 | } else { |
| 43 | iv = __iv; |
| 44 | } |
| 45 |
| 46 | int len = l == 0 ? (*env)->GetArrayLength(env, _what) : l; |
| 47 | AES_KEY akey; |
| 48 | if (!encrypt) { |
| 49 | AES_set_decrypt_key(key, (*env)->GetArrayLength(env, _key) * 8, &akey); |
| 50 | AES_ige_encrypt(what, what, len, &akey, iv, AES_DECRYPT); |
| 51 | } else { |
| 52 | AES_set_encrypt_key(key, (*env)->GetArrayLength(env, _key) * 8, &akey); |
| 53 | AES_ige_encrypt(what, what, len, &akey, iv, AES_ENCRYPT); |
| 54 | } |
| 55 | (*env)->ReleaseByteArrayElements(env, _what, what, 0); |
| 56 | (*env)->ReleaseByteArrayElements(env, _key, key, JNI_ABORT); |
| 57 | if (!changeIv) { |
| 58 | (*env)->ReleaseByteArrayElements(env, _iv, __iv, JNI_ABORT); |
| 59 | free(iv); |
| 60 | } else { |
| 61 | (*env)->ReleaseByteArrayElements(env, _iv, __iv, 0); |
| 62 | } |
| 63 | return _what; |
| 64 | } |
| 65 |
| 66 | JNIEXPORT void Java_org_telegram_messenger_Utilities_aesIgeEncryption2(JNIEnv *env, jclass class, jobject _what, jbyteArray _key, jbyteArray _iv, jboolean encrypt, jboolean changeIv, jint l) { |
| 67 | jbyte *what = (*env)->GetDirectBufferAddress(env, _what); |
| 68 | unsigned char *key = (unsigned char *)(*env)->GetByteArrayElements(env, _key, NULL); |
| 69 | unsigned char *__iv = (unsigned char *)(*env)->GetByteArrayElements(env, _iv, NULL); |
| 70 | unsigned char *iv = 0; |
| 71 |
| 72 | if (!changeIv) { |
| 73 | iv = (unsigned char *)malloc((*env)->GetArrayLength(env, _iv)); |
| 74 | memcpy(iv, __iv, (*env)->GetArrayLength(env, _iv)); |
| 75 | } else { |
| 76 | iv = __iv; |
| 77 | } |
| 78 |
| 79 | AES_KEY akey; |
| 80 | if (!encrypt) { |
| 81 | AES_set_decrypt_key(key, (*env)->GetArrayLength(env, _key) * 8, &akey); |
| 82 | AES_ige_encrypt(what, what, l, &akey, iv, AES_DECRYPT); |
| 83 | } else { |
| 84 | AES_set_encrypt_key(key, (*env)->GetArrayLength(env, _key) * 8, &akey); |
| 85 | AES_ige_encrypt(what, what, l, &akey, iv, AES_ENCRYPT); |
| 86 | } |
| 87 | (*env)->ReleaseByteArrayElements(env, _key, key, JNI_ABORT); |
| 88 | if (!changeIv) { |
| 89 | (*env)->ReleaseByteArrayElements(env, _iv, __iv, JNI_ABORT); |
| 90 | free(iv); |
| 91 | } else { |
| 92 | (*env)->ReleaseByteArrayElements(env, _iv, __iv, 0); |
| 93 | } |
| 94 | } |
| 95 |
| 96 | uint64_t gcd(uint64_t a, uint64_t b){ |
| 97 | while(a != 0 && b != 0) { |
| 98 | while((b & 1) == 0) b >>= 1; |
| 99 | while((a & 1) == 0) a >>= 1; |
| 100 | if(a > b) a -= b; else b -= a; |
| 101 | } |
| 102 | return b == 0 ? a : b; |
| 103 | } |
| 104 |
| 105 | JNIEXPORT jlong Java_org_telegram_messenger_Utilities_doPQNative(JNIEnv* env, jclass class, jlong _what) { |
| 106 | uint64_t what = _what; |
| 107 | int it = 0, i, j; |
| 108 | uint64_t g = 0; |
| 109 | for (i = 0; i < 3 || it < 1000; i++){ |
| 110 | int q = ((lrand48() & 15) + 17) % what; |
| 111 | uint64_t x = (long long)lrand48() % (what - 1) + 1, y = x; |
| 112 | int lim = 1 << (i + 18), j; |
| 113 | for(j = 1; j < lim; j++){ |
| 114 | ++it; |
| 115 | uint64_t a = x, b = x, c = q; |
| 116 | while(b){ |
| 117 | if(b & 1){ |
| 118 | c += a; |
| 119 | if(c >= what) c -= what; |
| 120 | } |
| 121 | a += a; |
| 122 | if(a >= what) a -= what; |
| 123 | b >>= 1; |
| 124 | } |
| 125 | x = c; |
| 126 | uint64_t z = x < y ? what + x - y : x - y; |
| 127 | g = gcd(z, what); |
| 128 | if(g != 1) break; |
| 129 | if(!(j & (j - 1))) y = x; |
| 130 | } |
| 131 | if(g > 1 && g < what) break; |
| 132 | } |
| 133 | return g; |
| 134 | } |
| 135 |