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

Commit 28e8d45

Browse files
author
HYChang
committed
fix(endpoint): Pass ta_cipher_ctx pointer
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
1 parent 87d7459 commit 28e8d45

File tree

6 files changed

+72
-51
lines changed

6 files changed

+72
-51
lines changed

common/macros.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ typedef enum mam_protocol_e { MAM_V1 } mam_protocol_t;
4343
// release cached data.
4444
#define CACHE_FAILED_TXN_TIMEOUT (7 * 24 * 60 * 60)
4545

46+
#define STR_HELPER(num) #num
47+
#define STR(num) STR_HELPER(num)
48+
4649
#ifdef __cplusplus
4750
}
4851
#endif

endpoint/endpoint.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ status_t send_transaction_information(int value, const char* message, const char
7070
fprintf(stderr, "%s\n", "encrypt msg error");
7171
return ret;
7272
}
73-
serialize_msg(iv, encrypt_ctx.ciphertext_len, (char*)encrypt_ctx.ciphertext, encrypt_ctx.timestamp, encrypt_ctx.hmac,
74-
msg, &msg_len);
73+
serialize_msg(&encrypt_ctx, msg, &msg_len);
7574
bytes_to_trytes((const unsigned char*)msg, msg_len, tryte_msg);
7675

7776
memset(req_body, 0, sizeof(char) * MAX_MSG_LEN);

tests/unit-test/test_text_serializer.c

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,33 @@ void setUp(void) {}
3737
void tearDown(void) {}
3838

3939
void test_serialize_deserialize(void) {
40-
uint8_t out[1024], iv_out[AES_BLOCK_SIZE], payload_out[1024];
41-
size_t payload_len_out, out_msg_len;
42-
uint64_t timestamp;
43-
uint8_t hmac[TA_AES_HMAC_SIZE];
40+
uint8_t out[1024], payload_out[1024];
41+
size_t out_msg_len = 0;
42+
ta_cipher_ctx serialize_ctx = {
43+
.ciphertext = (uint8_t*)payload,
44+
.ciphertext_len = payload_len,
45+
.timestamp = test_timestamp,
46+
.hmac = {0},
47+
.iv = {0},
48+
};
49+
memcpy(serialize_ctx.iv, iv, AES_IV_SIZE);
50+
memcpy(serialize_ctx.hmac, test_hmac, TA_AES_HMAC_SIZE);
4451

45-
int rc1 = serialize_msg(iv, payload_len, (char*)payload, test_timestamp, test_hmac, (char*)out, &out_msg_len);
52+
int rc1 = serialize_msg(&serialize_ctx, (char*)out, &out_msg_len);
4653
TEST_ASSERT_EQUAL_INT32(SC_OK, rc1);
47-
int rc2 = deserialize_msg((char*)out, iv_out, &payload_len_out, (char*)payload_out, &timestamp, hmac);
54+
ta_cipher_ctx deserialize_ctx = {
55+
.ciphertext = payload_out,
56+
.hmac = {0},
57+
.iv = {0},
58+
};
59+
int rc2 = deserialize_msg((char*)out, &deserialize_ctx);
4860
TEST_ASSERT_EQUAL_INT32(SC_OK, rc2);
4961

50-
out[1023] = 0;
51-
payload_out[payload_len] = 0;
52-
TEST_ASSERT_EQUAL_UINT8_ARRAY(iv, iv_out, AES_BLOCK_SIZE);
53-
TEST_ASSERT_EQUAL_UINT64(test_timestamp, timestamp);
54-
TEST_ASSERT_EQUAL_UINT8_ARRAY(test_hmac, hmac, TA_AES_HMAC_SIZE);
55-
TEST_ASSERT_EQUAL_UINT32(payload_len, payload_len_out);
56-
TEST_ASSERT_EQUAL_UINT8_ARRAY(payload, payload_out, payload_len);
62+
TEST_ASSERT_EQUAL_UINT8_ARRAY(iv, deserialize_ctx.iv, AES_BLOCK_SIZE);
63+
TEST_ASSERT_EQUAL_UINT64(test_timestamp, deserialize_ctx.timestamp);
64+
TEST_ASSERT_EQUAL_UINT8_ARRAY(test_hmac, deserialize_ctx.hmac, TA_AES_HMAC_SIZE);
65+
TEST_ASSERT_EQUAL_UINT32(payload_len, deserialize_ctx.ciphertext_len);
66+
TEST_ASSERT_EQUAL_UINT8_ARRAY(payload, deserialize_ctx.ciphertext, payload_len);
5767
}
5868

