Add sodium_mlock() and sodium_munlock()

This commit is contained in:
Frank Denis
2014-04-16 18:18:44 -07:00
parent 53f3784a18
commit 7b07e38c66
2 changed files with 36 additions and 0 deletions
+6
View File
@@ -35,6 +35,12 @@ int sodium_hex2bin(unsigned char * const bin, const size_t bin_maxlen,
const char * const ignore, size_t * const bin_len,
const char ** const hex_end);
SODIUM_EXPORT
int sodium_mlock(const void *addr, const size_t len);
SODIUM_EXPORT
int sodium_munlock(const void *addr, const size_t len);
#ifdef __cplusplus
}
#endif
+30
View File
@@ -9,6 +9,10 @@
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_SYS_MMAN_H
# include <sys/mman.h>
#endif
#include "utils.h"
#include "randombytes.h"
#ifdef _WIN32
@@ -152,3 +156,29 @@ sodium_hex2bin(unsigned char * const bin, const size_t bin_maxlen,
}
return ret;
}
int
sodium_mlock(const void *addr, const size_t len)
{
#ifdef HAVE_MLOCK
return mlock(addr, len);
#elif defined(HAVE_VIRTUALLOCK)
return -(VirtualLock(addr, len) != 0);
#else
errno = ENOSYS;
return -1;
#endif
}
int
sodium_munlock(const void *addr, const size_t len)
{
#ifdef HAVE_MLOCK
return munlock(addr, len);
#elif defined(HAVE_VIRTUALLOCK)
return -(VirtualUnlock(addr, len) != 0);
#else
errno = ENOSYS;
return -1;
#endif
}