Skip to content

Commit

Permalink
lib/sodium/SecretBox: wrappers for crypto_secretbox_*()
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Nov 9, 2023
1 parent 1048d67 commit 842b90c
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/lib/sodium/SecretBox.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: BSD-2-Clause
// Copyright CM4all GmbH
// author: Max Kellermann <[email protected]>

#pragma once

#include <sodium/crypto_secretbox.h>

static inline void
crypto_secretbox_easy(std::byte *ciphertext,
std::span<const std::byte> message,
std::span<const std::byte, crypto_secretbox_NONCEBYTES> nonce,
std::span<const std::byte, crypto_secretbox_KEYBYTES> key) noexcept
{
crypto_secretbox_easy(reinterpret_cast<unsigned char *>(ciphertext),
reinterpret_cast<const unsigned char *>(message.data()),
message.size(),
reinterpret_cast<const unsigned char *>(nonce.data()),
reinterpret_cast<const unsigned char *>(key.data()));
}

static inline bool
crypto_secretbox_open_easy(std::byte *message,
std::span<const std::byte> ciphertext,
std::span<const std::byte, crypto_secretbox_NONCEBYTES> nonce,
std::span<const std::byte, crypto_secretbox_KEYBYTES> key) noexcept
{
return crypto_secretbox_open_easy(reinterpret_cast<unsigned char *>(message),
reinterpret_cast<const unsigned char *>(ciphertext.data()),
ciphertext.size(),
reinterpret_cast<const unsigned char *>(nonce.data()),
reinterpret_cast<const unsigned char *>(key.data())) == 0;
}

0 comments on commit 842b90c

Please sign in to comment.