Skip to content

Commit

Permalink
Split along bn.h (skip when bisecting).
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWiederhake committed Oct 1, 2015
1 parent 509919e commit b2fa136
Show file tree
Hide file tree
Showing 15 changed files with 262 additions and 157 deletions.
4 changes: 2 additions & 2 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ AUTO=auto
OBJ=objs
LIB=libs
EXE=bin
DIR_LIST=${DEP} ${AUTO} ${EXE} ${OBJ} ${LIB} ${DEP}/auto ${OBJ}/auto
DIR_LIST=${DEP} ${DEP}/crypto ${AUTO} ${EXE} ${OBJ} ${OBJ}/crypto ${LIB} ${DEP}/auto ${OBJ}/auto

LIB_LIST=${LIB}/libtgl.a ${LIB}/libtgl.so

TGL_OBJECTS=${OBJ}/mtproto-common.o ${OBJ}/mtproto-client.o ${OBJ}/queries.o ${OBJ}/structures.o ${OBJ}/binlog.o ${OBJ}/tgl.o ${OBJ}/updates.o ${OBJ}/tg-mime-types.o ${OBJ}/mtproto-utils.o @EXTRA_OBJECTS@
TGL_OBJECTS=${OBJ}/mtproto-common.o ${OBJ}/mtproto-client.o ${OBJ}/queries.o ${OBJ}/structures.o ${OBJ}/binlog.o ${OBJ}/tgl.o ${OBJ}/updates.o ${OBJ}/tg-mime-types.o ${OBJ}/mtproto-utils.o ${OBJ}/crypto/bn_openssl.o ${OBJ}/crypto/bn_altern.o @EXTRA_OBJECTS@
TGL_OBJECTS_AUTO=${OBJ}/auto/auto-skip.o ${OBJ}/auto/auto-fetch.o ${OBJ}/auto/auto-store.o ${OBJ}/auto/auto-autocomplete.o ${OBJ}/auto/auto-types.o ${OBJ}/auto/auto-fetch-ds.o ${OBJ}/auto/auto-free-ds.o ${OBJ}/auto/auto-store-ds.o ${OBJ}/auto/auto-print-ds.o
TLD_OBJECTS=${OBJ}/dump-tl-file.o
GENERATE_OBJECTS=${OBJ}/generate.o
Expand Down
8 changes: 4 additions & 4 deletions binlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <openssl/bn.h>
#include "crypto/bn.h"

