diff options
author | Uros Majstorovic <majstor@majstor.org> | 2018-03-15 21:37:11 +0100 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2018-03-15 21:37:11 +0100 |
commit | 8755ad4c0b0652f16d3505ed72ed3d8310a35c6f (patch) | |
tree | 6d7c3b93563bf64b811bb198e13ebbe7d7907e67 /code | |
parent | b00f381106c941ea03e5ea04dc08e6c4b9e582fa (diff) |
removed *IFace from context
Diffstat (limited to 'code')
-rw-r--r-- | code/ecp/Makefile.fe310 | 2 | ||||
-rw-r--r-- | code/ecp/Makefile.posix | 2 | ||||
-rw-r--r-- | code/ecp/core.c | 128 | ||||
-rw-r--r-- | code/ecp/core.h | 71 | ||||
-rw-r--r-- | code/ecp/cr.h | 13 | ||||
-rw-r--r-- | code/ecp/crypto/crypto.c | 45 | ||||
-rw-r--r-- | code/ecp/fe310/time.c | 14 | ||||
-rw-r--r-- | code/ecp/fe310/transport.c | 35 | ||||
-rw-r--r-- | code/ecp/ht.h | 5 | ||||
-rw-r--r-- | code/ecp/htable/htable.c | 25 | ||||
-rw-r--r-- | code/ecp/posix/time.c | 12 | ||||
-rw-r--r-- | code/ecp/posix/transport.c | 31 | ||||
-rw-r--r-- | code/ecp/rbuf.c | 2 | ||||
-rw-r--r-- | code/ecp/rbuf_recv.c | 7 | ||||
-rw-r--r-- | code/ecp/rbuf_send.c | 6 | ||||
-rw-r--r-- | code/ecp/timer.c | 8 | ||||
-rw-r--r-- | code/ecp/tm.h | 3 | ||||
-rw-r--r-- | code/ecp/tr.h | 9 | ||||
-rw-r--r-- | code/ecp/vconn/vconn.c | 77 | ||||
-rw-r--r-- | code/util/util.c | 9 |
20 files changed, 200 insertions, 304 deletions
diff --git a/code/ecp/Makefile.fe310 b/code/ecp/Makefile.fe310 index 96b16a2..9070dd6 100644 --- a/code/ecp/Makefile.fe310 +++ b/code/ecp/Makefile.fe310 @@ -9,5 +9,5 @@ FE310_HOME=/opt/my/freedom-e-sdk CC=$(FE310_HOME)/work/build/riscv-gnu-toolchain/riscv64-unknown-elf/prefix/bin/riscv64-unknown-elf-gcc AR=$(FE310_HOME)/work/build/riscv-gnu-toolchain/riscv64-unknown-elf/prefix/bin/riscv64-unknown-elf-ar -CFLAGS_PL=-O3 -fno-builtin-printf -march=rv32imac -mabi=ilp32 -mcmodel=medany -I$(FE310_HOME)/bsp/include -I$(FE310_HOME)/bsp/drivers -I$(FE310_HOME)/bsp/env -I$(FE310_HOME)/bsp/env/freedom-e300-hifive1 -DECP_DEBUG=1 +CFLAGS_PL=-O3 -fno-builtin-printf -march=rv32imac -mabi=ilp32 -mcmodel=medany -I$(FE310_HOME)/bsp/include -I$(FE310_HOME)/bsp/drivers -I$(FE310_HOME)/bsp/env -I$(FE310_HOME)/bsp/env/freedom-e300-hifive1 -DECP_WITH_VCONN=1 -DECP_DEBUG=1 # LDFLAGS_PL=-lm -pthread
\ No newline at end of file diff --git a/code/ecp/Makefile.posix b/code/ecp/Makefile.posix index fe674a5..2822792 100644 --- a/code/ecp/Makefile.posix +++ b/code/ecp/Makefile.posix @@ -4,6 +4,6 @@ htable=htable vconn=vconn obj_rbuf=rbuf.o rbuf_send.o rbuf_recv.o msgq.o -CFLAGS_PL=-O3 -DECP_WITH_PTHREAD=1 -DECP_WITH_HTABLE=1 -DECP_WITH_RBUF=1 -DECP_WITH_MSGQ=1 -DECP_DEBUG=1 +CFLAGS_PL=-O3 -DECP_WITH_PTHREAD=1 -DECP_WITH_HTABLE=1 -DECP_WITH_RBUF=1 -DECP_WITH_MSGQ=1 -DECP_WITH_VCONN=1 -DECP_DEBUG=1 LDFLAGS_PL=-lm -pthread
\ No newline at end of file diff --git a/code/ecp/core.c b/code/ecp/core.c index bdcdd41..549c403 100644 --- a/code/ecp/core.c +++ b/code/ecp/core.c @@ -1,11 +1,19 @@ #include "core.h" +#include "cr.h" +#include "tr.h" +#include "tm.h" + +#ifdef ECP_WITH_HTABLE +#include "ht.h" +#endif + int ecp_dhkey_generate(ECPContext *ctx, ECPDHKey *key) { int rv; if (ctx->rng == NULL) return ECP_ERR_RNG; - rv = ctx->cr.dh_mkpair(&key->public, &key->private, ctx->rng); + rv = ecp_cr_dh_mkpair(&key->public, &key->private, ctx->rng); if (rv) return rv; key->valid = 1; @@ -19,7 +27,7 @@ int ecp_node_init(ECPContext *ctx, ECPNode *node, ecp_dh_public_t *public, void if (public) memcpy(&node->public, public, sizeof(node->public)); - if (addr && ctx->tr.addr_set) rv = ctx->tr.addr_set(&node->addr, addr); + if (addr) rv = ecp_tr_addr_set(&node->addr, addr); if (rv) return ECP_ERR_NET_ADDR; return ECP_OK; @@ -32,20 +40,6 @@ int ecp_ctx_create(ECPContext *ctx) { memset(ctx, 0, sizeof(ECPContext)); - ctx->pack = _ecp_pack; - ctx->pack_raw = _ecp_pack_raw; - - rv = ecp_crypto_init(&ctx->cr); - if (rv) return rv; - rv = ecp_transport_init(&ctx->tr); - if (rv) return rv; - rv = ecp_time_init(&ctx->tm); - if (rv) return rv; -#ifdef ECP_WITH_HTABLE - rv = ecp_htable_init(&ctx->ht); - if (rv) return rv; -#endif - return ECP_OK; } @@ -59,7 +53,7 @@ static int ctable_create(ECPSockCTable *conn, ECPContext *ctx) { memset(conn, 0, sizeof(ECPSockCTable)); #ifdef ECP_WITH_HTABLE - conn->htable = ctx->ht.create(ctx); + conn->htable = ecp_ht_create(ctx); if (conn->htable == NULL) return ECP_ERR_ALLOC; #endif @@ -67,7 +61,7 @@ static int ctable_create(ECPSockCTable *conn, ECPContext *ctx) { rv = pthread_mutex_init(&conn->mutex, NULL); if (rv) { #ifdef ECP_WITH_HTABLE - ctx->ht.destroy(conn->htable); + ecp_ht_destroy(conn->htable); #endif return ECP_ERR; } @@ -81,14 +75,13 @@ static void ctable_destroy(ECPSockCTable *conn, ECPContext *ctx) { pthread_mutex_destroy(&conn->mutex); #endif #ifdef ECP_WITH_HTABLE - ctx->ht.destroy(conn->htable); + ecp_ht_destroy(conn->htable); #endif } static int ctable_insert(ECPConnection *conn) { int with_htable = 0; ECPSocket *sock = conn->sock; - ECPContext *ctx = sock->ctx; #ifdef ECP_WITH_HTABLE with_htable = 1; @@ -99,10 +92,10 @@ static int ctable_insert(ECPConnection *conn) { int i, rv = ECP_OK; if (conn->out) { for (i=0; i<ECP_MAX_CONN_KEY; i++) { - if (conn->key[i].valid) rv = ctx->ht.insert(sock->conn.htable, ctx->cr.dh_pub_get_buf(&conn->key[i].public), conn); + if (conn->key[i].valid) rv = ecp_ht_insert(sock->conn.htable, ecp_cr_dh_pub_get_buf(&conn->key[i].public), conn); if (rv) { int j; - for (j=0; j<i; j++) if (conn->key[j].valid) ctx->ht.remove(sock->conn.htable, ctx->cr.dh_pub_get_buf(&conn->key[j].public)); + for (j=0; j<i; j++) if (conn->key[j].valid) ecp_ht_remove(sock->conn.htable, ecp_cr_dh_pub_get_buf(&conn->key[j].public)); return rv; } } @@ -110,10 +103,10 @@ static int ctable_insert(ECPConnection *conn) { ECPDHRKeyBucket *remote = &conn->remote; for (i=0; i<ECP_MAX_NODE_KEY; i++) { - if (remote->key[i].idx != ECP_ECDH_IDX_INV) rv = ctx->ht.insert(sock->conn.htable, ctx->cr.dh_pub_get_buf(&remote->key[i].public), conn); + if (remote->key[i].idx != ECP_ECDH_IDX_INV) rv = ecp_ht_insert(sock->conn.htable, ecp_cr_dh_pub_get_buf(&remote->key[i].public), conn); if (rv) { int j; - for (j=0; j<i; j++) if (remote->key[j].idx != ECP_ECDH_IDX_INV) ctx->ht.remove(sock->conn.htable, ctx->cr.dh_pub_get_buf(&remote->key[j].public)); + for (j=0; j<i; j++) if (remote->key[j].idx != ECP_ECDH_IDX_INV) ecp_ht_remove(sock->conn.htable, ecp_cr_dh_pub_get_buf(&remote->key[j].public)); return rv; } } @@ -133,7 +126,6 @@ static int ctable_insert(ECPConnection *conn) { static void ctable_remove(ECPConnection *conn) { int i, with_htable = 0; ECPSocket *sock = conn->sock; - ECPContext *ctx = sock->ctx; #ifdef ECP_WITH_HTABLE with_htable = 1; @@ -142,10 +134,10 @@ static void ctable_remove(ECPConnection *conn) { if (with_htable) { #ifdef ECP_WITH_HTABLE if (conn->out) { - for (i=0; i<ECP_MAX_CONN_KEY; i++) if (conn->key[i].valid) ctx->ht.remove(sock->conn.htable, ctx->cr.dh_pub_get_buf(&conn->key[i].public)); + for (i=0; i<ECP_MAX_CONN_KEY; i++) if (conn->key[i].valid) ecp_ht_remove(sock->conn.htable, ecp_cr_dh_pub_get_buf(&conn->key[i].public)); } else { ECPDHRKeyBucket *remote = &conn->remote; - for (i=0; i<ECP_MAX_NODE_KEY; i++) if (remote->key[i].idx != ECP_ECDH_IDX_INV) ctx->ht.remove(sock->conn.htable, ctx->cr.dh_pub_get_buf(&remote->key[i].public)); + for (i=0; i<ECP_MAX_NODE_KEY; i++) if (remote->key[i].idx != ECP_ECDH_IDX_INV) ecp_ht_remove(sock->conn.htable, ecp_cr_dh_pub_get_buf(&remote->key[i].public)); } #endif } else { @@ -172,7 +164,7 @@ static ECPConnection *ctable_search(ECPSocket *sock, unsigned char c_idx, unsign if (with_htable) { #ifdef ECP_WITH_HTABLE - return sock->ctx->ht.search(sock->conn.htable, c_public); + return ecp_ht_search(sock->conn.htable, c_public); #endif } else { #ifndef ECP_WITH_HTABLE @@ -180,10 +172,10 @@ static ECPConnection *ctable_search(ECPSocket *sock, unsigned char c_idx, unsign for (i=0; i<sock->conn.size; i++) { conn = sock->conn.array[i]; if (conn->out) { - if ((c_idx < ECP_MAX_CONN_KEY) && conn->key[c_idx].valid && sock->ctx->cr.dh_pub_eq(c_public, &conn->key[c_idx].public)) + if ((c_idx < ECP_MAX_CONN_KEY) && conn->key[c_idx].valid && ecp_cr_dh_pub_eq(c_public, &conn->key[c_idx].public)) return conn; } else { - if ((c_idx < ECP_MAX_SOCK_KEY) && (conn->remote.key_idx_map[c_idx] != ECP_ECDH_IDX_INV) && sock->ctx->cr.dh_pub_eq(c_public, &conn->remote.key[conn->remote.key_idx_map[c_idx]].public)) + if ((c_idx < ECP_MAX_SOCK_KEY) && (conn->remote.key_idx_map[c_idx] != ECP_ECDH_IDX_INV) && ecp_cr_dh_pub_eq(c_public, &conn->remote.key[conn->remote.key_idx_map[c_idx]].public)) return conn; } } @@ -191,7 +183,7 @@ static ECPConnection *ctable_search(ECPSocket *sock, unsigned char c_idx, unsign /* in case server is not returning client's public key in packet */ for (i=0; i<sock->conn.size; i++) { conn = sock->conn.array[i]; - if (conn->out && conn->sock->ctx->tr.addr_eq(&conn->node.addr, addr)) return conn; + if (conn->out && ecp_tr_addr_eq(&conn->node.addr, addr)) return conn; } } #endif @@ -245,11 +237,11 @@ void ecp_sock_destroy(ECPSocket *sock) { } int ecp_sock_open(ECPSocket *sock, void *myaddr) { - return sock->ctx->tr.open(&sock->sock, myaddr); + return ecp_tr_open(&sock->sock, myaddr); } void ecp_sock_close(ECPSocket *sock) { - sock->ctx->tr.close(&sock->sock); + ecp_tr_close(&sock->sock); } int ecp_sock_dhkey_get_curr(ECPSocket *sock, unsigned char *idx, unsigned char *public) { @@ -260,7 +252,7 @@ int ecp_sock_dhkey_get_curr(ECPSocket *sock, unsigned char *idx, unsigned char * #endif _idx = sock->key_curr; - if (_idx != ECP_ECDH_IDX_INV) sock->ctx->cr.dh_pub_to_buf(public, &sock->key[_idx].public); + if (_idx != ECP_ECDH_IDX_INV) ecp_cr_dh_pub_to_buf(public, &sock->key[_idx].public); #ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&sock->mutex); @@ -302,12 +294,11 @@ static ECPDHKey *conn_dhkey_get(ECPConnection *conn, unsigned char idx) { static int conn_dhkey_new_pair(ECPConnection *conn, ECPDHKey *key) { /* called when client makes new key pair */ ECPSocket *sock = conn->sock; - ECPContext *ctx = sock->ctx; conn->key_curr = conn->key_curr == ECP_ECDH_IDX_INV ? 0 : (conn->key_curr+1) % ECP_MAX_CONN_KEY; #ifdef ECP_WITH_HTABLE if (conn->out && ecp_conn_is_reg(conn) && conn->key[conn->key_curr].valid) { - ctx->ht.remove(sock->conn.htable, ctx->cr.dh_pub_get_buf(&conn->key[conn->key_curr].public)); + ecp_ht_remove(sock->conn.htable, ecp_cr_dh_pub_get_buf(&conn->key[conn->key_curr].public)); } #endif @@ -316,7 +307,7 @@ static int conn_dhkey_new_pair(ECPConnection *conn, ECPDHKey *key) { #ifdef ECP_WITH_HTABLE if (conn->out && ecp_conn_is_reg(conn)) { - int rv = ctx->ht.insert(sock->conn.htable, ctx->cr.dh_pub_get_buf(&conn->key[conn->key_curr].public), conn); + int rv = ecp_ht_insert(sock->conn.htable, ecp_cr_dh_pub_get_buf(&conn->key[conn->key_curr].public), conn); if (rv) return rv; } #endif @@ -326,11 +317,10 @@ static int conn_dhkey_new_pair(ECPConnection *conn, ECPDHKey *key) { static void conn_dhkey_del_pair(ECPConnection *conn, unsigned char idx) { ECPSocket *sock = conn->sock; - ECPContext *ctx = sock->ctx; #ifdef ECP_WITH_HTABLE if (conn->out && ecp_conn_is_reg(conn) && conn->key[idx].valid) { - ctx->ht.remove(sock->conn.htable, ctx->cr.dh_pub_get_buf(&conn->key[idx].public)); + ecp_ht_remove(sock->conn.htable, ecp_cr_dh_pub_get_buf(&conn->key[idx].public)); } #endif @@ -360,29 +350,28 @@ static int conn_dhkey_new_pub_local(ECPConnection *conn, unsigned char idx) { static int conn_dhkey_new_pub_remote(ECPConnection *conn, unsigned char idx, unsigned char *public) { // We obtained remote key ECPSocket *sock = conn->sock; - ECPContext *ctx = sock->ctx; ECPDHRKeyBucket *remote = &conn->remote; unsigned char new = remote->key_curr == ECP_ECDH_IDX_INV ? 0 : (remote->key_curr+1) % ECP_MAX_NODE_KEY; int i; if (idx >= ECP_MAX_SOCK_KEY) return ECP_ERR_ECDH_IDX; - if ((remote->key_idx_map[idx] != ECP_ECDH_IDX_INV) && ctx->cr.dh_pub_eq(public, &remote->key[remote->key_idx_map[idx]].public)) return ECP_ERR_ECDH_KEY_DUP; + if ((remote->key_idx_map[idx] != ECP_ECDH_IDX_INV) && ecp_cr_dh_pub_eq(public, &remote->key[remote->key_idx_map[idx]].public)) return ECP_ERR_ECDH_KEY_DUP; #ifdef ECP_WITH_HTABLE if (!conn->out && ecp_conn_is_reg(conn) && (remote->key[new].idx != ECP_ECDH_IDX_INV)) { - ctx->ht.remove(sock->conn.htable, ctx->cr.dh_pub_get_buf(&remote->key[new].public)); + ecp_ht_remove(sock->conn.htable, ecp_cr_dh_pub_get_buf(&remote->key[new].public)); } #endif if (remote->key[new].idx != ECP_ECDH_IDX_INV) remote->key_idx_map[remote->key[new].idx] = ECP_ECDH_IDX_INV; remote->key_idx_map[idx] = new; - ctx->cr.dh_pub_from_buf(&remote->key[new].public, public); + ecp_cr_dh_pub_from_buf(&remote->key[new].public, public); remote->key[new].idx = idx; remote->key_curr = new; #ifdef ECP_WITH_HTABLE if (!conn->out && ecp_conn_is_reg(conn)) { - int rv = ctx->ht.insert(sock->conn.htable, ctx->cr.dh_pub_get_buf(&remote->key[new].public), conn); + int rv = ecp_ht_insert(sock->conn.htable, ecp_cr_dh_pub_get_buf(&remote->key[new].public), conn); if (rv) return rv; } #endif @@ -413,7 +402,7 @@ static int conn_shsec_get(ECPConnection *conn, unsigned char s_idx, unsigned cha } if (public_p == NULL) return ECP_ERR_ECDH_IDX; if ((priv == NULL) || !priv->valid) return ECP_ERR_ECDH_IDX; - conn->sock->ctx->cr.dh_shsec(shsec, public_p, &priv->private); + ecp_cr_dh_shsec(shsec, public_p, &priv->private); } else { unsigned char l_idx = conn->out ? c_idx : s_idx; unsigned char r_idx = conn->out ? s_idx : c_idx; @@ -433,7 +422,7 @@ static int conn_shsec_get(ECPConnection *conn, unsigned char s_idx, unsigned cha if ((pub == NULL) || (pub->idx == ECP_ECDH_IDX_INV)) return ECP_ERR_ECDH_IDX; if ((priv == NULL) || !priv->valid) return ECP_ERR_ECDH_IDX; - conn->sock->ctx->cr.dh_shsec(&shared->secret, &pub->public, &priv->private); + ecp_cr_dh_shsec(&shared->secret, &pub->public, &priv->private); shared->valid = 1; } @@ -588,8 +577,8 @@ int ecp_conn_close(ECPConnection *conn, ecp_cts_t timeout) { refcount = conn->refcount; pthread_mutex_unlock(&conn->mutex); - if (timeout && refcount && sock->ctx->tm.sleep_ms) { - sock->ctx->tm.sleep_ms(timeout); + if (timeout && refcount) { + ecp_tm_sleep_ms(timeout); pthread_mutex_lock(&conn->mutex); refcount = conn->refcount; pthread_mutex_unlock(&conn->mutex); @@ -995,7 +984,7 @@ int ecp_conn_dhkey_get_curr(ECPConnection *conn, unsigned char *idx, unsigned ch #endif _idx = conn->key_curr; - if (_idx != ECP_ECDH_IDX_INV) sock->ctx->cr.dh_pub_to_buf(public, &conn->key[_idx].public); + if (_idx != ECP_ECDH_IDX_INV) ecp_cr_dh_pub_to_buf(public, &conn->key[_idx].public); #ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&conn->mutex); @@ -1007,7 +996,7 @@ int ecp_conn_dhkey_get_curr(ECPConnection *conn, unsigned char *idx, unsigned ch return ECP_OK; } -ssize_t ecp_pack(ECPContext *ctx, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, unsigned char *payload, size_t pld_size) { +ssize_t ecp_pkt_pack(ECPContext *ctx, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, unsigned char *payload, size_t pld_size) { ssize_t rv; if (pkt_size < ECP_SIZE_PKT_HDR) return ECP_ERR; @@ -1018,14 +1007,14 @@ ssize_t ecp_pack(ECPContext *ctx, unsigned char *packet, size_t pkt_size, unsign s_idx = s_idx & 0x0F; c_idx = c_idx & 0x0F; packet[ECP_SIZE_PROTO] = (s_idx << 4) | c_idx; - ctx->cr.dh_pub_to_buf(packet+ECP_SIZE_PROTO+1, public); + ecp_cr_dh_pub_to_buf(packet+ECP_SIZE_PROTO+1, public); memcpy(packet+ECP_SIZE_PROTO+1+ECP_ECDH_SIZE_KEY, nonce, ECP_AEAD_SIZE_NONCE); payload[0] = (seq & 0xFF000000) >> 24; payload[1] = (seq & 0x00FF0000) >> 16; payload[2] = (seq & 0x0000FF00) >> 8; payload[3] = (seq & 0x000000FF); - rv = ctx->cr.aead_enc(packet+ECP_SIZE_PKT_HDR, pkt_size-ECP_SIZE_PKT_HDR, payload, pld_size, shsec, nonce); + rv = ecp_cr_aead_enc(packet+ECP_SIZE_PKT_HDR, pkt_size-ECP_SIZE_PKT_HDR, payload, pld_size, shsec, nonce); if (rv < 0) return ECP_ERR_ENCRYPT; memcpy(nonce, packet+ECP_SIZE_PKT_HDR, ECP_AEAD_SIZE_NONCE); @@ -1033,10 +1022,10 @@ ssize_t ecp_pack(ECPContext *ctx, unsigned char *packet, size_t pkt_size, unsign return rv+ECP_SIZE_PKT_HDR; } -ssize_t ecp_pack_raw(ECPSocket *sock, ECPConnection *parent, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, unsigned char *payload, size_t pld_size, ECPNetAddr *addr) { +ssize_t ecp_pkt_pack_raw(ECPSocket *sock, ECPConnection *parent, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, unsigned char *payload, size_t pld_size, ECPNetAddr *addr) { ECPContext *ctx = sock->ctx; - return ecp_pack(ctx, packet, pkt_size, s_idx, c_idx, public, shsec, nonce, seq, payload, pld_size); + return ecp_pkt_pack(ctx, packet, pkt_size, s_idx, c_idx, public, shsec, nonce, seq, payload, pld_size); } ssize_t ecp_conn_pack(ECPConnection *conn, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, unsigned char *payload, size_t pld_size, ECPSeqItem *si, ECPNetAddr *addr) { @@ -1102,7 +1091,7 @@ ssize_t ecp_conn_pack(ECPConnection *conn, unsigned char *packet, size_t pkt_siz if (rv) return rv; - ssize_t _rv = ecp_pack(conn->sock->ctx, packet, pkt_size, s_idx, c_idx, &public, &shsec, nonce, _seq, payload, pld_size); + ssize_t _rv = ecp_pkt_pack(conn->sock->ctx, packet, pkt_size, s_idx, c_idx, &public, &shsec, nonce, _seq, payload, pld_size); if (_rv < 0) return _rv; #ifdef ECP_WITH_PTHREAD @@ -1116,20 +1105,21 @@ ssize_t ecp_conn_pack(ECPConnection *conn, unsigned char *packet, size_t pkt_siz return _rv; } -ssize_t _ecp_pack(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, ECPSeqItem *si, ECPNetAddr *addr) { +#ifndef ECP_WITH_VCONN +ssize_t ecp_pack(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, ECPSeqItem *si, ECPNetAddr *addr) { if ((packet == NULL) || (packet->buffer == NULL)) return ECP_ERR; if ((payload == NULL) || (payload->buffer == NULL)) return ECP_ERR; return ecp_conn_pack(conn, packet->buffer, packet->size, s_idx, c_idx, payload->buffer, pld_size, si, addr); } -ssize_t _ecp_pack_raw(ECPSocket *sock, ECPConnection *parent, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, ECPBuffer *payload, size_t pld_size, ECPNetAddr *addr) { +ssize_t ecp_pack_raw(ECPSocket *sock, ECPConnection *parent, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, ECPBuffer *payload, size_t pld_size, ECPNetAddr *addr) { if ((packet == NULL) || (packet->buffer == NULL)) return ECP_ERR; if ((payload == NULL) || (payload->buffer == NULL)) return ECP_ERR; - return ecp_pack_raw(sock, parent, packet->buffer, packet->size, s_idx, c_idx, public, shsec, nonce, seq, payload->buffer, pld_size, addr); + return ecp_pkt_pack_raw(sock, parent, packet->buffer, packet->size, s_idx, c_idx, public, shsec, nonce, seq, payload->buffer, pld_size, addr); } - +#endif ssize_t ecp_unpack(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECP2Buffer *bufs, size_t pkt_size, ECPConnection **_conn, ecp_seq_t *_seq) { unsigned char s_idx; @@ -1140,7 +1130,6 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECP ecp_aead_key_t shsec; ecp_dh_public_t public; ecp_dh_private_t private; - ECPContext *ctx = sock->ctx; ECPConnection *conn = NULL; ECPDHKey *key = NULL; int rv = ECP_OK; @@ -1206,12 +1195,12 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECP if (rv) return rv; if (key) { - sock->ctx->cr.dh_pub_from_buf(&public, packet+ECP_SIZE_PROTO+1); - sock->ctx->cr.dh_shsec(&shsec, &public, &private); + ecp_cr_dh_pub_from_buf(&public, packet+ECP_SIZE_PROTO+1); + ecp_cr_dh_shsec(&shsec, &public, &private); memset(&private, 0, sizeof(private)); } - dec_size = sock->ctx->cr.aead_dec(payload, bufs->payload->size, packet+ECP_SIZE_PKT_HDR, pkt_size-ECP_SIZE_PKT_HDR, &shsec, packet+ECP_SIZE_PROTO+1+ECP_ECDH_SIZE_KEY); + dec_size = ecp_cr_aead_dec(payload, bufs->payload->size, packet+ECP_SIZE_PKT_HDR, pkt_size-ECP_SIZE_PKT_HDR, &shsec, packet+ECP_SIZE_PROTO+1+ECP_ECDH_SIZE_KEY); if (dec_size < ECP_SIZE_PLD_HDR+1) rv = ECP_ERR_DECRYPT; if (rv) goto ecp_unpack_err; @@ -1221,7 +1210,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECP (payload[2] << 8) | \ (payload[3]); - if (ctx->tr.buf_free && ((payload[ECP_SIZE_PLD_HDR] & ECP_MTYPE_MASK) < ECP_MAX_MTYPE_SYS)) ctx->tr.buf_free(bufs, ECP_SEND_FLAG_MORE); + if ((payload[ECP_SIZE_PLD_HDR] & ECP_MTYPE_MASK) < ECP_MAX_MTYPE_SYS) ecp_tr_buf_free(bufs, ECP_SEND_FLAG_MORE); if (conn == NULL) { if (payload[ECP_SIZE_PLD_HDR] == ECP_MTYPE_OPEN_REQ) { is_new = 1; @@ -1372,7 +1361,7 @@ ssize_t ecp_pkt_handle(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, } ssize_t ecp_pkt_send(ECPSocket *sock, ECPNetAddr *addr, ECPBuffer *packet, size_t pkt_size, unsigned char flags) { - ssize_t rv = sock->ctx->tr.send(&sock->sock, packet, pkt_size, addr, flags); + ssize_t rv = ecp_tr_send(&sock->sock, packet, pkt_size, addr, flags); if (rv < 0) return rv; if (rv < ECP_MIN_PKT) return ECP_ERR_SEND; @@ -1561,10 +1550,9 @@ unsigned char *ecp_pld_get_buf(unsigned char *payload, unsigned char mtype) { static ssize_t pld_send(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, unsigned char flags, ECPTimerItem *ti, ECPSeqItem *si) { ECPSocket *sock = conn->sock; - ECPContext *ctx = sock->ctx; ECPNetAddr addr; - ssize_t rv = ctx->pack(conn, packet, s_idx, c_idx, payload, pld_size, si, &addr); + ssize_t rv = ecp_pack(conn, packet, s_idx, c_idx, payload, pld_size, si, &addr); if (rv < 0) return rv; #ifdef ECP_WITH_RBUF @@ -1603,11 +1591,10 @@ ssize_t ecp_pld_send_ll(ECPConnection *conn, ECPBuffer *packet, unsigned char s_ } ssize_t ecp_pld_send_raw(ECPSocket *sock, ECPConnection *parent, ECPNetAddr *addr, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, ECPBuffer *payload, size_t pld_size, unsigned char flags) { - ECPContext *ctx = sock->ctx; ECPNetAddr _addr; ssize_t rv; - rv = ctx->pack_raw(sock, parent, packet, s_idx, c_idx, public, shsec, nonce, seq, payload, pld_size, &_addr); + rv = ecp_pack_raw(sock, parent, packet, s_idx, c_idx, public, shsec, nonce, seq, payload, pld_size, &_addr); if (rv < 0) return rv; return ecp_pkt_send(sock, parent ? &_addr : addr, packet, rv, flags); @@ -1776,14 +1763,13 @@ int ecp_receiver(ECPSocket *sock) { unsigned char pkt_buf[ECP_MAX_PKT]; ecp_cts_t next = 0; ssize_t rv = 0; - if (sock->ctx->tr.recv == NULL) return ECP_ERR; sock->running = 1; while(sock->running) { packet.buffer = pkt_buf; packet.size = ECP_MAX_PKT; - rv = sock->ctx->tr.recv(&sock->sock, &packet, &addr, next ? next : sock->poll_timeout); + rv = ecp_tr_recv(&sock->sock, &packet, &addr, next ? next : sock->poll_timeout); if (rv > 0) { int _rv = recv_p(sock, &addr, &packet, rv); #ifdef ECP_DEBUG diff --git a/code/ecp/core.h b/code/ecp/core.h index b1a4e7f..3c6a488 100644 --- a/code/ecp/core.h +++ b/code/ecp/core.h @@ -161,55 +161,6 @@ typedef void ecp_conn_destroy_t (struct ECPConnection *c); typedef ssize_t ecp_conn_open_t (struct ECPConnection *c); typedef void ecp_conn_close_t (struct ECPConnection *c); -typedef struct ECPCryptoIface { - int init; - int (*dh_mkpair) (ecp_dh_public_t *p, ecp_dh_private_t *s, ecp_rng_t *rb); - int (*dh_shsec) (ecp_aead_key_t *sh, ecp_dh_public_t *p, ecp_dh_private_t *s); - unsigned char *(*dh_pub_get_buf) (ecp_dh_public_t *p); - void (*dh_pub_to_buf) (unsigned char *b, ecp_dh_public_t *p); - void (*dh_pub_from_buf) (ecp_dh_public_t *p, unsigned char *b); - int (*dh_pub_eq) (unsigned char *p1, ecp_dh_public_t *p2); - unsigned int (*dh_pub_hash_fn) (unsigned char *p); - int (*dh_pub_hash_eq) (unsigned char *p1, unsigned char *p2); - - ssize_t (*aead_enc) (unsigned char *ct, size_t cl, unsigned char *pt, size_t pl, ecp_aead_key_t *k, unsigned char *n); - ssize_t (*aead_dec) (unsigned char *pt, size_t pl, unsigned char *ct, size_t cl, ecp_aead_key_t *k, unsigned char *n); - int (*dsa_mkpair) (ecp_dsa_public_t *p, ecp_dsa_private_t *s, ecp_rng_t *rb); - int (*dsa_sign) (unsigned char *sig, unsigned char *m, size_t ml, ecp_dsa_public_t *p, ecp_dsa_private_t *s); - int (*dsa_verify) (unsigned char *m, size_t ml, unsigned char *sig, ecp_dsa_public_t *p); -} ECPCryptoIface; - -typedef struct ECPTransportIface { - int init; - int (*open) (ECPNetSock *, void *addr); - void (*close) (ECPNetSock *); - ssize_t (*send) (ECPNetSock *, struct ECPBuffer *packet, size_t, ECPNetAddr *, unsigned char); - ssize_t (*recv) (ECPNetSock *, struct ECPBuffer *packet, ECPNetAddr *, int); - int (*addr_eq) (ECPNetAddr *, ECPNetAddr *); - int (*addr_set) (ECPNetAddr *, void *addr); - void (*buf_free) (struct ECP2Buffer *, unsigned char); - void (*buf_flag_set) (struct ECP2Buffer *, unsigned char); - void (*buf_flag_clear) (struct ECP2Buffer *, unsigned char); -} ECPTransportIface; - -typedef struct ECPTimeIface { - int init; - ecp_cts_t (*abstime_ms) (ecp_cts_t); - void (*sleep_ms) (ecp_cts_t); - void (*timer_set) (ecp_cts_t); -} ECPTimeIface; - -#ifdef ECP_WITH_HTABLE -typedef struct ECPHTableIface { - int init; - void *(*create) (struct ECPContext *c); - void (*destroy) (void *t); - int (*insert) (void *t, unsigned char *k, struct ECPConnection *v); - struct ECPConnection *(*remove) (void *t, unsigned char *k); - struct ECPConnection *(*search) (void *t, unsigned char *k); -} ECPHTableIface; -#endif - typedef struct ECPBuffer { unsigned char *buffer; size_t size; @@ -289,14 +240,6 @@ typedef struct ECPContext { ecp_rng_t *rng; ecp_conn_alloc_t *conn_alloc; ecp_conn_free_t *conn_free; - ECPCryptoIface cr; - ECPTransportIface tr; - ECPTimeIface tm; -#ifdef ECP_WITH_HTABLE - ECPHTableIface ht; -#endif - ssize_t (*pack) (struct ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, ECPSeqItem *si, ECPNetAddr *addr); - ssize_t (*pack_raw) (struct ECPSocket *sock, struct ECPConnection *parent, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, ECPBuffer *payload, size_t pld_size, ECPNetAddr *addr); ECPConnHandler *handler[ECP_MAX_CTYPE]; } ECPContext; @@ -347,12 +290,6 @@ typedef struct ECPConnection { } ECPConnection; int ecp_init(ECPContext *ctx); -int ecp_crypto_init(ECPCryptoIface *t); -int ecp_transport_init(ECPTransportIface *t); -int ecp_time_init(ECPTimeIface *t); -#ifdef ECP_WITH_HTABLE -int ecp_htable_init(ECPHTableIface *h); -#endif int ecp_dhkey_generate(ECPContext *ctx, ECPDHKey *key); int ecp_node_init(ECPContext *ctx, ECPNode *node, ecp_dh_public_t *public, void *addr); @@ -388,12 +325,12 @@ int ecp_conn_dhkey_new(ECPConnection *conn); int ecp_conn_dhkey_new_pub(ECPConnection *conn, unsigned char idx, unsigned char *public); int ecp_conn_dhkey_get_curr(ECPConnection *conn, unsigned char *idx, unsigned char *public); -ssize_t ecp_pack(ECPContext *ctx, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, unsigned char *payload, size_t pld_size); -ssize_t ecp_pack_raw(ECPSocket *sock, ECPConnection *parent, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, unsigned char *payload, size_t pld_size, ECPNetAddr *addr); +ssize_t ecp_pkt_pack(ECPContext *ctx, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, unsigned char *payload, size_t pld_size); +ssize_t ecp_pkt_pack_raw(ECPSocket *sock, ECPConnection *parent, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, unsigned char *payload, size_t pld_size, ECPNetAddr *addr); ssize_t ecp_conn_pack(ECPConnection *conn, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, unsigned char *payload, size_t pld_size, ECPSeqItem *si, ECPNetAddr *addr); -ssize_t _ecp_pack(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, ECPSeqItem *si, ECPNetAddr *addr); -ssize_t _ecp_pack_raw(ECPSocket *sock, ECPConnection *parent, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, ECPBuffer *payload, size_t pld_size, ECPNetAddr *addr); +ssize_t ecp_pack(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, ECPSeqItem *si, ECPNetAddr *addr); +ssize_t ecp_pack_raw(ECPSocket *sock, ECPConnection *parent, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, ECPBuffer *payload, size_t pld_size, ECPNetAddr *addr); ssize_t ecp_unpack(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECP2Buffer *bufs, size_t pkt_size, ECPConnection **_conn, ecp_seq_t *_seq); ssize_t ecp_pkt_handle(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECP2Buffer *bufs, size_t pkt_size); diff --git a/code/ecp/cr.h b/code/ecp/cr.h new file mode 100644 index 0000000..05a95d7 --- /dev/null +++ b/code/ecp/cr.h @@ -0,0 +1,13 @@ +int ecp_cr_dh_mkpair(ecp_dh_public_t *pub, ecp_dh_private_t *priv, ecp_rng_t *rand_buf); +int ecp_cr_dh_shsec(ecp_aead_key_t *shared, ecp_dh_public_t *pub, ecp_dh_private_t *priv); +unsigned char *ecp_cr_dh_pub_get_buf(ecp_dh_public_t *p); +void ecp_cr_dh_pub_to_buf(unsigned char *b, ecp_dh_public_t *p); +void ecp_cr_dh_pub_from_buf(ecp_dh_public_t *p, unsigned char *b) ; +int ecp_cr_dh_pub_eq(unsigned char *p1, ecp_dh_public_t *p2) ; +unsigned int ecp_cr_dh_pub_hash_fn(unsigned char *p); +int ecp_cr_dh_pub_hash_eq(unsigned char *p1, unsigned char *p2); +ssize_t ecp_cr_aead_enc(unsigned char *ct, size_t cl, unsigned char *pt, size_t pl, ecp_aead_key_t *k, unsigned char *n); +ssize_t ecp_cr_aead_dec(unsigned char *pt, size_t pl, unsigned char *ct, size_t cl, ecp_aead_key_t *k, unsigned char *n) ; +int ecp_cr_dsa_mkpair(ecp_dsa_public_t *pub, ecp_dsa_private_t *priv, ecp_rng_t *rand_buf); +int ecp_cr_dsa_sign(unsigned char *sig, unsigned char *m, size_t ml, ecp_dsa_public_t *p, ecp_dsa_private_t *s) ; +int ecp_cr_dsa_verify(unsigned char *m, size_t ml, unsigned char *sig, ecp_dsa_public_t *p); diff --git a/code/ecp/crypto/crypto.c b/code/ecp/crypto/crypto.c index 81bf3cc..fd3fc9b 100644 --- a/code/ecp/crypto/crypto.c +++ b/code/ecp/crypto/crypto.c @@ -1,58 +1,59 @@ #include <core.h> +#include <cr.h> #include <curve25519.h> -static int dh_mkpair(ecp_dh_public_t *pub, ecp_dh_private_t *priv, ecp_rng_t *rand_buf) { +int ecp_cr_dh_mkpair(ecp_dh_public_t *pub, ecp_dh_private_t *priv, ecp_rng_t *rand_buf) { int rv = X25519_keypair(*pub, *priv, rand_buf); if (!rv) return ECP_ERR; return ECP_OK; } -static int dh_shsec(ecp_aead_key_t *shared, ecp_dh_public_t *pub, ecp_dh_private_t *priv) { +int ecp_cr_dh_shsec(ecp_aead_key_t *shared, ecp_dh_public_t *pub, ecp_dh_private_t *priv) { int rv = X25519(*shared, *priv, *pub); if (!rv) return ECP_ERR; return ECP_OK; } -static unsigned char *dh_pub_get_buf(ecp_dh_public_t *p) { +unsigned char *ecp_cr_dh_pub_get_buf(ecp_dh_public_t *p) { return (unsigned char *)p; } -static void dh_pub_to_buf(unsigned char *b, ecp_dh_public_t *p) { +void ecp_cr_dh_pub_to_buf(unsigned char *b, ecp_dh_public_t *p) { memcpy(b, p, ECP_ECDH_SIZE_KEY); } -static void dh_pub_from_buf(ecp_dh_public_t *p, unsigned char *b) { +void ecp_cr_dh_pub_from_buf(ecp_dh_public_t *p, unsigned char *b) { memcpy(p, b, ECP_ECDH_SIZE_KEY); } -static int dh_pub_eq(unsigned char *p1, ecp_dh_public_t *p2) { +int ecp_cr_dh_pub_eq(unsigned char *p1, ecp_dh_public_t *p2) { return !memcmp(p1, p2, ECP_ECDH_SIZE_KEY); } -static unsigned int dh_pub_hash_fn(unsigned char *p) { +unsigned int ecp_cr_dh_pub_hash_fn(unsigned char *p) { return *((unsigned int *)p); } -static int dh_pub_hash_eq(unsigned char *p1, unsigned char *p2) { +int ecp_cr_dh_pub_hash_eq(unsigned char *p1, unsigned char *p2) { return !memcmp(p1, p2, ECP_ECDH_SIZE_KEY); } -static ssize_t aead_enc(unsigned char *ct, size_t cl, unsigned char *pt, size_t pl, ecp_aead_key_t *k, unsigned char *n) { +ssize_t ecp_cr_aead_enc(unsigned char *ct, size_t cl, unsigned char *pt, size_t pl, ecp_aead_key_t *k, unsigned char *n) { size_t ol; int rv = aead_chacha20_poly1305_seal(ct, &ol, cl, *k, ECP_AEAD_SIZE_TAG, n, ECP_AEAD_SIZE_NONCE, pt, pl, NULL, 0); if (!rv) return ECP_ERR; return ol; } -static ssize_t aead_dec(unsigned char *pt, size_t pl, unsigned char *ct, size_t cl, ecp_aead_key_t *k, unsigned char *n) { +ssize_t ecp_cr_aead_dec(unsigned char *pt, size_t pl, unsigned char *ct, size_t cl, ecp_aead_key_t *k, unsigned char *n) { size_t ol; int rv = aead_chacha20_poly1305_open(pt, &ol, pl, *k, ECP_AEAD_SIZE_TAG, n, ECP_AEAD_SIZE_NONCE, ct, cl, NULL, 0); if (!rv) return ECP_ERR; return ol; } -int dsa_mkpair(ecp_dsa_public_t *pub, ecp_dsa_private_t *priv, ecp_rng_t *rand_buf) { +int ecp_cr_dsa_mkpair(ecp_dsa_public_t *pub, ecp_dsa_private_t *priv, ecp_rng_t *rand_buf) { unsigned char key[2*ECP_DSA_SIZE_KEY]; int rv = ED25519_keypair(*pub, key, rand_buf); @@ -61,7 +62,7 @@ int dsa_mkpair(ecp_dsa_public_t *pub, ecp_dsa_private_t *priv, ecp_rng_t *rand_b return ECP_OK; } -int dsa_sign(unsigned char *sig, unsigned char *m, size_t ml, ecp_dsa_public_t *p, ecp_dsa_private_t *s) { +int ecp_cr_dsa_sign(unsigned char *sig, unsigned char *m, size_t ml, ecp_dsa_public_t *p, ecp_dsa_private_t *s) { unsigned char key[2*ECP_DSA_SIZE_KEY]; memcpy(key, s, ECP_DSA_SIZE_KEY); memcpy(key+ECP_DSA_SIZE_KEY, p, ECP_DSA_SIZE_KEY); @@ -71,24 +72,6 @@ int dsa_sign(unsigned char *sig, unsigned char *m, size_t ml, ecp_dsa_public_t * return ECP_OK; } -int dsa_verify(unsigned char *m, size_t ml, unsigned char *sig, ecp_dsa_public_t *p) { +int ecp_cr_dsa_verify(unsigned char *m, size_t ml, unsigned char *sig, ecp_dsa_public_t *p) { return ED25519_verify(m, ml, sig, *p); } - -int ecp_crypto_init(ECPCryptoIface *cr) { - cr->init = 1; - cr->dh_mkpair = dh_mkpair; - cr->dh_shsec = dh_shsec; - cr->dh_pub_get_buf = dh_pub_get_buf; - cr->dh_pub_to_buf = dh_pub_to_buf; - cr->dh_pub_from_buf = dh_pub_from_buf; - cr->dh_pub_eq = dh_pub_eq; - cr->dh_pub_hash_fn = dh_pub_hash_fn; - cr->dh_pub_hash_eq = dh_pub_hash_eq; - cr->aead_enc = aead_enc; - cr->aead_dec = aead_dec; - cr->dsa_mkpair = dsa_mkpair; - cr->dsa_sign = dsa_sign; - cr->dsa_verify = dsa_verify; - return ECP_OK; -} diff --git a/code/ecp/fe310/time.c b/code/ecp/fe310/time.c index 4b8b2a7..fedd23a 100644 --- a/code/ecp/fe310/time.c +++ b/code/ecp/fe310/time.c @@ -1,25 +1,21 @@ #include <core.h> +#include <tr.h> #include <eos/timer.h> #include "encoding.h" #include "platform.h" -static ecp_cts_t t_abstime_ms(ecp_cts_t msec) { +ecp_cts_t ecp_tm_abstime_ms(ecp_cts_t msec) { volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME); uint64_t now_ms = *mtime * 1000 / RTC_FREQ; return now_ms + msec; } -static void t_timer_set(ecp_cts_t next) { +void ecp_tm_sleep_ms(ecp_cts_t msec) {} + +void ecp_tm_timer_set(ecp_cts_t next) { uint32_t tick = next * (uint64_t)RTC_FREQ / 1000; eos_timer_set(tick, 1); } - -int ecp_time_init(ECPTimeIface *t) { - t->init = 1; - t->abstime_ms = t_abstime_ms; - t->timer_set = t_timer_set; - return 0; -} diff --git a/code/ecp/fe310/transport.c b/code/ecp/fe310/transport.c index 5bffda9..3686e66 100644 --- a/code/ecp/fe310/transport.c +++ b/code/ecp/fe310/transport.c @@ -1,23 +1,28 @@ #include <core.h> +#include <tr.h> #include <eos/eos.h> #include <eos/net.h> -static int t_addr_eq(ECPNetAddr *addr1, ECPNetAddr *addr2) { +int ecp_tr_addr_eq(ECPNetAddr *addr1, ECPNetAddr *addr2) { if (addr1->port != addr2->port) return 0; if (memcmp(addr1->host, addr2->host, sizeof(addr1->host)) != 0) return 0; return 1; } -static int t_open(int *sock, void *addr_s) { +int ecp_tr_addr_set(ECPNetAddr *addr, void *addr_s) { + return ECP_ERR; +} + +int ecp_tr_open(int *sock, void *addr_s) { *sock = 0; return ECP_OK; } -static void t_close(int *sock) { +void ecp_tr_close(int *sock) { } -static ssize_t t_send(int *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr *addr, unsigned char flags) { +ssize_t ecp_tr_send(int *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr *addr, unsigned char flags) { unsigned char *buf = NULL; size_t addr_len = sizeof(addr->host) + sizeof(addr->port); uint16_t buf_size = msg_size + addr_len; @@ -41,32 +46,24 @@ static ssize_t t_send(int *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr return msg_size; } -static void t_buf_free(ECP2Buffer *b, unsigned char flags) { +ssize_t ecp_tr_recv(int *sock, ECPBuffer *packet, ECPNetAddr *addr, int timeout) { + return ECP_ERR; +} + +void ecp_tr_buf_free(ECP2Buffer *b, unsigned char flags) { size_t addr_len = ECP_IPv4_ADDR_SIZE + sizeof(uint16_t); if (b && b->packet && b->packet->buffer) eos_net_free(b->packet->buffer-addr_len, flags & ECP_SEND_FLAG_MORE); } -static void t_buf_flag_set(ECP2Buffer *b, unsigned char flags) { +void ecp_tr_buf_flag_set(ECP2Buffer *b, unsigned char flags) { size_t addr_len = ECP_IPv4_ADDR_SIZE + sizeof(uint16_t); if (flags & ECP_SEND_FLAG_MORE) { if (b && b->packet && b->packet->buffer) eos_net_reserve(b->packet->buffer-addr_len); } } -static void t_buf_flag_clear(ECP2Buffer *b, unsigned char flags) { +void ecp_tr_buf_flag_clear(ECP2Buffer *b, unsigned char flags) { if (flags & ECP_SEND_FLAG_MORE) { eos_net_release(1); } } - -int ecp_transport_init(ECPTransportIface *t) { - t->init = 1; - t->open = t_open; - t->close = t_close; - t->send = t_send; - t->addr_eq = t_addr_eq; - t->buf_free = t_buf_free; - t->buf_flag_set = t_buf_flag_set; - t->buf_flag_clear = t_buf_flag_clear; - return ECP_OK; -} diff --git a/code/ecp/ht.h b/code/ecp/ht.h new file mode 100644 index 0000000..13bf1d7 --- /dev/null +++ b/code/ecp/ht.h @@ -0,0 +1,5 @@ +void *ecp_ht_create(ECPContext *ctx); +void ecp_ht_destroy(void *h); +int ecp_ht_insert(void *h, unsigned char *k, ECPConnection *v); +ECPConnection *ecp_ht_remove(void *h, unsigned char *k); +ECPConnection *ecp_ht_search(void *h, unsigned char *k); diff --git a/code/ecp/htable/htable.c b/code/ecp/htable/htable.c index 77f5a35..a46291b 100644 --- a/code/ecp/htable/htable.c +++ b/code/ecp/htable/htable.c @@ -1,37 +1,28 @@ #include <core.h> +#include <ht.h> +#include <cr.h> #include "hashtable.h" -static void *h_create(ECPContext *ctx) { - return create_hashtable(1000, (unsigned int (*)(void *))ctx->cr.dh_pub_hash_fn, (int (*)(void *, void *))ctx->cr.dh_pub_hash_eq, NULL, NULL, NULL); +void *ecp_ht_create(ECPContext *ctx) { + return create_hashtable(1000, (unsigned int (*)(void *))ecp_cr_dh_pub_hash_fn, (int (*)(void *, void *))ecp_cr_dh_pub_hash_eq, NULL, NULL, NULL); } -static void h_destroy(void *h) { +void ecp_ht_destroy(void *h) { hashtable_destroy(h); } -static int h_insert(void *h, unsigned char *k, ECPConnection *v) { +int ecp_ht_insert(void *h, unsigned char *k, ECPConnection *v) { int rv = hashtable_insert(h, k, v); if (!rv) return ECP_ERR; return ECP_OK; } -static ECPConnection *h_remove(void *h, unsigned char *k) { +ECPConnection *ecp_ht_remove(void *h, unsigned char *k) { return hashtable_remove(h, k); } -static ECPConnection *h_search(void *h, unsigned char *k) { +ECPConnection *ecp_ht_search(void *h, unsigned char *k) { return hashtable_search(h, k); } -#ifdef ECP_WITH_HTABLE -int ecp_htable_init(ECPHTableIface *h) { - h->init = 1; - h->create = h_create; - h->destroy = h_destroy; - h->insert = h_insert; - h->remove = h_remove; - h->search = h_search; - return ECP_OK; -} -#endif
\ No newline at end of file diff --git a/code/ecp/posix/time.c b/code/ecp/posix/time.c index c7a238a..45d827e 100644 --- a/code/ecp/posix/time.c +++ b/code/ecp/posix/time.c @@ -2,8 +2,9 @@ #include <sys/time.h> #include <core.h> +#include <tm.h> -static ecp_cts_t t_abstime_ms(ecp_cts_t msec) { +ecp_cts_t ecp_tm_abstime_ms(ecp_cts_t msec) { struct timeval tv; ecp_cts_t ms_now; @@ -12,13 +13,8 @@ static ecp_cts_t t_abstime_ms(ecp_cts_t msec) { return ms_now + msec; } -static void t_sleep_ms(ecp_cts_t msec) { +void ecp_tm_sleep_ms(ecp_cts_t msec) { usleep(msec*1000); } -int ecp_time_init(ECPTimeIface *t) { - t->init = 1; - t->abstime_ms = t_abstime_ms; - t->sleep_ms = t_sleep_ms; - return 0; -} +void ecp_tm_timer_set(ecp_cts_t next) {}
\ No newline at end of file diff --git a/code/ecp/posix/transport.c b/code/ecp/posix/transport.c index 5415db6..d6426d5 100644 --- a/code/ecp/posix/transport.c +++ b/code/ecp/posix/transport.c @@ -5,16 +5,17 @@ #include <poll.h> #include <core.h> +#include <tr.h> #define ADDR_S_MAX 32 -static int t_addr_eq(ECPNetAddr *addr1, ECPNetAddr *addr2) { +int ecp_tr_addr_eq(ECPNetAddr *addr1, ECPNetAddr *addr2) { if (addr1->port != addr2->port) return 0; if (memcmp(addr1->host, addr2->host, sizeof(addr1->host)) != 0) return 0; return 1; } -static int t_addr_set(ECPNetAddr *addr, void *addr_s) { +int ecp_tr_addr_set(ECPNetAddr *addr, void *addr_s) { int rv; char addr_c[ADDR_S_MAX]; char *colon = NULL; @@ -37,14 +38,14 @@ static int t_addr_set(ECPNetAddr *addr, void *addr_s) { return 0; } -static int t_open(int *sock, void *addr_s) { +int ecp_tr_open(int *sock, void *addr_s) { struct sockaddr_in _myaddr; memset((char *)&_myaddr, 0, sizeof(_myaddr)); _myaddr.sin_family = AF_INET; if (addr_s) { ECPNetAddr addr; - int rv = t_addr_set(&addr, addr_s); + int rv = ecp_tr_addr_set(&addr, addr_s); if (rv) return rv; memcpy((void *)&_myaddr.sin_addr, addr.host, sizeof(addr.host)); @@ -62,14 +63,14 @@ static int t_open(int *sock, void *addr_s) { close(*sock); return rv; } - return 0; + return ECP_OK; } -static void t_close(int *sock) { +void ecp_tr_close(int *sock) { close(*sock); } -static ssize_t t_send(int *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr *addr, unsigned char flags) { +ssize_t ecp_tr_send(int *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr *addr, unsigned char flags) { struct sockaddr_in servaddr; memset((void *)&servaddr, 0, sizeof(servaddr)); @@ -79,7 +80,7 @@ static ssize_t t_send(int *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr return sendto(*sock, packet->buffer, msg_size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr)); } -static ssize_t t_recv(int *sock, ECPBuffer *packet, ECPNetAddr *addr, int timeout) { +ssize_t ecp_tr_recv(int *sock, ECPBuffer *packet, ECPNetAddr *addr, int timeout) { struct sockaddr_in servaddr; socklen_t addrlen = sizeof(servaddr); struct pollfd fds[] = { @@ -102,13 +103,7 @@ static ssize_t t_recv(int *sock, ECPBuffer *packet, ECPNetAddr *addr, int timeou return ECP_ERR_TIMEOUT; } -int ecp_transport_init(ECPTransportIface *t) { - t->init = 1; - t->open = t_open; - t->close = t_close; - t->send = t_send; - t->recv = t_recv; - t->addr_eq = t_addr_eq; - t->addr_set = t_addr_set; - return ECP_OK; -} +void ecp_tr_buf_free(ECP2Buffer *b, unsigned char flags) {} +void ecp_tr_buf_flag_set(ECP2Buffer *b, unsigned char flags) {} +void ecp_tr_buf_flag_clear(ECP2Buffer *b, unsigned char flags) {} + diff --git a/code/ecp/rbuf.c b/code/ecp/rbuf.c index e05aedc..080ba42 100644 --- a/code/ecp/rbuf.c +++ b/code/ecp/rbuf.c @@ -56,7 +56,7 @@ ssize_t ecp_rbuf_pld_send(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *pay seq_item.seq_w = 1; seq_item.rb_pass = 1; - rv = ctx->pack(conn, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload, pld_size, &seq_item, &addr); + rv = ecp_pack(conn, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload, pld_size, &seq_item, &addr); if (rv < 0) return rv; rv = ecp_pkt_send(sock, &addr, packet, rv, flags); diff --git a/code/ecp/rbuf_recv.c b/code/ecp/rbuf_recv.c index 7a74921..6ebfc64 100644 --- a/code/ecp/rbuf_recv.c +++ b/code/ecp/rbuf_recv.c @@ -1,4 +1,6 @@ #include "core.h" +#include "tr.h" +#include "tm.h" #define ACK_RATE 8 #define ACK_MASK_FIRST ((ecp_ack_t)1 << (ECP_SIZE_ACKB - 1)) @@ -76,7 +78,7 @@ static void msg_flush(ECPConnection *conn, ECP2Buffer *b) { rv = ecp_msg_get_pts(buf->rbuf.msg[idx].msg, buf->rbuf.msg[idx].size, &msg_pts); if (rv == ECP_OK) { - ecp_pts_t now = conn->sock->ctx->tm.abstime_ms(0); + ecp_pts_t now = ecp_tm_abstime_ms(0); if (ECP_PTS_LT(now, msg_pts)) { ECPTimerItem ti; ecp_seq_t seq_offset = seq - seq_next; @@ -196,11 +198,10 @@ static int ack_shift(ECPRBRecv *buf) { ssize_t ecp_rbuf_handle_flush(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) { if (size < 0) return size; - ECPContext *ctx = conn->sock->ctx; ECPRBRecv *buf = conn->rbuf.recv; if (buf == NULL) return ECP_ERR; - if (ctx->tr.buf_free) ctx->tr.buf_free(b, ECP_SEND_FLAG_MORE); + ecp_tr_buf_free(b, ECP_SEND_FLAG_MORE); ack_send(conn); return 0; } diff --git a/code/ecp/rbuf_send.c b/code/ecp/rbuf_send.c index e42a85b..975306a 100644 --- a/code/ecp/rbuf_send.c +++ b/code/ecp/rbuf_send.c @@ -1,4 +1,5 @@ #include "core.h" +#include "tr.h" #define NACK_RATE_UNIT 10000 @@ -84,7 +85,6 @@ static void cc_flush(ECPConnection *conn) { ssize_t ecp_rbuf_handle_ack(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) { ECPRBSend *buf; - ECPContext *ctx = conn->sock->ctx; ssize_t rsize = sizeof(ecp_seq_t)+sizeof(ecp_ack_t); ecp_seq_t seq_ack = 0; ecp_ack_t ack_map = 0; @@ -108,7 +108,7 @@ ssize_t ecp_rbuf_handle_ack(ECPConnection *conn, ecp_seq_t seq, unsigned char mt (msg[6] << 8) | \ (msg[7]); - if (ctx->tr.buf_flag_set) ctx->tr.buf_flag_set(b, ECP_SEND_FLAG_MORE); + ecp_tr_buf_flag_set(b, ECP_SEND_FLAG_MORE); #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&buf->mutex); @@ -201,7 +201,7 @@ ssize_t ecp_rbuf_handle_ack(ECPConnection *conn, ecp_seq_t seq, unsigned char mt if (_rv < 0) rv = _rv; } - if (ctx->tr.buf_flag_clear) ctx->tr.buf_flag_clear(b, ECP_SEND_FLAG_MORE); + ecp_tr_buf_flag_clear(b, ECP_SEND_FLAG_MORE); if (rv) return rv; return rsize; diff --git a/code/ecp/timer.c b/code/ecp/timer.c index f8d710b..695d42a 100644 --- a/code/ecp/timer.c +++ b/code/ecp/timer.c @@ -1,4 +1,5 @@ #include "core.h" +#include "tm.h" int ecp_timer_create(ECPTimer *timer) { int rv = ECP_OK; @@ -37,10 +38,9 @@ int ecp_timer_item_init(ECPTimerItem *ti, ECPConnection *conn, unsigned char mty int ecp_timer_push(ECPTimerItem *ti) { int i, is_reg, rv = ECP_OK; ECPConnection *conn = ti->conn; - ECPContext *ctx = conn->sock->ctx; ECPTimer *timer = &conn->sock->timer; - ti->abstime = ctx->tm.abstime_ms(ti->timeout); + ti->abstime = ecp_tm_abstime_ms(ti->timeout); #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&timer->mutex); @@ -56,7 +56,7 @@ int ecp_timer_push(ECPTimerItem *ti) { if (!rv && !is_reg) rv = ECP_ERR_CLOSED; if (!rv) { - if (ctx->tm.timer_set) ctx->tm.timer_set(ti->timeout); + ecp_tm_timer_set(ti->timeout); for (i=timer->head; i>=0; i--) { if (ECP_CTS_LTE(ti->abstime, timer->item[i].abstime)) { if (i != timer->head) memmove(timer->item+i+2, timer->item+i+1, sizeof(ECPTimerItem) * (timer->head-i)); @@ -154,7 +154,7 @@ ecp_cts_t ecp_timer_exe(ECPSocket *sock) { ECPTimer *timer = &sock->timer; ECPTimerItem to_exec[ECP_MAX_TIMER]; int to_exec_size = 0; - ecp_cts_t now = sock->ctx->tm.abstime_ms(0); + ecp_cts_t now = ecp_tm_abstime_ms(0); #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&timer->mutex); diff --git a/code/ecp/tm.h b/code/ecp/tm.h new file mode 100644 index 0000000..77b175d --- /dev/null +++ b/code/ecp/tm.h @@ -0,0 +1,3 @@ +ecp_cts_t ecp_tm_abstime_ms(ecp_cts_t msec); +void ecp_tm_sleep_ms(ecp_cts_t msec); +void ecp_tm_timer_set(ecp_cts_t next); diff --git a/code/ecp/tr.h b/code/ecp/tr.h new file mode 100644 index 0000000..c189925 --- /dev/null +++ b/code/ecp/tr.h @@ -0,0 +1,9 @@ +int ecp_tr_addr_eq(ECPNetAddr *addr1, ECPNetAddr *addr2); +int ecp_tr_addr_set(ECPNetAddr *addr, void *addr_s); +int ecp_tr_open(int *sock, void *addr_s); +void ecp_tr_close(int *sock); +ssize_t ecp_tr_send(int *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr *addr, unsigned char flags); +ssize_t ecp_tr_recv(int *sock, ECPBuffer *packet, ECPNetAddr *addr, int timeout); +void ecp_tr_buf_free(ECP2Buffer *b, unsigned char flags); +void ecp_tr_buf_flag_set(ECP2Buffer *b, unsigned char flags); +void ecp_tr_buf_flag_clear(ECP2Buffer *b, unsigned char flags); diff --git a/code/ecp/vconn/vconn.c b/code/ecp/vconn/vconn.c index 3e178bb..9549598 100644 --- a/code/ecp/vconn/vconn.c +++ b/code/ecp/vconn/vconn.c @@ -1,5 +1,10 @@ #include <core.h> +#ifdef ECP_WITH_HTABLE +#include <ht.h> +#endif +#include <cr.h> + #include "vconn.h" #ifdef ECP_WITH_HTABLE @@ -20,7 +25,6 @@ static ECPConnHandler handler_vl; #ifdef ECP_WITH_HTABLE static int vconn_create(ECPConnection *conn, unsigned char *payload, size_t size) { - ECPContext *ctx = conn->sock->ctx; ECPVConnIn *conn_v = (ECPVConnIn *)conn; int rv = ECP_OK; @@ -37,7 +41,7 @@ static int vconn_create(ECPConnection *conn, unsigned char *payload, size_t size #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&key_next_mutex); #endif - rv = ctx->ht.insert(key_next_table, conn_v->key_next[conn_v->key_next_curr], conn); + rv = ecp_ht_insert(key_next_table, conn_v->key_next[conn_v->key_next_curr], conn); #ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&key_next_mutex); #endif @@ -46,7 +50,6 @@ static int vconn_create(ECPConnection *conn, unsigned char *payload, size_t size } static void vconn_destroy(ECPConnection *conn) { - ECPContext *ctx = conn->sock->ctx; ECPVConnIn *conn_v = (ECPVConnIn *)conn; if (conn->out) return; @@ -58,7 +61,7 @@ static void vconn_destroy(ECPConnection *conn) { #endif int i; for (i=0; i<ECP_MAX_NODE_KEY; i++) { - if (memcmp(conn_v->key_next[i], key_null, ECP_ECDH_SIZE_KEY)) ctx->ht.remove(key_next_table, conn_v->key_next[i]); + if (memcmp(conn_v->key_next[i], key_null, ECP_ECDH_SIZE_KEY)) ecp_ht_remove(key_next_table, conn_v->key_next[i]); } #ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&conn->mutex); @@ -117,7 +120,6 @@ static ssize_t vconn_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned ch int rv = ECP_OK; #ifdef ECP_WITH_HTABLE - ECPContext *ctx = conn->sock->ctx; ECPVConnIn *conn_v = (ECPVConnIn *)conn; unsigned char ctype = 0; @@ -136,8 +138,8 @@ static ssize_t vconn_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned ch if (!ecp_conn_is_open(conn)) conn->flags |= ECP_CONN_FLAG_OPEN; if (memcmp(conn_v->key_next[conn_v->key_next_curr], msg, ECP_ECDH_SIZE_KEY)) { conn_v->key_next_curr = (conn_v->key_next_curr + 1) % ECP_MAX_NODE_KEY; - if (memcmp(conn_v->key_next[conn_v->key_next_curr], key_null, ECP_ECDH_SIZE_KEY)) ctx->ht.remove(key_next_table, conn_v->key_next[conn_v->key_next_curr]); - rv = ctx->ht.insert(key_next_table, conn_v->key_next[conn_v->key_next_curr], conn); + if (memcmp(conn_v->key_next[conn_v->key_next_curr], key_null, ECP_ECDH_SIZE_KEY)) ecp_ht_remove(key_next_table, conn_v->key_next[conn_v->key_next_curr]); + rv = ecp_ht_insert(key_next_table, conn_v->key_next[conn_v->key_next_curr], conn); if (!rv) memcpy(conn_v->key_next[conn_v->key_next_curr], msg, ECP_ECDH_SIZE_KEY); } @@ -161,7 +163,6 @@ static ssize_t vconn_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned ch #ifdef ECP_WITH_HTABLE static ssize_t vconn_handle_relay(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) { - ECPContext *ctx = conn->sock->ctx; ECPConnection *conn_out = NULL; ECPVConnIn *conn_v = (ECPVConnIn *)conn; ssize_t rv; @@ -176,7 +177,7 @@ static ssize_t vconn_handle_relay(ECPConnection *conn, ecp_seq_t seq, unsigned c #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&key_perma_mutex); #endif - conn_out = ctx->ht.search(key_perma_table, conn_v->key_out); + conn_out = ecp_ht_search(key_perma_table, conn_v->key_out); if (conn_out) { #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn_out->mutex); @@ -213,12 +214,11 @@ static ssize_t vconn_handle_relay(ECPConnection *conn, ecp_seq_t seq, unsigned c static int vlink_insert(ECPConnection *conn) { int rv = ECP_OK; - ECPContext *ctx = conn->sock->ctx; #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&key_perma_mutex); #endif - rv = ctx->ht.insert(key_perma_table, ctx->cr.dh_pub_get_buf(&conn->node.public), conn); + rv = ecp_ht_insert(key_perma_table, ecp_cr_dh_pub_get_buf(&conn->node.public), conn); #ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&key_perma_mutex); #endif @@ -227,33 +227,27 @@ static int vlink_insert(ECPConnection *conn) { } static void vlink_remove(ECPConnection *conn) { - ECPContext *ctx = conn->sock->ctx; - #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&key_perma_mutex); #endif - ctx->ht.remove(key_perma_table, ctx->cr.dh_pub_get_buf(&conn->node.public)); + ecp_ht_remove(key_perma_table, ecp_cr_dh_pub_get_buf(&conn->node.public)); #ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&key_perma_mutex); #endif } static int vlink_create(ECPConnection *conn, unsigned char *payload, size_t size) { - ECPContext *ctx = conn->sock->ctx; - if (conn->out) return ECP_ERR; if (conn->type != ECP_CTYPE_VLINK) return ECP_ERR; // XXX should verify perma_key if (size < ECP_ECDH_SIZE_KEY) return ECP_ERR; - ctx->cr.dh_pub_from_buf(&conn->node.public, payload); + ecp_cr_dh_pub_from_buf(&conn->node.public, payload); return vlink_insert(conn); } static void vlink_destroy(ECPConnection *conn) { - ECPContext *ctx = conn->sock->ctx; - if (conn->out) return; if (conn->type != ECP_CTYPE_VLINK) return; @@ -263,8 +257,6 @@ static void vlink_destroy(ECPConnection *conn) { static ssize_t _vlink_send_open(ECPConnection *conn, ECPTimerItem *ti) { ECPSocket *sock = conn->sock; - ECPContext *ctx = sock->ctx; - ECPBuffer packet; ECPBuffer payload; unsigned char pkt_buf[ECP_SIZE_PKT_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_OPEN_REQ, conn)]; @@ -280,7 +272,7 @@ static ssize_t _vlink_send_open(ECPConnection *conn, ECPTimerItem *ti) { // XXX server should verify perma_key ecp_pld_set_type(pld_buf, ECP_MTYPE_OPEN_REQ); buf[0] = conn->type; - memcpy(buf+1, ctx->cr.dh_pub_get_buf(&sock->key_perma.public), ECP_ECDH_SIZE_KEY); + memcpy(buf+1, ecp_cr_dh_pub_get_buf(&sock->key_perma.public), ECP_ECDH_SIZE_KEY); return ecp_pld_send_wtimer(conn, &packet, &payload, ECP_SIZE_PLD(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_OPEN_REQ), 0, ti); } @@ -345,7 +337,6 @@ static ssize_t vlink_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned ch #ifdef ECP_WITH_HTABLE static ssize_t vlink_handle_relay(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) { - ECPContext *ctx = conn->sock->ctx; ssize_t rv; if (conn->type != ECP_CTYPE_VLINK) return ECP_ERR; @@ -357,7 +348,7 @@ static ssize_t vlink_handle_relay(ECPConnection *conn, ecp_seq_t seq, unsigned c #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&key_next_mutex); #endif - conn = ctx->ht.search(key_next_table, msg+ECP_SIZE_PROTO+1); + conn = ecp_ht_search(key_next_table, msg+ECP_SIZE_PROTO+1); if (conn) { #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn->mutex); @@ -396,7 +387,6 @@ static ssize_t vlink_handle_relay(ECPConnection *conn, ecp_seq_t seq, unsigned c static ssize_t vconn_set_msg(ECPConnection *conn, ECPBuffer *payload, unsigned char mtype) { if (conn->out && (conn->type == ECP_CTYPE_VCONN) && ((mtype == ECP_MTYPE_OPEN_REQ) || (mtype == ECP_MTYPE_KGET_REQ))) { ECPVConnection *conn_v = (ECPVConnection *)conn; - ECPContext *ctx = conn->sock->ctx; ECPConnection *conn_next = conn_v->next; unsigned char *buf = NULL; int rv; @@ -411,7 +401,7 @@ static ssize_t vconn_set_msg(ECPConnection *conn, ECPBuffer *payload, unsigned c rv = ecp_conn_dhkey_get_curr(conn_next, NULL, buf+1); if (rv) return rv; - memcpy(buf+1+ECP_ECDH_SIZE_KEY, ctx->cr.dh_pub_get_buf(&conn_next->node.public), ECP_ECDH_SIZE_KEY); + memcpy(buf+1+ECP_ECDH_SIZE_KEY, ecp_cr_dh_pub_get_buf(&conn_next->node.public), ECP_ECDH_SIZE_KEY); buf[1+2*ECP_ECDH_SIZE_KEY] = ECP_MTYPE_RELAY; return ECP_SIZE_PLD_HDR + 3 + 2 * ECP_ECDH_SIZE_KEY; @@ -426,9 +416,7 @@ static ssize_t vconn_set_msg(ECPConnection *conn, ECPBuffer *payload, unsigned c } /* Memory limited version: */ -static ssize_t vconn_pack(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, ECPSeqItem *si, ECPNetAddr *addr) { - ECPContext *ctx = conn->sock->ctx; - +ssize_t ecp_pack(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, ECPSeqItem *si, ECPNetAddr *addr) { if ((packet == NULL) || (packet->buffer == NULL)) return ECP_ERR; if ((payload == NULL) || (payload->buffer == NULL)) return ECP_ERR; @@ -443,13 +431,13 @@ static ssize_t vconn_pack(ECPConnection *conn, ECPBuffer *packet, unsigned char if (payload->size < rv+hdr_size) return ECP_ERR; memcpy(payload->buffer, packet->buffer, rv+hdr_size); - return vconn_pack(conn->parent, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload, rv+hdr_size, NULL, addr); + return ecp_pack(conn->parent, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload, rv+hdr_size, NULL, addr); } else { return ecp_conn_pack(conn, packet->buffer, packet->size, s_idx, c_idx, payload->buffer, pld_size, si, addr); } } -static ssize_t vconn_pack_raw(ECPSocket *sock, ECPConnection *parent, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, ECPBuffer *payload, size_t pld_size, ECPNetAddr *addr) { +ssize_t ecp_pack_raw(ECPSocket *sock, ECPConnection *parent, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, ECPBuffer *payload, size_t pld_size, ECPNetAddr *addr) { ECPContext *ctx = sock->ctx; if ((packet == NULL) || (packet->buffer == NULL)) return ECP_ERR; @@ -460,23 +448,21 @@ static ssize_t vconn_pack_raw(ECPSocket *sock, ECPConnection *parent, ECPBuffer ssize_t rv, hdr_size = vconn_set_msg(parent, packet, mtype); if (hdr_size < 0) return hdr_size; - rv = ecp_pack(ctx, packet->buffer+hdr_size, packet->size-hdr_size, s_idx, c_idx, public, shsec, nonce, seq, payload->buffer, pld_size); + rv = ecp_pkt_pack(ctx, packet->buffer+hdr_size, packet->size-hdr_size, s_idx, c_idx, public, shsec, nonce, seq, payload->buffer, pld_size); if (rv < 0) return rv; if (payload->size < rv+hdr_size) return ECP_ERR; memcpy(payload->buffer, packet->buffer, rv+hdr_size); - return vconn_pack(parent, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload, rv+hdr_size, NULL, addr); + return ecp_pack(parent, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload, rv+hdr_size, NULL, addr); } else { - return ecp_pack(ctx, packet->buffer, packet->size, s_idx, c_idx, public, shsec, nonce, seq, payload->buffer, pld_size); + return ecp_pkt_pack(ctx, packet->buffer, packet->size, s_idx, c_idx, public, shsec, nonce, seq, payload->buffer, pld_size); } } /* Non memory limited version: */ /* -static ssize_t vconn_pack(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, ECPSeqItem *si, ECPNetAddr *addr) { - ECPContext *ctx = conn->sock->ctx; - +static ssize_t ecp_pack(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, ECPSeqItem *si, ECPNetAddr *addr) { if (conn->parent) { ECPBuffer payload_; unsigned char pld_buf[ECP_MAX_PLD]; @@ -491,13 +477,13 @@ static ssize_t vconn_pack(ECPConnection *conn, ECPBuffer *packet, unsigned char rv = ecp_conn_pack(conn, payload_.buffer+hdr_size, payload_.size-hdr_size, s_idx, c_idx, payload->buffer, pld_size, si, NULL); if (rv < 0) return rv; - return vconn_pack(conn->parent, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, &payload_, rv+hdr_size, NULL, addr); + return ecp_pack(conn->parent, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, &payload_, rv+hdr_size, NULL, addr); } else { return ecp_conn_pack(conn, packet->buffer, packet->size, s_idx, c_idx, payload->buffer, pld_size, si, addr); } } -static ssize_t vconn_pack_raw(ECPSocket *sock, ECPConnection *parent, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, ECPBuffer *payload, size_t pld_size, ECPNetAddr *addr) { +static ssize_t ecp_pack_raw(ECPSocket *sock, ECPConnection *parent, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, ECPBuffer *payload, size_t pld_size, ECPNetAddr *addr) { ECPContext *ctx = sock->ctx; if (parent) { @@ -511,12 +497,12 @@ static ssize_t vconn_pack_raw(ECPSocket *sock, ECPConnection *parent, ECPBuffer ssize_t rv, hdr_size = vconn_set_msg(parent, &payload_, mtype); if (hdr_size < 0) return hdr_size; - rv = ecp_pack(ctx, payload_.buffer+hdr_size, payload_.size-hdr_size, s_idx, c_idx, public, shsec, nonce, seq, payload->buffer, pld_size); + rv = ecp_pkt_pack(ctx, payload_.buffer+hdr_size, payload_.size-hdr_size, s_idx, c_idx, public, shsec, nonce, seq, payload->buffer, pld_size); if (rv < 0) return rv; - return vconn_pack(parent, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, &payload_, rv+hdr_size, NULL, addr); + return ecp_pack(parent, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, &payload_, rv+hdr_size, NULL, addr); } else { - return ecp_pack(ctx, packet->buffer, packet->size, s_idx, c_idx, public, shsec, nonce, seq, payload->buffer, pld_size); + return ecp_pkt_pack(ctx, packet->buffer, packet->size, s_idx, c_idx, public, shsec, nonce, seq, payload->buffer, pld_size); } } */ @@ -558,17 +544,14 @@ int ecp_ctx_create_vconn(ECPContext *ctx) { #endif /* ECP_WITH_HTABLE */ ctx->handler[ECP_CTYPE_VLINK] = &handler_vl; - ctx->pack = vconn_pack; - ctx->pack_raw = vconn_pack_raw; - #ifdef ECP_WITH_HTABLE #ifdef ECP_WITH_PTHREAD rv = pthread_mutex_init(&key_perma_mutex, NULL); if (!rv) pthread_mutex_init(&key_next_mutex, NULL); if (rv) return ECP_ERR; #endif - key_perma_table = ctx->ht.create(ctx); - key_next_table = ctx->ht.create(ctx); + key_perma_table = ecp_ht_create(ctx); + key_next_table = ecp_ht_create(ctx); if ((key_perma_table == NULL) || (key_next_table == NULL)) return ECP_ERR; #endif /* ECP_WITH_HTABLE */ diff --git a/code/util/util.c b/code/util/util.c index af47079..4f4adc2 100644 --- a/code/util/util.c +++ b/code/util/util.c @@ -3,6 +3,7 @@ #include <unistd.h> #include "core.h" +#include "cr.h" #include "util.h" int ecp_util_key_save(ECPContext *ctx, ECPDHKey *key, char *filename) { @@ -10,7 +11,7 @@ int ecp_util_key_save(ECPContext *ctx, ECPDHKey *key, char *filename) { ssize_t rv; if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR; - rv = write(fd, ctx->cr.dh_pub_get_buf(&key->public), ECP_ECDH_SIZE_KEY); + rv = write(fd, ecp_cr_dh_pub_get_buf(&key->public), ECP_ECDH_SIZE_KEY); if (rv != ECP_ECDH_SIZE_KEY) { close(fd); return ECP_ERR; @@ -42,7 +43,7 @@ int ecp_util_key_load(ECPContext *ctx, ECPDHKey *key, char *filename) { } close(fd); - ctx->cr.dh_pub_from_buf(&key->public, buf); + ecp_cr_dh_pub_from_buf(&key->public, buf); key->valid = 1; return ECP_OK; @@ -53,7 +54,7 @@ int ecp_util_node_save(ECPContext *ctx, ECPNode *node, char *filename) { ssize_t rv; if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR; - rv = write(fd, ctx->cr.dh_pub_get_buf(&node->public), ECP_ECDH_SIZE_KEY); + rv = write(fd, ecp_cr_dh_pub_get_buf(&node->public), ECP_ECDH_SIZE_KEY); if (rv != ECP_ECDH_SIZE_KEY) { close(fd); return ECP_ERR; @@ -85,7 +86,7 @@ int ecp_util_node_load(ECPContext *ctx, ECPNode *node, char *filename) { } close(fd); - ctx->cr.dh_pub_from_buf(&node->public, buf); + ecp_cr_dh_pub_from_buf(&node->public, buf); return ECP_OK; }
\ No newline at end of file |