Skip to content

Commit

Permalink
lib/sodium: add C++ wrappers for crypto_stream_chacha20 and crypto_on…
Browse files Browse the repository at this point in the history
…etimeauth_poly1305
  • Loading branch information
MaxKellermann committed Oct 21, 2023
1 parent 7d00543 commit d2c0a99
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/lib/sodium/OnetimeauthPoly1305.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: BSD-2-Clause
// Copyright CM4all GmbH
// author: Max Kellermann <[email protected]>

#pragma once

#include <sodium/crypto_onetimeauth_poly1305.h>

#include <cstddef>
#include <span>

static void
crypto_onetimeauth_poly1305(std::span<std::byte, crypto_onetimeauth_poly1305_BYTES> out,
std::span<const std::byte> in,
std::span<const std::byte, crypto_onetimeauth_poly1305_KEYBYTES> k) noexcept
{
crypto_onetimeauth_poly1305(reinterpret_cast<unsigned char *>(out.data()),
reinterpret_cast<const unsigned char *>(in.data()), in.size(),
reinterpret_cast<const unsigned char *>(k.data()));
}

[[nodiscard]] [[gnu::pure]]
static bool
crypto_onetimeauth_poly1305_verify(std::span<const std::byte, crypto_onetimeauth_poly1305_BYTES> h,
std::span<const std::byte> in,
std::span<const std::byte, crypto_onetimeauth_poly1305_KEYBYTES> k) noexcept
{
return crypto_onetimeauth_poly1305_verify(reinterpret_cast<const unsigned char *>(h.data()),
reinterpret_cast<const unsigned char *>(in.data()), in.size(),
reinterpret_cast<const unsigned char *>(k.data())) == 0;
}
34 changes: 34 additions & 0 deletions src/lib/sodium/StreamChaCha20.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: BSD-2-Clause
// Copyright CM4all GmbH
// author: Max Kellermann <[email protected]>

#pragma once

#include <sodium/crypto_stream_chacha20.h>

#include <cstddef>
#include <span>

static void
crypto_stream_chacha20_xor(std::byte *c, std::span<const std::byte> m,
std::span<const std::byte, crypto_stream_chacha20_NONCEBYTES> n,
std::span<const std::byte, crypto_stream_chacha20_KEYBYTES> k) noexcept
{
crypto_stream_chacha20_xor(reinterpret_cast<unsigned char *>(c),
reinterpret_cast<const unsigned char *>(m.data()), m.size(),
reinterpret_cast<const unsigned char *>(n.data()),
reinterpret_cast<const unsigned char *>(k.data()));
}

static void
crypto_stream_chacha20_xor_ic(std::byte *c, std::span<const std::byte> m,
std::span<const std::byte, crypto_stream_chacha20_NONCEBYTES> n,
uint64_t ic,
std::span<const std::byte, crypto_stream_chacha20_KEYBYTES> k) noexcept
{
crypto_stream_chacha20_xor_ic(reinterpret_cast<unsigned char *>(c),
reinterpret_cast<const unsigned char *>(m.data()), m.size(),
reinterpret_cast<const unsigned char *>(n.data()),
ic,
reinterpret_cast<const unsigned char *>(k.data()));
}

0 comments on commit d2c0a99

Please sign in to comment.