Coverage Report

Created: 2025-08-28 06:21

/src/Botan-3.4.0/src/lib/ffi/ffi_hash.cpp
Line
Count
Source (jump to first uncovered line)
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
12
extern "C" {
13
14
using namespace Botan_FFI;
15
16
BOTAN_FFI_DECLARE_STRUCT(botan_hash_struct, Botan::HashFunction, 0x1F0A4F84);
17
18
0
int botan_hash_init(botan_hash_t* hash, const char* hash_name, uint32_t flags) {
19
0
   return ffi_guard_thunk(__func__, [=]() -> int {
20
0
      if(hash == nullptr || hash_name == nullptr || *hash_name == 0) {
21
0
         return BOTAN_FFI_ERROR_NULL_POINTER;
22
0
      }
23
0
      if(flags != 0) {
24
0
         return BOTAN_FFI_ERROR_BAD_FLAG;
25
0
      }
26
27
0
      auto h = Botan::HashFunction::create(hash_name);
28
0
      if(h == nullptr) {
29
0
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
30
0
      }
31
32
0
      *hash = new botan_hash_struct(std::move(h));
33
0
      return BOTAN_FFI_SUCCESS;
34
0
   });
35
0
}
36
37
0
int botan_hash_destroy(botan_hash_t hash) {
38
0
   return BOTAN_FFI_CHECKED_DELETE(hash);
39
0
}
40
41
0
int botan_hash_output_length(botan_hash_t hash, size_t* out) {
42
0
   if(out == nullptr) {
43
0
      return BOTAN_FFI_ERROR_NULL_POINTER;
44
0
   }
45
0
   return BOTAN_FFI_VISIT(hash, [=](const auto& h) { *out = h.output_length(); });
46
0
}
47
48
0
int botan_hash_block_size(botan_hash_t hash, size_t* out) {
49
0
   if(out == nullptr) {
50
0
      return BOTAN_FFI_ERROR_NULL_POINTER;
51
0
   }
52
0
   return BOTAN_FFI_VISIT(hash, [=](const auto& h) { *out = h.hash_block_size(); });
53
0
}
54
55
0
int botan_hash_clear(botan_hash_t hash) {
56
0
   return BOTAN_FFI_VISIT(hash, [](auto& h) { h.clear(); });
57
0
}
58
59
0
int botan_hash_update(botan_hash_t hash, const uint8_t* buf, size_t len) {
60
0
   if(len == 0) {
61
0
      return 0;
62
0
   }
63
64
0
   if(buf == nullptr) {
65
0
      return BOTAN_FFI_ERROR_NULL_POINTER;
66
0
   }
67
68
0
   return BOTAN_FFI_VISIT(hash, [=](auto& h) { h.update(buf, len); });
69
0
}
70
71
0
int botan_hash_final(botan_hash_t hash, uint8_t out[]) {
72
0
   if(out == nullptr) {
73
0
      return BOTAN_FFI_ERROR_NULL_POINTER;
74
0
   }
75
0
   return BOTAN_FFI_VISIT(hash, [=](auto& h) { h.final(out); });
76
0
}
77
78
0
int botan_hash_copy_state(botan_hash_t* dest, const botan_hash_t source) {
79
0
   return BOTAN_FFI_VISIT(source, [=](const auto& src) { *dest = new botan_hash_struct(src.copy_state()); });
80
0
}
81
82
0
int botan_hash_name(botan_hash_t hash, char* name, size_t* name_len) {
83
0
   if(name_len == nullptr) {
84
0
      return BOTAN_FFI_ERROR_NULL_POINTER;
85
0
   }
86
87
0
   return BOTAN_FFI_VISIT(hash, [=](const auto& h) { return write_str_output(name, name_len, h.name()); });
88
0
}
89
}