Coverage Report

Created: 2025-07-18 06:20

/src/Botan-3.4.0/src/lib/ffi/ffi_totp.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_TOTP)
12
   #include <botan/otp.h>
13
#endif
14
15
extern "C" {
16
17
using namespace Botan_FFI;
18
19
#if defined(BOTAN_HAS_TOTP)
20
21
BOTAN_FFI_DECLARE_STRUCT(botan_totp_struct, Botan::TOTP, 0x3D9D2CD1);
22
23
#endif
24
25
int botan_totp_init(
26
0
   botan_totp_t* totp, const uint8_t key[], size_t key_len, const char* hash_algo, size_t digits, size_t time_step) {
27
0
   if(totp == nullptr || key == nullptr || hash_algo == nullptr) {
28
0
      return BOTAN_FFI_ERROR_NULL_POINTER;
29
0
   }
30
31
0
   *totp = nullptr;
32
33
#if defined(BOTAN_HAS_TOTP)
34
   return ffi_guard_thunk(__func__, [=]() -> int {
35
      auto otp = std::make_unique<Botan::TOTP>(key, key_len, hash_algo, digits, time_step);
36
      *totp = new botan_totp_struct(std::move(otp));
37
38
      return BOTAN_FFI_SUCCESS;
39
   });
40
#else
41
0
   BOTAN_UNUSED(totp, key, key_len, hash_algo, digits, time_step);
42
0
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
43
0
#endif
44
0
}
45
46
0
int botan_totp_destroy(botan_totp_t totp) {
47
#if defined(BOTAN_HAS_TOTP)
48
   return BOTAN_FFI_CHECKED_DELETE(totp);
49
#else
50
0
   BOTAN_UNUSED(totp);
51
0
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
52
0
#endif
53
0
}
54
55
0
int botan_totp_generate(botan_totp_t totp, uint32_t* totp_code, uint64_t timestamp) {
56
#if defined(BOTAN_HAS_TOTP)
57
   if(totp == nullptr || totp_code == nullptr) {
58
      return BOTAN_FFI_ERROR_NULL_POINTER;
59
   }
60
61
   return BOTAN_FFI_VISIT(totp, [=](auto& t) { *totp_code = t.generate_totp(timestamp); });
62
63
#else
64
0
   BOTAN_UNUSED(totp, totp_code, timestamp);
65
0
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
66
0
#endif
67
0
}
68
69
0
int botan_totp_check(botan_totp_t totp, uint32_t totp_code, uint64_t timestamp, size_t acceptable_clock_drift) {
70
#if defined(BOTAN_HAS_TOTP)
71
   return BOTAN_FFI_VISIT(totp, [=](auto& t) {
72
      const bool ok = t.verify_totp(totp_code, timestamp, acceptable_clock_drift);
73
      return (ok ? BOTAN_FFI_SUCCESS : BOTAN_FFI_INVALID_VERIFIER);
74
   });
75
76
#else
77
0
   BOTAN_UNUSED(totp, totp_code, timestamp, acceptable_clock_drift);
78
0
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
79
0
#endif
80
0
}
81
}