Skip to content

Commit

Permalink
Split sha512 and ed25519 into separate header files
Browse files Browse the repository at this point in the history
  • Loading branch information
snej committed May 28, 2024
1 parent 5c593f8 commit 20751be
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 22 deletions.
22 changes: 0 additions & 22 deletions include/monocypher/ext/ed25519.hh
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,4 @@ namespace monocypher {
using key_pair = monocypher::key_pair<Ed25519>;
};


/// SHA-512 algorithm, for use as the template parameter to `hash`.
struct SHA512 {
static constexpr const char* name = "SHA-512";
static constexpr size_t hash_size = 512 / 8;

using context = c::crypto_sha512_ctx;
static constexpr auto create_fn = c::crypto_sha512;
static constexpr auto init_fn = c::crypto_sha512_init;
static constexpr auto update_fn = c::crypto_sha512_update;
static constexpr auto final_fn = c::crypto_sha512_final;

struct mac {
using context = c::crypto_sha512_hmac_ctx;
static constexpr auto create_fn = c::crypto_sha512_hmac;
static constexpr auto init_fn = c::crypto_sha512_hmac_init;
static constexpr auto update_fn = c::crypto_sha512_hmac_update;
static constexpr auto final_fn = c::crypto_sha512_hmac_final;
};
};

using sha512 = hash<SHA512>;
}
64 changes: 64 additions & 0 deletions include/monocypher/ext/sha512.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// monocypher/ext/sha512.hh
//
// Monocypher-Cpp: Unofficial idiomatic C++17 wrapper for Monocypher
// <https://monocypher.org>
//
// Copyright (c) 2022 Jens Alfke. All rights reserved.
//
// --- Standard 2-clause BSD licence follows ---
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#pragma once
#include "../hash.hh"
#include "../../../vendor/monocypher/src/optional/monocypher-ed25519.h"

namespace monocypher {

// This functionality is an extension that comes with Monocypher.
// It's not considered part of the core API, but is provided for compatibility.

/// SHA-512 algorithm, for use as the template parameter to `hash`.
struct SHA512 {
static constexpr const char* name = "SHA-512";
static constexpr size_t hash_size = 512 / 8;

using context = c::crypto_sha512_ctx;
static constexpr auto create_fn = c::crypto_sha512;
static constexpr auto init_fn = c::crypto_sha512_init;
static constexpr auto update_fn = c::crypto_sha512_update;
static constexpr auto final_fn = c::crypto_sha512_final;

struct mac {
using context = c::crypto_sha512_hmac_ctx;
static constexpr auto create_fn = c::crypto_sha512_hmac;
static constexpr auto init_fn = c::crypto_sha512_hmac_init;
static constexpr auto update_fn = c::crypto_sha512_hmac_update;
static constexpr auto final_fn = c::crypto_sha512_hmac_final;
};
};

using sha512 = hash<SHA512>;
}
1 change: 1 addition & 0 deletions tests/MonocypherCppTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Monocypher.hh"
#include "monocypher/ext/ed25519.hh"
#include "monocypher/ext/sha256.hh"
#include "monocypher/ext/sha512.hh"
#include "monocypher/ext/xsalsa20.hh"
#include <iostream>
#include <tuple> // for `tie`
Expand Down

0 comments on commit 20751be

Please sign in to comment.