Leancrypto 0.12.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_memory_support.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 - 2024, Stephan Mueller <smueller@chronox.de>
3 *
4 * License: see LICENSE file in root directory
5 *
6 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
7 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
9 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
10 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
12 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
13 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
14 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 */
19
20#ifndef LC_MEMORY_SUPPORT_H
21#define LC_MEMORY_SUPPORT_H
22
23#include "ext_headers.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29/* Default memory alignment */
30#define LC_MEM_COMMON_ALIGNMENT (8)
31
41#define LC_ALIGNED_BUFFER(name, size, alignment) \
42 uint64_t name[(size + sizeof(uint64_t) - 1) / sizeof(uint64_t)] \
43 __attribute__((aligned(alignment)))
44
45/* Helpers to align a pointer */
46#define LC_ALIGNMENT_MASK(alignment) (alignment - 1)
47#define LC_ALIGN_APPLY(x, mask) (((x) + (mask)) & ~(mask))
48#define LC_ALIGN(x, a) LC_ALIGN_APPLY((x), (uintptr_t)(a))
49
56#define LC_ALIGN_PTR_64(p, a) ((uint64_t *)LC_ALIGN((uintptr_t)(p), (a)));
57
64#define LC_ALIGN_PTR_32(p, a) ((uint32_t *)LC_ALIGN((uintptr_t)(p), (a)))
65
72#define LC_ALIGN_PTR_16(p, a) ((uint16_t *)LC_ALIGN((uintptr_t)(p), (a)))
73
80#define LC_ALIGN_PTR_8(p, a) ((uint8_t *)LC_ALIGN((uintptr_t)(p), (a)))
81
85#define LC_XOR_MIN_ALIGNMENT(min, requested) \
86 ((min < requested) ? (requested) : (min))
87
88/* Macros set during leancrypto compile time for target platform */
89#define LC_DEF_HOST_X86_64
90#undef LC_DEF_HOST_ARM32_NEON
91#undef LC_DEF_HOST_AARCH64
92
93#ifdef LC_DEF_HOST_X86_64
94
95/*
96 * The load of data into __m256i does not require alignment, the store
97 * requires 64 bit alignment by using _mm_storel_pd / _mm_storeh_pd.
98 */
99#define LC_XOR_AVX2_ALIGNMENT (sizeof(uint64_t))
100#define LC_XOR_ALIGNMENT(min) LC_XOR_MIN_ALIGNMENT(min, LC_XOR_AVX2_ALIGNMENT)
101
102#elif (defined(LC_DEF_HOST_ARM32_NEON) || defined(LC_DEF_HOST_AARCH64)) && \
103 !defined(LINUX_KERNEL)
104
105/*
106 * The load of data into uint64x2_t requires 64 bit alignment, the store
107 * requires 64 bit alignment.
108 */
109#define LC_XOR_NEON_ALIGNMENT (sizeof(uint64_t))
110#define LC_XOR_ALIGNMENT(min) LC_XOR_MIN_ALIGNMENT(min, LC_XOR_NEON_ALIGNMENT)
111
112#else
113
114#define LC_XOR_ALIGNMENT(min) LC_XOR_MIN_ALIGNMENT(min, (sizeof(uint64_t)))
115
116#endif
117
125int lc_alloc_aligned(void **memptr, size_t alignment, size_t size);
126
135int lc_alloc_aligned_secure(void **memptr, size_t alignment, size_t size);
136
144int lc_alloc_high_aligned(void **memptr, size_t alignment, size_t size);
145
153void lc_free(void *ptr);
154
163void lc_free_high_aligned(void *ptr, size_t size);
164
173static inline int lc_mem_aligned(const uint8_t *ptr, uint32_t alignmask)
174{
175 if ((uintptr_t)ptr & alignmask)
176 return 0;
177 return 1;
178}
179
180#ifdef __cplusplus
181}
182#endif
183
184#endif /* LC_MEMORY_SUPPORT_H */
static int lc_mem_aligned(const uint8_t *ptr, uint32_t alignmask)
Check if memory pointer is aligned to given alignment mask.
int lc_alloc_high_aligned(void **memptr, size_t alignment, size_t size)
allocate aligned memory with arbitrary alignment
void lc_free(void *ptr)
free the memory allocated with lc_alloc_aligned
int lc_alloc_aligned_secure(void **memptr, size_t alignment, size_t size)
allocate aligned memory up to 8 bytes alignment with additional security precautions
int lc_alloc_aligned(void **memptr, size_t alignment, size_t size)
allocate aligned memory up to 8 bytes alignment
void lc_free_high_aligned(void *ptr, size_t size)
free the memory allocated with lc_alloc_high_aligned