This commit is contained in:
Frank Denis 2017-02-23 11:12:18 +01:00
parent 0ccc6d7661
commit 9cd732c88d

View File

@ -21,14 +21,15 @@
# include <unistd.h>
#endif
#include "utils.h"
#include "randombytes.h"
#include "utils.h"
#ifndef ENOSYS
# define ENOSYS ENXIO
#endif
#if defined(_WIN32) && (!defined(WINAPI_FAMILY) || WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)
#if defined(_WIN32) && \
(!defined(WINAPI_FAMILY) || WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)
# define WINAPI_DESKTOP
#endif
@ -41,13 +42,16 @@
#if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
# define MAP_ANON MAP_ANONYMOUS
#endif
#if defined(WINAPI_DESKTOP) || (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))
#if defined(HAVE_MPROTECT) && \
!(defined(PROT_NONE) && defined(PROT_READ) && defined(PROT_WRITE))
# undef HAVE_MPROTECT
#endif
#if defined(HAVE_ALIGNED_MALLOC) && (defined(WINAPI_DESKTOP) || defined(HAVE_MPROTECT))
#if defined(HAVE_ALIGNED_MALLOC) && \
(defined(WINAPI_DESKTOP) || defined(HAVE_MPROTECT))
# define HAVE_PAGE_PROTECTION
#endif
#if !defined(MADV_DODUMP) && defined(MADV_CORE)
@ -55,15 +59,16 @@
# define MADV_DONTDUMP MADV_NOCORE
#endif
static size_t page_size;
static size_t page_size;
static unsigned char canary[CANARY_SIZE];
#ifdef HAVE_WEAK_SYMBOLS
__attribute__ ((weak)) void
_sodium_memzero_as_a_weak_symbol_to_prevent_lto(void * const pnt, const size_t len)
__attribute__((weak)) void
_sodium_memzero_as_a_weak_symbol_to_prevent_lto(void *const pnt,
const size_t len)
{
unsigned char *pnt_ = (unsigned char *) pnt;
size_t i = (size_t) 0U;
size_t i = (size_t) 0U;
while (i < len) {
pnt_[i++] = 0U;
@ -72,7 +77,7 @@ _sodium_memzero_as_a_weak_symbol_to_prevent_lto(void * const pnt, const size_t l
#endif
void
sodium_memzero(void * const pnt, const size_t len)
sodium_memzero(void *const pnt, const size_t len)
{
#ifdef _WIN32
SecureZeroMemory(pnt, len);
@ -86,7 +91,7 @@ sodium_memzero(void * const pnt, const size_t len)
_sodium_memzero_as_a_weak_symbol_to_prevent_lto(pnt, len);
#else
volatile unsigned char *volatile pnt_ =
(volatile unsigned char * volatile) pnt;
(volatile unsigned char *volatile) pnt;
size_t i = (size_t) 0U;
while (i < len) {
@ -96,10 +101,10 @@ sodium_memzero(void * const pnt, const size_t len)
}
#ifdef HAVE_WEAK_SYMBOLS
__attribute__ ((weak)) void
__attribute__((weak)) void
_sodium_dummy_symbol_to_prevent_memcmp_lto(const unsigned char *b1,
const unsigned char *b2,
const size_t len)
const size_t len)
{
(void) b1;
(void) b2;
@ -108,19 +113,19 @@ _sodium_dummy_symbol_to_prevent_memcmp_lto(const unsigned char *b1,
#endif
int
sodium_memcmp(const void * const b1_, const void * const b2_, size_t len)
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 *volatile b1 =
(const volatile unsigned char * volatile) b1_;
(const volatile unsigned char *volatile) b1_;
const volatile unsigned char *volatile b2 =
(const volatile unsigned char * volatile) b2_;
(const volatile unsigned char *volatile) b2_;
#endif
size_t i;
unsigned char d = (unsigned char) 0U;
size_t i;
unsigned char d = (unsigned char) 0U;
#if HAVE_WEAK_SYMBOLS
_sodium_dummy_symbol_to_prevent_memcmp_lto(b1, b2, len);
@ -132,10 +137,10 @@ sodium_memcmp(const void * const b1_, const void * const b2_, size_t len)
}
#ifdef HAVE_WEAK_SYMBOLS
__attribute__ ((weak)) void
__attribute__((weak)) void
_sodium_dummy_symbol_to_prevent_compare_lto(const unsigned char *b1,
const unsigned char *b2,
const size_t len)
const size_t len)
{
(void) b1;
(void) b2;
@ -150,10 +155,10 @@ sodium_compare(const unsigned char *b1_, const unsigned char *b2_, size_t len)
const unsigned char *b1 = b1_;
const unsigned char *b2 = b2_;
#else
const volatile unsigned char * volatile b1 =
(const volatile unsigned char * volatile) b1_;
const volatile unsigned char * volatile b2 =
(const volatile unsigned char * volatile) b2_;
const volatile unsigned char *volatile b1 =
(const volatile unsigned char *volatile) b1_;
const volatile unsigned char *volatile b2 =
(const volatile unsigned char *volatile) b2_;
#endif
unsigned char gt = 0U;
unsigned char eq = 1U;
@ -190,28 +195,30 @@ sodium_increment(unsigned char *n, const size_t nlen)
uint_fast16_t c = 1U;
#ifdef HAVE_AMD64_ASM
uint64_t t64, t64_2;
uint32_t t32;
uint64_t t64, t64_2;
uint32_t t32;
if (nlen == 12U) {
__asm__ __volatile__("xorq %[t64], %[t64] \n"
"xorl %[t32], %[t32] \n"
"stc \n"
"adcq %[t64], (%[out]) \n"
"adcl %[t32], 8(%[out]) \n"
: [t64] "=&r"(t64), [t32] "=&r" (t32)
: [out] "D"(n)
: "memory", "flags", "cc");
__asm__ __volatile__(
"xorq %[t64], %[t64] \n"
"xorl %[t32], %[t32] \n"
"stc \n"
"adcq %[t64], (%[out]) \n"
"adcl %[t32], 8(%[out]) \n"
: [t64] "=&r"(t64), [t32] "=&r"(t32)
: [out] "D"(n)
: "memory", "flags", "cc");
return;
} else if (nlen == 24U) {
__asm__ __volatile__("movq $1, %[t64] \n"
"xorq %[t64_2], %[t64_2] \n"
"addq %[t64], (%[out]) \n"
"adcq %[t64_2], 8(%[out]) \n"
"adcq %[t64_2], 16(%[out]) \n"
: [t64] "=&r"(t64), [t64_2] "=&r" (t64_2)
: [out] "D"(n)
: "memory", "flags", "cc");
__asm__ __volatile__(
"movq $1, %[t64] \n"
"xorq %[t64_2], %[t64_2] \n"
"addq %[t64], (%[out]) \n"
"adcq %[t64_2], 8(%[out]) \n"
"adcq %[t64_2], 16(%[out]) \n"
: [t64] "=&r"(t64), [t64_2] "=&r"(t64_2)
: [out] "D"(n)
: "memory", "flags", "cc");
return;
} else if (nlen == 8U) {
__asm__ __volatile__("incq (%[out]) \n"
@ -235,35 +242,38 @@ sodium_add(unsigned char *a, const unsigned char *b, const size_t len)
uint_fast16_t c = 0U;
#ifdef HAVE_AMD64_ASM
uint64_t t64, t64_2, t64_3;
uint32_t t32;
uint64_t t64, t64_2, t64_3;
uint32_t t32;
if (len == 12U) {
__asm__ __volatile__("movq (%[in]), %[t64] \n"
"movl 8(%[in]), %[t32] \n"
"addq %[t64], (%[out]) \n"
"adcl %[t32], 8(%[out]) \n"
: [t64] "=&r"(t64), [t32] "=&r" (t32)
: [in] "S"(b), [out] "D"(a)
: "memory", "flags", "cc");
__asm__ __volatile__(
"movq (%[in]), %[t64] \n"
"movl 8(%[in]), %[t32] \n"
"addq %[t64], (%[out]) \n"
"adcl %[t32], 8(%[out]) \n"
: [t64] "=&r"(t64), [t32] "=&r"(t32)
: [in] "S"(b), [out] "D"(a)
: "memory", "flags", "cc");
return;
} else if (len == 24U) {
__asm__ __volatile__("movq (%[in]), %[t64] \n"
"movq 8(%[in]), %[t64_2] \n"
"movq 16(%[in]), %[t64_3] \n"
"addq %[t64], (%[out]) \n"
"adcq %[t64_2], 8(%[out]) \n"
"adcq %[t64_3], 16(%[out]) \n"
: [t64] "=&r"(t64), [t64_2] "=&r"(t64_2), [t64_3] "=&r"(t64_3)
: [in] "S"(b), [out] "D"(a)
: "memory", "flags", "cc");
__asm__ __volatile__(
"movq (%[in]), %[t64] \n"
"movq 8(%[in]), %[t64_2] \n"
"movq 16(%[in]), %[t64_3] \n"
"addq %[t64], (%[out]) \n"
"adcq %[t64_2], 8(%[out]) \n"
"adcq %[t64_3], 16(%[out]) \n"
: [t64] "=&r"(t64), [t64_2] "=&r"(t64_2), [t64_3] "=&r"(t64_3)
: [in] "S"(b), [out] "D"(a)
: "memory", "flags", "cc");
return;
} else if (len == 8U) {
__asm__ __volatile__("movq (%[in]), %[t64] \n"
"addq %[t64], (%[out]) \n"
: [t64] "=&r"(t64)
: [in] "S"(b), [out] "D"(a)
: "memory", "flags", "cc");
__asm__ __volatile__(
"movq (%[in]), %[t64] \n"
"addq %[t64], (%[out]) \n"
: [t64] "=&r"(t64)
: [in] "S"(b), [out] "D"(a)
: "memory", "flags", "cc");
return;
}
#endif
@ -276,8 +286,8 @@ sodium_add(unsigned char *a, const unsigned char *b, const size_t len)
/* 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)
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;
@ -303,14 +313,14 @@ sodium_bin2hex(char * const hex, const size_t hex_maxlen,
}
int
sodium_hex2bin(unsigned char * const bin, const size_t bin_maxlen,
const char * const hex, const size_t hex_len,
const char * const ignore, size_t * const bin_len,
const char ** const hex_end)
sodium_hex2bin(unsigned char *const bin, const size_t bin_maxlen,
const char *const hex, const size_t hex_len,
const char *const ignore, size_t *const bin_len,
const char **const hex_end)
{
size_t bin_pos = (size_t) 0U;
size_t hex_pos = (size_t) 0U;
int ret = 0;
int ret = 0;
unsigned char c;
unsigned char c_acc = 0U;
unsigned char c_alpha0, c_alpha;
@ -319,10 +329,10 @@ sodium_hex2bin(unsigned char * const bin, const size_t bin_maxlen,
unsigned char state = 0U;
while (hex_pos < hex_len) {
c = (unsigned char) hex[hex_pos];
c_num = c ^ 48U;
c_num0 = (c_num - 10U) >> 8;
c_alpha = (c & ~32U) - 55U;
c = (unsigned char) hex[hex_pos];
c_num = c ^ 48U;
c_num0 = (c_num - 10U) >> 8;
c_alpha = (c & ~32U) - 55U;
c_alpha0 = ((c_alpha - 10U) ^ (c_alpha - 16U)) >> 8;
if ((c_num0 | c_alpha0) == 0U) {
if (ignore != NULL && state == 0U && strchr(ignore, c) != NULL) {
@ -333,7 +343,7 @@ sodium_hex2bin(unsigned char * const bin, const size_t bin_maxlen,
}
c_val = (c_num0 & c_num) | (c_alpha0 & c_alpha);
if (bin_pos >= bin_maxlen) {
ret = -1;
ret = -1;
errno = ERANGE;
break;
}
@ -381,7 +391,7 @@ _sodium_alloc_init(void)
}
int
sodium_mlock(void * const addr, const size_t len)
sodium_mlock(void *const addr, const size_t len)
{
#if defined(MADV_DONTDUMP) && defined(HAVE_MADVISE)
(void) madvise(addr, len, MADV_DONTDUMP);
@ -397,7 +407,7 @@ sodium_mlock(void * const addr, const size_t len)
}
int
sodium_munlock(void * const addr, const size_t len)
sodium_munlock(void *const addr, const size_t len)
{
sodium_memzero(addr, len);
#if defined(MADV_DODUMP) && defined(HAVE_MADVISE)
@ -457,7 +467,7 @@ _mprotect_readwrite(void *ptr, size_t size)
#ifdef HAVE_ALIGNED_MALLOC
__attribute__ ((noreturn)) static void
__attribute__((noreturn)) static void
_out_of_bounds(void)
{
# ifdef SIGSEGV
@ -476,20 +486,21 @@ _page_round(const size_t size)
return (size + page_mask) & ~page_mask;
}
static __attribute__ ((malloc)) unsigned char *
static __attribute__((malloc)) unsigned char *
_alloc_aligned(const size_t size)
{
void *ptr;
# if defined(MAP_ANON) && defined(HAVE_MMAP)
if ((ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE | MAP_NOCORE, -1, 0)) == MAP_FAILED) {
MAP_ANON | MAP_PRIVATE | MAP_NOCORE, -1, 0)) ==
MAP_FAILED) {
ptr = NULL; /* LCOV_EXCL_LINE */
} /* LCOV_EXCL_LINE */
} /* LCOV_EXCL_LINE */
# elif defined(HAVE_POSIX_MEMALIGN)
if (posix_memalign(&ptr, page_size, size) != 0) {
ptr = NULL; /* LCOV_EXCL_LINE */
} /* LCOV_EXCL_LINE */
} /* LCOV_EXCL_LINE */
# elif defined(WINAPI_DESKTOP)
ptr = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
# else
@ -499,7 +510,7 @@ _alloc_aligned(const size_t size)
}
static void
_free_aligned(unsigned char * const ptr, const size_t size)
_free_aligned(unsigned char *const ptr, const size_t size)
{
# if defined(MAP_ANON) && defined(HAVE_MMAP)
(void) munmap(ptr, size);
@ -509,11 +520,11 @@ _free_aligned(unsigned char * const ptr, const size_t size)
VirtualFree(ptr, 0U, MEM_RELEASE);
# else
# error Bug
# endif
#endif
}
static unsigned char *
_unprotected_ptr_from_user_ptr(void * const ptr)
_unprotected_ptr_from_user_ptr(void *const ptr)
{
uintptr_t unprotected_ptr_u;
unsigned char *canary_ptr;
@ -531,13 +542,13 @@ _unprotected_ptr_from_user_ptr(void * const ptr)
#endif /* HAVE_ALIGNED_MALLOC */
#ifndef HAVE_ALIGNED_MALLOC
static __attribute__ ((malloc)) void *
static __attribute__((malloc)) void *
_sodium_malloc(const size_t size)
{
return malloc(size > (size_t) 0U ? size : (size_t) 1U);
}
#else
static __attribute__ ((malloc)) void *
static __attribute__((malloc)) void *
_sodium_malloc(const size_t size)
{
void *user_ptr;
@ -557,7 +568,7 @@ _sodium_malloc(const size_t size)
}
size_with_canary = (sizeof canary) + size;
unprotected_size = _page_round(size_with_canary);
total_size = page_size + page_size + unprotected_size + page_size;
total_size = page_size + page_size + unprotected_size + page_size;
if ((base_ptr = _alloc_aligned(total_size)) == NULL) {
return NULL; /* LCOV_EXCL_LINE */
}
@ -568,8 +579,8 @@ _sodium_malloc(const size_t size)
# endif
_mprotect_noaccess(unprotected_ptr + unprotected_size, page_size);
sodium_mlock(unprotected_ptr, unprotected_size);
canary_ptr = unprotected_ptr + _page_round(size_with_canary) -
size_with_canary;
canary_ptr =
unprotected_ptr + _page_round(size_with_canary) - size_with_canary;
user_ptr = canary_ptr + sizeof canary;
memcpy(canary_ptr, canary, sizeof canary);
memcpy(base_ptr, &unprotected_size, sizeof unprotected_size);
@ -580,7 +591,7 @@ _sodium_malloc(const size_t size)
}
#endif /* !HAVE_ALIGNED_MALLOC */
__attribute__ ((malloc)) void *
__attribute__((malloc)) void *
sodium_malloc(const size_t size)
{
void *ptr;
@ -593,7 +604,7 @@ sodium_malloc(const size_t size)
return ptr;
}
__attribute__ ((malloc)) void *
__attribute__((malloc)) void *
sodium_allocarray(size_t count, size_t size)
{
size_t total_size;
@ -626,9 +637,9 @@ sodium_free(void *ptr)
if (ptr == NULL) {
return;
}
canary_ptr = ((unsigned char *) ptr) - sizeof canary;
canary_ptr = ((unsigned char *) ptr) - sizeof canary;
unprotected_ptr = _unprotected_ptr_from_user_ptr(ptr);
base_ptr = unprotected_ptr - page_size * 2U;
base_ptr = unprotected_ptr - page_size * 2U;
memcpy(&unprotected_size, base_ptr, sizeof unprotected_size);
total_size = page_size + page_size + unprotected_size + page_size;
_mprotect_readwrite(base_ptr, total_size);
@ -636,8 +647,8 @@ sodium_free(void *ptr)
_out_of_bounds();
}
# ifndef HAVE_PAGE_PROTECTION
if (sodium_memcmp(unprotected_ptr + unprotected_size,
canary, sizeof canary) != 0) {
if (sodium_memcmp(unprotected_ptr + unprotected_size, canary,
sizeof canary) != 0) {
_out_of_bounds();
}
# endif
@ -664,7 +675,7 @@ _sodium_mprotect(void *ptr, int (*cb)(void *ptr, size_t size))
size_t unprotected_size;
unprotected_ptr = _unprotected_ptr_from_user_ptr(ptr);
base_ptr = unprotected_ptr - page_size * 2U;
base_ptr = unprotected_ptr - page_size * 2U;
memcpy(&unprotected_size, base_ptr, sizeof unprotected_size);
return cb(unprotected_ptr, unprotected_size);