Botan  2.6.0
Crypto and TLS for C++11
ffi_hash.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2015,2017 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6 
7 #include <botan/ffi.h>
8 #include <botan/internal/ffi_util.h>
9 #include <botan/hash.h>
10 
11 extern "C" {
12 
13 using namespace Botan_FFI;
14 
15 BOTAN_FFI_DECLARE_STRUCT(botan_hash_struct, Botan::HashFunction, 0x1F0A4F84);
16 
17 int botan_hash_init(botan_hash_t* hash, const char* hash_name, uint32_t flags)
18  {
19  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
20  if(hash == nullptr || hash_name == nullptr || *hash_name == 0)
22  if(flags != 0)
24 
25  std::unique_ptr<Botan::HashFunction> h = Botan::HashFunction::create(hash_name);
26  if(h == nullptr)
28 
29  *hash = new botan_hash_struct(h.release());
30  return BOTAN_FFI_SUCCESS;
31  });
32  }
33 
35  {
37  }
38 
40  {
41  return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { *out = h.output_length(); });
42  }
43 
45  {
46  return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { *out = h.hash_block_size(); });
47  }
48 
50  {
51  return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { h.clear(); });
52  }
53 
54 int botan_hash_update(botan_hash_t hash, const uint8_t* buf, size_t len)
55  {
56  return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { h.update(buf, len); });
57  }
58 
59 int botan_hash_final(botan_hash_t hash, uint8_t out[])
60  {
61  return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { h.final(out); });
62  }
63 
65  {
66  return BOTAN_FFI_DO(Botan::HashFunction, source, src, {
67  *dest = new botan_hash_struct(src.copy_state().release()); });
68  }
69 
70 }
int botan_hash_copy_state(botan_hash_t *dest, const botan_hash_t source)
Definition: ffi_hash.cpp:64
int botan_hash_update(botan_hash_t hash, const uint8_t *buf, size_t len)
Definition: ffi_hash.cpp:54
int ffi_guard_thunk(const char *func_name, Thunk thunk)
Definition: ffi_util.h:64
Flags flags(Flag flags)
Definition: p11.h:858
BOTAN_FFI_DECLARE_STRUCT(botan_hash_struct, Botan::HashFunction, 0x1F0A4F84)
int botan_hash_final(botan_hash_t hash, uint8_t out[])
Definition: ffi_hash.cpp:59
struct botan_hash_struct * botan_hash_t
Definition: ffi.h:226
int botan_hash_block_size(botan_hash_t hash, size_t *out)
Definition: ffi_hash.cpp:44
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:106
int botan_hash_output_length(botan_hash_t hash, size_t *out)
Definition: ffi_hash.cpp:39
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:126
int botan_hash_init(botan_hash_t *hash, const char *hash_name, uint32_t flags)
Definition: ffi_hash.cpp:17
#define BOTAN_CURRENT_FUNCTION
Definition: compiler.h:143
int botan_hash_destroy(botan_hash_t hash)
Definition: ffi_hash.cpp:34
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98
MechanismType hash
int botan_hash_clear(botan_hash_t hash)
Definition: ffi_hash.cpp:49