Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows port #47

Open
wants to merge 1 commit into
base: obliv-c
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ext/oblivc/atomic_queue.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include<semaphore.h>
#include<stddef.h>
#include<stdbool.h>

// What the heck! Why is there no small library already out there for this?
Expand Down
2 changes: 2 additions & 0 deletions src/ext/oblivc/obliv.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ void execDebugProtocol(ProtocolDesc* pd, protocol_run start, void* arg);
void execNetworkStressProtocol(ProtocolDesc* pd, int bytecount,
protocol_run start, void* arg);
void execYaoProtocol(ProtocolDesc* pd, protocol_run start, void* arg);
void execYaoProtocol_Init(ProtocolDesc* pd, protocol_run start, void* arg);
void execYaoProtocol_End(ProtocolDesc* pd, protocol_run start, void* arg);
void execYaoProtocol_noHalf(ProtocolDesc* pd, protocol_run start, void* arg);
bool execDualexProtocol(ProtocolDesc* pd, protocol_run start, void* arg);
bool execNpProtocol(ProtocolDesc* pd, protocol_run start, void* arg);
Expand Down
117 changes: 109 additions & 8 deletions src/ext/oblivc/obliv_bits.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
#include <inttypes.h>
#include <stdio.h> // for protoUseStdio()
#include <string.h>
#ifndef _WIN32
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netdb.h>
#endif
#include <pthread.h>
#include <unistd.h>
#include <gcrypt.h>
Expand Down Expand Up @@ -92,8 +94,13 @@ static int tcp2PSend(ProtocolTransport* pt,int dest,const void* s,size_t n)
size_t n2=0;
tcpt->needFlush=true;
while(n>n2) {
#ifdef _WIN32
int res = send(tcpt->sock, n2+(char*)s, n-n2, 0);
if(res<0) { perror("TCP write error: "); return res; }
#else
int res = fwrite(n2+(char*)s,1,n-n2,tcpt->sockStream);
if(res<0) { perror("TCP write error: "); return res; }
#endif
n2+=res;
}
return n2;
Expand All @@ -118,12 +125,17 @@ static int tcp2PRecv(ProtocolTransport* pt,int src,void* s,size_t n)
}
while(n>n2)
{
res = fread(n2+(char*)s,1,n-n2, tcpt->sockStream);
if(res<0 || feof(tcpt->sockStream))
{
perror("TCP read error: ");
return res;
}
#ifdef _WIN32
res = recv(tcpt->sock, n2+(char*)s, n-n2, 0);
if(res<0) { perror("TCP read error: "); return res; }
#else
res = fread(n2+(char*)s,1,n-n2, tcpt->sockStream);
if(res<0 || feof(tcpt->sockStream))
{
perror("TCP read error: ");
return res;
}
#endif
n2+=res;
}
return res;
Expand All @@ -132,7 +144,11 @@ static int tcp2PRecv(ProtocolTransport* pt,int src,void* s,size_t n)
static int tcp2PFlush(ProtocolTransport* pt)
{
struct tcp2PTransport* tcpt = CAST(pt);
#ifndef _WIN32
return fflush(tcpt->sockStream);
#else
return 0;
#endif
}

static int tcp2PFlushProfiled(ProtocolTransport* pt)
Expand All @@ -145,8 +161,13 @@ static int tcp2PFlushProfiled(ProtocolTransport* pt)
static void tcp2PCleanup(ProtocolTransport* pt)
{
tcp2PTransport* t = CAST(pt);
#ifdef _WIN32
shutdown(t->sock, SD_SEND);
if(!t->keepAlive) closesocket(t->sock);
#else
fflush(t->sockStream);
if(!t->keepAlive) fclose(t->sockStream);
#endif
free(pt);
}

