8#include <botan/internal/commoncrypto.h>
10#include <unordered_map>
12#include <CommonCrypto/CommonCrypto.h>
19class 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*);
34 if(m_info.init(&m_ctx) != 1)
35 throw CommonCrypto_Error(
"CC_" + m_info.name +
"_Init");
38 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
43 return std::make_unique<CommonCrypto_HashFunction>(m_info);
46 std::unique_ptr<HashFunction> copy_state()
const override
48 return std::unique_ptr<CommonCrypto_HashFunction>(
49 new CommonCrypto_HashFunction(m_info, m_ctx));
52 size_t output_length()
const override
54 return m_info.digestLength;
57 size_t hash_block_size()
const override
59 return m_info.blockSize;
62 CommonCrypto_HashFunction(
const digest_config_t& info) :
68 CommonCrypto_HashFunction(
const digest_config_t& info,
const CTX &ctx) :
69 m_ctx(ctx), m_info(info) {}
72 void add_data(
const uint8_t input[],
size_t length)
override
77 CC_LONG update_len = (length > 0xFFFFFFFFUL) ? 0xFFFFFFFFUL :
static_cast<CC_LONG
>(length);
78 m_info.update(&m_ctx, input, update_len);
84 void final_result(uint8_t output[])
override
86 if(m_info.final(output, &m_ctx) != 1)
87 throw CommonCrypto_Error(
"CC_" + m_info.name +
"_Final");
92 digest_config_t m_info;
96std::unique_ptr<HashFunction>
99#define MAKE_COMMONCRYPTO_HASH_3(name, hash, ctx) \
100 std::unique_ptr<HashFunction>( \
101 new CommonCrypto_HashFunction<CC_ ## ctx ## _CTX >({ \
103 CC_ ## hash ## _DIGEST_LENGTH, \
104 CC_ ## hash ## _BLOCK_BYTES, \
105 CC_ ## hash ## _Init, \
106 CC_ ## hash ## _Update, \
107 CC_ ## hash ## _Final \
110#define MAKE_COMMONCRYPTO_HASH_2(name, id) \
111 MAKE_COMMONCRYPTO_HASH_3(name, id, id)
113#define MAKE_COMMONCRYPTO_HASH_1(id) \
114 MAKE_COMMONCRYPTO_HASH_2(#id, id)
116#if defined(BOTAN_HAS_SHA2_32)
117 if(
name ==
"SHA-224")
119 if(
name ==
"SHA-256")
122#if defined(BOTAN_HAS_SHA2_64)
123 if(
name ==
"SHA-384")
125 if(
name ==
"SHA-512")
129#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)