mgmt/osdp: Use memcpy instead of raw loops in many places

This patch replaces many instances where raw loops were used to copy bytes
with memcpy calls.

No functional change intended.

Signed-off-by: Siddharth Chandrasekaran <sidcha.dev@gmail.com>
This commit is contained in:
Siddharth Chandrasekaran 2023-01-16 21:14:18 +05:30 committed by Stephanos Ioannidis
parent 4386dc355b
commit ac9510230b
3 changed files with 33 additions and 66 deletions

View file

@ -123,7 +123,7 @@ static inline void assert_buf_len(int need, int have)
static int cp_build_command(struct osdp_pd *pd, uint8_t *buf, int max_len) static int cp_build_command(struct osdp_pd *pd, uint8_t *buf, int max_len)
{ {
struct osdp_cmd *cmd = NULL; struct osdp_cmd *cmd = NULL;
int i, ret = -1, len = 0; int ret = -1, len = 0;
int data_off = osdp_phy_packet_get_data_offset(pd, buf); int data_off = osdp_phy_packet_get_data_offset(pd, buf);
#ifdef CONFIG_OSDP_SC_ENABLED #ifdef CONFIG_OSDP_SC_ENABLED
uint8_t *smb = osdp_phy_packet_get_smb(pd, buf); uint8_t *smb = osdp_phy_packet_get_smb(pd, buf);
@ -232,9 +232,8 @@ static int cp_build_command(struct osdp_pd *pd, uint8_t *buf, int max_len)
buf[len++] = cmd->text.offset_row; buf[len++] = cmd->text.offset_row;
buf[len++] = cmd->text.offset_col; buf[len++] = cmd->text.offset_col;
buf[len++] = cmd->text.length; buf[len++] = cmd->text.length;
for (i = 0; i < cmd->text.length; i++) { memcpy(buf + len, cmd->text.data, cmd->text.length);
buf[len++] = cmd->text.data[i]; len += cmd->text.length;
}
ret = 0; ret = 0;
break; break;
case CMD_COMSET: case CMD_COMSET:
@ -272,9 +271,8 @@ static int cp_build_command(struct osdp_pd *pd, uint8_t *buf, int max_len)
smb[1] = SCS_11; /* type */ smb[1] = SCS_11; /* type */
smb[2] = ISSET_FLAG(pd, PD_FLAG_SC_USE_SCBKD) ? 0 : 1; smb[2] = ISSET_FLAG(pd, PD_FLAG_SC_USE_SCBKD) ? 0 : 1;
buf[len++] = pd->cmd_id; buf[len++] = pd->cmd_id;
for (i = 0; i < 8; i++) { memcpy(buf + len, pd->sc.cp_random, 8);
buf[len++] = pd->sc.cp_random[i]; len += 8;
}
ret = 0; ret = 0;
break; break;
case CMD_SCRYPT: case CMD_SCRYPT:
@ -287,9 +285,8 @@ static int cp_build_command(struct osdp_pd *pd, uint8_t *buf, int max_len)
smb[1] = SCS_13; /* type */ smb[1] = SCS_13; /* type */
smb[2] = ISSET_FLAG(pd, PD_FLAG_SC_USE_SCBKD) ? 0 : 1; smb[2] = ISSET_FLAG(pd, PD_FLAG_SC_USE_SCBKD) ? 0 : 1;
buf[len++] = pd->cmd_id; buf[len++] = pd->cmd_id;
for (i = 0; i < 16; i++) { memcpy(buf + len, pd->sc.cp_cryptogram, 16);
buf[len++] = pd->sc.cp_cryptogram[i]; len += 16;
}
ret = 0; ret = 0;
break; break;
#endif /* CONFIG_OSDP_SC_ENABLED */ #endif /* CONFIG_OSDP_SC_ENABLED */
@ -321,7 +318,7 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
{ {
uint32_t temp32; uint32_t temp32;
struct osdp *ctx = pd_to_osdp(pd); struct osdp *ctx = pd_to_osdp(pd);
int i, ret = OSDP_CP_ERR_GENERIC, pos = 0, t1, t2; int ret = OSDP_CP_ERR_GENERIC, pos = 0, t1, t2;
struct osdp_event event; struct osdp_event event;
if (len < 1) { if (len < 1) {
@ -443,9 +440,7 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
if ((len - REPLY_KEYPPAD_DATA_LEN) != event.keypress.length) { if ((len - REPLY_KEYPPAD_DATA_LEN) != event.keypress.length) {
break; break;
} }
for (i = 0; i < event.keypress.length; i++) { memcpy(event.keypress.data, buf + pos, event.keypress.length);
event.keypress.data[i] = buf[pos + i];
}
ctx->event_callback(ctx->event_callback_arg, pd->idx, &event); ctx->event_callback(ctx->event_callback_arg, pd->idx, &event);
ret = OSDP_CP_ERR_NONE; ret = OSDP_CP_ERR_NONE;
break; break;
@ -463,9 +458,7 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
if (t1 != (len - REPLY_RAW_DATA_LEN)) { if (t1 != (len - REPLY_RAW_DATA_LEN)) {
break; break;
} }
for (i = 0; i < t1; i++) { memcpy(event.cardread.data, buf + pos, t1);
event.cardread.data[i] = buf[pos + i];
}
ctx->event_callback(ctx->event_callback_arg, pd->idx, &event); ctx->event_callback(ctx->event_callback_arg, pd->idx, &event);
ret = OSDP_CP_ERR_NONE; ret = OSDP_CP_ERR_NONE;
break; break;
@ -482,9 +475,7 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
event.cardread.length > OSDP_EVENT_MAX_DATALEN) { event.cardread.length > OSDP_EVENT_MAX_DATALEN) {
break; break;
} }
for (i = 0; i < event.cardread.length; i++) { memcpy(event.cardread.data, buf + pos, event.cardread.length);
event.cardread.data[i] = buf[pos + i];
}
ctx->event_callback(ctx->event_callback_arg, pd->idx, &event); ctx->event_callback(ctx->event_callback_arg, pd->idx, &event);
ret = OSDP_CP_ERR_NONE; ret = OSDP_CP_ERR_NONE;
break; break;
@ -500,15 +491,10 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
if (len != REPLY_CCRYPT_DATA_LEN) { if (len != REPLY_CCRYPT_DATA_LEN) {
break; break;
} }
for (i = 0; i < 8; i++) { memcpy(pd->sc.pd_client_uid, buf + pos, 8);
pd->sc.pd_client_uid[i] = buf[pos++]; memcpy(pd->sc.pd_random, buf + pos + 8, 8);
} memcpy(pd->sc.pd_cryptogram, buf + pos + 16, 16);
for (i = 0; i < 8; i++) { pos += 32;
pd->sc.pd_random[i] = buf[pos++];
}
for (i = 0; i < 16; i++) {
pd->sc.pd_cryptogram[i] = buf[pos++];
}
osdp_compute_session_keys(pd); osdp_compute_session_keys(pd);
if (osdp_verify_pd_cryptogram(pd) != 0) { if (osdp_verify_pd_cryptogram(pd) != 0) {
LOG_ERR("Failed to verify PD cryptogram"); LOG_ERR("Failed to verify PD cryptogram");
@ -520,9 +506,7 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
if (len != REPLY_RMAC_I_DATA_LEN) { if (len != REPLY_RMAC_I_DATA_LEN) {
break; break;
} }
for (i = 0; i < 16; i++) { memcpy(pd->sc.r_mac, buf + pos, 16);
pd->sc.r_mac[i] = buf[pos++];
}
sc_activate(pd); sc_activate(pd);
ret = OSDP_CP_ERR_NONE; ret = OSDP_CP_ERR_NONE;
break; break;

View file

@ -258,7 +258,7 @@ static int pd_cmd_cap_ok(struct osdp_pd *pd, struct osdp_cmd *cmd)
static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
{ {
int ret = OSDP_PD_ERR_GENERIC; int ret = OSDP_PD_ERR_GENERIC;
int i, pos = 0; int pos = 0;
struct osdp_cmd cmd; struct osdp_cmd cmd;
struct osdp_event *event; struct osdp_event *event;
@ -419,9 +419,7 @@ static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
cmd.text.length > OSDP_CMD_TEXT_MAX_LEN) { cmd.text.length > OSDP_CMD_TEXT_MAX_LEN) {
break; break;
} }
for (i = 0; i < cmd.text.length; i++) { memcpy(cmd.text.data, buf + pos, cmd.text.length);
cmd.text.data[i] = buf[pos++];
}
ret = OSDP_PD_ERR_REPLY; ret = OSDP_PD_ERR_REPLY;
if (!pd_cmd_cap_ok(pd, &cmd)) { if (!pd_cmd_cap_ok(pd, &cmd)) {
break; break;
@ -512,9 +510,7 @@ static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
} }
osdp_sc_init(pd); osdp_sc_init(pd);
sc_deactivate(pd); sc_deactivate(pd);
for (i = 0; i < 8; i++) { memcpy(pd->sc.cp_random, buf + pos, 8);
pd->sc.cp_random[i] = buf[pos++];
}
pd->reply_id = REPLY_CCRYPT; pd->reply_id = REPLY_CCRYPT;
ret = OSDP_PD_ERR_NONE; ret = OSDP_PD_ERR_NONE;
break; break;
@ -532,9 +528,7 @@ static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
LOG_ERR("Out of order CMD_SCRYPT; has CP gone rogue?"); LOG_ERR("Out of order CMD_SCRYPT; has CP gone rogue?");
break; break;
} }
for (i = 0; i < CMD_SCRYPT_DATA_LEN; i++) { memcpy(pd->sc.cp_cryptogram, buf + pos, CMD_SCRYPT_DATA_LEN);
pd->sc.cp_cryptogram[i] = buf[pos++];
}
pd->reply_id = REPLY_RMAC_I; pd->reply_id = REPLY_RMAC_I;
ret = OSDP_PD_ERR_NONE; ret = OSDP_PD_ERR_NONE;
break; break;
@ -650,9 +644,8 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len)
buf[len++] = pd->reply_id; buf[len++] = pd->reply_id;
buf[len++] = (uint8_t)event->keypress.reader_no; buf[len++] = (uint8_t)event->keypress.reader_no;
buf[len++] = (uint8_t)event->keypress.length; buf[len++] = (uint8_t)event->keypress.length;
for (i = 0; i < event->keypress.length; i++) { memcpy(buf + len, event->keypress.data, event->keypress.length);
buf[len++] = event->keypress.data[i]; len += event->keypress.length;
}
ret = OSDP_PD_ERR_NONE; ret = OSDP_PD_ERR_NONE;
break; break;
case REPLY_RAW: { case REPLY_RAW: {
@ -666,9 +659,8 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len)
buf[len++] = (uint8_t)event->cardread.format; buf[len++] = (uint8_t)event->cardread.format;
buf[len++] = BYTE_0(event->cardread.length); buf[len++] = BYTE_0(event->cardread.length);
buf[len++] = BYTE_1(event->cardread.length); buf[len++] = BYTE_1(event->cardread.length);
for (i = 0; i < len_bytes; i++) { memcpy(buf + len, event->cardread.data, len_bytes);
buf[len++] = event->cardread.data[i]; len += len_bytes;
}
ret = OSDP_PD_ERR_NONE; ret = OSDP_PD_ERR_NONE;
break; break;
} }
@ -679,9 +671,8 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len)
buf[len++] = (uint8_t)event->cardread.reader_no; buf[len++] = (uint8_t)event->cardread.reader_no;
buf[len++] = (uint8_t)event->cardread.direction; buf[len++] = (uint8_t)event->cardread.direction;
buf[len++] = (uint8_t)event->cardread.length; buf[len++] = (uint8_t)event->cardread.length;
for (i = 0; i < event->cardread.length; i++) { memcpy(buf + len, event->cardread.data, event->cardread.length);
buf[len++] = event->cardread.data[i]; len += event->cardread.length;
}
ret = OSDP_PD_ERR_NONE; ret = OSDP_PD_ERR_NONE;
break; break;
case REPLY_COM: case REPLY_COM:
@ -726,15 +717,10 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len)
osdp_compute_session_keys(pd); osdp_compute_session_keys(pd);
osdp_compute_pd_cryptogram(pd); osdp_compute_pd_cryptogram(pd);
buf[len++] = pd->reply_id; buf[len++] = pd->reply_id;
for (i = 0; i < 8; i++) { memcpy(buf + len, pd->sc.pd_client_uid, 8);
buf[len++] = pd->sc.pd_client_uid[i]; memcpy(buf + len + 8, pd->sc.pd_random, 8);
} memcpy(buf + len + 16, pd->sc.pd_cryptogram, 16);
for (i = 0; i < 8; i++) { len += 32;
buf[len++] = pd->sc.pd_random[i];
}
for (i = 0; i < 16; i++) {
buf[len++] = pd->sc.pd_cryptogram[i];
}
smb[0] = 3; /* length */ smb[0] = 3; /* length */
smb[1] = SCS_12; /* type */ smb[1] = SCS_12; /* type */
smb[2] = ISSET_FLAG(pd, PD_FLAG_SC_USE_SCBKD) ? 0 : 1; smb[2] = ISSET_FLAG(pd, PD_FLAG_SC_USE_SCBKD) ? 0 : 1;
@ -747,9 +733,8 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len)
assert_buf_len(REPLY_RMAC_I_LEN, max_len); assert_buf_len(REPLY_RMAC_I_LEN, max_len);
osdp_compute_rmac_i(pd); osdp_compute_rmac_i(pd);
buf[len++] = pd->reply_id; buf[len++] = pd->reply_id;
for (i = 0; i < 16; i++) { memcpy(buf + len, pd->sc.r_mac, 16);
buf[len++] = pd->sc.r_mac[i]; len += 16;
}
smb[0] = 3; /* length */ smb[0] = 3; /* length */
smb[1] = SCS_14; /* type */ smb[1] = SCS_14; /* type */
if (osdp_verify_cp_cryptogram(pd) == 0) { if (osdp_verify_cp_cryptogram(pd) == 0) {

View file

@ -231,9 +231,7 @@ int osdp_phy_packet_finalize(struct osdp_pd *pd, uint8_t *buf,
/* compute and extend the buf with 4 MAC bytes */ /* compute and extend the buf with 4 MAC bytes */
osdp_compute_mac(pd, is_cp_mode(pd), buf, len); osdp_compute_mac(pd, is_cp_mode(pd), buf, len);
data = is_cp_mode(pd) ? pd->sc.c_mac : pd->sc.r_mac; data = is_cp_mode(pd) ? pd->sc.c_mac : pd->sc.r_mac;
for (i = 0; i < 4; i++) { memcpy(buf + len, data, 4);
buf[len + i] = data[i];
}
len += 4; len += 4;
} }
#endif /* CONFIG_OSDP_SC_ENABLED */ #endif /* CONFIG_OSDP_SC_ENABLED */