diff --git a/common/macros.h b/common/macros.h index 07eeb402..f8170304 100644 --- a/common/macros.h +++ b/common/macros.h @@ -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 diff --git a/endpoint/endpoint.c b/endpoint/endpoint.c index c45b7745..eab88cc6 100644 --- a/endpoint/endpoint.c +++ b/endpoint/endpoint.c @@ -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); diff --git a/tests/unit-test/test_text_serializer.c b/tests/unit-test/test_text_serializer.c index 891d003f..f96f14f4 100644 --- a/tests/unit-test/test_text_serializer.c +++ b/tests/unit-test/test_text_serializer.c @@ -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, ×tamp, 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) { diff --git a/utils/BUILD b/utils/BUILD index a2fa7b6c..85ccb62f 100644 --- a/utils/BUILD +++ b/utils/BUILD @@ -70,7 +70,7 @@ cc_library( hdrs = ["text_serializer.h"], deps = [ ":cipher", - "//common:ta_errors", + "//common", ], ) diff --git a/utils/text_serializer.c b/utils/text_serializer.c index e00b14cb..6a43d2e9 100644 --- a/utils/text_serializer.c +++ b/utils/text_serializer.c @@ -7,6 +7,7 @@ */ #include "text_serializer.h" +#include "common/macros.h" #include #include #include @@ -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; } diff --git a/utils/text_serializer.h b/utils/text_serializer.h index 4538ddf1..38d5eead 100644 --- a/utils/text_serializer.h +++ b/utils/text_serializer.h @@ -12,6 +12,7 @@ #include #include #include "common/ta_errors.h" +#include "utils/cipher.h" #ifdef __cplusplus extern "C" { @@ -26,11 +27,7 @@ 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 * @@ -38,26 +35,20 @@ extern "C" { * - 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