summaryrefslogtreecommitdiffstats
path: root/security/nss/lib/util/secport.c
diff options
context:
space:
mode:
authorwolfbeast <mcwerewolf@gmail.com>2018-02-23 11:04:39 +0100
committerwolfbeast <mcwerewolf@gmail.com>2018-02-23 11:04:39 +0100
commitf1a0f0a56fdd0fc39f255174ce08c06b91c66c94 (patch)
tree99ccc8e212257e1da1902036ae261e8e55d55c1c /security/nss/lib/util/secport.c
parent8781f745556be5d7402d0f3adc67ecfe32fe04a0 (diff)
downloadUXP-f1a0f0a56fdd0fc39f255174ce08c06b91c66c94.tar
UXP-f1a0f0a56fdd0fc39f255174ce08c06b91c66c94.tar.gz
UXP-f1a0f0a56fdd0fc39f255174ce08c06b91c66c94.tar.lz
UXP-f1a0f0a56fdd0fc39f255174ce08c06b91c66c94.tar.xz
UXP-f1a0f0a56fdd0fc39f255174ce08c06b91c66c94.zip
Update NSS to 3.35-RTM
Diffstat (limited to 'security/nss/lib/util/secport.c')
-rw-r--r--security/nss/lib/util/secport.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/security/nss/lib/util/secport.c b/security/nss/lib/util/secport.c
index 01a7d0834..e5bd4c1bb 100644
--- a/security/nss/lib/util/secport.c
+++ b/security/nss/lib/util/secport.c
@@ -21,6 +21,8 @@
#include "prenv.h"
#include "prinit.h"
+#include <stdint.h>
+
#ifdef DEBUG
#define THREADMARK
#endif /* DEBUG */
@@ -119,6 +121,51 @@ PORT_ZAlloc(size_t bytes)
return rv;
}
+/* aligned_alloc is C11. This is an alternative to get aligned memory. */
+void *
+PORT_ZAllocAligned(size_t bytes, size_t alignment, void **mem)
+{
+ size_t x = alignment - 1;
+
+ /* This only works if alignment is a power of 2. */
+ if ((alignment == 0) || (alignment & (alignment - 1))) {
+ PORT_SetError(SEC_ERROR_INVALID_ARGS);
+ return NULL;
+ }
+
+ if (!mem) {
+ return NULL;
+ }
+
+ /* Always allocate a non-zero amount of bytes */
+ *mem = PORT_ZAlloc((bytes ? bytes : 1) + x);
+ if (!*mem) {
+ PORT_SetError(SEC_ERROR_NO_MEMORY);
+ return NULL;
+ }
+
+ return (void *)(((uintptr_t)*mem + x) & ~(uintptr_t)x);
+}
+
+void *
+PORT_ZAllocAlignedOffset(size_t size, size_t alignment, size_t offset)
+{
+ PORT_Assert(offset < size);
+ if (offset > size) {
+ return NULL;
+ }
+
+ void *mem = NULL;
+ void *v = PORT_ZAllocAligned(size, alignment, &mem);
+ if (!v) {
+ return NULL;
+ }
+
+ PORT_Assert(mem);
+ *((void **)((uintptr_t)v + offset)) = mem;
+ return v;
+}
+
void
PORT_Free(void *ptr)
{
@@ -733,3 +780,18 @@ NSS_SecureMemcmp(const void *ia, const void *ib, size_t n)
return r;
}
+
+/*
+ * Perform a constant-time check if a memory region is all 0. The return value
+ * is 0 if the memory region is all zero.
+ */
+unsigned int
+NSS_SecureMemcmpZero(const void *mem, size_t n)
+{
+ PRUint8 zero = 0;
+ size_t i;
+ for (i = 0; i < n; ++i) {
+ zero |= *(PRUint8 *)((uintptr_t)mem + i);
+ }
+ return zero;
+}