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)
{
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);
#ifdef CONFIG_OSDP_SC_ENABLED
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_col;
buf[len++] = cmd->text.length;
for (i = 0; i < cmd->text.length; i++) {
buf[len++] = cmd->text.data[i];
}
memcpy(buf + len, cmd->text.data, cmd->text.length);
len += cmd->text.length;
ret = 0;
break;
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[2] = ISSET_FLAG(pd, PD_FLAG_SC_USE_SCBKD) ? 0 : 1;
buf[len++] = pd->cmd_id;
for (i = 0; i < 8; i++) {
buf[len++] = pd->sc.cp_random[i];
}
memcpy(buf + len, pd->sc.cp_random, 8);
len += 8;
ret = 0;
break;
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[2] = ISSET_FLAG(pd, PD_FLAG_SC_USE_SCBKD) ? 0 : 1;
buf[len++] = pd->cmd_id;
for (i = 0; i < 16; i++) {
buf[len++] = pd->sc.cp_cryptogram[i];
}
memcpy(buf + len, pd->sc.cp_cryptogram, 16);
len += 16;
ret = 0;
break;
#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;
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;
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) {
break;
}
for (i = 0; i < event.keypress.length; i++) {
event.keypress.data[i] = buf[pos + i];
}
memcpy(event.keypress.data, buf + pos, event.keypress.length);
ctx->event_callback(ctx->event_callback_arg, pd->idx, &event);
ret = OSDP_CP_ERR_NONE;
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)) {
break;
}
for (i = 0; i < t1; i++) {
event.cardread.data[i] = buf[pos + i];
}
memcpy(event.cardread.data, buf + pos, t1);
ctx->event_callback(ctx->event_callback_arg, pd->idx, &event);
ret = OSDP_CP_ERR_NONE;
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) {
break;
}
for (i = 0; i < event.cardread.length; i++) {
event.cardread.data[i] = buf[pos + i];
}
memcpy(event.cardread.data, buf + pos, event.cardread.length);
ctx->event_callback(ctx->event_callback_arg, pd->idx, &event);
ret = OSDP_CP_ERR_NONE;
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) {
break;
}
for (i = 0; i < 8; i++) {
pd->sc.pd_client_uid[i] = buf[pos++];
}
for (i = 0; i < 8; i++) {
pd->sc.pd_random[i] = buf[pos++];
}
for (i = 0; i < 16; i++) {
pd->sc.pd_cryptogram[i] = buf[pos++];
}
memcpy(pd->sc.pd_client_uid, buf + pos, 8);
memcpy(pd->sc.pd_random, buf + pos + 8, 8);
memcpy(pd->sc.pd_cryptogram, buf + pos + 16, 16);
pos += 32;
osdp_compute_session_keys(pd);
if (osdp_verify_pd_cryptogram(pd) != 0) {
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) {
break;
}
for (i = 0; i < 16; i++) {
pd->sc.r_mac[i] = buf[pos++];
}
memcpy(pd->sc.r_mac, buf + pos, 16);
sc_activate(pd);
ret = OSDP_CP_ERR_NONE;
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)
{
int ret = OSDP_PD_ERR_GENERIC;
int i, pos = 0;
int pos = 0;
struct osdp_cmd cmd;
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) {
break;
}
for (i = 0; i < cmd.text.length; i++) {
cmd.text.data[i] = buf[pos++];
}
memcpy(cmd.text.data, buf + pos, cmd.text.length);
ret = OSDP_PD_ERR_REPLY;
if (!pd_cmd_cap_ok(pd, &cmd)) {
break;
@ -512,9 +510,7 @@ static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
}
osdp_sc_init(pd);
sc_deactivate(pd);
for (i = 0; i < 8; i++) {
pd->sc.cp_random[i] = buf[pos++];
}
memcpy(pd->sc.cp_random, buf + pos, 8);
pd->reply_id = REPLY_CCRYPT;
ret = OSDP_PD_ERR_NONE;
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?");
break;
}
for (i = 0; i < CMD_SCRYPT_DATA_LEN; i++) {
pd->sc.cp_cryptogram[i] = buf[pos++];
}
memcpy(pd->sc.cp_cryptogram, buf + pos, CMD_SCRYPT_DATA_LEN);
pd->reply_id = REPLY_RMAC_I;
ret = OSDP_PD_ERR_NONE;
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++] = (uint8_t)event->keypress.reader_no;
buf[len++] = (uint8_t)event->keypress.length;
for (i = 0; i < event->keypress.length; i++) {
buf[len++] = event->keypress.data[i];
}
memcpy(buf + len, event->keypress.data, event->keypress.length);
len += event->keypress.length;
ret = OSDP_PD_ERR_NONE;
break;
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++] = BYTE_0(event->cardread.length);
buf[len++] = BYTE_1(event->cardread.length);
for (i = 0; i < len_bytes; i++) {
buf[len++] = event->cardread.data[i];
}
memcpy(buf + len, event->cardread.data, len_bytes);
len += len_bytes;
ret = OSDP_PD_ERR_NONE;
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.direction;
buf[len++] = (uint8_t)event->cardread.length;
for (i = 0; i < event->cardread.length; i++) {
buf[len++] = event->cardread.data[i];
}
memcpy(buf + len, event->cardread.data, event->cardread.length);
len += event->cardread.length;
ret = OSDP_PD_ERR_NONE;
break;
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_pd_cryptogram(pd);
buf[len++] = pd->reply_id;
for (i = 0; i < 8; i++) {
buf[len++] = pd->sc.pd_client_uid[i];
}
for (i = 0; i < 8; i++) {
buf[len++] = pd->sc.pd_random[i];
}
for (i = 0; i < 16; i++) {
buf[len++] = pd->sc.pd_cryptogram[i];
}
memcpy(buf + len, pd->sc.pd_client_uid, 8);
memcpy(buf + len + 8, pd->sc.pd_random, 8);
memcpy(buf + len + 16, pd->sc.pd_cryptogram, 16);
len += 32;
smb[0] = 3; /* length */
smb[1] = SCS_12; /* type */
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);
osdp_compute_rmac_i(pd);
buf[len++] = pd->reply_id;
for (i = 0; i < 16; i++) {
buf[len++] = pd->sc.r_mac[i];
}
memcpy(buf + len, pd->sc.r_mac, 16);
len += 16;
smb[0] = 3; /* length */
smb[1] = SCS_14; /* type */
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 */
osdp_compute_mac(pd, is_cp_mode(pd), buf, len);
data = is_cp_mode(pd) ? pd->sc.c_mac : pd->sc.r_mac;
for (i = 0; i < 4; i++) {
buf[len + i] = data[i];
}
memcpy(buf + len, data, 4);
len += 4;
}
#endif /* CONFIG_OSDP_SC_ENABLED */