forked from TunSafe/TunSafe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_ops.h
42 lines (34 loc) · 1.02 KB
/
crypto_ops.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// SPDX-License-Identifier: AGPL-1.0-only
// Copyright (C) 2018 Ludvig Strigeus <info@tunsafe.com>. All Rights Reserved.
#ifndef TUNSAFE_CRYPTO_OPS_H_
#define TUNSAFE_CRYPTO_OPS_H_
#include "build_config.h"
#include "tunsafe_types.h"
#include <string.h>
#if defined(COMPILER_MSVC)
#include <intrin.h>
#endif // defined(COMPILER_MSVC)
#if defined(ARCH_CPU_X86_64) && defined(COMPILER_MSVC)
FORCEINLINE static void memzero_crypto(void *dst, size_t n) {
if (n & 7) {
__stosb((unsigned char*)dst, 0, n);
} else {
__stosq((uint64*)dst, 0, n >> 3);
}
}
#elif defined(ARCH_CPU_X86) && defined(COMPILER_MSVC)
FORCEINLINE static void memzero_crypto(void *dst, size_t n) {
if (n & 3) {
__stosb((unsigned char*)dst, 0, n);
} else {
__stosd((unsigned long*)dst, 0, n >> 2);
}
}
#else
FORCEINLINE static void memzero_crypto(void *dst, size_t n) {
memset(dst, 0, n);
__asm__ __volatile__("": :"r"(dst) :"memory");
}
#endif
int memcmp_crypto(const uint8 *a, const uint8 *b, size_t n);
#endif // TUNSAFE_CRYPTO_OPS_H_