Skip to content

Commit

Permalink
format with .uncrustify.cfg (shadowsocks#1759)
Browse files Browse the repository at this point in the history
* format with .uncrustify.cfg

format with .uncrustify.cfg

* format with .uncrustify.cfg

* format with .uncrustify.cfg exclude json.* base64.* uthash.h

* format with .uncrustify.cfg exclude json.* base64.* uthash.h
  • Loading branch information
wangxufire authored and madeye committed Oct 30, 2017
1 parent 26ae365 commit cc1bb4c
Show file tree
Hide file tree
Showing 22 changed files with 500 additions and 418 deletions.
35 changes: 35 additions & 0 deletions code-format.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@echo off

set root=%~dp0
set source=%root%src

goto start

:format
set filelist=%1
for /r "%filelist%" %%f in (*) do (
if "%%~xf" equ ".h" (
call :format_file %%f
) else if "%%~xf" equ ".c" (
call :format_file %%f
)
)
goto end

:format_file
set f=%1
if "%~n1" neq "base64" (
if "%~n1" neq "json" (
if "%~n1" neq "uthash" (
echo 'format file "%f%"'
uncrustify -c %root%\.uncrustify.cfg -l C --replace --no-backup %f%
DEL %~dp1*.uncrustify >nul 2>nul
)
)
)
goto end

:start
call :format %source%

:end
27 changes: 27 additions & 0 deletions code-format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

root=$(pwd)
source="$root"/src

function format() {
filelist=$(ls "$1")
pushd "$1"
for file in $filelist; do
if test -d "$file"; then
echo "format directory $file"
format "$file"
else
if ([ "${file%%.*}" != "base64" ] &&
[ "${file%%.*}" != "json" ] &&
[ "${file%%.*}" != "uthash" ]) &&
([ "${file##*.}" = "h" ] || [ "${file##*.}" = "c" ]); then
echo "format file $file"
uncrustify -c "$root"/.uncrustify.cfg -l C --replace --no-backup "$file"
rm ./*.uncrustify >/dev/null 2>&1
fi
fi
done
popd
}

format "$source"
40 changes: 19 additions & 21 deletions src/aead.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@ aead_cipher_ctx_set_key(cipher_ctx_t *cipher_ctx, int enc)
}

int err = crypto_hkdf(md,
cipher_ctx->salt, cipher_ctx->cipher->key_len,
cipher_ctx->cipher->key, cipher_ctx->cipher->key_len,
(uint8_t *)SUBKEY_INFO, strlen(SUBKEY_INFO),
cipher_ctx->skey, cipher_ctx->cipher->key_len);
cipher_ctx->salt, cipher_ctx->cipher->key_len,
cipher_ctx->cipher->key, cipher_ctx->cipher->key_len,
(uint8_t *)SUBKEY_INFO, strlen(SUBKEY_INFO),
cipher_ctx->skey, cipher_ctx->cipher->key_len);
if (err) {
FATAL("Unable to generate subkey");
}
Expand Down Expand Up @@ -358,9 +358,9 @@ aead_encrypt_all(buffer_t *plaintext, cipher_t *cipher, size_t capacity)
cipher_ctx_t cipher_ctx;
aead_ctx_init(cipher, &cipher_ctx, 1);

size_t salt_len = cipher->key_len;
size_t tag_len = cipher->tag_len;
int err = CRYPTO_OK;
size_t salt_len = cipher->key_len;
size_t tag_len = cipher->tag_len;
int err = CRYPTO_OK;

static buffer_t tmp = { 0, 0, 0, NULL };
brealloc(&tmp, salt_len + tag_len + plaintext->len, capacity);
Expand Down Expand Up @@ -395,9 +395,9 @@ aead_encrypt_all(buffer_t *plaintext, cipher_t *cipher, size_t capacity)
int
aead_decrypt_all(buffer_t *ciphertext, cipher_t *cipher, size_t capacity)
{
size_t salt_len = cipher->key_len;
size_t tag_len = cipher->tag_len;
int err = CRYPTO_OK;
size_t salt_len = cipher->key_len;
size_t tag_len = cipher->tag_len;
int err = CRYPTO_OK;

if (ciphertext->len <= salt_len + tag_len) {
return CRYPTO_ERROR;
Expand Down Expand Up @@ -469,7 +469,7 @@ aead_chunk_encrypt(cipher_ctx_t *ctx, uint8_t *p, uint8_t *c,
sodium_increment(n, nlen);

clen = plen + tlen;
err = aead_cipher_encrypt(ctx, c + CHUNK_SIZE_LEN + tlen, &clen,p, plen,
err = aead_cipher_encrypt(ctx, c + CHUNK_SIZE_LEN + tlen, &clen, p, plen,
NULL, 0, n, ctx->skey);
if (err)
return CRYPTO_ERROR;
Expand All @@ -495,11 +495,11 @@ aead_encrypt(buffer_t *plaintext, cipher_ctx_t *cipher_ctx, size_t capacity)
static buffer_t tmp = { 0, 0, 0, NULL };
buffer_t *ciphertext;

cipher_t *cipher = cipher_ctx->cipher;
int err = CRYPTO_ERROR;
size_t salt_ofst = 0;
size_t salt_len = cipher->key_len;
size_t tag_len = cipher->tag_len;
cipher_t *cipher = cipher_ctx->cipher;
int err = CRYPTO_ERROR;
size_t salt_ofst = 0;
size_t salt_len = cipher->key_len;
size_t tag_len = cipher->tag_len;

if (!cipher_ctx->init) {
salt_ofst = salt_len;
Expand Down Expand Up @@ -586,7 +586,7 @@ aead_decrypt(buffer_t *ciphertext, cipher_ctx_t *cipher_ctx, size_t capacity)

cipher_t *cipher = cipher_ctx->cipher;

size_t salt_len = cipher->key_len;
size_t salt_len = cipher->key_len;

if (cipher_ctx->chunk == NULL) {
cipher_ctx->chunk = (buffer_t *)ss_malloc(sizeof(buffer_t));
Expand Down Expand Up @@ -621,7 +621,6 @@ aead_decrypt(buffer_t *ciphertext, cipher_ctx_t *cipher_ctx, size_t capacity)
cipher_ctx->chunk->len -= salt_len;

cipher_ctx->init = 1;

}

size_t plen = 0;
Expand Down Expand Up @@ -686,10 +685,10 @@ aead_key_init(int method, const char *pass, const char *key)

if (key != NULL)
cipher->key_len = crypto_parse_key(key, cipher->key,
supported_aead_ciphers_key_size[method]);
supported_aead_ciphers_key_size[method]);
else
cipher->key_len = crypto_derive_key(pass, cipher->key,
supported_aead_ciphers_key_size[method]);
supported_aead_ciphers_key_size[method]);

if (cipher->key_len == 0) {
FATAL("Cannot generate key and nonce");
Expand Down Expand Up @@ -719,4 +718,3 @@ aead_init(const char *pass, const char *key, const char *method)
}
return aead_key_init(m, pass, key);
}

2 changes: 1 addition & 1 deletion src/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct cache {
};

int cache_create(struct cache **dst, const size_t capacity,
void (*free_cb)(void *key, void *element));
void (*free_cb)(void *key, void *element));
int cache_delete(struct cache *cache, int keep_data);
int cache_clear(struct cache *cache, ev_tstamp age);
int cache_lookup(struct cache *cache, char *key, size_t key_len, void *result);
Expand Down
44 changes: 24 additions & 20 deletions src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
#endif

#if defined(__linux__) && defined(HAVE_LINUX_RANDOM_H)
# include <fcntl.h>
# include <unistd.h>
# include <sys/ioctl.h>
# include <linux/random.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/random.h>
#endif

#include <stdint.h>
Expand Down Expand Up @@ -239,22 +239,24 @@ crypto_derive_key(const char *pass, uint8_t *key, size_t key_len)
}

/* HKDF-Extract + HKDF-Expand */
int crypto_hkdf(const mbedtls_md_info_t *md, const unsigned char *salt,
int salt_len, const unsigned char *ikm, int ikm_len,
const unsigned char *info, int info_len, unsigned char *okm,
int okm_len)
int
crypto_hkdf(const mbedtls_md_info_t *md, const unsigned char *salt,
int salt_len, const unsigned char *ikm, int ikm_len,
const unsigned char *info, int info_len, unsigned char *okm,
int okm_len)
{
unsigned char prk[MBEDTLS_MD_MAX_SIZE];

return crypto_hkdf_extract(md, salt, salt_len, ikm, ikm_len, prk) ||
crypto_hkdf_expand(md, prk, mbedtls_md_get_size(md), info, info_len,
okm, okm_len);
okm, okm_len);
}

/* HKDF-Extract(salt, IKM) -> PRK */
int crypto_hkdf_extract(const mbedtls_md_info_t *md, const unsigned char *salt,
int salt_len, const unsigned char *ikm, int ikm_len,
unsigned char *prk)
int
crypto_hkdf_extract(const mbedtls_md_info_t *md, const unsigned char *salt,
int salt_len, const unsigned char *ikm, int ikm_len,
unsigned char *prk)
{
int hash_len;
unsigned char null_salt[MBEDTLS_MD_MAX_SIZE] = { '\0' };
Expand All @@ -266,17 +268,18 @@ int crypto_hkdf_extract(const mbedtls_md_info_t *md, const unsigned char *salt,
hash_len = mbedtls_md_get_size(md);

if (salt == NULL) {
salt = null_salt;
salt = null_salt;
salt_len = hash_len;
}

return mbedtls_md_hmac(md, salt, salt_len, ikm, ikm_len, prk);
}

/* HKDF-Expand(PRK, info, L) -> OKM */
int crypto_hkdf_expand(const mbedtls_md_info_t *md, const unsigned char *prk,
int prk_len, const unsigned char *info, int info_len,
unsigned char *okm, int okm_len)
int
crypto_hkdf_expand(const mbedtls_md_info_t *md, const unsigned char *prk,
int prk_len, const unsigned char *info, int info_len,
unsigned char *okm, int okm_len)
{
int hash_len;
int N;
Expand Down Expand Up @@ -323,7 +326,7 @@ int crypto_hkdf_expand(const mbedtls_md_info_t *md, const unsigned char *prk,
mbedtls_md_hmac_update(&ctx, T, T_len) ||
mbedtls_md_hmac_update(&ctx, info, info_len) ||
/* The constant concatenated to the end of each T(n) is a single
octet. */
* octet. */
mbedtls_md_hmac_update(&ctx, &c, 1) ||
mbedtls_md_hmac_finish(&ctx, T);

Expand All @@ -334,7 +337,7 @@ int crypto_hkdf_expand(const mbedtls_md_info_t *md, const unsigned char *prk,

memcpy(okm + where, T, (i != N) ? hash_len : (okm_len - where));
where += hash_len;
T_len = hash_len;
T_len = hash_len;
}

mbedtls_md_free(&ctx);
Expand All @@ -346,14 +349,14 @@ int
crypto_parse_key(const char *base64, uint8_t *key, size_t key_len)
{
size_t base64_len = strlen(base64);
int out_len = BASE64_SIZE(base64_len);
int out_len = BASE64_SIZE(base64_len);
uint8_t out[out_len];

out_len = base64_decode(out, base64, out_len);
if (out_len > 0 && out_len >= key_len) {
memcpy(key, out, key_len);
#ifdef SS_DEBUG
dump("KEY", (char*)key, key_len);
dump("KEY", (char *)key, key_len);
#endif
return key_len;
}
Expand All @@ -379,4 +382,5 @@ dump(char *tag, char *text, int len)
printf("0x%02x ", (uint8_t)text[i]);
printf("\n");
}

#endif
14 changes: 7 additions & 7 deletions src/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ unsigned char *crypto_md5(const unsigned char *, size_t, unsigned char *);
int crypto_derive_key(const char *, uint8_t *, size_t);
int crypto_parse_key(const char *, uint8_t *, size_t);
int crypto_hkdf(const mbedtls_md_info_t *md, const unsigned char *salt,
int salt_len, const unsigned char *ikm, int ikm_len,
const unsigned char *info, int info_len, unsigned char *okm,
int okm_len);
int salt_len, const unsigned char *ikm, int ikm_len,
const unsigned char *info, int info_len, unsigned char *okm,
int okm_len);
int crypto_hkdf_extract(const mbedtls_md_info_t *md, const unsigned char *salt,
int salt_len, const unsigned char *ikm, int ikm_len,
unsigned char *prk);
int salt_len, const unsigned char *ikm, int ikm_len,
unsigned char *prk);
int crypto_hkdf_expand(const mbedtls_md_info_t *md, const unsigned char *prk,
int prk_len, const unsigned char *info, int info_len,
unsigned char *okm, int okm_len);
int prk_len, const unsigned char *info, int info_len,
unsigned char *okm, int okm_len);
#ifdef SS_DEBUG
void dump(char *tag, char *text, int len);
#endif
Expand Down
Loading

0 comments on commit cc1bb4c

Please sign in to comment.