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

fixed issues regarding unsigned comparison with 0 #20

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 13 additions & 5 deletions src/abycore/util/crypto/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,17 +440,25 @@ void sha512_hash(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t n

//Read random bytes from /dev/random - copied from stackoverflow (post by zneak)
void gen_secure_random(uint8_t* dest, uint32_t nbytes) {
int32_t randomData = open("/dev/random", O_RDONLY);
uint32_t bytectr = 0;
int fd = open("/dev/random", O_RDONLY);
if (fd < 0)
{
cerr << "Unable to open /dev/random, exiting" << endl;
exit(0);
}
size_t bytectr = 0;
while (bytectr < nbytes) {
uint32_t result = read(randomData, dest + bytectr, nbytes - bytectr);
ssize_t result = read(fd, dest + bytectr, nbytes - bytectr);
if (result < 0) {
cerr << "Unable to read from /dev/random, exiting" << endl;
exit(0);
}
bytectr += result;
bytectr += static_cast<size_t>(result);
}
if (close(fd) < 0)
{
cerr << "Unable to close /dev/random" << endl;
}
close(randomData);
}

seclvl get_sec_lvl(uint32_t symsecbits) {
Expand Down
53 changes: 46 additions & 7 deletions src/abycore/util/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,21 @@ class CSocket {
uint64_t Receive(void* pBuf, uint64_t nLen, int nFlags = 0) {
char* p = (char*) pBuf;
uint64_t n = nLen;
uint64_t ret = 0;
#ifdef WIN32
int ret = 0;
#else // POSIX
ssize_t ret = 0;
#endif

m_nRcvCount += nLen;


while (n > 0) {
ret = recv(m_hSock, p, n, 0);
ret = recv(m_hSock, p, n, nFlags);
#ifdef WIN32
if( ret <= 0 )
{
return ret;
return nLen - n;
}
#else
if (ret < 0) {
Expand All @@ -245,22 +249,57 @@ class CSocket {
} else {
cerr << "socket recv error: " << errno << endl;
perror("Socket error ");
return ret;
return nLen - n;
}
} else if (ret == 0) {
return ret;
cerr << "socket recv: unexpected shutdown by peer\n";
return nLen - n;
}
#endif

p += ret;
n -= ret;
n -= static_cast<uint64_t>(ret);
}
return nLen;
}

int Send(const void* pBuf, uint64_t nLen, int nFlags = 0) {
char* p = (char*) pBuf;
uint64_t n = nLen;
#ifdef WIN32
int ret = 0;
#else // POSIX
ssize_t ret = 0;
#endif
m_nSndCount += nLen;
return send(m_hSock, (char*) pBuf, nLen, nFlags);

while (n > 0)
{
ret = send(m_hSock, p, n, nFlags);
#ifdef WIN32
if( ret <= 0 )
{
return nLen - n;
}
#else
if (ret < 0)
{
if ( errno == EAGAIN) {
cerr << "socket send eror: EAGAIN" << endl;
SleepMiliSec(200);
continue;
} else {
cerr << "socket send error: " << errno << endl;
perror("Socket error ");
return nLen - n;
}
}
#endif

p += ret;
n -= static_cast<uint64_t>(ret);
}
return nLen;
}

private:
Expand Down