2020-05-09 12:04:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2022-03-13 05:42:01 +00:00
|
|
|
* Copyright (c) 2021-2022, Brian Gianforcaro <bgianf@serenityos.org>
|
2020-05-09 12:04:14 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-09 12:04:14 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-08 11:34:33 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
2020-05-16 10:00:04 +00:00
|
|
|
#if defined(KERNEL)
|
|
|
|
# include <Kernel/StdLib.h>
|
2020-03-08 11:34:33 +00:00
|
|
|
#else
|
|
|
|
# include <stdlib.h>
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE void fast_u32_copy(u32* dest, u32 const* src, size_t count)
|
2020-03-08 11:34:33 +00:00
|
|
|
{
|
2022-01-25 21:41:26 +00:00
|
|
|
#if ARCH(I386) || ARCH(X86_64)
|
2020-03-08 11:34:33 +00:00
|
|
|
asm volatile(
|
|
|
|
"rep movsl\n"
|
2020-07-27 13:54:39 +00:00
|
|
|
: "+S"(src), "+D"(dest), "+c"(count)::"memory");
|
2021-05-31 15:25:40 +00:00
|
|
|
#else
|
|
|
|
__builtin_memcpy(dest, src, count * 4);
|
|
|
|
#endif
|
2020-03-08 11:34:33 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 09:43:25 +00:00
|
|
|
ALWAYS_INLINE void fast_u32_fill(u32* dest, u32 value, size_t count)
|
2020-03-08 11:34:33 +00:00
|
|
|
{
|
2022-01-25 21:41:26 +00:00
|
|
|
#if ARCH(I386) || ARCH(X86_64)
|
2020-03-08 11:34:33 +00:00
|
|
|
asm volatile(
|
|
|
|
"rep stosl\n"
|
|
|
|
: "=D"(dest), "=c"(count)
|
|
|
|
: "D"(dest), "c"(count), "a"(value)
|
|
|
|
: "memory");
|
2021-05-31 15:25:40 +00:00
|
|
|
#else
|
|
|
|
for (auto* p = dest; p < (dest + count); ++p) {
|
|
|
|
*p = value;
|
|
|
|
}
|
|
|
|
#endif
|
2020-03-08 11:34:33 +00:00
|
|
|
}
|
2021-09-12 21:26:59 +00:00
|
|
|
|
|
|
|
namespace AK {
|
2022-03-13 05:42:01 +00:00
|
|
|
|
2021-09-12 21:26:59 +00: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 05:42:01 +00: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 17:58:27 +00:00
|
|
|
inline bool timing_safe_compare(void const* b1, void const* b2, size_t len)
|
2022-03-13 05:42:01 +00:00
|
|
|
{
|
2022-04-01 17:58:27 +00:00
|
|
|
auto* c1 = static_cast<char const*>(b1);
|
|
|
|
auto* c2 = static_cast<char const*>(b2);
|
2022-03-13 05:42:01 +00: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-12 21:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using AK::secure_zero;
|
2022-03-13 05:42:01 +00:00
|
|
|
using AK::timing_safe_compare;
|