#include "tgl-binlog.h"
#include "mtproto-common.h"
Expand Down Expand Up @@ -130,14 +130,14 @@ void bl_do_set_our_id (struct tgl_state *TLS, tgl_peer_id_t id) /* {{{ */ {
/* }}} */

void bl_do_set_dh_params (struct tgl_state *TLS, int root, unsigned char prime[], int version) /* {{{ */ {
if (TLS->encr_prime) { tfree (TLS->encr_prime, 256); BN_free (TLS->encr_prime_bn); }
if (TLS->encr_prime) { tfree (TLS->encr_prime, 256); TGLC_bn_free (TLS->encr_prime_bn); }

TLS->encr_root = root;

TLS->encr_prime = talloc (256);
memcpy (TLS->encr_prime, prime, 256);
TLS->encr_prime_bn = BN_new ();
BN_bin2bn ((void *)TLS->encr_prime, 256, TLS->encr_prime_bn);
TLS->encr_prime_bn = TGLC_bn_new ();
TGLC_bn_bin2bn ((void *)TLS->encr_prime, 256, TLS->encr_prime_bn);

TLS->encr_param_version = version;

Expand Down
11 changes: 7 additions & 4 deletions crypto/bn.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
#ifndef __TGL_CRYPTO_BN_H__
#define __TGL_CRYPTO_BN_H__

struct TGLC_bn_ctx;
struct TGLC_bn;
typedef struct TGLC_bn_ctx TGLC_bn_ctx;
typedef struct TGLC_bn TGLC_bn;

TGLC_bn_ctx *TGLC_bn_ctx_new (void);
void *TGLC_bn_ctx_free (TGLC_bn_ctx* ctx);
void TGLC_bn_ctx_free (TGLC_bn_ctx* ctx);

TGLC_bn *TGLC_bn_new (void);
void TGLC_bn_free (TGLC_bn *a);
void TGLC_bn_clear_free (TGLC_bn *a);
Expand All @@ -35,10 +36,12 @@ int TGLC_bn_bn2bin (const TGLC_bn *a, unsigned char *to);
TGLC_bn * TGLC_bn_bin2bn(const unsigned char *s, int len, TGLC_bn *ret);
int TGLC_bn_set_word (TGLC_bn *a, unsigned long w);
unsigned long TGLC_bn_get_word (const TGLC_bn *a);
int TGLC_bn_num_bytes (const TGLC_bn *a);
int TGLC_bn_num_bits (const TGLC_bn *a);
int TGLC_bn_sub (TGLC_bn *r, const TGLC_bn *a, const TGLC_bn *b);
int TGLC_bn_div (TGLC_bn *dv, TGLC_bn *rem, const TGLC_bn *a, const TGLC_bn *d, TGLC_bn_ctx *ctx);
int TGLC_bn_mod_exp (TGLC_bn *r, const TGLC_bn *a, const TGLC_bn *p, const TGLC_bn *m, TGLC_bn_ctx *ctx);

#define TGLC_bn_num_bytes(a) ((TGLC_bn_num_bits(a)+7)/8)
#define TGLC_bn_mod(rem,m,d,ctx) TGLC_bn_div(NULL,(rem),(m),(d),(ctx))

#endif
65 changes: 63 additions & 2 deletions crypto/bn_openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,69 @@
#include <openssl/bn.h>

#include "bn.h"
#include "meta.h"

/* FIXME */
#error Not yet implemented: OpenSSL-dependent defines for bn
TGLC_WRAPPER_ASSOC(bn_ctx,BN_CTX)
TGLC_WRAPPER_ASSOC(bn,BIGNUM)

TGLC_bn_ctx *TGLC_bn_ctx_new (void) {
return wrap_bn_ctx (BN_CTX_new ());
}

void TGLC_bn_ctx_free (TGLC_bn_ctx* ctx) {
BN_CTX_free (unwrap_bn_ctx (ctx));
}

TGLC_bn *TGLC_bn_new (void) {
return wrap_bn (BN_new ());
}

void TGLC_bn_free (TGLC_bn *a) {
BN_free (unwrap_bn (a));
}

void TGLC_bn_clear_free (TGLC_bn *a) {
BN_clear_free (unwrap_bn (a));
}

int TGLC_bn_cmp (const TGLC_bn *a, const TGLC_bn *b) {
return BN_cmp (unwrap_bn (a), unwrap_bn (b));
}

int TGLC_bn_is_prime (const TGLC_bn *a, int checks, void (*callback) (int, int, void *), TGLC_bn_ctx *ctx, void *cb_arg) {
return BN_is_prime (unwrap_bn (a), checks, callback, unwrap_bn_ctx (ctx), cb_arg);
}

int TGLC_bn_bn2bin (const TGLC_bn *a, unsigned char *to) {
return BN_bn2bin (unwrap_bn (a), to);
}

TGLC_bn * TGLC_bn_bin2bn(const unsigned char *s, int len, TGLC_bn *ret) {
return wrap_bn (BN_bin2bn (s, len, unwrap_bn (ret)));
}

int TGLC_bn_set_word (TGLC_bn *a, unsigned long w) {
return BN_set_word (unwrap_bn (a), w);
}

unsigned long TGLC_bn_get_word (const TGLC_bn *a) {
return BN_get_word (unwrap_bn (a));
}

int TGLC_bn_num_bits (const TGLC_bn *a) {
return BN_num_bits (unwrap_bn (a));
}

int TGLC_bn_sub (TGLC_bn *r, const TGLC_bn *a, const TGLC_bn *b) {
return BN_sub (unwrap_bn (r), unwrap_bn (a), unwrap_bn (b));
}

int TGLC_bn_div (TGLC_bn *dv, TGLC_bn *rem, const TGLC_bn *a, const TGLC_bn *d, TGLC_bn_ctx *ctx) {
return BN_div (unwrap_bn (dv), unwrap_bn (rem), unwrap_bn (a), unwrap_bn (d), unwrap_bn_ctx (ctx));
}

int TGLC_bn_mod_exp (TGLC_bn *r, const TGLC_bn *a, const TGLC_bn *p, const TGLC_bn *m, TGLC_bn_ctx *ctx) {
return BN_mod_exp (unwrap_bn (r), unwrap_bn (a), unwrap_bn (p), unwrap_bn (m), unwrap_bn_ctx (ctx));
}

#endif
39 changes: 39 additions & 0 deletions crypto/meta.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef CRYPTO_META_H_
#define CRYPTO_META_H_

#include <assert.h>

/* All this wrapping/unwrapping solves a fundamental problem:
* - The libtgl implementation wants access to the TGLC_* types.
* - The libtgl implementation should not see any other definitions from any
* external crypto library (to make sure no symbol slips "through" during the
* OpenSSL-to-gcrypt transition).
* - Most TGLC_* types (3 out of 4 types) are exclusively used as pointers
* throughout the libtgl source, in a way to allow for incomplete types.
* - The CORE type (e.g. BIGNUM from <openssl/bn.h>) may be incomplete,
* even for tglc.
* This means the standard approaches don't work:
* - TGLC_NAME can't be a typedef to CORE, since CORE shouldn't even be visible
* from libtgl.
* - TGLC_NAME can't be a pointer to CORE, same reason. Also, it would require
* a significant amount of rewriting (error-prone work).
* - TGLC_NAME can't be a void pointer. Retain type checking!
* - So TGLC_NAME *must* be an incomplete custom struct.
* However, this ensues the following ugliness.
*
* The standard doesn't explicitly allow it, but there's a pretty good argument
* that casting ptr-to-some-struct to ptr-to-other-struct is *probably* okay for
* most compilers: http://stackoverflow.com/a/8702750/3070326
*/
#define TGLC_WRAPPER_ASSOC(NAME,CORE) \
static TGLC_ ## NAME *wrap_ ## NAME (const CORE *p); \
static CORE *unwrap_ ## NAME (const TGLC_ ## NAME *p) { \
assert (wrap_ ## NAME); \
return (CORE *)p; \
} \
static TGLC_ ## NAME *wrap_ ## NAME (const CORE *p) { \
assert (unwrap_ ## NAME); \
return (TGLC_ ## NAME *)p; \
}

#endif /* CRYPTO_META_H_ */
50 changes: 25 additions & 25 deletions mtproto-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,11 @@ static int send_req_pq_temp_packet (struct tgl_state *TLS, struct connection *c)
// req_DH_params#d712e4be nonce:int128 server_nonce:int128 p:string q:string public_key_fingerprint:long encrypted_data:string = Server_DH_Params;
// p_q_inner_data#83c95aec pq:string p:string q:string nonce:int128 server_nonce:int128 new_nonce:int256 = P_Q_inner_data;
// p_q_inner_data_temp#3c6a84d4 pq:string p:string q:string nonce:int128 server_nonce:int128 new_nonce:int256 expires_in:int = P_Q_inner_data;
static void send_req_dh_packet (struct tgl_state *TLS, struct connection *c, BIGNUM *pq, int temp_key) {
static void send_req_dh_packet (struct tgl_state *TLS, struct connection *c, TGLC_bn *pq, int temp_key) {
struct tgl_dc *DC = TLS->net_methods->get_dc (c);

BIGNUM *p = BN_new ();
BIGNUM *q = BN_new ();
TGLC_bn *p = TGLC_bn_new ();
TGLC_bn *q = TGLC_bn_new ();
assert (bn_factorize (pq, p, q) >= 0);

clear_packet ();
Expand Down Expand Up @@ -302,8 +302,8 @@ static void send_req_dh_packet (struct tgl_state *TLS, struct connection *c, BIG
out_long (TLS->rsa_key_fingerprint[DC->rsa_key_idx]);
out_cstring ((char *) encrypt_buffer, l);

BN_free (p);
BN_free (q);
TGLC_bn_free (p);
TGLC_bn_free (q);
DC->state = temp_key ? st_reqdh_sent_temp : st_reqdh_sent;
rpc_send_packet (TLS, c);
}
Expand All @@ -312,7 +312,7 @@ static void send_req_dh_packet (struct tgl_state *TLS, struct connection *c, BIG
/* {{{ SEND DH PARAMS */
// set_client_DH_params#f5045f1f nonce:int128 server_nonce:int128 encrypted_data:string = Set_client_DH_params_answer;
// client_DH_inner_data#6643b654 nonce:int128 server_nonce:int128 retry_id:long g_b:string = Client_DH_Inner_Data
static void send_dh_params (struct tgl_state *TLS, struct connection *c, BIGNUM *dh_prime, BIGNUM *g_a, int g, int temp_key) {
static void send_dh_params (struct tgl_state *TLS, struct connection *c, TGLC_bn *dh_prime, TGLC_bn *g_a, int g, int temp_key) {
struct tgl_dc *DC = TLS->net_methods->get_dc (c);

clear_packet ();
Expand All @@ -322,34 +322,34 @@ static void send_dh_params (struct tgl_state *TLS, struct connection *c, BIGNUM
out_ints ((int *) DC->server_nonce, 4);
out_long (0);

BIGNUM *dh_g = BN_new ();
ensure (BN_set_word (dh_g, g));
TGLC_bn *dh_g = TGLC_bn_new ();
ensure (TGLC_bn_set_word (dh_g, g));

static unsigned char s_power[256];
tglt_secure_random (s_power, 256);
BIGNUM *dh_power = BN_bin2bn ((unsigned char *)s_power, 256, 0);
TGLC_bn *dh_power = TGLC_bn_bin2bn ((unsigned char *)s_power, 256, 0);
ensure_ptr (dh_power);

BIGNUM *y = BN_new ();
TGLC_bn *y = TGLC_bn_new ();
ensure_ptr (y);
ensure (BN_mod_exp (y, dh_g, dh_power, dh_prime, TLS->BN_ctx));
ensure (TGLC_bn_mod_exp (y, dh_g, dh_power, dh_prime, TLS->TGLC_bn_ctx));
out_bignum (y);
BN_free (y);
TGLC_bn_free (y);

BIGNUM *auth_key_num = BN_new ();
ensure (BN_mod_exp (auth_key_num, g_a, dh_power, dh_prime, TLS->BN_ctx));
int l = BN_num_bytes (auth_key_num);
TGLC_bn *auth_key_num = TGLC_bn_new ();
ensure (TGLC_bn_mod_exp (auth_key_num, g_a, dh_power, dh_prime, TLS->TGLC_bn_ctx));
int l = TGLC_bn_num_bytes (auth_key_num);
assert (l >= 250 && l <= 256);
assert (BN_bn2bin (auth_key_num, (unsigned char *)(temp_key ? DC->temp_auth_key : DC->auth_key)));
assert (TGLC_bn_bn2bin (auth_key_num, (unsigned char *)(temp_key ? DC->temp_auth_key : DC->auth_key)));
if (l < 256) {
char *key = temp_key ? DC->temp_auth_key : DC->auth_key;
memmove (key + 256 - l, key, l);
memset (key, 0, 256 - l);
}

BN_free (dh_power);
BN_free (auth_key_num);
BN_free (dh_g);
TGLC_bn_free (dh_power);
TGLC_bn_free (auth_key_num);
TGLC_bn_free (dh_g);

sha1 ((unsigned char *) (packet_buffer + 5), (packet_ptr - packet_buffer - 5) * 4, (unsigned char *) packet_buffer);

Expand Down Expand Up @@ -395,7 +395,7 @@ static int process_respq_answer (struct tgl_state *TLS, struct connection *c, ch
}
fetch_ints (DC->server_nonce, 4);

BIGNUM *pq = BN_new ();
TGLC_bn *pq = TGLC_bn_new ();
assert (fetch_bignum (pq) >= 0);

assert (fetch_int () == CODE_vector);
Expand Down Expand Up @@ -424,7 +424,7 @@ static int process_respq_answer (struct tgl_state *TLS, struct connection *c, ch

send_req_dh_packet (TLS, c, pq, temp_key);

BN_free (pq);
TGLC_bn_free (pq);
return 1;
}
/* }}} */
Expand Down Expand Up @@ -506,8 +506,8 @@ static int process_dh_answer (struct tgl_state *TLS, struct connection *c, char
assert (!memcmp (tmp, DC->server_nonce, 16));
int g = fetch_int ();

BIGNUM *dh_prime = BN_new ();
BIGNUM *g_a = BN_new ();
TGLC_bn *dh_prime = TGLC_bn_new ();
TGLC_bn *g_a = TGLC_bn_new ();
assert (fetch_bignum (dh_prime) > 0);
assert (fetch_bignum (g_a) > 0);

Expand Down Expand Up @@ -539,8 +539,8 @@ static int process_dh_answer (struct tgl_state *TLS, struct connection *c, char

send_dh_params (TLS, c, dh_prime, g_a, g, temp_key);

BN_free (dh_prime);
BN_free (g_a);
TGLC_bn_free (dh_prime);
TGLC_bn_free (g_a);

return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion mtproto-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#ifndef __MTPROTO_CLIENT_H__
#define __MTPROTO_CLIENT_H__
//#include "net.h"
#include <openssl/bn.h>
#include "crypto/bn.h"
//void on_start (void);
//..long long encrypt_send_message (struct connection *c, int *msg, int msg_ints, int useful);
//void dc_authorize (struct tgl_dc *DC);
Expand Down
Loading

0 comments on commit b2fa136

Please sign in to comment.