1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-23 20:15:19 -07:00

Add _sodium_alignedcalloc()

This commit is contained in:
Frank Denis 2013-04-19 11:19:05 +02:00
parent c3d9659fc6
commit 98a87d8ea7
2 changed files with 32 additions and 1 deletions

View File

@ -10,8 +10,10 @@ 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
#endif

View File

@ -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;
}