vendor/libsodium/src/libsodium/sodium/utils.c in rbnacl-libsodium-1.0.3 vs vendor/libsodium/src/libsodium/sodium/utils.c in rbnacl-libsodium-1.0.4
- old
+ new
@@ -21,35 +21,39 @@
# include <wincrypt.h>
#else
# include <unistd.h>
#endif
+#if defined(_WIN32) && (!defined(WINAPI_FAMILY) || WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)
+# define WINAPI_DESKTOP
+#endif
+
#define CANARY_SIZE 16U
#define GARBAGE_VALUE 0xd0
#ifndef MAP_NOCORE
# define MAP_NOCORE 0
#endif
#if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
# define MAP_ANON MAP_ANONYMOUS
#endif
-#if defined(_WIN32) || (defined(MAP_ANON) && defined(HAVE_MMAP)) || defined(HAVE_POSIX_MEMALIGN)
+#if defined(WINAPI_DESKTOP) || (defined(MAP_ANON) && defined(HAVE_MMAP)) || defined(HAVE_POSIX_MEMALIGN)
# define HAVE_ALIGNED_MALLOC
#endif
#if defined(HAVE_MPROTECT) && !(defined(PROT_NONE) && defined(PROT_READ) && defined(PROT_WRITE))
# undef HAVE_MPROTECT
#endif
-#if defined(HAVE_ALIGNED_MALLOC) && (defined(_WIN32) || defined(HAVE_MPROTECT))
+#if defined(HAVE_ALIGNED_MALLOC) && (defined(WINAPI_DESKTOP) || defined(HAVE_MPROTECT))
# define HAVE_PAGE_PROTECTION
#endif
static size_t page_size;
static unsigned char canary[CANARY_SIZE];
#ifdef HAVE_WEAK_SYMBOLS
__attribute__((weak)) void
-_sodium_dummy_symbol_to_prevent_lto(void * const pnt, const size_t len)
+_sodium_dummy_symbol_to_prevent_memzero_lto(void * const pnt, const size_t len)
{
(void) pnt;
(void) len;
}
#endif
@@ -65,46 +69,102 @@
}
#elif defined(HAVE_EXPLICIT_BZERO)
explicit_bzero(pnt, len);
#elif HAVE_WEAK_SYMBOLS
memset(pnt, 0, len);
- _sodium_dummy_symbol_to_prevent_lto(pnt, len);
+ _sodium_dummy_symbol_to_prevent_memzero_lto(pnt, len);
#else
volatile unsigned char *pnt_ = (volatile unsigned char *) pnt;
size_t i = (size_t) 0U;
while (i < len) {
pnt_[i++] = 0U;
}
#endif
}
+#ifdef HAVE_WEAK_SYMBOLS
+__attribute__((weak)) void
+_sodium_dummy_symbol_to_prevent_memcmp_lto(const unsigned char *b1,
+ const unsigned char *b2,
+ const size_t len)
+{
+ (void) b1;
+ (void) b2;
+ (void) len;
+}
+#endif
+
int
sodium_memcmp(const void * const b1_, const void * const b2_, size_t len)
{
+#ifdef HAVE_WEAK_SYMBOLS
const unsigned char *b1 = (const unsigned char *) b1_;
const unsigned char *b2 = (const unsigned char *) b2_;
+#else
+ const volatile unsigned char *b1 = (const volatile unsigned char *) b1_;
+ const volatile unsigned char *b2 = (const volatile unsigned char *) b2_;
+#endif
size_t i;
unsigned char d = (unsigned char) 0U;
+#if HAVE_WEAK_SYMBOLS
+ _sodium_dummy_symbol_to_prevent_memcmp_lto(b1, b2, len);
+#endif
for (i = 0U; i < len; i++) {
d |= b1[i] ^ b2[i];
}
return (int) ((1 & ((d - 1) >> 8)) - 1);
}
+#ifdef HAVE_WEAK_SYMBOLS
+__attribute__((weak)) void
+_sodium_dummy_symbol_to_prevent_compare_lto(const unsigned char *b1,
+ const unsigned char *b2,
+ const size_t len)
+{
+ (void) b1;
+ (void) b2;
+ (void) len;
+}
+#endif
+
+int
+sodium_compare(const unsigned char *b1_, const unsigned char *b2_, size_t len)
+{
+#ifdef HAVE_WEAK_SYMBOLS
+ const unsigned char *b1 = b1_;
+ const unsigned char *b2 = b2_;
+#else
+ const volatile unsigned char *b1 = (const volatile unsigned char *) b1_;
+ const volatile unsigned char *b2 = (const volatile unsigned char *) b2_;
+#endif
+ unsigned char gt = 0U;
+ unsigned char eq = 1U;
+ size_t i;
+
+#if HAVE_WEAK_SYMBOLS
+ _sodium_dummy_symbol_to_prevent_compare_lto(b1, b2, len);
+#endif
+ for (i = (size_t) 0U; i < len; i++) {
+ gt |= ((b2[i] - b1[i]) >> 8) & eq;
+ eq &= ((b2[i] ^ b1[i]) - 1) >> 8;
+ }
+ return (int) (gt + gt + eq) - 1;
+}
+
/* Derived from original code by CodesInChaos */
char *
sodium_bin2hex(char * const hex, const size_t hex_maxlen,
const unsigned char * const bin, const size_t bin_len)
{
size_t i = (size_t) 0U;
unsigned int x;
int b;
int c;
- if (bin_len >= SIZE_MAX / 2 || hex_maxlen < bin_len * 2U) {
+ if (bin_len >= SIZE_MAX / 2 || hex_maxlen <= bin_len * 2U) {
abort(); /* LCOV_EXCL_LINE */
}
while (i < bin_len) {
c = bin[i] & 0xf;
b = bin[i] >> 4;
@@ -182,11 +242,11 @@
# if defined(_SC_PAGESIZE)
long page_size_ = sysconf(_SC_PAGESIZE);
if (page_size_ > 0L) {
page_size = (size_t) page_size_;
}
-# elif defined(_WIN32)
+# elif defined(WINAPI_DESKTOP)
SYSTEM_INFO si;
GetSystemInfo(&si);
page_size = (size_t) si.dwPageSize;
# endif
if (page_size < CANARY_SIZE || page_size < sizeof(size_t)) {
@@ -204,11 +264,11 @@
#if defined(MADV_DONTDUMP) && defined(HAVE_MADVISE)
(void) madvise(addr, len, MADV_DONTDUMP);
#endif
#ifdef HAVE_MLOCK
return mlock(addr, len);
-#elif defined(_WIN32)
+#elif defined(WINAPI_DESKTOP)
return -(VirtualLock(addr, len) == 0);
#else
errno = ENOSYS;
return -1;
#endif
@@ -221,11 +281,11 @@
#if defined(MADV_DODUMP) && defined(HAVE_MADVISE)
(void) madvise(addr, len, MADV_DODUMP);
#endif
#ifdef HAVE_MLOCK
return munlock(addr, len);
-#elif defined(_WIN32)
+#elif defined(WINAPI_DESKTOP)
return -(VirtualUnlock(addr, len) == 0);
#else
errno = ENOSYS;
return -1;
#endif
@@ -234,11 +294,11 @@
static int
_mprotect_noaccess(void *ptr, size_t size)
{
#ifdef HAVE_MPROTECT
return mprotect(ptr, size, PROT_NONE);
-#elif defined(_WIN32)
+#elif defined(WINAPI_DESKTOP)
DWORD old;
return -(VirtualProtect(ptr, size, PAGE_NOACCESS, &old) == 0);
#else
errno = ENOSYS;
return -1;
@@ -248,11 +308,11 @@
static int
_mprotect_readonly(void *ptr, size_t size)
{
#ifdef HAVE_MPROTECT
return mprotect(ptr, size, PROT_READ);
-#elif defined(_WIN32)
+#elif defined(WINAPI_DESKTOP)
DWORD old;
return -(VirtualProtect(ptr, size, PAGE_READONLY, &old) == 0);
#else
errno = ENOSYS;
return -1;
@@ -262,11 +322,11 @@
static int
_mprotect_readwrite(void *ptr, size_t size)
{
#ifdef HAVE_MPROTECT
return mprotect(ptr, size, PROT_READ | PROT_WRITE);
-#elif defined(_WIN32)
+#elif defined(WINAPI_DESKTOP)
DWORD old;
return -(VirtualProtect(ptr, size, PAGE_READWRITE, &old) == 0);
#else
errno = ENOSYS;
return -1;
@@ -306,11 +366,11 @@
} /* LCOV_EXCL_LINE */
# elif defined(HAVE_POSIX_MEMALIGN)
if (posix_memalign(&ptr, page_size, size) != 0) {
ptr = NULL; /* LCOV_EXCL_LINE */
} /* LCOV_EXCL_LINE */
-# elif defined(_WIN32)
+# elif defined(WINAPI_DESKTOP)
ptr = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
# else
# error Bug
# endif
return (unsigned char *) ptr;
@@ -321,11 +381,11 @@
{
# if defined(MAP_ANON) && defined(HAVE_MMAP)
(void) munmap(ptr, size);
# elif defined(HAVE_POSIX_MEMALIGN)
free(ptr);
-# elif defined(_WIN32)
+# elif defined(WINAPI_DESKTOP)
VirtualFree(ptr, 0U, MEM_RELEASE);
# else
# error Bug
# endif
}
@@ -503,6 +563,19 @@
int
sodium_mprotect_readwrite(void *ptr)
{
return _sodium_mprotect(ptr, _mprotect_readwrite);
+}
+
+void
+sodium_increment(unsigned char *n, const size_t nlen)
+{
+ size_t i;
+ unsigned int c = 1U << 8;
+
+ for (i = (size_t) 0U; i < nlen; i++) {
+ c >>= 8;
+ c += n[i];
+ n[i] = (unsigned char) c;
+ }
}