2020-05-09 15:04:14 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2022-03-13 08:42:01 +03:00
|
|
|
* Copyright (c) 2021-2022, Brian Gianforcaro <bgianf@serenityos.org>
|
2020-05-09 15:04:14 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-09 15:04:14 +03:00
|
|
|
*/
|
|
|
|
|
2020-03-08 14:34:33 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
2020-05-16 13:00:04 +03:00
|
|
|
#if defined(KERNEL)
|
2023-02-24 21:10:59 +03:00
|
|
|
# include <Kernel/Library/StdLib.h>
|
2020-03-08 14:34:33 +03:00
|
|
|
#else
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ALWAYS_INLINE void fast_u32_copy(u32* dest, u32 const* src, size_t count)
|
2020-03-08 14:34:33 +03:00
|
|
|
{
|
2022-10-04 03:32:20 +03:00
|
|
|
#if ARCH(X86_64)
|
2020-03-08 14:34:33 +03:00
|
|
|
asm volatile(
|
|
|
|
"rep movsl\n"
|
2020-07-27 16:54:39 +03:00
|
|
|
: "+S"(src), "+D"(dest), "+c"(count)::"memory");
|
2021-05-31 18:25:40 +03:00
|
|
|
#else
|
|
|
|
__builtin_memcpy(dest, src, count * 4);
|
|
|
|
#endif
|
2020-03-08 14:34:33 +03:00
|
|
|
}
|
|
|
|
|
2020-04-30 12:43:25 +03:00
|
|
|
ALWAYS_INLINE void fast_u32_fill(u32* dest, u32 value, size_t count)
|
2020-03-08 14:34:33 +03:00
|
|
|
{
|
2022-10-04 03:32:20 +03:00
|
|
|
#if ARCH(X86_64)
|
2020-03-08 14:34:33 +03:00
|
|
|
asm volatile(
|
|
|
|
"rep stosl\n"
|
|
|
|
: "=D"(dest), "=c"(count)
|
|
|
|
: "D"(dest), "c"(count), "a"(value)
|
|
|
|
: "memory");
|
2021-05-31 18:25:40 +03:00
|
|
|
#else
|
|
|
|
for (auto* p = dest; p < (dest + count); ++p) {
|
|
|
|
*p = value;
|
|
|
|
}
|
|
|
|
#endif
|
2020-03-08 14:34:33 +03:00
|
|
|
}
|
2021-09-13 00:26:59 +03:00
|
|
|
|
|
|
|
namespace AK {
|
2022-03-13 08:42:01 +03:00
|
|
|
|
2021-09-13 00:26:59 +03:00
|
|
|
inline void secure_zero(void* ptr, size_t size)
|
|
|
|
{
|
|
|
|
__builtin_memset(ptr, 0, size);
|
|
|
|
// The memory barrier is here to avoid the compiler optimizing
|
|
|
|
// away the memset when we rely on it for wiping secrets.
|
|
|
|
asm volatile("" ::
|
|
|
|
: "memory");
|
|
|
|
}
|
2022-03-13 08:42:01 +03:00
|
|
|
|
|
|
|
// Naive implementation of a constant time buffer comparison function.
|
|
|
|
// The goal being to not use any conditional branching so calls are
|
|
|
|
// guarded against potential timing attacks.
|
|
|
|
//
|
|
|
|
// See OpenBSD's timingsafe_memcmp for more advanced implementations.
|
2022-04-01 20:58:27 +03:00
|
|
|
inline bool timing_safe_compare(void const* b1, void const* b2, size_t len)
|
2022-03-13 08:42:01 +03:00
|
|
|
{
|
2022-04-01 20:58:27 +03:00
|
|
|
auto* c1 = static_cast<char const*>(b1);
|
|
|
|
auto* c2 = static_cast<char const*>(b2);
|
2022-03-13 08:42:01 +03:00
|
|
|
|
|
|
|
u8 res = 0;
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
res |= c1[i] ^ c2[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: !res can potentially inject branches depending
|
|
|
|
// on which toolchain is being used for compilation. Ideally
|
|
|
|
// we would use a more advanced algorithm.
|
|
|
|
return !res;
|
|
|
|
}
|
|
|
|
|
2021-09-13 00:26:59 +03:00
|
|
|
}
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2021-09-13 00:26:59 +03:00
|
|
|
using AK::secure_zero;
|
2022-03-13 08:42:01 +03:00
|
|
|
using AK::timing_safe_compare;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|