From 98a87d8ea745cae26d160fba391f4ce9d38478b0 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 19 Apr 2013 11:19:05 +0200 Subject: [PATCH] Add _sodium_alignedcalloc() --- src/libsodium/include/sodium/utils.h | 4 +++- src/libsodium/sodium/utils.c | 29 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/libsodium/include/sodium/utils.h b/src/libsodium/include/sodium/utils.h index be45be10..1572a960 100644 --- a/src/libsodium/include/sodium/utils.h +++ b/src/libsodium/include/sodium/utils.h @@ -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 diff --git a/src/libsodium/sodium/utils.c b/src/libsodium/sodium/utils.c index acc80702..6c59fccd 100644 --- a/src/libsodium/sodium/utils.c +++ b/src/libsodium/sodium/utils.c @@ -1,5 +1,11 @@ +#include +#include +#include +#include + #include "utils.h" +#include "randombytes.h" #ifdef _WIN32 # include # include @@ -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; +}