Botan  2.4.0
Crypto and TLS for C++11
Functions
ffi_hash.cpp File Reference
#include <botan/ffi.h>
#include <botan/internal/ffi_util.h>
#include <botan/hash.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_output_length (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
hashhash object
block_sizeoutput buffer to hold the hash function output length
Returns
0 on success, a negative value on failure

Definition at line 44 of file ffi_hash.cpp.

References BOTAN_FFI_DO, and Botan::HashFunction::hash_block_size().

45  {
46  return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { *out = h.hash_block_size(); });
47  }
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98
MechanismType hash

◆ 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
hashhash object
Returns
0 on success, a negative value on failure

Definition at line 49 of file ffi_hash.cpp.

References BOTAN_FFI_DO, and Botan::HashFunction::clear().

50  {
51  return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { h.clear(); });
52  }
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98
MechanismType hash

◆ botan_hash_copy_state()

int botan_hash_copy_state ( botan_hash_t dest,
const 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 64 of file ffi_hash.cpp.

References BOTAN_FFI_DO.

65  {
66  return BOTAN_FFI_DO(Botan::HashFunction, source, src, {
67  *dest = new botan_hash_struct(src.copy_state().release()); });
68  }
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98

◆ botan_hash_destroy()

int botan_hash_destroy ( botan_hash_t  hash)

Frees all resources of the hash object

Parameters
hashhash object
Returns
always returns 0

Definition at line 34 of file ffi_hash.cpp.

References BOTAN_FFI_CHECKED_DELETE.

35  {
37  }
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:126
MechanismType hash

◆ 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
hashhash object
outoutput buffer
Returns
0 on success, a negative value on failure

Definition at line 59 of file ffi_hash.cpp.

References BOTAN_FFI_DO, and Botan::Buffered_Computation::final().

60  {
61  return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { h.final(out); });
62  }
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98
MechanismType hash

◆ botan_hash_init()

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

Initialize a hash function object

Parameters
hashhash 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

TODO: since output_length is effectively required to use this API, return it from init as an output parameter

Definition at line 17 of file ffi_hash.cpp.

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

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  }
#define BOTAN_FFI_ERROR_BAD_FLAG
Definition: ffi.h:146
#define BOTAN_FFI_SUCCESS
Definition: ffi.h:137
int ffi_guard_thunk(const char *func_name, Thunk thunk)
Definition: ffi_util.h:64
Flags flags(Flag flags)
Definition: p11.h:858
#define BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:149
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:106
#define BOTAN_CURRENT_FUNCTION
Definition: compiler.h:143
#define BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:147
MechanismType hash

◆ 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
hashhash object
output_lengthoutput buffer to hold the hash function output length
Returns
0 on success, a negative value on failure

Definition at line 39 of file ffi_hash.cpp.

References BOTAN_FFI_DO, and Botan::Buffered_Computation::output_length().

40  {
41  return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { *out = h.output_length(); });
42  }
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98
MechanismType hash

◆ 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
hashhash 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 54 of file ffi_hash.cpp.

References BOTAN_FFI_DO, and Botan::Buffered_Computation::update().

55  {
56  return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { h.update(buf, len); });
57  }
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98
MechanismType hash