8#include <botan/internal/commoncrypto.h>
10#include <botan/hash.h>
11#include <botan/internal/stl_util.h>
12#include <unordered_map>
14#include <CommonCrypto/CommonCrypto.h>
21class CommonCrypto_HashFunction
final :
public HashFunction {
23 struct digest_config_t {
28 int (*update)(CTX*,
const void*, CC_LONG len);
29 int (*
final)(
unsigned char*, CTX*);
32 void clear()
override {
33 if(m_info.init(&m_ctx) != 1)
34 throw CommonCrypto_Error(
"CC_" + m_info.name +
"_Init");
37 std::string provider()
const override {
return "commoncrypto"; }
39 std::string
name()
const override {
return m_info.name; }
41 std::unique_ptr<HashFunction> new_object()
const override {
42 return std::make_unique<CommonCrypto_HashFunction>(m_info);
45 std::unique_ptr<HashFunction> copy_state()
const override {
46 return std::unique_ptr<CommonCrypto_HashFunction>(
new CommonCrypto_HashFunction(m_info, m_ctx));
49 size_t output_length()
const override {
return m_info.digestLength; }
51 size_t hash_block_size()
const override {
return m_info.blockSize; }
53 CommonCrypto_HashFunction(
const digest_config_t& info) : m_info(info) { clear(); }
55 CommonCrypto_HashFunction(
const digest_config_t& info,
const CTX& ctx) : m_ctx(ctx), m_info(info) {}
58 void add_data(std::span<const uint8_t> input)
override {
59 BufferSlicer in(input);
63 CC_LONG update_len = (in.remaining() > 0xFFFFFFFFUL) ? 0xFFFFFFFFUL :
static_cast<CC_LONG
>(in.remaining());
64 const auto chunk = in.take(update_len);
65 m_info.update(&m_ctx, chunk.data(),
static_cast<CC_LONG
>(chunk.size()));
69 void final_result(std::span<uint8_t> output)
override {
70 if(m_info.final(output.data(), &m_ctx) != 1)
71 throw CommonCrypto_Error(
"CC_" + m_info.name +
"_Final");
76 digest_config_t m_info;
81#define MAKE_COMMONCRYPTO_HASH_3(name, hash, ctx) \
82 std::unique_ptr<HashFunction>(new CommonCrypto_HashFunction<CC_##ctx##_CTX>({std::string(name), \
83 CC_##hash##_DIGEST_LENGTH, \
84 CC_##hash##_BLOCK_BYTES, \
89#define MAKE_COMMONCRYPTO_HASH_2(name, id) MAKE_COMMONCRYPTO_HASH_3(name, id, id)
91#define MAKE_COMMONCRYPTO_HASH_1(id) MAKE_COMMONCRYPTO_HASH_2(#id, id)
93#if defined(BOTAN_HAS_SHA2_32)
99#if defined(BOTAN_HAS_SHA2_64)
100 if(
name ==
"SHA-384")
102 if(
name ==
"SHA-512")
106#if defined(BOTAN_HAS_SHA1)
#define MAKE_COMMONCRYPTO_HASH_2(name, id)
#define MAKE_COMMONCRYPTO_HASH_3(name, hash, ctx)
int(* final)(unsigned char *, CTX *)
std::unique_ptr< HashFunction > make_commoncrypto_hash(std::string_view name)