Botan 3.0.0
Crypto and TLS for C&
Functions
ffi_totp.cpp File Reference
#include <botan/ffi.h>
#include <botan/internal/ffi_util.h>

Go to the source code of this file.

Functions

int botan_totp_check (botan_totp_t totp, uint32_t totp_code, uint64_t timestamp, size_t acceptable_clock_drift)
 
int botan_totp_destroy (botan_totp_t totp)
 
int botan_totp_generate (botan_totp_t totp, uint32_t *totp_code, uint64_t timestamp)
 
int botan_totp_init (botan_totp_t *totp, const uint8_t key[], size_t key_len, const char *hash_algo, size_t digits, size_t time_step)
 

Function Documentation

◆ botan_totp_check()

int botan_totp_check ( botan_totp_t  totp,
uint32_t  totp_code,
uint64_t  timestamp,
size_t  acceptable_clock_drift 
)

Verify a TOTP code

Parameters
totpthe TOTP object
totp_codethe presented OTP
timestampthe current local timestamp
acceptable_clock_driftspecifies the acceptable amount of clock drift (in terms of time steps) between the two hosts.

Definition at line 76 of file ffi_totp.cpp.

80 {
81#if defined(BOTAN_HAS_TOTP)
82 return BOTAN_FFI_VISIT(totp, [=](auto& t) {
83 const bool ok = t.verify_totp(totp_code, timestamp, acceptable_clock_drift);
85 });
86
87#else
88 BOTAN_UNUSED(totp, totp_code, timestamp, acceptable_clock_drift);
90#endif
91 }
#define BOTAN_UNUSED(...)
Definition: assert.h:141
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:91
@ BOTAN_FFI_INVALID_VERIFIER
Definition: ffi.h:71
@ BOTAN_FFI_SUCCESS
Definition: ffi.h:70
#define BOTAN_FFI_VISIT(obj, lambda)
Definition: ffi_util.h:126

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_INVALID_VERIFIER, BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, and BOTAN_UNUSED.

◆ botan_totp_destroy()

int botan_totp_destroy ( botan_totp_t  totp)

Destroy a TOTP instance

Returns
0 if success, error if invalid object handle

Definition at line 48 of file ffi_totp.cpp.

49 {
50#if defined(BOTAN_HAS_TOTP)
51 return BOTAN_FFI_CHECKED_DELETE(totp);
52#else
53 BOTAN_UNUSED(totp);
55#endif
56 }
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:145

References BOTAN_FFI_CHECKED_DELETE, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, and BOTAN_UNUSED.

◆ botan_totp_generate()

int botan_totp_generate ( botan_totp_t  totp,
uint32_t *  totp_code,
uint64_t  timestamp 
)

Generate a TOTP code for the provided timestamp

Parameters
totpthe TOTP object
totp_codethe OTP code will be written here
timestampthe current local timestamp

Definition at line 58 of file ffi_totp.cpp.

61 {
62#if defined(BOTAN_HAS_TOTP)
63 if(totp == nullptr || totp_code == nullptr)
65
66 return BOTAN_FFI_VISIT(totp, [=](auto& t) {
67 *totp_code = t.generate_totp(timestamp);
68 });
69
70#else
71 BOTAN_UNUSED(totp, totp_code, timestamp);
73#endif
74 }
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:85

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_VISIT, and BOTAN_UNUSED.

◆ botan_totp_init()

int botan_totp_init ( botan_totp_t totp,
const uint8_t  key[],
size_t  key_len,
const char *  hash_algo,
size_t  digits,
size_t  time_step 
)

Initialize a TOTP instance

Definition at line 24 of file ffi_totp.cpp.

29 {
30 if(totp == nullptr || key == nullptr || hash_algo == nullptr)
32
33 *totp = nullptr;
34
35#if defined(BOTAN_HAS_TOTP)
36 return ffi_guard_thunk(__func__, [=]() -> int {
37 auto otp = std::make_unique<Botan::TOTP>(key, key_len, hash_algo, digits, time_step);
38 *totp = new botan_totp_struct(std::move(otp));
39
40 return BOTAN_FFI_SUCCESS;
41 });
42#else
43 BOTAN_UNUSED(totp, key, key_len, hash_algo, digits, time_step);
45#endif
46 }
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition: ffi.cpp:120

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_UNUSED, and Botan_FFI::ffi_guard_thunk().