From d2c0a99bf5bdcaa9e29e68b403200abb564fc2fe Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 21 Oct 2023 13:43:42 +0200 Subject: [PATCH] lib/sodium: add C++ wrappers for crypto_stream_chacha20 and crypto_onetimeauth_poly1305 --- src/lib/sodium/OnetimeauthPoly1305.hxx | 31 +++++++++++++++++++++++ src/lib/sodium/StreamChaCha20.hxx | 34 ++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/lib/sodium/OnetimeauthPoly1305.hxx create mode 100644 src/lib/sodium/StreamChaCha20.hxx diff --git a/src/lib/sodium/OnetimeauthPoly1305.hxx b/src/lib/sodium/OnetimeauthPoly1305.hxx new file mode 100644 index 000000000..4de6637e7 --- /dev/null +++ b/src/lib/sodium/OnetimeauthPoly1305.hxx @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: BSD-2-Clause +// Copyright CM4all GmbH +// author: Max Kellermann + +#pragma once + +#include + +#include +#include + +static void +crypto_onetimeauth_poly1305(std::span out, + std::span in, + std::span k) noexcept +{ + crypto_onetimeauth_poly1305(reinterpret_cast(out.data()), + reinterpret_cast(in.data()), in.size(), + reinterpret_cast(k.data())); +} + +[[nodiscard]] [[gnu::pure]] +static bool +crypto_onetimeauth_poly1305_verify(std::span h, + std::span in, + std::span k) noexcept +{ + return crypto_onetimeauth_poly1305_verify(reinterpret_cast(h.data()), + reinterpret_cast(in.data()), in.size(), + reinterpret_cast(k.data())) == 0; +} diff --git a/src/lib/sodium/StreamChaCha20.hxx b/src/lib/sodium/StreamChaCha20.hxx new file mode 100644 index 000000000..7e360371a --- /dev/null +++ b/src/lib/sodium/StreamChaCha20.hxx @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: BSD-2-Clause +// Copyright CM4all GmbH +// author: Max Kellermann + +#pragma once + +#include + +#include +#include + +static void +crypto_stream_chacha20_xor(std::byte *c, std::span m, + std::span n, + std::span k) noexcept +{ + crypto_stream_chacha20_xor(reinterpret_cast(c), + reinterpret_cast(m.data()), m.size(), + reinterpret_cast(n.data()), + reinterpret_cast(k.data())); +} + +static void +crypto_stream_chacha20_xor_ic(std::byte *c, std::span m, + std::span n, + uint64_t ic, + std::span k) noexcept +{ + crypto_stream_chacha20_xor_ic(reinterpret_cast(c), + reinterpret_cast(m.data()), m.size(), + reinterpret_cast(n.data()), + ic, + reinterpret_cast(k.data())); +}