Skip to content

Commit

Permalink
Merge pull request #594 from smukil/clean_dyn_aes_encrypt_msg_api
Browse files Browse the repository at this point in the history
Avoid implicit interpretation of return type for dyn_aes_encrypt_msg()
  • Loading branch information
ipapapa authored Aug 27, 2018
2 parents e63924f + 10512ae commit 4b9e839
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
9 changes: 6 additions & 3 deletions src/dyn_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,13 @@ dyn_aes_decrypt(unsigned char *enc_msg, size_t enc_msg_len, struct mbuf *mbuf, u
*
*/
rstatus_t
dyn_aes_encrypt_msg(struct msg *msg, unsigned char *arg_aes_key)
dyn_aes_encrypt_msg(struct msg *msg, unsigned char *arg_aes_key, size_t* outlen)
{
struct mhdr mhdr_tem;
int count = 0;

if (STAILQ_EMPTY(&msg->mhdr)) {
// 'msg' is empty. Nothing to encrypt.
return DN_ERROR;
}

Expand All @@ -409,8 +410,9 @@ dyn_aes_encrypt_msg(struct msg *msg, unsigned char *arg_aes_key)

struct mbuf *nbuf = mbuf_get();
if (nbuf == NULL) {
// Unable to obtain an 'mbuf'.
mbuf_put(mbuf);
return DN_ERROR;
return DN_ENOMEM;
}

int n = dyn_aes_encrypt(mbuf->pos, mbuf_length(mbuf), nbuf, arg_aes_key);
Expand Down Expand Up @@ -438,7 +440,8 @@ dyn_aes_encrypt_msg(struct msg *msg, unsigned char *arg_aes_key)
}
}

return count;
*outlen = count;
return DN_OK;
}


Expand Down
2 changes: 1 addition & 1 deletion src/dyn_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ rstatus_t dyn_aes_encrypt(const unsigned char *msg, size_t msgLen,
rstatus_t dyn_aes_decrypt(unsigned char *encMsg, size_t encMsgLen,
struct mbuf *mbuf, unsigned char *aes_key);

rstatus_t dyn_aes_encrypt_msg(struct msg *msg, unsigned char *aes_key);
rstatus_t dyn_aes_encrypt_msg(struct msg *msg, unsigned char *aes_key, size_t* outlen);
unsigned char* generate_aes_key(void);

int dyn_rsa_size(void);
Expand Down
19 changes: 12 additions & 7 deletions src/dyn_dnode_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,16 +454,21 @@ dnode_rsp_send_next(struct context *ctx, struct conn *conn)
}

if (ENCRYPTION) {
status = dyn_aes_encrypt_msg(rsp, conn->aes_key);
if (status == DN_ERROR) {
loga("OOM to obtain an mbuf for encryption!");
mbuf_put(header_buf);
rsp_put(rsp);
return NULL;
size_t encrypted_bytes;
status = dyn_aes_encrypt_msg(rsp, conn->aes_key, &encrypted_bytes);
if (status != DN_OK) {
if (status == DN_ENOMEM) {
loga("OOM to obtain an mbuf for encryption!");
} else if (status == DN_ERROR) {
loga("Encryption failed: Empty message");
}
mbuf_put(header_buf);
rsp_put(rsp);
return NULL;
}

if (log_loggable(LOG_VVERB)) {
log_debug(LOG_VERB, "#encrypted bytes : %d", status);
log_debug(LOG_VERB, "#encrypted bytes : %d", encrypted_bytes);
}

dmsg_write(header_buf, msg_id, msg_type, conn, msg_length(rsp));
Expand Down
15 changes: 10 additions & 5 deletions src/dyn_dnode_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,20 @@ dnode_peer_req_forward(struct context *ctx, struct conn *c_conn,

//write dnode header
if (ENCRYPTION) {
status = dyn_aes_encrypt_msg(req, p_conn->aes_key);
if (status == DN_ERROR) {
loga("OOM to obtain an mbuf for encryption!");
size_t encrypted_bytes;
status = dyn_aes_encrypt_msg(req, p_conn->aes_key, &encrypted_bytes);
if (status != DN_OK) {
if (status == DN_ENOMEM) {
loga("OOM to obtain an mbuf for encryption!");
} else if (status == DN_ERROR) {
loga("Encryption failed: Empty message");
}
*dyn_error_code = status;
mbuf_put(header_buf);
*dyn_error_code = DN_ENOMEM;
return status;
}

log_debug(LOG_VVERB, "#encrypted bytes : %d", status);
log_debug(LOG_VVERB, "#encrypted bytes : %d", encrypted_bytes);

dmsg_write(header_buf, req->id, msg_type, p_conn, msg_length(req));
} else {
Expand Down

0 comments on commit 4b9e839

Please sign in to comment.