Coverage Report

Created: 2025-07-18 06:20

/src/Botan-3.4.0/src/lib/ffi/ffi_hotp.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* (C) 2018 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/internal/ffi_util.h>
10
11
#if defined(BOTAN_HAS_HOTP)
12
   #include <botan/otp.h>
13
#endif
14
15
extern "C" {
16
17
using namespace Botan_FFI;
18
19
#if defined(BOTAN_HAS_HOTP)
20
21
BOTAN_FFI_DECLARE_STRUCT(botan_hotp_struct, Botan::HOTP, 0x89CBF191);
22
23
#endif
24
25
0
int botan_hotp_init(botan_hotp_t* hotp, const uint8_t key[], size_t key_len, const char* hash_algo, size_t digits) {
26
0
   if(hotp == nullptr || key == nullptr || hash_algo == nullptr) {
27
0
      return BOTAN_FFI_ERROR_NULL_POINTER;
28
0
   }
29
30
0
   *hotp = nullptr;
31
32
#if defined(BOTAN_HAS_HOTP)
33
   return ffi_guard_thunk(__func__, [=]() -> int {
34
      auto otp = std::make_unique<Botan::HOTP>(key, key_len, hash_algo, digits);
35
      *hotp = new botan_hotp_struct(std::move(otp));
36
37
      return BOTAN_FFI_SUCCESS;
38
   });
39
#else
40
0
   BOTAN_UNUSED(hotp, key, key_len, hash_algo, digits);
41
0
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
42
0
#endif
43
0
}
44
45
0
int botan_hotp_destroy(botan_hotp_t hotp) {
46
#if defined(BOTAN_HAS_HOTP)
47
   return BOTAN_FFI_CHECKED_DELETE(hotp);
48
#else
49
0
   BOTAN_UNUSED(hotp);
50
0
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
51
0
#endif
52
0
}
53
54
0
int botan_hotp_generate(botan_hotp_t hotp, uint32_t* hotp_code, uint64_t hotp_counter) {
55
#if defined(BOTAN_HAS_HOTP)
56
   if(hotp == nullptr || hotp_code == nullptr) {
57
      return BOTAN_FFI_ERROR_NULL_POINTER;
58
   }
59
60
   return BOTAN_FFI_VISIT(hotp, [=](auto& h) { *hotp_code = h.generate_hotp(hotp_counter); });
61
62
#else
63
0
   BOTAN_UNUSED(hotp, hotp_code, hotp_counter);
64
0
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
65
0
#endif
66
0
}
67
68
int botan_hotp_check(
69
0
   botan_hotp_t hotp, uint64_t* next_hotp_counter, uint32_t hotp_code, uint64_t hotp_counter, size_t resync_range) {
70
#if defined(BOTAN_HAS_HOTP)
71
   return BOTAN_FFI_VISIT(hotp, [=](auto& h) {
72
      auto resp = h.verify_hotp(hotp_code, hotp_counter, resync_range);
73
74
      if(next_hotp_counter)
75
         *next_hotp_counter = resp.second;
76
77
      return (resp.first == true) ? BOTAN_FFI_SUCCESS : BOTAN_FFI_INVALID_VERIFIER;
78
   });
79
80
#else
81
0
   BOTAN_UNUSED(hotp, next_hotp_counter, hotp_code, hotp_counter, resync_range);
82
0
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
83
0
#endif
84
0
}
85
}