From 6e47cabf80a08a84f14570eb8c0d61be30836831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A0=B4=E5=A8=83=E9=85=B1?= Date: Mon, 27 Mar 2017 17:16:25 +0800 Subject: [PATCH] move implement --- src/encrypt.c | 39 --------------------------------------- src/encrypt.h | 2 -- src/obfs/auth.c | 6 +++--- src/obfs/obfsutil.c | 17 +++++++++++++++++ src/obfs/obfsutil.h | 4 ++++ 5 files changed, 24 insertions(+), 44 deletions(-) diff --git a/src/encrypt.c b/src/encrypt.c index 875ae3b..6eca5c2 100644 --- a/src/encrypt.c +++ b/src/encrypt.c @@ -932,25 +932,6 @@ cipher_context_update(cipher_ctx_t *ctx, uint8_t *output, size_t *olen, (uint8_t *)output, olen); #endif } -int ss_md5_hmac(cipher_env_t* env, char *auth, char *msg, int msg_len, uint8_t *iv) -{ - uint8_t hash[MD5_BYTES]; - uint8_t auth_key[MAX_IV_LENGTH + MAX_KEY_LENGTH]; - memcpy(auth_key, iv, env->enc_iv_len); - memcpy(auth_key + env->enc_iv_len, env->enc_key, env->enc_key_len); - -#if defined(USE_CRYPTO_OPENSSL) - HMAC(EVP_md5(), auth_key, env->enc_iv_len + env->enc_key_len, (uint8_t *)msg, msg_len, (uint8_t *)hash, NULL); -#elif defined(USE_CRYPTO_MBEDTLS) - mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_MD5), auth_key, env->enc_iv_len + env->enc_key_len, (uint8_t *)msg, msg_len, (uint8_t *)hash); -#else - md5_hmac(auth_key, env->enc_iv_len + env->enc_key_len, (uint8_t *)msg, msg_len, (uint8_t *)hash); -#endif - - memcpy(auth, hash, MD5_BYTES); - - return 0; -} int ss_md5_hmac_with_key(char *auth, char *msg, int msg_len, uint8_t *auth_key, int key_len) { @@ -986,26 +967,6 @@ int ss_md5_hash_func(char *auth, char *msg, int msg_len) return 0; } -int ss_sha1_hmac(cipher_env_t* env, char *auth, char *msg, int msg_len, uint8_t *iv) -{ - uint8_t hash[SHA1_BYTES]; - uint8_t auth_key[MAX_IV_LENGTH + MAX_KEY_LENGTH]; - memcpy(auth_key, iv, env->enc_iv_len); - memcpy(auth_key + env->enc_iv_len, env->enc_key, env->enc_key_len); - -#if defined(USE_CRYPTO_OPENSSL) - HMAC(EVP_sha1(), auth_key, env->enc_iv_len + env->enc_key_len, (uint8_t *)msg, msg_len, (uint8_t *)hash, NULL); -#elif defined(USE_CRYPTO_MBEDTLS) - mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1), auth_key, env->enc_iv_len + env->enc_key_len, (uint8_t *)msg, msg_len, (uint8_t *)hash); -#else - sha1_hmac(auth_key, env->enc_iv_len + env->enc_key_len, (uint8_t *)msg, msg_len, (uint8_t *)hash); -#endif - - memcpy(auth, hash, SHA1_BYTES); - - return 0; -} - int ss_sha1_hmac_with_key(char *auth, char *msg, int msg_len, uint8_t *auth_key, int key_len) { uint8_t hash[SHA1_BYTES]; diff --git a/src/encrypt.h b/src/encrypt.h index a448ce4..1285e81 100644 --- a/src/encrypt.h +++ b/src/encrypt.h @@ -213,10 +213,8 @@ int enc_get_key_len(cipher_env_t* env); void cipher_context_release(cipher_env_t *env, cipher_ctx_t *ctx); unsigned char *enc_md5(const unsigned char *d, size_t n, unsigned char *md); -int ss_md5_hmac(cipher_env_t* env, char *auth, char *msg, int msg_len, uint8_t *iv); int ss_md5_hmac_with_key(char *auth, char *msg, int msg_len, uint8_t *auth_key, int key_len); int ss_md5_hash_func(char *auth, char *msg, int msg_len); -int ss_sha1_hmac(cipher_env_t* env, char *auth, char *msg, int msg_len, uint8_t *iv); int ss_sha1_hmac_with_key(char *auth, char *msg, int msg_len, uint8_t *auth_key, int key_len); int ss_sha1_hash_func(char *auth, char *msg, int msg_len); int ss_aes_128_cbc(char *encrypt, char *out_data, char *key); diff --git a/src/obfs/auth.c b/src/obfs/auth.c index 288653d..dd58e07 100644 --- a/src/obfs/auth.c +++ b/src/obfs/auth.c @@ -249,7 +249,7 @@ int auth_sha1_pack_auth_data(auth_simple_global_data *global, server_info *serve memintcopy_lt(outdata + data_offset + 8, global->connection_id); memmove(outdata + data_offset + 12, data, datalength); char hash[SHA1_BYTES]; - ss_sha1_hmac(server->cipher_env, hash, outdata, out_size - OBFS_HMAC_SHA1_LEN, server->iv); + ss_sha1_hmac(hash, outdata, out_size - OBFS_HMAC_SHA1_LEN, server->iv, (int)server->iv_len, server->key, (int)server->key_len); memcpy(outdata + out_size - OBFS_HMAC_SHA1_LEN, hash, OBFS_HMAC_SHA1_LEN); return out_size; } @@ -387,7 +387,7 @@ int auth_sha1_v2_pack_auth_data(auth_simple_global_data *global, server_info *se memintcopy_lt(outdata + data_offset + 8, global->connection_id); memmove(outdata + data_offset + 12, data, datalength); char hash[SHA1_BYTES]; - ss_sha1_hmac(server->cipher_env, hash, outdata, out_size - OBFS_HMAC_SHA1_LEN, server->iv); + ss_sha1_hmac(hash, outdata, out_size - OBFS_HMAC_SHA1_LEN, server->iv, (int)server->iv_len, server->key, (int)server->key_len); memcpy(outdata + out_size - OBFS_HMAC_SHA1_LEN, hash, OBFS_HMAC_SHA1_LEN); return out_size; } @@ -545,7 +545,7 @@ int auth_sha1_v4_pack_auth_data(auth_simple_global_data *global, server_info *se memintcopy_lt(outdata + data_offset + 8, global->connection_id); memmove(outdata + data_offset + 12, data, datalength); char hash[SHA1_BYTES]; - ss_sha1_hmac(server->cipher_env, hash, outdata, out_size - OBFS_HMAC_SHA1_LEN, server->iv); + ss_sha1_hmac(hash, outdata, out_size - OBFS_HMAC_SHA1_LEN, server->iv, (int)server->iv_len, server->key, (int)server->key_len); memcpy(outdata + out_size - OBFS_HMAC_SHA1_LEN, hash, OBFS_HMAC_SHA1_LEN); return out_size; } diff --git a/src/obfs/obfsutil.c b/src/obfs/obfsutil.c index 4c1ccf9..2b2faa3 100644 --- a/src/obfs/obfsutil.c +++ b/src/obfs/obfsutil.c @@ -2,6 +2,7 @@ #include #include "obfsutil.h" +#include "encrypt.h" int get_head_size(char *plaindata, int size, int def_size) { if (plaindata == NULL || size < 2) @@ -39,3 +40,19 @@ uint64_t xorshift128plus(void) { return x + y; } +int ss_md5_hmac(char *auth, char *msg, int msg_len, uint8_t *iv, int enc_iv_len, uint8_t *enc_key, int enc_key_len) +{ + uint8_t auth_key[MAX_IV_LENGTH + MAX_KEY_LENGTH]; + memcpy(auth_key, iv, enc_iv_len); + memcpy(auth_key + enc_iv_len, enc_key, enc_key_len); + return ss_md5_hmac_with_key(auth, msg, msg_len, auth_key, MAX_IV_LENGTH + MAX_KEY_LENGTH); +} + +int ss_sha1_hmac(char *auth, char *msg, int msg_len, uint8_t *iv, int enc_iv_len, uint8_t *enc_key, int enc_key_len) +{ + uint8_t auth_key[MAX_IV_LENGTH + MAX_KEY_LENGTH]; + memcpy(auth_key, iv, enc_iv_len); + memcpy(auth_key + enc_iv_len, enc_key, enc_key_len); + return ss_sha1_hmac_with_key(auth, msg, msg_len, auth_key, MAX_IV_LENGTH + MAX_KEY_LENGTH); +} + diff --git a/src/obfs/obfsutil.h b/src/obfs/obfsutil.h index 786c43e..0a8b3b1 100644 --- a/src/obfs/obfsutil.h +++ b/src/obfs/obfsutil.h @@ -7,4 +7,8 @@ void init_shift128plus(void); uint64_t xorshift128plus(void); +int ss_md5_hmac(char *auth, char *msg, int msg_len, uint8_t *iv, int enc_iv_len, uint8_t *enc_key, int enc_key_len); + +int ss_sha1_hmac(char *auth, char *msg, int msg_len, uint8_t *iv, int enc_iv_len, uint8_t *enc_key, int enc_key_len); + #endif // _OBFS_OBFSUTIL_H