libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / src / libsodium / sodium / utils.c
blobbf365ea3908fbecdaa9c2b5db1c93332b9a2ebf8
2 #include <limits.h>
3 #include <stddef.h>
4 #include <stdint.h>
5 #include <stdlib.h>
6 #include <string.h>
8 #include "utils.h"
9 #include "randombytes.h"
10 #ifdef _WIN32
11 # include <Windows.h>
12 # include <Wincrypt.h>
13 #endif
15 void
16 sodium_memzero(void * const pnt, const size_t len)
18 #ifdef HAVE_SECUREZEROMEMORY
19 SecureZeroMemory(pnt, len);
20 #else
21 volatile unsigned char *pnt_ = (volatile unsigned char *) pnt;
22 size_t i = (size_t) 0U;
24 while (i < len) {
25 pnt_[i++] = 0U;
27 #endif
30 int
31 sodium_memcmp(const void * const b1_, const void * const b2_, size_t size)
33 const unsigned char *b1 = (const unsigned char *) b1_;
34 const unsigned char *b2 = (const unsigned char *) b2_;
35 size_t i;
36 unsigned char d = (unsigned char) 0U;
38 for (i = 0U; i < size; i++) {
39 d |= b1[i] ^ b2[i];
41 return (int) d;
44 unsigned char *
45 _sodium_alignedcalloc(unsigned char ** const unaligned_p, const size_t len)
47 unsigned char *aligned;
48 unsigned char *unaligned;
49 size_t i;
51 if (SIZE_MAX - (size_t) 256U < len ||
52 (unaligned = (unsigned char *) malloc(len + (size_t) 256U)) == NULL) {
53 *unaligned_p = NULL;
54 return NULL;
56 *unaligned_p = unaligned;
57 for (i = (size_t) 0U; i < len + (size_t) 256U; ++i) {
58 unaligned[i] = (unsigned char) rand();
60 aligned = unaligned + 64;
61 aligned += (ptrdiff_t) 63 & (-(ptrdiff_t) aligned);
62 memset(aligned, 0, len);
64 return aligned;