Expand Down Expand Up @@ -197,10 +218,12 @@ static tcp2PTransport* tcp2PNew(int sock,bool isClient, bool isProfiled)
trans->sock = sock;
trans->isProfiled=isProfiled;
trans->isClient=isClient;
#ifndef _WIN32
trans->sockStream=fdopen(sock, "rb+");
#endif
trans->sinceFlush = 0;
const int one=1;
setsockopt(sock,IPPROTO_TCP,TCP_NODELAY,&one,sizeof(one));
setsockopt(sock,IPPROTO_TCP,TCP_NODELAY,(const char*)&one,sizeof(one));
/*setvbuf(trans->sockStream, trans->buffer, _IOFBF, BUFFER_SIZE);*/
return trans;
}
Expand Down Expand Up @@ -248,6 +271,12 @@ static int tcpConnect(struct sockaddr_in* sa)
int protocolConnectTcp2P(ProtocolDesc* pd,const char* server,const char* port)
{
struct sockaddr_in sa;
#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 0);
WSAStartup(wVersionRequested, &wsaData);
#endif
if(getsockaddr(server,port,(struct sockaddr*)&sa)<0) return -1; // dns error
int sock=tcpConnect(&sa); if(sock<0) return -1;
protocolUseTcp2P(pd,sock,true);
Expand All @@ -266,10 +295,24 @@ int protocolConnectTcp2PProfiled(ProtocolDesc* pd,const char* server,const char*
// used as sock=tcpListenAny(...); sock2=accept(sock); ...; close(both);
static int tcpListenAny(const char* portn)
{
#ifdef _WIN32
uint16_t port;
#else
in_port_t port;
#endif
int outsock;
#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 0);
WSAStartup(wVersionRequested, &wsaData);
#endif
if(sscanf(portn,"%hu",&port)<1) return -1;
#ifdef _WIN32
if((outsock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))<0) return -1;
#else
if((outsock=socket(AF_INET,SOCK_STREAM,0))<0) return -1;
#endif
int reuse = 1;
if (setsockopt(outsock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0)
{ fprintf(stderr,"setsockopt(SO_REUSEADDR) failed\n"); return -1; }
Expand All @@ -287,7 +330,11 @@ int protocolAcceptTcp2P(ProtocolDesc* pd,const char* port)
listenSock = tcpListenAny(port);
if((sock=accept(listenSock,0,0))<0) return -1;
protocolUseTcp2P(pd,sock,false);
#ifdef WIN32
closesocket(listenSock);
#else
close(listenSock);
#endif
return 0;
}

Expand Down Expand Up @@ -333,7 +380,12 @@ static int sockSplit(int sock,ProtocolTransport* t,bool isClient)
if(transSend(t,0,&sa.sin_port,sizeof(sa.sin_port))<0) return -1;
transFlush(t);
int newsock = accept(listenSock,0,0);
close(listenSock);
#ifdef _WIN32
shutdown(listenSock, SD_SEND);
closesocket(listenSock);
#else
close(listenSock);
#endif
return newsock;
}
}
Expand Down Expand Up @@ -1118,6 +1170,43 @@ void mainYaoProtocol(ProtocolDesc* pd, bool point_and_permute,
start(arg);
}

void mainYaoProtocol_Init(ProtocolDesc* pd, bool point_and_permute,
protocol_run start, void* arg)
{
YaoProtocolDesc* ypd = pd->extra;
int me = pd->thisParty;
ypd->ownOT=false;
ypd->gcount = ypd->gcount_offset = ypd->icount = ypd->ocount = 0;
if(me==1)
{
gcry_randomize(ypd->R,YAO_KEY_BYTES,GCRY_STRONG_RANDOM);
gcry_randomize(ypd->I,YAO_KEY_BYTES,GCRY_STRONG_RANDOM);
if(point_and_permute) ypd->R[0] |= 1; // flipper bit

if(ypd->sender.sender==NULL)
{ ypd->ownOT=true;
ypd->sender = honestOTExtSenderAbstract(honestOTExtSenderNew(pd,2));
}
}else
if(ypd->recver.recver==NULL)
{ ypd->ownOT=true;
ypd->recver = honestOTExtRecverAbstract(honestOTExtRecverNew(pd,1));
}

currentProto = pd;
}

void mainYaoProtocol_End(ProtocolDesc* pd, bool point_and_permute,
protocol_run start, void* arg)
{
YaoProtocolDesc* ypd = pd->extra;
int me = pd->thisParty;
//if(ownOT)
{ if(me==1) otSenderRelease(&ypd->sender);
else otRecverRelease(&ypd->recver);
}
}

void cleanupYaoProtocol(ProtocolDesc* pd)
{
YaoProtocolDesc* ypd = pd->extra;
Expand All @@ -1134,6 +1223,18 @@ void execYaoProtocol(ProtocolDesc* pd, protocol_run start, void* arg)
cleanupYaoProtocol(pd);
}

