Botan 3.13.0
Crypto and TLS for C&
ffi_hash.cpp File Reference
#include <botan/ffi.h>
#include <botan/hash.h>
#include <botan/internal/ffi_util.h>

Go to the source code of this file.

Functions

 BOTAN_FFI_DECLARE_STRUCT (botan_hash_struct, Botan::HashFunction, 0x1F0A4F84)
int botan_hash_block_size (botan_hash_t hash, size_t *out)
int botan_hash_clear (botan_hash_t hash)
int botan_hash_copy_state (botan_hash_t *dest, const botan_hash_t source)
int botan_hash_destroy (botan_hash_t hash)
int botan_hash_final (botan_hash_t hash, uint8_t out[])
int botan_hash_init (botan_hash_t *hash, const char *hash_name, uint32_t flags)
int botan_hash_name (botan_hash_t hash, char *name, size_t *name_len)
int botan_hash_output_length (botan_hash_t hash, size_t *out)
int botan_hash_security_level (botan_hash_t hash, size_t *out)
int botan_hash_update (botan_hash_t hash, const uint8_t *buf, size_t len)

Function Documentation

◆ BOTAN_FFI_DECLARE_STRUCT()

BOTAN_FFI_DECLARE_STRUCT ( botan_hash_struct ,
Botan::HashFunction ,
0x1F0A4F84  )

◆ botan_hash_block_size()

int botan_hash_block_size ( botan_hash_t hash,
size_t * block_size )

Writes the block size of the hash function to *block_size

Parameters
Hasheshash object
block_sizeoutput buffer to hold the hash function output length
Returns
0 on success, a negative value on failure

Definition at line 48 of file ffi_hash.cpp.

48 {
49 if(out == nullptr) {
51 }
52 return BOTAN_FFI_VISIT(hash, [=](const auto& h) { *out = h.hash_block_size(); });
53}
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:131
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:158

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.

◆ botan_hash_clear()

int botan_hash_clear ( botan_hash_t hash)

Reinitializes the state of the hash computation. A hash can be computed (with update/final) immediately.

Parameters
Hasheshash object
Returns
0 on success, a negative value on failure

Definition at line 62 of file ffi_hash.cpp.

62 {
63 return BOTAN_FFI_VISIT(hash, [](auto& h) { h.clear(); });
64}

References BOTAN_FFI_VISIT.

◆ botan_hash_copy_state()

int botan_hash_copy_state ( botan_hash_t * dest,
botan_hash_t source )

Copy the state of a hash function object

Parameters
destdestination hash object
sourcesource hash object
Returns
0 on success, a negative value on failure

Definition at line 86 of file ffi_hash.cpp.

86 {
87 if(dest == nullptr) {
89 }
90 return BOTAN_FFI_VISIT(source, [=](const auto& src) { return ffi_new_object(dest, src.copy_state()); });
91}
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:178

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_VISIT, and Botan_FFI::ffi_new_object().

◆ botan_hash_destroy()

int botan_hash_destroy ( botan_hash_t hash)

Frees all resources of the hash object

Parameters
Hasheshash object
Returns
0 if success, error if invalid object handle

Definition at line 37 of file ffi_hash.cpp.

37 {
38 return BOTAN_FFI_CHECKED_DELETE(hash);
39}
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:188

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_hash_final()

int botan_hash_final ( botan_hash_t hash,
uint8_t out[] )

Finalizes the hash computation and writes the output to out[0:botan_hash_output_length()] then reinitializes for computing another digest as if botan_hash_clear had been called.

Parameters
Hasheshash object
outoutput buffer
Returns
0 on success, a negative value on failure

Definition at line 78 of file ffi_hash.cpp.

78 {
79 if(out == nullptr) {
81 }
82 return BOTAN_FFI_VISIT(hash, [=](auto& h) { h.final(out); });
83}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.

◆ botan_hash_init()

int botan_hash_init ( botan_hash_t * hash,
const char * hash_name,
uint32_t flags )

Initialize a hash function object

Parameters
Hasheshash object
hash_namename of the hash function, e.g., "SHA-384"
flagsshould be 0 in current API revision, all other uses are reserved and return BOTAN_FFI_ERROR_BAD_FLAG

Definition at line 18 of file ffi_hash.cpp.

18 {
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}
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_SUCCESS
Definition ffi.h:114
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:95

References BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, Botan::HashFunction::create(), Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_hash_name()

int botan_hash_name ( botan_hash_t hash,
char * name,
size_t * name_len )

Get the name of this hash function

Parameters
Hashesthe object to read
nameoutput buffer
name_lenon input, the length of buffer, on success the number of bytes written

Definition at line 93 of file ffi_hash.cpp.

93 {
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}
int write_str_output(char out[], size_t *out_len, const std::string &str)
Definition ffi_util.h:271

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_VISIT, and Botan_FFI::write_str_output().

◆ botan_hash_output_length()

int botan_hash_output_length ( botan_hash_t hash,
size_t * output_length )

Writes the output length of the hash function to *output_length

Parameters
Hasheshash object
output_lengthoutput buffer to hold the hash function output length
Returns
0 on success, a negative value on failure

Definition at line 41 of file ffi_hash.cpp.

41 {
42 if(out == nullptr) {
44 }
45 return BOTAN_FFI_VISIT(hash, [=](const auto& h) { *out = h.output_length(); });
46}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.

◆ botan_hash_security_level()

int botan_hash_security_level ( botan_hash_t hash,
size_t * security_level )

Writes the estimated security level of the hash function, in bits, with respect to collision resistance, to *security_level. Returns zero for checksums and any hash where finding collisions is trivial.

Parameters
Hasheshash object
security_leveloutput variable to hold the security level
Returns
0 on success, a negative value on failure

Definition at line 55 of file ffi_hash.cpp.

55 {
56 if(out == nullptr) {
58 }
59 return BOTAN_FFI_VISIT(hash, [=](const auto& h) { *out = h.security_level(); });
60}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.

◆ botan_hash_update()

int botan_hash_update ( botan_hash_t hash,
const uint8_t * in,
size_t in_len )

Send more input to the hash function

Parameters
Hasheshash object
ininput buffer
in_lennumber of bytes to read from the input buffer
Returns
0 on success, a negative value on failure

Definition at line 66 of file ffi_hash.cpp.

66 {
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}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.