summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatt Dunwoodie <ncon@mail.noconroy.net>2019-07-14 16:13:55 +1000
committerMatt Dunwoodie <ncon@mail.noconroy.net>2019-07-14 16:13:55 +1000
commit379c7320868edb75e0bea04eb30e959b4525de94 (patch)
tree4b6f1b8c20645e74fee7f55e1af6e750e201f9dd /src
parentReset p_init_count when requesing initialisation (diff)
downloadwireguard-openbsd-379c7320868edb75e0bea04eb30e959b4525de94.tar.xz
wireguard-openbsd-379c7320868edb75e0bea04eb30e959b4525de94.zip
Add keyset struct, make compilable
Diffstat (limited to 'src')
-rw-r--r--src/if_wg.c10
-rw-r--r--src/wireguard.c34
-rw-r--r--src/wireguard.h10
3 files changed, 29 insertions, 25 deletions
diff --git a/src/if_wg.c b/src/if_wg.c
index 525aef56f34..5316b2d0f13 100644
--- a/src/if_wg.c
+++ b/src/if_wg.c
@@ -294,7 +294,7 @@ uint32_t
wg_id_get(void *_sc, struct wg_conn *c)
{
struct wg_softc *sc = _sc;
- struct wg_peer *p = wg_peer_lookup(sc, c->c_pubkey);
+ struct wg_peer *p = wg_peer_lookup(sc, c->c_ks.k_pubkey);
struct wg_id_item item = { arc4random() , p };
HASHMAP_SET(&sc->sc_id_hashmap, item);
return item.id;
@@ -373,7 +373,7 @@ wg_peer_destroy(struct wg_softc *sc, struct wg_peer *p)
wg_timer_cleanup(p);
/* Remove from key hashmap */
- memcpy(item.key, p->p_conn.c_pubkey, WG_KEY_SIZE);
+ memcpy(item.key, p->p_conn.c_ks.k_pubkey, WG_KEY_SIZE);
HASHMAP_UNSET(&sc->sc_peer_hashmap, item);
/* Barrier on packets */
@@ -410,7 +410,7 @@ void
wg_peer_session_notify(void *_sc, struct wg_conn *c)
{
struct wg_softc *sc = _sc;
- struct wg_peer *p = wg_peer_lookup(sc, c->c_pubkey);
+ struct wg_peer *p = wg_peer_lookup(sc, c->c_ks.k_pubkey);
struct mbuf *m;
wg_timer_reinit_unflag(&p->p_timers);
@@ -619,7 +619,7 @@ wg_receive_slow(struct wg_pkt *pkt)
if ((c = wg_conn_recv_initiation(&pkt->p_sc->sc_upcall, init)) == NULL)
goto dead;
- pkt->p_p = wg_peer_lookup(pkt->p_sc, c->c_pubkey);
+ pkt->p_p = wg_peer_lookup(pkt->p_sc, c->c_ks.k_pubkey);
wg_send_pkt(pkt->p_p, WG_PKT_RESPONSE);
break;
case WG_PKT_RESPONSE:
@@ -1256,7 +1256,7 @@ wg_ioctl_get_peer(struct wg_softc *sc, struct wg_get_peer *wgp)
/* We only want to pass the preshared key to root */
if (!suser(curproc))
- memcpy(wgp->gp_psk, p->p_conn.c_psk, sizeof(wgp->gp_psk));
+ memcpy(wgp->gp_psk, p->p_conn.c_ks.k_psk, sizeof(wgp->gp_psk));
else
bzero(wgp->gp_psk, sizeof(wgp->gp_psk));
diff --git a/src/wireguard.c b/src/wireguard.c
index 453a428ab29..97bf7b821ca 100644
--- a/src/wireguard.c
+++ b/src/wireguard.c
@@ -283,15 +283,15 @@ wg_conn_setkey(struct wg_conn *c, enum wg_keytype type, uint8_t *key)
rw_enter_write(&c->c_lock);
switch (type) {
case WG_KEY_PSK:
- memcpy(c->c_psk, key, sizeof(c->c_psk));
+ memcpy(c->c_ks.k_psk, key, sizeof(c->c_ks.k_psk));
break;
case WG_KEY_LOCAL:
wg_util_key_privtopub(local, key);
- memcpy(c->c_local_pubkey, local, sizeof(c->c_local_pubkey));
- memcpy(c->c_local_privkey, key, sizeof(c->c_local_privkey));
+ memcpy(c->c_ks.k_local_pubkey, local, sizeof(c->c_ks.k_local_pubkey));
+ memcpy(c->c_ks.k_local_privkey, key, sizeof(c->c_ks.k_local_privkey));
break;
case WG_KEY_REMOTE:
- memcpy(c->c_pubkey, key, sizeof(c->c_pubkey));
+ memcpy(c->c_ks.k_pubkey, key, sizeof(c->c_ks.k_pubkey));
break;
}
rw_exit_write(&c->c_lock);
@@ -350,27 +350,27 @@ wg_conn_make_initiation(struct wg_conn *c, struct wg_msg_initiation *m)
memcpy(hs->hs_hash, hs->hs_ck, WG_HASH_SIZE);
wg_mix_hash(hs, WG_IDENTIFIER, strlen(WG_IDENTIFIER));
- wg_mix_hash(hs, c->c_pubkey, WG_KEY_SIZE);
+ wg_mix_hash(hs, c->c_ks.k_pubkey, WG_KEY_SIZE);
wg_kdf(hs->hs_ck, NULL, NULL, hs->hs_ck, hs->hs_local_epub, WG_KEY_SIZE);
wg_mix_hash(hs, hs->hs_local_epub, WG_KEY_SIZE);
- wg_mix_dh(hs, hs->hs_local_epriv, c->c_pubkey);
+ wg_mix_dh(hs, hs->hs_local_epriv, c->c_ks.k_pubkey);
m->type = WG_MSG_INITIATION;
m->sender = hs->hs_local_id;
memcpy(m->ephemeral, hs->hs_local_epub, WG_KEY_SIZE);
- wg_hs_encrypt(hs, m->static_pub, c->c_local_pubkey, WG_KEY_SIZE);
+ wg_hs_encrypt(hs, m->static_pub, c->c_ks.k_local_pubkey, WG_KEY_SIZE);
wg_mix_hash(hs, m->static_pub, WG_ENCRYPTED_SIZE(WG_KEY_SIZE));
- wg_mix_dh(hs, c->c_local_privkey, c->c_pubkey);
+ wg_mix_dh(hs, c->c_ks.k_local_privkey, c->c_ks.k_pubkey);
wg_timestamp_get(&timestamp);
wg_hs_encrypt(hs, m->timestamp, timestamp.ts_bytes, WG_TIMESTAMP_SIZE);
wg_mix_hash(hs, m->timestamp, WG_ENCRYPTED_SIZE(WG_TIMESTAMP_SIZE));
- wg_hash2(hs->hs_k, WG_MAC1, strlen(WG_MAC1), c->c_pubkey, WG_KEY_SIZE);
+ wg_hash2(hs->hs_k, WG_MAC1, strlen(WG_MAC1), c->c_ks.k_pubkey, WG_KEY_SIZE);
blake2s(m->mac1, (void *) m, hs->hs_k, sizeof(m->mac1), offsetof(struct wg_msg_initiation, mac1), sizeof(hs->hs_k));
memcpy(hs->hs_mac, m->mac1, sizeof(hs->hs_mac));
@@ -403,15 +403,15 @@ wg_conn_make_response(struct wg_conn *c, struct wg_msg_response *m)
memcpy(m->ephemeral, hs->hs_local_epub, WG_KEY_SIZE);
wg_mix_dh(hs, hs->hs_local_epriv, hs->hs_remote_epub);
- wg_mix_dh(hs, hs->hs_local_epriv, c->c_pubkey);
+ wg_mix_dh(hs, hs->hs_local_epriv, c->c_ks.k_pubkey);
- wg_mix_psk(hs, c->c_psk);
+ wg_mix_psk(hs, c->c_ks.k_psk);
wg_hs_encrypt(hs, m->empty, NULL, 0);
wg_mix_hash(hs, m->empty, WG_ENCRYPTED_SIZE(0));
- wg_hash2(hs->hs_k, WG_MAC1, strlen(WG_MAC1), c->c_pubkey, WG_KEY_SIZE);
+ wg_hash2(hs->hs_k, WG_MAC1, strlen(WG_MAC1), c->c_ks.k_pubkey, WG_KEY_SIZE);
blake2s(m->mac1, (void *)m, hs->hs_k, sizeof(m->mac1), offsetof(struct wg_msg_response, mac1), sizeof(hs->hs_k));
memcpy(hs->hs_mac, m->mac1, sizeof(hs->hs_mac));
@@ -440,7 +440,7 @@ wg_conn_make_cookie(struct wg_conn *c, struct wg_msg_cookie *m)
m->receiver = c->c_hs.hs_remote_id;
arc4random_buf(m->nonce, sizeof(m->nonce));
- wg_hash2(key, WG_COOKIE, strlen(WG_COOKIE), c->c_local_pubkey,
+ wg_hash2(key, WG_COOKIE, strlen(WG_COOKIE), c->c_ks.k_local_pubkey,
WG_KEY_SIZE);
xchacha20poly1305_encrypt(m->cookie, cookie, WG_COOKIE_SIZE,
c->c_hs.hs_mac, WG_MAC_SIZE, m->nonce, key);
@@ -549,16 +549,16 @@ wg_conn_recv_response(struct wg_conn *c, struct wg_msg_response *m)
wg_mix_hash(hs, hs->hs_remote_epub, WG_KEY_SIZE);
wg_mix_dh(hs, hs->hs_local_epriv, hs->hs_remote_epub);
- wg_mix_dh(hs, c->c_local_privkey, hs->hs_remote_epub);
+ wg_mix_dh(hs, c->c_ks.k_local_privkey, hs->hs_remote_epub);
- wg_mix_psk(hs, c->c_psk);
+ wg_mix_psk(hs, c->c_ks.k_psk);
if (!wg_hs_decrypt(hs, NULL, m->empty, WG_MAC_SIZE))
goto fail;
wg_mix_hash(hs, m->empty, WG_ENCRYPTED_SIZE(0));
- wg_hash2(hs->hs_k, WG_MAC1, strlen(WG_MAC1), c->c_local_pubkey, WG_KEY_SIZE);
+ wg_hash2(hs->hs_k, WG_MAC1, strlen(WG_MAC1), c->c_ks.k_local_pubkey, WG_KEY_SIZE);
blake2s(mac1, (void *)m, hs->hs_k, sizeof(mac1), offsetof(struct wg_msg_response, mac1), sizeof(hs->hs_k));
if (timingsafe_bcmp(mac1, m->mac1, WG_MAC_SIZE))
@@ -581,7 +581,7 @@ wg_conn_recv_cookie(struct wg_conn *c, struct wg_msg_cookie *m)
rw_enter_write(&c->c_lock);
- wg_hash2(key, WG_COOKIE, strlen(WG_COOKIE), c->c_pubkey, WG_KEY_SIZE);
+ wg_hash2(key, WG_COOKIE, strlen(WG_COOKIE), c->c_ks.k_pubkey, WG_KEY_SIZE);
if(!xchacha20poly1305_decrypt(value, m->cookie, sizeof(m->cookie),
c->c_hs.hs_mac, WG_MAC_SIZE, m->nonce, key)) {
diff --git a/src/wireguard.h b/src/wireguard.h
index 74f2cd7ce12..1187eba959f 100644
--- a/src/wireguard.h
+++ b/src/wireguard.h
@@ -101,12 +101,16 @@ struct wg_upcall {
uint8_t *u_pubkey, *u_privkey;
};
+struct wg_keyset {
+ uint8_t k_psk[WG_KEY_SIZE];
+ uint8_t k_pubkey[WG_KEY_SIZE];
+ uint8_t k_local_pubkey[WG_KEY_SIZE], k_local_privkey[WG_KEY_SIZE];
+};
+
struct wg_conn {
struct rwlock c_lock;
- uint8_t c_psk[WG_KEY_SIZE];
- uint8_t c_pubkey[WG_KEY_SIZE];
- uint8_t c_local_pubkey[WG_KEY_SIZE], c_local_privkey[WG_KEY_SIZE];
+ struct wg_keyset c_ks;
struct wg_handshake c_hs;
struct wg_session c_sess, c_sess_old;
struct wg_cookie c_cookie;