This commit is contained in:
vvaltman 2014-08-21 02:33:19 +04:00
parent 839e9cd448
commit f3636ced4c
12 changed files with 162 additions and 129 deletions

View File

@ -182,7 +182,7 @@ static int fetch_comb_binlog_user_add (void *extra) {
U->first_name = fetch_str_dup ();
U->last_name = fetch_str_dup ();
assert (!U->print_name);
U->print_name = create_print_name (U->id, U->first_name, U->last_name, 0, 0);
U->print_name = tgl_state.callback.create_print_name (U->id, U->first_name, U->last_name, 0, 0);
tglp_peer_insert_name ((void *)U);
U->access_hash = fetch_long ();
@ -311,7 +311,7 @@ static int fetch_comb_binlog_user_set_name (void *extra) {
tglp_peer_delete_name (U);
tfree_str (U->print_name);
}
U->print_name = create_print_name (U->id, U->user.first_name, U->user.last_name, 0, 0);
U->print_name = tgl_state.callback.create_print_name (U->id, U->user.first_name, U->user.last_name, 0, 0);
tglp_peer_insert_name ((void *)U);
if (tgl_state.callback.user_update) {
@ -386,11 +386,11 @@ static int fetch_comb_binlog_encr_chat_requested (void *extra) {
tgl_peer_t *Us = tgl_peer_get (TGL_MK_USER (U->user_id));
assert (!U->print_name);
if (Us) {
U->print_name = create_print_name (id, "!", Us->user.first_name, Us->user.last_name, 0);
U->print_name = tgl_state.callback.create_print_name (id, "!", Us->user.first_name, Us->user.last_name, 0);
} else {
static char buf[100];
tsnprintf (buf, 99, "user#%d", U->user_id);
U->print_name = create_print_name (id, "!", buf, 0, 0);
U->print_name = tgl_state.callback.create_print_name (id, "!", buf, 0, 0);
}
tglp_peer_insert_name ((void *)U);
U->g_key = talloc (256);
@ -480,7 +480,7 @@ static int fetch_comb_binlog_encr_chat_init (void *extra) {
tglp_insert_encrypted_chat (P);
tgl_peer_t *Us = tgl_peer_get (TGL_MK_USER (P->encr_chat.user_id));
assert (Us);
P->print_name = create_print_name (P->id, "!", Us->user.first_name, Us->user.last_name, 0);
P->print_name = tgl_state.callback.create_print_name (P->id, "!", Us->user.first_name, Us->user.last_name, 0);
tglp_peer_insert_name (P);
P->encr_chat.g_key = talloc (256);
@ -508,7 +508,7 @@ static int fetch_comb_binlog_chat_create (void *extra) {
C->flags = FLAG_CREATED | fetch_int ();
C->title = fetch_str_dup ();
assert (!C->print_title);
C->print_title = create_print_name (id, C->title, 0, 0, 0);
C->print_title = tgl_state.callback.create_print_name (id, C->title, 0, 0, 0);
tglp_peer_insert_name ((void *)C);
C->users_num = fetch_int ();
C->date = fetch_int ();
@ -545,7 +545,7 @@ static int fetch_comb_binlog_chat_set_title (void *extra) {
tglp_peer_delete_name ((void *)C);
tfree_str (C->print_name);
}
C->print_name = create_print_name (C->id, C->chat.title, 0, 0, 0);
C->print_name = tgl_state.callback.create_print_name (C->id, C->chat.title, 0, 0, 0);
tglp_peer_insert_name ((void *)C);
if (tgl_state.callback.chat_update) {

View File

@ -871,7 +871,9 @@ int parse_tlo_file (void) {
printf ("#include \"auto.h\"\n");
printf ("#include <assert.h>\n");
printf ("extern int *in_ptr, *in_end;\n");
printf ("extern int *tgl_in_ptr, *tgl_in_end;\n");
printf ("#define in_ptr tgl_in_ptr\n");
printf ("#define in_end tgl_in_end\n");
printf ("#include \"auto-static.c\"\n");
for (i = 0; i < tn; i++) {

View File

@ -96,7 +96,7 @@ static long long precise_time;
static double get_utime (int clock_id) {
struct timespec T;
my_clock_gettime (clock_id, &T);
tgl_my_clock_gettime (clock_id, &T);
double res = T.tv_sec + (double) T.tv_nsec * 1e-9;
if (clock_id == CLOCK_REALTIME) {
precise_time = (long long) (res * (1LL << 32));
@ -139,9 +139,7 @@ static int rsa_load_public_key (const char *public_key_name) {
return -1;
}
if (verbosity) {
vlogprintf (E_WARNING, "public key '%s' loaded successfully\n", rsa_public_key_name);
}
return 0;
}
@ -173,12 +171,12 @@ static int encrypt_buffer[ENCRYPT_BUFFER_INTS];
static int decrypt_buffer[ENCRYPT_BUFFER_INTS];
static int encrypt_packet_buffer (void) {
return pad_rsa_encrypt ((char *) packet_buffer, (packet_ptr - packet_buffer) * 4, (char *) encrypt_buffer, ENCRYPT_BUFFER_INTS * 4, pubKey->n, pubKey->e);
return tgl_pad_rsa_encrypt ((char *) packet_buffer, (packet_ptr - packet_buffer) * 4, (char *) encrypt_buffer, ENCRYPT_BUFFER_INTS * 4, pubKey->n, pubKey->e);
}
static int encrypt_packet_buffer_aes_unauth (const char server_nonce[16], const char hidden_client_nonce[32]) {
init_aes_unauth (server_nonce, hidden_client_nonce, AES_ENCRYPT);
return pad_aes_encrypt ((char *) packet_buffer, (packet_ptr - packet_buffer) * 4, (char *) encrypt_buffer, ENCRYPT_BUFFER_INTS * 4);
tgl_init_aes_unauth (server_nonce, hidden_client_nonce, AES_ENCRYPT);
return tgl_pad_aes_encrypt ((char *) packet_buffer, (packet_ptr - packet_buffer) * 4, (char *) encrypt_buffer, ENCRYPT_BUFFER_INTS * 4);
}
@ -421,7 +419,7 @@ static int process_respq_answer (struct connection *c, char *packet, int len) {
}
static int check_prime (BIGNUM *p) {
int r = BN_is_prime (p, BN_prime_checks, 0, BN_ctx, 0);
int r = BN_is_prime (p, BN_prime_checks, 0, tgl_state.BN_ctx, 0);
ensure (r >= 0);
return r;
}
@ -434,7 +432,7 @@ int tglmp_check_DH_params (BIGNUM *p, int g) {
BN_init (&dh_g);
ensure (BN_set_word (&dh_g, 4 * g));
ensure (BN_mod (&t, p, &dh_g, BN_ctx));
ensure (BN_mod (&t, p, &dh_g, tgl_state.BN_ctx));
int x = BN_get_word (&t);
assert (x >= 0 && x < 4 * g);
@ -465,7 +463,7 @@ int tglmp_check_DH_params (BIGNUM *p, int g) {
BIGNUM b;
BN_init (&b);
ensure (BN_set_word (&b, 2));
ensure (BN_div (&t, 0, p, &b, BN_ctx));
ensure (BN_div (&t, 0, p, &b, tgl_state.BN_ctx));
if (!check_prime (&t)) { return -1; }
BN_free (&b);
BN_free (&t);
@ -528,12 +526,12 @@ static int process_dh_answer (struct connection *c, char *packet, int len) {
assert (*(int *) (packet + 20) == (int)CODE_server_DH_params_ok);
assert (!memcmp (packet + 24, nonce, 16));
assert (!memcmp (packet + 40, server_nonce, 16));
init_aes_unauth (server_nonce, new_nonce, AES_DECRYPT);
tgl_init_aes_unauth (server_nonce, new_nonce, AES_DECRYPT);
in_ptr = (int *)(packet + 56);
in_end = (int *)(packet + len);
int l = prefetch_strlen ();
assert (l > 0);
l = pad_aes_decrypt (fetch_str (l), l, (char *) decrypt_buffer, DECRYPT_BUFFER_INTS * 4 - 16);
l = tgl_pad_aes_decrypt (fetch_str (l), l, (char *) decrypt_buffer, DECRYPT_BUFFER_INTS * 4 - 16);
assert (in_ptr == in_end);
assert (l >= 60);
assert (decrypt_buffer[5] == (int)CODE_server_DH_inner_data);
@ -579,12 +577,12 @@ static int process_dh_answer (struct connection *c, char *packet, int len) {
BIGNUM *y = BN_new ();
ensure_ptr (y);
ensure (BN_mod_exp (y, &dh_g, dh_power, &dh_prime, BN_ctx));
ensure (BN_mod_exp (y, &dh_g, dh_power, &dh_prime, tgl_state.BN_ctx));
out_bignum (y);
BN_free (y);
BN_init (&auth_key_num);
ensure (BN_mod_exp (&auth_key_num, &g_a, dh_power, &dh_prime, BN_ctx));
ensure (BN_mod_exp (&auth_key_num, &g_a, dh_power, &dh_prime, tgl_state.BN_ctx));
l = BN_num_bytes (&auth_key_num);
assert (l >= 250 && l <= 256);
assert (BN_bn2bin (&auth_key_num, (unsigned char *)D->auth_key));
@ -711,9 +709,9 @@ static int aes_encrypt_message (struct dc *DC, struct encrypted_message *enc) {
//printf ("enc_len is %d\n", enc_len);
vlogprintf (E_DEBUG, "sending message with sha1 %08x\n", *(int *)sha1_buffer);
memcpy (enc->msg_key, sha1_buffer + 4, 16);
init_aes_auth (DC->auth_key, enc->msg_key, AES_ENCRYPT);
tgl_init_aes_auth (DC->auth_key, enc->msg_key, AES_ENCRYPT);
//hexdump ((char *)enc, (char *)enc + enc_len + 24);
return pad_aes_encrypt ((char *) &enc->server_salt, enc_len, (char *) &enc->server_salt, MAX_MESSAGE_INTS * 4 + (MINSZ - UNENCSZ));
return tgl_pad_aes_encrypt ((char *) &enc->server_salt, enc_len, (char *) &enc->server_salt, MAX_MESSAGE_INTS * 4 + (MINSZ - UNENCSZ));
}
long long tglmp_encrypt_send_message (struct connection *c, int *msg, int msg_ints, int useful) {
@ -821,7 +819,7 @@ static void work_packed (struct connection *c, long long msg_id) {
int l = prefetch_strlen ();
char *s = fetch_str (l);
int total_out = tinflate (s, l, buf, MAX_PACKED_SIZE);
int total_out = tgl_inflate (s, l, buf, MAX_PACKED_SIZE);
int *end = in_ptr;
int *eend = in_end;
//assert (total_out % 4 == 0);
@ -933,8 +931,8 @@ static int process_rpc_message (struct connection *c UU, struct encrypted_messag
struct dc *DC = tgl_state.net_methods->get_dc (c);
assert (enc->auth_key_id == DC->auth_key_id);
assert (DC->auth_key_id);
init_aes_auth (DC->auth_key + 8, enc->msg_key, AES_DECRYPT);
int l = pad_aes_decrypt ((char *)&enc->server_salt, len - UNENCSZ, (char *)&enc->server_salt, len - UNENCSZ);
tgl_init_aes_auth (DC->auth_key + 8, enc->msg_key, AES_DECRYPT);
int l = tgl_pad_aes_decrypt ((char *)&enc->server_salt, len - UNENCSZ, (char *)&enc->server_salt, len - UNENCSZ);
assert (l == len - UNENCSZ);
//assert (enc->auth_key_id2 == enc->auth_key_id);
assert (!(enc->msg_len & 3) && enc->msg_len > 0 && enc->msg_len <= len - MINSZ && len - MINSZ - enc->msg_len <= 12);
@ -1089,7 +1087,7 @@ static int rpc_close (struct connection *c) {
#define RANDSEED_PASSWORD_FILENAME NULL
#define RANDSEED_PASSWORD_LENGTH 0
void tglmp_on_start (const char *key) {
prng_seed (RANDSEED_PASSWORD_FILENAME, RANDSEED_PASSWORD_LENGTH);
tgl_prng_seed (RANDSEED_PASSWORD_FILENAME, RANDSEED_PASSWORD_LENGTH);
if (key) {
if (rsa_load_public_key (key) < 0) {
@ -1103,7 +1101,7 @@ void tglmp_on_start (const char *key) {
exit (1);
}
}
pk_fingerprint = compute_rsa_key_fingerprint (pubKey);
pk_fingerprint = tgl_do_compute_rsa_key_fingerprint (pubKey);
}
//int auth_ok (void) {

View File

@ -40,6 +40,7 @@
#include "mtproto-common.h"
#include "include.h"
#include "tools.h"
#ifdef __MACH__
#include <mach/clock.h>
@ -47,12 +48,12 @@
#endif
int __packet_buffer[PACKET_BUFFER_SIZE], *packet_ptr;
int *packet_buffer = __packet_buffer + 16;
static int __packet_buffer[PACKET_BUFFER_SIZE];
int *tgl_packet_ptr;
int *tgl_packet_buffer = __packet_buffer + 16;
long long rsa_encrypted_chunks, rsa_decrypted_chunks;
static long long rsa_encrypted_chunks, rsa_decrypted_chunks;
BN_CTX *BN_ctx;
//int verbosity;
static int get_random_bytes (unsigned char *buf, int n) {
@ -105,9 +106,9 @@ static __inline__ unsigned long long rdtsc (void) {
}
#endif
void prng_seed (const char *password_filename, int password_length) {
void tgl_prng_seed (const char *password_filename, int password_length) {
struct timespec T;
my_clock_gettime (CLOCK_REALTIME, &T);
tgl_my_clock_gettime (CLOCK_REALTIME, &T);
RAND_add (&T, sizeof (T), 4.0);
#ifdef HAVE_RDTSC
unsigned long long r = rdtsc ();
@ -140,11 +141,11 @@ void prng_seed (const char *password_filename, int password_length) {
tfree_secure (a, password_length);
}
}
BN_ctx = BN_CTX_new ();
ensure_ptr (BN_ctx);
tgl_state.BN_ctx = BN_CTX_new ();
ensure_ptr (tgl_state.BN_ctx);
}
int serialize_bignum (BIGNUM *b, char *buffer, int maxlen) {
int tgl_serialize_bignum (BIGNUM *b, char *buffer, int maxlen) {
int itslen = BN_num_bytes (b);
int reqlen;
if (itslen < 254) {
@ -174,19 +175,19 @@ int serialize_bignum (BIGNUM *b, char *buffer, int maxlen) {
}
long long compute_rsa_key_fingerprint (RSA *key) {
long long tgl_do_compute_rsa_key_fingerprint (RSA *key) {
static char tempbuff[4096];
static unsigned char sha[20];
assert (key->n && key->e);
int l1 = serialize_bignum (key->n, tempbuff, 4096);
int l1 = tgl_serialize_bignum (key->n, tempbuff, 4096);
assert (l1 > 0);
int l2 = serialize_bignum (key->e, tempbuff + l1, 4096 - l1);
int l2 = tgl_serialize_bignum (key->e, tempbuff + l1, 4096 - l1);
assert (l2 > 0 && l1 + l2 <= 4096);
SHA1 ((unsigned char *)tempbuff, l1 + l2, sha);
return *(long long *)(sha + 12);
}
void out_cstring (const char *str, long len) {
void tgl_out_cstring (const char *str, long len) {
assert (len >= 0 && len < (1 << 24));
assert ((char *) packet_ptr + len + 8 < (char *) (packet_buffer + PACKET_BUFFER_SIZE));
char *dest = (char *) packet_ptr;
@ -204,7 +205,7 @@ void out_cstring (const char *str, long len) {
packet_ptr = (int *) dest;
}
void out_cstring_careful (const char *str, long len) {
void tgl_out_cstring_careful (const char *str, long len) {
assert (len >= 0 && len < (1 << 24));
assert ((char *) packet_ptr + len + 8 < (char *) (packet_buffer + PACKET_BUFFER_SIZE));
char *dest = (char *) packet_ptr;
@ -229,16 +230,16 @@ void out_cstring_careful (const char *str, long len) {
}
void out_data (const void *data, long len) {
void tgl_out_data (const void *data, long len) {
assert (len >= 0 && len < (1 << 24) && !(len & 3));
assert ((char *) packet_ptr + len + 8 < (char *) (packet_buffer + PACKET_BUFFER_SIZE));
memcpy (packet_ptr, data, len);
packet_ptr += len >> 2;
}
int *in_ptr, *in_end;
int *tgl_in_ptr, *tgl_in_end;
int fetch_bignum (BIGNUM *x) {
int tgl_fetch_bignum (BIGNUM *x) {
int l = prefetch_strlen ();
if (l < 0) {
return l;
@ -248,7 +249,7 @@ int fetch_bignum (BIGNUM *x) {
return l;
}
int pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *E) {
int tgl_pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *E) {
int pad = (255000 - from_len - 32) % 255 + 32;
int chunks = (from_len + pad) / 255;
int bits = BN_num_bits (N);
@ -263,7 +264,7 @@ int pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BI
rsa_encrypted_chunks += chunks;
for (i = 0; i < chunks; i++) {
BN_bin2bn ((unsigned char *) from, 255, &x);
assert (BN_mod_exp (&y, &x, E, N, BN_ctx) == 1);
assert (BN_mod_exp (&y, &x, E, N, tgl_state.BN_ctx) == 1);
unsigned l = 256 - BN_num_bytes (&y);
assert (l <= 256);
memset (to, 0, l);
@ -275,7 +276,7 @@ int pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BI
return chunks * 256;
}
int pad_rsa_decrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *D) {
int tgl_pad_rsa_decrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *D) {
if (from_len < 0 || from_len > 0x1000 || (from_len & 0xff)) {
return -1;
}
@ -290,7 +291,7 @@ int pad_rsa_decrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BI
for (i = 0; i < chunks; i++) {
++rsa_decrypted_chunks;
BN_bin2bn ((unsigned char *) from, 256, &x);
assert (BN_mod_exp (&y, &x, D, N, BN_ctx) == 1);
assert (BN_mod_exp (&y, &x, D, N, tgl_state.BN_ctx) == 1);
int l = BN_num_bytes (&y);
if (l > 255) {
BN_free (&x);
@ -307,10 +308,10 @@ int pad_rsa_decrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BI
return chunks * 255;
}
unsigned char aes_key_raw[32], aes_iv[32];
AES_KEY aes_key;
static unsigned char aes_key_raw[32], aes_iv[32];
static AES_KEY aes_key;
void init_aes_unauth (const char server_nonce[16], const char hidden_client_nonce[32], int encrypt) {
void tgl_init_aes_unauth (const char server_nonce[16], const char hidden_client_nonce[32], int encrypt) {
static unsigned char buffer[64], hash[20];
memcpy (buffer, hidden_client_nonce, 32);
memcpy (buffer + 32, server_nonce, 16);
@ -331,7 +332,7 @@ void init_aes_unauth (const char server_nonce[16], const char hidden_client_nonc
memset (aes_key_raw, 0, sizeof (aes_key_raw));
}
void init_aes_auth (char auth_key[192], char msg_key[16], int encrypt) {
void tgl_init_aes_auth (char auth_key[192], char msg_key[16], int encrypt) {
static unsigned char buffer[48], hash[20];
// sha1_a = SHA1 (msg_key + substr (auth_key, 0, 32));
// sha1_b = SHA1 (substr (auth_key, 32, 16) + msg_key + substr (auth_key, 48, 16));
@ -371,7 +372,7 @@ void init_aes_auth (char auth_key[192], char msg_key[16], int encrypt) {
memset (aes_key_raw, 0, sizeof (aes_key_raw));
}
int pad_aes_encrypt (char *from, int from_len, char *to, int size) {
int tgl_pad_aes_encrypt (char *from, int from_len, char *to, int size) {
int padded_size = (from_len + 15) & -16;
assert (from_len > 0 && padded_size <= size);
if (from_len < padded_size) {
@ -381,7 +382,7 @@ int pad_aes_encrypt (char *from, int from_len, char *to, int size) {
return padded_size;
}
int pad_aes_decrypt (char *from, int from_len, char *to, int size) {
int tgl_pad_aes_decrypt (char *from, int from_len, char *to, int size) {
if (from_len <= 0 || from_len > size || (from_len & 15)) {
return -1;
}

View File

@ -111,14 +111,17 @@ struct encrypted_message {
#pragma pack(pop)
BN_CTX *BN_ctx;
//BN_CTX *BN_ctx;
void prng_seed (const char *password_filename, int password_length);
int serialize_bignum (BIGNUM *b, char *buffer, int maxlen);
long long compute_rsa_key_fingerprint (RSA *key);
void tgl_prng_seed (const char *password_filename, int password_length);
int tgl_serialize_bignum (BIGNUM *b, char *buffer, int maxlen);
long long tgl_do_compute_rsa_key_fingerprint (RSA *key);
extern int *packet_buffer;
extern int *packet_ptr;
#define packet_buffer tgl_packet_buffer
#define packet_ptr tgl_packet_ptr
extern int *tgl_packet_buffer;
extern int *tgl_packet_ptr;
static inline void out_ints (const int *what, int len) {
assert (packet_ptr + len <= packet_buffer + PACKET_BUFFER_SIZE);
@ -143,21 +146,28 @@ static inline void clear_packet (void) {
packet_ptr = packet_buffer;
}
void out_cstring (const char *str, long len);
void out_cstring_careful (const char *str, long len);
void out_data (const void *data, long len);
void tgl_out_cstring (const char *str, long len);
void tgl_out_cstring_careful (const char *str, long len);
void tgl_out_data (const void *data, long len);
#define out_cstring tgl_out_cstring
#define out_cstring_careful tgl_out_cstring_careful
#define out_data tgl_out_data
static inline void out_string (const char *str) {
out_cstring (str, strlen (str));
}
static inline void out_bignum (BIGNUM *n) {
int l = serialize_bignum (n, (char *)packet_ptr, (PACKET_BUFFER_SIZE - (packet_ptr - packet_buffer)) * 4);
int l = tgl_serialize_bignum (n, (char *)packet_ptr, (PACKET_BUFFER_SIZE - (packet_ptr - packet_buffer)) * 4);
assert (l > 0);
packet_ptr += l >> 2;
}
extern int *in_ptr, *in_end;
#define in_ptr tgl_in_ptr
#define in_end tgl_in_end
extern int *tgl_in_ptr, *tgl_in_end;
//void fetch_pts (void);
//void fetch_qts (void);
@ -179,7 +189,6 @@ static inline int prefetch_strlen (void) {
}
}
extern int verbosity;
static inline char *fetch_str (int len) {
assert (len >= 0);
vlogprintf (E_DEBUG + 3, "fetch_string: len = %d\n", len);
@ -270,7 +279,8 @@ static inline long have_prefetch_ints (void) {
return in_end - in_ptr;
}
int fetch_bignum (BIGNUM *x);
int tgl_fetch_bignum (BIGNUM *x);
#define fetch_bignum tgl_fetch_bignum
static inline int fetch_int (void) {
assert (in_ptr + 1 <= in_end);
@ -335,18 +345,18 @@ static inline void fetch256 (void *buf) {
//int get_random_bytes (unsigned char *buf, int n);
int pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *E);
int pad_rsa_decrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *D);
int tgl_pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *E);
int tgl_pad_rsa_decrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *D);
extern long long rsa_encrypted_chunks, rsa_decrypted_chunks;
//extern long long rsa_encrypted_chunks, rsa_decrypted_chunks;
extern unsigned char aes_key_raw[32], aes_iv[32];
extern AES_KEY aes_key;
//extern unsigned char aes_key_raw[32], aes_iv[32];
//extern AES_KEY aes_key;
void init_aes_unauth (const char server_nonce[16], const char hidden_client_nonce[32], int encrypt);
void init_aes_auth (char auth_key[192], char msg_key[16], int encrypt);
int pad_aes_encrypt (char *from, int from_len, char *to, int size);
int pad_aes_decrypt (char *from, int from_len, char *to, int size);
void tgl_init_aes_unauth (const char server_nonce[16], const char hidden_client_nonce[32], int encrypt);
void tgl_init_aes_auth (char auth_key[192], char msg_key[16], int encrypt);
int tgl_pad_aes_encrypt (char *from, int from_len, char *to, int size);
int tgl_pad_aes_decrypt (char *from, int from_len, char *to, int size);
/*
static inline void hexdump_in (void) {
hexdump (in_ptr, in_end);
@ -360,5 +370,4 @@ static inline void hexdump_out (void) {
#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 1
#endif
void my_clock_gettime (int clock_id, struct timespec *T);
#endif

View File

@ -220,7 +220,7 @@ void tglq_query_result (long long id UU) {
fetch_int ();
int l = prefetch_strlen ();
char *s = fetch_str (l);
int total_out = tinflate (s, l, packed_buffer, MAX_PACKED_SIZE);
int total_out = tgl_inflate (s, l, packed_buffer, MAX_PACKED_SIZE);
end = in_ptr;
eend = in_end;
//assert (total_out % 4 == 0);
@ -2468,7 +2468,7 @@ static struct query_methods send_encr_request_methods = {
//int encr_root;
//unsigned char *encr_prime;
//int encr_param_version;
static BN_CTX *ctx;
//static BN_CTX *ctx;
void tgl_do_send_accept_encr_chat (struct tgl_secret_chat *E, unsigned char *random, void (*callback)(void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra) {
int i;
@ -2493,15 +2493,15 @@ void tgl_do_send_accept_encr_chat (struct tgl_secret_chat *E, unsigned char *ran
BIGNUM *g_a = BN_bin2bn (E->g_key, 256, 0);
ensure_ptr (g_a);
assert (tglmp_check_g (tgl_state.encr_prime, g_a) >= 0);
if (!ctx) {
ctx = BN_CTX_new ();
ensure_ptr (ctx);
}
//if (!ctx) {
// ctx = BN_CTX_new ();
// ensure_ptr (ctx);
//}
BIGNUM *p = BN_bin2bn (tgl_state.encr_prime, 256, 0);
ensure_ptr (p);
BIGNUM *r = BN_new ();
ensure_ptr (r);
ensure (BN_mod_exp (r, g_a, b, p, ctx));
ensure (BN_mod_exp (r, g_a, b, p, tgl_state.BN_ctx));
static unsigned char kk[256];
memset (kk, 0, sizeof (kk));
BN_bn2bin (r, kk);
@ -2520,7 +2520,7 @@ void tgl_do_send_accept_encr_chat (struct tgl_secret_chat *E, unsigned char *ran
out_long (E->access_hash);
ensure (BN_set_word (g_a, tgl_state.encr_root));
ensure (BN_mod_exp (r, g_a, b, p, ctx));
ensure (BN_mod_exp (r, g_a, b, p, tgl_state.BN_ctx));
static unsigned char buf[256];
memset (buf, 0, sizeof (buf));
BN_bn2bin (r, buf);
@ -2540,17 +2540,13 @@ void tgl_do_create_keys_end (struct tgl_secret_chat *U) {
BIGNUM *g_b = BN_bin2bn (U->g_key, 256, 0);
ensure_ptr (g_b);
assert (tglmp_check_g (tgl_state.encr_prime, g_b) >= 0);
if (!ctx) {
ctx = BN_CTX_new ();
ensure_ptr (ctx);
}
BIGNUM *p = BN_bin2bn (tgl_state.encr_prime, 256, 0);
ensure_ptr (p);
BIGNUM *r = BN_new ();
ensure_ptr (r);
BIGNUM *a = BN_bin2bn ((void *)U->key, 256, 0);
ensure_ptr (a);
ensure (BN_mod_exp (r, g_b, a, p, ctx));
ensure (BN_mod_exp (r, g_b, a, p, tgl_state.BN_ctx));
unsigned char *t = talloc (256);
memcpy (t, U->key, 256);
@ -2586,10 +2582,6 @@ void tgl_do_send_create_encr_chat (void *x, unsigned char *random, void (*callba
for (i = 0; i < 256; i++) {
random[i] ^= random_here[i];
}
if (!ctx) {
ctx = BN_CTX_new ();
ensure_ptr (ctx);
}
BIGNUM *a = BN_bin2bn (random, 256, 0);
ensure_ptr (a);
BIGNUM *p = BN_bin2bn (tgl_state.encr_prime, 256, 0);
@ -2603,7 +2595,7 @@ void tgl_do_send_create_encr_chat (void *x, unsigned char *random, void (*callba
BIGNUM *r = BN_new ();
ensure_ptr (r);
ensure (BN_mod_exp (r, g, a, p, ctx));
ensure (BN_mod_exp (r, g, a, p, tgl_state.BN_ctx));
BN_clear_free (a);

View File

@ -68,7 +68,7 @@ static tgl_peer_t *Peers[TGL_MAX_PEER_NUM];
char *create_print_name (tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4) {
char *tgls_default_create_print_name (tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4) {
const char *d[4];
d[0] = a1; d[1] = a2; d[2] = a3; d[3] = a4;
static char buf[10000];

View File

@ -23,7 +23,7 @@
#include "tgl-layout.h"
#include "tgl-fetch.h"
char *create_print_name (tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4);
char *tgls_default_create_print_name (tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4);
void tgls_free_user (struct tgl_user *U);

4
tgl.c
View File

@ -5,6 +5,7 @@
#include "tgl.h"
#include "tools.h"
#include "mtproto-client.h"
#include "structures.h"
#include "net.h"
#include <event2/event.h>
@ -44,6 +45,9 @@ void tgl_init (void) {
if (!tgl_state.net_methods) {
tgl_state.net_methods = &tgl_conn_methods;
}
if (!tgl_state.callback.create_print_name) {
tgl_state.callback.create_print_name = tgls_default_create_print_name;
}
tglmp_on_start (tgl_state.rsa_key);
}

25
tgl.h
View File

@ -6,14 +6,15 @@
#define TGL_MAX_DC_NUM 100
// JUST RANDOM STRING
#define TGL_BUILD "1828"
#define TGL_VERSION "0.9-beta"
#define TGL_VERSION "1.0-beta"
// Do not modify this structure, unless you know what you do
struct connection;
struct mtproto_methods;
struct session;
struct dc;
struct bingnum_ctx;
#define TGL_UPDATE_CREATED 1
#define TGL_UPDATE_DELETED 2
@ -31,6 +32,21 @@ struct dc;
#define TGL_UPDATE_MEMBERS 8192
#define TGL_UPDATE_ACCESS_HASH 16384
struct tgl_allocator {
void *(*talloc)(size_t size);
void *(*trealloc)(void *ptr, size_t old_size, size_t size);
void *(*talloc0)(size_t size);
char *(*tstrdup)(const char *s);
char *(*tstrndup)(const char *s, size_t n);
void (*tfree)(void *ptr, int size);
void (*tfree_str)(void *ptr);
void (*tfree_secure)(void *ptr, int size);
int (*tasprintf)(char **res, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
int (*tsnprintf)(char *buf, int len, const char *format, ...) __attribute__ ((format (printf, 3, 4)));
void (*tcheck)(void);
void (*texists)(void *ptr, int size);
};
struct tgl_update_callback {
void (*new_msg)(struct tgl_message *M);
void (*marked_read)(int num, struct tgl_message *list[]);
@ -59,6 +75,7 @@ struct tgl_update_callback {
void (*secret_chat_update)(struct tgl_secret_chat *C, unsigned flags);
void (*msg_receive)(struct tgl_message *M);
void (*our_id)(int id);
char *(*create_print_name) (tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4);
};
struct tgl_net_methods {
@ -81,6 +98,7 @@ struct tgl_net_methods {
#define TGL_LOCK_DIFF 1
// Do not modify this structure, unless you know what you do
struct tgl_state {
int our_id; // ID of logged in user
int encr_root;
@ -115,6 +133,9 @@ struct tgl_state {
struct event_base *ev_base;
char *rsa_key;
struct bignum_ctx *BN_ctx;
struct tgl_allocator allocator;
};
extern struct tgl_state tgl_state;

32
tools.c
View File

@ -40,10 +40,10 @@
#define RES_PRE 8
#define RES_AFTER 8
#define MAX_BLOCKS 1000000
void *blocks[MAX_BLOCKS];
void *free_blocks[MAX_BLOCKS];
int used_blocks;
int free_blocks_cnt;
static void *blocks[MAX_BLOCKS];
static void *free_blocks[MAX_BLOCKS];
static int used_blocks;
static int free_blocks_cnt;
#endif
@ -59,11 +59,6 @@ extern int verbosity;
long long total_allocated_bytes;
static void out_of_memory (void) {
fprintf (stderr, "Out of memory\n");
exit (1);
}
int tsnprintf (char *buf, int len, const char *format, ...) {
va_list ap;
va_start (ap, format);
@ -201,21 +196,8 @@ char *tstrndup (const char *s, size_t n) {
#endif
}
void ensure (int r) {
if (!r) {
logprintf ("Open SSL error\n");
ERR_print_errors_fp (stderr);
assert (0);
}
}
void ensure_ptr (void *p) {
if (p == NULL) {
out_of_memory ();
}
}
int tinflate (void *input, int ilen, void *output, int olen) {
int tgl_inflate (void *input, int ilen, void *output, int olen) {
z_stream strm;
memset (&strm, 0, sizeof (strm));
assert (inflateInit2 (&strm, 16 + MAX_WBITS) == Z_OK);
@ -282,7 +264,7 @@ void texists (void *ptr, int size) {
}
#endif
void my_clock_gettime (int clock_id, struct timespec *T) {
void tgl_my_clock_gettime (int clock_id, struct timespec *T) {
#ifdef __MACH__
// We are ignoring MONOTONIC and hope time doesn't go back too often
clock_serv_t cclock;
@ -299,7 +281,7 @@ void my_clock_gettime (int clock_id, struct timespec *T) {
double tglt_get_double_time (void) {
struct timespec tv;
my_clock_gettime (CLOCK_REALTIME, &tv);
tgl_my_clock_gettime (CLOCK_REALTIME, &tv);
return tv.tv_sec + 1e-9 * tv.tv_nsec;
}

30
tools.h
View File

@ -19,6 +19,9 @@
#ifndef __TOOLS_H__
#define __TOOLS_H__
#include <time.h>
#include <openssl/err.h>
#include <assert.h>
double tglt_get_double_time (void);
@ -27,9 +30,28 @@ void *trealloc (void *ptr, size_t old_size, size_t size);
void *talloc0 (size_t size);
char *tstrdup (const char *s);
char *tstrndup (const char *s, size_t n);
int tinflate (void *input, int ilen, void *output, int olen);
void ensure (int r);
void ensure_ptr (void *p);
int tgl_inflate (void *input, int ilen, void *output, int olen);
//void ensure (int r);
//void ensure_ptr (void *p);
static inline void out_of_memory (void) {
fprintf (stderr, "Out of memory\n");
exit (1);
}
static inline void ensure (int r) {
if (!r) {
fprintf (stderr, "Open SSL error\n");
ERR_print_errors_fp (stderr);
assert (0);
}
}
static inline void ensure_ptr (void *p) {
if (p == NULL) {
out_of_memory ();
}
}
void tfree (void *ptr, int size);
void tfree_str (void *ptr);
@ -40,9 +62,11 @@ int tsnprintf (char *buf, int len, const char *format, ...) __attribute__ ((form
int tasprintf (char **res, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
void tglt_secure_random (void *s, int l);
void tgl_my_clock_gettime (int clock_id, struct timespec *T);
#ifdef DEBUG
void tcheck (void);
void texists (void *ptr, int size);
#endif
#endif