mirror of
https://github.com/jedisct1/libsodium.git
synced 2024-12-24 12:36:01 -07:00
Add _sodium_alignedcalloc()
This commit is contained in:
parent
c3d9659fc6
commit
98a87d8ea7
@ -10,6 +10,8 @@ extern "C" {
|
||||
|
||||
void sodium_memzero(void * const pnt, const size_t size);
|
||||
|
||||
void *_sodium_alignedcalloc(void ** const unaligned_p, const size_t len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -1,5 +1,11 @@
|
||||
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "randombytes.h"
|
||||
#ifdef _WIN32
|
||||
# include <Windows.h>
|
||||
# include <Wincrypt.h>
|
||||
@ -19,3 +25,26 @@ sodium_memzero(void * const pnt, const size_t size)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void *
|
||||
_sodium_alignedcalloc(void ** const unaligned_p, const size_t len)
|
||||
{
|
||||
unsigned char *aligned;
|
||||
unsigned char *unaligned;
|
||||
size_t i;
|
||||
|
||||
if (SIZE_MAX - (size_t) 256U < len ||
|
||||
(unaligned = malloc(len + (size_t) 256U)) == NULL) {
|
||||
*unaligned_p = NULL;
|
||||
return NULL;
|
||||
}
|
||||
*unaligned_p = unaligned;
|
||||
for (i = (size_t) 0U; i < len + (size_t) 256U; ++i) {
|
||||
unaligned[i] = (unsigned char) random();
|
||||
}
|
||||
aligned = unaligned + 64;
|
||||
aligned += (ptrdiff_t) 63 & (-(ptrdiff_t) aligned);
|
||||
memset(aligned, 0, len);
|
||||
|
||||
return aligned;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user