Botan 3.13.0
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
9#include <botan/hash.h>
10#include <botan/internal/ffi_util.h>
11
12extern "C" {
13
14using namespace Botan_FFI;
15
16BOTAN_FFI_DECLARE_STRUCT(botan_hash_struct, Botan::HashFunction, 0x1F0A4F84);
17
18int botan_hash_init(botan_hash_t* hash, const char* hash_name, uint32_t flags) {
19 return ffi_guard_thunk(__func__, [=]() -> int {
20 if(hash == nullptr || hash_name == nullptr || *hash_name == 0) {
22 }
23 if(flags != 0) {
25 }
26
27 auto h = Botan::HashFunction::create(hash_name);
28 if(h == nullptr) {
30 }
31
32 ffi_new_object(hash, std::move(h));
33 return BOTAN_FFI_SUCCESS;
34 });
35}
36
40
41int botan_hash_output_length(botan_hash_t hash, size_t* out) {
42 if(out == nullptr) {
44 }
45 return BOTAN_FFI_VISIT(hash, [=](const auto& h) { *out = h.output_length(); });
46}
47
48int botan_hash_block_size(botan_hash_t hash, size_t* out) {
49 if(out == nullptr) {
51 }
52 return BOTAN_FFI_VISIT(hash, [=](const auto& h) { *out = h.hash_block_size(); });
53}
54
56 if(out == nullptr) {
58 }
59 return BOTAN_FFI_VISIT(hash, [=](const auto& h) { *out = h.security_level(); });
60}
61
63 return BOTAN_FFI_VISIT(hash, [](auto& h) { h.clear(); });
64}
65
66int botan_hash_update(botan_hash_t hash, const uint8_t* buf, size_t len) {
67 if(len == 0) {
68 return 0;
69 }
70
71 if(buf == nullptr) {
73 }
74
75 return BOTAN_FFI_VISIT(hash, [=](auto& h) { h.update(buf, len); });
76}
77
78int botan_hash_final(botan_hash_t hash, uint8_t out[]) {
79 if(out == nullptr) {
81 }
82 return BOTAN_FFI_VISIT(hash, [=](auto& h) { h.final(out); });
83}
84
85// NOLINTNEXTLINE(misc-misplaced-const)
87 if(dest == nullptr) {
89 }
90 return BOTAN_FFI_VISIT(source, [=](const auto& src) { return ffi_new_object(dest, src.copy_state()); });
91}
92
93int botan_hash_name(botan_hash_t hash, char* name, size_t* name_len) {
94 if(name_len == nullptr) {
96 }
97
98 return BOTAN_FFI_VISIT(hash, [=](const auto& h) { return write_str_output(name, name_len, h.name()); });
99}
100}
static std::unique_ptr< HashFunction > create(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:111
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:130
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:131
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
struct botan_hash_struct * botan_hash_t
Definition ffi.h:488
int botan_hash_output_length(botan_hash_t hash, size_t *out)
Definition ffi_hash.cpp:41
int botan_hash_name(botan_hash_t hash, char *name, size_t *name_len)
Definition ffi_hash.cpp:93
int botan_hash_block_size(botan_hash_t hash, size_t *out)
Definition ffi_hash.cpp:48
int botan_hash_destroy(botan_hash_t hash)
Definition ffi_hash.cpp:37
int botan_hash_clear(botan_hash_t hash)
Definition ffi_hash.cpp:62
int botan_hash_copy_state(botan_hash_t *dest, const botan_hash_t source)
Definition ffi_hash.cpp:86
int botan_hash_final(botan_hash_t hash, uint8_t out[])
Definition ffi_hash.cpp:78
int botan_hash_init(botan_hash_t *hash, const char *hash_name, uint32_t flags)
Definition ffi_hash.cpp:18
int botan_hash_update(botan_hash_t hash, const uint8_t *buf, size_t len)
Definition ffi_hash.cpp:66
int botan_hash_security_level(botan_hash_t hash, size_t *out)
Definition ffi_hash.cpp:55
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:158
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:188
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
Definition ffi_util.h:61
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:178
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:95
int write_str_output(char out[], size_t *out_len, const std::string &str)
Definition ffi_util.h:271