void execYaoProtocol_Init(ProtocolDesc* pd, protocol_run start, void* arg)
{
setupYaoProtocol(pd,true);
mainYaoProtocol_Init(pd,true,start,arg);
}

void execYaoProtocol_End(ProtocolDesc* pd, protocol_run start, void* arg)
{
mainYaoProtocol_End(pd,true,start,arg);
cleanupYaoProtocol(pd);
}

void execYaoProtocol_noHalf(ProtocolDesc* pd, protocol_run start, void* arg)
{
setupYaoProtocol(pd,false);
Expand Down
5 changes: 5 additions & 0 deletions src/ext/oblivc/obliv_bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#define OBLIV_BITS_H
#define __oblivious_c

// To compile with x86_64-w64-mingw32-gcc.exe, it's necessary to explictly define the intrinsics from gcc builtins
#ifdef __CIL_COMPABILITY__
#include <gcc_builtins.h>
#endif

//void* memset(void* s, int c, size_t n); // Hack, had to declare memset
#include<string.h> // memset to zero
#include<stdbool.h>
Expand Down
10 changes: 8 additions & 2 deletions src/ext/oblivc/obliv_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ static inline int transSend(ProtocolTransport* t,int d,const void* p,size_t n)
{ return t->send(t,d,p,n); }
static inline int transRecv(ProtocolTransport* t,int s,void* p,size_t n)
{ return t->recv(t,s,p,n); }
static inline int transFlush(ProtocolTransport* t)
{ if (t->flush) return t->flush(t); else return 0; }
static inline int transFlush(ProtocolTransport* t) {
#ifndef _WIN32
if (t->flush)
return t->flush(t);
else
#endif
return 0;
}
static inline int osend(ProtocolDesc* pd,int d,const void* p,size_t n)
{ return transSend(pd->trans,d,p,n); }
static inline int orecv(ProtocolDesc* pd,int s,void* p,size_t n)
Expand Down
4 changes: 4 additions & 0 deletions src/ext/oblivc/obliv_yao.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const char* yaoKeyOfBit(const OblivBit* b);
extern void setupYaoProtocol(ProtocolDesc* pd,bool halfgates);
extern void mainYaoProtocol(ProtocolDesc* pd, bool point_and_permute,
protocol_run start, void* arg);
extern void mainYaoProtocol_Init(ProtocolDesc* pd, bool point_and_permute,
protocol_run start, void* arg);
extern void mainYaoProtocol_End(ProtocolDesc* pd, bool point_and_permute,
protocol_run start, void* arg);
extern void cleanupYaoProtocol(ProtocolDesc* pd);
extern bool yaoGenrRevealOblivBits(ProtocolDesc* pd,
widest_t* dest,const OblivBit* o,size_t n,int party);
Expand Down
2 changes: 1 addition & 1 deletion src/ext/oblivc/ot.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void dhSend(gcry_mpi_point_t u,ProtocolDesc* pd,int party,
}
// Allocates a new gcry_mpi_t, and returns it
gcry_mpi_point_t dhRecv(ProtocolDesc* pd,int party)
{ char buf[DHEltSerialBytes];
{ char buf[DHEltSerialBytes] = {};
gcry_mpi_point_t x;
orecv(pd,party,buf,DHEltSerialBytes);
dhDeserialize(&x,buf);
Expand Down
5 changes: 5 additions & 0 deletions src/ext/oblivc/psi.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include<obliv_common.h>
#include<obliv_psi.h>
#include<stdlib.h>
#include<sort_r.h> // from https://github.com/noporpoise/sort_r

#define HASH_ALGO GCRY_MD_SHA1
#define HASH_BITS 160
Expand Down Expand Up @@ -173,7 +174,11 @@ OcPsiResult* execPsiProtocol_DH(ProtocolDesc* pd,
xchgPoints(pd,curve,your,nu,mine,ni);

PointCompareArgs *args = pcaNew(curve);
#ifdef _WIN32
sort_r(your,nu,nu*sizeof(gcry_mpi_point_t),compare_points,args);
#else
qsort_r(your,nu,sizeof(*your),compare_points,args);
#endif
OcPsiResult* res = ocPsiResultNew(ni<nu?ni:nu);
for(i=0;i<ni;++i) if(bsearchPoint(mine[i],your,nu,args))
res->indices[res->n++] = order[i];
Expand Down
Loading