diff options
author | 2014-12-12 18:15:51 +0000 | |
---|---|---|
committer | 2014-12-12 18:15:51 +0000 | |
commit | 7e8b30f9cc0c34cf8fd9f52161472cafedc669a9 (patch) | |
tree | 854bf50605b41142570c6d05a83e272f62158c50 | |
parent | markup fixes from Kaspars at Bankovskis dot net; (diff) | |
download | wireguard-openbsd-7e8b30f9cc0c34cf8fd9f52161472cafedc669a9.tar.xz wireguard-openbsd-7e8b30f9cc0c34cf8fd9f52161472cafedc669a9.zip |
convert some hash tables (the easy ones) to siphash. ok benno.
-rw-r--r-- | usr.sbin/bgpd/rde_attr.c | 39 | ||||
-rw-r--r-- | usr.sbin/bgpd/rde_rib.c | 8 |
2 files changed, 32 insertions, 15 deletions
diff --git a/usr.sbin/bgpd/rde_attr.c b/usr.sbin/bgpd/rde_attr.c index 8a839488cd4..3f0c29b6386 100644 --- a/usr.sbin/bgpd/rde_attr.c +++ b/usr.sbin/bgpd/rde_attr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rde_attr.c,v 1.92 2014/10/08 16:15:37 deraadt Exp $ */ +/* $OpenBSD: rde_attr.c,v 1.93 2014/12/12 18:15:51 tedu Exp $ */ /* * Copyright (c) 2004 Claudio Jeker <claudio@openbsd.org> @@ -17,7 +17,6 @@ */ #include <sys/types.h> -#include <sys/hash.h> #include <sys/queue.h> #include <netinet/in.h> @@ -26,6 +25,7 @@ #include <stdlib.h> #include <stdio.h> #include <string.h> +#include <siphash.h> #include "bgpd.h" #include "rde.h" @@ -99,6 +99,8 @@ struct attr_table { u_int32_t hashmask; } attrtable; +SIPHASH_KEY attrtablekey; + #define ATTR_HASH(x) \ &attrtable.hashtbl[(x) & attrtable.hashmask] @@ -107,6 +109,7 @@ attr_init(u_int32_t hashsize) { u_int32_t hs, i; + arc4random_buf(&attrtablekey, sizeof(attrtablekey)); for (hs = 1; hs < hashsize; hs <<= 1) ; attrtable.hashtbl = calloc(hs, sizeof(struct attr_list)); @@ -316,6 +319,7 @@ struct attr * attr_alloc(u_int8_t flags, u_int8_t type, const void *data, u_int16_t len) { struct attr *a; + SIPHASH_CTX ctx; a = calloc(1, sizeof(struct attr)); if (a == NULL) @@ -324,9 +328,7 @@ attr_alloc(u_int8_t flags, u_int8_t type, const void *data, u_int16_t len) flags &= ~ATTR_DEFMASK; /* normalize mask */ a->flags = flags; - a->hash = hash32_buf(&flags, sizeof(flags), HASHINIT); a->type = type; - a->hash = hash32_buf(&type, sizeof(type), a->hash); a->len = len; if (len != 0) { if ((a->data = malloc(len)) == NULL) @@ -338,8 +340,12 @@ attr_alloc(u_int8_t flags, u_int8_t type, const void *data, u_int16_t len) } else a->data = NULL; - a->hash = hash32_buf(&len, sizeof(len), a->hash); - a->hash = hash32_buf(a->data, a->len, a->hash); + SipHash24_Init(&ctx, &attrtablekey); + SipHash24_Update(&ctx, &flags, sizeof(flags)); + SipHash24_Update(&ctx, &type, sizeof(type)); + SipHash24_Update(&ctx, &len, sizeof(len)); + SipHash24_Update(&ctx, a->data, a->len); + a->hash = SipHash24_End(&ctx); LIST_INSERT_HEAD(ATTR_HASH(a->hash), a, entry); return (a); @@ -351,12 +357,16 @@ attr_lookup(u_int8_t flags, u_int8_t type, const void *data, u_int16_t len) struct attr_list *head; struct attr *a; u_int32_t hash; + SIPHASH_CTX ctx; flags &= ~ATTR_DEFMASK; /* normalize mask */ - hash = hash32_buf(&flags, sizeof(flags), HASHINIT); - hash = hash32_buf(&type, sizeof(type), hash); - hash = hash32_buf(&len, sizeof(len), hash); - hash = hash32_buf(data, len, hash); + + SipHash24_Init(&ctx, &attrtablekey); + SipHash24_Update(&ctx, &flags, sizeof(flags)); + SipHash24_Update(&ctx, &type, sizeof(type)); + SipHash24_Update(&ctx, &len, sizeof(len)); + SipHash24_Update(&ctx, data, len); + hash = SipHash24_End(&ctx); head = ATTR_HASH(hash); LIST_FOREACH(a, head, entry) { @@ -402,6 +412,8 @@ struct aspath_table { u_int32_t hashmask; } astable; +SIPHASH_KEY astablekey; + #define ASPATH_HASH(x) \ &astable.hashtbl[(x) & astable.hashmask] @@ -464,6 +476,7 @@ aspath_init(u_int32_t hashsize) LIST_INIT(&astable.hashtbl[i]); astable.hashmask = hs - 1; + arc4random_buf(&astablekey, sizeof(astablekey)); } void @@ -500,8 +513,8 @@ aspath_get(void *data, u_int16_t len) memcpy(aspath->data, data, len); /* link */ - head = ASPATH_HASH(hash32_buf(aspath->data, aspath->len, - HASHINIT)); + head = ASPATH_HASH(SipHash24(&astablekey, aspath->data, + aspath->len)); LIST_INSERT_HEAD(head, aspath, entry); } aspath->refcnt++; @@ -831,7 +844,7 @@ aspath_lookup(const void *data, u_int16_t len) struct aspath *aspath; u_int32_t hash; - hash = hash32_buf(data, len, HASHINIT); + hash = SipHash24(&astablekey, data, len); head = ASPATH_HASH(hash); LIST_FOREACH(aspath, head, entry) { diff --git a/usr.sbin/bgpd/rde_rib.c b/usr.sbin/bgpd/rde_rib.c index d1cc5430508..8844802e2ba 100644 --- a/usr.sbin/bgpd/rde_rib.c +++ b/usr.sbin/bgpd/rde_rib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rde_rib.c,v 1.139 2014/10/08 16:15:37 deraadt Exp $ */ +/* $OpenBSD: rde_rib.c,v 1.140 2014/12/12 18:15:51 tedu Exp $ */ /* * Copyright (c) 2003, 2004 Claudio Jeker <claudio@openbsd.org> @@ -22,6 +22,7 @@ #include <stdlib.h> #include <string.h> +#include <siphash.h> #include "bgpd.h" #include "rde.h" @@ -357,9 +358,11 @@ static void path_link(struct rde_aspath *, struct rde_peer *); struct path_table pathtable; +SIPHASH_KEY pathtablekey; + /* XXX the hash should also include communities and the other attrs */ #define PATH_HASH(x) \ - &pathtable.path_hashtbl[hash32_buf((x)->data, (x)->len, HASHINIT) & \ + &pathtable.path_hashtbl[SipHash24(&pathtablekey, (x)->data, (x)->len) & \ pathtable.path_hashmask] void @@ -377,6 +380,7 @@ path_init(u_int32_t hashsize) LIST_INIT(&pathtable.path_hashtbl[i]); pathtable.path_hashmask = hs - 1; + arc4random_buf(&pathtablekey, sizeof(pathtablekey)); } void |