chiark / gitweb /
Use memcpy helpers and FILLZERO
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 2 Oct 2014 14:28:54 +0000 (15:28 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 5 Oct 2014 20:06:27 +0000 (21:06 +0100)
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 <ijackson@chiark.greenend.org.uk>
netlink.c
site.c
transform-cbcmac.c
transform-eax.c
util.c

index b54327bbb4aeb0efa94548a6b63ca98c89bd7077..07c1468488ad42235ad37457fbec903059159dcb 100644 (file)
--- 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;
        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) {
        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;
        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;
        indata += use;
 
        _Bool last_frag = indata >= endindata;
diff --git a/site.c b/site.c
index bfca29466654bdcd2e6e0de88e46dcd610aa0922..cb5b4edbc7e5fa805960d4e86358bc5544caa8a8 100644 (file)
--- 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);
     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;
     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;
     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);
 
     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);
     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;
 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));
 }
     transport_peers_debug(st,dst,"copy",
                          src->npeers, &src->peers->addr, sizeof(*src->peers));
 }
index 26e0a127d37cf8dcd7297eab6768795811453629..1390ee884a9ff70eefb5e17b970be05637839033 100644 (file)
@@ -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.) */
        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);
 
     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);
        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. */
 
     /* 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);
 
     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 */
     }
 
     /* CBC */
-    memset(iv,0,16);
+    FILLZERO(iv);
     {
        uint32_t ivword = buf_unprepend_uint32(buf);
        put_uint32(iv, ivword);
     {
        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];
        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);
     }
 
     /* CBCMAC */
     macexpected=buf_unappend(buf,16);
-    memset(iv,0,16);
+    FILLZERO(iv);
     put_uint32(iv, ti->maciv);
     serpentbe_encrypt(&ti->mackey,iv,macacc);
 
     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);
 
         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);
        }
        if (transform_forward(ti, &buf, &errmsg)) {
            fatal("transform_forward test: %s", errmsg);
        }
index f881abb9eb424ec444da7ce091e8a038592760f6..46dc879c69ffcb1c6f21c71aea28424cc28322c5 100644 (file)
@@ -179,7 +179,7 @@ static uint32_t transform_forward(void *sst, struct buffer_if *buf,
 
     TEAX_DEBUG(buf->start,buf->size);
 
 
     TEAX_DEBUG(buf->start,buf->size);
 
-    memcpy(buf_append(buf,SEQLEN), nonce, SEQLEN);
+    BUF_ADD_BYTES(append,buf,nonce,SEQLEN);
 
     TEAX_DEBUG(nonce,SEQLEN);
 
 
     TEAX_DEBUG(nonce,SEQLEN);
 
diff --git a/util.c b/util.c
index aa85559b37be4113800c623e8dbae4dfcd0ab716..94f9e35679acf34c7d500fade5e25919087cd9cc 100644 (file)
--- 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);
     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)
 }
 
 void buffer_new(struct buffer_if *buf, int32_t len)