-
Notifications
You must be signed in to change notification settings - Fork 26
/
compat.c
149 lines (133 loc) · 4.05 KB
/
compat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* This file is part of the optimized implementation of the Picnic signature scheme.
* See the accompanying documentation for complete details.
*
* The code is provided under the MIT license, see LICENSE for
* more details.
* SPDX-License-Identifier: MIT
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#else
#include "macros.h"
/* define HAVE_* for more known good configurations */
#if !defined(HAVE_POSIX_MEMALIGN) && \
((defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || defined(__APPLE__))
/* defined in POSIX and available on OS X */
#define HAVE_POSIX_MEMALIGN
#endif /* HAVE_POSIX_MEMALIGN */
#if !defined(HAVE_MEMALIGN) && defined(__linux__)
/* always available on Linux */
#define HAVE_MEMALIGN
#endif /* HAVE_MEMALIGN */
#if !defined(HAVE_CONSTTIME_MEMEQUAL) && NETBSD_CHECK(7, 0)
/* consttime_memequal was introduced in NetBSD 7.0 */
#define HAVE_CONSTTIME_MEMEQUAL
#endif /* HAVE_CONSTTIME_MEMEQUAL */
#endif /* HAVE_CONFIG_H */
#include "compat.h"
#if !defined(HAVE_ALIGNED_ALLOC)
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#if !defined(HAVE_POSIX_MEMALIGN) || defined(__MING32__) || defined(__MING64__) || defined(_MSC_VER)
#include <malloc.h>
#endif
#if defined(HAVE_POSIX_MEMALIGN)
static_assert((sizeof(void*) & (sizeof(void*) - 1)) == 0, "sizeof void* is not a power of 2");
#endif
/* The fallback implementation tries to be as generic as possible. While all callers in this code
* base satisfy all requirements, we still check them. Thereby, the fallback implementation may be
* of use for others as well. */
void* picnic_aligned_alloc(size_t alignment, size_t size) {
/* check alignment (power of 2) and size (multiple of alignment) */
if (alignment & (alignment - 1) || size & (alignment - 1)) {
errno = EINVAL;
return NULL;
}
#if defined(HAVE_POSIX_MEMALIGN)
/* check alignment (needs to be multiple of sizeof(void*)); posix_memalign has this additional
* requirement */
if (alignment < sizeof(void*)) {
alignment = sizeof(void*);
/* fix up size; needs to be a multiple of alignment, i.e., sizeof(void*) */
size = (size + sizeof(void*) - 1) & ~(sizeof(void*) - 1);
}
void* ptr = NULL;
const int err = posix_memalign(&ptr, alignment, size);
if (err) {
errno = err;
}
return ptr;
#elif defined(HAVE_MEMALIGN)
return memalign(alignment, size);
#elif defined(__MINGW32__) || defined(__MINGW64__)
return __mingw_aligned_malloc(size, alignment);
#elif defined(_MSC_VER)
return _aligned_malloc(size, alignment);
#else
if (!size) {
return NULL;
}
const size_t offset = alignment - 1 + sizeof(uint8_t);
uint8_t* buffer = malloc(size + offset);
if (!buffer) {
return NULL;
}
uint8_t* ptr = (uint8_t*)(((uintptr_t)(buffer) + offset) & ~(alignment - 1));
ptrdiff_t diff = ptr - buffer;
if (diff > UINT8_MAX) {
/* this should never happen in our code, but just to be safe */
free(buffer);
errno = EINVAL;
return NULL;
}
ptr[-1] = diff;
return ptr;
#endif
}
void picnic_aligned_free(void* ptr) {
#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN)
free(ptr);
#elif defined(__MINGW32__) || defined(__MINGW64__)
__mingw_aligned_free(ptr);
#elif defined(_MSC_VER)
_aligned_free(ptr);
#else
if (ptr) {
uint8_t* u8ptr = ptr;
free(u8ptr - u8ptr[-1]);
}
#endif
}
#endif /* HAVE_ALIGNED_ALLOC */
#if !defined(HAVE_TIMINGSAFE_BCMP)
int picnic_timingsafe_bcmp(const void* a, const void* b, size_t len) {
#if defined(HAVE_CONSTTIME_MEMEQUAL)
return !consttime_memequal(a, b, len);
#else
const unsigned char* p1 = a;
const unsigned char* p2 = b;
unsigned int res = 0;
for (; len; --len, ++p1, ++p2) {
res |= *p1 ^ *p2;
}
return res;
#endif
}
#endif /* HAVE_TIMINGSAFE_BCMP */
#if !defined(HAVE_EXPLICIT_BZERO)
#if defined(_WIN32)
#include <windows.h>
#endif
void picnic_explicit_bzero(void* a, size_t len) {
#if defined(_WIN32)
SecureZeroMemory(a, len);
#else
volatile char* p = a;
for (; len; ++p, --len) {
*p = 0;
}
#endif
}
#endif /* HAVE_EXPLICIT_BZERO */