VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Crypto/jitterentropy-base-user.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/Crypto/jitterentropy-base-user.h')
-rw-r--r--src/Crypto/jitterentropy-base-user.h68
1 files changed, 56 insertions, 12 deletions
diff --git a/src/Crypto/jitterentropy-base-user.h b/src/Crypto/jitterentropy-base-user.h
index cbb2f47e..aaefb41a 100644
--- a/src/Crypto/jitterentropy-base-user.h
+++ b/src/Crypto/jitterentropy-base-user.h
@@ -1,7 +1,7 @@
/*
* Non-physical true random number generator based on timing jitter.
*
- * Copyright Stephan Mueller <smueller@chronox.de>, 2013
+ * Copyright Stephan Mueller <smueller@chronox.de>, 2013 - 2019
*
* License
* =======
@@ -35,7 +35,7 @@
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
- e USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
+ * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
@@ -49,8 +49,6 @@
#include <stdlib.h>
#include <string.h>
-typedef uint64 __u64;
-
#ifdef _MSC_VER
typedef uint64 uint64_t;
@@ -70,17 +68,31 @@ typedef int32 ssize_t;
#endif
#endif
-static VC_INLINE void jent_get_nstime(__u64 *out)
+static VC_INLINE void jent_get_nstime(uint64 *out)
{
- *out = __rdtsc();;
+#ifdef _M_ARM64
+ LARGE_INTEGER v = { 0 };
+#ifdef TC_WINDOWS_DRIVER
+ v = KeQueryPerformanceCounter(NULL);
+#else
+ QueryPerformanceCounter(&v);
+#endif
+ * out = v.QuadPart;
+#else
+ *out = __rdtsc();
+#endif
}
#else
+#include <sys/types.h>
+
+#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
+
/* taken from Linux kernel */
#if CRYPTOPP_BOOL_X64
#define DECLARE_ARGS(val, low, high) unsigned low, high
-#define EAX_EDX_VAL(val, low, high) ((low) | ((__u64)(high) << 32))
+#define EAX_EDX_VAL(val, low, high) ((low) | ((uint64)(high) << 32))
#define EAX_EDX_RET(val, low, high) "=a" (low), "=d" (high)
#else
#define DECLARE_ARGS(val, low, high) unsigned long long val
@@ -88,16 +100,42 @@ static VC_INLINE void jent_get_nstime(__u64 *out)
#define EAX_EDX_RET(val, low, high) "=A" (val)
#endif
-static VC_INLINE void jent_get_nstime(__u64 *out)
+VC_INLINE void jent_get_nstime(uint64 *out)
{
DECLARE_ARGS(val, low, high);
asm volatile("rdtsc" : EAX_EDX_RET(val, low, high));
*out = EAX_EDX_VAL(val, low, high);
}
+#else
+
+#include <time.h>
+
+VC_INLINE void jent_get_nstime(uint64 *out)
+{
+ /* we could use CLOCK_MONOTONIC(_RAW), but with CLOCK_REALTIME
+ * we get some nice extra entropy once in a while from the NTP actions
+ * that we want to use as well... though, we do not rely on that
+ * extra little entropy */
+ uint64_t tmp = 0;
+ struct timespec time;
+ if (clock_gettime(CLOCK_REALTIME, &time) == 0)
+ {
+ tmp = time.tv_sec;
+ tmp = tmp << 32;
+ tmp = tmp | time.tv_nsec;
+ }
+ *out = tmp;
+}
+
#endif
-static VC_INLINE void *jent_zalloc(size_t len)
+#endif
+
+#ifdef _MSC_VER
+static
+#endif
+VC_INLINE void *jent_zalloc(size_t len)
{
void *tmp = NULL;
tmp = TCalloc(len);
@@ -111,7 +149,10 @@ static VC_INLINE void *jent_zalloc(size_t len)
return tmp;
}
-static VC_INLINE void jent_zfree(void *ptr, unsigned int len)
+#ifdef _MSC_VER
+static
+#endif
+VC_INLINE void jent_zfree(void *ptr, unsigned int len)
{
if (len % 8)
burn(ptr, len);
@@ -123,9 +164,12 @@ static VC_INLINE void jent_zfree(void *ptr, unsigned int len)
TCfree(ptr);
}
-static VC_INLINE int jent_fips_enabled(void)
+#ifdef _MSC_VER
+static
+#endif
+VC_INLINE int jent_fips_enabled(void)
{
- return 0;
+ return 1;
}
/* --- helpers needed in user space -- */