Skip to content

Commit 0c5b632

Browse files
authored
Merge pull request #455 from tropicsquare/ETR01SDK-429-Check-logic-of-low-level-L2-functions-buff-len-checking
ETR01SDK-429: check logic of low level l2 functions buff len checking
2 parents 3699bd0 + 3cb22d0 commit 0c5b632

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/libtropic.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,14 @@ lt_ret_t lt_session_start(lt_handle_t *h, const uint8_t *stpub, const lt_pkey_in
441441
goto cleanup;
442442
}
443443

444+
// Setup a pointer to a response in the L2 buffer.
445+
struct lt_l2_handshake_rsp_t *p_l2_resp = (struct lt_l2_handshake_rsp_t *)h->l2.buff;
446+
447+
if (TR01_L2_HANDSHAKE_RSP_LEN != (p_l2_resp->rsp_len)) {
448+
ret = LT_L2_RSP_LEN_ERROR;
449+
goto cleanup;
450+
}
451+
444452
ret = lt_in__session_start(h, stpub, pkey_index, shipriv, shipub, &host_eph_keys);
445453

446454
cleanup:

src/libtropic_l2.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ lt_ret_t lt_l2_send_encrypted_cmd(lt_l2_state_t *s2, uint8_t *buff, uint16_t buf
105105

106106
int ret = LT_FAIL;
107107

108-
// There is l3 payload in passed buffer.
108+
// There is L3 payload in provided buffer (buff).
109109
// First check how much data are to be send and if it actually fits into that buffer,
110110
// there must be a space for 2B of size value, ?B of command (ID + data) and 16B of TAG.
111111
struct lt_l3_gen_frame_t *p_frame = (struct lt_l3_gen_frame_t *)buff;
@@ -117,13 +117,13 @@ lt_ret_t lt_l2_send_encrypted_cmd(lt_l2_state_t *s2, uint8_t *buff, uint16_t buf
117117
TR01_L3_PACKET_MAX_SIZE);
118118
return LT_L3_DATA_LEN_ERROR;
119119
}
120-
// Prevent sending more data than is the size of passed buffer.
120+
// Prevent sending more data than is the size of the provided buffer.
121121
if (packet_size > buff_len) {
122122
LT_LOG_ERROR("Packet size %" PRIu16 "exceeds L3 buffer size %" PRIu16, packet_size, buff_len);
123123
return LT_PARAM_ERR;
124124
}
125125

126-
// Setup a request pointer to l2 buffer, which is placed in handle
126+
// Setup a request pointer to L2 buffer, which is placed in handle
127127
struct lt_l2_encrypted_cmd_req_t *req = (struct lt_l2_encrypted_cmd_req_t *)s2->buff;
128128

129129
// Calculate number of chunks to send.
@@ -138,7 +138,7 @@ lt_ret_t lt_l2_send_encrypted_cmd(lt_l2_state_t *s2, uint8_t *buff, uint16_t buf
138138

139139
uint16_t buff_offset = 0;
140140

141-
// Split encrypted buffer into chunks and proceed them into l2 transfers:
141+
// Split encrypted buffer into chunks and proceed them into L2 transfers:
142142
for (int i = 0; i < chunk_num; i++) {
143143
req->req_id = TR01_L2_ENCRYPTED_CMD_REQ_ID;
144144
// If the currently processed chunk is the last one, get its length (may be shorter than
@@ -153,13 +153,13 @@ lt_ret_t lt_l2_send_encrypted_cmd(lt_l2_state_t *s2, uint8_t *buff, uint16_t buf
153153
buff_offset += req->req_len; // Move offset for next chunk
154154
add_crc(req);
155155

156-
// Send l2 request cointaining a chunk from l3 buff
156+
// Send L2 request containing a chunk from L3 buff
157157
ret = lt_l1_write(s2, 2 + req->req_len + 2, LT_L1_TIMEOUT_MS_DEFAULT);
158158
if (ret != LT_OK) {
159159
return ret;
160160
}
161161

162-
// Read a response on this l2 request
162+
// Read a response on this L2 request
163163
ret = lt_l1_read(s2, TR01_L1_LEN_MAX, LT_L1_TIMEOUT_MS_DEFAULT);
164164
if (ret != LT_OK) {
165165
return ret;
@@ -178,28 +178,28 @@ lt_ret_t lt_l2_send_encrypted_cmd(lt_l2_state_t *s2, uint8_t *buff, uint16_t buf
178178
lt_ret_t lt_l2_recv_encrypted_res(lt_l2_state_t *s2, uint8_t *buff, uint16_t max_len)
179179
{
180180
if (!s2
181-
// Max len must be definitively smaller than size of l3 buffer
181+
// Max len must be definitively smaller than size of L3 buffer
182182
|| max_len > TR01_L3_PACKET_MAX_SIZE || !buff) {
183183
return LT_PARAM_ERR;
184184
}
185185

186186
int ret = LT_FAIL;
187-
// Setup a response pointer to l2 buffer, which is placed in handle
187+
// Setup a response pointer to L2 buffer, which is placed in handle
188188
struct lt_l2_encrypted_cmd_rsp_t *resp = (struct lt_l2_encrypted_cmd_rsp_t *)s2->buff;
189189

190-
// Position into l3 buffer where processed l2 chunk will be copied into
190+
// Position into L3 buffer where processed L2 chunk will be copied into
191191
uint16_t offset = 0;
192192
// Tropic can respond with various lengths of chunks, this loop should be limited
193193
uint16_t loops = 0;
194194

195195
do {
196-
/* Get one l2 frame of a device's response */
196+
// Get one L2 frame of a device's response
197197
ret = lt_l1_read(s2, TR01_L1_LEN_MAX, LT_L1_TIMEOUT_MS_DEFAULT);
198198
if (ret != LT_OK) {
199199
return ret;
200200
}
201201

202-
// Prevent receiving more data then is compiled size of l3 buffer
202+
// Prevent receiving more data than is the size of the provided L3 buffer.
203203
if (offset + resp->rsp_len > max_len) {
204204
return LT_L2_RSP_LEN_ERROR;
205205
}
@@ -208,17 +208,17 @@ lt_ret_t lt_l2_recv_encrypted_res(lt_l2_state_t *s2, uint8_t *buff, uint16_t max
208208
ret = lt_l2_frame_check(s2->buff);
209209
switch (ret) {
210210
case LT_L2_RES_CONT:
211-
// Copy content of l2 into certain offset of l3 buffer
211+
// Copy content of L2 into current offset of the L3 buffer
212212
memcpy(buff + offset, (struct l2_encrypted_rsp_t *)resp->l3_chunk, resp->rsp_len);
213213
offset += resp->rsp_len;
214214
loops++;
215215
break;
216216
case LT_OK:
217-
// This was last l2 frame of l3 packet, copy it and return
217+
// This was last L2 frame of L3 packet, copy it and return
218218
memcpy(buff + offset, (struct l2_encrypted_rsp_t *)resp->l3_chunk, resp->rsp_len);
219219
return LT_OK;
220220
default:
221-
// Any other L2 packet's status is not expected
221+
// Any other frame status is not expected
222222
return ret;
223223
}
224224
} while (loops < LT_L2_RECV_ENC_RES_MAX_LOOPS);

0 commit comments

Comments
 (0)