diff options
author | Ben Wiederhake <BenWiederhake.GitHub@gmx> | 2015-09-25 12:00:10 +0300 |
---|---|---|
committer | Ben Wiederhake <BenWiederhake.GitHub@gmx> | 2015-10-01 21:59:53 +0300 |
commit | 320b4de16094d41dd8555e01226a514bcab7f3ba (patch) | |
tree | 7d3d8e6039c357e75cd0053f5cfe675ff3100960 | |
parent | afedd63ff5028d2e9cb31d5f0aeae3106413b94c (diff) |
Split along aes.h.
-rw-r--r-- | Makefile.in | 2 | ||||
-rw-r--r-- | crypto/aes.h | 18 | ||||
-rw-r--r-- | crypto/aes_openssl.c | 18 | ||||
-rw-r--r-- | mtproto-client.c | 10 | ||||
-rw-r--r-- | mtproto-common.c | 21 | ||||
-rw-r--r-- | mtproto-common.h | 3 | ||||
-rw-r--r-- | queries-encrypted.c | 6 | ||||
-rw-r--r-- | queries.c | 14 | ||||
-rw-r--r-- | structures.c | 8 |
9 files changed, 64 insertions, 36 deletions
diff --git a/Makefile.in b/Makefile.in index dac2fb6..cec7a33 100644 --- a/Makefile.in +++ b/Makefile.in @@ -19,7 +19,7 @@ DIR_LIST=${DEP} ${DEP}/crypto ${AUTO} ${EXE} ${OBJ} ${OBJ}/crypto ${LIB} ${DEP}/ 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 ${OBJ}/crypto/bn_openssl.o ${OBJ}/crypto/bn_altern.o ${OBJ}/crypto/rsa_pem_openssl.o ${OBJ}/crypto/rsa_pem_altern.o ${OBJ}/crypto/md5_openssl.o ${OBJ}/crypto/md5_altern.o ${OBJ}/crypto/sha_openssl.o ${OBJ}/crypto/sha_altern.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 ${OBJ}/crypto/rsa_pem_openssl.o ${OBJ}/crypto/rsa_pem_altern.o ${OBJ}/crypto/md5_openssl.o ${OBJ}/crypto/md5_altern.o ${OBJ}/crypto/sha_openssl.o ${OBJ}/crypto/sha_altern.o ${OBJ}/crypto/aes_openssl.o ${OBJ}/crypto/aes_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 diff --git a/crypto/aes.h b/crypto/aes.h index 31f3185..a271316 100644 --- a/crypto/aes.h +++ b/crypto/aes.h @@ -21,6 +21,22 @@ #ifndef __TGL_CRYPTO_AES_H__ #define __TGL_CRYPTO_AES_H__ -/* Declarations go here. */ +#include <stddef.h> /* size_t */ + +#include "crypto-config.h" + +typedef struct TGLC_aes_key { + char _dummy[ +#ifdef TGL_AVOID_OPENSSL_AES +#error Not specified! +#else + 244 +#endif + ]; +} TGLC_aes_key; + +int TGLC_aes_set_encrypt_key (const unsigned char *userKey, const int bits, TGLC_aes_key *key); +int TGLC_aes_set_decrypt_key (const unsigned char *userKey, const int bits, TGLC_aes_key *key); +void TGLC_aes_ige_encrypt (const unsigned char *in, unsigned char *out, size_t length, const TGLC_aes_key *key, unsigned char *ivec, const int enc); #endif diff --git a/crypto/aes_openssl.c b/crypto/aes_openssl.c index b979979..d084532 100644 --- a/crypto/aes_openssl.c +++ b/crypto/aes_openssl.c @@ -25,8 +25,22 @@ #include <openssl/aes.h> #include "aes.h" +#include "meta.h" -/* FIXME */ -#error Not yet implemented: OpenSSL-dependent defines for aes +typedef char check_struct_sizes[(sizeof (AES_KEY) == sizeof (TGLC_aes_key)) - 1]; + +TGLC_WRAPPER_ASSOC(aes_key,AES_KEY) + +int TGLC_aes_set_encrypt_key (const unsigned char *userKey, const int bits, TGLC_aes_key *key) { + return AES_set_encrypt_key(userKey, bits, unwrap_aes_key (key)); +} + +int TGLC_aes_set_decrypt_key (const unsigned char *userKey, const int bits, TGLC_aes_key *key) { + return AES_set_decrypt_key(userKey, bits, unwrap_aes_key (key)); +} + +void TGLC_aes_ige_encrypt (const unsigned char *in, unsigned char *out, size_t length, const TGLC_aes_key *key, unsigned char *ivec, const int enc) { + AES_ige_encrypt (in, out, length, unwrap_aes_key (key), ivec, enc); +} #endif diff --git a/mtproto-client.c b/mtproto-client.c index cd55b87..280bfcc 100644 --- a/mtproto-client.c +++ b/mtproto-client.c @@ -144,7 +144,7 @@ static int encrypt_packet_buffer (struct tgl_state *TLS, struct tgl_dc *DC) { } static int encrypt_packet_buffer_aes_unauth (const char server_nonce[16], const char hidden_client_nonce[32]) { - tgl_init_aes_unauth (server_nonce, hidden_client_nonce, AES_ENCRYPT); + tgl_init_aes_unauth (server_nonce, hidden_client_nonce, 1); return tgl_pad_aes_encrypt ((char *) packet_buffer, (packet_ptr - packet_buffer) * 4, (char *) encrypt_buffer, ENCRYPT_BUFFER_INTS * 4); } @@ -469,7 +469,7 @@ static int process_dh_answer (struct tgl_state *TLS, struct connection *c, char return -1; } - tgl_init_aes_unauth (DC->server_nonce, DC->new_nonce, AES_DECRYPT); + tgl_init_aes_unauth (DC->server_nonce, DC->new_nonce, 0); int l = prefetch_strlen (); assert (l >= 0); @@ -738,7 +738,7 @@ static int aes_encrypt_message (struct tgl_state *TLS, char *key, struct encrypt TGLC_sha1 ((unsigned char *) &enc->server_salt, enc_len, sha1_buffer); vlogprintf (E_DEBUG, "sending message with sha1 %08x\n", *(int *)sha1_buffer); memcpy (enc->msg_key, sha1_buffer + 4, 16); - tgl_init_aes_auth (key, enc->msg_key, AES_ENCRYPT); + tgl_init_aes_auth (key, enc->msg_key, 1); return tgl_pad_aes_encrypt ((char *) &enc->server_salt, enc_len, (char *) &enc->server_salt, MAX_MESSAGE_INTS * 4 + (MINSZ - UNENCSZ)); } @@ -1072,11 +1072,11 @@ static int process_rpc_message (struct tgl_state *TLS, struct connection *c, str if (enc->auth_key_id == DC->temp_auth_key_id) { assert (enc->auth_key_id == DC->temp_auth_key_id); assert (DC->temp_auth_key_id); - tgl_init_aes_auth (DC->temp_auth_key + 8, enc->msg_key, AES_DECRYPT); + tgl_init_aes_auth (DC->temp_auth_key + 8, enc->msg_key, 0); } else { assert (enc->auth_key_id == DC->auth_key_id); assert (DC->auth_key_id); - tgl_init_aes_auth (DC->auth_key + 8, enc->msg_key, AES_DECRYPT); + tgl_init_aes_auth (DC->auth_key + 8, enc->msg_key, 0); } int l = tgl_pad_aes_decrypt ((char *)&enc->server_salt, len - UNENCSZ, (char *)&enc->server_salt, len - UNENCSZ); diff --git a/mtproto-common.c b/mtproto-common.c index 0ea2dd2..eee5d17 100644 --- a/mtproto-common.c +++ b/mtproto-common.c @@ -33,6 +33,7 @@ #include <fcntl.h> #include <sys/types.h> #include <netdb.h> +#include "crypto/aes.h" #include "crypto/rand.h" #include "crypto/sha.h" @@ -308,7 +309,7 @@ int tgl_pad_rsa_decrypt (struct tgl_state *TLS, char *from, int from_len, char * } static unsigned char aes_key_raw[32], aes_iv[32]; -static AES_KEY aes_key; +static TGLC_aes_key aes_key; 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]; @@ -323,10 +324,10 @@ void tgl_init_aes_unauth (const char server_nonce[16], const char hidden_client_ memcpy (aes_key_raw + 20, hash, 12); memcpy (aes_iv, hash + 12, 8); memcpy (aes_iv + 28, hidden_client_nonce, 4); - if (encrypt == AES_ENCRYPT) { - AES_set_encrypt_key (aes_key_raw, 32*8, &aes_key); + if (encrypt) { + TGLC_aes_set_encrypt_key (aes_key_raw, 32*8, &aes_key); } else { - AES_set_decrypt_key (aes_key_raw, 32*8, &aes_key); + TGLC_aes_set_decrypt_key (aes_key_raw, 32*8, &aes_key); } memset (aes_key_raw, 0, sizeof (aes_key_raw)); } @@ -363,10 +364,10 @@ void tgl_init_aes_auth (char auth_key[192], char msg_key[16], int encrypt) { TGLC_sha1 (buffer, 48, hash); memcpy (aes_iv + 24, hash, 8); - if (encrypt == AES_ENCRYPT) { - AES_set_encrypt_key (aes_key_raw, 32*8, &aes_key); + if (encrypt) { + TGLC_aes_set_encrypt_key (aes_key_raw, 32*8, &aes_key); } else { - AES_set_decrypt_key (aes_key_raw, 32*8, &aes_key); + TGLC_aes_set_decrypt_key (aes_key_raw, 32*8, &aes_key); } memset (aes_key_raw, 0, sizeof (aes_key_raw)); } @@ -377,7 +378,7 @@ int tgl_pad_aes_encrypt (char *from, int from_len, char *to, int size) { if (from_len < padded_size) { assert (TGLC_rand_pseudo_bytes ((unsigned char *) from + from_len, padded_size - from_len) >= 0); } - AES_ige_encrypt ((unsigned char *) from, (unsigned char *) to, padded_size, &aes_key, aes_iv, AES_ENCRYPT); + TGLC_aes_ige_encrypt ((unsigned char *) from, (unsigned char *) to, padded_size, &aes_key, aes_iv, 1); return padded_size; } @@ -385,8 +386,6 @@ 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; } - AES_ige_encrypt ((unsigned char *) from, (unsigned char *) to, from_len, &aes_key, aes_iv, AES_DECRYPT); + TGLC_aes_ige_encrypt ((unsigned char *) from, (unsigned char *) to, from_len, &aes_key, aes_iv, 0); return from_len; } - - diff --git a/mtproto-common.h b/mtproto-common.h index a1f641b..d6783c6 100644 --- a/mtproto-common.h +++ b/mtproto-common.h @@ -24,7 +24,6 @@ #include <string.h> #include "crypto/rsa_pem.h" #include "crypto/bn.h" -#include <openssl/aes.h> #include <stdio.h> #include <assert.h> @@ -363,7 +362,7 @@ int tgl_pad_rsa_decrypt (struct tgl_state *TLS, char *from, int from_len, char * //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 TGLC_aes_key aes_key; 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); diff --git a/queries-encrypted.c b/queries-encrypted.c index 76ebd9a..4812692 100644 --- a/queries-encrypted.c +++ b/queries-encrypted.c @@ -44,9 +44,9 @@ static char *encrypt_decrypted_message (struct tgl_secret_chat *E) { memcpy (iv + 20, sha1c_buffer + 16, 4); memcpy (iv + 24, sha1d_buffer + 0, 8); - AES_KEY aes_key; - AES_set_encrypt_key (key, 256, &aes_key); - AES_ige_encrypt ((void *)encr_ptr, (void *)encr_ptr, 4 * (encr_end - encr_ptr), &aes_key, iv, 1); + TGLC_aes_key aes_key; + TGLC_aes_set_encrypt_key (key, 256, &aes_key); + TGLC_aes_ige_encrypt ((void *)encr_ptr, (void *)encr_ptr, 4 * (encr_end - encr_ptr), &aes_key, iv, 1); memset (&aes_key, 0, sizeof (aes_key)); return (void *)msg_key; @@ -44,7 +44,7 @@ //#include "net.h" #include "crypto/bn.h" #include "crypto/rand.h" -#include <openssl/aes.h> +#include "crypto/aes.h" #include "crypto/sha.h" #include "crypto/md5.h" @@ -1719,9 +1719,9 @@ static void send_part (struct tgl_state *TLS, struct send_file *f, void *callbac x = (x + 15) & ~15; } - AES_KEY aes_key; - AES_set_encrypt_key (f->key, 256, &aes_key); - AES_ige_encrypt ((void *)buf, (void *)buf, x, &aes_key, f->iv, 1); + TGLC_aes_key aes_key; + TGLC_aes_set_encrypt_key (f->key, 256, &aes_key); + TGLC_aes_ige_encrypt ((void *)buf, (void *)buf, x, &aes_key, f->iv, 1); memset (&aes_key, 0, sizeof (aes_key)); } out_cstring (buf, x); @@ -2598,9 +2598,9 @@ static int download_on_answer (struct tgl_state *TLS, struct query *q, void *DD) assert (!(len & 15)); void *ptr = DS_UF->bytes->data; - AES_KEY aes_key; - AES_set_decrypt_key (D->key, 256, &aes_key); - AES_ige_encrypt (ptr, ptr, len, &aes_key, D->iv, 0); + TGLC_aes_key aes_key; + TGLC_aes_set_decrypt_key (D->key, 256, &aes_key); + TGLC_aes_ige_encrypt (ptr, ptr, len, &aes_key, D->iv, 0); memset (&aes_key, 0, sizeof (aes_key)); if (len > D->size - D->offset) { len = D->size - D->offset; diff --git a/structures.c b/structures.c index b608dbe..9dc9876 100644 --- a/structures.c +++ b/structures.c @@ -29,7 +29,7 @@ #include "mtproto-common.h" //#include "telegram.h" #include "tree.h" -#include <openssl/aes.h> +#include "crypto/aes.h" #include "crypto/bn.h" #include "crypto/sha.h" #include "queries.h" @@ -1492,9 +1492,9 @@ static int decrypt_encrypted_message (struct tgl_secret_chat *E) { memcpy (iv + 20, sha1c_buffer + 16, 4); memcpy (iv + 24, sha1d_buffer + 0, 8); - AES_KEY aes_key; - AES_set_decrypt_key (key, 256, &aes_key); - AES_ige_encrypt ((void *)decr_ptr, (void *)decr_ptr, 4 * (decr_end - decr_ptr), &aes_key, iv, 0); + TGLC_aes_key aes_key; + TGLC_aes_set_decrypt_key (key, 256, &aes_key); + TGLC_aes_ige_encrypt ((void *)decr_ptr, (void *)decr_ptr, 4 * (decr_end - decr_ptr), &aes_key, iv, 0); memset (&aes_key, 0, sizeof (aes_key)); int x = *(decr_ptr); |