0
0
mirror of https://github.com/OpenVPN/openvpn3.git synced 2024-09-20 20:13:05 +02:00
openvpn3/openvpn/common/memcmp.hpp
James Yonan e7a5d9f55b Start process of moving client logic out of cli.cpp into
general-purpose classes.

Rename ProtoStats to SessionStats and make it more flexible
by using an abstract base class model.

Add a client event queue for the beginnings of a client-backend
API.

Added logic to ProtoContext to invalidate session on certain
kinds of errors in TCP that would be normally be okay in UDP
such as HMAC_ERROR, DECRYPT_ERROR, etc.

Add some alignment adjustment logic for READ_LINK_TCP (3 bytes)
and READ_LINK_UDP (1 byte).
2012-02-04 10:24:54 +00:00

44 lines
1.2 KiB
C++

#ifndef OPENVPN_COMMON_MEMCMP_H
#define OPENVPN_COMMON_MEMCMP_H
#include <cstddef> // defines size_t and NULL
namespace openvpn {
// Is value of type T aligned on A boundary?
// NOTE: requires that sizeof(A) is a power of 2
template <typename T, typename A>
inline bool is_aligned(const T value)
{
return (size_t(value) & (sizeof(A)-1)) == 0;
}
// constant-time memcmp
inline bool memcmp_secure(const unsigned char *p1, const unsigned char *p2, size_t size)
{
typedef unsigned int altword;
if (is_aligned<const unsigned char *, altword>(p1) && is_aligned<const unsigned char *, altword>(p2) && is_aligned<size_t, altword>(size))
{
//std::cout << "*** MEMCMP ALT" << std::endl; // fixme
altword *u1 = (altword *)p1;
altword *u2 = (altword *)p2;
altword a = 0;
size /= sizeof(altword);
while (size--)
a |= (*u1++ ^ *u2++);
return bool(a);
}
else
{
//std::cout << "*** MEMCMP CHAR " << (size_t(p1) & (sizeof(altword)-1)) << ' ' << (size_t(p2) & (sizeof(altword)-1)) << ' ' << size << std::endl; // fixme
unsigned char a = 0;
while (size--)
a |= (*p1++ ^ *p2++);
return bool(a);
}
}
} // namespace openvpn
#endif // OPENVPN_COMMON_MEMCMP_H