Skip to content

Commit

Permalink
Reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
dingyi9982 committed Sep 20, 2018
1 parent 6ec9670 commit 178b7cb
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 97 deletions.
4 changes: 2 additions & 2 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,12 +434,12 @@ static int upload_file(genaro_env_t *env, char *bucket_id, const char *file_path

genaro_encryption_info_t *encryption_info = genaro_generate_encryption_info(env, bucket_id);

genaro_encryption_key_ctr_as_str_t *rsa_encryption_key_ctr_as_str = NULL;
genaro_key_ctr_as_str_t *rsa_key_ctr_as_str = NULL;
genaro_upload_state_t *state = genaro_bridge_store_file(env,
&upload_opts,
encryption_info->index,
encryption_info->key_ctr_as_str,
rsa_encryption_key_ctr_as_str,
rsa_key_ctr_as_str,
NULL,
progress_cb,
upload_file_complete);
Expand Down
72 changes: 36 additions & 36 deletions src/downloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ static void free_download_state(genaro_download_state_t *state)
free(state->excluded_farmer_ids);
}

if(state->decryption_key_ctr) {
if (state->decryption_key_ctr->key) {
free(state->decryption_key_ctr->key);
if(state->key_ctr) {
if (state->key_ctr->key) {
free(state->key_ctr->key);
}

if (state->decryption_key_ctr->ctr) {
free(state->decryption_key_ctr->ctr);
if (state->key_ctr->ctr) {
free(state->key_ctr->ctr);
}
free(state->decryption_key_ctr);
free(state->key_ctr);
}

if (state->info) {
Expand Down Expand Up @@ -1049,7 +1049,7 @@ static void determine_decryption_key_v1(genaro_download_state_t *state)
goto cleanup;
}

state->decryption_key_ctr->key = decrypt_key;
state->key_ctr->key = decrypt_key;

index = str2hex(strlen(state->info->index), (char *)state->info->index);
if (!index) {
Expand All @@ -1064,7 +1064,7 @@ static void determine_decryption_key_v1(genaro_download_state_t *state)
}

memcpy(decrypt_ctr, index, AES_BLOCK_SIZE);
state->decryption_key_ctr->ctr = decrypt_ctr;
state->key_ctr->ctr = decrypt_ctr;

cleanup:
if (file_key_as_str) {
Expand Down Expand Up @@ -1104,7 +1104,7 @@ static void determine_decryption_key_v0(genaro_download_state_t *state)
memset_zero(file_key, DETERMINISTIC_KEY_SIZE + 1);
free(file_key);

state->decryption_key_ctr->key = decrypt_key;
state->key_ctr->key = decrypt_key;

uint8_t *file_id_hash = calloc(RIPEMD160_DIGEST_SIZE + 1, sizeof(uint8_t));
if (!file_id_hash) {
Expand All @@ -1124,17 +1124,17 @@ static void determine_decryption_key_v0(genaro_download_state_t *state)

free(file_id_hash);

state->decryption_key_ctr->ctr = decrypt_ctr;
state->key_ctr->ctr = decrypt_ctr;
}

// has_key
static void determine_decryption_key(genaro_download_state_t *state)
{
if (!state->env->encrypt_options ||
!state->env->encrypt_options->priv_key) {
state->decryption_key_ctr->key = NULL;
state->decryption_key_ctr->ctr = NULL;
} else if(state->decryption_key_ctr && state->decryption_key_ctr->key && state->decryption_key_ctr->ctr) {
state->key_ctr->key = NULL;
state->key_ctr->ctr = NULL;
} else if(state->key_ctr && state->key_ctr->key && state->key_ctr->ctr) {
return;
} else if (state->info->index) {
// calculate decryption key based on the index
Expand Down Expand Up @@ -1257,7 +1257,7 @@ static void request_info(uv_work_t *work)
req->info->isShareFile = isShareFile;
}

if(isShareFile && !(state->decryption_key_ctr && state->decryption_key_ctr->key && state->decryption_key_ctr->ctr)) {
if(isShareFile && !(state->key_ctr && state->key_ctr->key && state->key_ctr->ctr)) {
state->error_status = GENARO_BRIDGE_DECRYPTION_KEY_ERROR;
goto clean_up;
}
Expand Down Expand Up @@ -1402,7 +1402,7 @@ static int prepare_file_hmac(genaro_download_state_t *state)
{
// initialize the hmac with the decrypt key
struct hmac_sha512_ctx hmac_ctx;
hmac_sha512_set_key(&hmac_ctx, SHA256_DIGEST_SIZE, state->decryption_key_ctr->key);
hmac_sha512_set_key(&hmac_ctx, SHA256_DIGEST_SIZE, state->key_ctr->key);

for (int i = 0; i < state->total_pointers; i++) {

Expand Down Expand Up @@ -1502,8 +1502,8 @@ static void after_recover_shards(uv_work_t *work, int status)

queue_next_work(state);

free(req->decryption_key_ctr.key);
free(req->decryption_key_ctr.ctr);
free(req->key_ctr.key);
free(req->key_ctr.ctr);

free(req->zilch);
free(req);
Expand Down Expand Up @@ -1616,7 +1616,7 @@ static void recover_shards(uv_work_t *work)

decrypt:

aes256_set_encrypt_key(&ctx, req->decryption_key_ctr.key);
aes256_set_encrypt_key(&ctx, req->key_ctr.key);

while (bytes_decrypted < req->data_filesize) {

Expand All @@ -1625,7 +1625,7 @@ static void recover_shards(uv_work_t *work)
}

ctr_crypt(&ctx, (nettle_cipher_func *)aes256_encrypt,
AES_BLOCK_SIZE, req->decryption_key_ctr.ctr,
AES_BLOCK_SIZE, req->key_ctr.ctr,
len,
data_map + bytes_decrypted,
data_map + bytes_decrypted);
Expand Down Expand Up @@ -1744,25 +1744,25 @@ static void queue_recover_shards(genaro_download_state_t *state)
req->zilch = zilch;
req->has_missing = has_missing;

if (state->decryption_key_ctr && state->decryption_key_ctr->key && state->decryption_key_ctr->ctr) {
req->decryption_key_ctr.key = calloc(SHA256_DIGEST_SIZE, sizeof(uint8_t));
if (!req->decryption_key_ctr.key) {
if (state->key_ctr && state->key_ctr->key && state->key_ctr->ctr) {
req->key_ctr.key = calloc(SHA256_DIGEST_SIZE, sizeof(uint8_t));
if (!req->key_ctr.key) {
state->error_status = GENARO_MEMORY_ERROR;
return;
}
req->decryption_key_ctr.ctr = calloc(AES_BLOCK_SIZE, sizeof(uint8_t));
if (!req->decryption_key_ctr.ctr) {
req->key_ctr.ctr = calloc(AES_BLOCK_SIZE, sizeof(uint8_t));
if (!req->key_ctr.ctr) {
state->error_status = GENARO_MEMORY_ERROR;
return;
}

memcpy(req->decryption_key_ctr.key, state->decryption_key_ctr->key, SHA256_DIGEST_SIZE);
memcpy(req->decryption_key_ctr.ctr, state->decryption_key_ctr->ctr, AES_BLOCK_SIZE);
memcpy(req->key_ctr.key, state->key_ctr->key, SHA256_DIGEST_SIZE);
memcpy(req->key_ctr.ctr, state->key_ctr->ctr, AES_BLOCK_SIZE);

increment_ctr_aes_iv(req->decryption_key_ctr.ctr, 0);
increment_ctr_aes_iv(req->key_ctr.ctr, 0);
} else {
req->decryption_key_ctr.key = NULL;
req->decryption_key_ctr.ctr = NULL;
req->key_ctr.key = NULL;
req->key_ctr.ctr = NULL;
}

req->state = state;
Expand Down Expand Up @@ -1896,7 +1896,7 @@ GENARO_API int genaro_bridge_resolve_file_cancel(genaro_download_state_t *state)
GENARO_API genaro_download_state_t *genaro_bridge_resolve_file(genaro_env_t *env,
const char *bucket_id,
const char *file_id,
genaro_decryption_key_ctr_as_str_t *key_ctr_as_str,
genaro_key_ctr_as_str_t *key_ctr_as_str,
const char *file_name,
const char *temp_file_name,
FILE *destination,
Expand Down Expand Up @@ -1944,9 +1944,9 @@ GENARO_API genaro_download_state_t *genaro_bridge_resolve_file(genaro_env_t *env
state->canceled = false;
state->log = env->log;
state->handle = handle;
state->decryption_key_ctr = NULL;
state->key_ctr = NULL;

state->decryption_key_ctr = (genaro_decryption_key_ctr_t *)malloc(sizeof(genaro_decryption_key_ctr_t));
state->key_ctr = (genaro_key_ctr_t *)malloc(sizeof(genaro_key_ctr_t));
if(key_ctr_as_str && key_ctr_as_str->key_as_str && key_ctr_as_str->ctr_as_str) {
uint8_t *key = str2hex(strlen(key_ctr_as_str->key_as_str), key_ctr_as_str->key_as_str);
uint8_t *ctr = str2hex(strlen(key_ctr_as_str->ctr_as_str), key_ctr_as_str->ctr_as_str);
Expand All @@ -1959,11 +1959,11 @@ GENARO_API genaro_download_state_t *genaro_bridge_resolve_file(genaro_env_t *env
return NULL;
}

state->decryption_key_ctr->key = key;
state->decryption_key_ctr->ctr = ctr;
state->key_ctr->key = key;
state->key_ctr->ctr = ctr;
} else {
state->decryption_key_ctr->key = NULL;
state->decryption_key_ctr->ctr = NULL;
state->key_ctr->key = NULL;
state->key_ctr->ctr = NULL;
}

// start download
Expand Down
2 changes: 1 addition & 1 deletion src/downloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ typedef struct {
uint32_t data_shards;
uint32_t parity_shards;
uint64_t shard_size;
genaro_decryption_key_ctr_t decryption_key_ctr;
genaro_key_ctr_t key_ctr;
uint8_t *zilch;
bool has_missing;
/* state should not be modified in worker threads */
Expand Down
13 changes: 1 addition & 12 deletions src/genaro.c
Original file line number Diff line number Diff line change
Expand Up @@ -1638,20 +1638,9 @@ GENARO_API genaro_encryption_info_t *genaro_generate_encryption_info(genaro_env_
goto cleanup;
}

// uint8_t *encryption_key = str2hex(strlen(key_as_str), key_as_str);
// if (!encryption_key) {
// goto cleanup;
// }

uint8_t *encryption_ctr = calloc(AES_BLOCK_SIZE, sizeof(uint8_t));
if (!encryption_ctr) {
goto cleanup;
}
memcpy(encryption_ctr, index, AES_BLOCK_SIZE);

genaro_encryption_info_t *encryption_info = (genaro_encryption_info_t *)malloc(sizeof(genaro_encryption_info_t));
encryption_info->index = index_as_str;
encryption_info->key_ctr_as_str = (genaro_encryption_key_ctr_as_str_t *)malloc(sizeof(genaro_encryption_key_ctr_as_str_t));
encryption_info->key_ctr_as_str = (genaro_key_ctr_as_str_t *)malloc(sizeof(genaro_key_ctr_as_str_t));
encryption_info->key_ctr_as_str->key_as_str = key_as_str;
encryption_info->key_ctr_as_str->ctr_as_str = strdup(index_as_str);

Expand Down
30 changes: 10 additions & 20 deletions src/genaro.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,28 +122,18 @@ extern "C" {
typedef struct {
uint8_t *key;
uint8_t *ctr;
} genaro_encryption_key_ctr_t;
} genaro_key_ctr_t;

typedef struct {
const char *key_as_str;
const char *ctr_as_str;
} genaro_encryption_key_ctr_as_str_t;
} genaro_key_ctr_as_str_t;

typedef struct {
genaro_encryption_key_ctr_as_str_t *key_ctr_as_str;
genaro_key_ctr_as_str_t *key_ctr_as_str;
char *index;
} genaro_encryption_info_t;

typedef struct {
uint8_t *key;
uint8_t *ctr;
} genaro_decryption_key_ctr_t;

typedef struct {
const char *key_as_str;
const char *ctr_as_str;
} genaro_decryption_key_ctr_as_str_t;

typedef struct {
uint8_t *encryption_key;
uint8_t *encryption_ctr;
Expand Down Expand Up @@ -527,7 +517,7 @@ typedef struct genaro_download_state {
bool requesting_pointers;
int error_status;
bool writing;
genaro_decryption_key_ctr_t *decryption_key_ctr;
genaro_key_ctr_t *key_ctr;
const char *hmac;
uint32_t pending_work_count;
genaro_log_levels_t *log;
Expand Down Expand Up @@ -587,8 +577,8 @@ typedef struct genaro_upload_state {
char *exclude;
char *frame_id;
char *hmac_id;
genaro_encryption_key_ctr_t *encryption_key_ctr;
genaro_encryption_key_ctr_as_str_t *rsa_encryption_key_ctr_as_str;
genaro_key_ctr_t *key_ctr;
genaro_key_ctr_as_str_t *rsa_key_ctr_as_str;

// TODO: change this to opts or env
bool rs;
Expand Down Expand Up @@ -969,7 +959,7 @@ GENARO_API int genaro_bridge_store_file_cancel(genaro_upload_state_t *state);
*
* @param[in] env A pointer to environment
* @param[in] opts The options for the upload
* @param[in] index The index that has generated encryption_key_ctr.
* @param[in] index The index that has generated key_ctr.
* @param[in] key_ctr_as_str The key and ctr for file encryption
* @param[in] rsa_key_ctr_as_str The RSA encrypted key and ctr
* @param[in] handle A pointer that will be available in the callback
Expand All @@ -980,8 +970,8 @@ GENARO_API int genaro_bridge_store_file_cancel(genaro_upload_state_t *state);
GENARO_API genaro_upload_state_t *genaro_bridge_store_file(genaro_env_t *env,
genaro_upload_opts_t *opts,
const char *index,
genaro_decryption_key_ctr_as_str_t *key_ctr_as_str,
genaro_decryption_key_ctr_as_str_t *rsa_key_ctr_as_str,
genaro_key_ctr_as_str_t *key_ctr_as_str,
genaro_key_ctr_as_str_t *rsa_key_ctr_as_str,
void *handle,
genaro_progress_upload_cb progress_cb,
genaro_finished_upload_cb finished_cb);
Expand Down Expand Up @@ -1013,7 +1003,7 @@ GENARO_API int genaro_bridge_resolve_file_cancel(genaro_download_state_t *state)
GENARO_API genaro_download_state_t *genaro_bridge_resolve_file(genaro_env_t *env,
const char *bucket_id,
const char *file_id,
genaro_decryption_key_ctr_as_str_t *key_ctr_as_str,
genaro_key_ctr_as_str_t *key_ctr_as_str,
const char *file_name,
const char *temp_file_name,
FILE *destination,
Expand Down
Loading

0 comments on commit 178b7cb

Please sign in to comment.