Botan 2.19.3
Crypto and TLS for C&
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
11extern "C" {
12
13using namespace Botan_FFI;
14
15BOTAN_FFI_DECLARE_STRUCT(botan_hash_struct, Botan::HashFunction, 0x1F0A4F84);
16
17int botan_hash_init(botan_hash_t* hash, const char* hash_name, uint32_t flags)
18 {
19 return ffi_guard_thunk(__func__, [=]() -> 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
38
40 {
41 if(out == nullptr)
43 return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { *out = h.output_length(); });
44 }
45
47 {
48 if(out == nullptr)
50 return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { *out = h.hash_block_size(); });
51 }
52
57
58int botan_hash_update(botan_hash_t hash, const uint8_t* buf, size_t len)
59 {
60 if(len == 0)
61 return 0;
62
63 if(buf == nullptr)
65
66 return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { h.update(buf, len); });
67 }
68
70 {
71 if(out == nullptr)
73 return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { h.final(out); });
74 }
75
77 {
78 return BOTAN_FFI_DO(Botan::HashFunction, source, src, {
79 *dest = new botan_hash_struct(src.copy_state().release()); });
80 }
81
82int botan_hash_name(botan_hash_t hash, char* name, size_t* name_len)
83 {
84 if(name_len == nullptr)
86
88 return write_str_output(name, name_len, h.name()); });
89 }
90
91}
void update(const uint8_t in[], size_t length)
Definition buf_comp.h:33
virtual size_t output_length() const =0
void final(uint8_t out[])
Definition buf_comp.h:83
virtual size_t hash_block_size() const
Definition hash.h:75
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition hash.cpp:102
virtual void clear()=0
std::string name
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:83
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:76
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:77
@ BOTAN_FFI_SUCCESS
Definition ffi.h:63
struct botan_hash_struct * botan_hash_t
Definition ffi.h:271
int botan_hash_output_length(botan_hash_t hash, size_t *out)
Definition ffi_hash.cpp:39
int botan_hash_name(botan_hash_t hash, char *name, size_t *name_len)
Definition ffi_hash.cpp:82
int botan_hash_block_size(botan_hash_t hash, size_t *out)
Definition ffi_hash.cpp:46
int botan_hash_destroy(botan_hash_t hash)
Definition ffi_hash.cpp:34
int botan_hash_clear(botan_hash_t hash)
Definition ffi_hash.cpp:53
int botan_hash_copy_state(botan_hash_t *dest, const botan_hash_t source)
Definition ffi_hash.cpp:76
int botan_hash_final(botan_hash_t hash, uint8_t out[])
Definition ffi_hash.cpp:69
int botan_hash_init(botan_hash_t *hash, const char *hash_name, uint32_t flags)
Definition ffi_hash.cpp:17
int botan_hash_update(botan_hash_t hash, const uint8_t *buf, size_t len)
Definition ffi_hash.cpp:58
#define BOTAN_FFI_DO(T, obj, param, block)
Definition ffi_util.h:92
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:129
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
Definition ffi_util.h:53
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition ffi.cpp:89
int write_str_output(uint8_t out[], size_t *out_len, const std::string &str)
Definition ffi_util.h:160
MechanismType hash