Botan 2.19.5 Crypto and TLS for C&

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_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_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

  hash | hash object
  block_size | output buffer to hold the hash function output length

Returns
  0 on success, a negative value on failure

Definition at line 46 of file ffi_hash.cpp.

47 {
48 if(out == nullptr)
49 return BOTAN_FFI_ERROR_NULL_POINTER;
50 return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { *out = h.hash_block_size(); });
51 }
Botan::HashFunction
Definition: hash.h:21
BOTAN_FFI_ERROR_NULL_POINTER
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:77
BOTAN_FFI_DO
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:92
hash
MechanismType hash
Definition: p11_mechanism.cpp:64

References BOTAN_FFI_DO, BOTAN_FFI_ERROR_NULL_POINTER, and 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

  hash | hash object

Returns
  0 on success, a negative value on failure

Definition at line 53 of file ffi_hash.cpp.

54 {
55 return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { h.clear(); });
56 }

References BOTAN_FFI_DO, and 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

  dest | destination hash object
  source | source hash object

Returns
  0 on success, a negative value on failure

Definition at line 76 of file ffi_hash.cpp.

77 {
78 return BOTAN_FFI_DO(Botan::HashFunction, source, src, {
79 *dest = new botan_hash_struct(src.copy_state().release()); });
80 }

References BOTAN_FFI_DO.

## ◆ botan_hash_destroy()

int botan_hash_destroy | ( | botan_hash_t | hash | ) |

Frees all resources of the hash object

Parameters

  hash | hash object

Returns
  0 if success, error if invalid object handle

Definition at line 34 of file ffi_hash.cpp.

35 {
36 return BOTAN_FFI_CHECKED_DELETE(hash);
37 }
BOTAN_FFI_CHECKED_DELETE
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:129

References BOTAN_FFI_CHECKED_DELETE, and 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

  hash | hash object
  out | output buffer

Returns
  0 on success, a negative value on failure

Definition at line 69 of file ffi_hash.cpp.

70 {
71 if(out == nullptr)
72 return BOTAN_FFI_ERROR_NULL_POINTER;
73 return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { h.final(out); });
74 }

References BOTAN_FFI_DO, BOTAN_FFI_ERROR_NULL_POINTER, and 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

  hash | hash object
  hash_name | name of the hash function, e.g., "SHA-384"
  flags | should be 0 in current API revision, all other uses are reserved and return BOTAN_FFI_ERROR_BAD_FLAG

Definition at line 17 of file ffi_hash.cpp.

18 {
19 return ffi_guard_thunk(__func__, [=]() -> int {
20 if(hash == nullptr || hash_name == nullptr || *hash_name == 0)
21 return BOTAN_FFI_ERROR_NULL_POINTER;
22 if(flags != 0)
23 return BOTAN_FFI_ERROR_BAD_FLAG;
24
25 std::unique_ptr<Botan::HashFunction> h = Botan::HashFunction::create(hash_name);
26 if(h == nullptr)
27 return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
28
29 *hash = new botan_hash_struct(h.release());
30 return BOTAN_FFI_SUCCESS;
31 });
32 }
Botan::HashFunction::create
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:102
BOTAN_FFI_ERROR_NOT_IMPLEMENTED
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:83
BOTAN_FFI_ERROR_BAD_FLAG
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition: ffi.h:76
BOTAN_FFI_SUCCESS
@ BOTAN_FFI_SUCCESS
Definition: ffi.h:63
Botan::PKCS11::flags
Flags flags(Flag flags)
Definition: p11.h:860
Botan_FFI::ffi_guard_thunk
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:89

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(), Botan::PKCS11::flags(), and hash.

## ◆ 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

  hash | the object to read
  name | output buffer
  name_len | on input, the length of buffer, on success the number of bytes written

Definition at line 82 of file ffi_hash.cpp.

83 {
84 if(name_len == nullptr)
85 return BOTAN_FFI_ERROR_NULL_POINTER;
86
87 return BOTAN_FFI_DO(Botan::HashFunction, hash, h, {
88 return write_str_output(name, name_len, h.name()); });
89 }
name
std::string name
Definition: commoncrypto_hash.cpp:24
Botan_FFI::write_str_output
int write_str_output(uint8_t out[], size_t *out_len, const std::string &str)
Definition: ffi_util.h:160

References BOTAN_FFI_DO, BOTAN_FFI_ERROR_NULL_POINTER, hash, name, 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

  hash | hash object
  output_length | output 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.

40 {
41 if(out == nullptr)
42 return BOTAN_FFI_ERROR_NULL_POINTER;
43 return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { *out = h.output_length(); });
44 }

References BOTAN_FFI_DO, BOTAN_FFI_ERROR_NULL_POINTER, and 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

  hash | hash object
  in | input buffer
  in_len | number of bytes to read from the input buffer

Returns
  0 on success, a negative value on failure

Definition at line 58 of file ffi_hash.cpp.

59 {
60 if(len == 0)
61 return 0;
62
63 if(buf == nullptr)
64 return BOTAN_FFI_ERROR_NULL_POINTER;
65
66 return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { h.update(buf, len); });
67 }

References BOTAN_FFI_DO, BOTAN_FFI_ERROR_NULL_POINTER, and hash.