Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
fix(endpoint): Pass ta_cipher_ctx pointer
Browse files Browse the repository at this point in the history
There are individual parameters of serialize_msg and
deserialize_msg. The individual parameters are the same as
ta_cipher_ctx's member. We can pass a ta_cipher_ctx pointer
instead of individual parameters.

Close #627
  • Loading branch information
splasky committed May 21, 2020
1 parent 87d7459 commit 28e8d45
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 51 deletions.
3 changes: 3 additions & 0 deletions common/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ typedef enum mam_protocol_e { MAM_V1 } mam_protocol_t;
// release cached data.
#define CACHE_FAILED_TXN_TIMEOUT (7 * 24 * 60 * 60)

#define STR_HELPER(num) #num
#define STR(num) STR_HELPER(num)

#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 1 addition & 2 deletions endpoint/endpoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ status_t send_transaction_information(int value, const char* message, const char
fprintf(stderr, "%s\n", "encrypt msg error");
return ret;
}
serialize_msg(iv, encrypt_ctx.ciphertext_len, (char*)encrypt_ctx.ciphertext, encrypt_ctx.timestamp, encrypt_ctx.hmac,
msg, &msg_len);
serialize_msg(&encrypt_ctx, msg, &msg_len);
bytes_to_trytes((const unsigned char*)msg, msg_len, tryte_msg);

memset(req_body, 0, sizeof(char) * MAX_MSG_LEN);
Expand Down
36 changes: 23 additions & 13 deletions tests/unit-test/test_text_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,33 @@ void setUp(void) {}
void tearDown(void) {}

void test_serialize_deserialize(void) {
uint8_t out[1024], iv_out[AES_BLOCK_SIZE], payload_out[1024];
size_t payload_len_out, out_msg_len;
uint64_t timestamp;
uint8_t hmac[TA_AES_HMAC_SIZE];
uint8_t out[1024], payload_out[1024];
size_t out_msg_len = 0;
ta_cipher_ctx serialize_ctx = {
.ciphertext = (uint8_t*)payload,
.ciphertext_len = payload_len,
.timestamp = test_timestamp,
.hmac = {0},
.iv = {0},
};
memcpy(serialize_ctx.iv, iv, AES_IV_SIZE);
memcpy(serialize_ctx.hmac, test_hmac, TA_AES_HMAC_SIZE);

int rc1 = serialize_msg(iv, payload_len, (char*)payload, test_timestamp, test_hmac, (char*)out, &out_msg_len);
int rc1 = serialize_msg(&serialize_ctx, (char*)out, &out_msg_len);
TEST_ASSERT_EQUAL_INT32(SC_OK, rc1);
int rc2 = deserialize_msg((char*)out, iv_out, &payload_len_out, (char*)payload_out, &timestamp, hmac);
ta_cipher_ctx deserialize_ctx = {
.ciphertext = payload_out,
.hmac = {0},
.iv = {0},
};
int rc2 = deserialize_msg((char*)out, &deserialize_ctx);
TEST_ASSERT_EQUAL_INT32(SC_OK, rc2);

out[1023] = 0;
payload_out[payload_len] = 0;
TEST_ASSERT_EQUAL_UINT8_ARRAY(iv, iv_out, AES_BLOCK_SIZE);
TEST_ASSERT_EQUAL_UINT64(test_timestamp, timestamp);
TEST_ASSERT_EQUAL_UINT8_ARRAY(test_hmac, hmac, TA_AES_HMAC_SIZE);
TEST_ASSERT_EQUAL_UINT32(payload_len, payload_len_out);
TEST_ASSERT_EQUAL_UINT8_ARRAY(payload, payload_out, payload_len);
TEST_ASSERT_EQUAL_UINT8_ARRAY(iv, deserialize_ctx.iv, AES_BLOCK_SIZE);
TEST_ASSERT_EQUAL_UINT64(test_timestamp, deserialize_ctx.timestamp);
TEST_ASSERT_EQUAL_UINT8_ARRAY(test_hmac, deserialize_ctx.hmac, TA_AES_HMAC_SIZE);
TEST_ASSERT_EQUAL_UINT32(payload_len, deserialize_ctx.ciphertext_len);
TEST_ASSERT_EQUAL_UINT8_ARRAY(payload, deserialize_ctx.ciphertext, payload_len);
}

