From 4f28e77ed21e75fcfb30e2f8d1d2b9b1657f44c6 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 2 Oct 2014 15:28:54 +0100 Subject: [PATCH] Use memcpy helpers and FILLZERO Replace various calls to memcpy and memset with equivalent calls to these macros. There are still a couple of open-coded memcpy(,buf_unprepend(),) in udp.c's proxy code which will be done later. Signed-off-by: Ian Jackson --- netlink.c | 4 ++-- site.c | 10 +++++----- transform-cbcmac.c | 14 +++++++------- transform-eax.c | 2 +- util.c | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/netlink.c b/netlink.c index b54327b..07c1468 100644 --- a/netlink.c +++ b/netlink.c @@ -430,7 +430,7 @@ static void netlink_icmp_simple(struct netlink *st, len=netlink_icmp_reply_len(buf); h=netlink_icmp_tmpl(st,icmpsource,icmpdest,len); h->type=type; h->code=code; h->d=info; - memcpy(buf_append(&st->icmp,len),buf->start,len); + BUF_ADD_BYTES(append,&st->icmp,buf->start,len); netlink_icmp_csum(h); if (!st->ptp) { @@ -591,7 +591,7 @@ static void netlink_maybe_fragment(struct netlink *st, long avail = mtu - hl; long remain = endindata - indata; long use = avail < remain ? (avail & ~(long)7) : remain; - memcpy(buf_append(buf, use), indata, use); + BUF_ADD_BYTES(append, buf, indata, use); indata += use; _Bool last_frag = indata >= endindata; diff --git a/site.c b/site.c index bfca294..cb5b4ed 100644 --- a/site.c +++ b/site.c @@ -549,9 +549,9 @@ static bool_t generate_msg(struct site *st, uint32_t type, cstring_t what) append_string_xinfo_done(&st->buffer,&xia); buf_append_string(&st->buffer,st->remotename); - memcpy(buf_append(&st->buffer,NONCELEN),st->localN,NONCELEN); + BUF_ADD_OBJ(append,&st->buffer,st->localN); if (type==LABEL_MSG1) return True; - memcpy(buf_append(&st->buffer,NONCELEN),st->remoteN,NONCELEN); + BUF_ADD_OBJ(append,&st->buffer,st->remoteN); if (type==LABEL_MSG2) return True; if (hacky_par_mid_failnow()) return False; @@ -1328,8 +1328,8 @@ static void enter_state_run(struct site *st) st->setup_session_id=0; transport_peers_clear(st,&st->setup_peers); - memset(st->localN,0,NONCELEN); - memset(st->remoteN,0,NONCELEN); + FILLZERO(st->localN); + FILLZERO(st->remoteN); dispose_transform(&st->new_transform); memset(st->dhsecret,0,st->dh->len); memset(st->sharedsecret,0,st->sharedsecretlen); @@ -2144,7 +2144,7 @@ static void transport_peers_clear(struct site *st, transport_peers *peers) { static void transport_peers_copy(struct site *st, transport_peers *dst, const transport_peers *src) { dst->npeers=src->npeers; - memcpy(dst->peers, src->peers, sizeof(*dst->peers) * dst->npeers); + COPY_ARRAY(dst->peers, src->peers, dst->npeers); transport_peers_debug(st,dst,"copy", src->npeers, &src->peers->addr, sizeof(*src->peers)); } diff --git a/transform-cbcmac.c b/transform-cbcmac.c index 26e0a12..1390ee8 100644 --- a/transform-cbcmac.c +++ b/transform-cbcmac.c @@ -114,7 +114,7 @@ static uint32_t transform_forward(void *sst, struct buffer_if *buf, bother sending the IV - it's the same each time. (If we wanted to send it we've have to add 16 bytes to each message, not 4, so that the message stays a multiple of 16 bytes long.) */ - memset(iv,0,16); + FILLZERO(iv); put_uint32(iv, ti->maciv); serpentbe_encrypt(&ti->mackey,iv,macacc); @@ -127,11 +127,11 @@ static uint32_t transform_forward(void *sst, struct buffer_if *buf, serpentbe_encrypt(&ti->mackey,macplain,macacc); } serpentbe_encrypt(&ti->mackey,macacc,macacc); - memcpy(buf_append(buf,16),macacc,16); + BUF_ADD_BYTES(append,buf,macacc,16); /* Serpent-CBC. We expand the ID as for CBCMAC, do the encryption, and prepend the IV before increasing it. */ - memset(iv,0,16); + FILLZERO(iv); put_uint32(iv, ti->cryptiv); serpentbe_encrypt(&ti->cryptkey,iv,iv); @@ -175,7 +175,7 @@ static uint32_t transform_reverse(void *sst, struct buffer_if *buf, } /* CBC */ - memset(iv,0,16); + FILLZERO(iv); { uint32_t ivword = buf_unprepend_uint32(buf); put_uint32(iv, ivword); @@ -193,12 +193,12 @@ static uint32_t transform_reverse(void *sst, struct buffer_if *buf, serpentbe_decrypt(&ti->cryptkey,n,n); for (i = 0; i < 16; i++) n[i] ^= iv[i]; - memcpy(iv, pct, 16); + COPY_OBJ(iv, pct); } /* CBCMAC */ macexpected=buf_unappend(buf,16); - memset(iv,0,16); + FILLZERO(iv); put_uint32(iv, ti->maciv); serpentbe_encrypt(&ti->mackey,iv,macacc); @@ -344,7 +344,7 @@ void transform_cbcmac_module(dict_t *dict) buf.base = malloc(4096); buffer_init(&buf, 2048); - memcpy(buf_append(&buf, sizeof(text)), text, sizeof(text)); + BUF_ADD_OBJ(append, buf, text, sizeof(text)); if (transform_forward(ti, &buf, &errmsg)) { fatal("transform_forward test: %s", errmsg); } diff --git a/transform-eax.c b/transform-eax.c index f881abb..46dc879 100644 --- a/transform-eax.c +++ b/transform-eax.c @@ -179,7 +179,7 @@ static uint32_t transform_forward(void *sst, struct buffer_if *buf, TEAX_DEBUG(buf->start,buf->size); - memcpy(buf_append(buf,SEQLEN), nonce, SEQLEN); + BUF_ADD_BYTES(append,buf,nonce,SEQLEN); TEAX_DEBUG(nonce,SEQLEN); diff --git a/util.c b/util.c index aa85559..94f9e35 100644 --- a/util.c +++ b/util.c @@ -303,7 +303,7 @@ void buf_append_string(struct buffer_if *buf, cstring_t s) len=strlen(s); /* fixme: if string is longer than 65535, result is a corrupted packet */ buf_append_uint16(buf,len); - memcpy(buf_append(buf,len),s,len); + BUF_ADD_BYTES(append,buf,s,len); } void buffer_new(struct buffer_if *buf, int32_t len) -- 2.30.2