Skip to content

Commit

Permalink
move implement
Browse files Browse the repository at this point in the history
  • Loading branch information
breakwa11 committed Mar 27, 2017
1 parent c936848 commit 6e47cab
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 44 deletions.
39 changes: 0 additions & 39 deletions src/encrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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];
Expand Down
2 changes: 0 additions & 2 deletions src/encrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/obfs/auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
17 changes: 17 additions & 0 deletions src/obfs/obfsutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <time.h>

#include "obfsutil.h"
#include "encrypt.h"

int get_head_size(char *plaindata, int size, int def_size) {
if (plaindata == NULL || size < 2)
Expand Down Expand Up @@ -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);
}

4 changes: 4 additions & 0 deletions src/obfs/obfsutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6e47cab

Please sign in to comment.