int main(void) {
Expand Down
2 changes: 1 addition & 1 deletion utils/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ cc_library(
hdrs = ["text_serializer.h"],
deps = [
":cipher",
"//common:ta_errors",
"//common",
],
)

Expand Down
60 changes: 39 additions & 21 deletions utils/text_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

#include "text_serializer.h"
#include "common/macros.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -17,63 +18,80 @@
#define UINT32_LEN 10
#define UINT64_LEN 20

status_t serialize_msg(const uint8_t *iv, uint32_t ciphertext_len, const char *ciphertext, const uint64_t timestamp,
const uint8_t *hmac, char *out_msg, size_t *out_msg_len) {

status_t serialize_msg(const ta_cipher_ctx *ctx, char *out_msg, size_t *out_msg_len) {
/* FIXME: Provide some checks here */
char str_ciphertext_len[UINT32_LEN + 1] = {0};
char buf[UINT64_LEN + 1] = {0};
char *ptr = out_msg;

snprintf(str_ciphertext_len, UINT32_LEN + 1, "%010u", ciphertext_len);
// initialize vector
if (iv) {
memcpy(ptr, iv, IV_LEN);
} else {
memset(ptr, 0, IV_LEN);
if (out_msg == NULL || out_msg_len == NULL) {
// FIXME: Use default logger
fprintf(stderr, "The output message and output message length cannot be NULL\n");
return SC_UTILS_TEXT_SERIALIZE;
}

if (ctx->ciphertext == NULL) {
// FIXME: Use default logger
fprintf(stderr, "The ciphertext cannot be NULL\n");
return SC_UTILS_TEXT_SERIALIZE;
}
ptr += IV_LEN;

snprintf(str_ciphertext_len, UINT32_LEN + 1, "%0" STR(UINT32_LEN) "zu", ctx->ciphertext_len);
// initialize vector
memcpy(ptr, ctx->iv, IV_LEN);
ptr += IV_LEN;
// timestamp
snprintf(buf, UINT64_LEN + 1, "%020" PRIu64, timestamp);
snprintf(buf, UINT64_LEN + 1, "%020" PRIu64, ctx->timestamp);
memcpy(ptr, buf, UINT64_LEN);
ptr += UINT64_LEN;
// hmac
memcpy(ptr, hmac, TA_AES_HMAC_SIZE);
memcpy(ptr, ctx->hmac, TA_AES_HMAC_SIZE);
ptr += TA_AES_HMAC_SIZE;
// ciphertext length
memcpy(ptr, str_ciphertext_len, UINT32_LEN);
ptr += UINT32_LEN;
// ciphertext
memcpy(ptr, ciphertext, ciphertext_len);
*out_msg_len = IV_LEN + UINT64_LEN + TA_AES_HMAC_SIZE + UINT32_LEN + ciphertext_len;
memcpy(ptr, ctx->ciphertext, ctx->ciphertext_len);
*out_msg_len = IV_LEN + UINT64_LEN + TA_AES_HMAC_SIZE + UINT32_LEN + ctx->ciphertext_len;

return SC_OK;
}

status_t deserialize_msg(char *msg, const uint8_t *iv, size_t *ciphertext_len, char *ciphertext, uint64_t *timestamp,
uint8_t *hmac) {
status_t deserialize_msg(const char *msg, ta_cipher_ctx *ctx) {
/* FIXME: Provide some checks here */
char str_ciphertext_len[UINT32_LEN + 1] = {};
char buf[UINT64_LEN + 1] = {0};
char *ptr = msg;
const char *ptr = msg;
if (ptr == NULL) {
// FIXME: Use default logger
fprintf(stderr, "The message cannot be NULL\n");
return SC_UTILS_TEXT_DESERIALIZE;
}

if (ctx->ciphertext == NULL) {
// FIXME: Use default logger
fprintf(stderr, "The ciphertext cannot be NULL\n");
return SC_UTILS_TEXT_DESERIALIZE;
}
uint32_t ciphertext_len_tmp;
// initialize vector
memcpy((char *)iv, ptr, IV_LEN);
memcpy(ctx->iv, ptr, IV_LEN);
ptr += IV_LEN;
// timestamp
memcpy(buf, ptr, UINT64_LEN);
*timestamp = atol(buf);
ctx->timestamp = atol(buf);
ptr += UINT64_LEN;
// hmac
memcpy(hmac, ptr, TA_AES_HMAC_SIZE);
memcpy(ctx->hmac, ptr, TA_AES_HMAC_SIZE);
ptr += TA_AES_HMAC_SIZE;
// ciphertext length
memcpy(str_ciphertext_len, ptr, UINT32_LEN);
ciphertext_len_tmp = atoi(str_ciphertext_len);
ptr += UINT32_LEN;
// ciphertext
memcpy(ciphertext, ptr, ciphertext_len_tmp);
*ciphertext_len = ciphertext_len_tmp;
memcpy(ctx->ciphertext, ptr, ciphertext_len_tmp);
ctx->ciphertext_len = ciphertext_len_tmp;

return SC_OK;
}
19 changes: 5 additions & 14 deletions utils/text_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <stddef.h>
#include <stdint.h>
#include "common/ta_errors.h"
#include "utils/cipher.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -26,38 +27,28 @@ extern "C" {
* - ciphertext length(10 bytes)
* - ciphertext(other bytes)
*
* @param[in] iv Pointer to initialize vector
* @param[in] ciphertext_len Length of ciphertext
* @param[in] ciphertext Ciphertext to be serialized
* @param[in] timestamp Timestamp to be serialized
* @param[in] hmac Hash mac array to be serialized
* @param[in] ctx The cipher context to be serialized
* @param[out] out_msg Pointer to output message
* @param[out] out_msg_len Pointer to length of serialized message
*
* @return
* - SC_OK on success
* - SC_UTILS_TEXT_SERIALIZE on error
*/
status_t serialize_msg(const uint8_t *iv, uint32_t ciphertext_len, const char *ciphertext, const uint64_t timestamp,
const uint8_t *hmac, char *out_msg, size_t *out_msg_len);
status_t serialize_msg(const ta_cipher_ctx* ctx, char *out_msg, size_t *out_msg_len);

/**
* @brief Deserialize message from serialize_msg
*
* @param[in] msg Pointer to serialize message
* @param[in] iv Pointer to initialize vector
* @param[out] ciphertext_len Pointer to plaintext length
* @param[out] ciphertext Pointer to plaintext output array
* @param[out] timestamp Pointer to timestamp
* @param[out] hmac Pointer to hash mac output array
* @param[in/out] ctx The cipher context to be deserialized
*
* @return
* - SC_OK on success
* - SC_UTILS_TEXT_DESERIALIZE on error
* @see #serialize_msg
*/
status_t deserialize_msg(char *msg, const uint8_t *iv, size_t *ciphertext_len, char *ciphertext, uint64_t *timestamp,
uint8_t *hmac);
status_t deserialize_msg(const char *msg, ta_cipher_ctx* ctx);
#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 28e8d45

Please sign in to comment.