-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib/sodium: add C++ wrappers for crypto_stream_chacha20 and crypto_on…
…etimeauth_poly1305
- Loading branch information
1 parent
7d00543
commit d2c0a99
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} |