Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions include/sframe/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ class SFrameError
const char* message_ = nullptr;
};

// Helper to convert SFrameError to appropriate exception type
void
throw_on_error(const SFrameError& error);

template<typename T>
class Result
{
Expand Down Expand Up @@ -141,18 +137,6 @@ class Result<void>

} // namespace SFRAME_NAMESPACE

// Unwrap a Result<T>, throwing the corresponding exception on error.
// Use in functions that have NOT yet been migrated away from exceptions.
// Usage: const auto val = SFRAME_VALUE_OR_THROW(some_result_expr);
#define SFRAME_VALUE_OR_THROW(expr) \
([&]() { \
auto _result = (expr); \
if (_result.is_err()) { \
SFRAME_NAMESPACE::throw_on_error(_result.error()); \
} \
return _result.value(); \
}())

// Unwrap a Result<T> into `var`, propagating the error by early return.
// Use in functions that already return Result<U>.
// Usage: SFRAME_VALUE_OR_RETURN(val, some_result_expr);
Expand Down
116 changes: 42 additions & 74 deletions include/sframe/sframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,6 @@

namespace SFRAME_NAMESPACE {

struct crypto_error : std::runtime_error
{
crypto_error();
};

struct unsupported_ciphersuite_error : std::runtime_error
{
unsupported_ciphersuite_error();
};

struct authentication_error : std::runtime_error
{
authentication_error();
};

struct buffer_too_small_error : std::runtime_error
{
using parent = std::runtime_error;
using parent::parent;
};

struct invalid_parameter_error : std::runtime_error
{
using parent = std::runtime_error;
using parent::parent;
};

struct invalid_key_usage_error : std::runtime_error
{
using parent = std::runtime_error;
using parent::parent;
};

enum class CipherSuite : uint16_t
{
AES_128_CTR_HMAC_SHA256_80 = 1,
Expand Down Expand Up @@ -111,15 +78,15 @@ class Context
Context(CipherSuite suite);
virtual ~Context();

void add_key(KeyID kid, KeyUsage usage, input_bytes key);
Result<void> add_key(KeyID kid, KeyUsage usage, input_bytes key);

output_bytes protect(KeyID key_id,
output_bytes ciphertext,
input_bytes plaintext,
input_bytes metadata);
output_bytes unprotect(output_bytes plaintext,
input_bytes ciphertext,
input_bytes metadata);
Result<output_bytes> protect(KeyID key_id,
output_bytes ciphertext,
input_bytes plaintext,
input_bytes metadata);
Result<output_bytes> unprotect(output_bytes plaintext,
input_bytes ciphertext,
input_bytes metadata);

static constexpr size_t max_overhead = 17 + 16;
static constexpr size_t max_metadata_size = 512;
Expand Down Expand Up @@ -150,54 +117,55 @@ class MLSContext : protected Context

MLSContext(CipherSuite suite_in, size_t epoch_bits_in);

void add_epoch(EpochID epoch_id, input_bytes sframe_epoch_secret);
void add_epoch(EpochID epoch_id,
input_bytes sframe_epoch_secret,
size_t sender_bits);
Result<void> add_epoch(EpochID epoch_id, input_bytes sframe_epoch_secret);
Result<void> add_epoch(EpochID epoch_id,
input_bytes sframe_epoch_secret,
size_t sender_bits);
void purge_before(EpochID keeper);

output_bytes protect(EpochID epoch_id,
SenderID sender_id,
output_bytes ciphertext,
input_bytes plaintext,
input_bytes metadata);
output_bytes protect(EpochID epoch_id,
SenderID sender_id,
ContextID context_id,
output_bytes ciphertext,
input_bytes plaintext,
input_bytes metadata);

output_bytes unprotect(output_bytes plaintext,
input_bytes ciphertext,
input_bytes metadata);
Result<output_bytes> protect(EpochID epoch_id,
SenderID sender_id,
output_bytes ciphertext,
input_bytes plaintext,
input_bytes metadata);
Result<output_bytes> protect(EpochID epoch_id,
SenderID sender_id,
ContextID context_id,
output_bytes ciphertext,
input_bytes plaintext,
input_bytes metadata);

Result<output_bytes> unprotect(output_bytes plaintext,
input_bytes ciphertext,
input_bytes metadata);

private:
struct EpochKeys
{
static constexpr size_t max_secret_size = 64;

EpochID full_epoch;
EpochID full_epoch = 0;
owned_bytes<max_secret_size> sframe_epoch_secret;
size_t sender_bits;
size_t context_bits;
uint64_t max_sender_id;
uint64_t max_context_id;

EpochKeys(EpochID full_epoch_in,
input_bytes sframe_epoch_secret_in,
size_t epoch_bits,
size_t sender_bits_in);
size_t sender_bits = 0;
size_t context_bits = 0;
uint64_t max_sender_id = 0;
uint64_t max_context_id = 0;

EpochKeys() = default;
static Result<EpochKeys> create(EpochID full_epoch_in,
input_bytes sframe_epoch_secret_in,
size_t epoch_bits,
size_t sender_bits_in);
Result<owned_bytes<max_secret_size>> base_key(CipherSuite suite,
SenderID sender_id) const;
};

void purge_epoch(EpochID epoch_id);

KeyID form_key_id(EpochID epoch_id,
SenderID sender_id,
ContextID context_id) const;
void ensure_key(KeyID key_id, KeyUsage usage);
Result<KeyID> form_key_id(EpochID epoch_id,
SenderID sender_id,
ContextID context_id) const;
Result<void> ensure_key(KeyID key_id, KeyUsage usage);

const size_t epoch_bits;
const size_t epoch_mask;
Expand Down
5 changes: 0 additions & 5 deletions src/crypto_boringssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ namespace SFRAME_NAMESPACE {
/// Convert between native identifiers / errors and OpenSSL ones
///

crypto_error::crypto_error()
: std::runtime_error(ERR_error_string(ERR_get_error(), nullptr))
{
}

static Result<const EVP_MD*>
openssl_digest_type(CipherSuite suite)
{
Expand Down
5 changes: 0 additions & 5 deletions src/crypto_openssl11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ using scoped_hmac_ctx = std::unique_ptr<HMAC_CTX, decltype(&HMAC_CTX_free)>;
/// Convert between native identifiers / errors and OpenSSL ones
///

crypto_error::crypto_error()
: std::runtime_error(ERR_error_string(ERR_get_error(), nullptr))
{
}

static Result<const EVP_MD*>
openssl_digest_type(CipherSuite suite)
{
Expand Down
5 changes: 0 additions & 5 deletions src/crypto_openssl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ namespace SFRAME_NAMESPACE {
/// Convert between native identifiers / errors and OpenSSL ones
///

crypto_error::crypto_error()
: std::runtime_error(ERR_error_string(ERR_get_error(), nullptr))
{
}

static Result<const EVP_CIPHER*>
openssl_cipher(CipherSuite suite)
{
Expand Down
26 changes: 0 additions & 26 deletions src/result.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1 @@
#include <sframe/result.h>
#include <sframe/sframe.h>

namespace SFRAME_NAMESPACE {

void
throw_on_error(const SFrameError& error)
{
switch (error.type()) {
case SFrameErrorType::buffer_too_small_error:
throw buffer_too_small_error(error.message());
case SFrameErrorType::invalid_parameter_error:
throw invalid_parameter_error(error.message());
case SFrameErrorType::crypto_error:
throw crypto_error();
case SFrameErrorType::unsupported_ciphersuite_error:
throw unsupported_ciphersuite_error();
case SFrameErrorType::authentication_error:
throw authentication_error();
case SFrameErrorType::invalid_key_usage_error:
throw invalid_key_usage_error(error.message());
default:
throw std::runtime_error(error.message());
}
}

} // namespace SFRAME_NAMESPACE
Loading
Loading