5969
int main(void) {

utils/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ cc_library(
7070
hdrs = ["text_serializer.h"],
7171
deps = [
7272
":cipher",
73-
"//common:ta_errors",
73+
"//common",
7474
],
7575
)
7676

utils/text_serializer.c

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
#include "text_serializer.h"
10+
#include "common/macros.h"
1011
#include <inttypes.h>
1112
#include <stdio.h>
1213
#include <stdlib.h>
@@ -17,63 +18,80 @@
1718
#define UINT32_LEN 10
1819
#define UINT64_LEN 20
1920

20-
status_t serialize_msg(const uint8_t *iv, uint32_t ciphertext_len, const char *ciphertext, const uint64_t timestamp,
21-
const uint8_t *hmac, char *out_msg, size_t *out_msg_len) {
21+
22+
status_t serialize_msg(const ta_cipher_ctx *ctx, char *out_msg, size_t *out_msg_len) {
2223
/* FIXME: Provide some checks here */
2324
char str_ciphertext_len[UINT32_LEN + 1] = {0};
2425
char buf[UINT64_LEN + 1] = {0};
2526
char *ptr = out_msg;
2627

27-
snprintf(str_ciphertext_len, UINT32_LEN + 1, "%010u", ciphertext_len);
28-
// initialize vector
29-
if (iv) {
30-
memcpy(ptr, iv, IV_LEN);
31-
} else {
32-
memset(ptr, 0, IV_LEN);
28+
if (out_msg == NULL || out_msg_len == NULL) {
29+
// FIXME: Use default logger
30+
fprintf(stderr, "The output message and output message length cannot be NULL\n");
31+
return SC_UTILS_TEXT_SERIALIZE;
32+
}
33+
34+
if (ctx->ciphertext == NULL) {
35+
// FIXME: Use default logger
36+
fprintf(stderr, "The ciphertext cannot be NULL\n");
37+
return SC_UTILS_TEXT_SERIALIZE;
3338
}
34-
ptr += IV_LEN;
3539

40+
snprintf(str_ciphertext_len, UINT32_LEN + 1, "%0" STR(UINT32_LEN) "zu", ctx->ciphertext_len);
41+
// initialize vector
42+
memcpy(ptr, ctx->iv, IV_LEN);
43+
ptr += IV_LEN;
3644
// timestamp
37-
snprintf(buf, UINT64_LEN + 1, "%020" PRIu64, timestamp);
45+
snprintf(buf, UINT64_LEN + 1, "%020" PRIu64, ctx->timestamp);
3846
memcpy(ptr, buf, UINT64_LEN);
3947
ptr += UINT64_LEN;
4048
// hmac
41-
memcpy(ptr, hmac, TA_AES_HMAC_SIZE);
49+
memcpy(ptr, ctx->hmac, TA_AES_HMAC_SIZE);
4250
ptr += TA_AES_HMAC_SIZE;
4351
// ciphertext length
4452
memcpy(ptr, str_ciphertext_len, UINT32_LEN);
4553
ptr += UINT32_LEN;
4654
// ciphertext
47-
memcpy(ptr, ciphertext, ciphertext_len);
48-
*out_msg_len = IV_LEN + UINT64_LEN + TA_AES_HMAC_SIZE + UINT32_LEN + ciphertext_len;
55+
memcpy(ptr, ctx->ciphertext, ctx->ciphertext_len);
56+
*out_msg_len = IV_LEN + UINT64_LEN + TA_AES_HMAC_SIZE + UINT32_LEN + ctx->ciphertext_len;
4957

5058
return SC_OK;
5159
}
5260

53-
status_t deserialize_msg(char *msg, const uint8_t *iv, size_t *ciphertext_len, char *ciphertext, uint64_t *timestamp,
54-
uint8_t *hmac) {
61+
status_t deserialize_msg(const char *msg, ta_cipher_ctx *ctx) {
5562
/* FIXME: Provide some checks here */
5663
char str_ciphertext_len[UINT32_LEN + 1] = {};
5764
char buf[UINT64_LEN + 1] = {0};
58-
char *ptr = msg;
65+
const char *ptr = msg;
66+
if (ptr == NULL) {
67+
// FIXME: Use default logger
68+
fprintf(stderr, "The message cannot be NULL\n");
69+
return SC_UTILS_TEXT_DESERIALIZE;
70+
}
71+
72+
if (ctx->ciphertext == NULL) {
73+
// FIXME: Use default logger
74+
fprintf(stderr, "The ciphertext cannot be NULL\n");
75+
return SC_UTILS_TEXT_DESERIALIZE;
76+
}
5977
uint32_t ciphertext_len_tmp;
6078
// initialize vector
61-
memcpy((char *)iv, ptr, IV_LEN);
79+
memcpy(ctx->iv, ptr, IV_LEN);
6280
ptr += IV_LEN;
6381
// timestamp
6482
memcpy(buf, ptr, UINT64_LEN);
65-
*timestamp = atol(buf);
83+
ctx->timestamp = atol(buf);
6684
ptr += UINT64_LEN;
6785
// hmac
68-
memcpy(hmac, ptr, TA_AES_HMAC_SIZE);
86+
memcpy(ctx->hmac, ptr, TA_AES_HMAC_SIZE);
6987
ptr += TA_AES_HMAC_SIZE;
7088
// ciphertext length
7189
memcpy(str_ciphertext_len, ptr, UINT32_LEN);
7290
ciphertext_len_tmp = atoi(str_ciphertext_len);
7391
ptr += UINT32_LEN;
7492
// ciphertext
75-
memcpy(ciphertext, ptr, ciphertext_len_tmp);
76-
*ciphertext_len = ciphertext_len_tmp;
93+
memcpy(ctx->ciphertext, ptr, ciphertext_len_tmp);
94+
ctx->ciphertext_len = ciphertext_len_tmp;
7795

7896
return SC_OK;
7997
}

utils/text_serializer.h

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <stddef.h>
1313
#include <stdint.h>
1414
#include "common/ta_errors.h"
15+
#include "utils/cipher.h"
1516

1617
#ifdef __cplusplus
1718
extern "C" {
@@ -26,38 +27,28 @@ extern "C" {
2627
* - ciphertext length(10 bytes)
2728
* - ciphertext(other bytes)
2829
*
29-
* @param[in] iv Pointer to initialize vector
30-
* @param[in] ciphertext_len Length of ciphertext
31-
* @param[in] ciphertext Ciphertext to be serialized
32-
* @param[in] timestamp Timestamp to be serialized
33-
* @param[in] hmac Hash mac array to be serialized
30+
* @param[in] ctx The cipher context to be serialized
3431
* @param[out] out_msg Pointer to output message
3532
* @param[out] out_msg_len Pointer to length of serialized message
3633
*
3734
* @return
3835
* - SC_OK on success
3936
* - SC_UTILS_TEXT_SERIALIZE on error
4037
*/
41-
status_t serialize_msg(const uint8_t *iv, uint32_t ciphertext_len, const char *ciphertext, const uint64_t timestamp,
42-
const uint8_t *hmac, char *out_msg, size_t *out_msg_len);
38+
status_t serialize_msg(const ta_cipher_ctx* ctx, char *out_msg, size_t *out_msg_len);
4339

4440
/**
4541
* @brief Deserialize message from serialize_msg
4642
*
4743
* @param[in] msg Pointer to serialize message
48-
* @param[in] iv Pointer to initialize vector
49-
* @param[out] ciphertext_len Pointer to plaintext length
50-
* @param[out] ciphertext Pointer to plaintext output array
51-
* @param[out] timestamp Pointer to timestamp
52-
* @param[out] hmac Pointer to hash mac output array
44+
* @param[in/out] ctx The cipher context to be deserialized
5345
*
5446
* @return
5547
* - SC_OK on success
5648
* - SC_UTILS_TEXT_DESERIALIZE on error
5749
* @see #serialize_msg
5850
*/
59-
status_t deserialize_msg(char *msg, const uint8_t *iv, size_t *ciphertext_len, char *ciphertext, uint64_t *timestamp,
60-
uint8_t *hmac);
51+
status_t deserialize_msg(const char *msg, ta_cipher_ctx* ctx);
6152
#ifdef __cplusplus
6253
}
6354
#endif

0 commit comments

Comments
 (0)